blob: 9fd4fd684fbf99ad83d7d3fedb4241cb0dc20638 [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#ifdef FEAT_MBYTE
228 int mi_cprefixlen; /* byte length of prefix in original
229 case */
230#else
231# define mi_cprefixlen mi_prefixlen /* it's the same value */
232#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000233
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000234 /* for when checking a compound word */
235 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000236 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
237 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000238 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000239
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000240 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000241 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000242 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200243 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000244
245 /* for NOBREAK */
246 int mi_result2; /* "mi_resul" without following word */
247 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000248} matchinf_T;
249
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000250
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100251static int spell_iswordp(char_u *p, win_T *wp);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000252#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100253static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000254#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000255
256/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000257 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000258 */
259typedef enum
260{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000261 STATE_START = 0, /* At start of node check for NUL bytes (goodword
262 * ends); if badword ends there is a match, otherwise
263 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000264 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000265 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000266 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
267 STATE_PLAIN, /* Use each byte of the node. */
268 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000269 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000270 STATE_INS, /* Insert a byte in the bad word. */
271 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000272 STATE_UNSWAP, /* Undo swap two characters. */
273 STATE_SWAP3, /* Swap two characters over three. */
274 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000275 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000276 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000277 STATE_REP_INI, /* Prepare for using REP items. */
278 STATE_REP, /* Use matching REP items from the .aff file. */
279 STATE_REP_UNDO, /* Undo a REP item replacement. */
280 STATE_FINAL /* End of this node. */
281} state_T;
282
283/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000284 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285 */
286typedef struct trystate_S
287{
Bram Moolenaarea424162005-06-16 21:51:00 +0000288 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000289 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000290 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000291 short ts_curi; /* index in list of child nodes */
292 char_u ts_fidx; /* index in fword[], case-folded bad word */
293 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
294 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000295 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000296 * PFD_PREFIXTREE or PFD_NOPREFIX */
297 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000298#ifdef FEAT_MBYTE
299 char_u ts_tcharlen; /* number of bytes in tword character */
300 char_u ts_tcharidx; /* current byte index in tword character */
301 char_u ts_isdiff; /* DIFF_ values */
302 char_u ts_fcharstart; /* index in fword where badword char started */
303#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000304 char_u ts_prewordlen; /* length of word in "preword[]" */
305 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000306 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000307 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000308 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000309 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000310 char_u ts_delidx; /* index in fword for char that was deleted,
311 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000312} trystate_T;
313
Bram Moolenaarea424162005-06-16 21:51:00 +0000314/* values for ts_isdiff */
315#define DIFF_NONE 0 /* no different byte (yet) */
316#define DIFF_YES 1 /* different byte found */
317#define DIFF_INSERT 2 /* inserting character */
318
Bram Moolenaard12a1322005-08-21 22:08:24 +0000319/* values for ts_flags */
320#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
321#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000322#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000323
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000324/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000325#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000326#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000327#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000328
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000329/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000330#define FIND_FOLDWORD 0 /* find word case-folded */
331#define FIND_KEEPWORD 1 /* find keep-case word */
332#define FIND_PREFIX 2 /* find word after prefix */
333#define FIND_COMPOUND 3 /* find case-folded compound word */
334#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000335
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100336static void find_word(matchinf_T *mip, int mode);
337static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap);
338static int can_compound(slang_T *slang, char_u *word, char_u *flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100339static int match_compoundrule(slang_T *slang, char_u *compflags);
340static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req);
341static void find_prefix(matchinf_T *mip, int mode);
342static int fold_more(matchinf_T *mip);
343static int spell_valid_case(int wordflags, int treeflags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100344static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100345static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100346static void clear_midword(win_T *buf);
347static void use_midword(slang_T *lp, win_T *buf);
348static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100349static int check_need_cap(linenr_T lnum, colnr_T col);
350static 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 +0000351#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100352static void spell_suggest_expr(suginfo_T *su, char_u *expr);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000353#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100354static void spell_suggest_file(suginfo_T *su, char_u *fname);
355static void spell_suggest_intern(suginfo_T *su, int interactive);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100356static void spell_find_cleanup(suginfo_T *su);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100357static void suggest_try_special(suginfo_T *su);
358static void suggest_try_change(suginfo_T *su);
359static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
360static void go_deeper(trystate_T *stack, int depth, int score_add);
Bram Moolenaar53805d12005-08-01 07:08:33 +0000361#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100362static int nofold_len(char_u *fword, int flen, char_u *word);
Bram Moolenaar53805d12005-08-01 07:08:33 +0000363#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100364static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
365static void score_comp_sal(suginfo_T *su);
366static void score_combine(suginfo_T *su);
367static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
368static void suggest_try_soundalike_prep(void);
369static void suggest_try_soundalike(suginfo_T *su);
370static void suggest_try_soundalike_finish(void);
371static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
372static int soundfold_find(slang_T *slang, char_u *word);
373static void make_case_word(char_u *fword, char_u *cword, int flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100374static int similar_chars(slang_T *slang, int c1, int c2);
375static 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);
376static void check_suggestions(suginfo_T *su, garray_T *gap);
377static void add_banned(suginfo_T *su, char_u *word);
378static void rescore_suggestions(suginfo_T *su);
379static void rescore_one(suginfo_T *su, suggest_T *stp);
380static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100381static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
382static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000383#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100384static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000385#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100386static int soundalike_score(char_u *goodsound, char_u *badsound);
387static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
388static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000389#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100390static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000391#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100392static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
393static 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 +0000394
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000395
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000396/* Remember what "z?" replaced. */
397static char_u *repl_from = NULL;
398static char_u *repl_to = NULL;
399
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000400/*
401 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000402 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000403 * "*attrp" is set to the highlight index for a badly spelled word. For a
404 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000405 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000406 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000407 * "capcol" is used to check for a Capitalised word after the end of a
408 * sentence. If it's zero then perform the check. Return the column where to
409 * check next, or -1 when no sentence end was found. If it's NULL then don't
410 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000411 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000412 * Returns the length of the word in bytes, also when it's OK, so that the
413 * caller can skip over the word.
414 */
415 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100416spell_check(
417 win_T *wp, /* current window */
418 char_u *ptr,
419 hlf_T *attrp,
420 int *capcol, /* column to check for Capital */
421 int docount) /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000422{
423 matchinf_T mi; /* Most things are put in "mi" so that it can
424 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000425 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000426 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000427 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000428 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000429 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000430
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000431 /* A word never starts at a space or a control character. Return quickly
432 * then, skipping over the character. */
433 if (*ptr <= ' ')
434 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000435
436 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200437 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000438 return 1;
439
Bram Moolenaar5195e452005-08-19 20:32:47 +0000440 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000441
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000442 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000443 * 0X99FF. But always do check spelling to find "3GPP" and "11
444 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000445 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000446 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100447 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
448 mi.mi_end = skipbin(ptr + 2);
449 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000450 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000451 else
452 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000453 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000455
Bram Moolenaar0c405862005-06-22 22:26:26 +0000456 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000457 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000458 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200459 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000460 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000461 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000462 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100463 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200464 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000465
Bram Moolenaar860cae12010-06-05 23:22:07 +0200466 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000467 {
468 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000469 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000470 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000472 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000473 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000474 if (capcol != NULL)
475 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000476
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000477 /* We always use the characters up to the next non-word character,
478 * also for bad words. */
479 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000480
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000481 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200482 mi.mi_capflags = 0;
483 mi.mi_cend = NULL;
484 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000485
Bram Moolenaar5195e452005-08-19 20:32:47 +0000486 /* case-fold the word with one non-word character, so that we can check
487 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100489 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000490
491 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
492 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000493 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000494
495 /* The word is bad unless we recognize it. */
496 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000497 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000498
499 /*
500 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000501 * We check them all, because a word may be matched longer in another
502 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000503 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200504 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000505 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200506 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000507
508 /* If reloading fails the language is still in the list but everything
509 * has been cleared. */
510 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
511 continue;
512
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000513 /* Check for a matching word in case-folded words. */
514 find_word(&mi, FIND_FOLDWORD);
515
516 /* Check for a matching word in keep-case words. */
517 find_word(&mi, FIND_KEEPWORD);
518
519 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000520 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000521
522 /* For a NOBREAK language, may want to use a word without a following
523 * word as a backup. */
524 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
525 && mi.mi_result2 != SP_BAD)
526 {
527 mi.mi_result = mi.mi_result2;
528 mi.mi_end = mi.mi_end2;
529 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000530
531 /* Count the word in the first language where it's found to be OK. */
532 if (count_word && mi.mi_result == SP_OK)
533 {
534 count_common_word(mi.mi_lp->lp_slang, ptr,
535 (int)(mi.mi_end - ptr), 1);
536 count_word = FALSE;
537 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000538 }
539
540 if (mi.mi_result != SP_OK)
541 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000542 /* If we found a number skip over it. Allows for "42nd". Do flag
543 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000544 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000545 {
546 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
547 return nrlen;
548 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000549
550 /* When we are at a non-word character there is no error, just
551 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100552 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000553 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200554 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000555 {
556 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100557 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000558
559 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200560 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000561 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100562 r = vim_regexec(&regmatch, ptr, 0);
563 wp->w_s->b_cap_prog = regmatch.regprog;
564 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000565 *capcol = (int)(regmatch.endp[0] - ptr);
566 }
567
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000568#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000569 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000570 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000572 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000573 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000574 else if (mi.mi_end == ptr)
575 /* Always include at least one character. Required for when there
576 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100577 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000578 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200579 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000580 {
581 char_u *p, *fp;
582 int save_result = mi.mi_result;
583
584 /* First language in 'spelllang' is NOBREAK. Find first position
585 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200586 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000587 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000588 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000589 p = mi.mi_word;
590 fp = mi.mi_fword;
591 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000592 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100593 MB_PTR_ADV(p);
594 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000595 if (p >= mi.mi_end)
596 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000597 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000598 find_word(&mi, FIND_COMPOUND);
599 if (mi.mi_result != SP_BAD)
600 {
601 mi.mi_end = p;
602 break;
603 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000604 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000605 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000606 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000607 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000608
609 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000610 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000611 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000612 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000613 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000614 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000615 }
616
Bram Moolenaar5195e452005-08-19 20:32:47 +0000617 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
618 {
619 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000620 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000621 return wrongcaplen;
622 }
623
Bram Moolenaar51485f02005-06-04 21:55:20 +0000624 return (int)(mi.mi_end - ptr);
625}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000626
Bram Moolenaar51485f02005-06-04 21:55:20 +0000627/*
628 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000629 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
630 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
631 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
632 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000633 *
634 * For a match mip->mi_result is updated.
635 */
636 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100637find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000638{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000640 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000641 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000642 int endidxcnt = 0;
643 int len;
644 int wlen = 0;
645 int flen;
646 int c;
647 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000648 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000649#ifdef FEAT_MBYTE
650 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000651#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +0000652 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000653 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000654 slang_T *slang = mip->mi_lp->lp_slang;
655 unsigned flags;
656 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000657 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000658 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000659 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000660 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000661
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000662 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000663 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000664 /* Check for word with matching case in keep-case tree. */
665 ptr = mip->mi_word;
666 flen = 9999; /* no case folding, always enough bytes */
667 byts = slang->sl_kbyts;
668 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000669
670 if (mode == FIND_KEEPCOMPOUND)
671 /* Skip over the previously found word(s). */
672 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000673 }
674 else
675 {
676 /* Check for case-folded in case-folded tree. */
677 ptr = mip->mi_fword;
678 flen = mip->mi_fwordlen; /* available case-folded bytes */
679 byts = slang->sl_fbyts;
680 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000681
682 if (mode == FIND_PREFIX)
683 {
684 /* Skip over the prefix. */
685 wlen = mip->mi_prefixlen;
686 flen -= mip->mi_prefixlen;
687 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000688 else if (mode == FIND_COMPOUND)
689 {
690 /* Skip over the previously found word(s). */
691 wlen = mip->mi_compoff;
692 flen -= mip->mi_compoff;
693 }
694
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695 }
696
Bram Moolenaar51485f02005-06-04 21:55:20 +0000697 if (byts == NULL)
698 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000699
Bram Moolenaar51485f02005-06-04 21:55:20 +0000700 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000701 * Repeat advancing in the tree until:
702 * - there is a byte that doesn't match,
703 * - we reach the end of the tree,
704 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 */
706 for (;;)
707 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000708 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000709 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000710
711 len = byts[arridx++];
712
713 /* If the first possible byte is a zero the word could end here.
714 * Remember this index, we first check for the longest word. */
715 if (byts[arridx] == 0)
716 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717 if (endidxcnt == MAXWLEN)
718 {
719 /* Must be a corrupted spell file. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100720 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000721 return;
722 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000723 endlen[endidxcnt] = wlen;
724 endidx[endidxcnt++] = arridx++;
725 --len;
726
727 /* Skip over the zeros, there can be several flag/region
728 * combinations. */
729 while (len > 0 && byts[arridx] == 0)
730 {
731 ++arridx;
732 --len;
733 }
734 if (len == 0)
735 break; /* no children, word must end here */
736 }
737
738 /* Stop looking at end of the line. */
739 if (ptr[wlen] == NUL)
740 break;
741
742 /* Perform a binary search in the list of accepted bytes. */
743 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000744 if (c == TAB) /* <Tab> is handled like <Space> */
745 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000746 lo = arridx;
747 hi = arridx + len - 1;
748 while (lo < hi)
749 {
750 m = (lo + hi) / 2;
751 if (byts[m] > c)
752 hi = m - 1;
753 else if (byts[m] < c)
754 lo = m + 1;
755 else
756 {
757 lo = hi = m;
758 break;
759 }
760 }
761
762 /* Stop if there is no matching byte. */
763 if (hi < lo || byts[lo] != c)
764 break;
765
766 /* Continue at the child (if there is one). */
767 arridx = idxs[lo];
768 ++wlen;
769 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000770
771 /* One space in the good word may stand for several spaces in the
772 * checked word. */
773 if (c == ' ')
774 {
775 for (;;)
776 {
777 if (flen <= 0 && *mip->mi_fend != NUL)
778 flen = fold_more(mip);
779 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
780 break;
781 ++wlen;
782 --flen;
783 }
784 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000785 }
786
787 /*
788 * Verify that one of the possible endings is valid. Try the longest
789 * first.
790 */
791 while (endidxcnt > 0)
792 {
793 --endidxcnt;
794 arridx = endidx[endidxcnt];
795 wlen = endlen[endidxcnt];
796
797#ifdef FEAT_MBYTE
798 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
799 continue; /* not at first byte of character */
800#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200801 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000802 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000803 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000804 continue; /* next char is a word character */
805 word_ends = FALSE;
806 }
807 else
808 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000809 /* The prefix flag is before compound flags. Once a valid prefix flag
810 * has been found we try compound flags. */
811 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000812
813#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000814 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000815 {
816 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000817 * when folding case. This can be slow, take a shortcut when the
818 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000819 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000820 if (STRNCMP(ptr, p, wlen) != 0)
821 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100822 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
823 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000824 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000825 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000826 }
827#endif
828
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000829 /* Check flags and region. For FIND_PREFIX check the condition and
830 * prefix ID.
831 * Repeat this if there are more flags/region alternatives until there
832 * is a match. */
833 res = SP_BAD;
834 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
835 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000836 {
837 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000838
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000839 /* For the fold-case tree check that the case of the checked word
840 * matches with what the word in the tree requires.
841 * For keep-case tree the case is always right. For prefixes we
842 * don't bother to check. */
843 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000844 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000845 if (mip->mi_cend != mip->mi_word + wlen)
846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000847 /* mi_capflags was set for a different word length, need
848 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000849 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000850 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851 }
852
Bram Moolenaar0c405862005-06-22 22:26:26 +0000853 if (mip->mi_capflags == WF_KEEPCAP
854 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000855 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000856 }
857
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000858 /* When mode is FIND_PREFIX the word must support the prefix:
859 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000860 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000861 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000862 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000863 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000864 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000865 mip->mi_word + mip->mi_cprefixlen, slang,
866 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000867 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000868 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000869
870 /* Use the WF_RARE flag for a rare prefix. */
871 if (c & WF_RAREPFX)
872 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000873 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000874 }
875
Bram Moolenaar78622822005-08-23 21:00:13 +0000876 if (slang->sl_nobreak)
877 {
878 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
879 && (flags & WF_BANNED) == 0)
880 {
881 /* NOBREAK: found a valid following word. That's all we
882 * need to know, so return. */
883 mip->mi_result = SP_OK;
884 break;
885 }
886 }
887
888 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
889 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000890 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000891 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000892 * COMPOUNDMIN reject it quickly.
893 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000894 * that's too short... Myspell compatibility requires this
895 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000896 if (((unsigned)flags >> 24) == 0
897 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000898 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000899#ifdef FEAT_MBYTE
900 /* For multi-byte chars check character length against
901 * COMPOUNDMIN. */
902 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000903 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000904 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
905 wlen - mip->mi_compoff) < slang->sl_compminlen)
906 continue;
907#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000908
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000909 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000910 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000911 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
912 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000913 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000914 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000915
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000916 /* Don't allow compounding on a side where an affix was added,
917 * unless COMPOUNDPERMITFLAG was used. */
918 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
919 continue;
920 if (!word_ends && (flags & WF_NOCOMPAFT))
921 continue;
922
Bram Moolenaard12a1322005-08-21 22:08:24 +0000923 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000924 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000925 ? slang->sl_compstartflags
926 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000927 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000928 continue;
929
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000930 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
931 * discard the compound word. */
932 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
933 continue;
934
Bram Moolenaare52325c2005-08-22 22:54:29 +0000935 if (mode == FIND_COMPOUND)
936 {
937 int capflags;
938
939 /* Need to check the caps type of the appended compound
940 * word. */
941#ifdef FEAT_MBYTE
942 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
943 mip->mi_compoff) != 0)
944 {
945 /* case folding may have changed the length */
946 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100947 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
948 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000949 }
950 else
951#endif
952 p = mip->mi_word + mip->mi_compoff;
953 capflags = captype(p, mip->mi_word + wlen);
954 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
955 && (flags & WF_FIXCAP) != 0))
956 continue;
957
958 if (capflags != WF_ALLCAP)
959 {
960 /* When the character before the word is a word
961 * character we do not accept a Onecap word. We do
962 * accept a no-caps word, even when the dictionary
963 * word specifies ONECAP. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100964 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100965 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000966 ? capflags == WF_ONECAP
967 : (flags & WF_ONECAP) != 0
968 && capflags != WF_ONECAP)
969 continue;
970 }
971 }
972
Bram Moolenaar5195e452005-08-19 20:32:47 +0000973 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000974 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000975 * the number of syllables must not be too large. */
976 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
977 mip->mi_compflags[mip->mi_complen + 1] = NUL;
978 if (word_ends)
979 {
980 char_u fword[MAXWLEN];
981
982 if (slang->sl_compsylmax < MAXWLEN)
983 {
984 /* "fword" is only needed for checking syllables. */
985 if (ptr == mip->mi_word)
986 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
987 else
988 vim_strncpy(fword, ptr, endlen[endidxcnt]);
989 }
990 if (!can_compound(slang, fword, mip->mi_compflags))
991 continue;
992 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000993 else if (slang->sl_comprules != NULL
994 && !match_compoundrule(slang, mip->mi_compflags))
995 /* The compound flags collected so far do not match any
996 * COMPOUNDRULE, discard the compounded word. */
997 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000998 }
999
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001000 /* Check NEEDCOMPOUND: can't use word without compounding. */
1001 else if (flags & WF_NEEDCOMP)
1002 continue;
1003
Bram Moolenaar78622822005-08-23 21:00:13 +00001004 nobreak_result = SP_OK;
1005
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001006 if (!word_ends)
1007 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001008 int save_result = mip->mi_result;
1009 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001010 langp_T *save_lp = mip->mi_lp;
1011 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001012
1013 /* Check that a valid word follows. If there is one and we
1014 * are compounding, it will set "mi_result", thus we are
1015 * always finished here. For NOBREAK we only check that a
1016 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001017 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001018 if (slang->sl_nobreak)
1019 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001020
1021 /* Find following word in case-folded tree. */
1022 mip->mi_compoff = endlen[endidxcnt];
1023#ifdef FEAT_MBYTE
1024 if (has_mbyte && mode == FIND_KEEPWORD)
1025 {
1026 /* Compute byte length in case-folded word from "wlen":
1027 * byte length in keep-case word. Length may change when
1028 * folding case. This can be slow, take a shortcut when
1029 * the case-folded word is equal to the keep-case word. */
1030 p = mip->mi_fword;
1031 if (STRNCMP(ptr, p, wlen) != 0)
1032 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001033 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
1034 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001035 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001036 }
1037 }
1038#endif
Bram Moolenaarba534352016-04-21 09:20:26 +02001039#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001040 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001041#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001042 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001043 if (flags & WF_COMPROOT)
1044 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001045
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001046 /* For NOBREAK we need to try all NOBREAK languages, at least
1047 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001048 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001049 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001050 if (slang->sl_nobreak)
1051 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001052 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001053 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1054 || !mip->mi_lp->lp_slang->sl_nobreak)
1055 continue;
1056 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001057
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001058 find_word(mip, FIND_COMPOUND);
1059
1060 /* When NOBREAK any word that matches is OK. Otherwise we
1061 * need to find the longest match, thus try with keep-case
1062 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001063 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1064 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001065 /* Find following word in keep-case tree. */
1066 mip->mi_compoff = wlen;
1067 find_word(mip, FIND_KEEPCOMPOUND);
1068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001069#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1070 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1071 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001072 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1073 {
1074 /* Check for following word with prefix. */
1075 mip->mi_compoff = c;
1076 find_prefix(mip, FIND_COMPOUND);
1077 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001078#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001079 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001080
1081 if (!slang->sl_nobreak)
1082 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001083 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001084 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001085 if (flags & WF_COMPROOT)
1086 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001087 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001088
Bram Moolenaar78622822005-08-23 21:00:13 +00001089 if (slang->sl_nobreak)
1090 {
1091 nobreak_result = mip->mi_result;
1092 mip->mi_result = save_result;
1093 mip->mi_end = save_end;
1094 }
1095 else
1096 {
1097 if (mip->mi_result == SP_OK)
1098 break;
1099 continue;
1100 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001101 }
1102
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001103 if (flags & WF_BANNED)
1104 res = SP_BANNED;
1105 else if (flags & WF_REGION)
1106 {
1107 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001108 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001109 res = SP_OK;
1110 else
1111 res = SP_LOCAL;
1112 }
1113 else if (flags & WF_RARE)
1114 res = SP_RARE;
1115 else
1116 res = SP_OK;
1117
Bram Moolenaar78622822005-08-23 21:00:13 +00001118 /* Always use the longest match and the best result. For NOBREAK
1119 * we separately keep the longest match without a following good
1120 * word as a fall-back. */
1121 if (nobreak_result == SP_BAD)
1122 {
1123 if (mip->mi_result2 > res)
1124 {
1125 mip->mi_result2 = res;
1126 mip->mi_end2 = mip->mi_word + wlen;
1127 }
1128 else if (mip->mi_result2 == res
1129 && mip->mi_end2 < mip->mi_word + wlen)
1130 mip->mi_end2 = mip->mi_word + wlen;
1131 }
1132 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001133 {
1134 mip->mi_result = res;
1135 mip->mi_end = mip->mi_word + wlen;
1136 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001137 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001138 mip->mi_end = mip->mi_word + wlen;
1139
Bram Moolenaar78622822005-08-23 21:00:13 +00001140 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001141 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001142 }
1143
Bram Moolenaar78622822005-08-23 21:00:13 +00001144 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001145 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001146 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001147}
1148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001149/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001150 * Return TRUE if there is a match between the word ptr[wlen] and
1151 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1152 * word.
1153 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1154 * end of ptr[wlen] and the second part matches after it.
1155 */
1156 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001157match_checkcompoundpattern(
1158 char_u *ptr,
1159 int wlen,
1160 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001161{
1162 int i;
1163 char_u *p;
1164 int len;
1165
1166 for (i = 0; i + 1 < gap->ga_len; i += 2)
1167 {
1168 p = ((char_u **)gap->ga_data)[i + 1];
1169 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1170 {
1171 /* Second part matches at start of following compound word, now
1172 * check if first part matches at end of previous word. */
1173 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001174 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001175 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1176 return TRUE;
1177 }
1178 }
1179 return FALSE;
1180}
1181
1182/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001183 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1184 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001185 */
1186 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001187can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001188{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001189#ifdef FEAT_MBYTE
1190 char_u uflags[MAXWLEN * 2];
1191 int i;
1192#endif
1193 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001194
1195 if (slang->sl_compprog == NULL)
1196 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001197#ifdef FEAT_MBYTE
1198 if (enc_utf8)
1199 {
1200 /* Need to convert the single byte flags to utf8 characters. */
1201 p = uflags;
1202 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001203 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001204 *p = NUL;
1205 p = uflags;
1206 }
1207 else
1208#endif
1209 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001210 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001211 return FALSE;
1212
Bram Moolenaare52325c2005-08-22 22:54:29 +00001213 /* Count the number of syllables. This may be slow, do it last. If there
1214 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001215 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001216 if (slang->sl_compsylmax < MAXWLEN
1217 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001218 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001219 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001220}
1221
1222/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001223 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1224 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1225 * lines if they don't contain wildcards.
1226 */
1227 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001228can_be_compound(
1229 trystate_T *sp,
1230 slang_T *slang,
1231 char_u *compflags,
1232 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001233{
1234 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1235 * then it can't possibly compound. */
1236 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1237 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1238 return FALSE;
1239
1240 /* If there are no wildcards, we can check if the flags collected so far
1241 * possibly can form a match with COMPOUNDRULE patterns. This only
1242 * makes sense when we have two or more words. */
1243 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1244 {
1245 int v;
1246
1247 compflags[sp->ts_complen] = flag;
1248 compflags[sp->ts_complen + 1] = NUL;
1249 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1250 compflags[sp->ts_complen] = NUL;
1251 return v;
1252 }
1253
1254 return TRUE;
1255}
1256
1257
1258/*
1259 * Return TRUE if the compound flags in compflags[] match the start of any
1260 * compound rule. This is used to stop trying a compound if the flags
1261 * collected so far can't possibly match any compound rule.
1262 * Caller must check that slang->sl_comprules is not NULL.
1263 */
1264 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001265match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001266{
1267 char_u *p;
1268 int i;
1269 int c;
1270
1271 /* loop over all the COMPOUNDRULE entries */
1272 for (p = slang->sl_comprules; *p != NUL; ++p)
1273 {
1274 /* loop over the flags in the compound word we have made, match
1275 * them against the current rule entry */
1276 for (i = 0; ; ++i)
1277 {
1278 c = compflags[i];
1279 if (c == NUL)
1280 /* found a rule that matches for the flags we have so far */
1281 return TRUE;
1282 if (*p == '/' || *p == NUL)
1283 break; /* end of rule, it's too short */
1284 if (*p == '[')
1285 {
1286 int match = FALSE;
1287
1288 /* compare against all the flags in [] */
1289 ++p;
1290 while (*p != ']' && *p != NUL)
1291 if (*p++ == c)
1292 match = TRUE;
1293 if (!match)
1294 break; /* none matches */
1295 }
1296 else if (*p != c)
1297 break; /* flag of word doesn't match flag in pattern */
1298 ++p;
1299 }
1300
1301 /* Skip to the next "/", where the next pattern starts. */
1302 p = vim_strchr(p, '/');
1303 if (p == NULL)
1304 break;
1305 }
1306
1307 /* Checked all the rules and none of them match the flags, so there
1308 * can't possibly be a compound starting with these flags. */
1309 return FALSE;
1310}
1311
1312/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001313 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1314 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001315 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001316 */
1317 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001318valid_word_prefix(
1319 int totprefcnt, /* nr of prefix IDs */
1320 int arridx, /* idx in sl_pidxs[] */
1321 int flags,
1322 char_u *word,
1323 slang_T *slang,
1324 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001325{
1326 int prefcnt;
1327 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001328 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001329 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001330
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001331 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001332 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1333 {
1334 pidx = slang->sl_pidxs[arridx + prefcnt];
1335
1336 /* Check the prefix ID. */
1337 if (prefid != (pidx & 0xff))
1338 continue;
1339
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001340 /* Check if the prefix doesn't combine and the word already has a
1341 * suffix. */
1342 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1343 continue;
1344
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001345 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001346 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001347 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1348 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001349 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001350 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001351 continue;
1352 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001353 else if (cond_req)
1354 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001355
Bram Moolenaar53805d12005-08-01 07:08:33 +00001356 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001357 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001358 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001359 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001360}
1361
1362/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001363 * Check if the word at "mip->mi_word" has a matching prefix.
1364 * If it does, then check the following word.
1365 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001366 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1367 * prefix in a compound word.
1368 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001369 * For a match mip->mi_result is updated.
1370 */
1371 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001372find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001373{
1374 idx_T arridx = 0;
1375 int len;
1376 int wlen = 0;
1377 int flen;
1378 int c;
1379 char_u *ptr;
1380 idx_T lo, hi, m;
1381 slang_T *slang = mip->mi_lp->lp_slang;
1382 char_u *byts;
1383 idx_T *idxs;
1384
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001385 byts = slang->sl_pbyts;
1386 if (byts == NULL)
1387 return; /* array is empty */
1388
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001389 /* We use the case-folded word here, since prefixes are always
1390 * case-folded. */
1391 ptr = mip->mi_fword;
1392 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001393 if (mode == FIND_COMPOUND)
1394 {
1395 /* Skip over the previously found word(s). */
1396 ptr += mip->mi_compoff;
1397 flen -= mip->mi_compoff;
1398 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001399 idxs = slang->sl_pidxs;
1400
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001401 /*
1402 * Repeat advancing in the tree until:
1403 * - there is a byte that doesn't match,
1404 * - we reach the end of the tree,
1405 * - or we reach the end of the line.
1406 */
1407 for (;;)
1408 {
1409 if (flen == 0 && *mip->mi_fend != NUL)
1410 flen = fold_more(mip);
1411
1412 len = byts[arridx++];
1413
1414 /* If the first possible byte is a zero the prefix could end here.
1415 * Check if the following word matches and supports the prefix. */
1416 if (byts[arridx] == 0)
1417 {
1418 /* There can be several prefixes with different conditions. We
1419 * try them all, since we don't know which one will give the
1420 * longest match. The word is the same each time, pass the list
1421 * of possible prefixes to find_word(). */
1422 mip->mi_prefarridx = arridx;
1423 mip->mi_prefcnt = len;
1424 while (len > 0 && byts[arridx] == 0)
1425 {
1426 ++arridx;
1427 --len;
1428 }
1429 mip->mi_prefcnt -= len;
1430
1431 /* Find the word that comes after the prefix. */
1432 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001433 if (mode == FIND_COMPOUND)
1434 /* Skip over the previously found word(s). */
1435 mip->mi_prefixlen += mip->mi_compoff;
1436
Bram Moolenaar53805d12005-08-01 07:08:33 +00001437#ifdef FEAT_MBYTE
1438 if (has_mbyte)
1439 {
1440 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001441 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1442 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001443 }
1444 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001445 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001446#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001447 find_word(mip, FIND_PREFIX);
1448
1449
1450 if (len == 0)
1451 break; /* no children, word must end here */
1452 }
1453
1454 /* Stop looking at end of the line. */
1455 if (ptr[wlen] == NUL)
1456 break;
1457
1458 /* Perform a binary search in the list of accepted bytes. */
1459 c = ptr[wlen];
1460 lo = arridx;
1461 hi = arridx + len - 1;
1462 while (lo < hi)
1463 {
1464 m = (lo + hi) / 2;
1465 if (byts[m] > c)
1466 hi = m - 1;
1467 else if (byts[m] < c)
1468 lo = m + 1;
1469 else
1470 {
1471 lo = hi = m;
1472 break;
1473 }
1474 }
1475
1476 /* Stop if there is no matching byte. */
1477 if (hi < lo || byts[lo] != c)
1478 break;
1479
1480 /* Continue at the child (if there is one). */
1481 arridx = idxs[lo];
1482 ++wlen;
1483 --flen;
1484 }
1485}
1486
1487/*
1488 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001489 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001490 * Return the length of the folded chars in bytes.
1491 */
1492 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001493fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001494{
1495 int flen;
1496 char_u *p;
1497
1498 p = mip->mi_fend;
1499 do
1500 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001501 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001502 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001503
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001504 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001505 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001506 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001507
1508 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1509 mip->mi_fword + mip->mi_fwordlen,
1510 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001511 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001512 mip->mi_fwordlen += flen;
1513 return flen;
1514}
1515
1516/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001517 * Check case flags for a word. Return TRUE if the word has the requested
1518 * case.
1519 */
1520 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001521spell_valid_case(
1522 int wordflags, /* flags for the checked word. */
1523 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001524{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001525 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001526 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001527 && ((treeflags & WF_ONECAP) == 0
1528 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001529}
1530
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001531/*
1532 * Return TRUE if spell checking is not enabled.
1533 */
1534 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001535no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001536{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001537 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1538 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001540 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001541 return TRUE;
1542 }
1543 return FALSE;
1544}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001545
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001546/*
1547 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001548 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1549 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001550 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1551 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001552 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001553 */
1554 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001555spell_move_to(
1556 win_T *wp,
1557 int dir, /* FORWARD or BACKWARD */
1558 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
1559 int curline,
1560 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001561 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001562{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001563 linenr_T lnum;
1564 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001565 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001566 char_u *line;
1567 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001568 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001569 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001570 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001571#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001572 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001573#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001574 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001575 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001576 char_u *buf = NULL;
1577 int buflen = 0;
1578 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001579 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001580 int found_one = FALSE;
1581 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001582
Bram Moolenaar95529562005-08-25 21:21:38 +00001583 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001584 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001585
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001586 /*
1587 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001588 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001589 *
1590 * When searching backwards, we continue in the line to find the last
1591 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001592 *
1593 * We concatenate the start of the next line, so that wrapped words work
1594 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1595 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001596 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001597 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001598 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001599
1600 while (!got_int)
1601 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001602 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001603
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001604 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001605 if (buflen < len + MAXWLEN + 2)
1606 {
1607 vim_free(buf);
1608 buflen = len + MAXWLEN + 2;
1609 buf = alloc(buflen);
1610 if (buf == NULL)
1611 break;
1612 }
1613
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001614 /* In first line check first word for Capital. */
1615 if (lnum == 1)
1616 capcol = 0;
1617
1618 /* For checking first word with a capital skip white space. */
1619 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001620 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001621 else if (curline && wp == curwin)
1622 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001623 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001624 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001625 if (check_need_cap(lnum, col))
1626 capcol = col;
1627
1628 /* Need to get the line again, may have looked at the previous
1629 * one. */
1630 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1631 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001632
Bram Moolenaar0c405862005-06-22 22:26:26 +00001633 /* Copy the line into "buf" and append the start of the next line if
1634 * possible. */
1635 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001636 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001637 spell_cat_line(buf + STRLEN(buf),
1638 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001639
1640 p = buf + skip;
1641 endp = buf + len;
1642 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001643 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001644 /* When searching backward don't search after the cursor. Unless
1645 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001646 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001647 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001648 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001649 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001650 break;
1651
1652 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001653 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001654 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001655
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001656 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001657 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001658 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001659 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001660 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001661 /* When searching forward only accept a bad word after
1662 * the cursor. */
1663 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001664 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001665 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001666 && (wrapped
1667 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001668 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001669 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001670 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001671#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001672 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001673 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001674 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001675 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001676 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001677 if (!can_spell)
1678 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001679 }
1680 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001681#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001682 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001683
Bram Moolenaar51485f02005-06-04 21:55:20 +00001684 if (can_spell)
1685 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001686 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001687 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001688 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001689#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001690 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001691#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001692 if (dir == FORWARD)
1693 {
1694 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001695 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001696 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001697 if (attrp != NULL)
1698 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001699 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001700 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001701 else if (curline)
1702 /* Insert mode completion: put cursor after
1703 * the bad word. */
1704 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001705 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001706 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001707 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001708 else
1709 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001710 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001711 }
1712
Bram Moolenaar51485f02005-06-04 21:55:20 +00001713 /* advance to character after the word */
1714 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001715 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001716 }
1717
Bram Moolenaar5195e452005-08-19 20:32:47 +00001718 if (dir == BACKWARD && found_pos.lnum != 0)
1719 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001720 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001721 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001722 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001723 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001724 }
1725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001726 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001727 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001728
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001729 /* If we are back at the starting line and searched it again there
1730 * is no match, give up. */
1731 if (lnum == wp->w_cursor.lnum && wrapped)
1732 break;
1733
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001734 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001735 if (dir == BACKWARD)
1736 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001737 if (lnum > 1)
1738 --lnum;
1739 else if (!p_ws)
1740 break; /* at first line and 'nowrapscan' */
1741 else
1742 {
1743 /* Wrap around to the end of the buffer. May search the
1744 * starting line again and accept the last match. */
1745 lnum = wp->w_buffer->b_ml.ml_line_count;
1746 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001747 if (!shortmess(SHM_SEARCH))
1748 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001749 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001750 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001751 }
1752 else
1753 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001754 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1755 ++lnum;
1756 else if (!p_ws)
1757 break; /* at first line and 'nowrapscan' */
1758 else
1759 {
1760 /* Wrap around to the start of the buffer. May search the
1761 * starting line again and accept the first match. */
1762 lnum = 1;
1763 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001764 if (!shortmess(SHM_SEARCH))
1765 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001766 }
1767
1768 /* If we are back at the starting line and there is no match then
1769 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001770 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001771 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001772
1773 /* Skip the characters at the start of the next line that were
1774 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001775 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001776 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001777 else
1778 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001779
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001780 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001781 --capcol;
1782
1783 /* But after empty line check first word in next line */
1784 if (*skipwhite(line) == NUL)
1785 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001786 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001787
1788 line_breakcheck();
1789 }
1790
Bram Moolenaar0c405862005-06-22 22:26:26 +00001791 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001792 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001793}
1794
1795/*
1796 * For spell checking: concatenate the start of the following line "line" into
1797 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001798 * Keep the blanks at the start of the next line, this is used in win_line()
1799 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001800 */
1801 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001802spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001803{
1804 char_u *p;
1805 int n;
1806
1807 p = skipwhite(line);
1808 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1809 p = skipwhite(p + 1);
1810
1811 if (*p != NUL)
1812 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001813 /* Only worth concatenating if there is something else than spaces to
1814 * concatenate. */
1815 n = (int)(p - line) + 1;
1816 if (n < maxlen - 1)
1817 {
1818 vim_memset(buf, ' ', n);
1819 vim_strncpy(buf + n, p, maxlen - 1 - n);
1820 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001821 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822}
1823
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001824/*
1825 * Structure used for the cookie argument of do_in_runtimepath().
1826 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001827typedef struct spelload_S
1828{
1829 char_u sl_lang[MAXWLEN + 1]; /* language name */
1830 slang_T *sl_slang; /* resulting slang_T struct */
1831 int sl_nobreak; /* NOBREAK language found */
1832} spelload_T;
1833
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001834/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001835 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001836 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001837 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001838 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001839spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001841 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001842 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001843 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001844 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001845
Bram Moolenaarb765d632005-06-07 21:00:02 +00001846 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001847 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001848 STRCPY(sl.sl_lang, lang);
1849 sl.sl_slang = NULL;
1850 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001851
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001852 /* We may retry when no spell file is found for the language, an
1853 * autocommand may load it then. */
1854 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001855 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001856 /*
1857 * Find the first spell file for "lang" in 'runtimepath' and load it.
1858 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001859 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001860#ifdef VMS
1861 "spell/%s_%s.spl",
1862#else
1863 "spell/%s.%s.spl",
1864#endif
1865 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001866 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001867
1868 if (r == FAIL && *sl.sl_lang != NUL)
1869 {
1870 /* Try loading the ASCII version. */
1871 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001872#ifdef VMS
1873 "spell/%s_ascii.spl",
1874#else
1875 "spell/%s.ascii.spl",
1876#endif
1877 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001878 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001879
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001880 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1881 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1882 curbuf->b_fname, FALSE, curbuf))
1883 continue;
1884 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001885 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001886 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001887 }
1888
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001889 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001891 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001892#ifdef VMS
1893 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1894#else
1895 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1896#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001897 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001898 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001899 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001900 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001901 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001902 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001903 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001904 }
1905}
1906
1907/*
1908 * Return the encoding used for spell checking: Use 'encoding', except that we
1909 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1910 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001911 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001912spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001913{
1914
1915#ifdef FEAT_MBYTE
1916 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1917 return p_enc;
1918#endif
1919 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001920}
1921
1922/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001923 * Get the name of the .spl file for the internal wordlist into
1924 * "fname[MAXPATHL]".
1925 */
1926 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001927int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001928{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001929 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001930 int_wordlist, spell_enc());
1931}
1932
1933/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001934 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001935 * Caller must fill "sl_next".
1936 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001937 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001938slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001939{
1940 slang_T *lp;
1941
Bram Moolenaar51485f02005-06-04 21:55:20 +00001942 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001943 if (lp != NULL)
1944 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001945 if (lang != NULL)
1946 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001947 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001948 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001949 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001950 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001951 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001953
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001954 return lp;
1955}
1956
1957/*
1958 * Free the contents of an slang_T and the structure itself.
1959 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001960 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001961slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001962{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001963 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001964 vim_free(lp->sl_fname);
1965 slang_clear(lp);
1966 vim_free(lp);
1967}
1968
1969/*
1970 * Clear an slang_T so that the file can be reloaded.
1971 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001972 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001973slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001974{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975 garray_T *gap;
1976 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001977 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001978 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001979 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001980
Bram Moolenaard23a8232018-02-10 18:45:26 +01001981 VIM_CLEAR(lp->sl_fbyts);
1982 VIM_CLEAR(lp->sl_kbyts);
1983 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001984
Bram Moolenaard23a8232018-02-10 18:45:26 +01001985 VIM_CLEAR(lp->sl_fidxs);
1986 VIM_CLEAR(lp->sl_kidxs);
1987 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001988
Bram Moolenaar4770d092006-01-12 23:22:24 +00001989 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001990 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001991 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1992 while (gap->ga_len > 0)
1993 {
1994 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1995 vim_free(ftp->ft_from);
1996 vim_free(ftp->ft_to);
1997 }
1998 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001999 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002000
2001 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002002 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002003 {
2004 /* "ga_len" is set to 1 without adding an item for latin1 */
2005 if (gap->ga_data != NULL)
2006 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2007 for (i = 0; i < gap->ga_len; ++i)
2008 vim_free(((int **)gap->ga_data)[i]);
2009 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002010 else
2011 /* SAL items: free salitem_T items */
2012 while (gap->ga_len > 0)
2013 {
2014 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2015 vim_free(smp->sm_lead);
2016 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2017 vim_free(smp->sm_to);
2018#ifdef FEAT_MBYTE
2019 vim_free(smp->sm_lead_w);
2020 vim_free(smp->sm_oneof_w);
2021 vim_free(smp->sm_to_w);
2022#endif
2023 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002024 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002025
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002026 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002027 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002028 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002029 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002030
Bram Moolenaard23a8232018-02-10 18:45:26 +01002031 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002032
Bram Moolenaard23a8232018-02-10 18:45:26 +01002033 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002034
Bram Moolenaar473de612013-06-08 18:19:48 +02002035 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002036 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002037 VIM_CLEAR(lp->sl_comprules);
2038 VIM_CLEAR(lp->sl_compstartflags);
2039 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002040
Bram Moolenaard23a8232018-02-10 18:45:26 +01002041 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002042 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002043
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002044 ga_clear_strings(&lp->sl_comppat);
2045
Bram Moolenaar4770d092006-01-12 23:22:24 +00002046 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2047 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002048
Bram Moolenaar4770d092006-01-12 23:22:24 +00002049#ifdef FEAT_MBYTE
2050 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002051#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002052
Bram Moolenaar4770d092006-01-12 23:22:24 +00002053 /* Clear info from .sug file. */
2054 slang_clear_sug(lp);
2055
Bram Moolenaar5195e452005-08-19 20:32:47 +00002056 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002057 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002058 lp->sl_compsylmax = MAXWLEN;
2059 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060}
2061
2062/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002063 * Clear the info from the .sug file in "lp".
2064 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002065 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002066slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002067{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002068 VIM_CLEAR(lp->sl_sbyts);
2069 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002070 close_spellbuf(lp->sl_sugbuf);
2071 lp->sl_sugbuf = NULL;
2072 lp->sl_sugloaded = FALSE;
2073 lp->sl_sugtime = 0;
2074}
2075
2076/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002077 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002078 * Invoked through do_in_runtimepath().
2079 */
2080 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002081spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002083 spelload_T *slp = (spelload_T *)cookie;
2084 slang_T *slang;
2085
2086 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2087 if (slang != NULL)
2088 {
2089 /* When a previously loaded file has NOBREAK also use it for the
2090 * ".add" files. */
2091 if (slp->sl_nobreak && slang->sl_add)
2092 slang->sl_nobreak = TRUE;
2093 else if (slang->sl_nobreak)
2094 slp->sl_nobreak = TRUE;
2095
2096 slp->sl_slang = slang;
2097 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002098}
2099
Bram Moolenaar4770d092006-01-12 23:22:24 +00002100
2101/*
2102 * Add a word to the hashtable of common words.
2103 * If it's already there then the counter is increased.
2104 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002105 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002106count_common_word(
2107 slang_T *lp,
2108 char_u *word,
2109 int len, /* word length, -1 for upto NUL */
2110 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002111{
2112 hash_T hash;
2113 hashitem_T *hi;
2114 wordcount_T *wc;
2115 char_u buf[MAXWLEN];
2116 char_u *p;
2117
2118 if (len == -1)
2119 p = word;
2120 else
2121 {
2122 vim_strncpy(buf, word, len);
2123 p = buf;
2124 }
2125
2126 hash = hash_hash(p);
2127 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2128 if (HASHITEM_EMPTY(hi))
2129 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002130 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002131 if (wc == NULL)
2132 return;
2133 STRCPY(wc->wc_word, p);
2134 wc->wc_count = count;
2135 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2136 }
2137 else
2138 {
2139 wc = HI2WC(hi);
2140 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2141 wc->wc_count = MAXWORDCOUNT;
2142 }
2143}
2144
2145/*
2146 * Adjust the score of common words.
2147 */
2148 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002149score_wordcount_adj(
2150 slang_T *slang,
2151 int score,
2152 char_u *word,
2153 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002154{
2155 hashitem_T *hi;
2156 wordcount_T *wc;
2157 int bonus;
2158 int newscore;
2159
2160 hi = hash_find(&slang->sl_wordcount, word);
2161 if (!HASHITEM_EMPTY(hi))
2162 {
2163 wc = HI2WC(hi);
2164 if (wc->wc_count < SCORE_THRES2)
2165 bonus = SCORE_COMMON1;
2166 else if (wc->wc_count < SCORE_THRES3)
2167 bonus = SCORE_COMMON2;
2168 else
2169 bonus = SCORE_COMMON3;
2170 if (split)
2171 newscore = score - bonus / 2;
2172 else
2173 newscore = score - bonus;
2174 if (newscore < 0)
2175 return 0;
2176 return newscore;
2177 }
2178 return score;
2179}
2180
Bram Moolenaar5195e452005-08-19 20:32:47 +00002181
Bram Moolenaar6de68532005-08-24 22:08:48 +00002182/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002183 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002184 * Like strchr() but independent of locale.
2185 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002186 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002187byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002188{
2189 char_u *p;
2190
2191 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002192 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002193 return TRUE;
2194 return FALSE;
2195}
2196
Bram Moolenaar5195e452005-08-19 20:32:47 +00002197#define SY_MAXLEN 30
2198typedef struct syl_item_S
2199{
2200 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2201 int sy_len;
2202} syl_item_T;
2203
2204/*
2205 * Truncate "slang->sl_syllable" at the first slash and put the following items
2206 * in "slang->sl_syl_items".
2207 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002208 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002209init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002210{
2211 char_u *p;
2212 char_u *s;
2213 int l;
2214 syl_item_T *syl;
2215
2216 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2217 p = vim_strchr(slang->sl_syllable, '/');
2218 while (p != NULL)
2219 {
2220 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002221 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002222 break;
2223 s = p;
2224 p = vim_strchr(p, '/');
2225 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002226 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002227 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002228 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002229 if (l >= SY_MAXLEN)
2230 return SP_FORMERROR;
2231 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002232 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002233 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2234 + slang->sl_syl_items.ga_len++;
2235 vim_strncpy(syl->sy_chars, s, l);
2236 syl->sy_len = l;
2237 }
2238 return OK;
2239}
2240
2241/*
2242 * Count the number of syllables in "word".
2243 * When "word" contains spaces the syllables after the last space are counted.
2244 * Returns zero if syllables are not defines.
2245 */
2246 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002247count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002248{
2249 int cnt = 0;
2250 int skip = FALSE;
2251 char_u *p;
2252 int len;
2253 int i;
2254 syl_item_T *syl;
2255 int c;
2256
2257 if (slang->sl_syllable == NULL)
2258 return 0;
2259
2260 for (p = word; *p != NUL; p += len)
2261 {
2262 /* When running into a space reset counter. */
2263 if (*p == ' ')
2264 {
2265 len = 1;
2266 cnt = 0;
2267 continue;
2268 }
2269
2270 /* Find longest match of syllable items. */
2271 len = 0;
2272 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2273 {
2274 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2275 if (syl->sy_len > len
2276 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2277 len = syl->sy_len;
2278 }
2279 if (len != 0) /* found a match, count syllable */
2280 {
2281 ++cnt;
2282 skip = FALSE;
2283 }
2284 else
2285 {
2286 /* No recognized syllable item, at least a syllable char then? */
2287#ifdef FEAT_MBYTE
2288 c = mb_ptr2char(p);
2289 len = (*mb_ptr2len)(p);
2290#else
2291 c = *p;
2292 len = 1;
2293#endif
2294 if (vim_strchr(slang->sl_syllable, c) == NULL)
2295 skip = FALSE; /* No, search for next syllable */
2296 else if (!skip)
2297 {
2298 ++cnt; /* Yes, count it */
2299 skip = TRUE; /* don't count following syllable chars */
2300 }
2301 }
2302 }
2303 return cnt;
2304}
2305
2306/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002307 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002308 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002309 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002310 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002311did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002312{
2313 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002314 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002316 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002317 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002318 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002319 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002321 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002322 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002323 int len;
2324 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002325 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002326 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002327 char_u *use_region = NULL;
2328 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002329 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002330 int i, j;
2331 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002332 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002334 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002335 bufref_T bufref;
2336
2337 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002338
2339 /* We don't want to do this recursively. May happen when a language is
2340 * not available and the SpellFileMissing autocommand opens a new buffer
2341 * in which 'spell' is set. */
2342 if (recursive)
2343 return NULL;
2344 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002345
2346 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002347 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002348
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002349 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002350 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002351 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002352 if (spl_copy == NULL)
2353 goto theend;
2354
Bram Moolenaar2593e032013-11-14 03:54:07 +01002355#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002356 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002357#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002358
2359 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002360 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002361 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002362 /* Get one language name. */
2363 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002364 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002365 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002367 if (STRCMP(lang, "cjk") == 0)
2368 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01002369#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002370 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002371#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002372 continue;
2373 }
2374
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002375 /* If the name ends in ".spl" use it as the name of the spell file.
2376 * If there is a region name let "region" point to it and remove it
2377 * from the name. */
2378 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2379 {
2380 filename = TRUE;
2381
Bram Moolenaarb6356332005-07-18 21:40:44 +00002382 /* Locate a region and remove it from the file name. */
2383 p = vim_strchr(gettail(lang), '_');
2384 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2385 && !ASCII_ISALPHA(p[3]))
2386 {
2387 vim_strncpy(region_cp, p + 1, 2);
2388 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002389 region = region_cp;
2390 }
2391 else
2392 dont_use_region = TRUE;
2393
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002394 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002395 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2396 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002397 break;
2398 }
2399 else
2400 {
2401 filename = FALSE;
2402 if (len > 3 && lang[len - 3] == '_')
2403 {
2404 region = lang + len - 2;
2405 len -= 3;
2406 lang[len] = NUL;
2407 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002408 else
2409 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002410
2411 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002412 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2413 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002414 break;
2415 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002416
Bram Moolenaarb6356332005-07-18 21:40:44 +00002417 if (region != NULL)
2418 {
2419 /* If the region differs from what was used before then don't
2420 * use it for 'spellfile'. */
2421 if (use_region != NULL && STRCMP(region, use_region) != 0)
2422 dont_use_region = TRUE;
2423 use_region = region;
2424 }
2425
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002426 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002427 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002428 {
2429 if (filename)
2430 (void)spell_load_file(lang, lang, NULL, FALSE);
2431 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002432 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002433 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002434 /* SpellFileMissing autocommands may do anything, including
2435 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002436 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002439 goto theend;
2440 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002441 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002442 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002444 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002445 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002446 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002447 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2448 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2449 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002450 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002451 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002452 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002453 {
2454 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002455 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002456 if (c == REGION_ALL)
2457 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002458 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002459 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002460 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002461 /* This addition file is for other regions. */
2462 region_mask = 0;
2463 }
2464 else
2465 /* This is probably an error. Give a warning and
2466 * accept the words anyway. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002467 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002468 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469 }
2470 else
2471 region_mask = 1 << c;
2472 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002473
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002474 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002475 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002476 if (ga_grow(&ga, 1) == FAIL)
2477 {
2478 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002479 ret_msg = e_outofmem;
2480 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002481 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002482 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002483 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2484 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002485 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002486 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002487 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002488 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002489 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002490 }
2491
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002492 /* round 0: load int_wordlist, if possible.
2493 * round 1: load first name in 'spellfile'.
2494 * round 2: load second name in 'spellfile.
2495 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002496 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002497 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002499 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002500 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002501 /* Internal wordlist, if there is one. */
2502 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002503 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002504 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002505 }
2506 else
2507 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002508 /* One entry in 'spellfile'. */
2509 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2510 STRCAT(spf_name, ".spl");
2511
2512 /* If it was already found above then skip it. */
2513 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002514 {
2515 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2516 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002517 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002518 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002519 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002520 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002521 }
2522
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002523 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002524 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2525 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002526 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002527 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002529 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002530 * region name, the region is ignored otherwise. for int_wordlist
2531 * use an arbitrary name. */
2532 if (round == 0)
2533 STRCPY(lang, "internal wordlist");
2534 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002535 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002536 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002537 p = vim_strchr(lang, '.');
2538 if (p != NULL)
2539 *p = NUL; /* truncate at ".encoding.add" */
2540 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002541 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002542
2543 /* If one of the languages has NOBREAK we assume the addition
2544 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002545 if (slang != NULL && nobreak)
2546 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002548 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002550 region_mask = REGION_ALL;
2551 if (use_region != NULL && !dont_use_region)
2552 {
2553 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002554 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002555 if (c != REGION_ALL)
2556 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002557 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002558 /* This spell file is for other regions. */
2559 region_mask = 0;
2560 }
2561
2562 if (region_mask != 0)
2563 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002564 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2565 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2566 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002567 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2568 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002569 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002570 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002571 }
2572 }
2573
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002574 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002575 ga_clear(&wp->w_s->b_langp);
2576 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002577
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002578 /* For each language figure out what language to use for sound folding and
2579 * REP items. If the language doesn't support it itself use another one
2580 * with the same name. E.g. for "en-math" use "en". */
2581 for (i = 0; i < ga.ga_len; ++i)
2582 {
2583 lp = LANGP_ENTRY(ga, i);
2584
2585 /* sound folding */
2586 if (lp->lp_slang->sl_sal.ga_len > 0)
2587 /* language does sound folding itself */
2588 lp->lp_sallang = lp->lp_slang;
2589 else
2590 /* find first similar language that does sound folding */
2591 for (j = 0; j < ga.ga_len; ++j)
2592 {
2593 lp2 = LANGP_ENTRY(ga, j);
2594 if (lp2->lp_slang->sl_sal.ga_len > 0
2595 && STRNCMP(lp->lp_slang->sl_name,
2596 lp2->lp_slang->sl_name, 2) == 0)
2597 {
2598 lp->lp_sallang = lp2->lp_slang;
2599 break;
2600 }
2601 }
2602
2603 /* REP items */
2604 if (lp->lp_slang->sl_rep.ga_len > 0)
2605 /* language has REP items itself */
2606 lp->lp_replang = lp->lp_slang;
2607 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002608 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002609 for (j = 0; j < ga.ga_len; ++j)
2610 {
2611 lp2 = LANGP_ENTRY(ga, j);
2612 if (lp2->lp_slang->sl_rep.ga_len > 0
2613 && STRNCMP(lp->lp_slang->sl_name,
2614 lp2->lp_slang->sl_name, 2) == 0)
2615 {
2616 lp->lp_replang = lp2->lp_slang;
2617 break;
2618 }
2619 }
2620 }
2621
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002622theend:
2623 vim_free(spl_copy);
2624 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002625 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002626 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002627}
2628
2629/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002630 * Clear the midword characters for buffer "buf".
2631 */
2632 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002633clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002634{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002635 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002636#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01002637 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002638#endif
2639}
2640
2641/*
2642 * Use the "sl_midword" field of language "lp" for buffer "buf".
2643 * They add up to any currently used midword characters.
2644 */
2645 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002646use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002647{
2648 char_u *p;
2649
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002650 if (lp->sl_midword == NULL) /* there aren't any */
2651 return;
2652
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002653 for (p = lp->sl_midword; *p != NUL; )
2654#ifdef FEAT_MBYTE
2655 if (has_mbyte)
2656 {
2657 int c, l, n;
2658 char_u *bp;
2659
2660 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002661 l = (*mb_ptr2len)(p);
2662 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002663 wp->w_s->b_spell_ismw[c] = TRUE;
2664 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002665 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002666 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002667 else
2668 {
2669 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002670 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2671 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002672 if (bp != NULL)
2673 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002674 vim_free(wp->w_s->b_spell_ismw_mb);
2675 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002676 vim_strncpy(bp + n, p, l);
2677 }
2678 }
2679 p += l;
2680 }
2681 else
2682#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002683 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002684}
2685
2686/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002687 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002688 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002689 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002690 */
2691 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002693{
2694 int i;
2695
2696 for (i = 0; ; i += 2)
2697 {
2698 if (rp[i] == NUL)
2699 return REGION_ALL;
2700 if (rp[i] == region[0] && rp[i + 1] == region[1])
2701 break;
2702 }
2703 return i / 2;
2704}
2705
2706/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002707 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002709 * Word WF_ONECAP
2710 * W WORD WF_ALLCAP
2711 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002712 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002713 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714captype(
2715 char_u *word,
2716 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717{
2718 char_u *p;
2719 int c;
2720 int firstcap;
2721 int allcap;
2722 int past_second = FALSE; /* past second word char */
2723
2724 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002725 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002726 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002727 return 0; /* only non-word characters, illegal word */
2728#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002729 if (has_mbyte)
2730 c = mb_ptr2char_adv(&p);
2731 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002732#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002733 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002734 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002735
2736 /*
2737 * Need to check all letters to find a word with mixed upper/lower.
2738 * But a word with an upper char only at start is a ONECAP.
2739 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002740 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002741 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002742 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002743 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002744 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002745 {
2746 /* UUl -> KEEPCAP */
2747 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002748 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002749 allcap = FALSE;
2750 }
2751 else if (!allcap)
2752 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002753 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002754 past_second = TRUE;
2755 }
2756
2757 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002758 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002759 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002760 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002761 return 0;
2762}
2763
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002764/*
2765 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2766 * capital. So that make_case_word() can turn WOrd into Word.
2767 * Add ALLCAP for "WOrD".
2768 */
2769 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002770badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002771{
2772 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002773 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002774 int l, u;
2775 int first;
2776 char_u *p;
2777
2778 if (flags & WF_KEEPCAP)
2779 {
2780 /* Count the number of UPPER and lower case letters. */
2781 l = u = 0;
2782 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002783 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002784 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002785 c = PTR2CHAR(p);
2786 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002787 {
2788 ++u;
2789 if (p == word)
2790 first = TRUE;
2791 }
2792 else
2793 ++l;
2794 }
2795
2796 /* If there are more UPPER than lower case letters suggest an
2797 * ALLCAP word. Otherwise, if the first letter is UPPER then
2798 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2799 * require three upper case letters. */
2800 if (u > l && u > 2)
2801 flags |= WF_ALLCAP;
2802 else if (first)
2803 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002804
2805 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2806 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002807 }
2808 return flags;
2809}
2810
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002811/*
2812 * Delete the internal wordlist and its .spl file.
2813 */
2814 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002815spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002816{
2817 char_u fname[MAXPATHL];
2818
2819 if (int_wordlist != NULL)
2820 {
2821 mch_remove(int_wordlist);
2822 int_wordlist_spl(fname);
2823 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002824 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002825 }
2826}
2827
2828#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002829/*
2830 * Free all languages.
2831 */
2832 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002833spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002834{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002835 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002836 buf_T *buf;
2837
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002838 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002839 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002840 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002841
2842 while (first_lang != NULL)
2843 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002844 slang = first_lang;
2845 first_lang = slang->sl_next;
2846 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002847 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002848
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002849 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002850
Bram Moolenaard23a8232018-02-10 18:45:26 +01002851 VIM_CLEAR(repl_to);
2852 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002853}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002854#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002855
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002856#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002857/*
2858 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002859 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002860 */
2861 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002862spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002863{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002864 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002865
Bram Moolenaarea408852005-06-25 22:49:46 +00002866 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002867 init_spell_chartab();
2868
2869 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002870 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002871
2872 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002873 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002874 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002875 /* Only load the wordlists when 'spelllang' is set and there is a
2876 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002877 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002878 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002880 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002881 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002882 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002883 }
2884 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002885 }
2886}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002887#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002888
Bram Moolenaarb765d632005-06-07 21:00:02 +00002889/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002890 * Opposite of offset2bytes().
2891 * "pp" points to the bytes and is advanced over it.
2892 * Returns the offset.
2893 */
2894 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002895bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002896{
2897 char_u *p = *pp;
2898 int nr;
2899 int c;
2900
2901 c = *p++;
2902 if ((c & 0x80) == 0x00) /* 1 byte */
2903 {
2904 nr = c - 1;
2905 }
2906 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2907 {
2908 nr = (c & 0x3f) - 1;
2909 nr = nr * 255 + (*p++ - 1);
2910 }
2911 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2912 {
2913 nr = (c & 0x1f) - 1;
2914 nr = nr * 255 + (*p++ - 1);
2915 nr = nr * 255 + (*p++ - 1);
2916 }
2917 else /* 4 bytes */
2918 {
2919 nr = (c & 0x0f) - 1;
2920 nr = nr * 255 + (*p++ - 1);
2921 nr = nr * 255 + (*p++ - 1);
2922 nr = nr * 255 + (*p++ - 1);
2923 }
2924
2925 *pp = p;
2926 return nr;
2927}
2928
Bram Moolenaar4770d092006-01-12 23:22:24 +00002929
2930/*
2931 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2932 * list and only contains text lines. Can use a swapfile to reduce memory
2933 * use.
2934 * Most other fields are invalid! Esp. watch out for string options being
2935 * NULL and there is no undo info.
2936 * Returns NULL when out of memory.
2937 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002938 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002939open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002940{
2941 buf_T *buf;
2942
2943 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2944 if (buf != NULL)
2945 {
2946 buf->b_spell = TRUE;
2947 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002948#ifdef FEAT_CRYPT
2949 buf->b_p_key = empty_option;
2950#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002951 ml_open(buf);
2952 ml_open_file(buf); /* create swap file now */
2953 }
2954 return buf;
2955}
2956
2957/*
2958 * Close the buffer used for spell info.
2959 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002960 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002961close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002962{
2963 if (buf != NULL)
2964 {
2965 ml_close(buf, TRUE);
2966 vim_free(buf);
2967 }
2968}
2969
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002970/*
2971 * Init the chartab used for spelling for ASCII.
2972 * EBCDIC is not supported!
2973 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002974 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002975clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002976{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002977 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002978
2979 /* Init everything to FALSE. */
2980 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2981 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2982 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002983 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002984 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002985 sp->st_upper[i] = i;
2986 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002987
2988 /* We include digits. A word shouldn't start with a digit, but handling
2989 * that is done separately. */
2990 for (i = '0'; i <= '9'; ++i)
2991 sp->st_isw[i] = TRUE;
2992 for (i = 'A'; i <= 'Z'; ++i)
2993 {
2994 sp->st_isw[i] = TRUE;
2995 sp->st_isu[i] = TRUE;
2996 sp->st_fold[i] = i + 0x20;
2997 }
2998 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002999 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003000 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003001 sp->st_upper[i] = i - 0x20;
3002 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003003}
3004
3005/*
3006 * Init the chartab used for spelling. Only depends on 'encoding'.
3007 * Called once while starting up and when 'encoding' changes.
3008 * The default is to use isalpha(), but the spell file should define the word
3009 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003010 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003011 */
3012 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003013init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003014{
3015 int i;
3016
3017 did_set_spelltab = FALSE;
3018 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003019#ifdef FEAT_MBYTE
3020 if (enc_dbcs)
3021 {
3022 /* DBCS: assume double-wide characters are word characters. */
3023 for (i = 128; i <= 255; ++i)
3024 if (MB_BYTE2LEN(i) == 2)
3025 spelltab.st_isw[i] = TRUE;
3026 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003027 else if (enc_utf8)
3028 {
3029 for (i = 128; i < 256; ++i)
3030 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003031 int f = utf_fold(i);
3032 int u = utf_toupper(i);
3033
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003034 spelltab.st_isu[i] = utf_isupper(i);
3035 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003036 /* The folded/upper-cased value is different between latin1 and
3037 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
3038 * value for utf-8 to avoid this. */
3039 spelltab.st_fold[i] = (f < 256) ? f : i;
3040 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003041 }
3042 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003043 else
3044#endif
3045 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003046 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003047 for (i = 128; i < 256; ++i)
3048 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003049 if (MB_ISUPPER(i))
3050 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003051 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003052 spelltab.st_isu[i] = TRUE;
3053 spelltab.st_fold[i] = MB_TOLOWER(i);
3054 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003055 else if (MB_ISLOWER(i))
3056 {
3057 spelltab.st_isw[i] = TRUE;
3058 spelltab.st_upper[i] = MB_TOUPPER(i);
3059 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003060 }
3061 }
3062}
3063
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003064
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003065/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003066 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003067 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003068 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003069 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003070 */
3071 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003072spell_iswordp(
3073 char_u *p,
3074 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003075{
Bram Moolenaarea408852005-06-25 22:49:46 +00003076#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003077 char_u *s;
3078 int l;
3079 int c;
3080
3081 if (has_mbyte)
3082 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003083 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003084 s = p;
3085 if (l == 1)
3086 {
3087 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003088 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003089 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003090 }
3091 else
3092 {
3093 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003094 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3095 : (wp->w_s->b_spell_ismw_mb != NULL
3096 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003097 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003098 }
3099
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003100 c = mb_ptr2char(s);
3101 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003102 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003103 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003104 }
Bram Moolenaarea408852005-06-25 22:49:46 +00003105#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003106
Bram Moolenaar860cae12010-06-05 23:22:07 +02003107 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003108}
3109
3110/*
3111 * Return TRUE if "p" points to a word character.
3112 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3113 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003114 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003115spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003116{
3117#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003118 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003119
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003120 if (has_mbyte)
3121 {
3122 c = mb_ptr2char(p);
3123 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003124 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003125 return spelltab.st_isw[c];
3126 }
3127#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003128 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003129}
3130
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003131#ifdef FEAT_MBYTE
3132/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003133 * Return TRUE if word class indicates a word character.
3134 * Only for characters above 255.
3135 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003136 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003137 */
3138 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003139spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003140{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003141 if (wp->w_s->b_cjk)
3142 /* East Asian characters are not considered word characters. */
3143 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003144 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3145}
3146
3147/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003148 * Return TRUE if "p" points to a word character.
3149 * Wide version of spell_iswordp().
3150 */
3151 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003152spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003153{
3154 int *s;
3155
Bram Moolenaar860cae12010-06-05 23:22:07 +02003156 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3157 : (wp->w_s->b_spell_ismw_mb != NULL
3158 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003159 s = p + 1;
3160 else
3161 s = p;
3162
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003163 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003164 {
3165 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003166 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003167 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003168 return spell_mb_isword_class(
3169 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003170 return 0;
3171 }
3172 return spelltab.st_isw[*s];
3173}
3174#endif
3175
Bram Moolenaarea408852005-06-25 22:49:46 +00003176/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003177 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3178 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003179 * When using a multi-byte 'encoding' the length may change!
3180 * Returns FAIL when something wrong.
3181 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003182 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003183spell_casefold(
3184 char_u *str,
3185 int len,
3186 char_u *buf,
3187 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003188{
3189 int i;
3190
3191 if (len >= buflen)
3192 {
3193 buf[0] = NUL;
3194 return FAIL; /* result will not fit */
3195 }
3196
3197#ifdef FEAT_MBYTE
3198 if (has_mbyte)
3199 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003200 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003201 char_u *p;
3202 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003203
3204 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003205 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003206 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003207 if (outi + MB_MAXBYTES > buflen)
3208 {
3209 buf[outi] = NUL;
3210 return FAIL;
3211 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003212 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003213 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003214 }
3215 buf[outi] = NUL;
3216 }
3217 else
3218#endif
3219 {
3220 /* Be quick for non-multibyte encodings. */
3221 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003222 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003223 buf[i] = NUL;
3224 }
3225
3226 return OK;
3227}
3228
Bram Moolenaar4770d092006-01-12 23:22:24 +00003229/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003230#define SPS_BEST 1
3231#define SPS_FAST 2
3232#define SPS_DOUBLE 4
3233
Bram Moolenaar4770d092006-01-12 23:22:24 +00003234static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3235static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003236
3237/*
3238 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003239 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003240 */
3241 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003242spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003243{
3244 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003245 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003246 char_u buf[MAXPATHL];
3247 int f;
3248
3249 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003250 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003251
3252 for (p = p_sps; *p != NUL; )
3253 {
3254 copy_option_part(&p, buf, MAXPATHL, ",");
3255
3256 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003257 if (VIM_ISDIGIT(*buf))
3258 {
3259 s = buf;
3260 sps_limit = getdigits(&s);
3261 if (*s != NUL && !VIM_ISDIGIT(*s))
3262 f = -1;
3263 }
3264 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003265 f = SPS_BEST;
3266 else if (STRCMP(buf, "fast") == 0)
3267 f = SPS_FAST;
3268 else if (STRCMP(buf, "double") == 0)
3269 f = SPS_DOUBLE;
3270 else if (STRNCMP(buf, "expr:", 5) != 0
3271 && STRNCMP(buf, "file:", 5) != 0)
3272 f = -1;
3273
3274 if (f == -1 || (sps_flags != 0 && f != 0))
3275 {
3276 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003277 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003278 return FAIL;
3279 }
3280 if (f != 0)
3281 sps_flags = f;
3282 }
3283
3284 if (sps_flags == 0)
3285 sps_flags = SPS_BEST;
3286
3287 return OK;
3288}
3289
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003290/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003291 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003292 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003293 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003294 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003295 */
3296 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003297spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003298{
3299 char_u *line;
3300 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003301 char_u wcopy[MAXWLEN + 2];
3302 char_u *p;
3303 int i;
3304 int c;
3305 suginfo_T sug;
3306 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003307 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003308 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003309 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003310 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003311 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003312 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003314 if (no_spell_checking(curwin))
3315 return;
3316
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003317 if (VIsual_active)
3318 {
3319 /* Use the Visually selected text as the bad word. But reject
3320 * a multi-line selection. */
3321 if (curwin->w_cursor.lnum != VIsual.lnum)
3322 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003323 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003324 return;
3325 }
3326 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3327 if (badlen < 0)
3328 badlen = -badlen;
3329 else
3330 curwin->w_cursor.col = VIsual.col;
3331 ++badlen;
3332 end_visual_mode();
3333 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003334 /* Find the start of the badly spelled word. */
3335 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003336 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003338 /* No bad word or it starts after the cursor: use the word under the
3339 * cursor. */
3340 curwin->w_cursor = prev_cursor;
3341 line = ml_get_curline();
3342 p = line + curwin->w_cursor.col;
3343 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003344 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003345 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003346 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003347 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003348 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003349
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003350 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003351 {
3352 beep_flush();
3353 return;
3354 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003355 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003356 }
3357
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003358 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003360 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003361 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003362
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003363 /* Make a copy of current line since autocommands may free the line. */
3364 line = vim_strsave(ml_get_curline());
3365 if (line == NULL)
3366 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003367
Bram Moolenaar5195e452005-08-19 20:32:47 +00003368 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3369 * 'spellsuggest', whatever is smaller. */
3370 if (sps_limit > (int)Rows - 2)
3371 limit = (int)Rows - 2;
3372 else
3373 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003374 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003375 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003376
3377 if (sug.su_ga.ga_len == 0)
3378 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003379 else if (count > 0)
3380 {
3381 if (count > sug.su_ga.ga_len)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003382 smsg(_("Sorry, only %ld suggestions"),
Bram Moolenaard12a1322005-08-21 22:08:24 +00003383 (long)sug.su_ga.ga_len);
3384 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003385 else
3386 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003387 VIM_CLEAR(repl_from);
3388 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003389
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003390#ifdef FEAT_RIGHTLEFT
3391 /* When 'rightleft' is set the list is drawn right-left. */
3392 cmdmsg_rl = curwin->w_p_rl;
3393 if (cmdmsg_rl)
3394 msg_col = Columns - 1;
3395#endif
3396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 /* List the suggestions. */
3398 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003399 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003400 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003401 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3402 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003403#ifdef FEAT_RIGHTLEFT
3404 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3405 {
3406 /* And now the rabbit from the high hat: Avoid showing the
3407 * untranslated message rightleft. */
3408 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3409 sug.su_badlen, sug.su_badptr);
3410 }
3411#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003412 msg_puts(IObuff);
3413 msg_clr_eos();
3414 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003416 msg_scroll = TRUE;
3417 for (i = 0; i < sug.su_ga.ga_len; ++i)
3418 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003419 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420
3421 /* The suggested word may replace only part of the bad word, add
3422 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003423 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003424 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003425 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 sug.su_badptr + stp->st_orglen,
3427 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003428 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3429#ifdef FEAT_RIGHTLEFT
3430 if (cmdmsg_rl)
3431 rl_mirror(IObuff);
3432#endif
3433 msg_puts(IObuff);
3434
3435 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003436 msg_puts(IObuff);
3437
3438 /* The word may replace more than "su_badlen". */
3439 if (sug.su_badlen < stp->st_orglen)
3440 {
3441 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3442 stp->st_orglen, sug.su_badptr);
3443 msg_puts(IObuff);
3444 }
3445
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003446 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003447 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003448 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003449 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003450 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003451 stp->st_salscore ? "s " : "",
3452 stp->st_score, stp->st_altscore);
3453 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003454 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003455 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003456#ifdef FEAT_RIGHTLEFT
3457 if (cmdmsg_rl)
3458 /* Mirror the numbers, but keep the leading space. */
3459 rl_mirror(IObuff + 1);
3460#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003461 msg_advance(30);
3462 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003464 msg_putchar('\n');
3465 }
3466
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003467#ifdef FEAT_RIGHTLEFT
3468 cmdmsg_rl = FALSE;
3469 msg_col = 0;
3470#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003471 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003472 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003473 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003474 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003475 lines_left = Rows; /* avoid more prompt */
3476 /* don't delay for 'smd' in normal_cmd() */
3477 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003478 }
3479
Bram Moolenaard12a1322005-08-21 22:08:24 +00003480 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3481 {
3482 /* Save the from and to text for :spellrepall. */
3483 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003484 if (sug.su_badlen > stp->st_orglen)
3485 {
3486 /* Replacing less than "su_badlen", append the remainder to
3487 * repl_to. */
3488 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3489 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3490 sug.su_badlen - stp->st_orglen,
3491 sug.su_badptr + stp->st_orglen);
3492 repl_to = vim_strsave(IObuff);
3493 }
3494 else
3495 {
3496 /* Replacing su_badlen or more, use the whole word. */
3497 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3498 repl_to = vim_strsave(stp->st_word);
3499 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003500
3501 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003502 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3503 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003504 if (p != NULL)
3505 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003506 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003507 mch_memmove(p, line, c);
3508 STRCPY(p + c, stp->st_word);
3509 STRCAT(p, sug.su_badptr + stp->st_orglen);
3510 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3511 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003512
3513 /* For redo we use a change-word command. */
3514 ResetRedobuff();
3515 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003516 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003517 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003518 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003519
3520 /* After this "p" may be invalid. */
3521 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003522 }
3523 }
3524 else
3525 curwin->w_cursor = prev_cursor;
3526
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003527 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003528skip:
3529 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003530}
3531
3532/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003533 * Check if the word at line "lnum" column "col" is required to start with a
3534 * capital. This uses 'spellcapcheck' of the current buffer.
3535 */
3536 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003537check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003538{
3539 int need_cap = FALSE;
3540 char_u *line;
3541 char_u *line_copy = NULL;
3542 char_u *p;
3543 colnr_T endcol;
3544 regmatch_T regmatch;
3545
Bram Moolenaar860cae12010-06-05 23:22:07 +02003546 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003547 return FALSE;
3548
3549 line = ml_get_curline();
3550 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003551 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003552 {
3553 /* At start of line, check if previous line is empty or sentence
3554 * ends there. */
3555 if (lnum == 1)
3556 need_cap = TRUE;
3557 else
3558 {
3559 line = ml_get(lnum - 1);
3560 if (*skipwhite(line) == NUL)
3561 need_cap = TRUE;
3562 else
3563 {
3564 /* Append a space in place of the line break. */
3565 line_copy = concat_str(line, (char_u *)" ");
3566 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003568 }
3569 }
3570 }
3571 else
3572 endcol = col;
3573
3574 if (endcol > 0)
3575 {
3576 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003577 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003578 regmatch.rm_ic = FALSE;
3579 p = line + endcol;
3580 for (;;)
3581 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003582 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003583 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003584 break;
3585 if (vim_regexec(&regmatch, p, 0)
3586 && regmatch.endp[0] == line + endcol)
3587 {
3588 need_cap = TRUE;
3589 break;
3590 }
3591 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003592 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003593 }
3594
3595 vim_free(line_copy);
3596
3597 return need_cap;
3598}
3599
3600
3601/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003602 * ":spellrepall"
3603 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003604 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003605ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003606{
3607 pos_T pos = curwin->w_cursor;
3608 char_u *frompat;
3609 int addlen;
3610 char_u *line;
3611 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003612 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003613 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003614
3615 if (repl_from == NULL || repl_to == NULL)
3616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003617 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003618 return;
3619 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003620 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003621
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003622 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003623 if (frompat == NULL)
3624 return;
3625 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3626 p_ws = FALSE;
3627
Bram Moolenaar5195e452005-08-19 20:32:47 +00003628 sub_nsubs = 0;
3629 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003630 curwin->w_cursor.lnum = 0;
3631 while (!got_int)
3632 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003633 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003634 || u_save_cursor() == FAIL)
3635 break;
3636
3637 /* Only replace when the right word isn't there yet. This happens
3638 * when changing "etc" to "etc.". */
3639 line = ml_get_curline();
3640 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3641 repl_to, STRLEN(repl_to)) != 0)
3642 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003643 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003644 if (p == NULL)
3645 break;
3646 mch_memmove(p, line, curwin->w_cursor.col);
3647 STRCPY(p + curwin->w_cursor.col, repl_to);
3648 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3649 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3650 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003651
3652 if (curwin->w_cursor.lnum != prev_lnum)
3653 {
3654 ++sub_nlines;
3655 prev_lnum = curwin->w_cursor.lnum;
3656 }
3657 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003658 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003659 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003660 }
3661
3662 p_ws = save_ws;
3663 curwin->w_cursor = pos;
3664 vim_free(frompat);
3665
Bram Moolenaar5195e452005-08-19 20:32:47 +00003666 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003667 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003668 else
3669 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003670}
3671
3672/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003673 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3674 * a list of allocated strings.
3675 */
3676 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003677spell_suggest_list(
3678 garray_T *gap,
3679 char_u *word,
3680 int maxcount, /* maximum nr of suggestions */
3681 int need_cap, /* 'spellcapcheck' matched */
3682 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003683{
3684 suginfo_T sug;
3685 int i;
3686 suggest_T *stp;
3687 char_u *wcopy;
3688
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003689 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003690
3691 /* Make room in "gap". */
3692 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003693 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003694 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003695 for (i = 0; i < sug.su_ga.ga_len; ++i)
3696 {
3697 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003698
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003699 /* The suggested word may replace only part of "word", add the not
3700 * replaced part. */
3701 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003702 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003703 if (wcopy == NULL)
3704 break;
3705 STRCPY(wcopy, stp->st_word);
3706 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3707 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3708 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003709 }
3710
3711 spell_find_cleanup(&sug);
3712}
3713
3714/*
3715 * Find spell suggestions for the word at the start of "badptr".
3716 * Return the suggestions in "su->su_ga".
3717 * The maximum number of suggestions is "maxcount".
3718 * Note: does use info for the current window.
3719 * This is based on the mechanisms of Aspell, but completely reimplemented.
3720 */
3721 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003722spell_find_suggest(
3723 char_u *badptr,
3724 int badlen, /* length of bad word or 0 if unknown */
3725 suginfo_T *su,
3726 int maxcount,
3727 int banbadword, /* don't include badword in suggestions */
3728 int need_cap, /* word should start with capital */
3729 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003730{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003731 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003732 char_u buf[MAXPATHL];
3733 char_u *p;
3734 int do_combine = FALSE;
3735 char_u *sps_copy;
3736#ifdef FEAT_EVAL
3737 static int expr_busy = FALSE;
3738#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003739 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003740 int i;
3741 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003742
3743 /*
3744 * Set the info in "*su".
3745 */
3746 vim_memset(su, 0, sizeof(suginfo_T));
3747 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3748 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003749 if (*badptr == NUL)
3750 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003751 hash_init(&su->su_banned);
3752
3753 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003754 if (badlen != 0)
3755 su->su_badlen = badlen;
3756 else
3757 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003758 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003759 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003760
3761 if (su->su_badlen >= MAXWLEN)
3762 su->su_badlen = MAXWLEN - 1; /* just in case */
3763 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3764 (void)spell_casefold(su->su_badptr, su->su_badlen,
3765 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003766 /* TODO: make this work if the case-folded text is longer than the original
3767 * text. Currently an illegal byte causes wrong pointer computations. */
3768 su->su_fbadword[su->su_badlen] = NUL;
3769
Bram Moolenaar0c405862005-06-22 22:26:26 +00003770 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003771 su->su_badflags = badword_captype(su->su_badptr,
3772 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003773 if (need_cap)
3774 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003775
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003776 /* Find the default language for sound folding. We simply use the first
3777 * one in 'spelllang' that supports sound folding. That's good for when
3778 * using multiple files for one language, it's not that bad when mixing
3779 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003780 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003781 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003782 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003783 if (lp->lp_sallang != NULL)
3784 {
3785 su->su_sallang = lp->lp_sallang;
3786 break;
3787 }
3788 }
3789
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003790 /* Soundfold the bad word with the default sound folding, so that we don't
3791 * have to do this many times. */
3792 if (su->su_sallang != NULL)
3793 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3794 su->su_sal_badword);
3795
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003796 /* If the word is not capitalised and spell_check() doesn't consider the
3797 * word to be bad then it might need to be capitalised. Add a suggestion
3798 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003799 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003800 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003801 {
3802 make_case_word(su->su_badword, buf, WF_ONECAP);
3803 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003804 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003805 }
3806
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003807 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003808 if (banbadword)
3809 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003810
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003811 /* Make a copy of 'spellsuggest', because the expression may change it. */
3812 sps_copy = vim_strsave(p_sps);
3813 if (sps_copy == NULL)
3814 return;
3815
3816 /* Loop over the items in 'spellsuggest'. */
3817 for (p = sps_copy; *p != NUL; )
3818 {
3819 copy_option_part(&p, buf, MAXPATHL, ",");
3820
3821 if (STRNCMP(buf, "expr:", 5) == 0)
3822 {
3823#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003824 /* Evaluate an expression. Skip this when called recursively,
3825 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003826 if (!expr_busy)
3827 {
3828 expr_busy = TRUE;
3829 spell_suggest_expr(su, buf + 5);
3830 expr_busy = FALSE;
3831 }
3832#endif
3833 }
3834 else if (STRNCMP(buf, "file:", 5) == 0)
3835 /* Use list of suggestions in a file. */
3836 spell_suggest_file(su, buf + 5);
3837 else
3838 {
3839 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003840 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003841 if (sps_flags & SPS_DOUBLE)
3842 do_combine = TRUE;
3843 }
3844 }
3845
3846 vim_free(sps_copy);
3847
3848 if (do_combine)
3849 /* Combine the two list of suggestions. This must be done last,
3850 * because sorting changes the order again. */
3851 score_combine(su);
3852}
3853
3854#ifdef FEAT_EVAL
3855/*
3856 * Find suggestions by evaluating expression "expr".
3857 */
3858 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003859spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003860{
3861 list_T *list;
3862 listitem_T *li;
3863 int score;
3864 char_u *p;
3865
3866 /* The work is split up in a few parts to avoid having to export
3867 * suginfo_T.
3868 * First evaluate the expression and get the resulting list. */
3869 list = eval_spell_expr(su->su_badword, expr);
3870 if (list != NULL)
3871 {
3872 /* Loop over the items in the list. */
3873 for (li = list->lv_first; li != NULL; li = li->li_next)
3874 if (li->li_tv.v_type == VAR_LIST)
3875 {
3876 /* Get the word and the score from the items. */
3877 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003878 if (score >= 0 && score <= su->su_maxscore)
3879 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3880 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003881 }
3882 list_unref(list);
3883 }
3884
Bram Moolenaar4770d092006-01-12 23:22:24 +00003885 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3886 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003887 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3888}
3889#endif
3890
3891/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003892 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003893 */
3894 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003895spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003896{
3897 FILE *fd;
3898 char_u line[MAXWLEN * 2];
3899 char_u *p;
3900 int len;
3901 char_u cword[MAXWLEN];
3902
3903 /* Open the file. */
3904 fd = mch_fopen((char *)fname, "r");
3905 if (fd == NULL)
3906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003907 semsg(_(e_notopen), fname);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003908 return;
3909 }
3910
3911 /* Read it line by line. */
3912 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3913 {
3914 line_breakcheck();
3915
3916 p = vim_strchr(line, '/');
3917 if (p == NULL)
3918 continue; /* No Tab found, just skip the line. */
3919 *p++ = NUL;
3920 if (STRICMP(su->su_badword, line) == 0)
3921 {
3922 /* Match! Isolate the good word, until CR or NL. */
3923 for (len = 0; p[len] >= ' '; ++len)
3924 ;
3925 p[len] = NUL;
3926
3927 /* If the suggestion doesn't have specific case duplicate the case
3928 * of the bad word. */
3929 if (captype(p, NULL) == 0)
3930 {
3931 make_case_word(p, cword, su->su_badflags);
3932 p = cword;
3933 }
3934
3935 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003936 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003937 }
3938 }
3939
3940 fclose(fd);
3941
Bram Moolenaar4770d092006-01-12 23:22:24 +00003942 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3943 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003944 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3945}
3946
3947/*
3948 * Find suggestions for the internal method indicated by "sps_flags".
3949 */
3950 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003951spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003952{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003953 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003954 * Load the .sug file(s) that are available and not done yet.
3955 */
3956 suggest_load_files();
3957
3958 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003959 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003960 *
3961 * Set a maximum score to limit the combination of operations that is
3962 * tried.
3963 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003964 suggest_try_special(su);
3965
3966 /*
3967 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3968 * from the .aff file and inserting a space (split the word).
3969 */
3970 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003971
3972 /* For the resulting top-scorers compute the sound-a-like score. */
3973 if (sps_flags & SPS_DOUBLE)
3974 score_comp_sal(su);
3975
3976 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003977 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003978 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003979 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003980 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00003981 if (sps_flags & SPS_BEST)
3982 /* Adjust the word score for the suggestions found so far for how
3983 * they sounds like. */
3984 rescore_suggestions(su);
3985
3986 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003987 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00003988 * for the soundfold word, limits the changes that are being tried,
3989 * and "su_sfmaxscore" the rescored score, which is set by
3990 * cleanup_suggestions().
3991 * First find words with a small edit distance, because this is much
3992 * faster and often already finds the top-N suggestions. If we didn't
3993 * find many suggestions try again with a higher edit distance.
3994 * "sl_sounddone" is used to avoid doing the same word twice.
3995 */
3996 suggest_try_soundalike_prep();
3997 su->su_maxscore = SCORE_SFMAX1;
3998 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003999 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004000 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4001 {
4002 /* We didn't find enough matches, try again, allowing more
4003 * changes to the soundfold word. */
4004 su->su_maxscore = SCORE_SFMAX2;
4005 suggest_try_soundalike(su);
4006 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4007 {
4008 /* Still didn't find enough matches, try again, allowing even
4009 * more changes to the soundfold word. */
4010 su->su_maxscore = SCORE_SFMAX3;
4011 suggest_try_soundalike(su);
4012 }
4013 }
4014 su->su_maxscore = su->su_sfmaxscore;
4015 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004016 }
4017
Bram Moolenaar4770d092006-01-12 23:22:24 +00004018 /* When CTRL-C was hit while searching do show the results. Only clear
4019 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004020 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00004021 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004022 {
4023 (void)vgetc();
4024 got_int = FALSE;
4025 }
4026
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004027 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004028 {
4029 if (sps_flags & SPS_BEST)
4030 /* Adjust the word score for how it sounds like. */
4031 rescore_suggestions(su);
4032
Bram Moolenaar4770d092006-01-12 23:22:24 +00004033 /* Remove bogus suggestions, sort and truncate at "maxcount". */
4034 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004035 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004036 }
4037}
4038
4039/*
4040 * Free the info put in "*su" by spell_find_suggest().
4041 */
4042 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004043spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004044{
4045 int i;
4046
4047 /* Free the suggestions. */
4048 for (i = 0; i < su->su_ga.ga_len; ++i)
4049 vim_free(SUG(su->su_ga, i).st_word);
4050 ga_clear(&su->su_ga);
4051 for (i = 0; i < su->su_sga.ga_len; ++i)
4052 vim_free(SUG(su->su_sga, i).st_word);
4053 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004054
4055 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004056 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004057}
4058
4059/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004060 * Make a copy of "word", with the first letter upper or lower cased, to
4061 * "wcopy[MAXWLEN]". "word" must not be empty.
4062 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004064 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004065onecap_copy(
4066 char_u *word,
4067 char_u *wcopy,
4068 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004069{
4070 char_u *p;
4071 int c;
4072 int l;
4073
4074 p = word;
4075#ifdef FEAT_MBYTE
4076 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004077 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078 else
4079#endif
4080 c = *p++;
4081 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004082 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004084 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085#ifdef FEAT_MBYTE
4086 if (has_mbyte)
4087 l = mb_char2bytes(c, wcopy);
4088 else
4089#endif
4090 {
4091 l = 1;
4092 wcopy[0] = c;
4093 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004094 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095}
4096
4097/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004098 * Make a copy of "word" with all the letters upper cased into
4099 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 */
4101 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004102allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103{
4104 char_u *s;
4105 char_u *d;
4106 int c;
4107
4108 d = wcopy;
4109 for (s = word; *s != NUL; )
4110 {
4111#ifdef FEAT_MBYTE
4112 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004113 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 else
4115#endif
4116 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004117
4118#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +02004119 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004120 * would cause weird errors in other 8-bit encodings. */
4121 if (enc_latin1like && c == 0xdf)
4122 {
4123 c = 'S';
4124 if (d - wcopy >= MAXWLEN - 1)
4125 break;
4126 *d++ = c;
4127 }
4128 else
4129#endif
4130 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131
4132#ifdef FEAT_MBYTE
4133 if (has_mbyte)
4134 {
4135 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4136 break;
4137 d += mb_char2bytes(c, d);
4138 }
4139 else
4140#endif
4141 {
4142 if (d - wcopy >= MAXWLEN - 1)
4143 break;
4144 *d++ = c;
4145 }
4146 }
4147 *d = NUL;
4148}
4149
4150/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004151 * Try finding suggestions by recognizing specific situations.
4152 */
4153 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004154suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004155{
4156 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004157 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004158 int c;
4159 char_u word[MAXWLEN];
4160
4161 /*
4162 * Recognize a word that is repeated: "the the".
4163 */
4164 p = skiptowhite(su->su_fbadword);
4165 len = p - su->su_fbadword;
4166 p = skipwhite(p);
4167 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4168 {
4169 /* Include badflags: if the badword is onecap or allcap
4170 * use that for the goodword too: "The the" -> "The". */
4171 c = su->su_fbadword[len];
4172 su->su_fbadword[len] = NUL;
4173 make_case_word(su->su_fbadword, word, su->su_badflags);
4174 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004175
4176 /* Give a soundalike score of 0, compute the score as if deleting one
4177 * character. */
4178 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004179 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004180 }
4181}
4182
4183/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004184 * Change the 0 to 1 to measure how much time is spent in each state.
4185 * Output is dumped in "suggestprof".
4186 */
4187#if 0
4188# define SUGGEST_PROFILE
4189proftime_T current;
4190proftime_T total;
4191proftime_T times[STATE_FINAL + 1];
4192long counts[STATE_FINAL + 1];
4193
4194 static void
4195prof_init(void)
4196{
4197 for (int i = 0; i <= STATE_FINAL; ++i)
4198 {
4199 profile_zero(&times[i]);
4200 counts[i] = 0;
4201 }
4202 profile_start(&current);
4203 profile_start(&total);
4204}
4205
4206/* call before changing state */
4207 static void
4208prof_store(state_T state)
4209{
4210 profile_end(&current);
4211 profile_add(&times[state], &current);
4212 ++counts[state];
4213 profile_start(&current);
4214}
4215# define PROF_STORE(state) prof_store(state);
4216
4217 static void
4218prof_report(char *name)
4219{
4220 FILE *fd = fopen("suggestprof", "a");
4221
4222 profile_end(&total);
4223 fprintf(fd, "-----------------------\n");
4224 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4225 for (int i = 0; i <= STATE_FINAL; ++i)
4226 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4227 fclose(fd);
4228}
4229#else
4230# define PROF_STORE(state)
4231#endif
4232
4233/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 * Try finding suggestions by adding/removing/swapping letters.
4235 */
4236 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004237suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238{
4239 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004240 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004242 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004243 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244
4245 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004246 * to find matches (esp. REP items). Append some more text, changing
4247 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004248 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004249 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004250 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004251 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252
Bram Moolenaar860cae12010-06-05 23:22:07 +02004253 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004255 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004256
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004257 /* If reloading a spell file fails it's still in the list but
4258 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004259 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004260 continue;
4261
Bram Moolenaar4770d092006-01-12 23:22:24 +00004262 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004263#ifdef SUGGEST_PROFILE
4264 prof_init();
4265#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004266 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004267#ifdef SUGGEST_PROFILE
4268 prof_report("try_change");
4269#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004270 }
4271}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272
Bram Moolenaar4770d092006-01-12 23:22:24 +00004273/* Check the maximum score, if we go over it we won't try this change. */
4274#define TRY_DEEPER(su, stack, depth, add) \
4275 (stack[depth].ts_score + (add) < su->su_maxscore)
4276
4277/*
4278 * Try finding suggestions by adding/removing/swapping letters.
4279 *
4280 * This uses a state machine. At each node in the tree we try various
4281 * operations. When trying if an operation works "depth" is increased and the
4282 * stack[] is used to store info. This allows combinations, thus insert one
4283 * character, replace one and delete another. The number of changes is
4284 * limited by su->su_maxscore.
4285 *
4286 * After implementing this I noticed an article by Kemal Oflazer that
4287 * describes something similar: "Error-tolerant Finite State Recognition with
4288 * Applications to Morphological Analysis and Spelling Correction" (1996).
4289 * The implementation in the article is simplified and requires a stack of
4290 * unknown depth. The implementation here only needs a stack depth equal to
4291 * the length of the word.
4292 *
4293 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4294 * The mechanism is the same, but we find a match with a sound-folded word
4295 * that comes from one or more original words. Each of these words may be
4296 * added, this is done by add_sound_suggest().
4297 * Don't use:
4298 * the prefix tree or the keep-case tree
4299 * "su->su_badlen"
4300 * anything to do with upper and lower case
4301 * anything to do with word or non-word characters ("spell_iswordp()")
4302 * banned words
4303 * word flags (rare, region, compounding)
4304 * word splitting for now
4305 * "similar_chars()"
4306 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4307 */
4308 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004309suggest_trie_walk(
4310 suginfo_T *su,
4311 langp_T *lp,
4312 char_u *fword,
4313 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004314{
4315 char_u tword[MAXWLEN]; /* good word collected so far */
4316 trystate_T stack[MAXWLEN];
4317 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004318 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004319 * words and split word. NUL terminated
4320 * when going deeper but not when coming
4321 * back. */
4322 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4323 trystate_T *sp;
4324 int newscore;
4325 int score;
4326 char_u *byts, *fbyts, *pbyts;
4327 idx_T *idxs, *fidxs, *pidxs;
4328 int depth;
4329 int c, c2, c3;
4330 int n = 0;
4331 int flags;
4332 garray_T *gap;
4333 idx_T arridx;
4334 int len;
4335 char_u *p;
4336 fromto_T *ftp;
4337 int fl = 0, tl;
4338 int repextra = 0; /* extra bytes in fword[] from REP item */
4339 slang_T *slang = lp->lp_slang;
4340 int fword_ends;
4341 int goodword_ends;
4342#ifdef DEBUG_TRIEWALK
4343 /* Stores the name of the change made at each level. */
4344 char_u changename[MAXWLEN][80];
4345#endif
4346 int breakcheckcount = 1000;
4347 int compound_ok;
4348
4349 /*
4350 * Go through the whole case-fold tree, try changes at each node.
4351 * "tword[]" contains the word collected from nodes in the tree.
4352 * "fword[]" the word we are trying to match with (initially the bad
4353 * word).
4354 */
4355 depth = 0;
4356 sp = &stack[0];
4357 vim_memset(sp, 0, sizeof(trystate_T));
4358 sp->ts_curi = 1;
4359
4360 if (soundfold)
4361 {
4362 /* Going through the soundfold tree. */
4363 byts = fbyts = slang->sl_sbyts;
4364 idxs = fidxs = slang->sl_sidxs;
4365 pbyts = NULL;
4366 pidxs = NULL;
4367 sp->ts_prefixdepth = PFD_NOPREFIX;
4368 sp->ts_state = STATE_START;
4369 }
4370 else
4371 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004372 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004373 * When there are postponed prefixes we need to use these first. At
4374 * the end of the prefix we continue in the case-fold tree.
4375 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004376 fbyts = slang->sl_fbyts;
4377 fidxs = slang->sl_fidxs;
4378 pbyts = slang->sl_pbyts;
4379 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004380 if (pbyts != NULL)
4381 {
4382 byts = pbyts;
4383 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004384 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004385 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4386 }
4387 else
4388 {
4389 byts = fbyts;
4390 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004391 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004392 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004393 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004394 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004395
Bram Moolenaar4770d092006-01-12 23:22:24 +00004396 /*
4397 * Loop to find all suggestions. At each round we either:
4398 * - For the current state try one operation, advance "ts_curi",
4399 * increase "depth".
4400 * - When a state is done go to the next, set "ts_state".
4401 * - When all states are tried decrease "depth".
4402 */
4403 while (depth >= 0 && !got_int)
4404 {
4405 sp = &stack[depth];
4406 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004408 case STATE_START:
4409 case STATE_NOPREFIX:
4410 /*
4411 * Start of node: Deal with NUL bytes, which means
4412 * tword[] may end here.
4413 */
4414 arridx = sp->ts_arridx; /* current node in the tree */
4415 len = byts[arridx]; /* bytes in this node */
4416 arridx += sp->ts_curi; /* index of current byte */
4417
4418 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004419 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004420 /* Skip over the NUL bytes, we use them later. */
4421 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4422 ;
4423 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424
Bram Moolenaar4770d092006-01-12 23:22:24 +00004425 /* Always past NUL bytes now. */
4426 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004427 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004428 sp->ts_state = STATE_ENDNUL;
4429 sp->ts_save_badflags = su->su_badflags;
4430
4431 /* At end of a prefix or at start of prefixtree: check for
4432 * following word. */
4433 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004434 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004435 /* Set su->su_badflags to the caps type at this position.
4436 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004437#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004438 if (has_mbyte)
4439 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4440 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004441#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004442 n = sp->ts_fidx;
4443 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4444 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004445 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004446#ifdef DEBUG_TRIEWALK
4447 sprintf(changename[depth], "prefix");
4448#endif
4449 go_deeper(stack, depth, 0);
4450 ++depth;
4451 sp = &stack[depth];
4452 sp->ts_prefixdepth = depth - 1;
4453 byts = fbyts;
4454 idxs = fidxs;
4455 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004456
Bram Moolenaar4770d092006-01-12 23:22:24 +00004457 /* Move the prefix to preword[] with the right case
4458 * and make find_keepcap_word() works. */
4459 tword[sp->ts_twordlen] = NUL;
4460 make_case_word(tword + sp->ts_splitoff,
4461 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004462 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004463 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004464 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004465 break;
4466 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004467
Bram Moolenaar4770d092006-01-12 23:22:24 +00004468 if (sp->ts_curi > len || byts[arridx] != 0)
4469 {
4470 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004471 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004472 sp->ts_state = STATE_ENDNUL;
4473 sp->ts_save_badflags = su->su_badflags;
4474 break;
4475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004476
Bram Moolenaar4770d092006-01-12 23:22:24 +00004477 /*
4478 * End of word in tree.
4479 */
4480 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481
Bram Moolenaar4770d092006-01-12 23:22:24 +00004482 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004483
4484 /* Skip words with the NOSUGGEST flag. */
4485 if (flags & WF_NOSUGGEST)
4486 break;
4487
Bram Moolenaar4770d092006-01-12 23:22:24 +00004488 fword_ends = (fword[sp->ts_fidx] == NUL
4489 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004490 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004491 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004492 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004493
Bram Moolenaar4770d092006-01-12 23:22:24 +00004494 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004495 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004496 {
4497 /* There was a prefix before the word. Check that the prefix
4498 * can be used with this word. */
4499 /* Count the length of the NULs in the prefix. If there are
4500 * none this must be the first try without a prefix. */
4501 n = stack[sp->ts_prefixdepth].ts_arridx;
4502 len = pbyts[n++];
4503 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4504 ;
4505 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004506 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004507 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004508 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004509 if (c == 0)
4510 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004511
Bram Moolenaar4770d092006-01-12 23:22:24 +00004512 /* Use the WF_RARE flag for a rare prefix. */
4513 if (c & WF_RAREPFX)
4514 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004515
Bram Moolenaar4770d092006-01-12 23:22:24 +00004516 /* Tricky: when checking for both prefix and compounding
4517 * we run into the prefix flag first.
4518 * Remember that it's OK, so that we accept the prefix
4519 * when arriving at a compound flag. */
4520 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004521 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004522 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004523
Bram Moolenaar4770d092006-01-12 23:22:24 +00004524 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4525 * appending another compound word below. */
4526 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004527 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004528 goodword_ends = FALSE;
4529 else
4530 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004531
Bram Moolenaar4770d092006-01-12 23:22:24 +00004532 p = NULL;
4533 compound_ok = TRUE;
4534 if (sp->ts_complen > sp->ts_compsplit)
4535 {
4536 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004537 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004538 /* There was a word before this word. When there was no
4539 * change in this word (it was correct) add the first word
4540 * as a suggestion. If this word was corrected too, we
4541 * need to check if a correct word follows. */
4542 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004543 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004544 && STRNCMP(fword + sp->ts_splitfidx,
4545 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004546 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004547 {
4548 preword[sp->ts_prewordlen] = NUL;
4549 newscore = score_wordcount_adj(slang, sp->ts_score,
4550 preword + sp->ts_prewordlen,
4551 sp->ts_prewordlen > 0);
4552 /* Add the suggestion if the score isn't too bad. */
4553 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004554 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004555 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004556 newscore, 0, FALSE,
4557 lp->lp_sallang, FALSE);
4558 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004559 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004560 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004561 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004562 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004563 /* There was a compound word before this word. If this
4564 * word does not support compounding then give up
4565 * (splitting is tried for the word without compound
4566 * flag). */
4567 if (((unsigned)flags >> 24) == 0
4568 || sp->ts_twordlen - sp->ts_splitoff
4569 < slang->sl_compminlen)
4570 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004571#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004572 /* For multi-byte chars check character length against
4573 * COMPOUNDMIN. */
4574 if (has_mbyte
4575 && slang->sl_compminlen > 0
4576 && mb_charlen(tword + sp->ts_splitoff)
4577 < slang->sl_compminlen)
4578 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004579#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00004580
Bram Moolenaar4770d092006-01-12 23:22:24 +00004581 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4582 compflags[sp->ts_complen + 1] = NUL;
4583 vim_strncpy(preword + sp->ts_prewordlen,
4584 tword + sp->ts_splitoff,
4585 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004586
4587 /* Verify CHECKCOMPOUNDPATTERN rules. */
4588 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4589 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004590 compound_ok = FALSE;
4591
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004592 if (compound_ok)
4593 {
4594 p = preword;
4595 while (*skiptowhite(p) != NUL)
4596 p = skipwhite(skiptowhite(p));
4597 if (fword_ends && !can_compound(slang, p,
4598 compflags + sp->ts_compsplit))
4599 /* Compound is not allowed. But it may still be
4600 * possible if we add another (short) word. */
4601 compound_ok = FALSE;
4602 }
4603
Bram Moolenaar4770d092006-01-12 23:22:24 +00004604 /* Get pointer to last char of previous word. */
4605 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004606 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004607 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004609
Bram Moolenaar4770d092006-01-12 23:22:24 +00004610 /*
4611 * Form the word with proper case in preword.
4612 * If there is a word from a previous split, append.
4613 * For the soundfold tree don't change the case, simply append.
4614 */
4615 if (soundfold)
4616 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4617 else if (flags & WF_KEEPCAP)
4618 /* Must find the word in the keep-case tree. */
4619 find_keepcap_word(slang, tword + sp->ts_splitoff,
4620 preword + sp->ts_prewordlen);
4621 else
4622 {
4623 /* Include badflags: If the badword is onecap or allcap
4624 * use that for the goodword too. But if the badword is
4625 * allcap and it's only one char long use onecap. */
4626 c = su->su_badflags;
4627 if ((c & WF_ALLCAP)
4628#ifdef FEAT_MBYTE
4629 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
4630#else
4631 && su->su_badlen == 1
4632#endif
4633 )
4634 c = WF_ONECAP;
4635 c |= flags;
4636
4637 /* When appending a compound word after a word character don't
4638 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004639 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004640 c &= ~WF_ONECAP;
4641 make_case_word(tword + sp->ts_splitoff,
4642 preword + sp->ts_prewordlen, c);
4643 }
4644
4645 if (!soundfold)
4646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 /* Don't use a banned word. It may appear again as a good
4648 * word, thus remember it. */
4649 if (flags & WF_BANNED)
4650 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004651 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 break;
4653 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004654 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004655 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4656 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004657 {
4658 if (slang->sl_compprog == NULL)
4659 break;
4660 /* the word so far was banned but we may try compounding */
4661 goodword_ends = FALSE;
4662 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004663 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004664
Bram Moolenaar4770d092006-01-12 23:22:24 +00004665 newscore = 0;
4666 if (!soundfold) /* soundfold words don't have flags */
4667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004669 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 newscore += SCORE_REGION;
4671 if (flags & WF_RARE)
4672 newscore += SCORE_RARE;
4673
Bram Moolenaar0c405862005-06-22 22:26:26 +00004674 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004675 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004676 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004677 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004678
Bram Moolenaar4770d092006-01-12 23:22:24 +00004679 /* TODO: how about splitting in the soundfold tree? */
4680 if (fword_ends
4681 && goodword_ends
4682 && sp->ts_fidx >= sp->ts_fidxtry
4683 && compound_ok)
4684 {
4685 /* The badword also ends: add suggestions. */
4686#ifdef DEBUG_TRIEWALK
4687 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004689 int j;
4690
4691 /* print the stack of changes that brought us here */
4692 smsg("------ %s -------", fword);
4693 for (j = 0; j < depth; ++j)
4694 smsg("%s", changename[j]);
4695 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004696#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004697 if (soundfold)
4698 {
4699 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004700 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004701 add_sound_suggest(su, preword, sp->ts_score, lp);
4702 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004703 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004704 {
4705 /* Give a penalty when changing non-word char to word
4706 * char, e.g., "thes," -> "these". */
4707 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004708 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004709 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004710 {
4711 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004712 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004713 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004714 newscore += SCORE_NONWORD;
4715 }
4716
Bram Moolenaar4770d092006-01-12 23:22:24 +00004717 /* Give a bonus to words seen before. */
4718 score = score_wordcount_adj(slang,
4719 sp->ts_score + newscore,
4720 preword + sp->ts_prewordlen,
4721 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004722
Bram Moolenaar4770d092006-01-12 23:22:24 +00004723 /* Add the suggestion if the score isn't too bad. */
4724 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004725 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004726 add_suggestion(su, &su->su_ga, preword,
4727 sp->ts_fidx - repextra,
4728 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004729
4730 if (su->su_badflags & WF_MIXCAP)
4731 {
4732 /* We really don't know if the word should be
4733 * upper or lower case, add both. */
4734 c = captype(preword, NULL);
4735 if (c == 0 || c == WF_ALLCAP)
4736 {
4737 make_case_word(tword + sp->ts_splitoff,
4738 preword + sp->ts_prewordlen,
4739 c == 0 ? WF_ALLCAP : 0);
4740
4741 add_suggestion(su, &su->su_ga, preword,
4742 sp->ts_fidx - repextra,
4743 score + SCORE_ICASE, 0, FALSE,
4744 lp->lp_sallang, FALSE);
4745 }
4746 }
4747 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004749 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004750
Bram Moolenaar4770d092006-01-12 23:22:24 +00004751 /*
4752 * Try word split and/or compounding.
4753 */
4754 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00004755#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004756 /* Don't split halfway a character. */
4757 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004758#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004759 )
4760 {
4761 int try_compound;
4762 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004763
Bram Moolenaar4770d092006-01-12 23:22:24 +00004764 /* If past the end of the bad word don't try a split.
4765 * Otherwise try changing the next word. E.g., find
4766 * suggestions for "the the" where the second "the" is
4767 * different. It's done like a split.
4768 * TODO: word split for soundfold words */
4769 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4770 && !soundfold;
4771
4772 /* Get here in several situations:
4773 * 1. The word in the tree ends:
4774 * If the word allows compounding try that. Otherwise try
4775 * a split by inserting a space. For both check that a
4776 * valid words starts at fword[sp->ts_fidx].
4777 * For NOBREAK do like compounding to be able to check if
4778 * the next word is valid.
4779 * 2. The badword does end, but it was due to a change (e.g.,
4780 * a swap). No need to split, but do check that the
4781 * following word is valid.
4782 * 3. The badword and the word in the tree end. It may still
4783 * be possible to compound another (short) word.
4784 */
4785 try_compound = FALSE;
4786 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004787 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004788 && slang->sl_compprog != NULL
4789 && ((unsigned)flags >> 24) != 0
4790 && sp->ts_twordlen - sp->ts_splitoff
4791 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004792#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004793 && (!has_mbyte
4794 || slang->sl_compminlen == 0
4795 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004796 >= slang->sl_compminlen)
4797#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004798 && (slang->sl_compsylmax < MAXWLEN
4799 || sp->ts_complen + 1 - sp->ts_compsplit
4800 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004801 && (can_be_compound(sp, slang,
4802 compflags, ((unsigned)flags >> 24))))
4803
Bram Moolenaar4770d092006-01-12 23:22:24 +00004804 {
4805 try_compound = TRUE;
4806 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4807 compflags[sp->ts_complen + 1] = NUL;
4808 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004809
Bram Moolenaar4770d092006-01-12 23:22:24 +00004810 /* For NOBREAK we never try splitting, it won't make any word
4811 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004812 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004813 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004814
Bram Moolenaar4770d092006-01-12 23:22:24 +00004815 /* If we could add a compound word, and it's also possible to
4816 * split at this point, do the split first and set
4817 * TSF_DIDSPLIT to avoid doing it again. */
4818 else if (!fword_ends
4819 && try_compound
4820 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4821 {
4822 try_compound = FALSE;
4823 sp->ts_flags |= TSF_DIDSPLIT;
4824 --sp->ts_curi; /* do the same NUL again */
4825 compflags[sp->ts_complen] = NUL;
4826 }
4827 else
4828 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004829
Bram Moolenaar4770d092006-01-12 23:22:24 +00004830 if (try_split || try_compound)
4831 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004832 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004833 {
4834 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004835 * words so far are valid for compounding. If there
4836 * is only one word it must not have the NEEDCOMPOUND
4837 * flag. */
4838 if (sp->ts_complen == sp->ts_compsplit
4839 && (flags & WF_NEEDCOMP))
4840 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004841 p = preword;
4842 while (*skiptowhite(p) != NUL)
4843 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004844 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004845 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004846 compflags + sp->ts_compsplit))
4847 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004848
4849 if (slang->sl_nosplitsugs)
4850 newscore += SCORE_SPLIT_NO;
4851 else
4852 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004853
4854 /* Give a bonus to words seen before. */
4855 newscore = score_wordcount_adj(slang, newscore,
4856 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004857 }
4858
Bram Moolenaar4770d092006-01-12 23:22:24 +00004859 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004860 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004861 go_deeper(stack, depth, newscore);
4862#ifdef DEBUG_TRIEWALK
4863 if (!try_compound && !fword_ends)
4864 sprintf(changename[depth], "%.*s-%s: split",
4865 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4866 else
4867 sprintf(changename[depth], "%.*s-%s: compound",
4868 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004871 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004872 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004873 sp->ts_state = STATE_SPLITUNDO;
4874
4875 ++depth;
4876 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004877
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004878 /* Append a space to preword when splitting. */
4879 if (!try_compound && !fword_ends)
4880 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004881 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004882 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004883 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004884
4885 /* If the badword has a non-word character at this
4886 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004887 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004888 * character when the word ends. But only when the
4889 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004890 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004891 + sp->ts_fidx,
4892 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004893 || fword_ends)
4894 && fword[sp->ts_fidx] != NUL
4895 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004896 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004897 int l;
4898
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004899 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004900 if (fword_ends)
4901 {
4902 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004903 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004904 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004905 sp->ts_prewordlen += l;
4906 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004907 }
4908 else
4909 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4910 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004911 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004912
Bram Moolenaard12a1322005-08-21 22:08:24 +00004913 /* When compounding include compound flag in
4914 * compflags[] (already set above). When splitting we
4915 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004916 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004917 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004918 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004919 sp->ts_compsplit = sp->ts_complen;
4920 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004921
Bram Moolenaar53805d12005-08-01 07:08:33 +00004922 /* set su->su_badflags to the caps type at this
4923 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004924#ifdef FEAT_MBYTE
4925 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004926 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004927 else
4928#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004929 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004930 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004931 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004933 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004934 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004935
4936 /* If there are postponed prefixes, try these too. */
4937 if (pbyts != NULL)
4938 {
4939 byts = pbyts;
4940 idxs = pidxs;
4941 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004942 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004943 sp->ts_state = STATE_NOPREFIX;
4944 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004945 }
4946 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004947 }
4948 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004949
Bram Moolenaar4770d092006-01-12 23:22:24 +00004950 case STATE_SPLITUNDO:
4951 /* Undo the changes done for word split or compound word. */
4952 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004953
Bram Moolenaar4770d092006-01-12 23:22:24 +00004954 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004955 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004956 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004957
Bram Moolenaar4770d092006-01-12 23:22:24 +00004958 /* In case we went into the prefix tree. */
4959 byts = fbyts;
4960 idxs = fidxs;
4961 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962
Bram Moolenaar4770d092006-01-12 23:22:24 +00004963 case STATE_ENDNUL:
4964 /* Past the NUL bytes in the node. */
4965 su->su_badflags = sp->ts_save_badflags;
4966 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004967#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004968 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004969#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004970 )
4971 {
4972 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004973 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004974 sp->ts_state = STATE_DEL;
4975 break;
4976 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004977 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004978 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004979 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004980
4981 case STATE_PLAIN:
4982 /*
4983 * Go over all possible bytes at this node, add each to tword[]
4984 * and use child node. "ts_curi" is the index.
4985 */
4986 arridx = sp->ts_arridx;
4987 if (sp->ts_curi > byts[arridx])
4988 {
4989 /* Done all bytes at this node, do next state. When still at
4990 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004991 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004992 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004993 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004994 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004995 sp->ts_state = STATE_FINAL;
4996 }
4997 else
4998 {
4999 arridx += sp->ts_curi++;
5000 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005001
Bram Moolenaar4770d092006-01-12 23:22:24 +00005002 /* Normal byte, go one level deeper. If it's not equal to the
5003 * byte in the bad word adjust the score. But don't even try
5004 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01005005 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00005006 * delete + substitute. */
5007 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +00005008#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005009 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005010#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005011 )
5012 newscore = 0;
5013 else
5014 newscore = SCORE_SUBST;
5015 if ((newscore == 0
5016 || (sp->ts_fidx >= sp->ts_fidxtry
5017 && ((sp->ts_flags & TSF_DIDDEL) == 0
5018 || c != fword[sp->ts_delidx])))
5019 && TRY_DEEPER(su, stack, depth, newscore))
5020 {
5021 go_deeper(stack, depth, newscore);
5022#ifdef DEBUG_TRIEWALK
5023 if (newscore > 0)
5024 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
5025 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5026 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005027 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005028 sprintf(changename[depth], "%.*s-%s: accept %c",
5029 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5030 fword[sp->ts_fidx]);
5031#endif
5032 ++depth;
5033 sp = &stack[depth];
5034 ++sp->ts_fidx;
5035 tword[sp->ts_twordlen++] = c;
5036 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +00005037#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005038 if (newscore == SCORE_SUBST)
5039 sp->ts_isdiff = DIFF_YES;
5040 if (has_mbyte)
5041 {
5042 /* Multi-byte characters are a bit complicated to
5043 * handle: They differ when any of the bytes differ
5044 * and then their length may also differ. */
5045 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005046 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005047 /* First byte. */
5048 sp->ts_tcharidx = 0;
5049 sp->ts_tcharlen = MB_BYTE2LEN(c);
5050 sp->ts_fcharstart = sp->ts_fidx - 1;
5051 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005052 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005053 }
5054 else if (sp->ts_isdiff == DIFF_INSERT)
5055 /* When inserting trail bytes don't advance in the
5056 * bad word. */
5057 --sp->ts_fidx;
5058 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5059 {
5060 /* Last byte of character. */
5061 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00005062 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005063 /* Correct ts_fidx for the byte length of the
5064 * character (we didn't check that before). */
5065 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005066 + MB_PTR2LEN(
5067 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005068 /* For changing a composing character adjust
5069 * the score from SCORE_SUBST to
5070 * SCORE_SUBCOMP. */
5071 if (enc_utf8
5072 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005073 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00005074 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005075 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005076 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005077 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005078 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005079 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005080 SCORE_SUBST - SCORE_SUBCOMP;
5081
Bram Moolenaar4770d092006-01-12 23:22:24 +00005082 /* For a similar character adjust score from
5083 * SCORE_SUBST to SCORE_SIMILAR. */
5084 else if (!soundfold
5085 && slang->sl_has_map
5086 && similar_chars(slang,
5087 mb_ptr2char(tword
5088 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00005089 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00005090 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00005091 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005092 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00005093 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00005094 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005095 else if (sp->ts_isdiff == DIFF_INSERT
5096 && sp->ts_twordlen > sp->ts_tcharlen)
5097 {
5098 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
5099 c = mb_ptr2char(p);
5100 if (enc_utf8 && utf_iscomposing(c))
5101 {
5102 /* Inserting a composing char doesn't
5103 * count that much. */
5104 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5105 }
5106 else
5107 {
5108 /* If the previous character was the same,
5109 * thus doubling a character, give a bonus
5110 * to the score. Also for the soundfold
5111 * tree (might seem illogical but does
5112 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005113 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005114 if (c == mb_ptr2char(p))
5115 sp->ts_score -= SCORE_INS
5116 - SCORE_INSDUP;
5117 }
5118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005119
Bram Moolenaar4770d092006-01-12 23:22:24 +00005120 /* Starting a new char, reset the length. */
5121 sp->ts_tcharlen = 0;
5122 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005123 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005124 else
5125#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005126 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005127 /* If we found a similar char adjust the score.
5128 * We do this after calling go_deeper() because
5129 * it's slow. */
5130 if (newscore != 0
5131 && !soundfold
5132 && slang->sl_has_map
5133 && similar_chars(slang,
5134 c, fword[sp->ts_fidx - 1]))
5135 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005136 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005137 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005138 }
5139 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140
Bram Moolenaar4770d092006-01-12 23:22:24 +00005141 case STATE_DEL:
5142#ifdef FEAT_MBYTE
5143 /* When past the first byte of a multi-byte char don't try
5144 * delete/insert/swap a character. */
5145 if (has_mbyte && sp->ts_tcharlen > 0)
5146 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005147 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005148 sp->ts_state = STATE_FINAL;
5149 break;
5150 }
5151#endif
5152 /*
5153 * Try skipping one character in the bad word (delete it).
5154 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005155 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005156 sp->ts_state = STATE_INS_PREP;
5157 sp->ts_curi = 1;
5158 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5159 /* Deleting a vowel at the start of a word counts less, see
5160 * soundalike_score(). */
5161 newscore = 2 * SCORE_DEL / 3;
5162 else
5163 newscore = SCORE_DEL;
5164 if (fword[sp->ts_fidx] != NUL
5165 && TRY_DEEPER(su, stack, depth, newscore))
5166 {
5167 go_deeper(stack, depth, newscore);
5168#ifdef DEBUG_TRIEWALK
5169 sprintf(changename[depth], "%.*s-%s: delete %c",
5170 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5171 fword[sp->ts_fidx]);
5172#endif
5173 ++depth;
5174
5175 /* Remember what character we deleted, so that we can avoid
5176 * inserting it again. */
5177 stack[depth].ts_flags |= TSF_DIDDEL;
5178 stack[depth].ts_delidx = sp->ts_fidx;
5179
5180 /* Advance over the character in fword[]. Give a bonus to the
5181 * score if the same character is following "nn" -> "n". It's
5182 * a bit illogical for soundfold tree but it does give better
5183 * results. */
5184#ifdef FEAT_MBYTE
5185 if (has_mbyte)
5186 {
5187 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005188 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005189 if (enc_utf8 && utf_iscomposing(c))
5190 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5191 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5192 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5193 }
5194 else
5195#endif
5196 {
5197 ++stack[depth].ts_fidx;
5198 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5199 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5200 }
5201 break;
5202 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005203 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005204
5205 case STATE_INS_PREP:
5206 if (sp->ts_flags & TSF_DIDDEL)
5207 {
5208 /* If we just deleted a byte then inserting won't make sense,
5209 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005210 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005211 sp->ts_state = STATE_SWAP;
5212 break;
5213 }
5214
5215 /* skip over NUL bytes */
5216 n = sp->ts_arridx;
5217 for (;;)
5218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 if (sp->ts_curi > byts[n])
5220 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005221 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005222 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005224 break;
5225 }
5226 if (byts[n + sp->ts_curi] != NUL)
5227 {
5228 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005229 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005230 sp->ts_state = STATE_INS;
5231 break;
5232 }
5233 ++sp->ts_curi;
5234 }
5235 break;
5236
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005237 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005238
5239 case STATE_INS:
5240 /* Insert one byte. Repeat this for each possible byte at this
5241 * node. */
5242 n = sp->ts_arridx;
5243 if (sp->ts_curi > byts[n])
5244 {
5245 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005246 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005247 sp->ts_state = STATE_SWAP;
5248 break;
5249 }
5250
5251 /* Do one more byte at this node, but:
5252 * - Skip NUL bytes.
5253 * - Skip the byte if it's equal to the byte in the word,
5254 * accepting that byte is always better.
5255 */
5256 n += sp->ts_curi++;
5257 c = byts[n];
5258 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5259 /* Inserting a vowel at the start of a word counts less,
5260 * see soundalike_score(). */
5261 newscore = 2 * SCORE_INS / 3;
5262 else
5263 newscore = SCORE_INS;
5264 if (c != fword[sp->ts_fidx]
5265 && TRY_DEEPER(su, stack, depth, newscore))
5266 {
5267 go_deeper(stack, depth, newscore);
5268#ifdef DEBUG_TRIEWALK
5269 sprintf(changename[depth], "%.*s-%s: insert %c",
5270 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5271 c);
5272#endif
5273 ++depth;
5274 sp = &stack[depth];
5275 tword[sp->ts_twordlen++] = c;
5276 sp->ts_arridx = idxs[n];
5277#ifdef FEAT_MBYTE
5278 if (has_mbyte)
5279 {
5280 fl = MB_BYTE2LEN(c);
5281 if (fl > 1)
5282 {
5283 /* There are following bytes for the same character.
5284 * We must find all bytes before trying
5285 * delete/insert/swap/etc. */
5286 sp->ts_tcharlen = fl;
5287 sp->ts_tcharidx = 1;
5288 sp->ts_isdiff = DIFF_INSERT;
5289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 }
5291 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005292 fl = 1;
5293 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00005294#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005295 {
5296 /* If the previous character was the same, thus doubling a
5297 * character, give a bonus to the score. Also for
5298 * soundfold words (illogical but does give a better
5299 * score). */
5300 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005301 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005302 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005304 }
5305 break;
5306
5307 case STATE_SWAP:
5308 /*
5309 * Swap two bytes in the bad word: "12" -> "21".
5310 * We change "fword" here, it's changed back afterwards at
5311 * STATE_UNSWAP.
5312 */
5313 p = fword + sp->ts_fidx;
5314 c = *p;
5315 if (c == NUL)
5316 {
5317 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005318 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005319 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322
Bram Moolenaar4770d092006-01-12 23:22:24 +00005323 /* Don't swap if the first character is not a word character.
5324 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005326 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005327 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005328 sp->ts_state = STATE_REP_INI;
5329 break;
5330 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005331
Bram Moolenaar4770d092006-01-12 23:22:24 +00005332#ifdef FEAT_MBYTE
5333 if (has_mbyte)
5334 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005335 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005336 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005337 if (p[n] == NUL)
5338 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005339 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005340 c2 = c; /* don't swap non-word char */
5341 else
5342 c2 = mb_ptr2char(p + n);
5343 }
5344 else
5345#endif
5346 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005347 if (p[1] == NUL)
5348 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005349 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005350 c2 = c; /* don't swap non-word char */
5351 else
5352 c2 = p[1];
5353 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005354
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005355 /* When the second character is NUL we can't swap. */
5356 if (c2 == NUL)
5357 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005358 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005359 sp->ts_state = STATE_REP_INI;
5360 break;
5361 }
5362
Bram Moolenaar4770d092006-01-12 23:22:24 +00005363 /* When characters are identical, swap won't do anything.
5364 * Also get here if the second char is not a word character. */
5365 if (c == c2)
5366 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005367 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005368 sp->ts_state = STATE_SWAP3;
5369 break;
5370 }
5371 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5372 {
5373 go_deeper(stack, depth, SCORE_SWAP);
5374#ifdef DEBUG_TRIEWALK
5375 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5376 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5377 c, c2);
5378#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005379 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005380 sp->ts_state = STATE_UNSWAP;
5381 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005382#ifdef FEAT_MBYTE
5383 if (has_mbyte)
5384 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005385 fl = mb_char2len(c2);
5386 mch_memmove(p, p + n, fl);
5387 mb_char2bytes(c, p + fl);
5388 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005389 }
5390 else
5391#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005392 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005393 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005394 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005395 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005396 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005397 }
5398 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005399 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005400 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005401 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005402 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005403 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005404 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005405
Bram Moolenaar4770d092006-01-12 23:22:24 +00005406 case STATE_UNSWAP:
5407 /* Undo the STATE_SWAP swap: "21" -> "12". */
5408 p = fword + sp->ts_fidx;
5409#ifdef FEAT_MBYTE
5410 if (has_mbyte)
5411 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005412 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005413 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005414 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005415 mb_char2bytes(c, p);
5416 }
5417 else
5418#endif
5419 {
5420 c = *p;
5421 *p = p[1];
5422 p[1] = c;
5423 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005424 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005425
5426 case STATE_SWAP3:
5427 /* Swap two bytes, skipping one: "123" -> "321". We change
5428 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5429 p = fword + sp->ts_fidx;
5430#ifdef FEAT_MBYTE
5431 if (has_mbyte)
5432 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005433 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005434 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005435 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005436 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005437 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005438 c3 = c; /* don't swap non-word char */
5439 else
5440 c3 = mb_ptr2char(p + n + fl);
5441 }
5442 else
5443#endif
5444 {
5445 c = *p;
5446 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005447 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005448 c3 = c; /* don't swap non-word char */
5449 else
5450 c3 = p[2];
5451 }
5452
5453 /* When characters are identical: "121" then SWAP3 result is
5454 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5455 * same as SWAP on next char: "112". Thus skip all swapping.
5456 * Also skip when c3 is NUL.
5457 * Also get here when the third character is not a word character.
5458 * Second character may any char: "a.b" -> "b.a" */
5459 if (c == c3 || c3 == NUL)
5460 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005461 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005462 sp->ts_state = STATE_REP_INI;
5463 break;
5464 }
5465 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5466 {
5467 go_deeper(stack, depth, SCORE_SWAP3);
5468#ifdef DEBUG_TRIEWALK
5469 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5470 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5471 c, c3);
5472#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005473 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005474 sp->ts_state = STATE_UNSWAP3;
5475 ++depth;
5476#ifdef FEAT_MBYTE
5477 if (has_mbyte)
5478 {
5479 tl = mb_char2len(c3);
5480 mch_memmove(p, p + n + fl, tl);
5481 mb_char2bytes(c2, p + tl);
5482 mb_char2bytes(c, p + fl + tl);
5483 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5484 }
5485 else
5486#endif
5487 {
5488 p[0] = p[2];
5489 p[2] = c;
5490 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5491 }
5492 }
5493 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005494 {
5495 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005496 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005497 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005498 break;
5499
5500 case STATE_UNSWAP3:
5501 /* Undo STATE_SWAP3: "321" -> "123" */
5502 p = fword + sp->ts_fidx;
5503#ifdef FEAT_MBYTE
5504 if (has_mbyte)
5505 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005506 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005507 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005508 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005509 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005510 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005511 mch_memmove(p + fl + tl, p, n);
5512 mb_char2bytes(c, p);
5513 mb_char2bytes(c2, p + tl);
5514 p = p + tl;
5515 }
5516 else
5517#endif
5518 {
5519 c = *p;
5520 *p = p[2];
5521 p[2] = c;
5522 ++p;
5523 }
5524
Bram Moolenaar860cae12010-06-05 23:22:07 +02005525 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005526 {
5527 /* Middle char is not a word char, skip the rotate. First and
5528 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005529 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005530 sp->ts_state = STATE_REP_INI;
5531 break;
5532 }
5533
5534 /* Rotate three characters left: "123" -> "231". We change
5535 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5536 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5537 {
5538 go_deeper(stack, depth, SCORE_SWAP3);
5539#ifdef DEBUG_TRIEWALK
5540 p = fword + sp->ts_fidx;
5541 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5542 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5543 p[0], p[1], p[2]);
5544#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005545 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005546 sp->ts_state = STATE_UNROT3L;
5547 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005548 p = fword + sp->ts_fidx;
5549#ifdef FEAT_MBYTE
5550 if (has_mbyte)
5551 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005552 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005553 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005554 fl = MB_CPTR2LEN(p + n);
5555 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005556 mch_memmove(p, p + n, fl);
5557 mb_char2bytes(c, p + fl);
5558 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005559 }
5560 else
5561#endif
5562 {
5563 c = *p;
5564 *p = p[1];
5565 p[1] = p[2];
5566 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005567 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005568 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005569 }
5570 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005571 {
5572 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005573 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005574 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005575 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005576
Bram Moolenaar4770d092006-01-12 23:22:24 +00005577 case STATE_UNROT3L:
5578 /* Undo ROT3L: "231" -> "123" */
5579 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005580#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005581 if (has_mbyte)
5582 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005583 n = MB_PTR2LEN(p);
5584 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005585 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005586 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005587 mch_memmove(p + tl, p, n);
5588 mb_char2bytes(c, p);
5589 }
5590 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005591#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005592 {
5593 c = p[2];
5594 p[2] = p[1];
5595 p[1] = *p;
5596 *p = c;
5597 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005598
Bram Moolenaar4770d092006-01-12 23:22:24 +00005599 /* Rotate three bytes right: "123" -> "312". We change "fword"
5600 * here, it's changed back afterwards at STATE_UNROT3R. */
5601 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5602 {
5603 go_deeper(stack, depth, SCORE_SWAP3);
5604#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005605 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005606 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5607 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5608 p[0], p[1], p[2]);
5609#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005610 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005611 sp->ts_state = STATE_UNROT3R;
5612 ++depth;
5613 p = fword + sp->ts_fidx;
5614#ifdef FEAT_MBYTE
5615 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005616 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005617 n = MB_CPTR2LEN(p);
5618 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005619 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005620 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005621 mch_memmove(p + tl, p, n);
5622 mb_char2bytes(c, p);
5623 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005624 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005625 else
5626#endif
5627 {
5628 c = p[2];
5629 p[2] = p[1];
5630 p[1] = *p;
5631 *p = c;
5632 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5633 }
5634 }
5635 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005636 {
5637 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005638 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005639 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005640 break;
5641
5642 case STATE_UNROT3R:
5643 /* Undo ROT3R: "312" -> "123" */
5644 p = fword + sp->ts_fidx;
5645#ifdef FEAT_MBYTE
5646 if (has_mbyte)
5647 {
5648 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005649 tl = MB_PTR2LEN(p);
5650 n = MB_PTR2LEN(p + tl);
5651 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005652 mch_memmove(p, p + tl, n);
5653 mb_char2bytes(c, p + n);
5654 }
5655 else
5656#endif
5657 {
5658 c = *p;
5659 *p = p[1];
5660 p[1] = p[2];
5661 p[2] = c;
5662 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005663 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005664
5665 case STATE_REP_INI:
5666 /* Check if matching with REP items from the .aff file would work.
5667 * Quickly skip if:
5668 * - there are no REP items and we are not in the soundfold trie
5669 * - the score is going to be too high anyway
5670 * - already applied a REP item or swapped here */
5671 if ((lp->lp_replang == NULL && !soundfold)
5672 || sp->ts_score + SCORE_REP >= su->su_maxscore
5673 || sp->ts_fidx < sp->ts_fidxtry)
5674 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005675 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005676 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005677 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005679
Bram Moolenaar4770d092006-01-12 23:22:24 +00005680 /* Use the first byte to quickly find the first entry that may
5681 * match. If the index is -1 there is none. */
5682 if (soundfold)
5683 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5684 else
5685 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005686
Bram Moolenaar4770d092006-01-12 23:22:24 +00005687 if (sp->ts_curi < 0)
5688 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005689 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005690 sp->ts_state = STATE_FINAL;
5691 break;
5692 }
5693
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005694 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005695 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005696 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005697
5698 case STATE_REP:
5699 /* Try matching with REP items from the .aff file. For each match
5700 * replace the characters and check if the resulting word is
5701 * valid. */
5702 p = fword + sp->ts_fidx;
5703
5704 if (soundfold)
5705 gap = &slang->sl_repsal;
5706 else
5707 gap = &lp->lp_replang->sl_rep;
5708 while (sp->ts_curi < gap->ga_len)
5709 {
5710 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5711 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005712 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005713 /* past possible matching entries */
5714 sp->ts_curi = gap->ga_len;
5715 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005716 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005717 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5718 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5719 {
5720 go_deeper(stack, depth, SCORE_REP);
5721#ifdef DEBUG_TRIEWALK
5722 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5723 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5724 ftp->ft_from, ftp->ft_to);
5725#endif
5726 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005727 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005728 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005729
Bram Moolenaar4770d092006-01-12 23:22:24 +00005730 /* Change the "from" to the "to" string. */
5731 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005732 fl = (int)STRLEN(ftp->ft_from);
5733 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005734 if (fl != tl)
5735 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005736 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005737 repextra += tl - fl;
5738 }
5739 mch_memmove(p, ftp->ft_to, tl);
5740 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
5741#ifdef FEAT_MBYTE
5742 stack[depth].ts_tcharlen = 0;
5743#endif
5744 break;
5745 }
5746 }
5747
5748 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005749 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005750 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005751 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005752 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005753 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005754
5755 break;
5756
5757 case STATE_REP_UNDO:
5758 /* Undo a REP replacement and continue with the next one. */
5759 if (soundfold)
5760 gap = &slang->sl_repsal;
5761 else
5762 gap = &lp->lp_replang->sl_rep;
5763 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005764 fl = (int)STRLEN(ftp->ft_from);
5765 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005766 p = fword + sp->ts_fidx;
5767 if (fl != tl)
5768 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005769 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005770 repextra -= tl - fl;
5771 }
5772 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005773 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005774 sp->ts_state = STATE_REP;
5775 break;
5776
5777 default:
5778 /* Did all possible states at this level, go up one level. */
5779 --depth;
5780
5781 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5782 {
5783 /* Continue in or go back to the prefix tree. */
5784 byts = pbyts;
5785 idxs = pidxs;
5786 }
5787
5788 /* Don't check for CTRL-C too often, it takes time. */
5789 if (--breakcheckcount == 0)
5790 {
5791 ui_breakcheck();
5792 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005794 }
5795 }
5796}
5797
Bram Moolenaar4770d092006-01-12 23:22:24 +00005798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005799/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005800 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005801 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005802 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005803go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005804{
Bram Moolenaarea424162005-06-16 21:51:00 +00005805 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005806 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005807 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005808 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005809 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005810}
5811
Bram Moolenaar53805d12005-08-01 07:08:33 +00005812#ifdef FEAT_MBYTE
5813/*
5814 * Case-folding may change the number of bytes: Count nr of chars in
5815 * fword[flen] and return the byte length of that many chars in "word".
5816 */
5817 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005818nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005819{
5820 char_u *p;
5821 int i = 0;
5822
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005823 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005824 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005825 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005826 --i;
5827 return (int)(p - word);
5828}
5829#endif
5830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005831/*
5832 * "fword" is a good word with case folded. Find the matching keep-case
5833 * words and put it in "kword".
5834 * Theoretically there could be several keep-case words that result in the
5835 * same case-folded word, but we only find one...
5836 */
5837 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005838find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005839{
5840 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5841 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005842 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005843
5844 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005845 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005846 int round[MAXWLEN];
5847 int fwordidx[MAXWLEN];
5848 int uwordidx[MAXWLEN];
5849 int kwordlen[MAXWLEN];
5850
5851 int flen, ulen;
5852 int l;
5853 int len;
5854 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005855 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005856 char_u *p;
5857 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005858 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005859
5860 if (byts == NULL)
5861 {
5862 /* array is empty: "cannot happen" */
5863 *kword = NUL;
5864 return;
5865 }
5866
5867 /* Make an all-cap version of "fword". */
5868 allcap_copy(fword, uword);
5869
5870 /*
5871 * Each character needs to be tried both case-folded and upper-case.
5872 * All this gets very complicated if we keep in mind that changing case
5873 * may change the byte length of a multi-byte character...
5874 */
5875 depth = 0;
5876 arridx[0] = 0;
5877 round[0] = 0;
5878 fwordidx[0] = 0;
5879 uwordidx[0] = 0;
5880 kwordlen[0] = 0;
5881 while (depth >= 0)
5882 {
5883 if (fword[fwordidx[depth]] == NUL)
5884 {
5885 /* We are at the end of "fword". If the tree allows a word to end
5886 * here we have found a match. */
5887 if (byts[arridx[depth] + 1] == 0)
5888 {
5889 kword[kwordlen[depth]] = NUL;
5890 return;
5891 }
5892
5893 /* kword is getting too long, continue one level up */
5894 --depth;
5895 }
5896 else if (++round[depth] > 2)
5897 {
5898 /* tried both fold-case and upper-case character, continue one
5899 * level up */
5900 --depth;
5901 }
5902 else
5903 {
5904 /*
5905 * round[depth] == 1: Try using the folded-case character.
5906 * round[depth] == 2: Try using the upper-case character.
5907 */
5908#ifdef FEAT_MBYTE
5909 if (has_mbyte)
5910 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005911 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5912 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005913 }
5914 else
5915#endif
5916 ulen = flen = 1;
5917 if (round[depth] == 1)
5918 {
5919 p = fword + fwordidx[depth];
5920 l = flen;
5921 }
5922 else
5923 {
5924 p = uword + uwordidx[depth];
5925 l = ulen;
5926 }
5927
5928 for (tryidx = arridx[depth]; l > 0; --l)
5929 {
5930 /* Perform a binary search in the list of accepted bytes. */
5931 len = byts[tryidx++];
5932 c = *p++;
5933 lo = tryidx;
5934 hi = tryidx + len - 1;
5935 while (lo < hi)
5936 {
5937 m = (lo + hi) / 2;
5938 if (byts[m] > c)
5939 hi = m - 1;
5940 else if (byts[m] < c)
5941 lo = m + 1;
5942 else
5943 {
5944 lo = hi = m;
5945 break;
5946 }
5947 }
5948
5949 /* Stop if there is no matching byte. */
5950 if (hi < lo || byts[lo] != c)
5951 break;
5952
5953 /* Continue at the child (if there is one). */
5954 tryidx = idxs[lo];
5955 }
5956
5957 if (l == 0)
5958 {
5959 /*
5960 * Found the matching char. Copy it to "kword" and go a
5961 * level deeper.
5962 */
5963 if (round[depth] == 1)
5964 {
5965 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5966 flen);
5967 kwordlen[depth + 1] = kwordlen[depth] + flen;
5968 }
5969 else
5970 {
5971 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5972 ulen);
5973 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5974 }
5975 fwordidx[depth + 1] = fwordidx[depth] + flen;
5976 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5977
5978 ++depth;
5979 arridx[depth] = tryidx;
5980 round[depth] = 0;
5981 }
5982 }
5983 }
5984
5985 /* Didn't find it: "cannot happen". */
5986 *kword = NUL;
5987}
5988
5989/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005990 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
5991 * su->su_sga.
5992 */
5993 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005994score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005995{
5996 langp_T *lp;
5997 char_u badsound[MAXWLEN];
5998 int i;
5999 suggest_T *stp;
6000 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006001 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006002 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006003
6004 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6005 return;
6006
6007 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006008 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006009 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006010 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006011 if (lp->lp_slang->sl_sal.ga_len > 0)
6012 {
6013 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006014 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006015
6016 for (i = 0; i < su->su_ga.ga_len; ++i)
6017 {
6018 stp = &SUG(su->su_ga, i);
6019
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006020 /* Case-fold the suggested word, sound-fold it and compute the
6021 * sound-a-like score. */
6022 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006023 if (score < SCORE_MAXMAX)
6024 {
6025 /* Add the suggestion. */
6026 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6027 sstp->st_word = vim_strsave(stp->st_word);
6028 if (sstp->st_word != NULL)
6029 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006030 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006031 sstp->st_score = score;
6032 sstp->st_altscore = 0;
6033 sstp->st_orglen = stp->st_orglen;
6034 ++su->su_sga.ga_len;
6035 }
6036 }
6037 }
6038 break;
6039 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006040 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006041}
6042
6043/*
6044 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006045 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006046 */
6047 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006048score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006049{
6050 int i;
6051 int j;
6052 garray_T ga;
6053 garray_T *gap;
6054 langp_T *lp;
6055 suggest_T *stp;
6056 char_u *p;
6057 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006058 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006059 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006060 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006061
6062 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006063 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006064 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006065 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006066 if (lp->lp_slang->sl_sal.ga_len > 0)
6067 {
6068 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006069 slang = lp->lp_slang;
6070 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006071
6072 for (i = 0; i < su->su_ga.ga_len; ++i)
6073 {
6074 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006075 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006076 if (stp->st_altscore == SCORE_MAXMAX)
6077 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6078 else
6079 stp->st_score = (stp->st_score * 3
6080 + stp->st_altscore) / 4;
6081 stp->st_salscore = FALSE;
6082 }
6083 break;
6084 }
6085 }
6086
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006087 if (slang == NULL) /* Using "double" without sound folding. */
6088 {
6089 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
6090 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006091 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006092 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006093
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006094 /* Add the alternate score to su_sga. */
6095 for (i = 0; i < su->su_sga.ga_len; ++i)
6096 {
6097 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006098 stp->st_altscore = spell_edit_score(slang,
6099 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006100 if (stp->st_score == SCORE_MAXMAX)
6101 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6102 else
6103 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6104 stp->st_salscore = TRUE;
6105 }
6106
Bram Moolenaar4770d092006-01-12 23:22:24 +00006107 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
6108 * for both lists. */
6109 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006110 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006111 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006112 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6113
6114 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6115 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6116 return;
6117
6118 stp = &SUG(ga, 0);
6119 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6120 {
6121 /* round 1: get a suggestion from su_ga
6122 * round 2: get a suggestion from su_sga */
6123 for (round = 1; round <= 2; ++round)
6124 {
6125 gap = round == 1 ? &su->su_ga : &su->su_sga;
6126 if (i < gap->ga_len)
6127 {
6128 /* Don't add a word if it's already there. */
6129 p = SUG(*gap, i).st_word;
6130 for (j = 0; j < ga.ga_len; ++j)
6131 if (STRCMP(stp[j].st_word, p) == 0)
6132 break;
6133 if (j == ga.ga_len)
6134 stp[ga.ga_len++] = SUG(*gap, i);
6135 else
6136 vim_free(p);
6137 }
6138 }
6139 }
6140
6141 ga_clear(&su->su_ga);
6142 ga_clear(&su->su_sga);
6143
6144 /* Truncate the list to the number of suggestions that will be displayed. */
6145 if (ga.ga_len > su->su_maxcount)
6146 {
6147 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6148 vim_free(stp[i].st_word);
6149 ga.ga_len = su->su_maxcount;
6150 }
6151
6152 su->su_ga = ga;
6153}
6154
6155/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006156 * For the goodword in "stp" compute the soundalike score compared to the
6157 * badword.
6158 */
6159 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006160stp_sal_score(
6161 suggest_T *stp,
6162 suginfo_T *su,
6163 slang_T *slang,
6164 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006165{
6166 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006167 char_u *pbad;
6168 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006169 char_u badsound2[MAXWLEN];
6170 char_u fword[MAXWLEN];
6171 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006172 char_u goodword[MAXWLEN];
6173 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006174
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006175 lendiff = (int)(su->su_badlen - stp->st_orglen);
6176 if (lendiff >= 0)
6177 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006178 else
6179 {
6180 /* soundfold the bad word with more characters following */
6181 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6182
6183 /* When joining two words the sound often changes a lot. E.g., "t he"
6184 * sounds like "t h" while "the" sounds like "@". Avoid that by
6185 * removing the space. Don't do it when the good word also contains a
6186 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006187 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006188 && *skiptowhite(stp->st_word) == NUL)
6189 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006190 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006191
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006192 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006193 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006194 }
6195
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006196 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006197 {
6198 /* Add part of the bad word to the good word, so that we soundfold
6199 * what replaces the bad word. */
6200 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006201 vim_strncpy(goodword + stp->st_wordlen,
6202 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006203 pgood = goodword;
6204 }
6205 else
6206 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006207
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006208 /* Sound-fold the word and compute the score for the difference. */
6209 spell_soundfold(slang, pgood, FALSE, goodsound);
6210
6211 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006212}
6213
Bram Moolenaar4770d092006-01-12 23:22:24 +00006214/* structure used to store soundfolded words that add_sound_suggest() has
6215 * handled already. */
6216typedef struct
6217{
6218 short sft_score; /* lowest score used */
6219 char_u sft_word[1]; /* soundfolded word, actually longer */
6220} sftword_T;
6221
6222static sftword_T dumsft;
6223#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6224#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6225
6226/*
6227 * Prepare for calling suggest_try_soundalike().
6228 */
6229 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006230suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006231{
6232 langp_T *lp;
6233 int lpi;
6234 slang_T *slang;
6235
6236 /* Do this for all languages that support sound folding and for which a
6237 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006238 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006239 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006240 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006241 slang = lp->lp_slang;
6242 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6243 /* prepare the hashtable used by add_sound_suggest() */
6244 hash_init(&slang->sl_sounddone);
6245 }
6246}
6247
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006248/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006249 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006250 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006251 */
6252 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006253suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006254{
6255 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006256 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006257 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006258 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006259
Bram Moolenaar4770d092006-01-12 23:22:24 +00006260 /* Do this for all languages that support sound folding and for which a
6261 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006262 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006263 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006264 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006265 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006266 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006267 {
6268 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006269 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006270
Bram Moolenaar4770d092006-01-12 23:22:24 +00006271 /* try all kinds of inserts/deletes/swaps/etc. */
6272 /* TODO: also soundfold the next words, so that we can try joining
6273 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006274#ifdef SUGGEST_PROFILE
6275 prof_init();
6276#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006277 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006278#ifdef SUGGEST_PROFILE
6279 prof_report("soundalike");
6280#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006281 }
6282 }
6283}
6284
6285/*
6286 * Finish up after calling suggest_try_soundalike().
6287 */
6288 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006289suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006290{
6291 langp_T *lp;
6292 int lpi;
6293 slang_T *slang;
6294 int todo;
6295 hashitem_T *hi;
6296
6297 /* Do this for all languages that support sound folding and for which a
6298 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006299 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006300 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006301 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006302 slang = lp->lp_slang;
6303 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6304 {
6305 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006306 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006307 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6308 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006309 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006310 vim_free(HI2SFT(hi));
6311 --todo;
6312 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006313
6314 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006315 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006316 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006317 }
6318 }
6319}
6320
6321/*
6322 * A match with a soundfolded word is found. Add the good word(s) that
6323 * produce this soundfolded word.
6324 */
6325 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006326add_sound_suggest(
6327 suginfo_T *su,
6328 char_u *goodword,
6329 int score, /* soundfold score */
6330 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006331{
6332 slang_T *slang = lp->lp_slang; /* language for sound folding */
6333 int sfwordnr;
6334 char_u *nrline;
6335 int orgnr;
6336 char_u theword[MAXWLEN];
6337 int i;
6338 int wlen;
6339 char_u *byts;
6340 idx_T *idxs;
6341 int n;
6342 int wordcount;
6343 int wc;
6344 int goodscore;
6345 hash_T hash;
6346 hashitem_T *hi;
6347 sftword_T *sft;
6348 int bc, gc;
6349 int limit;
6350
6351 /*
6352 * It's very well possible that the same soundfold word is found several
6353 * times with different scores. Since the following is quite slow only do
6354 * the words that have a better score than before. Use a hashtable to
6355 * remember the words that have been done.
6356 */
6357 hash = hash_hash(goodword);
6358 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6359 if (HASHITEM_EMPTY(hi))
6360 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006361 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6362 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006363 if (sft != NULL)
6364 {
6365 sft->sft_score = score;
6366 STRCPY(sft->sft_word, goodword);
6367 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6368 }
6369 }
6370 else
6371 {
6372 sft = HI2SFT(hi);
6373 if (score >= sft->sft_score)
6374 return;
6375 sft->sft_score = score;
6376 }
6377
6378 /*
6379 * Find the word nr in the soundfold tree.
6380 */
6381 sfwordnr = soundfold_find(slang, goodword);
6382 if (sfwordnr < 0)
6383 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006384 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006385 return;
6386 }
6387
6388 /*
6389 * go over the list of good words that produce this soundfold word
6390 */
6391 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6392 orgnr = 0;
6393 while (*nrline != NUL)
6394 {
6395 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6396 * previous wordnr. */
6397 orgnr += bytes2offset(&nrline);
6398
6399 byts = slang->sl_fbyts;
6400 idxs = slang->sl_fidxs;
6401
6402 /* Lookup the word "orgnr" one of the two tries. */
6403 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006404 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006405 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006406 {
6407 i = 1;
6408 if (wordcount == orgnr && byts[n + 1] == NUL)
6409 break; /* found end of word */
6410
6411 if (byts[n + 1] == NUL)
6412 ++wordcount;
6413
6414 /* skip over the NUL bytes */
6415 for ( ; byts[n + i] == NUL; ++i)
6416 if (i > byts[n]) /* safety check */
6417 {
6418 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006419 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006420 goto badword;
6421 }
6422
6423 /* One of the siblings must have the word. */
6424 for ( ; i < byts[n]; ++i)
6425 {
6426 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6427 if (wordcount + wc > orgnr)
6428 break;
6429 wordcount += wc;
6430 }
6431
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006432 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006433 n = idxs[n + i];
6434 }
6435badword:
6436 theword[wlen] = NUL;
6437
6438 /* Go over the possible flags and regions. */
6439 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6440 {
6441 char_u cword[MAXWLEN];
6442 char_u *p;
6443 int flags = (int)idxs[n + i];
6444
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006445 /* Skip words with the NOSUGGEST flag */
6446 if (flags & WF_NOSUGGEST)
6447 continue;
6448
Bram Moolenaar4770d092006-01-12 23:22:24 +00006449 if (flags & WF_KEEPCAP)
6450 {
6451 /* Must find the word in the keep-case tree. */
6452 find_keepcap_word(slang, theword, cword);
6453 p = cword;
6454 }
6455 else
6456 {
6457 flags |= su->su_badflags;
6458 if ((flags & WF_CAPMASK) != 0)
6459 {
6460 /* Need to fix case according to "flags". */
6461 make_case_word(theword, cword, flags);
6462 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006463 }
6464 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006465 p = theword;
6466 }
6467
6468 /* Add the suggestion. */
6469 if (sps_flags & SPS_DOUBLE)
6470 {
6471 /* Add the suggestion if the score isn't too bad. */
6472 if (score <= su->su_maxscore)
6473 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6474 score, 0, FALSE, slang, FALSE);
6475 }
6476 else
6477 {
6478 /* Add a penalty for words in another region. */
6479 if ((flags & WF_REGION)
6480 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6481 goodscore = SCORE_REGION;
6482 else
6483 goodscore = 0;
6484
6485 /* Add a small penalty for changing the first letter from
6486 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006487 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006488 * letter is the same, that has already been counted. */
6489 gc = PTR2CHAR(p);
6490 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006491 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006492 bc = PTR2CHAR(su->su_badword);
6493 if (!SPELL_ISUPPER(bc)
6494 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6495 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006496 }
6497
Bram Moolenaar4770d092006-01-12 23:22:24 +00006498 /* Compute the score for the good word. This only does letter
6499 * insert/delete/swap/replace. REP items are not considered,
6500 * which may make the score a bit higher.
6501 * Use a limit for the score to make it work faster. Use
6502 * MAXSCORE(), because RESCORE() will change the score.
6503 * If the limit is very high then the iterative method is
6504 * inefficient, using an array is quicker. */
6505 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6506 if (limit > SCORE_LIMITMAX)
6507 goodscore += spell_edit_score(slang, su->su_badword, p);
6508 else
6509 goodscore += spell_edit_score_limit(slang, su->su_badword,
6510 p, limit);
6511
6512 /* When going over the limit don't bother to do the rest. */
6513 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006514 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006515 /* Give a bonus to words seen before. */
6516 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006517
Bram Moolenaar4770d092006-01-12 23:22:24 +00006518 /* Add the suggestion if the score isn't too bad. */
6519 goodscore = RESCORE(goodscore, score);
6520 if (goodscore <= su->su_sfmaxscore)
6521 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6522 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006523 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006524 }
6525 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006526 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006527 }
6528}
6529
6530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006531 * Find word "word" in fold-case tree for "slang" and return the word number.
6532 */
6533 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006534soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006535{
6536 idx_T arridx = 0;
6537 int len;
6538 int wlen = 0;
6539 int c;
6540 char_u *ptr = word;
6541 char_u *byts;
6542 idx_T *idxs;
6543 int wordnr = 0;
6544
6545 byts = slang->sl_sbyts;
6546 idxs = slang->sl_sidxs;
6547
6548 for (;;)
6549 {
6550 /* First byte is the number of possible bytes. */
6551 len = byts[arridx++];
6552
6553 /* If the first possible byte is a zero the word could end here.
6554 * If the word ends we found the word. If not skip the NUL bytes. */
6555 c = ptr[wlen];
6556 if (byts[arridx] == NUL)
6557 {
6558 if (c == NUL)
6559 break;
6560
6561 /* Skip over the zeros, there can be several. */
6562 while (len > 0 && byts[arridx] == NUL)
6563 {
6564 ++arridx;
6565 --len;
6566 }
6567 if (len == 0)
6568 return -1; /* no children, word should have ended here */
6569 ++wordnr;
6570 }
6571
6572 /* If the word ends we didn't find it. */
6573 if (c == NUL)
6574 return -1;
6575
6576 /* Perform a binary search in the list of accepted bytes. */
6577 if (c == TAB) /* <Tab> is handled like <Space> */
6578 c = ' ';
6579 while (byts[arridx] < c)
6580 {
6581 /* The word count is in the first idxs[] entry of the child. */
6582 wordnr += idxs[idxs[arridx]];
6583 ++arridx;
6584 if (--len == 0) /* end of the bytes, didn't find it */
6585 return -1;
6586 }
6587 if (byts[arridx] != c) /* didn't find the byte */
6588 return -1;
6589
6590 /* Continue at the child (if there is one). */
6591 arridx = idxs[arridx];
6592 ++wlen;
6593
6594 /* One space in the good word may stand for several spaces in the
6595 * checked word. */
6596 if (c == ' ')
6597 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6598 ++wlen;
6599 }
6600
6601 return wordnr;
6602}
6603
6604/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006605 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006606 */
6607 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006608make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006609{
6610 if (flags & WF_ALLCAP)
6611 /* Make it all upper-case */
6612 allcap_copy(fword, cword);
6613 else if (flags & WF_ONECAP)
6614 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006615 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006616 else
6617 /* Use goodword as-is. */
6618 STRCPY(cword, fword);
6619}
6620
Bram Moolenaarea424162005-06-16 21:51:00 +00006621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006622/*
6623 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6624 * lines in the .aff file.
6625 */
6626 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006627similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006628{
Bram Moolenaarea424162005-06-16 21:51:00 +00006629 int m1, m2;
6630#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006631 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006632 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006633
Bram Moolenaarea424162005-06-16 21:51:00 +00006634 if (c1 >= 256)
6635 {
6636 buf[mb_char2bytes(c1, buf)] = 0;
6637 hi = hash_find(&slang->sl_map_hash, buf);
6638 if (HASHITEM_EMPTY(hi))
6639 m1 = 0;
6640 else
6641 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6642 }
6643 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006644#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006645 m1 = slang->sl_map_array[c1];
6646 if (m1 == 0)
6647 return FALSE;
6648
6649
6650#ifdef FEAT_MBYTE
6651 if (c2 >= 256)
6652 {
6653 buf[mb_char2bytes(c2, buf)] = 0;
6654 hi = hash_find(&slang->sl_map_hash, buf);
6655 if (HASHITEM_EMPTY(hi))
6656 m2 = 0;
6657 else
6658 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6659 }
6660 else
6661#endif
6662 m2 = slang->sl_map_array[c2];
6663
6664 return m1 == m2;
6665}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006666
6667/*
6668 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006669 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006670 */
6671 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006672add_suggestion(
6673 suginfo_T *su,
6674 garray_T *gap, /* either su_ga or su_sga */
6675 char_u *goodword,
6676 int badlenarg, /* len of bad word replaced with "goodword" */
6677 int score,
6678 int altscore,
6679 int had_bonus, /* value for st_had_bonus */
6680 slang_T *slang, /* language for sound folding */
6681 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006682 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006683{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006684 int goodlen; /* len of goodword changed */
6685 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006686 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006687 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006688 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006689 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006690
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006691 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6692 * "thee the" is added next to changing the first "the" the "thee". */
6693 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006694 pbad = su->su_badptr + badlenarg;
6695 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006696 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006697 goodlen = (int)(pgood - goodword);
6698 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006699 if (goodlen <= 0 || badlen <= 0)
6700 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006701 MB_PTR_BACK(goodword, pgood);
6702 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006703#ifdef FEAT_MBYTE
6704 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006705 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006706 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6707 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006708 }
6709 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006710#endif
6711 if (*pgood != *pbad)
6712 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006713 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006714
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006715 if (badlen == 0 && goodlen == 0)
6716 /* goodword doesn't change anything; may happen for "the the" changing
6717 * the first "the" to itself. */
6718 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006719
Bram Moolenaar89d40322006-08-29 15:30:07 +00006720 if (gap->ga_len == 0)
6721 i = -1;
6722 else
6723 {
6724 /* Check if the word is already there. Also check the length that is
6725 * being replaced "thes," -> "these" is a different suggestion from
6726 * "thes" -> "these". */
6727 stp = &SUG(*gap, 0);
6728 for (i = gap->ga_len; --i >= 0; ++stp)
6729 if (stp->st_wordlen == goodlen
6730 && stp->st_orglen == badlen
6731 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006732 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006733 /*
6734 * Found it. Remember the word with the lowest score.
6735 */
6736 if (stp->st_slang == NULL)
6737 stp->st_slang = slang;
6738
6739 new_sug.st_score = score;
6740 new_sug.st_altscore = altscore;
6741 new_sug.st_had_bonus = had_bonus;
6742
6743 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006744 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006745 /* Only one of the two had the soundalike score computed.
6746 * Need to do that for the other one now, otherwise the
6747 * scores can't be compared. This happens because
6748 * suggest_try_change() doesn't compute the soundalike
6749 * word to keep it fast, while some special methods set
6750 * the soundalike score to zero. */
6751 if (had_bonus)
6752 rescore_one(su, stp);
6753 else
6754 {
6755 new_sug.st_word = stp->st_word;
6756 new_sug.st_wordlen = stp->st_wordlen;
6757 new_sug.st_slang = stp->st_slang;
6758 new_sug.st_orglen = badlen;
6759 rescore_one(su, &new_sug);
6760 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006762
Bram Moolenaar89d40322006-08-29 15:30:07 +00006763 if (stp->st_score > new_sug.st_score)
6764 {
6765 stp->st_score = new_sug.st_score;
6766 stp->st_altscore = new_sug.st_altscore;
6767 stp->st_had_bonus = new_sug.st_had_bonus;
6768 }
6769 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006770 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006772
Bram Moolenaar4770d092006-01-12 23:22:24 +00006773 if (i < 0 && ga_grow(gap, 1) == OK)
6774 {
6775 /* Add a suggestion. */
6776 stp = &SUG(*gap, gap->ga_len);
6777 stp->st_word = vim_strnsave(goodword, goodlen);
6778 if (stp->st_word != NULL)
6779 {
6780 stp->st_wordlen = goodlen;
6781 stp->st_score = score;
6782 stp->st_altscore = altscore;
6783 stp->st_had_bonus = had_bonus;
6784 stp->st_orglen = badlen;
6785 stp->st_slang = slang;
6786 ++gap->ga_len;
6787
6788 /* If we have too many suggestions now, sort the list and keep
6789 * the best suggestions. */
6790 if (gap->ga_len > SUG_MAX_COUNT(su))
6791 {
6792 if (maxsf)
6793 su->su_sfmaxscore = cleanup_suggestions(gap,
6794 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6795 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006796 su->su_maxscore = cleanup_suggestions(gap,
6797 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006798 }
6799 }
6800 }
6801}
6802
6803/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006804 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6805 * for split words, such as "the the". Remove these from the list here.
6806 */
6807 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006808check_suggestions(
6809 suginfo_T *su,
6810 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006811{
6812 suggest_T *stp;
6813 int i;
6814 char_u longword[MAXWLEN + 1];
6815 int len;
6816 hlf_T attr;
6817
6818 stp = &SUG(*gap, 0);
6819 for (i = gap->ga_len - 1; i >= 0; --i)
6820 {
6821 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006822 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006823 len = stp[i].st_wordlen;
6824 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6825 MAXWLEN - len);
6826 attr = HLF_COUNT;
6827 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6828 if (attr != HLF_COUNT)
6829 {
6830 /* Remove this entry. */
6831 vim_free(stp[i].st_word);
6832 --gap->ga_len;
6833 if (i < gap->ga_len)
6834 mch_memmove(stp + i, stp + i + 1,
6835 sizeof(suggest_T) * (gap->ga_len - i));
6836 }
6837 }
6838}
6839
6840
6841/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006842 * Add a word to be banned.
6843 */
6844 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006845add_banned(
6846 suginfo_T *su,
6847 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006848{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006849 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006850 hash_T hash;
6851 hashitem_T *hi;
6852
Bram Moolenaar4770d092006-01-12 23:22:24 +00006853 hash = hash_hash(word);
6854 hi = hash_lookup(&su->su_banned, word, hash);
6855 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006856 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006857 s = vim_strsave(word);
6858 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006859 hash_add_item(&su->su_banned, hi, s, hash);
6860 }
6861}
6862
6863/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006864 * Recompute the score for all suggestions if sound-folding is possible. This
6865 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006866 */
6867 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006868rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006869{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006870 int i;
6871
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006872 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006873 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006874 rescore_one(su, &SUG(su->su_ga, i));
6875}
6876
6877/*
6878 * Recompute the score for one suggestion if sound-folding is possible.
6879 */
6880 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006881rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006882{
6883 slang_T *slang = stp->st_slang;
6884 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006885 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006886
6887 /* Only rescore suggestions that have no sal score yet and do have a
6888 * language. */
6889 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6890 {
6891 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006892 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006893 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006894 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006895 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006896 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006897 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006898
6899 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006900 if (stp->st_altscore == SCORE_MAXMAX)
6901 stp->st_altscore = SCORE_BIG;
6902 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6903 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006904 }
6905}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006907static int
6908#ifdef __BORLANDC__
6909_RTLENTRYF
6910#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006911sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006912
6913/*
6914 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006915 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006916 */
6917 static int
6918#ifdef __BORLANDC__
6919_RTLENTRYF
6920#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006921sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006922{
6923 suggest_T *p1 = (suggest_T *)s1;
6924 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006925 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006926
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006927 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006928 {
6929 n = p1->st_altscore - p2->st_altscore;
6930 if (n == 0)
6931 n = STRICMP(p1->st_word, p2->st_word);
6932 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006933 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006934}
6935
6936/*
6937 * Cleanup the suggestions:
6938 * - Sort on score.
6939 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006940 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006941 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006942 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006943cleanup_suggestions(
6944 garray_T *gap,
6945 int maxscore,
6946 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006947{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006948 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006949 int i;
6950
6951 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006952 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006953
6954 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006955 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006956 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006957 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006958 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006959 gap->ga_len = keep;
6960 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006961 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006962 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006963}
6964
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006965#if defined(FEAT_EVAL) || defined(PROTO)
6966/*
6967 * Soundfold a string, for soundfold().
6968 * Result is in allocated memory, NULL for an error.
6969 */
6970 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006971eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006972{
6973 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006974 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006975 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006976
Bram Moolenaar860cae12010-06-05 23:22:07 +02006977 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006978 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006979 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006980 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006981 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006982 if (lp->lp_slang->sl_sal.ga_len > 0)
6983 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006984 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006985 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006986 return vim_strsave(sound);
6987 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006988 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006989
6990 /* No language with sound folding, return word as-is. */
6991 return vim_strsave(word);
6992}
6993#endif
6994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006995/*
6996 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00006997 *
6998 * There are many ways to turn a word into a sound-a-like representation. The
6999 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
7000 * swedish name matching - survey and test of different algorithms" by Klas
7001 * Erikson.
7002 *
7003 * We support two methods:
7004 * 1. SOFOFROM/SOFOTO do a simple character mapping.
7005 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007006 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02007007 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007008spell_soundfold(
7009 slang_T *slang,
7010 char_u *inword,
7011 int folded, /* "inword" is already case-folded */
7012 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007013{
7014 char_u fword[MAXWLEN];
7015 char_u *word;
7016
7017 if (slang->sl_sofo)
7018 /* SOFOFROM and SOFOTO used */
7019 spell_soundfold_sofo(slang, inword, res);
7020 else
7021 {
7022 /* SAL items used. Requires the word to be case-folded. */
7023 if (folded)
7024 word = inword;
7025 else
7026 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007027 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007028 word = fword;
7029 }
7030
7031#ifdef FEAT_MBYTE
7032 if (has_mbyte)
7033 spell_soundfold_wsal(slang, word, res);
7034 else
7035#endif
7036 spell_soundfold_sal(slang, word, res);
7037 }
7038}
7039
7040/*
7041 * Perform sound folding of "inword" into "res" according to SOFOFROM and
7042 * SOFOTO lines.
7043 */
7044 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007045spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007046{
7047 char_u *s;
7048 int ri = 0;
7049 int c;
7050
7051#ifdef FEAT_MBYTE
7052 if (has_mbyte)
7053 {
7054 int prevc = 0;
7055 int *ip;
7056
7057 /* The sl_sal_first[] table contains the translation for chars up to
7058 * 255, sl_sal the rest. */
7059 for (s = inword; *s != NUL; )
7060 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007061 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007062 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007063 c = ' ';
7064 else if (c < 256)
7065 c = slang->sl_sal_first[c];
7066 else
7067 {
7068 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
7069 if (ip == NULL) /* empty list, can't match */
7070 c = NUL;
7071 else
7072 for (;;) /* find "c" in the list */
7073 {
7074 if (*ip == 0) /* not found */
7075 {
7076 c = NUL;
7077 break;
7078 }
7079 if (*ip == c) /* match! */
7080 {
7081 c = ip[1];
7082 break;
7083 }
7084 ip += 2;
7085 }
7086 }
7087
7088 if (c != NUL && c != prevc)
7089 {
7090 ri += mb_char2bytes(c, res + ri);
7091 if (ri + MB_MAXBYTES > MAXWLEN)
7092 break;
7093 prevc = c;
7094 }
7095 }
7096 }
7097 else
7098#endif
7099 {
7100 /* The sl_sal_first[] table contains the translation. */
7101 for (s = inword; (c = *s) != NUL; ++s)
7102 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007103 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007104 c = ' ';
7105 else
7106 c = slang->sl_sal_first[c];
7107 if (c != NUL && (ri == 0 || res[ri - 1] != c))
7108 res[ri++] = c;
7109 }
7110 }
7111
7112 res[ri] = NUL;
7113}
7114
7115 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007116spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007117{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007118 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007119 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007120 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007121 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007122 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007123 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007124 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007125 int n, k = 0;
7126 int z0;
7127 int k0;
7128 int n0;
7129 int c;
7130 int pri;
7131 int p0 = -333;
7132 int c0;
7133
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007134 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007135 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007136 if (slang->sl_rem_accents)
7137 {
7138 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007139 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007140 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007141 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007142 {
7143 *t++ = ' ';
7144 s = skipwhite(s);
7145 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007146 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007147 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007148 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007149 *t++ = *s;
7150 ++s;
7151 }
7152 }
7153 *t = NUL;
7154 }
7155 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007156 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007158 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007159
7160 /*
7161 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007162 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007163 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007164 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007165 while ((c = word[i]) != NUL)
7166 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007167 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007168 n = slang->sl_sal_first[c];
7169 z0 = 0;
7170
7171 if (n >= 0)
7172 {
7173 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007174 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007175 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007176 /* Quickly skip entries that don't match the word. Most
7177 * entries are less then three chars, optimize for that. */
7178 k = smp[n].sm_leadlen;
7179 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007180 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007181 if (word[i + 1] != s[1])
7182 continue;
7183 if (k > 2)
7184 {
7185 for (j = 2; j < k; ++j)
7186 if (word[i + j] != s[j])
7187 break;
7188 if (j < k)
7189 continue;
7190 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007191 }
7192
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007193 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007194 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007195 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007196 while (*pf != NUL && *pf != word[i + k])
7197 ++pf;
7198 if (*pf == NUL)
7199 continue;
7200 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007201 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007202 s = smp[n].sm_rules;
7203 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007204
7205 p0 = *s;
7206 k0 = k;
7207 while (*s == '-' && k > 1)
7208 {
7209 k--;
7210 s++;
7211 }
7212 if (*s == '<')
7213 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007214 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007215 {
7216 /* determine priority */
7217 pri = *s - '0';
7218 s++;
7219 }
7220 if (*s == '^' && *(s + 1) == '^')
7221 s++;
7222
7223 if (*s == NUL
7224 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007225 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007226 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007228 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007229 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007230 && spell_iswordp(word + i - 1, curwin)
7231 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007232 {
7233 /* search for followup rules, if: */
7234 /* followup and k > 1 and NO '-' in searchstring */
7235 c0 = word[i + k - 1];
7236 n0 = slang->sl_sal_first[c0];
7237
7238 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007239 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007240 {
7241 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007242 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007243 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007244 /* Quickly skip entries that don't match the word.
7245 * */
7246 k0 = smp[n0].sm_leadlen;
7247 if (k0 > 1)
7248 {
7249 if (word[i + k] != s[1])
7250 continue;
7251 if (k0 > 2)
7252 {
7253 pf = word + i + k + 1;
7254 for (j = 2; j < k0; ++j)
7255 if (*pf++ != s[j])
7256 break;
7257 if (j < k0)
7258 continue;
7259 }
7260 }
7261 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007263 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007264 {
7265 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007266 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007267 while (*pf != NUL && *pf != word[i + k0])
7268 ++pf;
7269 if (*pf == NUL)
7270 continue;
7271 ++k0;
7272 }
7273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007274 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007275 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007276 while (*s == '-')
7277 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007278 /* "k0" gets NOT reduced because
7279 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007280 s++;
7281 }
7282 if (*s == '<')
7283 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007284 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007285 {
7286 p0 = *s - '0';
7287 s++;
7288 }
7289
7290 if (*s == NUL
7291 /* *s == '^' cuts */
7292 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007293 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007294 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295 {
7296 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007298 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007299
7300 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007301 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007302 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007303 /* rule fits; stop search */
7304 break;
7305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 }
7307
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007308 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007309 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007310 }
7311
7312 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007313 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007314 if (s == NULL)
7315 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007316 pf = smp[n].sm_rules;
7317 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 if (p0 == 1 && z == 0)
7319 {
7320 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007321 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7322 || res[reslen - 1] == *s))
7323 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 z0 = 1;
7325 z = 1;
7326 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007327 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 {
7329 word[i + k0] = *s;
7330 k0++;
7331 s++;
7332 }
7333 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007334 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007335
7336 /* new "actual letter" */
7337 c = word[i];
7338 }
7339 else
7340 {
7341 /* no '<' rule used */
7342 i += k - 1;
7343 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007344 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007346 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007347 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007348 s++;
7349 }
7350 /* new "actual letter" */
7351 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007352 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007353 {
7354 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007355 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007356 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007357 i = 0;
7358 z0 = 1;
7359 }
7360 }
7361 break;
7362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007363 }
7364 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007365 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007366 {
7367 c = ' ';
7368 k = 1;
7369 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007370
7371 if (z0 == 0)
7372 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007373 if (k && !p0 && reslen < MAXWLEN && c != NUL
7374 && (!slang->sl_collapse || reslen == 0
7375 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007377 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378
7379 i++;
7380 z = 0;
7381 k = 0;
7382 }
7383 }
7384
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007385 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386}
7387
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007388#ifdef FEAT_MBYTE
7389/*
7390 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7391 * Multi-byte version of spell_soundfold().
7392 */
7393 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007394spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007395{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007396 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007397 int word[MAXWLEN];
7398 int wres[MAXWLEN];
7399 int l;
7400 char_u *s;
7401 int *ws;
7402 char_u *t;
7403 int *pf;
7404 int i, j, z;
7405 int reslen;
7406 int n, k = 0;
7407 int z0;
7408 int k0;
7409 int n0;
7410 int c;
7411 int pri;
7412 int p0 = -333;
7413 int c0;
7414 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007415 int wordlen;
7416
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007417
7418 /*
7419 * Convert the multi-byte string to a wide-character string.
7420 * Remove accents, if wanted. We actually remove all non-word characters.
7421 * But keep white space.
7422 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007423 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007424 for (s = inword; *s != NUL; )
7425 {
7426 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007427 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007428 if (slang->sl_rem_accents)
7429 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007430 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007431 {
7432 if (did_white)
7433 continue;
7434 c = ' ';
7435 did_white = TRUE;
7436 }
7437 else
7438 {
7439 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007440 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007441 continue;
7442 }
7443 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007444 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007445 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007446 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007447
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007448 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007449 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007450 * Converted from C++ to C. Added support for multi-byte chars.
7451 * Changed to keep spaces.
7452 */
7453 i = reslen = z = 0;
7454 while ((c = word[i]) != NUL)
7455 {
7456 /* Start with the first rule that has the character in the word. */
7457 n = slang->sl_sal_first[c & 0xff];
7458 z0 = 0;
7459
7460 if (n >= 0)
7461 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007462 /* Check all rules for the same index byte.
7463 * If c is 0x300 need extra check for the end of the array, as
7464 * (c & 0xff) is NUL. */
7465 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7466 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007467 {
7468 /* Quickly skip entries that don't match the word. Most
7469 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007470 if (c != ws[0])
7471 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007472 k = smp[n].sm_leadlen;
7473 if (k > 1)
7474 {
7475 if (word[i + 1] != ws[1])
7476 continue;
7477 if (k > 2)
7478 {
7479 for (j = 2; j < k; ++j)
7480 if (word[i + j] != ws[j])
7481 break;
7482 if (j < k)
7483 continue;
7484 }
7485 }
7486
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007487 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007488 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007489 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007490 while (*pf != NUL && *pf != word[i + k])
7491 ++pf;
7492 if (*pf == NUL)
7493 continue;
7494 ++k;
7495 }
7496 s = smp[n].sm_rules;
7497 pri = 5; /* default priority */
7498
7499 p0 = *s;
7500 k0 = k;
7501 while (*s == '-' && k > 1)
7502 {
7503 k--;
7504 s++;
7505 }
7506 if (*s == '<')
7507 s++;
7508 if (VIM_ISDIGIT(*s))
7509 {
7510 /* determine priority */
7511 pri = *s - '0';
7512 s++;
7513 }
7514 if (*s == '^' && *(s + 1) == '^')
7515 s++;
7516
7517 if (*s == NUL
7518 || (*s == '^'
7519 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007520 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007521 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007522 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007523 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007524 && spell_iswordp_w(word + i - 1, curwin)
7525 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007526 {
7527 /* search for followup rules, if: */
7528 /* followup and k > 1 and NO '-' in searchstring */
7529 c0 = word[i + k - 1];
7530 n0 = slang->sl_sal_first[c0 & 0xff];
7531
7532 if (slang->sl_followup && k > 1 && n0 >= 0
7533 && p0 != '-' && word[i + k] != NUL)
7534 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007535 /* Test follow-up rule for "word[i + k]"; loop over
7536 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007537 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7538 == (c0 & 0xff); ++n0)
7539 {
7540 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007541 */
7542 if (c0 != ws[0])
7543 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007544 k0 = smp[n0].sm_leadlen;
7545 if (k0 > 1)
7546 {
7547 if (word[i + k] != ws[1])
7548 continue;
7549 if (k0 > 2)
7550 {
7551 pf = word + i + k + 1;
7552 for (j = 2; j < k0; ++j)
7553 if (*pf++ != ws[j])
7554 break;
7555 if (j < k0)
7556 continue;
7557 }
7558 }
7559 k0 += k - 1;
7560
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007561 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007562 {
7563 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007564 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007565 while (*pf != NUL && *pf != word[i + k0])
7566 ++pf;
7567 if (*pf == NUL)
7568 continue;
7569 ++k0;
7570 }
7571
7572 p0 = 5;
7573 s = smp[n0].sm_rules;
7574 while (*s == '-')
7575 {
7576 /* "k0" gets NOT reduced because
7577 * "if (k0 == k)" */
7578 s++;
7579 }
7580 if (*s == '<')
7581 s++;
7582 if (VIM_ISDIGIT(*s))
7583 {
7584 p0 = *s - '0';
7585 s++;
7586 }
7587
7588 if (*s == NUL
7589 /* *s == '^' cuts */
7590 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007591 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007592 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007593 {
7594 if (k0 == k)
7595 /* this is just a piece of the string */
7596 continue;
7597
7598 if (p0 < pri)
7599 /* priority too low */
7600 continue;
7601 /* rule fits; stop search */
7602 break;
7603 }
7604 }
7605
7606 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7607 == (c0 & 0xff))
7608 continue;
7609 }
7610
7611 /* replace string */
7612 ws = smp[n].sm_to_w;
7613 s = smp[n].sm_rules;
7614 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7615 if (p0 == 1 && z == 0)
7616 {
7617 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007618 if (reslen > 0 && ws != NULL && *ws != NUL
7619 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007620 || wres[reslen - 1] == *ws))
7621 reslen--;
7622 z0 = 1;
7623 z = 1;
7624 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007625 if (ws != NULL)
7626 while (*ws != NUL && word[i + k0] != NUL)
7627 {
7628 word[i + k0] = *ws;
7629 k0++;
7630 ws++;
7631 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007632 if (k > k0)
7633 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007634 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007635
7636 /* new "actual letter" */
7637 c = word[i];
7638 }
7639 else
7640 {
7641 /* no '<' rule used */
7642 i += k - 1;
7643 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007644 if (ws != NULL)
7645 while (*ws != NUL && ws[1] != NUL
7646 && reslen < MAXWLEN)
7647 {
7648 if (reslen == 0 || wres[reslen - 1] != *ws)
7649 wres[reslen++] = *ws;
7650 ws++;
7651 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007652 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007653 if (ws == NULL)
7654 c = NUL;
7655 else
7656 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007657 if (strstr((char *)s, "^^") != NULL)
7658 {
7659 if (c != NUL)
7660 wres[reslen++] = c;
7661 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007662 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007663 i = 0;
7664 z0 = 1;
7665 }
7666 }
7667 break;
7668 }
7669 }
7670 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007671 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007672 {
7673 c = ' ';
7674 k = 1;
7675 }
7676
7677 if (z0 == 0)
7678 {
7679 if (k && !p0 && reslen < MAXWLEN && c != NUL
7680 && (!slang->sl_collapse || reslen == 0
7681 || wres[reslen - 1] != c))
7682 /* condense only double letters */
7683 wres[reslen++] = c;
7684
7685 i++;
7686 z = 0;
7687 k = 0;
7688 }
7689 }
7690
7691 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7692 l = 0;
7693 for (n = 0; n < reslen; ++n)
7694 {
7695 l += mb_char2bytes(wres[n], res + l);
7696 if (l + MB_MAXBYTES > MAXWLEN)
7697 break;
7698 }
7699 res[l] = NUL;
7700}
7701#endif
7702
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007703/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007704 * Compute a score for two sound-a-like words.
7705 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7706 * Instead of a generic loop we write out the code. That keeps it fast by
7707 * avoiding checks that will not be possible.
7708 */
7709 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007710soundalike_score(
7711 char_u *goodstart, /* sound-folded good word */
7712 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007713{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007714 char_u *goodsound = goodstart;
7715 char_u *badsound = badstart;
7716 int goodlen;
7717 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007718 int n;
7719 char_u *pl, *ps;
7720 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007721 int score = 0;
7722
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007723 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007724 * counted so much, vowels halfway the word aren't counted at all. */
7725 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7726 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007727 if ((badsound[0] == NUL && goodsound[1] == NUL)
7728 || (goodsound[0] == NUL && badsound[1] == NUL))
7729 /* changing word with vowel to word without a sound */
7730 return SCORE_DEL;
7731 if (badsound[0] == NUL || goodsound[0] == NUL)
7732 /* more than two changes */
7733 return SCORE_MAXMAX;
7734
Bram Moolenaar4770d092006-01-12 23:22:24 +00007735 if (badsound[1] == goodsound[1]
7736 || (badsound[1] != NUL
7737 && goodsound[1] != NUL
7738 && badsound[2] == goodsound[2]))
7739 {
7740 /* handle like a substitute */
7741 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007742 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007743 {
7744 score = 2 * SCORE_DEL / 3;
7745 if (*badsound == '*')
7746 ++badsound;
7747 else
7748 ++goodsound;
7749 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007750 }
7751
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007752 goodlen = (int)STRLEN(goodsound);
7753 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007754
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007755 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007756 * changes. */
7757 n = goodlen - badlen;
7758 if (n < -2 || n > 2)
7759 return SCORE_MAXMAX;
7760
7761 if (n > 0)
7762 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007763 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007764 ps = badsound;
7765 }
7766 else
7767 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007768 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007769 ps = goodsound;
7770 }
7771
7772 /* Skip over the identical part. */
7773 while (*pl == *ps && *pl != NUL)
7774 {
7775 ++pl;
7776 ++ps;
7777 }
7778
7779 switch (n)
7780 {
7781 case -2:
7782 case 2:
7783 /*
7784 * Must delete two characters from "pl".
7785 */
7786 ++pl; /* first delete */
7787 while (*pl == *ps)
7788 {
7789 ++pl;
7790 ++ps;
7791 }
7792 /* strings must be equal after second delete */
7793 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007794 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007795
7796 /* Failed to compare. */
7797 break;
7798
7799 case -1:
7800 case 1:
7801 /*
7802 * Minimal one delete from "pl" required.
7803 */
7804
7805 /* 1: delete */
7806 pl2 = pl + 1;
7807 ps2 = ps;
7808 while (*pl2 == *ps2)
7809 {
7810 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007811 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007812 ++pl2;
7813 ++ps2;
7814 }
7815
7816 /* 2: delete then swap, then rest must be equal */
7817 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7818 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007819 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007820
7821 /* 3: delete then substitute, then the rest must be equal */
7822 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007823 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007824
7825 /* 4: first swap then delete */
7826 if (pl[0] == ps[1] && pl[1] == ps[0])
7827 {
7828 pl2 = pl + 2; /* swap, skip two chars */
7829 ps2 = ps + 2;
7830 while (*pl2 == *ps2)
7831 {
7832 ++pl2;
7833 ++ps2;
7834 }
7835 /* delete a char and then strings must be equal */
7836 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007837 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007838 }
7839
7840 /* 5: first substitute then delete */
7841 pl2 = pl + 1; /* substitute, skip one char */
7842 ps2 = ps + 1;
7843 while (*pl2 == *ps2)
7844 {
7845 ++pl2;
7846 ++ps2;
7847 }
7848 /* delete a char and then strings must be equal */
7849 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007850 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007851
7852 /* Failed to compare. */
7853 break;
7854
7855 case 0:
7856 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007857 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007858 * insert is only possible in combination with a delete.
7859 * 1: check if for identical strings
7860 */
7861 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007862 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007863
7864 /* 2: swap */
7865 if (pl[0] == ps[1] && pl[1] == ps[0])
7866 {
7867 pl2 = pl + 2; /* swap, skip two chars */
7868 ps2 = ps + 2;
7869 while (*pl2 == *ps2)
7870 {
7871 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007872 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007873 ++pl2;
7874 ++ps2;
7875 }
7876 /* 3: swap and swap again */
7877 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7878 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007879 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007880
7881 /* 4: swap and substitute */
7882 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007883 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007884 }
7885
7886 /* 5: substitute */
7887 pl2 = pl + 1;
7888 ps2 = ps + 1;
7889 while (*pl2 == *ps2)
7890 {
7891 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007892 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007893 ++pl2;
7894 ++ps2;
7895 }
7896
7897 /* 6: substitute and swap */
7898 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7899 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007900 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007901
7902 /* 7: substitute and substitute */
7903 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007904 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007905
7906 /* 8: insert then delete */
7907 pl2 = pl;
7908 ps2 = ps + 1;
7909 while (*pl2 == *ps2)
7910 {
7911 ++pl2;
7912 ++ps2;
7913 }
7914 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007915 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007916
7917 /* 9: delete then insert */
7918 pl2 = pl + 1;
7919 ps2 = ps;
7920 while (*pl2 == *ps2)
7921 {
7922 ++pl2;
7923 ++ps2;
7924 }
7925 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007926 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007927
7928 /* Failed to compare. */
7929 break;
7930 }
7931
7932 return SCORE_MAXMAX;
7933}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007935/*
7936 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007937 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007938 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007939 * The algorithm is described by Du and Chang, 1992.
7940 * The implementation of the algorithm comes from Aspell editdist.cpp,
7941 * edit_distance(). It has been converted from C++ to C and modified to
7942 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007943 */
7944 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007945spell_edit_score(
7946 slang_T *slang,
7947 char_u *badword,
7948 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007949{
7950 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007951 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007952 int j, i;
7953 int t;
7954 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007955 int pbc, pgc;
7956#ifdef FEAT_MBYTE
7957 char_u *p;
7958 int wbadword[MAXWLEN];
7959 int wgoodword[MAXWLEN];
7960
7961 if (has_mbyte)
7962 {
7963 /* Get the characters from the multi-byte strings and put them in an
7964 * int array for easy access. */
7965 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007966 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007967 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007968 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007969 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007970 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007971 }
7972 else
7973#endif
7974 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007975 badlen = (int)STRLEN(badword) + 1;
7976 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007977 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007978
7979 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7980#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007981 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7982 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007983 if (cnt == NULL)
7984 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007985
7986 CNT(0, 0) = 0;
7987 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007988 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007989
7990 for (i = 1; i <= badlen; ++i)
7991 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007992 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007993 for (j = 1; j <= goodlen; ++j)
7994 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007995#ifdef FEAT_MBYTE
7996 if (has_mbyte)
7997 {
7998 bc = wbadword[i - 1];
7999 gc = wgoodword[j - 1];
8000 }
8001 else
8002#endif
8003 {
8004 bc = badword[i - 1];
8005 gc = goodword[j - 1];
8006 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008007 if (bc == gc)
8008 CNT(i, j) = CNT(i - 1, j - 1);
8009 else
8010 {
8011 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008012 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
8014 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008015 {
8016 /* For a similar character use SCORE_SIMILAR. */
8017 if (slang != NULL
8018 && slang->sl_has_map
8019 && similar_chars(slang, gc, bc))
8020 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
8021 else
8022 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
8023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008024
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008025 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008026 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008027#ifdef FEAT_MBYTE
8028 if (has_mbyte)
8029 {
8030 pbc = wbadword[i - 2];
8031 pgc = wgoodword[j - 2];
8032 }
8033 else
8034#endif
8035 {
8036 pbc = badword[i - 2];
8037 pgc = goodword[j - 2];
8038 }
8039 if (bc == pgc && pbc == gc)
8040 {
8041 t = SCORE_SWAP + CNT(i - 2, j - 2);
8042 if (t < CNT(i, j))
8043 CNT(i, j) = t;
8044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008045 }
8046 t = SCORE_DEL + CNT(i - 1, j);
8047 if (t < CNT(i, j))
8048 CNT(i, j) = t;
8049 t = SCORE_INS + CNT(i, j - 1);
8050 if (t < CNT(i, j))
8051 CNT(i, j) = t;
8052 }
8053 }
8054 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008055
8056 i = CNT(badlen - 1, goodlen - 1);
8057 vim_free(cnt);
8058 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008059}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008060
Bram Moolenaar4770d092006-01-12 23:22:24 +00008061typedef struct
8062{
8063 int badi;
8064 int goodi;
8065 int score;
8066} limitscore_T;
8067
8068/*
8069 * Like spell_edit_score(), but with a limit on the score to make it faster.
8070 * May return SCORE_MAXMAX when the score is higher than "limit".
8071 *
8072 * This uses a stack for the edits still to be tried.
8073 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
8074 * for multi-byte characters.
8075 */
8076 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008077spell_edit_score_limit(
8078 slang_T *slang,
8079 char_u *badword,
8080 char_u *goodword,
8081 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008082{
8083 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8084 int stackidx;
8085 int bi, gi;
8086 int bi2, gi2;
8087 int bc, gc;
8088 int score;
8089 int score_off;
8090 int minscore;
8091 int round;
8092
8093#ifdef FEAT_MBYTE
8094 /* Multi-byte characters require a bit more work, use a different function
8095 * to avoid testing "has_mbyte" quite often. */
8096 if (has_mbyte)
8097 return spell_edit_score_limit_w(slang, badword, goodword, limit);
8098#endif
8099
8100 /*
8101 * The idea is to go from start to end over the words. So long as
8102 * characters are equal just continue, this always gives the lowest score.
8103 * When there is a difference try several alternatives. Each alternative
8104 * increases "score" for the edit distance. Some of the alternatives are
8105 * pushed unto a stack and tried later, some are tried right away. At the
8106 * end of the word the score for one alternative is known. The lowest
8107 * possible score is stored in "minscore".
8108 */
8109 stackidx = 0;
8110 bi = 0;
8111 gi = 0;
8112 score = 0;
8113 minscore = limit + 1;
8114
8115 for (;;)
8116 {
8117 /* Skip over an equal part, score remains the same. */
8118 for (;;)
8119 {
8120 bc = badword[bi];
8121 gc = goodword[gi];
8122 if (bc != gc) /* stop at a char that's different */
8123 break;
8124 if (bc == NUL) /* both words end */
8125 {
8126 if (score < minscore)
8127 minscore = score;
8128 goto pop; /* do next alternative */
8129 }
8130 ++bi;
8131 ++gi;
8132 }
8133
8134 if (gc == NUL) /* goodword ends, delete badword chars */
8135 {
8136 do
8137 {
8138 if ((score += SCORE_DEL) >= minscore)
8139 goto pop; /* do next alternative */
8140 } while (badword[++bi] != NUL);
8141 minscore = score;
8142 }
8143 else if (bc == NUL) /* badword ends, insert badword chars */
8144 {
8145 do
8146 {
8147 if ((score += SCORE_INS) >= minscore)
8148 goto pop; /* do next alternative */
8149 } while (goodword[++gi] != NUL);
8150 minscore = score;
8151 }
8152 else /* both words continue */
8153 {
8154 /* If not close to the limit, perform a change. Only try changes
8155 * that may lead to a lower score than "minscore".
8156 * round 0: try deleting a char from badword
8157 * round 1: try inserting a char in badword */
8158 for (round = 0; round <= 1; ++round)
8159 {
8160 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8161 if (score_off < minscore)
8162 {
8163 if (score_off + SCORE_EDIT_MIN >= minscore)
8164 {
8165 /* Near the limit, rest of the words must match. We
8166 * can check that right now, no need to push an item
8167 * onto the stack. */
8168 bi2 = bi + 1 - round;
8169 gi2 = gi + round;
8170 while (goodword[gi2] == badword[bi2])
8171 {
8172 if (goodword[gi2] == NUL)
8173 {
8174 minscore = score_off;
8175 break;
8176 }
8177 ++bi2;
8178 ++gi2;
8179 }
8180 }
8181 else
8182 {
8183 /* try deleting/inserting a character later */
8184 stack[stackidx].badi = bi + 1 - round;
8185 stack[stackidx].goodi = gi + round;
8186 stack[stackidx].score = score_off;
8187 ++stackidx;
8188 }
8189 }
8190 }
8191
8192 if (score + SCORE_SWAP < minscore)
8193 {
8194 /* If swapping two characters makes a match then the
8195 * substitution is more expensive, thus there is no need to
8196 * try both. */
8197 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8198 {
8199 /* Swap two characters, that is: skip them. */
8200 gi += 2;
8201 bi += 2;
8202 score += SCORE_SWAP;
8203 continue;
8204 }
8205 }
8206
8207 /* Substitute one character for another which is the same
8208 * thing as deleting a character from both goodword and badword.
8209 * Use a better score when there is only a case difference. */
8210 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8211 score += SCORE_ICASE;
8212 else
8213 {
8214 /* For a similar character use SCORE_SIMILAR. */
8215 if (slang != NULL
8216 && slang->sl_has_map
8217 && similar_chars(slang, gc, bc))
8218 score += SCORE_SIMILAR;
8219 else
8220 score += SCORE_SUBST;
8221 }
8222
8223 if (score < minscore)
8224 {
8225 /* Do the substitution. */
8226 ++gi;
8227 ++bi;
8228 continue;
8229 }
8230 }
8231pop:
8232 /*
8233 * Get here to try the next alternative, pop it from the stack.
8234 */
8235 if (stackidx == 0) /* stack is empty, finished */
8236 break;
8237
8238 /* pop an item from the stack */
8239 --stackidx;
8240 gi = stack[stackidx].goodi;
8241 bi = stack[stackidx].badi;
8242 score = stack[stackidx].score;
8243 }
8244
8245 /* When the score goes over "limit" it may actually be much higher.
8246 * Return a very large number to avoid going below the limit when giving a
8247 * bonus. */
8248 if (minscore > limit)
8249 return SCORE_MAXMAX;
8250 return minscore;
8251}
8252
8253#ifdef FEAT_MBYTE
8254/*
8255 * Multi-byte version of spell_edit_score_limit().
8256 * Keep it in sync with the above!
8257 */
8258 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008259spell_edit_score_limit_w(
8260 slang_T *slang,
8261 char_u *badword,
8262 char_u *goodword,
8263 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008264{
8265 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8266 int stackidx;
8267 int bi, gi;
8268 int bi2, gi2;
8269 int bc, gc;
8270 int score;
8271 int score_off;
8272 int minscore;
8273 int round;
8274 char_u *p;
8275 int wbadword[MAXWLEN];
8276 int wgoodword[MAXWLEN];
8277
8278 /* Get the characters from the multi-byte strings and put them in an
8279 * int array for easy access. */
8280 bi = 0;
8281 for (p = badword; *p != NUL; )
8282 wbadword[bi++] = mb_cptr2char_adv(&p);
8283 wbadword[bi++] = 0;
8284 gi = 0;
8285 for (p = goodword; *p != NUL; )
8286 wgoodword[gi++] = mb_cptr2char_adv(&p);
8287 wgoodword[gi++] = 0;
8288
8289 /*
8290 * The idea is to go from start to end over the words. So long as
8291 * characters are equal just continue, this always gives the lowest score.
8292 * When there is a difference try several alternatives. Each alternative
8293 * increases "score" for the edit distance. Some of the alternatives are
8294 * pushed unto a stack and tried later, some are tried right away. At the
8295 * end of the word the score for one alternative is known. The lowest
8296 * possible score is stored in "minscore".
8297 */
8298 stackidx = 0;
8299 bi = 0;
8300 gi = 0;
8301 score = 0;
8302 minscore = limit + 1;
8303
8304 for (;;)
8305 {
8306 /* Skip over an equal part, score remains the same. */
8307 for (;;)
8308 {
8309 bc = wbadword[bi];
8310 gc = wgoodword[gi];
8311
8312 if (bc != gc) /* stop at a char that's different */
8313 break;
8314 if (bc == NUL) /* both words end */
8315 {
8316 if (score < minscore)
8317 minscore = score;
8318 goto pop; /* do next alternative */
8319 }
8320 ++bi;
8321 ++gi;
8322 }
8323
8324 if (gc == NUL) /* goodword ends, delete badword chars */
8325 {
8326 do
8327 {
8328 if ((score += SCORE_DEL) >= minscore)
8329 goto pop; /* do next alternative */
8330 } while (wbadword[++bi] != NUL);
8331 minscore = score;
8332 }
8333 else if (bc == NUL) /* badword ends, insert badword chars */
8334 {
8335 do
8336 {
8337 if ((score += SCORE_INS) >= minscore)
8338 goto pop; /* do next alternative */
8339 } while (wgoodword[++gi] != NUL);
8340 minscore = score;
8341 }
8342 else /* both words continue */
8343 {
8344 /* If not close to the limit, perform a change. Only try changes
8345 * that may lead to a lower score than "minscore".
8346 * round 0: try deleting a char from badword
8347 * round 1: try inserting a char in badword */
8348 for (round = 0; round <= 1; ++round)
8349 {
8350 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8351 if (score_off < minscore)
8352 {
8353 if (score_off + SCORE_EDIT_MIN >= minscore)
8354 {
8355 /* Near the limit, rest of the words must match. We
8356 * can check that right now, no need to push an item
8357 * onto the stack. */
8358 bi2 = bi + 1 - round;
8359 gi2 = gi + round;
8360 while (wgoodword[gi2] == wbadword[bi2])
8361 {
8362 if (wgoodword[gi2] == NUL)
8363 {
8364 minscore = score_off;
8365 break;
8366 }
8367 ++bi2;
8368 ++gi2;
8369 }
8370 }
8371 else
8372 {
8373 /* try deleting a character from badword later */
8374 stack[stackidx].badi = bi + 1 - round;
8375 stack[stackidx].goodi = gi + round;
8376 stack[stackidx].score = score_off;
8377 ++stackidx;
8378 }
8379 }
8380 }
8381
8382 if (score + SCORE_SWAP < minscore)
8383 {
8384 /* If swapping two characters makes a match then the
8385 * substitution is more expensive, thus there is no need to
8386 * try both. */
8387 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8388 {
8389 /* Swap two characters, that is: skip them. */
8390 gi += 2;
8391 bi += 2;
8392 score += SCORE_SWAP;
8393 continue;
8394 }
8395 }
8396
8397 /* Substitute one character for another which is the same
8398 * thing as deleting a character from both goodword and badword.
8399 * Use a better score when there is only a case difference. */
8400 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8401 score += SCORE_ICASE;
8402 else
8403 {
8404 /* For a similar character use SCORE_SIMILAR. */
8405 if (slang != NULL
8406 && slang->sl_has_map
8407 && similar_chars(slang, gc, bc))
8408 score += SCORE_SIMILAR;
8409 else
8410 score += SCORE_SUBST;
8411 }
8412
8413 if (score < minscore)
8414 {
8415 /* Do the substitution. */
8416 ++gi;
8417 ++bi;
8418 continue;
8419 }
8420 }
8421pop:
8422 /*
8423 * Get here to try the next alternative, pop it from the stack.
8424 */
8425 if (stackidx == 0) /* stack is empty, finished */
8426 break;
8427
8428 /* pop an item from the stack */
8429 --stackidx;
8430 gi = stack[stackidx].goodi;
8431 bi = stack[stackidx].badi;
8432 score = stack[stackidx].score;
8433 }
8434
8435 /* When the score goes over "limit" it may actually be much higher.
8436 * Return a very large number to avoid going below the limit when giving a
8437 * bonus. */
8438 if (minscore > limit)
8439 return SCORE_MAXMAX;
8440 return minscore;
8441}
8442#endif
8443
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008444/*
8445 * ":spellinfo"
8446 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008447 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008448ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008449{
8450 int lpi;
8451 langp_T *lp;
8452 char_u *p;
8453
8454 if (no_spell_checking(curwin))
8455 return;
8456
8457 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008458 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008459 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008460 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008461 msg_puts((char_u *)"file: ");
8462 msg_puts(lp->lp_slang->sl_fname);
8463 msg_putchar('\n');
8464 p = lp->lp_slang->sl_info;
8465 if (p != NULL)
8466 {
8467 msg_puts(p);
8468 msg_putchar('\n');
8469 }
8470 }
8471 msg_end();
8472}
8473
Bram Moolenaar4770d092006-01-12 23:22:24 +00008474#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8475#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008476#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008477#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8478#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008479
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008480/*
8481 * ":spelldump"
8482 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008483 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008484ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008485{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008486 char_u *spl;
8487 long dummy;
8488
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008489 if (no_spell_checking(curwin))
8490 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008491 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008492
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008493 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008494 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008495
8496 /* enable spelling locally in the new window */
8497 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008498 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008499 vim_free(spl);
8500
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008501 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008502 return;
8503
Bram Moolenaar860cae12010-06-05 23:22:07 +02008504 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008505
8506 /* Delete the empty line that we started with. */
8507 if (curbuf->b_ml.ml_line_count > 1)
8508 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8509
8510 redraw_later(NOT_VALID);
8511}
8512
8513/*
8514 * Go through all possible words and:
8515 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8516 * "ic" and "dir" are not used.
8517 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8518 */
8519 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008520spell_dump_compl(
8521 char_u *pat, /* leading part of the word */
8522 int ic, /* ignore case */
8523 int *dir, /* direction for adding matches */
8524 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008525{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008526 langp_T *lp;
8527 slang_T *slang;
8528 idx_T arridx[MAXWLEN];
8529 int curi[MAXWLEN];
8530 char_u word[MAXWLEN];
8531 int c;
8532 char_u *byts;
8533 idx_T *idxs;
8534 linenr_T lnum = 0;
8535 int round;
8536 int depth;
8537 int n;
8538 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008539 char_u *region_names = NULL; /* region names being used */
8540 int do_region = TRUE; /* dump region names and numbers */
8541 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008542 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008543 int dumpflags = dumpflags_arg;
8544 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008545
Bram Moolenaard0131a82006-03-04 21:46:13 +00008546 /* When ignoring case or when the pattern starts with capital pass this on
8547 * to dump_word(). */
8548 if (pat != NULL)
8549 {
8550 if (ic)
8551 dumpflags |= DUMPFLAG_ICASE;
8552 else
8553 {
8554 n = captype(pat, NULL);
8555 if (n == WF_ONECAP)
8556 dumpflags |= DUMPFLAG_ONECAP;
8557 else if (n == WF_ALLCAP
8558#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008559 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008560#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008561 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +00008562#endif
8563 )
8564 dumpflags |= DUMPFLAG_ALLCAP;
8565 }
8566 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008567
Bram Moolenaar7887d882005-07-01 22:33:52 +00008568 /* Find out if we can support regions: All languages must support the same
8569 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008570 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008571 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008572 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008573 p = lp->lp_slang->sl_regions;
8574 if (p[0] != 0)
8575 {
8576 if (region_names == NULL) /* first language with regions */
8577 region_names = p;
8578 else if (STRCMP(region_names, p) != 0)
8579 {
8580 do_region = FALSE; /* region names are different */
8581 break;
8582 }
8583 }
8584 }
8585
8586 if (do_region && region_names != NULL)
8587 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008588 if (pat == NULL)
8589 {
8590 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8591 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8592 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008593 }
8594 else
8595 do_region = FALSE;
8596
8597 /*
8598 * Loop over all files loaded for the entries in 'spelllang'.
8599 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008600 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008601 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008602 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008603 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008604 if (slang->sl_fbyts == NULL) /* reloading failed */
8605 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008606
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008607 if (pat == NULL)
8608 {
8609 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8610 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8611 }
8612
8613 /* When matching with a pattern and there are no prefixes only use
8614 * parts of the tree that match "pat". */
8615 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008616 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008617 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008618 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008619
8620 /* round 1: case-folded tree
8621 * round 2: keep-case tree */
8622 for (round = 1; round <= 2; ++round)
8623 {
8624 if (round == 1)
8625 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008626 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008627 byts = slang->sl_fbyts;
8628 idxs = slang->sl_fidxs;
8629 }
8630 else
8631 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008632 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008633 byts = slang->sl_kbyts;
8634 idxs = slang->sl_kidxs;
8635 }
8636 if (byts == NULL)
8637 continue; /* array is empty */
8638
8639 depth = 0;
8640 arridx[0] = 0;
8641 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008642 while (depth >= 0 && !got_int
8643 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008644 {
8645 if (curi[depth] > byts[arridx[depth]])
8646 {
8647 /* Done all bytes at this node, go up one level. */
8648 --depth;
8649 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008650 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008651 }
8652 else
8653 {
8654 /* Do one more byte at this node. */
8655 n = arridx[depth] + curi[depth];
8656 ++curi[depth];
8657 c = byts[n];
8658 if (c == 0)
8659 {
8660 /* End of word, deal with the word.
8661 * Don't use keep-case words in the fold-case tree,
8662 * they will appear in the keep-case tree.
8663 * Only use the word when the region matches. */
8664 flags = (int)idxs[n];
8665 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008666 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008667 && (do_region
8668 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008669 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008670 & lp->lp_region) != 0))
8671 {
8672 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008673 if (!do_region)
8674 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008675
8676 /* Dump the basic word if there is no prefix or
8677 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008678 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008679 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008680 {
8681 dump_word(slang, word, pat, dir,
8682 dumpflags, flags, lnum);
8683 if (pat == NULL)
8684 ++lnum;
8685 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008686
8687 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008688 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008689 lnum = dump_prefixes(slang, word, pat, dir,
8690 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008691 }
8692 }
8693 else
8694 {
8695 /* Normal char, go one level deeper. */
8696 word[depth++] = c;
8697 arridx[depth] = idxs[n];
8698 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008699
8700 /* Check if this characters matches with the pattern.
8701 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008702 * Always ignore case here, dump_word() will check
8703 * proper case later. This isn't exactly right when
8704 * length changes for multi-byte characters with
8705 * ignore case... */
8706 if (depth <= patlen
8707 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008708 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008709 }
8710 }
8711 }
8712 }
8713 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008714}
8715
8716/*
8717 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008718 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008719 */
8720 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008721dump_word(
8722 slang_T *slang,
8723 char_u *word,
8724 char_u *pat,
8725 int *dir,
8726 int dumpflags,
8727 int wordflags,
8728 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008729{
8730 int keepcap = FALSE;
8731 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008732 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008733 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008734 char_u badword[MAXWLEN + 10];
8735 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008736 int flags = wordflags;
8737
8738 if (dumpflags & DUMPFLAG_ONECAP)
8739 flags |= WF_ONECAP;
8740 if (dumpflags & DUMPFLAG_ALLCAP)
8741 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008742
Bram Moolenaar4770d092006-01-12 23:22:24 +00008743 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008744 {
8745 /* Need to fix case according to "flags". */
8746 make_case_word(word, cword, flags);
8747 p = cword;
8748 }
8749 else
8750 {
8751 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008752 if ((dumpflags & DUMPFLAG_KEEPCASE)
8753 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008754 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008755 keepcap = TRUE;
8756 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008757 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008758
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008759 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008760 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008761 /* Add flags and regions after a slash. */
8762 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008763 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008764 STRCPY(badword, p);
8765 STRCAT(badword, "/");
8766 if (keepcap)
8767 STRCAT(badword, "=");
8768 if (flags & WF_BANNED)
8769 STRCAT(badword, "!");
8770 else if (flags & WF_RARE)
8771 STRCAT(badword, "?");
8772 if (flags & WF_REGION)
8773 for (i = 0; i < 7; ++i)
8774 if (flags & (0x10000 << i))
8775 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8776 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008777 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008778
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008779 if (dumpflags & DUMPFLAG_COUNT)
8780 {
8781 hashitem_T *hi;
8782
8783 /* Include the word count for ":spelldump!". */
8784 hi = hash_find(&slang->sl_wordcount, tw);
8785 if (!HASHITEM_EMPTY(hi))
8786 {
8787 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8788 tw, HI2WC(hi)->wc_count);
8789 p = IObuff;
8790 }
8791 }
8792
8793 ml_append(lnum, p, (colnr_T)0, FALSE);
8794 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008795 else if (((dumpflags & DUMPFLAG_ICASE)
8796 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8797 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008798 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008799 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008800 /* if dir was BACKWARD then honor it just once */
8801 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008802}
8803
8804/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008805 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8806 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008807 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008808 * Return the updated line number.
8809 */
8810 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008811dump_prefixes(
8812 slang_T *slang,
8813 char_u *word, /* case-folded word */
8814 char_u *pat,
8815 int *dir,
8816 int dumpflags,
8817 int flags, /* flags with prefix ID */
8818 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008819{
8820 idx_T arridx[MAXWLEN];
8821 int curi[MAXWLEN];
8822 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008823 char_u word_up[MAXWLEN];
8824 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008825 int c;
8826 char_u *byts;
8827 idx_T *idxs;
8828 linenr_T lnum = startlnum;
8829 int depth;
8830 int n;
8831 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008832 int i;
8833
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008834 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008835 * upper-case letter in word_up[]. */
8836 c = PTR2CHAR(word);
8837 if (SPELL_TOUPPER(c) != c)
8838 {
8839 onecap_copy(word, word_up, TRUE);
8840 has_word_up = TRUE;
8841 }
8842
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008843 byts = slang->sl_pbyts;
8844 idxs = slang->sl_pidxs;
8845 if (byts != NULL) /* array not is empty */
8846 {
8847 /*
8848 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008849 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008850 */
8851 depth = 0;
8852 arridx[0] = 0;
8853 curi[0] = 1;
8854 while (depth >= 0 && !got_int)
8855 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008856 n = arridx[depth];
8857 len = byts[n];
8858 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008859 {
8860 /* Done all bytes at this node, go up one level. */
8861 --depth;
8862 line_breakcheck();
8863 }
8864 else
8865 {
8866 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008867 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008868 ++curi[depth];
8869 c = byts[n];
8870 if (c == 0)
8871 {
8872 /* End of prefix, find out how many IDs there are. */
8873 for (i = 1; i < len; ++i)
8874 if (byts[n + i] != 0)
8875 break;
8876 curi[depth] += i - 1;
8877
Bram Moolenaar53805d12005-08-01 07:08:33 +00008878 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8879 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008880 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008881 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008882 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008883 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008884 : flags, lnum);
8885 if (lnum != 0)
8886 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008887 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008888
8889 /* Check for prefix that matches the word when the
8890 * first letter is upper-case, but only if the prefix has
8891 * a condition. */
8892 if (has_word_up)
8893 {
8894 c = valid_word_prefix(i, n, flags, word_up, slang,
8895 TRUE);
8896 if (c != 0)
8897 {
8898 vim_strncpy(prefix + depth, word_up,
8899 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008900 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008901 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008902 : flags, lnum);
8903 if (lnum != 0)
8904 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008905 }
8906 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008907 }
8908 else
8909 {
8910 /* Normal char, go one level deeper. */
8911 prefix[depth++] = c;
8912 arridx[depth] = idxs[n];
8913 curi[depth] = 1;
8914 }
8915 }
8916 }
8917 }
8918
8919 return lnum;
8920}
8921
Bram Moolenaar95529562005-08-25 21:21:38 +00008922/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008923 * Move "p" to the end of word "start".
8924 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008925 */
8926 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008927spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008928{
8929 char_u *p = start;
8930
Bram Moolenaar860cae12010-06-05 23:22:07 +02008931 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008932 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008933 return p;
8934}
8935
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008936#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008937/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008938 * For Insert mode completion CTRL-X s:
8939 * Find start of the word in front of column "startcol".
8940 * We don't check if it is badly spelled, with completion we can only change
8941 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008942 * Returns the column number of the word.
8943 */
8944 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008945spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008946{
8947 char_u *line;
8948 char_u *p;
8949 int col = 0;
8950
Bram Moolenaar95529562005-08-25 21:21:38 +00008951 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008952 return startcol;
8953
8954 /* Find a word character before "startcol". */
8955 line = ml_get_curline();
8956 for (p = line + startcol; p > line; )
8957 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008958 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008959 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008960 break;
8961 }
8962
8963 /* Go back to start of the word. */
8964 while (p > line)
8965 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008966 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008967 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008968 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008969 break;
8970 col = 0;
8971 }
8972
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008973 return col;
8974}
8975
8976/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008977 * Need to check for 'spellcapcheck' now, the word is removed before
8978 * expand_spelling() is called. Therefore the ugly global variable.
8979 */
8980static int spell_expand_need_cap;
8981
8982 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008983spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00008984{
8985 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
8986}
8987
8988/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008989 * Get list of spelling suggestions.
8990 * Used for Insert mode completion CTRL-X ?.
8991 * Returns the number of matches. The matches are in "matchp[]", array of
8992 * allocated strings.
8993 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008994 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008995expand_spelling(
8996 linenr_T lnum UNUSED,
8997 char_u *pat,
8998 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008999{
9000 garray_T ga;
9001
Bram Moolenaar4770d092006-01-12 23:22:24 +00009002 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009003 *matchp = ga.ga_data;
9004 return ga.ga_len;
9005}
9006#endif
9007
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009008#endif /* FEAT_SPELL */