blob: 3c53d1d250ac3f08a90187d5e71b6f9131f25517 [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);
2407 len -= 3;
2408 region = region_cp;
2409 }
2410 else
2411 dont_use_region = TRUE;
2412
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002413 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002414 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2415 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002416 break;
2417 }
2418 else
2419 {
2420 filename = FALSE;
2421 if (len > 3 && lang[len - 3] == '_')
2422 {
2423 region = lang + len - 2;
2424 len -= 3;
2425 lang[len] = NUL;
2426 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002427 else
2428 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002429
2430 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002431 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2432 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002433 break;
2434 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002435
Bram Moolenaarb6356332005-07-18 21:40:44 +00002436 if (region != NULL)
2437 {
2438 /* If the region differs from what was used before then don't
2439 * use it for 'spellfile'. */
2440 if (use_region != NULL && STRCMP(region, use_region) != 0)
2441 dont_use_region = TRUE;
2442 use_region = region;
2443 }
2444
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002445 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002447 {
2448 if (filename)
2449 (void)spell_load_file(lang, lang, NULL, FALSE);
2450 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002451 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002452 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002453#ifdef FEAT_AUTOCMD
2454 /* SpellFileMissing autocommands may do anything, including
2455 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002456 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002457 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02002458 ret_msg = (char_u *)N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002459 goto theend;
2460 }
2461#endif
2462 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002464
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002465 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002466 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002467 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002468 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2469 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2470 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002471 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002472 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002473 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002474 {
2475 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002476 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002477 if (c == REGION_ALL)
2478 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002479 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002480 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002481 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002482 /* This addition file is for other regions. */
2483 region_mask = 0;
2484 }
2485 else
2486 /* This is probably an error. Give a warning and
2487 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002488 smsg((char_u *)
2489 _("Warning: region %s not supported"),
2490 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002491 }
2492 else
2493 region_mask = 1 << c;
2494 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002495
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002496 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002497 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002498 if (ga_grow(&ga, 1) == FAIL)
2499 {
2500 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002501 ret_msg = e_outofmem;
2502 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002503 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002504 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002505 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2506 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002507 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002508 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002509 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002512 }
2513
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002514 /* round 0: load int_wordlist, if possible.
2515 * round 1: load first name in 'spellfile'.
2516 * round 2: load second name in 'spellfile.
2517 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002518 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002519 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002520 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002521 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002522 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002523 /* Internal wordlist, if there is one. */
2524 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002525 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002526 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002527 }
2528 else
2529 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002530 /* One entry in 'spellfile'. */
2531 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2532 STRCAT(spf_name, ".spl");
2533
2534 /* If it was already found above then skip it. */
2535 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002536 {
2537 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2538 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002539 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002540 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002541 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002542 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002543 }
2544
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002545 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002546 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2547 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002549 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002550 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002551 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002552 * region name, the region is ignored otherwise. for int_wordlist
2553 * use an arbitrary name. */
2554 if (round == 0)
2555 STRCPY(lang, "internal wordlist");
2556 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002557 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002558 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002559 p = vim_strchr(lang, '.');
2560 if (p != NULL)
2561 *p = NUL; /* truncate at ".encoding.add" */
2562 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002563 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002564
2565 /* If one of the languages has NOBREAK we assume the addition
2566 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002567 if (slang != NULL && nobreak)
2568 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002569 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002570 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002571 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002572 region_mask = REGION_ALL;
2573 if (use_region != NULL && !dont_use_region)
2574 {
2575 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002576 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002577 if (c != REGION_ALL)
2578 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002579 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002580 /* This spell file is for other regions. */
2581 region_mask = 0;
2582 }
2583
2584 if (region_mask != 0)
2585 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002586 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2587 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2588 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002589 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2590 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002591 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002593 }
2594 }
2595
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002596 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002597 ga_clear(&wp->w_s->b_langp);
2598 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002599
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002600 /* For each language figure out what language to use for sound folding and
2601 * REP items. If the language doesn't support it itself use another one
2602 * with the same name. E.g. for "en-math" use "en". */
2603 for (i = 0; i < ga.ga_len; ++i)
2604 {
2605 lp = LANGP_ENTRY(ga, i);
2606
2607 /* sound folding */
2608 if (lp->lp_slang->sl_sal.ga_len > 0)
2609 /* language does sound folding itself */
2610 lp->lp_sallang = lp->lp_slang;
2611 else
2612 /* find first similar language that does sound folding */
2613 for (j = 0; j < ga.ga_len; ++j)
2614 {
2615 lp2 = LANGP_ENTRY(ga, j);
2616 if (lp2->lp_slang->sl_sal.ga_len > 0
2617 && STRNCMP(lp->lp_slang->sl_name,
2618 lp2->lp_slang->sl_name, 2) == 0)
2619 {
2620 lp->lp_sallang = lp2->lp_slang;
2621 break;
2622 }
2623 }
2624
2625 /* REP items */
2626 if (lp->lp_slang->sl_rep.ga_len > 0)
2627 /* language has REP items itself */
2628 lp->lp_replang = lp->lp_slang;
2629 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002630 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002631 for (j = 0; j < ga.ga_len; ++j)
2632 {
2633 lp2 = LANGP_ENTRY(ga, j);
2634 if (lp2->lp_slang->sl_rep.ga_len > 0
2635 && STRNCMP(lp->lp_slang->sl_name,
2636 lp2->lp_slang->sl_name, 2) == 0)
2637 {
2638 lp->lp_replang = lp2->lp_slang;
2639 break;
2640 }
2641 }
2642 }
2643
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002644theend:
2645 vim_free(spl_copy);
2646 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002647 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002648 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002649}
2650
2651/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002652 * Clear the midword characters for buffer "buf".
2653 */
2654 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002656{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002657 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002658#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01002659 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002660#endif
2661}
2662
2663/*
2664 * Use the "sl_midword" field of language "lp" for buffer "buf".
2665 * They add up to any currently used midword characters.
2666 */
2667 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002668use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002669{
2670 char_u *p;
2671
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002672 if (lp->sl_midword == NULL) /* there aren't any */
2673 return;
2674
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002675 for (p = lp->sl_midword; *p != NUL; )
2676#ifdef FEAT_MBYTE
2677 if (has_mbyte)
2678 {
2679 int c, l, n;
2680 char_u *bp;
2681
2682 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002683 l = (*mb_ptr2len)(p);
2684 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002685 wp->w_s->b_spell_ismw[c] = TRUE;
2686 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002687 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002688 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002689 else
2690 {
2691 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002692 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2693 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002694 if (bp != NULL)
2695 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002696 vim_free(wp->w_s->b_spell_ismw_mb);
2697 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002698 vim_strncpy(bp + n, p, l);
2699 }
2700 }
2701 p += l;
2702 }
2703 else
2704#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002705 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002706}
2707
2708/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002709 * Find the region "region[2]" in "rp" (points to "sl_regions").
2710 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002711 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002712 */
2713 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002714find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002715{
2716 int i;
2717
2718 for (i = 0; ; i += 2)
2719 {
2720 if (rp[i] == NUL)
2721 return REGION_ALL;
2722 if (rp[i] == region[0] && rp[i + 1] == region[1])
2723 break;
2724 }
2725 return i / 2;
2726}
2727
2728/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002729 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002730 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002731 * Word WF_ONECAP
2732 * W WORD WF_ALLCAP
2733 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002734 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002735 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002736captype(
2737 char_u *word,
2738 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002739{
2740 char_u *p;
2741 int c;
2742 int firstcap;
2743 int allcap;
2744 int past_second = FALSE; /* past second word char */
2745
2746 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002747 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002749 return 0; /* only non-word characters, illegal word */
2750#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002751 if (has_mbyte)
2752 c = mb_ptr2char_adv(&p);
2753 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002754#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002755 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002756 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002757
2758 /*
2759 * Need to check all letters to find a word with mixed upper/lower.
2760 * But a word with an upper char only at start is a ONECAP.
2761 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002762 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002763 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002764 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002765 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002766 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002767 {
2768 /* UUl -> KEEPCAP */
2769 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002770 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002771 allcap = FALSE;
2772 }
2773 else if (!allcap)
2774 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002775 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776 past_second = TRUE;
2777 }
2778
2779 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002780 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002781 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002782 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002783 return 0;
2784}
2785
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002786/*
2787 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2788 * capital. So that make_case_word() can turn WOrd into Word.
2789 * Add ALLCAP for "WOrD".
2790 */
2791 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002792badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002793{
2794 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002795 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002796 int l, u;
2797 int first;
2798 char_u *p;
2799
2800 if (flags & WF_KEEPCAP)
2801 {
2802 /* Count the number of UPPER and lower case letters. */
2803 l = u = 0;
2804 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002805 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002806 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002807 c = PTR2CHAR(p);
2808 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002809 {
2810 ++u;
2811 if (p == word)
2812 first = TRUE;
2813 }
2814 else
2815 ++l;
2816 }
2817
2818 /* If there are more UPPER than lower case letters suggest an
2819 * ALLCAP word. Otherwise, if the first letter is UPPER then
2820 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2821 * require three upper case letters. */
2822 if (u > l && u > 2)
2823 flags |= WF_ALLCAP;
2824 else if (first)
2825 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002826
2827 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2828 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002829 }
2830 return flags;
2831}
2832
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002833/*
2834 * Delete the internal wordlist and its .spl file.
2835 */
2836 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002837spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002838{
2839 char_u fname[MAXPATHL];
2840
2841 if (int_wordlist != NULL)
2842 {
2843 mch_remove(int_wordlist);
2844 int_wordlist_spl(fname);
2845 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002846 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002847 }
2848}
2849
2850#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002851/*
2852 * Free all languages.
2853 */
2854 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002855spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002856{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002857 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002858 buf_T *buf;
2859
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002860 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002861 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002862 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002863
2864 while (first_lang != NULL)
2865 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002866 slang = first_lang;
2867 first_lang = slang->sl_next;
2868 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002869 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002870
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002871 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002872
Bram Moolenaard23a8232018-02-10 18:45:26 +01002873 VIM_CLEAR(repl_to);
2874 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002875}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002876#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002877
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002878#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002879/*
2880 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002881 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002882 */
2883 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002884spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002885{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002886 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002887
Bram Moolenaarea408852005-06-25 22:49:46 +00002888 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002889 init_spell_chartab();
2890
2891 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002892 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002893
2894 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002895 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002896 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002897 /* Only load the wordlists when 'spelllang' is set and there is a
2898 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002899 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002900 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002901 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002902 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002903 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002904 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002905 }
2906 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002907 }
2908}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002909#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002910
Bram Moolenaarb765d632005-06-07 21:00:02 +00002911/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002912 * Opposite of offset2bytes().
2913 * "pp" points to the bytes and is advanced over it.
2914 * Returns the offset.
2915 */
2916 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002917bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002918{
2919 char_u *p = *pp;
2920 int nr;
2921 int c;
2922
2923 c = *p++;
2924 if ((c & 0x80) == 0x00) /* 1 byte */
2925 {
2926 nr = c - 1;
2927 }
2928 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2929 {
2930 nr = (c & 0x3f) - 1;
2931 nr = nr * 255 + (*p++ - 1);
2932 }
2933 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2934 {
2935 nr = (c & 0x1f) - 1;
2936 nr = nr * 255 + (*p++ - 1);
2937 nr = nr * 255 + (*p++ - 1);
2938 }
2939 else /* 4 bytes */
2940 {
2941 nr = (c & 0x0f) - 1;
2942 nr = nr * 255 + (*p++ - 1);
2943 nr = nr * 255 + (*p++ - 1);
2944 nr = nr * 255 + (*p++ - 1);
2945 }
2946
2947 *pp = p;
2948 return nr;
2949}
2950
Bram Moolenaar4770d092006-01-12 23:22:24 +00002951
2952/*
2953 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2954 * list and only contains text lines. Can use a swapfile to reduce memory
2955 * use.
2956 * Most other fields are invalid! Esp. watch out for string options being
2957 * NULL and there is no undo info.
2958 * Returns NULL when out of memory.
2959 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002960 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002961open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002962{
2963 buf_T *buf;
2964
2965 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2966 if (buf != NULL)
2967 {
2968 buf->b_spell = TRUE;
2969 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002970#ifdef FEAT_CRYPT
2971 buf->b_p_key = empty_option;
2972#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002973 ml_open(buf);
2974 ml_open_file(buf); /* create swap file now */
2975 }
2976 return buf;
2977}
2978
2979/*
2980 * Close the buffer used for spell info.
2981 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002982 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002983close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002984{
2985 if (buf != NULL)
2986 {
2987 ml_close(buf, TRUE);
2988 vim_free(buf);
2989 }
2990}
2991
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002992/*
2993 * Init the chartab used for spelling for ASCII.
2994 * EBCDIC is not supported!
2995 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002996 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002997clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002998{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002999 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003000
3001 /* Init everything to FALSE. */
3002 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
3003 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
3004 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003005 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003006 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003007 sp->st_upper[i] = i;
3008 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003009
3010 /* We include digits. A word shouldn't start with a digit, but handling
3011 * that is done separately. */
3012 for (i = '0'; i <= '9'; ++i)
3013 sp->st_isw[i] = TRUE;
3014 for (i = 'A'; i <= 'Z'; ++i)
3015 {
3016 sp->st_isw[i] = TRUE;
3017 sp->st_isu[i] = TRUE;
3018 sp->st_fold[i] = i + 0x20;
3019 }
3020 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003021 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003022 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003023 sp->st_upper[i] = i - 0x20;
3024 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003025}
3026
3027/*
3028 * Init the chartab used for spelling. Only depends on 'encoding'.
3029 * Called once while starting up and when 'encoding' changes.
3030 * The default is to use isalpha(), but the spell file should define the word
3031 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003032 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003033 */
3034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003035init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003036{
3037 int i;
3038
3039 did_set_spelltab = FALSE;
3040 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003041#ifdef FEAT_MBYTE
3042 if (enc_dbcs)
3043 {
3044 /* DBCS: assume double-wide characters are word characters. */
3045 for (i = 128; i <= 255; ++i)
3046 if (MB_BYTE2LEN(i) == 2)
3047 spelltab.st_isw[i] = TRUE;
3048 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003049 else if (enc_utf8)
3050 {
3051 for (i = 128; i < 256; ++i)
3052 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003053 int f = utf_fold(i);
3054 int u = utf_toupper(i);
3055
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003056 spelltab.st_isu[i] = utf_isupper(i);
3057 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003058 /* The folded/upper-cased value is different between latin1 and
3059 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
3060 * value for utf-8 to avoid this. */
3061 spelltab.st_fold[i] = (f < 256) ? f : i;
3062 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003063 }
3064 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003065 else
3066#endif
3067 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003068 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003069 for (i = 128; i < 256; ++i)
3070 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003071 if (MB_ISUPPER(i))
3072 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003073 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003074 spelltab.st_isu[i] = TRUE;
3075 spelltab.st_fold[i] = MB_TOLOWER(i);
3076 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003077 else if (MB_ISLOWER(i))
3078 {
3079 spelltab.st_isw[i] = TRUE;
3080 spelltab.st_upper[i] = MB_TOUPPER(i);
3081 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003082 }
3083 }
3084}
3085
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003086
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003087/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003088 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003089 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003090 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003091 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003092 */
3093 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003094spell_iswordp(
3095 char_u *p,
3096 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003097{
Bram Moolenaarea408852005-06-25 22:49:46 +00003098#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003099 char_u *s;
3100 int l;
3101 int c;
3102
3103 if (has_mbyte)
3104 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003105 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003106 s = p;
3107 if (l == 1)
3108 {
3109 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003110 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003111 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003112 }
3113 else
3114 {
3115 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003116 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3117 : (wp->w_s->b_spell_ismw_mb != NULL
3118 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003119 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003120 }
3121
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003122 c = mb_ptr2char(s);
3123 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003124 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003125 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003126 }
Bram Moolenaarea408852005-06-25 22:49:46 +00003127#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003128
Bram Moolenaar860cae12010-06-05 23:22:07 +02003129 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003130}
3131
3132/*
3133 * Return TRUE if "p" points to a word character.
3134 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3135 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003136 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003137spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003138{
3139#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003140 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003141
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003142 if (has_mbyte)
3143 {
3144 c = mb_ptr2char(p);
3145 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003146 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003147 return spelltab.st_isw[c];
3148 }
3149#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003150 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003151}
3152
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003153#ifdef FEAT_MBYTE
3154/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003155 * Return TRUE if word class indicates a word character.
3156 * Only for characters above 255.
3157 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003158 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003159 */
3160 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003161spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003162{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003163 if (wp->w_s->b_cjk)
3164 /* East Asian characters are not considered word characters. */
3165 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003166 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3167}
3168
3169/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003170 * Return TRUE if "p" points to a word character.
3171 * Wide version of spell_iswordp().
3172 */
3173 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003174spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003175{
3176 int *s;
3177
Bram Moolenaar860cae12010-06-05 23:22:07 +02003178 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3179 : (wp->w_s->b_spell_ismw_mb != NULL
3180 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003181 s = p + 1;
3182 else
3183 s = p;
3184
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003185 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003186 {
3187 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003188 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003189 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003190 return spell_mb_isword_class(
3191 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003192 return 0;
3193 }
3194 return spelltab.st_isw[*s];
3195}
3196#endif
3197
Bram Moolenaarea408852005-06-25 22:49:46 +00003198/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003199 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3200 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003201 * When using a multi-byte 'encoding' the length may change!
3202 * Returns FAIL when something wrong.
3203 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003204 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003205spell_casefold(
3206 char_u *str,
3207 int len,
3208 char_u *buf,
3209 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003210{
3211 int i;
3212
3213 if (len >= buflen)
3214 {
3215 buf[0] = NUL;
3216 return FAIL; /* result will not fit */
3217 }
3218
3219#ifdef FEAT_MBYTE
3220 if (has_mbyte)
3221 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003222 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003223 char_u *p;
3224 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003225
3226 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003227 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003228 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003229 if (outi + MB_MAXBYTES > buflen)
3230 {
3231 buf[outi] = NUL;
3232 return FAIL;
3233 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003234 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003235 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003236 }
3237 buf[outi] = NUL;
3238 }
3239 else
3240#endif
3241 {
3242 /* Be quick for non-multibyte encodings. */
3243 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003244 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003245 buf[i] = NUL;
3246 }
3247
3248 return OK;
3249}
3250
Bram Moolenaar4770d092006-01-12 23:22:24 +00003251/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003252#define SPS_BEST 1
3253#define SPS_FAST 2
3254#define SPS_DOUBLE 4
3255
Bram Moolenaar4770d092006-01-12 23:22:24 +00003256static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3257static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003258
3259/*
3260 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003261 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003262 */
3263 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003264spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003265{
3266 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003267 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003268 char_u buf[MAXPATHL];
3269 int f;
3270
3271 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003272 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003273
3274 for (p = p_sps; *p != NUL; )
3275 {
3276 copy_option_part(&p, buf, MAXPATHL, ",");
3277
3278 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003279 if (VIM_ISDIGIT(*buf))
3280 {
3281 s = buf;
3282 sps_limit = getdigits(&s);
3283 if (*s != NUL && !VIM_ISDIGIT(*s))
3284 f = -1;
3285 }
3286 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003287 f = SPS_BEST;
3288 else if (STRCMP(buf, "fast") == 0)
3289 f = SPS_FAST;
3290 else if (STRCMP(buf, "double") == 0)
3291 f = SPS_DOUBLE;
3292 else if (STRNCMP(buf, "expr:", 5) != 0
3293 && STRNCMP(buf, "file:", 5) != 0)
3294 f = -1;
3295
3296 if (f == -1 || (sps_flags != 0 && f != 0))
3297 {
3298 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003299 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003300 return FAIL;
3301 }
3302 if (f != 0)
3303 sps_flags = f;
3304 }
3305
3306 if (sps_flags == 0)
3307 sps_flags = SPS_BEST;
3308
3309 return OK;
3310}
3311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003312/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003313 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003314 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003315 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003316 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003317 */
3318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003320{
3321 char_u *line;
3322 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003323 char_u wcopy[MAXWLEN + 2];
3324 char_u *p;
3325 int i;
3326 int c;
3327 suginfo_T sug;
3328 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003329 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003330 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003331 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003332 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003333 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003334 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003335
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003336 if (no_spell_checking(curwin))
3337 return;
3338
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003339 if (VIsual_active)
3340 {
3341 /* Use the Visually selected text as the bad word. But reject
3342 * a multi-line selection. */
3343 if (curwin->w_cursor.lnum != VIsual.lnum)
3344 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003345 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003346 return;
3347 }
3348 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3349 if (badlen < 0)
3350 badlen = -badlen;
3351 else
3352 curwin->w_cursor.col = VIsual.col;
3353 ++badlen;
3354 end_visual_mode();
3355 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003356 /* Find the start of the badly spelled word. */
3357 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003358 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003360 /* No bad word or it starts after the cursor: use the word under the
3361 * cursor. */
3362 curwin->w_cursor = prev_cursor;
3363 line = ml_get_curline();
3364 p = line + curwin->w_cursor.col;
3365 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003366 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003367 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003368 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003369 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003370 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003371
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003372 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003373 {
3374 beep_flush();
3375 return;
3376 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003377 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 }
3379
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003380 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003381
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003382 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003383 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003384
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003385 /* Make a copy of current line since autocommands may free the line. */
3386 line = vim_strsave(ml_get_curline());
3387 if (line == NULL)
3388 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003389
Bram Moolenaar5195e452005-08-19 20:32:47 +00003390 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3391 * 'spellsuggest', whatever is smaller. */
3392 if (sps_limit > (int)Rows - 2)
3393 limit = (int)Rows - 2;
3394 else
3395 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003396 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003397 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003398
3399 if (sug.su_ga.ga_len == 0)
3400 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003401 else if (count > 0)
3402 {
3403 if (count > sug.su_ga.ga_len)
3404 smsg((char_u *)_("Sorry, only %ld suggestions"),
3405 (long)sug.su_ga.ga_len);
3406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 else
3408 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003409 VIM_CLEAR(repl_from);
3410 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003411
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003412#ifdef FEAT_RIGHTLEFT
3413 /* When 'rightleft' is set the list is drawn right-left. */
3414 cmdmsg_rl = curwin->w_p_rl;
3415 if (cmdmsg_rl)
3416 msg_col = Columns - 1;
3417#endif
3418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419 /* List the suggestions. */
3420 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003421 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003422 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003423 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3424 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003425#ifdef FEAT_RIGHTLEFT
3426 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3427 {
3428 /* And now the rabbit from the high hat: Avoid showing the
3429 * untranslated message rightleft. */
3430 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3431 sug.su_badlen, sug.su_badptr);
3432 }
3433#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003434 msg_puts(IObuff);
3435 msg_clr_eos();
3436 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003438 msg_scroll = TRUE;
3439 for (i = 0; i < sug.su_ga.ga_len; ++i)
3440 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003441 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003442
3443 /* The suggested word may replace only part of the bad word, add
3444 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003445 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003447 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003448 sug.su_badptr + stp->st_orglen,
3449 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003450 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3451#ifdef FEAT_RIGHTLEFT
3452 if (cmdmsg_rl)
3453 rl_mirror(IObuff);
3454#endif
3455 msg_puts(IObuff);
3456
3457 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003458 msg_puts(IObuff);
3459
3460 /* The word may replace more than "su_badlen". */
3461 if (sug.su_badlen < stp->st_orglen)
3462 {
3463 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3464 stp->st_orglen, sug.su_badptr);
3465 msg_puts(IObuff);
3466 }
3467
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003468 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003469 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003470 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003471 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003472 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003473 stp->st_salscore ? "s " : "",
3474 stp->st_score, stp->st_altscore);
3475 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003476 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003477 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003478#ifdef FEAT_RIGHTLEFT
3479 if (cmdmsg_rl)
3480 /* Mirror the numbers, but keep the leading space. */
3481 rl_mirror(IObuff + 1);
3482#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003483 msg_advance(30);
3484 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003486 msg_putchar('\n');
3487 }
3488
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003489#ifdef FEAT_RIGHTLEFT
3490 cmdmsg_rl = FALSE;
3491 msg_col = 0;
3492#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003493 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003494 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003495 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003496 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003497 lines_left = Rows; /* avoid more prompt */
3498 /* don't delay for 'smd' in normal_cmd() */
3499 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003500 }
3501
Bram Moolenaard12a1322005-08-21 22:08:24 +00003502 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3503 {
3504 /* Save the from and to text for :spellrepall. */
3505 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003506 if (sug.su_badlen > stp->st_orglen)
3507 {
3508 /* Replacing less than "su_badlen", append the remainder to
3509 * repl_to. */
3510 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3511 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3512 sug.su_badlen - stp->st_orglen,
3513 sug.su_badptr + stp->st_orglen);
3514 repl_to = vim_strsave(IObuff);
3515 }
3516 else
3517 {
3518 /* Replacing su_badlen or more, use the whole word. */
3519 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3520 repl_to = vim_strsave(stp->st_word);
3521 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003522
3523 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003524 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3525 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003526 if (p != NULL)
3527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003529 mch_memmove(p, line, c);
3530 STRCPY(p + c, stp->st_word);
3531 STRCAT(p, sug.su_badptr + stp->st_orglen);
3532 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3533 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003534
3535 /* For redo we use a change-word command. */
3536 ResetRedobuff();
3537 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003538 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003539 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003540 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003541
3542 /* After this "p" may be invalid. */
3543 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003544 }
3545 }
3546 else
3547 curwin->w_cursor = prev_cursor;
3548
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003549 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003550skip:
3551 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003552}
3553
3554/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003555 * Check if the word at line "lnum" column "col" is required to start with a
3556 * capital. This uses 'spellcapcheck' of the current buffer.
3557 */
3558 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003559check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003560{
3561 int need_cap = FALSE;
3562 char_u *line;
3563 char_u *line_copy = NULL;
3564 char_u *p;
3565 colnr_T endcol;
3566 regmatch_T regmatch;
3567
Bram Moolenaar860cae12010-06-05 23:22:07 +02003568 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003569 return FALSE;
3570
3571 line = ml_get_curline();
3572 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003573 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003574 {
3575 /* At start of line, check if previous line is empty or sentence
3576 * ends there. */
3577 if (lnum == 1)
3578 need_cap = TRUE;
3579 else
3580 {
3581 line = ml_get(lnum - 1);
3582 if (*skipwhite(line) == NUL)
3583 need_cap = TRUE;
3584 else
3585 {
3586 /* Append a space in place of the line break. */
3587 line_copy = concat_str(line, (char_u *)" ");
3588 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003589 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003590 }
3591 }
3592 }
3593 else
3594 endcol = col;
3595
3596 if (endcol > 0)
3597 {
3598 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003599 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003600 regmatch.rm_ic = FALSE;
3601 p = line + endcol;
3602 for (;;)
3603 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003604 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003605 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003606 break;
3607 if (vim_regexec(&regmatch, p, 0)
3608 && regmatch.endp[0] == line + endcol)
3609 {
3610 need_cap = TRUE;
3611 break;
3612 }
3613 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003614 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003615 }
3616
3617 vim_free(line_copy);
3618
3619 return need_cap;
3620}
3621
3622
3623/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003624 * ":spellrepall"
3625 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003626 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003627ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003628{
3629 pos_T pos = curwin->w_cursor;
3630 char_u *frompat;
3631 int addlen;
3632 char_u *line;
3633 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003634 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003635 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003636
3637 if (repl_from == NULL || repl_to == NULL)
3638 {
3639 EMSG(_("E752: No previous spell replacement"));
3640 return;
3641 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003642 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003643
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003644 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003645 if (frompat == NULL)
3646 return;
3647 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3648 p_ws = FALSE;
3649
Bram Moolenaar5195e452005-08-19 20:32:47 +00003650 sub_nsubs = 0;
3651 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003652 curwin->w_cursor.lnum = 0;
3653 while (!got_int)
3654 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003655 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003656 || u_save_cursor() == FAIL)
3657 break;
3658
3659 /* Only replace when the right word isn't there yet. This happens
3660 * when changing "etc" to "etc.". */
3661 line = ml_get_curline();
3662 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3663 repl_to, STRLEN(repl_to)) != 0)
3664 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003665 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003666 if (p == NULL)
3667 break;
3668 mch_memmove(p, line, curwin->w_cursor.col);
3669 STRCPY(p + curwin->w_cursor.col, repl_to);
3670 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3671 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3672 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003673
3674 if (curwin->w_cursor.lnum != prev_lnum)
3675 {
3676 ++sub_nlines;
3677 prev_lnum = curwin->w_cursor.lnum;
3678 }
3679 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003680 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003681 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003682 }
3683
3684 p_ws = save_ws;
3685 curwin->w_cursor = pos;
3686 vim_free(frompat);
3687
Bram Moolenaar5195e452005-08-19 20:32:47 +00003688 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003689 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003690 else
3691 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003692}
3693
3694/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003695 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3696 * a list of allocated strings.
3697 */
3698 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003699spell_suggest_list(
3700 garray_T *gap,
3701 char_u *word,
3702 int maxcount, /* maximum nr of suggestions */
3703 int need_cap, /* 'spellcapcheck' matched */
3704 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003705{
3706 suginfo_T sug;
3707 int i;
3708 suggest_T *stp;
3709 char_u *wcopy;
3710
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003711 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003712
3713 /* Make room in "gap". */
3714 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003715 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003716 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003717 for (i = 0; i < sug.su_ga.ga_len; ++i)
3718 {
3719 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003720
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003721 /* The suggested word may replace only part of "word", add the not
3722 * replaced part. */
3723 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003724 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003725 if (wcopy == NULL)
3726 break;
3727 STRCPY(wcopy, stp->st_word);
3728 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3729 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3730 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003731 }
3732
3733 spell_find_cleanup(&sug);
3734}
3735
3736/*
3737 * Find spell suggestions for the word at the start of "badptr".
3738 * Return the suggestions in "su->su_ga".
3739 * The maximum number of suggestions is "maxcount".
3740 * Note: does use info for the current window.
3741 * This is based on the mechanisms of Aspell, but completely reimplemented.
3742 */
3743 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003744spell_find_suggest(
3745 char_u *badptr,
3746 int badlen, /* length of bad word or 0 if unknown */
3747 suginfo_T *su,
3748 int maxcount,
3749 int banbadword, /* don't include badword in suggestions */
3750 int need_cap, /* word should start with capital */
3751 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003753 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003754 char_u buf[MAXPATHL];
3755 char_u *p;
3756 int do_combine = FALSE;
3757 char_u *sps_copy;
3758#ifdef FEAT_EVAL
3759 static int expr_busy = FALSE;
3760#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003761 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003762 int i;
3763 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003764
3765 /*
3766 * Set the info in "*su".
3767 */
3768 vim_memset(su, 0, sizeof(suginfo_T));
3769 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3770 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003771 if (*badptr == NUL)
3772 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003773 hash_init(&su->su_banned);
3774
3775 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003776 if (badlen != 0)
3777 su->su_badlen = badlen;
3778 else
3779 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003780 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003781 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003782
3783 if (su->su_badlen >= MAXWLEN)
3784 su->su_badlen = MAXWLEN - 1; /* just in case */
3785 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3786 (void)spell_casefold(su->su_badptr, su->su_badlen,
3787 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003788 /* TODO: make this work if the case-folded text is longer than the original
3789 * text. Currently an illegal byte causes wrong pointer computations. */
3790 su->su_fbadword[su->su_badlen] = NUL;
3791
Bram Moolenaar0c405862005-06-22 22:26:26 +00003792 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003793 su->su_badflags = badword_captype(su->su_badptr,
3794 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003795 if (need_cap)
3796 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003797
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003798 /* Find the default language for sound folding. We simply use the first
3799 * one in 'spelllang' that supports sound folding. That's good for when
3800 * using multiple files for one language, it's not that bad when mixing
3801 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003802 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003803 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003804 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003805 if (lp->lp_sallang != NULL)
3806 {
3807 su->su_sallang = lp->lp_sallang;
3808 break;
3809 }
3810 }
3811
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003812 /* Soundfold the bad word with the default sound folding, so that we don't
3813 * have to do this many times. */
3814 if (su->su_sallang != NULL)
3815 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3816 su->su_sal_badword);
3817
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003818 /* If the word is not capitalised and spell_check() doesn't consider the
3819 * word to be bad then it might need to be capitalised. Add a suggestion
3820 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003821 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003822 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003823 {
3824 make_case_word(su->su_badword, buf, WF_ONECAP);
3825 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003826 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003827 }
3828
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003829 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003830 if (banbadword)
3831 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003832
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003833 /* Make a copy of 'spellsuggest', because the expression may change it. */
3834 sps_copy = vim_strsave(p_sps);
3835 if (sps_copy == NULL)
3836 return;
3837
3838 /* Loop over the items in 'spellsuggest'. */
3839 for (p = sps_copy; *p != NUL; )
3840 {
3841 copy_option_part(&p, buf, MAXPATHL, ",");
3842
3843 if (STRNCMP(buf, "expr:", 5) == 0)
3844 {
3845#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003846 /* Evaluate an expression. Skip this when called recursively,
3847 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003848 if (!expr_busy)
3849 {
3850 expr_busy = TRUE;
3851 spell_suggest_expr(su, buf + 5);
3852 expr_busy = FALSE;
3853 }
3854#endif
3855 }
3856 else if (STRNCMP(buf, "file:", 5) == 0)
3857 /* Use list of suggestions in a file. */
3858 spell_suggest_file(su, buf + 5);
3859 else
3860 {
3861 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003862 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003863 if (sps_flags & SPS_DOUBLE)
3864 do_combine = TRUE;
3865 }
3866 }
3867
3868 vim_free(sps_copy);
3869
3870 if (do_combine)
3871 /* Combine the two list of suggestions. This must be done last,
3872 * because sorting changes the order again. */
3873 score_combine(su);
3874}
3875
3876#ifdef FEAT_EVAL
3877/*
3878 * Find suggestions by evaluating expression "expr".
3879 */
3880 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003881spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003882{
3883 list_T *list;
3884 listitem_T *li;
3885 int score;
3886 char_u *p;
3887
3888 /* The work is split up in a few parts to avoid having to export
3889 * suginfo_T.
3890 * First evaluate the expression and get the resulting list. */
3891 list = eval_spell_expr(su->su_badword, expr);
3892 if (list != NULL)
3893 {
3894 /* Loop over the items in the list. */
3895 for (li = list->lv_first; li != NULL; li = li->li_next)
3896 if (li->li_tv.v_type == VAR_LIST)
3897 {
3898 /* Get the word and the score from the items. */
3899 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003900 if (score >= 0 && score <= su->su_maxscore)
3901 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3902 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003903 }
3904 list_unref(list);
3905 }
3906
Bram Moolenaar4770d092006-01-12 23:22:24 +00003907 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3908 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003909 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3910}
3911#endif
3912
3913/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003914 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003915 */
3916 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003917spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003918{
3919 FILE *fd;
3920 char_u line[MAXWLEN * 2];
3921 char_u *p;
3922 int len;
3923 char_u cword[MAXWLEN];
3924
3925 /* Open the file. */
3926 fd = mch_fopen((char *)fname, "r");
3927 if (fd == NULL)
3928 {
3929 EMSG2(_(e_notopen), fname);
3930 return;
3931 }
3932
3933 /* Read it line by line. */
3934 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3935 {
3936 line_breakcheck();
3937
3938 p = vim_strchr(line, '/');
3939 if (p == NULL)
3940 continue; /* No Tab found, just skip the line. */
3941 *p++ = NUL;
3942 if (STRICMP(su->su_badword, line) == 0)
3943 {
3944 /* Match! Isolate the good word, until CR or NL. */
3945 for (len = 0; p[len] >= ' '; ++len)
3946 ;
3947 p[len] = NUL;
3948
3949 /* If the suggestion doesn't have specific case duplicate the case
3950 * of the bad word. */
3951 if (captype(p, NULL) == 0)
3952 {
3953 make_case_word(p, cword, su->su_badflags);
3954 p = cword;
3955 }
3956
3957 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003958 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003959 }
3960 }
3961
3962 fclose(fd);
3963
Bram Moolenaar4770d092006-01-12 23:22:24 +00003964 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3965 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003966 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3967}
3968
3969/*
3970 * Find suggestions for the internal method indicated by "sps_flags".
3971 */
3972 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003973spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003974{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003975 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003976 * Load the .sug file(s) that are available and not done yet.
3977 */
3978 suggest_load_files();
3979
3980 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003981 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003982 *
3983 * Set a maximum score to limit the combination of operations that is
3984 * tried.
3985 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003986 suggest_try_special(su);
3987
3988 /*
3989 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3990 * from the .aff file and inserting a space (split the word).
3991 */
3992 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003993
3994 /* For the resulting top-scorers compute the sound-a-like score. */
3995 if (sps_flags & SPS_DOUBLE)
3996 score_comp_sal(su);
3997
3998 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003999 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004000 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004001 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004002 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004003 if (sps_flags & SPS_BEST)
4004 /* Adjust the word score for the suggestions found so far for how
4005 * they sounds like. */
4006 rescore_suggestions(su);
4007
4008 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004009 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00004010 * for the soundfold word, limits the changes that are being tried,
4011 * and "su_sfmaxscore" the rescored score, which is set by
4012 * cleanup_suggestions().
4013 * First find words with a small edit distance, because this is much
4014 * faster and often already finds the top-N suggestions. If we didn't
4015 * find many suggestions try again with a higher edit distance.
4016 * "sl_sounddone" is used to avoid doing the same word twice.
4017 */
4018 suggest_try_soundalike_prep();
4019 su->su_maxscore = SCORE_SFMAX1;
4020 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004021 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004022 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4023 {
4024 /* We didn't find enough matches, try again, allowing more
4025 * changes to the soundfold word. */
4026 su->su_maxscore = SCORE_SFMAX2;
4027 suggest_try_soundalike(su);
4028 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4029 {
4030 /* Still didn't find enough matches, try again, allowing even
4031 * more changes to the soundfold word. */
4032 su->su_maxscore = SCORE_SFMAX3;
4033 suggest_try_soundalike(su);
4034 }
4035 }
4036 su->su_maxscore = su->su_sfmaxscore;
4037 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004038 }
4039
Bram Moolenaar4770d092006-01-12 23:22:24 +00004040 /* When CTRL-C was hit while searching do show the results. Only clear
4041 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004042 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00004043 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004044 {
4045 (void)vgetc();
4046 got_int = FALSE;
4047 }
4048
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004049 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004050 {
4051 if (sps_flags & SPS_BEST)
4052 /* Adjust the word score for how it sounds like. */
4053 rescore_suggestions(su);
4054
Bram Moolenaar4770d092006-01-12 23:22:24 +00004055 /* Remove bogus suggestions, sort and truncate at "maxcount". */
4056 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004057 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004058 }
4059}
4060
4061/*
4062 * Free the info put in "*su" by spell_find_suggest().
4063 */
4064 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004065spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004066{
4067 int i;
4068
4069 /* Free the suggestions. */
4070 for (i = 0; i < su->su_ga.ga_len; ++i)
4071 vim_free(SUG(su->su_ga, i).st_word);
4072 ga_clear(&su->su_ga);
4073 for (i = 0; i < su->su_sga.ga_len; ++i)
4074 vim_free(SUG(su->su_sga, i).st_word);
4075 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004076
4077 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004078 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004079}
4080
4081/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004082 * Make a copy of "word", with the first letter upper or lower cased, to
4083 * "wcopy[MAXWLEN]". "word" must not be empty.
4084 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004086 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004087onecap_copy(
4088 char_u *word,
4089 char_u *wcopy,
4090 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004091{
4092 char_u *p;
4093 int c;
4094 int l;
4095
4096 p = word;
4097#ifdef FEAT_MBYTE
4098 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004099 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 else
4101#endif
4102 c = *p++;
4103 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004104 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004106 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004107#ifdef FEAT_MBYTE
4108 if (has_mbyte)
4109 l = mb_char2bytes(c, wcopy);
4110 else
4111#endif
4112 {
4113 l = 1;
4114 wcopy[0] = c;
4115 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004116 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117}
4118
4119/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004120 * Make a copy of "word" with all the letters upper cased into
4121 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004122 */
4123 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004124allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004125{
4126 char_u *s;
4127 char_u *d;
4128 int c;
4129
4130 d = wcopy;
4131 for (s = word; *s != NUL; )
4132 {
4133#ifdef FEAT_MBYTE
4134 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004135 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004136 else
4137#endif
4138 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004139
4140#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +02004141 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004142 * would cause weird errors in other 8-bit encodings. */
4143 if (enc_latin1like && c == 0xdf)
4144 {
4145 c = 'S';
4146 if (d - wcopy >= MAXWLEN - 1)
4147 break;
4148 *d++ = c;
4149 }
4150 else
4151#endif
4152 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153
4154#ifdef FEAT_MBYTE
4155 if (has_mbyte)
4156 {
4157 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4158 break;
4159 d += mb_char2bytes(c, d);
4160 }
4161 else
4162#endif
4163 {
4164 if (d - wcopy >= MAXWLEN - 1)
4165 break;
4166 *d++ = c;
4167 }
4168 }
4169 *d = NUL;
4170}
4171
4172/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004173 * Try finding suggestions by recognizing specific situations.
4174 */
4175 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004176suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004177{
4178 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004179 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004180 int c;
4181 char_u word[MAXWLEN];
4182
4183 /*
4184 * Recognize a word that is repeated: "the the".
4185 */
4186 p = skiptowhite(su->su_fbadword);
4187 len = p - su->su_fbadword;
4188 p = skipwhite(p);
4189 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4190 {
4191 /* Include badflags: if the badword is onecap or allcap
4192 * use that for the goodword too: "The the" -> "The". */
4193 c = su->su_fbadword[len];
4194 su->su_fbadword[len] = NUL;
4195 make_case_word(su->su_fbadword, word, su->su_badflags);
4196 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004197
4198 /* Give a soundalike score of 0, compute the score as if deleting one
4199 * character. */
4200 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004201 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004202 }
4203}
4204
4205/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004206 * Change the 0 to 1 to measure how much time is spent in each state.
4207 * Output is dumped in "suggestprof".
4208 */
4209#if 0
4210# define SUGGEST_PROFILE
4211proftime_T current;
4212proftime_T total;
4213proftime_T times[STATE_FINAL + 1];
4214long counts[STATE_FINAL + 1];
4215
4216 static void
4217prof_init(void)
4218{
4219 for (int i = 0; i <= STATE_FINAL; ++i)
4220 {
4221 profile_zero(&times[i]);
4222 counts[i] = 0;
4223 }
4224 profile_start(&current);
4225 profile_start(&total);
4226}
4227
4228/* call before changing state */
4229 static void
4230prof_store(state_T state)
4231{
4232 profile_end(&current);
4233 profile_add(&times[state], &current);
4234 ++counts[state];
4235 profile_start(&current);
4236}
4237# define PROF_STORE(state) prof_store(state);
4238
4239 static void
4240prof_report(char *name)
4241{
4242 FILE *fd = fopen("suggestprof", "a");
4243
4244 profile_end(&total);
4245 fprintf(fd, "-----------------------\n");
4246 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4247 for (int i = 0; i <= STATE_FINAL; ++i)
4248 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4249 fclose(fd);
4250}
4251#else
4252# define PROF_STORE(state)
4253#endif
4254
4255/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 * Try finding suggestions by adding/removing/swapping letters.
4257 */
4258 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004259suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260{
4261 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004262 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004264 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004265 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266
4267 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004268 * to find matches (esp. REP items). Append some more text, changing
4269 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004271 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004272 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004273 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274
Bram Moolenaar860cae12010-06-05 23:22:07 +02004275 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004277 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004278
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004279 /* If reloading a spell file fails it's still in the list but
4280 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004281 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004282 continue;
4283
Bram Moolenaar4770d092006-01-12 23:22:24 +00004284 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004285#ifdef SUGGEST_PROFILE
4286 prof_init();
4287#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004288 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004289#ifdef SUGGEST_PROFILE
4290 prof_report("try_change");
4291#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004292 }
4293}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294
Bram Moolenaar4770d092006-01-12 23:22:24 +00004295/* Check the maximum score, if we go over it we won't try this change. */
4296#define TRY_DEEPER(su, stack, depth, add) \
4297 (stack[depth].ts_score + (add) < su->su_maxscore)
4298
4299/*
4300 * Try finding suggestions by adding/removing/swapping letters.
4301 *
4302 * This uses a state machine. At each node in the tree we try various
4303 * operations. When trying if an operation works "depth" is increased and the
4304 * stack[] is used to store info. This allows combinations, thus insert one
4305 * character, replace one and delete another. The number of changes is
4306 * limited by su->su_maxscore.
4307 *
4308 * After implementing this I noticed an article by Kemal Oflazer that
4309 * describes something similar: "Error-tolerant Finite State Recognition with
4310 * Applications to Morphological Analysis and Spelling Correction" (1996).
4311 * The implementation in the article is simplified and requires a stack of
4312 * unknown depth. The implementation here only needs a stack depth equal to
4313 * the length of the word.
4314 *
4315 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4316 * The mechanism is the same, but we find a match with a sound-folded word
4317 * that comes from one or more original words. Each of these words may be
4318 * added, this is done by add_sound_suggest().
4319 * Don't use:
4320 * the prefix tree or the keep-case tree
4321 * "su->su_badlen"
4322 * anything to do with upper and lower case
4323 * anything to do with word or non-word characters ("spell_iswordp()")
4324 * banned words
4325 * word flags (rare, region, compounding)
4326 * word splitting for now
4327 * "similar_chars()"
4328 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4329 */
4330 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004331suggest_trie_walk(
4332 suginfo_T *su,
4333 langp_T *lp,
4334 char_u *fword,
4335 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004336{
4337 char_u tword[MAXWLEN]; /* good word collected so far */
4338 trystate_T stack[MAXWLEN];
4339 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004340 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004341 * words and split word. NUL terminated
4342 * when going deeper but not when coming
4343 * back. */
4344 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4345 trystate_T *sp;
4346 int newscore;
4347 int score;
4348 char_u *byts, *fbyts, *pbyts;
4349 idx_T *idxs, *fidxs, *pidxs;
4350 int depth;
4351 int c, c2, c3;
4352 int n = 0;
4353 int flags;
4354 garray_T *gap;
4355 idx_T arridx;
4356 int len;
4357 char_u *p;
4358 fromto_T *ftp;
4359 int fl = 0, tl;
4360 int repextra = 0; /* extra bytes in fword[] from REP item */
4361 slang_T *slang = lp->lp_slang;
4362 int fword_ends;
4363 int goodword_ends;
4364#ifdef DEBUG_TRIEWALK
4365 /* Stores the name of the change made at each level. */
4366 char_u changename[MAXWLEN][80];
4367#endif
4368 int breakcheckcount = 1000;
4369 int compound_ok;
4370
4371 /*
4372 * Go through the whole case-fold tree, try changes at each node.
4373 * "tword[]" contains the word collected from nodes in the tree.
4374 * "fword[]" the word we are trying to match with (initially the bad
4375 * word).
4376 */
4377 depth = 0;
4378 sp = &stack[0];
4379 vim_memset(sp, 0, sizeof(trystate_T));
4380 sp->ts_curi = 1;
4381
4382 if (soundfold)
4383 {
4384 /* Going through the soundfold tree. */
4385 byts = fbyts = slang->sl_sbyts;
4386 idxs = fidxs = slang->sl_sidxs;
4387 pbyts = NULL;
4388 pidxs = NULL;
4389 sp->ts_prefixdepth = PFD_NOPREFIX;
4390 sp->ts_state = STATE_START;
4391 }
4392 else
4393 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004394 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004395 * When there are postponed prefixes we need to use these first. At
4396 * the end of the prefix we continue in the case-fold tree.
4397 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004398 fbyts = slang->sl_fbyts;
4399 fidxs = slang->sl_fidxs;
4400 pbyts = slang->sl_pbyts;
4401 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004402 if (pbyts != NULL)
4403 {
4404 byts = pbyts;
4405 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004406 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004407 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4408 }
4409 else
4410 {
4411 byts = fbyts;
4412 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004413 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004414 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004415 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004416 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004417
Bram Moolenaar4770d092006-01-12 23:22:24 +00004418 /*
4419 * Loop to find all suggestions. At each round we either:
4420 * - For the current state try one operation, advance "ts_curi",
4421 * increase "depth".
4422 * - When a state is done go to the next, set "ts_state".
4423 * - When all states are tried decrease "depth".
4424 */
4425 while (depth >= 0 && !got_int)
4426 {
4427 sp = &stack[depth];
4428 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004430 case STATE_START:
4431 case STATE_NOPREFIX:
4432 /*
4433 * Start of node: Deal with NUL bytes, which means
4434 * tword[] may end here.
4435 */
4436 arridx = sp->ts_arridx; /* current node in the tree */
4437 len = byts[arridx]; /* bytes in this node */
4438 arridx += sp->ts_curi; /* index of current byte */
4439
4440 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004442 /* Skip over the NUL bytes, we use them later. */
4443 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4444 ;
4445 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446
Bram Moolenaar4770d092006-01-12 23:22:24 +00004447 /* Always past NUL bytes now. */
4448 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004449 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004450 sp->ts_state = STATE_ENDNUL;
4451 sp->ts_save_badflags = su->su_badflags;
4452
4453 /* At end of a prefix or at start of prefixtree: check for
4454 * following word. */
4455 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004456 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004457 /* Set su->su_badflags to the caps type at this position.
4458 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004459#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004460 if (has_mbyte)
4461 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4462 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004463#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004464 n = sp->ts_fidx;
4465 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4466 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004467 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004468#ifdef DEBUG_TRIEWALK
4469 sprintf(changename[depth], "prefix");
4470#endif
4471 go_deeper(stack, depth, 0);
4472 ++depth;
4473 sp = &stack[depth];
4474 sp->ts_prefixdepth = depth - 1;
4475 byts = fbyts;
4476 idxs = fidxs;
4477 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004478
Bram Moolenaar4770d092006-01-12 23:22:24 +00004479 /* Move the prefix to preword[] with the right case
4480 * and make find_keepcap_word() works. */
4481 tword[sp->ts_twordlen] = NUL;
4482 make_case_word(tword + sp->ts_splitoff,
4483 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004484 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004485 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004486 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004487 break;
4488 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004489
Bram Moolenaar4770d092006-01-12 23:22:24 +00004490 if (sp->ts_curi > len || byts[arridx] != 0)
4491 {
4492 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004493 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004494 sp->ts_state = STATE_ENDNUL;
4495 sp->ts_save_badflags = su->su_badflags;
4496 break;
4497 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004498
Bram Moolenaar4770d092006-01-12 23:22:24 +00004499 /*
4500 * End of word in tree.
4501 */
4502 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004503
Bram Moolenaar4770d092006-01-12 23:22:24 +00004504 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004505
4506 /* Skip words with the NOSUGGEST flag. */
4507 if (flags & WF_NOSUGGEST)
4508 break;
4509
Bram Moolenaar4770d092006-01-12 23:22:24 +00004510 fword_ends = (fword[sp->ts_fidx] == NUL
4511 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004512 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004513 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004514 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004515
Bram Moolenaar4770d092006-01-12 23:22:24 +00004516 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004517 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004518 {
4519 /* There was a prefix before the word. Check that the prefix
4520 * can be used with this word. */
4521 /* Count the length of the NULs in the prefix. If there are
4522 * none this must be the first try without a prefix. */
4523 n = stack[sp->ts_prefixdepth].ts_arridx;
4524 len = pbyts[n++];
4525 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4526 ;
4527 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004528 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004529 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004530 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004531 if (c == 0)
4532 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004533
Bram Moolenaar4770d092006-01-12 23:22:24 +00004534 /* Use the WF_RARE flag for a rare prefix. */
4535 if (c & WF_RAREPFX)
4536 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004537
Bram Moolenaar4770d092006-01-12 23:22:24 +00004538 /* Tricky: when checking for both prefix and compounding
4539 * we run into the prefix flag first.
4540 * Remember that it's OK, so that we accept the prefix
4541 * when arriving at a compound flag. */
4542 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004543 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004544 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004545
Bram Moolenaar4770d092006-01-12 23:22:24 +00004546 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4547 * appending another compound word below. */
4548 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004549 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004550 goodword_ends = FALSE;
4551 else
4552 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004553
Bram Moolenaar4770d092006-01-12 23:22:24 +00004554 p = NULL;
4555 compound_ok = TRUE;
4556 if (sp->ts_complen > sp->ts_compsplit)
4557 {
4558 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004559 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004560 /* There was a word before this word. When there was no
4561 * change in this word (it was correct) add the first word
4562 * as a suggestion. If this word was corrected too, we
4563 * need to check if a correct word follows. */
4564 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004565 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004566 && STRNCMP(fword + sp->ts_splitfidx,
4567 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004568 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004569 {
4570 preword[sp->ts_prewordlen] = NUL;
4571 newscore = score_wordcount_adj(slang, sp->ts_score,
4572 preword + sp->ts_prewordlen,
4573 sp->ts_prewordlen > 0);
4574 /* Add the suggestion if the score isn't too bad. */
4575 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004576 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004577 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004578 newscore, 0, FALSE,
4579 lp->lp_sallang, FALSE);
4580 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004581 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004582 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004583 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004584 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004585 /* There was a compound word before this word. If this
4586 * word does not support compounding then give up
4587 * (splitting is tried for the word without compound
4588 * flag). */
4589 if (((unsigned)flags >> 24) == 0
4590 || sp->ts_twordlen - sp->ts_splitoff
4591 < slang->sl_compminlen)
4592 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004593#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004594 /* For multi-byte chars check character length against
4595 * COMPOUNDMIN. */
4596 if (has_mbyte
4597 && slang->sl_compminlen > 0
4598 && mb_charlen(tword + sp->ts_splitoff)
4599 < slang->sl_compminlen)
4600 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004601#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00004602
Bram Moolenaar4770d092006-01-12 23:22:24 +00004603 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4604 compflags[sp->ts_complen + 1] = NUL;
4605 vim_strncpy(preword + sp->ts_prewordlen,
4606 tword + sp->ts_splitoff,
4607 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004608
4609 /* Verify CHECKCOMPOUNDPATTERN rules. */
4610 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4611 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004612 compound_ok = FALSE;
4613
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004614 if (compound_ok)
4615 {
4616 p = preword;
4617 while (*skiptowhite(p) != NUL)
4618 p = skipwhite(skiptowhite(p));
4619 if (fword_ends && !can_compound(slang, p,
4620 compflags + sp->ts_compsplit))
4621 /* Compound is not allowed. But it may still be
4622 * possible if we add another (short) word. */
4623 compound_ok = FALSE;
4624 }
4625
Bram Moolenaar4770d092006-01-12 23:22:24 +00004626 /* Get pointer to last char of previous word. */
4627 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004628 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004629 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631
Bram Moolenaar4770d092006-01-12 23:22:24 +00004632 /*
4633 * Form the word with proper case in preword.
4634 * If there is a word from a previous split, append.
4635 * For the soundfold tree don't change the case, simply append.
4636 */
4637 if (soundfold)
4638 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4639 else if (flags & WF_KEEPCAP)
4640 /* Must find the word in the keep-case tree. */
4641 find_keepcap_word(slang, tword + sp->ts_splitoff,
4642 preword + sp->ts_prewordlen);
4643 else
4644 {
4645 /* Include badflags: If the badword is onecap or allcap
4646 * use that for the goodword too. But if the badword is
4647 * allcap and it's only one char long use onecap. */
4648 c = su->su_badflags;
4649 if ((c & WF_ALLCAP)
4650#ifdef FEAT_MBYTE
4651 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
4652#else
4653 && su->su_badlen == 1
4654#endif
4655 )
4656 c = WF_ONECAP;
4657 c |= flags;
4658
4659 /* When appending a compound word after a word character don't
4660 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004661 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004662 c &= ~WF_ONECAP;
4663 make_case_word(tword + sp->ts_splitoff,
4664 preword + sp->ts_prewordlen, c);
4665 }
4666
4667 if (!soundfold)
4668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 /* Don't use a banned word. It may appear again as a good
4670 * word, thus remember it. */
4671 if (flags & WF_BANNED)
4672 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004673 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 break;
4675 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004676 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004677 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4678 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004679 {
4680 if (slang->sl_compprog == NULL)
4681 break;
4682 /* the word so far was banned but we may try compounding */
4683 goodword_ends = FALSE;
4684 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004685 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686
Bram Moolenaar4770d092006-01-12 23:22:24 +00004687 newscore = 0;
4688 if (!soundfold) /* soundfold words don't have flags */
4689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004691 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 newscore += SCORE_REGION;
4693 if (flags & WF_RARE)
4694 newscore += SCORE_RARE;
4695
Bram Moolenaar0c405862005-06-22 22:26:26 +00004696 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004697 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700
Bram Moolenaar4770d092006-01-12 23:22:24 +00004701 /* TODO: how about splitting in the soundfold tree? */
4702 if (fword_ends
4703 && goodword_ends
4704 && sp->ts_fidx >= sp->ts_fidxtry
4705 && compound_ok)
4706 {
4707 /* The badword also ends: add suggestions. */
4708#ifdef DEBUG_TRIEWALK
4709 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004711 int j;
4712
4713 /* print the stack of changes that brought us here */
4714 smsg("------ %s -------", fword);
4715 for (j = 0; j < depth; ++j)
4716 smsg("%s", changename[j]);
4717 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004718#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004719 if (soundfold)
4720 {
4721 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004722 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004723 add_sound_suggest(su, preword, sp->ts_score, lp);
4724 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004725 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004726 {
4727 /* Give a penalty when changing non-word char to word
4728 * char, e.g., "thes," -> "these". */
4729 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004730 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004731 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004732 {
4733 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004734 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004736 newscore += SCORE_NONWORD;
4737 }
4738
Bram Moolenaar4770d092006-01-12 23:22:24 +00004739 /* Give a bonus to words seen before. */
4740 score = score_wordcount_adj(slang,
4741 sp->ts_score + newscore,
4742 preword + sp->ts_prewordlen,
4743 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004744
Bram Moolenaar4770d092006-01-12 23:22:24 +00004745 /* Add the suggestion if the score isn't too bad. */
4746 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004747 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004748 add_suggestion(su, &su->su_ga, preword,
4749 sp->ts_fidx - repextra,
4750 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004751
4752 if (su->su_badflags & WF_MIXCAP)
4753 {
4754 /* We really don't know if the word should be
4755 * upper or lower case, add both. */
4756 c = captype(preword, NULL);
4757 if (c == 0 || c == WF_ALLCAP)
4758 {
4759 make_case_word(tword + sp->ts_splitoff,
4760 preword + sp->ts_prewordlen,
4761 c == 0 ? WF_ALLCAP : 0);
4762
4763 add_suggestion(su, &su->su_ga, preword,
4764 sp->ts_fidx - repextra,
4765 score + SCORE_ICASE, 0, FALSE,
4766 lp->lp_sallang, FALSE);
4767 }
4768 }
4769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004771 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004772
Bram Moolenaar4770d092006-01-12 23:22:24 +00004773 /*
4774 * Try word split and/or compounding.
4775 */
4776 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00004777#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004778 /* Don't split halfway a character. */
4779 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004780#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004781 )
4782 {
4783 int try_compound;
4784 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004785
Bram Moolenaar4770d092006-01-12 23:22:24 +00004786 /* If past the end of the bad word don't try a split.
4787 * Otherwise try changing the next word. E.g., find
4788 * suggestions for "the the" where the second "the" is
4789 * different. It's done like a split.
4790 * TODO: word split for soundfold words */
4791 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4792 && !soundfold;
4793
4794 /* Get here in several situations:
4795 * 1. The word in the tree ends:
4796 * If the word allows compounding try that. Otherwise try
4797 * a split by inserting a space. For both check that a
4798 * valid words starts at fword[sp->ts_fidx].
4799 * For NOBREAK do like compounding to be able to check if
4800 * the next word is valid.
4801 * 2. The badword does end, but it was due to a change (e.g.,
4802 * a swap). No need to split, but do check that the
4803 * following word is valid.
4804 * 3. The badword and the word in the tree end. It may still
4805 * be possible to compound another (short) word.
4806 */
4807 try_compound = FALSE;
4808 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004809 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004810 && slang->sl_compprog != NULL
4811 && ((unsigned)flags >> 24) != 0
4812 && sp->ts_twordlen - sp->ts_splitoff
4813 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004814#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004815 && (!has_mbyte
4816 || slang->sl_compminlen == 0
4817 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004818 >= slang->sl_compminlen)
4819#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004820 && (slang->sl_compsylmax < MAXWLEN
4821 || sp->ts_complen + 1 - sp->ts_compsplit
4822 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004823 && (can_be_compound(sp, slang,
4824 compflags, ((unsigned)flags >> 24))))
4825
Bram Moolenaar4770d092006-01-12 23:22:24 +00004826 {
4827 try_compound = TRUE;
4828 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4829 compflags[sp->ts_complen + 1] = NUL;
4830 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004831
Bram Moolenaar4770d092006-01-12 23:22:24 +00004832 /* For NOBREAK we never try splitting, it won't make any word
4833 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004834 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004835 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004836
Bram Moolenaar4770d092006-01-12 23:22:24 +00004837 /* If we could add a compound word, and it's also possible to
4838 * split at this point, do the split first and set
4839 * TSF_DIDSPLIT to avoid doing it again. */
4840 else if (!fword_ends
4841 && try_compound
4842 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4843 {
4844 try_compound = FALSE;
4845 sp->ts_flags |= TSF_DIDSPLIT;
4846 --sp->ts_curi; /* do the same NUL again */
4847 compflags[sp->ts_complen] = NUL;
4848 }
4849 else
4850 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004851
Bram Moolenaar4770d092006-01-12 23:22:24 +00004852 if (try_split || try_compound)
4853 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004854 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004855 {
4856 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004857 * words so far are valid for compounding. If there
4858 * is only one word it must not have the NEEDCOMPOUND
4859 * flag. */
4860 if (sp->ts_complen == sp->ts_compsplit
4861 && (flags & WF_NEEDCOMP))
4862 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004863 p = preword;
4864 while (*skiptowhite(p) != NUL)
4865 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004866 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004867 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004868 compflags + sp->ts_compsplit))
4869 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004870
4871 if (slang->sl_nosplitsugs)
4872 newscore += SCORE_SPLIT_NO;
4873 else
4874 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004875
4876 /* Give a bonus to words seen before. */
4877 newscore = score_wordcount_adj(slang, newscore,
4878 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004879 }
4880
Bram Moolenaar4770d092006-01-12 23:22:24 +00004881 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004883 go_deeper(stack, depth, newscore);
4884#ifdef DEBUG_TRIEWALK
4885 if (!try_compound && !fword_ends)
4886 sprintf(changename[depth], "%.*s-%s: split",
4887 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4888 else
4889 sprintf(changename[depth], "%.*s-%s: compound",
4890 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4891#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004892 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004893 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004894 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004895 sp->ts_state = STATE_SPLITUNDO;
4896
4897 ++depth;
4898 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004900 /* Append a space to preword when splitting. */
4901 if (!try_compound && !fword_ends)
4902 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004903 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004904 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004905 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004906
4907 /* If the badword has a non-word character at this
4908 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004909 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004910 * character when the word ends. But only when the
4911 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004912 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004913 + sp->ts_fidx,
4914 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004915 || fword_ends)
4916 && fword[sp->ts_fidx] != NUL
4917 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004918 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004919 int l;
4920
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004921 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004922 if (fword_ends)
4923 {
4924 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004925 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004926 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004927 sp->ts_prewordlen += l;
4928 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004929 }
4930 else
4931 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4932 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004933 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004934
Bram Moolenaard12a1322005-08-21 22:08:24 +00004935 /* When compounding include compound flag in
4936 * compflags[] (already set above). When splitting we
4937 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004938 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004939 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004940 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004941 sp->ts_compsplit = sp->ts_complen;
4942 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004943
Bram Moolenaar53805d12005-08-01 07:08:33 +00004944 /* set su->su_badflags to the caps type at this
4945 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004946#ifdef FEAT_MBYTE
4947 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004948 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004949 else
4950#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004951 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004952 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004953 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004955 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004956 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004957
4958 /* If there are postponed prefixes, try these too. */
4959 if (pbyts != NULL)
4960 {
4961 byts = pbyts;
4962 idxs = pidxs;
4963 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004964 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004965 sp->ts_state = STATE_NOPREFIX;
4966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004967 }
4968 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004969 }
4970 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004971
Bram Moolenaar4770d092006-01-12 23:22:24 +00004972 case STATE_SPLITUNDO:
4973 /* Undo the changes done for word split or compound word. */
4974 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004975
Bram Moolenaar4770d092006-01-12 23:22:24 +00004976 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004977 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004978 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004979
Bram Moolenaar4770d092006-01-12 23:22:24 +00004980 /* In case we went into the prefix tree. */
4981 byts = fbyts;
4982 idxs = fidxs;
4983 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004984
Bram Moolenaar4770d092006-01-12 23:22:24 +00004985 case STATE_ENDNUL:
4986 /* Past the NUL bytes in the node. */
4987 su->su_badflags = sp->ts_save_badflags;
4988 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004989#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004990 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004991#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004992 )
4993 {
4994 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004995 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004996 sp->ts_state = STATE_DEL;
4997 break;
4998 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004999 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005000 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005001 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005002
5003 case STATE_PLAIN:
5004 /*
5005 * Go over all possible bytes at this node, add each to tword[]
5006 * and use child node. "ts_curi" is the index.
5007 */
5008 arridx = sp->ts_arridx;
5009 if (sp->ts_curi > byts[arridx])
5010 {
5011 /* Done all bytes at this node, do next state. When still at
5012 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005013 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005014 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005015 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005016 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005017 sp->ts_state = STATE_FINAL;
5018 }
5019 else
5020 {
5021 arridx += sp->ts_curi++;
5022 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005023
Bram Moolenaar4770d092006-01-12 23:22:24 +00005024 /* Normal byte, go one level deeper. If it's not equal to the
5025 * byte in the bad word adjust the score. But don't even try
5026 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01005027 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00005028 * delete + substitute. */
5029 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +00005030#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005031 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005032#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005033 )
5034 newscore = 0;
5035 else
5036 newscore = SCORE_SUBST;
5037 if ((newscore == 0
5038 || (sp->ts_fidx >= sp->ts_fidxtry
5039 && ((sp->ts_flags & TSF_DIDDEL) == 0
5040 || c != fword[sp->ts_delidx])))
5041 && TRY_DEEPER(su, stack, depth, newscore))
5042 {
5043 go_deeper(stack, depth, newscore);
5044#ifdef DEBUG_TRIEWALK
5045 if (newscore > 0)
5046 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
5047 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5048 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005049 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005050 sprintf(changename[depth], "%.*s-%s: accept %c",
5051 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5052 fword[sp->ts_fidx]);
5053#endif
5054 ++depth;
5055 sp = &stack[depth];
5056 ++sp->ts_fidx;
5057 tword[sp->ts_twordlen++] = c;
5058 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +00005059#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005060 if (newscore == SCORE_SUBST)
5061 sp->ts_isdiff = DIFF_YES;
5062 if (has_mbyte)
5063 {
5064 /* Multi-byte characters are a bit complicated to
5065 * handle: They differ when any of the bytes differ
5066 * and then their length may also differ. */
5067 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005068 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005069 /* First byte. */
5070 sp->ts_tcharidx = 0;
5071 sp->ts_tcharlen = MB_BYTE2LEN(c);
5072 sp->ts_fcharstart = sp->ts_fidx - 1;
5073 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005074 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005075 }
5076 else if (sp->ts_isdiff == DIFF_INSERT)
5077 /* When inserting trail bytes don't advance in the
5078 * bad word. */
5079 --sp->ts_fidx;
5080 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5081 {
5082 /* Last byte of character. */
5083 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00005084 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005085 /* Correct ts_fidx for the byte length of the
5086 * character (we didn't check that before). */
5087 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005088 + MB_PTR2LEN(
5089 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005090 /* For changing a composing character adjust
5091 * the score from SCORE_SUBST to
5092 * SCORE_SUBCOMP. */
5093 if (enc_utf8
5094 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005095 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00005096 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005097 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005098 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005099 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005100 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005101 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005102 SCORE_SUBST - SCORE_SUBCOMP;
5103
Bram Moolenaar4770d092006-01-12 23:22:24 +00005104 /* For a similar character adjust score from
5105 * SCORE_SUBST to SCORE_SIMILAR. */
5106 else if (!soundfold
5107 && slang->sl_has_map
5108 && similar_chars(slang,
5109 mb_ptr2char(tword
5110 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00005111 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00005112 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00005113 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005114 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00005115 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00005116 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005117 else if (sp->ts_isdiff == DIFF_INSERT
5118 && sp->ts_twordlen > sp->ts_tcharlen)
5119 {
5120 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
5121 c = mb_ptr2char(p);
5122 if (enc_utf8 && utf_iscomposing(c))
5123 {
5124 /* Inserting a composing char doesn't
5125 * count that much. */
5126 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5127 }
5128 else
5129 {
5130 /* If the previous character was the same,
5131 * thus doubling a character, give a bonus
5132 * to the score. Also for the soundfold
5133 * tree (might seem illogical but does
5134 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005135 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005136 if (c == mb_ptr2char(p))
5137 sp->ts_score -= SCORE_INS
5138 - SCORE_INSDUP;
5139 }
5140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005141
Bram Moolenaar4770d092006-01-12 23:22:24 +00005142 /* Starting a new char, reset the length. */
5143 sp->ts_tcharlen = 0;
5144 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005145 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005146 else
5147#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005148 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005149 /* If we found a similar char adjust the score.
5150 * We do this after calling go_deeper() because
5151 * it's slow. */
5152 if (newscore != 0
5153 && !soundfold
5154 && slang->sl_has_map
5155 && similar_chars(slang,
5156 c, fword[sp->ts_fidx - 1]))
5157 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005159 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005160 }
5161 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005162
Bram Moolenaar4770d092006-01-12 23:22:24 +00005163 case STATE_DEL:
5164#ifdef FEAT_MBYTE
5165 /* When past the first byte of a multi-byte char don't try
5166 * delete/insert/swap a character. */
5167 if (has_mbyte && sp->ts_tcharlen > 0)
5168 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005169 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005170 sp->ts_state = STATE_FINAL;
5171 break;
5172 }
5173#endif
5174 /*
5175 * Try skipping one character in the bad word (delete it).
5176 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005177 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005178 sp->ts_state = STATE_INS_PREP;
5179 sp->ts_curi = 1;
5180 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5181 /* Deleting a vowel at the start of a word counts less, see
5182 * soundalike_score(). */
5183 newscore = 2 * SCORE_DEL / 3;
5184 else
5185 newscore = SCORE_DEL;
5186 if (fword[sp->ts_fidx] != NUL
5187 && TRY_DEEPER(su, stack, depth, newscore))
5188 {
5189 go_deeper(stack, depth, newscore);
5190#ifdef DEBUG_TRIEWALK
5191 sprintf(changename[depth], "%.*s-%s: delete %c",
5192 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5193 fword[sp->ts_fidx]);
5194#endif
5195 ++depth;
5196
5197 /* Remember what character we deleted, so that we can avoid
5198 * inserting it again. */
5199 stack[depth].ts_flags |= TSF_DIDDEL;
5200 stack[depth].ts_delidx = sp->ts_fidx;
5201
5202 /* Advance over the character in fword[]. Give a bonus to the
5203 * score if the same character is following "nn" -> "n". It's
5204 * a bit illogical for soundfold tree but it does give better
5205 * results. */
5206#ifdef FEAT_MBYTE
5207 if (has_mbyte)
5208 {
5209 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005210 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005211 if (enc_utf8 && utf_iscomposing(c))
5212 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5213 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5214 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5215 }
5216 else
5217#endif
5218 {
5219 ++stack[depth].ts_fidx;
5220 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5221 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5222 }
5223 break;
5224 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005225 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005226
5227 case STATE_INS_PREP:
5228 if (sp->ts_flags & TSF_DIDDEL)
5229 {
5230 /* If we just deleted a byte then inserting won't make sense,
5231 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005232 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005233 sp->ts_state = STATE_SWAP;
5234 break;
5235 }
5236
5237 /* skip over NUL bytes */
5238 n = sp->ts_arridx;
5239 for (;;)
5240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005241 if (sp->ts_curi > byts[n])
5242 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005243 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005244 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005245 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005246 break;
5247 }
5248 if (byts[n + sp->ts_curi] != NUL)
5249 {
5250 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005251 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005252 sp->ts_state = STATE_INS;
5253 break;
5254 }
5255 ++sp->ts_curi;
5256 }
5257 break;
5258
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005259 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005260
5261 case STATE_INS:
5262 /* Insert one byte. Repeat this for each possible byte at this
5263 * node. */
5264 n = sp->ts_arridx;
5265 if (sp->ts_curi > byts[n])
5266 {
5267 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005268 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005269 sp->ts_state = STATE_SWAP;
5270 break;
5271 }
5272
5273 /* Do one more byte at this node, but:
5274 * - Skip NUL bytes.
5275 * - Skip the byte if it's equal to the byte in the word,
5276 * accepting that byte is always better.
5277 */
5278 n += sp->ts_curi++;
5279 c = byts[n];
5280 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5281 /* Inserting a vowel at the start of a word counts less,
5282 * see soundalike_score(). */
5283 newscore = 2 * SCORE_INS / 3;
5284 else
5285 newscore = SCORE_INS;
5286 if (c != fword[sp->ts_fidx]
5287 && TRY_DEEPER(su, stack, depth, newscore))
5288 {
5289 go_deeper(stack, depth, newscore);
5290#ifdef DEBUG_TRIEWALK
5291 sprintf(changename[depth], "%.*s-%s: insert %c",
5292 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5293 c);
5294#endif
5295 ++depth;
5296 sp = &stack[depth];
5297 tword[sp->ts_twordlen++] = c;
5298 sp->ts_arridx = idxs[n];
5299#ifdef FEAT_MBYTE
5300 if (has_mbyte)
5301 {
5302 fl = MB_BYTE2LEN(c);
5303 if (fl > 1)
5304 {
5305 /* There are following bytes for the same character.
5306 * We must find all bytes before trying
5307 * delete/insert/swap/etc. */
5308 sp->ts_tcharlen = fl;
5309 sp->ts_tcharidx = 1;
5310 sp->ts_isdiff = DIFF_INSERT;
5311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 }
5313 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005314 fl = 1;
5315 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00005316#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005317 {
5318 /* If the previous character was the same, thus doubling a
5319 * character, give a bonus to the score. Also for
5320 * soundfold words (illogical but does give a better
5321 * score). */
5322 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005323 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005324 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005326 }
5327 break;
5328
5329 case STATE_SWAP:
5330 /*
5331 * Swap two bytes in the bad word: "12" -> "21".
5332 * We change "fword" here, it's changed back afterwards at
5333 * STATE_UNSWAP.
5334 */
5335 p = fword + sp->ts_fidx;
5336 c = *p;
5337 if (c == NUL)
5338 {
5339 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005340 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005341 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005342 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344
Bram Moolenaar4770d092006-01-12 23:22:24 +00005345 /* Don't swap if the first character is not a word character.
5346 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005347 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005348 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005349 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005350 sp->ts_state = STATE_REP_INI;
5351 break;
5352 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005353
Bram Moolenaar4770d092006-01-12 23:22:24 +00005354#ifdef FEAT_MBYTE
5355 if (has_mbyte)
5356 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005357 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005358 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005359 if (p[n] == NUL)
5360 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005361 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005362 c2 = c; /* don't swap non-word char */
5363 else
5364 c2 = mb_ptr2char(p + n);
5365 }
5366 else
5367#endif
5368 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005369 if (p[1] == NUL)
5370 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005371 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005372 c2 = c; /* don't swap non-word char */
5373 else
5374 c2 = p[1];
5375 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005376
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005377 /* When the second character is NUL we can't swap. */
5378 if (c2 == NUL)
5379 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005380 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005381 sp->ts_state = STATE_REP_INI;
5382 break;
5383 }
5384
Bram Moolenaar4770d092006-01-12 23:22:24 +00005385 /* When characters are identical, swap won't do anything.
5386 * Also get here if the second char is not a word character. */
5387 if (c == c2)
5388 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005389 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005390 sp->ts_state = STATE_SWAP3;
5391 break;
5392 }
5393 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5394 {
5395 go_deeper(stack, depth, SCORE_SWAP);
5396#ifdef DEBUG_TRIEWALK
5397 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5398 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5399 c, c2);
5400#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005401 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005402 sp->ts_state = STATE_UNSWAP;
5403 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005404#ifdef FEAT_MBYTE
5405 if (has_mbyte)
5406 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005407 fl = mb_char2len(c2);
5408 mch_memmove(p, p + n, fl);
5409 mb_char2bytes(c, p + fl);
5410 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005411 }
5412 else
5413#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005414 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005415 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005416 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005417 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005418 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005419 }
5420 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005421 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005422 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005423 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005424 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005425 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005426 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005427
Bram Moolenaar4770d092006-01-12 23:22:24 +00005428 case STATE_UNSWAP:
5429 /* Undo the STATE_SWAP swap: "21" -> "12". */
5430 p = fword + sp->ts_fidx;
5431#ifdef FEAT_MBYTE
5432 if (has_mbyte)
5433 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005434 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005435 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005436 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005437 mb_char2bytes(c, p);
5438 }
5439 else
5440#endif
5441 {
5442 c = *p;
5443 *p = p[1];
5444 p[1] = c;
5445 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005446 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005447
5448 case STATE_SWAP3:
5449 /* Swap two bytes, skipping one: "123" -> "321". We change
5450 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5451 p = fword + sp->ts_fidx;
5452#ifdef FEAT_MBYTE
5453 if (has_mbyte)
5454 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005455 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005456 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005457 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005458 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005459 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005460 c3 = c; /* don't swap non-word char */
5461 else
5462 c3 = mb_ptr2char(p + n + fl);
5463 }
5464 else
5465#endif
5466 {
5467 c = *p;
5468 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005469 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005470 c3 = c; /* don't swap non-word char */
5471 else
5472 c3 = p[2];
5473 }
5474
5475 /* When characters are identical: "121" then SWAP3 result is
5476 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5477 * same as SWAP on next char: "112". Thus skip all swapping.
5478 * Also skip when c3 is NUL.
5479 * Also get here when the third character is not a word character.
5480 * Second character may any char: "a.b" -> "b.a" */
5481 if (c == c3 || c3 == NUL)
5482 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005483 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005484 sp->ts_state = STATE_REP_INI;
5485 break;
5486 }
5487 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5488 {
5489 go_deeper(stack, depth, SCORE_SWAP3);
5490#ifdef DEBUG_TRIEWALK
5491 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5492 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5493 c, c3);
5494#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005495 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005496 sp->ts_state = STATE_UNSWAP3;
5497 ++depth;
5498#ifdef FEAT_MBYTE
5499 if (has_mbyte)
5500 {
5501 tl = mb_char2len(c3);
5502 mch_memmove(p, p + n + fl, tl);
5503 mb_char2bytes(c2, p + tl);
5504 mb_char2bytes(c, p + fl + tl);
5505 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5506 }
5507 else
5508#endif
5509 {
5510 p[0] = p[2];
5511 p[2] = c;
5512 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5513 }
5514 }
5515 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005516 {
5517 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005518 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005519 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005520 break;
5521
5522 case STATE_UNSWAP3:
5523 /* Undo STATE_SWAP3: "321" -> "123" */
5524 p = fword + sp->ts_fidx;
5525#ifdef FEAT_MBYTE
5526 if (has_mbyte)
5527 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005528 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005529 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005530 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005531 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005532 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005533 mch_memmove(p + fl + tl, p, n);
5534 mb_char2bytes(c, p);
5535 mb_char2bytes(c2, p + tl);
5536 p = p + tl;
5537 }
5538 else
5539#endif
5540 {
5541 c = *p;
5542 *p = p[2];
5543 p[2] = c;
5544 ++p;
5545 }
5546
Bram Moolenaar860cae12010-06-05 23:22:07 +02005547 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005548 {
5549 /* Middle char is not a word char, skip the rotate. First and
5550 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005551 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005552 sp->ts_state = STATE_REP_INI;
5553 break;
5554 }
5555
5556 /* Rotate three characters left: "123" -> "231". We change
5557 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5558 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5559 {
5560 go_deeper(stack, depth, SCORE_SWAP3);
5561#ifdef DEBUG_TRIEWALK
5562 p = fword + sp->ts_fidx;
5563 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5564 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5565 p[0], p[1], p[2]);
5566#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005567 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005568 sp->ts_state = STATE_UNROT3L;
5569 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005570 p = fword + sp->ts_fidx;
5571#ifdef FEAT_MBYTE
5572 if (has_mbyte)
5573 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005574 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005575 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005576 fl = MB_CPTR2LEN(p + n);
5577 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005578 mch_memmove(p, p + n, fl);
5579 mb_char2bytes(c, p + fl);
5580 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005581 }
5582 else
5583#endif
5584 {
5585 c = *p;
5586 *p = p[1];
5587 p[1] = p[2];
5588 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005589 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005590 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005591 }
5592 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005593 {
5594 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005595 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005596 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005597 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005598
Bram Moolenaar4770d092006-01-12 23:22:24 +00005599 case STATE_UNROT3L:
5600 /* Undo ROT3L: "231" -> "123" */
5601 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005602#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005603 if (has_mbyte)
5604 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005605 n = MB_PTR2LEN(p);
5606 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005607 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005608 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005609 mch_memmove(p + tl, p, n);
5610 mb_char2bytes(c, p);
5611 }
5612 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005613#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005614 {
5615 c = p[2];
5616 p[2] = p[1];
5617 p[1] = *p;
5618 *p = c;
5619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005620
Bram Moolenaar4770d092006-01-12 23:22:24 +00005621 /* Rotate three bytes right: "123" -> "312". We change "fword"
5622 * here, it's changed back afterwards at STATE_UNROT3R. */
5623 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5624 {
5625 go_deeper(stack, depth, SCORE_SWAP3);
5626#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005627 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005628 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5629 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5630 p[0], p[1], p[2]);
5631#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005632 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005633 sp->ts_state = STATE_UNROT3R;
5634 ++depth;
5635 p = fword + sp->ts_fidx;
5636#ifdef FEAT_MBYTE
5637 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005638 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005639 n = MB_CPTR2LEN(p);
5640 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005641 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005642 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005643 mch_memmove(p + tl, p, n);
5644 mb_char2bytes(c, p);
5645 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005646 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005647 else
5648#endif
5649 {
5650 c = p[2];
5651 p[2] = p[1];
5652 p[1] = *p;
5653 *p = c;
5654 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5655 }
5656 }
5657 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005658 {
5659 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005660 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005661 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005662 break;
5663
5664 case STATE_UNROT3R:
5665 /* Undo ROT3R: "312" -> "123" */
5666 p = fword + sp->ts_fidx;
5667#ifdef FEAT_MBYTE
5668 if (has_mbyte)
5669 {
5670 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005671 tl = MB_PTR2LEN(p);
5672 n = MB_PTR2LEN(p + tl);
5673 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005674 mch_memmove(p, p + tl, n);
5675 mb_char2bytes(c, p + n);
5676 }
5677 else
5678#endif
5679 {
5680 c = *p;
5681 *p = p[1];
5682 p[1] = p[2];
5683 p[2] = c;
5684 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005685 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005686
5687 case STATE_REP_INI:
5688 /* Check if matching with REP items from the .aff file would work.
5689 * Quickly skip if:
5690 * - there are no REP items and we are not in the soundfold trie
5691 * - the score is going to be too high anyway
5692 * - already applied a REP item or swapped here */
5693 if ((lp->lp_replang == NULL && !soundfold)
5694 || sp->ts_score + SCORE_REP >= su->su_maxscore
5695 || sp->ts_fidx < sp->ts_fidxtry)
5696 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005697 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005698 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005699 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005700 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005701
Bram Moolenaar4770d092006-01-12 23:22:24 +00005702 /* Use the first byte to quickly find the first entry that may
5703 * match. If the index is -1 there is none. */
5704 if (soundfold)
5705 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5706 else
5707 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005708
Bram Moolenaar4770d092006-01-12 23:22:24 +00005709 if (sp->ts_curi < 0)
5710 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005711 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005712 sp->ts_state = STATE_FINAL;
5713 break;
5714 }
5715
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005716 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005717 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005718 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005719
5720 case STATE_REP:
5721 /* Try matching with REP items from the .aff file. For each match
5722 * replace the characters and check if the resulting word is
5723 * valid. */
5724 p = fword + sp->ts_fidx;
5725
5726 if (soundfold)
5727 gap = &slang->sl_repsal;
5728 else
5729 gap = &lp->lp_replang->sl_rep;
5730 while (sp->ts_curi < gap->ga_len)
5731 {
5732 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5733 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005734 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005735 /* past possible matching entries */
5736 sp->ts_curi = gap->ga_len;
5737 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005738 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005739 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5740 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5741 {
5742 go_deeper(stack, depth, SCORE_REP);
5743#ifdef DEBUG_TRIEWALK
5744 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5745 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5746 ftp->ft_from, ftp->ft_to);
5747#endif
5748 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005749 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005750 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005751
Bram Moolenaar4770d092006-01-12 23:22:24 +00005752 /* Change the "from" to the "to" string. */
5753 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005754 fl = (int)STRLEN(ftp->ft_from);
5755 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005756 if (fl != tl)
5757 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005758 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005759 repextra += tl - fl;
5760 }
5761 mch_memmove(p, ftp->ft_to, tl);
5762 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
5763#ifdef FEAT_MBYTE
5764 stack[depth].ts_tcharlen = 0;
5765#endif
5766 break;
5767 }
5768 }
5769
5770 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005771 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005772 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005773 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005774 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005775 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005776
5777 break;
5778
5779 case STATE_REP_UNDO:
5780 /* Undo a REP replacement and continue with the next one. */
5781 if (soundfold)
5782 gap = &slang->sl_repsal;
5783 else
5784 gap = &lp->lp_replang->sl_rep;
5785 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005786 fl = (int)STRLEN(ftp->ft_from);
5787 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005788 p = fword + sp->ts_fidx;
5789 if (fl != tl)
5790 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005791 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005792 repextra -= tl - fl;
5793 }
5794 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005795 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005796 sp->ts_state = STATE_REP;
5797 break;
5798
5799 default:
5800 /* Did all possible states at this level, go up one level. */
5801 --depth;
5802
5803 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5804 {
5805 /* Continue in or go back to the prefix tree. */
5806 byts = pbyts;
5807 idxs = pidxs;
5808 }
5809
5810 /* Don't check for CTRL-C too often, it takes time. */
5811 if (--breakcheckcount == 0)
5812 {
5813 ui_breakcheck();
5814 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005815 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005816 }
5817 }
5818}
5819
Bram Moolenaar4770d092006-01-12 23:22:24 +00005820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005821/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005822 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005823 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005824 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005825go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005826{
Bram Moolenaarea424162005-06-16 21:51:00 +00005827 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005828 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005829 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005830 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005831 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005832}
5833
Bram Moolenaar53805d12005-08-01 07:08:33 +00005834#ifdef FEAT_MBYTE
5835/*
5836 * Case-folding may change the number of bytes: Count nr of chars in
5837 * fword[flen] and return the byte length of that many chars in "word".
5838 */
5839 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005840nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005841{
5842 char_u *p;
5843 int i = 0;
5844
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005845 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005846 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005847 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005848 --i;
5849 return (int)(p - word);
5850}
5851#endif
5852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005853/*
5854 * "fword" is a good word with case folded. Find the matching keep-case
5855 * words and put it in "kword".
5856 * Theoretically there could be several keep-case words that result in the
5857 * same case-folded word, but we only find one...
5858 */
5859 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005860find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005861{
5862 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5863 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005864 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005865
5866 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005867 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005868 int round[MAXWLEN];
5869 int fwordidx[MAXWLEN];
5870 int uwordidx[MAXWLEN];
5871 int kwordlen[MAXWLEN];
5872
5873 int flen, ulen;
5874 int l;
5875 int len;
5876 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005877 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005878 char_u *p;
5879 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005880 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005881
5882 if (byts == NULL)
5883 {
5884 /* array is empty: "cannot happen" */
5885 *kword = NUL;
5886 return;
5887 }
5888
5889 /* Make an all-cap version of "fword". */
5890 allcap_copy(fword, uword);
5891
5892 /*
5893 * Each character needs to be tried both case-folded and upper-case.
5894 * All this gets very complicated if we keep in mind that changing case
5895 * may change the byte length of a multi-byte character...
5896 */
5897 depth = 0;
5898 arridx[0] = 0;
5899 round[0] = 0;
5900 fwordidx[0] = 0;
5901 uwordidx[0] = 0;
5902 kwordlen[0] = 0;
5903 while (depth >= 0)
5904 {
5905 if (fword[fwordidx[depth]] == NUL)
5906 {
5907 /* We are at the end of "fword". If the tree allows a word to end
5908 * here we have found a match. */
5909 if (byts[arridx[depth] + 1] == 0)
5910 {
5911 kword[kwordlen[depth]] = NUL;
5912 return;
5913 }
5914
5915 /* kword is getting too long, continue one level up */
5916 --depth;
5917 }
5918 else if (++round[depth] > 2)
5919 {
5920 /* tried both fold-case and upper-case character, continue one
5921 * level up */
5922 --depth;
5923 }
5924 else
5925 {
5926 /*
5927 * round[depth] == 1: Try using the folded-case character.
5928 * round[depth] == 2: Try using the upper-case character.
5929 */
5930#ifdef FEAT_MBYTE
5931 if (has_mbyte)
5932 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005933 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5934 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005935 }
5936 else
5937#endif
5938 ulen = flen = 1;
5939 if (round[depth] == 1)
5940 {
5941 p = fword + fwordidx[depth];
5942 l = flen;
5943 }
5944 else
5945 {
5946 p = uword + uwordidx[depth];
5947 l = ulen;
5948 }
5949
5950 for (tryidx = arridx[depth]; l > 0; --l)
5951 {
5952 /* Perform a binary search in the list of accepted bytes. */
5953 len = byts[tryidx++];
5954 c = *p++;
5955 lo = tryidx;
5956 hi = tryidx + len - 1;
5957 while (lo < hi)
5958 {
5959 m = (lo + hi) / 2;
5960 if (byts[m] > c)
5961 hi = m - 1;
5962 else if (byts[m] < c)
5963 lo = m + 1;
5964 else
5965 {
5966 lo = hi = m;
5967 break;
5968 }
5969 }
5970
5971 /* Stop if there is no matching byte. */
5972 if (hi < lo || byts[lo] != c)
5973 break;
5974
5975 /* Continue at the child (if there is one). */
5976 tryidx = idxs[lo];
5977 }
5978
5979 if (l == 0)
5980 {
5981 /*
5982 * Found the matching char. Copy it to "kword" and go a
5983 * level deeper.
5984 */
5985 if (round[depth] == 1)
5986 {
5987 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5988 flen);
5989 kwordlen[depth + 1] = kwordlen[depth] + flen;
5990 }
5991 else
5992 {
5993 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5994 ulen);
5995 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5996 }
5997 fwordidx[depth + 1] = fwordidx[depth] + flen;
5998 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5999
6000 ++depth;
6001 arridx[depth] = tryidx;
6002 round[depth] = 0;
6003 }
6004 }
6005 }
6006
6007 /* Didn't find it: "cannot happen". */
6008 *kword = NUL;
6009}
6010
6011/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006012 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6013 * su->su_sga.
6014 */
6015 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006016score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006017{
6018 langp_T *lp;
6019 char_u badsound[MAXWLEN];
6020 int i;
6021 suggest_T *stp;
6022 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006023 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006024 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006025
6026 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6027 return;
6028
6029 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006030 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006031 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006032 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006033 if (lp->lp_slang->sl_sal.ga_len > 0)
6034 {
6035 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006036 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006037
6038 for (i = 0; i < su->su_ga.ga_len; ++i)
6039 {
6040 stp = &SUG(su->su_ga, i);
6041
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006042 /* Case-fold the suggested word, sound-fold it and compute the
6043 * sound-a-like score. */
6044 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006045 if (score < SCORE_MAXMAX)
6046 {
6047 /* Add the suggestion. */
6048 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6049 sstp->st_word = vim_strsave(stp->st_word);
6050 if (sstp->st_word != NULL)
6051 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006052 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006053 sstp->st_score = score;
6054 sstp->st_altscore = 0;
6055 sstp->st_orglen = stp->st_orglen;
6056 ++su->su_sga.ga_len;
6057 }
6058 }
6059 }
6060 break;
6061 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006062 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006063}
6064
6065/*
6066 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006067 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006068 */
6069 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006070score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006071{
6072 int i;
6073 int j;
6074 garray_T ga;
6075 garray_T *gap;
6076 langp_T *lp;
6077 suggest_T *stp;
6078 char_u *p;
6079 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006080 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006081 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006082 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006083
6084 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006085 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006086 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006087 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006088 if (lp->lp_slang->sl_sal.ga_len > 0)
6089 {
6090 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006091 slang = lp->lp_slang;
6092 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006093
6094 for (i = 0; i < su->su_ga.ga_len; ++i)
6095 {
6096 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006097 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006098 if (stp->st_altscore == SCORE_MAXMAX)
6099 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6100 else
6101 stp->st_score = (stp->st_score * 3
6102 + stp->st_altscore) / 4;
6103 stp->st_salscore = FALSE;
6104 }
6105 break;
6106 }
6107 }
6108
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006109 if (slang == NULL) /* Using "double" without sound folding. */
6110 {
6111 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
6112 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006113 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006114 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006115
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006116 /* Add the alternate score to su_sga. */
6117 for (i = 0; i < su->su_sga.ga_len; ++i)
6118 {
6119 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006120 stp->st_altscore = spell_edit_score(slang,
6121 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006122 if (stp->st_score == SCORE_MAXMAX)
6123 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6124 else
6125 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6126 stp->st_salscore = TRUE;
6127 }
6128
Bram Moolenaar4770d092006-01-12 23:22:24 +00006129 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
6130 * for both lists. */
6131 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006132 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006133 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006134 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6135
6136 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6137 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6138 return;
6139
6140 stp = &SUG(ga, 0);
6141 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6142 {
6143 /* round 1: get a suggestion from su_ga
6144 * round 2: get a suggestion from su_sga */
6145 for (round = 1; round <= 2; ++round)
6146 {
6147 gap = round == 1 ? &su->su_ga : &su->su_sga;
6148 if (i < gap->ga_len)
6149 {
6150 /* Don't add a word if it's already there. */
6151 p = SUG(*gap, i).st_word;
6152 for (j = 0; j < ga.ga_len; ++j)
6153 if (STRCMP(stp[j].st_word, p) == 0)
6154 break;
6155 if (j == ga.ga_len)
6156 stp[ga.ga_len++] = SUG(*gap, i);
6157 else
6158 vim_free(p);
6159 }
6160 }
6161 }
6162
6163 ga_clear(&su->su_ga);
6164 ga_clear(&su->su_sga);
6165
6166 /* Truncate the list to the number of suggestions that will be displayed. */
6167 if (ga.ga_len > su->su_maxcount)
6168 {
6169 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6170 vim_free(stp[i].st_word);
6171 ga.ga_len = su->su_maxcount;
6172 }
6173
6174 su->su_ga = ga;
6175}
6176
6177/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006178 * For the goodword in "stp" compute the soundalike score compared to the
6179 * badword.
6180 */
6181 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006182stp_sal_score(
6183 suggest_T *stp,
6184 suginfo_T *su,
6185 slang_T *slang,
6186 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006187{
6188 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006189 char_u *pbad;
6190 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006191 char_u badsound2[MAXWLEN];
6192 char_u fword[MAXWLEN];
6193 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006194 char_u goodword[MAXWLEN];
6195 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006196
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006197 lendiff = (int)(su->su_badlen - stp->st_orglen);
6198 if (lendiff >= 0)
6199 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006200 else
6201 {
6202 /* soundfold the bad word with more characters following */
6203 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6204
6205 /* When joining two words the sound often changes a lot. E.g., "t he"
6206 * sounds like "t h" while "the" sounds like "@". Avoid that by
6207 * removing the space. Don't do it when the good word also contains a
6208 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006209 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006210 && *skiptowhite(stp->st_word) == NUL)
6211 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006212 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006213
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006214 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006215 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006216 }
6217
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006218 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006219 {
6220 /* Add part of the bad word to the good word, so that we soundfold
6221 * what replaces the bad word. */
6222 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006223 vim_strncpy(goodword + stp->st_wordlen,
6224 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006225 pgood = goodword;
6226 }
6227 else
6228 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006229
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006230 /* Sound-fold the word and compute the score for the difference. */
6231 spell_soundfold(slang, pgood, FALSE, goodsound);
6232
6233 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006234}
6235
Bram Moolenaar4770d092006-01-12 23:22:24 +00006236/* structure used to store soundfolded words that add_sound_suggest() has
6237 * handled already. */
6238typedef struct
6239{
6240 short sft_score; /* lowest score used */
6241 char_u sft_word[1]; /* soundfolded word, actually longer */
6242} sftword_T;
6243
6244static sftword_T dumsft;
6245#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6246#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6247
6248/*
6249 * Prepare for calling suggest_try_soundalike().
6250 */
6251 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006252suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006253{
6254 langp_T *lp;
6255 int lpi;
6256 slang_T *slang;
6257
6258 /* Do this for all languages that support sound folding and for which a
6259 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006260 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006261 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006262 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006263 slang = lp->lp_slang;
6264 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6265 /* prepare the hashtable used by add_sound_suggest() */
6266 hash_init(&slang->sl_sounddone);
6267 }
6268}
6269
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006270/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006271 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006272 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006273 */
6274 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006275suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006276{
6277 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006278 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006279 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006280 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006281
Bram Moolenaar4770d092006-01-12 23:22:24 +00006282 /* Do this for all languages that support sound folding and for which a
6283 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006284 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006285 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006286 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006287 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006288 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006289 {
6290 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006291 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006292
Bram Moolenaar4770d092006-01-12 23:22:24 +00006293 /* try all kinds of inserts/deletes/swaps/etc. */
6294 /* TODO: also soundfold the next words, so that we can try joining
6295 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006296#ifdef SUGGEST_PROFILE
6297 prof_init();
6298#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006299 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006300#ifdef SUGGEST_PROFILE
6301 prof_report("soundalike");
6302#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006303 }
6304 }
6305}
6306
6307/*
6308 * Finish up after calling suggest_try_soundalike().
6309 */
6310 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006311suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006312{
6313 langp_T *lp;
6314 int lpi;
6315 slang_T *slang;
6316 int todo;
6317 hashitem_T *hi;
6318
6319 /* Do this for all languages that support sound folding and for which a
6320 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006321 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006322 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006323 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006324 slang = lp->lp_slang;
6325 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6326 {
6327 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006328 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006329 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6330 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006331 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006332 vim_free(HI2SFT(hi));
6333 --todo;
6334 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006335
6336 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006337 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006338 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006339 }
6340 }
6341}
6342
6343/*
6344 * A match with a soundfolded word is found. Add the good word(s) that
6345 * produce this soundfolded word.
6346 */
6347 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006348add_sound_suggest(
6349 suginfo_T *su,
6350 char_u *goodword,
6351 int score, /* soundfold score */
6352 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006353{
6354 slang_T *slang = lp->lp_slang; /* language for sound folding */
6355 int sfwordnr;
6356 char_u *nrline;
6357 int orgnr;
6358 char_u theword[MAXWLEN];
6359 int i;
6360 int wlen;
6361 char_u *byts;
6362 idx_T *idxs;
6363 int n;
6364 int wordcount;
6365 int wc;
6366 int goodscore;
6367 hash_T hash;
6368 hashitem_T *hi;
6369 sftword_T *sft;
6370 int bc, gc;
6371 int limit;
6372
6373 /*
6374 * It's very well possible that the same soundfold word is found several
6375 * times with different scores. Since the following is quite slow only do
6376 * the words that have a better score than before. Use a hashtable to
6377 * remember the words that have been done.
6378 */
6379 hash = hash_hash(goodword);
6380 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6381 if (HASHITEM_EMPTY(hi))
6382 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006383 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6384 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006385 if (sft != NULL)
6386 {
6387 sft->sft_score = score;
6388 STRCPY(sft->sft_word, goodword);
6389 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6390 }
6391 }
6392 else
6393 {
6394 sft = HI2SFT(hi);
6395 if (score >= sft->sft_score)
6396 return;
6397 sft->sft_score = score;
6398 }
6399
6400 /*
6401 * Find the word nr in the soundfold tree.
6402 */
6403 sfwordnr = soundfold_find(slang, goodword);
6404 if (sfwordnr < 0)
6405 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006406 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006407 return;
6408 }
6409
6410 /*
6411 * go over the list of good words that produce this soundfold word
6412 */
6413 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6414 orgnr = 0;
6415 while (*nrline != NUL)
6416 {
6417 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6418 * previous wordnr. */
6419 orgnr += bytes2offset(&nrline);
6420
6421 byts = slang->sl_fbyts;
6422 idxs = slang->sl_fidxs;
6423
6424 /* Lookup the word "orgnr" one of the two tries. */
6425 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006426 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006427 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006428 {
6429 i = 1;
6430 if (wordcount == orgnr && byts[n + 1] == NUL)
6431 break; /* found end of word */
6432
6433 if (byts[n + 1] == NUL)
6434 ++wordcount;
6435
6436 /* skip over the NUL bytes */
6437 for ( ; byts[n + i] == NUL; ++i)
6438 if (i > byts[n]) /* safety check */
6439 {
6440 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006441 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006442 goto badword;
6443 }
6444
6445 /* One of the siblings must have the word. */
6446 for ( ; i < byts[n]; ++i)
6447 {
6448 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6449 if (wordcount + wc > orgnr)
6450 break;
6451 wordcount += wc;
6452 }
6453
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006454 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006455 n = idxs[n + i];
6456 }
6457badword:
6458 theword[wlen] = NUL;
6459
6460 /* Go over the possible flags and regions. */
6461 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6462 {
6463 char_u cword[MAXWLEN];
6464 char_u *p;
6465 int flags = (int)idxs[n + i];
6466
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006467 /* Skip words with the NOSUGGEST flag */
6468 if (flags & WF_NOSUGGEST)
6469 continue;
6470
Bram Moolenaar4770d092006-01-12 23:22:24 +00006471 if (flags & WF_KEEPCAP)
6472 {
6473 /* Must find the word in the keep-case tree. */
6474 find_keepcap_word(slang, theword, cword);
6475 p = cword;
6476 }
6477 else
6478 {
6479 flags |= su->su_badflags;
6480 if ((flags & WF_CAPMASK) != 0)
6481 {
6482 /* Need to fix case according to "flags". */
6483 make_case_word(theword, cword, flags);
6484 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006485 }
6486 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006487 p = theword;
6488 }
6489
6490 /* Add the suggestion. */
6491 if (sps_flags & SPS_DOUBLE)
6492 {
6493 /* Add the suggestion if the score isn't too bad. */
6494 if (score <= su->su_maxscore)
6495 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6496 score, 0, FALSE, slang, FALSE);
6497 }
6498 else
6499 {
6500 /* Add a penalty for words in another region. */
6501 if ((flags & WF_REGION)
6502 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6503 goodscore = SCORE_REGION;
6504 else
6505 goodscore = 0;
6506
6507 /* Add a small penalty for changing the first letter from
6508 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006509 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006510 * letter is the same, that has already been counted. */
6511 gc = PTR2CHAR(p);
6512 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006513 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006514 bc = PTR2CHAR(su->su_badword);
6515 if (!SPELL_ISUPPER(bc)
6516 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6517 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006518 }
6519
Bram Moolenaar4770d092006-01-12 23:22:24 +00006520 /* Compute the score for the good word. This only does letter
6521 * insert/delete/swap/replace. REP items are not considered,
6522 * which may make the score a bit higher.
6523 * Use a limit for the score to make it work faster. Use
6524 * MAXSCORE(), because RESCORE() will change the score.
6525 * If the limit is very high then the iterative method is
6526 * inefficient, using an array is quicker. */
6527 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6528 if (limit > SCORE_LIMITMAX)
6529 goodscore += spell_edit_score(slang, su->su_badword, p);
6530 else
6531 goodscore += spell_edit_score_limit(slang, su->su_badword,
6532 p, limit);
6533
6534 /* When going over the limit don't bother to do the rest. */
6535 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006536 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006537 /* Give a bonus to words seen before. */
6538 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006539
Bram Moolenaar4770d092006-01-12 23:22:24 +00006540 /* Add the suggestion if the score isn't too bad. */
6541 goodscore = RESCORE(goodscore, score);
6542 if (goodscore <= su->su_sfmaxscore)
6543 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6544 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006546 }
6547 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006548 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006549 }
6550}
6551
6552/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006553 * Find word "word" in fold-case tree for "slang" and return the word number.
6554 */
6555 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006556soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006557{
6558 idx_T arridx = 0;
6559 int len;
6560 int wlen = 0;
6561 int c;
6562 char_u *ptr = word;
6563 char_u *byts;
6564 idx_T *idxs;
6565 int wordnr = 0;
6566
6567 byts = slang->sl_sbyts;
6568 idxs = slang->sl_sidxs;
6569
6570 for (;;)
6571 {
6572 /* First byte is the number of possible bytes. */
6573 len = byts[arridx++];
6574
6575 /* If the first possible byte is a zero the word could end here.
6576 * If the word ends we found the word. If not skip the NUL bytes. */
6577 c = ptr[wlen];
6578 if (byts[arridx] == NUL)
6579 {
6580 if (c == NUL)
6581 break;
6582
6583 /* Skip over the zeros, there can be several. */
6584 while (len > 0 && byts[arridx] == NUL)
6585 {
6586 ++arridx;
6587 --len;
6588 }
6589 if (len == 0)
6590 return -1; /* no children, word should have ended here */
6591 ++wordnr;
6592 }
6593
6594 /* If the word ends we didn't find it. */
6595 if (c == NUL)
6596 return -1;
6597
6598 /* Perform a binary search in the list of accepted bytes. */
6599 if (c == TAB) /* <Tab> is handled like <Space> */
6600 c = ' ';
6601 while (byts[arridx] < c)
6602 {
6603 /* The word count is in the first idxs[] entry of the child. */
6604 wordnr += idxs[idxs[arridx]];
6605 ++arridx;
6606 if (--len == 0) /* end of the bytes, didn't find it */
6607 return -1;
6608 }
6609 if (byts[arridx] != c) /* didn't find the byte */
6610 return -1;
6611
6612 /* Continue at the child (if there is one). */
6613 arridx = idxs[arridx];
6614 ++wlen;
6615
6616 /* One space in the good word may stand for several spaces in the
6617 * checked word. */
6618 if (c == ' ')
6619 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6620 ++wlen;
6621 }
6622
6623 return wordnr;
6624}
6625
6626/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006627 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006628 */
6629 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006630make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006631{
6632 if (flags & WF_ALLCAP)
6633 /* Make it all upper-case */
6634 allcap_copy(fword, cword);
6635 else if (flags & WF_ONECAP)
6636 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006637 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006638 else
6639 /* Use goodword as-is. */
6640 STRCPY(cword, fword);
6641}
6642
Bram Moolenaarea424162005-06-16 21:51:00 +00006643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006644/*
6645 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6646 * lines in the .aff file.
6647 */
6648 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006649similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006650{
Bram Moolenaarea424162005-06-16 21:51:00 +00006651 int m1, m2;
6652#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006653 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006654 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006655
Bram Moolenaarea424162005-06-16 21:51:00 +00006656 if (c1 >= 256)
6657 {
6658 buf[mb_char2bytes(c1, buf)] = 0;
6659 hi = hash_find(&slang->sl_map_hash, buf);
6660 if (HASHITEM_EMPTY(hi))
6661 m1 = 0;
6662 else
6663 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6664 }
6665 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006666#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006667 m1 = slang->sl_map_array[c1];
6668 if (m1 == 0)
6669 return FALSE;
6670
6671
6672#ifdef FEAT_MBYTE
6673 if (c2 >= 256)
6674 {
6675 buf[mb_char2bytes(c2, buf)] = 0;
6676 hi = hash_find(&slang->sl_map_hash, buf);
6677 if (HASHITEM_EMPTY(hi))
6678 m2 = 0;
6679 else
6680 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6681 }
6682 else
6683#endif
6684 m2 = slang->sl_map_array[c2];
6685
6686 return m1 == m2;
6687}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006688
6689/*
6690 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006691 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006692 */
6693 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006694add_suggestion(
6695 suginfo_T *su,
6696 garray_T *gap, /* either su_ga or su_sga */
6697 char_u *goodword,
6698 int badlenarg, /* len of bad word replaced with "goodword" */
6699 int score,
6700 int altscore,
6701 int had_bonus, /* value for st_had_bonus */
6702 slang_T *slang, /* language for sound folding */
6703 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006704 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006706 int goodlen; /* len of goodword changed */
6707 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006709 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006710 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006711 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006712
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006713 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6714 * "thee the" is added next to changing the first "the" the "thee". */
6715 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006716 pbad = su->su_badptr + badlenarg;
6717 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006718 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006719 goodlen = (int)(pgood - goodword);
6720 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006721 if (goodlen <= 0 || badlen <= 0)
6722 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006723 MB_PTR_BACK(goodword, pgood);
6724 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006725#ifdef FEAT_MBYTE
6726 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006727 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006728 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6729 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006730 }
6731 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006732#endif
6733 if (*pgood != *pbad)
6734 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006735 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006736
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006737 if (badlen == 0 && goodlen == 0)
6738 /* goodword doesn't change anything; may happen for "the the" changing
6739 * the first "the" to itself. */
6740 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006741
Bram Moolenaar89d40322006-08-29 15:30:07 +00006742 if (gap->ga_len == 0)
6743 i = -1;
6744 else
6745 {
6746 /* Check if the word is already there. Also check the length that is
6747 * being replaced "thes," -> "these" is a different suggestion from
6748 * "thes" -> "these". */
6749 stp = &SUG(*gap, 0);
6750 for (i = gap->ga_len; --i >= 0; ++stp)
6751 if (stp->st_wordlen == goodlen
6752 && stp->st_orglen == badlen
6753 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006754 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006755 /*
6756 * Found it. Remember the word with the lowest score.
6757 */
6758 if (stp->st_slang == NULL)
6759 stp->st_slang = slang;
6760
6761 new_sug.st_score = score;
6762 new_sug.st_altscore = altscore;
6763 new_sug.st_had_bonus = had_bonus;
6764
6765 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006766 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006767 /* Only one of the two had the soundalike score computed.
6768 * Need to do that for the other one now, otherwise the
6769 * scores can't be compared. This happens because
6770 * suggest_try_change() doesn't compute the soundalike
6771 * word to keep it fast, while some special methods set
6772 * the soundalike score to zero. */
6773 if (had_bonus)
6774 rescore_one(su, stp);
6775 else
6776 {
6777 new_sug.st_word = stp->st_word;
6778 new_sug.st_wordlen = stp->st_wordlen;
6779 new_sug.st_slang = stp->st_slang;
6780 new_sug.st_orglen = badlen;
6781 rescore_one(su, &new_sug);
6782 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006784
Bram Moolenaar89d40322006-08-29 15:30:07 +00006785 if (stp->st_score > new_sug.st_score)
6786 {
6787 stp->st_score = new_sug.st_score;
6788 stp->st_altscore = new_sug.st_altscore;
6789 stp->st_had_bonus = new_sug.st_had_bonus;
6790 }
6791 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006792 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006794
Bram Moolenaar4770d092006-01-12 23:22:24 +00006795 if (i < 0 && ga_grow(gap, 1) == OK)
6796 {
6797 /* Add a suggestion. */
6798 stp = &SUG(*gap, gap->ga_len);
6799 stp->st_word = vim_strnsave(goodword, goodlen);
6800 if (stp->st_word != NULL)
6801 {
6802 stp->st_wordlen = goodlen;
6803 stp->st_score = score;
6804 stp->st_altscore = altscore;
6805 stp->st_had_bonus = had_bonus;
6806 stp->st_orglen = badlen;
6807 stp->st_slang = slang;
6808 ++gap->ga_len;
6809
6810 /* If we have too many suggestions now, sort the list and keep
6811 * the best suggestions. */
6812 if (gap->ga_len > SUG_MAX_COUNT(su))
6813 {
6814 if (maxsf)
6815 su->su_sfmaxscore = cleanup_suggestions(gap,
6816 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6817 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006818 su->su_maxscore = cleanup_suggestions(gap,
6819 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006820 }
6821 }
6822 }
6823}
6824
6825/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006826 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6827 * for split words, such as "the the". Remove these from the list here.
6828 */
6829 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006830check_suggestions(
6831 suginfo_T *su,
6832 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006833{
6834 suggest_T *stp;
6835 int i;
6836 char_u longword[MAXWLEN + 1];
6837 int len;
6838 hlf_T attr;
6839
6840 stp = &SUG(*gap, 0);
6841 for (i = gap->ga_len - 1; i >= 0; --i)
6842 {
6843 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006844 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006845 len = stp[i].st_wordlen;
6846 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6847 MAXWLEN - len);
6848 attr = HLF_COUNT;
6849 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6850 if (attr != HLF_COUNT)
6851 {
6852 /* Remove this entry. */
6853 vim_free(stp[i].st_word);
6854 --gap->ga_len;
6855 if (i < gap->ga_len)
6856 mch_memmove(stp + i, stp + i + 1,
6857 sizeof(suggest_T) * (gap->ga_len - i));
6858 }
6859 }
6860}
6861
6862
6863/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006864 * Add a word to be banned.
6865 */
6866 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006867add_banned(
6868 suginfo_T *su,
6869 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006870{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006871 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006872 hash_T hash;
6873 hashitem_T *hi;
6874
Bram Moolenaar4770d092006-01-12 23:22:24 +00006875 hash = hash_hash(word);
6876 hi = hash_lookup(&su->su_banned, word, hash);
6877 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006878 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006879 s = vim_strsave(word);
6880 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006881 hash_add_item(&su->su_banned, hi, s, hash);
6882 }
6883}
6884
6885/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006886 * Recompute the score for all suggestions if sound-folding is possible. This
6887 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006888 */
6889 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006890rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006891{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006892 int i;
6893
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006894 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006895 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006896 rescore_one(su, &SUG(su->su_ga, i));
6897}
6898
6899/*
6900 * Recompute the score for one suggestion if sound-folding is possible.
6901 */
6902 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006903rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006904{
6905 slang_T *slang = stp->st_slang;
6906 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006907 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006908
6909 /* Only rescore suggestions that have no sal score yet and do have a
6910 * language. */
6911 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6912 {
6913 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006914 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006915 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006916 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006917 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006918 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006919 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006920
6921 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006922 if (stp->st_altscore == SCORE_MAXMAX)
6923 stp->st_altscore = SCORE_BIG;
6924 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6925 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006926 }
6927}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006929static int
6930#ifdef __BORLANDC__
6931_RTLENTRYF
6932#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006933sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006934
6935/*
6936 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006937 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006938 */
6939 static int
6940#ifdef __BORLANDC__
6941_RTLENTRYF
6942#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006943sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006944{
6945 suggest_T *p1 = (suggest_T *)s1;
6946 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006947 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006948
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006949 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006950 {
6951 n = p1->st_altscore - p2->st_altscore;
6952 if (n == 0)
6953 n = STRICMP(p1->st_word, p2->st_word);
6954 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006955 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006956}
6957
6958/*
6959 * Cleanup the suggestions:
6960 * - Sort on score.
6961 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006962 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006963 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006964 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006965cleanup_suggestions(
6966 garray_T *gap,
6967 int maxscore,
6968 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006969{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006970 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006971 int i;
6972
6973 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006974 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006975
6976 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006977 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006978 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006979 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006980 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006981 gap->ga_len = keep;
6982 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006983 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006984 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006985}
6986
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006987#if defined(FEAT_EVAL) || defined(PROTO)
6988/*
6989 * Soundfold a string, for soundfold().
6990 * Result is in allocated memory, NULL for an error.
6991 */
6992 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006993eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006994{
6995 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006996 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006997 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006998
Bram Moolenaar860cae12010-06-05 23:22:07 +02006999 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007000 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02007001 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007002 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02007003 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007004 if (lp->lp_slang->sl_sal.ga_len > 0)
7005 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007006 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007007 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007008 return vim_strsave(sound);
7009 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007010 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007011
7012 /* No language with sound folding, return word as-is. */
7013 return vim_strsave(word);
7014}
7015#endif
7016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007017/*
7018 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00007019 *
7020 * There are many ways to turn a word into a sound-a-like representation. The
7021 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
7022 * swedish name matching - survey and test of different algorithms" by Klas
7023 * Erikson.
7024 *
7025 * We support two methods:
7026 * 1. SOFOFROM/SOFOTO do a simple character mapping.
7027 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007028 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02007029 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007030spell_soundfold(
7031 slang_T *slang,
7032 char_u *inword,
7033 int folded, /* "inword" is already case-folded */
7034 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007035{
7036 char_u fword[MAXWLEN];
7037 char_u *word;
7038
7039 if (slang->sl_sofo)
7040 /* SOFOFROM and SOFOTO used */
7041 spell_soundfold_sofo(slang, inword, res);
7042 else
7043 {
7044 /* SAL items used. Requires the word to be case-folded. */
7045 if (folded)
7046 word = inword;
7047 else
7048 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007049 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007050 word = fword;
7051 }
7052
7053#ifdef FEAT_MBYTE
7054 if (has_mbyte)
7055 spell_soundfold_wsal(slang, word, res);
7056 else
7057#endif
7058 spell_soundfold_sal(slang, word, res);
7059 }
7060}
7061
7062/*
7063 * Perform sound folding of "inword" into "res" according to SOFOFROM and
7064 * SOFOTO lines.
7065 */
7066 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007067spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007068{
7069 char_u *s;
7070 int ri = 0;
7071 int c;
7072
7073#ifdef FEAT_MBYTE
7074 if (has_mbyte)
7075 {
7076 int prevc = 0;
7077 int *ip;
7078
7079 /* The sl_sal_first[] table contains the translation for chars up to
7080 * 255, sl_sal the rest. */
7081 for (s = inword; *s != NUL; )
7082 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007083 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007084 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007085 c = ' ';
7086 else if (c < 256)
7087 c = slang->sl_sal_first[c];
7088 else
7089 {
7090 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
7091 if (ip == NULL) /* empty list, can't match */
7092 c = NUL;
7093 else
7094 for (;;) /* find "c" in the list */
7095 {
7096 if (*ip == 0) /* not found */
7097 {
7098 c = NUL;
7099 break;
7100 }
7101 if (*ip == c) /* match! */
7102 {
7103 c = ip[1];
7104 break;
7105 }
7106 ip += 2;
7107 }
7108 }
7109
7110 if (c != NUL && c != prevc)
7111 {
7112 ri += mb_char2bytes(c, res + ri);
7113 if (ri + MB_MAXBYTES > MAXWLEN)
7114 break;
7115 prevc = c;
7116 }
7117 }
7118 }
7119 else
7120#endif
7121 {
7122 /* The sl_sal_first[] table contains the translation. */
7123 for (s = inword; (c = *s) != NUL; ++s)
7124 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007125 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007126 c = ' ';
7127 else
7128 c = slang->sl_sal_first[c];
7129 if (c != NUL && (ri == 0 || res[ri - 1] != c))
7130 res[ri++] = c;
7131 }
7132 }
7133
7134 res[ri] = NUL;
7135}
7136
7137 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007138spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007139{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007140 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007141 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007142 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007143 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007144 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007145 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007146 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007147 int n, k = 0;
7148 int z0;
7149 int k0;
7150 int n0;
7151 int c;
7152 int pri;
7153 int p0 = -333;
7154 int c0;
7155
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007156 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007157 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158 if (slang->sl_rem_accents)
7159 {
7160 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007161 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007163 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007164 {
7165 *t++ = ' ';
7166 s = skipwhite(s);
7167 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007168 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007169 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007170 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007171 *t++ = *s;
7172 ++s;
7173 }
7174 }
7175 *t = NUL;
7176 }
7177 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007178 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007179
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007180 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007181
7182 /*
7183 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007184 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007185 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007186 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007187 while ((c = word[i]) != NUL)
7188 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007189 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007190 n = slang->sl_sal_first[c];
7191 z0 = 0;
7192
7193 if (n >= 0)
7194 {
7195 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007196 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007197 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007198 /* Quickly skip entries that don't match the word. Most
7199 * entries are less then three chars, optimize for that. */
7200 k = smp[n].sm_leadlen;
7201 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007203 if (word[i + 1] != s[1])
7204 continue;
7205 if (k > 2)
7206 {
7207 for (j = 2; j < k; ++j)
7208 if (word[i + j] != s[j])
7209 break;
7210 if (j < k)
7211 continue;
7212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007213 }
7214
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007215 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007216 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007217 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007218 while (*pf != NUL && *pf != word[i + k])
7219 ++pf;
7220 if (*pf == NUL)
7221 continue;
7222 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007223 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007224 s = smp[n].sm_rules;
7225 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007226
7227 p0 = *s;
7228 k0 = k;
7229 while (*s == '-' && k > 1)
7230 {
7231 k--;
7232 s++;
7233 }
7234 if (*s == '<')
7235 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007236 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237 {
7238 /* determine priority */
7239 pri = *s - '0';
7240 s++;
7241 }
7242 if (*s == '^' && *(s + 1) == '^')
7243 s++;
7244
7245 if (*s == NUL
7246 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007247 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007248 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007249 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007250 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007251 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007252 && spell_iswordp(word + i - 1, curwin)
7253 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007254 {
7255 /* search for followup rules, if: */
7256 /* followup and k > 1 and NO '-' in searchstring */
7257 c0 = word[i + k - 1];
7258 n0 = slang->sl_sal_first[c0];
7259
7260 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007261 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007262 {
7263 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007264 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007265 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007266 /* Quickly skip entries that don't match the word.
7267 * */
7268 k0 = smp[n0].sm_leadlen;
7269 if (k0 > 1)
7270 {
7271 if (word[i + k] != s[1])
7272 continue;
7273 if (k0 > 2)
7274 {
7275 pf = word + i + k + 1;
7276 for (j = 2; j < k0; ++j)
7277 if (*pf++ != s[j])
7278 break;
7279 if (j < k0)
7280 continue;
7281 }
7282 }
7283 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007284
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007285 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007286 {
7287 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007288 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007289 while (*pf != NUL && *pf != word[i + k0])
7290 ++pf;
7291 if (*pf == NUL)
7292 continue;
7293 ++k0;
7294 }
7295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007296 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007297 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007298 while (*s == '-')
7299 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007300 /* "k0" gets NOT reduced because
7301 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007302 s++;
7303 }
7304 if (*s == '<')
7305 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007306 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007307 {
7308 p0 = *s - '0';
7309 s++;
7310 }
7311
7312 if (*s == NUL
7313 /* *s == '^' cuts */
7314 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007315 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007316 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 {
7318 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007320 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007321
7322 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007323 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007324 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007325 /* rule fits; stop search */
7326 break;
7327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 }
7329
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007330 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007331 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007332 }
7333
7334 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007335 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007336 if (s == NULL)
7337 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007338 pf = smp[n].sm_rules;
7339 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 if (p0 == 1 && z == 0)
7341 {
7342 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007343 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7344 || res[reslen - 1] == *s))
7345 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007346 z0 = 1;
7347 z = 1;
7348 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007349 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007350 {
7351 word[i + k0] = *s;
7352 k0++;
7353 s++;
7354 }
7355 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007356 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007357
7358 /* new "actual letter" */
7359 c = word[i];
7360 }
7361 else
7362 {
7363 /* no '<' rule used */
7364 i += k - 1;
7365 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007366 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007367 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007368 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007369 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007370 s++;
7371 }
7372 /* new "actual letter" */
7373 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007374 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375 {
7376 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007377 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007378 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007379 i = 0;
7380 z0 = 1;
7381 }
7382 }
7383 break;
7384 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007385 }
7386 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007387 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007388 {
7389 c = ' ';
7390 k = 1;
7391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392
7393 if (z0 == 0)
7394 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007395 if (k && !p0 && reslen < MAXWLEN && c != NUL
7396 && (!slang->sl_collapse || reslen == 0
7397 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007398 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007399 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007400
7401 i++;
7402 z = 0;
7403 k = 0;
7404 }
7405 }
7406
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007407 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007408}
7409
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007410#ifdef FEAT_MBYTE
7411/*
7412 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7413 * Multi-byte version of spell_soundfold().
7414 */
7415 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007416spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007417{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007418 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007419 int word[MAXWLEN];
7420 int wres[MAXWLEN];
7421 int l;
7422 char_u *s;
7423 int *ws;
7424 char_u *t;
7425 int *pf;
7426 int i, j, z;
7427 int reslen;
7428 int n, k = 0;
7429 int z0;
7430 int k0;
7431 int n0;
7432 int c;
7433 int pri;
7434 int p0 = -333;
7435 int c0;
7436 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007437 int wordlen;
7438
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007439
7440 /*
7441 * Convert the multi-byte string to a wide-character string.
7442 * Remove accents, if wanted. We actually remove all non-word characters.
7443 * But keep white space.
7444 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007445 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007446 for (s = inword; *s != NUL; )
7447 {
7448 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007449 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007450 if (slang->sl_rem_accents)
7451 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007452 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007453 {
7454 if (did_white)
7455 continue;
7456 c = ' ';
7457 did_white = TRUE;
7458 }
7459 else
7460 {
7461 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007462 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007463 continue;
7464 }
7465 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007466 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007467 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007468 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007469
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007470 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007471 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007472 * Converted from C++ to C. Added support for multi-byte chars.
7473 * Changed to keep spaces.
7474 */
7475 i = reslen = z = 0;
7476 while ((c = word[i]) != NUL)
7477 {
7478 /* Start with the first rule that has the character in the word. */
7479 n = slang->sl_sal_first[c & 0xff];
7480 z0 = 0;
7481
7482 if (n >= 0)
7483 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007484 /* Check all rules for the same index byte.
7485 * If c is 0x300 need extra check for the end of the array, as
7486 * (c & 0xff) is NUL. */
7487 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7488 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007489 {
7490 /* Quickly skip entries that don't match the word. Most
7491 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007492 if (c != ws[0])
7493 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007494 k = smp[n].sm_leadlen;
7495 if (k > 1)
7496 {
7497 if (word[i + 1] != ws[1])
7498 continue;
7499 if (k > 2)
7500 {
7501 for (j = 2; j < k; ++j)
7502 if (word[i + j] != ws[j])
7503 break;
7504 if (j < k)
7505 continue;
7506 }
7507 }
7508
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007509 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007510 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007511 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007512 while (*pf != NUL && *pf != word[i + k])
7513 ++pf;
7514 if (*pf == NUL)
7515 continue;
7516 ++k;
7517 }
7518 s = smp[n].sm_rules;
7519 pri = 5; /* default priority */
7520
7521 p0 = *s;
7522 k0 = k;
7523 while (*s == '-' && k > 1)
7524 {
7525 k--;
7526 s++;
7527 }
7528 if (*s == '<')
7529 s++;
7530 if (VIM_ISDIGIT(*s))
7531 {
7532 /* determine priority */
7533 pri = *s - '0';
7534 s++;
7535 }
7536 if (*s == '^' && *(s + 1) == '^')
7537 s++;
7538
7539 if (*s == NUL
7540 || (*s == '^'
7541 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007542 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007543 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007544 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007545 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007546 && spell_iswordp_w(word + i - 1, curwin)
7547 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007548 {
7549 /* search for followup rules, if: */
7550 /* followup and k > 1 and NO '-' in searchstring */
7551 c0 = word[i + k - 1];
7552 n0 = slang->sl_sal_first[c0 & 0xff];
7553
7554 if (slang->sl_followup && k > 1 && n0 >= 0
7555 && p0 != '-' && word[i + k] != NUL)
7556 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007557 /* Test follow-up rule for "word[i + k]"; loop over
7558 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007559 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7560 == (c0 & 0xff); ++n0)
7561 {
7562 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007563 */
7564 if (c0 != ws[0])
7565 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007566 k0 = smp[n0].sm_leadlen;
7567 if (k0 > 1)
7568 {
7569 if (word[i + k] != ws[1])
7570 continue;
7571 if (k0 > 2)
7572 {
7573 pf = word + i + k + 1;
7574 for (j = 2; j < k0; ++j)
7575 if (*pf++ != ws[j])
7576 break;
7577 if (j < k0)
7578 continue;
7579 }
7580 }
7581 k0 += k - 1;
7582
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007583 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007584 {
7585 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007586 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007587 while (*pf != NUL && *pf != word[i + k0])
7588 ++pf;
7589 if (*pf == NUL)
7590 continue;
7591 ++k0;
7592 }
7593
7594 p0 = 5;
7595 s = smp[n0].sm_rules;
7596 while (*s == '-')
7597 {
7598 /* "k0" gets NOT reduced because
7599 * "if (k0 == k)" */
7600 s++;
7601 }
7602 if (*s == '<')
7603 s++;
7604 if (VIM_ISDIGIT(*s))
7605 {
7606 p0 = *s - '0';
7607 s++;
7608 }
7609
7610 if (*s == NUL
7611 /* *s == '^' cuts */
7612 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007613 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007614 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007615 {
7616 if (k0 == k)
7617 /* this is just a piece of the string */
7618 continue;
7619
7620 if (p0 < pri)
7621 /* priority too low */
7622 continue;
7623 /* rule fits; stop search */
7624 break;
7625 }
7626 }
7627
7628 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7629 == (c0 & 0xff))
7630 continue;
7631 }
7632
7633 /* replace string */
7634 ws = smp[n].sm_to_w;
7635 s = smp[n].sm_rules;
7636 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7637 if (p0 == 1 && z == 0)
7638 {
7639 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007640 if (reslen > 0 && ws != NULL && *ws != NUL
7641 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007642 || wres[reslen - 1] == *ws))
7643 reslen--;
7644 z0 = 1;
7645 z = 1;
7646 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007647 if (ws != NULL)
7648 while (*ws != NUL && word[i + k0] != NUL)
7649 {
7650 word[i + k0] = *ws;
7651 k0++;
7652 ws++;
7653 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007654 if (k > k0)
7655 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007656 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007657
7658 /* new "actual letter" */
7659 c = word[i];
7660 }
7661 else
7662 {
7663 /* no '<' rule used */
7664 i += k - 1;
7665 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007666 if (ws != NULL)
7667 while (*ws != NUL && ws[1] != NUL
7668 && reslen < MAXWLEN)
7669 {
7670 if (reslen == 0 || wres[reslen - 1] != *ws)
7671 wres[reslen++] = *ws;
7672 ws++;
7673 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007674 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007675 if (ws == NULL)
7676 c = NUL;
7677 else
7678 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007679 if (strstr((char *)s, "^^") != NULL)
7680 {
7681 if (c != NUL)
7682 wres[reslen++] = c;
7683 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007684 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007685 i = 0;
7686 z0 = 1;
7687 }
7688 }
7689 break;
7690 }
7691 }
7692 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007693 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007694 {
7695 c = ' ';
7696 k = 1;
7697 }
7698
7699 if (z0 == 0)
7700 {
7701 if (k && !p0 && reslen < MAXWLEN && c != NUL
7702 && (!slang->sl_collapse || reslen == 0
7703 || wres[reslen - 1] != c))
7704 /* condense only double letters */
7705 wres[reslen++] = c;
7706
7707 i++;
7708 z = 0;
7709 k = 0;
7710 }
7711 }
7712
7713 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7714 l = 0;
7715 for (n = 0; n < reslen; ++n)
7716 {
7717 l += mb_char2bytes(wres[n], res + l);
7718 if (l + MB_MAXBYTES > MAXWLEN)
7719 break;
7720 }
7721 res[l] = NUL;
7722}
7723#endif
7724
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007725/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007726 * Compute a score for two sound-a-like words.
7727 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7728 * Instead of a generic loop we write out the code. That keeps it fast by
7729 * avoiding checks that will not be possible.
7730 */
7731 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007732soundalike_score(
7733 char_u *goodstart, /* sound-folded good word */
7734 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007735{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007736 char_u *goodsound = goodstart;
7737 char_u *badsound = badstart;
7738 int goodlen;
7739 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007740 int n;
7741 char_u *pl, *ps;
7742 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007743 int score = 0;
7744
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007745 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007746 * counted so much, vowels halfway the word aren't counted at all. */
7747 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7748 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007749 if ((badsound[0] == NUL && goodsound[1] == NUL)
7750 || (goodsound[0] == NUL && badsound[1] == NUL))
7751 /* changing word with vowel to word without a sound */
7752 return SCORE_DEL;
7753 if (badsound[0] == NUL || goodsound[0] == NUL)
7754 /* more than two changes */
7755 return SCORE_MAXMAX;
7756
Bram Moolenaar4770d092006-01-12 23:22:24 +00007757 if (badsound[1] == goodsound[1]
7758 || (badsound[1] != NUL
7759 && goodsound[1] != NUL
7760 && badsound[2] == goodsound[2]))
7761 {
7762 /* handle like a substitute */
7763 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007764 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007765 {
7766 score = 2 * SCORE_DEL / 3;
7767 if (*badsound == '*')
7768 ++badsound;
7769 else
7770 ++goodsound;
7771 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007772 }
7773
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007774 goodlen = (int)STRLEN(goodsound);
7775 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007776
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007777 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007778 * changes. */
7779 n = goodlen - badlen;
7780 if (n < -2 || n > 2)
7781 return SCORE_MAXMAX;
7782
7783 if (n > 0)
7784 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007785 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007786 ps = badsound;
7787 }
7788 else
7789 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007790 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007791 ps = goodsound;
7792 }
7793
7794 /* Skip over the identical part. */
7795 while (*pl == *ps && *pl != NUL)
7796 {
7797 ++pl;
7798 ++ps;
7799 }
7800
7801 switch (n)
7802 {
7803 case -2:
7804 case 2:
7805 /*
7806 * Must delete two characters from "pl".
7807 */
7808 ++pl; /* first delete */
7809 while (*pl == *ps)
7810 {
7811 ++pl;
7812 ++ps;
7813 }
7814 /* strings must be equal after second delete */
7815 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007816 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007817
7818 /* Failed to compare. */
7819 break;
7820
7821 case -1:
7822 case 1:
7823 /*
7824 * Minimal one delete from "pl" required.
7825 */
7826
7827 /* 1: delete */
7828 pl2 = pl + 1;
7829 ps2 = ps;
7830 while (*pl2 == *ps2)
7831 {
7832 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007833 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007834 ++pl2;
7835 ++ps2;
7836 }
7837
7838 /* 2: delete then swap, then rest must be equal */
7839 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7840 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007841 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007842
7843 /* 3: delete then substitute, then the rest must be equal */
7844 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007845 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007846
7847 /* 4: first swap then delete */
7848 if (pl[0] == ps[1] && pl[1] == ps[0])
7849 {
7850 pl2 = pl + 2; /* swap, skip two chars */
7851 ps2 = ps + 2;
7852 while (*pl2 == *ps2)
7853 {
7854 ++pl2;
7855 ++ps2;
7856 }
7857 /* delete a char and then strings must be equal */
7858 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007859 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007860 }
7861
7862 /* 5: first substitute then delete */
7863 pl2 = pl + 1; /* substitute, skip one char */
7864 ps2 = ps + 1;
7865 while (*pl2 == *ps2)
7866 {
7867 ++pl2;
7868 ++ps2;
7869 }
7870 /* delete a char and then strings must be equal */
7871 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007872 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007873
7874 /* Failed to compare. */
7875 break;
7876
7877 case 0:
7878 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007879 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007880 * insert is only possible in combination with a delete.
7881 * 1: check if for identical strings
7882 */
7883 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007884 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007885
7886 /* 2: swap */
7887 if (pl[0] == ps[1] && pl[1] == ps[0])
7888 {
7889 pl2 = pl + 2; /* swap, skip two chars */
7890 ps2 = ps + 2;
7891 while (*pl2 == *ps2)
7892 {
7893 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007894 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007895 ++pl2;
7896 ++ps2;
7897 }
7898 /* 3: swap and swap again */
7899 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7900 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007901 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007902
7903 /* 4: swap and substitute */
7904 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007905 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007906 }
7907
7908 /* 5: substitute */
7909 pl2 = pl + 1;
7910 ps2 = ps + 1;
7911 while (*pl2 == *ps2)
7912 {
7913 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007914 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007915 ++pl2;
7916 ++ps2;
7917 }
7918
7919 /* 6: substitute and swap */
7920 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7921 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007922 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007923
7924 /* 7: substitute and substitute */
7925 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007926 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007927
7928 /* 8: insert then delete */
7929 pl2 = pl;
7930 ps2 = ps + 1;
7931 while (*pl2 == *ps2)
7932 {
7933 ++pl2;
7934 ++ps2;
7935 }
7936 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007937 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007938
7939 /* 9: delete then insert */
7940 pl2 = pl + 1;
7941 ps2 = ps;
7942 while (*pl2 == *ps2)
7943 {
7944 ++pl2;
7945 ++ps2;
7946 }
7947 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007948 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007949
7950 /* Failed to compare. */
7951 break;
7952 }
7953
7954 return SCORE_MAXMAX;
7955}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007957/*
7958 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007959 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007960 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007961 * The algorithm is described by Du and Chang, 1992.
7962 * The implementation of the algorithm comes from Aspell editdist.cpp,
7963 * edit_distance(). It has been converted from C++ to C and modified to
7964 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007965 */
7966 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007967spell_edit_score(
7968 slang_T *slang,
7969 char_u *badword,
7970 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007971{
7972 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007973 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007974 int j, i;
7975 int t;
7976 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007977 int pbc, pgc;
7978#ifdef FEAT_MBYTE
7979 char_u *p;
7980 int wbadword[MAXWLEN];
7981 int wgoodword[MAXWLEN];
7982
7983 if (has_mbyte)
7984 {
7985 /* Get the characters from the multi-byte strings and put them in an
7986 * int array for easy access. */
7987 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007988 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007989 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007990 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007991 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007992 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007993 }
7994 else
7995#endif
7996 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007997 badlen = (int)STRLEN(badword) + 1;
7998 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007999 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008000
8001 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
8002#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008003 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
8004 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008005 if (cnt == NULL)
8006 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008007
8008 CNT(0, 0) = 0;
8009 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008010 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008011
8012 for (i = 1; i <= badlen; ++i)
8013 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00008014 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008015 for (j = 1; j <= goodlen; ++j)
8016 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008017#ifdef FEAT_MBYTE
8018 if (has_mbyte)
8019 {
8020 bc = wbadword[i - 1];
8021 gc = wgoodword[j - 1];
8022 }
8023 else
8024#endif
8025 {
8026 bc = badword[i - 1];
8027 gc = goodword[j - 1];
8028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008029 if (bc == gc)
8030 CNT(i, j) = CNT(i - 1, j - 1);
8031 else
8032 {
8033 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008034 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008035 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
8036 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008037 {
8038 /* For a similar character use SCORE_SIMILAR. */
8039 if (slang != NULL
8040 && slang->sl_has_map
8041 && similar_chars(slang, gc, bc))
8042 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
8043 else
8044 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
8045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008047 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008049#ifdef FEAT_MBYTE
8050 if (has_mbyte)
8051 {
8052 pbc = wbadword[i - 2];
8053 pgc = wgoodword[j - 2];
8054 }
8055 else
8056#endif
8057 {
8058 pbc = badword[i - 2];
8059 pgc = goodword[j - 2];
8060 }
8061 if (bc == pgc && pbc == gc)
8062 {
8063 t = SCORE_SWAP + CNT(i - 2, j - 2);
8064 if (t < CNT(i, j))
8065 CNT(i, j) = t;
8066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067 }
8068 t = SCORE_DEL + CNT(i - 1, j);
8069 if (t < CNT(i, j))
8070 CNT(i, j) = t;
8071 t = SCORE_INS + CNT(i, j - 1);
8072 if (t < CNT(i, j))
8073 CNT(i, j) = t;
8074 }
8075 }
8076 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008077
8078 i = CNT(badlen - 1, goodlen - 1);
8079 vim_free(cnt);
8080 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008082
Bram Moolenaar4770d092006-01-12 23:22:24 +00008083typedef struct
8084{
8085 int badi;
8086 int goodi;
8087 int score;
8088} limitscore_T;
8089
8090/*
8091 * Like spell_edit_score(), but with a limit on the score to make it faster.
8092 * May return SCORE_MAXMAX when the score is higher than "limit".
8093 *
8094 * This uses a stack for the edits still to be tried.
8095 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
8096 * for multi-byte characters.
8097 */
8098 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008099spell_edit_score_limit(
8100 slang_T *slang,
8101 char_u *badword,
8102 char_u *goodword,
8103 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008104{
8105 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8106 int stackidx;
8107 int bi, gi;
8108 int bi2, gi2;
8109 int bc, gc;
8110 int score;
8111 int score_off;
8112 int minscore;
8113 int round;
8114
8115#ifdef FEAT_MBYTE
8116 /* Multi-byte characters require a bit more work, use a different function
8117 * to avoid testing "has_mbyte" quite often. */
8118 if (has_mbyte)
8119 return spell_edit_score_limit_w(slang, badword, goodword, limit);
8120#endif
8121
8122 /*
8123 * The idea is to go from start to end over the words. So long as
8124 * characters are equal just continue, this always gives the lowest score.
8125 * When there is a difference try several alternatives. Each alternative
8126 * increases "score" for the edit distance. Some of the alternatives are
8127 * pushed unto a stack and tried later, some are tried right away. At the
8128 * end of the word the score for one alternative is known. The lowest
8129 * possible score is stored in "minscore".
8130 */
8131 stackidx = 0;
8132 bi = 0;
8133 gi = 0;
8134 score = 0;
8135 minscore = limit + 1;
8136
8137 for (;;)
8138 {
8139 /* Skip over an equal part, score remains the same. */
8140 for (;;)
8141 {
8142 bc = badword[bi];
8143 gc = goodword[gi];
8144 if (bc != gc) /* stop at a char that's different */
8145 break;
8146 if (bc == NUL) /* both words end */
8147 {
8148 if (score < minscore)
8149 minscore = score;
8150 goto pop; /* do next alternative */
8151 }
8152 ++bi;
8153 ++gi;
8154 }
8155
8156 if (gc == NUL) /* goodword ends, delete badword chars */
8157 {
8158 do
8159 {
8160 if ((score += SCORE_DEL) >= minscore)
8161 goto pop; /* do next alternative */
8162 } while (badword[++bi] != NUL);
8163 minscore = score;
8164 }
8165 else if (bc == NUL) /* badword ends, insert badword chars */
8166 {
8167 do
8168 {
8169 if ((score += SCORE_INS) >= minscore)
8170 goto pop; /* do next alternative */
8171 } while (goodword[++gi] != NUL);
8172 minscore = score;
8173 }
8174 else /* both words continue */
8175 {
8176 /* If not close to the limit, perform a change. Only try changes
8177 * that may lead to a lower score than "minscore".
8178 * round 0: try deleting a char from badword
8179 * round 1: try inserting a char in badword */
8180 for (round = 0; round <= 1; ++round)
8181 {
8182 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8183 if (score_off < minscore)
8184 {
8185 if (score_off + SCORE_EDIT_MIN >= minscore)
8186 {
8187 /* Near the limit, rest of the words must match. We
8188 * can check that right now, no need to push an item
8189 * onto the stack. */
8190 bi2 = bi + 1 - round;
8191 gi2 = gi + round;
8192 while (goodword[gi2] == badword[bi2])
8193 {
8194 if (goodword[gi2] == NUL)
8195 {
8196 minscore = score_off;
8197 break;
8198 }
8199 ++bi2;
8200 ++gi2;
8201 }
8202 }
8203 else
8204 {
8205 /* try deleting/inserting a character later */
8206 stack[stackidx].badi = bi + 1 - round;
8207 stack[stackidx].goodi = gi + round;
8208 stack[stackidx].score = score_off;
8209 ++stackidx;
8210 }
8211 }
8212 }
8213
8214 if (score + SCORE_SWAP < minscore)
8215 {
8216 /* If swapping two characters makes a match then the
8217 * substitution is more expensive, thus there is no need to
8218 * try both. */
8219 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8220 {
8221 /* Swap two characters, that is: skip them. */
8222 gi += 2;
8223 bi += 2;
8224 score += SCORE_SWAP;
8225 continue;
8226 }
8227 }
8228
8229 /* Substitute one character for another which is the same
8230 * thing as deleting a character from both goodword and badword.
8231 * Use a better score when there is only a case difference. */
8232 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8233 score += SCORE_ICASE;
8234 else
8235 {
8236 /* For a similar character use SCORE_SIMILAR. */
8237 if (slang != NULL
8238 && slang->sl_has_map
8239 && similar_chars(slang, gc, bc))
8240 score += SCORE_SIMILAR;
8241 else
8242 score += SCORE_SUBST;
8243 }
8244
8245 if (score < minscore)
8246 {
8247 /* Do the substitution. */
8248 ++gi;
8249 ++bi;
8250 continue;
8251 }
8252 }
8253pop:
8254 /*
8255 * Get here to try the next alternative, pop it from the stack.
8256 */
8257 if (stackidx == 0) /* stack is empty, finished */
8258 break;
8259
8260 /* pop an item from the stack */
8261 --stackidx;
8262 gi = stack[stackidx].goodi;
8263 bi = stack[stackidx].badi;
8264 score = stack[stackidx].score;
8265 }
8266
8267 /* When the score goes over "limit" it may actually be much higher.
8268 * Return a very large number to avoid going below the limit when giving a
8269 * bonus. */
8270 if (minscore > limit)
8271 return SCORE_MAXMAX;
8272 return minscore;
8273}
8274
8275#ifdef FEAT_MBYTE
8276/*
8277 * Multi-byte version of spell_edit_score_limit().
8278 * Keep it in sync with the above!
8279 */
8280 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008281spell_edit_score_limit_w(
8282 slang_T *slang,
8283 char_u *badword,
8284 char_u *goodword,
8285 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008286{
8287 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8288 int stackidx;
8289 int bi, gi;
8290 int bi2, gi2;
8291 int bc, gc;
8292 int score;
8293 int score_off;
8294 int minscore;
8295 int round;
8296 char_u *p;
8297 int wbadword[MAXWLEN];
8298 int wgoodword[MAXWLEN];
8299
8300 /* Get the characters from the multi-byte strings and put them in an
8301 * int array for easy access. */
8302 bi = 0;
8303 for (p = badword; *p != NUL; )
8304 wbadword[bi++] = mb_cptr2char_adv(&p);
8305 wbadword[bi++] = 0;
8306 gi = 0;
8307 for (p = goodword; *p != NUL; )
8308 wgoodword[gi++] = mb_cptr2char_adv(&p);
8309 wgoodword[gi++] = 0;
8310
8311 /*
8312 * The idea is to go from start to end over the words. So long as
8313 * characters are equal just continue, this always gives the lowest score.
8314 * When there is a difference try several alternatives. Each alternative
8315 * increases "score" for the edit distance. Some of the alternatives are
8316 * pushed unto a stack and tried later, some are tried right away. At the
8317 * end of the word the score for one alternative is known. The lowest
8318 * possible score is stored in "minscore".
8319 */
8320 stackidx = 0;
8321 bi = 0;
8322 gi = 0;
8323 score = 0;
8324 minscore = limit + 1;
8325
8326 for (;;)
8327 {
8328 /* Skip over an equal part, score remains the same. */
8329 for (;;)
8330 {
8331 bc = wbadword[bi];
8332 gc = wgoodword[gi];
8333
8334 if (bc != gc) /* stop at a char that's different */
8335 break;
8336 if (bc == NUL) /* both words end */
8337 {
8338 if (score < minscore)
8339 minscore = score;
8340 goto pop; /* do next alternative */
8341 }
8342 ++bi;
8343 ++gi;
8344 }
8345
8346 if (gc == NUL) /* goodword ends, delete badword chars */
8347 {
8348 do
8349 {
8350 if ((score += SCORE_DEL) >= minscore)
8351 goto pop; /* do next alternative */
8352 } while (wbadword[++bi] != NUL);
8353 minscore = score;
8354 }
8355 else if (bc == NUL) /* badword ends, insert badword chars */
8356 {
8357 do
8358 {
8359 if ((score += SCORE_INS) >= minscore)
8360 goto pop; /* do next alternative */
8361 } while (wgoodword[++gi] != NUL);
8362 minscore = score;
8363 }
8364 else /* both words continue */
8365 {
8366 /* If not close to the limit, perform a change. Only try changes
8367 * that may lead to a lower score than "minscore".
8368 * round 0: try deleting a char from badword
8369 * round 1: try inserting a char in badword */
8370 for (round = 0; round <= 1; ++round)
8371 {
8372 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8373 if (score_off < minscore)
8374 {
8375 if (score_off + SCORE_EDIT_MIN >= minscore)
8376 {
8377 /* Near the limit, rest of the words must match. We
8378 * can check that right now, no need to push an item
8379 * onto the stack. */
8380 bi2 = bi + 1 - round;
8381 gi2 = gi + round;
8382 while (wgoodword[gi2] == wbadword[bi2])
8383 {
8384 if (wgoodword[gi2] == NUL)
8385 {
8386 minscore = score_off;
8387 break;
8388 }
8389 ++bi2;
8390 ++gi2;
8391 }
8392 }
8393 else
8394 {
8395 /* try deleting a character from badword later */
8396 stack[stackidx].badi = bi + 1 - round;
8397 stack[stackidx].goodi = gi + round;
8398 stack[stackidx].score = score_off;
8399 ++stackidx;
8400 }
8401 }
8402 }
8403
8404 if (score + SCORE_SWAP < minscore)
8405 {
8406 /* If swapping two characters makes a match then the
8407 * substitution is more expensive, thus there is no need to
8408 * try both. */
8409 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8410 {
8411 /* Swap two characters, that is: skip them. */
8412 gi += 2;
8413 bi += 2;
8414 score += SCORE_SWAP;
8415 continue;
8416 }
8417 }
8418
8419 /* Substitute one character for another which is the same
8420 * thing as deleting a character from both goodword and badword.
8421 * Use a better score when there is only a case difference. */
8422 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8423 score += SCORE_ICASE;
8424 else
8425 {
8426 /* For a similar character use SCORE_SIMILAR. */
8427 if (slang != NULL
8428 && slang->sl_has_map
8429 && similar_chars(slang, gc, bc))
8430 score += SCORE_SIMILAR;
8431 else
8432 score += SCORE_SUBST;
8433 }
8434
8435 if (score < minscore)
8436 {
8437 /* Do the substitution. */
8438 ++gi;
8439 ++bi;
8440 continue;
8441 }
8442 }
8443pop:
8444 /*
8445 * Get here to try the next alternative, pop it from the stack.
8446 */
8447 if (stackidx == 0) /* stack is empty, finished */
8448 break;
8449
8450 /* pop an item from the stack */
8451 --stackidx;
8452 gi = stack[stackidx].goodi;
8453 bi = stack[stackidx].badi;
8454 score = stack[stackidx].score;
8455 }
8456
8457 /* When the score goes over "limit" it may actually be much higher.
8458 * Return a very large number to avoid going below the limit when giving a
8459 * bonus. */
8460 if (minscore > limit)
8461 return SCORE_MAXMAX;
8462 return minscore;
8463}
8464#endif
8465
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008466/*
8467 * ":spellinfo"
8468 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008470ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008471{
8472 int lpi;
8473 langp_T *lp;
8474 char_u *p;
8475
8476 if (no_spell_checking(curwin))
8477 return;
8478
8479 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008480 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008481 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008482 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008483 msg_puts((char_u *)"file: ");
8484 msg_puts(lp->lp_slang->sl_fname);
8485 msg_putchar('\n');
8486 p = lp->lp_slang->sl_info;
8487 if (p != NULL)
8488 {
8489 msg_puts(p);
8490 msg_putchar('\n');
8491 }
8492 }
8493 msg_end();
8494}
8495
Bram Moolenaar4770d092006-01-12 23:22:24 +00008496#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8497#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008498#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008499#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8500#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008501
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008502/*
8503 * ":spelldump"
8504 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008505 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008506ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008507{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008508 char_u *spl;
8509 long dummy;
8510
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008511 if (no_spell_checking(curwin))
8512 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008513 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008514
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008515 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008516 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008517
8518 /* enable spelling locally in the new window */
8519 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008520 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008521 vim_free(spl);
8522
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008523 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008524 return;
8525
Bram Moolenaar860cae12010-06-05 23:22:07 +02008526 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008527
8528 /* Delete the empty line that we started with. */
8529 if (curbuf->b_ml.ml_line_count > 1)
8530 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8531
8532 redraw_later(NOT_VALID);
8533}
8534
8535/*
8536 * Go through all possible words and:
8537 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8538 * "ic" and "dir" are not used.
8539 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8540 */
8541 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008542spell_dump_compl(
8543 char_u *pat, /* leading part of the word */
8544 int ic, /* ignore case */
8545 int *dir, /* direction for adding matches */
8546 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008547{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008548 langp_T *lp;
8549 slang_T *slang;
8550 idx_T arridx[MAXWLEN];
8551 int curi[MAXWLEN];
8552 char_u word[MAXWLEN];
8553 int c;
8554 char_u *byts;
8555 idx_T *idxs;
8556 linenr_T lnum = 0;
8557 int round;
8558 int depth;
8559 int n;
8560 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008561 char_u *region_names = NULL; /* region names being used */
8562 int do_region = TRUE; /* dump region names and numbers */
8563 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008564 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008565 int dumpflags = dumpflags_arg;
8566 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008567
Bram Moolenaard0131a82006-03-04 21:46:13 +00008568 /* When ignoring case or when the pattern starts with capital pass this on
8569 * to dump_word(). */
8570 if (pat != NULL)
8571 {
8572 if (ic)
8573 dumpflags |= DUMPFLAG_ICASE;
8574 else
8575 {
8576 n = captype(pat, NULL);
8577 if (n == WF_ONECAP)
8578 dumpflags |= DUMPFLAG_ONECAP;
8579 else if (n == WF_ALLCAP
8580#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008581 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008582#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008583 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +00008584#endif
8585 )
8586 dumpflags |= DUMPFLAG_ALLCAP;
8587 }
8588 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008589
Bram Moolenaar7887d882005-07-01 22:33:52 +00008590 /* Find out if we can support regions: All languages must support the same
8591 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008592 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008593 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008594 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008595 p = lp->lp_slang->sl_regions;
8596 if (p[0] != 0)
8597 {
8598 if (region_names == NULL) /* first language with regions */
8599 region_names = p;
8600 else if (STRCMP(region_names, p) != 0)
8601 {
8602 do_region = FALSE; /* region names are different */
8603 break;
8604 }
8605 }
8606 }
8607
8608 if (do_region && region_names != NULL)
8609 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008610 if (pat == NULL)
8611 {
8612 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8613 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8614 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008615 }
8616 else
8617 do_region = FALSE;
8618
8619 /*
8620 * Loop over all files loaded for the entries in 'spelllang'.
8621 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008622 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008623 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008624 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008625 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008626 if (slang->sl_fbyts == NULL) /* reloading failed */
8627 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008628
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008629 if (pat == NULL)
8630 {
8631 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8632 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8633 }
8634
8635 /* When matching with a pattern and there are no prefixes only use
8636 * parts of the tree that match "pat". */
8637 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008638 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008639 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008640 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008641
8642 /* round 1: case-folded tree
8643 * round 2: keep-case tree */
8644 for (round = 1; round <= 2; ++round)
8645 {
8646 if (round == 1)
8647 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008648 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008649 byts = slang->sl_fbyts;
8650 idxs = slang->sl_fidxs;
8651 }
8652 else
8653 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008654 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008655 byts = slang->sl_kbyts;
8656 idxs = slang->sl_kidxs;
8657 }
8658 if (byts == NULL)
8659 continue; /* array is empty */
8660
8661 depth = 0;
8662 arridx[0] = 0;
8663 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008664 while (depth >= 0 && !got_int
8665 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008666 {
8667 if (curi[depth] > byts[arridx[depth]])
8668 {
8669 /* Done all bytes at this node, go up one level. */
8670 --depth;
8671 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008672 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008673 }
8674 else
8675 {
8676 /* Do one more byte at this node. */
8677 n = arridx[depth] + curi[depth];
8678 ++curi[depth];
8679 c = byts[n];
8680 if (c == 0)
8681 {
8682 /* End of word, deal with the word.
8683 * Don't use keep-case words in the fold-case tree,
8684 * they will appear in the keep-case tree.
8685 * Only use the word when the region matches. */
8686 flags = (int)idxs[n];
8687 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008688 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008689 && (do_region
8690 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008691 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008692 & lp->lp_region) != 0))
8693 {
8694 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008695 if (!do_region)
8696 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008697
8698 /* Dump the basic word if there is no prefix or
8699 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008700 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008701 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008702 {
8703 dump_word(slang, word, pat, dir,
8704 dumpflags, flags, lnum);
8705 if (pat == NULL)
8706 ++lnum;
8707 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008708
8709 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008710 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008711 lnum = dump_prefixes(slang, word, pat, dir,
8712 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008713 }
8714 }
8715 else
8716 {
8717 /* Normal char, go one level deeper. */
8718 word[depth++] = c;
8719 arridx[depth] = idxs[n];
8720 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008721
8722 /* Check if this characters matches with the pattern.
8723 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008724 * Always ignore case here, dump_word() will check
8725 * proper case later. This isn't exactly right when
8726 * length changes for multi-byte characters with
8727 * ignore case... */
8728 if (depth <= patlen
8729 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008730 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008731 }
8732 }
8733 }
8734 }
8735 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008736}
8737
8738/*
8739 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008740 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008741 */
8742 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008743dump_word(
8744 slang_T *slang,
8745 char_u *word,
8746 char_u *pat,
8747 int *dir,
8748 int dumpflags,
8749 int wordflags,
8750 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008751{
8752 int keepcap = FALSE;
8753 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008754 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008755 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008756 char_u badword[MAXWLEN + 10];
8757 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008758 int flags = wordflags;
8759
8760 if (dumpflags & DUMPFLAG_ONECAP)
8761 flags |= WF_ONECAP;
8762 if (dumpflags & DUMPFLAG_ALLCAP)
8763 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008764
Bram Moolenaar4770d092006-01-12 23:22:24 +00008765 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008766 {
8767 /* Need to fix case according to "flags". */
8768 make_case_word(word, cword, flags);
8769 p = cword;
8770 }
8771 else
8772 {
8773 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008774 if ((dumpflags & DUMPFLAG_KEEPCASE)
8775 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008776 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008777 keepcap = TRUE;
8778 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008779 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008780
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008781 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008782 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008783 /* Add flags and regions after a slash. */
8784 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008785 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008786 STRCPY(badword, p);
8787 STRCAT(badword, "/");
8788 if (keepcap)
8789 STRCAT(badword, "=");
8790 if (flags & WF_BANNED)
8791 STRCAT(badword, "!");
8792 else if (flags & WF_RARE)
8793 STRCAT(badword, "?");
8794 if (flags & WF_REGION)
8795 for (i = 0; i < 7; ++i)
8796 if (flags & (0x10000 << i))
8797 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8798 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008799 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008800
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008801 if (dumpflags & DUMPFLAG_COUNT)
8802 {
8803 hashitem_T *hi;
8804
8805 /* Include the word count for ":spelldump!". */
8806 hi = hash_find(&slang->sl_wordcount, tw);
8807 if (!HASHITEM_EMPTY(hi))
8808 {
8809 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8810 tw, HI2WC(hi)->wc_count);
8811 p = IObuff;
8812 }
8813 }
8814
8815 ml_append(lnum, p, (colnr_T)0, FALSE);
8816 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008817 else if (((dumpflags & DUMPFLAG_ICASE)
8818 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8819 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008820 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008821 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008822 /* if dir was BACKWARD then honor it just once */
8823 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008824}
8825
8826/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008827 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8828 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008829 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008830 * Return the updated line number.
8831 */
8832 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008833dump_prefixes(
8834 slang_T *slang,
8835 char_u *word, /* case-folded word */
8836 char_u *pat,
8837 int *dir,
8838 int dumpflags,
8839 int flags, /* flags with prefix ID */
8840 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008841{
8842 idx_T arridx[MAXWLEN];
8843 int curi[MAXWLEN];
8844 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008845 char_u word_up[MAXWLEN];
8846 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008847 int c;
8848 char_u *byts;
8849 idx_T *idxs;
8850 linenr_T lnum = startlnum;
8851 int depth;
8852 int n;
8853 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008854 int i;
8855
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008856 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008857 * upper-case letter in word_up[]. */
8858 c = PTR2CHAR(word);
8859 if (SPELL_TOUPPER(c) != c)
8860 {
8861 onecap_copy(word, word_up, TRUE);
8862 has_word_up = TRUE;
8863 }
8864
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008865 byts = slang->sl_pbyts;
8866 idxs = slang->sl_pidxs;
8867 if (byts != NULL) /* array not is empty */
8868 {
8869 /*
8870 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008871 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008872 */
8873 depth = 0;
8874 arridx[0] = 0;
8875 curi[0] = 1;
8876 while (depth >= 0 && !got_int)
8877 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008878 n = arridx[depth];
8879 len = byts[n];
8880 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008881 {
8882 /* Done all bytes at this node, go up one level. */
8883 --depth;
8884 line_breakcheck();
8885 }
8886 else
8887 {
8888 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008889 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008890 ++curi[depth];
8891 c = byts[n];
8892 if (c == 0)
8893 {
8894 /* End of prefix, find out how many IDs there are. */
8895 for (i = 1; i < len; ++i)
8896 if (byts[n + i] != 0)
8897 break;
8898 curi[depth] += i - 1;
8899
Bram Moolenaar53805d12005-08-01 07:08:33 +00008900 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8901 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008902 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008903 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008904 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008905 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008906 : flags, lnum);
8907 if (lnum != 0)
8908 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008909 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008910
8911 /* Check for prefix that matches the word when the
8912 * first letter is upper-case, but only if the prefix has
8913 * a condition. */
8914 if (has_word_up)
8915 {
8916 c = valid_word_prefix(i, n, flags, word_up, slang,
8917 TRUE);
8918 if (c != 0)
8919 {
8920 vim_strncpy(prefix + depth, word_up,
8921 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008922 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008923 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008924 : flags, lnum);
8925 if (lnum != 0)
8926 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008927 }
8928 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008929 }
8930 else
8931 {
8932 /* Normal char, go one level deeper. */
8933 prefix[depth++] = c;
8934 arridx[depth] = idxs[n];
8935 curi[depth] = 1;
8936 }
8937 }
8938 }
8939 }
8940
8941 return lnum;
8942}
8943
Bram Moolenaar95529562005-08-25 21:21:38 +00008944/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008945 * Move "p" to the end of word "start".
8946 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008947 */
8948 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008949spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008950{
8951 char_u *p = start;
8952
Bram Moolenaar860cae12010-06-05 23:22:07 +02008953 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008954 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008955 return p;
8956}
8957
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008958#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008959/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008960 * For Insert mode completion CTRL-X s:
8961 * Find start of the word in front of column "startcol".
8962 * We don't check if it is badly spelled, with completion we can only change
8963 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008964 * Returns the column number of the word.
8965 */
8966 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008967spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008968{
8969 char_u *line;
8970 char_u *p;
8971 int col = 0;
8972
Bram Moolenaar95529562005-08-25 21:21:38 +00008973 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008974 return startcol;
8975
8976 /* Find a word character before "startcol". */
8977 line = ml_get_curline();
8978 for (p = line + startcol; p > line; )
8979 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008980 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008981 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008982 break;
8983 }
8984
8985 /* Go back to start of the word. */
8986 while (p > line)
8987 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008988 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008989 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008990 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008991 break;
8992 col = 0;
8993 }
8994
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008995 return col;
8996}
8997
8998/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008999 * Need to check for 'spellcapcheck' now, the word is removed before
9000 * expand_spelling() is called. Therefore the ugly global variable.
9001 */
9002static int spell_expand_need_cap;
9003
9004 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009005spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00009006{
9007 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
9008}
9009
9010/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009011 * Get list of spelling suggestions.
9012 * Used for Insert mode completion CTRL-X ?.
9013 * Returns the number of matches. The matches are in "matchp[]", array of
9014 * allocated strings.
9015 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009016 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009017expand_spelling(
9018 linenr_T lnum UNUSED,
9019 char_u *pat,
9020 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009021{
9022 garray_T ga;
9023
Bram Moolenaar4770d092006-01-12 23:22:24 +00009024 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009025 *matchp = ga.ga_data;
9026 return ga.ga_len;
9027}
9028#endif
9029
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009030#endif /* FEAT_SPELL */