blob: 0e08e9e7b44acef9833542581f2b30d7a9ed304e [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);
254static int spell_iswordp_w(int *p, win_T *wp);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000255#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000256
257/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000258 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000259 */
260typedef enum
261{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000262 STATE_START = 0, /* At start of node check for NUL bytes (goodword
263 * ends); if badword ends there is a match, otherwise
264 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000265 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000266 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000267 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
268 STATE_PLAIN, /* Use each byte of the node. */
269 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000270 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000271 STATE_INS, /* Insert a byte in the bad word. */
272 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000273 STATE_UNSWAP, /* Undo swap two characters. */
274 STATE_SWAP3, /* Swap two characters over three. */
275 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000276 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000277 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000278 STATE_REP_INI, /* Prepare for using REP items. */
279 STATE_REP, /* Use matching REP items from the .aff file. */
280 STATE_REP_UNDO, /* Undo a REP item replacement. */
281 STATE_FINAL /* End of this node. */
282} state_T;
283
284/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000285 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000286 */
287typedef struct trystate_S
288{
Bram Moolenaarea424162005-06-16 21:51:00 +0000289 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000290 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000291 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000292 short ts_curi; /* index in list of child nodes */
293 char_u ts_fidx; /* index in fword[], case-folded bad word */
294 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
295 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000296 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000297 * PFD_PREFIXTREE or PFD_NOPREFIX */
298 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000299#ifdef FEAT_MBYTE
300 char_u ts_tcharlen; /* number of bytes in tword character */
301 char_u ts_tcharidx; /* current byte index in tword character */
302 char_u ts_isdiff; /* DIFF_ values */
303 char_u ts_fcharstart; /* index in fword where badword char started */
304#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000305 char_u ts_prewordlen; /* length of word in "preword[]" */
306 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000307 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000308 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000309 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000310 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000311 char_u ts_delidx; /* index in fword for char that was deleted,
312 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000313} trystate_T;
314
Bram Moolenaarea424162005-06-16 21:51:00 +0000315/* values for ts_isdiff */
316#define DIFF_NONE 0 /* no different byte (yet) */
317#define DIFF_YES 1 /* different byte found */
318#define DIFF_INSERT 2 /* inserting character */
319
Bram Moolenaard12a1322005-08-21 22:08:24 +0000320/* values for ts_flags */
321#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
322#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000323#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000324
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000325/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000326#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000327#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000328#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000329
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000330/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000331#define FIND_FOLDWORD 0 /* find word case-folded */
332#define FIND_KEEPWORD 1 /* find keep-case word */
333#define FIND_PREFIX 2 /* find word after prefix */
334#define FIND_COMPOUND 3 /* find case-folded compound word */
335#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000336
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100337static void find_word(matchinf_T *mip, int mode);
338static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap);
339static int can_compound(slang_T *slang, char_u *word, char_u *flags);
340static int can_be_compound(trystate_T *sp, slang_T *slang, char_u *compflags, int flag);
341static int match_compoundrule(slang_T *slang, char_u *compflags);
342static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req);
343static void find_prefix(matchinf_T *mip, int mode);
344static int fold_more(matchinf_T *mip);
345static int spell_valid_case(int wordflags, int treeflags);
346static int no_spell_checking(win_T *wp);
347static void spell_load_lang(char_u *lang);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100348static void int_wordlist_spl(char_u *fname);
349static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100350static int score_wordcount_adj(slang_T *slang, int score, char_u *word, int split);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100351static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100352static void clear_midword(win_T *buf);
353static void use_midword(slang_T *lp, win_T *buf);
354static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100355static int badword_captype(char_u *word, char_u *end);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100356static int check_need_cap(linenr_T lnum, colnr_T col);
357static 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 +0000358#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100359static void spell_suggest_expr(suginfo_T *su, char_u *expr);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000360#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100361static void spell_suggest_file(suginfo_T *su, char_u *fname);
362static void spell_suggest_intern(suginfo_T *su, int interactive);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100363static void spell_find_cleanup(suginfo_T *su);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100364static void allcap_copy(char_u *word, char_u *wcopy);
365static void suggest_try_special(suginfo_T *su);
366static void suggest_try_change(suginfo_T *su);
367static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
368static void go_deeper(trystate_T *stack, int depth, int score_add);
Bram Moolenaar53805d12005-08-01 07:08:33 +0000369#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100370static int nofold_len(char_u *fword, int flen, char_u *word);
Bram Moolenaar53805d12005-08-01 07:08:33 +0000371#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100372static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
373static void score_comp_sal(suginfo_T *su);
374static void score_combine(suginfo_T *su);
375static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
376static void suggest_try_soundalike_prep(void);
377static void suggest_try_soundalike(suginfo_T *su);
378static void suggest_try_soundalike_finish(void);
379static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
380static int soundfold_find(slang_T *slang, char_u *word);
381static void make_case_word(char_u *fword, char_u *cword, int flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100382static int similar_chars(slang_T *slang, int c1, int c2);
383static 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);
384static void check_suggestions(suginfo_T *su, garray_T *gap);
385static void add_banned(suginfo_T *su, char_u *word);
386static void rescore_suggestions(suginfo_T *su);
387static void rescore_one(suginfo_T *su, suggest_T *stp);
388static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100389static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
390static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000391#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100392static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000393#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100394static int soundalike_score(char_u *goodsound, char_u *badsound);
395static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
396static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000397#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100398static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000399#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100400static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
401static 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 +0000402
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000403
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000404/* Remember what "z?" replaced. */
405static char_u *repl_from = NULL;
406static char_u *repl_to = NULL;
407
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408/*
409 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000410 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000411 * "*attrp" is set to the highlight index for a badly spelled word. For a
412 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000413 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000414 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000415 * "capcol" is used to check for a Capitalised word after the end of a
416 * sentence. If it's zero then perform the check. Return the column where to
417 * check next, or -1 when no sentence end was found. If it's NULL then don't
418 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000419 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000420 * Returns the length of the word in bytes, also when it's OK, so that the
421 * caller can skip over the word.
422 */
423 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100424spell_check(
425 win_T *wp, /* current window */
426 char_u *ptr,
427 hlf_T *attrp,
428 int *capcol, /* column to check for Capital */
429 int docount) /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000430{
431 matchinf_T mi; /* Most things are put in "mi" so that it can
432 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000433 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000434 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000435 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000436 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000437 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000438
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000439 /* A word never starts at a space or a control character. Return quickly
440 * then, skipping over the character. */
441 if (*ptr <= ' ')
442 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000443
444 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200445 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000446 return 1;
447
Bram Moolenaar5195e452005-08-19 20:32:47 +0000448 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000449
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000450 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000451 * 0X99FF. But always do check spelling to find "3GPP" and "11
452 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000453 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100455 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
456 mi.mi_end = skipbin(ptr + 2);
457 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000458 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000459 else
460 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000461 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000462 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463
Bram Moolenaar0c405862005-06-22 22:26:26 +0000464 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000465 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000466 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200467 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000468 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000469 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000470 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100471 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200472 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000473
Bram Moolenaar860cae12010-06-05 23:22:07 +0200474 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000475 {
476 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000477 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000478 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000480 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000481 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000482 if (capcol != NULL)
483 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000484
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485 /* We always use the characters up to the next non-word character,
486 * also for bad words. */
487 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000488
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200490 mi.mi_capflags = 0;
491 mi.mi_cend = NULL;
492 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000493
Bram Moolenaar5195e452005-08-19 20:32:47 +0000494 /* case-fold the word with one non-word character, so that we can check
495 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000496 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100497 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000498
499 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
500 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000501 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000502
503 /* The word is bad unless we recognize it. */
504 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000505 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000506
507 /*
508 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000509 * We check them all, because a word may be matched longer in another
510 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000511 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200512 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000513 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200514 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000515
516 /* If reloading fails the language is still in the list but everything
517 * has been cleared. */
518 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
519 continue;
520
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000521 /* Check for a matching word in case-folded words. */
522 find_word(&mi, FIND_FOLDWORD);
523
524 /* Check for a matching word in keep-case words. */
525 find_word(&mi, FIND_KEEPWORD);
526
527 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000528 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000529
530 /* For a NOBREAK language, may want to use a word without a following
531 * word as a backup. */
532 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
533 && mi.mi_result2 != SP_BAD)
534 {
535 mi.mi_result = mi.mi_result2;
536 mi.mi_end = mi.mi_end2;
537 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000538
539 /* Count the word in the first language where it's found to be OK. */
540 if (count_word && mi.mi_result == SP_OK)
541 {
542 count_common_word(mi.mi_lp->lp_slang, ptr,
543 (int)(mi.mi_end - ptr), 1);
544 count_word = FALSE;
545 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000546 }
547
548 if (mi.mi_result != SP_OK)
549 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000550 /* If we found a number skip over it. Allows for "42nd". Do flag
551 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000552 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000553 {
554 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
555 return nrlen;
556 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000557
558 /* When we are at a non-word character there is no error, just
559 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100560 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000561 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200562 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000563 {
564 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100565 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000566
567 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200568 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000569 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100570 r = vim_regexec(&regmatch, ptr, 0);
571 wp->w_s->b_cap_prog = regmatch.regprog;
572 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000573 *capcol = (int)(regmatch.endp[0] - ptr);
574 }
575
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000576#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000577 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000578 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000579#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000580 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000581 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000582 else if (mi.mi_end == ptr)
583 /* Always include at least one character. Required for when there
584 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100585 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000586 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200587 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000588 {
589 char_u *p, *fp;
590 int save_result = mi.mi_result;
591
592 /* First language in 'spelllang' is NOBREAK. Find first position
593 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200594 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000595 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000596 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000597 p = mi.mi_word;
598 fp = mi.mi_fword;
599 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000600 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100601 MB_PTR_ADV(p);
602 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000603 if (p >= mi.mi_end)
604 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000605 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000606 find_word(&mi, FIND_COMPOUND);
607 if (mi.mi_result != SP_BAD)
608 {
609 mi.mi_end = p;
610 break;
611 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000612 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000613 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000614 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000615 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616
617 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000618 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000619 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000620 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000622 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000623 }
624
Bram Moolenaar5195e452005-08-19 20:32:47 +0000625 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
626 {
627 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000628 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000629 return wrongcaplen;
630 }
631
Bram Moolenaar51485f02005-06-04 21:55:20 +0000632 return (int)(mi.mi_end - ptr);
633}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000634
Bram Moolenaar51485f02005-06-04 21:55:20 +0000635/*
636 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000637 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
638 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
639 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
640 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000641 *
642 * For a match mip->mi_result is updated.
643 */
644 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100645find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000646{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000647 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000648 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000649 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000650 int endidxcnt = 0;
651 int len;
652 int wlen = 0;
653 int flen;
654 int c;
655 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000657#ifdef FEAT_MBYTE
658 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000659#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +0000660 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000661 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000662 slang_T *slang = mip->mi_lp->lp_slang;
663 unsigned flags;
664 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000665 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000666 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000667 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000668 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000669
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000671 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000672 /* Check for word with matching case in keep-case tree. */
673 ptr = mip->mi_word;
674 flen = 9999; /* no case folding, always enough bytes */
675 byts = slang->sl_kbyts;
676 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000677
678 if (mode == FIND_KEEPCOMPOUND)
679 /* Skip over the previously found word(s). */
680 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000681 }
682 else
683 {
684 /* Check for case-folded in case-folded tree. */
685 ptr = mip->mi_fword;
686 flen = mip->mi_fwordlen; /* available case-folded bytes */
687 byts = slang->sl_fbyts;
688 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000689
690 if (mode == FIND_PREFIX)
691 {
692 /* Skip over the prefix. */
693 wlen = mip->mi_prefixlen;
694 flen -= mip->mi_prefixlen;
695 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000696 else if (mode == FIND_COMPOUND)
697 {
698 /* Skip over the previously found word(s). */
699 wlen = mip->mi_compoff;
700 flen -= mip->mi_compoff;
701 }
702
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000703 }
704
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 if (byts == NULL)
706 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000707
Bram Moolenaar51485f02005-06-04 21:55:20 +0000708 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000709 * Repeat advancing in the tree until:
710 * - there is a byte that doesn't match,
711 * - we reach the end of the tree,
712 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000713 */
714 for (;;)
715 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000716 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000717 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000718
719 len = byts[arridx++];
720
721 /* If the first possible byte is a zero the word could end here.
722 * Remember this index, we first check for the longest word. */
723 if (byts[arridx] == 0)
724 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000725 if (endidxcnt == MAXWLEN)
726 {
727 /* Must be a corrupted spell file. */
728 EMSG(_(e_format));
729 return;
730 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000731 endlen[endidxcnt] = wlen;
732 endidx[endidxcnt++] = arridx++;
733 --len;
734
735 /* Skip over the zeros, there can be several flag/region
736 * combinations. */
737 while (len > 0 && byts[arridx] == 0)
738 {
739 ++arridx;
740 --len;
741 }
742 if (len == 0)
743 break; /* no children, word must end here */
744 }
745
746 /* Stop looking at end of the line. */
747 if (ptr[wlen] == NUL)
748 break;
749
750 /* Perform a binary search in the list of accepted bytes. */
751 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000752 if (c == TAB) /* <Tab> is handled like <Space> */
753 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000754 lo = arridx;
755 hi = arridx + len - 1;
756 while (lo < hi)
757 {
758 m = (lo + hi) / 2;
759 if (byts[m] > c)
760 hi = m - 1;
761 else if (byts[m] < c)
762 lo = m + 1;
763 else
764 {
765 lo = hi = m;
766 break;
767 }
768 }
769
770 /* Stop if there is no matching byte. */
771 if (hi < lo || byts[lo] != c)
772 break;
773
774 /* Continue at the child (if there is one). */
775 arridx = idxs[lo];
776 ++wlen;
777 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000778
779 /* One space in the good word may stand for several spaces in the
780 * checked word. */
781 if (c == ' ')
782 {
783 for (;;)
784 {
785 if (flen <= 0 && *mip->mi_fend != NUL)
786 flen = fold_more(mip);
787 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
788 break;
789 ++wlen;
790 --flen;
791 }
792 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000793 }
794
795 /*
796 * Verify that one of the possible endings is valid. Try the longest
797 * first.
798 */
799 while (endidxcnt > 0)
800 {
801 --endidxcnt;
802 arridx = endidx[endidxcnt];
803 wlen = endlen[endidxcnt];
804
805#ifdef FEAT_MBYTE
806 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
807 continue; /* not at first byte of character */
808#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200809 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000810 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000811 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000812 continue; /* next char is a word character */
813 word_ends = FALSE;
814 }
815 else
816 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000817 /* The prefix flag is before compound flags. Once a valid prefix flag
818 * has been found we try compound flags. */
819 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000820
821#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000822 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000823 {
824 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000825 * when folding case. This can be slow, take a shortcut when the
826 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000827 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000828 if (STRNCMP(ptr, p, wlen) != 0)
829 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100830 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
831 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000832 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 }
835#endif
836
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000837 /* Check flags and region. For FIND_PREFIX check the condition and
838 * prefix ID.
839 * Repeat this if there are more flags/region alternatives until there
840 * is a match. */
841 res = SP_BAD;
842 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
843 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000844 {
845 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000846
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000847 /* For the fold-case tree check that the case of the checked word
848 * matches with what the word in the tree requires.
849 * For keep-case tree the case is always right. For prefixes we
850 * don't bother to check. */
851 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000852 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 if (mip->mi_cend != mip->mi_word + wlen)
854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000855 /* mi_capflags was set for a different word length, need
856 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000858 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000859 }
860
Bram Moolenaar0c405862005-06-22 22:26:26 +0000861 if (mip->mi_capflags == WF_KEEPCAP
862 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000863 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000864 }
865
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000866 /* When mode is FIND_PREFIX the word must support the prefix:
867 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000868 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000869 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000871 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000872 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000873 mip->mi_word + mip->mi_cprefixlen, slang,
874 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000875 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000876 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000877
878 /* Use the WF_RARE flag for a rare prefix. */
879 if (c & WF_RAREPFX)
880 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000881 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000882 }
883
Bram Moolenaar78622822005-08-23 21:00:13 +0000884 if (slang->sl_nobreak)
885 {
886 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
887 && (flags & WF_BANNED) == 0)
888 {
889 /* NOBREAK: found a valid following word. That's all we
890 * need to know, so return. */
891 mip->mi_result = SP_OK;
892 break;
893 }
894 }
895
896 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
897 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000898 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000899 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000900 * COMPOUNDMIN reject it quickly.
901 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000902 * that's too short... Myspell compatibility requires this
903 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000904 if (((unsigned)flags >> 24) == 0
905 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000906 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000907#ifdef FEAT_MBYTE
908 /* For multi-byte chars check character length against
909 * COMPOUNDMIN. */
910 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000911 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000912 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
913 wlen - mip->mi_compoff) < slang->sl_compminlen)
914 continue;
915#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000916
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000917 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000918 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000919 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
920 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000921 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000922 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000923
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000924 /* Don't allow compounding on a side where an affix was added,
925 * unless COMPOUNDPERMITFLAG was used. */
926 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
927 continue;
928 if (!word_ends && (flags & WF_NOCOMPAFT))
929 continue;
930
Bram Moolenaard12a1322005-08-21 22:08:24 +0000931 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000932 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000933 ? slang->sl_compstartflags
934 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000935 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000936 continue;
937
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000938 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
939 * discard the compound word. */
940 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
941 continue;
942
Bram Moolenaare52325c2005-08-22 22:54:29 +0000943 if (mode == FIND_COMPOUND)
944 {
945 int capflags;
946
947 /* Need to check the caps type of the appended compound
948 * word. */
949#ifdef FEAT_MBYTE
950 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
951 mip->mi_compoff) != 0)
952 {
953 /* case folding may have changed the length */
954 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100955 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
956 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000957 }
958 else
959#endif
960 p = mip->mi_word + mip->mi_compoff;
961 capflags = captype(p, mip->mi_word + wlen);
962 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
963 && (flags & WF_FIXCAP) != 0))
964 continue;
965
966 if (capflags != WF_ALLCAP)
967 {
968 /* When the character before the word is a word
969 * character we do not accept a Onecap word. We do
970 * accept a no-caps word, even when the dictionary
971 * word specifies ONECAP. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100972 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100973 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000974 ? capflags == WF_ONECAP
975 : (flags & WF_ONECAP) != 0
976 && capflags != WF_ONECAP)
977 continue;
978 }
979 }
980
Bram Moolenaar5195e452005-08-19 20:32:47 +0000981 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000982 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000983 * the number of syllables must not be too large. */
984 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
985 mip->mi_compflags[mip->mi_complen + 1] = NUL;
986 if (word_ends)
987 {
988 char_u fword[MAXWLEN];
989
990 if (slang->sl_compsylmax < MAXWLEN)
991 {
992 /* "fword" is only needed for checking syllables. */
993 if (ptr == mip->mi_word)
994 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
995 else
996 vim_strncpy(fword, ptr, endlen[endidxcnt]);
997 }
998 if (!can_compound(slang, fword, mip->mi_compflags))
999 continue;
1000 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001001 else if (slang->sl_comprules != NULL
1002 && !match_compoundrule(slang, mip->mi_compflags))
1003 /* The compound flags collected so far do not match any
1004 * COMPOUNDRULE, discard the compounded word. */
1005 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001006 }
1007
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001008 /* Check NEEDCOMPOUND: can't use word without compounding. */
1009 else if (flags & WF_NEEDCOMP)
1010 continue;
1011
Bram Moolenaar78622822005-08-23 21:00:13 +00001012 nobreak_result = SP_OK;
1013
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001014 if (!word_ends)
1015 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001016 int save_result = mip->mi_result;
1017 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001018 langp_T *save_lp = mip->mi_lp;
1019 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001020
1021 /* Check that a valid word follows. If there is one and we
1022 * are compounding, it will set "mi_result", thus we are
1023 * always finished here. For NOBREAK we only check that a
1024 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001025 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001026 if (slang->sl_nobreak)
1027 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001028
1029 /* Find following word in case-folded tree. */
1030 mip->mi_compoff = endlen[endidxcnt];
1031#ifdef FEAT_MBYTE
1032 if (has_mbyte && mode == FIND_KEEPWORD)
1033 {
1034 /* Compute byte length in case-folded word from "wlen":
1035 * byte length in keep-case word. Length may change when
1036 * folding case. This can be slow, take a shortcut when
1037 * the case-folded word is equal to the keep-case word. */
1038 p = mip->mi_fword;
1039 if (STRNCMP(ptr, p, wlen) != 0)
1040 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001041 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
1042 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001043 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001044 }
1045 }
1046#endif
Bram Moolenaarba534352016-04-21 09:20:26 +02001047#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001048 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001049#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001050 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001051 if (flags & WF_COMPROOT)
1052 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001053
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001054 /* For NOBREAK we need to try all NOBREAK languages, at least
1055 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001056 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001057 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001058 if (slang->sl_nobreak)
1059 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001060 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001061 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1062 || !mip->mi_lp->lp_slang->sl_nobreak)
1063 continue;
1064 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001065
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001066 find_word(mip, FIND_COMPOUND);
1067
1068 /* When NOBREAK any word that matches is OK. Otherwise we
1069 * need to find the longest match, thus try with keep-case
1070 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001071 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1072 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001073 /* Find following word in keep-case tree. */
1074 mip->mi_compoff = wlen;
1075 find_word(mip, FIND_KEEPCOMPOUND);
1076
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001077#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1078 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1079 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001080 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1081 {
1082 /* Check for following word with prefix. */
1083 mip->mi_compoff = c;
1084 find_prefix(mip, FIND_COMPOUND);
1085 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001086#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001087 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001088
1089 if (!slang->sl_nobreak)
1090 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001091 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001092 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001093 if (flags & WF_COMPROOT)
1094 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001095 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001096
Bram Moolenaar78622822005-08-23 21:00:13 +00001097 if (slang->sl_nobreak)
1098 {
1099 nobreak_result = mip->mi_result;
1100 mip->mi_result = save_result;
1101 mip->mi_end = save_end;
1102 }
1103 else
1104 {
1105 if (mip->mi_result == SP_OK)
1106 break;
1107 continue;
1108 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001109 }
1110
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001111 if (flags & WF_BANNED)
1112 res = SP_BANNED;
1113 else if (flags & WF_REGION)
1114 {
1115 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001116 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001117 res = SP_OK;
1118 else
1119 res = SP_LOCAL;
1120 }
1121 else if (flags & WF_RARE)
1122 res = SP_RARE;
1123 else
1124 res = SP_OK;
1125
Bram Moolenaar78622822005-08-23 21:00:13 +00001126 /* Always use the longest match and the best result. For NOBREAK
1127 * we separately keep the longest match without a following good
1128 * word as a fall-back. */
1129 if (nobreak_result == SP_BAD)
1130 {
1131 if (mip->mi_result2 > res)
1132 {
1133 mip->mi_result2 = res;
1134 mip->mi_end2 = mip->mi_word + wlen;
1135 }
1136 else if (mip->mi_result2 == res
1137 && mip->mi_end2 < mip->mi_word + wlen)
1138 mip->mi_end2 = mip->mi_word + wlen;
1139 }
1140 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001141 {
1142 mip->mi_result = res;
1143 mip->mi_end = mip->mi_word + wlen;
1144 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001145 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001146 mip->mi_end = mip->mi_word + wlen;
1147
Bram Moolenaar78622822005-08-23 21:00:13 +00001148 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001149 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001150 }
1151
Bram Moolenaar78622822005-08-23 21:00:13 +00001152 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001153 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001154 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001155}
1156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001157/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001158 * Return TRUE if there is a match between the word ptr[wlen] and
1159 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1160 * word.
1161 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1162 * end of ptr[wlen] and the second part matches after it.
1163 */
1164 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001165match_checkcompoundpattern(
1166 char_u *ptr,
1167 int wlen,
1168 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001169{
1170 int i;
1171 char_u *p;
1172 int len;
1173
1174 for (i = 0; i + 1 < gap->ga_len; i += 2)
1175 {
1176 p = ((char_u **)gap->ga_data)[i + 1];
1177 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1178 {
1179 /* Second part matches at start of following compound word, now
1180 * check if first part matches at end of previous word. */
1181 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001182 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001183 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1184 return TRUE;
1185 }
1186 }
1187 return FALSE;
1188}
1189
1190/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001191 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1192 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001193 */
1194 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001195can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001196{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001197#ifdef FEAT_MBYTE
1198 char_u uflags[MAXWLEN * 2];
1199 int i;
1200#endif
1201 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001202
1203 if (slang->sl_compprog == NULL)
1204 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001205#ifdef FEAT_MBYTE
1206 if (enc_utf8)
1207 {
1208 /* Need to convert the single byte flags to utf8 characters. */
1209 p = uflags;
1210 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001211 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001212 *p = NUL;
1213 p = uflags;
1214 }
1215 else
1216#endif
1217 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001218 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001219 return FALSE;
1220
Bram Moolenaare52325c2005-08-22 22:54:29 +00001221 /* Count the number of syllables. This may be slow, do it last. If there
1222 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001223 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001224 if (slang->sl_compsylmax < MAXWLEN
1225 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001226 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001227 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001228}
1229
1230/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001231 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1232 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1233 * lines if they don't contain wildcards.
1234 */
1235 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001236can_be_compound(
1237 trystate_T *sp,
1238 slang_T *slang,
1239 char_u *compflags,
1240 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001241{
1242 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1243 * then it can't possibly compound. */
1244 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1245 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1246 return FALSE;
1247
1248 /* If there are no wildcards, we can check if the flags collected so far
1249 * possibly can form a match with COMPOUNDRULE patterns. This only
1250 * makes sense when we have two or more words. */
1251 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1252 {
1253 int v;
1254
1255 compflags[sp->ts_complen] = flag;
1256 compflags[sp->ts_complen + 1] = NUL;
1257 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1258 compflags[sp->ts_complen] = NUL;
1259 return v;
1260 }
1261
1262 return TRUE;
1263}
1264
1265
1266/*
1267 * Return TRUE if the compound flags in compflags[] match the start of any
1268 * compound rule. This is used to stop trying a compound if the flags
1269 * collected so far can't possibly match any compound rule.
1270 * Caller must check that slang->sl_comprules is not NULL.
1271 */
1272 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001273match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001274{
1275 char_u *p;
1276 int i;
1277 int c;
1278
1279 /* loop over all the COMPOUNDRULE entries */
1280 for (p = slang->sl_comprules; *p != NUL; ++p)
1281 {
1282 /* loop over the flags in the compound word we have made, match
1283 * them against the current rule entry */
1284 for (i = 0; ; ++i)
1285 {
1286 c = compflags[i];
1287 if (c == NUL)
1288 /* found a rule that matches for the flags we have so far */
1289 return TRUE;
1290 if (*p == '/' || *p == NUL)
1291 break; /* end of rule, it's too short */
1292 if (*p == '[')
1293 {
1294 int match = FALSE;
1295
1296 /* compare against all the flags in [] */
1297 ++p;
1298 while (*p != ']' && *p != NUL)
1299 if (*p++ == c)
1300 match = TRUE;
1301 if (!match)
1302 break; /* none matches */
1303 }
1304 else if (*p != c)
1305 break; /* flag of word doesn't match flag in pattern */
1306 ++p;
1307 }
1308
1309 /* Skip to the next "/", where the next pattern starts. */
1310 p = vim_strchr(p, '/');
1311 if (p == NULL)
1312 break;
1313 }
1314
1315 /* Checked all the rules and none of them match the flags, so there
1316 * can't possibly be a compound starting with these flags. */
1317 return FALSE;
1318}
1319
1320/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001321 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1322 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001323 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001324 */
1325 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001326valid_word_prefix(
1327 int totprefcnt, /* nr of prefix IDs */
1328 int arridx, /* idx in sl_pidxs[] */
1329 int flags,
1330 char_u *word,
1331 slang_T *slang,
1332 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001333{
1334 int prefcnt;
1335 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001336 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001337 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001338
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001339 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001340 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1341 {
1342 pidx = slang->sl_pidxs[arridx + prefcnt];
1343
1344 /* Check the prefix ID. */
1345 if (prefid != (pidx & 0xff))
1346 continue;
1347
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001348 /* Check if the prefix doesn't combine and the word already has a
1349 * suffix. */
1350 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1351 continue;
1352
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001353 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001354 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001355 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1356 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001357 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001358 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001359 continue;
1360 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001361 else if (cond_req)
1362 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001363
Bram Moolenaar53805d12005-08-01 07:08:33 +00001364 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001365 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001366 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001367 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001368}
1369
1370/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001371 * Check if the word at "mip->mi_word" has a matching prefix.
1372 * If it does, then check the following word.
1373 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001374 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1375 * prefix in a compound word.
1376 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001377 * For a match mip->mi_result is updated.
1378 */
1379 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001380find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001381{
1382 idx_T arridx = 0;
1383 int len;
1384 int wlen = 0;
1385 int flen;
1386 int c;
1387 char_u *ptr;
1388 idx_T lo, hi, m;
1389 slang_T *slang = mip->mi_lp->lp_slang;
1390 char_u *byts;
1391 idx_T *idxs;
1392
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001393 byts = slang->sl_pbyts;
1394 if (byts == NULL)
1395 return; /* array is empty */
1396
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001397 /* We use the case-folded word here, since prefixes are always
1398 * case-folded. */
1399 ptr = mip->mi_fword;
1400 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001401 if (mode == FIND_COMPOUND)
1402 {
1403 /* Skip over the previously found word(s). */
1404 ptr += mip->mi_compoff;
1405 flen -= mip->mi_compoff;
1406 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001407 idxs = slang->sl_pidxs;
1408
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 /*
1410 * Repeat advancing in the tree until:
1411 * - there is a byte that doesn't match,
1412 * - we reach the end of the tree,
1413 * - or we reach the end of the line.
1414 */
1415 for (;;)
1416 {
1417 if (flen == 0 && *mip->mi_fend != NUL)
1418 flen = fold_more(mip);
1419
1420 len = byts[arridx++];
1421
1422 /* If the first possible byte is a zero the prefix could end here.
1423 * Check if the following word matches and supports the prefix. */
1424 if (byts[arridx] == 0)
1425 {
1426 /* There can be several prefixes with different conditions. We
1427 * try them all, since we don't know which one will give the
1428 * longest match. The word is the same each time, pass the list
1429 * of possible prefixes to find_word(). */
1430 mip->mi_prefarridx = arridx;
1431 mip->mi_prefcnt = len;
1432 while (len > 0 && byts[arridx] == 0)
1433 {
1434 ++arridx;
1435 --len;
1436 }
1437 mip->mi_prefcnt -= len;
1438
1439 /* Find the word that comes after the prefix. */
1440 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001441 if (mode == FIND_COMPOUND)
1442 /* Skip over the previously found word(s). */
1443 mip->mi_prefixlen += mip->mi_compoff;
1444
Bram Moolenaar53805d12005-08-01 07:08:33 +00001445#ifdef FEAT_MBYTE
1446 if (has_mbyte)
1447 {
1448 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001449 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1450 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001451 }
1452 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001453 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001454#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001455 find_word(mip, FIND_PREFIX);
1456
1457
1458 if (len == 0)
1459 break; /* no children, word must end here */
1460 }
1461
1462 /* Stop looking at end of the line. */
1463 if (ptr[wlen] == NUL)
1464 break;
1465
1466 /* Perform a binary search in the list of accepted bytes. */
1467 c = ptr[wlen];
1468 lo = arridx;
1469 hi = arridx + len - 1;
1470 while (lo < hi)
1471 {
1472 m = (lo + hi) / 2;
1473 if (byts[m] > c)
1474 hi = m - 1;
1475 else if (byts[m] < c)
1476 lo = m + 1;
1477 else
1478 {
1479 lo = hi = m;
1480 break;
1481 }
1482 }
1483
1484 /* Stop if there is no matching byte. */
1485 if (hi < lo || byts[lo] != c)
1486 break;
1487
1488 /* Continue at the child (if there is one). */
1489 arridx = idxs[lo];
1490 ++wlen;
1491 --flen;
1492 }
1493}
1494
1495/*
1496 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001497 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001498 * Return the length of the folded chars in bytes.
1499 */
1500 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001501fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001502{
1503 int flen;
1504 char_u *p;
1505
1506 p = mip->mi_fend;
1507 do
1508 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001509 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001510 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001511
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001512 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001513 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001514 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001515
1516 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1517 mip->mi_fword + mip->mi_fwordlen,
1518 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001519 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001520 mip->mi_fwordlen += flen;
1521 return flen;
1522}
1523
1524/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001525 * Check case flags for a word. Return TRUE if the word has the requested
1526 * case.
1527 */
1528 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001529spell_valid_case(
1530 int wordflags, /* flags for the checked word. */
1531 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001532{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001533 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001534 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001535 && ((treeflags & WF_ONECAP) == 0
1536 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001537}
1538
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001539/*
1540 * Return TRUE if spell checking is not enabled.
1541 */
1542 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001543no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001544{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001545 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1546 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001547 {
1548 EMSG(_("E756: Spell checking is not enabled"));
1549 return TRUE;
1550 }
1551 return FALSE;
1552}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001553
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001554/*
1555 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001556 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1557 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001558 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1559 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001560 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001561 */
1562 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001563spell_move_to(
1564 win_T *wp,
1565 int dir, /* FORWARD or BACKWARD */
1566 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
1567 int curline,
1568 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001569 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001570{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001571 linenr_T lnum;
1572 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001573 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001574 char_u *line;
1575 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001576 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001577 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001578 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001579#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001580 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001581#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001582 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001583 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001584 char_u *buf = NULL;
1585 int buflen = 0;
1586 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001587 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001588 int found_one = FALSE;
1589 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001590
Bram Moolenaar95529562005-08-25 21:21:38 +00001591 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001592 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001593
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001594 /*
1595 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001596 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001597 *
1598 * When searching backwards, we continue in the line to find the last
1599 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001600 *
1601 * We concatenate the start of the next line, so that wrapped words work
1602 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1603 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001604 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001605 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001606 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001607
1608 while (!got_int)
1609 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001610 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001611
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001612 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001613 if (buflen < len + MAXWLEN + 2)
1614 {
1615 vim_free(buf);
1616 buflen = len + MAXWLEN + 2;
1617 buf = alloc(buflen);
1618 if (buf == NULL)
1619 break;
1620 }
1621
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001622 /* In first line check first word for Capital. */
1623 if (lnum == 1)
1624 capcol = 0;
1625
1626 /* For checking first word with a capital skip white space. */
1627 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001628 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001629 else if (curline && wp == curwin)
1630 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001631 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001632 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001633 if (check_need_cap(lnum, col))
1634 capcol = col;
1635
1636 /* Need to get the line again, may have looked at the previous
1637 * one. */
1638 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1639 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001640
Bram Moolenaar0c405862005-06-22 22:26:26 +00001641 /* Copy the line into "buf" and append the start of the next line if
1642 * possible. */
1643 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001644 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001645 spell_cat_line(buf + STRLEN(buf),
1646 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001647
1648 p = buf + skip;
1649 endp = buf + len;
1650 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001651 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001652 /* When searching backward don't search after the cursor. Unless
1653 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001654 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001655 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001656 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001657 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001658 break;
1659
1660 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001661 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001662 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001663
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001664 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001665 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001666 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001667 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001668 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001669 /* When searching forward only accept a bad word after
1670 * the cursor. */
1671 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001672 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001673 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001674 && (wrapped
1675 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001676 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001677 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001678 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001679#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001680 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001681 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001682 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001683 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001684 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001685 if (!can_spell)
1686 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001687 }
1688 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001689#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001690 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001691
Bram Moolenaar51485f02005-06-04 21:55:20 +00001692 if (can_spell)
1693 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001694 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001695 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001696 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001697#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001698 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001699#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001700 if (dir == FORWARD)
1701 {
1702 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001703 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001704 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001705 if (attrp != NULL)
1706 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001707 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001708 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001709 else if (curline)
1710 /* Insert mode completion: put cursor after
1711 * the bad word. */
1712 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001713 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001714 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001715 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001716 else
1717 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001718 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001719 }
1720
Bram Moolenaar51485f02005-06-04 21:55:20 +00001721 /* advance to character after the word */
1722 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001723 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001724 }
1725
Bram Moolenaar5195e452005-08-19 20:32:47 +00001726 if (dir == BACKWARD && found_pos.lnum != 0)
1727 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001728 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001729 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001730 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001731 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001732 }
1733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001734 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001735 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001736
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001737 /* If we are back at the starting line and searched it again there
1738 * is no match, give up. */
1739 if (lnum == wp->w_cursor.lnum && wrapped)
1740 break;
1741
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001742 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001743 if (dir == BACKWARD)
1744 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001745 if (lnum > 1)
1746 --lnum;
1747 else if (!p_ws)
1748 break; /* at first line and 'nowrapscan' */
1749 else
1750 {
1751 /* Wrap around to the end of the buffer. May search the
1752 * starting line again and accept the last match. */
1753 lnum = wp->w_buffer->b_ml.ml_line_count;
1754 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001755 if (!shortmess(SHM_SEARCH))
1756 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001757 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001758 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001759 }
1760 else
1761 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001762 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1763 ++lnum;
1764 else if (!p_ws)
1765 break; /* at first line and 'nowrapscan' */
1766 else
1767 {
1768 /* Wrap around to the start of the buffer. May search the
1769 * starting line again and accept the first match. */
1770 lnum = 1;
1771 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001772 if (!shortmess(SHM_SEARCH))
1773 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001774 }
1775
1776 /* If we are back at the starting line and there is no match then
1777 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001778 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001779 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001780
1781 /* Skip the characters at the start of the next line that were
1782 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001783 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001784 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001785 else
1786 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001787
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001788 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001789 --capcol;
1790
1791 /* But after empty line check first word in next line */
1792 if (*skipwhite(line) == NUL)
1793 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001794 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001795
1796 line_breakcheck();
1797 }
1798
Bram Moolenaar0c405862005-06-22 22:26:26 +00001799 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001800 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001801}
1802
1803/*
1804 * For spell checking: concatenate the start of the following line "line" into
1805 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001806 * Keep the blanks at the start of the next line, this is used in win_line()
1807 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001808 */
1809 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001810spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001811{
1812 char_u *p;
1813 int n;
1814
1815 p = skipwhite(line);
1816 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1817 p = skipwhite(p + 1);
1818
1819 if (*p != NUL)
1820 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001821 /* Only worth concatenating if there is something else than spaces to
1822 * concatenate. */
1823 n = (int)(p - line) + 1;
1824 if (n < maxlen - 1)
1825 {
1826 vim_memset(buf, ' ', n);
1827 vim_strncpy(buf + n, p, maxlen - 1 - n);
1828 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001829 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830}
1831
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001832/*
1833 * Structure used for the cookie argument of do_in_runtimepath().
1834 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001835typedef struct spelload_S
1836{
1837 char_u sl_lang[MAXWLEN + 1]; /* language name */
1838 slang_T *sl_slang; /* resulting slang_T struct */
1839 int sl_nobreak; /* NOBREAK language found */
1840} spelload_T;
1841
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001842/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001843 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001844 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001845 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001846 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001847spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001848{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001849 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001850 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001851 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001852#ifdef FEAT_AUTOCMD
1853 int round;
1854#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001855
Bram Moolenaarb765d632005-06-07 21:00:02 +00001856 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001857 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001858 STRCPY(sl.sl_lang, lang);
1859 sl.sl_slang = NULL;
1860 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001861
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001862#ifdef FEAT_AUTOCMD
1863 /* We may retry when no spell file is found for the language, an
1864 * autocommand may load it then. */
1865 for (round = 1; round <= 2; ++round)
1866#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001867 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001868 /*
1869 * Find the first spell file for "lang" in 'runtimepath' and load it.
1870 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001871 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001872#ifdef VMS
1873 "spell/%s_%s.spl",
1874#else
1875 "spell/%s.%s.spl",
1876#endif
1877 lang, spell_enc());
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
1880 if (r == FAIL && *sl.sl_lang != NUL)
1881 {
1882 /* Try loading the ASCII version. */
1883 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001884#ifdef VMS
1885 "spell/%s_ascii.spl",
1886#else
1887 "spell/%s.ascii.spl",
1888#endif
1889 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001890 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001891
1892#ifdef FEAT_AUTOCMD
1893 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1894 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1895 curbuf->b_fname, FALSE, curbuf))
1896 continue;
1897 break;
1898#endif
1899 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001900#ifdef FEAT_AUTOCMD
1901 break;
1902#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903 }
1904
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001905 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001906 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01001907 smsg((char_u *)
1908#ifdef VMS
1909 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1910#else
1911 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1912#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001913 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001914 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001915 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001916 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001917 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001918 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001919 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001920 }
1921}
1922
1923/*
1924 * Return the encoding used for spell checking: Use 'encoding', except that we
1925 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1926 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001927 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001928spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001929{
1930
1931#ifdef FEAT_MBYTE
1932 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1933 return p_enc;
1934#endif
1935 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001936}
1937
1938/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001939 * Get the name of the .spl file for the internal wordlist into
1940 * "fname[MAXPATHL]".
1941 */
1942 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001943int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001944{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001945 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001946 int_wordlist, spell_enc());
1947}
1948
1949/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001950 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 * Caller must fill "sl_next".
1952 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001953 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001954slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001955{
1956 slang_T *lp;
1957
Bram Moolenaar51485f02005-06-04 21:55:20 +00001958 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001959 if (lp != NULL)
1960 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001961 if (lang != NULL)
1962 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001963 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001964 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001965 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001966 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001967 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001968 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001969
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970 return lp;
1971}
1972
1973/*
1974 * Free the contents of an slang_T and the structure itself.
1975 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001976 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001977slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001978{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001979 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001980 vim_free(lp->sl_fname);
1981 slang_clear(lp);
1982 vim_free(lp);
1983}
1984
1985/*
1986 * Clear an slang_T so that the file can be reloaded.
1987 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001988 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001989slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001990{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001991 garray_T *gap;
1992 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001993 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001994 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001995 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996
Bram Moolenaard23a8232018-02-10 18:45:26 +01001997 VIM_CLEAR(lp->sl_fbyts);
1998 VIM_CLEAR(lp->sl_kbyts);
1999 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002000
Bram Moolenaard23a8232018-02-10 18:45:26 +01002001 VIM_CLEAR(lp->sl_fidxs);
2002 VIM_CLEAR(lp->sl_kidxs);
2003 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002004
Bram Moolenaar4770d092006-01-12 23:22:24 +00002005 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002006 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002007 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2008 while (gap->ga_len > 0)
2009 {
2010 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2011 vim_free(ftp->ft_from);
2012 vim_free(ftp->ft_to);
2013 }
2014 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002015 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002016
2017 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002018 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002019 {
2020 /* "ga_len" is set to 1 without adding an item for latin1 */
2021 if (gap->ga_data != NULL)
2022 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2023 for (i = 0; i < gap->ga_len; ++i)
2024 vim_free(((int **)gap->ga_data)[i]);
2025 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002026 else
2027 /* SAL items: free salitem_T items */
2028 while (gap->ga_len > 0)
2029 {
2030 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2031 vim_free(smp->sm_lead);
2032 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2033 vim_free(smp->sm_to);
2034#ifdef FEAT_MBYTE
2035 vim_free(smp->sm_lead_w);
2036 vim_free(smp->sm_oneof_w);
2037 vim_free(smp->sm_to_w);
2038#endif
2039 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002040 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002041
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002042 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002043 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002044 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002045 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002046
Bram Moolenaard23a8232018-02-10 18:45:26 +01002047 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002048
Bram Moolenaard23a8232018-02-10 18:45:26 +01002049 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002050
Bram Moolenaar473de612013-06-08 18:19:48 +02002051 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002052 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002053 VIM_CLEAR(lp->sl_comprules);
2054 VIM_CLEAR(lp->sl_compstartflags);
2055 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002056
Bram Moolenaard23a8232018-02-10 18:45:26 +01002057 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002058 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002059
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002060 ga_clear_strings(&lp->sl_comppat);
2061
Bram Moolenaar4770d092006-01-12 23:22:24 +00002062 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2063 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002064
Bram Moolenaar4770d092006-01-12 23:22:24 +00002065#ifdef FEAT_MBYTE
2066 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002067#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002068
Bram Moolenaar4770d092006-01-12 23:22:24 +00002069 /* Clear info from .sug file. */
2070 slang_clear_sug(lp);
2071
Bram Moolenaar5195e452005-08-19 20:32:47 +00002072 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002073 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002074 lp->sl_compsylmax = MAXWLEN;
2075 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002076}
2077
2078/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002079 * Clear the info from the .sug file in "lp".
2080 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002081 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002082slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002083{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002084 VIM_CLEAR(lp->sl_sbyts);
2085 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002086 close_spellbuf(lp->sl_sugbuf);
2087 lp->sl_sugbuf = NULL;
2088 lp->sl_sugloaded = FALSE;
2089 lp->sl_sugtime = 0;
2090}
2091
2092/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002093 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002094 * Invoked through do_in_runtimepath().
2095 */
2096 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002097spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002099 spelload_T *slp = (spelload_T *)cookie;
2100 slang_T *slang;
2101
2102 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2103 if (slang != NULL)
2104 {
2105 /* When a previously loaded file has NOBREAK also use it for the
2106 * ".add" files. */
2107 if (slp->sl_nobreak && slang->sl_add)
2108 slang->sl_nobreak = TRUE;
2109 else if (slang->sl_nobreak)
2110 slp->sl_nobreak = TRUE;
2111
2112 slp->sl_slang = slang;
2113 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002114}
2115
Bram Moolenaar4770d092006-01-12 23:22:24 +00002116
2117/*
2118 * Add a word to the hashtable of common words.
2119 * If it's already there then the counter is increased.
2120 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002121 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002122count_common_word(
2123 slang_T *lp,
2124 char_u *word,
2125 int len, /* word length, -1 for upto NUL */
2126 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002127{
2128 hash_T hash;
2129 hashitem_T *hi;
2130 wordcount_T *wc;
2131 char_u buf[MAXWLEN];
2132 char_u *p;
2133
2134 if (len == -1)
2135 p = word;
2136 else
2137 {
2138 vim_strncpy(buf, word, len);
2139 p = buf;
2140 }
2141
2142 hash = hash_hash(p);
2143 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2144 if (HASHITEM_EMPTY(hi))
2145 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002146 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002147 if (wc == NULL)
2148 return;
2149 STRCPY(wc->wc_word, p);
2150 wc->wc_count = count;
2151 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2152 }
2153 else
2154 {
2155 wc = HI2WC(hi);
2156 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2157 wc->wc_count = MAXWORDCOUNT;
2158 }
2159}
2160
2161/*
2162 * Adjust the score of common words.
2163 */
2164 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002165score_wordcount_adj(
2166 slang_T *slang,
2167 int score,
2168 char_u *word,
2169 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002170{
2171 hashitem_T *hi;
2172 wordcount_T *wc;
2173 int bonus;
2174 int newscore;
2175
2176 hi = hash_find(&slang->sl_wordcount, word);
2177 if (!HASHITEM_EMPTY(hi))
2178 {
2179 wc = HI2WC(hi);
2180 if (wc->wc_count < SCORE_THRES2)
2181 bonus = SCORE_COMMON1;
2182 else if (wc->wc_count < SCORE_THRES3)
2183 bonus = SCORE_COMMON2;
2184 else
2185 bonus = SCORE_COMMON3;
2186 if (split)
2187 newscore = score - bonus / 2;
2188 else
2189 newscore = score - bonus;
2190 if (newscore < 0)
2191 return 0;
2192 return newscore;
2193 }
2194 return score;
2195}
2196
Bram Moolenaar5195e452005-08-19 20:32:47 +00002197
Bram Moolenaar6de68532005-08-24 22:08:48 +00002198/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002199 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002200 * Like strchr() but independent of locale.
2201 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002202 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002203byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002204{
2205 char_u *p;
2206
2207 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002208 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002209 return TRUE;
2210 return FALSE;
2211}
2212
Bram Moolenaar5195e452005-08-19 20:32:47 +00002213#define SY_MAXLEN 30
2214typedef struct syl_item_S
2215{
2216 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2217 int sy_len;
2218} syl_item_T;
2219
2220/*
2221 * Truncate "slang->sl_syllable" at the first slash and put the following items
2222 * in "slang->sl_syl_items".
2223 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002224 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002225init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002226{
2227 char_u *p;
2228 char_u *s;
2229 int l;
2230 syl_item_T *syl;
2231
2232 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2233 p = vim_strchr(slang->sl_syllable, '/');
2234 while (p != NULL)
2235 {
2236 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002237 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002238 break;
2239 s = p;
2240 p = vim_strchr(p, '/');
2241 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002242 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002243 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002244 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002245 if (l >= SY_MAXLEN)
2246 return SP_FORMERROR;
2247 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002248 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002249 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2250 + slang->sl_syl_items.ga_len++;
2251 vim_strncpy(syl->sy_chars, s, l);
2252 syl->sy_len = l;
2253 }
2254 return OK;
2255}
2256
2257/*
2258 * Count the number of syllables in "word".
2259 * When "word" contains spaces the syllables after the last space are counted.
2260 * Returns zero if syllables are not defines.
2261 */
2262 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002263count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002264{
2265 int cnt = 0;
2266 int skip = FALSE;
2267 char_u *p;
2268 int len;
2269 int i;
2270 syl_item_T *syl;
2271 int c;
2272
2273 if (slang->sl_syllable == NULL)
2274 return 0;
2275
2276 for (p = word; *p != NUL; p += len)
2277 {
2278 /* When running into a space reset counter. */
2279 if (*p == ' ')
2280 {
2281 len = 1;
2282 cnt = 0;
2283 continue;
2284 }
2285
2286 /* Find longest match of syllable items. */
2287 len = 0;
2288 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2289 {
2290 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2291 if (syl->sy_len > len
2292 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2293 len = syl->sy_len;
2294 }
2295 if (len != 0) /* found a match, count syllable */
2296 {
2297 ++cnt;
2298 skip = FALSE;
2299 }
2300 else
2301 {
2302 /* No recognized syllable item, at least a syllable char then? */
2303#ifdef FEAT_MBYTE
2304 c = mb_ptr2char(p);
2305 len = (*mb_ptr2len)(p);
2306#else
2307 c = *p;
2308 len = 1;
2309#endif
2310 if (vim_strchr(slang->sl_syllable, c) == NULL)
2311 skip = FALSE; /* No, search for next syllable */
2312 else if (!skip)
2313 {
2314 ++cnt; /* Yes, count it */
2315 skip = TRUE; /* don't count following syllable chars */
2316 }
2317 }
2318 }
2319 return cnt;
2320}
2321
2322/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002323 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002324 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325 */
2326 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002327did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328{
2329 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002330 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002331 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002332 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002333 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002334 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002335 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002336 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002337 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002339 int len;
2340 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002341 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002342 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002343 char_u *use_region = NULL;
2344 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002345 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002346 int i, j;
2347 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002348 static int recursive = FALSE;
2349 char_u *ret_msg = NULL;
2350 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002351#ifdef FEAT_AUTOCMD
2352 bufref_T bufref;
2353
2354 set_bufref(&bufref, wp->w_buffer);
2355#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002356
2357 /* We don't want to do this recursively. May happen when a language is
2358 * not available and the SpellFileMissing autocommand opens a new buffer
2359 * in which 'spell' is set. */
2360 if (recursive)
2361 return NULL;
2362 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002363
2364 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002365 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002367 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002368 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002369 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002370 if (spl_copy == NULL)
2371 goto theend;
2372
Bram Moolenaar2593e032013-11-14 03:54:07 +01002373#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002374 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002375#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002376
2377 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002378 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002379 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002380 /* Get one language name. */
2381 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002382 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002383 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002384
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002385 if (STRCMP(lang, "cjk") == 0)
2386 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01002387#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002388 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002389#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002390 continue;
2391 }
2392
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002393 /* If the name ends in ".spl" use it as the name of the spell file.
2394 * If there is a region name let "region" point to it and remove it
2395 * from the name. */
2396 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2397 {
2398 filename = TRUE;
2399
Bram Moolenaarb6356332005-07-18 21:40:44 +00002400 /* Locate a region and remove it from the file name. */
2401 p = vim_strchr(gettail(lang), '_');
2402 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2403 && !ASCII_ISALPHA(p[3]))
2404 {
2405 vim_strncpy(region_cp, p + 1, 2);
2406 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002407 region = region_cp;
2408 }
2409 else
2410 dont_use_region = TRUE;
2411
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002412 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2414 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002415 break;
2416 }
2417 else
2418 {
2419 filename = FALSE;
2420 if (len > 3 && lang[len - 3] == '_')
2421 {
2422 region = lang + len - 2;
2423 len -= 3;
2424 lang[len] = NUL;
2425 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002426 else
2427 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002428
2429 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002430 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2431 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002432 break;
2433 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002434
Bram Moolenaarb6356332005-07-18 21:40:44 +00002435 if (region != NULL)
2436 {
2437 /* If the region differs from what was used before then don't
2438 * use it for 'spellfile'. */
2439 if (use_region != NULL && STRCMP(region, use_region) != 0)
2440 dont_use_region = TRUE;
2441 use_region = region;
2442 }
2443
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002444 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002445 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002446 {
2447 if (filename)
2448 (void)spell_load_file(lang, lang, NULL, FALSE);
2449 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002450 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002451 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002452#ifdef FEAT_AUTOCMD
2453 /* SpellFileMissing autocommands may do anything, including
2454 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002455 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002456 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02002457 ret_msg = (char_u *)N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002458 goto theend;
2459 }
2460#endif
2461 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002462 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002464 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002465 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002466 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002467 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2468 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2469 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002470 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002471 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002472 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002473 {
2474 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002475 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002476 if (c == REGION_ALL)
2477 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002478 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002479 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002480 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002481 /* This addition file is for other regions. */
2482 region_mask = 0;
2483 }
2484 else
2485 /* This is probably an error. Give a warning and
2486 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002487 smsg((char_u *)
2488 _("Warning: region %s not supported"),
2489 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002490 }
2491 else
2492 region_mask = 1 << c;
2493 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002494
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002495 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002496 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002497 if (ga_grow(&ga, 1) == FAIL)
2498 {
2499 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002500 ret_msg = e_outofmem;
2501 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002502 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002503 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002504 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2505 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002506 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002507 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002508 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002509 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 }
2512
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002513 /* round 0: load int_wordlist, if possible.
2514 * round 1: load first name in 'spellfile'.
2515 * round 2: load second name in 'spellfile.
2516 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002517 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002518 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002520 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002521 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002522 /* Internal wordlist, if there is one. */
2523 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002524 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002525 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002526 }
2527 else
2528 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002529 /* One entry in 'spellfile'. */
2530 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2531 STRCAT(spf_name, ".spl");
2532
2533 /* If it was already found above then skip it. */
2534 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002535 {
2536 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2537 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002538 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002539 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002540 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002541 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002542 }
2543
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002544 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002545 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2546 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002548 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002549 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002550 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002551 * region name, the region is ignored otherwise. for int_wordlist
2552 * use an arbitrary name. */
2553 if (round == 0)
2554 STRCPY(lang, "internal wordlist");
2555 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002556 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002557 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002558 p = vim_strchr(lang, '.');
2559 if (p != NULL)
2560 *p = NUL; /* truncate at ".encoding.add" */
2561 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002562 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002563
2564 /* If one of the languages has NOBREAK we assume the addition
2565 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002566 if (slang != NULL && nobreak)
2567 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002568 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002569 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002570 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002571 region_mask = REGION_ALL;
2572 if (use_region != NULL && !dont_use_region)
2573 {
2574 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002575 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002576 if (c != REGION_ALL)
2577 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002578 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002579 /* This spell file is for other regions. */
2580 region_mask = 0;
2581 }
2582
2583 if (region_mask != 0)
2584 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002585 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2586 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2587 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002588 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2589 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002590 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002591 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002592 }
2593 }
2594
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002595 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002596 ga_clear(&wp->w_s->b_langp);
2597 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002599 /* For each language figure out what language to use for sound folding and
2600 * REP items. If the language doesn't support it itself use another one
2601 * with the same name. E.g. for "en-math" use "en". */
2602 for (i = 0; i < ga.ga_len; ++i)
2603 {
2604 lp = LANGP_ENTRY(ga, i);
2605
2606 /* sound folding */
2607 if (lp->lp_slang->sl_sal.ga_len > 0)
2608 /* language does sound folding itself */
2609 lp->lp_sallang = lp->lp_slang;
2610 else
2611 /* find first similar language that does sound folding */
2612 for (j = 0; j < ga.ga_len; ++j)
2613 {
2614 lp2 = LANGP_ENTRY(ga, j);
2615 if (lp2->lp_slang->sl_sal.ga_len > 0
2616 && STRNCMP(lp->lp_slang->sl_name,
2617 lp2->lp_slang->sl_name, 2) == 0)
2618 {
2619 lp->lp_sallang = lp2->lp_slang;
2620 break;
2621 }
2622 }
2623
2624 /* REP items */
2625 if (lp->lp_slang->sl_rep.ga_len > 0)
2626 /* language has REP items itself */
2627 lp->lp_replang = lp->lp_slang;
2628 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002629 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002630 for (j = 0; j < ga.ga_len; ++j)
2631 {
2632 lp2 = LANGP_ENTRY(ga, j);
2633 if (lp2->lp_slang->sl_rep.ga_len > 0
2634 && STRNCMP(lp->lp_slang->sl_name,
2635 lp2->lp_slang->sl_name, 2) == 0)
2636 {
2637 lp->lp_replang = lp2->lp_slang;
2638 break;
2639 }
2640 }
2641 }
2642
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002643theend:
2644 vim_free(spl_copy);
2645 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002646 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002647 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002648}
2649
2650/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002651 * Clear the midword characters for buffer "buf".
2652 */
2653 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002654clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002655{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002656 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002657#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01002658 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002659#endif
2660}
2661
2662/*
2663 * Use the "sl_midword" field of language "lp" for buffer "buf".
2664 * They add up to any currently used midword characters.
2665 */
2666 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002667use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002668{
2669 char_u *p;
2670
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002671 if (lp->sl_midword == NULL) /* there aren't any */
2672 return;
2673
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002674 for (p = lp->sl_midword; *p != NUL; )
2675#ifdef FEAT_MBYTE
2676 if (has_mbyte)
2677 {
2678 int c, l, n;
2679 char_u *bp;
2680
2681 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002682 l = (*mb_ptr2len)(p);
2683 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002684 wp->w_s->b_spell_ismw[c] = TRUE;
2685 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002686 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002687 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002688 else
2689 {
2690 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002691 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2692 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002693 if (bp != NULL)
2694 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002695 vim_free(wp->w_s->b_spell_ismw_mb);
2696 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002697 vim_strncpy(bp + n, p, l);
2698 }
2699 }
2700 p += l;
2701 }
2702 else
2703#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002704 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002705}
2706
2707/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708 * Find the region "region[2]" in "rp" (points to "sl_regions").
2709 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002710 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002711 */
2712 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002713find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002714{
2715 int i;
2716
2717 for (i = 0; ; i += 2)
2718 {
2719 if (rp[i] == NUL)
2720 return REGION_ALL;
2721 if (rp[i] == region[0] && rp[i + 1] == region[1])
2722 break;
2723 }
2724 return i / 2;
2725}
2726
2727/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002729 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002730 * Word WF_ONECAP
2731 * W WORD WF_ALLCAP
2732 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002733 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002734 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002735captype(
2736 char_u *word,
2737 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002738{
2739 char_u *p;
2740 int c;
2741 int firstcap;
2742 int allcap;
2743 int past_second = FALSE; /* past second word char */
2744
2745 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002746 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002747 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002748 return 0; /* only non-word characters, illegal word */
2749#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002750 if (has_mbyte)
2751 c = mb_ptr2char_adv(&p);
2752 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002753#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002754 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002755 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756
2757 /*
2758 * Need to check all letters to find a word with mixed upper/lower.
2759 * But a word with an upper char only at start is a ONECAP.
2760 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002761 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002762 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002763 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002764 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002765 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002766 {
2767 /* UUl -> KEEPCAP */
2768 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002769 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002770 allcap = FALSE;
2771 }
2772 else if (!allcap)
2773 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002774 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002775 past_second = TRUE;
2776 }
2777
2778 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002779 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002780 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002781 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002782 return 0;
2783}
2784
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002785/*
2786 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2787 * capital. So that make_case_word() can turn WOrd into Word.
2788 * Add ALLCAP for "WOrD".
2789 */
2790 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002791badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002792{
2793 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002794 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002795 int l, u;
2796 int first;
2797 char_u *p;
2798
2799 if (flags & WF_KEEPCAP)
2800 {
2801 /* Count the number of UPPER and lower case letters. */
2802 l = u = 0;
2803 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002804 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002805 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002806 c = PTR2CHAR(p);
2807 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002808 {
2809 ++u;
2810 if (p == word)
2811 first = TRUE;
2812 }
2813 else
2814 ++l;
2815 }
2816
2817 /* If there are more UPPER than lower case letters suggest an
2818 * ALLCAP word. Otherwise, if the first letter is UPPER then
2819 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2820 * require three upper case letters. */
2821 if (u > l && u > 2)
2822 flags |= WF_ALLCAP;
2823 else if (first)
2824 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002825
2826 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2827 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002828 }
2829 return flags;
2830}
2831
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002832/*
2833 * Delete the internal wordlist and its .spl file.
2834 */
2835 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002836spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002837{
2838 char_u fname[MAXPATHL];
2839
2840 if (int_wordlist != NULL)
2841 {
2842 mch_remove(int_wordlist);
2843 int_wordlist_spl(fname);
2844 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002845 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002846 }
2847}
2848
2849#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002850/*
2851 * Free all languages.
2852 */
2853 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002854spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002855{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002856 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002857 buf_T *buf;
2858
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002859 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002860 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002861 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002862
2863 while (first_lang != NULL)
2864 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002865 slang = first_lang;
2866 first_lang = slang->sl_next;
2867 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002868 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002869
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002870 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002871
Bram Moolenaard23a8232018-02-10 18:45:26 +01002872 VIM_CLEAR(repl_to);
2873 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002874}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002875#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002876
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002877#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002878/*
2879 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002880 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002881 */
2882 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002883spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002884{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002885 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002886
Bram Moolenaarea408852005-06-25 22:49:46 +00002887 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002888 init_spell_chartab();
2889
2890 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002891 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002892
2893 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002894 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002895 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002896 /* Only load the wordlists when 'spelllang' is set and there is a
2897 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002898 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002899 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002900 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002901 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002902 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002903 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002904 }
2905 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002906 }
2907}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002908#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002909
Bram Moolenaarb765d632005-06-07 21:00:02 +00002910/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002911 * Opposite of offset2bytes().
2912 * "pp" points to the bytes and is advanced over it.
2913 * Returns the offset.
2914 */
2915 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002916bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002917{
2918 char_u *p = *pp;
2919 int nr;
2920 int c;
2921
2922 c = *p++;
2923 if ((c & 0x80) == 0x00) /* 1 byte */
2924 {
2925 nr = c - 1;
2926 }
2927 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2928 {
2929 nr = (c & 0x3f) - 1;
2930 nr = nr * 255 + (*p++ - 1);
2931 }
2932 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2933 {
2934 nr = (c & 0x1f) - 1;
2935 nr = nr * 255 + (*p++ - 1);
2936 nr = nr * 255 + (*p++ - 1);
2937 }
2938 else /* 4 bytes */
2939 {
2940 nr = (c & 0x0f) - 1;
2941 nr = nr * 255 + (*p++ - 1);
2942 nr = nr * 255 + (*p++ - 1);
2943 nr = nr * 255 + (*p++ - 1);
2944 }
2945
2946 *pp = p;
2947 return nr;
2948}
2949
Bram Moolenaar4770d092006-01-12 23:22:24 +00002950
2951/*
2952 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2953 * list and only contains text lines. Can use a swapfile to reduce memory
2954 * use.
2955 * Most other fields are invalid! Esp. watch out for string options being
2956 * NULL and there is no undo info.
2957 * Returns NULL when out of memory.
2958 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002959 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002960open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002961{
2962 buf_T *buf;
2963
2964 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2965 if (buf != NULL)
2966 {
2967 buf->b_spell = TRUE;
2968 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002969#ifdef FEAT_CRYPT
2970 buf->b_p_key = empty_option;
2971#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002972 ml_open(buf);
2973 ml_open_file(buf); /* create swap file now */
2974 }
2975 return buf;
2976}
2977
2978/*
2979 * Close the buffer used for spell info.
2980 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002981 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002982close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002983{
2984 if (buf != NULL)
2985 {
2986 ml_close(buf, TRUE);
2987 vim_free(buf);
2988 }
2989}
2990
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002991/*
2992 * Init the chartab used for spelling for ASCII.
2993 * EBCDIC is not supported!
2994 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002996clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002997{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002998 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002999
3000 /* Init everything to FALSE. */
3001 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
3002 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
3003 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003004 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003005 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003006 sp->st_upper[i] = i;
3007 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003008
3009 /* We include digits. A word shouldn't start with a digit, but handling
3010 * that is done separately. */
3011 for (i = '0'; i <= '9'; ++i)
3012 sp->st_isw[i] = TRUE;
3013 for (i = 'A'; i <= 'Z'; ++i)
3014 {
3015 sp->st_isw[i] = TRUE;
3016 sp->st_isu[i] = TRUE;
3017 sp->st_fold[i] = i + 0x20;
3018 }
3019 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003020 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003021 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003022 sp->st_upper[i] = i - 0x20;
3023 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003024}
3025
3026/*
3027 * Init the chartab used for spelling. Only depends on 'encoding'.
3028 * Called once while starting up and when 'encoding' changes.
3029 * The default is to use isalpha(), but the spell file should define the word
3030 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003031 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003032 */
3033 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003034init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003035{
3036 int i;
3037
3038 did_set_spelltab = FALSE;
3039 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003040#ifdef FEAT_MBYTE
3041 if (enc_dbcs)
3042 {
3043 /* DBCS: assume double-wide characters are word characters. */
3044 for (i = 128; i <= 255; ++i)
3045 if (MB_BYTE2LEN(i) == 2)
3046 spelltab.st_isw[i] = TRUE;
3047 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003048 else if (enc_utf8)
3049 {
3050 for (i = 128; i < 256; ++i)
3051 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003052 int f = utf_fold(i);
3053 int u = utf_toupper(i);
3054
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003055 spelltab.st_isu[i] = utf_isupper(i);
3056 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003057 /* The folded/upper-cased value is different between latin1 and
3058 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
3059 * value for utf-8 to avoid this. */
3060 spelltab.st_fold[i] = (f < 256) ? f : i;
3061 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003062 }
3063 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003064 else
3065#endif
3066 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003067 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003068 for (i = 128; i < 256; ++i)
3069 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003070 if (MB_ISUPPER(i))
3071 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003072 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003073 spelltab.st_isu[i] = TRUE;
3074 spelltab.st_fold[i] = MB_TOLOWER(i);
3075 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003076 else if (MB_ISLOWER(i))
3077 {
3078 spelltab.st_isw[i] = TRUE;
3079 spelltab.st_upper[i] = MB_TOUPPER(i);
3080 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003081 }
3082 }
3083}
3084
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003085
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003086/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003087 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003088 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003089 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003090 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003091 */
3092 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003093spell_iswordp(
3094 char_u *p,
3095 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003096{
Bram Moolenaarea408852005-06-25 22:49:46 +00003097#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003098 char_u *s;
3099 int l;
3100 int c;
3101
3102 if (has_mbyte)
3103 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003104 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003105 s = p;
3106 if (l == 1)
3107 {
3108 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003109 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003110 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003111 }
3112 else
3113 {
3114 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003115 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3116 : (wp->w_s->b_spell_ismw_mb != NULL
3117 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003118 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003119 }
3120
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003121 c = mb_ptr2char(s);
3122 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003123 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003124 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003125 }
Bram Moolenaarea408852005-06-25 22:49:46 +00003126#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003127
Bram Moolenaar860cae12010-06-05 23:22:07 +02003128 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003129}
3130
3131/*
3132 * Return TRUE if "p" points to a word character.
3133 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3134 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003135 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003136spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003137{
3138#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003139 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003140
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003141 if (has_mbyte)
3142 {
3143 c = mb_ptr2char(p);
3144 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003145 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003146 return spelltab.st_isw[c];
3147 }
3148#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003149 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003150}
3151
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003152#ifdef FEAT_MBYTE
3153/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003154 * Return TRUE if word class indicates a word character.
3155 * Only for characters above 255.
3156 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003157 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003158 */
3159 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003160spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003161{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003162 if (wp->w_s->b_cjk)
3163 /* East Asian characters are not considered word characters. */
3164 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003165 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3166}
3167
3168/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003169 * Return TRUE if "p" points to a word character.
3170 * Wide version of spell_iswordp().
3171 */
3172 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003173spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003174{
3175 int *s;
3176
Bram Moolenaar860cae12010-06-05 23:22:07 +02003177 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3178 : (wp->w_s->b_spell_ismw_mb != NULL
3179 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003180 s = p + 1;
3181 else
3182 s = p;
3183
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003184 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003185 {
3186 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003187 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003188 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003189 return spell_mb_isword_class(
3190 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003191 return 0;
3192 }
3193 return spelltab.st_isw[*s];
3194}
3195#endif
3196
Bram Moolenaarea408852005-06-25 22:49:46 +00003197/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003198 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3199 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003200 * When using a multi-byte 'encoding' the length may change!
3201 * Returns FAIL when something wrong.
3202 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003203 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003204spell_casefold(
3205 char_u *str,
3206 int len,
3207 char_u *buf,
3208 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003209{
3210 int i;
3211
3212 if (len >= buflen)
3213 {
3214 buf[0] = NUL;
3215 return FAIL; /* result will not fit */
3216 }
3217
3218#ifdef FEAT_MBYTE
3219 if (has_mbyte)
3220 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003221 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003222 char_u *p;
3223 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003224
3225 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003226 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003227 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003228 if (outi + MB_MAXBYTES > buflen)
3229 {
3230 buf[outi] = NUL;
3231 return FAIL;
3232 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003233 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003234 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003235 }
3236 buf[outi] = NUL;
3237 }
3238 else
3239#endif
3240 {
3241 /* Be quick for non-multibyte encodings. */
3242 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003243 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003244 buf[i] = NUL;
3245 }
3246
3247 return OK;
3248}
3249
Bram Moolenaar4770d092006-01-12 23:22:24 +00003250/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003251#define SPS_BEST 1
3252#define SPS_FAST 2
3253#define SPS_DOUBLE 4
3254
Bram Moolenaar4770d092006-01-12 23:22:24 +00003255static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3256static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003257
3258/*
3259 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003260 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003261 */
3262 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003263spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003264{
3265 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003266 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003267 char_u buf[MAXPATHL];
3268 int f;
3269
3270 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003271 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003272
3273 for (p = p_sps; *p != NUL; )
3274 {
3275 copy_option_part(&p, buf, MAXPATHL, ",");
3276
3277 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003278 if (VIM_ISDIGIT(*buf))
3279 {
3280 s = buf;
3281 sps_limit = getdigits(&s);
3282 if (*s != NUL && !VIM_ISDIGIT(*s))
3283 f = -1;
3284 }
3285 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003286 f = SPS_BEST;
3287 else if (STRCMP(buf, "fast") == 0)
3288 f = SPS_FAST;
3289 else if (STRCMP(buf, "double") == 0)
3290 f = SPS_DOUBLE;
3291 else if (STRNCMP(buf, "expr:", 5) != 0
3292 && STRNCMP(buf, "file:", 5) != 0)
3293 f = -1;
3294
3295 if (f == -1 || (sps_flags != 0 && f != 0))
3296 {
3297 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003298 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003299 return FAIL;
3300 }
3301 if (f != 0)
3302 sps_flags = f;
3303 }
3304
3305 if (sps_flags == 0)
3306 sps_flags = SPS_BEST;
3307
3308 return OK;
3309}
3310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003312 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003314 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003315 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003316 */
3317 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003318spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003319{
3320 char_u *line;
3321 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322 char_u wcopy[MAXWLEN + 2];
3323 char_u *p;
3324 int i;
3325 int c;
3326 suginfo_T sug;
3327 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003328 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003329 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003330 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003331 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003332 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003333 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003335 if (no_spell_checking(curwin))
3336 return;
3337
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003338 if (VIsual_active)
3339 {
3340 /* Use the Visually selected text as the bad word. But reject
3341 * a multi-line selection. */
3342 if (curwin->w_cursor.lnum != VIsual.lnum)
3343 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003344 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003345 return;
3346 }
3347 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3348 if (badlen < 0)
3349 badlen = -badlen;
3350 else
3351 curwin->w_cursor.col = VIsual.col;
3352 ++badlen;
3353 end_visual_mode();
3354 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003355 /* Find the start of the badly spelled word. */
3356 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003357 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003359 /* No bad word or it starts after the cursor: use the word under the
3360 * cursor. */
3361 curwin->w_cursor = prev_cursor;
3362 line = ml_get_curline();
3363 p = line + curwin->w_cursor.col;
3364 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003365 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003366 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003367 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003368 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003369 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003370
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003371 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003372 {
3373 beep_flush();
3374 return;
3375 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003376 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 }
3378
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003379 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003380
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003381 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003382 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003383
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003384 /* Make a copy of current line since autocommands may free the line. */
3385 line = vim_strsave(ml_get_curline());
3386 if (line == NULL)
3387 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003388
Bram Moolenaar5195e452005-08-19 20:32:47 +00003389 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3390 * 'spellsuggest', whatever is smaller. */
3391 if (sps_limit > (int)Rows - 2)
3392 limit = (int)Rows - 2;
3393 else
3394 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003395 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003396 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397
3398 if (sug.su_ga.ga_len == 0)
3399 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003400 else if (count > 0)
3401 {
3402 if (count > sug.su_ga.ga_len)
3403 smsg((char_u *)_("Sorry, only %ld suggestions"),
3404 (long)sug.su_ga.ga_len);
3405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 else
3407 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003408 VIM_CLEAR(repl_from);
3409 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003410
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003411#ifdef FEAT_RIGHTLEFT
3412 /* When 'rightleft' is set the list is drawn right-left. */
3413 cmdmsg_rl = curwin->w_p_rl;
3414 if (cmdmsg_rl)
3415 msg_col = Columns - 1;
3416#endif
3417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003418 /* List the suggestions. */
3419 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003420 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003421 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003422 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3423 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003424#ifdef FEAT_RIGHTLEFT
3425 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3426 {
3427 /* And now the rabbit from the high hat: Avoid showing the
3428 * untranslated message rightleft. */
3429 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3430 sug.su_badlen, sug.su_badptr);
3431 }
3432#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 msg_puts(IObuff);
3434 msg_clr_eos();
3435 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003437 msg_scroll = TRUE;
3438 for (i = 0; i < sug.su_ga.ga_len; ++i)
3439 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003440 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003441
3442 /* The suggested word may replace only part of the bad word, add
3443 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003444 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003445 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003446 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003447 sug.su_badptr + stp->st_orglen,
3448 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003449 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3450#ifdef FEAT_RIGHTLEFT
3451 if (cmdmsg_rl)
3452 rl_mirror(IObuff);
3453#endif
3454 msg_puts(IObuff);
3455
3456 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003457 msg_puts(IObuff);
3458
3459 /* The word may replace more than "su_badlen". */
3460 if (sug.su_badlen < stp->st_orglen)
3461 {
3462 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3463 stp->st_orglen, sug.su_badptr);
3464 msg_puts(IObuff);
3465 }
3466
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003467 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003468 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003469 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003470 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003471 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003472 stp->st_salscore ? "s " : "",
3473 stp->st_score, stp->st_altscore);
3474 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003475 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003476 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003477#ifdef FEAT_RIGHTLEFT
3478 if (cmdmsg_rl)
3479 /* Mirror the numbers, but keep the leading space. */
3480 rl_mirror(IObuff + 1);
3481#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003482 msg_advance(30);
3483 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003485 msg_putchar('\n');
3486 }
3487
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003488#ifdef FEAT_RIGHTLEFT
3489 cmdmsg_rl = FALSE;
3490 msg_col = 0;
3491#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003493 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003494 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003495 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003496 lines_left = Rows; /* avoid more prompt */
3497 /* don't delay for 'smd' in normal_cmd() */
3498 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 }
3500
Bram Moolenaard12a1322005-08-21 22:08:24 +00003501 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3502 {
3503 /* Save the from and to text for :spellrepall. */
3504 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003505 if (sug.su_badlen > stp->st_orglen)
3506 {
3507 /* Replacing less than "su_badlen", append the remainder to
3508 * repl_to. */
3509 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3510 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3511 sug.su_badlen - stp->st_orglen,
3512 sug.su_badptr + stp->st_orglen);
3513 repl_to = vim_strsave(IObuff);
3514 }
3515 else
3516 {
3517 /* Replacing su_badlen or more, use the whole word. */
3518 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3519 repl_to = vim_strsave(stp->st_word);
3520 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003521
3522 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003523 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3524 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003525 if (p != NULL)
3526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003528 mch_memmove(p, line, c);
3529 STRCPY(p + c, stp->st_word);
3530 STRCAT(p, sug.su_badptr + stp->st_orglen);
3531 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3532 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003533
3534 /* For redo we use a change-word command. */
3535 ResetRedobuff();
3536 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003537 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003538 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003539 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003540
3541 /* After this "p" may be invalid. */
3542 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003543 }
3544 }
3545 else
3546 curwin->w_cursor = prev_cursor;
3547
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003548 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003549skip:
3550 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003551}
3552
3553/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003554 * Check if the word at line "lnum" column "col" is required to start with a
3555 * capital. This uses 'spellcapcheck' of the current buffer.
3556 */
3557 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003558check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003559{
3560 int need_cap = FALSE;
3561 char_u *line;
3562 char_u *line_copy = NULL;
3563 char_u *p;
3564 colnr_T endcol;
3565 regmatch_T regmatch;
3566
Bram Moolenaar860cae12010-06-05 23:22:07 +02003567 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003568 return FALSE;
3569
3570 line = ml_get_curline();
3571 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003572 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003573 {
3574 /* At start of line, check if previous line is empty or sentence
3575 * ends there. */
3576 if (lnum == 1)
3577 need_cap = TRUE;
3578 else
3579 {
3580 line = ml_get(lnum - 1);
3581 if (*skipwhite(line) == NUL)
3582 need_cap = TRUE;
3583 else
3584 {
3585 /* Append a space in place of the line break. */
3586 line_copy = concat_str(line, (char_u *)" ");
3587 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003588 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003589 }
3590 }
3591 }
3592 else
3593 endcol = col;
3594
3595 if (endcol > 0)
3596 {
3597 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003598 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003599 regmatch.rm_ic = FALSE;
3600 p = line + endcol;
3601 for (;;)
3602 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003603 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003604 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003605 break;
3606 if (vim_regexec(&regmatch, p, 0)
3607 && regmatch.endp[0] == line + endcol)
3608 {
3609 need_cap = TRUE;
3610 break;
3611 }
3612 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003613 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003614 }
3615
3616 vim_free(line_copy);
3617
3618 return need_cap;
3619}
3620
3621
3622/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003623 * ":spellrepall"
3624 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003626ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003627{
3628 pos_T pos = curwin->w_cursor;
3629 char_u *frompat;
3630 int addlen;
3631 char_u *line;
3632 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003633 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003634 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003635
3636 if (repl_from == NULL || repl_to == NULL)
3637 {
3638 EMSG(_("E752: No previous spell replacement"));
3639 return;
3640 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003641 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003642
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003643 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003644 if (frompat == NULL)
3645 return;
3646 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3647 p_ws = FALSE;
3648
Bram Moolenaar5195e452005-08-19 20:32:47 +00003649 sub_nsubs = 0;
3650 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003651 curwin->w_cursor.lnum = 0;
3652 while (!got_int)
3653 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003654 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003655 || u_save_cursor() == FAIL)
3656 break;
3657
3658 /* Only replace when the right word isn't there yet. This happens
3659 * when changing "etc" to "etc.". */
3660 line = ml_get_curline();
3661 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3662 repl_to, STRLEN(repl_to)) != 0)
3663 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003664 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003665 if (p == NULL)
3666 break;
3667 mch_memmove(p, line, curwin->w_cursor.col);
3668 STRCPY(p + curwin->w_cursor.col, repl_to);
3669 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3670 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3671 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003672
3673 if (curwin->w_cursor.lnum != prev_lnum)
3674 {
3675 ++sub_nlines;
3676 prev_lnum = curwin->w_cursor.lnum;
3677 }
3678 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003679 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003680 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003681 }
3682
3683 p_ws = save_ws;
3684 curwin->w_cursor = pos;
3685 vim_free(frompat);
3686
Bram Moolenaar5195e452005-08-19 20:32:47 +00003687 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003688 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003689 else
3690 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003691}
3692
3693/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003694 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3695 * a list of allocated strings.
3696 */
3697 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003698spell_suggest_list(
3699 garray_T *gap,
3700 char_u *word,
3701 int maxcount, /* maximum nr of suggestions */
3702 int need_cap, /* 'spellcapcheck' matched */
3703 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003704{
3705 suginfo_T sug;
3706 int i;
3707 suggest_T *stp;
3708 char_u *wcopy;
3709
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003710 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003711
3712 /* Make room in "gap". */
3713 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003714 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003715 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003716 for (i = 0; i < sug.su_ga.ga_len; ++i)
3717 {
3718 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003719
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003720 /* The suggested word may replace only part of "word", add the not
3721 * replaced part. */
3722 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003723 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003724 if (wcopy == NULL)
3725 break;
3726 STRCPY(wcopy, stp->st_word);
3727 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3728 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3729 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003730 }
3731
3732 spell_find_cleanup(&sug);
3733}
3734
3735/*
3736 * Find spell suggestions for the word at the start of "badptr".
3737 * Return the suggestions in "su->su_ga".
3738 * The maximum number of suggestions is "maxcount".
3739 * Note: does use info for the current window.
3740 * This is based on the mechanisms of Aspell, but completely reimplemented.
3741 */
3742 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003743spell_find_suggest(
3744 char_u *badptr,
3745 int badlen, /* length of bad word or 0 if unknown */
3746 suginfo_T *su,
3747 int maxcount,
3748 int banbadword, /* don't include badword in suggestions */
3749 int need_cap, /* word should start with capital */
3750 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003751{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003752 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003753 char_u buf[MAXPATHL];
3754 char_u *p;
3755 int do_combine = FALSE;
3756 char_u *sps_copy;
3757#ifdef FEAT_EVAL
3758 static int expr_busy = FALSE;
3759#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003760 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003761 int i;
3762 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003763
3764 /*
3765 * Set the info in "*su".
3766 */
3767 vim_memset(su, 0, sizeof(suginfo_T));
3768 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3769 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003770 if (*badptr == NUL)
3771 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003772 hash_init(&su->su_banned);
3773
3774 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003775 if (badlen != 0)
3776 su->su_badlen = badlen;
3777 else
3778 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003779 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003780 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003781
3782 if (su->su_badlen >= MAXWLEN)
3783 su->su_badlen = MAXWLEN - 1; /* just in case */
3784 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3785 (void)spell_casefold(su->su_badptr, su->su_badlen,
3786 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003787 /* TODO: make this work if the case-folded text is longer than the original
3788 * text. Currently an illegal byte causes wrong pointer computations. */
3789 su->su_fbadword[su->su_badlen] = NUL;
3790
Bram Moolenaar0c405862005-06-22 22:26:26 +00003791 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003792 su->su_badflags = badword_captype(su->su_badptr,
3793 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003794 if (need_cap)
3795 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003796
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003797 /* Find the default language for sound folding. We simply use the first
3798 * one in 'spelllang' that supports sound folding. That's good for when
3799 * using multiple files for one language, it's not that bad when mixing
3800 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003801 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003802 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003803 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003804 if (lp->lp_sallang != NULL)
3805 {
3806 su->su_sallang = lp->lp_sallang;
3807 break;
3808 }
3809 }
3810
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003811 /* Soundfold the bad word with the default sound folding, so that we don't
3812 * have to do this many times. */
3813 if (su->su_sallang != NULL)
3814 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3815 su->su_sal_badword);
3816
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003817 /* If the word is not capitalised and spell_check() doesn't consider the
3818 * word to be bad then it might need to be capitalised. Add a suggestion
3819 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003820 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003821 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003822 {
3823 make_case_word(su->su_badword, buf, WF_ONECAP);
3824 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003825 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003826 }
3827
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003828 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003829 if (banbadword)
3830 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003831
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003832 /* Make a copy of 'spellsuggest', because the expression may change it. */
3833 sps_copy = vim_strsave(p_sps);
3834 if (sps_copy == NULL)
3835 return;
3836
3837 /* Loop over the items in 'spellsuggest'. */
3838 for (p = sps_copy; *p != NUL; )
3839 {
3840 copy_option_part(&p, buf, MAXPATHL, ",");
3841
3842 if (STRNCMP(buf, "expr:", 5) == 0)
3843 {
3844#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003845 /* Evaluate an expression. Skip this when called recursively,
3846 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003847 if (!expr_busy)
3848 {
3849 expr_busy = TRUE;
3850 spell_suggest_expr(su, buf + 5);
3851 expr_busy = FALSE;
3852 }
3853#endif
3854 }
3855 else if (STRNCMP(buf, "file:", 5) == 0)
3856 /* Use list of suggestions in a file. */
3857 spell_suggest_file(su, buf + 5);
3858 else
3859 {
3860 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003861 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003862 if (sps_flags & SPS_DOUBLE)
3863 do_combine = TRUE;
3864 }
3865 }
3866
3867 vim_free(sps_copy);
3868
3869 if (do_combine)
3870 /* Combine the two list of suggestions. This must be done last,
3871 * because sorting changes the order again. */
3872 score_combine(su);
3873}
3874
3875#ifdef FEAT_EVAL
3876/*
3877 * Find suggestions by evaluating expression "expr".
3878 */
3879 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003880spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003881{
3882 list_T *list;
3883 listitem_T *li;
3884 int score;
3885 char_u *p;
3886
3887 /* The work is split up in a few parts to avoid having to export
3888 * suginfo_T.
3889 * First evaluate the expression and get the resulting list. */
3890 list = eval_spell_expr(su->su_badword, expr);
3891 if (list != NULL)
3892 {
3893 /* Loop over the items in the list. */
3894 for (li = list->lv_first; li != NULL; li = li->li_next)
3895 if (li->li_tv.v_type == VAR_LIST)
3896 {
3897 /* Get the word and the score from the items. */
3898 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003899 if (score >= 0 && score <= su->su_maxscore)
3900 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3901 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003902 }
3903 list_unref(list);
3904 }
3905
Bram Moolenaar4770d092006-01-12 23:22:24 +00003906 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3907 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003908 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3909}
3910#endif
3911
3912/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003913 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003914 */
3915 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003916spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003917{
3918 FILE *fd;
3919 char_u line[MAXWLEN * 2];
3920 char_u *p;
3921 int len;
3922 char_u cword[MAXWLEN];
3923
3924 /* Open the file. */
3925 fd = mch_fopen((char *)fname, "r");
3926 if (fd == NULL)
3927 {
3928 EMSG2(_(e_notopen), fname);
3929 return;
3930 }
3931
3932 /* Read it line by line. */
3933 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3934 {
3935 line_breakcheck();
3936
3937 p = vim_strchr(line, '/');
3938 if (p == NULL)
3939 continue; /* No Tab found, just skip the line. */
3940 *p++ = NUL;
3941 if (STRICMP(su->su_badword, line) == 0)
3942 {
3943 /* Match! Isolate the good word, until CR or NL. */
3944 for (len = 0; p[len] >= ' '; ++len)
3945 ;
3946 p[len] = NUL;
3947
3948 /* If the suggestion doesn't have specific case duplicate the case
3949 * of the bad word. */
3950 if (captype(p, NULL) == 0)
3951 {
3952 make_case_word(p, cword, su->su_badflags);
3953 p = cword;
3954 }
3955
3956 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003957 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003958 }
3959 }
3960
3961 fclose(fd);
3962
Bram Moolenaar4770d092006-01-12 23:22:24 +00003963 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3964 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003965 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3966}
3967
3968/*
3969 * Find suggestions for the internal method indicated by "sps_flags".
3970 */
3971 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003972spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003973{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003974 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003975 * Load the .sug file(s) that are available and not done yet.
3976 */
3977 suggest_load_files();
3978
3979 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003980 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003981 *
3982 * Set a maximum score to limit the combination of operations that is
3983 * tried.
3984 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003985 suggest_try_special(su);
3986
3987 /*
3988 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3989 * from the .aff file and inserting a space (split the word).
3990 */
3991 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003992
3993 /* For the resulting top-scorers compute the sound-a-like score. */
3994 if (sps_flags & SPS_DOUBLE)
3995 score_comp_sal(su);
3996
3997 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003998 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003999 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004000 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004001 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004002 if (sps_flags & SPS_BEST)
4003 /* Adjust the word score for the suggestions found so far for how
4004 * they sounds like. */
4005 rescore_suggestions(su);
4006
4007 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004008 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00004009 * for the soundfold word, limits the changes that are being tried,
4010 * and "su_sfmaxscore" the rescored score, which is set by
4011 * cleanup_suggestions().
4012 * First find words with a small edit distance, because this is much
4013 * faster and often already finds the top-N suggestions. If we didn't
4014 * find many suggestions try again with a higher edit distance.
4015 * "sl_sounddone" is used to avoid doing the same word twice.
4016 */
4017 suggest_try_soundalike_prep();
4018 su->su_maxscore = SCORE_SFMAX1;
4019 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004020 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004021 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4022 {
4023 /* We didn't find enough matches, try again, allowing more
4024 * changes to the soundfold word. */
4025 su->su_maxscore = SCORE_SFMAX2;
4026 suggest_try_soundalike(su);
4027 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4028 {
4029 /* Still didn't find enough matches, try again, allowing even
4030 * more changes to the soundfold word. */
4031 su->su_maxscore = SCORE_SFMAX3;
4032 suggest_try_soundalike(su);
4033 }
4034 }
4035 su->su_maxscore = su->su_sfmaxscore;
4036 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004037 }
4038
Bram Moolenaar4770d092006-01-12 23:22:24 +00004039 /* When CTRL-C was hit while searching do show the results. Only clear
4040 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004041 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00004042 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004043 {
4044 (void)vgetc();
4045 got_int = FALSE;
4046 }
4047
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004048 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004049 {
4050 if (sps_flags & SPS_BEST)
4051 /* Adjust the word score for how it sounds like. */
4052 rescore_suggestions(su);
4053
Bram Moolenaar4770d092006-01-12 23:22:24 +00004054 /* Remove bogus suggestions, sort and truncate at "maxcount". */
4055 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004056 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004057 }
4058}
4059
4060/*
4061 * Free the info put in "*su" by spell_find_suggest().
4062 */
4063 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004064spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004065{
4066 int i;
4067
4068 /* Free the suggestions. */
4069 for (i = 0; i < su->su_ga.ga_len; ++i)
4070 vim_free(SUG(su->su_ga, i).st_word);
4071 ga_clear(&su->su_ga);
4072 for (i = 0; i < su->su_sga.ga_len; ++i)
4073 vim_free(SUG(su->su_sga, i).st_word);
4074 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004075
4076 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004077 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078}
4079
4080/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004081 * Make a copy of "word", with the first letter upper or lower cased, to
4082 * "wcopy[MAXWLEN]". "word" must not be empty.
4083 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004084 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004085 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004086onecap_copy(
4087 char_u *word,
4088 char_u *wcopy,
4089 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090{
4091 char_u *p;
4092 int c;
4093 int l;
4094
4095 p = word;
4096#ifdef FEAT_MBYTE
4097 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004098 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 else
4100#endif
4101 c = *p++;
4102 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004103 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004105 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106#ifdef FEAT_MBYTE
4107 if (has_mbyte)
4108 l = mb_char2bytes(c, wcopy);
4109 else
4110#endif
4111 {
4112 l = 1;
4113 wcopy[0] = c;
4114 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004115 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004116}
4117
4118/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004119 * Make a copy of "word" with all the letters upper cased into
4120 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 */
4122 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004123allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004124{
4125 char_u *s;
4126 char_u *d;
4127 int c;
4128
4129 d = wcopy;
4130 for (s = word; *s != NUL; )
4131 {
4132#ifdef FEAT_MBYTE
4133 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004134 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004135 else
4136#endif
4137 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004138
4139#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +02004140 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004141 * would cause weird errors in other 8-bit encodings. */
4142 if (enc_latin1like && c == 0xdf)
4143 {
4144 c = 'S';
4145 if (d - wcopy >= MAXWLEN - 1)
4146 break;
4147 *d++ = c;
4148 }
4149 else
4150#endif
4151 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152
4153#ifdef FEAT_MBYTE
4154 if (has_mbyte)
4155 {
4156 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4157 break;
4158 d += mb_char2bytes(c, d);
4159 }
4160 else
4161#endif
4162 {
4163 if (d - wcopy >= MAXWLEN - 1)
4164 break;
4165 *d++ = c;
4166 }
4167 }
4168 *d = NUL;
4169}
4170
4171/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004172 * Try finding suggestions by recognizing specific situations.
4173 */
4174 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004175suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004176{
4177 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004178 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004179 int c;
4180 char_u word[MAXWLEN];
4181
4182 /*
4183 * Recognize a word that is repeated: "the the".
4184 */
4185 p = skiptowhite(su->su_fbadword);
4186 len = p - su->su_fbadword;
4187 p = skipwhite(p);
4188 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4189 {
4190 /* Include badflags: if the badword is onecap or allcap
4191 * use that for the goodword too: "The the" -> "The". */
4192 c = su->su_fbadword[len];
4193 su->su_fbadword[len] = NUL;
4194 make_case_word(su->su_fbadword, word, su->su_badflags);
4195 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004196
4197 /* Give a soundalike score of 0, compute the score as if deleting one
4198 * character. */
4199 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004200 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004201 }
4202}
4203
4204/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004205 * Change the 0 to 1 to measure how much time is spent in each state.
4206 * Output is dumped in "suggestprof".
4207 */
4208#if 0
4209# define SUGGEST_PROFILE
4210proftime_T current;
4211proftime_T total;
4212proftime_T times[STATE_FINAL + 1];
4213long counts[STATE_FINAL + 1];
4214
4215 static void
4216prof_init(void)
4217{
4218 for (int i = 0; i <= STATE_FINAL; ++i)
4219 {
4220 profile_zero(&times[i]);
4221 counts[i] = 0;
4222 }
4223 profile_start(&current);
4224 profile_start(&total);
4225}
4226
4227/* call before changing state */
4228 static void
4229prof_store(state_T state)
4230{
4231 profile_end(&current);
4232 profile_add(&times[state], &current);
4233 ++counts[state];
4234 profile_start(&current);
4235}
4236# define PROF_STORE(state) prof_store(state);
4237
4238 static void
4239prof_report(char *name)
4240{
4241 FILE *fd = fopen("suggestprof", "a");
4242
4243 profile_end(&total);
4244 fprintf(fd, "-----------------------\n");
4245 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4246 for (int i = 0; i <= STATE_FINAL; ++i)
4247 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4248 fclose(fd);
4249}
4250#else
4251# define PROF_STORE(state)
4252#endif
4253
4254/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 * Try finding suggestions by adding/removing/swapping letters.
4256 */
4257 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004258suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259{
4260 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004261 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004263 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004264 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265
4266 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004267 * to find matches (esp. REP items). Append some more text, changing
4268 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004270 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004271 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004272 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273
Bram Moolenaar860cae12010-06-05 23:22:07 +02004274 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004276 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004277
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004278 /* If reloading a spell file fails it's still in the list but
4279 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004280 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004281 continue;
4282
Bram Moolenaar4770d092006-01-12 23:22:24 +00004283 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004284#ifdef SUGGEST_PROFILE
4285 prof_init();
4286#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004287 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004288#ifdef SUGGEST_PROFILE
4289 prof_report("try_change");
4290#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004291 }
4292}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293
Bram Moolenaar4770d092006-01-12 23:22:24 +00004294/* Check the maximum score, if we go over it we won't try this change. */
4295#define TRY_DEEPER(su, stack, depth, add) \
4296 (stack[depth].ts_score + (add) < su->su_maxscore)
4297
4298/*
4299 * Try finding suggestions by adding/removing/swapping letters.
4300 *
4301 * This uses a state machine. At each node in the tree we try various
4302 * operations. When trying if an operation works "depth" is increased and the
4303 * stack[] is used to store info. This allows combinations, thus insert one
4304 * character, replace one and delete another. The number of changes is
4305 * limited by su->su_maxscore.
4306 *
4307 * After implementing this I noticed an article by Kemal Oflazer that
4308 * describes something similar: "Error-tolerant Finite State Recognition with
4309 * Applications to Morphological Analysis and Spelling Correction" (1996).
4310 * The implementation in the article is simplified and requires a stack of
4311 * unknown depth. The implementation here only needs a stack depth equal to
4312 * the length of the word.
4313 *
4314 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4315 * The mechanism is the same, but we find a match with a sound-folded word
4316 * that comes from one or more original words. Each of these words may be
4317 * added, this is done by add_sound_suggest().
4318 * Don't use:
4319 * the prefix tree or the keep-case tree
4320 * "su->su_badlen"
4321 * anything to do with upper and lower case
4322 * anything to do with word or non-word characters ("spell_iswordp()")
4323 * banned words
4324 * word flags (rare, region, compounding)
4325 * word splitting for now
4326 * "similar_chars()"
4327 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4328 */
4329 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004330suggest_trie_walk(
4331 suginfo_T *su,
4332 langp_T *lp,
4333 char_u *fword,
4334 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004335{
4336 char_u tword[MAXWLEN]; /* good word collected so far */
4337 trystate_T stack[MAXWLEN];
4338 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004339 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004340 * words and split word. NUL terminated
4341 * when going deeper but not when coming
4342 * back. */
4343 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4344 trystate_T *sp;
4345 int newscore;
4346 int score;
4347 char_u *byts, *fbyts, *pbyts;
4348 idx_T *idxs, *fidxs, *pidxs;
4349 int depth;
4350 int c, c2, c3;
4351 int n = 0;
4352 int flags;
4353 garray_T *gap;
4354 idx_T arridx;
4355 int len;
4356 char_u *p;
4357 fromto_T *ftp;
4358 int fl = 0, tl;
4359 int repextra = 0; /* extra bytes in fword[] from REP item */
4360 slang_T *slang = lp->lp_slang;
4361 int fword_ends;
4362 int goodword_ends;
4363#ifdef DEBUG_TRIEWALK
4364 /* Stores the name of the change made at each level. */
4365 char_u changename[MAXWLEN][80];
4366#endif
4367 int breakcheckcount = 1000;
4368 int compound_ok;
4369
4370 /*
4371 * Go through the whole case-fold tree, try changes at each node.
4372 * "tword[]" contains the word collected from nodes in the tree.
4373 * "fword[]" the word we are trying to match with (initially the bad
4374 * word).
4375 */
4376 depth = 0;
4377 sp = &stack[0];
4378 vim_memset(sp, 0, sizeof(trystate_T));
4379 sp->ts_curi = 1;
4380
4381 if (soundfold)
4382 {
4383 /* Going through the soundfold tree. */
4384 byts = fbyts = slang->sl_sbyts;
4385 idxs = fidxs = slang->sl_sidxs;
4386 pbyts = NULL;
4387 pidxs = NULL;
4388 sp->ts_prefixdepth = PFD_NOPREFIX;
4389 sp->ts_state = STATE_START;
4390 }
4391 else
4392 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004393 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004394 * When there are postponed prefixes we need to use these first. At
4395 * the end of the prefix we continue in the case-fold tree.
4396 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004397 fbyts = slang->sl_fbyts;
4398 fidxs = slang->sl_fidxs;
4399 pbyts = slang->sl_pbyts;
4400 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004401 if (pbyts != NULL)
4402 {
4403 byts = pbyts;
4404 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004405 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004406 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4407 }
4408 else
4409 {
4410 byts = fbyts;
4411 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004412 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004413 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004414 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004415 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004416
Bram Moolenaar4770d092006-01-12 23:22:24 +00004417 /*
4418 * Loop to find all suggestions. At each round we either:
4419 * - For the current state try one operation, advance "ts_curi",
4420 * increase "depth".
4421 * - When a state is done go to the next, set "ts_state".
4422 * - When all states are tried decrease "depth".
4423 */
4424 while (depth >= 0 && !got_int)
4425 {
4426 sp = &stack[depth];
4427 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004429 case STATE_START:
4430 case STATE_NOPREFIX:
4431 /*
4432 * Start of node: Deal with NUL bytes, which means
4433 * tword[] may end here.
4434 */
4435 arridx = sp->ts_arridx; /* current node in the tree */
4436 len = byts[arridx]; /* bytes in this node */
4437 arridx += sp->ts_curi; /* index of current byte */
4438
4439 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004440 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004441 /* Skip over the NUL bytes, we use them later. */
4442 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4443 ;
4444 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445
Bram Moolenaar4770d092006-01-12 23:22:24 +00004446 /* Always past NUL bytes now. */
4447 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004448 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004449 sp->ts_state = STATE_ENDNUL;
4450 sp->ts_save_badflags = su->su_badflags;
4451
4452 /* At end of a prefix or at start of prefixtree: check for
4453 * following word. */
4454 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004455 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004456 /* Set su->su_badflags to the caps type at this position.
4457 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004458#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004459 if (has_mbyte)
4460 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4461 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004462#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004463 n = sp->ts_fidx;
4464 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4465 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004466 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004467#ifdef DEBUG_TRIEWALK
4468 sprintf(changename[depth], "prefix");
4469#endif
4470 go_deeper(stack, depth, 0);
4471 ++depth;
4472 sp = &stack[depth];
4473 sp->ts_prefixdepth = depth - 1;
4474 byts = fbyts;
4475 idxs = fidxs;
4476 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004477
Bram Moolenaar4770d092006-01-12 23:22:24 +00004478 /* Move the prefix to preword[] with the right case
4479 * and make find_keepcap_word() works. */
4480 tword[sp->ts_twordlen] = NUL;
4481 make_case_word(tword + sp->ts_splitoff,
4482 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004483 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004484 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004485 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004486 break;
4487 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004488
Bram Moolenaar4770d092006-01-12 23:22:24 +00004489 if (sp->ts_curi > len || byts[arridx] != 0)
4490 {
4491 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004492 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004493 sp->ts_state = STATE_ENDNUL;
4494 sp->ts_save_badflags = su->su_badflags;
4495 break;
4496 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004497
Bram Moolenaar4770d092006-01-12 23:22:24 +00004498 /*
4499 * End of word in tree.
4500 */
4501 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502
Bram Moolenaar4770d092006-01-12 23:22:24 +00004503 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004504
4505 /* Skip words with the NOSUGGEST flag. */
4506 if (flags & WF_NOSUGGEST)
4507 break;
4508
Bram Moolenaar4770d092006-01-12 23:22:24 +00004509 fword_ends = (fword[sp->ts_fidx] == NUL
4510 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004511 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004512 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004513 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514
Bram Moolenaar4770d092006-01-12 23:22:24 +00004515 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004516 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004517 {
4518 /* There was a prefix before the word. Check that the prefix
4519 * can be used with this word. */
4520 /* Count the length of the NULs in the prefix. If there are
4521 * none this must be the first try without a prefix. */
4522 n = stack[sp->ts_prefixdepth].ts_arridx;
4523 len = pbyts[n++];
4524 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4525 ;
4526 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004527 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004528 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004529 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004530 if (c == 0)
4531 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004532
Bram Moolenaar4770d092006-01-12 23:22:24 +00004533 /* Use the WF_RARE flag for a rare prefix. */
4534 if (c & WF_RAREPFX)
4535 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004536
Bram Moolenaar4770d092006-01-12 23:22:24 +00004537 /* Tricky: when checking for both prefix and compounding
4538 * we run into the prefix flag first.
4539 * Remember that it's OK, so that we accept the prefix
4540 * when arriving at a compound flag. */
4541 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004542 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004543 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004544
Bram Moolenaar4770d092006-01-12 23:22:24 +00004545 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4546 * appending another compound word below. */
4547 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004548 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004549 goodword_ends = FALSE;
4550 else
4551 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004552
Bram Moolenaar4770d092006-01-12 23:22:24 +00004553 p = NULL;
4554 compound_ok = TRUE;
4555 if (sp->ts_complen > sp->ts_compsplit)
4556 {
4557 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004558 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004559 /* There was a word before this word. When there was no
4560 * change in this word (it was correct) add the first word
4561 * as a suggestion. If this word was corrected too, we
4562 * need to check if a correct word follows. */
4563 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004564 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004565 && STRNCMP(fword + sp->ts_splitfidx,
4566 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004567 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004568 {
4569 preword[sp->ts_prewordlen] = NUL;
4570 newscore = score_wordcount_adj(slang, sp->ts_score,
4571 preword + sp->ts_prewordlen,
4572 sp->ts_prewordlen > 0);
4573 /* Add the suggestion if the score isn't too bad. */
4574 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004575 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004576 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004577 newscore, 0, FALSE,
4578 lp->lp_sallang, FALSE);
4579 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004580 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004581 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004582 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004583 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004584 /* There was a compound word before this word. If this
4585 * word does not support compounding then give up
4586 * (splitting is tried for the word without compound
4587 * flag). */
4588 if (((unsigned)flags >> 24) == 0
4589 || sp->ts_twordlen - sp->ts_splitoff
4590 < slang->sl_compminlen)
4591 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004592#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004593 /* For multi-byte chars check character length against
4594 * COMPOUNDMIN. */
4595 if (has_mbyte
4596 && slang->sl_compminlen > 0
4597 && mb_charlen(tword + sp->ts_splitoff)
4598 < slang->sl_compminlen)
4599 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004600#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00004601
Bram Moolenaar4770d092006-01-12 23:22:24 +00004602 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4603 compflags[sp->ts_complen + 1] = NUL;
4604 vim_strncpy(preword + sp->ts_prewordlen,
4605 tword + sp->ts_splitoff,
4606 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004607
4608 /* Verify CHECKCOMPOUNDPATTERN rules. */
4609 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4610 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004611 compound_ok = FALSE;
4612
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004613 if (compound_ok)
4614 {
4615 p = preword;
4616 while (*skiptowhite(p) != NUL)
4617 p = skipwhite(skiptowhite(p));
4618 if (fword_ends && !can_compound(slang, p,
4619 compflags + sp->ts_compsplit))
4620 /* Compound is not allowed. But it may still be
4621 * possible if we add another (short) word. */
4622 compound_ok = FALSE;
4623 }
4624
Bram Moolenaar4770d092006-01-12 23:22:24 +00004625 /* Get pointer to last char of previous word. */
4626 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004627 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004628 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004629 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630
Bram Moolenaar4770d092006-01-12 23:22:24 +00004631 /*
4632 * Form the word with proper case in preword.
4633 * If there is a word from a previous split, append.
4634 * For the soundfold tree don't change the case, simply append.
4635 */
4636 if (soundfold)
4637 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4638 else if (flags & WF_KEEPCAP)
4639 /* Must find the word in the keep-case tree. */
4640 find_keepcap_word(slang, tword + sp->ts_splitoff,
4641 preword + sp->ts_prewordlen);
4642 else
4643 {
4644 /* Include badflags: If the badword is onecap or allcap
4645 * use that for the goodword too. But if the badword is
4646 * allcap and it's only one char long use onecap. */
4647 c = su->su_badflags;
4648 if ((c & WF_ALLCAP)
4649#ifdef FEAT_MBYTE
4650 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
4651#else
4652 && su->su_badlen == 1
4653#endif
4654 )
4655 c = WF_ONECAP;
4656 c |= flags;
4657
4658 /* When appending a compound word after a word character don't
4659 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004660 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004661 c &= ~WF_ONECAP;
4662 make_case_word(tword + sp->ts_splitoff,
4663 preword + sp->ts_prewordlen, c);
4664 }
4665
4666 if (!soundfold)
4667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 /* Don't use a banned word. It may appear again as a good
4669 * word, thus remember it. */
4670 if (flags & WF_BANNED)
4671 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004672 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 break;
4674 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004675 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004676 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4677 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004678 {
4679 if (slang->sl_compprog == NULL)
4680 break;
4681 /* the word so far was banned but we may try compounding */
4682 goodword_ends = FALSE;
4683 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685
Bram Moolenaar4770d092006-01-12 23:22:24 +00004686 newscore = 0;
4687 if (!soundfold) /* soundfold words don't have flags */
4688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004690 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 newscore += SCORE_REGION;
4692 if (flags & WF_RARE)
4693 newscore += SCORE_RARE;
4694
Bram Moolenaar0c405862005-06-22 22:26:26 +00004695 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004696 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004697 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699
Bram Moolenaar4770d092006-01-12 23:22:24 +00004700 /* TODO: how about splitting in the soundfold tree? */
4701 if (fword_ends
4702 && goodword_ends
4703 && sp->ts_fidx >= sp->ts_fidxtry
4704 && compound_ok)
4705 {
4706 /* The badword also ends: add suggestions. */
4707#ifdef DEBUG_TRIEWALK
4708 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004710 int j;
4711
4712 /* print the stack of changes that brought us here */
4713 smsg("------ %s -------", fword);
4714 for (j = 0; j < depth; ++j)
4715 smsg("%s", changename[j]);
4716 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004717#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004718 if (soundfold)
4719 {
4720 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004721 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004722 add_sound_suggest(su, preword, sp->ts_score, lp);
4723 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004724 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004725 {
4726 /* Give a penalty when changing non-word char to word
4727 * char, e.g., "thes," -> "these". */
4728 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004729 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004730 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004731 {
4732 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004733 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004734 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004735 newscore += SCORE_NONWORD;
4736 }
4737
Bram Moolenaar4770d092006-01-12 23:22:24 +00004738 /* Give a bonus to words seen before. */
4739 score = score_wordcount_adj(slang,
4740 sp->ts_score + newscore,
4741 preword + sp->ts_prewordlen,
4742 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004743
Bram Moolenaar4770d092006-01-12 23:22:24 +00004744 /* Add the suggestion if the score isn't too bad. */
4745 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004746 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004747 add_suggestion(su, &su->su_ga, preword,
4748 sp->ts_fidx - repextra,
4749 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004750
4751 if (su->su_badflags & WF_MIXCAP)
4752 {
4753 /* We really don't know if the word should be
4754 * upper or lower case, add both. */
4755 c = captype(preword, NULL);
4756 if (c == 0 || c == WF_ALLCAP)
4757 {
4758 make_case_word(tword + sp->ts_splitoff,
4759 preword + sp->ts_prewordlen,
4760 c == 0 ? WF_ALLCAP : 0);
4761
4762 add_suggestion(su, &su->su_ga, preword,
4763 sp->ts_fidx - repextra,
4764 score + SCORE_ICASE, 0, FALSE,
4765 lp->lp_sallang, FALSE);
4766 }
4767 }
4768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004769 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004770 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004771
Bram Moolenaar4770d092006-01-12 23:22:24 +00004772 /*
4773 * Try word split and/or compounding.
4774 */
4775 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00004776#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004777 /* Don't split halfway a character. */
4778 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004779#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004780 )
4781 {
4782 int try_compound;
4783 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004784
Bram Moolenaar4770d092006-01-12 23:22:24 +00004785 /* If past the end of the bad word don't try a split.
4786 * Otherwise try changing the next word. E.g., find
4787 * suggestions for "the the" where the second "the" is
4788 * different. It's done like a split.
4789 * TODO: word split for soundfold words */
4790 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4791 && !soundfold;
4792
4793 /* Get here in several situations:
4794 * 1. The word in the tree ends:
4795 * If the word allows compounding try that. Otherwise try
4796 * a split by inserting a space. For both check that a
4797 * valid words starts at fword[sp->ts_fidx].
4798 * For NOBREAK do like compounding to be able to check if
4799 * the next word is valid.
4800 * 2. The badword does end, but it was due to a change (e.g.,
4801 * a swap). No need to split, but do check that the
4802 * following word is valid.
4803 * 3. The badword and the word in the tree end. It may still
4804 * be possible to compound another (short) word.
4805 */
4806 try_compound = FALSE;
4807 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004808 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004809 && slang->sl_compprog != NULL
4810 && ((unsigned)flags >> 24) != 0
4811 && sp->ts_twordlen - sp->ts_splitoff
4812 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004813#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004814 && (!has_mbyte
4815 || slang->sl_compminlen == 0
4816 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004817 >= slang->sl_compminlen)
4818#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004819 && (slang->sl_compsylmax < MAXWLEN
4820 || sp->ts_complen + 1 - sp->ts_compsplit
4821 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004822 && (can_be_compound(sp, slang,
4823 compflags, ((unsigned)flags >> 24))))
4824
Bram Moolenaar4770d092006-01-12 23:22:24 +00004825 {
4826 try_compound = TRUE;
4827 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4828 compflags[sp->ts_complen + 1] = NUL;
4829 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004830
Bram Moolenaar4770d092006-01-12 23:22:24 +00004831 /* For NOBREAK we never try splitting, it won't make any word
4832 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004833 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004834 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004835
Bram Moolenaar4770d092006-01-12 23:22:24 +00004836 /* If we could add a compound word, and it's also possible to
4837 * split at this point, do the split first and set
4838 * TSF_DIDSPLIT to avoid doing it again. */
4839 else if (!fword_ends
4840 && try_compound
4841 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4842 {
4843 try_compound = FALSE;
4844 sp->ts_flags |= TSF_DIDSPLIT;
4845 --sp->ts_curi; /* do the same NUL again */
4846 compflags[sp->ts_complen] = NUL;
4847 }
4848 else
4849 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004850
Bram Moolenaar4770d092006-01-12 23:22:24 +00004851 if (try_split || try_compound)
4852 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004853 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004854 {
4855 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004856 * words so far are valid for compounding. If there
4857 * is only one word it must not have the NEEDCOMPOUND
4858 * flag. */
4859 if (sp->ts_complen == sp->ts_compsplit
4860 && (flags & WF_NEEDCOMP))
4861 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004862 p = preword;
4863 while (*skiptowhite(p) != NUL)
4864 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004865 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004866 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004867 compflags + sp->ts_compsplit))
4868 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004869
4870 if (slang->sl_nosplitsugs)
4871 newscore += SCORE_SPLIT_NO;
4872 else
4873 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004874
4875 /* Give a bonus to words seen before. */
4876 newscore = score_wordcount_adj(slang, newscore,
4877 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004878 }
4879
Bram Moolenaar4770d092006-01-12 23:22:24 +00004880 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004881 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004882 go_deeper(stack, depth, newscore);
4883#ifdef DEBUG_TRIEWALK
4884 if (!try_compound && !fword_ends)
4885 sprintf(changename[depth], "%.*s-%s: split",
4886 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4887 else
4888 sprintf(changename[depth], "%.*s-%s: compound",
4889 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4890#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004891 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004892 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004893 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004894 sp->ts_state = STATE_SPLITUNDO;
4895
4896 ++depth;
4897 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004898
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004899 /* Append a space to preword when splitting. */
4900 if (!try_compound && !fword_ends)
4901 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004902 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004903 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004904 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004905
4906 /* If the badword has a non-word character at this
4907 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004908 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004909 * character when the word ends. But only when the
4910 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004911 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004912 + sp->ts_fidx,
4913 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004914 || fword_ends)
4915 && fword[sp->ts_fidx] != NUL
4916 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004917 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004918 int l;
4919
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004920 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004921 if (fword_ends)
4922 {
4923 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004924 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004925 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004926 sp->ts_prewordlen += l;
4927 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004928 }
4929 else
4930 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4931 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004932 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004933
Bram Moolenaard12a1322005-08-21 22:08:24 +00004934 /* When compounding include compound flag in
4935 * compflags[] (already set above). When splitting we
4936 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004937 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004938 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004939 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004940 sp->ts_compsplit = sp->ts_complen;
4941 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004942
Bram Moolenaar53805d12005-08-01 07:08:33 +00004943 /* set su->su_badflags to the caps type at this
4944 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004945#ifdef FEAT_MBYTE
4946 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004947 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004948 else
4949#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004950 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004951 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004952 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004954 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004955 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004956
4957 /* If there are postponed prefixes, try these too. */
4958 if (pbyts != NULL)
4959 {
4960 byts = pbyts;
4961 idxs = pidxs;
4962 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004963 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004964 sp->ts_state = STATE_NOPREFIX;
4965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004966 }
4967 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004968 }
4969 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004970
Bram Moolenaar4770d092006-01-12 23:22:24 +00004971 case STATE_SPLITUNDO:
4972 /* Undo the changes done for word split or compound word. */
4973 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004974
Bram Moolenaar4770d092006-01-12 23:22:24 +00004975 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004976 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004977 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004978
Bram Moolenaar4770d092006-01-12 23:22:24 +00004979 /* In case we went into the prefix tree. */
4980 byts = fbyts;
4981 idxs = fidxs;
4982 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004983
Bram Moolenaar4770d092006-01-12 23:22:24 +00004984 case STATE_ENDNUL:
4985 /* Past the NUL bytes in the node. */
4986 su->su_badflags = sp->ts_save_badflags;
4987 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004988#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004989 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004990#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004991 )
4992 {
4993 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004994 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004995 sp->ts_state = STATE_DEL;
4996 break;
4997 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004998 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004999 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005000 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001
5002 case STATE_PLAIN:
5003 /*
5004 * Go over all possible bytes at this node, add each to tword[]
5005 * and use child node. "ts_curi" is the index.
5006 */
5007 arridx = sp->ts_arridx;
5008 if (sp->ts_curi > byts[arridx])
5009 {
5010 /* Done all bytes at this node, do next state. When still at
5011 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005012 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005013 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005014 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005015 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005016 sp->ts_state = STATE_FINAL;
5017 }
5018 else
5019 {
5020 arridx += sp->ts_curi++;
5021 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005022
Bram Moolenaar4770d092006-01-12 23:22:24 +00005023 /* Normal byte, go one level deeper. If it's not equal to the
5024 * byte in the bad word adjust the score. But don't even try
5025 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01005026 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00005027 * delete + substitute. */
5028 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +00005029#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005030 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005031#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005032 )
5033 newscore = 0;
5034 else
5035 newscore = SCORE_SUBST;
5036 if ((newscore == 0
5037 || (sp->ts_fidx >= sp->ts_fidxtry
5038 && ((sp->ts_flags & TSF_DIDDEL) == 0
5039 || c != fword[sp->ts_delidx])))
5040 && TRY_DEEPER(su, stack, depth, newscore))
5041 {
5042 go_deeper(stack, depth, newscore);
5043#ifdef DEBUG_TRIEWALK
5044 if (newscore > 0)
5045 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
5046 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5047 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005048 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005049 sprintf(changename[depth], "%.*s-%s: accept %c",
5050 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5051 fword[sp->ts_fidx]);
5052#endif
5053 ++depth;
5054 sp = &stack[depth];
5055 ++sp->ts_fidx;
5056 tword[sp->ts_twordlen++] = c;
5057 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +00005058#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005059 if (newscore == SCORE_SUBST)
5060 sp->ts_isdiff = DIFF_YES;
5061 if (has_mbyte)
5062 {
5063 /* Multi-byte characters are a bit complicated to
5064 * handle: They differ when any of the bytes differ
5065 * and then their length may also differ. */
5066 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005067 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005068 /* First byte. */
5069 sp->ts_tcharidx = 0;
5070 sp->ts_tcharlen = MB_BYTE2LEN(c);
5071 sp->ts_fcharstart = sp->ts_fidx - 1;
5072 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005073 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005074 }
5075 else if (sp->ts_isdiff == DIFF_INSERT)
5076 /* When inserting trail bytes don't advance in the
5077 * bad word. */
5078 --sp->ts_fidx;
5079 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5080 {
5081 /* Last byte of character. */
5082 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00005083 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005084 /* Correct ts_fidx for the byte length of the
5085 * character (we didn't check that before). */
5086 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005087 + MB_PTR2LEN(
5088 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005089 /* For changing a composing character adjust
5090 * the score from SCORE_SUBST to
5091 * SCORE_SUBCOMP. */
5092 if (enc_utf8
5093 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005094 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00005095 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005096 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005097 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005098 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005099 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005100 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005101 SCORE_SUBST - SCORE_SUBCOMP;
5102
Bram Moolenaar4770d092006-01-12 23:22:24 +00005103 /* For a similar character adjust score from
5104 * SCORE_SUBST to SCORE_SIMILAR. */
5105 else if (!soundfold
5106 && slang->sl_has_map
5107 && similar_chars(slang,
5108 mb_ptr2char(tword
5109 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00005110 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00005111 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00005112 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005113 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00005114 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00005115 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005116 else if (sp->ts_isdiff == DIFF_INSERT
5117 && sp->ts_twordlen > sp->ts_tcharlen)
5118 {
5119 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
5120 c = mb_ptr2char(p);
5121 if (enc_utf8 && utf_iscomposing(c))
5122 {
5123 /* Inserting a composing char doesn't
5124 * count that much. */
5125 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5126 }
5127 else
5128 {
5129 /* If the previous character was the same,
5130 * thus doubling a character, give a bonus
5131 * to the score. Also for the soundfold
5132 * tree (might seem illogical but does
5133 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005134 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005135 if (c == mb_ptr2char(p))
5136 sp->ts_score -= SCORE_INS
5137 - SCORE_INSDUP;
5138 }
5139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005140
Bram Moolenaar4770d092006-01-12 23:22:24 +00005141 /* Starting a new char, reset the length. */
5142 sp->ts_tcharlen = 0;
5143 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005144 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005145 else
5146#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005147 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005148 /* If we found a similar char adjust the score.
5149 * We do this after calling go_deeper() because
5150 * it's slow. */
5151 if (newscore != 0
5152 && !soundfold
5153 && slang->sl_has_map
5154 && similar_chars(slang,
5155 c, fword[sp->ts_fidx - 1]))
5156 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005158 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005159 }
5160 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005161
Bram Moolenaar4770d092006-01-12 23:22:24 +00005162 case STATE_DEL:
5163#ifdef FEAT_MBYTE
5164 /* When past the first byte of a multi-byte char don't try
5165 * delete/insert/swap a character. */
5166 if (has_mbyte && sp->ts_tcharlen > 0)
5167 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005168 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005169 sp->ts_state = STATE_FINAL;
5170 break;
5171 }
5172#endif
5173 /*
5174 * Try skipping one character in the bad word (delete it).
5175 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005176 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005177 sp->ts_state = STATE_INS_PREP;
5178 sp->ts_curi = 1;
5179 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5180 /* Deleting a vowel at the start of a word counts less, see
5181 * soundalike_score(). */
5182 newscore = 2 * SCORE_DEL / 3;
5183 else
5184 newscore = SCORE_DEL;
5185 if (fword[sp->ts_fidx] != NUL
5186 && TRY_DEEPER(su, stack, depth, newscore))
5187 {
5188 go_deeper(stack, depth, newscore);
5189#ifdef DEBUG_TRIEWALK
5190 sprintf(changename[depth], "%.*s-%s: delete %c",
5191 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5192 fword[sp->ts_fidx]);
5193#endif
5194 ++depth;
5195
5196 /* Remember what character we deleted, so that we can avoid
5197 * inserting it again. */
5198 stack[depth].ts_flags |= TSF_DIDDEL;
5199 stack[depth].ts_delidx = sp->ts_fidx;
5200
5201 /* Advance over the character in fword[]. Give a bonus to the
5202 * score if the same character is following "nn" -> "n". It's
5203 * a bit illogical for soundfold tree but it does give better
5204 * results. */
5205#ifdef FEAT_MBYTE
5206 if (has_mbyte)
5207 {
5208 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005209 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005210 if (enc_utf8 && utf_iscomposing(c))
5211 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5212 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5213 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5214 }
5215 else
5216#endif
5217 {
5218 ++stack[depth].ts_fidx;
5219 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5220 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5221 }
5222 break;
5223 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005224 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005225
5226 case STATE_INS_PREP:
5227 if (sp->ts_flags & TSF_DIDDEL)
5228 {
5229 /* If we just deleted a byte then inserting won't make sense,
5230 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005231 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005232 sp->ts_state = STATE_SWAP;
5233 break;
5234 }
5235
5236 /* skip over NUL bytes */
5237 n = sp->ts_arridx;
5238 for (;;)
5239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005240 if (sp->ts_curi > byts[n])
5241 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005242 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005243 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005244 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005245 break;
5246 }
5247 if (byts[n + sp->ts_curi] != NUL)
5248 {
5249 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005250 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005251 sp->ts_state = STATE_INS;
5252 break;
5253 }
5254 ++sp->ts_curi;
5255 }
5256 break;
5257
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005258 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005259
5260 case STATE_INS:
5261 /* Insert one byte. Repeat this for each possible byte at this
5262 * node. */
5263 n = sp->ts_arridx;
5264 if (sp->ts_curi > byts[n])
5265 {
5266 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005267 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005268 sp->ts_state = STATE_SWAP;
5269 break;
5270 }
5271
5272 /* Do one more byte at this node, but:
5273 * - Skip NUL bytes.
5274 * - Skip the byte if it's equal to the byte in the word,
5275 * accepting that byte is always better.
5276 */
5277 n += sp->ts_curi++;
5278 c = byts[n];
5279 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5280 /* Inserting a vowel at the start of a word counts less,
5281 * see soundalike_score(). */
5282 newscore = 2 * SCORE_INS / 3;
5283 else
5284 newscore = SCORE_INS;
5285 if (c != fword[sp->ts_fidx]
5286 && TRY_DEEPER(su, stack, depth, newscore))
5287 {
5288 go_deeper(stack, depth, newscore);
5289#ifdef DEBUG_TRIEWALK
5290 sprintf(changename[depth], "%.*s-%s: insert %c",
5291 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5292 c);
5293#endif
5294 ++depth;
5295 sp = &stack[depth];
5296 tword[sp->ts_twordlen++] = c;
5297 sp->ts_arridx = idxs[n];
5298#ifdef FEAT_MBYTE
5299 if (has_mbyte)
5300 {
5301 fl = MB_BYTE2LEN(c);
5302 if (fl > 1)
5303 {
5304 /* There are following bytes for the same character.
5305 * We must find all bytes before trying
5306 * delete/insert/swap/etc. */
5307 sp->ts_tcharlen = fl;
5308 sp->ts_tcharidx = 1;
5309 sp->ts_isdiff = DIFF_INSERT;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 }
5312 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005313 fl = 1;
5314 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00005315#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005316 {
5317 /* If the previous character was the same, thus doubling a
5318 * character, give a bonus to the score. Also for
5319 * soundfold words (illogical but does give a better
5320 * score). */
5321 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005322 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005323 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005325 }
5326 break;
5327
5328 case STATE_SWAP:
5329 /*
5330 * Swap two bytes in the bad word: "12" -> "21".
5331 * We change "fword" here, it's changed back afterwards at
5332 * STATE_UNSWAP.
5333 */
5334 p = fword + sp->ts_fidx;
5335 c = *p;
5336 if (c == NUL)
5337 {
5338 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005339 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005340 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005341 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343
Bram Moolenaar4770d092006-01-12 23:22:24 +00005344 /* Don't swap if the first character is not a word character.
5345 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005346 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005347 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005348 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005349 sp->ts_state = STATE_REP_INI;
5350 break;
5351 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005352
Bram Moolenaar4770d092006-01-12 23:22:24 +00005353#ifdef FEAT_MBYTE
5354 if (has_mbyte)
5355 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005356 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005357 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005358 if (p[n] == NUL)
5359 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005360 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005361 c2 = c; /* don't swap non-word char */
5362 else
5363 c2 = mb_ptr2char(p + n);
5364 }
5365 else
5366#endif
5367 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005368 if (p[1] == NUL)
5369 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005370 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005371 c2 = c; /* don't swap non-word char */
5372 else
5373 c2 = p[1];
5374 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005375
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005376 /* When the second character is NUL we can't swap. */
5377 if (c2 == NUL)
5378 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005379 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005380 sp->ts_state = STATE_REP_INI;
5381 break;
5382 }
5383
Bram Moolenaar4770d092006-01-12 23:22:24 +00005384 /* When characters are identical, swap won't do anything.
5385 * Also get here if the second char is not a word character. */
5386 if (c == c2)
5387 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005388 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005389 sp->ts_state = STATE_SWAP3;
5390 break;
5391 }
5392 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5393 {
5394 go_deeper(stack, depth, SCORE_SWAP);
5395#ifdef DEBUG_TRIEWALK
5396 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5397 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5398 c, c2);
5399#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005400 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005401 sp->ts_state = STATE_UNSWAP;
5402 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005403#ifdef FEAT_MBYTE
5404 if (has_mbyte)
5405 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005406 fl = mb_char2len(c2);
5407 mch_memmove(p, p + n, fl);
5408 mb_char2bytes(c, p + fl);
5409 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005410 }
5411 else
5412#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005413 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005414 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005415 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005416 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005417 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005418 }
5419 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005420 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005421 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005422 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005423 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005424 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005425 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005426
Bram Moolenaar4770d092006-01-12 23:22:24 +00005427 case STATE_UNSWAP:
5428 /* Undo the STATE_SWAP swap: "21" -> "12". */
5429 p = fword + sp->ts_fidx;
5430#ifdef FEAT_MBYTE
5431 if (has_mbyte)
5432 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005433 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005434 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005435 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005436 mb_char2bytes(c, p);
5437 }
5438 else
5439#endif
5440 {
5441 c = *p;
5442 *p = p[1];
5443 p[1] = c;
5444 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005445 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005446
5447 case STATE_SWAP3:
5448 /* Swap two bytes, skipping one: "123" -> "321". We change
5449 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5450 p = fword + sp->ts_fidx;
5451#ifdef FEAT_MBYTE
5452 if (has_mbyte)
5453 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005454 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005455 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005456 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005457 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005458 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005459 c3 = c; /* don't swap non-word char */
5460 else
5461 c3 = mb_ptr2char(p + n + fl);
5462 }
5463 else
5464#endif
5465 {
5466 c = *p;
5467 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005468 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005469 c3 = c; /* don't swap non-word char */
5470 else
5471 c3 = p[2];
5472 }
5473
5474 /* When characters are identical: "121" then SWAP3 result is
5475 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5476 * same as SWAP on next char: "112". Thus skip all swapping.
5477 * Also skip when c3 is NUL.
5478 * Also get here when the third character is not a word character.
5479 * Second character may any char: "a.b" -> "b.a" */
5480 if (c == c3 || c3 == NUL)
5481 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005482 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005483 sp->ts_state = STATE_REP_INI;
5484 break;
5485 }
5486 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5487 {
5488 go_deeper(stack, depth, SCORE_SWAP3);
5489#ifdef DEBUG_TRIEWALK
5490 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5491 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5492 c, c3);
5493#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005494 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005495 sp->ts_state = STATE_UNSWAP3;
5496 ++depth;
5497#ifdef FEAT_MBYTE
5498 if (has_mbyte)
5499 {
5500 tl = mb_char2len(c3);
5501 mch_memmove(p, p + n + fl, tl);
5502 mb_char2bytes(c2, p + tl);
5503 mb_char2bytes(c, p + fl + tl);
5504 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5505 }
5506 else
5507#endif
5508 {
5509 p[0] = p[2];
5510 p[2] = c;
5511 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5512 }
5513 }
5514 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005515 {
5516 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005517 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005518 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005519 break;
5520
5521 case STATE_UNSWAP3:
5522 /* Undo STATE_SWAP3: "321" -> "123" */
5523 p = fword + sp->ts_fidx;
5524#ifdef FEAT_MBYTE
5525 if (has_mbyte)
5526 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005527 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005528 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005529 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005530 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005531 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005532 mch_memmove(p + fl + tl, p, n);
5533 mb_char2bytes(c, p);
5534 mb_char2bytes(c2, p + tl);
5535 p = p + tl;
5536 }
5537 else
5538#endif
5539 {
5540 c = *p;
5541 *p = p[2];
5542 p[2] = c;
5543 ++p;
5544 }
5545
Bram Moolenaar860cae12010-06-05 23:22:07 +02005546 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005547 {
5548 /* Middle char is not a word char, skip the rotate. First and
5549 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005550 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005551 sp->ts_state = STATE_REP_INI;
5552 break;
5553 }
5554
5555 /* Rotate three characters left: "123" -> "231". We change
5556 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5557 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5558 {
5559 go_deeper(stack, depth, SCORE_SWAP3);
5560#ifdef DEBUG_TRIEWALK
5561 p = fword + sp->ts_fidx;
5562 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5563 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5564 p[0], p[1], p[2]);
5565#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005566 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005567 sp->ts_state = STATE_UNROT3L;
5568 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005569 p = fword + sp->ts_fidx;
5570#ifdef FEAT_MBYTE
5571 if (has_mbyte)
5572 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005573 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005574 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005575 fl = MB_CPTR2LEN(p + n);
5576 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005577 mch_memmove(p, p + n, fl);
5578 mb_char2bytes(c, p + fl);
5579 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005580 }
5581 else
5582#endif
5583 {
5584 c = *p;
5585 *p = p[1];
5586 p[1] = p[2];
5587 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005588 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005589 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005590 }
5591 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005592 {
5593 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005594 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005595 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005596 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005597
Bram Moolenaar4770d092006-01-12 23:22:24 +00005598 case STATE_UNROT3L:
5599 /* Undo ROT3L: "231" -> "123" */
5600 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005601#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005602 if (has_mbyte)
5603 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005604 n = MB_PTR2LEN(p);
5605 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005606 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005607 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005608 mch_memmove(p + tl, p, n);
5609 mb_char2bytes(c, p);
5610 }
5611 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005612#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005613 {
5614 c = p[2];
5615 p[2] = p[1];
5616 p[1] = *p;
5617 *p = c;
5618 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005619
Bram Moolenaar4770d092006-01-12 23:22:24 +00005620 /* Rotate three bytes right: "123" -> "312". We change "fword"
5621 * here, it's changed back afterwards at STATE_UNROT3R. */
5622 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5623 {
5624 go_deeper(stack, depth, SCORE_SWAP3);
5625#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005626 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005627 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5628 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5629 p[0], p[1], p[2]);
5630#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005631 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005632 sp->ts_state = STATE_UNROT3R;
5633 ++depth;
5634 p = fword + sp->ts_fidx;
5635#ifdef FEAT_MBYTE
5636 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005637 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005638 n = MB_CPTR2LEN(p);
5639 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005640 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005641 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005642 mch_memmove(p + tl, p, n);
5643 mb_char2bytes(c, p);
5644 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005645 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005646 else
5647#endif
5648 {
5649 c = p[2];
5650 p[2] = p[1];
5651 p[1] = *p;
5652 *p = c;
5653 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5654 }
5655 }
5656 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005657 {
5658 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005659 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005660 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005661 break;
5662
5663 case STATE_UNROT3R:
5664 /* Undo ROT3R: "312" -> "123" */
5665 p = fword + sp->ts_fidx;
5666#ifdef FEAT_MBYTE
5667 if (has_mbyte)
5668 {
5669 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005670 tl = MB_PTR2LEN(p);
5671 n = MB_PTR2LEN(p + tl);
5672 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005673 mch_memmove(p, p + tl, n);
5674 mb_char2bytes(c, p + n);
5675 }
5676 else
5677#endif
5678 {
5679 c = *p;
5680 *p = p[1];
5681 p[1] = p[2];
5682 p[2] = c;
5683 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005684 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005685
5686 case STATE_REP_INI:
5687 /* Check if matching with REP items from the .aff file would work.
5688 * Quickly skip if:
5689 * - there are no REP items and we are not in the soundfold trie
5690 * - the score is going to be too high anyway
5691 * - already applied a REP item or swapped here */
5692 if ((lp->lp_replang == NULL && !soundfold)
5693 || sp->ts_score + SCORE_REP >= su->su_maxscore
5694 || sp->ts_fidx < sp->ts_fidxtry)
5695 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005696 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005697 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005698 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005700
Bram Moolenaar4770d092006-01-12 23:22:24 +00005701 /* Use the first byte to quickly find the first entry that may
5702 * match. If the index is -1 there is none. */
5703 if (soundfold)
5704 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5705 else
5706 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005707
Bram Moolenaar4770d092006-01-12 23:22:24 +00005708 if (sp->ts_curi < 0)
5709 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005710 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005711 sp->ts_state = STATE_FINAL;
5712 break;
5713 }
5714
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005715 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005716 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005717 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005718
5719 case STATE_REP:
5720 /* Try matching with REP items from the .aff file. For each match
5721 * replace the characters and check if the resulting word is
5722 * valid. */
5723 p = fword + sp->ts_fidx;
5724
5725 if (soundfold)
5726 gap = &slang->sl_repsal;
5727 else
5728 gap = &lp->lp_replang->sl_rep;
5729 while (sp->ts_curi < gap->ga_len)
5730 {
5731 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5732 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005733 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005734 /* past possible matching entries */
5735 sp->ts_curi = gap->ga_len;
5736 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005737 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005738 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5739 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5740 {
5741 go_deeper(stack, depth, SCORE_REP);
5742#ifdef DEBUG_TRIEWALK
5743 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5744 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5745 ftp->ft_from, ftp->ft_to);
5746#endif
5747 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005748 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005749 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005750
Bram Moolenaar4770d092006-01-12 23:22:24 +00005751 /* Change the "from" to the "to" string. */
5752 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005753 fl = (int)STRLEN(ftp->ft_from);
5754 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005755 if (fl != tl)
5756 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005757 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005758 repextra += tl - fl;
5759 }
5760 mch_memmove(p, ftp->ft_to, tl);
5761 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
5762#ifdef FEAT_MBYTE
5763 stack[depth].ts_tcharlen = 0;
5764#endif
5765 break;
5766 }
5767 }
5768
5769 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005770 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005771 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005772 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005773 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005774 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005775
5776 break;
5777
5778 case STATE_REP_UNDO:
5779 /* Undo a REP replacement and continue with the next one. */
5780 if (soundfold)
5781 gap = &slang->sl_repsal;
5782 else
5783 gap = &lp->lp_replang->sl_rep;
5784 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005785 fl = (int)STRLEN(ftp->ft_from);
5786 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005787 p = fword + sp->ts_fidx;
5788 if (fl != tl)
5789 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005790 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005791 repextra -= tl - fl;
5792 }
5793 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005794 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005795 sp->ts_state = STATE_REP;
5796 break;
5797
5798 default:
5799 /* Did all possible states at this level, go up one level. */
5800 --depth;
5801
5802 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5803 {
5804 /* Continue in or go back to the prefix tree. */
5805 byts = pbyts;
5806 idxs = pidxs;
5807 }
5808
5809 /* Don't check for CTRL-C too often, it takes time. */
5810 if (--breakcheckcount == 0)
5811 {
5812 ui_breakcheck();
5813 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005814 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005815 }
5816 }
5817}
5818
Bram Moolenaar4770d092006-01-12 23:22:24 +00005819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005820/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005821 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005822 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005823 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005824go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005825{
Bram Moolenaarea424162005-06-16 21:51:00 +00005826 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005827 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005828 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005829 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005830 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005831}
5832
Bram Moolenaar53805d12005-08-01 07:08:33 +00005833#ifdef FEAT_MBYTE
5834/*
5835 * Case-folding may change the number of bytes: Count nr of chars in
5836 * fword[flen] and return the byte length of that many chars in "word".
5837 */
5838 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005839nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005840{
5841 char_u *p;
5842 int i = 0;
5843
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005844 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005845 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005846 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005847 --i;
5848 return (int)(p - word);
5849}
5850#endif
5851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005852/*
5853 * "fword" is a good word with case folded. Find the matching keep-case
5854 * words and put it in "kword".
5855 * Theoretically there could be several keep-case words that result in the
5856 * same case-folded word, but we only find one...
5857 */
5858 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005859find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005860{
5861 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5862 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005863 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005864
5865 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005866 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005867 int round[MAXWLEN];
5868 int fwordidx[MAXWLEN];
5869 int uwordidx[MAXWLEN];
5870 int kwordlen[MAXWLEN];
5871
5872 int flen, ulen;
5873 int l;
5874 int len;
5875 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005876 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005877 char_u *p;
5878 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005879 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005880
5881 if (byts == NULL)
5882 {
5883 /* array is empty: "cannot happen" */
5884 *kword = NUL;
5885 return;
5886 }
5887
5888 /* Make an all-cap version of "fword". */
5889 allcap_copy(fword, uword);
5890
5891 /*
5892 * Each character needs to be tried both case-folded and upper-case.
5893 * All this gets very complicated if we keep in mind that changing case
5894 * may change the byte length of a multi-byte character...
5895 */
5896 depth = 0;
5897 arridx[0] = 0;
5898 round[0] = 0;
5899 fwordidx[0] = 0;
5900 uwordidx[0] = 0;
5901 kwordlen[0] = 0;
5902 while (depth >= 0)
5903 {
5904 if (fword[fwordidx[depth]] == NUL)
5905 {
5906 /* We are at the end of "fword". If the tree allows a word to end
5907 * here we have found a match. */
5908 if (byts[arridx[depth] + 1] == 0)
5909 {
5910 kword[kwordlen[depth]] = NUL;
5911 return;
5912 }
5913
5914 /* kword is getting too long, continue one level up */
5915 --depth;
5916 }
5917 else if (++round[depth] > 2)
5918 {
5919 /* tried both fold-case and upper-case character, continue one
5920 * level up */
5921 --depth;
5922 }
5923 else
5924 {
5925 /*
5926 * round[depth] == 1: Try using the folded-case character.
5927 * round[depth] == 2: Try using the upper-case character.
5928 */
5929#ifdef FEAT_MBYTE
5930 if (has_mbyte)
5931 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005932 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5933 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005934 }
5935 else
5936#endif
5937 ulen = flen = 1;
5938 if (round[depth] == 1)
5939 {
5940 p = fword + fwordidx[depth];
5941 l = flen;
5942 }
5943 else
5944 {
5945 p = uword + uwordidx[depth];
5946 l = ulen;
5947 }
5948
5949 for (tryidx = arridx[depth]; l > 0; --l)
5950 {
5951 /* Perform a binary search in the list of accepted bytes. */
5952 len = byts[tryidx++];
5953 c = *p++;
5954 lo = tryidx;
5955 hi = tryidx + len - 1;
5956 while (lo < hi)
5957 {
5958 m = (lo + hi) / 2;
5959 if (byts[m] > c)
5960 hi = m - 1;
5961 else if (byts[m] < c)
5962 lo = m + 1;
5963 else
5964 {
5965 lo = hi = m;
5966 break;
5967 }
5968 }
5969
5970 /* Stop if there is no matching byte. */
5971 if (hi < lo || byts[lo] != c)
5972 break;
5973
5974 /* Continue at the child (if there is one). */
5975 tryidx = idxs[lo];
5976 }
5977
5978 if (l == 0)
5979 {
5980 /*
5981 * Found the matching char. Copy it to "kword" and go a
5982 * level deeper.
5983 */
5984 if (round[depth] == 1)
5985 {
5986 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5987 flen);
5988 kwordlen[depth + 1] = kwordlen[depth] + flen;
5989 }
5990 else
5991 {
5992 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5993 ulen);
5994 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5995 }
5996 fwordidx[depth + 1] = fwordidx[depth] + flen;
5997 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5998
5999 ++depth;
6000 arridx[depth] = tryidx;
6001 round[depth] = 0;
6002 }
6003 }
6004 }
6005
6006 /* Didn't find it: "cannot happen". */
6007 *kword = NUL;
6008}
6009
6010/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006011 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6012 * su->su_sga.
6013 */
6014 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006015score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006016{
6017 langp_T *lp;
6018 char_u badsound[MAXWLEN];
6019 int i;
6020 suggest_T *stp;
6021 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006022 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006023 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006024
6025 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6026 return;
6027
6028 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006029 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006030 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006031 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006032 if (lp->lp_slang->sl_sal.ga_len > 0)
6033 {
6034 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006035 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006036
6037 for (i = 0; i < su->su_ga.ga_len; ++i)
6038 {
6039 stp = &SUG(su->su_ga, i);
6040
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006041 /* Case-fold the suggested word, sound-fold it and compute the
6042 * sound-a-like score. */
6043 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006044 if (score < SCORE_MAXMAX)
6045 {
6046 /* Add the suggestion. */
6047 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6048 sstp->st_word = vim_strsave(stp->st_word);
6049 if (sstp->st_word != NULL)
6050 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006051 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006052 sstp->st_score = score;
6053 sstp->st_altscore = 0;
6054 sstp->st_orglen = stp->st_orglen;
6055 ++su->su_sga.ga_len;
6056 }
6057 }
6058 }
6059 break;
6060 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006061 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006062}
6063
6064/*
6065 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006066 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006067 */
6068 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006069score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006070{
6071 int i;
6072 int j;
6073 garray_T ga;
6074 garray_T *gap;
6075 langp_T *lp;
6076 suggest_T *stp;
6077 char_u *p;
6078 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006079 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006080 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006081 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006082
6083 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006084 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006085 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006086 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006087 if (lp->lp_slang->sl_sal.ga_len > 0)
6088 {
6089 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006090 slang = lp->lp_slang;
6091 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006092
6093 for (i = 0; i < su->su_ga.ga_len; ++i)
6094 {
6095 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006096 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006097 if (stp->st_altscore == SCORE_MAXMAX)
6098 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6099 else
6100 stp->st_score = (stp->st_score * 3
6101 + stp->st_altscore) / 4;
6102 stp->st_salscore = FALSE;
6103 }
6104 break;
6105 }
6106 }
6107
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006108 if (slang == NULL) /* Using "double" without sound folding. */
6109 {
6110 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
6111 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006112 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006113 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006114
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006115 /* Add the alternate score to su_sga. */
6116 for (i = 0; i < su->su_sga.ga_len; ++i)
6117 {
6118 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006119 stp->st_altscore = spell_edit_score(slang,
6120 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006121 if (stp->st_score == SCORE_MAXMAX)
6122 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6123 else
6124 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6125 stp->st_salscore = TRUE;
6126 }
6127
Bram Moolenaar4770d092006-01-12 23:22:24 +00006128 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
6129 * for both lists. */
6130 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006131 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006132 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006133 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6134
6135 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6136 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6137 return;
6138
6139 stp = &SUG(ga, 0);
6140 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6141 {
6142 /* round 1: get a suggestion from su_ga
6143 * round 2: get a suggestion from su_sga */
6144 for (round = 1; round <= 2; ++round)
6145 {
6146 gap = round == 1 ? &su->su_ga : &su->su_sga;
6147 if (i < gap->ga_len)
6148 {
6149 /* Don't add a word if it's already there. */
6150 p = SUG(*gap, i).st_word;
6151 for (j = 0; j < ga.ga_len; ++j)
6152 if (STRCMP(stp[j].st_word, p) == 0)
6153 break;
6154 if (j == ga.ga_len)
6155 stp[ga.ga_len++] = SUG(*gap, i);
6156 else
6157 vim_free(p);
6158 }
6159 }
6160 }
6161
6162 ga_clear(&su->su_ga);
6163 ga_clear(&su->su_sga);
6164
6165 /* Truncate the list to the number of suggestions that will be displayed. */
6166 if (ga.ga_len > su->su_maxcount)
6167 {
6168 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6169 vim_free(stp[i].st_word);
6170 ga.ga_len = su->su_maxcount;
6171 }
6172
6173 su->su_ga = ga;
6174}
6175
6176/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006177 * For the goodword in "stp" compute the soundalike score compared to the
6178 * badword.
6179 */
6180 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006181stp_sal_score(
6182 suggest_T *stp,
6183 suginfo_T *su,
6184 slang_T *slang,
6185 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006186{
6187 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006188 char_u *pbad;
6189 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006190 char_u badsound2[MAXWLEN];
6191 char_u fword[MAXWLEN];
6192 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006193 char_u goodword[MAXWLEN];
6194 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006195
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006196 lendiff = (int)(su->su_badlen - stp->st_orglen);
6197 if (lendiff >= 0)
6198 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006199 else
6200 {
6201 /* soundfold the bad word with more characters following */
6202 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6203
6204 /* When joining two words the sound often changes a lot. E.g., "t he"
6205 * sounds like "t h" while "the" sounds like "@". Avoid that by
6206 * removing the space. Don't do it when the good word also contains a
6207 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006208 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006209 && *skiptowhite(stp->st_word) == NUL)
6210 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006211 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006212
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006213 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006214 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006215 }
6216
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006217 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006218 {
6219 /* Add part of the bad word to the good word, so that we soundfold
6220 * what replaces the bad word. */
6221 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006222 vim_strncpy(goodword + stp->st_wordlen,
6223 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006224 pgood = goodword;
6225 }
6226 else
6227 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006228
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006229 /* Sound-fold the word and compute the score for the difference. */
6230 spell_soundfold(slang, pgood, FALSE, goodsound);
6231
6232 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006233}
6234
Bram Moolenaar4770d092006-01-12 23:22:24 +00006235/* structure used to store soundfolded words that add_sound_suggest() has
6236 * handled already. */
6237typedef struct
6238{
6239 short sft_score; /* lowest score used */
6240 char_u sft_word[1]; /* soundfolded word, actually longer */
6241} sftword_T;
6242
6243static sftword_T dumsft;
6244#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6245#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6246
6247/*
6248 * Prepare for calling suggest_try_soundalike().
6249 */
6250 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006251suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006252{
6253 langp_T *lp;
6254 int lpi;
6255 slang_T *slang;
6256
6257 /* Do this for all languages that support sound folding and for which a
6258 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006259 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006260 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006261 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006262 slang = lp->lp_slang;
6263 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6264 /* prepare the hashtable used by add_sound_suggest() */
6265 hash_init(&slang->sl_sounddone);
6266 }
6267}
6268
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006269/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006270 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006271 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006272 */
6273 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006274suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006275{
6276 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006277 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006278 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006279 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006280
Bram Moolenaar4770d092006-01-12 23:22:24 +00006281 /* Do this for all languages that support sound folding and for which a
6282 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006283 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006284 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006285 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006286 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006287 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006288 {
6289 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006290 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006291
Bram Moolenaar4770d092006-01-12 23:22:24 +00006292 /* try all kinds of inserts/deletes/swaps/etc. */
6293 /* TODO: also soundfold the next words, so that we can try joining
6294 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006295#ifdef SUGGEST_PROFILE
6296 prof_init();
6297#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006298 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006299#ifdef SUGGEST_PROFILE
6300 prof_report("soundalike");
6301#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006302 }
6303 }
6304}
6305
6306/*
6307 * Finish up after calling suggest_try_soundalike().
6308 */
6309 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006310suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006311{
6312 langp_T *lp;
6313 int lpi;
6314 slang_T *slang;
6315 int todo;
6316 hashitem_T *hi;
6317
6318 /* Do this for all languages that support sound folding and for which a
6319 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006320 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006321 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006322 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006323 slang = lp->lp_slang;
6324 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6325 {
6326 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006327 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006328 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6329 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006330 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006331 vim_free(HI2SFT(hi));
6332 --todo;
6333 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006334
6335 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006336 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006337 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006338 }
6339 }
6340}
6341
6342/*
6343 * A match with a soundfolded word is found. Add the good word(s) that
6344 * produce this soundfolded word.
6345 */
6346 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006347add_sound_suggest(
6348 suginfo_T *su,
6349 char_u *goodword,
6350 int score, /* soundfold score */
6351 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006352{
6353 slang_T *slang = lp->lp_slang; /* language for sound folding */
6354 int sfwordnr;
6355 char_u *nrline;
6356 int orgnr;
6357 char_u theword[MAXWLEN];
6358 int i;
6359 int wlen;
6360 char_u *byts;
6361 idx_T *idxs;
6362 int n;
6363 int wordcount;
6364 int wc;
6365 int goodscore;
6366 hash_T hash;
6367 hashitem_T *hi;
6368 sftword_T *sft;
6369 int bc, gc;
6370 int limit;
6371
6372 /*
6373 * It's very well possible that the same soundfold word is found several
6374 * times with different scores. Since the following is quite slow only do
6375 * the words that have a better score than before. Use a hashtable to
6376 * remember the words that have been done.
6377 */
6378 hash = hash_hash(goodword);
6379 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6380 if (HASHITEM_EMPTY(hi))
6381 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006382 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6383 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006384 if (sft != NULL)
6385 {
6386 sft->sft_score = score;
6387 STRCPY(sft->sft_word, goodword);
6388 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6389 }
6390 }
6391 else
6392 {
6393 sft = HI2SFT(hi);
6394 if (score >= sft->sft_score)
6395 return;
6396 sft->sft_score = score;
6397 }
6398
6399 /*
6400 * Find the word nr in the soundfold tree.
6401 */
6402 sfwordnr = soundfold_find(slang, goodword);
6403 if (sfwordnr < 0)
6404 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006405 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006406 return;
6407 }
6408
6409 /*
6410 * go over the list of good words that produce this soundfold word
6411 */
6412 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6413 orgnr = 0;
6414 while (*nrline != NUL)
6415 {
6416 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6417 * previous wordnr. */
6418 orgnr += bytes2offset(&nrline);
6419
6420 byts = slang->sl_fbyts;
6421 idxs = slang->sl_fidxs;
6422
6423 /* Lookup the word "orgnr" one of the two tries. */
6424 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006425 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006426 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006427 {
6428 i = 1;
6429 if (wordcount == orgnr && byts[n + 1] == NUL)
6430 break; /* found end of word */
6431
6432 if (byts[n + 1] == NUL)
6433 ++wordcount;
6434
6435 /* skip over the NUL bytes */
6436 for ( ; byts[n + i] == NUL; ++i)
6437 if (i > byts[n]) /* safety check */
6438 {
6439 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006440 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006441 goto badword;
6442 }
6443
6444 /* One of the siblings must have the word. */
6445 for ( ; i < byts[n]; ++i)
6446 {
6447 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6448 if (wordcount + wc > orgnr)
6449 break;
6450 wordcount += wc;
6451 }
6452
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006453 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006454 n = idxs[n + i];
6455 }
6456badword:
6457 theword[wlen] = NUL;
6458
6459 /* Go over the possible flags and regions. */
6460 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6461 {
6462 char_u cword[MAXWLEN];
6463 char_u *p;
6464 int flags = (int)idxs[n + i];
6465
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006466 /* Skip words with the NOSUGGEST flag */
6467 if (flags & WF_NOSUGGEST)
6468 continue;
6469
Bram Moolenaar4770d092006-01-12 23:22:24 +00006470 if (flags & WF_KEEPCAP)
6471 {
6472 /* Must find the word in the keep-case tree. */
6473 find_keepcap_word(slang, theword, cword);
6474 p = cword;
6475 }
6476 else
6477 {
6478 flags |= su->su_badflags;
6479 if ((flags & WF_CAPMASK) != 0)
6480 {
6481 /* Need to fix case according to "flags". */
6482 make_case_word(theword, cword, flags);
6483 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006484 }
6485 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006486 p = theword;
6487 }
6488
6489 /* Add the suggestion. */
6490 if (sps_flags & SPS_DOUBLE)
6491 {
6492 /* Add the suggestion if the score isn't too bad. */
6493 if (score <= su->su_maxscore)
6494 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6495 score, 0, FALSE, slang, FALSE);
6496 }
6497 else
6498 {
6499 /* Add a penalty for words in another region. */
6500 if ((flags & WF_REGION)
6501 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6502 goodscore = SCORE_REGION;
6503 else
6504 goodscore = 0;
6505
6506 /* Add a small penalty for changing the first letter from
6507 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006508 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006509 * letter is the same, that has already been counted. */
6510 gc = PTR2CHAR(p);
6511 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006512 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006513 bc = PTR2CHAR(su->su_badword);
6514 if (!SPELL_ISUPPER(bc)
6515 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6516 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006517 }
6518
Bram Moolenaar4770d092006-01-12 23:22:24 +00006519 /* Compute the score for the good word. This only does letter
6520 * insert/delete/swap/replace. REP items are not considered,
6521 * which may make the score a bit higher.
6522 * Use a limit for the score to make it work faster. Use
6523 * MAXSCORE(), because RESCORE() will change the score.
6524 * If the limit is very high then the iterative method is
6525 * inefficient, using an array is quicker. */
6526 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6527 if (limit > SCORE_LIMITMAX)
6528 goodscore += spell_edit_score(slang, su->su_badword, p);
6529 else
6530 goodscore += spell_edit_score_limit(slang, su->su_badword,
6531 p, limit);
6532
6533 /* When going over the limit don't bother to do the rest. */
6534 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006535 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006536 /* Give a bonus to words seen before. */
6537 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006538
Bram Moolenaar4770d092006-01-12 23:22:24 +00006539 /* Add the suggestion if the score isn't too bad. */
6540 goodscore = RESCORE(goodscore, score);
6541 if (goodscore <= su->su_sfmaxscore)
6542 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6543 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006544 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006545 }
6546 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006547 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006548 }
6549}
6550
6551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006552 * Find word "word" in fold-case tree for "slang" and return the word number.
6553 */
6554 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006555soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006556{
6557 idx_T arridx = 0;
6558 int len;
6559 int wlen = 0;
6560 int c;
6561 char_u *ptr = word;
6562 char_u *byts;
6563 idx_T *idxs;
6564 int wordnr = 0;
6565
6566 byts = slang->sl_sbyts;
6567 idxs = slang->sl_sidxs;
6568
6569 for (;;)
6570 {
6571 /* First byte is the number of possible bytes. */
6572 len = byts[arridx++];
6573
6574 /* If the first possible byte is a zero the word could end here.
6575 * If the word ends we found the word. If not skip the NUL bytes. */
6576 c = ptr[wlen];
6577 if (byts[arridx] == NUL)
6578 {
6579 if (c == NUL)
6580 break;
6581
6582 /* Skip over the zeros, there can be several. */
6583 while (len > 0 && byts[arridx] == NUL)
6584 {
6585 ++arridx;
6586 --len;
6587 }
6588 if (len == 0)
6589 return -1; /* no children, word should have ended here */
6590 ++wordnr;
6591 }
6592
6593 /* If the word ends we didn't find it. */
6594 if (c == NUL)
6595 return -1;
6596
6597 /* Perform a binary search in the list of accepted bytes. */
6598 if (c == TAB) /* <Tab> is handled like <Space> */
6599 c = ' ';
6600 while (byts[arridx] < c)
6601 {
6602 /* The word count is in the first idxs[] entry of the child. */
6603 wordnr += idxs[idxs[arridx]];
6604 ++arridx;
6605 if (--len == 0) /* end of the bytes, didn't find it */
6606 return -1;
6607 }
6608 if (byts[arridx] != c) /* didn't find the byte */
6609 return -1;
6610
6611 /* Continue at the child (if there is one). */
6612 arridx = idxs[arridx];
6613 ++wlen;
6614
6615 /* One space in the good word may stand for several spaces in the
6616 * checked word. */
6617 if (c == ' ')
6618 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6619 ++wlen;
6620 }
6621
6622 return wordnr;
6623}
6624
6625/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006626 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006627 */
6628 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006629make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006630{
6631 if (flags & WF_ALLCAP)
6632 /* Make it all upper-case */
6633 allcap_copy(fword, cword);
6634 else if (flags & WF_ONECAP)
6635 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006636 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006637 else
6638 /* Use goodword as-is. */
6639 STRCPY(cword, fword);
6640}
6641
Bram Moolenaarea424162005-06-16 21:51:00 +00006642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006643/*
6644 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6645 * lines in the .aff file.
6646 */
6647 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006648similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006649{
Bram Moolenaarea424162005-06-16 21:51:00 +00006650 int m1, m2;
6651#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006652 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006653 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006654
Bram Moolenaarea424162005-06-16 21:51:00 +00006655 if (c1 >= 256)
6656 {
6657 buf[mb_char2bytes(c1, buf)] = 0;
6658 hi = hash_find(&slang->sl_map_hash, buf);
6659 if (HASHITEM_EMPTY(hi))
6660 m1 = 0;
6661 else
6662 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6663 }
6664 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006665#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006666 m1 = slang->sl_map_array[c1];
6667 if (m1 == 0)
6668 return FALSE;
6669
6670
6671#ifdef FEAT_MBYTE
6672 if (c2 >= 256)
6673 {
6674 buf[mb_char2bytes(c2, buf)] = 0;
6675 hi = hash_find(&slang->sl_map_hash, buf);
6676 if (HASHITEM_EMPTY(hi))
6677 m2 = 0;
6678 else
6679 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6680 }
6681 else
6682#endif
6683 m2 = slang->sl_map_array[c2];
6684
6685 return m1 == m2;
6686}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006687
6688/*
6689 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006690 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006691 */
6692 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006693add_suggestion(
6694 suginfo_T *su,
6695 garray_T *gap, /* either su_ga or su_sga */
6696 char_u *goodword,
6697 int badlenarg, /* len of bad word replaced with "goodword" */
6698 int score,
6699 int altscore,
6700 int had_bonus, /* value for st_had_bonus */
6701 slang_T *slang, /* language for sound folding */
6702 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006703 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006704{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006705 int goodlen; /* len of goodword changed */
6706 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006707 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006708 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006709 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006710 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006711
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006712 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6713 * "thee the" is added next to changing the first "the" the "thee". */
6714 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006715 pbad = su->su_badptr + badlenarg;
6716 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006717 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006718 goodlen = (int)(pgood - goodword);
6719 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006720 if (goodlen <= 0 || badlen <= 0)
6721 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006722 MB_PTR_BACK(goodword, pgood);
6723 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006724#ifdef FEAT_MBYTE
6725 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006726 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006727 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6728 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006729 }
6730 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006731#endif
6732 if (*pgood != *pbad)
6733 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006734 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006735
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006736 if (badlen == 0 && goodlen == 0)
6737 /* goodword doesn't change anything; may happen for "the the" changing
6738 * the first "the" to itself. */
6739 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006740
Bram Moolenaar89d40322006-08-29 15:30:07 +00006741 if (gap->ga_len == 0)
6742 i = -1;
6743 else
6744 {
6745 /* Check if the word is already there. Also check the length that is
6746 * being replaced "thes," -> "these" is a different suggestion from
6747 * "thes" -> "these". */
6748 stp = &SUG(*gap, 0);
6749 for (i = gap->ga_len; --i >= 0; ++stp)
6750 if (stp->st_wordlen == goodlen
6751 && stp->st_orglen == badlen
6752 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006753 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006754 /*
6755 * Found it. Remember the word with the lowest score.
6756 */
6757 if (stp->st_slang == NULL)
6758 stp->st_slang = slang;
6759
6760 new_sug.st_score = score;
6761 new_sug.st_altscore = altscore;
6762 new_sug.st_had_bonus = had_bonus;
6763
6764 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006765 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006766 /* Only one of the two had the soundalike score computed.
6767 * Need to do that for the other one now, otherwise the
6768 * scores can't be compared. This happens because
6769 * suggest_try_change() doesn't compute the soundalike
6770 * word to keep it fast, while some special methods set
6771 * the soundalike score to zero. */
6772 if (had_bonus)
6773 rescore_one(su, stp);
6774 else
6775 {
6776 new_sug.st_word = stp->st_word;
6777 new_sug.st_wordlen = stp->st_wordlen;
6778 new_sug.st_slang = stp->st_slang;
6779 new_sug.st_orglen = badlen;
6780 rescore_one(su, &new_sug);
6781 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006783
Bram Moolenaar89d40322006-08-29 15:30:07 +00006784 if (stp->st_score > new_sug.st_score)
6785 {
6786 stp->st_score = new_sug.st_score;
6787 stp->st_altscore = new_sug.st_altscore;
6788 stp->st_had_bonus = new_sug.st_had_bonus;
6789 }
6790 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006791 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006793
Bram Moolenaar4770d092006-01-12 23:22:24 +00006794 if (i < 0 && ga_grow(gap, 1) == OK)
6795 {
6796 /* Add a suggestion. */
6797 stp = &SUG(*gap, gap->ga_len);
6798 stp->st_word = vim_strnsave(goodword, goodlen);
6799 if (stp->st_word != NULL)
6800 {
6801 stp->st_wordlen = goodlen;
6802 stp->st_score = score;
6803 stp->st_altscore = altscore;
6804 stp->st_had_bonus = had_bonus;
6805 stp->st_orglen = badlen;
6806 stp->st_slang = slang;
6807 ++gap->ga_len;
6808
6809 /* If we have too many suggestions now, sort the list and keep
6810 * the best suggestions. */
6811 if (gap->ga_len > SUG_MAX_COUNT(su))
6812 {
6813 if (maxsf)
6814 su->su_sfmaxscore = cleanup_suggestions(gap,
6815 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6816 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006817 su->su_maxscore = cleanup_suggestions(gap,
6818 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006819 }
6820 }
6821 }
6822}
6823
6824/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006825 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6826 * for split words, such as "the the". Remove these from the list here.
6827 */
6828 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006829check_suggestions(
6830 suginfo_T *su,
6831 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006832{
6833 suggest_T *stp;
6834 int i;
6835 char_u longword[MAXWLEN + 1];
6836 int len;
6837 hlf_T attr;
6838
6839 stp = &SUG(*gap, 0);
6840 for (i = gap->ga_len - 1; i >= 0; --i)
6841 {
6842 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006843 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006844 len = stp[i].st_wordlen;
6845 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6846 MAXWLEN - len);
6847 attr = HLF_COUNT;
6848 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6849 if (attr != HLF_COUNT)
6850 {
6851 /* Remove this entry. */
6852 vim_free(stp[i].st_word);
6853 --gap->ga_len;
6854 if (i < gap->ga_len)
6855 mch_memmove(stp + i, stp + i + 1,
6856 sizeof(suggest_T) * (gap->ga_len - i));
6857 }
6858 }
6859}
6860
6861
6862/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006863 * Add a word to be banned.
6864 */
6865 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006866add_banned(
6867 suginfo_T *su,
6868 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006869{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006870 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006871 hash_T hash;
6872 hashitem_T *hi;
6873
Bram Moolenaar4770d092006-01-12 23:22:24 +00006874 hash = hash_hash(word);
6875 hi = hash_lookup(&su->su_banned, word, hash);
6876 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006877 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006878 s = vim_strsave(word);
6879 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006880 hash_add_item(&su->su_banned, hi, s, hash);
6881 }
6882}
6883
6884/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006885 * Recompute the score for all suggestions if sound-folding is possible. This
6886 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006887 */
6888 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006889rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006890{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006891 int i;
6892
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006893 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006894 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006895 rescore_one(su, &SUG(su->su_ga, i));
6896}
6897
6898/*
6899 * Recompute the score for one suggestion if sound-folding is possible.
6900 */
6901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006902rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006903{
6904 slang_T *slang = stp->st_slang;
6905 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006906 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006907
6908 /* Only rescore suggestions that have no sal score yet and do have a
6909 * language. */
6910 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6911 {
6912 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006913 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006914 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006915 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006916 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006917 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006918 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006919
6920 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006921 if (stp->st_altscore == SCORE_MAXMAX)
6922 stp->st_altscore = SCORE_BIG;
6923 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6924 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006925 }
6926}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006928static int
6929#ifdef __BORLANDC__
6930_RTLENTRYF
6931#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006932sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006933
6934/*
6935 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006936 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006937 */
6938 static int
6939#ifdef __BORLANDC__
6940_RTLENTRYF
6941#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006942sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006943{
6944 suggest_T *p1 = (suggest_T *)s1;
6945 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006946 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006947
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006948 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006949 {
6950 n = p1->st_altscore - p2->st_altscore;
6951 if (n == 0)
6952 n = STRICMP(p1->st_word, p2->st_word);
6953 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006954 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006955}
6956
6957/*
6958 * Cleanup the suggestions:
6959 * - Sort on score.
6960 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006961 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006962 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006963 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006964cleanup_suggestions(
6965 garray_T *gap,
6966 int maxscore,
6967 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006968{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006969 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006970 int i;
6971
6972 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006973 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006974
6975 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006976 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006977 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006978 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006979 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006980 gap->ga_len = keep;
6981 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006982 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006983 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006984}
6985
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006986#if defined(FEAT_EVAL) || defined(PROTO)
6987/*
6988 * Soundfold a string, for soundfold().
6989 * Result is in allocated memory, NULL for an error.
6990 */
6991 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006992eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006993{
6994 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006995 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006996 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006997
Bram Moolenaar860cae12010-06-05 23:22:07 +02006998 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006999 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02007000 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007001 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02007002 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007003 if (lp->lp_slang->sl_sal.ga_len > 0)
7004 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007005 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007006 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007007 return vim_strsave(sound);
7008 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007009 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007010
7011 /* No language with sound folding, return word as-is. */
7012 return vim_strsave(word);
7013}
7014#endif
7015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007016/*
7017 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00007018 *
7019 * There are many ways to turn a word into a sound-a-like representation. The
7020 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
7021 * swedish name matching - survey and test of different algorithms" by Klas
7022 * Erikson.
7023 *
7024 * We support two methods:
7025 * 1. SOFOFROM/SOFOTO do a simple character mapping.
7026 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007027 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02007028 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007029spell_soundfold(
7030 slang_T *slang,
7031 char_u *inword,
7032 int folded, /* "inword" is already case-folded */
7033 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007034{
7035 char_u fword[MAXWLEN];
7036 char_u *word;
7037
7038 if (slang->sl_sofo)
7039 /* SOFOFROM and SOFOTO used */
7040 spell_soundfold_sofo(slang, inword, res);
7041 else
7042 {
7043 /* SAL items used. Requires the word to be case-folded. */
7044 if (folded)
7045 word = inword;
7046 else
7047 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007048 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007049 word = fword;
7050 }
7051
7052#ifdef FEAT_MBYTE
7053 if (has_mbyte)
7054 spell_soundfold_wsal(slang, word, res);
7055 else
7056#endif
7057 spell_soundfold_sal(slang, word, res);
7058 }
7059}
7060
7061/*
7062 * Perform sound folding of "inword" into "res" according to SOFOFROM and
7063 * SOFOTO lines.
7064 */
7065 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007066spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007067{
7068 char_u *s;
7069 int ri = 0;
7070 int c;
7071
7072#ifdef FEAT_MBYTE
7073 if (has_mbyte)
7074 {
7075 int prevc = 0;
7076 int *ip;
7077
7078 /* The sl_sal_first[] table contains the translation for chars up to
7079 * 255, sl_sal the rest. */
7080 for (s = inword; *s != NUL; )
7081 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007082 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007083 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007084 c = ' ';
7085 else if (c < 256)
7086 c = slang->sl_sal_first[c];
7087 else
7088 {
7089 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
7090 if (ip == NULL) /* empty list, can't match */
7091 c = NUL;
7092 else
7093 for (;;) /* find "c" in the list */
7094 {
7095 if (*ip == 0) /* not found */
7096 {
7097 c = NUL;
7098 break;
7099 }
7100 if (*ip == c) /* match! */
7101 {
7102 c = ip[1];
7103 break;
7104 }
7105 ip += 2;
7106 }
7107 }
7108
7109 if (c != NUL && c != prevc)
7110 {
7111 ri += mb_char2bytes(c, res + ri);
7112 if (ri + MB_MAXBYTES > MAXWLEN)
7113 break;
7114 prevc = c;
7115 }
7116 }
7117 }
7118 else
7119#endif
7120 {
7121 /* The sl_sal_first[] table contains the translation. */
7122 for (s = inword; (c = *s) != NUL; ++s)
7123 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007124 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007125 c = ' ';
7126 else
7127 c = slang->sl_sal_first[c];
7128 if (c != NUL && (ri == 0 || res[ri - 1] != c))
7129 res[ri++] = c;
7130 }
7131 }
7132
7133 res[ri] = NUL;
7134}
7135
7136 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007137spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007138{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007139 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007140 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007141 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007142 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007143 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007144 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007145 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007146 int n, k = 0;
7147 int z0;
7148 int k0;
7149 int n0;
7150 int c;
7151 int pri;
7152 int p0 = -333;
7153 int c0;
7154
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007155 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007156 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157 if (slang->sl_rem_accents)
7158 {
7159 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007160 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007161 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007162 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007163 {
7164 *t++ = ' ';
7165 s = skipwhite(s);
7166 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007167 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007168 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007169 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007170 *t++ = *s;
7171 ++s;
7172 }
7173 }
7174 *t = NUL;
7175 }
7176 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007177 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007178
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007179 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007180
7181 /*
7182 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007183 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007184 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007185 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007186 while ((c = word[i]) != NUL)
7187 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007188 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007189 n = slang->sl_sal_first[c];
7190 z0 = 0;
7191
7192 if (n >= 0)
7193 {
7194 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007195 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007196 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007197 /* Quickly skip entries that don't match the word. Most
7198 * entries are less then three chars, optimize for that. */
7199 k = smp[n].sm_leadlen;
7200 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007201 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007202 if (word[i + 1] != s[1])
7203 continue;
7204 if (k > 2)
7205 {
7206 for (j = 2; j < k; ++j)
7207 if (word[i + j] != s[j])
7208 break;
7209 if (j < k)
7210 continue;
7211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007212 }
7213
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007214 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007215 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007216 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007217 while (*pf != NUL && *pf != word[i + k])
7218 ++pf;
7219 if (*pf == NUL)
7220 continue;
7221 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007222 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007223 s = smp[n].sm_rules;
7224 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007225
7226 p0 = *s;
7227 k0 = k;
7228 while (*s == '-' && k > 1)
7229 {
7230 k--;
7231 s++;
7232 }
7233 if (*s == '<')
7234 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007235 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007236 {
7237 /* determine priority */
7238 pri = *s - '0';
7239 s++;
7240 }
7241 if (*s == '^' && *(s + 1) == '^')
7242 s++;
7243
7244 if (*s == NUL
7245 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007246 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007247 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007248 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007249 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007250 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007251 && spell_iswordp(word + i - 1, curwin)
7252 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007253 {
7254 /* search for followup rules, if: */
7255 /* followup and k > 1 and NO '-' in searchstring */
7256 c0 = word[i + k - 1];
7257 n0 = slang->sl_sal_first[c0];
7258
7259 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007260 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007261 {
7262 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007263 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007264 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007265 /* Quickly skip entries that don't match the word.
7266 * */
7267 k0 = smp[n0].sm_leadlen;
7268 if (k0 > 1)
7269 {
7270 if (word[i + k] != s[1])
7271 continue;
7272 if (k0 > 2)
7273 {
7274 pf = word + i + k + 1;
7275 for (j = 2; j < k0; ++j)
7276 if (*pf++ != s[j])
7277 break;
7278 if (j < k0)
7279 continue;
7280 }
7281 }
7282 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007283
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007284 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007285 {
7286 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007287 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007288 while (*pf != NUL && *pf != word[i + k0])
7289 ++pf;
7290 if (*pf == NUL)
7291 continue;
7292 ++k0;
7293 }
7294
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007296 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 while (*s == '-')
7298 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007299 /* "k0" gets NOT reduced because
7300 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007301 s++;
7302 }
7303 if (*s == '<')
7304 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007305 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 {
7307 p0 = *s - '0';
7308 s++;
7309 }
7310
7311 if (*s == NUL
7312 /* *s == '^' cuts */
7313 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007314 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007315 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007316 {
7317 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320
7321 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007322 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007323 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 /* rule fits; stop search */
7325 break;
7326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007327 }
7328
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007329 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007331 }
7332
7333 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007334 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007335 if (s == NULL)
7336 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007337 pf = smp[n].sm_rules;
7338 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007339 if (p0 == 1 && z == 0)
7340 {
7341 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007342 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7343 || res[reslen - 1] == *s))
7344 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345 z0 = 1;
7346 z = 1;
7347 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007348 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349 {
7350 word[i + k0] = *s;
7351 k0++;
7352 s++;
7353 }
7354 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007355 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007356
7357 /* new "actual letter" */
7358 c = word[i];
7359 }
7360 else
7361 {
7362 /* no '<' rule used */
7363 i += k - 1;
7364 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007365 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007366 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007367 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007368 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007369 s++;
7370 }
7371 /* new "actual letter" */
7372 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007373 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007374 {
7375 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007376 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007377 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378 i = 0;
7379 z0 = 1;
7380 }
7381 }
7382 break;
7383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007384 }
7385 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007386 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007387 {
7388 c = ' ';
7389 k = 1;
7390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007391
7392 if (z0 == 0)
7393 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007394 if (k && !p0 && reslen < MAXWLEN && c != NUL
7395 && (!slang->sl_collapse || reslen == 0
7396 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007397 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007398 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007399
7400 i++;
7401 z = 0;
7402 k = 0;
7403 }
7404 }
7405
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007406 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407}
7408
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007409#ifdef FEAT_MBYTE
7410/*
7411 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7412 * Multi-byte version of spell_soundfold().
7413 */
7414 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007415spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007416{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007417 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007418 int word[MAXWLEN];
7419 int wres[MAXWLEN];
7420 int l;
7421 char_u *s;
7422 int *ws;
7423 char_u *t;
7424 int *pf;
7425 int i, j, z;
7426 int reslen;
7427 int n, k = 0;
7428 int z0;
7429 int k0;
7430 int n0;
7431 int c;
7432 int pri;
7433 int p0 = -333;
7434 int c0;
7435 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007436 int wordlen;
7437
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007438
7439 /*
7440 * Convert the multi-byte string to a wide-character string.
7441 * Remove accents, if wanted. We actually remove all non-word characters.
7442 * But keep white space.
7443 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007444 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007445 for (s = inword; *s != NUL; )
7446 {
7447 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007448 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007449 if (slang->sl_rem_accents)
7450 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007451 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007452 {
7453 if (did_white)
7454 continue;
7455 c = ' ';
7456 did_white = TRUE;
7457 }
7458 else
7459 {
7460 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007461 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007462 continue;
7463 }
7464 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007465 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007466 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007467 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007468
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007469 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007470 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007471 * Converted from C++ to C. Added support for multi-byte chars.
7472 * Changed to keep spaces.
7473 */
7474 i = reslen = z = 0;
7475 while ((c = word[i]) != NUL)
7476 {
7477 /* Start with the first rule that has the character in the word. */
7478 n = slang->sl_sal_first[c & 0xff];
7479 z0 = 0;
7480
7481 if (n >= 0)
7482 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007483 /* Check all rules for the same index byte.
7484 * If c is 0x300 need extra check for the end of the array, as
7485 * (c & 0xff) is NUL. */
7486 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7487 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007488 {
7489 /* Quickly skip entries that don't match the word. Most
7490 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007491 if (c != ws[0])
7492 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007493 k = smp[n].sm_leadlen;
7494 if (k > 1)
7495 {
7496 if (word[i + 1] != ws[1])
7497 continue;
7498 if (k > 2)
7499 {
7500 for (j = 2; j < k; ++j)
7501 if (word[i + j] != ws[j])
7502 break;
7503 if (j < k)
7504 continue;
7505 }
7506 }
7507
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007508 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007509 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007510 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007511 while (*pf != NUL && *pf != word[i + k])
7512 ++pf;
7513 if (*pf == NUL)
7514 continue;
7515 ++k;
7516 }
7517 s = smp[n].sm_rules;
7518 pri = 5; /* default priority */
7519
7520 p0 = *s;
7521 k0 = k;
7522 while (*s == '-' && k > 1)
7523 {
7524 k--;
7525 s++;
7526 }
7527 if (*s == '<')
7528 s++;
7529 if (VIM_ISDIGIT(*s))
7530 {
7531 /* determine priority */
7532 pri = *s - '0';
7533 s++;
7534 }
7535 if (*s == '^' && *(s + 1) == '^')
7536 s++;
7537
7538 if (*s == NUL
7539 || (*s == '^'
7540 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007541 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007542 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007543 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007544 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007545 && spell_iswordp_w(word + i - 1, curwin)
7546 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007547 {
7548 /* search for followup rules, if: */
7549 /* followup and k > 1 and NO '-' in searchstring */
7550 c0 = word[i + k - 1];
7551 n0 = slang->sl_sal_first[c0 & 0xff];
7552
7553 if (slang->sl_followup && k > 1 && n0 >= 0
7554 && p0 != '-' && word[i + k] != NUL)
7555 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007556 /* Test follow-up rule for "word[i + k]"; loop over
7557 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007558 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7559 == (c0 & 0xff); ++n0)
7560 {
7561 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007562 */
7563 if (c0 != ws[0])
7564 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007565 k0 = smp[n0].sm_leadlen;
7566 if (k0 > 1)
7567 {
7568 if (word[i + k] != ws[1])
7569 continue;
7570 if (k0 > 2)
7571 {
7572 pf = word + i + k + 1;
7573 for (j = 2; j < k0; ++j)
7574 if (*pf++ != ws[j])
7575 break;
7576 if (j < k0)
7577 continue;
7578 }
7579 }
7580 k0 += k - 1;
7581
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007582 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007583 {
7584 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007585 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007586 while (*pf != NUL && *pf != word[i + k0])
7587 ++pf;
7588 if (*pf == NUL)
7589 continue;
7590 ++k0;
7591 }
7592
7593 p0 = 5;
7594 s = smp[n0].sm_rules;
7595 while (*s == '-')
7596 {
7597 /* "k0" gets NOT reduced because
7598 * "if (k0 == k)" */
7599 s++;
7600 }
7601 if (*s == '<')
7602 s++;
7603 if (VIM_ISDIGIT(*s))
7604 {
7605 p0 = *s - '0';
7606 s++;
7607 }
7608
7609 if (*s == NUL
7610 /* *s == '^' cuts */
7611 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007612 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007613 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007614 {
7615 if (k0 == k)
7616 /* this is just a piece of the string */
7617 continue;
7618
7619 if (p0 < pri)
7620 /* priority too low */
7621 continue;
7622 /* rule fits; stop search */
7623 break;
7624 }
7625 }
7626
7627 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7628 == (c0 & 0xff))
7629 continue;
7630 }
7631
7632 /* replace string */
7633 ws = smp[n].sm_to_w;
7634 s = smp[n].sm_rules;
7635 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7636 if (p0 == 1 && z == 0)
7637 {
7638 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007639 if (reslen > 0 && ws != NULL && *ws != NUL
7640 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007641 || wres[reslen - 1] == *ws))
7642 reslen--;
7643 z0 = 1;
7644 z = 1;
7645 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007646 if (ws != NULL)
7647 while (*ws != NUL && word[i + k0] != NUL)
7648 {
7649 word[i + k0] = *ws;
7650 k0++;
7651 ws++;
7652 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007653 if (k > k0)
7654 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007655 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007656
7657 /* new "actual letter" */
7658 c = word[i];
7659 }
7660 else
7661 {
7662 /* no '<' rule used */
7663 i += k - 1;
7664 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007665 if (ws != NULL)
7666 while (*ws != NUL && ws[1] != NUL
7667 && reslen < MAXWLEN)
7668 {
7669 if (reslen == 0 || wres[reslen - 1] != *ws)
7670 wres[reslen++] = *ws;
7671 ws++;
7672 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007673 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007674 if (ws == NULL)
7675 c = NUL;
7676 else
7677 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007678 if (strstr((char *)s, "^^") != NULL)
7679 {
7680 if (c != NUL)
7681 wres[reslen++] = c;
7682 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007683 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007684 i = 0;
7685 z0 = 1;
7686 }
7687 }
7688 break;
7689 }
7690 }
7691 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007692 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007693 {
7694 c = ' ';
7695 k = 1;
7696 }
7697
7698 if (z0 == 0)
7699 {
7700 if (k && !p0 && reslen < MAXWLEN && c != NUL
7701 && (!slang->sl_collapse || reslen == 0
7702 || wres[reslen - 1] != c))
7703 /* condense only double letters */
7704 wres[reslen++] = c;
7705
7706 i++;
7707 z = 0;
7708 k = 0;
7709 }
7710 }
7711
7712 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7713 l = 0;
7714 for (n = 0; n < reslen; ++n)
7715 {
7716 l += mb_char2bytes(wres[n], res + l);
7717 if (l + MB_MAXBYTES > MAXWLEN)
7718 break;
7719 }
7720 res[l] = NUL;
7721}
7722#endif
7723
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007724/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007725 * Compute a score for two sound-a-like words.
7726 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7727 * Instead of a generic loop we write out the code. That keeps it fast by
7728 * avoiding checks that will not be possible.
7729 */
7730 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007731soundalike_score(
7732 char_u *goodstart, /* sound-folded good word */
7733 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007734{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007735 char_u *goodsound = goodstart;
7736 char_u *badsound = badstart;
7737 int goodlen;
7738 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007739 int n;
7740 char_u *pl, *ps;
7741 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007742 int score = 0;
7743
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007744 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007745 * counted so much, vowels halfway the word aren't counted at all. */
7746 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7747 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007748 if ((badsound[0] == NUL && goodsound[1] == NUL)
7749 || (goodsound[0] == NUL && badsound[1] == NUL))
7750 /* changing word with vowel to word without a sound */
7751 return SCORE_DEL;
7752 if (badsound[0] == NUL || goodsound[0] == NUL)
7753 /* more than two changes */
7754 return SCORE_MAXMAX;
7755
Bram Moolenaar4770d092006-01-12 23:22:24 +00007756 if (badsound[1] == goodsound[1]
7757 || (badsound[1] != NUL
7758 && goodsound[1] != NUL
7759 && badsound[2] == goodsound[2]))
7760 {
7761 /* handle like a substitute */
7762 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007763 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007764 {
7765 score = 2 * SCORE_DEL / 3;
7766 if (*badsound == '*')
7767 ++badsound;
7768 else
7769 ++goodsound;
7770 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007771 }
7772
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007773 goodlen = (int)STRLEN(goodsound);
7774 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007775
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007776 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007777 * changes. */
7778 n = goodlen - badlen;
7779 if (n < -2 || n > 2)
7780 return SCORE_MAXMAX;
7781
7782 if (n > 0)
7783 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007784 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007785 ps = badsound;
7786 }
7787 else
7788 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007789 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007790 ps = goodsound;
7791 }
7792
7793 /* Skip over the identical part. */
7794 while (*pl == *ps && *pl != NUL)
7795 {
7796 ++pl;
7797 ++ps;
7798 }
7799
7800 switch (n)
7801 {
7802 case -2:
7803 case 2:
7804 /*
7805 * Must delete two characters from "pl".
7806 */
7807 ++pl; /* first delete */
7808 while (*pl == *ps)
7809 {
7810 ++pl;
7811 ++ps;
7812 }
7813 /* strings must be equal after second delete */
7814 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007815 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007816
7817 /* Failed to compare. */
7818 break;
7819
7820 case -1:
7821 case 1:
7822 /*
7823 * Minimal one delete from "pl" required.
7824 */
7825
7826 /* 1: delete */
7827 pl2 = pl + 1;
7828 ps2 = ps;
7829 while (*pl2 == *ps2)
7830 {
7831 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007832 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007833 ++pl2;
7834 ++ps2;
7835 }
7836
7837 /* 2: delete then swap, then rest must be equal */
7838 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7839 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007840 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007841
7842 /* 3: delete then substitute, then the rest must be equal */
7843 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007844 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007845
7846 /* 4: first swap then delete */
7847 if (pl[0] == ps[1] && pl[1] == ps[0])
7848 {
7849 pl2 = pl + 2; /* swap, skip two chars */
7850 ps2 = ps + 2;
7851 while (*pl2 == *ps2)
7852 {
7853 ++pl2;
7854 ++ps2;
7855 }
7856 /* delete a char and then strings must be equal */
7857 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007858 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007859 }
7860
7861 /* 5: first substitute then delete */
7862 pl2 = pl + 1; /* substitute, skip one char */
7863 ps2 = ps + 1;
7864 while (*pl2 == *ps2)
7865 {
7866 ++pl2;
7867 ++ps2;
7868 }
7869 /* delete a char and then strings must be equal */
7870 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007871 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007872
7873 /* Failed to compare. */
7874 break;
7875
7876 case 0:
7877 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007878 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007879 * insert is only possible in combination with a delete.
7880 * 1: check if for identical strings
7881 */
7882 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007883 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007884
7885 /* 2: swap */
7886 if (pl[0] == ps[1] && pl[1] == ps[0])
7887 {
7888 pl2 = pl + 2; /* swap, skip two chars */
7889 ps2 = ps + 2;
7890 while (*pl2 == *ps2)
7891 {
7892 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007893 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007894 ++pl2;
7895 ++ps2;
7896 }
7897 /* 3: swap and swap again */
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_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007901
7902 /* 4: swap and substitute */
7903 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007904 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007905 }
7906
7907 /* 5: substitute */
7908 pl2 = pl + 1;
7909 ps2 = ps + 1;
7910 while (*pl2 == *ps2)
7911 {
7912 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007913 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007914 ++pl2;
7915 ++ps2;
7916 }
7917
7918 /* 6: substitute and swap */
7919 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7920 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007921 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007922
7923 /* 7: substitute and substitute */
7924 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007925 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007926
7927 /* 8: insert then delete */
7928 pl2 = pl;
7929 ps2 = ps + 1;
7930 while (*pl2 == *ps2)
7931 {
7932 ++pl2;
7933 ++ps2;
7934 }
7935 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007936 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007937
7938 /* 9: delete then insert */
7939 pl2 = pl + 1;
7940 ps2 = ps;
7941 while (*pl2 == *ps2)
7942 {
7943 ++pl2;
7944 ++ps2;
7945 }
7946 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007947 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007948
7949 /* Failed to compare. */
7950 break;
7951 }
7952
7953 return SCORE_MAXMAX;
7954}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007956/*
7957 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007958 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007959 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007960 * The algorithm is described by Du and Chang, 1992.
7961 * The implementation of the algorithm comes from Aspell editdist.cpp,
7962 * edit_distance(). It has been converted from C++ to C and modified to
7963 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007964 */
7965 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007966spell_edit_score(
7967 slang_T *slang,
7968 char_u *badword,
7969 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007970{
7971 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007972 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007973 int j, i;
7974 int t;
7975 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007976 int pbc, pgc;
7977#ifdef FEAT_MBYTE
7978 char_u *p;
7979 int wbadword[MAXWLEN];
7980 int wgoodword[MAXWLEN];
7981
7982 if (has_mbyte)
7983 {
7984 /* Get the characters from the multi-byte strings and put them in an
7985 * int array for easy access. */
7986 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007987 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007988 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007989 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007990 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007991 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007992 }
7993 else
7994#endif
7995 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007996 badlen = (int)STRLEN(badword) + 1;
7997 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007998 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007999
8000 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
8001#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
8003 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008004 if (cnt == NULL)
8005 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008006
8007 CNT(0, 0) = 0;
8008 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008009 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008010
8011 for (i = 1; i <= badlen; ++i)
8012 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00008013 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008014 for (j = 1; j <= goodlen; ++j)
8015 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008016#ifdef FEAT_MBYTE
8017 if (has_mbyte)
8018 {
8019 bc = wbadword[i - 1];
8020 gc = wgoodword[j - 1];
8021 }
8022 else
8023#endif
8024 {
8025 bc = badword[i - 1];
8026 gc = goodword[j - 1];
8027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008028 if (bc == gc)
8029 CNT(i, j) = CNT(i - 1, j - 1);
8030 else
8031 {
8032 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008033 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008034 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
8035 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008036 {
8037 /* For a similar character use SCORE_SIMILAR. */
8038 if (slang != NULL
8039 && slang->sl_has_map
8040 && similar_chars(slang, gc, bc))
8041 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
8042 else
8043 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
8044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008045
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008046 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008047 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008048#ifdef FEAT_MBYTE
8049 if (has_mbyte)
8050 {
8051 pbc = wbadword[i - 2];
8052 pgc = wgoodword[j - 2];
8053 }
8054 else
8055#endif
8056 {
8057 pbc = badword[i - 2];
8058 pgc = goodword[j - 2];
8059 }
8060 if (bc == pgc && pbc == gc)
8061 {
8062 t = SCORE_SWAP + CNT(i - 2, j - 2);
8063 if (t < CNT(i, j))
8064 CNT(i, j) = t;
8065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008066 }
8067 t = SCORE_DEL + CNT(i - 1, j);
8068 if (t < CNT(i, j))
8069 CNT(i, j) = t;
8070 t = SCORE_INS + CNT(i, j - 1);
8071 if (t < CNT(i, j))
8072 CNT(i, j) = t;
8073 }
8074 }
8075 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008076
8077 i = CNT(badlen - 1, goodlen - 1);
8078 vim_free(cnt);
8079 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008081
Bram Moolenaar4770d092006-01-12 23:22:24 +00008082typedef struct
8083{
8084 int badi;
8085 int goodi;
8086 int score;
8087} limitscore_T;
8088
8089/*
8090 * Like spell_edit_score(), but with a limit on the score to make it faster.
8091 * May return SCORE_MAXMAX when the score is higher than "limit".
8092 *
8093 * This uses a stack for the edits still to be tried.
8094 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
8095 * for multi-byte characters.
8096 */
8097 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008098spell_edit_score_limit(
8099 slang_T *slang,
8100 char_u *badword,
8101 char_u *goodword,
8102 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008103{
8104 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8105 int stackidx;
8106 int bi, gi;
8107 int bi2, gi2;
8108 int bc, gc;
8109 int score;
8110 int score_off;
8111 int minscore;
8112 int round;
8113
8114#ifdef FEAT_MBYTE
8115 /* Multi-byte characters require a bit more work, use a different function
8116 * to avoid testing "has_mbyte" quite often. */
8117 if (has_mbyte)
8118 return spell_edit_score_limit_w(slang, badword, goodword, limit);
8119#endif
8120
8121 /*
8122 * The idea is to go from start to end over the words. So long as
8123 * characters are equal just continue, this always gives the lowest score.
8124 * When there is a difference try several alternatives. Each alternative
8125 * increases "score" for the edit distance. Some of the alternatives are
8126 * pushed unto a stack and tried later, some are tried right away. At the
8127 * end of the word the score for one alternative is known. The lowest
8128 * possible score is stored in "minscore".
8129 */
8130 stackidx = 0;
8131 bi = 0;
8132 gi = 0;
8133 score = 0;
8134 minscore = limit + 1;
8135
8136 for (;;)
8137 {
8138 /* Skip over an equal part, score remains the same. */
8139 for (;;)
8140 {
8141 bc = badword[bi];
8142 gc = goodword[gi];
8143 if (bc != gc) /* stop at a char that's different */
8144 break;
8145 if (bc == NUL) /* both words end */
8146 {
8147 if (score < minscore)
8148 minscore = score;
8149 goto pop; /* do next alternative */
8150 }
8151 ++bi;
8152 ++gi;
8153 }
8154
8155 if (gc == NUL) /* goodword ends, delete badword chars */
8156 {
8157 do
8158 {
8159 if ((score += SCORE_DEL) >= minscore)
8160 goto pop; /* do next alternative */
8161 } while (badword[++bi] != NUL);
8162 minscore = score;
8163 }
8164 else if (bc == NUL) /* badword ends, insert badword chars */
8165 {
8166 do
8167 {
8168 if ((score += SCORE_INS) >= minscore)
8169 goto pop; /* do next alternative */
8170 } while (goodword[++gi] != NUL);
8171 minscore = score;
8172 }
8173 else /* both words continue */
8174 {
8175 /* If not close to the limit, perform a change. Only try changes
8176 * that may lead to a lower score than "minscore".
8177 * round 0: try deleting a char from badword
8178 * round 1: try inserting a char in badword */
8179 for (round = 0; round <= 1; ++round)
8180 {
8181 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8182 if (score_off < minscore)
8183 {
8184 if (score_off + SCORE_EDIT_MIN >= minscore)
8185 {
8186 /* Near the limit, rest of the words must match. We
8187 * can check that right now, no need to push an item
8188 * onto the stack. */
8189 bi2 = bi + 1 - round;
8190 gi2 = gi + round;
8191 while (goodword[gi2] == badword[bi2])
8192 {
8193 if (goodword[gi2] == NUL)
8194 {
8195 minscore = score_off;
8196 break;
8197 }
8198 ++bi2;
8199 ++gi2;
8200 }
8201 }
8202 else
8203 {
8204 /* try deleting/inserting a character later */
8205 stack[stackidx].badi = bi + 1 - round;
8206 stack[stackidx].goodi = gi + round;
8207 stack[stackidx].score = score_off;
8208 ++stackidx;
8209 }
8210 }
8211 }
8212
8213 if (score + SCORE_SWAP < minscore)
8214 {
8215 /* If swapping two characters makes a match then the
8216 * substitution is more expensive, thus there is no need to
8217 * try both. */
8218 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8219 {
8220 /* Swap two characters, that is: skip them. */
8221 gi += 2;
8222 bi += 2;
8223 score += SCORE_SWAP;
8224 continue;
8225 }
8226 }
8227
8228 /* Substitute one character for another which is the same
8229 * thing as deleting a character from both goodword and badword.
8230 * Use a better score when there is only a case difference. */
8231 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8232 score += SCORE_ICASE;
8233 else
8234 {
8235 /* For a similar character use SCORE_SIMILAR. */
8236 if (slang != NULL
8237 && slang->sl_has_map
8238 && similar_chars(slang, gc, bc))
8239 score += SCORE_SIMILAR;
8240 else
8241 score += SCORE_SUBST;
8242 }
8243
8244 if (score < minscore)
8245 {
8246 /* Do the substitution. */
8247 ++gi;
8248 ++bi;
8249 continue;
8250 }
8251 }
8252pop:
8253 /*
8254 * Get here to try the next alternative, pop it from the stack.
8255 */
8256 if (stackidx == 0) /* stack is empty, finished */
8257 break;
8258
8259 /* pop an item from the stack */
8260 --stackidx;
8261 gi = stack[stackidx].goodi;
8262 bi = stack[stackidx].badi;
8263 score = stack[stackidx].score;
8264 }
8265
8266 /* When the score goes over "limit" it may actually be much higher.
8267 * Return a very large number to avoid going below the limit when giving a
8268 * bonus. */
8269 if (minscore > limit)
8270 return SCORE_MAXMAX;
8271 return minscore;
8272}
8273
8274#ifdef FEAT_MBYTE
8275/*
8276 * Multi-byte version of spell_edit_score_limit().
8277 * Keep it in sync with the above!
8278 */
8279 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008280spell_edit_score_limit_w(
8281 slang_T *slang,
8282 char_u *badword,
8283 char_u *goodword,
8284 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008285{
8286 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8287 int stackidx;
8288 int bi, gi;
8289 int bi2, gi2;
8290 int bc, gc;
8291 int score;
8292 int score_off;
8293 int minscore;
8294 int round;
8295 char_u *p;
8296 int wbadword[MAXWLEN];
8297 int wgoodword[MAXWLEN];
8298
8299 /* Get the characters from the multi-byte strings and put them in an
8300 * int array for easy access. */
8301 bi = 0;
8302 for (p = badword; *p != NUL; )
8303 wbadword[bi++] = mb_cptr2char_adv(&p);
8304 wbadword[bi++] = 0;
8305 gi = 0;
8306 for (p = goodword; *p != NUL; )
8307 wgoodword[gi++] = mb_cptr2char_adv(&p);
8308 wgoodword[gi++] = 0;
8309
8310 /*
8311 * The idea is to go from start to end over the words. So long as
8312 * characters are equal just continue, this always gives the lowest score.
8313 * When there is a difference try several alternatives. Each alternative
8314 * increases "score" for the edit distance. Some of the alternatives are
8315 * pushed unto a stack and tried later, some are tried right away. At the
8316 * end of the word the score for one alternative is known. The lowest
8317 * possible score is stored in "minscore".
8318 */
8319 stackidx = 0;
8320 bi = 0;
8321 gi = 0;
8322 score = 0;
8323 minscore = limit + 1;
8324
8325 for (;;)
8326 {
8327 /* Skip over an equal part, score remains the same. */
8328 for (;;)
8329 {
8330 bc = wbadword[bi];
8331 gc = wgoodword[gi];
8332
8333 if (bc != gc) /* stop at a char that's different */
8334 break;
8335 if (bc == NUL) /* both words end */
8336 {
8337 if (score < minscore)
8338 minscore = score;
8339 goto pop; /* do next alternative */
8340 }
8341 ++bi;
8342 ++gi;
8343 }
8344
8345 if (gc == NUL) /* goodword ends, delete badword chars */
8346 {
8347 do
8348 {
8349 if ((score += SCORE_DEL) >= minscore)
8350 goto pop; /* do next alternative */
8351 } while (wbadword[++bi] != NUL);
8352 minscore = score;
8353 }
8354 else if (bc == NUL) /* badword ends, insert badword chars */
8355 {
8356 do
8357 {
8358 if ((score += SCORE_INS) >= minscore)
8359 goto pop; /* do next alternative */
8360 } while (wgoodword[++gi] != NUL);
8361 minscore = score;
8362 }
8363 else /* both words continue */
8364 {
8365 /* If not close to the limit, perform a change. Only try changes
8366 * that may lead to a lower score than "minscore".
8367 * round 0: try deleting a char from badword
8368 * round 1: try inserting a char in badword */
8369 for (round = 0; round <= 1; ++round)
8370 {
8371 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8372 if (score_off < minscore)
8373 {
8374 if (score_off + SCORE_EDIT_MIN >= minscore)
8375 {
8376 /* Near the limit, rest of the words must match. We
8377 * can check that right now, no need to push an item
8378 * onto the stack. */
8379 bi2 = bi + 1 - round;
8380 gi2 = gi + round;
8381 while (wgoodword[gi2] == wbadword[bi2])
8382 {
8383 if (wgoodword[gi2] == NUL)
8384 {
8385 minscore = score_off;
8386 break;
8387 }
8388 ++bi2;
8389 ++gi2;
8390 }
8391 }
8392 else
8393 {
8394 /* try deleting a character from badword later */
8395 stack[stackidx].badi = bi + 1 - round;
8396 stack[stackidx].goodi = gi + round;
8397 stack[stackidx].score = score_off;
8398 ++stackidx;
8399 }
8400 }
8401 }
8402
8403 if (score + SCORE_SWAP < minscore)
8404 {
8405 /* If swapping two characters makes a match then the
8406 * substitution is more expensive, thus there is no need to
8407 * try both. */
8408 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8409 {
8410 /* Swap two characters, that is: skip them. */
8411 gi += 2;
8412 bi += 2;
8413 score += SCORE_SWAP;
8414 continue;
8415 }
8416 }
8417
8418 /* Substitute one character for another which is the same
8419 * thing as deleting a character from both goodword and badword.
8420 * Use a better score when there is only a case difference. */
8421 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8422 score += SCORE_ICASE;
8423 else
8424 {
8425 /* For a similar character use SCORE_SIMILAR. */
8426 if (slang != NULL
8427 && slang->sl_has_map
8428 && similar_chars(slang, gc, bc))
8429 score += SCORE_SIMILAR;
8430 else
8431 score += SCORE_SUBST;
8432 }
8433
8434 if (score < minscore)
8435 {
8436 /* Do the substitution. */
8437 ++gi;
8438 ++bi;
8439 continue;
8440 }
8441 }
8442pop:
8443 /*
8444 * Get here to try the next alternative, pop it from the stack.
8445 */
8446 if (stackidx == 0) /* stack is empty, finished */
8447 break;
8448
8449 /* pop an item from the stack */
8450 --stackidx;
8451 gi = stack[stackidx].goodi;
8452 bi = stack[stackidx].badi;
8453 score = stack[stackidx].score;
8454 }
8455
8456 /* When the score goes over "limit" it may actually be much higher.
8457 * Return a very large number to avoid going below the limit when giving a
8458 * bonus. */
8459 if (minscore > limit)
8460 return SCORE_MAXMAX;
8461 return minscore;
8462}
8463#endif
8464
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008465/*
8466 * ":spellinfo"
8467 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008468 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008469ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008470{
8471 int lpi;
8472 langp_T *lp;
8473 char_u *p;
8474
8475 if (no_spell_checking(curwin))
8476 return;
8477
8478 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008479 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008480 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008481 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008482 msg_puts((char_u *)"file: ");
8483 msg_puts(lp->lp_slang->sl_fname);
8484 msg_putchar('\n');
8485 p = lp->lp_slang->sl_info;
8486 if (p != NULL)
8487 {
8488 msg_puts(p);
8489 msg_putchar('\n');
8490 }
8491 }
8492 msg_end();
8493}
8494
Bram Moolenaar4770d092006-01-12 23:22:24 +00008495#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8496#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008497#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008498#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8499#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008500
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008501/*
8502 * ":spelldump"
8503 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008505ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008506{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008507 char_u *spl;
8508 long dummy;
8509
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008510 if (no_spell_checking(curwin))
8511 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008512 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008513
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008514 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008515 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008516
8517 /* enable spelling locally in the new window */
8518 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008519 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008520 vim_free(spl);
8521
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008522 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008523 return;
8524
Bram Moolenaar860cae12010-06-05 23:22:07 +02008525 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008526
8527 /* Delete the empty line that we started with. */
8528 if (curbuf->b_ml.ml_line_count > 1)
8529 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8530
8531 redraw_later(NOT_VALID);
8532}
8533
8534/*
8535 * Go through all possible words and:
8536 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8537 * "ic" and "dir" are not used.
8538 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8539 */
8540 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008541spell_dump_compl(
8542 char_u *pat, /* leading part of the word */
8543 int ic, /* ignore case */
8544 int *dir, /* direction for adding matches */
8545 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008546{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008547 langp_T *lp;
8548 slang_T *slang;
8549 idx_T arridx[MAXWLEN];
8550 int curi[MAXWLEN];
8551 char_u word[MAXWLEN];
8552 int c;
8553 char_u *byts;
8554 idx_T *idxs;
8555 linenr_T lnum = 0;
8556 int round;
8557 int depth;
8558 int n;
8559 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008560 char_u *region_names = NULL; /* region names being used */
8561 int do_region = TRUE; /* dump region names and numbers */
8562 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008563 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008564 int dumpflags = dumpflags_arg;
8565 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008566
Bram Moolenaard0131a82006-03-04 21:46:13 +00008567 /* When ignoring case or when the pattern starts with capital pass this on
8568 * to dump_word(). */
8569 if (pat != NULL)
8570 {
8571 if (ic)
8572 dumpflags |= DUMPFLAG_ICASE;
8573 else
8574 {
8575 n = captype(pat, NULL);
8576 if (n == WF_ONECAP)
8577 dumpflags |= DUMPFLAG_ONECAP;
8578 else if (n == WF_ALLCAP
8579#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008580 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008581#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008582 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +00008583#endif
8584 )
8585 dumpflags |= DUMPFLAG_ALLCAP;
8586 }
8587 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008588
Bram Moolenaar7887d882005-07-01 22:33:52 +00008589 /* Find out if we can support regions: All languages must support the same
8590 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008591 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008592 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008593 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008594 p = lp->lp_slang->sl_regions;
8595 if (p[0] != 0)
8596 {
8597 if (region_names == NULL) /* first language with regions */
8598 region_names = p;
8599 else if (STRCMP(region_names, p) != 0)
8600 {
8601 do_region = FALSE; /* region names are different */
8602 break;
8603 }
8604 }
8605 }
8606
8607 if (do_region && region_names != NULL)
8608 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008609 if (pat == NULL)
8610 {
8611 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8612 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8613 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008614 }
8615 else
8616 do_region = FALSE;
8617
8618 /*
8619 * Loop over all files loaded for the entries in 'spelllang'.
8620 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008621 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008622 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008623 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008624 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008625 if (slang->sl_fbyts == NULL) /* reloading failed */
8626 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008627
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008628 if (pat == NULL)
8629 {
8630 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8631 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8632 }
8633
8634 /* When matching with a pattern and there are no prefixes only use
8635 * parts of the tree that match "pat". */
8636 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008637 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008638 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008639 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008640
8641 /* round 1: case-folded tree
8642 * round 2: keep-case tree */
8643 for (round = 1; round <= 2; ++round)
8644 {
8645 if (round == 1)
8646 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008647 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008648 byts = slang->sl_fbyts;
8649 idxs = slang->sl_fidxs;
8650 }
8651 else
8652 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008653 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008654 byts = slang->sl_kbyts;
8655 idxs = slang->sl_kidxs;
8656 }
8657 if (byts == NULL)
8658 continue; /* array is empty */
8659
8660 depth = 0;
8661 arridx[0] = 0;
8662 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008663 while (depth >= 0 && !got_int
8664 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008665 {
8666 if (curi[depth] > byts[arridx[depth]])
8667 {
8668 /* Done all bytes at this node, go up one level. */
8669 --depth;
8670 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008671 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008672 }
8673 else
8674 {
8675 /* Do one more byte at this node. */
8676 n = arridx[depth] + curi[depth];
8677 ++curi[depth];
8678 c = byts[n];
8679 if (c == 0)
8680 {
8681 /* End of word, deal with the word.
8682 * Don't use keep-case words in the fold-case tree,
8683 * they will appear in the keep-case tree.
8684 * Only use the word when the region matches. */
8685 flags = (int)idxs[n];
8686 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008687 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008688 && (do_region
8689 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008690 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008691 & lp->lp_region) != 0))
8692 {
8693 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008694 if (!do_region)
8695 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008696
8697 /* Dump the basic word if there is no prefix or
8698 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008699 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008700 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008701 {
8702 dump_word(slang, word, pat, dir,
8703 dumpflags, flags, lnum);
8704 if (pat == NULL)
8705 ++lnum;
8706 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008707
8708 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008709 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008710 lnum = dump_prefixes(slang, word, pat, dir,
8711 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008712 }
8713 }
8714 else
8715 {
8716 /* Normal char, go one level deeper. */
8717 word[depth++] = c;
8718 arridx[depth] = idxs[n];
8719 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008720
8721 /* Check if this characters matches with the pattern.
8722 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008723 * Always ignore case here, dump_word() will check
8724 * proper case later. This isn't exactly right when
8725 * length changes for multi-byte characters with
8726 * ignore case... */
8727 if (depth <= patlen
8728 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008729 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008730 }
8731 }
8732 }
8733 }
8734 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008735}
8736
8737/*
8738 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008739 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008740 */
8741 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008742dump_word(
8743 slang_T *slang,
8744 char_u *word,
8745 char_u *pat,
8746 int *dir,
8747 int dumpflags,
8748 int wordflags,
8749 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008750{
8751 int keepcap = FALSE;
8752 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008753 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008754 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008755 char_u badword[MAXWLEN + 10];
8756 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008757 int flags = wordflags;
8758
8759 if (dumpflags & DUMPFLAG_ONECAP)
8760 flags |= WF_ONECAP;
8761 if (dumpflags & DUMPFLAG_ALLCAP)
8762 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008763
Bram Moolenaar4770d092006-01-12 23:22:24 +00008764 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008765 {
8766 /* Need to fix case according to "flags". */
8767 make_case_word(word, cword, flags);
8768 p = cword;
8769 }
8770 else
8771 {
8772 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008773 if ((dumpflags & DUMPFLAG_KEEPCASE)
8774 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008775 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008776 keepcap = TRUE;
8777 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008778 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008779
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008780 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008781 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008782 /* Add flags and regions after a slash. */
8783 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008784 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008785 STRCPY(badword, p);
8786 STRCAT(badword, "/");
8787 if (keepcap)
8788 STRCAT(badword, "=");
8789 if (flags & WF_BANNED)
8790 STRCAT(badword, "!");
8791 else if (flags & WF_RARE)
8792 STRCAT(badword, "?");
8793 if (flags & WF_REGION)
8794 for (i = 0; i < 7; ++i)
8795 if (flags & (0x10000 << i))
8796 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8797 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008798 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008799
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008800 if (dumpflags & DUMPFLAG_COUNT)
8801 {
8802 hashitem_T *hi;
8803
8804 /* Include the word count for ":spelldump!". */
8805 hi = hash_find(&slang->sl_wordcount, tw);
8806 if (!HASHITEM_EMPTY(hi))
8807 {
8808 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8809 tw, HI2WC(hi)->wc_count);
8810 p = IObuff;
8811 }
8812 }
8813
8814 ml_append(lnum, p, (colnr_T)0, FALSE);
8815 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008816 else if (((dumpflags & DUMPFLAG_ICASE)
8817 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8818 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008819 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008820 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008821 /* if dir was BACKWARD then honor it just once */
8822 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008823}
8824
8825/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008826 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8827 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008828 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008829 * Return the updated line number.
8830 */
8831 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008832dump_prefixes(
8833 slang_T *slang,
8834 char_u *word, /* case-folded word */
8835 char_u *pat,
8836 int *dir,
8837 int dumpflags,
8838 int flags, /* flags with prefix ID */
8839 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008840{
8841 idx_T arridx[MAXWLEN];
8842 int curi[MAXWLEN];
8843 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008844 char_u word_up[MAXWLEN];
8845 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008846 int c;
8847 char_u *byts;
8848 idx_T *idxs;
8849 linenr_T lnum = startlnum;
8850 int depth;
8851 int n;
8852 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008853 int i;
8854
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008855 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008856 * upper-case letter in word_up[]. */
8857 c = PTR2CHAR(word);
8858 if (SPELL_TOUPPER(c) != c)
8859 {
8860 onecap_copy(word, word_up, TRUE);
8861 has_word_up = TRUE;
8862 }
8863
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008864 byts = slang->sl_pbyts;
8865 idxs = slang->sl_pidxs;
8866 if (byts != NULL) /* array not is empty */
8867 {
8868 /*
8869 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008870 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008871 */
8872 depth = 0;
8873 arridx[0] = 0;
8874 curi[0] = 1;
8875 while (depth >= 0 && !got_int)
8876 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008877 n = arridx[depth];
8878 len = byts[n];
8879 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008880 {
8881 /* Done all bytes at this node, go up one level. */
8882 --depth;
8883 line_breakcheck();
8884 }
8885 else
8886 {
8887 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008888 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008889 ++curi[depth];
8890 c = byts[n];
8891 if (c == 0)
8892 {
8893 /* End of prefix, find out how many IDs there are. */
8894 for (i = 1; i < len; ++i)
8895 if (byts[n + i] != 0)
8896 break;
8897 curi[depth] += i - 1;
8898
Bram Moolenaar53805d12005-08-01 07:08:33 +00008899 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8900 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008901 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008902 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008903 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008904 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008905 : flags, lnum);
8906 if (lnum != 0)
8907 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008908 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008909
8910 /* Check for prefix that matches the word when the
8911 * first letter is upper-case, but only if the prefix has
8912 * a condition. */
8913 if (has_word_up)
8914 {
8915 c = valid_word_prefix(i, n, flags, word_up, slang,
8916 TRUE);
8917 if (c != 0)
8918 {
8919 vim_strncpy(prefix + depth, word_up,
8920 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008921 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008922 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008923 : flags, lnum);
8924 if (lnum != 0)
8925 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008926 }
8927 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008928 }
8929 else
8930 {
8931 /* Normal char, go one level deeper. */
8932 prefix[depth++] = c;
8933 arridx[depth] = idxs[n];
8934 curi[depth] = 1;
8935 }
8936 }
8937 }
8938 }
8939
8940 return lnum;
8941}
8942
Bram Moolenaar95529562005-08-25 21:21:38 +00008943/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008944 * Move "p" to the end of word "start".
8945 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008946 */
8947 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008948spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008949{
8950 char_u *p = start;
8951
Bram Moolenaar860cae12010-06-05 23:22:07 +02008952 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008953 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008954 return p;
8955}
8956
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008957#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008958/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008959 * For Insert mode completion CTRL-X s:
8960 * Find start of the word in front of column "startcol".
8961 * We don't check if it is badly spelled, with completion we can only change
8962 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008963 * Returns the column number of the word.
8964 */
8965 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008966spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008967{
8968 char_u *line;
8969 char_u *p;
8970 int col = 0;
8971
Bram Moolenaar95529562005-08-25 21:21:38 +00008972 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008973 return startcol;
8974
8975 /* Find a word character before "startcol". */
8976 line = ml_get_curline();
8977 for (p = line + startcol; p > line; )
8978 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008979 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008980 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008981 break;
8982 }
8983
8984 /* Go back to start of the word. */
8985 while (p > line)
8986 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008987 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008988 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008989 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008990 break;
8991 col = 0;
8992 }
8993
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008994 return col;
8995}
8996
8997/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008998 * Need to check for 'spellcapcheck' now, the word is removed before
8999 * expand_spelling() is called. Therefore the ugly global variable.
9000 */
9001static int spell_expand_need_cap;
9002
9003 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009004spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00009005{
9006 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
9007}
9008
9009/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009010 * Get list of spelling suggestions.
9011 * Used for Insert mode completion CTRL-X ?.
9012 * Returns the number of matches. The matches are in "matchp[]", array of
9013 * allocated strings.
9014 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009015 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009016expand_spelling(
9017 linenr_T lnum UNUSED,
9018 char_u *pat,
9019 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009020{
9021 garray_T ga;
9022
Bram Moolenaar4770d092006-01-12 23:22:24 +00009023 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009024 *matchp = ga.ga_data;
9025 return ga.ga_len;
9026}
9027#endif
9028
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009029#endif /* FEAT_SPELL */