blob: 05a9d2cd84013cad820df6f9ea6652ca81f66e4e [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 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001853
Bram Moolenaarb765d632005-06-07 21:00:02 +00001854 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001855 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001856 STRCPY(sl.sl_lang, lang);
1857 sl.sl_slang = NULL;
1858 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001859
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001860 /* We may retry when no spell file is found for the language, an
1861 * autocommand may load it then. */
1862 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001863 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001864 /*
1865 * Find the first spell file for "lang" in 'runtimepath' and load it.
1866 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001867 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001868#ifdef VMS
1869 "spell/%s_%s.spl",
1870#else
1871 "spell/%s.%s.spl",
1872#endif
1873 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001874 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001875
1876 if (r == FAIL && *sl.sl_lang != NUL)
1877 {
1878 /* Try loading the ASCII version. */
1879 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001880#ifdef VMS
1881 "spell/%s_ascii.spl",
1882#else
1883 "spell/%s.ascii.spl",
1884#endif
1885 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001886 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001887
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001888 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1889 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1890 curbuf->b_fname, FALSE, curbuf))
1891 continue;
1892 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001893 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001894 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001895 }
1896
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001897 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001898 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01001899 smsg((char_u *)
1900#ifdef VMS
1901 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1902#else
1903 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1904#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001905 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001906 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001907 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001908 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001909 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001910 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001911 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001912 }
1913}
1914
1915/*
1916 * Return the encoding used for spell checking: Use 'encoding', except that we
1917 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1918 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001919 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001920spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001921{
1922
1923#ifdef FEAT_MBYTE
1924 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1925 return p_enc;
1926#endif
1927 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001928}
1929
1930/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001931 * Get the name of the .spl file for the internal wordlist into
1932 * "fname[MAXPATHL]".
1933 */
1934 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001935int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001936{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001937 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001938 int_wordlist, spell_enc());
1939}
1940
1941/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001942 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001943 * Caller must fill "sl_next".
1944 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001945 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001946slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001947{
1948 slang_T *lp;
1949
Bram Moolenaar51485f02005-06-04 21:55:20 +00001950 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 if (lp != NULL)
1952 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001953 if (lang != NULL)
1954 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001955 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001956 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001957 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001958 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001959 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001960 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001961
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001962 return lp;
1963}
1964
1965/*
1966 * Free the contents of an slang_T and the structure itself.
1967 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001968 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001969slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001971 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001972 vim_free(lp->sl_fname);
1973 slang_clear(lp);
1974 vim_free(lp);
1975}
1976
1977/*
1978 * Clear an slang_T so that the file can be reloaded.
1979 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001980 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001981slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001982{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001983 garray_T *gap;
1984 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001985 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001986 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001987 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001988
Bram Moolenaard23a8232018-02-10 18:45:26 +01001989 VIM_CLEAR(lp->sl_fbyts);
1990 VIM_CLEAR(lp->sl_kbyts);
1991 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001992
Bram Moolenaard23a8232018-02-10 18:45:26 +01001993 VIM_CLEAR(lp->sl_fidxs);
1994 VIM_CLEAR(lp->sl_kidxs);
1995 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996
Bram Moolenaar4770d092006-01-12 23:22:24 +00001997 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001999 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2000 while (gap->ga_len > 0)
2001 {
2002 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2003 vim_free(ftp->ft_from);
2004 vim_free(ftp->ft_to);
2005 }
2006 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002008
2009 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002010 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002011 {
2012 /* "ga_len" is set to 1 without adding an item for latin1 */
2013 if (gap->ga_data != NULL)
2014 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2015 for (i = 0; i < gap->ga_len; ++i)
2016 vim_free(((int **)gap->ga_data)[i]);
2017 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002018 else
2019 /* SAL items: free salitem_T items */
2020 while (gap->ga_len > 0)
2021 {
2022 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2023 vim_free(smp->sm_lead);
2024 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2025 vim_free(smp->sm_to);
2026#ifdef FEAT_MBYTE
2027 vim_free(smp->sm_lead_w);
2028 vim_free(smp->sm_oneof_w);
2029 vim_free(smp->sm_to_w);
2030#endif
2031 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002032 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002033
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002034 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02002035 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002036 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002037 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002038
Bram Moolenaard23a8232018-02-10 18:45:26 +01002039 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002040
Bram Moolenaard23a8232018-02-10 18:45:26 +01002041 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002042
Bram Moolenaar473de612013-06-08 18:19:48 +02002043 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002044 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002045 VIM_CLEAR(lp->sl_comprules);
2046 VIM_CLEAR(lp->sl_compstartflags);
2047 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002048
Bram Moolenaard23a8232018-02-10 18:45:26 +01002049 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002050 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002051
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002052 ga_clear_strings(&lp->sl_comppat);
2053
Bram Moolenaar4770d092006-01-12 23:22:24 +00002054 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2055 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002056
Bram Moolenaar4770d092006-01-12 23:22:24 +00002057#ifdef FEAT_MBYTE
2058 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002059#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002060
Bram Moolenaar4770d092006-01-12 23:22:24 +00002061 /* Clear info from .sug file. */
2062 slang_clear_sug(lp);
2063
Bram Moolenaar5195e452005-08-19 20:32:47 +00002064 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002065 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002066 lp->sl_compsylmax = MAXWLEN;
2067 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002068}
2069
2070/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002071 * Clear the info from the .sug file in "lp".
2072 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002073 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002074slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002075{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002076 VIM_CLEAR(lp->sl_sbyts);
2077 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002078 close_spellbuf(lp->sl_sugbuf);
2079 lp->sl_sugbuf = NULL;
2080 lp->sl_sugloaded = FALSE;
2081 lp->sl_sugtime = 0;
2082}
2083
2084/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002085 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002086 * Invoked through do_in_runtimepath().
2087 */
2088 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002089spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002090{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002091 spelload_T *slp = (spelload_T *)cookie;
2092 slang_T *slang;
2093
2094 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2095 if (slang != NULL)
2096 {
2097 /* When a previously loaded file has NOBREAK also use it for the
2098 * ".add" files. */
2099 if (slp->sl_nobreak && slang->sl_add)
2100 slang->sl_nobreak = TRUE;
2101 else if (slang->sl_nobreak)
2102 slp->sl_nobreak = TRUE;
2103
2104 slp->sl_slang = slang;
2105 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002106}
2107
Bram Moolenaar4770d092006-01-12 23:22:24 +00002108
2109/*
2110 * Add a word to the hashtable of common words.
2111 * If it's already there then the counter is increased.
2112 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002114count_common_word(
2115 slang_T *lp,
2116 char_u *word,
2117 int len, /* word length, -1 for upto NUL */
2118 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002119{
2120 hash_T hash;
2121 hashitem_T *hi;
2122 wordcount_T *wc;
2123 char_u buf[MAXWLEN];
2124 char_u *p;
2125
2126 if (len == -1)
2127 p = word;
2128 else
2129 {
2130 vim_strncpy(buf, word, len);
2131 p = buf;
2132 }
2133
2134 hash = hash_hash(p);
2135 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2136 if (HASHITEM_EMPTY(hi))
2137 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002138 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002139 if (wc == NULL)
2140 return;
2141 STRCPY(wc->wc_word, p);
2142 wc->wc_count = count;
2143 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2144 }
2145 else
2146 {
2147 wc = HI2WC(hi);
2148 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2149 wc->wc_count = MAXWORDCOUNT;
2150 }
2151}
2152
2153/*
2154 * Adjust the score of common words.
2155 */
2156 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002157score_wordcount_adj(
2158 slang_T *slang,
2159 int score,
2160 char_u *word,
2161 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002162{
2163 hashitem_T *hi;
2164 wordcount_T *wc;
2165 int bonus;
2166 int newscore;
2167
2168 hi = hash_find(&slang->sl_wordcount, word);
2169 if (!HASHITEM_EMPTY(hi))
2170 {
2171 wc = HI2WC(hi);
2172 if (wc->wc_count < SCORE_THRES2)
2173 bonus = SCORE_COMMON1;
2174 else if (wc->wc_count < SCORE_THRES3)
2175 bonus = SCORE_COMMON2;
2176 else
2177 bonus = SCORE_COMMON3;
2178 if (split)
2179 newscore = score - bonus / 2;
2180 else
2181 newscore = score - bonus;
2182 if (newscore < 0)
2183 return 0;
2184 return newscore;
2185 }
2186 return score;
2187}
2188
Bram Moolenaar5195e452005-08-19 20:32:47 +00002189
Bram Moolenaar6de68532005-08-24 22:08:48 +00002190/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002191 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002192 * Like strchr() but independent of locale.
2193 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002194 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002195byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196{
2197 char_u *p;
2198
2199 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002200 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002201 return TRUE;
2202 return FALSE;
2203}
2204
Bram Moolenaar5195e452005-08-19 20:32:47 +00002205#define SY_MAXLEN 30
2206typedef struct syl_item_S
2207{
2208 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2209 int sy_len;
2210} syl_item_T;
2211
2212/*
2213 * Truncate "slang->sl_syllable" at the first slash and put the following items
2214 * in "slang->sl_syl_items".
2215 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002216 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002217init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002218{
2219 char_u *p;
2220 char_u *s;
2221 int l;
2222 syl_item_T *syl;
2223
2224 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2225 p = vim_strchr(slang->sl_syllable, '/');
2226 while (p != NULL)
2227 {
2228 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002229 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002230 break;
2231 s = p;
2232 p = vim_strchr(p, '/');
2233 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002234 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002235 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002236 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002237 if (l >= SY_MAXLEN)
2238 return SP_FORMERROR;
2239 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002240 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002241 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2242 + slang->sl_syl_items.ga_len++;
2243 vim_strncpy(syl->sy_chars, s, l);
2244 syl->sy_len = l;
2245 }
2246 return OK;
2247}
2248
2249/*
2250 * Count the number of syllables in "word".
2251 * When "word" contains spaces the syllables after the last space are counted.
2252 * Returns zero if syllables are not defines.
2253 */
2254 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002255count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002256{
2257 int cnt = 0;
2258 int skip = FALSE;
2259 char_u *p;
2260 int len;
2261 int i;
2262 syl_item_T *syl;
2263 int c;
2264
2265 if (slang->sl_syllable == NULL)
2266 return 0;
2267
2268 for (p = word; *p != NUL; p += len)
2269 {
2270 /* When running into a space reset counter. */
2271 if (*p == ' ')
2272 {
2273 len = 1;
2274 cnt = 0;
2275 continue;
2276 }
2277
2278 /* Find longest match of syllable items. */
2279 len = 0;
2280 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2281 {
2282 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2283 if (syl->sy_len > len
2284 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2285 len = syl->sy_len;
2286 }
2287 if (len != 0) /* found a match, count syllable */
2288 {
2289 ++cnt;
2290 skip = FALSE;
2291 }
2292 else
2293 {
2294 /* No recognized syllable item, at least a syllable char then? */
2295#ifdef FEAT_MBYTE
2296 c = mb_ptr2char(p);
2297 len = (*mb_ptr2len)(p);
2298#else
2299 c = *p;
2300 len = 1;
2301#endif
2302 if (vim_strchr(slang->sl_syllable, c) == NULL)
2303 skip = FALSE; /* No, search for next syllable */
2304 else if (!skip)
2305 {
2306 ++cnt; /* Yes, count it */
2307 skip = TRUE; /* don't count following syllable chars */
2308 }
2309 }
2310 }
2311 return cnt;
2312}
2313
2314/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002315 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002316 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002317 */
2318 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002319did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320{
2321 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002322 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002324 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002325 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002326 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002327 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002329 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002330 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002331 int len;
2332 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002333 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002334 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002335 char_u *use_region = NULL;
2336 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002337 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002338 int i, j;
2339 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002340 static int recursive = FALSE;
2341 char_u *ret_msg = NULL;
2342 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002343 bufref_T bufref;
2344
2345 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002346
2347 /* We don't want to do this recursively. May happen when a language is
2348 * not available and the SpellFileMissing autocommand opens a new buffer
2349 * in which 'spell' is set. */
2350 if (recursive)
2351 return NULL;
2352 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002353
2354 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002355 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002356
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002357 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002358 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002359 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002360 if (spl_copy == NULL)
2361 goto theend;
2362
Bram Moolenaar2593e032013-11-14 03:54:07 +01002363#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002364 wp->w_s->b_cjk = 0;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002365#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002366
2367 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002368 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002369 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002370 /* Get one language name. */
2371 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002372 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002373 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002374
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002375 if (STRCMP(lang, "cjk") == 0)
2376 {
Bram Moolenaar2593e032013-11-14 03:54:07 +01002377#ifdef FEAT_MBYTE
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002378 wp->w_s->b_cjk = 1;
Bram Moolenaar2593e032013-11-14 03:54:07 +01002379#endif
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002380 continue;
2381 }
2382
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002383 /* If the name ends in ".spl" use it as the name of the spell file.
2384 * If there is a region name let "region" point to it and remove it
2385 * from the name. */
2386 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2387 {
2388 filename = TRUE;
2389
Bram Moolenaarb6356332005-07-18 21:40:44 +00002390 /* Locate a region and remove it from the file name. */
2391 p = vim_strchr(gettail(lang), '_');
2392 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2393 && !ASCII_ISALPHA(p[3]))
2394 {
2395 vim_strncpy(region_cp, p + 1, 2);
2396 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002397 region = region_cp;
2398 }
2399 else
2400 dont_use_region = TRUE;
2401
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002402 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002403 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2404 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002405 break;
2406 }
2407 else
2408 {
2409 filename = FALSE;
2410 if (len > 3 && lang[len - 3] == '_')
2411 {
2412 region = lang + len - 2;
2413 len -= 3;
2414 lang[len] = NUL;
2415 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002416 else
2417 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002418
2419 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002420 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2421 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002422 break;
2423 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002424
Bram Moolenaarb6356332005-07-18 21:40:44 +00002425 if (region != NULL)
2426 {
2427 /* If the region differs from what was used before then don't
2428 * use it for 'spellfile'. */
2429 if (use_region != NULL && STRCMP(region, use_region) != 0)
2430 dont_use_region = TRUE;
2431 use_region = region;
2432 }
2433
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002434 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002435 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002436 {
2437 if (filename)
2438 (void)spell_load_file(lang, lang, NULL, FALSE);
2439 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002440 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002441 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002442 /* SpellFileMissing autocommands may do anything, including
2443 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002444 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002445 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02002446 ret_msg = (char_u *)N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002447 goto theend;
2448 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002449 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002450 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002452 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002453 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002454 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002455 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2456 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2457 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002459 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002460 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002461 {
2462 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002463 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002464 if (c == REGION_ALL)
2465 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002466 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002467 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002468 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002469 /* This addition file is for other regions. */
2470 region_mask = 0;
2471 }
2472 else
2473 /* This is probably an error. Give a warning and
2474 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002475 smsg((char_u *)
2476 _("Warning: region %s not supported"),
2477 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002478 }
2479 else
2480 region_mask = 1 << c;
2481 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002482
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002483 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002484 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002485 if (ga_grow(&ga, 1) == FAIL)
2486 {
2487 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002488 ret_msg = e_outofmem;
2489 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002490 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002491 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002492 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2493 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002494 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002495 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002496 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002497 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002498 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499 }
2500
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002501 /* round 0: load int_wordlist, if possible.
2502 * round 1: load first name in 'spellfile'.
2503 * round 2: load second name in 'spellfile.
2504 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002505 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002506 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002508 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002509 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002510 /* Internal wordlist, if there is one. */
2511 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002512 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002513 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002514 }
2515 else
2516 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002517 /* One entry in 'spellfile'. */
2518 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2519 STRCAT(spf_name, ".spl");
2520
2521 /* If it was already found above then skip it. */
2522 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002523 {
2524 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2525 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002526 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002527 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002528 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002529 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002530 }
2531
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002532 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002533 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2534 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002535 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002536 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002537 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002538 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002539 * region name, the region is ignored otherwise. for int_wordlist
2540 * use an arbitrary name. */
2541 if (round == 0)
2542 STRCPY(lang, "internal wordlist");
2543 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002544 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002545 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002546 p = vim_strchr(lang, '.');
2547 if (p != NULL)
2548 *p = NUL; /* truncate at ".encoding.add" */
2549 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002550 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002551
2552 /* If one of the languages has NOBREAK we assume the addition
2553 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002554 if (slang != NULL && nobreak)
2555 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002557 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002559 region_mask = REGION_ALL;
2560 if (use_region != NULL && !dont_use_region)
2561 {
2562 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002563 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002564 if (c != REGION_ALL)
2565 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002566 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002567 /* This spell file is for other regions. */
2568 region_mask = 0;
2569 }
2570
2571 if (region_mask != 0)
2572 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002573 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2574 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2575 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002576 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2577 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002578 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002580 }
2581 }
2582
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002583 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002584 ga_clear(&wp->w_s->b_langp);
2585 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002586
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002587 /* For each language figure out what language to use for sound folding and
2588 * REP items. If the language doesn't support it itself use another one
2589 * with the same name. E.g. for "en-math" use "en". */
2590 for (i = 0; i < ga.ga_len; ++i)
2591 {
2592 lp = LANGP_ENTRY(ga, i);
2593
2594 /* sound folding */
2595 if (lp->lp_slang->sl_sal.ga_len > 0)
2596 /* language does sound folding itself */
2597 lp->lp_sallang = lp->lp_slang;
2598 else
2599 /* find first similar language that does sound folding */
2600 for (j = 0; j < ga.ga_len; ++j)
2601 {
2602 lp2 = LANGP_ENTRY(ga, j);
2603 if (lp2->lp_slang->sl_sal.ga_len > 0
2604 && STRNCMP(lp->lp_slang->sl_name,
2605 lp2->lp_slang->sl_name, 2) == 0)
2606 {
2607 lp->lp_sallang = lp2->lp_slang;
2608 break;
2609 }
2610 }
2611
2612 /* REP items */
2613 if (lp->lp_slang->sl_rep.ga_len > 0)
2614 /* language has REP items itself */
2615 lp->lp_replang = lp->lp_slang;
2616 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002617 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002618 for (j = 0; j < ga.ga_len; ++j)
2619 {
2620 lp2 = LANGP_ENTRY(ga, j);
2621 if (lp2->lp_slang->sl_rep.ga_len > 0
2622 && STRNCMP(lp->lp_slang->sl_name,
2623 lp2->lp_slang->sl_name, 2) == 0)
2624 {
2625 lp->lp_replang = lp2->lp_slang;
2626 break;
2627 }
2628 }
2629 }
2630
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002631theend:
2632 vim_free(spl_copy);
2633 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002634 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002635 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002636}
2637
2638/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002639 * Clear the midword characters for buffer "buf".
2640 */
2641 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002642clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002643{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002644 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002645#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01002646 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002647#endif
2648}
2649
2650/*
2651 * Use the "sl_midword" field of language "lp" for buffer "buf".
2652 * They add up to any currently used midword characters.
2653 */
2654 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002656{
2657 char_u *p;
2658
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002659 if (lp->sl_midword == NULL) /* there aren't any */
2660 return;
2661
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002662 for (p = lp->sl_midword; *p != NUL; )
2663#ifdef FEAT_MBYTE
2664 if (has_mbyte)
2665 {
2666 int c, l, n;
2667 char_u *bp;
2668
2669 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002670 l = (*mb_ptr2len)(p);
2671 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002672 wp->w_s->b_spell_ismw[c] = TRUE;
2673 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002674 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002675 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002676 else
2677 {
2678 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002679 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2680 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002681 if (bp != NULL)
2682 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002683 vim_free(wp->w_s->b_spell_ismw_mb);
2684 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002685 vim_strncpy(bp + n, p, l);
2686 }
2687 }
2688 p += l;
2689 }
2690 else
2691#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002692 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002693}
2694
2695/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002696 * Find the region "region[2]" in "rp" (points to "sl_regions").
2697 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002698 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002699 */
2700 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002701find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002702{
2703 int i;
2704
2705 for (i = 0; ; i += 2)
2706 {
2707 if (rp[i] == NUL)
2708 return REGION_ALL;
2709 if (rp[i] == region[0] && rp[i + 1] == region[1])
2710 break;
2711 }
2712 return i / 2;
2713}
2714
2715/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002718 * Word WF_ONECAP
2719 * W WORD WF_ALLCAP
2720 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002721 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002722 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002723captype(
2724 char_u *word,
2725 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726{
2727 char_u *p;
2728 int c;
2729 int firstcap;
2730 int allcap;
2731 int past_second = FALSE; /* past second word char */
2732
2733 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002734 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002736 return 0; /* only non-word characters, illegal word */
2737#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002738 if (has_mbyte)
2739 c = mb_ptr2char_adv(&p);
2740 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002741#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002742 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002743 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002744
2745 /*
2746 * Need to check all letters to find a word with mixed upper/lower.
2747 * But a word with an upper char only at start is a ONECAP.
2748 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002749 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002750 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002751 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002752 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002753 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002754 {
2755 /* UUl -> KEEPCAP */
2756 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002757 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002758 allcap = FALSE;
2759 }
2760 else if (!allcap)
2761 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002762 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002763 past_second = TRUE;
2764 }
2765
2766 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002767 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002768 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002769 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002770 return 0;
2771}
2772
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002773/*
2774 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2775 * capital. So that make_case_word() can turn WOrd into Word.
2776 * Add ALLCAP for "WOrD".
2777 */
2778 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002779badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002780{
2781 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002782 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002783 int l, u;
2784 int first;
2785 char_u *p;
2786
2787 if (flags & WF_KEEPCAP)
2788 {
2789 /* Count the number of UPPER and lower case letters. */
2790 l = u = 0;
2791 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002792 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002793 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002794 c = PTR2CHAR(p);
2795 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002796 {
2797 ++u;
2798 if (p == word)
2799 first = TRUE;
2800 }
2801 else
2802 ++l;
2803 }
2804
2805 /* If there are more UPPER than lower case letters suggest an
2806 * ALLCAP word. Otherwise, if the first letter is UPPER then
2807 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2808 * require three upper case letters. */
2809 if (u > l && u > 2)
2810 flags |= WF_ALLCAP;
2811 else if (first)
2812 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002813
2814 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2815 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002816 }
2817 return flags;
2818}
2819
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002820/*
2821 * Delete the internal wordlist and its .spl file.
2822 */
2823 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002824spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002825{
2826 char_u fname[MAXPATHL];
2827
2828 if (int_wordlist != NULL)
2829 {
2830 mch_remove(int_wordlist);
2831 int_wordlist_spl(fname);
2832 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002833 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002834 }
2835}
2836
2837#if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002838/*
2839 * Free all languages.
2840 */
2841 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002842spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002843{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002844 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002845 buf_T *buf;
2846
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002847 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002848 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002849 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002850
2851 while (first_lang != NULL)
2852 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002853 slang = first_lang;
2854 first_lang = slang->sl_next;
2855 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002856 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002857
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002858 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002859
Bram Moolenaard23a8232018-02-10 18:45:26 +01002860 VIM_CLEAR(repl_to);
2861 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002862}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002863#endif
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002864
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002865#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002866/*
2867 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002868 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869 */
2870 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002871spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002872{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002873 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002874
Bram Moolenaarea408852005-06-25 22:49:46 +00002875 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002876 init_spell_chartab();
2877
2878 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002879 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002880
2881 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002882 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002883 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002884 /* Only load the wordlists when 'spelllang' is set and there is a
2885 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002887 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002888 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002889 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002890 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002891 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002892 }
2893 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002894 }
2895}
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002896#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002897
Bram Moolenaarb765d632005-06-07 21:00:02 +00002898/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002899 * Opposite of offset2bytes().
2900 * "pp" points to the bytes and is advanced over it.
2901 * Returns the offset.
2902 */
2903 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002904bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002905{
2906 char_u *p = *pp;
2907 int nr;
2908 int c;
2909
2910 c = *p++;
2911 if ((c & 0x80) == 0x00) /* 1 byte */
2912 {
2913 nr = c - 1;
2914 }
2915 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2916 {
2917 nr = (c & 0x3f) - 1;
2918 nr = nr * 255 + (*p++ - 1);
2919 }
2920 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2921 {
2922 nr = (c & 0x1f) - 1;
2923 nr = nr * 255 + (*p++ - 1);
2924 nr = nr * 255 + (*p++ - 1);
2925 }
2926 else /* 4 bytes */
2927 {
2928 nr = (c & 0x0f) - 1;
2929 nr = nr * 255 + (*p++ - 1);
2930 nr = nr * 255 + (*p++ - 1);
2931 nr = nr * 255 + (*p++ - 1);
2932 }
2933
2934 *pp = p;
2935 return nr;
2936}
2937
Bram Moolenaar4770d092006-01-12 23:22:24 +00002938
2939/*
2940 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2941 * list and only contains text lines. Can use a swapfile to reduce memory
2942 * use.
2943 * Most other fields are invalid! Esp. watch out for string options being
2944 * NULL and there is no undo info.
2945 * Returns NULL when out of memory.
2946 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002947 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002948open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002949{
2950 buf_T *buf;
2951
2952 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2953 if (buf != NULL)
2954 {
2955 buf->b_spell = TRUE;
2956 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002957#ifdef FEAT_CRYPT
2958 buf->b_p_key = empty_option;
2959#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002960 ml_open(buf);
2961 ml_open_file(buf); /* create swap file now */
2962 }
2963 return buf;
2964}
2965
2966/*
2967 * Close the buffer used for spell info.
2968 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002969 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002970close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002971{
2972 if (buf != NULL)
2973 {
2974 ml_close(buf, TRUE);
2975 vim_free(buf);
2976 }
2977}
2978
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002979/*
2980 * Init the chartab used for spelling for ASCII.
2981 * EBCDIC is not supported!
2982 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002983 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002984clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002985{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002986 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002987
2988 /* Init everything to FALSE. */
2989 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2990 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2991 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002992 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002993 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002994 sp->st_upper[i] = i;
2995 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002996
2997 /* We include digits. A word shouldn't start with a digit, but handling
2998 * that is done separately. */
2999 for (i = '0'; i <= '9'; ++i)
3000 sp->st_isw[i] = TRUE;
3001 for (i = 'A'; i <= 'Z'; ++i)
3002 {
3003 sp->st_isw[i] = TRUE;
3004 sp->st_isu[i] = TRUE;
3005 sp->st_fold[i] = i + 0x20;
3006 }
3007 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003008 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003009 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003010 sp->st_upper[i] = i - 0x20;
3011 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003012}
3013
3014/*
3015 * Init the chartab used for spelling. Only depends on 'encoding'.
3016 * Called once while starting up and when 'encoding' changes.
3017 * The default is to use isalpha(), but the spell file should define the word
3018 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003019 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003020 */
3021 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003022init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003023{
3024 int i;
3025
3026 did_set_spelltab = FALSE;
3027 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003028#ifdef FEAT_MBYTE
3029 if (enc_dbcs)
3030 {
3031 /* DBCS: assume double-wide characters are word characters. */
3032 for (i = 128; i <= 255; ++i)
3033 if (MB_BYTE2LEN(i) == 2)
3034 spelltab.st_isw[i] = TRUE;
3035 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003036 else if (enc_utf8)
3037 {
3038 for (i = 128; i < 256; ++i)
3039 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003040 int f = utf_fold(i);
3041 int u = utf_toupper(i);
3042
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003043 spelltab.st_isu[i] = utf_isupper(i);
3044 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02003045 /* The folded/upper-cased value is different between latin1 and
3046 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
3047 * value for utf-8 to avoid this. */
3048 spelltab.st_fold[i] = (f < 256) ? f : i;
3049 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003050 }
3051 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003052 else
3053#endif
3054 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003055 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003056 for (i = 128; i < 256; ++i)
3057 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003058 if (MB_ISUPPER(i))
3059 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003060 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003061 spelltab.st_isu[i] = TRUE;
3062 spelltab.st_fold[i] = MB_TOLOWER(i);
3063 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003064 else if (MB_ISLOWER(i))
3065 {
3066 spelltab.st_isw[i] = TRUE;
3067 spelltab.st_upper[i] = MB_TOUPPER(i);
3068 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003069 }
3070 }
3071}
3072
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003073
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003074/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003075 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003076 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003077 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003078 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003079 */
3080 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003081spell_iswordp(
3082 char_u *p,
3083 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003084{
Bram Moolenaarea408852005-06-25 22:49:46 +00003085#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003086 char_u *s;
3087 int l;
3088 int c;
3089
3090 if (has_mbyte)
3091 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003092 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003093 s = p;
3094 if (l == 1)
3095 {
3096 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003097 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003098 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003099 }
3100 else
3101 {
3102 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003103 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3104 : (wp->w_s->b_spell_ismw_mb != NULL
3105 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003106 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003107 }
3108
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003109 c = mb_ptr2char(s);
3110 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003111 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003112 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003113 }
Bram Moolenaarea408852005-06-25 22:49:46 +00003114#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003115
Bram Moolenaar860cae12010-06-05 23:22:07 +02003116 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003117}
3118
3119/*
3120 * Return TRUE if "p" points to a word character.
3121 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3122 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003123 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003124spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003125{
3126#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003127 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003128
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003129 if (has_mbyte)
3130 {
3131 c = mb_ptr2char(p);
3132 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003133 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003134 return spelltab.st_isw[c];
3135 }
3136#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003137 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003138}
3139
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003140#ifdef FEAT_MBYTE
3141/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003142 * Return TRUE if word class indicates a word character.
3143 * Only for characters above 255.
3144 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003145 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003146 */
3147 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003148spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003149{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003150 if (wp->w_s->b_cjk)
3151 /* East Asian characters are not considered word characters. */
3152 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003153 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3154}
3155
3156/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003157 * Return TRUE if "p" points to a word character.
3158 * Wide version of spell_iswordp().
3159 */
3160 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003161spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003162{
3163 int *s;
3164
Bram Moolenaar860cae12010-06-05 23:22:07 +02003165 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3166 : (wp->w_s->b_spell_ismw_mb != NULL
3167 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003168 s = p + 1;
3169 else
3170 s = p;
3171
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003172 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003173 {
3174 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003175 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003176 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003177 return spell_mb_isword_class(
3178 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003179 return 0;
3180 }
3181 return spelltab.st_isw[*s];
3182}
3183#endif
3184
Bram Moolenaarea408852005-06-25 22:49:46 +00003185/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003186 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3187 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003188 * When using a multi-byte 'encoding' the length may change!
3189 * Returns FAIL when something wrong.
3190 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003191 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003192spell_casefold(
3193 char_u *str,
3194 int len,
3195 char_u *buf,
3196 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003197{
3198 int i;
3199
3200 if (len >= buflen)
3201 {
3202 buf[0] = NUL;
3203 return FAIL; /* result will not fit */
3204 }
3205
3206#ifdef FEAT_MBYTE
3207 if (has_mbyte)
3208 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003209 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003210 char_u *p;
3211 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003212
3213 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003214 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003215 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003216 if (outi + MB_MAXBYTES > buflen)
3217 {
3218 buf[outi] = NUL;
3219 return FAIL;
3220 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003221 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003222 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003223 }
3224 buf[outi] = NUL;
3225 }
3226 else
3227#endif
3228 {
3229 /* Be quick for non-multibyte encodings. */
3230 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003231 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003232 buf[i] = NUL;
3233 }
3234
3235 return OK;
3236}
3237
Bram Moolenaar4770d092006-01-12 23:22:24 +00003238/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003239#define SPS_BEST 1
3240#define SPS_FAST 2
3241#define SPS_DOUBLE 4
3242
Bram Moolenaar4770d092006-01-12 23:22:24 +00003243static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3244static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003245
3246/*
3247 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003248 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003249 */
3250 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003251spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003252{
3253 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003254 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003255 char_u buf[MAXPATHL];
3256 int f;
3257
3258 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003259 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003260
3261 for (p = p_sps; *p != NUL; )
3262 {
3263 copy_option_part(&p, buf, MAXPATHL, ",");
3264
3265 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003266 if (VIM_ISDIGIT(*buf))
3267 {
3268 s = buf;
3269 sps_limit = getdigits(&s);
3270 if (*s != NUL && !VIM_ISDIGIT(*s))
3271 f = -1;
3272 }
3273 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003274 f = SPS_BEST;
3275 else if (STRCMP(buf, "fast") == 0)
3276 f = SPS_FAST;
3277 else if (STRCMP(buf, "double") == 0)
3278 f = SPS_DOUBLE;
3279 else if (STRNCMP(buf, "expr:", 5) != 0
3280 && STRNCMP(buf, "file:", 5) != 0)
3281 f = -1;
3282
3283 if (f == -1 || (sps_flags != 0 && f != 0))
3284 {
3285 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003286 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003287 return FAIL;
3288 }
3289 if (f != 0)
3290 sps_flags = f;
3291 }
3292
3293 if (sps_flags == 0)
3294 sps_flags = SPS_BEST;
3295
3296 return OK;
3297}
3298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003299/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003300 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003301 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003302 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003303 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003304 */
3305 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003306spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307{
3308 char_u *line;
3309 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003310 char_u wcopy[MAXWLEN + 2];
3311 char_u *p;
3312 int i;
3313 int c;
3314 suginfo_T sug;
3315 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003316 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003317 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003318 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003319 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003320 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003321 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003322
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003323 if (no_spell_checking(curwin))
3324 return;
3325
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003326 if (VIsual_active)
3327 {
3328 /* Use the Visually selected text as the bad word. But reject
3329 * a multi-line selection. */
3330 if (curwin->w_cursor.lnum != VIsual.lnum)
3331 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003332 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003333 return;
3334 }
3335 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3336 if (badlen < 0)
3337 badlen = -badlen;
3338 else
3339 curwin->w_cursor.col = VIsual.col;
3340 ++badlen;
3341 end_visual_mode();
3342 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003343 /* Find the start of the badly spelled word. */
3344 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003345 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003346 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003347 /* No bad word or it starts after the cursor: use the word under the
3348 * cursor. */
3349 curwin->w_cursor = prev_cursor;
3350 line = ml_get_curline();
3351 p = line + curwin->w_cursor.col;
3352 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003353 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003354 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003355 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003356 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003357 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003358
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003359 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003360 {
3361 beep_flush();
3362 return;
3363 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003364 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 }
3366
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003367 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003368
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003369 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003370 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003371
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003372 /* Make a copy of current line since autocommands may free the line. */
3373 line = vim_strsave(ml_get_curline());
3374 if (line == NULL)
3375 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003376
Bram Moolenaar5195e452005-08-19 20:32:47 +00003377 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3378 * 'spellsuggest', whatever is smaller. */
3379 if (sps_limit > (int)Rows - 2)
3380 limit = (int)Rows - 2;
3381 else
3382 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003383 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003384 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003385
3386 if (sug.su_ga.ga_len == 0)
3387 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003388 else if (count > 0)
3389 {
3390 if (count > sug.su_ga.ga_len)
3391 smsg((char_u *)_("Sorry, only %ld suggestions"),
3392 (long)sug.su_ga.ga_len);
3393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 else
3395 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003396 VIM_CLEAR(repl_from);
3397 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003398
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003399#ifdef FEAT_RIGHTLEFT
3400 /* When 'rightleft' is set the list is drawn right-left. */
3401 cmdmsg_rl = curwin->w_p_rl;
3402 if (cmdmsg_rl)
3403 msg_col = Columns - 1;
3404#endif
3405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 /* List the suggestions. */
3407 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003408 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003409 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003410 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3411 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003412#ifdef FEAT_RIGHTLEFT
3413 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3414 {
3415 /* And now the rabbit from the high hat: Avoid showing the
3416 * untranslated message rightleft. */
3417 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3418 sug.su_badlen, sug.su_badptr);
3419 }
3420#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003421 msg_puts(IObuff);
3422 msg_clr_eos();
3423 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003425 msg_scroll = TRUE;
3426 for (i = 0; i < sug.su_ga.ga_len; ++i)
3427 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003428 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429
3430 /* The suggested word may replace only part of the bad word, add
3431 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003432 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003433 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003434 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003435 sug.su_badptr + stp->st_orglen,
3436 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003437 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3438#ifdef FEAT_RIGHTLEFT
3439 if (cmdmsg_rl)
3440 rl_mirror(IObuff);
3441#endif
3442 msg_puts(IObuff);
3443
3444 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003445 msg_puts(IObuff);
3446
3447 /* The word may replace more than "su_badlen". */
3448 if (sug.su_badlen < stp->st_orglen)
3449 {
3450 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3451 stp->st_orglen, sug.su_badptr);
3452 msg_puts(IObuff);
3453 }
3454
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003455 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003456 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003457 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003458 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003459 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003460 stp->st_salscore ? "s " : "",
3461 stp->st_score, stp->st_altscore);
3462 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003463 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003464 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003465#ifdef FEAT_RIGHTLEFT
3466 if (cmdmsg_rl)
3467 /* Mirror the numbers, but keep the leading space. */
3468 rl_mirror(IObuff + 1);
3469#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003470 msg_advance(30);
3471 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003473 msg_putchar('\n');
3474 }
3475
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003476#ifdef FEAT_RIGHTLEFT
3477 cmdmsg_rl = FALSE;
3478 msg_col = 0;
3479#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003480 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003481 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003482 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003483 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003484 lines_left = Rows; /* avoid more prompt */
3485 /* don't delay for 'smd' in normal_cmd() */
3486 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003487 }
3488
Bram Moolenaard12a1322005-08-21 22:08:24 +00003489 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3490 {
3491 /* Save the from and to text for :spellrepall. */
3492 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003493 if (sug.su_badlen > stp->st_orglen)
3494 {
3495 /* Replacing less than "su_badlen", append the remainder to
3496 * repl_to. */
3497 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3498 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3499 sug.su_badlen - stp->st_orglen,
3500 sug.su_badptr + stp->st_orglen);
3501 repl_to = vim_strsave(IObuff);
3502 }
3503 else
3504 {
3505 /* Replacing su_badlen or more, use the whole word. */
3506 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3507 repl_to = vim_strsave(stp->st_word);
3508 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003509
3510 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003511 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3512 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003513 if (p != NULL)
3514 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003515 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003516 mch_memmove(p, line, c);
3517 STRCPY(p + c, stp->st_word);
3518 STRCAT(p, sug.su_badptr + stp->st_orglen);
3519 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3520 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003521
3522 /* For redo we use a change-word command. */
3523 ResetRedobuff();
3524 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003525 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003526 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003527 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003528
3529 /* After this "p" may be invalid. */
3530 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003531 }
3532 }
3533 else
3534 curwin->w_cursor = prev_cursor;
3535
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003536 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003537skip:
3538 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003539}
3540
3541/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003542 * Check if the word at line "lnum" column "col" is required to start with a
3543 * capital. This uses 'spellcapcheck' of the current buffer.
3544 */
3545 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003546check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003547{
3548 int need_cap = FALSE;
3549 char_u *line;
3550 char_u *line_copy = NULL;
3551 char_u *p;
3552 colnr_T endcol;
3553 regmatch_T regmatch;
3554
Bram Moolenaar860cae12010-06-05 23:22:07 +02003555 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003556 return FALSE;
3557
3558 line = ml_get_curline();
3559 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003560 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003561 {
3562 /* At start of line, check if previous line is empty or sentence
3563 * ends there. */
3564 if (lnum == 1)
3565 need_cap = TRUE;
3566 else
3567 {
3568 line = ml_get(lnum - 1);
3569 if (*skipwhite(line) == NUL)
3570 need_cap = TRUE;
3571 else
3572 {
3573 /* Append a space in place of the line break. */
3574 line_copy = concat_str(line, (char_u *)" ");
3575 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003577 }
3578 }
3579 }
3580 else
3581 endcol = col;
3582
3583 if (endcol > 0)
3584 {
3585 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003586 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003587 regmatch.rm_ic = FALSE;
3588 p = line + endcol;
3589 for (;;)
3590 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003591 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003592 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003593 break;
3594 if (vim_regexec(&regmatch, p, 0)
3595 && regmatch.endp[0] == line + endcol)
3596 {
3597 need_cap = TRUE;
3598 break;
3599 }
3600 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003601 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003602 }
3603
3604 vim_free(line_copy);
3605
3606 return need_cap;
3607}
3608
3609
3610/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003611 * ":spellrepall"
3612 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003613 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003614ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003615{
3616 pos_T pos = curwin->w_cursor;
3617 char_u *frompat;
3618 int addlen;
3619 char_u *line;
3620 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003621 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003622 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003623
3624 if (repl_from == NULL || repl_to == NULL)
3625 {
3626 EMSG(_("E752: No previous spell replacement"));
3627 return;
3628 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003629 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003630
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003631 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003632 if (frompat == NULL)
3633 return;
3634 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3635 p_ws = FALSE;
3636
Bram Moolenaar5195e452005-08-19 20:32:47 +00003637 sub_nsubs = 0;
3638 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003639 curwin->w_cursor.lnum = 0;
3640 while (!got_int)
3641 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003642 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003643 || u_save_cursor() == FAIL)
3644 break;
3645
3646 /* Only replace when the right word isn't there yet. This happens
3647 * when changing "etc" to "etc.". */
3648 line = ml_get_curline();
3649 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3650 repl_to, STRLEN(repl_to)) != 0)
3651 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003652 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003653 if (p == NULL)
3654 break;
3655 mch_memmove(p, line, curwin->w_cursor.col);
3656 STRCPY(p + curwin->w_cursor.col, repl_to);
3657 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3658 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3659 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003660
3661 if (curwin->w_cursor.lnum != prev_lnum)
3662 {
3663 ++sub_nlines;
3664 prev_lnum = curwin->w_cursor.lnum;
3665 }
3666 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003667 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003668 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003669 }
3670
3671 p_ws = save_ws;
3672 curwin->w_cursor = pos;
3673 vim_free(frompat);
3674
Bram Moolenaar5195e452005-08-19 20:32:47 +00003675 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003676 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003677 else
3678 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003679}
3680
3681/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003682 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3683 * a list of allocated strings.
3684 */
3685 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003686spell_suggest_list(
3687 garray_T *gap,
3688 char_u *word,
3689 int maxcount, /* maximum nr of suggestions */
3690 int need_cap, /* 'spellcapcheck' matched */
3691 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003692{
3693 suginfo_T sug;
3694 int i;
3695 suggest_T *stp;
3696 char_u *wcopy;
3697
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003698 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003699
3700 /* Make room in "gap". */
3701 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003702 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003703 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003704 for (i = 0; i < sug.su_ga.ga_len; ++i)
3705 {
3706 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003707
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003708 /* The suggested word may replace only part of "word", add the not
3709 * replaced part. */
3710 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003711 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003712 if (wcopy == NULL)
3713 break;
3714 STRCPY(wcopy, stp->st_word);
3715 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3716 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3717 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003718 }
3719
3720 spell_find_cleanup(&sug);
3721}
3722
3723/*
3724 * Find spell suggestions for the word at the start of "badptr".
3725 * Return the suggestions in "su->su_ga".
3726 * The maximum number of suggestions is "maxcount".
3727 * Note: does use info for the current window.
3728 * This is based on the mechanisms of Aspell, but completely reimplemented.
3729 */
3730 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003731spell_find_suggest(
3732 char_u *badptr,
3733 int badlen, /* length of bad word or 0 if unknown */
3734 suginfo_T *su,
3735 int maxcount,
3736 int banbadword, /* don't include badword in suggestions */
3737 int need_cap, /* word should start with capital */
3738 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003739{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003740 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003741 char_u buf[MAXPATHL];
3742 char_u *p;
3743 int do_combine = FALSE;
3744 char_u *sps_copy;
3745#ifdef FEAT_EVAL
3746 static int expr_busy = FALSE;
3747#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003748 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003749 int i;
3750 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003751
3752 /*
3753 * Set the info in "*su".
3754 */
3755 vim_memset(su, 0, sizeof(suginfo_T));
3756 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3757 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003758 if (*badptr == NUL)
3759 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003760 hash_init(&su->su_banned);
3761
3762 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003763 if (badlen != 0)
3764 su->su_badlen = badlen;
3765 else
3766 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003767 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003768 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003769
3770 if (su->su_badlen >= MAXWLEN)
3771 su->su_badlen = MAXWLEN - 1; /* just in case */
3772 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3773 (void)spell_casefold(su->su_badptr, su->su_badlen,
3774 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003775 /* TODO: make this work if the case-folded text is longer than the original
3776 * text. Currently an illegal byte causes wrong pointer computations. */
3777 su->su_fbadword[su->su_badlen] = NUL;
3778
Bram Moolenaar0c405862005-06-22 22:26:26 +00003779 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003780 su->su_badflags = badword_captype(su->su_badptr,
3781 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003782 if (need_cap)
3783 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003784
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003785 /* Find the default language for sound folding. We simply use the first
3786 * one in 'spelllang' that supports sound folding. That's good for when
3787 * using multiple files for one language, it's not that bad when mixing
3788 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003789 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003790 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003791 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003792 if (lp->lp_sallang != NULL)
3793 {
3794 su->su_sallang = lp->lp_sallang;
3795 break;
3796 }
3797 }
3798
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003799 /* Soundfold the bad word with the default sound folding, so that we don't
3800 * have to do this many times. */
3801 if (su->su_sallang != NULL)
3802 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3803 su->su_sal_badword);
3804
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003805 /* If the word is not capitalised and spell_check() doesn't consider the
3806 * word to be bad then it might need to be capitalised. Add a suggestion
3807 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003808 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003809 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003810 {
3811 make_case_word(su->su_badword, buf, WF_ONECAP);
3812 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003813 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003814 }
3815
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003816 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003817 if (banbadword)
3818 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003819
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003820 /* Make a copy of 'spellsuggest', because the expression may change it. */
3821 sps_copy = vim_strsave(p_sps);
3822 if (sps_copy == NULL)
3823 return;
3824
3825 /* Loop over the items in 'spellsuggest'. */
3826 for (p = sps_copy; *p != NUL; )
3827 {
3828 copy_option_part(&p, buf, MAXPATHL, ",");
3829
3830 if (STRNCMP(buf, "expr:", 5) == 0)
3831 {
3832#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003833 /* Evaluate an expression. Skip this when called recursively,
3834 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003835 if (!expr_busy)
3836 {
3837 expr_busy = TRUE;
3838 spell_suggest_expr(su, buf + 5);
3839 expr_busy = FALSE;
3840 }
3841#endif
3842 }
3843 else if (STRNCMP(buf, "file:", 5) == 0)
3844 /* Use list of suggestions in a file. */
3845 spell_suggest_file(su, buf + 5);
3846 else
3847 {
3848 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003849 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003850 if (sps_flags & SPS_DOUBLE)
3851 do_combine = TRUE;
3852 }
3853 }
3854
3855 vim_free(sps_copy);
3856
3857 if (do_combine)
3858 /* Combine the two list of suggestions. This must be done last,
3859 * because sorting changes the order again. */
3860 score_combine(su);
3861}
3862
3863#ifdef FEAT_EVAL
3864/*
3865 * Find suggestions by evaluating expression "expr".
3866 */
3867 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003868spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003869{
3870 list_T *list;
3871 listitem_T *li;
3872 int score;
3873 char_u *p;
3874
3875 /* The work is split up in a few parts to avoid having to export
3876 * suginfo_T.
3877 * First evaluate the expression and get the resulting list. */
3878 list = eval_spell_expr(su->su_badword, expr);
3879 if (list != NULL)
3880 {
3881 /* Loop over the items in the list. */
3882 for (li = list->lv_first; li != NULL; li = li->li_next)
3883 if (li->li_tv.v_type == VAR_LIST)
3884 {
3885 /* Get the word and the score from the items. */
3886 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003887 if (score >= 0 && score <= su->su_maxscore)
3888 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3889 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003890 }
3891 list_unref(list);
3892 }
3893
Bram Moolenaar4770d092006-01-12 23:22:24 +00003894 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3895 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003896 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3897}
3898#endif
3899
3900/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003901 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003902 */
3903 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003904spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003905{
3906 FILE *fd;
3907 char_u line[MAXWLEN * 2];
3908 char_u *p;
3909 int len;
3910 char_u cword[MAXWLEN];
3911
3912 /* Open the file. */
3913 fd = mch_fopen((char *)fname, "r");
3914 if (fd == NULL)
3915 {
3916 EMSG2(_(e_notopen), fname);
3917 return;
3918 }
3919
3920 /* Read it line by line. */
3921 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3922 {
3923 line_breakcheck();
3924
3925 p = vim_strchr(line, '/');
3926 if (p == NULL)
3927 continue; /* No Tab found, just skip the line. */
3928 *p++ = NUL;
3929 if (STRICMP(su->su_badword, line) == 0)
3930 {
3931 /* Match! Isolate the good word, until CR or NL. */
3932 for (len = 0; p[len] >= ' '; ++len)
3933 ;
3934 p[len] = NUL;
3935
3936 /* If the suggestion doesn't have specific case duplicate the case
3937 * of the bad word. */
3938 if (captype(p, NULL) == 0)
3939 {
3940 make_case_word(p, cword, su->su_badflags);
3941 p = cword;
3942 }
3943
3944 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003945 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003946 }
3947 }
3948
3949 fclose(fd);
3950
Bram Moolenaar4770d092006-01-12 23:22:24 +00003951 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3952 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003953 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3954}
3955
3956/*
3957 * Find suggestions for the internal method indicated by "sps_flags".
3958 */
3959 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003960spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003961{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003962 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003963 * Load the .sug file(s) that are available and not done yet.
3964 */
3965 suggest_load_files();
3966
3967 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003968 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003969 *
3970 * Set a maximum score to limit the combination of operations that is
3971 * tried.
3972 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003973 suggest_try_special(su);
3974
3975 /*
3976 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3977 * from the .aff file and inserting a space (split the word).
3978 */
3979 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003980
3981 /* For the resulting top-scorers compute the sound-a-like score. */
3982 if (sps_flags & SPS_DOUBLE)
3983 score_comp_sal(su);
3984
3985 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003986 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003987 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003988 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003989 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00003990 if (sps_flags & SPS_BEST)
3991 /* Adjust the word score for the suggestions found so far for how
3992 * they sounds like. */
3993 rescore_suggestions(su);
3994
3995 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003996 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00003997 * for the soundfold word, limits the changes that are being tried,
3998 * and "su_sfmaxscore" the rescored score, which is set by
3999 * cleanup_suggestions().
4000 * First find words with a small edit distance, because this is much
4001 * faster and often already finds the top-N suggestions. If we didn't
4002 * find many suggestions try again with a higher edit distance.
4003 * "sl_sounddone" is used to avoid doing the same word twice.
4004 */
4005 suggest_try_soundalike_prep();
4006 su->su_maxscore = SCORE_SFMAX1;
4007 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004008 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004009 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4010 {
4011 /* We didn't find enough matches, try again, allowing more
4012 * changes to the soundfold word. */
4013 su->su_maxscore = SCORE_SFMAX2;
4014 suggest_try_soundalike(su);
4015 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
4016 {
4017 /* Still didn't find enough matches, try again, allowing even
4018 * more changes to the soundfold word. */
4019 su->su_maxscore = SCORE_SFMAX3;
4020 suggest_try_soundalike(su);
4021 }
4022 }
4023 su->su_maxscore = su->su_sfmaxscore;
4024 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004025 }
4026
Bram Moolenaar4770d092006-01-12 23:22:24 +00004027 /* When CTRL-C was hit while searching do show the results. Only clear
4028 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004029 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00004030 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004031 {
4032 (void)vgetc();
4033 got_int = FALSE;
4034 }
4035
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004036 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004037 {
4038 if (sps_flags & SPS_BEST)
4039 /* Adjust the word score for how it sounds like. */
4040 rescore_suggestions(su);
4041
Bram Moolenaar4770d092006-01-12 23:22:24 +00004042 /* Remove bogus suggestions, sort and truncate at "maxcount". */
4043 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004044 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004045 }
4046}
4047
4048/*
4049 * Free the info put in "*su" by spell_find_suggest().
4050 */
4051 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004052spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004053{
4054 int i;
4055
4056 /* Free the suggestions. */
4057 for (i = 0; i < su->su_ga.ga_len; ++i)
4058 vim_free(SUG(su->su_ga, i).st_word);
4059 ga_clear(&su->su_ga);
4060 for (i = 0; i < su->su_sga.ga_len; ++i)
4061 vim_free(SUG(su->su_sga, i).st_word);
4062 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004063
4064 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004065 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066}
4067
4068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004069 * Make a copy of "word", with the first letter upper or lower cased, to
4070 * "wcopy[MAXWLEN]". "word" must not be empty.
4071 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004073 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004074onecap_copy(
4075 char_u *word,
4076 char_u *wcopy,
4077 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004078{
4079 char_u *p;
4080 int c;
4081 int l;
4082
4083 p = word;
4084#ifdef FEAT_MBYTE
4085 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004086 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004087 else
4088#endif
4089 c = *p++;
4090 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004091 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004093 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094#ifdef FEAT_MBYTE
4095 if (has_mbyte)
4096 l = mb_char2bytes(c, wcopy);
4097 else
4098#endif
4099 {
4100 l = 1;
4101 wcopy[0] = c;
4102 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004103 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104}
4105
4106/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004107 * Make a copy of "word" with all the letters upper cased into
4108 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 */
4110 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004111allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112{
4113 char_u *s;
4114 char_u *d;
4115 int c;
4116
4117 d = wcopy;
4118 for (s = word; *s != NUL; )
4119 {
4120#ifdef FEAT_MBYTE
4121 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004122 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123 else
4124#endif
4125 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004126
4127#ifdef FEAT_MBYTE
Bram Moolenaard3184b52011-09-02 14:18:20 +02004128 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004129 * would cause weird errors in other 8-bit encodings. */
4130 if (enc_latin1like && c == 0xdf)
4131 {
4132 c = 'S';
4133 if (d - wcopy >= MAXWLEN - 1)
4134 break;
4135 *d++ = c;
4136 }
4137 else
4138#endif
4139 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004140
4141#ifdef FEAT_MBYTE
4142 if (has_mbyte)
4143 {
4144 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4145 break;
4146 d += mb_char2bytes(c, d);
4147 }
4148 else
4149#endif
4150 {
4151 if (d - wcopy >= MAXWLEN - 1)
4152 break;
4153 *d++ = c;
4154 }
4155 }
4156 *d = NUL;
4157}
4158
4159/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004160 * Try finding suggestions by recognizing specific situations.
4161 */
4162 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004163suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004164{
4165 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004166 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004167 int c;
4168 char_u word[MAXWLEN];
4169
4170 /*
4171 * Recognize a word that is repeated: "the the".
4172 */
4173 p = skiptowhite(su->su_fbadword);
4174 len = p - su->su_fbadword;
4175 p = skipwhite(p);
4176 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4177 {
4178 /* Include badflags: if the badword is onecap or allcap
4179 * use that for the goodword too: "The the" -> "The". */
4180 c = su->su_fbadword[len];
4181 su->su_fbadword[len] = NUL;
4182 make_case_word(su->su_fbadword, word, su->su_badflags);
4183 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004184
4185 /* Give a soundalike score of 0, compute the score as if deleting one
4186 * character. */
4187 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004188 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004189 }
4190}
4191
4192/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004193 * Change the 0 to 1 to measure how much time is spent in each state.
4194 * Output is dumped in "suggestprof".
4195 */
4196#if 0
4197# define SUGGEST_PROFILE
4198proftime_T current;
4199proftime_T total;
4200proftime_T times[STATE_FINAL + 1];
4201long counts[STATE_FINAL + 1];
4202
4203 static void
4204prof_init(void)
4205{
4206 for (int i = 0; i <= STATE_FINAL; ++i)
4207 {
4208 profile_zero(&times[i]);
4209 counts[i] = 0;
4210 }
4211 profile_start(&current);
4212 profile_start(&total);
4213}
4214
4215/* call before changing state */
4216 static void
4217prof_store(state_T state)
4218{
4219 profile_end(&current);
4220 profile_add(&times[state], &current);
4221 ++counts[state];
4222 profile_start(&current);
4223}
4224# define PROF_STORE(state) prof_store(state);
4225
4226 static void
4227prof_report(char *name)
4228{
4229 FILE *fd = fopen("suggestprof", "a");
4230
4231 profile_end(&total);
4232 fprintf(fd, "-----------------------\n");
4233 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4234 for (int i = 0; i <= STATE_FINAL; ++i)
4235 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4236 fclose(fd);
4237}
4238#else
4239# define PROF_STORE(state)
4240#endif
4241
4242/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 * Try finding suggestions by adding/removing/swapping letters.
4244 */
4245 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004246suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247{
4248 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004249 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004251 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004252 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253
4254 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004255 * to find matches (esp. REP items). Append some more text, changing
4256 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004258 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004259 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004260 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261
Bram Moolenaar860cae12010-06-05 23:22:07 +02004262 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004264 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004265
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004266 /* If reloading a spell file fails it's still in the list but
4267 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004268 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004269 continue;
4270
Bram Moolenaar4770d092006-01-12 23:22:24 +00004271 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004272#ifdef SUGGEST_PROFILE
4273 prof_init();
4274#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004275 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004276#ifdef SUGGEST_PROFILE
4277 prof_report("try_change");
4278#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004279 }
4280}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281
Bram Moolenaar4770d092006-01-12 23:22:24 +00004282/* Check the maximum score, if we go over it we won't try this change. */
4283#define TRY_DEEPER(su, stack, depth, add) \
4284 (stack[depth].ts_score + (add) < su->su_maxscore)
4285
4286/*
4287 * Try finding suggestions by adding/removing/swapping letters.
4288 *
4289 * This uses a state machine. At each node in the tree we try various
4290 * operations. When trying if an operation works "depth" is increased and the
4291 * stack[] is used to store info. This allows combinations, thus insert one
4292 * character, replace one and delete another. The number of changes is
4293 * limited by su->su_maxscore.
4294 *
4295 * After implementing this I noticed an article by Kemal Oflazer that
4296 * describes something similar: "Error-tolerant Finite State Recognition with
4297 * Applications to Morphological Analysis and Spelling Correction" (1996).
4298 * The implementation in the article is simplified and requires a stack of
4299 * unknown depth. The implementation here only needs a stack depth equal to
4300 * the length of the word.
4301 *
4302 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4303 * The mechanism is the same, but we find a match with a sound-folded word
4304 * that comes from one or more original words. Each of these words may be
4305 * added, this is done by add_sound_suggest().
4306 * Don't use:
4307 * the prefix tree or the keep-case tree
4308 * "su->su_badlen"
4309 * anything to do with upper and lower case
4310 * anything to do with word or non-word characters ("spell_iswordp()")
4311 * banned words
4312 * word flags (rare, region, compounding)
4313 * word splitting for now
4314 * "similar_chars()"
4315 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4316 */
4317 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004318suggest_trie_walk(
4319 suginfo_T *su,
4320 langp_T *lp,
4321 char_u *fword,
4322 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004323{
4324 char_u tword[MAXWLEN]; /* good word collected so far */
4325 trystate_T stack[MAXWLEN];
4326 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004327 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004328 * words and split word. NUL terminated
4329 * when going deeper but not when coming
4330 * back. */
4331 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4332 trystate_T *sp;
4333 int newscore;
4334 int score;
4335 char_u *byts, *fbyts, *pbyts;
4336 idx_T *idxs, *fidxs, *pidxs;
4337 int depth;
4338 int c, c2, c3;
4339 int n = 0;
4340 int flags;
4341 garray_T *gap;
4342 idx_T arridx;
4343 int len;
4344 char_u *p;
4345 fromto_T *ftp;
4346 int fl = 0, tl;
4347 int repextra = 0; /* extra bytes in fword[] from REP item */
4348 slang_T *slang = lp->lp_slang;
4349 int fword_ends;
4350 int goodword_ends;
4351#ifdef DEBUG_TRIEWALK
4352 /* Stores the name of the change made at each level. */
4353 char_u changename[MAXWLEN][80];
4354#endif
4355 int breakcheckcount = 1000;
4356 int compound_ok;
4357
4358 /*
4359 * Go through the whole case-fold tree, try changes at each node.
4360 * "tword[]" contains the word collected from nodes in the tree.
4361 * "fword[]" the word we are trying to match with (initially the bad
4362 * word).
4363 */
4364 depth = 0;
4365 sp = &stack[0];
4366 vim_memset(sp, 0, sizeof(trystate_T));
4367 sp->ts_curi = 1;
4368
4369 if (soundfold)
4370 {
4371 /* Going through the soundfold tree. */
4372 byts = fbyts = slang->sl_sbyts;
4373 idxs = fidxs = slang->sl_sidxs;
4374 pbyts = NULL;
4375 pidxs = NULL;
4376 sp->ts_prefixdepth = PFD_NOPREFIX;
4377 sp->ts_state = STATE_START;
4378 }
4379 else
4380 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004381 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004382 * When there are postponed prefixes we need to use these first. At
4383 * the end of the prefix we continue in the case-fold tree.
4384 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004385 fbyts = slang->sl_fbyts;
4386 fidxs = slang->sl_fidxs;
4387 pbyts = slang->sl_pbyts;
4388 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004389 if (pbyts != NULL)
4390 {
4391 byts = pbyts;
4392 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004393 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004394 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4395 }
4396 else
4397 {
4398 byts = fbyts;
4399 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004400 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004401 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004402 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004403 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004404
Bram Moolenaar4770d092006-01-12 23:22:24 +00004405 /*
4406 * Loop to find all suggestions. At each round we either:
4407 * - For the current state try one operation, advance "ts_curi",
4408 * increase "depth".
4409 * - When a state is done go to the next, set "ts_state".
4410 * - When all states are tried decrease "depth".
4411 */
4412 while (depth >= 0 && !got_int)
4413 {
4414 sp = &stack[depth];
4415 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004416 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004417 case STATE_START:
4418 case STATE_NOPREFIX:
4419 /*
4420 * Start of node: Deal with NUL bytes, which means
4421 * tword[] may end here.
4422 */
4423 arridx = sp->ts_arridx; /* current node in the tree */
4424 len = byts[arridx]; /* bytes in this node */
4425 arridx += sp->ts_curi; /* index of current byte */
4426
4427 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004429 /* Skip over the NUL bytes, we use them later. */
4430 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4431 ;
4432 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004433
Bram Moolenaar4770d092006-01-12 23:22:24 +00004434 /* Always past NUL bytes now. */
4435 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004436 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004437 sp->ts_state = STATE_ENDNUL;
4438 sp->ts_save_badflags = su->su_badflags;
4439
4440 /* At end of a prefix or at start of prefixtree: check for
4441 * following word. */
4442 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004443 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004444 /* Set su->su_badflags to the caps type at this position.
4445 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004446#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004447 if (has_mbyte)
4448 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4449 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004450#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004451 n = sp->ts_fidx;
4452 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4453 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004454 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004455#ifdef DEBUG_TRIEWALK
4456 sprintf(changename[depth], "prefix");
4457#endif
4458 go_deeper(stack, depth, 0);
4459 ++depth;
4460 sp = &stack[depth];
4461 sp->ts_prefixdepth = depth - 1;
4462 byts = fbyts;
4463 idxs = fidxs;
4464 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004465
Bram Moolenaar4770d092006-01-12 23:22:24 +00004466 /* Move the prefix to preword[] with the right case
4467 * and make find_keepcap_word() works. */
4468 tword[sp->ts_twordlen] = NUL;
4469 make_case_word(tword + sp->ts_splitoff,
4470 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004471 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004472 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004473 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004474 break;
4475 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004476
Bram Moolenaar4770d092006-01-12 23:22:24 +00004477 if (sp->ts_curi > len || byts[arridx] != 0)
4478 {
4479 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004480 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004481 sp->ts_state = STATE_ENDNUL;
4482 sp->ts_save_badflags = su->su_badflags;
4483 break;
4484 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004485
Bram Moolenaar4770d092006-01-12 23:22:24 +00004486 /*
4487 * End of word in tree.
4488 */
4489 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004490
Bram Moolenaar4770d092006-01-12 23:22:24 +00004491 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004492
4493 /* Skip words with the NOSUGGEST flag. */
4494 if (flags & WF_NOSUGGEST)
4495 break;
4496
Bram Moolenaar4770d092006-01-12 23:22:24 +00004497 fword_ends = (fword[sp->ts_fidx] == NUL
4498 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004499 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004500 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004501 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502
Bram Moolenaar4770d092006-01-12 23:22:24 +00004503 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004504 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004505 {
4506 /* There was a prefix before the word. Check that the prefix
4507 * can be used with this word. */
4508 /* Count the length of the NULs in the prefix. If there are
4509 * none this must be the first try without a prefix. */
4510 n = stack[sp->ts_prefixdepth].ts_arridx;
4511 len = pbyts[n++];
4512 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4513 ;
4514 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004515 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004516 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004517 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004518 if (c == 0)
4519 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004520
Bram Moolenaar4770d092006-01-12 23:22:24 +00004521 /* Use the WF_RARE flag for a rare prefix. */
4522 if (c & WF_RAREPFX)
4523 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004524
Bram Moolenaar4770d092006-01-12 23:22:24 +00004525 /* Tricky: when checking for both prefix and compounding
4526 * we run into the prefix flag first.
4527 * Remember that it's OK, so that we accept the prefix
4528 * when arriving at a compound flag. */
4529 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004530 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004531 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004532
Bram Moolenaar4770d092006-01-12 23:22:24 +00004533 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4534 * appending another compound word below. */
4535 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004536 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004537 goodword_ends = FALSE;
4538 else
4539 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004540
Bram Moolenaar4770d092006-01-12 23:22:24 +00004541 p = NULL;
4542 compound_ok = TRUE;
4543 if (sp->ts_complen > sp->ts_compsplit)
4544 {
4545 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004546 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004547 /* There was a word before this word. When there was no
4548 * change in this word (it was correct) add the first word
4549 * as a suggestion. If this word was corrected too, we
4550 * need to check if a correct word follows. */
4551 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004552 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004553 && STRNCMP(fword + sp->ts_splitfidx,
4554 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004555 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004556 {
4557 preword[sp->ts_prewordlen] = NUL;
4558 newscore = score_wordcount_adj(slang, sp->ts_score,
4559 preword + sp->ts_prewordlen,
4560 sp->ts_prewordlen > 0);
4561 /* Add the suggestion if the score isn't too bad. */
4562 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004563 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004564 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004565 newscore, 0, FALSE,
4566 lp->lp_sallang, FALSE);
4567 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004568 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004569 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004570 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004571 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004572 /* There was a compound word before this word. If this
4573 * word does not support compounding then give up
4574 * (splitting is tried for the word without compound
4575 * flag). */
4576 if (((unsigned)flags >> 24) == 0
4577 || sp->ts_twordlen - sp->ts_splitoff
4578 < slang->sl_compminlen)
4579 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004580#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004581 /* For multi-byte chars check character length against
4582 * COMPOUNDMIN. */
4583 if (has_mbyte
4584 && slang->sl_compminlen > 0
4585 && mb_charlen(tword + sp->ts_splitoff)
4586 < slang->sl_compminlen)
4587 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004588#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00004589
Bram Moolenaar4770d092006-01-12 23:22:24 +00004590 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4591 compflags[sp->ts_complen + 1] = NUL;
4592 vim_strncpy(preword + sp->ts_prewordlen,
4593 tword + sp->ts_splitoff,
4594 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004595
4596 /* Verify CHECKCOMPOUNDPATTERN rules. */
4597 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4598 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004599 compound_ok = FALSE;
4600
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004601 if (compound_ok)
4602 {
4603 p = preword;
4604 while (*skiptowhite(p) != NUL)
4605 p = skipwhite(skiptowhite(p));
4606 if (fword_ends && !can_compound(slang, p,
4607 compflags + sp->ts_compsplit))
4608 /* Compound is not allowed. But it may still be
4609 * possible if we add another (short) word. */
4610 compound_ok = FALSE;
4611 }
4612
Bram Moolenaar4770d092006-01-12 23:22:24 +00004613 /* Get pointer to last char of previous word. */
4614 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004615 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004616 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004617 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004618
Bram Moolenaar4770d092006-01-12 23:22:24 +00004619 /*
4620 * Form the word with proper case in preword.
4621 * If there is a word from a previous split, append.
4622 * For the soundfold tree don't change the case, simply append.
4623 */
4624 if (soundfold)
4625 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4626 else if (flags & WF_KEEPCAP)
4627 /* Must find the word in the keep-case tree. */
4628 find_keepcap_word(slang, tword + sp->ts_splitoff,
4629 preword + sp->ts_prewordlen);
4630 else
4631 {
4632 /* Include badflags: If the badword is onecap or allcap
4633 * use that for the goodword too. But if the badword is
4634 * allcap and it's only one char long use onecap. */
4635 c = su->su_badflags;
4636 if ((c & WF_ALLCAP)
4637#ifdef FEAT_MBYTE
4638 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
4639#else
4640 && su->su_badlen == 1
4641#endif
4642 )
4643 c = WF_ONECAP;
4644 c |= flags;
4645
4646 /* When appending a compound word after a word character don't
4647 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004648 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004649 c &= ~WF_ONECAP;
4650 make_case_word(tword + sp->ts_splitoff,
4651 preword + sp->ts_prewordlen, c);
4652 }
4653
4654 if (!soundfold)
4655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 /* Don't use a banned word. It may appear again as a good
4657 * word, thus remember it. */
4658 if (flags & WF_BANNED)
4659 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004660 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004661 break;
4662 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004663 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004664 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4665 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004666 {
4667 if (slang->sl_compprog == NULL)
4668 break;
4669 /* the word so far was banned but we may try compounding */
4670 goodword_ends = FALSE;
4671 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673
Bram Moolenaar4770d092006-01-12 23:22:24 +00004674 newscore = 0;
4675 if (!soundfold) /* soundfold words don't have flags */
4676 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004677 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004678 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004679 newscore += SCORE_REGION;
4680 if (flags & WF_RARE)
4681 newscore += SCORE_RARE;
4682
Bram Moolenaar0c405862005-06-22 22:26:26 +00004683 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004684 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687
Bram Moolenaar4770d092006-01-12 23:22:24 +00004688 /* TODO: how about splitting in the soundfold tree? */
4689 if (fword_ends
4690 && goodword_ends
4691 && sp->ts_fidx >= sp->ts_fidxtry
4692 && compound_ok)
4693 {
4694 /* The badword also ends: add suggestions. */
4695#ifdef DEBUG_TRIEWALK
4696 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004697 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004698 int j;
4699
4700 /* print the stack of changes that brought us here */
4701 smsg("------ %s -------", fword);
4702 for (j = 0; j < depth; ++j)
4703 smsg("%s", changename[j]);
4704 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004705#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004706 if (soundfold)
4707 {
4708 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004709 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004710 add_sound_suggest(su, preword, sp->ts_score, lp);
4711 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004712 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004713 {
4714 /* Give a penalty when changing non-word char to word
4715 * char, e.g., "thes," -> "these". */
4716 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004717 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004718 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004719 {
4720 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004721 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004722 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004723 newscore += SCORE_NONWORD;
4724 }
4725
Bram Moolenaar4770d092006-01-12 23:22:24 +00004726 /* Give a bonus to words seen before. */
4727 score = score_wordcount_adj(slang,
4728 sp->ts_score + newscore,
4729 preword + sp->ts_prewordlen,
4730 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004731
Bram Moolenaar4770d092006-01-12 23:22:24 +00004732 /* Add the suggestion if the score isn't too bad. */
4733 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004734 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004735 add_suggestion(su, &su->su_ga, preword,
4736 sp->ts_fidx - repextra,
4737 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004738
4739 if (su->su_badflags & WF_MIXCAP)
4740 {
4741 /* We really don't know if the word should be
4742 * upper or lower case, add both. */
4743 c = captype(preword, NULL);
4744 if (c == 0 || c == WF_ALLCAP)
4745 {
4746 make_case_word(tword + sp->ts_splitoff,
4747 preword + sp->ts_prewordlen,
4748 c == 0 ? WF_ALLCAP : 0);
4749
4750 add_suggestion(su, &su->su_ga, preword,
4751 sp->ts_fidx - repextra,
4752 score + SCORE_ICASE, 0, FALSE,
4753 lp->lp_sallang, FALSE);
4754 }
4755 }
4756 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004757 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004758 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004759
Bram Moolenaar4770d092006-01-12 23:22:24 +00004760 /*
4761 * Try word split and/or compounding.
4762 */
4763 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00004764#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004765 /* Don't split halfway a character. */
4766 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004767#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004768 )
4769 {
4770 int try_compound;
4771 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004772
Bram Moolenaar4770d092006-01-12 23:22:24 +00004773 /* If past the end of the bad word don't try a split.
4774 * Otherwise try changing the next word. E.g., find
4775 * suggestions for "the the" where the second "the" is
4776 * different. It's done like a split.
4777 * TODO: word split for soundfold words */
4778 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4779 && !soundfold;
4780
4781 /* Get here in several situations:
4782 * 1. The word in the tree ends:
4783 * If the word allows compounding try that. Otherwise try
4784 * a split by inserting a space. For both check that a
4785 * valid words starts at fword[sp->ts_fidx].
4786 * For NOBREAK do like compounding to be able to check if
4787 * the next word is valid.
4788 * 2. The badword does end, but it was due to a change (e.g.,
4789 * a swap). No need to split, but do check that the
4790 * following word is valid.
4791 * 3. The badword and the word in the tree end. It may still
4792 * be possible to compound another (short) word.
4793 */
4794 try_compound = FALSE;
4795 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004796 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004797 && slang->sl_compprog != NULL
4798 && ((unsigned)flags >> 24) != 0
4799 && sp->ts_twordlen - sp->ts_splitoff
4800 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004801#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004802 && (!has_mbyte
4803 || slang->sl_compminlen == 0
4804 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004805 >= slang->sl_compminlen)
4806#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004807 && (slang->sl_compsylmax < MAXWLEN
4808 || sp->ts_complen + 1 - sp->ts_compsplit
4809 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004810 && (can_be_compound(sp, slang,
4811 compflags, ((unsigned)flags >> 24))))
4812
Bram Moolenaar4770d092006-01-12 23:22:24 +00004813 {
4814 try_compound = TRUE;
4815 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4816 compflags[sp->ts_complen + 1] = NUL;
4817 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004818
Bram Moolenaar4770d092006-01-12 23:22:24 +00004819 /* For NOBREAK we never try splitting, it won't make any word
4820 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004821 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004822 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004823
Bram Moolenaar4770d092006-01-12 23:22:24 +00004824 /* If we could add a compound word, and it's also possible to
4825 * split at this point, do the split first and set
4826 * TSF_DIDSPLIT to avoid doing it again. */
4827 else if (!fword_ends
4828 && try_compound
4829 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4830 {
4831 try_compound = FALSE;
4832 sp->ts_flags |= TSF_DIDSPLIT;
4833 --sp->ts_curi; /* do the same NUL again */
4834 compflags[sp->ts_complen] = NUL;
4835 }
4836 else
4837 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004838
Bram Moolenaar4770d092006-01-12 23:22:24 +00004839 if (try_split || try_compound)
4840 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004841 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004842 {
4843 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004844 * words so far are valid for compounding. If there
4845 * is only one word it must not have the NEEDCOMPOUND
4846 * flag. */
4847 if (sp->ts_complen == sp->ts_compsplit
4848 && (flags & WF_NEEDCOMP))
4849 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004850 p = preword;
4851 while (*skiptowhite(p) != NUL)
4852 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004853 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004854 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004855 compflags + sp->ts_compsplit))
4856 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004857
4858 if (slang->sl_nosplitsugs)
4859 newscore += SCORE_SPLIT_NO;
4860 else
4861 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004862
4863 /* Give a bonus to words seen before. */
4864 newscore = score_wordcount_adj(slang, newscore,
4865 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004866 }
4867
Bram Moolenaar4770d092006-01-12 23:22:24 +00004868 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004870 go_deeper(stack, depth, newscore);
4871#ifdef DEBUG_TRIEWALK
4872 if (!try_compound && !fword_ends)
4873 sprintf(changename[depth], "%.*s-%s: split",
4874 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4875 else
4876 sprintf(changename[depth], "%.*s-%s: compound",
4877 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004880 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004881 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004882 sp->ts_state = STATE_SPLITUNDO;
4883
4884 ++depth;
4885 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004887 /* Append a space to preword when splitting. */
4888 if (!try_compound && !fword_ends)
4889 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004890 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004891 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004892 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004893
4894 /* If the badword has a non-word character at this
4895 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004896 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004897 * character when the word ends. But only when the
4898 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004899 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004900 + sp->ts_fidx,
4901 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004902 || fword_ends)
4903 && fword[sp->ts_fidx] != NUL
4904 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004905 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004906 int l;
4907
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004908 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004909 if (fword_ends)
4910 {
4911 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004912 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004913 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004914 sp->ts_prewordlen += l;
4915 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004916 }
4917 else
4918 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4919 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004920 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004921
Bram Moolenaard12a1322005-08-21 22:08:24 +00004922 /* When compounding include compound flag in
4923 * compflags[] (already set above). When splitting we
4924 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004925 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004926 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004927 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004928 sp->ts_compsplit = sp->ts_complen;
4929 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004930
Bram Moolenaar53805d12005-08-01 07:08:33 +00004931 /* set su->su_badflags to the caps type at this
4932 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004933#ifdef FEAT_MBYTE
4934 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004935 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004936 else
4937#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004938 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004939 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004940 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004942 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004943 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004944
4945 /* If there are postponed prefixes, try these too. */
4946 if (pbyts != NULL)
4947 {
4948 byts = pbyts;
4949 idxs = pidxs;
4950 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004951 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004952 sp->ts_state = STATE_NOPREFIX;
4953 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004954 }
4955 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004956 }
4957 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004958
Bram Moolenaar4770d092006-01-12 23:22:24 +00004959 case STATE_SPLITUNDO:
4960 /* Undo the changes done for word split or compound word. */
4961 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004962
Bram Moolenaar4770d092006-01-12 23:22:24 +00004963 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004964 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004965 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004966
Bram Moolenaar4770d092006-01-12 23:22:24 +00004967 /* In case we went into the prefix tree. */
4968 byts = fbyts;
4969 idxs = fidxs;
4970 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004971
Bram Moolenaar4770d092006-01-12 23:22:24 +00004972 case STATE_ENDNUL:
4973 /* Past the NUL bytes in the node. */
4974 su->su_badflags = sp->ts_save_badflags;
4975 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004976#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00004977 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004978#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004979 )
4980 {
4981 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004982 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 sp->ts_state = STATE_DEL;
4984 break;
4985 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004986 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004987 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004988 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004989
4990 case STATE_PLAIN:
4991 /*
4992 * Go over all possible bytes at this node, add each to tword[]
4993 * and use child node. "ts_curi" is the index.
4994 */
4995 arridx = sp->ts_arridx;
4996 if (sp->ts_curi > byts[arridx])
4997 {
4998 /* Done all bytes at this node, do next state. When still at
4999 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005000 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005001 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005002 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005004 sp->ts_state = STATE_FINAL;
5005 }
5006 else
5007 {
5008 arridx += sp->ts_curi++;
5009 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005010
Bram Moolenaar4770d092006-01-12 23:22:24 +00005011 /* Normal byte, go one level deeper. If it's not equal to the
5012 * byte in the bad word adjust the score. But don't even try
5013 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01005014 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00005015 * delete + substitute. */
5016 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +00005017#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005018 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005019#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005020 )
5021 newscore = 0;
5022 else
5023 newscore = SCORE_SUBST;
5024 if ((newscore == 0
5025 || (sp->ts_fidx >= sp->ts_fidxtry
5026 && ((sp->ts_flags & TSF_DIDDEL) == 0
5027 || c != fword[sp->ts_delidx])))
5028 && TRY_DEEPER(su, stack, depth, newscore))
5029 {
5030 go_deeper(stack, depth, newscore);
5031#ifdef DEBUG_TRIEWALK
5032 if (newscore > 0)
5033 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
5034 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5035 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005037 sprintf(changename[depth], "%.*s-%s: accept %c",
5038 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5039 fword[sp->ts_fidx]);
5040#endif
5041 ++depth;
5042 sp = &stack[depth];
5043 ++sp->ts_fidx;
5044 tword[sp->ts_twordlen++] = c;
5045 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +00005046#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005047 if (newscore == SCORE_SUBST)
5048 sp->ts_isdiff = DIFF_YES;
5049 if (has_mbyte)
5050 {
5051 /* Multi-byte characters are a bit complicated to
5052 * handle: They differ when any of the bytes differ
5053 * and then their length may also differ. */
5054 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005055 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005056 /* First byte. */
5057 sp->ts_tcharidx = 0;
5058 sp->ts_tcharlen = MB_BYTE2LEN(c);
5059 sp->ts_fcharstart = sp->ts_fidx - 1;
5060 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00005061 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005062 }
5063 else if (sp->ts_isdiff == DIFF_INSERT)
5064 /* When inserting trail bytes don't advance in the
5065 * bad word. */
5066 --sp->ts_fidx;
5067 if (++sp->ts_tcharidx == sp->ts_tcharlen)
5068 {
5069 /* Last byte of character. */
5070 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00005071 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005072 /* Correct ts_fidx for the byte length of the
5073 * character (we didn't check that before). */
5074 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005075 + MB_PTR2LEN(
5076 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005077 /* For changing a composing character adjust
5078 * the score from SCORE_SUBST to
5079 * SCORE_SUBCOMP. */
5080 if (enc_utf8
5081 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005082 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00005083 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005084 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005085 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02005086 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005087 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005088 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00005089 SCORE_SUBST - SCORE_SUBCOMP;
5090
Bram Moolenaar4770d092006-01-12 23:22:24 +00005091 /* For a similar character adjust score from
5092 * SCORE_SUBST to SCORE_SIMILAR. */
5093 else if (!soundfold
5094 && slang->sl_has_map
5095 && similar_chars(slang,
5096 mb_ptr2char(tword
5097 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00005098 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00005099 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00005100 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005101 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00005102 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00005103 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005104 else if (sp->ts_isdiff == DIFF_INSERT
5105 && sp->ts_twordlen > sp->ts_tcharlen)
5106 {
5107 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
5108 c = mb_ptr2char(p);
5109 if (enc_utf8 && utf_iscomposing(c))
5110 {
5111 /* Inserting a composing char doesn't
5112 * count that much. */
5113 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5114 }
5115 else
5116 {
5117 /* If the previous character was the same,
5118 * thus doubling a character, give a bonus
5119 * to the score. Also for the soundfold
5120 * tree (might seem illogical but does
5121 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005122 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005123 if (c == mb_ptr2char(p))
5124 sp->ts_score -= SCORE_INS
5125 - SCORE_INSDUP;
5126 }
5127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128
Bram Moolenaar4770d092006-01-12 23:22:24 +00005129 /* Starting a new char, reset the length. */
5130 sp->ts_tcharlen = 0;
5131 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005132 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005133 else
5134#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00005135 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005136 /* If we found a similar char adjust the score.
5137 * We do this after calling go_deeper() because
5138 * it's slow. */
5139 if (newscore != 0
5140 && !soundfold
5141 && slang->sl_has_map
5142 && similar_chars(slang,
5143 c, fword[sp->ts_fidx - 1]))
5144 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005147 }
5148 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005149
Bram Moolenaar4770d092006-01-12 23:22:24 +00005150 case STATE_DEL:
5151#ifdef FEAT_MBYTE
5152 /* When past the first byte of a multi-byte char don't try
5153 * delete/insert/swap a character. */
5154 if (has_mbyte && sp->ts_tcharlen > 0)
5155 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005156 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005157 sp->ts_state = STATE_FINAL;
5158 break;
5159 }
5160#endif
5161 /*
5162 * Try skipping one character in the bad word (delete it).
5163 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005164 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005165 sp->ts_state = STATE_INS_PREP;
5166 sp->ts_curi = 1;
5167 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5168 /* Deleting a vowel at the start of a word counts less, see
5169 * soundalike_score(). */
5170 newscore = 2 * SCORE_DEL / 3;
5171 else
5172 newscore = SCORE_DEL;
5173 if (fword[sp->ts_fidx] != NUL
5174 && TRY_DEEPER(su, stack, depth, newscore))
5175 {
5176 go_deeper(stack, depth, newscore);
5177#ifdef DEBUG_TRIEWALK
5178 sprintf(changename[depth], "%.*s-%s: delete %c",
5179 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5180 fword[sp->ts_fidx]);
5181#endif
5182 ++depth;
5183
5184 /* Remember what character we deleted, so that we can avoid
5185 * inserting it again. */
5186 stack[depth].ts_flags |= TSF_DIDDEL;
5187 stack[depth].ts_delidx = sp->ts_fidx;
5188
5189 /* Advance over the character in fword[]. Give a bonus to the
5190 * score if the same character is following "nn" -> "n". It's
5191 * a bit illogical for soundfold tree but it does give better
5192 * results. */
5193#ifdef FEAT_MBYTE
5194 if (has_mbyte)
5195 {
5196 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005197 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005198 if (enc_utf8 && utf_iscomposing(c))
5199 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5200 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5201 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5202 }
5203 else
5204#endif
5205 {
5206 ++stack[depth].ts_fidx;
5207 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5208 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5209 }
5210 break;
5211 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005212 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005213
5214 case STATE_INS_PREP:
5215 if (sp->ts_flags & TSF_DIDDEL)
5216 {
5217 /* If we just deleted a byte then inserting won't make sense,
5218 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005219 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005220 sp->ts_state = STATE_SWAP;
5221 break;
5222 }
5223
5224 /* skip over NUL bytes */
5225 n = sp->ts_arridx;
5226 for (;;)
5227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (sp->ts_curi > byts[n])
5229 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005230 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005231 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005232 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005233 break;
5234 }
5235 if (byts[n + sp->ts_curi] != NUL)
5236 {
5237 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005238 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005239 sp->ts_state = STATE_INS;
5240 break;
5241 }
5242 ++sp->ts_curi;
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:
5249 /* Insert one byte. Repeat this for each possible byte at this
5250 * node. */
5251 n = sp->ts_arridx;
5252 if (sp->ts_curi > byts[n])
5253 {
5254 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005255 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005256 sp->ts_state = STATE_SWAP;
5257 break;
5258 }
5259
5260 /* Do one more byte at this node, but:
5261 * - Skip NUL bytes.
5262 * - Skip the byte if it's equal to the byte in the word,
5263 * accepting that byte is always better.
5264 */
5265 n += sp->ts_curi++;
5266 c = byts[n];
5267 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5268 /* Inserting a vowel at the start of a word counts less,
5269 * see soundalike_score(). */
5270 newscore = 2 * SCORE_INS / 3;
5271 else
5272 newscore = SCORE_INS;
5273 if (c != fword[sp->ts_fidx]
5274 && TRY_DEEPER(su, stack, depth, newscore))
5275 {
5276 go_deeper(stack, depth, newscore);
5277#ifdef DEBUG_TRIEWALK
5278 sprintf(changename[depth], "%.*s-%s: insert %c",
5279 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5280 c);
5281#endif
5282 ++depth;
5283 sp = &stack[depth];
5284 tword[sp->ts_twordlen++] = c;
5285 sp->ts_arridx = idxs[n];
5286#ifdef FEAT_MBYTE
5287 if (has_mbyte)
5288 {
5289 fl = MB_BYTE2LEN(c);
5290 if (fl > 1)
5291 {
5292 /* There are following bytes for the same character.
5293 * We must find all bytes before trying
5294 * delete/insert/swap/etc. */
5295 sp->ts_tcharlen = fl;
5296 sp->ts_tcharidx = 1;
5297 sp->ts_isdiff = DIFF_INSERT;
5298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 }
5300 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005301 fl = 1;
5302 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00005303#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005304 {
5305 /* If the previous character was the same, thus doubling a
5306 * character, give a bonus to the score. Also for
5307 * soundfold words (illogical but does give a better
5308 * score). */
5309 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005310 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005311 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005313 }
5314 break;
5315
5316 case STATE_SWAP:
5317 /*
5318 * Swap two bytes in the bad word: "12" -> "21".
5319 * We change "fword" here, it's changed back afterwards at
5320 * STATE_UNSWAP.
5321 */
5322 p = fword + sp->ts_fidx;
5323 c = *p;
5324 if (c == NUL)
5325 {
5326 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005327 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005328 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331
Bram Moolenaar4770d092006-01-12 23:22:24 +00005332 /* Don't swap if the first character is not a word character.
5333 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005335 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005336 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005337 sp->ts_state = STATE_REP_INI;
5338 break;
5339 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005340
Bram Moolenaar4770d092006-01-12 23:22:24 +00005341#ifdef FEAT_MBYTE
5342 if (has_mbyte)
5343 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005344 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005345 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005346 if (p[n] == NUL)
5347 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005348 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005349 c2 = c; /* don't swap non-word char */
5350 else
5351 c2 = mb_ptr2char(p + n);
5352 }
5353 else
5354#endif
5355 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005356 if (p[1] == NUL)
5357 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005358 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005359 c2 = c; /* don't swap non-word char */
5360 else
5361 c2 = p[1];
5362 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005363
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005364 /* When the second character is NUL we can't swap. */
5365 if (c2 == NUL)
5366 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005367 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005368 sp->ts_state = STATE_REP_INI;
5369 break;
5370 }
5371
Bram Moolenaar4770d092006-01-12 23:22:24 +00005372 /* When characters are identical, swap won't do anything.
5373 * Also get here if the second char is not a word character. */
5374 if (c == c2)
5375 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005376 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005377 sp->ts_state = STATE_SWAP3;
5378 break;
5379 }
5380 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5381 {
5382 go_deeper(stack, depth, SCORE_SWAP);
5383#ifdef DEBUG_TRIEWALK
5384 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5385 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5386 c, c2);
5387#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005388 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005389 sp->ts_state = STATE_UNSWAP;
5390 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005391#ifdef FEAT_MBYTE
5392 if (has_mbyte)
5393 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005394 fl = mb_char2len(c2);
5395 mch_memmove(p, p + n, fl);
5396 mb_char2bytes(c, p + fl);
5397 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005398 }
5399 else
5400#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005401 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005402 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005403 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005404 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005405 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005406 }
5407 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005408 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005409 /* If this swap doesn't work then SWAP3 won't either. */
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_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005412 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005413 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005414
Bram Moolenaar4770d092006-01-12 23:22:24 +00005415 case STATE_UNSWAP:
5416 /* Undo the STATE_SWAP swap: "21" -> "12". */
5417 p = fword + sp->ts_fidx;
5418#ifdef FEAT_MBYTE
5419 if (has_mbyte)
5420 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005421 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005422 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005423 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005424 mb_char2bytes(c, p);
5425 }
5426 else
5427#endif
5428 {
5429 c = *p;
5430 *p = p[1];
5431 p[1] = c;
5432 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005433 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005434
5435 case STATE_SWAP3:
5436 /* Swap two bytes, skipping one: "123" -> "321". We change
5437 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5438 p = fword + sp->ts_fidx;
5439#ifdef FEAT_MBYTE
5440 if (has_mbyte)
5441 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005442 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005443 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005444 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005445 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005446 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005447 c3 = c; /* don't swap non-word char */
5448 else
5449 c3 = mb_ptr2char(p + n + fl);
5450 }
5451 else
5452#endif
5453 {
5454 c = *p;
5455 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005456 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005457 c3 = c; /* don't swap non-word char */
5458 else
5459 c3 = p[2];
5460 }
5461
5462 /* When characters are identical: "121" then SWAP3 result is
5463 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5464 * same as SWAP on next char: "112". Thus skip all swapping.
5465 * Also skip when c3 is NUL.
5466 * Also get here when the third character is not a word character.
5467 * Second character may any char: "a.b" -> "b.a" */
5468 if (c == c3 || c3 == NUL)
5469 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005470 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005471 sp->ts_state = STATE_REP_INI;
5472 break;
5473 }
5474 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5475 {
5476 go_deeper(stack, depth, SCORE_SWAP3);
5477#ifdef DEBUG_TRIEWALK
5478 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5479 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5480 c, c3);
5481#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005482 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005483 sp->ts_state = STATE_UNSWAP3;
5484 ++depth;
5485#ifdef FEAT_MBYTE
5486 if (has_mbyte)
5487 {
5488 tl = mb_char2len(c3);
5489 mch_memmove(p, p + n + fl, tl);
5490 mb_char2bytes(c2, p + tl);
5491 mb_char2bytes(c, p + fl + tl);
5492 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5493 }
5494 else
5495#endif
5496 {
5497 p[0] = p[2];
5498 p[2] = c;
5499 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5500 }
5501 }
5502 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005503 {
5504 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005505 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005506 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005507 break;
5508
5509 case STATE_UNSWAP3:
5510 /* Undo STATE_SWAP3: "321" -> "123" */
5511 p = fword + sp->ts_fidx;
5512#ifdef FEAT_MBYTE
5513 if (has_mbyte)
5514 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005515 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005516 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005517 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005518 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005519 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005520 mch_memmove(p + fl + tl, p, n);
5521 mb_char2bytes(c, p);
5522 mb_char2bytes(c2, p + tl);
5523 p = p + tl;
5524 }
5525 else
5526#endif
5527 {
5528 c = *p;
5529 *p = p[2];
5530 p[2] = c;
5531 ++p;
5532 }
5533
Bram Moolenaar860cae12010-06-05 23:22:07 +02005534 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005535 {
5536 /* Middle char is not a word char, skip the rotate. First and
5537 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005538 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005539 sp->ts_state = STATE_REP_INI;
5540 break;
5541 }
5542
5543 /* Rotate three characters left: "123" -> "231". We change
5544 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5545 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5546 {
5547 go_deeper(stack, depth, SCORE_SWAP3);
5548#ifdef DEBUG_TRIEWALK
5549 p = fword + sp->ts_fidx;
5550 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5551 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5552 p[0], p[1], p[2]);
5553#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005554 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005555 sp->ts_state = STATE_UNROT3L;
5556 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005557 p = fword + sp->ts_fidx;
5558#ifdef FEAT_MBYTE
5559 if (has_mbyte)
5560 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005561 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005562 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005563 fl = MB_CPTR2LEN(p + n);
5564 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005565 mch_memmove(p, p + n, fl);
5566 mb_char2bytes(c, p + fl);
5567 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005568 }
5569 else
5570#endif
5571 {
5572 c = *p;
5573 *p = p[1];
5574 p[1] = p[2];
5575 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005576 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005577 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005578 }
5579 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005580 {
5581 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005582 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005583 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005584 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005585
Bram Moolenaar4770d092006-01-12 23:22:24 +00005586 case STATE_UNROT3L:
5587 /* Undo ROT3L: "231" -> "123" */
5588 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005589#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +00005590 if (has_mbyte)
5591 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005592 n = MB_PTR2LEN(p);
5593 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005594 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005595 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005596 mch_memmove(p + tl, p, n);
5597 mb_char2bytes(c, p);
5598 }
5599 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005600#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00005601 {
5602 c = p[2];
5603 p[2] = p[1];
5604 p[1] = *p;
5605 *p = c;
5606 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005607
Bram Moolenaar4770d092006-01-12 23:22:24 +00005608 /* Rotate three bytes right: "123" -> "312". We change "fword"
5609 * here, it's changed back afterwards at STATE_UNROT3R. */
5610 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5611 {
5612 go_deeper(stack, depth, SCORE_SWAP3);
5613#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005614 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005615 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5616 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5617 p[0], p[1], p[2]);
5618#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005619 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005620 sp->ts_state = STATE_UNROT3R;
5621 ++depth;
5622 p = fword + sp->ts_fidx;
5623#ifdef FEAT_MBYTE
5624 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005625 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005626 n = MB_CPTR2LEN(p);
5627 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005628 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005629 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005630 mch_memmove(p + tl, p, n);
5631 mb_char2bytes(c, p);
5632 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005633 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005634 else
5635#endif
5636 {
5637 c = p[2];
5638 p[2] = p[1];
5639 p[1] = *p;
5640 *p = c;
5641 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5642 }
5643 }
5644 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005645 {
5646 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005647 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005648 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005649 break;
5650
5651 case STATE_UNROT3R:
5652 /* Undo ROT3R: "312" -> "123" */
5653 p = fword + sp->ts_fidx;
5654#ifdef FEAT_MBYTE
5655 if (has_mbyte)
5656 {
5657 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005658 tl = MB_PTR2LEN(p);
5659 n = MB_PTR2LEN(p + tl);
5660 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005661 mch_memmove(p, p + tl, n);
5662 mb_char2bytes(c, p + n);
5663 }
5664 else
5665#endif
5666 {
5667 c = *p;
5668 *p = p[1];
5669 p[1] = p[2];
5670 p[2] = c;
5671 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005672 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005673
5674 case STATE_REP_INI:
5675 /* Check if matching with REP items from the .aff file would work.
5676 * Quickly skip if:
5677 * - there are no REP items and we are not in the soundfold trie
5678 * - the score is going to be too high anyway
5679 * - already applied a REP item or swapped here */
5680 if ((lp->lp_replang == NULL && !soundfold)
5681 || sp->ts_score + SCORE_REP >= su->su_maxscore
5682 || sp->ts_fidx < sp->ts_fidxtry)
5683 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005684 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005685 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005686 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005688
Bram Moolenaar4770d092006-01-12 23:22:24 +00005689 /* Use the first byte to quickly find the first entry that may
5690 * match. If the index is -1 there is none. */
5691 if (soundfold)
5692 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5693 else
5694 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005695
Bram Moolenaar4770d092006-01-12 23:22:24 +00005696 if (sp->ts_curi < 0)
5697 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005698 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005699 sp->ts_state = STATE_FINAL;
5700 break;
5701 }
5702
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005703 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005704 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005705 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005706
5707 case STATE_REP:
5708 /* Try matching with REP items from the .aff file. For each match
5709 * replace the characters and check if the resulting word is
5710 * valid. */
5711 p = fword + sp->ts_fidx;
5712
5713 if (soundfold)
5714 gap = &slang->sl_repsal;
5715 else
5716 gap = &lp->lp_replang->sl_rep;
5717 while (sp->ts_curi < gap->ga_len)
5718 {
5719 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5720 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005721 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005722 /* past possible matching entries */
5723 sp->ts_curi = gap->ga_len;
5724 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005725 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005726 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5727 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5728 {
5729 go_deeper(stack, depth, SCORE_REP);
5730#ifdef DEBUG_TRIEWALK
5731 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5732 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5733 ftp->ft_from, ftp->ft_to);
5734#endif
5735 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005736 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005737 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005738
Bram Moolenaar4770d092006-01-12 23:22:24 +00005739 /* Change the "from" to the "to" string. */
5740 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005741 fl = (int)STRLEN(ftp->ft_from);
5742 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005743 if (fl != tl)
5744 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005745 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005746 repextra += tl - fl;
5747 }
5748 mch_memmove(p, ftp->ft_to, tl);
5749 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
5750#ifdef FEAT_MBYTE
5751 stack[depth].ts_tcharlen = 0;
5752#endif
5753 break;
5754 }
5755 }
5756
5757 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005758 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005759 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005760 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005761 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005762 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005763
5764 break;
5765
5766 case STATE_REP_UNDO:
5767 /* Undo a REP replacement and continue with the next one. */
5768 if (soundfold)
5769 gap = &slang->sl_repsal;
5770 else
5771 gap = &lp->lp_replang->sl_rep;
5772 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005773 fl = (int)STRLEN(ftp->ft_from);
5774 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005775 p = fword + sp->ts_fidx;
5776 if (fl != tl)
5777 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005778 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005779 repextra -= tl - fl;
5780 }
5781 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005782 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005783 sp->ts_state = STATE_REP;
5784 break;
5785
5786 default:
5787 /* Did all possible states at this level, go up one level. */
5788 --depth;
5789
5790 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5791 {
5792 /* Continue in or go back to the prefix tree. */
5793 byts = pbyts;
5794 idxs = pidxs;
5795 }
5796
5797 /* Don't check for CTRL-C too often, it takes time. */
5798 if (--breakcheckcount == 0)
5799 {
5800 ui_breakcheck();
5801 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005803 }
5804 }
5805}
5806
Bram Moolenaar4770d092006-01-12 23:22:24 +00005807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005808/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005809 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005810 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005811 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005812go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005813{
Bram Moolenaarea424162005-06-16 21:51:00 +00005814 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005815 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005816 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005817 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005818 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005819}
5820
Bram Moolenaar53805d12005-08-01 07:08:33 +00005821#ifdef FEAT_MBYTE
5822/*
5823 * Case-folding may change the number of bytes: Count nr of chars in
5824 * fword[flen] and return the byte length of that many chars in "word".
5825 */
5826 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005827nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005828{
5829 char_u *p;
5830 int i = 0;
5831
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005832 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005833 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005834 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005835 --i;
5836 return (int)(p - word);
5837}
5838#endif
5839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005840/*
5841 * "fword" is a good word with case folded. Find the matching keep-case
5842 * words and put it in "kword".
5843 * Theoretically there could be several keep-case words that result in the
5844 * same case-folded word, but we only find one...
5845 */
5846 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005847find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005848{
5849 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5850 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005851 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005852
5853 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005854 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005855 int round[MAXWLEN];
5856 int fwordidx[MAXWLEN];
5857 int uwordidx[MAXWLEN];
5858 int kwordlen[MAXWLEN];
5859
5860 int flen, ulen;
5861 int l;
5862 int len;
5863 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005864 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005865 char_u *p;
5866 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005867 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005868
5869 if (byts == NULL)
5870 {
5871 /* array is empty: "cannot happen" */
5872 *kword = NUL;
5873 return;
5874 }
5875
5876 /* Make an all-cap version of "fword". */
5877 allcap_copy(fword, uword);
5878
5879 /*
5880 * Each character needs to be tried both case-folded and upper-case.
5881 * All this gets very complicated if we keep in mind that changing case
5882 * may change the byte length of a multi-byte character...
5883 */
5884 depth = 0;
5885 arridx[0] = 0;
5886 round[0] = 0;
5887 fwordidx[0] = 0;
5888 uwordidx[0] = 0;
5889 kwordlen[0] = 0;
5890 while (depth >= 0)
5891 {
5892 if (fword[fwordidx[depth]] == NUL)
5893 {
5894 /* We are at the end of "fword". If the tree allows a word to end
5895 * here we have found a match. */
5896 if (byts[arridx[depth] + 1] == 0)
5897 {
5898 kword[kwordlen[depth]] = NUL;
5899 return;
5900 }
5901
5902 /* kword is getting too long, continue one level up */
5903 --depth;
5904 }
5905 else if (++round[depth] > 2)
5906 {
5907 /* tried both fold-case and upper-case character, continue one
5908 * level up */
5909 --depth;
5910 }
5911 else
5912 {
5913 /*
5914 * round[depth] == 1: Try using the folded-case character.
5915 * round[depth] == 2: Try using the upper-case character.
5916 */
5917#ifdef FEAT_MBYTE
5918 if (has_mbyte)
5919 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005920 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5921 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005922 }
5923 else
5924#endif
5925 ulen = flen = 1;
5926 if (round[depth] == 1)
5927 {
5928 p = fword + fwordidx[depth];
5929 l = flen;
5930 }
5931 else
5932 {
5933 p = uword + uwordidx[depth];
5934 l = ulen;
5935 }
5936
5937 for (tryidx = arridx[depth]; l > 0; --l)
5938 {
5939 /* Perform a binary search in the list of accepted bytes. */
5940 len = byts[tryidx++];
5941 c = *p++;
5942 lo = tryidx;
5943 hi = tryidx + len - 1;
5944 while (lo < hi)
5945 {
5946 m = (lo + hi) / 2;
5947 if (byts[m] > c)
5948 hi = m - 1;
5949 else if (byts[m] < c)
5950 lo = m + 1;
5951 else
5952 {
5953 lo = hi = m;
5954 break;
5955 }
5956 }
5957
5958 /* Stop if there is no matching byte. */
5959 if (hi < lo || byts[lo] != c)
5960 break;
5961
5962 /* Continue at the child (if there is one). */
5963 tryidx = idxs[lo];
5964 }
5965
5966 if (l == 0)
5967 {
5968 /*
5969 * Found the matching char. Copy it to "kword" and go a
5970 * level deeper.
5971 */
5972 if (round[depth] == 1)
5973 {
5974 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5975 flen);
5976 kwordlen[depth + 1] = kwordlen[depth] + flen;
5977 }
5978 else
5979 {
5980 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5981 ulen);
5982 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5983 }
5984 fwordidx[depth + 1] = fwordidx[depth] + flen;
5985 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5986
5987 ++depth;
5988 arridx[depth] = tryidx;
5989 round[depth] = 0;
5990 }
5991 }
5992 }
5993
5994 /* Didn't find it: "cannot happen". */
5995 *kword = NUL;
5996}
5997
5998/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005999 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
6000 * su->su_sga.
6001 */
6002 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006003score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006004{
6005 langp_T *lp;
6006 char_u badsound[MAXWLEN];
6007 int i;
6008 suggest_T *stp;
6009 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006010 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006011 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006012
6013 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
6014 return;
6015
6016 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006017 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006018 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006019 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006020 if (lp->lp_slang->sl_sal.ga_len > 0)
6021 {
6022 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006023 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006024
6025 for (i = 0; i < su->su_ga.ga_len; ++i)
6026 {
6027 stp = &SUG(su->su_ga, i);
6028
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006029 /* Case-fold the suggested word, sound-fold it and compute the
6030 * sound-a-like score. */
6031 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006032 if (score < SCORE_MAXMAX)
6033 {
6034 /* Add the suggestion. */
6035 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
6036 sstp->st_word = vim_strsave(stp->st_word);
6037 if (sstp->st_word != NULL)
6038 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006039 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006040 sstp->st_score = score;
6041 sstp->st_altscore = 0;
6042 sstp->st_orglen = stp->st_orglen;
6043 ++su->su_sga.ga_len;
6044 }
6045 }
6046 }
6047 break;
6048 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006049 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006050}
6051
6052/*
6053 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006054 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006055 */
6056 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006057score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006058{
6059 int i;
6060 int j;
6061 garray_T ga;
6062 garray_T *gap;
6063 langp_T *lp;
6064 suggest_T *stp;
6065 char_u *p;
6066 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006067 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006068 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006069 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006070
6071 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006072 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006073 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006074 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006075 if (lp->lp_slang->sl_sal.ga_len > 0)
6076 {
6077 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006078 slang = lp->lp_slang;
6079 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006080
6081 for (i = 0; i < su->su_ga.ga_len; ++i)
6082 {
6083 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006084 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006085 if (stp->st_altscore == SCORE_MAXMAX)
6086 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
6087 else
6088 stp->st_score = (stp->st_score * 3
6089 + stp->st_altscore) / 4;
6090 stp->st_salscore = FALSE;
6091 }
6092 break;
6093 }
6094 }
6095
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006096 if (slang == NULL) /* Using "double" without sound folding. */
6097 {
6098 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
6099 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006100 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006101 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006102
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006103 /* Add the alternate score to su_sga. */
6104 for (i = 0; i < su->su_sga.ga_len; ++i)
6105 {
6106 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006107 stp->st_altscore = spell_edit_score(slang,
6108 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006109 if (stp->st_score == SCORE_MAXMAX)
6110 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
6111 else
6112 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
6113 stp->st_salscore = TRUE;
6114 }
6115
Bram Moolenaar4770d092006-01-12 23:22:24 +00006116 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
6117 * for both lists. */
6118 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006119 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006120 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006121 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
6122
6123 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
6124 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
6125 return;
6126
6127 stp = &SUG(ga, 0);
6128 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
6129 {
6130 /* round 1: get a suggestion from su_ga
6131 * round 2: get a suggestion from su_sga */
6132 for (round = 1; round <= 2; ++round)
6133 {
6134 gap = round == 1 ? &su->su_ga : &su->su_sga;
6135 if (i < gap->ga_len)
6136 {
6137 /* Don't add a word if it's already there. */
6138 p = SUG(*gap, i).st_word;
6139 for (j = 0; j < ga.ga_len; ++j)
6140 if (STRCMP(stp[j].st_word, p) == 0)
6141 break;
6142 if (j == ga.ga_len)
6143 stp[ga.ga_len++] = SUG(*gap, i);
6144 else
6145 vim_free(p);
6146 }
6147 }
6148 }
6149
6150 ga_clear(&su->su_ga);
6151 ga_clear(&su->su_sga);
6152
6153 /* Truncate the list to the number of suggestions that will be displayed. */
6154 if (ga.ga_len > su->su_maxcount)
6155 {
6156 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6157 vim_free(stp[i].st_word);
6158 ga.ga_len = su->su_maxcount;
6159 }
6160
6161 su->su_ga = ga;
6162}
6163
6164/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006165 * For the goodword in "stp" compute the soundalike score compared to the
6166 * badword.
6167 */
6168 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006169stp_sal_score(
6170 suggest_T *stp,
6171 suginfo_T *su,
6172 slang_T *slang,
6173 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006174{
6175 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006176 char_u *pbad;
6177 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006178 char_u badsound2[MAXWLEN];
6179 char_u fword[MAXWLEN];
6180 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006181 char_u goodword[MAXWLEN];
6182 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006183
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006184 lendiff = (int)(su->su_badlen - stp->st_orglen);
6185 if (lendiff >= 0)
6186 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006187 else
6188 {
6189 /* soundfold the bad word with more characters following */
6190 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6191
6192 /* When joining two words the sound often changes a lot. E.g., "t he"
6193 * sounds like "t h" while "the" sounds like "@". Avoid that by
6194 * removing the space. Don't do it when the good word also contains a
6195 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006196 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006197 && *skiptowhite(stp->st_word) == NUL)
6198 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006199 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006200
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006201 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006202 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006203 }
6204
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006205 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006206 {
6207 /* Add part of the bad word to the good word, so that we soundfold
6208 * what replaces the bad word. */
6209 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006210 vim_strncpy(goodword + stp->st_wordlen,
6211 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006212 pgood = goodword;
6213 }
6214 else
6215 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006216
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006217 /* Sound-fold the word and compute the score for the difference. */
6218 spell_soundfold(slang, pgood, FALSE, goodsound);
6219
6220 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006221}
6222
Bram Moolenaar4770d092006-01-12 23:22:24 +00006223/* structure used to store soundfolded words that add_sound_suggest() has
6224 * handled already. */
6225typedef struct
6226{
6227 short sft_score; /* lowest score used */
6228 char_u sft_word[1]; /* soundfolded word, actually longer */
6229} sftword_T;
6230
6231static sftword_T dumsft;
6232#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6233#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6234
6235/*
6236 * Prepare for calling suggest_try_soundalike().
6237 */
6238 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006239suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006240{
6241 langp_T *lp;
6242 int lpi;
6243 slang_T *slang;
6244
6245 /* Do this for all languages that support sound folding and for which a
6246 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006247 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006248 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006249 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006250 slang = lp->lp_slang;
6251 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6252 /* prepare the hashtable used by add_sound_suggest() */
6253 hash_init(&slang->sl_sounddone);
6254 }
6255}
6256
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006257/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006258 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006259 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006260 */
6261 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006262suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006263{
6264 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006265 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006266 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006267 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006268
Bram Moolenaar4770d092006-01-12 23:22:24 +00006269 /* Do this for all languages that support sound folding and for which a
6270 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006271 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006272 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006273 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006274 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006275 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006276 {
6277 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006278 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006279
Bram Moolenaar4770d092006-01-12 23:22:24 +00006280 /* try all kinds of inserts/deletes/swaps/etc. */
6281 /* TODO: also soundfold the next words, so that we can try joining
6282 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006283#ifdef SUGGEST_PROFILE
6284 prof_init();
6285#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006286 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006287#ifdef SUGGEST_PROFILE
6288 prof_report("soundalike");
6289#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006290 }
6291 }
6292}
6293
6294/*
6295 * Finish up after calling suggest_try_soundalike().
6296 */
6297 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006298suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006299{
6300 langp_T *lp;
6301 int lpi;
6302 slang_T *slang;
6303 int todo;
6304 hashitem_T *hi;
6305
6306 /* Do this for all languages that support sound folding and for which a
6307 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006308 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006309 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006310 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006311 slang = lp->lp_slang;
6312 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6313 {
6314 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006315 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006316 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6317 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006318 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006319 vim_free(HI2SFT(hi));
6320 --todo;
6321 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006322
6323 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006324 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006325 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006326 }
6327 }
6328}
6329
6330/*
6331 * A match with a soundfolded word is found. Add the good word(s) that
6332 * produce this soundfolded word.
6333 */
6334 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006335add_sound_suggest(
6336 suginfo_T *su,
6337 char_u *goodword,
6338 int score, /* soundfold score */
6339 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006340{
6341 slang_T *slang = lp->lp_slang; /* language for sound folding */
6342 int sfwordnr;
6343 char_u *nrline;
6344 int orgnr;
6345 char_u theword[MAXWLEN];
6346 int i;
6347 int wlen;
6348 char_u *byts;
6349 idx_T *idxs;
6350 int n;
6351 int wordcount;
6352 int wc;
6353 int goodscore;
6354 hash_T hash;
6355 hashitem_T *hi;
6356 sftword_T *sft;
6357 int bc, gc;
6358 int limit;
6359
6360 /*
6361 * It's very well possible that the same soundfold word is found several
6362 * times with different scores. Since the following is quite slow only do
6363 * the words that have a better score than before. Use a hashtable to
6364 * remember the words that have been done.
6365 */
6366 hash = hash_hash(goodword);
6367 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6368 if (HASHITEM_EMPTY(hi))
6369 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006370 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6371 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006372 if (sft != NULL)
6373 {
6374 sft->sft_score = score;
6375 STRCPY(sft->sft_word, goodword);
6376 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6377 }
6378 }
6379 else
6380 {
6381 sft = HI2SFT(hi);
6382 if (score >= sft->sft_score)
6383 return;
6384 sft->sft_score = score;
6385 }
6386
6387 /*
6388 * Find the word nr in the soundfold tree.
6389 */
6390 sfwordnr = soundfold_find(slang, goodword);
6391 if (sfwordnr < 0)
6392 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006393 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006394 return;
6395 }
6396
6397 /*
6398 * go over the list of good words that produce this soundfold word
6399 */
6400 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6401 orgnr = 0;
6402 while (*nrline != NUL)
6403 {
6404 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6405 * previous wordnr. */
6406 orgnr += bytes2offset(&nrline);
6407
6408 byts = slang->sl_fbyts;
6409 idxs = slang->sl_fidxs;
6410
6411 /* Lookup the word "orgnr" one of the two tries. */
6412 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006413 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006414 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006415 {
6416 i = 1;
6417 if (wordcount == orgnr && byts[n + 1] == NUL)
6418 break; /* found end of word */
6419
6420 if (byts[n + 1] == NUL)
6421 ++wordcount;
6422
6423 /* skip over the NUL bytes */
6424 for ( ; byts[n + i] == NUL; ++i)
6425 if (i > byts[n]) /* safety check */
6426 {
6427 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006428 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006429 goto badword;
6430 }
6431
6432 /* One of the siblings must have the word. */
6433 for ( ; i < byts[n]; ++i)
6434 {
6435 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6436 if (wordcount + wc > orgnr)
6437 break;
6438 wordcount += wc;
6439 }
6440
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006441 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006442 n = idxs[n + i];
6443 }
6444badword:
6445 theword[wlen] = NUL;
6446
6447 /* Go over the possible flags and regions. */
6448 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6449 {
6450 char_u cword[MAXWLEN];
6451 char_u *p;
6452 int flags = (int)idxs[n + i];
6453
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006454 /* Skip words with the NOSUGGEST flag */
6455 if (flags & WF_NOSUGGEST)
6456 continue;
6457
Bram Moolenaar4770d092006-01-12 23:22:24 +00006458 if (flags & WF_KEEPCAP)
6459 {
6460 /* Must find the word in the keep-case tree. */
6461 find_keepcap_word(slang, theword, cword);
6462 p = cword;
6463 }
6464 else
6465 {
6466 flags |= su->su_badflags;
6467 if ((flags & WF_CAPMASK) != 0)
6468 {
6469 /* Need to fix case according to "flags". */
6470 make_case_word(theword, cword, flags);
6471 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006472 }
6473 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006474 p = theword;
6475 }
6476
6477 /* Add the suggestion. */
6478 if (sps_flags & SPS_DOUBLE)
6479 {
6480 /* Add the suggestion if the score isn't too bad. */
6481 if (score <= su->su_maxscore)
6482 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6483 score, 0, FALSE, slang, FALSE);
6484 }
6485 else
6486 {
6487 /* Add a penalty for words in another region. */
6488 if ((flags & WF_REGION)
6489 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6490 goodscore = SCORE_REGION;
6491 else
6492 goodscore = 0;
6493
6494 /* Add a small penalty for changing the first letter from
6495 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006496 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006497 * letter is the same, that has already been counted. */
6498 gc = PTR2CHAR(p);
6499 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006500 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006501 bc = PTR2CHAR(su->su_badword);
6502 if (!SPELL_ISUPPER(bc)
6503 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6504 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006505 }
6506
Bram Moolenaar4770d092006-01-12 23:22:24 +00006507 /* Compute the score for the good word. This only does letter
6508 * insert/delete/swap/replace. REP items are not considered,
6509 * which may make the score a bit higher.
6510 * Use a limit for the score to make it work faster. Use
6511 * MAXSCORE(), because RESCORE() will change the score.
6512 * If the limit is very high then the iterative method is
6513 * inefficient, using an array is quicker. */
6514 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6515 if (limit > SCORE_LIMITMAX)
6516 goodscore += spell_edit_score(slang, su->su_badword, p);
6517 else
6518 goodscore += spell_edit_score_limit(slang, su->su_badword,
6519 p, limit);
6520
6521 /* When going over the limit don't bother to do the rest. */
6522 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006523 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006524 /* Give a bonus to words seen before. */
6525 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006526
Bram Moolenaar4770d092006-01-12 23:22:24 +00006527 /* Add the suggestion if the score isn't too bad. */
6528 goodscore = RESCORE(goodscore, score);
6529 if (goodscore <= su->su_sfmaxscore)
6530 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6531 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006533 }
6534 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006535 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006536 }
6537}
6538
6539/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006540 * Find word "word" in fold-case tree for "slang" and return the word number.
6541 */
6542 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006543soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006544{
6545 idx_T arridx = 0;
6546 int len;
6547 int wlen = 0;
6548 int c;
6549 char_u *ptr = word;
6550 char_u *byts;
6551 idx_T *idxs;
6552 int wordnr = 0;
6553
6554 byts = slang->sl_sbyts;
6555 idxs = slang->sl_sidxs;
6556
6557 for (;;)
6558 {
6559 /* First byte is the number of possible bytes. */
6560 len = byts[arridx++];
6561
6562 /* If the first possible byte is a zero the word could end here.
6563 * If the word ends we found the word. If not skip the NUL bytes. */
6564 c = ptr[wlen];
6565 if (byts[arridx] == NUL)
6566 {
6567 if (c == NUL)
6568 break;
6569
6570 /* Skip over the zeros, there can be several. */
6571 while (len > 0 && byts[arridx] == NUL)
6572 {
6573 ++arridx;
6574 --len;
6575 }
6576 if (len == 0)
6577 return -1; /* no children, word should have ended here */
6578 ++wordnr;
6579 }
6580
6581 /* If the word ends we didn't find it. */
6582 if (c == NUL)
6583 return -1;
6584
6585 /* Perform a binary search in the list of accepted bytes. */
6586 if (c == TAB) /* <Tab> is handled like <Space> */
6587 c = ' ';
6588 while (byts[arridx] < c)
6589 {
6590 /* The word count is in the first idxs[] entry of the child. */
6591 wordnr += idxs[idxs[arridx]];
6592 ++arridx;
6593 if (--len == 0) /* end of the bytes, didn't find it */
6594 return -1;
6595 }
6596 if (byts[arridx] != c) /* didn't find the byte */
6597 return -1;
6598
6599 /* Continue at the child (if there is one). */
6600 arridx = idxs[arridx];
6601 ++wlen;
6602
6603 /* One space in the good word may stand for several spaces in the
6604 * checked word. */
6605 if (c == ' ')
6606 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6607 ++wlen;
6608 }
6609
6610 return wordnr;
6611}
6612
6613/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006614 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006615 */
6616 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006617make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006618{
6619 if (flags & WF_ALLCAP)
6620 /* Make it all upper-case */
6621 allcap_copy(fword, cword);
6622 else if (flags & WF_ONECAP)
6623 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006624 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006625 else
6626 /* Use goodword as-is. */
6627 STRCPY(cword, fword);
6628}
6629
Bram Moolenaarea424162005-06-16 21:51:00 +00006630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006631/*
6632 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6633 * lines in the .aff file.
6634 */
6635 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006636similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006637{
Bram Moolenaarea424162005-06-16 21:51:00 +00006638 int m1, m2;
6639#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006640 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006641 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006642
Bram Moolenaarea424162005-06-16 21:51:00 +00006643 if (c1 >= 256)
6644 {
6645 buf[mb_char2bytes(c1, buf)] = 0;
6646 hi = hash_find(&slang->sl_map_hash, buf);
6647 if (HASHITEM_EMPTY(hi))
6648 m1 = 0;
6649 else
6650 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6651 }
6652 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006653#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00006654 m1 = slang->sl_map_array[c1];
6655 if (m1 == 0)
6656 return FALSE;
6657
6658
6659#ifdef FEAT_MBYTE
6660 if (c2 >= 256)
6661 {
6662 buf[mb_char2bytes(c2, buf)] = 0;
6663 hi = hash_find(&slang->sl_map_hash, buf);
6664 if (HASHITEM_EMPTY(hi))
6665 m2 = 0;
6666 else
6667 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6668 }
6669 else
6670#endif
6671 m2 = slang->sl_map_array[c2];
6672
6673 return m1 == m2;
6674}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006675
6676/*
6677 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006678 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006679 */
6680 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006681add_suggestion(
6682 suginfo_T *su,
6683 garray_T *gap, /* either su_ga or su_sga */
6684 char_u *goodword,
6685 int badlenarg, /* len of bad word replaced with "goodword" */
6686 int score,
6687 int altscore,
6688 int had_bonus, /* value for st_had_bonus */
6689 slang_T *slang, /* language for sound folding */
6690 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006691 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006692{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006693 int goodlen; /* len of goodword changed */
6694 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006695 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006696 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006697 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006698 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006699
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006700 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6701 * "thee the" is added next to changing the first "the" the "thee". */
6702 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006703 pbad = su->su_badptr + badlenarg;
6704 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006705 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006706 goodlen = (int)(pgood - goodword);
6707 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006708 if (goodlen <= 0 || badlen <= 0)
6709 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006710 MB_PTR_BACK(goodword, pgood);
6711 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006712#ifdef FEAT_MBYTE
6713 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006714 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006715 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6716 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006717 }
6718 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006719#endif
6720 if (*pgood != *pbad)
6721 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006722 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006723
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006724 if (badlen == 0 && goodlen == 0)
6725 /* goodword doesn't change anything; may happen for "the the" changing
6726 * the first "the" to itself. */
6727 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006728
Bram Moolenaar89d40322006-08-29 15:30:07 +00006729 if (gap->ga_len == 0)
6730 i = -1;
6731 else
6732 {
6733 /* Check if the word is already there. Also check the length that is
6734 * being replaced "thes," -> "these" is a different suggestion from
6735 * "thes" -> "these". */
6736 stp = &SUG(*gap, 0);
6737 for (i = gap->ga_len; --i >= 0; ++stp)
6738 if (stp->st_wordlen == goodlen
6739 && stp->st_orglen == badlen
6740 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006741 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006742 /*
6743 * Found it. Remember the word with the lowest score.
6744 */
6745 if (stp->st_slang == NULL)
6746 stp->st_slang = slang;
6747
6748 new_sug.st_score = score;
6749 new_sug.st_altscore = altscore;
6750 new_sug.st_had_bonus = had_bonus;
6751
6752 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006753 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006754 /* Only one of the two had the soundalike score computed.
6755 * Need to do that for the other one now, otherwise the
6756 * scores can't be compared. This happens because
6757 * suggest_try_change() doesn't compute the soundalike
6758 * word to keep it fast, while some special methods set
6759 * the soundalike score to zero. */
6760 if (had_bonus)
6761 rescore_one(su, stp);
6762 else
6763 {
6764 new_sug.st_word = stp->st_word;
6765 new_sug.st_wordlen = stp->st_wordlen;
6766 new_sug.st_slang = stp->st_slang;
6767 new_sug.st_orglen = badlen;
6768 rescore_one(su, &new_sug);
6769 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006771
Bram Moolenaar89d40322006-08-29 15:30:07 +00006772 if (stp->st_score > new_sug.st_score)
6773 {
6774 stp->st_score = new_sug.st_score;
6775 stp->st_altscore = new_sug.st_altscore;
6776 stp->st_had_bonus = new_sug.st_had_bonus;
6777 }
6778 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006779 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006781
Bram Moolenaar4770d092006-01-12 23:22:24 +00006782 if (i < 0 && ga_grow(gap, 1) == OK)
6783 {
6784 /* Add a suggestion. */
6785 stp = &SUG(*gap, gap->ga_len);
6786 stp->st_word = vim_strnsave(goodword, goodlen);
6787 if (stp->st_word != NULL)
6788 {
6789 stp->st_wordlen = goodlen;
6790 stp->st_score = score;
6791 stp->st_altscore = altscore;
6792 stp->st_had_bonus = had_bonus;
6793 stp->st_orglen = badlen;
6794 stp->st_slang = slang;
6795 ++gap->ga_len;
6796
6797 /* If we have too many suggestions now, sort the list and keep
6798 * the best suggestions. */
6799 if (gap->ga_len > SUG_MAX_COUNT(su))
6800 {
6801 if (maxsf)
6802 su->su_sfmaxscore = cleanup_suggestions(gap,
6803 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6804 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006805 su->su_maxscore = cleanup_suggestions(gap,
6806 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006807 }
6808 }
6809 }
6810}
6811
6812/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006813 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6814 * for split words, such as "the the". Remove these from the list here.
6815 */
6816 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006817check_suggestions(
6818 suginfo_T *su,
6819 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006820{
6821 suggest_T *stp;
6822 int i;
6823 char_u longword[MAXWLEN + 1];
6824 int len;
6825 hlf_T attr;
6826
6827 stp = &SUG(*gap, 0);
6828 for (i = gap->ga_len - 1; i >= 0; --i)
6829 {
6830 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006831 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006832 len = stp[i].st_wordlen;
6833 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6834 MAXWLEN - len);
6835 attr = HLF_COUNT;
6836 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6837 if (attr != HLF_COUNT)
6838 {
6839 /* Remove this entry. */
6840 vim_free(stp[i].st_word);
6841 --gap->ga_len;
6842 if (i < gap->ga_len)
6843 mch_memmove(stp + i, stp + i + 1,
6844 sizeof(suggest_T) * (gap->ga_len - i));
6845 }
6846 }
6847}
6848
6849
6850/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006851 * Add a word to be banned.
6852 */
6853 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006854add_banned(
6855 suginfo_T *su,
6856 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006857{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006858 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006859 hash_T hash;
6860 hashitem_T *hi;
6861
Bram Moolenaar4770d092006-01-12 23:22:24 +00006862 hash = hash_hash(word);
6863 hi = hash_lookup(&su->su_banned, word, hash);
6864 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006865 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006866 s = vim_strsave(word);
6867 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006868 hash_add_item(&su->su_banned, hi, s, hash);
6869 }
6870}
6871
6872/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006873 * Recompute the score for all suggestions if sound-folding is possible. This
6874 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006875 */
6876 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006877rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006878{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006879 int i;
6880
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006881 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006882 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006883 rescore_one(su, &SUG(su->su_ga, i));
6884}
6885
6886/*
6887 * Recompute the score for one suggestion if sound-folding is possible.
6888 */
6889 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006890rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006891{
6892 slang_T *slang = stp->st_slang;
6893 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006894 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006895
6896 /* Only rescore suggestions that have no sal score yet and do have a
6897 * language. */
6898 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6899 {
6900 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006901 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006902 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006903 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006904 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006905 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006906 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006907
6908 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006909 if (stp->st_altscore == SCORE_MAXMAX)
6910 stp->st_altscore = SCORE_BIG;
6911 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6912 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006913 }
6914}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006916static int
6917#ifdef __BORLANDC__
6918_RTLENTRYF
6919#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006920sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006921
6922/*
6923 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006924 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006925 */
6926 static int
6927#ifdef __BORLANDC__
6928_RTLENTRYF
6929#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006930sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006931{
6932 suggest_T *p1 = (suggest_T *)s1;
6933 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006934 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006935
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006936 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006937 {
6938 n = p1->st_altscore - p2->st_altscore;
6939 if (n == 0)
6940 n = STRICMP(p1->st_word, p2->st_word);
6941 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006942 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006943}
6944
6945/*
6946 * Cleanup the suggestions:
6947 * - Sort on score.
6948 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006949 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006950 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006951 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006952cleanup_suggestions(
6953 garray_T *gap,
6954 int maxscore,
6955 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006956{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006957 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006958 int i;
6959
6960 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006961 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006962
6963 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006964 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006966 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006967 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006968 gap->ga_len = keep;
6969 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006970 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006971 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006972}
6973
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006974#if defined(FEAT_EVAL) || defined(PROTO)
6975/*
6976 * Soundfold a string, for soundfold().
6977 * Result is in allocated memory, NULL for an error.
6978 */
6979 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006980eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006981{
6982 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006983 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006984 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006985
Bram Moolenaar860cae12010-06-05 23:22:07 +02006986 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006987 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006988 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006989 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006990 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006991 if (lp->lp_slang->sl_sal.ga_len > 0)
6992 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006993 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006994 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006995 return vim_strsave(sound);
6996 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006997 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006998
6999 /* No language with sound folding, return word as-is. */
7000 return vim_strsave(word);
7001}
7002#endif
7003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007004/*
7005 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00007006 *
7007 * There are many ways to turn a word into a sound-a-like representation. The
7008 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
7009 * swedish name matching - survey and test of different algorithms" by Klas
7010 * Erikson.
7011 *
7012 * We support two methods:
7013 * 1. SOFOFROM/SOFOTO do a simple character mapping.
7014 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007015 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02007016 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007017spell_soundfold(
7018 slang_T *slang,
7019 char_u *inword,
7020 int folded, /* "inword" is already case-folded */
7021 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007022{
7023 char_u fword[MAXWLEN];
7024 char_u *word;
7025
7026 if (slang->sl_sofo)
7027 /* SOFOFROM and SOFOTO used */
7028 spell_soundfold_sofo(slang, inword, res);
7029 else
7030 {
7031 /* SAL items used. Requires the word to be case-folded. */
7032 if (folded)
7033 word = inword;
7034 else
7035 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007036 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007037 word = fword;
7038 }
7039
7040#ifdef FEAT_MBYTE
7041 if (has_mbyte)
7042 spell_soundfold_wsal(slang, word, res);
7043 else
7044#endif
7045 spell_soundfold_sal(slang, word, res);
7046 }
7047}
7048
7049/*
7050 * Perform sound folding of "inword" into "res" according to SOFOFROM and
7051 * SOFOTO lines.
7052 */
7053 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007054spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007055{
7056 char_u *s;
7057 int ri = 0;
7058 int c;
7059
7060#ifdef FEAT_MBYTE
7061 if (has_mbyte)
7062 {
7063 int prevc = 0;
7064 int *ip;
7065
7066 /* The sl_sal_first[] table contains the translation for chars up to
7067 * 255, sl_sal the rest. */
7068 for (s = inword; *s != NUL; )
7069 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007070 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007071 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007072 c = ' ';
7073 else if (c < 256)
7074 c = slang->sl_sal_first[c];
7075 else
7076 {
7077 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
7078 if (ip == NULL) /* empty list, can't match */
7079 c = NUL;
7080 else
7081 for (;;) /* find "c" in the list */
7082 {
7083 if (*ip == 0) /* not found */
7084 {
7085 c = NUL;
7086 break;
7087 }
7088 if (*ip == c) /* match! */
7089 {
7090 c = ip[1];
7091 break;
7092 }
7093 ip += 2;
7094 }
7095 }
7096
7097 if (c != NUL && c != prevc)
7098 {
7099 ri += mb_char2bytes(c, res + ri);
7100 if (ri + MB_MAXBYTES > MAXWLEN)
7101 break;
7102 prevc = c;
7103 }
7104 }
7105 }
7106 else
7107#endif
7108 {
7109 /* The sl_sal_first[] table contains the translation. */
7110 for (s = inword; (c = *s) != NUL; ++s)
7111 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007112 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007113 c = ' ';
7114 else
7115 c = slang->sl_sal_first[c];
7116 if (c != NUL && (ri == 0 || res[ri - 1] != c))
7117 res[ri++] = c;
7118 }
7119 }
7120
7121 res[ri] = NUL;
7122}
7123
7124 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007125spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007126{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007127 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007128 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007129 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007130 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007131 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007132 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007133 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007134 int n, k = 0;
7135 int z0;
7136 int k0;
7137 int n0;
7138 int c;
7139 int pri;
7140 int p0 = -333;
7141 int c0;
7142
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007143 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007144 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007145 if (slang->sl_rem_accents)
7146 {
7147 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007148 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007149 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007150 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007151 {
7152 *t++ = ' ';
7153 s = skipwhite(s);
7154 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007155 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007156 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007157 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158 *t++ = *s;
7159 ++s;
7160 }
7161 }
7162 *t = NUL;
7163 }
7164 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007165 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007166
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007167 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007168
7169 /*
7170 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007171 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007172 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007173 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007174 while ((c = word[i]) != NUL)
7175 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007176 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007177 n = slang->sl_sal_first[c];
7178 z0 = 0;
7179
7180 if (n >= 0)
7181 {
7182 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007183 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007184 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007185 /* Quickly skip entries that don't match the word. Most
7186 * entries are less then three chars, optimize for that. */
7187 k = smp[n].sm_leadlen;
7188 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007189 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007190 if (word[i + 1] != s[1])
7191 continue;
7192 if (k > 2)
7193 {
7194 for (j = 2; j < k; ++j)
7195 if (word[i + j] != s[j])
7196 break;
7197 if (j < k)
7198 continue;
7199 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007200 }
7201
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007202 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007203 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007204 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007205 while (*pf != NUL && *pf != word[i + k])
7206 ++pf;
7207 if (*pf == NUL)
7208 continue;
7209 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007210 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007211 s = smp[n].sm_rules;
7212 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007213
7214 p0 = *s;
7215 k0 = k;
7216 while (*s == '-' && k > 1)
7217 {
7218 k--;
7219 s++;
7220 }
7221 if (*s == '<')
7222 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007223 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007224 {
7225 /* determine priority */
7226 pri = *s - '0';
7227 s++;
7228 }
7229 if (*s == '^' && *(s + 1) == '^')
7230 s++;
7231
7232 if (*s == NUL
7233 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007234 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007235 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007236 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007237 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007238 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007239 && spell_iswordp(word + i - 1, curwin)
7240 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007241 {
7242 /* search for followup rules, if: */
7243 /* followup and k > 1 and NO '-' in searchstring */
7244 c0 = word[i + k - 1];
7245 n0 = slang->sl_sal_first[c0];
7246
7247 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007248 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007249 {
7250 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007251 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007253 /* Quickly skip entries that don't match the word.
7254 * */
7255 k0 = smp[n0].sm_leadlen;
7256 if (k0 > 1)
7257 {
7258 if (word[i + k] != s[1])
7259 continue;
7260 if (k0 > 2)
7261 {
7262 pf = word + i + k + 1;
7263 for (j = 2; j < k0; ++j)
7264 if (*pf++ != s[j])
7265 break;
7266 if (j < k0)
7267 continue;
7268 }
7269 }
7270 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007271
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007272 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007273 {
7274 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007275 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007276 while (*pf != NUL && *pf != word[i + k0])
7277 ++pf;
7278 if (*pf == NUL)
7279 continue;
7280 ++k0;
7281 }
7282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007283 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007284 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007285 while (*s == '-')
7286 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007287 /* "k0" gets NOT reduced because
7288 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007289 s++;
7290 }
7291 if (*s == '<')
7292 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007293 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007294 {
7295 p0 = *s - '0';
7296 s++;
7297 }
7298
7299 if (*s == NUL
7300 /* *s == '^' cuts */
7301 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007302 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007303 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007304 {
7305 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007306 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007307 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007308
7309 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007310 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007311 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007312 /* rule fits; stop search */
7313 break;
7314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315 }
7316
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007317 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007318 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007319 }
7320
7321 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007322 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007323 if (s == NULL)
7324 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007325 pf = smp[n].sm_rules;
7326 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007327 if (p0 == 1 && z == 0)
7328 {
7329 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007330 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7331 || res[reslen - 1] == *s))
7332 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007333 z0 = 1;
7334 z = 1;
7335 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007336 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007337 {
7338 word[i + k0] = *s;
7339 k0++;
7340 s++;
7341 }
7342 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007343 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007344
7345 /* new "actual letter" */
7346 c = word[i];
7347 }
7348 else
7349 {
7350 /* no '<' rule used */
7351 i += k - 1;
7352 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007353 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007354 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007355 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007356 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007357 s++;
7358 }
7359 /* new "actual letter" */
7360 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007361 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007362 {
7363 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007364 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007365 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007366 i = 0;
7367 z0 = 1;
7368 }
7369 }
7370 break;
7371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007372 }
7373 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007374 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007375 {
7376 c = ' ';
7377 k = 1;
7378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007379
7380 if (z0 == 0)
7381 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007382 if (k && !p0 && reslen < MAXWLEN && c != NUL
7383 && (!slang->sl_collapse || reslen == 0
7384 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007385 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007386 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007387
7388 i++;
7389 z = 0;
7390 k = 0;
7391 }
7392 }
7393
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007394 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007395}
7396
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007397#ifdef FEAT_MBYTE
7398/*
7399 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7400 * Multi-byte version of spell_soundfold().
7401 */
7402 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007403spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007404{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007405 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007406 int word[MAXWLEN];
7407 int wres[MAXWLEN];
7408 int l;
7409 char_u *s;
7410 int *ws;
7411 char_u *t;
7412 int *pf;
7413 int i, j, z;
7414 int reslen;
7415 int n, k = 0;
7416 int z0;
7417 int k0;
7418 int n0;
7419 int c;
7420 int pri;
7421 int p0 = -333;
7422 int c0;
7423 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007424 int wordlen;
7425
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007426
7427 /*
7428 * Convert the multi-byte string to a wide-character string.
7429 * Remove accents, if wanted. We actually remove all non-word characters.
7430 * But keep white space.
7431 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007432 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007433 for (s = inword; *s != NUL; )
7434 {
7435 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007436 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007437 if (slang->sl_rem_accents)
7438 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007439 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007440 {
7441 if (did_white)
7442 continue;
7443 c = ' ';
7444 did_white = TRUE;
7445 }
7446 else
7447 {
7448 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007449 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007450 continue;
7451 }
7452 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007453 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007454 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007455 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007456
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007457 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007458 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007459 * Converted from C++ to C. Added support for multi-byte chars.
7460 * Changed to keep spaces.
7461 */
7462 i = reslen = z = 0;
7463 while ((c = word[i]) != NUL)
7464 {
7465 /* Start with the first rule that has the character in the word. */
7466 n = slang->sl_sal_first[c & 0xff];
7467 z0 = 0;
7468
7469 if (n >= 0)
7470 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007471 /* Check all rules for the same index byte.
7472 * If c is 0x300 need extra check for the end of the array, as
7473 * (c & 0xff) is NUL. */
7474 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7475 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007476 {
7477 /* Quickly skip entries that don't match the word. Most
7478 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007479 if (c != ws[0])
7480 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007481 k = smp[n].sm_leadlen;
7482 if (k > 1)
7483 {
7484 if (word[i + 1] != ws[1])
7485 continue;
7486 if (k > 2)
7487 {
7488 for (j = 2; j < k; ++j)
7489 if (word[i + j] != ws[j])
7490 break;
7491 if (j < k)
7492 continue;
7493 }
7494 }
7495
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007496 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007497 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007498 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007499 while (*pf != NUL && *pf != word[i + k])
7500 ++pf;
7501 if (*pf == NUL)
7502 continue;
7503 ++k;
7504 }
7505 s = smp[n].sm_rules;
7506 pri = 5; /* default priority */
7507
7508 p0 = *s;
7509 k0 = k;
7510 while (*s == '-' && k > 1)
7511 {
7512 k--;
7513 s++;
7514 }
7515 if (*s == '<')
7516 s++;
7517 if (VIM_ISDIGIT(*s))
7518 {
7519 /* determine priority */
7520 pri = *s - '0';
7521 s++;
7522 }
7523 if (*s == '^' && *(s + 1) == '^')
7524 s++;
7525
7526 if (*s == NUL
7527 || (*s == '^'
7528 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007529 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007530 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007531 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007532 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007533 && spell_iswordp_w(word + i - 1, curwin)
7534 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007535 {
7536 /* search for followup rules, if: */
7537 /* followup and k > 1 and NO '-' in searchstring */
7538 c0 = word[i + k - 1];
7539 n0 = slang->sl_sal_first[c0 & 0xff];
7540
7541 if (slang->sl_followup && k > 1 && n0 >= 0
7542 && p0 != '-' && word[i + k] != NUL)
7543 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007544 /* Test follow-up rule for "word[i + k]"; loop over
7545 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007546 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7547 == (c0 & 0xff); ++n0)
7548 {
7549 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007550 */
7551 if (c0 != ws[0])
7552 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007553 k0 = smp[n0].sm_leadlen;
7554 if (k0 > 1)
7555 {
7556 if (word[i + k] != ws[1])
7557 continue;
7558 if (k0 > 2)
7559 {
7560 pf = word + i + k + 1;
7561 for (j = 2; j < k0; ++j)
7562 if (*pf++ != ws[j])
7563 break;
7564 if (j < k0)
7565 continue;
7566 }
7567 }
7568 k0 += k - 1;
7569
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007570 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007571 {
7572 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007573 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007574 while (*pf != NUL && *pf != word[i + k0])
7575 ++pf;
7576 if (*pf == NUL)
7577 continue;
7578 ++k0;
7579 }
7580
7581 p0 = 5;
7582 s = smp[n0].sm_rules;
7583 while (*s == '-')
7584 {
7585 /* "k0" gets NOT reduced because
7586 * "if (k0 == k)" */
7587 s++;
7588 }
7589 if (*s == '<')
7590 s++;
7591 if (VIM_ISDIGIT(*s))
7592 {
7593 p0 = *s - '0';
7594 s++;
7595 }
7596
7597 if (*s == NUL
7598 /* *s == '^' cuts */
7599 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007600 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007601 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007602 {
7603 if (k0 == k)
7604 /* this is just a piece of the string */
7605 continue;
7606
7607 if (p0 < pri)
7608 /* priority too low */
7609 continue;
7610 /* rule fits; stop search */
7611 break;
7612 }
7613 }
7614
7615 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7616 == (c0 & 0xff))
7617 continue;
7618 }
7619
7620 /* replace string */
7621 ws = smp[n].sm_to_w;
7622 s = smp[n].sm_rules;
7623 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7624 if (p0 == 1 && z == 0)
7625 {
7626 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007627 if (reslen > 0 && ws != NULL && *ws != NUL
7628 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007629 || wres[reslen - 1] == *ws))
7630 reslen--;
7631 z0 = 1;
7632 z = 1;
7633 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007634 if (ws != NULL)
7635 while (*ws != NUL && word[i + k0] != NUL)
7636 {
7637 word[i + k0] = *ws;
7638 k0++;
7639 ws++;
7640 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007641 if (k > k0)
7642 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007643 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007644
7645 /* new "actual letter" */
7646 c = word[i];
7647 }
7648 else
7649 {
7650 /* no '<' rule used */
7651 i += k - 1;
7652 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007653 if (ws != NULL)
7654 while (*ws != NUL && ws[1] != NUL
7655 && reslen < MAXWLEN)
7656 {
7657 if (reslen == 0 || wres[reslen - 1] != *ws)
7658 wres[reslen++] = *ws;
7659 ws++;
7660 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007661 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007662 if (ws == NULL)
7663 c = NUL;
7664 else
7665 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007666 if (strstr((char *)s, "^^") != NULL)
7667 {
7668 if (c != NUL)
7669 wres[reslen++] = c;
7670 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007671 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007672 i = 0;
7673 z0 = 1;
7674 }
7675 }
7676 break;
7677 }
7678 }
7679 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007680 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007681 {
7682 c = ' ';
7683 k = 1;
7684 }
7685
7686 if (z0 == 0)
7687 {
7688 if (k && !p0 && reslen < MAXWLEN && c != NUL
7689 && (!slang->sl_collapse || reslen == 0
7690 || wres[reslen - 1] != c))
7691 /* condense only double letters */
7692 wres[reslen++] = c;
7693
7694 i++;
7695 z = 0;
7696 k = 0;
7697 }
7698 }
7699
7700 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7701 l = 0;
7702 for (n = 0; n < reslen; ++n)
7703 {
7704 l += mb_char2bytes(wres[n], res + l);
7705 if (l + MB_MAXBYTES > MAXWLEN)
7706 break;
7707 }
7708 res[l] = NUL;
7709}
7710#endif
7711
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007712/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007713 * Compute a score for two sound-a-like words.
7714 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7715 * Instead of a generic loop we write out the code. That keeps it fast by
7716 * avoiding checks that will not be possible.
7717 */
7718 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007719soundalike_score(
7720 char_u *goodstart, /* sound-folded good word */
7721 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007722{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007723 char_u *goodsound = goodstart;
7724 char_u *badsound = badstart;
7725 int goodlen;
7726 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007727 int n;
7728 char_u *pl, *ps;
7729 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007730 int score = 0;
7731
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007732 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007733 * counted so much, vowels halfway the word aren't counted at all. */
7734 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7735 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007736 if ((badsound[0] == NUL && goodsound[1] == NUL)
7737 || (goodsound[0] == NUL && badsound[1] == NUL))
7738 /* changing word with vowel to word without a sound */
7739 return SCORE_DEL;
7740 if (badsound[0] == NUL || goodsound[0] == NUL)
7741 /* more than two changes */
7742 return SCORE_MAXMAX;
7743
Bram Moolenaar4770d092006-01-12 23:22:24 +00007744 if (badsound[1] == goodsound[1]
7745 || (badsound[1] != NUL
7746 && goodsound[1] != NUL
7747 && badsound[2] == goodsound[2]))
7748 {
7749 /* handle like a substitute */
7750 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007751 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007752 {
7753 score = 2 * SCORE_DEL / 3;
7754 if (*badsound == '*')
7755 ++badsound;
7756 else
7757 ++goodsound;
7758 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007759 }
7760
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007761 goodlen = (int)STRLEN(goodsound);
7762 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007763
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007764 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007765 * changes. */
7766 n = goodlen - badlen;
7767 if (n < -2 || n > 2)
7768 return SCORE_MAXMAX;
7769
7770 if (n > 0)
7771 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007772 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007773 ps = badsound;
7774 }
7775 else
7776 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007777 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007778 ps = goodsound;
7779 }
7780
7781 /* Skip over the identical part. */
7782 while (*pl == *ps && *pl != NUL)
7783 {
7784 ++pl;
7785 ++ps;
7786 }
7787
7788 switch (n)
7789 {
7790 case -2:
7791 case 2:
7792 /*
7793 * Must delete two characters from "pl".
7794 */
7795 ++pl; /* first delete */
7796 while (*pl == *ps)
7797 {
7798 ++pl;
7799 ++ps;
7800 }
7801 /* strings must be equal after second delete */
7802 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007803 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007804
7805 /* Failed to compare. */
7806 break;
7807
7808 case -1:
7809 case 1:
7810 /*
7811 * Minimal one delete from "pl" required.
7812 */
7813
7814 /* 1: delete */
7815 pl2 = pl + 1;
7816 ps2 = ps;
7817 while (*pl2 == *ps2)
7818 {
7819 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007820 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007821 ++pl2;
7822 ++ps2;
7823 }
7824
7825 /* 2: delete then swap, then rest must be equal */
7826 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7827 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007828 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007829
7830 /* 3: delete then substitute, then the rest must be equal */
7831 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007832 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007833
7834 /* 4: first swap then delete */
7835 if (pl[0] == ps[1] && pl[1] == ps[0])
7836 {
7837 pl2 = pl + 2; /* swap, skip two chars */
7838 ps2 = ps + 2;
7839 while (*pl2 == *ps2)
7840 {
7841 ++pl2;
7842 ++ps2;
7843 }
7844 /* delete a char and then strings must be equal */
7845 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007846 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007847 }
7848
7849 /* 5: first substitute then delete */
7850 pl2 = pl + 1; /* substitute, skip one char */
7851 ps2 = ps + 1;
7852 while (*pl2 == *ps2)
7853 {
7854 ++pl2;
7855 ++ps2;
7856 }
7857 /* delete a char and then strings must be equal */
7858 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007859 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007860
7861 /* Failed to compare. */
7862 break;
7863
7864 case 0:
7865 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007866 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007867 * insert is only possible in combination with a delete.
7868 * 1: check if for identical strings
7869 */
7870 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007871 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007872
7873 /* 2: swap */
7874 if (pl[0] == ps[1] && pl[1] == ps[0])
7875 {
7876 pl2 = pl + 2; /* swap, skip two chars */
7877 ps2 = ps + 2;
7878 while (*pl2 == *ps2)
7879 {
7880 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007881 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007882 ++pl2;
7883 ++ps2;
7884 }
7885 /* 3: swap and swap again */
7886 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7887 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007888 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007889
7890 /* 4: swap and substitute */
7891 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007892 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007893 }
7894
7895 /* 5: substitute */
7896 pl2 = pl + 1;
7897 ps2 = ps + 1;
7898 while (*pl2 == *ps2)
7899 {
7900 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007901 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007902 ++pl2;
7903 ++ps2;
7904 }
7905
7906 /* 6: substitute and swap */
7907 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7908 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007909 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007910
7911 /* 7: substitute and substitute */
7912 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007913 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007914
7915 /* 8: insert then delete */
7916 pl2 = pl;
7917 ps2 = ps + 1;
7918 while (*pl2 == *ps2)
7919 {
7920 ++pl2;
7921 ++ps2;
7922 }
7923 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007924 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007925
7926 /* 9: delete then insert */
7927 pl2 = pl + 1;
7928 ps2 = ps;
7929 while (*pl2 == *ps2)
7930 {
7931 ++pl2;
7932 ++ps2;
7933 }
7934 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007935 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007936
7937 /* Failed to compare. */
7938 break;
7939 }
7940
7941 return SCORE_MAXMAX;
7942}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944/*
7945 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007946 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007947 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007948 * The algorithm is described by Du and Chang, 1992.
7949 * The implementation of the algorithm comes from Aspell editdist.cpp,
7950 * edit_distance(). It has been converted from C++ to C and modified to
7951 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007952 */
7953 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007954spell_edit_score(
7955 slang_T *slang,
7956 char_u *badword,
7957 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007958{
7959 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007960 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007961 int j, i;
7962 int t;
7963 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007964 int pbc, pgc;
7965#ifdef FEAT_MBYTE
7966 char_u *p;
7967 int wbadword[MAXWLEN];
7968 int wgoodword[MAXWLEN];
7969
7970 if (has_mbyte)
7971 {
7972 /* Get the characters from the multi-byte strings and put them in an
7973 * int array for easy access. */
7974 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007975 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007976 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007977 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007978 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007979 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007980 }
7981 else
7982#endif
7983 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007984 badlen = (int)STRLEN(badword) + 1;
7985 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007986 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007987
7988 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7989#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007990 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7991 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007992 if (cnt == NULL)
7993 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007994
7995 CNT(0, 0) = 0;
7996 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007997 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007998
7999 for (i = 1; i <= badlen; ++i)
8000 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00008001 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 for (j = 1; j <= goodlen; ++j)
8003 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008004#ifdef FEAT_MBYTE
8005 if (has_mbyte)
8006 {
8007 bc = wbadword[i - 1];
8008 gc = wgoodword[j - 1];
8009 }
8010 else
8011#endif
8012 {
8013 bc = badword[i - 1];
8014 gc = goodword[j - 1];
8015 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008016 if (bc == gc)
8017 CNT(i, j) = CNT(i - 1, j - 1);
8018 else
8019 {
8020 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008021 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
8023 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008024 {
8025 /* For a similar character use SCORE_SIMILAR. */
8026 if (slang != NULL
8027 && slang->sl_has_map
8028 && similar_chars(slang, gc, bc))
8029 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
8030 else
8031 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
8032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008033
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008034 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008035 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008036#ifdef FEAT_MBYTE
8037 if (has_mbyte)
8038 {
8039 pbc = wbadword[i - 2];
8040 pgc = wgoodword[j - 2];
8041 }
8042 else
8043#endif
8044 {
8045 pbc = badword[i - 2];
8046 pgc = goodword[j - 2];
8047 }
8048 if (bc == pgc && pbc == gc)
8049 {
8050 t = SCORE_SWAP + CNT(i - 2, j - 2);
8051 if (t < CNT(i, j))
8052 CNT(i, j) = t;
8053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008054 }
8055 t = SCORE_DEL + CNT(i - 1, j);
8056 if (t < CNT(i, j))
8057 CNT(i, j) = t;
8058 t = SCORE_INS + CNT(i, j - 1);
8059 if (t < CNT(i, j))
8060 CNT(i, j) = t;
8061 }
8062 }
8063 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008064
8065 i = CNT(badlen - 1, goodlen - 1);
8066 vim_free(cnt);
8067 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008068}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008069
Bram Moolenaar4770d092006-01-12 23:22:24 +00008070typedef struct
8071{
8072 int badi;
8073 int goodi;
8074 int score;
8075} limitscore_T;
8076
8077/*
8078 * Like spell_edit_score(), but with a limit on the score to make it faster.
8079 * May return SCORE_MAXMAX when the score is higher than "limit".
8080 *
8081 * This uses a stack for the edits still to be tried.
8082 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
8083 * for multi-byte characters.
8084 */
8085 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008086spell_edit_score_limit(
8087 slang_T *slang,
8088 char_u *badword,
8089 char_u *goodword,
8090 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008091{
8092 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8093 int stackidx;
8094 int bi, gi;
8095 int bi2, gi2;
8096 int bc, gc;
8097 int score;
8098 int score_off;
8099 int minscore;
8100 int round;
8101
8102#ifdef FEAT_MBYTE
8103 /* Multi-byte characters require a bit more work, use a different function
8104 * to avoid testing "has_mbyte" quite often. */
8105 if (has_mbyte)
8106 return spell_edit_score_limit_w(slang, badword, goodword, limit);
8107#endif
8108
8109 /*
8110 * The idea is to go from start to end over the words. So long as
8111 * characters are equal just continue, this always gives the lowest score.
8112 * When there is a difference try several alternatives. Each alternative
8113 * increases "score" for the edit distance. Some of the alternatives are
8114 * pushed unto a stack and tried later, some are tried right away. At the
8115 * end of the word the score for one alternative is known. The lowest
8116 * possible score is stored in "minscore".
8117 */
8118 stackidx = 0;
8119 bi = 0;
8120 gi = 0;
8121 score = 0;
8122 minscore = limit + 1;
8123
8124 for (;;)
8125 {
8126 /* Skip over an equal part, score remains the same. */
8127 for (;;)
8128 {
8129 bc = badword[bi];
8130 gc = goodword[gi];
8131 if (bc != gc) /* stop at a char that's different */
8132 break;
8133 if (bc == NUL) /* both words end */
8134 {
8135 if (score < minscore)
8136 minscore = score;
8137 goto pop; /* do next alternative */
8138 }
8139 ++bi;
8140 ++gi;
8141 }
8142
8143 if (gc == NUL) /* goodword ends, delete badword chars */
8144 {
8145 do
8146 {
8147 if ((score += SCORE_DEL) >= minscore)
8148 goto pop; /* do next alternative */
8149 } while (badword[++bi] != NUL);
8150 minscore = score;
8151 }
8152 else if (bc == NUL) /* badword ends, insert badword chars */
8153 {
8154 do
8155 {
8156 if ((score += SCORE_INS) >= minscore)
8157 goto pop; /* do next alternative */
8158 } while (goodword[++gi] != NUL);
8159 minscore = score;
8160 }
8161 else /* both words continue */
8162 {
8163 /* If not close to the limit, perform a change. Only try changes
8164 * that may lead to a lower score than "minscore".
8165 * round 0: try deleting a char from badword
8166 * round 1: try inserting a char in badword */
8167 for (round = 0; round <= 1; ++round)
8168 {
8169 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8170 if (score_off < minscore)
8171 {
8172 if (score_off + SCORE_EDIT_MIN >= minscore)
8173 {
8174 /* Near the limit, rest of the words must match. We
8175 * can check that right now, no need to push an item
8176 * onto the stack. */
8177 bi2 = bi + 1 - round;
8178 gi2 = gi + round;
8179 while (goodword[gi2] == badword[bi2])
8180 {
8181 if (goodword[gi2] == NUL)
8182 {
8183 minscore = score_off;
8184 break;
8185 }
8186 ++bi2;
8187 ++gi2;
8188 }
8189 }
8190 else
8191 {
8192 /* try deleting/inserting a character later */
8193 stack[stackidx].badi = bi + 1 - round;
8194 stack[stackidx].goodi = gi + round;
8195 stack[stackidx].score = score_off;
8196 ++stackidx;
8197 }
8198 }
8199 }
8200
8201 if (score + SCORE_SWAP < minscore)
8202 {
8203 /* If swapping two characters makes a match then the
8204 * substitution is more expensive, thus there is no need to
8205 * try both. */
8206 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8207 {
8208 /* Swap two characters, that is: skip them. */
8209 gi += 2;
8210 bi += 2;
8211 score += SCORE_SWAP;
8212 continue;
8213 }
8214 }
8215
8216 /* Substitute one character for another which is the same
8217 * thing as deleting a character from both goodword and badword.
8218 * Use a better score when there is only a case difference. */
8219 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8220 score += SCORE_ICASE;
8221 else
8222 {
8223 /* For a similar character use SCORE_SIMILAR. */
8224 if (slang != NULL
8225 && slang->sl_has_map
8226 && similar_chars(slang, gc, bc))
8227 score += SCORE_SIMILAR;
8228 else
8229 score += SCORE_SUBST;
8230 }
8231
8232 if (score < minscore)
8233 {
8234 /* Do the substitution. */
8235 ++gi;
8236 ++bi;
8237 continue;
8238 }
8239 }
8240pop:
8241 /*
8242 * Get here to try the next alternative, pop it from the stack.
8243 */
8244 if (stackidx == 0) /* stack is empty, finished */
8245 break;
8246
8247 /* pop an item from the stack */
8248 --stackidx;
8249 gi = stack[stackidx].goodi;
8250 bi = stack[stackidx].badi;
8251 score = stack[stackidx].score;
8252 }
8253
8254 /* When the score goes over "limit" it may actually be much higher.
8255 * Return a very large number to avoid going below the limit when giving a
8256 * bonus. */
8257 if (minscore > limit)
8258 return SCORE_MAXMAX;
8259 return minscore;
8260}
8261
8262#ifdef FEAT_MBYTE
8263/*
8264 * Multi-byte version of spell_edit_score_limit().
8265 * Keep it in sync with the above!
8266 */
8267 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008268spell_edit_score_limit_w(
8269 slang_T *slang,
8270 char_u *badword,
8271 char_u *goodword,
8272 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008273{
8274 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8275 int stackidx;
8276 int bi, gi;
8277 int bi2, gi2;
8278 int bc, gc;
8279 int score;
8280 int score_off;
8281 int minscore;
8282 int round;
8283 char_u *p;
8284 int wbadword[MAXWLEN];
8285 int wgoodword[MAXWLEN];
8286
8287 /* Get the characters from the multi-byte strings and put them in an
8288 * int array for easy access. */
8289 bi = 0;
8290 for (p = badword; *p != NUL; )
8291 wbadword[bi++] = mb_cptr2char_adv(&p);
8292 wbadword[bi++] = 0;
8293 gi = 0;
8294 for (p = goodword; *p != NUL; )
8295 wgoodword[gi++] = mb_cptr2char_adv(&p);
8296 wgoodword[gi++] = 0;
8297
8298 /*
8299 * The idea is to go from start to end over the words. So long as
8300 * characters are equal just continue, this always gives the lowest score.
8301 * When there is a difference try several alternatives. Each alternative
8302 * increases "score" for the edit distance. Some of the alternatives are
8303 * pushed unto a stack and tried later, some are tried right away. At the
8304 * end of the word the score for one alternative is known. The lowest
8305 * possible score is stored in "minscore".
8306 */
8307 stackidx = 0;
8308 bi = 0;
8309 gi = 0;
8310 score = 0;
8311 minscore = limit + 1;
8312
8313 for (;;)
8314 {
8315 /* Skip over an equal part, score remains the same. */
8316 for (;;)
8317 {
8318 bc = wbadword[bi];
8319 gc = wgoodword[gi];
8320
8321 if (bc != gc) /* stop at a char that's different */
8322 break;
8323 if (bc == NUL) /* both words end */
8324 {
8325 if (score < minscore)
8326 minscore = score;
8327 goto pop; /* do next alternative */
8328 }
8329 ++bi;
8330 ++gi;
8331 }
8332
8333 if (gc == NUL) /* goodword ends, delete badword chars */
8334 {
8335 do
8336 {
8337 if ((score += SCORE_DEL) >= minscore)
8338 goto pop; /* do next alternative */
8339 } while (wbadword[++bi] != NUL);
8340 minscore = score;
8341 }
8342 else if (bc == NUL) /* badword ends, insert badword chars */
8343 {
8344 do
8345 {
8346 if ((score += SCORE_INS) >= minscore)
8347 goto pop; /* do next alternative */
8348 } while (wgoodword[++gi] != NUL);
8349 minscore = score;
8350 }
8351 else /* both words continue */
8352 {
8353 /* If not close to the limit, perform a change. Only try changes
8354 * that may lead to a lower score than "minscore".
8355 * round 0: try deleting a char from badword
8356 * round 1: try inserting a char in badword */
8357 for (round = 0; round <= 1; ++round)
8358 {
8359 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8360 if (score_off < minscore)
8361 {
8362 if (score_off + SCORE_EDIT_MIN >= minscore)
8363 {
8364 /* Near the limit, rest of the words must match. We
8365 * can check that right now, no need to push an item
8366 * onto the stack. */
8367 bi2 = bi + 1 - round;
8368 gi2 = gi + round;
8369 while (wgoodword[gi2] == wbadword[bi2])
8370 {
8371 if (wgoodword[gi2] == NUL)
8372 {
8373 minscore = score_off;
8374 break;
8375 }
8376 ++bi2;
8377 ++gi2;
8378 }
8379 }
8380 else
8381 {
8382 /* try deleting a character from badword later */
8383 stack[stackidx].badi = bi + 1 - round;
8384 stack[stackidx].goodi = gi + round;
8385 stack[stackidx].score = score_off;
8386 ++stackidx;
8387 }
8388 }
8389 }
8390
8391 if (score + SCORE_SWAP < minscore)
8392 {
8393 /* If swapping two characters makes a match then the
8394 * substitution is more expensive, thus there is no need to
8395 * try both. */
8396 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8397 {
8398 /* Swap two characters, that is: skip them. */
8399 gi += 2;
8400 bi += 2;
8401 score += SCORE_SWAP;
8402 continue;
8403 }
8404 }
8405
8406 /* Substitute one character for another which is the same
8407 * thing as deleting a character from both goodword and badword.
8408 * Use a better score when there is only a case difference. */
8409 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8410 score += SCORE_ICASE;
8411 else
8412 {
8413 /* For a similar character use SCORE_SIMILAR. */
8414 if (slang != NULL
8415 && slang->sl_has_map
8416 && similar_chars(slang, gc, bc))
8417 score += SCORE_SIMILAR;
8418 else
8419 score += SCORE_SUBST;
8420 }
8421
8422 if (score < minscore)
8423 {
8424 /* Do the substitution. */
8425 ++gi;
8426 ++bi;
8427 continue;
8428 }
8429 }
8430pop:
8431 /*
8432 * Get here to try the next alternative, pop it from the stack.
8433 */
8434 if (stackidx == 0) /* stack is empty, finished */
8435 break;
8436
8437 /* pop an item from the stack */
8438 --stackidx;
8439 gi = stack[stackidx].goodi;
8440 bi = stack[stackidx].badi;
8441 score = stack[stackidx].score;
8442 }
8443
8444 /* When the score goes over "limit" it may actually be much higher.
8445 * Return a very large number to avoid going below the limit when giving a
8446 * bonus. */
8447 if (minscore > limit)
8448 return SCORE_MAXMAX;
8449 return minscore;
8450}
8451#endif
8452
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008453/*
8454 * ":spellinfo"
8455 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008456 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008457ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008458{
8459 int lpi;
8460 langp_T *lp;
8461 char_u *p;
8462
8463 if (no_spell_checking(curwin))
8464 return;
8465
8466 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008467 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008468 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008469 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008470 msg_puts((char_u *)"file: ");
8471 msg_puts(lp->lp_slang->sl_fname);
8472 msg_putchar('\n');
8473 p = lp->lp_slang->sl_info;
8474 if (p != NULL)
8475 {
8476 msg_puts(p);
8477 msg_putchar('\n');
8478 }
8479 }
8480 msg_end();
8481}
8482
Bram Moolenaar4770d092006-01-12 23:22:24 +00008483#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8484#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008485#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008486#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8487#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008488
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008489/*
8490 * ":spelldump"
8491 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008492 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008493ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008494{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008495 char_u *spl;
8496 long dummy;
8497
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008498 if (no_spell_checking(curwin))
8499 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008500 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008501
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008502 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008503 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008504
8505 /* enable spelling locally in the new window */
8506 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008507 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008508 vim_free(spl);
8509
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008510 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008511 return;
8512
Bram Moolenaar860cae12010-06-05 23:22:07 +02008513 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008514
8515 /* Delete the empty line that we started with. */
8516 if (curbuf->b_ml.ml_line_count > 1)
8517 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8518
8519 redraw_later(NOT_VALID);
8520}
8521
8522/*
8523 * Go through all possible words and:
8524 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8525 * "ic" and "dir" are not used.
8526 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8527 */
8528 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008529spell_dump_compl(
8530 char_u *pat, /* leading part of the word */
8531 int ic, /* ignore case */
8532 int *dir, /* direction for adding matches */
8533 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008534{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008535 langp_T *lp;
8536 slang_T *slang;
8537 idx_T arridx[MAXWLEN];
8538 int curi[MAXWLEN];
8539 char_u word[MAXWLEN];
8540 int c;
8541 char_u *byts;
8542 idx_T *idxs;
8543 linenr_T lnum = 0;
8544 int round;
8545 int depth;
8546 int n;
8547 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008548 char_u *region_names = NULL; /* region names being used */
8549 int do_region = TRUE; /* dump region names and numbers */
8550 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008551 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008552 int dumpflags = dumpflags_arg;
8553 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008554
Bram Moolenaard0131a82006-03-04 21:46:13 +00008555 /* When ignoring case or when the pattern starts with capital pass this on
8556 * to dump_word(). */
8557 if (pat != NULL)
8558 {
8559 if (ic)
8560 dumpflags |= DUMPFLAG_ICASE;
8561 else
8562 {
8563 n = captype(pat, NULL);
8564 if (n == WF_ONECAP)
8565 dumpflags |= DUMPFLAG_ONECAP;
8566 else if (n == WF_ALLCAP
8567#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008568 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008569#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008570 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +00008571#endif
8572 )
8573 dumpflags |= DUMPFLAG_ALLCAP;
8574 }
8575 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008576
Bram Moolenaar7887d882005-07-01 22:33:52 +00008577 /* Find out if we can support regions: All languages must support the same
8578 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008579 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008580 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008581 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008582 p = lp->lp_slang->sl_regions;
8583 if (p[0] != 0)
8584 {
8585 if (region_names == NULL) /* first language with regions */
8586 region_names = p;
8587 else if (STRCMP(region_names, p) != 0)
8588 {
8589 do_region = FALSE; /* region names are different */
8590 break;
8591 }
8592 }
8593 }
8594
8595 if (do_region && region_names != NULL)
8596 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008597 if (pat == NULL)
8598 {
8599 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8600 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8601 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008602 }
8603 else
8604 do_region = FALSE;
8605
8606 /*
8607 * Loop over all files loaded for the entries in 'spelllang'.
8608 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008609 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008610 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008611 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008612 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008613 if (slang->sl_fbyts == NULL) /* reloading failed */
8614 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008615
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008616 if (pat == NULL)
8617 {
8618 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8619 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8620 }
8621
8622 /* When matching with a pattern and there are no prefixes only use
8623 * parts of the tree that match "pat". */
8624 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008625 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008626 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008627 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008628
8629 /* round 1: case-folded tree
8630 * round 2: keep-case tree */
8631 for (round = 1; round <= 2; ++round)
8632 {
8633 if (round == 1)
8634 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008635 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008636 byts = slang->sl_fbyts;
8637 idxs = slang->sl_fidxs;
8638 }
8639 else
8640 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008641 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008642 byts = slang->sl_kbyts;
8643 idxs = slang->sl_kidxs;
8644 }
8645 if (byts == NULL)
8646 continue; /* array is empty */
8647
8648 depth = 0;
8649 arridx[0] = 0;
8650 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008651 while (depth >= 0 && !got_int
8652 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008653 {
8654 if (curi[depth] > byts[arridx[depth]])
8655 {
8656 /* Done all bytes at this node, go up one level. */
8657 --depth;
8658 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008659 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008660 }
8661 else
8662 {
8663 /* Do one more byte at this node. */
8664 n = arridx[depth] + curi[depth];
8665 ++curi[depth];
8666 c = byts[n];
8667 if (c == 0)
8668 {
8669 /* End of word, deal with the word.
8670 * Don't use keep-case words in the fold-case tree,
8671 * they will appear in the keep-case tree.
8672 * Only use the word when the region matches. */
8673 flags = (int)idxs[n];
8674 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008675 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008676 && (do_region
8677 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008678 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008679 & lp->lp_region) != 0))
8680 {
8681 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008682 if (!do_region)
8683 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008684
8685 /* Dump the basic word if there is no prefix or
8686 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008687 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008688 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008689 {
8690 dump_word(slang, word, pat, dir,
8691 dumpflags, flags, lnum);
8692 if (pat == NULL)
8693 ++lnum;
8694 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008695
8696 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008697 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008698 lnum = dump_prefixes(slang, word, pat, dir,
8699 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008700 }
8701 }
8702 else
8703 {
8704 /* Normal char, go one level deeper. */
8705 word[depth++] = c;
8706 arridx[depth] = idxs[n];
8707 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008708
8709 /* Check if this characters matches with the pattern.
8710 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008711 * Always ignore case here, dump_word() will check
8712 * proper case later. This isn't exactly right when
8713 * length changes for multi-byte characters with
8714 * ignore case... */
8715 if (depth <= patlen
8716 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008717 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008718 }
8719 }
8720 }
8721 }
8722 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008723}
8724
8725/*
8726 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008727 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008728 */
8729 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008730dump_word(
8731 slang_T *slang,
8732 char_u *word,
8733 char_u *pat,
8734 int *dir,
8735 int dumpflags,
8736 int wordflags,
8737 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008738{
8739 int keepcap = FALSE;
8740 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008741 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008742 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008743 char_u badword[MAXWLEN + 10];
8744 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008745 int flags = wordflags;
8746
8747 if (dumpflags & DUMPFLAG_ONECAP)
8748 flags |= WF_ONECAP;
8749 if (dumpflags & DUMPFLAG_ALLCAP)
8750 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008751
Bram Moolenaar4770d092006-01-12 23:22:24 +00008752 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008753 {
8754 /* Need to fix case according to "flags". */
8755 make_case_word(word, cword, flags);
8756 p = cword;
8757 }
8758 else
8759 {
8760 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008761 if ((dumpflags & DUMPFLAG_KEEPCASE)
8762 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008763 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008764 keepcap = TRUE;
8765 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008766 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008767
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008768 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008769 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008770 /* Add flags and regions after a slash. */
8771 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008772 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008773 STRCPY(badword, p);
8774 STRCAT(badword, "/");
8775 if (keepcap)
8776 STRCAT(badword, "=");
8777 if (flags & WF_BANNED)
8778 STRCAT(badword, "!");
8779 else if (flags & WF_RARE)
8780 STRCAT(badword, "?");
8781 if (flags & WF_REGION)
8782 for (i = 0; i < 7; ++i)
8783 if (flags & (0x10000 << i))
8784 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8785 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008786 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008787
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008788 if (dumpflags & DUMPFLAG_COUNT)
8789 {
8790 hashitem_T *hi;
8791
8792 /* Include the word count for ":spelldump!". */
8793 hi = hash_find(&slang->sl_wordcount, tw);
8794 if (!HASHITEM_EMPTY(hi))
8795 {
8796 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8797 tw, HI2WC(hi)->wc_count);
8798 p = IObuff;
8799 }
8800 }
8801
8802 ml_append(lnum, p, (colnr_T)0, FALSE);
8803 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008804 else if (((dumpflags & DUMPFLAG_ICASE)
8805 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8806 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008807 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008808 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008809 /* if dir was BACKWARD then honor it just once */
8810 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008811}
8812
8813/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008814 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8815 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008816 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008817 * Return the updated line number.
8818 */
8819 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008820dump_prefixes(
8821 slang_T *slang,
8822 char_u *word, /* case-folded word */
8823 char_u *pat,
8824 int *dir,
8825 int dumpflags,
8826 int flags, /* flags with prefix ID */
8827 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008828{
8829 idx_T arridx[MAXWLEN];
8830 int curi[MAXWLEN];
8831 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008832 char_u word_up[MAXWLEN];
8833 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008834 int c;
8835 char_u *byts;
8836 idx_T *idxs;
8837 linenr_T lnum = startlnum;
8838 int depth;
8839 int n;
8840 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008841 int i;
8842
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008843 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008844 * upper-case letter in word_up[]. */
8845 c = PTR2CHAR(word);
8846 if (SPELL_TOUPPER(c) != c)
8847 {
8848 onecap_copy(word, word_up, TRUE);
8849 has_word_up = TRUE;
8850 }
8851
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008852 byts = slang->sl_pbyts;
8853 idxs = slang->sl_pidxs;
8854 if (byts != NULL) /* array not is empty */
8855 {
8856 /*
8857 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008858 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008859 */
8860 depth = 0;
8861 arridx[0] = 0;
8862 curi[0] = 1;
8863 while (depth >= 0 && !got_int)
8864 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008865 n = arridx[depth];
8866 len = byts[n];
8867 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008868 {
8869 /* Done all bytes at this node, go up one level. */
8870 --depth;
8871 line_breakcheck();
8872 }
8873 else
8874 {
8875 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008876 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008877 ++curi[depth];
8878 c = byts[n];
8879 if (c == 0)
8880 {
8881 /* End of prefix, find out how many IDs there are. */
8882 for (i = 1; i < len; ++i)
8883 if (byts[n + i] != 0)
8884 break;
8885 curi[depth] += i - 1;
8886
Bram Moolenaar53805d12005-08-01 07:08:33 +00008887 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8888 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008889 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008890 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008891 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008892 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008893 : flags, lnum);
8894 if (lnum != 0)
8895 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008896 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008897
8898 /* Check for prefix that matches the word when the
8899 * first letter is upper-case, but only if the prefix has
8900 * a condition. */
8901 if (has_word_up)
8902 {
8903 c = valid_word_prefix(i, n, flags, word_up, slang,
8904 TRUE);
8905 if (c != 0)
8906 {
8907 vim_strncpy(prefix + depth, word_up,
8908 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008909 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008910 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008911 : flags, lnum);
8912 if (lnum != 0)
8913 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008914 }
8915 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008916 }
8917 else
8918 {
8919 /* Normal char, go one level deeper. */
8920 prefix[depth++] = c;
8921 arridx[depth] = idxs[n];
8922 curi[depth] = 1;
8923 }
8924 }
8925 }
8926 }
8927
8928 return lnum;
8929}
8930
Bram Moolenaar95529562005-08-25 21:21:38 +00008931/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008932 * Move "p" to the end of word "start".
8933 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008934 */
8935 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008936spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008937{
8938 char_u *p = start;
8939
Bram Moolenaar860cae12010-06-05 23:22:07 +02008940 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008941 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008942 return p;
8943}
8944
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008945#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008946/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008947 * For Insert mode completion CTRL-X s:
8948 * Find start of the word in front of column "startcol".
8949 * We don't check if it is badly spelled, with completion we can only change
8950 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008951 * Returns the column number of the word.
8952 */
8953 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008954spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008955{
8956 char_u *line;
8957 char_u *p;
8958 int col = 0;
8959
Bram Moolenaar95529562005-08-25 21:21:38 +00008960 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008961 return startcol;
8962
8963 /* Find a word character before "startcol". */
8964 line = ml_get_curline();
8965 for (p = line + startcol; p > line; )
8966 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008967 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008968 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008969 break;
8970 }
8971
8972 /* Go back to start of the word. */
8973 while (p > line)
8974 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008975 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008976 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008977 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008978 break;
8979 col = 0;
8980 }
8981
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008982 return col;
8983}
8984
8985/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008986 * Need to check for 'spellcapcheck' now, the word is removed before
8987 * expand_spelling() is called. Therefore the ugly global variable.
8988 */
8989static int spell_expand_need_cap;
8990
8991 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008992spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00008993{
8994 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
8995}
8996
8997/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008998 * Get list of spelling suggestions.
8999 * Used for Insert mode completion CTRL-X ?.
9000 * Returns the number of matches. The matches are in "matchp[]", array of
9001 * allocated strings.
9002 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009003 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01009004expand_spelling(
9005 linenr_T lnum UNUSED,
9006 char_u *pat,
9007 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009008{
9009 garray_T ga;
9010
Bram Moolenaar4770d092006-01-12 23:22:24 +00009011 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009012 *matchp = ga.ga_data;
9013 return ga.ga_len;
9014}
9015#endif
9016
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009017#endif /* FEAT_SPELL */