blob: cdcf822957693663f25b495b82cd3dc98d7a2a84 [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 Moolenaar51485f02005-06-04 21:55:20 +00001997 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001998 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001999 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002000 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002001 vim_free(lp->sl_pbyts);
2002 lp->sl_pbyts = NULL;
2003
Bram Moolenaar51485f02005-06-04 21:55:20 +00002004 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002005 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002006 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002007 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002008 vim_free(lp->sl_pidxs);
2009 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002010
Bram Moolenaar4770d092006-01-12 23:22:24 +00002011 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002012 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002013 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2014 while (gap->ga_len > 0)
2015 {
2016 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2017 vim_free(ftp->ft_from);
2018 vim_free(ftp->ft_to);
2019 }
2020 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002021 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002022
2023 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002024 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002025 {
2026 /* "ga_len" is set to 1 without adding an item for latin1 */
2027 if (gap->ga_data != NULL)
2028 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2029 for (i = 0; i < gap->ga_len; ++i)
2030 vim_free(((int **)gap->ga_data)[i]);
2031 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002032 else
2033 /* SAL items: free salitem_T items */
2034 while (gap->ga_len > 0)
2035 {
2036 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2037 vim_free(smp->sm_lead);
2038 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2039 vim_free(smp->sm_to);
2040#ifdef FEAT_MBYTE
2041 vim_free(smp->sm_lead_w);
2042 vim_free(smp->sm_oneof_w);
2043 vim_free(smp->sm_to_w);
2044#endif
2045 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002046 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002047
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002048 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002049 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002050 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002051 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002052 lp->sl_prefprog = NULL;
2053
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002054 vim_free(lp->sl_info);
2055 lp->sl_info = NULL;
2056
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002057 vim_free(lp->sl_midword);
2058 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002059
Bram Moolenaar473de612013-06-08 18:19:48 +02002060 vim_regfree(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002061 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002062 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002063 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002064 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002065 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002066 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002067 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002068
2069 vim_free(lp->sl_syllable);
2070 lp->sl_syllable = NULL;
2071 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002072
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002073 ga_clear_strings(&lp->sl_comppat);
2074
Bram Moolenaar4770d092006-01-12 23:22:24 +00002075 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2076 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002077
Bram Moolenaar4770d092006-01-12 23:22:24 +00002078#ifdef FEAT_MBYTE
2079 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002080#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002081
Bram Moolenaar4770d092006-01-12 23:22:24 +00002082 /* Clear info from .sug file. */
2083 slang_clear_sug(lp);
2084
Bram Moolenaar5195e452005-08-19 20:32:47 +00002085 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002086 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002087 lp->sl_compsylmax = MAXWLEN;
2088 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089}
2090
2091/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002092 * Clear the info from the .sug file in "lp".
2093 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002094 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002095slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002096{
2097 vim_free(lp->sl_sbyts);
2098 lp->sl_sbyts = NULL;
2099 vim_free(lp->sl_sidxs);
2100 lp->sl_sidxs = NULL;
2101 close_spellbuf(lp->sl_sugbuf);
2102 lp->sl_sugbuf = NULL;
2103 lp->sl_sugloaded = FALSE;
2104 lp->sl_sugtime = 0;
2105}
2106
2107/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002108 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002109 * Invoked through do_in_runtimepath().
2110 */
2111 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002112spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002113{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002114 spelload_T *slp = (spelload_T *)cookie;
2115 slang_T *slang;
2116
2117 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2118 if (slang != NULL)
2119 {
2120 /* When a previously loaded file has NOBREAK also use it for the
2121 * ".add" files. */
2122 if (slp->sl_nobreak && slang->sl_add)
2123 slang->sl_nobreak = TRUE;
2124 else if (slang->sl_nobreak)
2125 slp->sl_nobreak = TRUE;
2126
2127 slp->sl_slang = slang;
2128 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002129}
2130
Bram Moolenaar4770d092006-01-12 23:22:24 +00002131
2132/*
2133 * Add a word to the hashtable of common words.
2134 * If it's already there then the counter is increased.
2135 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002136 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002137count_common_word(
2138 slang_T *lp,
2139 char_u *word,
2140 int len, /* word length, -1 for upto NUL */
2141 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002142{
2143 hash_T hash;
2144 hashitem_T *hi;
2145 wordcount_T *wc;
2146 char_u buf[MAXWLEN];
2147 char_u *p;
2148
2149 if (len == -1)
2150 p = word;
2151 else
2152 {
2153 vim_strncpy(buf, word, len);
2154 p = buf;
2155 }
2156
2157 hash = hash_hash(p);
2158 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2159 if (HASHITEM_EMPTY(hi))
2160 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002161 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002162 if (wc == NULL)
2163 return;
2164 STRCPY(wc->wc_word, p);
2165 wc->wc_count = count;
2166 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2167 }
2168 else
2169 {
2170 wc = HI2WC(hi);
2171 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2172 wc->wc_count = MAXWORDCOUNT;
2173 }
2174}
2175
2176/*
2177 * Adjust the score of common words.
2178 */
2179 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002180score_wordcount_adj(
2181 slang_T *slang,
2182 int score,
2183 char_u *word,
2184 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002185{
2186 hashitem_T *hi;
2187 wordcount_T *wc;
2188 int bonus;
2189 int newscore;
2190
2191 hi = hash_find(&slang->sl_wordcount, word);
2192 if (!HASHITEM_EMPTY(hi))
2193 {
2194 wc = HI2WC(hi);
2195 if (wc->wc_count < SCORE_THRES2)
2196 bonus = SCORE_COMMON1;
2197 else if (wc->wc_count < SCORE_THRES3)
2198 bonus = SCORE_COMMON2;
2199 else
2200 bonus = SCORE_COMMON3;
2201 if (split)
2202 newscore = score - bonus / 2;
2203 else
2204 newscore = score - bonus;
2205 if (newscore < 0)
2206 return 0;
2207 return newscore;
2208 }
2209 return score;
2210}
2211
Bram Moolenaar5195e452005-08-19 20:32:47 +00002212
Bram Moolenaar6de68532005-08-24 22:08:48 +00002213/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002214 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002215 * Like strchr() but independent of locale.
2216 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002217 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002218byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002219{
2220 char_u *p;
2221
2222 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002223 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002224 return TRUE;
2225 return FALSE;
2226}
2227
Bram Moolenaar5195e452005-08-19 20:32:47 +00002228#define SY_MAXLEN 30
2229typedef struct syl_item_S
2230{
2231 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2232 int sy_len;
2233} syl_item_T;
2234
2235/*
2236 * Truncate "slang->sl_syllable" at the first slash and put the following items
2237 * in "slang->sl_syl_items".
2238 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002239 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002240init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002241{
2242 char_u *p;
2243 char_u *s;
2244 int l;
2245 syl_item_T *syl;
2246
2247 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2248 p = vim_strchr(slang->sl_syllable, '/');
2249 while (p != NULL)
2250 {
2251 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002252 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002253 break;
2254 s = p;
2255 p = vim_strchr(p, '/');
2256 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002257 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002258 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002259 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002260 if (l >= SY_MAXLEN)
2261 return SP_FORMERROR;
2262 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002263 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002264 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2265 + slang->sl_syl_items.ga_len++;
2266 vim_strncpy(syl->sy_chars, s, l);
2267 syl->sy_len = l;
2268 }
2269 return OK;
2270}
2271
2272/*
2273 * Count the number of syllables in "word".
2274 * When "word" contains spaces the syllables after the last space are counted.
2275 * Returns zero if syllables are not defines.
2276 */
2277 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002278count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002279{
2280 int cnt = 0;
2281 int skip = FALSE;
2282 char_u *p;
2283 int len;
2284 int i;
2285 syl_item_T *syl;
2286 int c;
2287
2288 if (slang->sl_syllable == NULL)
2289 return 0;
2290
2291 for (p = word; *p != NUL; p += len)
2292 {
2293 /* When running into a space reset counter. */
2294 if (*p == ' ')
2295 {
2296 len = 1;
2297 cnt = 0;
2298 continue;
2299 }
2300
2301 /* Find longest match of syllable items. */
2302 len = 0;
2303 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2304 {
2305 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2306 if (syl->sy_len > len
2307 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2308 len = syl->sy_len;
2309 }
2310 if (len != 0) /* found a match, count syllable */
2311 {
2312 ++cnt;
2313 skip = FALSE;
2314 }
2315 else
2316 {
2317 /* No recognized syllable item, at least a syllable char then? */
2318#ifdef FEAT_MBYTE
2319 c = mb_ptr2char(p);
2320 len = (*mb_ptr2len)(p);
2321#else
2322 c = *p;
2323 len = 1;
2324#endif
2325 if (vim_strchr(slang->sl_syllable, c) == NULL)
2326 skip = FALSE; /* No, search for next syllable */
2327 else if (!skip)
2328 {
2329 ++cnt; /* Yes, count it */
2330 skip = TRUE; /* don't count following syllable chars */
2331 }
2332 }
2333 }
2334 return cnt;
2335}
2336
2337/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002338 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002339 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002340 */
2341 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002342did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002343{
2344 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002345 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002346 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002347 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002348 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002349 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002350 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002351 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002352 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002354 int len;
2355 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002356 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002357 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002358 char_u *use_region = NULL;
2359 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002360 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002361 int i, j;
2362 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002363 static int recursive = FALSE;
2364 char_u *ret_msg = NULL;
2365 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002366#ifdef FEAT_AUTOCMD
2367 bufref_T bufref;
2368
2369 set_bufref(&bufref, wp->w_buffer);
2370#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002371
2372 /* We don't want to do this recursively. May happen when a language is
2373 * not available and the SpellFileMissing autocommand opens a new buffer
2374 * in which 'spell' is set. */
2375 if (recursive)
2376 return NULL;
2377 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378
2379 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002380 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002382 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002383 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002384 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002385 if (spl_copy == NULL)
2386 goto theend;
2387
Bram Moolenaar2593e032013-11-14 03:54:07 +01002388#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002389 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002390#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002391
2392 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002393 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002394 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002395 /* Get one language name. */
2396 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002397 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002398 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002400 if (STRCMP(lang, "cjk") == 0)
2401 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01002402#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002403 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002404#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002405 continue;
2406 }
2407
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002408 /* If the name ends in ".spl" use it as the name of the spell file.
2409 * If there is a region name let "region" point to it and remove it
2410 * from the name. */
2411 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2412 {
2413 filename = TRUE;
2414
Bram Moolenaarb6356332005-07-18 21:40:44 +00002415 /* Locate a region and remove it from the file name. */
2416 p = vim_strchr(gettail(lang), '_');
2417 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2418 && !ASCII_ISALPHA(p[3]))
2419 {
2420 vim_strncpy(region_cp, p + 1, 2);
2421 mch_memmove(p, p + 3, len - (p - lang) - 2);
2422 len -= 3;
2423 region = region_cp;
2424 }
2425 else
2426 dont_use_region = TRUE;
2427
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002428 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002429 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2430 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002431 break;
2432 }
2433 else
2434 {
2435 filename = FALSE;
2436 if (len > 3 && lang[len - 3] == '_')
2437 {
2438 region = lang + len - 2;
2439 len -= 3;
2440 lang[len] = NUL;
2441 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002442 else
2443 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002444
2445 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2447 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002448 break;
2449 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002450
Bram Moolenaarb6356332005-07-18 21:40:44 +00002451 if (region != NULL)
2452 {
2453 /* If the region differs from what was used before then don't
2454 * use it for 'spellfile'. */
2455 if (use_region != NULL && STRCMP(region, use_region) != 0)
2456 dont_use_region = TRUE;
2457 use_region = region;
2458 }
2459
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002460 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002461 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002462 {
2463 if (filename)
2464 (void)spell_load_file(lang, lang, NULL, FALSE);
2465 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002466 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002467 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002468#ifdef FEAT_AUTOCMD
2469 /* SpellFileMissing autocommands may do anything, including
2470 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002471 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002472 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02002473 ret_msg = (char_u *)N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002474 goto theend;
2475 }
2476#endif
2477 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002478 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002479
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002480 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002481 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002482 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002483 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2484 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2485 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002486 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002487 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002488 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002489 {
2490 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002491 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002492 if (c == REGION_ALL)
2493 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002494 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002495 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002497 /* This addition file is for other regions. */
2498 region_mask = 0;
2499 }
2500 else
2501 /* This is probably an error. Give a warning and
2502 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002503 smsg((char_u *)
2504 _("Warning: region %s not supported"),
2505 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002506 }
2507 else
2508 region_mask = 1 << c;
2509 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002511 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002512 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002513 if (ga_grow(&ga, 1) == FAIL)
2514 {
2515 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002516 ret_msg = e_outofmem;
2517 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002518 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002519 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002520 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2521 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002522 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002523 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002524 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002525 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002526 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002527 }
2528
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002529 /* round 0: load int_wordlist, if possible.
2530 * round 1: load first name in 'spellfile'.
2531 * round 2: load second name in 'spellfile.
2532 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002533 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002534 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002536 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002537 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002538 /* Internal wordlist, if there is one. */
2539 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002540 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002541 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002542 }
2543 else
2544 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002545 /* One entry in 'spellfile'. */
2546 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2547 STRCAT(spf_name, ".spl");
2548
2549 /* If it was already found above then skip it. */
2550 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002551 {
2552 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2553 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002554 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002555 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002556 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002557 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002558 }
2559
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002560 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002561 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2562 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002563 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002564 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002565 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002566 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002567 * region name, the region is ignored otherwise. for int_wordlist
2568 * use an arbitrary name. */
2569 if (round == 0)
2570 STRCPY(lang, "internal wordlist");
2571 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002572 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002573 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002574 p = vim_strchr(lang, '.');
2575 if (p != NULL)
2576 *p = NUL; /* truncate at ".encoding.add" */
2577 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002578 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002579
2580 /* If one of the languages has NOBREAK we assume the addition
2581 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002582 if (slang != NULL && nobreak)
2583 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002584 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002585 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002586 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002587 region_mask = REGION_ALL;
2588 if (use_region != NULL && !dont_use_region)
2589 {
2590 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002591 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002592 if (c != REGION_ALL)
2593 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002594 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002595 /* This spell file is for other regions. */
2596 region_mask = 0;
2597 }
2598
2599 if (region_mask != 0)
2600 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002601 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2602 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2603 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002604 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2605 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002606 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002607 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608 }
2609 }
2610
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002612 ga_clear(&wp->w_s->b_langp);
2613 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002614
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002615 /* For each language figure out what language to use for sound folding and
2616 * REP items. If the language doesn't support it itself use another one
2617 * with the same name. E.g. for "en-math" use "en". */
2618 for (i = 0; i < ga.ga_len; ++i)
2619 {
2620 lp = LANGP_ENTRY(ga, i);
2621
2622 /* sound folding */
2623 if (lp->lp_slang->sl_sal.ga_len > 0)
2624 /* language does sound folding itself */
2625 lp->lp_sallang = lp->lp_slang;
2626 else
2627 /* find first similar language that does sound folding */
2628 for (j = 0; j < ga.ga_len; ++j)
2629 {
2630 lp2 = LANGP_ENTRY(ga, j);
2631 if (lp2->lp_slang->sl_sal.ga_len > 0
2632 && STRNCMP(lp->lp_slang->sl_name,
2633 lp2->lp_slang->sl_name, 2) == 0)
2634 {
2635 lp->lp_sallang = lp2->lp_slang;
2636 break;
2637 }
2638 }
2639
2640 /* REP items */
2641 if (lp->lp_slang->sl_rep.ga_len > 0)
2642 /* language has REP items itself */
2643 lp->lp_replang = lp->lp_slang;
2644 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002645 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002646 for (j = 0; j < ga.ga_len; ++j)
2647 {
2648 lp2 = LANGP_ENTRY(ga, j);
2649 if (lp2->lp_slang->sl_rep.ga_len > 0
2650 && STRNCMP(lp->lp_slang->sl_name,
2651 lp2->lp_slang->sl_name, 2) == 0)
2652 {
2653 lp->lp_replang = lp2->lp_slang;
2654 break;
2655 }
2656 }
2657 }
2658
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002659theend:
2660 vim_free(spl_copy);
2661 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002662 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002663 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002664}
2665
2666/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002667 * Clear the midword characters for buffer "buf".
2668 */
2669 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002670clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002671{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002672 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002673#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002674 vim_free(wp->w_s->b_spell_ismw_mb);
2675 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002676#endif
2677}
2678
2679/*
2680 * Use the "sl_midword" field of language "lp" for buffer "buf".
2681 * They add up to any currently used midword characters.
2682 */
2683 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002684use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002685{
2686 char_u *p;
2687
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002688 if (lp->sl_midword == NULL) /* there aren't any */
2689 return;
2690
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002691 for (p = lp->sl_midword; *p != NUL; )
2692#ifdef FEAT_MBYTE
2693 if (has_mbyte)
2694 {
2695 int c, l, n;
2696 char_u *bp;
2697
2698 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002699 l = (*mb_ptr2len)(p);
2700 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002701 wp->w_s->b_spell_ismw[c] = TRUE;
2702 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002703 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002704 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002705 else
2706 {
2707 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002708 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2709 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002710 if (bp != NULL)
2711 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002712 vim_free(wp->w_s->b_spell_ismw_mb);
2713 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002714 vim_strncpy(bp + n, p, l);
2715 }
2716 }
2717 p += l;
2718 }
2719 else
2720#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002721 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002722}
2723
2724/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002725 * Find the region "region[2]" in "rp" (points to "sl_regions").
2726 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002727 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002728 */
2729 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002730find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002731{
2732 int i;
2733
2734 for (i = 0; ; i += 2)
2735 {
2736 if (rp[i] == NUL)
2737 return REGION_ALL;
2738 if (rp[i] == region[0] && rp[i + 1] == region[1])
2739 break;
2740 }
2741 return i / 2;
2742}
2743
2744/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002745 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002746 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002747 * Word WF_ONECAP
2748 * W WORD WF_ALLCAP
2749 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002750 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002751 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002752captype(
2753 char_u *word,
2754 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002755{
2756 char_u *p;
2757 int c;
2758 int firstcap;
2759 int allcap;
2760 int past_second = FALSE; /* past second word char */
2761
2762 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002763 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002765 return 0; /* only non-word characters, illegal word */
2766#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002767 if (has_mbyte)
2768 c = mb_ptr2char_adv(&p);
2769 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002770#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002771 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002772 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002773
2774 /*
2775 * Need to check all letters to find a word with mixed upper/lower.
2776 * But a word with an upper char only at start is a ONECAP.
2777 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002778 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002779 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002780 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002781 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002782 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002783 {
2784 /* UUl -> KEEPCAP */
2785 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002786 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002787 allcap = FALSE;
2788 }
2789 else if (!allcap)
2790 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002791 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002792 past_second = TRUE;
2793 }
2794
2795 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002796 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002798 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002799 return 0;
2800}
2801
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002802/*
2803 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2804 * capital. So that make_case_word() can turn WOrd into Word.
2805 * Add ALLCAP for "WOrD".
2806 */
2807 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002808badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002809{
2810 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002811 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002812 int l, u;
2813 int first;
2814 char_u *p;
2815
2816 if (flags & WF_KEEPCAP)
2817 {
2818 /* Count the number of UPPER and lower case letters. */
2819 l = u = 0;
2820 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002821 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002822 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002823 c = PTR2CHAR(p);
2824 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002825 {
2826 ++u;
2827 if (p == word)
2828 first = TRUE;
2829 }
2830 else
2831 ++l;
2832 }
2833
2834 /* If there are more UPPER than lower case letters suggest an
2835 * ALLCAP word. Otherwise, if the first letter is UPPER then
2836 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2837 * require three upper case letters. */
2838 if (u > l && u > 2)
2839 flags |= WF_ALLCAP;
2840 else if (first)
2841 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002842
2843 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2844 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002845 }
2846 return flags;
2847}
2848
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002849/*
2850 * Delete the internal wordlist and its .spl file.
2851 */
2852 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002853spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002854{
2855 char_u fname[MAXPATHL];
2856
2857 if (int_wordlist != NULL)
2858 {
2859 mch_remove(int_wordlist);
2860 int_wordlist_spl(fname);
2861 mch_remove(fname);
2862 vim_free(int_wordlist);
2863 int_wordlist = NULL;
2864 }
2865}
2866
2867#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002868/*
2869 * Free all languages.
2870 */
2871 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002872spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002873{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002874 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002875 buf_T *buf;
2876
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002877 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002878 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002879 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002880
2881 while (first_lang != NULL)
2882 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002883 slang = first_lang;
2884 first_lang = slang->sl_next;
2885 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002886 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002887
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002888 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002889
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002890 vim_free(repl_to);
2891 repl_to = NULL;
2892 vim_free(repl_from);
2893 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002894}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002895#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002896
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002897#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002898/*
2899 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002900 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002901 */
2902 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002903spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002904{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002905 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002906
Bram Moolenaarea408852005-06-25 22:49:46 +00002907 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002908 init_spell_chartab();
2909
2910 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002911 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002912
2913 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002914 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002915 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002916 /* Only load the wordlists when 'spelllang' is set and there is a
2917 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002918 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002919 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002920 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002921 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002922 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002923 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002924 }
2925 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002926 }
2927}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002928#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002929
Bram Moolenaarb765d632005-06-07 21:00:02 +00002930/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002931 * Opposite of offset2bytes().
2932 * "pp" points to the bytes and is advanced over it.
2933 * Returns the offset.
2934 */
2935 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002936bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002937{
2938 char_u *p = *pp;
2939 int nr;
2940 int c;
2941
2942 c = *p++;
2943 if ((c & 0x80) == 0x00) /* 1 byte */
2944 {
2945 nr = c - 1;
2946 }
2947 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2948 {
2949 nr = (c & 0x3f) - 1;
2950 nr = nr * 255 + (*p++ - 1);
2951 }
2952 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2953 {
2954 nr = (c & 0x1f) - 1;
2955 nr = nr * 255 + (*p++ - 1);
2956 nr = nr * 255 + (*p++ - 1);
2957 }
2958 else /* 4 bytes */
2959 {
2960 nr = (c & 0x0f) - 1;
2961 nr = nr * 255 + (*p++ - 1);
2962 nr = nr * 255 + (*p++ - 1);
2963 nr = nr * 255 + (*p++ - 1);
2964 }
2965
2966 *pp = p;
2967 return nr;
2968}
2969
Bram Moolenaar4770d092006-01-12 23:22:24 +00002970
2971/*
2972 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2973 * list and only contains text lines. Can use a swapfile to reduce memory
2974 * use.
2975 * Most other fields are invalid! Esp. watch out for string options being
2976 * NULL and there is no undo info.
2977 * Returns NULL when out of memory.
2978 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002979 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002980open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002981{
2982 buf_T *buf;
2983
2984 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2985 if (buf != NULL)
2986 {
2987 buf->b_spell = TRUE;
2988 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002989#ifdef FEAT_CRYPT
2990 buf->b_p_key = empty_option;
2991#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002992 ml_open(buf);
2993 ml_open_file(buf); /* create swap file now */
2994 }
2995 return buf;
2996}
2997
2998/*
2999 * Close the buffer used for spell info.
3000 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003001 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003002close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003003{
3004 if (buf != NULL)
3005 {
3006 ml_close(buf, TRUE);
3007 vim_free(buf);
3008 }
3009}
3010
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003011/*
3012 * Init the chartab used for spelling for ASCII.
3013 * EBCDIC is not supported!
3014 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003015 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003016clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003017{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003018 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003019
3020 /* Init everything to FALSE. */
3021 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
3022 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
3023 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003024 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003025 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003026 sp->st_upper[i] = i;
3027 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003028
3029 /* We include digits. A word shouldn't start with a digit, but handling
3030 * that is done separately. */
3031 for (i = '0'; i <= '9'; ++i)
3032 sp->st_isw[i] = TRUE;
3033 for (i = 'A'; i <= 'Z'; ++i)
3034 {
3035 sp->st_isw[i] = TRUE;
3036 sp->st_isu[i] = TRUE;
3037 sp->st_fold[i] = i + 0x20;
3038 }
3039 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003040 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003041 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003042 sp->st_upper[i] = i - 0x20;
3043 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003044}
3045
3046/*
3047 * Init the chartab used for spelling. Only depends on 'encoding'.
3048 * Called once while starting up and when 'encoding' changes.
3049 * The default is to use isalpha(), but the spell file should define the word
3050 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003051 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003052 */
3053 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003054init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003055{
3056 int i;
3057
3058 did_set_spelltab = FALSE;
3059 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003060#ifdef FEAT_MBYTE
3061 if (enc_dbcs)
3062 {
3063 /* DBCS: assume double-wide characters are word characters. */
3064 for (i = 128; i <= 255; ++i)
3065 if (MB_BYTE2LEN(i) == 2)
3066 spelltab.st_isw[i] = TRUE;
3067 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003068 else if (enc_utf8)
3069 {
3070 for (i = 128; i < 256; ++i)
3071 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003072 int f = utf_fold(i);
3073 int u = utf_toupper(i);
3074
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003075 spelltab.st_isu[i] = utf_isupper(i);
3076 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003077 /* The folded/upper-cased value is different between latin1 and
3078 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
3079 * value for utf-8 to avoid this. */
3080 spelltab.st_fold[i] = (f < 256) ? f : i;
3081 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003082 }
3083 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003084 else
3085#endif
3086 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003087 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003088 for (i = 128; i < 256; ++i)
3089 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003090 if (MB_ISUPPER(i))
3091 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003092 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003093 spelltab.st_isu[i] = TRUE;
3094 spelltab.st_fold[i] = MB_TOLOWER(i);
3095 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003096 else if (MB_ISLOWER(i))
3097 {
3098 spelltab.st_isw[i] = TRUE;
3099 spelltab.st_upper[i] = MB_TOUPPER(i);
3100 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003101 }
3102 }
3103}
3104
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003105
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003106/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003107 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003108 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003109 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003110 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003111 */
3112 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003113spell_iswordp(
3114 char_u *p,
3115 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003116{
Bram Moolenaarea408852005-06-25 22:49:46 +00003117#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003118 char_u *s;
3119 int l;
3120 int c;
3121
3122 if (has_mbyte)
3123 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003124 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003125 s = p;
3126 if (l == 1)
3127 {
3128 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003129 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003130 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003131 }
3132 else
3133 {
3134 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003135 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3136 : (wp->w_s->b_spell_ismw_mb != NULL
3137 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003138 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003139 }
3140
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003141 c = mb_ptr2char(s);
3142 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003143 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003144 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003145 }
Bram Moolenaarea408852005-06-25 22:49:46 +00003146#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003147
Bram Moolenaar860cae12010-06-05 23:22:07 +02003148 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003149}
3150
3151/*
3152 * Return TRUE if "p" points to a word character.
3153 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3154 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003155 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003156spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003157{
3158#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003159 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003160
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003161 if (has_mbyte)
3162 {
3163 c = mb_ptr2char(p);
3164 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003165 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003166 return spelltab.st_isw[c];
3167 }
3168#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003169 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003170}
3171
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003172#ifdef FEAT_MBYTE
3173/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003174 * Return TRUE if word class indicates a word character.
3175 * Only for characters above 255.
3176 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003177 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003178 */
3179 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003180spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003181{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003182 if (wp->w_s->b_cjk)
3183 /* East Asian characters are not considered word characters. */
3184 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003185 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3186}
3187
3188/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003189 * Return TRUE if "p" points to a word character.
3190 * Wide version of spell_iswordp().
3191 */
3192 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003193spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003194{
3195 int *s;
3196
Bram Moolenaar860cae12010-06-05 23:22:07 +02003197 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3198 : (wp->w_s->b_spell_ismw_mb != NULL
3199 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003200 s = p + 1;
3201 else
3202 s = p;
3203
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003204 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003205 {
3206 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003207 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003208 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003209 return spell_mb_isword_class(
3210 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003211 return 0;
3212 }
3213 return spelltab.st_isw[*s];
3214}
3215#endif
3216
Bram Moolenaarea408852005-06-25 22:49:46 +00003217/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003218 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3219 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003220 * When using a multi-byte 'encoding' the length may change!
3221 * Returns FAIL when something wrong.
3222 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003223 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003224spell_casefold(
3225 char_u *str,
3226 int len,
3227 char_u *buf,
3228 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003229{
3230 int i;
3231
3232 if (len >= buflen)
3233 {
3234 buf[0] = NUL;
3235 return FAIL; /* result will not fit */
3236 }
3237
3238#ifdef FEAT_MBYTE
3239 if (has_mbyte)
3240 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003241 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003242 char_u *p;
3243 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003244
3245 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003246 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003247 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003248 if (outi + MB_MAXBYTES > buflen)
3249 {
3250 buf[outi] = NUL;
3251 return FAIL;
3252 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003253 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003254 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003255 }
3256 buf[outi] = NUL;
3257 }
3258 else
3259#endif
3260 {
3261 /* Be quick for non-multibyte encodings. */
3262 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003263 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003264 buf[i] = NUL;
3265 }
3266
3267 return OK;
3268}
3269
Bram Moolenaar4770d092006-01-12 23:22:24 +00003270/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003271#define SPS_BEST 1
3272#define SPS_FAST 2
3273#define SPS_DOUBLE 4
3274
Bram Moolenaar4770d092006-01-12 23:22:24 +00003275static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3276static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003277
3278/*
3279 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003280 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003281 */
3282 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003283spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003284{
3285 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003286 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003287 char_u buf[MAXPATHL];
3288 int f;
3289
3290 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003291 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003292
3293 for (p = p_sps; *p != NUL; )
3294 {
3295 copy_option_part(&p, buf, MAXPATHL, ",");
3296
3297 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003298 if (VIM_ISDIGIT(*buf))
3299 {
3300 s = buf;
3301 sps_limit = getdigits(&s);
3302 if (*s != NUL && !VIM_ISDIGIT(*s))
3303 f = -1;
3304 }
3305 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003306 f = SPS_BEST;
3307 else if (STRCMP(buf, "fast") == 0)
3308 f = SPS_FAST;
3309 else if (STRCMP(buf, "double") == 0)
3310 f = SPS_DOUBLE;
3311 else if (STRNCMP(buf, "expr:", 5) != 0
3312 && STRNCMP(buf, "file:", 5) != 0)
3313 f = -1;
3314
3315 if (f == -1 || (sps_flags != 0 && f != 0))
3316 {
3317 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003318 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003319 return FAIL;
3320 }
3321 if (f != 0)
3322 sps_flags = f;
3323 }
3324
3325 if (sps_flags == 0)
3326 sps_flags = SPS_BEST;
3327
3328 return OK;
3329}
3330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003331/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003332 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003333 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003334 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003335 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 */
3337 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003338spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003339{
3340 char_u *line;
3341 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 char_u wcopy[MAXWLEN + 2];
3343 char_u *p;
3344 int i;
3345 int c;
3346 suginfo_T sug;
3347 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003348 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003349 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003350 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003351 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003352 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003353 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003355 if (no_spell_checking(curwin))
3356 return;
3357
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003358 if (VIsual_active)
3359 {
3360 /* Use the Visually selected text as the bad word. But reject
3361 * a multi-line selection. */
3362 if (curwin->w_cursor.lnum != VIsual.lnum)
3363 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003364 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003365 return;
3366 }
3367 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3368 if (badlen < 0)
3369 badlen = -badlen;
3370 else
3371 curwin->w_cursor.col = VIsual.col;
3372 ++badlen;
3373 end_visual_mode();
3374 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003375 /* Find the start of the badly spelled word. */
3376 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003377 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003379 /* No bad word or it starts after the cursor: use the word under the
3380 * cursor. */
3381 curwin->w_cursor = prev_cursor;
3382 line = ml_get_curline();
3383 p = line + curwin->w_cursor.col;
3384 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003385 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003386 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003387 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003388 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003389 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003390
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003391 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003392 {
3393 beep_flush();
3394 return;
3395 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003396 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 }
3398
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003399 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003400
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003401 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003402 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003403
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003404 /* Make a copy of current line since autocommands may free the line. */
3405 line = vim_strsave(ml_get_curline());
3406 if (line == NULL)
3407 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003408
Bram Moolenaar5195e452005-08-19 20:32:47 +00003409 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3410 * 'spellsuggest', whatever is smaller. */
3411 if (sps_limit > (int)Rows - 2)
3412 limit = (int)Rows - 2;
3413 else
3414 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003415 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003416 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417
3418 if (sug.su_ga.ga_len == 0)
3419 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003420 else if (count > 0)
3421 {
3422 if (count > sug.su_ga.ga_len)
3423 smsg((char_u *)_("Sorry, only %ld suggestions"),
3424 (long)sug.su_ga.ga_len);
3425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 else
3427 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003428 vim_free(repl_from);
3429 repl_from = NULL;
3430 vim_free(repl_to);
3431 repl_to = NULL;
3432
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003433#ifdef FEAT_RIGHTLEFT
3434 /* When 'rightleft' is set the list is drawn right-left. */
3435 cmdmsg_rl = curwin->w_p_rl;
3436 if (cmdmsg_rl)
3437 msg_col = Columns - 1;
3438#endif
3439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003440 /* List the suggestions. */
3441 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003442 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003443 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003444 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3445 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003446#ifdef FEAT_RIGHTLEFT
3447 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3448 {
3449 /* And now the rabbit from the high hat: Avoid showing the
3450 * untranslated message rightleft. */
3451 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3452 sug.su_badlen, sug.su_badptr);
3453 }
3454#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003455 msg_puts(IObuff);
3456 msg_clr_eos();
3457 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003459 msg_scroll = TRUE;
3460 for (i = 0; i < sug.su_ga.ga_len; ++i)
3461 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003462 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003463
3464 /* The suggested word may replace only part of the bad word, add
3465 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003466 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003467 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003468 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003469 sug.su_badptr + stp->st_orglen,
3470 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003471 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3472#ifdef FEAT_RIGHTLEFT
3473 if (cmdmsg_rl)
3474 rl_mirror(IObuff);
3475#endif
3476 msg_puts(IObuff);
3477
3478 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003479 msg_puts(IObuff);
3480
3481 /* The word may replace more than "su_badlen". */
3482 if (sug.su_badlen < stp->st_orglen)
3483 {
3484 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3485 stp->st_orglen, sug.su_badptr);
3486 msg_puts(IObuff);
3487 }
3488
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003489 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003490 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003491 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003492 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003493 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003494 stp->st_salscore ? "s " : "",
3495 stp->st_score, stp->st_altscore);
3496 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003497 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003498 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003499#ifdef FEAT_RIGHTLEFT
3500 if (cmdmsg_rl)
3501 /* Mirror the numbers, but keep the leading space. */
3502 rl_mirror(IObuff + 1);
3503#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003504 msg_advance(30);
3505 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003506 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003507 msg_putchar('\n');
3508 }
3509
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003510#ifdef FEAT_RIGHTLEFT
3511 cmdmsg_rl = FALSE;
3512 msg_col = 0;
3513#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003514 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003515 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003516 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003517 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003518 lines_left = Rows; /* avoid more prompt */
3519 /* don't delay for 'smd' in normal_cmd() */
3520 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003521 }
3522
Bram Moolenaard12a1322005-08-21 22:08:24 +00003523 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3524 {
3525 /* Save the from and to text for :spellrepall. */
3526 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003527 if (sug.su_badlen > stp->st_orglen)
3528 {
3529 /* Replacing less than "su_badlen", append the remainder to
3530 * repl_to. */
3531 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3532 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3533 sug.su_badlen - stp->st_orglen,
3534 sug.su_badptr + stp->st_orglen);
3535 repl_to = vim_strsave(IObuff);
3536 }
3537 else
3538 {
3539 /* Replacing su_badlen or more, use the whole word. */
3540 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3541 repl_to = vim_strsave(stp->st_word);
3542 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003543
3544 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003545 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3546 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003547 if (p != NULL)
3548 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003549 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003550 mch_memmove(p, line, c);
3551 STRCPY(p + c, stp->st_word);
3552 STRCAT(p, sug.su_badptr + stp->st_orglen);
3553 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3554 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003555
3556 /* For redo we use a change-word command. */
3557 ResetRedobuff();
3558 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003559 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003560 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003561 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003562
3563 /* After this "p" may be invalid. */
3564 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003565 }
3566 }
3567 else
3568 curwin->w_cursor = prev_cursor;
3569
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003570 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003571skip:
3572 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003573}
3574
3575/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003576 * Check if the word at line "lnum" column "col" is required to start with a
3577 * capital. This uses 'spellcapcheck' of the current buffer.
3578 */
3579 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003580check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003581{
3582 int need_cap = FALSE;
3583 char_u *line;
3584 char_u *line_copy = NULL;
3585 char_u *p;
3586 colnr_T endcol;
3587 regmatch_T regmatch;
3588
Bram Moolenaar860cae12010-06-05 23:22:07 +02003589 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003590 return FALSE;
3591
3592 line = ml_get_curline();
3593 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003594 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003595 {
3596 /* At start of line, check if previous line is empty or sentence
3597 * ends there. */
3598 if (lnum == 1)
3599 need_cap = TRUE;
3600 else
3601 {
3602 line = ml_get(lnum - 1);
3603 if (*skipwhite(line) == NUL)
3604 need_cap = TRUE;
3605 else
3606 {
3607 /* Append a space in place of the line break. */
3608 line_copy = concat_str(line, (char_u *)" ");
3609 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003610 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003611 }
3612 }
3613 }
3614 else
3615 endcol = col;
3616
3617 if (endcol > 0)
3618 {
3619 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003620 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003621 regmatch.rm_ic = FALSE;
3622 p = line + endcol;
3623 for (;;)
3624 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003625 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003626 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003627 break;
3628 if (vim_regexec(&regmatch, p, 0)
3629 && regmatch.endp[0] == line + endcol)
3630 {
3631 need_cap = TRUE;
3632 break;
3633 }
3634 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003635 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003636 }
3637
3638 vim_free(line_copy);
3639
3640 return need_cap;
3641}
3642
3643
3644/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003645 * ":spellrepall"
3646 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003647 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003648ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003649{
3650 pos_T pos = curwin->w_cursor;
3651 char_u *frompat;
3652 int addlen;
3653 char_u *line;
3654 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003655 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003656 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003657
3658 if (repl_from == NULL || repl_to == NULL)
3659 {
3660 EMSG(_("E752: No previous spell replacement"));
3661 return;
3662 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003663 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003664
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003665 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003666 if (frompat == NULL)
3667 return;
3668 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3669 p_ws = FALSE;
3670
Bram Moolenaar5195e452005-08-19 20:32:47 +00003671 sub_nsubs = 0;
3672 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003673 curwin->w_cursor.lnum = 0;
3674 while (!got_int)
3675 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003676 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003677 || u_save_cursor() == FAIL)
3678 break;
3679
3680 /* Only replace when the right word isn't there yet. This happens
3681 * when changing "etc" to "etc.". */
3682 line = ml_get_curline();
3683 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3684 repl_to, STRLEN(repl_to)) != 0)
3685 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003686 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003687 if (p == NULL)
3688 break;
3689 mch_memmove(p, line, curwin->w_cursor.col);
3690 STRCPY(p + curwin->w_cursor.col, repl_to);
3691 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3692 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3693 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003694
3695 if (curwin->w_cursor.lnum != prev_lnum)
3696 {
3697 ++sub_nlines;
3698 prev_lnum = curwin->w_cursor.lnum;
3699 }
3700 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003701 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003702 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003703 }
3704
3705 p_ws = save_ws;
3706 curwin->w_cursor = pos;
3707 vim_free(frompat);
3708
Bram Moolenaar5195e452005-08-19 20:32:47 +00003709 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003710 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003711 else
3712 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003713}
3714
3715/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003716 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3717 * a list of allocated strings.
3718 */
3719 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003720spell_suggest_list(
3721 garray_T *gap,
3722 char_u *word,
3723 int maxcount, /* maximum nr of suggestions */
3724 int need_cap, /* 'spellcapcheck' matched */
3725 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003726{
3727 suginfo_T sug;
3728 int i;
3729 suggest_T *stp;
3730 char_u *wcopy;
3731
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003732 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003733
3734 /* Make room in "gap". */
3735 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003736 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003737 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003738 for (i = 0; i < sug.su_ga.ga_len; ++i)
3739 {
3740 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003741
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003742 /* The suggested word may replace only part of "word", add the not
3743 * replaced part. */
3744 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003745 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003746 if (wcopy == NULL)
3747 break;
3748 STRCPY(wcopy, stp->st_word);
3749 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3750 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3751 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752 }
3753
3754 spell_find_cleanup(&sug);
3755}
3756
3757/*
3758 * Find spell suggestions for the word at the start of "badptr".
3759 * Return the suggestions in "su->su_ga".
3760 * The maximum number of suggestions is "maxcount".
3761 * Note: does use info for the current window.
3762 * This is based on the mechanisms of Aspell, but completely reimplemented.
3763 */
3764 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003765spell_find_suggest(
3766 char_u *badptr,
3767 int badlen, /* length of bad word or 0 if unknown */
3768 suginfo_T *su,
3769 int maxcount,
3770 int banbadword, /* don't include badword in suggestions */
3771 int need_cap, /* word should start with capital */
3772 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003773{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003774 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003775 char_u buf[MAXPATHL];
3776 char_u *p;
3777 int do_combine = FALSE;
3778 char_u *sps_copy;
3779#ifdef FEAT_EVAL
3780 static int expr_busy = FALSE;
3781#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003782 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003783 int i;
3784 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003785
3786 /*
3787 * Set the info in "*su".
3788 */
3789 vim_memset(su, 0, sizeof(suginfo_T));
3790 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3791 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003792 if (*badptr == NUL)
3793 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003794 hash_init(&su->su_banned);
3795
3796 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003797 if (badlen != 0)
3798 su->su_badlen = badlen;
3799 else
3800 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003801 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003802 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003803
3804 if (su->su_badlen >= MAXWLEN)
3805 su->su_badlen = MAXWLEN - 1; /* just in case */
3806 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3807 (void)spell_casefold(su->su_badptr, su->su_badlen,
3808 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003809 /* TODO: make this work if the case-folded text is longer than the original
3810 * text. Currently an illegal byte causes wrong pointer computations. */
3811 su->su_fbadword[su->su_badlen] = NUL;
3812
Bram Moolenaar0c405862005-06-22 22:26:26 +00003813 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003814 su->su_badflags = badword_captype(su->su_badptr,
3815 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003816 if (need_cap)
3817 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003818
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003819 /* Find the default language for sound folding. We simply use the first
3820 * one in 'spelllang' that supports sound folding. That's good for when
3821 * using multiple files for one language, it's not that bad when mixing
3822 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003823 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003824 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003825 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003826 if (lp->lp_sallang != NULL)
3827 {
3828 su->su_sallang = lp->lp_sallang;
3829 break;
3830 }
3831 }
3832
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003833 /* Soundfold the bad word with the default sound folding, so that we don't
3834 * have to do this many times. */
3835 if (su->su_sallang != NULL)
3836 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3837 su->su_sal_badword);
3838
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003839 /* If the word is not capitalised and spell_check() doesn't consider the
3840 * word to be bad then it might need to be capitalised. Add a suggestion
3841 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003842 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003843 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003844 {
3845 make_case_word(su->su_badword, buf, WF_ONECAP);
3846 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003847 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003848 }
3849
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003850 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003851 if (banbadword)
3852 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003853
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003854 /* Make a copy of 'spellsuggest', because the expression may change it. */
3855 sps_copy = vim_strsave(p_sps);
3856 if (sps_copy == NULL)
3857 return;
3858
3859 /* Loop over the items in 'spellsuggest'. */
3860 for (p = sps_copy; *p != NUL; )
3861 {
3862 copy_option_part(&p, buf, MAXPATHL, ",");
3863
3864 if (STRNCMP(buf, "expr:", 5) == 0)
3865 {
3866#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003867 /* Evaluate an expression. Skip this when called recursively,
3868 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003869 if (!expr_busy)
3870 {
3871 expr_busy = TRUE;
3872 spell_suggest_expr(su, buf + 5);
3873 expr_busy = FALSE;
3874 }
3875#endif
3876 }
3877 else if (STRNCMP(buf, "file:", 5) == 0)
3878 /* Use list of suggestions in a file. */
3879 spell_suggest_file(su, buf + 5);
3880 else
3881 {
3882 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003883 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003884 if (sps_flags & SPS_DOUBLE)
3885 do_combine = TRUE;
3886 }
3887 }
3888
3889 vim_free(sps_copy);
3890
3891 if (do_combine)
3892 /* Combine the two list of suggestions. This must be done last,
3893 * because sorting changes the order again. */
3894 score_combine(su);
3895}
3896
3897#ifdef FEAT_EVAL
3898/*
3899 * Find suggestions by evaluating expression "expr".
3900 */
3901 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003902spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003903{
3904 list_T *list;
3905 listitem_T *li;
3906 int score;
3907 char_u *p;
3908
3909 /* The work is split up in a few parts to avoid having to export
3910 * suginfo_T.
3911 * First evaluate the expression and get the resulting list. */
3912 list = eval_spell_expr(su->su_badword, expr);
3913 if (list != NULL)
3914 {
3915 /* Loop over the items in the list. */
3916 for (li = list->lv_first; li != NULL; li = li->li_next)
3917 if (li->li_tv.v_type == VAR_LIST)
3918 {
3919 /* Get the word and the score from the items. */
3920 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003921 if (score >= 0 && score <= su->su_maxscore)
3922 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3923 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003924 }
3925 list_unref(list);
3926 }
3927
Bram Moolenaar4770d092006-01-12 23:22:24 +00003928 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3929 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003930 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3931}
3932#endif
3933
3934/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003935 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003936 */
3937 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003938spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003939{
3940 FILE *fd;
3941 char_u line[MAXWLEN * 2];
3942 char_u *p;
3943 int len;
3944 char_u cword[MAXWLEN];
3945
3946 /* Open the file. */
3947 fd = mch_fopen((char *)fname, "r");
3948 if (fd == NULL)
3949 {
3950 EMSG2(_(e_notopen), fname);
3951 return;
3952 }
3953
3954 /* Read it line by line. */
3955 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3956 {
3957 line_breakcheck();
3958
3959 p = vim_strchr(line, '/');
3960 if (p == NULL)
3961 continue; /* No Tab found, just skip the line. */
3962 *p++ = NUL;
3963 if (STRICMP(su->su_badword, line) == 0)
3964 {
3965 /* Match! Isolate the good word, until CR or NL. */
3966 for (len = 0; p[len] >= ' '; ++len)
3967 ;
3968 p[len] = NUL;
3969
3970 /* If the suggestion doesn't have specific case duplicate the case
3971 * of the bad word. */
3972 if (captype(p, NULL) == 0)
3973 {
3974 make_case_word(p, cword, su->su_badflags);
3975 p = cword;
3976 }
3977
3978 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003979 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003980 }
3981 }
3982
3983 fclose(fd);
3984
Bram Moolenaar4770d092006-01-12 23:22:24 +00003985 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3986 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003987 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3988}
3989
3990/*
3991 * Find suggestions for the internal method indicated by "sps_flags".
3992 */
3993 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003994spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003995{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003996 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003997 * Load the .sug file(s) that are available and not done yet.
3998 */
3999 suggest_load_files();
4000
4001 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004002 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004003 *
4004 * Set a maximum score to limit the combination of operations that is
4005 * tried.
4006 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004007 suggest_try_special(su);
4008
4009 /*
4010 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
4011 * from the .aff file and inserting a space (split the word).
4012 */
4013 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004014
4015 /* For the resulting top-scorers compute the sound-a-like score. */
4016 if (sps_flags & SPS_DOUBLE)
4017 score_comp_sal(su);
4018
4019 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004020 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004021 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004022 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004023 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004024 if (sps_flags & SPS_BEST)
4025 /* Adjust the word score for the suggestions found so far for how
4026 * they sounds like. */
4027 rescore_suggestions(su);
4028
4029 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004030 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00004031 * for the soundfold word, limits the changes that are being tried,
4032 * and "su_sfmaxscore" the rescored score, which is set by
4033 * cleanup_suggestions().
4034 * First find words with a small edit distance, because this is much
4035 * faster and often already finds the top-N suggestions. If we didn't
4036 * find many suggestions try again with a higher edit distance.
4037 * "sl_sounddone" is used to avoid doing the same word twice.
4038 */
4039 suggest_try_soundalike_prep();
4040 su->su_maxscore = SCORE_SFMAX1;
4041 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004042 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004043 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4044 {
4045 /* We didn't find enough matches, try again, allowing more
4046 * changes to the soundfold word. */
4047 su->su_maxscore = SCORE_SFMAX2;
4048 suggest_try_soundalike(su);
4049 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4050 {
4051 /* Still didn't find enough matches, try again, allowing even
4052 * more changes to the soundfold word. */
4053 su->su_maxscore = SCORE_SFMAX3;
4054 suggest_try_soundalike(su);
4055 }
4056 }
4057 su->su_maxscore = su->su_sfmaxscore;
4058 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004059 }
4060
Bram Moolenaar4770d092006-01-12 23:22:24 +00004061 /* When CTRL-C was hit while searching do show the results. Only clear
4062 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004063 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00004064 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004065 {
4066 (void)vgetc();
4067 got_int = FALSE;
4068 }
4069
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004070 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004071 {
4072 if (sps_flags & SPS_BEST)
4073 /* Adjust the word score for how it sounds like. */
4074 rescore_suggestions(su);
4075
Bram Moolenaar4770d092006-01-12 23:22:24 +00004076 /* Remove bogus suggestions, sort and truncate at "maxcount". */
4077 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004078 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004079 }
4080}
4081
4082/*
4083 * Free the info put in "*su" by spell_find_suggest().
4084 */
4085 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004086spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004087{
4088 int i;
4089
4090 /* Free the suggestions. */
4091 for (i = 0; i < su->su_ga.ga_len; ++i)
4092 vim_free(SUG(su->su_ga, i).st_word);
4093 ga_clear(&su->su_ga);
4094 for (i = 0; i < su->su_sga.ga_len; ++i)
4095 vim_free(SUG(su->su_sga, i).st_word);
4096 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004097
4098 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004099 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100}
4101
4102/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004103 * Make a copy of "word", with the first letter upper or lower cased, to
4104 * "wcopy[MAXWLEN]". "word" must not be empty.
4105 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004107 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004108onecap_copy(
4109 char_u *word,
4110 char_u *wcopy,
4111 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112{
4113 char_u *p;
4114 int c;
4115 int l;
4116
4117 p = word;
4118#ifdef FEAT_MBYTE
4119 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004120 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 else
4122#endif
4123 c = *p++;
4124 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004125 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004126 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004127 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128#ifdef FEAT_MBYTE
4129 if (has_mbyte)
4130 l = mb_char2bytes(c, wcopy);
4131 else
4132#endif
4133 {
4134 l = 1;
4135 wcopy[0] = c;
4136 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004137 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138}
4139
4140/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004141 * Make a copy of "word" with all the letters upper cased into
4142 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 */
4144 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004145allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004146{
4147 char_u *s;
4148 char_u *d;
4149 int c;
4150
4151 d = wcopy;
4152 for (s = word; *s != NUL; )
4153 {
4154#ifdef FEAT_MBYTE
4155 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004156 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 else
4158#endif
4159 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004160
4161#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +02004162 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004163 * would cause weird errors in other 8-bit encodings. */
4164 if (enc_latin1like && c == 0xdf)
4165 {
4166 c = 'S';
4167 if (d - wcopy >= MAXWLEN - 1)
4168 break;
4169 *d++ = c;
4170 }
4171 else
4172#endif
4173 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004174
4175#ifdef FEAT_MBYTE
4176 if (has_mbyte)
4177 {
4178 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4179 break;
4180 d += mb_char2bytes(c, d);
4181 }
4182 else
4183#endif
4184 {
4185 if (d - wcopy >= MAXWLEN - 1)
4186 break;
4187 *d++ = c;
4188 }
4189 }
4190 *d = NUL;
4191}
4192
4193/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004194 * Try finding suggestions by recognizing specific situations.
4195 */
4196 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004197suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004198{
4199 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004200 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004201 int c;
4202 char_u word[MAXWLEN];
4203
4204 /*
4205 * Recognize a word that is repeated: "the the".
4206 */
4207 p = skiptowhite(su->su_fbadword);
4208 len = p - su->su_fbadword;
4209 p = skipwhite(p);
4210 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4211 {
4212 /* Include badflags: if the badword is onecap or allcap
4213 * use that for the goodword too: "The the" -> "The". */
4214 c = su->su_fbadword[len];
4215 su->su_fbadword[len] = NUL;
4216 make_case_word(su->su_fbadword, word, su->su_badflags);
4217 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004218
4219 /* Give a soundalike score of 0, compute the score as if deleting one
4220 * character. */
4221 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004222 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004223 }
4224}
4225
4226/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004227 * Change the 0 to 1 to measure how much time is spent in each state.
4228 * Output is dumped in "suggestprof".
4229 */
4230#if 0
4231# define SUGGEST_PROFILE
4232proftime_T current;
4233proftime_T total;
4234proftime_T times[STATE_FINAL + 1];
4235long counts[STATE_FINAL + 1];
4236
4237 static void
4238prof_init(void)
4239{
4240 for (int i = 0; i <= STATE_FINAL; ++i)
4241 {
4242 profile_zero(&times[i]);
4243 counts[i] = 0;
4244 }
4245 profile_start(&current);
4246 profile_start(&total);
4247}
4248
4249/* call before changing state */
4250 static void
4251prof_store(state_T state)
4252{
4253 profile_end(&current);
4254 profile_add(&times[state], &current);
4255 ++counts[state];
4256 profile_start(&current);
4257}
4258# define PROF_STORE(state) prof_store(state);
4259
4260 static void
4261prof_report(char *name)
4262{
4263 FILE *fd = fopen("suggestprof", "a");
4264
4265 profile_end(&total);
4266 fprintf(fd, "-----------------------\n");
4267 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4268 for (int i = 0; i <= STATE_FINAL; ++i)
4269 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4270 fclose(fd);
4271}
4272#else
4273# define PROF_STORE(state)
4274#endif
4275
4276/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 * Try finding suggestions by adding/removing/swapping letters.
4278 */
4279 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004280suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281{
4282 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004283 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004285 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004286 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287
4288 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004289 * to find matches (esp. REP items). Append some more text, changing
4290 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004292 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004293 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004294 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295
Bram Moolenaar860cae12010-06-05 23:22:07 +02004296 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004298 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004299
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004300 /* If reloading a spell file fails it's still in the list but
4301 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004302 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004303 continue;
4304
Bram Moolenaar4770d092006-01-12 23:22:24 +00004305 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004306#ifdef SUGGEST_PROFILE
4307 prof_init();
4308#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004309 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004310#ifdef SUGGEST_PROFILE
4311 prof_report("try_change");
4312#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004313 }
4314}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315
Bram Moolenaar4770d092006-01-12 23:22:24 +00004316/* Check the maximum score, if we go over it we won't try this change. */
4317#define TRY_DEEPER(su, stack, depth, add) \
4318 (stack[depth].ts_score + (add) < su->su_maxscore)
4319
4320/*
4321 * Try finding suggestions by adding/removing/swapping letters.
4322 *
4323 * This uses a state machine. At each node in the tree we try various
4324 * operations. When trying if an operation works "depth" is increased and the
4325 * stack[] is used to store info. This allows combinations, thus insert one
4326 * character, replace one and delete another. The number of changes is
4327 * limited by su->su_maxscore.
4328 *
4329 * After implementing this I noticed an article by Kemal Oflazer that
4330 * describes something similar: "Error-tolerant Finite State Recognition with
4331 * Applications to Morphological Analysis and Spelling Correction" (1996).
4332 * The implementation in the article is simplified and requires a stack of
4333 * unknown depth. The implementation here only needs a stack depth equal to
4334 * the length of the word.
4335 *
4336 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4337 * The mechanism is the same, but we find a match with a sound-folded word
4338 * that comes from one or more original words. Each of these words may be
4339 * added, this is done by add_sound_suggest().
4340 * Don't use:
4341 * the prefix tree or the keep-case tree
4342 * "su->su_badlen"
4343 * anything to do with upper and lower case
4344 * anything to do with word or non-word characters ("spell_iswordp()")
4345 * banned words
4346 * word flags (rare, region, compounding)
4347 * word splitting for now
4348 * "similar_chars()"
4349 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4350 */
4351 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004352suggest_trie_walk(
4353 suginfo_T *su,
4354 langp_T *lp,
4355 char_u *fword,
4356 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004357{
4358 char_u tword[MAXWLEN]; /* good word collected so far */
4359 trystate_T stack[MAXWLEN];
4360 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004361 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004362 * words and split word. NUL terminated
4363 * when going deeper but not when coming
4364 * back. */
4365 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4366 trystate_T *sp;
4367 int newscore;
4368 int score;
4369 char_u *byts, *fbyts, *pbyts;
4370 idx_T *idxs, *fidxs, *pidxs;
4371 int depth;
4372 int c, c2, c3;
4373 int n = 0;
4374 int flags;
4375 garray_T *gap;
4376 idx_T arridx;
4377 int len;
4378 char_u *p;
4379 fromto_T *ftp;
4380 int fl = 0, tl;
4381 int repextra = 0; /* extra bytes in fword[] from REP item */
4382 slang_T *slang = lp->lp_slang;
4383 int fword_ends;
4384 int goodword_ends;
4385#ifdef DEBUG_TRIEWALK
4386 /* Stores the name of the change made at each level. */
4387 char_u changename[MAXWLEN][80];
4388#endif
4389 int breakcheckcount = 1000;
4390 int compound_ok;
4391
4392 /*
4393 * Go through the whole case-fold tree, try changes at each node.
4394 * "tword[]" contains the word collected from nodes in the tree.
4395 * "fword[]" the word we are trying to match with (initially the bad
4396 * word).
4397 */
4398 depth = 0;
4399 sp = &stack[0];
4400 vim_memset(sp, 0, sizeof(trystate_T));
4401 sp->ts_curi = 1;
4402
4403 if (soundfold)
4404 {
4405 /* Going through the soundfold tree. */
4406 byts = fbyts = slang->sl_sbyts;
4407 idxs = fidxs = slang->sl_sidxs;
4408 pbyts = NULL;
4409 pidxs = NULL;
4410 sp->ts_prefixdepth = PFD_NOPREFIX;
4411 sp->ts_state = STATE_START;
4412 }
4413 else
4414 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004415 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004416 * When there are postponed prefixes we need to use these first. At
4417 * the end of the prefix we continue in the case-fold tree.
4418 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004419 fbyts = slang->sl_fbyts;
4420 fidxs = slang->sl_fidxs;
4421 pbyts = slang->sl_pbyts;
4422 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004423 if (pbyts != NULL)
4424 {
4425 byts = pbyts;
4426 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004427 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004428 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4429 }
4430 else
4431 {
4432 byts = fbyts;
4433 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004434 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004435 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004436 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004437 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004438
Bram Moolenaar4770d092006-01-12 23:22:24 +00004439 /*
4440 * Loop to find all suggestions. At each round we either:
4441 * - For the current state try one operation, advance "ts_curi",
4442 * increase "depth".
4443 * - When a state is done go to the next, set "ts_state".
4444 * - When all states are tried decrease "depth".
4445 */
4446 while (depth >= 0 && !got_int)
4447 {
4448 sp = &stack[depth];
4449 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004450 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004451 case STATE_START:
4452 case STATE_NOPREFIX:
4453 /*
4454 * Start of node: Deal with NUL bytes, which means
4455 * tword[] may end here.
4456 */
4457 arridx = sp->ts_arridx; /* current node in the tree */
4458 len = byts[arridx]; /* bytes in this node */
4459 arridx += sp->ts_curi; /* index of current byte */
4460
4461 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004462 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004463 /* Skip over the NUL bytes, we use them later. */
4464 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4465 ;
4466 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004467
Bram Moolenaar4770d092006-01-12 23:22:24 +00004468 /* Always past NUL bytes now. */
4469 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004470 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004471 sp->ts_state = STATE_ENDNUL;
4472 sp->ts_save_badflags = su->su_badflags;
4473
4474 /* At end of a prefix or at start of prefixtree: check for
4475 * following word. */
4476 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004477 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004478 /* Set su->su_badflags to the caps type at this position.
4479 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004480#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004481 if (has_mbyte)
4482 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4483 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004484#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004485 n = sp->ts_fidx;
4486 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4487 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004488 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004489#ifdef DEBUG_TRIEWALK
4490 sprintf(changename[depth], "prefix");
4491#endif
4492 go_deeper(stack, depth, 0);
4493 ++depth;
4494 sp = &stack[depth];
4495 sp->ts_prefixdepth = depth - 1;
4496 byts = fbyts;
4497 idxs = fidxs;
4498 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004499
Bram Moolenaar4770d092006-01-12 23:22:24 +00004500 /* Move the prefix to preword[] with the right case
4501 * and make find_keepcap_word() works. */
4502 tword[sp->ts_twordlen] = NUL;
4503 make_case_word(tword + sp->ts_splitoff,
4504 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004505 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004506 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004507 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004508 break;
4509 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004510
Bram Moolenaar4770d092006-01-12 23:22:24 +00004511 if (sp->ts_curi > len || byts[arridx] != 0)
4512 {
4513 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004514 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004515 sp->ts_state = STATE_ENDNUL;
4516 sp->ts_save_badflags = su->su_badflags;
4517 break;
4518 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004519
Bram Moolenaar4770d092006-01-12 23:22:24 +00004520 /*
4521 * End of word in tree.
4522 */
4523 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004524
Bram Moolenaar4770d092006-01-12 23:22:24 +00004525 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004526
4527 /* Skip words with the NOSUGGEST flag. */
4528 if (flags & WF_NOSUGGEST)
4529 break;
4530
Bram Moolenaar4770d092006-01-12 23:22:24 +00004531 fword_ends = (fword[sp->ts_fidx] == NUL
4532 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004533 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004534 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004535 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004536
Bram Moolenaar4770d092006-01-12 23:22:24 +00004537 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004538 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004539 {
4540 /* There was a prefix before the word. Check that the prefix
4541 * can be used with this word. */
4542 /* Count the length of the NULs in the prefix. If there are
4543 * none this must be the first try without a prefix. */
4544 n = stack[sp->ts_prefixdepth].ts_arridx;
4545 len = pbyts[n++];
4546 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4547 ;
4548 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004549 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004550 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004551 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004552 if (c == 0)
4553 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004554
Bram Moolenaar4770d092006-01-12 23:22:24 +00004555 /* Use the WF_RARE flag for a rare prefix. */
4556 if (c & WF_RAREPFX)
4557 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004558
Bram Moolenaar4770d092006-01-12 23:22:24 +00004559 /* Tricky: when checking for both prefix and compounding
4560 * we run into the prefix flag first.
4561 * Remember that it's OK, so that we accept the prefix
4562 * when arriving at a compound flag. */
4563 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004564 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004565 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004566
Bram Moolenaar4770d092006-01-12 23:22:24 +00004567 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4568 * appending another compound word below. */
4569 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004570 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004571 goodword_ends = FALSE;
4572 else
4573 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004574
Bram Moolenaar4770d092006-01-12 23:22:24 +00004575 p = NULL;
4576 compound_ok = TRUE;
4577 if (sp->ts_complen > sp->ts_compsplit)
4578 {
4579 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004580 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004581 /* There was a word before this word. When there was no
4582 * change in this word (it was correct) add the first word
4583 * as a suggestion. If this word was corrected too, we
4584 * need to check if a correct word follows. */
4585 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004586 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004587 && STRNCMP(fword + sp->ts_splitfidx,
4588 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004589 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004590 {
4591 preword[sp->ts_prewordlen] = NUL;
4592 newscore = score_wordcount_adj(slang, sp->ts_score,
4593 preword + sp->ts_prewordlen,
4594 sp->ts_prewordlen > 0);
4595 /* Add the suggestion if the score isn't too bad. */
4596 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004597 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004598 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004599 newscore, 0, FALSE,
4600 lp->lp_sallang, FALSE);
4601 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004602 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004603 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004604 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004605 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004606 /* There was a compound word before this word. If this
4607 * word does not support compounding then give up
4608 * (splitting is tried for the word without compound
4609 * flag). */
4610 if (((unsigned)flags >> 24) == 0
4611 || sp->ts_twordlen - sp->ts_splitoff
4612 < slang->sl_compminlen)
4613 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004614#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004615 /* For multi-byte chars check character length against
4616 * COMPOUNDMIN. */
4617 if (has_mbyte
4618 && slang->sl_compminlen > 0
4619 && mb_charlen(tword + sp->ts_splitoff)
4620 < slang->sl_compminlen)
4621 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004622#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00004623
Bram Moolenaar4770d092006-01-12 23:22:24 +00004624 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4625 compflags[sp->ts_complen + 1] = NUL;
4626 vim_strncpy(preword + sp->ts_prewordlen,
4627 tword + sp->ts_splitoff,
4628 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004629
4630 /* Verify CHECKCOMPOUNDPATTERN rules. */
4631 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4632 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004633 compound_ok = FALSE;
4634
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004635 if (compound_ok)
4636 {
4637 p = preword;
4638 while (*skiptowhite(p) != NUL)
4639 p = skipwhite(skiptowhite(p));
4640 if (fword_ends && !can_compound(slang, p,
4641 compflags + sp->ts_compsplit))
4642 /* Compound is not allowed. But it may still be
4643 * possible if we add another (short) word. */
4644 compound_ok = FALSE;
4645 }
4646
Bram Moolenaar4770d092006-01-12 23:22:24 +00004647 /* Get pointer to last char of previous word. */
4648 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004649 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004650 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004651 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652
Bram Moolenaar4770d092006-01-12 23:22:24 +00004653 /*
4654 * Form the word with proper case in preword.
4655 * If there is a word from a previous split, append.
4656 * For the soundfold tree don't change the case, simply append.
4657 */
4658 if (soundfold)
4659 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4660 else if (flags & WF_KEEPCAP)
4661 /* Must find the word in the keep-case tree. */
4662 find_keepcap_word(slang, tword + sp->ts_splitoff,
4663 preword + sp->ts_prewordlen);
4664 else
4665 {
4666 /* Include badflags: If the badword is onecap or allcap
4667 * use that for the goodword too. But if the badword is
4668 * allcap and it's only one char long use onecap. */
4669 c = su->su_badflags;
4670 if ((c & WF_ALLCAP)
4671#ifdef FEAT_MBYTE
4672 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
4673#else
4674 && su->su_badlen == 1
4675#endif
4676 )
4677 c = WF_ONECAP;
4678 c |= flags;
4679
4680 /* When appending a compound word after a word character don't
4681 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004682 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004683 c &= ~WF_ONECAP;
4684 make_case_word(tword + sp->ts_splitoff,
4685 preword + sp->ts_prewordlen, c);
4686 }
4687
4688 if (!soundfold)
4689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 /* Don't use a banned word. It may appear again as a good
4691 * word, thus remember it. */
4692 if (flags & WF_BANNED)
4693 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004694 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004695 break;
4696 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004697 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004698 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4699 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004700 {
4701 if (slang->sl_compprog == NULL)
4702 break;
4703 /* the word so far was banned but we may try compounding */
4704 goodword_ends = FALSE;
4705 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707
Bram Moolenaar4770d092006-01-12 23:22:24 +00004708 newscore = 0;
4709 if (!soundfold) /* soundfold words don't have flags */
4710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004712 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 newscore += SCORE_REGION;
4714 if (flags & WF_RARE)
4715 newscore += SCORE_RARE;
4716
Bram Moolenaar0c405862005-06-22 22:26:26 +00004717 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004718 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004719 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004720 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721
Bram Moolenaar4770d092006-01-12 23:22:24 +00004722 /* TODO: how about splitting in the soundfold tree? */
4723 if (fword_ends
4724 && goodword_ends
4725 && sp->ts_fidx >= sp->ts_fidxtry
4726 && compound_ok)
4727 {
4728 /* The badword also ends: add suggestions. */
4729#ifdef DEBUG_TRIEWALK
4730 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004732 int j;
4733
4734 /* print the stack of changes that brought us here */
4735 smsg("------ %s -------", fword);
4736 for (j = 0; j < depth; ++j)
4737 smsg("%s", changename[j]);
4738 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004739#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004740 if (soundfold)
4741 {
4742 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004743 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004744 add_sound_suggest(su, preword, sp->ts_score, lp);
4745 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004746 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004747 {
4748 /* Give a penalty when changing non-word char to word
4749 * char, e.g., "thes," -> "these". */
4750 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004751 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004752 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004753 {
4754 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004755 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004756 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004757 newscore += SCORE_NONWORD;
4758 }
4759
Bram Moolenaar4770d092006-01-12 23:22:24 +00004760 /* Give a bonus to words seen before. */
4761 score = score_wordcount_adj(slang,
4762 sp->ts_score + newscore,
4763 preword + sp->ts_prewordlen,
4764 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004765
Bram Moolenaar4770d092006-01-12 23:22:24 +00004766 /* Add the suggestion if the score isn't too bad. */
4767 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004768 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004769 add_suggestion(su, &su->su_ga, preword,
4770 sp->ts_fidx - repextra,
4771 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004772
4773 if (su->su_badflags & WF_MIXCAP)
4774 {
4775 /* We really don't know if the word should be
4776 * upper or lower case, add both. */
4777 c = captype(preword, NULL);
4778 if (c == 0 || c == WF_ALLCAP)
4779 {
4780 make_case_word(tword + sp->ts_splitoff,
4781 preword + sp->ts_prewordlen,
4782 c == 0 ? WF_ALLCAP : 0);
4783
4784 add_suggestion(su, &su->su_ga, preword,
4785 sp->ts_fidx - repextra,
4786 score + SCORE_ICASE, 0, FALSE,
4787 lp->lp_sallang, FALSE);
4788 }
4789 }
4790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004792 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004793
Bram Moolenaar4770d092006-01-12 23:22:24 +00004794 /*
4795 * Try word split and/or compounding.
4796 */
4797 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00004798#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004799 /* Don't split halfway a character. */
4800 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004801#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004802 )
4803 {
4804 int try_compound;
4805 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004806
Bram Moolenaar4770d092006-01-12 23:22:24 +00004807 /* If past the end of the bad word don't try a split.
4808 * Otherwise try changing the next word. E.g., find
4809 * suggestions for "the the" where the second "the" is
4810 * different. It's done like a split.
4811 * TODO: word split for soundfold words */
4812 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4813 && !soundfold;
4814
4815 /* Get here in several situations:
4816 * 1. The word in the tree ends:
4817 * If the word allows compounding try that. Otherwise try
4818 * a split by inserting a space. For both check that a
4819 * valid words starts at fword[sp->ts_fidx].
4820 * For NOBREAK do like compounding to be able to check if
4821 * the next word is valid.
4822 * 2. The badword does end, but it was due to a change (e.g.,
4823 * a swap). No need to split, but do check that the
4824 * following word is valid.
4825 * 3. The badword and the word in the tree end. It may still
4826 * be possible to compound another (short) word.
4827 */
4828 try_compound = FALSE;
4829 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004830 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004831 && slang->sl_compprog != NULL
4832 && ((unsigned)flags >> 24) != 0
4833 && sp->ts_twordlen - sp->ts_splitoff
4834 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004835#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004836 && (!has_mbyte
4837 || slang->sl_compminlen == 0
4838 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004839 >= slang->sl_compminlen)
4840#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004841 && (slang->sl_compsylmax < MAXWLEN
4842 || sp->ts_complen + 1 - sp->ts_compsplit
4843 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004844 && (can_be_compound(sp, slang,
4845 compflags, ((unsigned)flags >> 24))))
4846
Bram Moolenaar4770d092006-01-12 23:22:24 +00004847 {
4848 try_compound = TRUE;
4849 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4850 compflags[sp->ts_complen + 1] = NUL;
4851 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004852
Bram Moolenaar4770d092006-01-12 23:22:24 +00004853 /* For NOBREAK we never try splitting, it won't make any word
4854 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004855 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004856 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004857
Bram Moolenaar4770d092006-01-12 23:22:24 +00004858 /* If we could add a compound word, and it's also possible to
4859 * split at this point, do the split first and set
4860 * TSF_DIDSPLIT to avoid doing it again. */
4861 else if (!fword_ends
4862 && try_compound
4863 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4864 {
4865 try_compound = FALSE;
4866 sp->ts_flags |= TSF_DIDSPLIT;
4867 --sp->ts_curi; /* do the same NUL again */
4868 compflags[sp->ts_complen] = NUL;
4869 }
4870 else
4871 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004872
Bram Moolenaar4770d092006-01-12 23:22:24 +00004873 if (try_split || try_compound)
4874 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004875 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004876 {
4877 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004878 * words so far are valid for compounding. If there
4879 * is only one word it must not have the NEEDCOMPOUND
4880 * flag. */
4881 if (sp->ts_complen == sp->ts_compsplit
4882 && (flags & WF_NEEDCOMP))
4883 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004884 p = preword;
4885 while (*skiptowhite(p) != NUL)
4886 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004887 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004888 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004889 compflags + sp->ts_compsplit))
4890 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004891
4892 if (slang->sl_nosplitsugs)
4893 newscore += SCORE_SPLIT_NO;
4894 else
4895 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004896
4897 /* Give a bonus to words seen before. */
4898 newscore = score_wordcount_adj(slang, newscore,
4899 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004900 }
4901
Bram Moolenaar4770d092006-01-12 23:22:24 +00004902 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004903 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004904 go_deeper(stack, depth, newscore);
4905#ifdef DEBUG_TRIEWALK
4906 if (!try_compound && !fword_ends)
4907 sprintf(changename[depth], "%.*s-%s: split",
4908 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4909 else
4910 sprintf(changename[depth], "%.*s-%s: compound",
4911 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4912#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004914 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004915 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004916 sp->ts_state = STATE_SPLITUNDO;
4917
4918 ++depth;
4919 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004921 /* Append a space to preword when splitting. */
4922 if (!try_compound && !fword_ends)
4923 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004924 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004925 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004926 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004927
4928 /* If the badword has a non-word character at this
4929 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004930 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004931 * character when the word ends. But only when the
4932 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004933 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004934 + sp->ts_fidx,
4935 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004936 || fword_ends)
4937 && fword[sp->ts_fidx] != NUL
4938 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004939 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004940 int l;
4941
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004942 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004943 if (fword_ends)
4944 {
4945 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004946 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004947 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004948 sp->ts_prewordlen += l;
4949 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004950 }
4951 else
4952 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4953 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004954 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004955
Bram Moolenaard12a1322005-08-21 22:08:24 +00004956 /* When compounding include compound flag in
4957 * compflags[] (already set above). When splitting we
4958 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004959 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004960 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004961 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004962 sp->ts_compsplit = sp->ts_complen;
4963 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004964
Bram Moolenaar53805d12005-08-01 07:08:33 +00004965 /* set su->su_badflags to the caps type at this
4966 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004967#ifdef FEAT_MBYTE
4968 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004969 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004970 else
4971#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004972 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004973 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004974 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004976 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004977 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004978
4979 /* If there are postponed prefixes, try these too. */
4980 if (pbyts != NULL)
4981 {
4982 byts = pbyts;
4983 idxs = pidxs;
4984 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004985 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004986 sp->ts_state = STATE_NOPREFIX;
4987 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004988 }
4989 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004990 }
4991 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004992
Bram Moolenaar4770d092006-01-12 23:22:24 +00004993 case STATE_SPLITUNDO:
4994 /* Undo the changes done for word split or compound word. */
4995 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004996
Bram Moolenaar4770d092006-01-12 23:22:24 +00004997 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004998 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004999 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00005000
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001 /* In case we went into the prefix tree. */
5002 byts = fbyts;
5003 idxs = fidxs;
5004 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005005
Bram Moolenaar4770d092006-01-12 23:22:24 +00005006 case STATE_ENDNUL:
5007 /* Past the NUL bytes in the node. */
5008 su->su_badflags = sp->ts_save_badflags;
5009 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005010#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005011 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005012#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005013 )
5014 {
5015 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005016 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005017 sp->ts_state = STATE_DEL;
5018 break;
5019 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005020 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005021 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005022 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005023
5024 case STATE_PLAIN:
5025 /*
5026 * Go over all possible bytes at this node, add each to tword[]
5027 * and use child node. "ts_curi" is the index.
5028 */
5029 arridx = sp->ts_arridx;
5030 if (sp->ts_curi > byts[arridx])
5031 {
5032 /* Done all bytes at this node, do next state. When still at
5033 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005034 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005035 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005037 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005038 sp->ts_state = STATE_FINAL;
5039 }
5040 else
5041 {
5042 arridx += sp->ts_curi++;
5043 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005044
Bram Moolenaar4770d092006-01-12 23:22:24 +00005045 /* Normal byte, go one level deeper. If it's not equal to the
5046 * byte in the bad word adjust the score. But don't even try
5047 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01005048 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00005049 * delete + substitute. */
5050 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +00005051#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005052 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005053#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005054 )
5055 newscore = 0;
5056 else
5057 newscore = SCORE_SUBST;
5058 if ((newscore == 0
5059 || (sp->ts_fidx >= sp->ts_fidxtry
5060 && ((sp->ts_flags & TSF_DIDDEL) == 0
5061 || c != fword[sp->ts_delidx])))
5062 && TRY_DEEPER(su, stack, depth, newscore))
5063 {
5064 go_deeper(stack, depth, newscore);
5065#ifdef DEBUG_TRIEWALK
5066 if (newscore > 0)
5067 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
5068 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5069 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005070 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005071 sprintf(changename[depth], "%.*s-%s: accept %c",
5072 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5073 fword[sp->ts_fidx]);
5074#endif
5075 ++depth;
5076 sp = &stack[depth];
5077 ++sp->ts_fidx;
5078 tword[sp->ts_twordlen++] = c;
5079 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +00005080#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005081 if (newscore == SCORE_SUBST)
5082 sp->ts_isdiff = DIFF_YES;
5083 if (has_mbyte)
5084 {
5085 /* Multi-byte characters are a bit complicated to
5086 * handle: They differ when any of the bytes differ
5087 * and then their length may also differ. */
5088 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005089 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005090 /* First byte. */
5091 sp->ts_tcharidx = 0;
5092 sp->ts_tcharlen = MB_BYTE2LEN(c);
5093 sp->ts_fcharstart = sp->ts_fidx - 1;
5094 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005095 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005096 }
5097 else if (sp->ts_isdiff == DIFF_INSERT)
5098 /* When inserting trail bytes don't advance in the
5099 * bad word. */
5100 --sp->ts_fidx;
5101 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5102 {
5103 /* Last byte of character. */
5104 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00005105 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005106 /* Correct ts_fidx for the byte length of the
5107 * character (we didn't check that before). */
5108 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005109 + MB_PTR2LEN(
5110 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005111 /* For changing a composing character adjust
5112 * the score from SCORE_SUBST to
5113 * SCORE_SUBCOMP. */
5114 if (enc_utf8
5115 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005116 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00005117 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005118 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005119 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005120 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005121 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005122 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005123 SCORE_SUBST - SCORE_SUBCOMP;
5124
Bram Moolenaar4770d092006-01-12 23:22:24 +00005125 /* For a similar character adjust score from
5126 * SCORE_SUBST to SCORE_SIMILAR. */
5127 else if (!soundfold
5128 && slang->sl_has_map
5129 && similar_chars(slang,
5130 mb_ptr2char(tword
5131 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00005132 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00005133 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00005134 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005135 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00005136 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00005137 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005138 else if (sp->ts_isdiff == DIFF_INSERT
5139 && sp->ts_twordlen > sp->ts_tcharlen)
5140 {
5141 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
5142 c = mb_ptr2char(p);
5143 if (enc_utf8 && utf_iscomposing(c))
5144 {
5145 /* Inserting a composing char doesn't
5146 * count that much. */
5147 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5148 }
5149 else
5150 {
5151 /* If the previous character was the same,
5152 * thus doubling a character, give a bonus
5153 * to the score. Also for the soundfold
5154 * tree (might seem illogical but does
5155 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005156 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005157 if (c == mb_ptr2char(p))
5158 sp->ts_score -= SCORE_INS
5159 - SCORE_INSDUP;
5160 }
5161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005162
Bram Moolenaar4770d092006-01-12 23:22:24 +00005163 /* Starting a new char, reset the length. */
5164 sp->ts_tcharlen = 0;
5165 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005166 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005167 else
5168#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005169 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005170 /* If we found a similar char adjust the score.
5171 * We do this after calling go_deeper() because
5172 * it's slow. */
5173 if (newscore != 0
5174 && !soundfold
5175 && slang->sl_has_map
5176 && similar_chars(slang,
5177 c, fword[sp->ts_fidx - 1]))
5178 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005180 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005181 }
5182 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183
Bram Moolenaar4770d092006-01-12 23:22:24 +00005184 case STATE_DEL:
5185#ifdef FEAT_MBYTE
5186 /* When past the first byte of a multi-byte char don't try
5187 * delete/insert/swap a character. */
5188 if (has_mbyte && sp->ts_tcharlen > 0)
5189 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005190 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005191 sp->ts_state = STATE_FINAL;
5192 break;
5193 }
5194#endif
5195 /*
5196 * Try skipping one character in the bad word (delete it).
5197 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005198 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005199 sp->ts_state = STATE_INS_PREP;
5200 sp->ts_curi = 1;
5201 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5202 /* Deleting a vowel at the start of a word counts less, see
5203 * soundalike_score(). */
5204 newscore = 2 * SCORE_DEL / 3;
5205 else
5206 newscore = SCORE_DEL;
5207 if (fword[sp->ts_fidx] != NUL
5208 && TRY_DEEPER(su, stack, depth, newscore))
5209 {
5210 go_deeper(stack, depth, newscore);
5211#ifdef DEBUG_TRIEWALK
5212 sprintf(changename[depth], "%.*s-%s: delete %c",
5213 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5214 fword[sp->ts_fidx]);
5215#endif
5216 ++depth;
5217
5218 /* Remember what character we deleted, so that we can avoid
5219 * inserting it again. */
5220 stack[depth].ts_flags |= TSF_DIDDEL;
5221 stack[depth].ts_delidx = sp->ts_fidx;
5222
5223 /* Advance over the character in fword[]. Give a bonus to the
5224 * score if the same character is following "nn" -> "n". It's
5225 * a bit illogical for soundfold tree but it does give better
5226 * results. */
5227#ifdef FEAT_MBYTE
5228 if (has_mbyte)
5229 {
5230 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005231 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005232 if (enc_utf8 && utf_iscomposing(c))
5233 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5234 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5235 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5236 }
5237 else
5238#endif
5239 {
5240 ++stack[depth].ts_fidx;
5241 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5242 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5243 }
5244 break;
5245 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005246 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005247
5248 case STATE_INS_PREP:
5249 if (sp->ts_flags & TSF_DIDDEL)
5250 {
5251 /* If we just deleted a byte then inserting won't make sense,
5252 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005253 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005254 sp->ts_state = STATE_SWAP;
5255 break;
5256 }
5257
5258 /* skip over NUL bytes */
5259 n = sp->ts_arridx;
5260 for (;;)
5261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005262 if (sp->ts_curi > byts[n])
5263 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005264 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005265 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005267 break;
5268 }
5269 if (byts[n + sp->ts_curi] != NUL)
5270 {
5271 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005272 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005273 sp->ts_state = STATE_INS;
5274 break;
5275 }
5276 ++sp->ts_curi;
5277 }
5278 break;
5279
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005280 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005281
5282 case STATE_INS:
5283 /* Insert one byte. Repeat this for each possible byte at this
5284 * node. */
5285 n = sp->ts_arridx;
5286 if (sp->ts_curi > byts[n])
5287 {
5288 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005289 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005290 sp->ts_state = STATE_SWAP;
5291 break;
5292 }
5293
5294 /* Do one more byte at this node, but:
5295 * - Skip NUL bytes.
5296 * - Skip the byte if it's equal to the byte in the word,
5297 * accepting that byte is always better.
5298 */
5299 n += sp->ts_curi++;
5300 c = byts[n];
5301 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5302 /* Inserting a vowel at the start of a word counts less,
5303 * see soundalike_score(). */
5304 newscore = 2 * SCORE_INS / 3;
5305 else
5306 newscore = SCORE_INS;
5307 if (c != fword[sp->ts_fidx]
5308 && TRY_DEEPER(su, stack, depth, newscore))
5309 {
5310 go_deeper(stack, depth, newscore);
5311#ifdef DEBUG_TRIEWALK
5312 sprintf(changename[depth], "%.*s-%s: insert %c",
5313 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5314 c);
5315#endif
5316 ++depth;
5317 sp = &stack[depth];
5318 tword[sp->ts_twordlen++] = c;
5319 sp->ts_arridx = idxs[n];
5320#ifdef FEAT_MBYTE
5321 if (has_mbyte)
5322 {
5323 fl = MB_BYTE2LEN(c);
5324 if (fl > 1)
5325 {
5326 /* There are following bytes for the same character.
5327 * We must find all bytes before trying
5328 * delete/insert/swap/etc. */
5329 sp->ts_tcharlen = fl;
5330 sp->ts_tcharidx = 1;
5331 sp->ts_isdiff = DIFF_INSERT;
5332 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005333 }
5334 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005335 fl = 1;
5336 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00005337#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005338 {
5339 /* If the previous character was the same, thus doubling a
5340 * character, give a bonus to the score. Also for
5341 * soundfold words (illogical but does give a better
5342 * score). */
5343 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005344 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005345 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005347 }
5348 break;
5349
5350 case STATE_SWAP:
5351 /*
5352 * Swap two bytes in the bad word: "12" -> "21".
5353 * We change "fword" here, it's changed back afterwards at
5354 * STATE_UNSWAP.
5355 */
5356 p = fword + sp->ts_fidx;
5357 c = *p;
5358 if (c == NUL)
5359 {
5360 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005361 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005362 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005364 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005365
Bram Moolenaar4770d092006-01-12 23:22:24 +00005366 /* Don't swap if the first character is not a word character.
5367 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005368 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005369 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005370 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005371 sp->ts_state = STATE_REP_INI;
5372 break;
5373 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005374
Bram Moolenaar4770d092006-01-12 23:22:24 +00005375#ifdef FEAT_MBYTE
5376 if (has_mbyte)
5377 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005378 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005379 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005380 if (p[n] == NUL)
5381 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005382 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005383 c2 = c; /* don't swap non-word char */
5384 else
5385 c2 = mb_ptr2char(p + n);
5386 }
5387 else
5388#endif
5389 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005390 if (p[1] == NUL)
5391 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005392 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005393 c2 = c; /* don't swap non-word char */
5394 else
5395 c2 = p[1];
5396 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005397
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005398 /* When the second character is NUL we can't swap. */
5399 if (c2 == NUL)
5400 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005401 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005402 sp->ts_state = STATE_REP_INI;
5403 break;
5404 }
5405
Bram Moolenaar4770d092006-01-12 23:22:24 +00005406 /* When characters are identical, swap won't do anything.
5407 * Also get here if the second char is not a word character. */
5408 if (c == c2)
5409 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005410 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005411 sp->ts_state = STATE_SWAP3;
5412 break;
5413 }
5414 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5415 {
5416 go_deeper(stack, depth, SCORE_SWAP);
5417#ifdef DEBUG_TRIEWALK
5418 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5419 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5420 c, c2);
5421#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005422 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005423 sp->ts_state = STATE_UNSWAP;
5424 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005425#ifdef FEAT_MBYTE
5426 if (has_mbyte)
5427 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005428 fl = mb_char2len(c2);
5429 mch_memmove(p, p + n, fl);
5430 mb_char2bytes(c, p + fl);
5431 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005432 }
5433 else
5434#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005435 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005436 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005437 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005438 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005439 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005440 }
5441 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005442 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005443 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005444 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005445 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005446 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005447 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005448
Bram Moolenaar4770d092006-01-12 23:22:24 +00005449 case STATE_UNSWAP:
5450 /* Undo the STATE_SWAP swap: "21" -> "12". */
5451 p = fword + sp->ts_fidx;
5452#ifdef FEAT_MBYTE
5453 if (has_mbyte)
5454 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005455 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005456 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005457 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005458 mb_char2bytes(c, p);
5459 }
5460 else
5461#endif
5462 {
5463 c = *p;
5464 *p = p[1];
5465 p[1] = c;
5466 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005467 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005468
5469 case STATE_SWAP3:
5470 /* Swap two bytes, skipping one: "123" -> "321". We change
5471 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5472 p = fword + sp->ts_fidx;
5473#ifdef FEAT_MBYTE
5474 if (has_mbyte)
5475 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005476 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005477 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005478 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005479 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005480 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005481 c3 = c; /* don't swap non-word char */
5482 else
5483 c3 = mb_ptr2char(p + n + fl);
5484 }
5485 else
5486#endif
5487 {
5488 c = *p;
5489 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005490 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005491 c3 = c; /* don't swap non-word char */
5492 else
5493 c3 = p[2];
5494 }
5495
5496 /* When characters are identical: "121" then SWAP3 result is
5497 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5498 * same as SWAP on next char: "112". Thus skip all swapping.
5499 * Also skip when c3 is NUL.
5500 * Also get here when the third character is not a word character.
5501 * Second character may any char: "a.b" -> "b.a" */
5502 if (c == c3 || c3 == NUL)
5503 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005504 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005505 sp->ts_state = STATE_REP_INI;
5506 break;
5507 }
5508 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5509 {
5510 go_deeper(stack, depth, SCORE_SWAP3);
5511#ifdef DEBUG_TRIEWALK
5512 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5513 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5514 c, c3);
5515#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005516 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005517 sp->ts_state = STATE_UNSWAP3;
5518 ++depth;
5519#ifdef FEAT_MBYTE
5520 if (has_mbyte)
5521 {
5522 tl = mb_char2len(c3);
5523 mch_memmove(p, p + n + fl, tl);
5524 mb_char2bytes(c2, p + tl);
5525 mb_char2bytes(c, p + fl + tl);
5526 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5527 }
5528 else
5529#endif
5530 {
5531 p[0] = p[2];
5532 p[2] = c;
5533 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5534 }
5535 }
5536 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005537 {
5538 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005539 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005540 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005541 break;
5542
5543 case STATE_UNSWAP3:
5544 /* Undo STATE_SWAP3: "321" -> "123" */
5545 p = fword + sp->ts_fidx;
5546#ifdef FEAT_MBYTE
5547 if (has_mbyte)
5548 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005549 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005550 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005551 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005552 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005553 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005554 mch_memmove(p + fl + tl, p, n);
5555 mb_char2bytes(c, p);
5556 mb_char2bytes(c2, p + tl);
5557 p = p + tl;
5558 }
5559 else
5560#endif
5561 {
5562 c = *p;
5563 *p = p[2];
5564 p[2] = c;
5565 ++p;
5566 }
5567
Bram Moolenaar860cae12010-06-05 23:22:07 +02005568 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005569 {
5570 /* Middle char is not a word char, skip the rotate. First and
5571 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005572 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005573 sp->ts_state = STATE_REP_INI;
5574 break;
5575 }
5576
5577 /* Rotate three characters left: "123" -> "231". We change
5578 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5579 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5580 {
5581 go_deeper(stack, depth, SCORE_SWAP3);
5582#ifdef DEBUG_TRIEWALK
5583 p = fword + sp->ts_fidx;
5584 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5585 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5586 p[0], p[1], p[2]);
5587#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005588 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005589 sp->ts_state = STATE_UNROT3L;
5590 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005591 p = fword + sp->ts_fidx;
5592#ifdef FEAT_MBYTE
5593 if (has_mbyte)
5594 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005595 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005596 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005597 fl = MB_CPTR2LEN(p + n);
5598 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005599 mch_memmove(p, p + n, fl);
5600 mb_char2bytes(c, p + fl);
5601 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005602 }
5603 else
5604#endif
5605 {
5606 c = *p;
5607 *p = p[1];
5608 p[1] = p[2];
5609 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005610 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005611 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005612 }
5613 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005614 {
5615 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005616 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005617 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005618 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005619
Bram Moolenaar4770d092006-01-12 23:22:24 +00005620 case STATE_UNROT3L:
5621 /* Undo ROT3L: "231" -> "123" */
5622 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005623#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005624 if (has_mbyte)
5625 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005626 n = MB_PTR2LEN(p);
5627 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005628 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005629 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005630 mch_memmove(p + tl, p, n);
5631 mb_char2bytes(c, p);
5632 }
5633 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005634#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005635 {
5636 c = p[2];
5637 p[2] = p[1];
5638 p[1] = *p;
5639 *p = c;
5640 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005641
Bram Moolenaar4770d092006-01-12 23:22:24 +00005642 /* Rotate three bytes right: "123" -> "312". We change "fword"
5643 * here, it's changed back afterwards at STATE_UNROT3R. */
5644 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5645 {
5646 go_deeper(stack, depth, SCORE_SWAP3);
5647#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005648 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005649 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5650 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5651 p[0], p[1], p[2]);
5652#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005653 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005654 sp->ts_state = STATE_UNROT3R;
5655 ++depth;
5656 p = fword + sp->ts_fidx;
5657#ifdef FEAT_MBYTE
5658 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005659 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005660 n = MB_CPTR2LEN(p);
5661 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005662 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005663 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005664 mch_memmove(p + tl, p, n);
5665 mb_char2bytes(c, p);
5666 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005667 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005668 else
5669#endif
5670 {
5671 c = p[2];
5672 p[2] = p[1];
5673 p[1] = *p;
5674 *p = c;
5675 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5676 }
5677 }
5678 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005679 {
5680 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005681 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005682 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005683 break;
5684
5685 case STATE_UNROT3R:
5686 /* Undo ROT3R: "312" -> "123" */
5687 p = fword + sp->ts_fidx;
5688#ifdef FEAT_MBYTE
5689 if (has_mbyte)
5690 {
5691 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005692 tl = MB_PTR2LEN(p);
5693 n = MB_PTR2LEN(p + tl);
5694 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005695 mch_memmove(p, p + tl, n);
5696 mb_char2bytes(c, p + n);
5697 }
5698 else
5699#endif
5700 {
5701 c = *p;
5702 *p = p[1];
5703 p[1] = p[2];
5704 p[2] = c;
5705 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005706 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005707
5708 case STATE_REP_INI:
5709 /* Check if matching with REP items from the .aff file would work.
5710 * Quickly skip if:
5711 * - there are no REP items and we are not in the soundfold trie
5712 * - the score is going to be too high anyway
5713 * - already applied a REP item or swapped here */
5714 if ((lp->lp_replang == NULL && !soundfold)
5715 || sp->ts_score + SCORE_REP >= su->su_maxscore
5716 || sp->ts_fidx < sp->ts_fidxtry)
5717 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005718 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005719 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005720 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005721 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005722
Bram Moolenaar4770d092006-01-12 23:22:24 +00005723 /* Use the first byte to quickly find the first entry that may
5724 * match. If the index is -1 there is none. */
5725 if (soundfold)
5726 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5727 else
5728 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005729
Bram Moolenaar4770d092006-01-12 23:22:24 +00005730 if (sp->ts_curi < 0)
5731 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005732 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005733 sp->ts_state = STATE_FINAL;
5734 break;
5735 }
5736
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005737 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005738 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005739 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005740
5741 case STATE_REP:
5742 /* Try matching with REP items from the .aff file. For each match
5743 * replace the characters and check if the resulting word is
5744 * valid. */
5745 p = fword + sp->ts_fidx;
5746
5747 if (soundfold)
5748 gap = &slang->sl_repsal;
5749 else
5750 gap = &lp->lp_replang->sl_rep;
5751 while (sp->ts_curi < gap->ga_len)
5752 {
5753 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5754 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005755 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005756 /* past possible matching entries */
5757 sp->ts_curi = gap->ga_len;
5758 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005759 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005760 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5761 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5762 {
5763 go_deeper(stack, depth, SCORE_REP);
5764#ifdef DEBUG_TRIEWALK
5765 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5766 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5767 ftp->ft_from, ftp->ft_to);
5768#endif
5769 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005770 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005771 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005772
Bram Moolenaar4770d092006-01-12 23:22:24 +00005773 /* Change the "from" to the "to" string. */
5774 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005775 fl = (int)STRLEN(ftp->ft_from);
5776 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005777 if (fl != tl)
5778 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005779 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005780 repextra += tl - fl;
5781 }
5782 mch_memmove(p, ftp->ft_to, tl);
5783 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
5784#ifdef FEAT_MBYTE
5785 stack[depth].ts_tcharlen = 0;
5786#endif
5787 break;
5788 }
5789 }
5790
5791 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005793 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005794 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005795 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005796 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005797
5798 break;
5799
5800 case STATE_REP_UNDO:
5801 /* Undo a REP replacement and continue with the next one. */
5802 if (soundfold)
5803 gap = &slang->sl_repsal;
5804 else
5805 gap = &lp->lp_replang->sl_rep;
5806 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005807 fl = (int)STRLEN(ftp->ft_from);
5808 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005809 p = fword + sp->ts_fidx;
5810 if (fl != tl)
5811 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005812 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005813 repextra -= tl - fl;
5814 }
5815 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005816 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005817 sp->ts_state = STATE_REP;
5818 break;
5819
5820 default:
5821 /* Did all possible states at this level, go up one level. */
5822 --depth;
5823
5824 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5825 {
5826 /* Continue in or go back to the prefix tree. */
5827 byts = pbyts;
5828 idxs = pidxs;
5829 }
5830
5831 /* Don't check for CTRL-C too often, it takes time. */
5832 if (--breakcheckcount == 0)
5833 {
5834 ui_breakcheck();
5835 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005837 }
5838 }
5839}
5840
Bram Moolenaar4770d092006-01-12 23:22:24 +00005841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005842/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005843 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005844 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005845 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005846go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005847{
Bram Moolenaarea424162005-06-16 21:51:00 +00005848 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005849 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005850 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005851 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005852 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005853}
5854
Bram Moolenaar53805d12005-08-01 07:08:33 +00005855#ifdef FEAT_MBYTE
5856/*
5857 * Case-folding may change the number of bytes: Count nr of chars in
5858 * fword[flen] and return the byte length of that many chars in "word".
5859 */
5860 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005861nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005862{
5863 char_u *p;
5864 int i = 0;
5865
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005866 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005867 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005868 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005869 --i;
5870 return (int)(p - word);
5871}
5872#endif
5873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005874/*
5875 * "fword" is a good word with case folded. Find the matching keep-case
5876 * words and put it in "kword".
5877 * Theoretically there could be several keep-case words that result in the
5878 * same case-folded word, but we only find one...
5879 */
5880 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005881find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005882{
5883 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5884 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005885 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005886
5887 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005888 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005889 int round[MAXWLEN];
5890 int fwordidx[MAXWLEN];
5891 int uwordidx[MAXWLEN];
5892 int kwordlen[MAXWLEN];
5893
5894 int flen, ulen;
5895 int l;
5896 int len;
5897 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005898 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005899 char_u *p;
5900 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005901 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005902
5903 if (byts == NULL)
5904 {
5905 /* array is empty: "cannot happen" */
5906 *kword = NUL;
5907 return;
5908 }
5909
5910 /* Make an all-cap version of "fword". */
5911 allcap_copy(fword, uword);
5912
5913 /*
5914 * Each character needs to be tried both case-folded and upper-case.
5915 * All this gets very complicated if we keep in mind that changing case
5916 * may change the byte length of a multi-byte character...
5917 */
5918 depth = 0;
5919 arridx[0] = 0;
5920 round[0] = 0;
5921 fwordidx[0] = 0;
5922 uwordidx[0] = 0;
5923 kwordlen[0] = 0;
5924 while (depth >= 0)
5925 {
5926 if (fword[fwordidx[depth]] == NUL)
5927 {
5928 /* We are at the end of "fword". If the tree allows a word to end
5929 * here we have found a match. */
5930 if (byts[arridx[depth] + 1] == 0)
5931 {
5932 kword[kwordlen[depth]] = NUL;
5933 return;
5934 }
5935
5936 /* kword is getting too long, continue one level up */
5937 --depth;
5938 }
5939 else if (++round[depth] > 2)
5940 {
5941 /* tried both fold-case and upper-case character, continue one
5942 * level up */
5943 --depth;
5944 }
5945 else
5946 {
5947 /*
5948 * round[depth] == 1: Try using the folded-case character.
5949 * round[depth] == 2: Try using the upper-case character.
5950 */
5951#ifdef FEAT_MBYTE
5952 if (has_mbyte)
5953 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005954 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5955 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005956 }
5957 else
5958#endif
5959 ulen = flen = 1;
5960 if (round[depth] == 1)
5961 {
5962 p = fword + fwordidx[depth];
5963 l = flen;
5964 }
5965 else
5966 {
5967 p = uword + uwordidx[depth];
5968 l = ulen;
5969 }
5970
5971 for (tryidx = arridx[depth]; l > 0; --l)
5972 {
5973 /* Perform a binary search in the list of accepted bytes. */
5974 len = byts[tryidx++];
5975 c = *p++;
5976 lo = tryidx;
5977 hi = tryidx + len - 1;
5978 while (lo < hi)
5979 {
5980 m = (lo + hi) / 2;
5981 if (byts[m] > c)
5982 hi = m - 1;
5983 else if (byts[m] < c)
5984 lo = m + 1;
5985 else
5986 {
5987 lo = hi = m;
5988 break;
5989 }
5990 }
5991
5992 /* Stop if there is no matching byte. */
5993 if (hi < lo || byts[lo] != c)
5994 break;
5995
5996 /* Continue at the child (if there is one). */
5997 tryidx = idxs[lo];
5998 }
5999
6000 if (l == 0)
6001 {
6002 /*
6003 * Found the matching char. Copy it to "kword" and go a
6004 * level deeper.
6005 */
6006 if (round[depth] == 1)
6007 {
6008 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
6009 flen);
6010 kwordlen[depth + 1] = kwordlen[depth] + flen;
6011 }
6012 else
6013 {
6014 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
6015 ulen);
6016 kwordlen[depth + 1] = kwordlen[depth] + ulen;
6017 }
6018 fwordidx[depth + 1] = fwordidx[depth] + flen;
6019 uwordidx[depth + 1] = uwordidx[depth] + ulen;
6020
6021 ++depth;
6022 arridx[depth] = tryidx;
6023 round[depth] = 0;
6024 }
6025 }
6026 }
6027
6028 /* Didn't find it: "cannot happen". */
6029 *kword = NUL;
6030}
6031
6032/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006033 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6034 * su->su_sga.
6035 */
6036 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006037score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006038{
6039 langp_T *lp;
6040 char_u badsound[MAXWLEN];
6041 int i;
6042 suggest_T *stp;
6043 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006044 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006045 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006046
6047 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6048 return;
6049
6050 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006051 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006052 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006053 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006054 if (lp->lp_slang->sl_sal.ga_len > 0)
6055 {
6056 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006057 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006058
6059 for (i = 0; i < su->su_ga.ga_len; ++i)
6060 {
6061 stp = &SUG(su->su_ga, i);
6062
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006063 /* Case-fold the suggested word, sound-fold it and compute the
6064 * sound-a-like score. */
6065 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006066 if (score < SCORE_MAXMAX)
6067 {
6068 /* Add the suggestion. */
6069 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6070 sstp->st_word = vim_strsave(stp->st_word);
6071 if (sstp->st_word != NULL)
6072 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006073 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006074 sstp->st_score = score;
6075 sstp->st_altscore = 0;
6076 sstp->st_orglen = stp->st_orglen;
6077 ++su->su_sga.ga_len;
6078 }
6079 }
6080 }
6081 break;
6082 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006083 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006084}
6085
6086/*
6087 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006088 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006089 */
6090 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006091score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006092{
6093 int i;
6094 int j;
6095 garray_T ga;
6096 garray_T *gap;
6097 langp_T *lp;
6098 suggest_T *stp;
6099 char_u *p;
6100 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006101 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006102 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006103 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006104
6105 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006106 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006107 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006108 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006109 if (lp->lp_slang->sl_sal.ga_len > 0)
6110 {
6111 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006112 slang = lp->lp_slang;
6113 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006114
6115 for (i = 0; i < su->su_ga.ga_len; ++i)
6116 {
6117 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006118 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006119 if (stp->st_altscore == SCORE_MAXMAX)
6120 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6121 else
6122 stp->st_score = (stp->st_score * 3
6123 + stp->st_altscore) / 4;
6124 stp->st_salscore = FALSE;
6125 }
6126 break;
6127 }
6128 }
6129
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006130 if (slang == NULL) /* Using "double" without sound folding. */
6131 {
6132 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
6133 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006134 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006135 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006136
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006137 /* Add the alternate score to su_sga. */
6138 for (i = 0; i < su->su_sga.ga_len; ++i)
6139 {
6140 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006141 stp->st_altscore = spell_edit_score(slang,
6142 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006143 if (stp->st_score == SCORE_MAXMAX)
6144 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6145 else
6146 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6147 stp->st_salscore = TRUE;
6148 }
6149
Bram Moolenaar4770d092006-01-12 23:22:24 +00006150 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
6151 * for both lists. */
6152 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006153 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006154 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006155 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6156
6157 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6158 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6159 return;
6160
6161 stp = &SUG(ga, 0);
6162 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6163 {
6164 /* round 1: get a suggestion from su_ga
6165 * round 2: get a suggestion from su_sga */
6166 for (round = 1; round <= 2; ++round)
6167 {
6168 gap = round == 1 ? &su->su_ga : &su->su_sga;
6169 if (i < gap->ga_len)
6170 {
6171 /* Don't add a word if it's already there. */
6172 p = SUG(*gap, i).st_word;
6173 for (j = 0; j < ga.ga_len; ++j)
6174 if (STRCMP(stp[j].st_word, p) == 0)
6175 break;
6176 if (j == ga.ga_len)
6177 stp[ga.ga_len++] = SUG(*gap, i);
6178 else
6179 vim_free(p);
6180 }
6181 }
6182 }
6183
6184 ga_clear(&su->su_ga);
6185 ga_clear(&su->su_sga);
6186
6187 /* Truncate the list to the number of suggestions that will be displayed. */
6188 if (ga.ga_len > su->su_maxcount)
6189 {
6190 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6191 vim_free(stp[i].st_word);
6192 ga.ga_len = su->su_maxcount;
6193 }
6194
6195 su->su_ga = ga;
6196}
6197
6198/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006199 * For the goodword in "stp" compute the soundalike score compared to the
6200 * badword.
6201 */
6202 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006203stp_sal_score(
6204 suggest_T *stp,
6205 suginfo_T *su,
6206 slang_T *slang,
6207 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006208{
6209 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006210 char_u *pbad;
6211 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006212 char_u badsound2[MAXWLEN];
6213 char_u fword[MAXWLEN];
6214 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006215 char_u goodword[MAXWLEN];
6216 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006217
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006218 lendiff = (int)(su->su_badlen - stp->st_orglen);
6219 if (lendiff >= 0)
6220 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006221 else
6222 {
6223 /* soundfold the bad word with more characters following */
6224 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6225
6226 /* When joining two words the sound often changes a lot. E.g., "t he"
6227 * sounds like "t h" while "the" sounds like "@". Avoid that by
6228 * removing the space. Don't do it when the good word also contains a
6229 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006230 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006231 && *skiptowhite(stp->st_word) == NUL)
6232 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006233 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006234
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006235 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006236 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006237 }
6238
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006239 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006240 {
6241 /* Add part of the bad word to the good word, so that we soundfold
6242 * what replaces the bad word. */
6243 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006244 vim_strncpy(goodword + stp->st_wordlen,
6245 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006246 pgood = goodword;
6247 }
6248 else
6249 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006250
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006251 /* Sound-fold the word and compute the score for the difference. */
6252 spell_soundfold(slang, pgood, FALSE, goodsound);
6253
6254 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006255}
6256
Bram Moolenaar4770d092006-01-12 23:22:24 +00006257/* structure used to store soundfolded words that add_sound_suggest() has
6258 * handled already. */
6259typedef struct
6260{
6261 short sft_score; /* lowest score used */
6262 char_u sft_word[1]; /* soundfolded word, actually longer */
6263} sftword_T;
6264
6265static sftword_T dumsft;
6266#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6267#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6268
6269/*
6270 * Prepare for calling suggest_try_soundalike().
6271 */
6272 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006273suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006274{
6275 langp_T *lp;
6276 int lpi;
6277 slang_T *slang;
6278
6279 /* Do this for all languages that support sound folding and for which a
6280 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006281 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006282 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006283 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006284 slang = lp->lp_slang;
6285 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6286 /* prepare the hashtable used by add_sound_suggest() */
6287 hash_init(&slang->sl_sounddone);
6288 }
6289}
6290
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006291/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006292 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006293 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006294 */
6295 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006296suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006297{
6298 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006299 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006300 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006301 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006302
Bram Moolenaar4770d092006-01-12 23:22:24 +00006303 /* Do this for all languages that support sound folding and for which a
6304 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006305 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006306 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006307 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006308 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006309 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006310 {
6311 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006312 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006313
Bram Moolenaar4770d092006-01-12 23:22:24 +00006314 /* try all kinds of inserts/deletes/swaps/etc. */
6315 /* TODO: also soundfold the next words, so that we can try joining
6316 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006317#ifdef SUGGEST_PROFILE
6318 prof_init();
6319#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006320 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006321#ifdef SUGGEST_PROFILE
6322 prof_report("soundalike");
6323#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006324 }
6325 }
6326}
6327
6328/*
6329 * Finish up after calling suggest_try_soundalike().
6330 */
6331 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006332suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006333{
6334 langp_T *lp;
6335 int lpi;
6336 slang_T *slang;
6337 int todo;
6338 hashitem_T *hi;
6339
6340 /* Do this for all languages that support sound folding and for which a
6341 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006342 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006343 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006344 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006345 slang = lp->lp_slang;
6346 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6347 {
6348 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006349 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006350 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6351 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006352 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006353 vim_free(HI2SFT(hi));
6354 --todo;
6355 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006356
6357 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006358 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006359 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006360 }
6361 }
6362}
6363
6364/*
6365 * A match with a soundfolded word is found. Add the good word(s) that
6366 * produce this soundfolded word.
6367 */
6368 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006369add_sound_suggest(
6370 suginfo_T *su,
6371 char_u *goodword,
6372 int score, /* soundfold score */
6373 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006374{
6375 slang_T *slang = lp->lp_slang; /* language for sound folding */
6376 int sfwordnr;
6377 char_u *nrline;
6378 int orgnr;
6379 char_u theword[MAXWLEN];
6380 int i;
6381 int wlen;
6382 char_u *byts;
6383 idx_T *idxs;
6384 int n;
6385 int wordcount;
6386 int wc;
6387 int goodscore;
6388 hash_T hash;
6389 hashitem_T *hi;
6390 sftword_T *sft;
6391 int bc, gc;
6392 int limit;
6393
6394 /*
6395 * It's very well possible that the same soundfold word is found several
6396 * times with different scores. Since the following is quite slow only do
6397 * the words that have a better score than before. Use a hashtable to
6398 * remember the words that have been done.
6399 */
6400 hash = hash_hash(goodword);
6401 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6402 if (HASHITEM_EMPTY(hi))
6403 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006404 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6405 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006406 if (sft != NULL)
6407 {
6408 sft->sft_score = score;
6409 STRCPY(sft->sft_word, goodword);
6410 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6411 }
6412 }
6413 else
6414 {
6415 sft = HI2SFT(hi);
6416 if (score >= sft->sft_score)
6417 return;
6418 sft->sft_score = score;
6419 }
6420
6421 /*
6422 * Find the word nr in the soundfold tree.
6423 */
6424 sfwordnr = soundfold_find(slang, goodword);
6425 if (sfwordnr < 0)
6426 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006427 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006428 return;
6429 }
6430
6431 /*
6432 * go over the list of good words that produce this soundfold word
6433 */
6434 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6435 orgnr = 0;
6436 while (*nrline != NUL)
6437 {
6438 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6439 * previous wordnr. */
6440 orgnr += bytes2offset(&nrline);
6441
6442 byts = slang->sl_fbyts;
6443 idxs = slang->sl_fidxs;
6444
6445 /* Lookup the word "orgnr" one of the two tries. */
6446 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006447 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006448 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006449 {
6450 i = 1;
6451 if (wordcount == orgnr && byts[n + 1] == NUL)
6452 break; /* found end of word */
6453
6454 if (byts[n + 1] == NUL)
6455 ++wordcount;
6456
6457 /* skip over the NUL bytes */
6458 for ( ; byts[n + i] == NUL; ++i)
6459 if (i > byts[n]) /* safety check */
6460 {
6461 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006462 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006463 goto badword;
6464 }
6465
6466 /* One of the siblings must have the word. */
6467 for ( ; i < byts[n]; ++i)
6468 {
6469 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6470 if (wordcount + wc > orgnr)
6471 break;
6472 wordcount += wc;
6473 }
6474
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006475 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006476 n = idxs[n + i];
6477 }
6478badword:
6479 theword[wlen] = NUL;
6480
6481 /* Go over the possible flags and regions. */
6482 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6483 {
6484 char_u cword[MAXWLEN];
6485 char_u *p;
6486 int flags = (int)idxs[n + i];
6487
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006488 /* Skip words with the NOSUGGEST flag */
6489 if (flags & WF_NOSUGGEST)
6490 continue;
6491
Bram Moolenaar4770d092006-01-12 23:22:24 +00006492 if (flags & WF_KEEPCAP)
6493 {
6494 /* Must find the word in the keep-case tree. */
6495 find_keepcap_word(slang, theword, cword);
6496 p = cword;
6497 }
6498 else
6499 {
6500 flags |= su->su_badflags;
6501 if ((flags & WF_CAPMASK) != 0)
6502 {
6503 /* Need to fix case according to "flags". */
6504 make_case_word(theword, cword, flags);
6505 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006506 }
6507 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006508 p = theword;
6509 }
6510
6511 /* Add the suggestion. */
6512 if (sps_flags & SPS_DOUBLE)
6513 {
6514 /* Add the suggestion if the score isn't too bad. */
6515 if (score <= su->su_maxscore)
6516 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6517 score, 0, FALSE, slang, FALSE);
6518 }
6519 else
6520 {
6521 /* Add a penalty for words in another region. */
6522 if ((flags & WF_REGION)
6523 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6524 goodscore = SCORE_REGION;
6525 else
6526 goodscore = 0;
6527
6528 /* Add a small penalty for changing the first letter from
6529 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006530 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006531 * letter is the same, that has already been counted. */
6532 gc = PTR2CHAR(p);
6533 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006534 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006535 bc = PTR2CHAR(su->su_badword);
6536 if (!SPELL_ISUPPER(bc)
6537 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6538 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006539 }
6540
Bram Moolenaar4770d092006-01-12 23:22:24 +00006541 /* Compute the score for the good word. This only does letter
6542 * insert/delete/swap/replace. REP items are not considered,
6543 * which may make the score a bit higher.
6544 * Use a limit for the score to make it work faster. Use
6545 * MAXSCORE(), because RESCORE() will change the score.
6546 * If the limit is very high then the iterative method is
6547 * inefficient, using an array is quicker. */
6548 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6549 if (limit > SCORE_LIMITMAX)
6550 goodscore += spell_edit_score(slang, su->su_badword, p);
6551 else
6552 goodscore += spell_edit_score_limit(slang, su->su_badword,
6553 p, limit);
6554
6555 /* When going over the limit don't bother to do the rest. */
6556 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006557 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006558 /* Give a bonus to words seen before. */
6559 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006560
Bram Moolenaar4770d092006-01-12 23:22:24 +00006561 /* Add the suggestion if the score isn't too bad. */
6562 goodscore = RESCORE(goodscore, score);
6563 if (goodscore <= su->su_sfmaxscore)
6564 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6565 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006566 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006567 }
6568 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006569 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006570 }
6571}
6572
6573/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006574 * Find word "word" in fold-case tree for "slang" and return the word number.
6575 */
6576 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006577soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006578{
6579 idx_T arridx = 0;
6580 int len;
6581 int wlen = 0;
6582 int c;
6583 char_u *ptr = word;
6584 char_u *byts;
6585 idx_T *idxs;
6586 int wordnr = 0;
6587
6588 byts = slang->sl_sbyts;
6589 idxs = slang->sl_sidxs;
6590
6591 for (;;)
6592 {
6593 /* First byte is the number of possible bytes. */
6594 len = byts[arridx++];
6595
6596 /* If the first possible byte is a zero the word could end here.
6597 * If the word ends we found the word. If not skip the NUL bytes. */
6598 c = ptr[wlen];
6599 if (byts[arridx] == NUL)
6600 {
6601 if (c == NUL)
6602 break;
6603
6604 /* Skip over the zeros, there can be several. */
6605 while (len > 0 && byts[arridx] == NUL)
6606 {
6607 ++arridx;
6608 --len;
6609 }
6610 if (len == 0)
6611 return -1; /* no children, word should have ended here */
6612 ++wordnr;
6613 }
6614
6615 /* If the word ends we didn't find it. */
6616 if (c == NUL)
6617 return -1;
6618
6619 /* Perform a binary search in the list of accepted bytes. */
6620 if (c == TAB) /* <Tab> is handled like <Space> */
6621 c = ' ';
6622 while (byts[arridx] < c)
6623 {
6624 /* The word count is in the first idxs[] entry of the child. */
6625 wordnr += idxs[idxs[arridx]];
6626 ++arridx;
6627 if (--len == 0) /* end of the bytes, didn't find it */
6628 return -1;
6629 }
6630 if (byts[arridx] != c) /* didn't find the byte */
6631 return -1;
6632
6633 /* Continue at the child (if there is one). */
6634 arridx = idxs[arridx];
6635 ++wlen;
6636
6637 /* One space in the good word may stand for several spaces in the
6638 * checked word. */
6639 if (c == ' ')
6640 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6641 ++wlen;
6642 }
6643
6644 return wordnr;
6645}
6646
6647/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006648 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006649 */
6650 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006651make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006652{
6653 if (flags & WF_ALLCAP)
6654 /* Make it all upper-case */
6655 allcap_copy(fword, cword);
6656 else if (flags & WF_ONECAP)
6657 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006658 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006659 else
6660 /* Use goodword as-is. */
6661 STRCPY(cword, fword);
6662}
6663
Bram Moolenaarea424162005-06-16 21:51:00 +00006664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006665/*
6666 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6667 * lines in the .aff file.
6668 */
6669 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006670similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006671{
Bram Moolenaarea424162005-06-16 21:51:00 +00006672 int m1, m2;
6673#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006674 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006675 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006676
Bram Moolenaarea424162005-06-16 21:51:00 +00006677 if (c1 >= 256)
6678 {
6679 buf[mb_char2bytes(c1, buf)] = 0;
6680 hi = hash_find(&slang->sl_map_hash, buf);
6681 if (HASHITEM_EMPTY(hi))
6682 m1 = 0;
6683 else
6684 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6685 }
6686 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006687#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006688 m1 = slang->sl_map_array[c1];
6689 if (m1 == 0)
6690 return FALSE;
6691
6692
6693#ifdef FEAT_MBYTE
6694 if (c2 >= 256)
6695 {
6696 buf[mb_char2bytes(c2, buf)] = 0;
6697 hi = hash_find(&slang->sl_map_hash, buf);
6698 if (HASHITEM_EMPTY(hi))
6699 m2 = 0;
6700 else
6701 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6702 }
6703 else
6704#endif
6705 m2 = slang->sl_map_array[c2];
6706
6707 return m1 == m2;
6708}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006709
6710/*
6711 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006712 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006713 */
6714 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006715add_suggestion(
6716 suginfo_T *su,
6717 garray_T *gap, /* either su_ga or su_sga */
6718 char_u *goodword,
6719 int badlenarg, /* len of bad word replaced with "goodword" */
6720 int score,
6721 int altscore,
6722 int had_bonus, /* value for st_had_bonus */
6723 slang_T *slang, /* language for sound folding */
6724 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006725 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006726{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006727 int goodlen; /* len of goodword changed */
6728 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006729 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006730 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006731 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006732 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006733
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006734 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6735 * "thee the" is added next to changing the first "the" the "thee". */
6736 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006737 pbad = su->su_badptr + badlenarg;
6738 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006739 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006740 goodlen = (int)(pgood - goodword);
6741 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006742 if (goodlen <= 0 || badlen <= 0)
6743 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006744 MB_PTR_BACK(goodword, pgood);
6745 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006746#ifdef FEAT_MBYTE
6747 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006748 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006749 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6750 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006751 }
6752 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006753#endif
6754 if (*pgood != *pbad)
6755 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006756 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006757
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006758 if (badlen == 0 && goodlen == 0)
6759 /* goodword doesn't change anything; may happen for "the the" changing
6760 * the first "the" to itself. */
6761 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006762
Bram Moolenaar89d40322006-08-29 15:30:07 +00006763 if (gap->ga_len == 0)
6764 i = -1;
6765 else
6766 {
6767 /* Check if the word is already there. Also check the length that is
6768 * being replaced "thes," -> "these" is a different suggestion from
6769 * "thes" -> "these". */
6770 stp = &SUG(*gap, 0);
6771 for (i = gap->ga_len; --i >= 0; ++stp)
6772 if (stp->st_wordlen == goodlen
6773 && stp->st_orglen == badlen
6774 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006775 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006776 /*
6777 * Found it. Remember the word with the lowest score.
6778 */
6779 if (stp->st_slang == NULL)
6780 stp->st_slang = slang;
6781
6782 new_sug.st_score = score;
6783 new_sug.st_altscore = altscore;
6784 new_sug.st_had_bonus = had_bonus;
6785
6786 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006787 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006788 /* Only one of the two had the soundalike score computed.
6789 * Need to do that for the other one now, otherwise the
6790 * scores can't be compared. This happens because
6791 * suggest_try_change() doesn't compute the soundalike
6792 * word to keep it fast, while some special methods set
6793 * the soundalike score to zero. */
6794 if (had_bonus)
6795 rescore_one(su, stp);
6796 else
6797 {
6798 new_sug.st_word = stp->st_word;
6799 new_sug.st_wordlen = stp->st_wordlen;
6800 new_sug.st_slang = stp->st_slang;
6801 new_sug.st_orglen = badlen;
6802 rescore_one(su, &new_sug);
6803 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006805
Bram Moolenaar89d40322006-08-29 15:30:07 +00006806 if (stp->st_score > new_sug.st_score)
6807 {
6808 stp->st_score = new_sug.st_score;
6809 stp->st_altscore = new_sug.st_altscore;
6810 stp->st_had_bonus = new_sug.st_had_bonus;
6811 }
6812 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006813 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006814 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006815
Bram Moolenaar4770d092006-01-12 23:22:24 +00006816 if (i < 0 && ga_grow(gap, 1) == OK)
6817 {
6818 /* Add a suggestion. */
6819 stp = &SUG(*gap, gap->ga_len);
6820 stp->st_word = vim_strnsave(goodword, goodlen);
6821 if (stp->st_word != NULL)
6822 {
6823 stp->st_wordlen = goodlen;
6824 stp->st_score = score;
6825 stp->st_altscore = altscore;
6826 stp->st_had_bonus = had_bonus;
6827 stp->st_orglen = badlen;
6828 stp->st_slang = slang;
6829 ++gap->ga_len;
6830
6831 /* If we have too many suggestions now, sort the list and keep
6832 * the best suggestions. */
6833 if (gap->ga_len > SUG_MAX_COUNT(su))
6834 {
6835 if (maxsf)
6836 su->su_sfmaxscore = cleanup_suggestions(gap,
6837 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6838 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006839 su->su_maxscore = cleanup_suggestions(gap,
6840 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006841 }
6842 }
6843 }
6844}
6845
6846/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006847 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6848 * for split words, such as "the the". Remove these from the list here.
6849 */
6850 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006851check_suggestions(
6852 suginfo_T *su,
6853 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006854{
6855 suggest_T *stp;
6856 int i;
6857 char_u longword[MAXWLEN + 1];
6858 int len;
6859 hlf_T attr;
6860
6861 stp = &SUG(*gap, 0);
6862 for (i = gap->ga_len - 1; i >= 0; --i)
6863 {
6864 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006865 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006866 len = stp[i].st_wordlen;
6867 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6868 MAXWLEN - len);
6869 attr = HLF_COUNT;
6870 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6871 if (attr != HLF_COUNT)
6872 {
6873 /* Remove this entry. */
6874 vim_free(stp[i].st_word);
6875 --gap->ga_len;
6876 if (i < gap->ga_len)
6877 mch_memmove(stp + i, stp + i + 1,
6878 sizeof(suggest_T) * (gap->ga_len - i));
6879 }
6880 }
6881}
6882
6883
6884/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006885 * Add a word to be banned.
6886 */
6887 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006888add_banned(
6889 suginfo_T *su,
6890 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006891{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006892 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006893 hash_T hash;
6894 hashitem_T *hi;
6895
Bram Moolenaar4770d092006-01-12 23:22:24 +00006896 hash = hash_hash(word);
6897 hi = hash_lookup(&su->su_banned, word, hash);
6898 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006899 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006900 s = vim_strsave(word);
6901 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006902 hash_add_item(&su->su_banned, hi, s, hash);
6903 }
6904}
6905
6906/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006907 * Recompute the score for all suggestions if sound-folding is possible. This
6908 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006909 */
6910 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006911rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006912{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006913 int i;
6914
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006915 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006916 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006917 rescore_one(su, &SUG(su->su_ga, i));
6918}
6919
6920/*
6921 * Recompute the score for one suggestion if sound-folding is possible.
6922 */
6923 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006924rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006925{
6926 slang_T *slang = stp->st_slang;
6927 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006928 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006929
6930 /* Only rescore suggestions that have no sal score yet and do have a
6931 * language. */
6932 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6933 {
6934 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006935 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006936 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006937 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006938 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006939 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006940 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006941
6942 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006943 if (stp->st_altscore == SCORE_MAXMAX)
6944 stp->st_altscore = SCORE_BIG;
6945 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6946 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006947 }
6948}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006950static int
6951#ifdef __BORLANDC__
6952_RTLENTRYF
6953#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006954sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006955
6956/*
6957 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006958 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006959 */
6960 static int
6961#ifdef __BORLANDC__
6962_RTLENTRYF
6963#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006964sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965{
6966 suggest_T *p1 = (suggest_T *)s1;
6967 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006968 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006969
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006970 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006971 {
6972 n = p1->st_altscore - p2->st_altscore;
6973 if (n == 0)
6974 n = STRICMP(p1->st_word, p2->st_word);
6975 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006976 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006977}
6978
6979/*
6980 * Cleanup the suggestions:
6981 * - Sort on score.
6982 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006983 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006984 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006985 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006986cleanup_suggestions(
6987 garray_T *gap,
6988 int maxscore,
6989 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006990{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006991 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006992 int i;
6993
6994 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006995 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006996
6997 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006998 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006999 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007000 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007001 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007002 gap->ga_len = keep;
7003 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007004 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007005 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007006}
7007
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007008#if defined(FEAT_EVAL) || defined(PROTO)
7009/*
7010 * Soundfold a string, for soundfold().
7011 * Result is in allocated memory, NULL for an error.
7012 */
7013 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007014eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007015{
7016 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007017 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007018 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007019
Bram Moolenaar860cae12010-06-05 23:22:07 +02007020 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007021 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02007022 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007023 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02007024 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007025 if (lp->lp_slang->sl_sal.ga_len > 0)
7026 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007027 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007028 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007029 return vim_strsave(sound);
7030 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007031 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007032
7033 /* No language with sound folding, return word as-is. */
7034 return vim_strsave(word);
7035}
7036#endif
7037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007038/*
7039 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00007040 *
7041 * There are many ways to turn a word into a sound-a-like representation. The
7042 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
7043 * swedish name matching - survey and test of different algorithms" by Klas
7044 * Erikson.
7045 *
7046 * We support two methods:
7047 * 1. SOFOFROM/SOFOTO do a simple character mapping.
7048 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007049 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02007050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007051spell_soundfold(
7052 slang_T *slang,
7053 char_u *inword,
7054 int folded, /* "inword" is already case-folded */
7055 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007056{
7057 char_u fword[MAXWLEN];
7058 char_u *word;
7059
7060 if (slang->sl_sofo)
7061 /* SOFOFROM and SOFOTO used */
7062 spell_soundfold_sofo(slang, inword, res);
7063 else
7064 {
7065 /* SAL items used. Requires the word to be case-folded. */
7066 if (folded)
7067 word = inword;
7068 else
7069 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007070 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007071 word = fword;
7072 }
7073
7074#ifdef FEAT_MBYTE
7075 if (has_mbyte)
7076 spell_soundfold_wsal(slang, word, res);
7077 else
7078#endif
7079 spell_soundfold_sal(slang, word, res);
7080 }
7081}
7082
7083/*
7084 * Perform sound folding of "inword" into "res" according to SOFOFROM and
7085 * SOFOTO lines.
7086 */
7087 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007088spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007089{
7090 char_u *s;
7091 int ri = 0;
7092 int c;
7093
7094#ifdef FEAT_MBYTE
7095 if (has_mbyte)
7096 {
7097 int prevc = 0;
7098 int *ip;
7099
7100 /* The sl_sal_first[] table contains the translation for chars up to
7101 * 255, sl_sal the rest. */
7102 for (s = inword; *s != NUL; )
7103 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007104 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007105 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007106 c = ' ';
7107 else if (c < 256)
7108 c = slang->sl_sal_first[c];
7109 else
7110 {
7111 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
7112 if (ip == NULL) /* empty list, can't match */
7113 c = NUL;
7114 else
7115 for (;;) /* find "c" in the list */
7116 {
7117 if (*ip == 0) /* not found */
7118 {
7119 c = NUL;
7120 break;
7121 }
7122 if (*ip == c) /* match! */
7123 {
7124 c = ip[1];
7125 break;
7126 }
7127 ip += 2;
7128 }
7129 }
7130
7131 if (c != NUL && c != prevc)
7132 {
7133 ri += mb_char2bytes(c, res + ri);
7134 if (ri + MB_MAXBYTES > MAXWLEN)
7135 break;
7136 prevc = c;
7137 }
7138 }
7139 }
7140 else
7141#endif
7142 {
7143 /* The sl_sal_first[] table contains the translation. */
7144 for (s = inword; (c = *s) != NUL; ++s)
7145 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007146 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007147 c = ' ';
7148 else
7149 c = slang->sl_sal_first[c];
7150 if (c != NUL && (ri == 0 || res[ri - 1] != c))
7151 res[ri++] = c;
7152 }
7153 }
7154
7155 res[ri] = NUL;
7156}
7157
7158 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007159spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007160{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007161 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007163 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007164 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007165 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007166 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007167 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007168 int n, k = 0;
7169 int z0;
7170 int k0;
7171 int n0;
7172 int c;
7173 int pri;
7174 int p0 = -333;
7175 int c0;
7176
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007177 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007178 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007179 if (slang->sl_rem_accents)
7180 {
7181 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007182 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007183 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007184 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007185 {
7186 *t++ = ' ';
7187 s = skipwhite(s);
7188 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007189 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007190 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007191 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007192 *t++ = *s;
7193 ++s;
7194 }
7195 }
7196 *t = NUL;
7197 }
7198 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007199 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007200
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007201 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202
7203 /*
7204 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007205 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007206 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007207 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007208 while ((c = word[i]) != NUL)
7209 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007210 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007211 n = slang->sl_sal_first[c];
7212 z0 = 0;
7213
7214 if (n >= 0)
7215 {
7216 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007217 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007218 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007219 /* Quickly skip entries that don't match the word. Most
7220 * entries are less then three chars, optimize for that. */
7221 k = smp[n].sm_leadlen;
7222 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007223 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007224 if (word[i + 1] != s[1])
7225 continue;
7226 if (k > 2)
7227 {
7228 for (j = 2; j < k; ++j)
7229 if (word[i + j] != s[j])
7230 break;
7231 if (j < k)
7232 continue;
7233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007234 }
7235
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007236 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007238 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007239 while (*pf != NUL && *pf != word[i + k])
7240 ++pf;
7241 if (*pf == NUL)
7242 continue;
7243 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007244 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007245 s = smp[n].sm_rules;
7246 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007247
7248 p0 = *s;
7249 k0 = k;
7250 while (*s == '-' && k > 1)
7251 {
7252 k--;
7253 s++;
7254 }
7255 if (*s == '<')
7256 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007257 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007258 {
7259 /* determine priority */
7260 pri = *s - '0';
7261 s++;
7262 }
7263 if (*s == '^' && *(s + 1) == '^')
7264 s++;
7265
7266 if (*s == NUL
7267 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007268 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007269 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007270 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007271 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007272 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007273 && spell_iswordp(word + i - 1, curwin)
7274 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007275 {
7276 /* search for followup rules, if: */
7277 /* followup and k > 1 and NO '-' in searchstring */
7278 c0 = word[i + k - 1];
7279 n0 = slang->sl_sal_first[c0];
7280
7281 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007282 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007283 {
7284 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007285 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007286 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007287 /* Quickly skip entries that don't match the word.
7288 * */
7289 k0 = smp[n0].sm_leadlen;
7290 if (k0 > 1)
7291 {
7292 if (word[i + k] != s[1])
7293 continue;
7294 if (k0 > 2)
7295 {
7296 pf = word + i + k + 1;
7297 for (j = 2; j < k0; ++j)
7298 if (*pf++ != s[j])
7299 break;
7300 if (j < k0)
7301 continue;
7302 }
7303 }
7304 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007305
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007306 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007307 {
7308 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007309 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007310 while (*pf != NUL && *pf != word[i + k0])
7311 ++pf;
7312 if (*pf == NUL)
7313 continue;
7314 ++k0;
7315 }
7316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007317 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007318 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 while (*s == '-')
7320 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007321 /* "k0" gets NOT reduced because
7322 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007323 s++;
7324 }
7325 if (*s == '<')
7326 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007327 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 {
7329 p0 = *s - '0';
7330 s++;
7331 }
7332
7333 if (*s == NUL
7334 /* *s == '^' cuts */
7335 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007336 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007337 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007338 {
7339 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007340 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007341 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007342
7343 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007344 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007345 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007346 /* rule fits; stop search */
7347 break;
7348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349 }
7350
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007351 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007352 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007353 }
7354
7355 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007356 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007357 if (s == NULL)
7358 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007359 pf = smp[n].sm_rules;
7360 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007361 if (p0 == 1 && z == 0)
7362 {
7363 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007364 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7365 || res[reslen - 1] == *s))
7366 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007367 z0 = 1;
7368 z = 1;
7369 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007370 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007371 {
7372 word[i + k0] = *s;
7373 k0++;
7374 s++;
7375 }
7376 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007377 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378
7379 /* new "actual letter" */
7380 c = word[i];
7381 }
7382 else
7383 {
7384 /* no '<' rule used */
7385 i += k - 1;
7386 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007387 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007388 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007389 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007390 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007391 s++;
7392 }
7393 /* new "actual letter" */
7394 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007395 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007396 {
7397 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007398 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007399 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007400 i = 0;
7401 z0 = 1;
7402 }
7403 }
7404 break;
7405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 }
7407 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007408 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007409 {
7410 c = ' ';
7411 k = 1;
7412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007413
7414 if (z0 == 0)
7415 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007416 if (k && !p0 && reslen < MAXWLEN && c != NUL
7417 && (!slang->sl_collapse || reslen == 0
7418 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007419 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007420 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007421
7422 i++;
7423 z = 0;
7424 k = 0;
7425 }
7426 }
7427
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007428 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007429}
7430
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007431#ifdef FEAT_MBYTE
7432/*
7433 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7434 * Multi-byte version of spell_soundfold().
7435 */
7436 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007437spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007438{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007439 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007440 int word[MAXWLEN];
7441 int wres[MAXWLEN];
7442 int l;
7443 char_u *s;
7444 int *ws;
7445 char_u *t;
7446 int *pf;
7447 int i, j, z;
7448 int reslen;
7449 int n, k = 0;
7450 int z0;
7451 int k0;
7452 int n0;
7453 int c;
7454 int pri;
7455 int p0 = -333;
7456 int c0;
7457 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007458 int wordlen;
7459
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007460
7461 /*
7462 * Convert the multi-byte string to a wide-character string.
7463 * Remove accents, if wanted. We actually remove all non-word characters.
7464 * But keep white space.
7465 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007466 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007467 for (s = inword; *s != NUL; )
7468 {
7469 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007470 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007471 if (slang->sl_rem_accents)
7472 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007473 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007474 {
7475 if (did_white)
7476 continue;
7477 c = ' ';
7478 did_white = TRUE;
7479 }
7480 else
7481 {
7482 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007483 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007484 continue;
7485 }
7486 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007487 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007488 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007489 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007490
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007491 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007492 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007493 * Converted from C++ to C. Added support for multi-byte chars.
7494 * Changed to keep spaces.
7495 */
7496 i = reslen = z = 0;
7497 while ((c = word[i]) != NUL)
7498 {
7499 /* Start with the first rule that has the character in the word. */
7500 n = slang->sl_sal_first[c & 0xff];
7501 z0 = 0;
7502
7503 if (n >= 0)
7504 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007505 /* Check all rules for the same index byte.
7506 * If c is 0x300 need extra check for the end of the array, as
7507 * (c & 0xff) is NUL. */
7508 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7509 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007510 {
7511 /* Quickly skip entries that don't match the word. Most
7512 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007513 if (c != ws[0])
7514 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007515 k = smp[n].sm_leadlen;
7516 if (k > 1)
7517 {
7518 if (word[i + 1] != ws[1])
7519 continue;
7520 if (k > 2)
7521 {
7522 for (j = 2; j < k; ++j)
7523 if (word[i + j] != ws[j])
7524 break;
7525 if (j < k)
7526 continue;
7527 }
7528 }
7529
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007530 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007531 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007532 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007533 while (*pf != NUL && *pf != word[i + k])
7534 ++pf;
7535 if (*pf == NUL)
7536 continue;
7537 ++k;
7538 }
7539 s = smp[n].sm_rules;
7540 pri = 5; /* default priority */
7541
7542 p0 = *s;
7543 k0 = k;
7544 while (*s == '-' && k > 1)
7545 {
7546 k--;
7547 s++;
7548 }
7549 if (*s == '<')
7550 s++;
7551 if (VIM_ISDIGIT(*s))
7552 {
7553 /* determine priority */
7554 pri = *s - '0';
7555 s++;
7556 }
7557 if (*s == '^' && *(s + 1) == '^')
7558 s++;
7559
7560 if (*s == NUL
7561 || (*s == '^'
7562 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007563 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007564 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007565 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007566 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007567 && spell_iswordp_w(word + i - 1, curwin)
7568 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007569 {
7570 /* search for followup rules, if: */
7571 /* followup and k > 1 and NO '-' in searchstring */
7572 c0 = word[i + k - 1];
7573 n0 = slang->sl_sal_first[c0 & 0xff];
7574
7575 if (slang->sl_followup && k > 1 && n0 >= 0
7576 && p0 != '-' && word[i + k] != NUL)
7577 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007578 /* Test follow-up rule for "word[i + k]"; loop over
7579 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007580 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7581 == (c0 & 0xff); ++n0)
7582 {
7583 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007584 */
7585 if (c0 != ws[0])
7586 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007587 k0 = smp[n0].sm_leadlen;
7588 if (k0 > 1)
7589 {
7590 if (word[i + k] != ws[1])
7591 continue;
7592 if (k0 > 2)
7593 {
7594 pf = word + i + k + 1;
7595 for (j = 2; j < k0; ++j)
7596 if (*pf++ != ws[j])
7597 break;
7598 if (j < k0)
7599 continue;
7600 }
7601 }
7602 k0 += k - 1;
7603
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007604 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007605 {
7606 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007607 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007608 while (*pf != NUL && *pf != word[i + k0])
7609 ++pf;
7610 if (*pf == NUL)
7611 continue;
7612 ++k0;
7613 }
7614
7615 p0 = 5;
7616 s = smp[n0].sm_rules;
7617 while (*s == '-')
7618 {
7619 /* "k0" gets NOT reduced because
7620 * "if (k0 == k)" */
7621 s++;
7622 }
7623 if (*s == '<')
7624 s++;
7625 if (VIM_ISDIGIT(*s))
7626 {
7627 p0 = *s - '0';
7628 s++;
7629 }
7630
7631 if (*s == NUL
7632 /* *s == '^' cuts */
7633 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007634 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007635 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007636 {
7637 if (k0 == k)
7638 /* this is just a piece of the string */
7639 continue;
7640
7641 if (p0 < pri)
7642 /* priority too low */
7643 continue;
7644 /* rule fits; stop search */
7645 break;
7646 }
7647 }
7648
7649 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7650 == (c0 & 0xff))
7651 continue;
7652 }
7653
7654 /* replace string */
7655 ws = smp[n].sm_to_w;
7656 s = smp[n].sm_rules;
7657 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7658 if (p0 == 1 && z == 0)
7659 {
7660 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007661 if (reslen > 0 && ws != NULL && *ws != NUL
7662 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007663 || wres[reslen - 1] == *ws))
7664 reslen--;
7665 z0 = 1;
7666 z = 1;
7667 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007668 if (ws != NULL)
7669 while (*ws != NUL && word[i + k0] != NUL)
7670 {
7671 word[i + k0] = *ws;
7672 k0++;
7673 ws++;
7674 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007675 if (k > k0)
7676 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007677 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007678
7679 /* new "actual letter" */
7680 c = word[i];
7681 }
7682 else
7683 {
7684 /* no '<' rule used */
7685 i += k - 1;
7686 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007687 if (ws != NULL)
7688 while (*ws != NUL && ws[1] != NUL
7689 && reslen < MAXWLEN)
7690 {
7691 if (reslen == 0 || wres[reslen - 1] != *ws)
7692 wres[reslen++] = *ws;
7693 ws++;
7694 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007695 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007696 if (ws == NULL)
7697 c = NUL;
7698 else
7699 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007700 if (strstr((char *)s, "^^") != NULL)
7701 {
7702 if (c != NUL)
7703 wres[reslen++] = c;
7704 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007705 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007706 i = 0;
7707 z0 = 1;
7708 }
7709 }
7710 break;
7711 }
7712 }
7713 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007714 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007715 {
7716 c = ' ';
7717 k = 1;
7718 }
7719
7720 if (z0 == 0)
7721 {
7722 if (k && !p0 && reslen < MAXWLEN && c != NUL
7723 && (!slang->sl_collapse || reslen == 0
7724 || wres[reslen - 1] != c))
7725 /* condense only double letters */
7726 wres[reslen++] = c;
7727
7728 i++;
7729 z = 0;
7730 k = 0;
7731 }
7732 }
7733
7734 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7735 l = 0;
7736 for (n = 0; n < reslen; ++n)
7737 {
7738 l += mb_char2bytes(wres[n], res + l);
7739 if (l + MB_MAXBYTES > MAXWLEN)
7740 break;
7741 }
7742 res[l] = NUL;
7743}
7744#endif
7745
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007746/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007747 * Compute a score for two sound-a-like words.
7748 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7749 * Instead of a generic loop we write out the code. That keeps it fast by
7750 * avoiding checks that will not be possible.
7751 */
7752 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007753soundalike_score(
7754 char_u *goodstart, /* sound-folded good word */
7755 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007756{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007757 char_u *goodsound = goodstart;
7758 char_u *badsound = badstart;
7759 int goodlen;
7760 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007761 int n;
7762 char_u *pl, *ps;
7763 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007764 int score = 0;
7765
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007766 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007767 * counted so much, vowels halfway the word aren't counted at all. */
7768 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7769 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007770 if ((badsound[0] == NUL && goodsound[1] == NUL)
7771 || (goodsound[0] == NUL && badsound[1] == NUL))
7772 /* changing word with vowel to word without a sound */
7773 return SCORE_DEL;
7774 if (badsound[0] == NUL || goodsound[0] == NUL)
7775 /* more than two changes */
7776 return SCORE_MAXMAX;
7777
Bram Moolenaar4770d092006-01-12 23:22:24 +00007778 if (badsound[1] == goodsound[1]
7779 || (badsound[1] != NUL
7780 && goodsound[1] != NUL
7781 && badsound[2] == goodsound[2]))
7782 {
7783 /* handle like a substitute */
7784 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007785 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007786 {
7787 score = 2 * SCORE_DEL / 3;
7788 if (*badsound == '*')
7789 ++badsound;
7790 else
7791 ++goodsound;
7792 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007793 }
7794
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007795 goodlen = (int)STRLEN(goodsound);
7796 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007797
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007798 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007799 * changes. */
7800 n = goodlen - badlen;
7801 if (n < -2 || n > 2)
7802 return SCORE_MAXMAX;
7803
7804 if (n > 0)
7805 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007806 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007807 ps = badsound;
7808 }
7809 else
7810 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007811 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007812 ps = goodsound;
7813 }
7814
7815 /* Skip over the identical part. */
7816 while (*pl == *ps && *pl != NUL)
7817 {
7818 ++pl;
7819 ++ps;
7820 }
7821
7822 switch (n)
7823 {
7824 case -2:
7825 case 2:
7826 /*
7827 * Must delete two characters from "pl".
7828 */
7829 ++pl; /* first delete */
7830 while (*pl == *ps)
7831 {
7832 ++pl;
7833 ++ps;
7834 }
7835 /* strings must be equal after second delete */
7836 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007837 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007838
7839 /* Failed to compare. */
7840 break;
7841
7842 case -1:
7843 case 1:
7844 /*
7845 * Minimal one delete from "pl" required.
7846 */
7847
7848 /* 1: delete */
7849 pl2 = pl + 1;
7850 ps2 = ps;
7851 while (*pl2 == *ps2)
7852 {
7853 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007854 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007855 ++pl2;
7856 ++ps2;
7857 }
7858
7859 /* 2: delete then swap, then rest must be equal */
7860 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7861 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007862 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007863
7864 /* 3: delete then substitute, then the rest must be equal */
7865 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007866 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007867
7868 /* 4: first swap then delete */
7869 if (pl[0] == ps[1] && pl[1] == ps[0])
7870 {
7871 pl2 = pl + 2; /* swap, skip two chars */
7872 ps2 = ps + 2;
7873 while (*pl2 == *ps2)
7874 {
7875 ++pl2;
7876 ++ps2;
7877 }
7878 /* delete a char and then strings must be equal */
7879 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007880 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007881 }
7882
7883 /* 5: first substitute then delete */
7884 pl2 = pl + 1; /* substitute, skip one char */
7885 ps2 = ps + 1;
7886 while (*pl2 == *ps2)
7887 {
7888 ++pl2;
7889 ++ps2;
7890 }
7891 /* delete a char and then strings must be equal */
7892 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007893 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007894
7895 /* Failed to compare. */
7896 break;
7897
7898 case 0:
7899 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007900 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007901 * insert is only possible in combination with a delete.
7902 * 1: check if for identical strings
7903 */
7904 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007905 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007906
7907 /* 2: swap */
7908 if (pl[0] == ps[1] && pl[1] == ps[0])
7909 {
7910 pl2 = pl + 2; /* swap, skip two chars */
7911 ps2 = ps + 2;
7912 while (*pl2 == *ps2)
7913 {
7914 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007915 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007916 ++pl2;
7917 ++ps2;
7918 }
7919 /* 3: swap and swap again */
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_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007923
7924 /* 4: swap and substitute */
7925 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007926 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007927 }
7928
7929 /* 5: substitute */
7930 pl2 = pl + 1;
7931 ps2 = ps + 1;
7932 while (*pl2 == *ps2)
7933 {
7934 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007935 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007936 ++pl2;
7937 ++ps2;
7938 }
7939
7940 /* 6: substitute and swap */
7941 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7942 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007943 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007944
7945 /* 7: substitute and substitute */
7946 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007947 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007948
7949 /* 8: insert then delete */
7950 pl2 = pl;
7951 ps2 = ps + 1;
7952 while (*pl2 == *ps2)
7953 {
7954 ++pl2;
7955 ++ps2;
7956 }
7957 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007958 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007959
7960 /* 9: delete then insert */
7961 pl2 = pl + 1;
7962 ps2 = ps;
7963 while (*pl2 == *ps2)
7964 {
7965 ++pl2;
7966 ++ps2;
7967 }
7968 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007969 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007970
7971 /* Failed to compare. */
7972 break;
7973 }
7974
7975 return SCORE_MAXMAX;
7976}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007978/*
7979 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007980 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007981 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007982 * The algorithm is described by Du and Chang, 1992.
7983 * The implementation of the algorithm comes from Aspell editdist.cpp,
7984 * edit_distance(). It has been converted from C++ to C and modified to
7985 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007986 */
7987 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007988spell_edit_score(
7989 slang_T *slang,
7990 char_u *badword,
7991 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007992{
7993 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007994 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007995 int j, i;
7996 int t;
7997 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007998 int pbc, pgc;
7999#ifdef FEAT_MBYTE
8000 char_u *p;
8001 int wbadword[MAXWLEN];
8002 int wgoodword[MAXWLEN];
8003
8004 if (has_mbyte)
8005 {
8006 /* Get the characters from the multi-byte strings and put them in an
8007 * int array for easy access. */
8008 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008009 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00008010 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008011 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008012 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00008013 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008014 }
8015 else
8016#endif
8017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008018 badlen = (int)STRLEN(badword) + 1;
8019 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008020 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008021
8022 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
8023#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008024 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
8025 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008026 if (cnt == NULL)
8027 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008028
8029 CNT(0, 0) = 0;
8030 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008031 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008032
8033 for (i = 1; i <= badlen; ++i)
8034 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00008035 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008036 for (j = 1; j <= goodlen; ++j)
8037 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008038#ifdef FEAT_MBYTE
8039 if (has_mbyte)
8040 {
8041 bc = wbadword[i - 1];
8042 gc = wgoodword[j - 1];
8043 }
8044 else
8045#endif
8046 {
8047 bc = badword[i - 1];
8048 gc = goodword[j - 1];
8049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008050 if (bc == gc)
8051 CNT(i, j) = CNT(i - 1, j - 1);
8052 else
8053 {
8054 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008055 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008056 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
8057 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008058 {
8059 /* For a similar character use SCORE_SIMILAR. */
8060 if (slang != NULL
8061 && slang->sl_has_map
8062 && similar_chars(slang, gc, bc))
8063 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
8064 else
8065 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
8066 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008067
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008068 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008069 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008070#ifdef FEAT_MBYTE
8071 if (has_mbyte)
8072 {
8073 pbc = wbadword[i - 2];
8074 pgc = wgoodword[j - 2];
8075 }
8076 else
8077#endif
8078 {
8079 pbc = badword[i - 2];
8080 pgc = goodword[j - 2];
8081 }
8082 if (bc == pgc && pbc == gc)
8083 {
8084 t = SCORE_SWAP + CNT(i - 2, j - 2);
8085 if (t < CNT(i, j))
8086 CNT(i, j) = t;
8087 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008088 }
8089 t = SCORE_DEL + CNT(i - 1, j);
8090 if (t < CNT(i, j))
8091 CNT(i, j) = t;
8092 t = SCORE_INS + CNT(i, j - 1);
8093 if (t < CNT(i, j))
8094 CNT(i, j) = t;
8095 }
8096 }
8097 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008098
8099 i = CNT(badlen - 1, goodlen - 1);
8100 vim_free(cnt);
8101 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008102}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008103
Bram Moolenaar4770d092006-01-12 23:22:24 +00008104typedef struct
8105{
8106 int badi;
8107 int goodi;
8108 int score;
8109} limitscore_T;
8110
8111/*
8112 * Like spell_edit_score(), but with a limit on the score to make it faster.
8113 * May return SCORE_MAXMAX when the score is higher than "limit".
8114 *
8115 * This uses a stack for the edits still to be tried.
8116 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
8117 * for multi-byte characters.
8118 */
8119 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008120spell_edit_score_limit(
8121 slang_T *slang,
8122 char_u *badword,
8123 char_u *goodword,
8124 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008125{
8126 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8127 int stackidx;
8128 int bi, gi;
8129 int bi2, gi2;
8130 int bc, gc;
8131 int score;
8132 int score_off;
8133 int minscore;
8134 int round;
8135
8136#ifdef FEAT_MBYTE
8137 /* Multi-byte characters require a bit more work, use a different function
8138 * to avoid testing "has_mbyte" quite often. */
8139 if (has_mbyte)
8140 return spell_edit_score_limit_w(slang, badword, goodword, limit);
8141#endif
8142
8143 /*
8144 * The idea is to go from start to end over the words. So long as
8145 * characters are equal just continue, this always gives the lowest score.
8146 * When there is a difference try several alternatives. Each alternative
8147 * increases "score" for the edit distance. Some of the alternatives are
8148 * pushed unto a stack and tried later, some are tried right away. At the
8149 * end of the word the score for one alternative is known. The lowest
8150 * possible score is stored in "minscore".
8151 */
8152 stackidx = 0;
8153 bi = 0;
8154 gi = 0;
8155 score = 0;
8156 minscore = limit + 1;
8157
8158 for (;;)
8159 {
8160 /* Skip over an equal part, score remains the same. */
8161 for (;;)
8162 {
8163 bc = badword[bi];
8164 gc = goodword[gi];
8165 if (bc != gc) /* stop at a char that's different */
8166 break;
8167 if (bc == NUL) /* both words end */
8168 {
8169 if (score < minscore)
8170 minscore = score;
8171 goto pop; /* do next alternative */
8172 }
8173 ++bi;
8174 ++gi;
8175 }
8176
8177 if (gc == NUL) /* goodword ends, delete badword chars */
8178 {
8179 do
8180 {
8181 if ((score += SCORE_DEL) >= minscore)
8182 goto pop; /* do next alternative */
8183 } while (badword[++bi] != NUL);
8184 minscore = score;
8185 }
8186 else if (bc == NUL) /* badword ends, insert badword chars */
8187 {
8188 do
8189 {
8190 if ((score += SCORE_INS) >= minscore)
8191 goto pop; /* do next alternative */
8192 } while (goodword[++gi] != NUL);
8193 minscore = score;
8194 }
8195 else /* both words continue */
8196 {
8197 /* If not close to the limit, perform a change. Only try changes
8198 * that may lead to a lower score than "minscore".
8199 * round 0: try deleting a char from badword
8200 * round 1: try inserting a char in badword */
8201 for (round = 0; round <= 1; ++round)
8202 {
8203 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8204 if (score_off < minscore)
8205 {
8206 if (score_off + SCORE_EDIT_MIN >= minscore)
8207 {
8208 /* Near the limit, rest of the words must match. We
8209 * can check that right now, no need to push an item
8210 * onto the stack. */
8211 bi2 = bi + 1 - round;
8212 gi2 = gi + round;
8213 while (goodword[gi2] == badword[bi2])
8214 {
8215 if (goodword[gi2] == NUL)
8216 {
8217 minscore = score_off;
8218 break;
8219 }
8220 ++bi2;
8221 ++gi2;
8222 }
8223 }
8224 else
8225 {
8226 /* try deleting/inserting a character later */
8227 stack[stackidx].badi = bi + 1 - round;
8228 stack[stackidx].goodi = gi + round;
8229 stack[stackidx].score = score_off;
8230 ++stackidx;
8231 }
8232 }
8233 }
8234
8235 if (score + SCORE_SWAP < minscore)
8236 {
8237 /* If swapping two characters makes a match then the
8238 * substitution is more expensive, thus there is no need to
8239 * try both. */
8240 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8241 {
8242 /* Swap two characters, that is: skip them. */
8243 gi += 2;
8244 bi += 2;
8245 score += SCORE_SWAP;
8246 continue;
8247 }
8248 }
8249
8250 /* Substitute one character for another which is the same
8251 * thing as deleting a character from both goodword and badword.
8252 * Use a better score when there is only a case difference. */
8253 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8254 score += SCORE_ICASE;
8255 else
8256 {
8257 /* For a similar character use SCORE_SIMILAR. */
8258 if (slang != NULL
8259 && slang->sl_has_map
8260 && similar_chars(slang, gc, bc))
8261 score += SCORE_SIMILAR;
8262 else
8263 score += SCORE_SUBST;
8264 }
8265
8266 if (score < minscore)
8267 {
8268 /* Do the substitution. */
8269 ++gi;
8270 ++bi;
8271 continue;
8272 }
8273 }
8274pop:
8275 /*
8276 * Get here to try the next alternative, pop it from the stack.
8277 */
8278 if (stackidx == 0) /* stack is empty, finished */
8279 break;
8280
8281 /* pop an item from the stack */
8282 --stackidx;
8283 gi = stack[stackidx].goodi;
8284 bi = stack[stackidx].badi;
8285 score = stack[stackidx].score;
8286 }
8287
8288 /* When the score goes over "limit" it may actually be much higher.
8289 * Return a very large number to avoid going below the limit when giving a
8290 * bonus. */
8291 if (minscore > limit)
8292 return SCORE_MAXMAX;
8293 return minscore;
8294}
8295
8296#ifdef FEAT_MBYTE
8297/*
8298 * Multi-byte version of spell_edit_score_limit().
8299 * Keep it in sync with the above!
8300 */
8301 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008302spell_edit_score_limit_w(
8303 slang_T *slang,
8304 char_u *badword,
8305 char_u *goodword,
8306 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008307{
8308 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8309 int stackidx;
8310 int bi, gi;
8311 int bi2, gi2;
8312 int bc, gc;
8313 int score;
8314 int score_off;
8315 int minscore;
8316 int round;
8317 char_u *p;
8318 int wbadword[MAXWLEN];
8319 int wgoodword[MAXWLEN];
8320
8321 /* Get the characters from the multi-byte strings and put them in an
8322 * int array for easy access. */
8323 bi = 0;
8324 for (p = badword; *p != NUL; )
8325 wbadword[bi++] = mb_cptr2char_adv(&p);
8326 wbadword[bi++] = 0;
8327 gi = 0;
8328 for (p = goodword; *p != NUL; )
8329 wgoodword[gi++] = mb_cptr2char_adv(&p);
8330 wgoodword[gi++] = 0;
8331
8332 /*
8333 * The idea is to go from start to end over the words. So long as
8334 * characters are equal just continue, this always gives the lowest score.
8335 * When there is a difference try several alternatives. Each alternative
8336 * increases "score" for the edit distance. Some of the alternatives are
8337 * pushed unto a stack and tried later, some are tried right away. At the
8338 * end of the word the score for one alternative is known. The lowest
8339 * possible score is stored in "minscore".
8340 */
8341 stackidx = 0;
8342 bi = 0;
8343 gi = 0;
8344 score = 0;
8345 minscore = limit + 1;
8346
8347 for (;;)
8348 {
8349 /* Skip over an equal part, score remains the same. */
8350 for (;;)
8351 {
8352 bc = wbadword[bi];
8353 gc = wgoodword[gi];
8354
8355 if (bc != gc) /* stop at a char that's different */
8356 break;
8357 if (bc == NUL) /* both words end */
8358 {
8359 if (score < minscore)
8360 minscore = score;
8361 goto pop; /* do next alternative */
8362 }
8363 ++bi;
8364 ++gi;
8365 }
8366
8367 if (gc == NUL) /* goodword ends, delete badword chars */
8368 {
8369 do
8370 {
8371 if ((score += SCORE_DEL) >= minscore)
8372 goto pop; /* do next alternative */
8373 } while (wbadword[++bi] != NUL);
8374 minscore = score;
8375 }
8376 else if (bc == NUL) /* badword ends, insert badword chars */
8377 {
8378 do
8379 {
8380 if ((score += SCORE_INS) >= minscore)
8381 goto pop; /* do next alternative */
8382 } while (wgoodword[++gi] != NUL);
8383 minscore = score;
8384 }
8385 else /* both words continue */
8386 {
8387 /* If not close to the limit, perform a change. Only try changes
8388 * that may lead to a lower score than "minscore".
8389 * round 0: try deleting a char from badword
8390 * round 1: try inserting a char in badword */
8391 for (round = 0; round <= 1; ++round)
8392 {
8393 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8394 if (score_off < minscore)
8395 {
8396 if (score_off + SCORE_EDIT_MIN >= minscore)
8397 {
8398 /* Near the limit, rest of the words must match. We
8399 * can check that right now, no need to push an item
8400 * onto the stack. */
8401 bi2 = bi + 1 - round;
8402 gi2 = gi + round;
8403 while (wgoodword[gi2] == wbadword[bi2])
8404 {
8405 if (wgoodword[gi2] == NUL)
8406 {
8407 minscore = score_off;
8408 break;
8409 }
8410 ++bi2;
8411 ++gi2;
8412 }
8413 }
8414 else
8415 {
8416 /* try deleting a character from badword later */
8417 stack[stackidx].badi = bi + 1 - round;
8418 stack[stackidx].goodi = gi + round;
8419 stack[stackidx].score = score_off;
8420 ++stackidx;
8421 }
8422 }
8423 }
8424
8425 if (score + SCORE_SWAP < minscore)
8426 {
8427 /* If swapping two characters makes a match then the
8428 * substitution is more expensive, thus there is no need to
8429 * try both. */
8430 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8431 {
8432 /* Swap two characters, that is: skip them. */
8433 gi += 2;
8434 bi += 2;
8435 score += SCORE_SWAP;
8436 continue;
8437 }
8438 }
8439
8440 /* Substitute one character for another which is the same
8441 * thing as deleting a character from both goodword and badword.
8442 * Use a better score when there is only a case difference. */
8443 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8444 score += SCORE_ICASE;
8445 else
8446 {
8447 /* For a similar character use SCORE_SIMILAR. */
8448 if (slang != NULL
8449 && slang->sl_has_map
8450 && similar_chars(slang, gc, bc))
8451 score += SCORE_SIMILAR;
8452 else
8453 score += SCORE_SUBST;
8454 }
8455
8456 if (score < minscore)
8457 {
8458 /* Do the substitution. */
8459 ++gi;
8460 ++bi;
8461 continue;
8462 }
8463 }
8464pop:
8465 /*
8466 * Get here to try the next alternative, pop it from the stack.
8467 */
8468 if (stackidx == 0) /* stack is empty, finished */
8469 break;
8470
8471 /* pop an item from the stack */
8472 --stackidx;
8473 gi = stack[stackidx].goodi;
8474 bi = stack[stackidx].badi;
8475 score = stack[stackidx].score;
8476 }
8477
8478 /* When the score goes over "limit" it may actually be much higher.
8479 * Return a very large number to avoid going below the limit when giving a
8480 * bonus. */
8481 if (minscore > limit)
8482 return SCORE_MAXMAX;
8483 return minscore;
8484}
8485#endif
8486
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008487/*
8488 * ":spellinfo"
8489 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008491ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008492{
8493 int lpi;
8494 langp_T *lp;
8495 char_u *p;
8496
8497 if (no_spell_checking(curwin))
8498 return;
8499
8500 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008501 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008502 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008503 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008504 msg_puts((char_u *)"file: ");
8505 msg_puts(lp->lp_slang->sl_fname);
8506 msg_putchar('\n');
8507 p = lp->lp_slang->sl_info;
8508 if (p != NULL)
8509 {
8510 msg_puts(p);
8511 msg_putchar('\n');
8512 }
8513 }
8514 msg_end();
8515}
8516
Bram Moolenaar4770d092006-01-12 23:22:24 +00008517#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8518#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008519#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008520#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8521#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008522
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008523/*
8524 * ":spelldump"
8525 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008526 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008527ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008528{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008529 char_u *spl;
8530 long dummy;
8531
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008532 if (no_spell_checking(curwin))
8533 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008534 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008535
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008536 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008537 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008538
8539 /* enable spelling locally in the new window */
8540 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008541 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008542 vim_free(spl);
8543
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008544 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008545 return;
8546
Bram Moolenaar860cae12010-06-05 23:22:07 +02008547 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008548
8549 /* Delete the empty line that we started with. */
8550 if (curbuf->b_ml.ml_line_count > 1)
8551 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8552
8553 redraw_later(NOT_VALID);
8554}
8555
8556/*
8557 * Go through all possible words and:
8558 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8559 * "ic" and "dir" are not used.
8560 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8561 */
8562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008563spell_dump_compl(
8564 char_u *pat, /* leading part of the word */
8565 int ic, /* ignore case */
8566 int *dir, /* direction for adding matches */
8567 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008568{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008569 langp_T *lp;
8570 slang_T *slang;
8571 idx_T arridx[MAXWLEN];
8572 int curi[MAXWLEN];
8573 char_u word[MAXWLEN];
8574 int c;
8575 char_u *byts;
8576 idx_T *idxs;
8577 linenr_T lnum = 0;
8578 int round;
8579 int depth;
8580 int n;
8581 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008582 char_u *region_names = NULL; /* region names being used */
8583 int do_region = TRUE; /* dump region names and numbers */
8584 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008585 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008586 int dumpflags = dumpflags_arg;
8587 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008588
Bram Moolenaard0131a82006-03-04 21:46:13 +00008589 /* When ignoring case or when the pattern starts with capital pass this on
8590 * to dump_word(). */
8591 if (pat != NULL)
8592 {
8593 if (ic)
8594 dumpflags |= DUMPFLAG_ICASE;
8595 else
8596 {
8597 n = captype(pat, NULL);
8598 if (n == WF_ONECAP)
8599 dumpflags |= DUMPFLAG_ONECAP;
8600 else if (n == WF_ALLCAP
8601#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008602 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008603#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008604 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +00008605#endif
8606 )
8607 dumpflags |= DUMPFLAG_ALLCAP;
8608 }
8609 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008610
Bram Moolenaar7887d882005-07-01 22:33:52 +00008611 /* Find out if we can support regions: All languages must support the same
8612 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008613 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008614 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008615 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008616 p = lp->lp_slang->sl_regions;
8617 if (p[0] != 0)
8618 {
8619 if (region_names == NULL) /* first language with regions */
8620 region_names = p;
8621 else if (STRCMP(region_names, p) != 0)
8622 {
8623 do_region = FALSE; /* region names are different */
8624 break;
8625 }
8626 }
8627 }
8628
8629 if (do_region && region_names != NULL)
8630 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008631 if (pat == NULL)
8632 {
8633 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8634 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8635 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008636 }
8637 else
8638 do_region = FALSE;
8639
8640 /*
8641 * Loop over all files loaded for the entries in 'spelllang'.
8642 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008643 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008644 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008645 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008646 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008647 if (slang->sl_fbyts == NULL) /* reloading failed */
8648 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008649
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008650 if (pat == NULL)
8651 {
8652 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8653 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8654 }
8655
8656 /* When matching with a pattern and there are no prefixes only use
8657 * parts of the tree that match "pat". */
8658 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008659 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008660 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008661 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008662
8663 /* round 1: case-folded tree
8664 * round 2: keep-case tree */
8665 for (round = 1; round <= 2; ++round)
8666 {
8667 if (round == 1)
8668 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008669 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008670 byts = slang->sl_fbyts;
8671 idxs = slang->sl_fidxs;
8672 }
8673 else
8674 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008675 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008676 byts = slang->sl_kbyts;
8677 idxs = slang->sl_kidxs;
8678 }
8679 if (byts == NULL)
8680 continue; /* array is empty */
8681
8682 depth = 0;
8683 arridx[0] = 0;
8684 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008685 while (depth >= 0 && !got_int
8686 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008687 {
8688 if (curi[depth] > byts[arridx[depth]])
8689 {
8690 /* Done all bytes at this node, go up one level. */
8691 --depth;
8692 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008693 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008694 }
8695 else
8696 {
8697 /* Do one more byte at this node. */
8698 n = arridx[depth] + curi[depth];
8699 ++curi[depth];
8700 c = byts[n];
8701 if (c == 0)
8702 {
8703 /* End of word, deal with the word.
8704 * Don't use keep-case words in the fold-case tree,
8705 * they will appear in the keep-case tree.
8706 * Only use the word when the region matches. */
8707 flags = (int)idxs[n];
8708 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008709 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008710 && (do_region
8711 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008712 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008713 & lp->lp_region) != 0))
8714 {
8715 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008716 if (!do_region)
8717 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008718
8719 /* Dump the basic word if there is no prefix or
8720 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008721 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008722 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008723 {
8724 dump_word(slang, word, pat, dir,
8725 dumpflags, flags, lnum);
8726 if (pat == NULL)
8727 ++lnum;
8728 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008729
8730 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008731 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008732 lnum = dump_prefixes(slang, word, pat, dir,
8733 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008734 }
8735 }
8736 else
8737 {
8738 /* Normal char, go one level deeper. */
8739 word[depth++] = c;
8740 arridx[depth] = idxs[n];
8741 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008742
8743 /* Check if this characters matches with the pattern.
8744 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008745 * Always ignore case here, dump_word() will check
8746 * proper case later. This isn't exactly right when
8747 * length changes for multi-byte characters with
8748 * ignore case... */
8749 if (depth <= patlen
8750 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008751 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008752 }
8753 }
8754 }
8755 }
8756 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008757}
8758
8759/*
8760 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008761 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008762 */
8763 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008764dump_word(
8765 slang_T *slang,
8766 char_u *word,
8767 char_u *pat,
8768 int *dir,
8769 int dumpflags,
8770 int wordflags,
8771 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008772{
8773 int keepcap = FALSE;
8774 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008775 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008776 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008777 char_u badword[MAXWLEN + 10];
8778 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008779 int flags = wordflags;
8780
8781 if (dumpflags & DUMPFLAG_ONECAP)
8782 flags |= WF_ONECAP;
8783 if (dumpflags & DUMPFLAG_ALLCAP)
8784 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008785
Bram Moolenaar4770d092006-01-12 23:22:24 +00008786 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008787 {
8788 /* Need to fix case according to "flags". */
8789 make_case_word(word, cword, flags);
8790 p = cword;
8791 }
8792 else
8793 {
8794 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008795 if ((dumpflags & DUMPFLAG_KEEPCASE)
8796 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008797 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008798 keepcap = TRUE;
8799 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008800 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008801
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008802 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008803 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008804 /* Add flags and regions after a slash. */
8805 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008806 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008807 STRCPY(badword, p);
8808 STRCAT(badword, "/");
8809 if (keepcap)
8810 STRCAT(badword, "=");
8811 if (flags & WF_BANNED)
8812 STRCAT(badword, "!");
8813 else if (flags & WF_RARE)
8814 STRCAT(badword, "?");
8815 if (flags & WF_REGION)
8816 for (i = 0; i < 7; ++i)
8817 if (flags & (0x10000 << i))
8818 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8819 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008820 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008821
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008822 if (dumpflags & DUMPFLAG_COUNT)
8823 {
8824 hashitem_T *hi;
8825
8826 /* Include the word count for ":spelldump!". */
8827 hi = hash_find(&slang->sl_wordcount, tw);
8828 if (!HASHITEM_EMPTY(hi))
8829 {
8830 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8831 tw, HI2WC(hi)->wc_count);
8832 p = IObuff;
8833 }
8834 }
8835
8836 ml_append(lnum, p, (colnr_T)0, FALSE);
8837 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008838 else if (((dumpflags & DUMPFLAG_ICASE)
8839 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8840 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008841 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008842 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008843 /* if dir was BACKWARD then honor it just once */
8844 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008845}
8846
8847/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008848 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8849 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008850 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008851 * Return the updated line number.
8852 */
8853 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008854dump_prefixes(
8855 slang_T *slang,
8856 char_u *word, /* case-folded word */
8857 char_u *pat,
8858 int *dir,
8859 int dumpflags,
8860 int flags, /* flags with prefix ID */
8861 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008862{
8863 idx_T arridx[MAXWLEN];
8864 int curi[MAXWLEN];
8865 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008866 char_u word_up[MAXWLEN];
8867 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008868 int c;
8869 char_u *byts;
8870 idx_T *idxs;
8871 linenr_T lnum = startlnum;
8872 int depth;
8873 int n;
8874 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008875 int i;
8876
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008877 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008878 * upper-case letter in word_up[]. */
8879 c = PTR2CHAR(word);
8880 if (SPELL_TOUPPER(c) != c)
8881 {
8882 onecap_copy(word, word_up, TRUE);
8883 has_word_up = TRUE;
8884 }
8885
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008886 byts = slang->sl_pbyts;
8887 idxs = slang->sl_pidxs;
8888 if (byts != NULL) /* array not is empty */
8889 {
8890 /*
8891 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008892 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008893 */
8894 depth = 0;
8895 arridx[0] = 0;
8896 curi[0] = 1;
8897 while (depth >= 0 && !got_int)
8898 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008899 n = arridx[depth];
8900 len = byts[n];
8901 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008902 {
8903 /* Done all bytes at this node, go up one level. */
8904 --depth;
8905 line_breakcheck();
8906 }
8907 else
8908 {
8909 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008910 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008911 ++curi[depth];
8912 c = byts[n];
8913 if (c == 0)
8914 {
8915 /* End of prefix, find out how many IDs there are. */
8916 for (i = 1; i < len; ++i)
8917 if (byts[n + i] != 0)
8918 break;
8919 curi[depth] += i - 1;
8920
Bram Moolenaar53805d12005-08-01 07:08:33 +00008921 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8922 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008923 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008924 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008925 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008926 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008927 : flags, lnum);
8928 if (lnum != 0)
8929 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008930 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008931
8932 /* Check for prefix that matches the word when the
8933 * first letter is upper-case, but only if the prefix has
8934 * a condition. */
8935 if (has_word_up)
8936 {
8937 c = valid_word_prefix(i, n, flags, word_up, slang,
8938 TRUE);
8939 if (c != 0)
8940 {
8941 vim_strncpy(prefix + depth, word_up,
8942 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008943 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008944 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008945 : flags, lnum);
8946 if (lnum != 0)
8947 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008948 }
8949 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008950 }
8951 else
8952 {
8953 /* Normal char, go one level deeper. */
8954 prefix[depth++] = c;
8955 arridx[depth] = idxs[n];
8956 curi[depth] = 1;
8957 }
8958 }
8959 }
8960 }
8961
8962 return lnum;
8963}
8964
Bram Moolenaar95529562005-08-25 21:21:38 +00008965/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008966 * Move "p" to the end of word "start".
8967 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008968 */
8969 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008970spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008971{
8972 char_u *p = start;
8973
Bram Moolenaar860cae12010-06-05 23:22:07 +02008974 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008975 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008976 return p;
8977}
8978
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008979#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008980/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008981 * For Insert mode completion CTRL-X s:
8982 * Find start of the word in front of column "startcol".
8983 * We don't check if it is badly spelled, with completion we can only change
8984 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008985 * Returns the column number of the word.
8986 */
8987 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008988spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008989{
8990 char_u *line;
8991 char_u *p;
8992 int col = 0;
8993
Bram Moolenaar95529562005-08-25 21:21:38 +00008994 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008995 return startcol;
8996
8997 /* Find a word character before "startcol". */
8998 line = ml_get_curline();
8999 for (p = line + startcol; p > line; )
9000 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009001 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01009002 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009003 break;
9004 }
9005
9006 /* Go back to start of the word. */
9007 while (p > line)
9008 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009009 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01009010 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009011 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009012 break;
9013 col = 0;
9014 }
9015
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009016 return col;
9017}
9018
9019/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00009020 * Need to check for 'spellcapcheck' now, the word is removed before
9021 * expand_spelling() is called. Therefore the ugly global variable.
9022 */
9023static int spell_expand_need_cap;
9024
9025 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009026spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00009027{
9028 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
9029}
9030
9031/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009032 * Get list of spelling suggestions.
9033 * Used for Insert mode completion CTRL-X ?.
9034 * Returns the number of matches. The matches are in "matchp[]", array of
9035 * allocated strings.
9036 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009037 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009038expand_spelling(
9039 linenr_T lnum UNUSED,
9040 char_u *pat,
9041 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009042{
9043 garray_T ga;
9044
Bram Moolenaar4770d092006-01-12 23:22:24 +00009045 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009046 *matchp = ga.ga_data;
9047 return ga.ga_len;
9048}
9049#endif
9050
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009051#endif /* FEAT_SPELL */