blob: 8aadb995318232fcecf94d8aa9ed29fd489d0270 [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 int mi_cprefixlen; /* byte length of prefix in original
228 case */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000229
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 /* for when checking a compound word */
231 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000232 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
233 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000234 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000235
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000236 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000237 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200239 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000240
241 /* for NOBREAK */
242 int mi_result2; /* "mi_resul" without following word */
243 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000244} matchinf_T;
245
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000246
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100247static int spell_iswordp(char_u *p, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100248static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000249
250/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000251 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000252 */
253typedef enum
254{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255 STATE_START = 0, /* At start of node check for NUL bytes (goodword
256 * ends); if badword ends there is a match, otherwise
257 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000258 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000259 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000260 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
261 STATE_PLAIN, /* Use each byte of the node. */
262 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000263 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000264 STATE_INS, /* Insert a byte in the bad word. */
265 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000266 STATE_UNSWAP, /* Undo swap two characters. */
267 STATE_SWAP3, /* Swap two characters over three. */
268 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000269 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000270 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000271 STATE_REP_INI, /* Prepare for using REP items. */
272 STATE_REP, /* Use matching REP items from the .aff file. */
273 STATE_REP_UNDO, /* Undo a REP item replacement. */
274 STATE_FINAL /* End of this node. */
275} state_T;
276
277/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000278 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000279 */
280typedef struct trystate_S
281{
Bram Moolenaarea424162005-06-16 21:51:00 +0000282 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000284 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000285 short ts_curi; /* index in list of child nodes */
286 char_u ts_fidx; /* index in fword[], case-folded bad word */
287 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
288 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000289 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000290 * PFD_PREFIXTREE or PFD_NOPREFIX */
291 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000292 char_u ts_tcharlen; /* number of bytes in tword character */
293 char_u ts_tcharidx; /* current byte index in tword character */
294 char_u ts_isdiff; /* DIFF_ values */
295 char_u ts_fcharstart; /* index in fword where badword char started */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000296 char_u ts_prewordlen; /* length of word in "preword[]" */
297 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000298 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000299 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000300 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000301 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000302 char_u ts_delidx; /* index in fword for char that was deleted,
303 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304} trystate_T;
305
Bram Moolenaarea424162005-06-16 21:51:00 +0000306/* values for ts_isdiff */
307#define DIFF_NONE 0 /* no different byte (yet) */
308#define DIFF_YES 1 /* different byte found */
309#define DIFF_INSERT 2 /* inserting character */
310
Bram Moolenaard12a1322005-08-21 22:08:24 +0000311/* values for ts_flags */
312#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
313#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000314#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000315
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000316/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000317#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000318#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000319#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000321/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000322#define FIND_FOLDWORD 0 /* find word case-folded */
323#define FIND_KEEPWORD 1 /* find keep-case word */
324#define FIND_PREFIX 2 /* find word after prefix */
325#define FIND_COMPOUND 3 /* find case-folded compound word */
326#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000327
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100328static void find_word(matchinf_T *mip, int mode);
329static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap);
330static int can_compound(slang_T *slang, char_u *word, char_u *flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100331static int match_compoundrule(slang_T *slang, char_u *compflags);
332static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req);
333static void find_prefix(matchinf_T *mip, int mode);
334static int fold_more(matchinf_T *mip);
335static int spell_valid_case(int wordflags, int treeflags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100336static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100337static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100338static void clear_midword(win_T *buf);
339static void use_midword(slang_T *lp, win_T *buf);
340static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100341static int check_need_cap(linenr_T lnum, colnr_T col);
342static 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 +0000343#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100344static void spell_suggest_expr(suginfo_T *su, char_u *expr);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000345#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100346static void spell_suggest_file(suginfo_T *su, char_u *fname);
347static void spell_suggest_intern(suginfo_T *su, int interactive);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100348static void spell_find_cleanup(suginfo_T *su);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100349static void suggest_try_special(suginfo_T *su);
350static void suggest_try_change(suginfo_T *su);
351static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
352static void go_deeper(trystate_T *stack, int depth, int score_add);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100353static int nofold_len(char_u *fword, int flen, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100354static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
355static void score_comp_sal(suginfo_T *su);
356static void score_combine(suginfo_T *su);
357static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
358static void suggest_try_soundalike_prep(void);
359static void suggest_try_soundalike(suginfo_T *su);
360static void suggest_try_soundalike_finish(void);
361static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
362static int soundfold_find(slang_T *slang, char_u *word);
363static void make_case_word(char_u *fword, char_u *cword, int flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100364static int similar_chars(slang_T *slang, int c1, int c2);
365static 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);
366static void check_suggestions(suginfo_T *su, garray_T *gap);
367static void add_banned(suginfo_T *su, char_u *word);
368static void rescore_suggestions(suginfo_T *su);
369static void rescore_one(suginfo_T *su, suggest_T *stp);
370static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100371static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
372static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100373static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100374static int soundalike_score(char_u *goodsound, char_u *badsound);
375static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
376static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100377static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100378static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
379static 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 +0000380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000381
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000382/* Remember what "z?" replaced. */
383static char_u *repl_from = NULL;
384static char_u *repl_to = NULL;
385
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000386/*
387 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000388 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000389 * "*attrp" is set to the highlight index for a badly spelled word. For a
390 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000391 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000393 * "capcol" is used to check for a Capitalised word after the end of a
394 * sentence. If it's zero then perform the check. Return the column where to
395 * check next, or -1 when no sentence end was found. If it's NULL then don't
396 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000397 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000398 * Returns the length of the word in bytes, also when it's OK, so that the
399 * caller can skip over the word.
400 */
401 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100402spell_check(
403 win_T *wp, /* current window */
404 char_u *ptr,
405 hlf_T *attrp,
406 int *capcol, /* column to check for Capital */
407 int docount) /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408{
409 matchinf_T mi; /* Most things are put in "mi" so that it can
410 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000411 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000412 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000413 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000414 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000415 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000416
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000417 /* A word never starts at a space or a control character. Return quickly
418 * then, skipping over the character. */
419 if (*ptr <= ' ')
420 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000421
422 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200423 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000424 return 1;
425
Bram Moolenaar5195e452005-08-19 20:32:47 +0000426 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000428 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000429 * 0X99FF. But always do check spelling to find "3GPP" and "11
430 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000432 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100433 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
434 mi.mi_end = skipbin(ptr + 2);
435 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000436 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000437 else
438 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000439 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000440 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441
Bram Moolenaar0c405862005-06-22 22:26:26 +0000442 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000443 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000444 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200445 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000446 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000447 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000448 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100449 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200450 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000451
Bram Moolenaar860cae12010-06-05 23:22:07 +0200452 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000453 {
454 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000455 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000456 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000457 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000458 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000459 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000460 if (capcol != NULL)
461 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000462
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463 /* We always use the characters up to the next non-word character,
464 * also for bad words. */
465 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000466
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200468 mi.mi_capflags = 0;
469 mi.mi_cend = NULL;
470 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471
Bram Moolenaar5195e452005-08-19 20:32:47 +0000472 /* case-fold the word with one non-word character, so that we can check
473 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100475 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000476
477 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
478 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000479 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000480
481 /* The word is bad unless we recognize it. */
482 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000483 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000484
485 /*
486 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000487 * We check them all, because a word may be matched longer in another
488 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200490 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200492 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000493
494 /* If reloading fails the language is still in the list but everything
495 * has been cleared. */
496 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
497 continue;
498
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000499 /* Check for a matching word in case-folded words. */
500 find_word(&mi, FIND_FOLDWORD);
501
502 /* Check for a matching word in keep-case words. */
503 find_word(&mi, FIND_KEEPWORD);
504
505 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000506 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000507
508 /* For a NOBREAK language, may want to use a word without a following
509 * word as a backup. */
510 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
511 && mi.mi_result2 != SP_BAD)
512 {
513 mi.mi_result = mi.mi_result2;
514 mi.mi_end = mi.mi_end2;
515 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516
517 /* Count the word in the first language where it's found to be OK. */
518 if (count_word && mi.mi_result == SP_OK)
519 {
520 count_common_word(mi.mi_lp->lp_slang, ptr,
521 (int)(mi.mi_end - ptr), 1);
522 count_word = FALSE;
523 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524 }
525
526 if (mi.mi_result != SP_OK)
527 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000528 /* If we found a number skip over it. Allows for "42nd". Do flag
529 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000530 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000531 {
532 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
533 return nrlen;
534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000535
536 /* When we are at a non-word character there is no error, just
537 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100538 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200540 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000541 {
542 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100543 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000544
545 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200546 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000547 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100548 r = vim_regexec(&regmatch, ptr, 0);
549 wp->w_s->b_cap_prog = regmatch.regprog;
550 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000551 *capcol = (int)(regmatch.endp[0] - ptr);
552 }
553
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000554 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000555 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000556 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000557 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000558 else if (mi.mi_end == ptr)
559 /* Always include at least one character. Required for when there
560 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100561 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000562 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200563 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000564 {
565 char_u *p, *fp;
566 int save_result = mi.mi_result;
567
568 /* First language in 'spelllang' is NOBREAK. Find first position
569 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200570 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000571 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000572 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000573 p = mi.mi_word;
574 fp = mi.mi_fword;
575 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000576 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100577 MB_PTR_ADV(p);
578 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000579 if (p >= mi.mi_end)
580 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000581 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000582 find_word(&mi, FIND_COMPOUND);
583 if (mi.mi_result != SP_BAD)
584 {
585 mi.mi_end = p;
586 break;
587 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000588 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000589 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000590 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000591 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592
593 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000594 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000596 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000598 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000599 }
600
Bram Moolenaar5195e452005-08-19 20:32:47 +0000601 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
602 {
603 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000604 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000605 return wrongcaplen;
606 }
607
Bram Moolenaar51485f02005-06-04 21:55:20 +0000608 return (int)(mi.mi_end - ptr);
609}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000610
Bram Moolenaar51485f02005-06-04 21:55:20 +0000611/*
612 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000613 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
614 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
615 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
616 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000617 *
618 * For a match mip->mi_result is updated.
619 */
620 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100621find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000622{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000624 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000625 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000626 int endidxcnt = 0;
627 int len;
628 int wlen = 0;
629 int flen;
630 int c;
631 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000632 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000633 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000634 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000635 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000636 slang_T *slang = mip->mi_lp->lp_slang;
637 unsigned flags;
638 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000640 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000641 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000642 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000643
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000644 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000645 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000646 /* Check for word with matching case in keep-case tree. */
647 ptr = mip->mi_word;
648 flen = 9999; /* no case folding, always enough bytes */
649 byts = slang->sl_kbyts;
650 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000651
652 if (mode == FIND_KEEPCOMPOUND)
653 /* Skip over the previously found word(s). */
654 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000655 }
656 else
657 {
658 /* Check for case-folded in case-folded tree. */
659 ptr = mip->mi_fword;
660 flen = mip->mi_fwordlen; /* available case-folded bytes */
661 byts = slang->sl_fbyts;
662 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000663
664 if (mode == FIND_PREFIX)
665 {
666 /* Skip over the prefix. */
667 wlen = mip->mi_prefixlen;
668 flen -= mip->mi_prefixlen;
669 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670 else if (mode == FIND_COMPOUND)
671 {
672 /* Skip over the previously found word(s). */
673 wlen = mip->mi_compoff;
674 flen -= mip->mi_compoff;
675 }
676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677 }
678
Bram Moolenaar51485f02005-06-04 21:55:20 +0000679 if (byts == NULL)
680 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000681
Bram Moolenaar51485f02005-06-04 21:55:20 +0000682 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000683 * Repeat advancing in the tree until:
684 * - there is a byte that doesn't match,
685 * - we reach the end of the tree,
686 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000687 */
688 for (;;)
689 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000690 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000691 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692
693 len = byts[arridx++];
694
695 /* If the first possible byte is a zero the word could end here.
696 * Remember this index, we first check for the longest word. */
697 if (byts[arridx] == 0)
698 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000699 if (endidxcnt == MAXWLEN)
700 {
701 /* Must be a corrupted spell file. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100702 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703 return;
704 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 endlen[endidxcnt] = wlen;
706 endidx[endidxcnt++] = arridx++;
707 --len;
708
709 /* Skip over the zeros, there can be several flag/region
710 * combinations. */
711 while (len > 0 && byts[arridx] == 0)
712 {
713 ++arridx;
714 --len;
715 }
716 if (len == 0)
717 break; /* no children, word must end here */
718 }
719
720 /* Stop looking at end of the line. */
721 if (ptr[wlen] == NUL)
722 break;
723
724 /* Perform a binary search in the list of accepted bytes. */
725 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000726 if (c == TAB) /* <Tab> is handled like <Space> */
727 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000728 lo = arridx;
729 hi = arridx + len - 1;
730 while (lo < hi)
731 {
732 m = (lo + hi) / 2;
733 if (byts[m] > c)
734 hi = m - 1;
735 else if (byts[m] < c)
736 lo = m + 1;
737 else
738 {
739 lo = hi = m;
740 break;
741 }
742 }
743
744 /* Stop if there is no matching byte. */
745 if (hi < lo || byts[lo] != c)
746 break;
747
748 /* Continue at the child (if there is one). */
749 arridx = idxs[lo];
750 ++wlen;
751 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000752
753 /* One space in the good word may stand for several spaces in the
754 * checked word. */
755 if (c == ' ')
756 {
757 for (;;)
758 {
759 if (flen <= 0 && *mip->mi_fend != NUL)
760 flen = fold_more(mip);
761 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
762 break;
763 ++wlen;
764 --flen;
765 }
766 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000767 }
768
769 /*
770 * Verify that one of the possible endings is valid. Try the longest
771 * first.
772 */
773 while (endidxcnt > 0)
774 {
775 --endidxcnt;
776 arridx = endidx[endidxcnt];
777 wlen = endlen[endidxcnt];
778
Bram Moolenaar51485f02005-06-04 21:55:20 +0000779 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
780 continue; /* not at first byte of character */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200781 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000782 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000783 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000784 continue; /* next char is a word character */
785 word_ends = FALSE;
786 }
787 else
788 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000789 /* The prefix flag is before compound flags. Once a valid prefix flag
790 * has been found we try compound flags. */
791 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000792
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000793 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000794 {
795 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000796 * when folding case. This can be slow, take a shortcut when the
797 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000798 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000799 if (STRNCMP(ptr, p, wlen) != 0)
800 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100801 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
802 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000803 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000804 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000805 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000806
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000807 /* Check flags and region. For FIND_PREFIX check the condition and
808 * prefix ID.
809 * Repeat this if there are more flags/region alternatives until there
810 * is a match. */
811 res = SP_BAD;
812 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
813 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000814 {
815 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000816
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000817 /* For the fold-case tree check that the case of the checked word
818 * matches with what the word in the tree requires.
819 * For keep-case tree the case is always right. For prefixes we
820 * don't bother to check. */
821 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000823 if (mip->mi_cend != mip->mi_word + wlen)
824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000825 /* mi_capflags was set for a different word length, need
826 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000827 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000828 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000829 }
830
Bram Moolenaar0c405862005-06-22 22:26:26 +0000831 if (mip->mi_capflags == WF_KEEPCAP
832 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 }
835
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000836 /* When mode is FIND_PREFIX the word must support the prefix:
837 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000838 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000839 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000840 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000841 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000842 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000843 mip->mi_word + mip->mi_cprefixlen, slang,
844 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000845 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000846 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000847
848 /* Use the WF_RARE flag for a rare prefix. */
849 if (c & WF_RAREPFX)
850 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000851 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000852 }
853
Bram Moolenaar78622822005-08-23 21:00:13 +0000854 if (slang->sl_nobreak)
855 {
856 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
857 && (flags & WF_BANNED) == 0)
858 {
859 /* NOBREAK: found a valid following word. That's all we
860 * need to know, so return. */
861 mip->mi_result = SP_OK;
862 break;
863 }
864 }
865
866 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
867 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000868 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000869 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870 * COMPOUNDMIN reject it quickly.
871 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000872 * that's too short... Myspell compatibility requires this
873 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000874 if (((unsigned)flags >> 24) == 0
875 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000876 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000877 /* For multi-byte chars check character length against
878 * COMPOUNDMIN. */
879 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000880 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000881 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
882 wlen - mip->mi_compoff) < slang->sl_compminlen)
883 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000884
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000885 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000886 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000887 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
888 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000889 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000890 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000891
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000892 /* Don't allow compounding on a side where an affix was added,
893 * unless COMPOUNDPERMITFLAG was used. */
894 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
895 continue;
896 if (!word_ends && (flags & WF_NOCOMPAFT))
897 continue;
898
Bram Moolenaard12a1322005-08-21 22:08:24 +0000899 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000900 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000901 ? slang->sl_compstartflags
902 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000903 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000904 continue;
905
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000906 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
907 * discard the compound word. */
908 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
909 continue;
910
Bram Moolenaare52325c2005-08-22 22:54:29 +0000911 if (mode == FIND_COMPOUND)
912 {
913 int capflags;
914
915 /* Need to check the caps type of the appended compound
916 * word. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000917 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
918 mip->mi_compoff) != 0)
919 {
920 /* case folding may have changed the length */
921 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100922 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
923 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000924 }
925 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000926 p = mip->mi_word + mip->mi_compoff;
927 capflags = captype(p, mip->mi_word + wlen);
928 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
929 && (flags & WF_FIXCAP) != 0))
930 continue;
931
932 if (capflags != WF_ALLCAP)
933 {
934 /* When the character before the word is a word
935 * character we do not accept a Onecap word. We do
936 * accept a no-caps word, even when the dictionary
937 * word specifies ONECAP. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100938 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100939 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000940 ? capflags == WF_ONECAP
941 : (flags & WF_ONECAP) != 0
942 && capflags != WF_ONECAP)
943 continue;
944 }
945 }
946
Bram Moolenaar5195e452005-08-19 20:32:47 +0000947 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000948 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000949 * the number of syllables must not be too large. */
950 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
951 mip->mi_compflags[mip->mi_complen + 1] = NUL;
952 if (word_ends)
953 {
954 char_u fword[MAXWLEN];
955
956 if (slang->sl_compsylmax < MAXWLEN)
957 {
958 /* "fword" is only needed for checking syllables. */
959 if (ptr == mip->mi_word)
960 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
961 else
962 vim_strncpy(fword, ptr, endlen[endidxcnt]);
963 }
964 if (!can_compound(slang, fword, mip->mi_compflags))
965 continue;
966 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000967 else if (slang->sl_comprules != NULL
968 && !match_compoundrule(slang, mip->mi_compflags))
969 /* The compound flags collected so far do not match any
970 * COMPOUNDRULE, discard the compounded word. */
971 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000972 }
973
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000974 /* Check NEEDCOMPOUND: can't use word without compounding. */
975 else if (flags & WF_NEEDCOMP)
976 continue;
977
Bram Moolenaar78622822005-08-23 21:00:13 +0000978 nobreak_result = SP_OK;
979
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000980 if (!word_ends)
981 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000982 int save_result = mip->mi_result;
983 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000984 langp_T *save_lp = mip->mi_lp;
985 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000986
987 /* Check that a valid word follows. If there is one and we
988 * are compounding, it will set "mi_result", thus we are
989 * always finished here. For NOBREAK we only check that a
990 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000991 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +0000992 if (slang->sl_nobreak)
993 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000994
995 /* Find following word in case-folded tree. */
996 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000997 if (has_mbyte && mode == FIND_KEEPWORD)
998 {
999 /* Compute byte length in case-folded word from "wlen":
1000 * byte length in keep-case word. Length may change when
1001 * folding case. This can be slow, take a shortcut when
1002 * the case-folded word is equal to the keep-case word. */
1003 p = mip->mi_fword;
1004 if (STRNCMP(ptr, p, wlen) != 0)
1005 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001006 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
1007 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001008 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001009 }
1010 }
Bram Moolenaarba534352016-04-21 09:20:26 +02001011#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001012 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001013#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001014 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001015 if (flags & WF_COMPROOT)
1016 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001017
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001018 /* For NOBREAK we need to try all NOBREAK languages, at least
1019 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001020 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001021 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001022 if (slang->sl_nobreak)
1023 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001024 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001025 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1026 || !mip->mi_lp->lp_slang->sl_nobreak)
1027 continue;
1028 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001029
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001030 find_word(mip, FIND_COMPOUND);
1031
1032 /* When NOBREAK any word that matches is OK. Otherwise we
1033 * need to find the longest match, thus try with keep-case
1034 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001035 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1036 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001037 /* Find following word in keep-case tree. */
1038 mip->mi_compoff = wlen;
1039 find_word(mip, FIND_KEEPCOMPOUND);
1040
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001041#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1042 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1043 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001044 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1045 {
1046 /* Check for following word with prefix. */
1047 mip->mi_compoff = c;
1048 find_prefix(mip, FIND_COMPOUND);
1049 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001050#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001051 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001052
1053 if (!slang->sl_nobreak)
1054 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001055 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001056 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001057 if (flags & WF_COMPROOT)
1058 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001059 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001060
Bram Moolenaar78622822005-08-23 21:00:13 +00001061 if (slang->sl_nobreak)
1062 {
1063 nobreak_result = mip->mi_result;
1064 mip->mi_result = save_result;
1065 mip->mi_end = save_end;
1066 }
1067 else
1068 {
1069 if (mip->mi_result == SP_OK)
1070 break;
1071 continue;
1072 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001073 }
1074
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001075 if (flags & WF_BANNED)
1076 res = SP_BANNED;
1077 else if (flags & WF_REGION)
1078 {
1079 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001080 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001081 res = SP_OK;
1082 else
1083 res = SP_LOCAL;
1084 }
1085 else if (flags & WF_RARE)
1086 res = SP_RARE;
1087 else
1088 res = SP_OK;
1089
Bram Moolenaar78622822005-08-23 21:00:13 +00001090 /* Always use the longest match and the best result. For NOBREAK
1091 * we separately keep the longest match without a following good
1092 * word as a fall-back. */
1093 if (nobreak_result == SP_BAD)
1094 {
1095 if (mip->mi_result2 > res)
1096 {
1097 mip->mi_result2 = res;
1098 mip->mi_end2 = mip->mi_word + wlen;
1099 }
1100 else if (mip->mi_result2 == res
1101 && mip->mi_end2 < mip->mi_word + wlen)
1102 mip->mi_end2 = mip->mi_word + wlen;
1103 }
1104 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001105 {
1106 mip->mi_result = res;
1107 mip->mi_end = mip->mi_word + wlen;
1108 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001109 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001110 mip->mi_end = mip->mi_word + wlen;
1111
Bram Moolenaar78622822005-08-23 21:00:13 +00001112 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001114 }
1115
Bram Moolenaar78622822005-08-23 21:00:13 +00001116 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001117 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001119}
1120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001121/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001122 * Return TRUE if there is a match between the word ptr[wlen] and
1123 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1124 * word.
1125 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1126 * end of ptr[wlen] and the second part matches after it.
1127 */
1128 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001129match_checkcompoundpattern(
1130 char_u *ptr,
1131 int wlen,
1132 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001133{
1134 int i;
1135 char_u *p;
1136 int len;
1137
1138 for (i = 0; i + 1 < gap->ga_len; i += 2)
1139 {
1140 p = ((char_u **)gap->ga_data)[i + 1];
1141 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1142 {
1143 /* Second part matches at start of following compound word, now
1144 * check if first part matches at end of previous word. */
1145 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001146 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001147 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1148 return TRUE;
1149 }
1150 }
1151 return FALSE;
1152}
1153
1154/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001155 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1156 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001157 */
1158 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001159can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001160{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001161 char_u uflags[MAXWLEN * 2];
1162 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001163 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001164
1165 if (slang->sl_compprog == NULL)
1166 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001167 if (enc_utf8)
1168 {
1169 /* Need to convert the single byte flags to utf8 characters. */
1170 p = uflags;
1171 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001172 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001173 *p = NUL;
1174 p = uflags;
1175 }
1176 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00001177 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001178 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001179 return FALSE;
1180
Bram Moolenaare52325c2005-08-22 22:54:29 +00001181 /* Count the number of syllables. This may be slow, do it last. If there
1182 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001183 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001184 if (slang->sl_compsylmax < MAXWLEN
1185 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001186 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001187 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001188}
1189
1190/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001191 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1192 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1193 * lines if they don't contain wildcards.
1194 */
1195 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001196can_be_compound(
1197 trystate_T *sp,
1198 slang_T *slang,
1199 char_u *compflags,
1200 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001201{
1202 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1203 * then it can't possibly compound. */
1204 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1205 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1206 return FALSE;
1207
1208 /* If there are no wildcards, we can check if the flags collected so far
1209 * possibly can form a match with COMPOUNDRULE patterns. This only
1210 * makes sense when we have two or more words. */
1211 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1212 {
1213 int v;
1214
1215 compflags[sp->ts_complen] = flag;
1216 compflags[sp->ts_complen + 1] = NUL;
1217 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1218 compflags[sp->ts_complen] = NUL;
1219 return v;
1220 }
1221
1222 return TRUE;
1223}
1224
1225
1226/*
1227 * Return TRUE if the compound flags in compflags[] match the start of any
1228 * compound rule. This is used to stop trying a compound if the flags
1229 * collected so far can't possibly match any compound rule.
1230 * Caller must check that slang->sl_comprules is not NULL.
1231 */
1232 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001233match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001234{
1235 char_u *p;
1236 int i;
1237 int c;
1238
1239 /* loop over all the COMPOUNDRULE entries */
1240 for (p = slang->sl_comprules; *p != NUL; ++p)
1241 {
1242 /* loop over the flags in the compound word we have made, match
1243 * them against the current rule entry */
1244 for (i = 0; ; ++i)
1245 {
1246 c = compflags[i];
1247 if (c == NUL)
1248 /* found a rule that matches for the flags we have so far */
1249 return TRUE;
1250 if (*p == '/' || *p == NUL)
1251 break; /* end of rule, it's too short */
1252 if (*p == '[')
1253 {
1254 int match = FALSE;
1255
1256 /* compare against all the flags in [] */
1257 ++p;
1258 while (*p != ']' && *p != NUL)
1259 if (*p++ == c)
1260 match = TRUE;
1261 if (!match)
1262 break; /* none matches */
1263 }
1264 else if (*p != c)
1265 break; /* flag of word doesn't match flag in pattern */
1266 ++p;
1267 }
1268
1269 /* Skip to the next "/", where the next pattern starts. */
1270 p = vim_strchr(p, '/');
1271 if (p == NULL)
1272 break;
1273 }
1274
1275 /* Checked all the rules and none of them match the flags, so there
1276 * can't possibly be a compound starting with these flags. */
1277 return FALSE;
1278}
1279
1280/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001281 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1282 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001283 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001284 */
1285 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001286valid_word_prefix(
1287 int totprefcnt, /* nr of prefix IDs */
1288 int arridx, /* idx in sl_pidxs[] */
1289 int flags,
1290 char_u *word,
1291 slang_T *slang,
1292 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001293{
1294 int prefcnt;
1295 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001296 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001297 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001298
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001299 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001300 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1301 {
1302 pidx = slang->sl_pidxs[arridx + prefcnt];
1303
1304 /* Check the prefix ID. */
1305 if (prefid != (pidx & 0xff))
1306 continue;
1307
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001308 /* Check if the prefix doesn't combine and the word already has a
1309 * suffix. */
1310 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1311 continue;
1312
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001313 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001314 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001315 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1316 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001317 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001318 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001319 continue;
1320 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001321 else if (cond_req)
1322 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001323
Bram Moolenaar53805d12005-08-01 07:08:33 +00001324 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001325 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001326 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001327 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001328}
1329
1330/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001331 * Check if the word at "mip->mi_word" has a matching prefix.
1332 * If it does, then check the following word.
1333 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001334 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1335 * prefix in a compound word.
1336 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001337 * For a match mip->mi_result is updated.
1338 */
1339 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001340find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001341{
1342 idx_T arridx = 0;
1343 int len;
1344 int wlen = 0;
1345 int flen;
1346 int c;
1347 char_u *ptr;
1348 idx_T lo, hi, m;
1349 slang_T *slang = mip->mi_lp->lp_slang;
1350 char_u *byts;
1351 idx_T *idxs;
1352
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001353 byts = slang->sl_pbyts;
1354 if (byts == NULL)
1355 return; /* array is empty */
1356
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001357 /* We use the case-folded word here, since prefixes are always
1358 * case-folded. */
1359 ptr = mip->mi_fword;
1360 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001361 if (mode == FIND_COMPOUND)
1362 {
1363 /* Skip over the previously found word(s). */
1364 ptr += mip->mi_compoff;
1365 flen -= mip->mi_compoff;
1366 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001367 idxs = slang->sl_pidxs;
1368
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001369 /*
1370 * Repeat advancing in the tree until:
1371 * - there is a byte that doesn't match,
1372 * - we reach the end of the tree,
1373 * - or we reach the end of the line.
1374 */
1375 for (;;)
1376 {
1377 if (flen == 0 && *mip->mi_fend != NUL)
1378 flen = fold_more(mip);
1379
1380 len = byts[arridx++];
1381
1382 /* If the first possible byte is a zero the prefix could end here.
1383 * Check if the following word matches and supports the prefix. */
1384 if (byts[arridx] == 0)
1385 {
1386 /* There can be several prefixes with different conditions. We
1387 * try them all, since we don't know which one will give the
1388 * longest match. The word is the same each time, pass the list
1389 * of possible prefixes to find_word(). */
1390 mip->mi_prefarridx = arridx;
1391 mip->mi_prefcnt = len;
1392 while (len > 0 && byts[arridx] == 0)
1393 {
1394 ++arridx;
1395 --len;
1396 }
1397 mip->mi_prefcnt -= len;
1398
1399 /* Find the word that comes after the prefix. */
1400 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001401 if (mode == FIND_COMPOUND)
1402 /* Skip over the previously found word(s). */
1403 mip->mi_prefixlen += mip->mi_compoff;
1404
Bram Moolenaar53805d12005-08-01 07:08:33 +00001405 if (has_mbyte)
1406 {
1407 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001408 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1409 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001410 }
1411 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001412 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 find_word(mip, FIND_PREFIX);
1414
1415
1416 if (len == 0)
1417 break; /* no children, word must end here */
1418 }
1419
1420 /* Stop looking at end of the line. */
1421 if (ptr[wlen] == NUL)
1422 break;
1423
1424 /* Perform a binary search in the list of accepted bytes. */
1425 c = ptr[wlen];
1426 lo = arridx;
1427 hi = arridx + len - 1;
1428 while (lo < hi)
1429 {
1430 m = (lo + hi) / 2;
1431 if (byts[m] > c)
1432 hi = m - 1;
1433 else if (byts[m] < c)
1434 lo = m + 1;
1435 else
1436 {
1437 lo = hi = m;
1438 break;
1439 }
1440 }
1441
1442 /* Stop if there is no matching byte. */
1443 if (hi < lo || byts[lo] != c)
1444 break;
1445
1446 /* Continue at the child (if there is one). */
1447 arridx = idxs[lo];
1448 ++wlen;
1449 --flen;
1450 }
1451}
1452
1453/*
1454 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001455 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001456 * Return the length of the folded chars in bytes.
1457 */
1458 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001459fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460{
1461 int flen;
1462 char_u *p;
1463
1464 p = mip->mi_fend;
1465 do
1466 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001467 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001468 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001470 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001471 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001472 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473
1474 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1475 mip->mi_fword + mip->mi_fwordlen,
1476 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001477 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001478 mip->mi_fwordlen += flen;
1479 return flen;
1480}
1481
1482/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001483 * Check case flags for a word. Return TRUE if the word has the requested
1484 * case.
1485 */
1486 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001487spell_valid_case(
1488 int wordflags, /* flags for the checked word. */
1489 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001491 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001492 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001493 && ((treeflags & WF_ONECAP) == 0
1494 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495}
1496
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001497/*
1498 * Return TRUE if spell checking is not enabled.
1499 */
1500 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001501no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001502{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001503 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1504 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001506 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001507 return TRUE;
1508 }
1509 return FALSE;
1510}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001511
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001512/*
1513 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001514 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1515 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001516 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1517 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001518 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001519 */
1520 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001521spell_move_to(
1522 win_T *wp,
1523 int dir, /* FORWARD or BACKWARD */
1524 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
1525 int curline,
1526 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001527 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001528{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001529 linenr_T lnum;
1530 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001531 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001532 char_u *line;
1533 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001534 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001535 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001536 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001537#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001538 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001539#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001541 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001542 char_u *buf = NULL;
1543 int buflen = 0;
1544 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001545 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001546 int found_one = FALSE;
1547 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001548
Bram Moolenaar95529562005-08-25 21:21:38 +00001549 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001550 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001551
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001552 /*
1553 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001554 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001555 *
1556 * When searching backwards, we continue in the line to find the last
1557 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001558 *
1559 * We concatenate the start of the next line, so that wrapped words work
1560 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1561 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001562 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001563 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001564 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001565
1566 while (!got_int)
1567 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001568 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001569
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001570 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001571 if (buflen < len + MAXWLEN + 2)
1572 {
1573 vim_free(buf);
1574 buflen = len + MAXWLEN + 2;
1575 buf = alloc(buflen);
1576 if (buf == NULL)
1577 break;
1578 }
1579
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001580 /* In first line check first word for Capital. */
1581 if (lnum == 1)
1582 capcol = 0;
1583
1584 /* For checking first word with a capital skip white space. */
1585 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001586 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001587 else if (curline && wp == curwin)
1588 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001589 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001590 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001591 if (check_need_cap(lnum, col))
1592 capcol = col;
1593
1594 /* Need to get the line again, may have looked at the previous
1595 * one. */
1596 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1597 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001598
Bram Moolenaar0c405862005-06-22 22:26:26 +00001599 /* Copy the line into "buf" and append the start of the next line if
1600 * possible. */
1601 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001602 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001603 spell_cat_line(buf + STRLEN(buf),
1604 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001605
1606 p = buf + skip;
1607 endp = buf + len;
1608 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001609 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001610 /* When searching backward don't search after the cursor. Unless
1611 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001612 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001613 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001614 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001615 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001616 break;
1617
1618 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001619 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001620 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001621
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001622 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001623 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001624 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001625 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001626 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001627 /* When searching forward only accept a bad word after
1628 * the cursor. */
1629 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001630 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001631 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001632 && (wrapped
1633 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001634 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001635 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001637#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001638 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001639 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001640 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001641 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001642 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001643 if (!can_spell)
1644 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001645 }
1646 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001647#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001648 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001649
Bram Moolenaar51485f02005-06-04 21:55:20 +00001650 if (can_spell)
1651 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001652 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001653 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001654 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001655 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001656 if (dir == FORWARD)
1657 {
1658 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001659 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001660 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001661 if (attrp != NULL)
1662 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001663 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001664 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001665 else if (curline)
1666 /* Insert mode completion: put cursor after
1667 * the bad word. */
1668 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001669 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001670 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001672 else
1673 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001674 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001675 }
1676
Bram Moolenaar51485f02005-06-04 21:55:20 +00001677 /* advance to character after the word */
1678 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001679 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001680 }
1681
Bram Moolenaar5195e452005-08-19 20:32:47 +00001682 if (dir == BACKWARD && found_pos.lnum != 0)
1683 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001684 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001685 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001686 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001687 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001688 }
1689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001690 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001691 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001692
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001693 /* If we are back at the starting line and searched it again there
1694 * is no match, give up. */
1695 if (lnum == wp->w_cursor.lnum && wrapped)
1696 break;
1697
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001698 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001699 if (dir == BACKWARD)
1700 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001701 if (lnum > 1)
1702 --lnum;
1703 else if (!p_ws)
1704 break; /* at first line and 'nowrapscan' */
1705 else
1706 {
1707 /* Wrap around to the end of the buffer. May search the
1708 * starting line again and accept the last match. */
1709 lnum = wp->w_buffer->b_ml.ml_line_count;
1710 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001711 if (!shortmess(SHM_SEARCH))
1712 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001713 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001714 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001715 }
1716 else
1717 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001718 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1719 ++lnum;
1720 else if (!p_ws)
1721 break; /* at first line and 'nowrapscan' */
1722 else
1723 {
1724 /* Wrap around to the start of the buffer. May search the
1725 * starting line again and accept the first match. */
1726 lnum = 1;
1727 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001728 if (!shortmess(SHM_SEARCH))
1729 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001730 }
1731
1732 /* If we are back at the starting line and there is no match then
1733 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001734 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001735 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001736
1737 /* Skip the characters at the start of the next line that were
1738 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001739 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001740 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001741 else
1742 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001743
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001744 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001745 --capcol;
1746
1747 /* But after empty line check first word in next line */
1748 if (*skipwhite(line) == NUL)
1749 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001750 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001751
1752 line_breakcheck();
1753 }
1754
Bram Moolenaar0c405862005-06-22 22:26:26 +00001755 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001756 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001757}
1758
1759/*
1760 * For spell checking: concatenate the start of the following line "line" into
1761 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001762 * Keep the blanks at the start of the next line, this is used in win_line()
1763 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001764 */
1765 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001766spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001767{
1768 char_u *p;
1769 int n;
1770
1771 p = skipwhite(line);
1772 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1773 p = skipwhite(p + 1);
1774
1775 if (*p != NUL)
1776 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001777 /* Only worth concatenating if there is something else than spaces to
1778 * concatenate. */
1779 n = (int)(p - line) + 1;
1780 if (n < maxlen - 1)
1781 {
1782 vim_memset(buf, ' ', n);
1783 vim_strncpy(buf + n, p, maxlen - 1 - n);
1784 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001785 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001786}
1787
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001788/*
1789 * Structure used for the cookie argument of do_in_runtimepath().
1790 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001791typedef struct spelload_S
1792{
1793 char_u sl_lang[MAXWLEN + 1]; /* language name */
1794 slang_T *sl_slang; /* resulting slang_T struct */
1795 int sl_nobreak; /* NOBREAK language found */
1796} spelload_T;
1797
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001798/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001799 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001800 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001801 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001802 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001803spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001805 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001807 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001808 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001809
Bram Moolenaarb765d632005-06-07 21:00:02 +00001810 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001811 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001812 STRCPY(sl.sl_lang, lang);
1813 sl.sl_slang = NULL;
1814 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001815
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001816 /* We may retry when no spell file is found for the language, an
1817 * autocommand may load it then. */
1818 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001819 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001820 /*
1821 * Find the first spell file for "lang" in 'runtimepath' and load it.
1822 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001823 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001824#ifdef VMS
1825 "spell/%s_%s.spl",
1826#else
1827 "spell/%s.%s.spl",
1828#endif
1829 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001830 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001831
1832 if (r == FAIL && *sl.sl_lang != NUL)
1833 {
1834 /* Try loading the ASCII version. */
1835 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001836#ifdef VMS
1837 "spell/%s_ascii.spl",
1838#else
1839 "spell/%s.ascii.spl",
1840#endif
1841 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001842 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001843
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001844 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1845 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1846 curbuf->b_fname, FALSE, curbuf))
1847 continue;
1848 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001849 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001850 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001851 }
1852
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001853 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001855 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001856#ifdef VMS
1857 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1858#else
1859 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1860#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001861 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001862 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001863 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001864 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001865 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001866 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001867 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001868 }
1869}
1870
1871/*
1872 * Return the encoding used for spell checking: Use 'encoding', except that we
1873 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1874 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001875 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001876spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001877{
1878
Bram Moolenaarb765d632005-06-07 21:00:02 +00001879 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1880 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001881 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001882}
1883
1884/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001885 * Get the name of the .spl file for the internal wordlist into
1886 * "fname[MAXPATHL]".
1887 */
1888 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001889int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001890{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001891 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001892 int_wordlist, spell_enc());
1893}
1894
1895/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001896 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001897 * Caller must fill "sl_next".
1898 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001899 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001900slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001901{
1902 slang_T *lp;
1903
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001905 if (lp != NULL)
1906 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001907 if (lang != NULL)
1908 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001909 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001910 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001911 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001912 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001913 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001914 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001915
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001916 return lp;
1917}
1918
1919/*
1920 * Free the contents of an slang_T and the structure itself.
1921 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001922 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001923slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001924{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001925 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001926 vim_free(lp->sl_fname);
1927 slang_clear(lp);
1928 vim_free(lp);
1929}
1930
1931/*
1932 * Clear an slang_T so that the file can be reloaded.
1933 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001934 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001935slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001936{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001937 garray_T *gap;
1938 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001939 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001940 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001941 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001942
Bram Moolenaard23a8232018-02-10 18:45:26 +01001943 VIM_CLEAR(lp->sl_fbyts);
1944 VIM_CLEAR(lp->sl_kbyts);
1945 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001946
Bram Moolenaard23a8232018-02-10 18:45:26 +01001947 VIM_CLEAR(lp->sl_fidxs);
1948 VIM_CLEAR(lp->sl_kidxs);
1949 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001950
Bram Moolenaar4770d092006-01-12 23:22:24 +00001951 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001952 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001953 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1954 while (gap->ga_len > 0)
1955 {
1956 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1957 vim_free(ftp->ft_from);
1958 vim_free(ftp->ft_to);
1959 }
1960 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001961 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001962
1963 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001964 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001965 {
1966 /* "ga_len" is set to 1 without adding an item for latin1 */
1967 if (gap->ga_data != NULL)
1968 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1969 for (i = 0; i < gap->ga_len; ++i)
1970 vim_free(((int **)gap->ga_data)[i]);
1971 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001972 else
1973 /* SAL items: free salitem_T items */
1974 while (gap->ga_len > 0)
1975 {
1976 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1977 vim_free(smp->sm_lead);
1978 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1979 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001980 vim_free(smp->sm_lead_w);
1981 vim_free(smp->sm_oneof_w);
1982 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001983 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001984 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001985
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001986 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001987 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001988 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001989 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001990
Bram Moolenaard23a8232018-02-10 18:45:26 +01001991 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001992
Bram Moolenaard23a8232018-02-10 18:45:26 +01001993 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001994
Bram Moolenaar473de612013-06-08 18:19:48 +02001995 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001996 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001997 VIM_CLEAR(lp->sl_comprules);
1998 VIM_CLEAR(lp->sl_compstartflags);
1999 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002000
Bram Moolenaard23a8232018-02-10 18:45:26 +01002001 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002002 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002003
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002004 ga_clear_strings(&lp->sl_comppat);
2005
Bram Moolenaar4770d092006-01-12 23:22:24 +00002006 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2007 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002008
Bram Moolenaar4770d092006-01-12 23:22:24 +00002009 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002010
Bram Moolenaar4770d092006-01-12 23:22:24 +00002011 /* Clear info from .sug file. */
2012 slang_clear_sug(lp);
2013
Bram Moolenaar5195e452005-08-19 20:32:47 +00002014 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002015 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002016 lp->sl_compsylmax = MAXWLEN;
2017 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002018}
2019
2020/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002021 * Clear the info from the .sug file in "lp".
2022 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002023 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002024slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002025{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002026 VIM_CLEAR(lp->sl_sbyts);
2027 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002028 close_spellbuf(lp->sl_sugbuf);
2029 lp->sl_sugbuf = NULL;
2030 lp->sl_sugloaded = FALSE;
2031 lp->sl_sugtime = 0;
2032}
2033
2034/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002035 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002036 * Invoked through do_in_runtimepath().
2037 */
2038 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002039spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002040{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002041 spelload_T *slp = (spelload_T *)cookie;
2042 slang_T *slang;
2043
2044 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2045 if (slang != NULL)
2046 {
2047 /* When a previously loaded file has NOBREAK also use it for the
2048 * ".add" files. */
2049 if (slp->sl_nobreak && slang->sl_add)
2050 slang->sl_nobreak = TRUE;
2051 else if (slang->sl_nobreak)
2052 slp->sl_nobreak = TRUE;
2053
2054 slp->sl_slang = slang;
2055 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002056}
2057
Bram Moolenaar4770d092006-01-12 23:22:24 +00002058
2059/*
2060 * Add a word to the hashtable of common words.
2061 * If it's already there then the counter is increased.
2062 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002063 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002064count_common_word(
2065 slang_T *lp,
2066 char_u *word,
2067 int len, /* word length, -1 for upto NUL */
2068 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002069{
2070 hash_T hash;
2071 hashitem_T *hi;
2072 wordcount_T *wc;
2073 char_u buf[MAXWLEN];
2074 char_u *p;
2075
2076 if (len == -1)
2077 p = word;
2078 else
2079 {
2080 vim_strncpy(buf, word, len);
2081 p = buf;
2082 }
2083
2084 hash = hash_hash(p);
2085 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2086 if (HASHITEM_EMPTY(hi))
2087 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002089 if (wc == NULL)
2090 return;
2091 STRCPY(wc->wc_word, p);
2092 wc->wc_count = count;
2093 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2094 }
2095 else
2096 {
2097 wc = HI2WC(hi);
2098 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2099 wc->wc_count = MAXWORDCOUNT;
2100 }
2101}
2102
2103/*
2104 * Adjust the score of common words.
2105 */
2106 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002107score_wordcount_adj(
2108 slang_T *slang,
2109 int score,
2110 char_u *word,
2111 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002112{
2113 hashitem_T *hi;
2114 wordcount_T *wc;
2115 int bonus;
2116 int newscore;
2117
2118 hi = hash_find(&slang->sl_wordcount, word);
2119 if (!HASHITEM_EMPTY(hi))
2120 {
2121 wc = HI2WC(hi);
2122 if (wc->wc_count < SCORE_THRES2)
2123 bonus = SCORE_COMMON1;
2124 else if (wc->wc_count < SCORE_THRES3)
2125 bonus = SCORE_COMMON2;
2126 else
2127 bonus = SCORE_COMMON3;
2128 if (split)
2129 newscore = score - bonus / 2;
2130 else
2131 newscore = score - bonus;
2132 if (newscore < 0)
2133 return 0;
2134 return newscore;
2135 }
2136 return score;
2137}
2138
Bram Moolenaar5195e452005-08-19 20:32:47 +00002139
Bram Moolenaar6de68532005-08-24 22:08:48 +00002140/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002141 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002142 * Like strchr() but independent of locale.
2143 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002144 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002145byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002146{
2147 char_u *p;
2148
2149 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002150 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002151 return TRUE;
2152 return FALSE;
2153}
2154
Bram Moolenaar5195e452005-08-19 20:32:47 +00002155#define SY_MAXLEN 30
2156typedef struct syl_item_S
2157{
2158 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2159 int sy_len;
2160} syl_item_T;
2161
2162/*
2163 * Truncate "slang->sl_syllable" at the first slash and put the following items
2164 * in "slang->sl_syl_items".
2165 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002166 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002167init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002168{
2169 char_u *p;
2170 char_u *s;
2171 int l;
2172 syl_item_T *syl;
2173
2174 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2175 p = vim_strchr(slang->sl_syllable, '/');
2176 while (p != NULL)
2177 {
2178 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002179 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002180 break;
2181 s = p;
2182 p = vim_strchr(p, '/');
2183 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002184 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002185 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002187 if (l >= SY_MAXLEN)
2188 return SP_FORMERROR;
2189 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002190 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002191 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2192 + slang->sl_syl_items.ga_len++;
2193 vim_strncpy(syl->sy_chars, s, l);
2194 syl->sy_len = l;
2195 }
2196 return OK;
2197}
2198
2199/*
2200 * Count the number of syllables in "word".
2201 * When "word" contains spaces the syllables after the last space are counted.
2202 * Returns zero if syllables are not defines.
2203 */
2204 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002205count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002206{
2207 int cnt = 0;
2208 int skip = FALSE;
2209 char_u *p;
2210 int len;
2211 int i;
2212 syl_item_T *syl;
2213 int c;
2214
2215 if (slang->sl_syllable == NULL)
2216 return 0;
2217
2218 for (p = word; *p != NUL; p += len)
2219 {
2220 /* When running into a space reset counter. */
2221 if (*p == ' ')
2222 {
2223 len = 1;
2224 cnt = 0;
2225 continue;
2226 }
2227
2228 /* Find longest match of syllable items. */
2229 len = 0;
2230 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2231 {
2232 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2233 if (syl->sy_len > len
2234 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2235 len = syl->sy_len;
2236 }
2237 if (len != 0) /* found a match, count syllable */
2238 {
2239 ++cnt;
2240 skip = FALSE;
2241 }
2242 else
2243 {
2244 /* No recognized syllable item, at least a syllable char then? */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002245 c = mb_ptr2char(p);
2246 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002247 if (vim_strchr(slang->sl_syllable, c) == NULL)
2248 skip = FALSE; /* No, search for next syllable */
2249 else if (!skip)
2250 {
2251 ++cnt; /* Yes, count it */
2252 skip = TRUE; /* don't count following syllable chars */
2253 }
2254 }
2255 }
2256 return cnt;
2257}
2258
2259/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002260 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002261 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002262 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002263 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002264did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002265{
2266 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002267 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002268 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002269 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002270 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002271 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002272 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002273 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002274 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002275 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002276 int len;
2277 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002278 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002279 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002280 char_u *use_region = NULL;
2281 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002282 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002283 int i, j;
2284 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002285 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002286 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002287 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002288 bufref_T bufref;
2289
2290 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002291
2292 /* We don't want to do this recursively. May happen when a language is
2293 * not available and the SpellFileMissing autocommand opens a new buffer
2294 * in which 'spell' is set. */
2295 if (recursive)
2296 return NULL;
2297 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002298
2299 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002300 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002301
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002302 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002303 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002304 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002305 if (spl_copy == NULL)
2306 goto theend;
2307
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002308 wp->w_s->b_cjk = 0;
2309
2310 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002311 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002312 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002313 /* Get one language name. */
2314 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002315 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002316 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002317
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002318 if (STRCMP(lang, "cjk") == 0)
2319 {
2320 wp->w_s->b_cjk = 1;
2321 continue;
2322 }
2323
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002324 /* If the name ends in ".spl" use it as the name of the spell file.
2325 * If there is a region name let "region" point to it and remove it
2326 * from the name. */
2327 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2328 {
2329 filename = TRUE;
2330
Bram Moolenaarb6356332005-07-18 21:40:44 +00002331 /* Locate a region and remove it from the file name. */
2332 p = vim_strchr(gettail(lang), '_');
2333 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2334 && !ASCII_ISALPHA(p[3]))
2335 {
2336 vim_strncpy(region_cp, p + 1, 2);
2337 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002338 region = region_cp;
2339 }
2340 else
2341 dont_use_region = TRUE;
2342
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002343 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002344 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2345 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002346 break;
2347 }
2348 else
2349 {
2350 filename = FALSE;
2351 if (len > 3 && lang[len - 3] == '_')
2352 {
2353 region = lang + len - 2;
2354 len -= 3;
2355 lang[len] = NUL;
2356 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002357 else
2358 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002359
2360 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002361 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2362 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002363 break;
2364 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002365
Bram Moolenaarb6356332005-07-18 21:40:44 +00002366 if (region != NULL)
2367 {
2368 /* If the region differs from what was used before then don't
2369 * use it for 'spellfile'. */
2370 if (use_region != NULL && STRCMP(region, use_region) != 0)
2371 dont_use_region = TRUE;
2372 use_region = region;
2373 }
2374
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002375 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002377 {
2378 if (filename)
2379 (void)spell_load_file(lang, lang, NULL, FALSE);
2380 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002381 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002382 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002383 /* SpellFileMissing autocommands may do anything, including
2384 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002385 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002388 goto theend;
2389 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002390 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002391 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002392
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002393 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002394 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002395 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002396 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2397 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2398 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002399 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002400 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002401 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002402 {
2403 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002404 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002405 if (c == REGION_ALL)
2406 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002407 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002408 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002409 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002410 /* This addition file is for other regions. */
2411 region_mask = 0;
2412 }
2413 else
2414 /* This is probably an error. Give a warning and
2415 * accept the words anyway. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002416 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002417 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002418 }
2419 else
2420 region_mask = 1 << c;
2421 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002422
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002423 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002424 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002425 if (ga_grow(&ga, 1) == FAIL)
2426 {
2427 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002428 ret_msg = e_outofmem;
2429 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002430 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002431 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002432 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2433 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002434 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002435 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002436 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002437 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002438 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002439 }
2440
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002441 /* round 0: load int_wordlist, if possible.
2442 * round 1: load first name in 'spellfile'.
2443 * round 2: load second name in 'spellfile.
2444 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002445 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002446 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002448 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002449 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002450 /* Internal wordlist, if there is one. */
2451 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002452 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002453 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002454 }
2455 else
2456 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002457 /* One entry in 'spellfile'. */
2458 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2459 STRCAT(spf_name, ".spl");
2460
2461 /* If it was already found above then skip it. */
2462 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002463 {
2464 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2465 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002466 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002467 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002468 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002469 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002470 }
2471
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002472 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002473 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2474 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002476 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002478 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002479 * region name, the region is ignored otherwise. for int_wordlist
2480 * use an arbitrary name. */
2481 if (round == 0)
2482 STRCPY(lang, "internal wordlist");
2483 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002484 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002485 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002486 p = vim_strchr(lang, '.');
2487 if (p != NULL)
2488 *p = NUL; /* truncate at ".encoding.add" */
2489 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002490 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002491
2492 /* If one of the languages has NOBREAK we assume the addition
2493 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002494 if (slang != NULL && nobreak)
2495 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002497 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002499 region_mask = REGION_ALL;
2500 if (use_region != NULL && !dont_use_region)
2501 {
2502 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002503 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002504 if (c != REGION_ALL)
2505 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002506 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002507 /* This spell file is for other regions. */
2508 region_mask = 0;
2509 }
2510
2511 if (region_mask != 0)
2512 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002513 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2514 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2515 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002516 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2517 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002518 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002519 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002520 }
2521 }
2522
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002523 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002524 ga_clear(&wp->w_s->b_langp);
2525 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002526
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002527 /* For each language figure out what language to use for sound folding and
2528 * REP items. If the language doesn't support it itself use another one
2529 * with the same name. E.g. for "en-math" use "en". */
2530 for (i = 0; i < ga.ga_len; ++i)
2531 {
2532 lp = LANGP_ENTRY(ga, i);
2533
2534 /* sound folding */
2535 if (lp->lp_slang->sl_sal.ga_len > 0)
2536 /* language does sound folding itself */
2537 lp->lp_sallang = lp->lp_slang;
2538 else
2539 /* find first similar language that does sound folding */
2540 for (j = 0; j < ga.ga_len; ++j)
2541 {
2542 lp2 = LANGP_ENTRY(ga, j);
2543 if (lp2->lp_slang->sl_sal.ga_len > 0
2544 && STRNCMP(lp->lp_slang->sl_name,
2545 lp2->lp_slang->sl_name, 2) == 0)
2546 {
2547 lp->lp_sallang = lp2->lp_slang;
2548 break;
2549 }
2550 }
2551
2552 /* REP items */
2553 if (lp->lp_slang->sl_rep.ga_len > 0)
2554 /* language has REP items itself */
2555 lp->lp_replang = lp->lp_slang;
2556 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002557 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002558 for (j = 0; j < ga.ga_len; ++j)
2559 {
2560 lp2 = LANGP_ENTRY(ga, j);
2561 if (lp2->lp_slang->sl_rep.ga_len > 0
2562 && STRNCMP(lp->lp_slang->sl_name,
2563 lp2->lp_slang->sl_name, 2) == 0)
2564 {
2565 lp->lp_replang = lp2->lp_slang;
2566 break;
2567 }
2568 }
2569 }
2570
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002571theend:
2572 vim_free(spl_copy);
2573 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002574 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002575 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002576}
2577
2578/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002579 * Clear the midword characters for buffer "buf".
2580 */
2581 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002582clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002583{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002584 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002585 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002586}
2587
2588/*
2589 * Use the "sl_midword" field of language "lp" for buffer "buf".
2590 * They add up to any currently used midword characters.
2591 */
2592 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002593use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002594{
2595 char_u *p;
2596
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002597 if (lp->sl_midword == NULL) /* there aren't any */
2598 return;
2599
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002600 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002601 if (has_mbyte)
2602 {
2603 int c, l, n;
2604 char_u *bp;
2605
2606 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002607 l = (*mb_ptr2len)(p);
2608 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002609 wp->w_s->b_spell_ismw[c] = TRUE;
2610 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002611 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002612 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002613 else
2614 {
2615 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002616 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2617 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002618 if (bp != NULL)
2619 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002620 vim_free(wp->w_s->b_spell_ismw_mb);
2621 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002622 vim_strncpy(bp + n, p, l);
2623 }
2624 }
2625 p += l;
2626 }
2627 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002628 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002629}
2630
2631/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002632 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002633 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002634 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002635 */
2636 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002637find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638{
2639 int i;
2640
2641 for (i = 0; ; i += 2)
2642 {
2643 if (rp[i] == NUL)
2644 return REGION_ALL;
2645 if (rp[i] == region[0] && rp[i + 1] == region[1])
2646 break;
2647 }
2648 return i / 2;
2649}
2650
2651/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002652 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002653 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002654 * Word WF_ONECAP
2655 * W WORD WF_ALLCAP
2656 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002657 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002658 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002659captype(
2660 char_u *word,
2661 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002662{
2663 char_u *p;
2664 int c;
2665 int firstcap;
2666 int allcap;
2667 int past_second = FALSE; /* past second word char */
2668
2669 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002670 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002671 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002672 return 0; /* only non-word characters, illegal word */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002673 if (has_mbyte)
2674 c = mb_ptr2char_adv(&p);
2675 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002676 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002677 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002678
2679 /*
2680 * Need to check all letters to find a word with mixed upper/lower.
2681 * But a word with an upper char only at start is a ONECAP.
2682 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002683 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002684 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002685 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002686 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002687 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002688 {
2689 /* UUl -> KEEPCAP */
2690 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002691 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002692 allcap = FALSE;
2693 }
2694 else if (!allcap)
2695 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002696 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002697 past_second = TRUE;
2698 }
2699
2700 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002701 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002702 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002703 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002704 return 0;
2705}
2706
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002707/*
2708 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2709 * capital. So that make_case_word() can turn WOrd into Word.
2710 * Add ALLCAP for "WOrD".
2711 */
2712 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002713badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002714{
2715 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002716 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002717 int l, u;
2718 int first;
2719 char_u *p;
2720
2721 if (flags & WF_KEEPCAP)
2722 {
2723 /* Count the number of UPPER and lower case letters. */
2724 l = u = 0;
2725 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002726 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002727 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002728 c = PTR2CHAR(p);
2729 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002730 {
2731 ++u;
2732 if (p == word)
2733 first = TRUE;
2734 }
2735 else
2736 ++l;
2737 }
2738
2739 /* If there are more UPPER than lower case letters suggest an
2740 * ALLCAP word. Otherwise, if the first letter is UPPER then
2741 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2742 * require three upper case letters. */
2743 if (u > l && u > 2)
2744 flags |= WF_ALLCAP;
2745 else if (first)
2746 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002747
2748 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2749 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002750 }
2751 return flags;
2752}
2753
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002754/*
2755 * Delete the internal wordlist and its .spl file.
2756 */
2757 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002758spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002759{
2760 char_u fname[MAXPATHL];
2761
2762 if (int_wordlist != NULL)
2763 {
2764 mch_remove(int_wordlist);
2765 int_wordlist_spl(fname);
2766 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002767 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002768 }
2769}
2770
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002771/*
2772 * Free all languages.
2773 */
2774 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002775spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002776{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002777 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002778 buf_T *buf;
2779
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002780 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002781 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002782 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002783
2784 while (first_lang != NULL)
2785 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002786 slang = first_lang;
2787 first_lang = slang->sl_next;
2788 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002789 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002790
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002791 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002792
Bram Moolenaard23a8232018-02-10 18:45:26 +01002793 VIM_CLEAR(repl_to);
2794 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002795}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002796
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002797/*
2798 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002799 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002800 */
2801 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002802spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002804 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002805
Bram Moolenaarea408852005-06-25 22:49:46 +00002806 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807 init_spell_chartab();
2808
2809 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002810 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002811
2812 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002813 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002814 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002815 /* Only load the wordlists when 'spelllang' is set and there is a
2816 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002817 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002818 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002820 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002821 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002822 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002823 }
2824 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002825 }
2826}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002827
Bram Moolenaarb765d632005-06-07 21:00:02 +00002828/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002829 * Opposite of offset2bytes().
2830 * "pp" points to the bytes and is advanced over it.
2831 * Returns the offset.
2832 */
2833 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002834bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002835{
2836 char_u *p = *pp;
2837 int nr;
2838 int c;
2839
2840 c = *p++;
2841 if ((c & 0x80) == 0x00) /* 1 byte */
2842 {
2843 nr = c - 1;
2844 }
2845 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2846 {
2847 nr = (c & 0x3f) - 1;
2848 nr = nr * 255 + (*p++ - 1);
2849 }
2850 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2851 {
2852 nr = (c & 0x1f) - 1;
2853 nr = nr * 255 + (*p++ - 1);
2854 nr = nr * 255 + (*p++ - 1);
2855 }
2856 else /* 4 bytes */
2857 {
2858 nr = (c & 0x0f) - 1;
2859 nr = nr * 255 + (*p++ - 1);
2860 nr = nr * 255 + (*p++ - 1);
2861 nr = nr * 255 + (*p++ - 1);
2862 }
2863
2864 *pp = p;
2865 return nr;
2866}
2867
Bram Moolenaar4770d092006-01-12 23:22:24 +00002868
2869/*
2870 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2871 * list and only contains text lines. Can use a swapfile to reduce memory
2872 * use.
2873 * Most other fields are invalid! Esp. watch out for string options being
2874 * NULL and there is no undo info.
2875 * Returns NULL when out of memory.
2876 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002877 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002878open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002879{
2880 buf_T *buf;
2881
2882 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2883 if (buf != NULL)
2884 {
2885 buf->b_spell = TRUE;
2886 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002887#ifdef FEAT_CRYPT
2888 buf->b_p_key = empty_option;
2889#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002890 ml_open(buf);
2891 ml_open_file(buf); /* create swap file now */
2892 }
2893 return buf;
2894}
2895
2896/*
2897 * Close the buffer used for spell info.
2898 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002899 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002900close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002901{
2902 if (buf != NULL)
2903 {
2904 ml_close(buf, TRUE);
2905 vim_free(buf);
2906 }
2907}
2908
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002909/*
2910 * Init the chartab used for spelling for ASCII.
2911 * EBCDIC is not supported!
2912 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002913 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002914clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002915{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002916 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002917
2918 /* Init everything to FALSE. */
2919 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2920 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2921 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002922 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002923 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002924 sp->st_upper[i] = i;
2925 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002926
2927 /* We include digits. A word shouldn't start with a digit, but handling
2928 * that is done separately. */
2929 for (i = '0'; i <= '9'; ++i)
2930 sp->st_isw[i] = TRUE;
2931 for (i = 'A'; i <= 'Z'; ++i)
2932 {
2933 sp->st_isw[i] = TRUE;
2934 sp->st_isu[i] = TRUE;
2935 sp->st_fold[i] = i + 0x20;
2936 }
2937 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002938 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002939 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002940 sp->st_upper[i] = i - 0x20;
2941 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002942}
2943
2944/*
2945 * Init the chartab used for spelling. Only depends on 'encoding'.
2946 * Called once while starting up and when 'encoding' changes.
2947 * The default is to use isalpha(), but the spell file should define the word
2948 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002949 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002950 */
2951 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002952init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002953{
2954 int i;
2955
2956 did_set_spelltab = FALSE;
2957 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002958 if (enc_dbcs)
2959 {
2960 /* DBCS: assume double-wide characters are word characters. */
2961 for (i = 128; i <= 255; ++i)
2962 if (MB_BYTE2LEN(i) == 2)
2963 spelltab.st_isw[i] = TRUE;
2964 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002965 else if (enc_utf8)
2966 {
2967 for (i = 128; i < 256; ++i)
2968 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002969 int f = utf_fold(i);
2970 int u = utf_toupper(i);
2971
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002972 spelltab.st_isu[i] = utf_isupper(i);
2973 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002974 /* The folded/upper-cased value is different between latin1 and
2975 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2976 * value for utf-8 to avoid this. */
2977 spelltab.st_fold[i] = (f < 256) ? f : i;
2978 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002979 }
2980 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002981 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002982 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002983 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002984 for (i = 128; i < 256; ++i)
2985 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002986 if (MB_ISUPPER(i))
2987 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002988 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002989 spelltab.st_isu[i] = TRUE;
2990 spelltab.st_fold[i] = MB_TOLOWER(i);
2991 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002992 else if (MB_ISLOWER(i))
2993 {
2994 spelltab.st_isw[i] = TRUE;
2995 spelltab.st_upper[i] = MB_TOUPPER(i);
2996 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002997 }
2998 }
2999}
3000
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003001
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003002/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003003 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003004 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003005 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003006 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003007 */
3008 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003009spell_iswordp(
3010 char_u *p,
3011 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003012{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003013 char_u *s;
3014 int l;
3015 int c;
3016
3017 if (has_mbyte)
3018 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003019 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003020 s = p;
3021 if (l == 1)
3022 {
3023 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003024 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003025 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003026 }
3027 else
3028 {
3029 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003030 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3031 : (wp->w_s->b_spell_ismw_mb != NULL
3032 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003033 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003034 }
3035
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003036 c = mb_ptr2char(s);
3037 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003038 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003039 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003040 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003041
Bram Moolenaar860cae12010-06-05 23:22:07 +02003042 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003043}
3044
3045/*
3046 * Return TRUE if "p" points to a word character.
3047 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3048 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003049 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003050spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003051{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003052 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003053
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003054 if (has_mbyte)
3055 {
3056 c = mb_ptr2char(p);
3057 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003058 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003059 return spelltab.st_isw[c];
3060 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003061 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003062}
3063
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003064/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003065 * Return TRUE if word class indicates a word character.
3066 * Only for characters above 255.
3067 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003068 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003069 */
3070 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003071spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003072{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003073 if (wp->w_s->b_cjk)
3074 /* East Asian characters are not considered word characters. */
3075 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003076 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3077}
3078
3079/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003080 * Return TRUE if "p" points to a word character.
3081 * Wide version of spell_iswordp().
3082 */
3083 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003084spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003085{
3086 int *s;
3087
Bram Moolenaar860cae12010-06-05 23:22:07 +02003088 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3089 : (wp->w_s->b_spell_ismw_mb != NULL
3090 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003091 s = p + 1;
3092 else
3093 s = p;
3094
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003095 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003096 {
3097 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003098 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003099 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003100 return spell_mb_isword_class(
3101 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003102 return 0;
3103 }
3104 return spelltab.st_isw[*s];
3105}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003106
Bram Moolenaarea408852005-06-25 22:49:46 +00003107/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003108 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3109 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003110 * When using a multi-byte 'encoding' the length may change!
3111 * Returns FAIL when something wrong.
3112 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003113 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003114spell_casefold(
3115 char_u *str,
3116 int len,
3117 char_u *buf,
3118 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003119{
3120 int i;
3121
3122 if (len >= buflen)
3123 {
3124 buf[0] = NUL;
3125 return FAIL; /* result will not fit */
3126 }
3127
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003128 if (has_mbyte)
3129 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003130 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003131 char_u *p;
3132 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003133
3134 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003135 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003136 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003137 if (outi + MB_MAXBYTES > buflen)
3138 {
3139 buf[outi] = NUL;
3140 return FAIL;
3141 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003142 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003143 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003144 }
3145 buf[outi] = NUL;
3146 }
3147 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003148 {
3149 /* Be quick for non-multibyte encodings. */
3150 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003151 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003152 buf[i] = NUL;
3153 }
3154
3155 return OK;
3156}
3157
Bram Moolenaar4770d092006-01-12 23:22:24 +00003158/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003159#define SPS_BEST 1
3160#define SPS_FAST 2
3161#define SPS_DOUBLE 4
3162
Bram Moolenaar4770d092006-01-12 23:22:24 +00003163static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3164static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003165
3166/*
3167 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003168 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003169 */
3170 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003171spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003172{
3173 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003174 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003175 char_u buf[MAXPATHL];
3176 int f;
3177
3178 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003179 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003180
3181 for (p = p_sps; *p != NUL; )
3182 {
3183 copy_option_part(&p, buf, MAXPATHL, ",");
3184
3185 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003186 if (VIM_ISDIGIT(*buf))
3187 {
3188 s = buf;
3189 sps_limit = getdigits(&s);
3190 if (*s != NUL && !VIM_ISDIGIT(*s))
3191 f = -1;
3192 }
3193 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003194 f = SPS_BEST;
3195 else if (STRCMP(buf, "fast") == 0)
3196 f = SPS_FAST;
3197 else if (STRCMP(buf, "double") == 0)
3198 f = SPS_DOUBLE;
3199 else if (STRNCMP(buf, "expr:", 5) != 0
3200 && STRNCMP(buf, "file:", 5) != 0)
3201 f = -1;
3202
3203 if (f == -1 || (sps_flags != 0 && f != 0))
3204 {
3205 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003206 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003207 return FAIL;
3208 }
3209 if (f != 0)
3210 sps_flags = f;
3211 }
3212
3213 if (sps_flags == 0)
3214 sps_flags = SPS_BEST;
3215
3216 return OK;
3217}
3218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003219/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003220 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003221 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003222 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003223 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003224 */
3225 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003226spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003227{
3228 char_u *line;
3229 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003230 char_u wcopy[MAXWLEN + 2];
3231 char_u *p;
3232 int i;
3233 int c;
3234 suginfo_T sug;
3235 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003236 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003237 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003238 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003239 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003240 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003241 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003242
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003243 if (no_spell_checking(curwin))
3244 return;
3245
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003246 if (VIsual_active)
3247 {
3248 /* Use the Visually selected text as the bad word. But reject
3249 * a multi-line selection. */
3250 if (curwin->w_cursor.lnum != VIsual.lnum)
3251 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003252 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003253 return;
3254 }
3255 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3256 if (badlen < 0)
3257 badlen = -badlen;
3258 else
3259 curwin->w_cursor.col = VIsual.col;
3260 ++badlen;
3261 end_visual_mode();
3262 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003263 /* Find the start of the badly spelled word. */
3264 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003265 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003266 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003267 /* No bad word or it starts after the cursor: use the word under the
3268 * cursor. */
3269 curwin->w_cursor = prev_cursor;
3270 line = ml_get_curline();
3271 p = line + curwin->w_cursor.col;
3272 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003273 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003274 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003275 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003276 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003277 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003278
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003279 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003280 {
3281 beep_flush();
3282 return;
3283 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003284 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003285 }
3286
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003287 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003289 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003290 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003291
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003292 /* Make a copy of current line since autocommands may free the line. */
3293 line = vim_strsave(ml_get_curline());
3294 if (line == NULL)
3295 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003296
Bram Moolenaar5195e452005-08-19 20:32:47 +00003297 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3298 * 'spellsuggest', whatever is smaller. */
3299 if (sps_limit > (int)Rows - 2)
3300 limit = (int)Rows - 2;
3301 else
3302 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003303 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003304 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003305
3306 if (sug.su_ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003307 msg(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003308 else if (count > 0)
3309 {
3310 if (count > sug.su_ga.ga_len)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003311 smsg(_("Sorry, only %ld suggestions"),
Bram Moolenaard12a1322005-08-21 22:08:24 +00003312 (long)sug.su_ga.ga_len);
3313 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003314 else
3315 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003316 VIM_CLEAR(repl_from);
3317 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003318
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003319#ifdef FEAT_RIGHTLEFT
3320 /* When 'rightleft' is set the list is drawn right-left. */
3321 cmdmsg_rl = curwin->w_p_rl;
3322 if (cmdmsg_rl)
3323 msg_col = Columns - 1;
3324#endif
3325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003326 /* List the suggestions. */
3327 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003328 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003329 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003330 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3331 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003332#ifdef FEAT_RIGHTLEFT
3333 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3334 {
3335 /* And now the rabbit from the high hat: Avoid showing the
3336 * untranslated message rightleft. */
3337 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3338 sug.su_badlen, sug.su_badptr);
3339 }
3340#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003341 msg_puts((char *)IObuff);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003342 msg_clr_eos();
3343 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 msg_scroll = TRUE;
3346 for (i = 0; i < sug.su_ga.ga_len; ++i)
3347 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003348 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003349
3350 /* The suggested word may replace only part of the bad word, add
3351 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003352 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003353 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003354 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 sug.su_badptr + stp->st_orglen,
3356 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003357 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3358#ifdef FEAT_RIGHTLEFT
3359 if (cmdmsg_rl)
3360 rl_mirror(IObuff);
3361#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003362 msg_puts((char *)IObuff);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003363
3364 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003365 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003366
3367 /* The word may replace more than "su_badlen". */
3368 if (sug.su_badlen < stp->st_orglen)
3369 {
3370 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3371 stp->st_orglen, sug.su_badptr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003372 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003373 }
3374
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003375 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003376 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003377 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003378 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003379 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003380 stp->st_salscore ? "s " : "",
3381 stp->st_score, stp->st_altscore);
3382 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003383 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003384 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003385#ifdef FEAT_RIGHTLEFT
3386 if (cmdmsg_rl)
3387 /* Mirror the numbers, but keep the leading space. */
3388 rl_mirror(IObuff + 1);
3389#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003390 msg_advance(30);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003391 msg_puts((char *)IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003393 msg_putchar('\n');
3394 }
3395
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003396#ifdef FEAT_RIGHTLEFT
3397 cmdmsg_rl = FALSE;
3398 msg_col = 0;
3399#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003400 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003401 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003402 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003403 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003404 lines_left = Rows; /* avoid more prompt */
3405 /* don't delay for 'smd' in normal_cmd() */
3406 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003407 }
3408
Bram Moolenaard12a1322005-08-21 22:08:24 +00003409 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3410 {
3411 /* Save the from and to text for :spellrepall. */
3412 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003413 if (sug.su_badlen > stp->st_orglen)
3414 {
3415 /* Replacing less than "su_badlen", append the remainder to
3416 * repl_to. */
3417 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3418 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3419 sug.su_badlen - stp->st_orglen,
3420 sug.su_badptr + stp->st_orglen);
3421 repl_to = vim_strsave(IObuff);
3422 }
3423 else
3424 {
3425 /* Replacing su_badlen or more, use the whole word. */
3426 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3427 repl_to = vim_strsave(stp->st_word);
3428 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003429
3430 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003431 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3432 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003433 if (p != NULL)
3434 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003435 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003436 mch_memmove(p, line, c);
3437 STRCPY(p + c, stp->st_word);
3438 STRCAT(p, sug.su_badptr + stp->st_orglen);
3439 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3440 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003441
3442 /* For redo we use a change-word command. */
3443 ResetRedobuff();
3444 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003445 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003446 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003447 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003448
3449 /* After this "p" may be invalid. */
3450 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003451 }
3452 }
3453 else
3454 curwin->w_cursor = prev_cursor;
3455
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003456 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003457skip:
3458 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003459}
3460
3461/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003462 * Check if the word at line "lnum" column "col" is required to start with a
3463 * capital. This uses 'spellcapcheck' of the current buffer.
3464 */
3465 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003466check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003467{
3468 int need_cap = FALSE;
3469 char_u *line;
3470 char_u *line_copy = NULL;
3471 char_u *p;
3472 colnr_T endcol;
3473 regmatch_T regmatch;
3474
Bram Moolenaar860cae12010-06-05 23:22:07 +02003475 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003476 return FALSE;
3477
3478 line = ml_get_curline();
3479 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003480 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003481 {
3482 /* At start of line, check if previous line is empty or sentence
3483 * ends there. */
3484 if (lnum == 1)
3485 need_cap = TRUE;
3486 else
3487 {
3488 line = ml_get(lnum - 1);
3489 if (*skipwhite(line) == NUL)
3490 need_cap = TRUE;
3491 else
3492 {
3493 /* Append a space in place of the line break. */
3494 line_copy = concat_str(line, (char_u *)" ");
3495 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003497 }
3498 }
3499 }
3500 else
3501 endcol = col;
3502
3503 if (endcol > 0)
3504 {
3505 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003506 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003507 regmatch.rm_ic = FALSE;
3508 p = line + endcol;
3509 for (;;)
3510 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003511 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003512 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003513 break;
3514 if (vim_regexec(&regmatch, p, 0)
3515 && regmatch.endp[0] == line + endcol)
3516 {
3517 need_cap = TRUE;
3518 break;
3519 }
3520 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003521 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003522 }
3523
3524 vim_free(line_copy);
3525
3526 return need_cap;
3527}
3528
3529
3530/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003531 * ":spellrepall"
3532 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003533 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003534ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003535{
3536 pos_T pos = curwin->w_cursor;
3537 char_u *frompat;
3538 int addlen;
3539 char_u *line;
3540 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003541 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003542 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003543
3544 if (repl_from == NULL || repl_to == NULL)
3545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003546 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003547 return;
3548 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003549 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003552 if (frompat == NULL)
3553 return;
3554 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3555 p_ws = FALSE;
3556
Bram Moolenaar5195e452005-08-19 20:32:47 +00003557 sub_nsubs = 0;
3558 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003559 curwin->w_cursor.lnum = 0;
3560 while (!got_int)
3561 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003562 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003563 || u_save_cursor() == FAIL)
3564 break;
3565
3566 /* Only replace when the right word isn't there yet. This happens
3567 * when changing "etc" to "etc.". */
3568 line = ml_get_curline();
3569 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3570 repl_to, STRLEN(repl_to)) != 0)
3571 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003572 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003573 if (p == NULL)
3574 break;
3575 mch_memmove(p, line, curwin->w_cursor.col);
3576 STRCPY(p + curwin->w_cursor.col, repl_to);
3577 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3578 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3579 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003580
3581 if (curwin->w_cursor.lnum != prev_lnum)
3582 {
3583 ++sub_nlines;
3584 prev_lnum = curwin->w_cursor.lnum;
3585 }
3586 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003587 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003588 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003589 }
3590
3591 p_ws = save_ws;
3592 curwin->w_cursor = pos;
3593 vim_free(frompat);
3594
Bram Moolenaar5195e452005-08-19 20:32:47 +00003595 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003596 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003597 else
3598 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003599}
3600
3601/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003602 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3603 * a list of allocated strings.
3604 */
3605 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003606spell_suggest_list(
3607 garray_T *gap,
3608 char_u *word,
3609 int maxcount, /* maximum nr of suggestions */
3610 int need_cap, /* 'spellcapcheck' matched */
3611 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003612{
3613 suginfo_T sug;
3614 int i;
3615 suggest_T *stp;
3616 char_u *wcopy;
3617
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003618 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003619
3620 /* Make room in "gap". */
3621 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003622 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003623 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003624 for (i = 0; i < sug.su_ga.ga_len; ++i)
3625 {
3626 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003627
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003628 /* The suggested word may replace only part of "word", add the not
3629 * replaced part. */
3630 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003631 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003632 if (wcopy == NULL)
3633 break;
3634 STRCPY(wcopy, stp->st_word);
3635 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3636 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3637 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003638 }
3639
3640 spell_find_cleanup(&sug);
3641}
3642
3643/*
3644 * Find spell suggestions for the word at the start of "badptr".
3645 * Return the suggestions in "su->su_ga".
3646 * The maximum number of suggestions is "maxcount".
3647 * Note: does use info for the current window.
3648 * This is based on the mechanisms of Aspell, but completely reimplemented.
3649 */
3650 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003651spell_find_suggest(
3652 char_u *badptr,
3653 int badlen, /* length of bad word or 0 if unknown */
3654 suginfo_T *su,
3655 int maxcount,
3656 int banbadword, /* don't include badword in suggestions */
3657 int need_cap, /* word should start with capital */
3658 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003659{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003660 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003661 char_u buf[MAXPATHL];
3662 char_u *p;
3663 int do_combine = FALSE;
3664 char_u *sps_copy;
3665#ifdef FEAT_EVAL
3666 static int expr_busy = FALSE;
3667#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003668 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003669 int i;
3670 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003671
3672 /*
3673 * Set the info in "*su".
3674 */
3675 vim_memset(su, 0, sizeof(suginfo_T));
3676 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3677 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003678 if (*badptr == NUL)
3679 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003680 hash_init(&su->su_banned);
3681
3682 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003683 if (badlen != 0)
3684 su->su_badlen = badlen;
3685 else
3686 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003687 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003688 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003689
3690 if (su->su_badlen >= MAXWLEN)
3691 su->su_badlen = MAXWLEN - 1; /* just in case */
3692 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3693 (void)spell_casefold(su->su_badptr, su->su_badlen,
3694 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003695 /* TODO: make this work if the case-folded text is longer than the original
3696 * text. Currently an illegal byte causes wrong pointer computations. */
3697 su->su_fbadword[su->su_badlen] = NUL;
3698
Bram Moolenaar0c405862005-06-22 22:26:26 +00003699 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003700 su->su_badflags = badword_captype(su->su_badptr,
3701 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003702 if (need_cap)
3703 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003704
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003705 /* Find the default language for sound folding. We simply use the first
3706 * one in 'spelllang' that supports sound folding. That's good for when
3707 * using multiple files for one language, it's not that bad when mixing
3708 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003709 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003710 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003711 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003712 if (lp->lp_sallang != NULL)
3713 {
3714 su->su_sallang = lp->lp_sallang;
3715 break;
3716 }
3717 }
3718
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003719 /* Soundfold the bad word with the default sound folding, so that we don't
3720 * have to do this many times. */
3721 if (su->su_sallang != NULL)
3722 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3723 su->su_sal_badword);
3724
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003725 /* If the word is not capitalised and spell_check() doesn't consider the
3726 * word to be bad then it might need to be capitalised. Add a suggestion
3727 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003728 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003729 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003730 {
3731 make_case_word(su->su_badword, buf, WF_ONECAP);
3732 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003733 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003734 }
3735
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003736 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003737 if (banbadword)
3738 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003739
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003740 /* Make a copy of 'spellsuggest', because the expression may change it. */
3741 sps_copy = vim_strsave(p_sps);
3742 if (sps_copy == NULL)
3743 return;
3744
3745 /* Loop over the items in 'spellsuggest'. */
3746 for (p = sps_copy; *p != NUL; )
3747 {
3748 copy_option_part(&p, buf, MAXPATHL, ",");
3749
3750 if (STRNCMP(buf, "expr:", 5) == 0)
3751 {
3752#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003753 /* Evaluate an expression. Skip this when called recursively,
3754 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003755 if (!expr_busy)
3756 {
3757 expr_busy = TRUE;
3758 spell_suggest_expr(su, buf + 5);
3759 expr_busy = FALSE;
3760 }
3761#endif
3762 }
3763 else if (STRNCMP(buf, "file:", 5) == 0)
3764 /* Use list of suggestions in a file. */
3765 spell_suggest_file(su, buf + 5);
3766 else
3767 {
3768 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003769 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003770 if (sps_flags & SPS_DOUBLE)
3771 do_combine = TRUE;
3772 }
3773 }
3774
3775 vim_free(sps_copy);
3776
3777 if (do_combine)
3778 /* Combine the two list of suggestions. This must be done last,
3779 * because sorting changes the order again. */
3780 score_combine(su);
3781}
3782
3783#ifdef FEAT_EVAL
3784/*
3785 * Find suggestions by evaluating expression "expr".
3786 */
3787 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003788spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003789{
3790 list_T *list;
3791 listitem_T *li;
3792 int score;
3793 char_u *p;
3794
3795 /* The work is split up in a few parts to avoid having to export
3796 * suginfo_T.
3797 * First evaluate the expression and get the resulting list. */
3798 list = eval_spell_expr(su->su_badword, expr);
3799 if (list != NULL)
3800 {
3801 /* Loop over the items in the list. */
3802 for (li = list->lv_first; li != NULL; li = li->li_next)
3803 if (li->li_tv.v_type == VAR_LIST)
3804 {
3805 /* Get the word and the score from the items. */
3806 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003807 if (score >= 0 && score <= su->su_maxscore)
3808 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3809 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003810 }
3811 list_unref(list);
3812 }
3813
Bram Moolenaar4770d092006-01-12 23:22:24 +00003814 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3815 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003816 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3817}
3818#endif
3819
3820/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003821 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003822 */
3823 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003824spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003825{
3826 FILE *fd;
3827 char_u line[MAXWLEN * 2];
3828 char_u *p;
3829 int len;
3830 char_u cword[MAXWLEN];
3831
3832 /* Open the file. */
3833 fd = mch_fopen((char *)fname, "r");
3834 if (fd == NULL)
3835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003836 semsg(_(e_notopen), fname);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003837 return;
3838 }
3839
3840 /* Read it line by line. */
3841 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3842 {
3843 line_breakcheck();
3844
3845 p = vim_strchr(line, '/');
3846 if (p == NULL)
3847 continue; /* No Tab found, just skip the line. */
3848 *p++ = NUL;
3849 if (STRICMP(su->su_badword, line) == 0)
3850 {
3851 /* Match! Isolate the good word, until CR or NL. */
3852 for (len = 0; p[len] >= ' '; ++len)
3853 ;
3854 p[len] = NUL;
3855
3856 /* If the suggestion doesn't have specific case duplicate the case
3857 * of the bad word. */
3858 if (captype(p, NULL) == 0)
3859 {
3860 make_case_word(p, cword, su->su_badflags);
3861 p = cword;
3862 }
3863
3864 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003865 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003866 }
3867 }
3868
3869 fclose(fd);
3870
Bram Moolenaar4770d092006-01-12 23:22:24 +00003871 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3872 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003873 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3874}
3875
3876/*
3877 * Find suggestions for the internal method indicated by "sps_flags".
3878 */
3879 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003880spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003881{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003882 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003883 * Load the .sug file(s) that are available and not done yet.
3884 */
3885 suggest_load_files();
3886
3887 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003888 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003889 *
3890 * Set a maximum score to limit the combination of operations that is
3891 * tried.
3892 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003893 suggest_try_special(su);
3894
3895 /*
3896 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3897 * from the .aff file and inserting a space (split the word).
3898 */
3899 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003900
3901 /* For the resulting top-scorers compute the sound-a-like score. */
3902 if (sps_flags & SPS_DOUBLE)
3903 score_comp_sal(su);
3904
3905 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003906 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003907 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003908 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003909 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00003910 if (sps_flags & SPS_BEST)
3911 /* Adjust the word score for the suggestions found so far for how
3912 * they sounds like. */
3913 rescore_suggestions(su);
3914
3915 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003916 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00003917 * for the soundfold word, limits the changes that are being tried,
3918 * and "su_sfmaxscore" the rescored score, which is set by
3919 * cleanup_suggestions().
3920 * First find words with a small edit distance, because this is much
3921 * faster and often already finds the top-N suggestions. If we didn't
3922 * find many suggestions try again with a higher edit distance.
3923 * "sl_sounddone" is used to avoid doing the same word twice.
3924 */
3925 suggest_try_soundalike_prep();
3926 su->su_maxscore = SCORE_SFMAX1;
3927 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003928 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003929 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3930 {
3931 /* We didn't find enough matches, try again, allowing more
3932 * changes to the soundfold word. */
3933 su->su_maxscore = SCORE_SFMAX2;
3934 suggest_try_soundalike(su);
3935 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3936 {
3937 /* Still didn't find enough matches, try again, allowing even
3938 * more changes to the soundfold word. */
3939 su->su_maxscore = SCORE_SFMAX3;
3940 suggest_try_soundalike(su);
3941 }
3942 }
3943 su->su_maxscore = su->su_sfmaxscore;
3944 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003945 }
3946
Bram Moolenaar4770d092006-01-12 23:22:24 +00003947 /* When CTRL-C was hit while searching do show the results. Only clear
3948 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003949 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00003950 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003951 {
3952 (void)vgetc();
3953 got_int = FALSE;
3954 }
3955
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003956 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003957 {
3958 if (sps_flags & SPS_BEST)
3959 /* Adjust the word score for how it sounds like. */
3960 rescore_suggestions(su);
3961
Bram Moolenaar4770d092006-01-12 23:22:24 +00003962 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3963 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003964 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003965 }
3966}
3967
3968/*
3969 * Free the info put in "*su" by spell_find_suggest().
3970 */
3971 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003972spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003973{
3974 int i;
3975
3976 /* Free the suggestions. */
3977 for (i = 0; i < su->su_ga.ga_len; ++i)
3978 vim_free(SUG(su->su_ga, i).st_word);
3979 ga_clear(&su->su_ga);
3980 for (i = 0; i < su->su_sga.ga_len; ++i)
3981 vim_free(SUG(su->su_sga, i).st_word);
3982 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003983
3984 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003985 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986}
3987
3988/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003989 * Make a copy of "word", with the first letter upper or lower cased, to
3990 * "wcopy[MAXWLEN]". "word" must not be empty.
3991 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003992 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003993 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003994onecap_copy(
3995 char_u *word,
3996 char_u *wcopy,
3997 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003998{
3999 char_u *p;
4000 int c;
4001 int l;
4002
4003 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004004 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004005 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004007 c = *p++;
4008 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004009 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004010 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004011 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 if (has_mbyte)
4013 l = mb_char2bytes(c, wcopy);
4014 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 {
4016 l = 1;
4017 wcopy[0] = c;
4018 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004019 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020}
4021
4022/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004023 * Make a copy of "word" with all the letters upper cased into
4024 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 */
4026 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004027allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004028{
4029 char_u *s;
4030 char_u *d;
4031 int c;
4032
4033 d = wcopy;
4034 for (s = word; *s != NUL; )
4035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004036 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004037 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004039 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004040
Bram Moolenaard3184b52011-09-02 14:18:20 +02004041 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004042 * would cause weird errors in other 8-bit encodings. */
4043 if (enc_latin1like && c == 0xdf)
4044 {
4045 c = 'S';
4046 if (d - wcopy >= MAXWLEN - 1)
4047 break;
4048 *d++ = c;
4049 }
4050 else
Bram Moolenaar78622822005-08-23 21:00:13 +00004051 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004053 if (has_mbyte)
4054 {
4055 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4056 break;
4057 d += mb_char2bytes(c, d);
4058 }
4059 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004060 {
4061 if (d - wcopy >= MAXWLEN - 1)
4062 break;
4063 *d++ = c;
4064 }
4065 }
4066 *d = NUL;
4067}
4068
4069/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004070 * Try finding suggestions by recognizing specific situations.
4071 */
4072 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004073suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004074{
4075 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004076 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004077 int c;
4078 char_u word[MAXWLEN];
4079
4080 /*
4081 * Recognize a word that is repeated: "the the".
4082 */
4083 p = skiptowhite(su->su_fbadword);
4084 len = p - su->su_fbadword;
4085 p = skipwhite(p);
4086 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4087 {
4088 /* Include badflags: if the badword is onecap or allcap
4089 * use that for the goodword too: "The the" -> "The". */
4090 c = su->su_fbadword[len];
4091 su->su_fbadword[len] = NUL;
4092 make_case_word(su->su_fbadword, word, su->su_badflags);
4093 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004094
4095 /* Give a soundalike score of 0, compute the score as if deleting one
4096 * character. */
4097 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004098 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004099 }
4100}
4101
4102/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004103 * Change the 0 to 1 to measure how much time is spent in each state.
4104 * Output is dumped in "suggestprof".
4105 */
4106#if 0
4107# define SUGGEST_PROFILE
4108proftime_T current;
4109proftime_T total;
4110proftime_T times[STATE_FINAL + 1];
4111long counts[STATE_FINAL + 1];
4112
4113 static void
4114prof_init(void)
4115{
4116 for (int i = 0; i <= STATE_FINAL; ++i)
4117 {
4118 profile_zero(&times[i]);
4119 counts[i] = 0;
4120 }
4121 profile_start(&current);
4122 profile_start(&total);
4123}
4124
4125/* call before changing state */
4126 static void
4127prof_store(state_T state)
4128{
4129 profile_end(&current);
4130 profile_add(&times[state], &current);
4131 ++counts[state];
4132 profile_start(&current);
4133}
4134# define PROF_STORE(state) prof_store(state);
4135
4136 static void
4137prof_report(char *name)
4138{
4139 FILE *fd = fopen("suggestprof", "a");
4140
4141 profile_end(&total);
4142 fprintf(fd, "-----------------------\n");
4143 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4144 for (int i = 0; i <= STATE_FINAL; ++i)
4145 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4146 fclose(fd);
4147}
4148#else
4149# define PROF_STORE(state)
4150#endif
4151
4152/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 * Try finding suggestions by adding/removing/swapping letters.
4154 */
4155 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004156suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157{
4158 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004159 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004161 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004162 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163
4164 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004165 * to find matches (esp. REP items). Append some more text, changing
4166 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004168 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004169 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004170 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171
Bram Moolenaar860cae12010-06-05 23:22:07 +02004172 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004174 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004175
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004176 /* If reloading a spell file fails it's still in the list but
4177 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004178 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004179 continue;
4180
Bram Moolenaar4770d092006-01-12 23:22:24 +00004181 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004182#ifdef SUGGEST_PROFILE
4183 prof_init();
4184#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004185 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004186#ifdef SUGGEST_PROFILE
4187 prof_report("try_change");
4188#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004189 }
4190}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191
Bram Moolenaar4770d092006-01-12 23:22:24 +00004192/* Check the maximum score, if we go over it we won't try this change. */
4193#define TRY_DEEPER(su, stack, depth, add) \
4194 (stack[depth].ts_score + (add) < su->su_maxscore)
4195
4196/*
4197 * Try finding suggestions by adding/removing/swapping letters.
4198 *
4199 * This uses a state machine. At each node in the tree we try various
4200 * operations. When trying if an operation works "depth" is increased and the
4201 * stack[] is used to store info. This allows combinations, thus insert one
4202 * character, replace one and delete another. The number of changes is
4203 * limited by su->su_maxscore.
4204 *
4205 * After implementing this I noticed an article by Kemal Oflazer that
4206 * describes something similar: "Error-tolerant Finite State Recognition with
4207 * Applications to Morphological Analysis and Spelling Correction" (1996).
4208 * The implementation in the article is simplified and requires a stack of
4209 * unknown depth. The implementation here only needs a stack depth equal to
4210 * the length of the word.
4211 *
4212 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4213 * The mechanism is the same, but we find a match with a sound-folded word
4214 * that comes from one or more original words. Each of these words may be
4215 * added, this is done by add_sound_suggest().
4216 * Don't use:
4217 * the prefix tree or the keep-case tree
4218 * "su->su_badlen"
4219 * anything to do with upper and lower case
4220 * anything to do with word or non-word characters ("spell_iswordp()")
4221 * banned words
4222 * word flags (rare, region, compounding)
4223 * word splitting for now
4224 * "similar_chars()"
4225 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4226 */
4227 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004228suggest_trie_walk(
4229 suginfo_T *su,
4230 langp_T *lp,
4231 char_u *fword,
4232 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004233{
4234 char_u tword[MAXWLEN]; /* good word collected so far */
4235 trystate_T stack[MAXWLEN];
4236 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004237 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004238 * words and split word. NUL terminated
4239 * when going deeper but not when coming
4240 * back. */
4241 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4242 trystate_T *sp;
4243 int newscore;
4244 int score;
4245 char_u *byts, *fbyts, *pbyts;
4246 idx_T *idxs, *fidxs, *pidxs;
4247 int depth;
4248 int c, c2, c3;
4249 int n = 0;
4250 int flags;
4251 garray_T *gap;
4252 idx_T arridx;
4253 int len;
4254 char_u *p;
4255 fromto_T *ftp;
4256 int fl = 0, tl;
4257 int repextra = 0; /* extra bytes in fword[] from REP item */
4258 slang_T *slang = lp->lp_slang;
4259 int fword_ends;
4260 int goodword_ends;
4261#ifdef DEBUG_TRIEWALK
4262 /* Stores the name of the change made at each level. */
4263 char_u changename[MAXWLEN][80];
4264#endif
4265 int breakcheckcount = 1000;
4266 int compound_ok;
4267
4268 /*
4269 * Go through the whole case-fold tree, try changes at each node.
4270 * "tword[]" contains the word collected from nodes in the tree.
4271 * "fword[]" the word we are trying to match with (initially the bad
4272 * word).
4273 */
4274 depth = 0;
4275 sp = &stack[0];
4276 vim_memset(sp, 0, sizeof(trystate_T));
4277 sp->ts_curi = 1;
4278
4279 if (soundfold)
4280 {
4281 /* Going through the soundfold tree. */
4282 byts = fbyts = slang->sl_sbyts;
4283 idxs = fidxs = slang->sl_sidxs;
4284 pbyts = NULL;
4285 pidxs = NULL;
4286 sp->ts_prefixdepth = PFD_NOPREFIX;
4287 sp->ts_state = STATE_START;
4288 }
4289 else
4290 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004291 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004292 * When there are postponed prefixes we need to use these first. At
4293 * the end of the prefix we continue in the case-fold tree.
4294 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004295 fbyts = slang->sl_fbyts;
4296 fidxs = slang->sl_fidxs;
4297 pbyts = slang->sl_pbyts;
4298 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004299 if (pbyts != NULL)
4300 {
4301 byts = pbyts;
4302 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004303 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004304 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4305 }
4306 else
4307 {
4308 byts = fbyts;
4309 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004310 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004311 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004312 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004313 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004314
Bram Moolenaar4770d092006-01-12 23:22:24 +00004315 /*
4316 * Loop to find all suggestions. At each round we either:
4317 * - For the current state try one operation, advance "ts_curi",
4318 * increase "depth".
4319 * - When a state is done go to the next, set "ts_state".
4320 * - When all states are tried decrease "depth".
4321 */
4322 while (depth >= 0 && !got_int)
4323 {
4324 sp = &stack[depth];
4325 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004327 case STATE_START:
4328 case STATE_NOPREFIX:
4329 /*
4330 * Start of node: Deal with NUL bytes, which means
4331 * tword[] may end here.
4332 */
4333 arridx = sp->ts_arridx; /* current node in the tree */
4334 len = byts[arridx]; /* bytes in this node */
4335 arridx += sp->ts_curi; /* index of current byte */
4336
4337 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004339 /* Skip over the NUL bytes, we use them later. */
4340 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4341 ;
4342 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343
Bram Moolenaar4770d092006-01-12 23:22:24 +00004344 /* Always past NUL bytes now. */
4345 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004346 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004347 sp->ts_state = STATE_ENDNUL;
4348 sp->ts_save_badflags = su->su_badflags;
4349
4350 /* At end of a prefix or at start of prefixtree: check for
4351 * following word. */
4352 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004353 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004354 /* Set su->su_badflags to the caps type at this position.
4355 * Use the caps type until here for the prefix itself. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004356 if (has_mbyte)
4357 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4358 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004359 n = sp->ts_fidx;
4360 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4361 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004362 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004363#ifdef DEBUG_TRIEWALK
4364 sprintf(changename[depth], "prefix");
4365#endif
4366 go_deeper(stack, depth, 0);
4367 ++depth;
4368 sp = &stack[depth];
4369 sp->ts_prefixdepth = depth - 1;
4370 byts = fbyts;
4371 idxs = fidxs;
4372 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004373
Bram Moolenaar4770d092006-01-12 23:22:24 +00004374 /* Move the prefix to preword[] with the right case
4375 * and make find_keepcap_word() works. */
4376 tword[sp->ts_twordlen] = NUL;
4377 make_case_word(tword + sp->ts_splitoff,
4378 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004379 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004380 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004381 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004382 break;
4383 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004384
Bram Moolenaar4770d092006-01-12 23:22:24 +00004385 if (sp->ts_curi > len || byts[arridx] != 0)
4386 {
4387 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004388 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004389 sp->ts_state = STATE_ENDNUL;
4390 sp->ts_save_badflags = su->su_badflags;
4391 break;
4392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004393
Bram Moolenaar4770d092006-01-12 23:22:24 +00004394 /*
4395 * End of word in tree.
4396 */
4397 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398
Bram Moolenaar4770d092006-01-12 23:22:24 +00004399 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004400
4401 /* Skip words with the NOSUGGEST flag. */
4402 if (flags & WF_NOSUGGEST)
4403 break;
4404
Bram Moolenaar4770d092006-01-12 23:22:24 +00004405 fword_ends = (fword[sp->ts_fidx] == NUL
4406 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004407 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004408 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004409 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410
Bram Moolenaar4770d092006-01-12 23:22:24 +00004411 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004412 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004413 {
4414 /* There was a prefix before the word. Check that the prefix
4415 * can be used with this word. */
4416 /* Count the length of the NULs in the prefix. If there are
4417 * none this must be the first try without a prefix. */
4418 n = stack[sp->ts_prefixdepth].ts_arridx;
4419 len = pbyts[n++];
4420 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4421 ;
4422 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004423 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004424 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004425 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004426 if (c == 0)
4427 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004428
Bram Moolenaar4770d092006-01-12 23:22:24 +00004429 /* Use the WF_RARE flag for a rare prefix. */
4430 if (c & WF_RAREPFX)
4431 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004432
Bram Moolenaar4770d092006-01-12 23:22:24 +00004433 /* Tricky: when checking for both prefix and compounding
4434 * we run into the prefix flag first.
4435 * Remember that it's OK, so that we accept the prefix
4436 * when arriving at a compound flag. */
4437 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004438 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004439 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004440
Bram Moolenaar4770d092006-01-12 23:22:24 +00004441 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4442 * appending another compound word below. */
4443 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004444 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004445 goodword_ends = FALSE;
4446 else
4447 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004448
Bram Moolenaar4770d092006-01-12 23:22:24 +00004449 p = NULL;
4450 compound_ok = TRUE;
4451 if (sp->ts_complen > sp->ts_compsplit)
4452 {
4453 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004454 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004455 /* There was a word before this word. When there was no
4456 * change in this word (it was correct) add the first word
4457 * as a suggestion. If this word was corrected too, we
4458 * need to check if a correct word follows. */
4459 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004460 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004461 && STRNCMP(fword + sp->ts_splitfidx,
4462 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004463 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004464 {
4465 preword[sp->ts_prewordlen] = NUL;
4466 newscore = score_wordcount_adj(slang, sp->ts_score,
4467 preword + sp->ts_prewordlen,
4468 sp->ts_prewordlen > 0);
4469 /* Add the suggestion if the score isn't too bad. */
4470 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004471 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004472 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004473 newscore, 0, FALSE,
4474 lp->lp_sallang, FALSE);
4475 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004476 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004477 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004478 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004479 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004480 /* There was a compound word before this word. If this
4481 * word does not support compounding then give up
4482 * (splitting is tried for the word without compound
4483 * flag). */
4484 if (((unsigned)flags >> 24) == 0
4485 || sp->ts_twordlen - sp->ts_splitoff
4486 < slang->sl_compminlen)
4487 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004488 /* For multi-byte chars check character length against
4489 * COMPOUNDMIN. */
4490 if (has_mbyte
4491 && slang->sl_compminlen > 0
4492 && mb_charlen(tword + sp->ts_splitoff)
4493 < slang->sl_compminlen)
4494 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004495
Bram Moolenaar4770d092006-01-12 23:22:24 +00004496 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4497 compflags[sp->ts_complen + 1] = NUL;
4498 vim_strncpy(preword + sp->ts_prewordlen,
4499 tword + sp->ts_splitoff,
4500 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004501
4502 /* Verify CHECKCOMPOUNDPATTERN rules. */
4503 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4504 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004505 compound_ok = FALSE;
4506
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004507 if (compound_ok)
4508 {
4509 p = preword;
4510 while (*skiptowhite(p) != NUL)
4511 p = skipwhite(skiptowhite(p));
4512 if (fword_ends && !can_compound(slang, p,
4513 compflags + sp->ts_compsplit))
4514 /* Compound is not allowed. But it may still be
4515 * possible if we add another (short) word. */
4516 compound_ok = FALSE;
4517 }
4518
Bram Moolenaar4770d092006-01-12 23:22:24 +00004519 /* Get pointer to last char of previous word. */
4520 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004521 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004522 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004523 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004524
Bram Moolenaar4770d092006-01-12 23:22:24 +00004525 /*
4526 * Form the word with proper case in preword.
4527 * If there is a word from a previous split, append.
4528 * For the soundfold tree don't change the case, simply append.
4529 */
4530 if (soundfold)
4531 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4532 else if (flags & WF_KEEPCAP)
4533 /* Must find the word in the keep-case tree. */
4534 find_keepcap_word(slang, tword + sp->ts_splitoff,
4535 preword + sp->ts_prewordlen);
4536 else
4537 {
4538 /* Include badflags: If the badword is onecap or allcap
4539 * use that for the goodword too. But if the badword is
4540 * allcap and it's only one char long use onecap. */
4541 c = su->su_badflags;
4542 if ((c & WF_ALLCAP)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004543 && su->su_badlen == (*mb_ptr2len)(su->su_badptr))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004544 c = WF_ONECAP;
4545 c |= flags;
4546
4547 /* When appending a compound word after a word character don't
4548 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004549 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004550 c &= ~WF_ONECAP;
4551 make_case_word(tword + sp->ts_splitoff,
4552 preword + sp->ts_prewordlen, c);
4553 }
4554
4555 if (!soundfold)
4556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004557 /* Don't use a banned word. It may appear again as a good
4558 * word, thus remember it. */
4559 if (flags & WF_BANNED)
4560 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004561 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 break;
4563 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004564 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004565 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4566 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004567 {
4568 if (slang->sl_compprog == NULL)
4569 break;
4570 /* the word so far was banned but we may try compounding */
4571 goodword_ends = FALSE;
4572 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004573 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574
Bram Moolenaar4770d092006-01-12 23:22:24 +00004575 newscore = 0;
4576 if (!soundfold) /* soundfold words don't have flags */
4577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004578 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004579 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 newscore += SCORE_REGION;
4581 if (flags & WF_RARE)
4582 newscore += SCORE_RARE;
4583
Bram Moolenaar0c405862005-06-22 22:26:26 +00004584 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004585 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004586 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004587 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004588
Bram Moolenaar4770d092006-01-12 23:22:24 +00004589 /* TODO: how about splitting in the soundfold tree? */
4590 if (fword_ends
4591 && goodword_ends
4592 && sp->ts_fidx >= sp->ts_fidxtry
4593 && compound_ok)
4594 {
4595 /* The badword also ends: add suggestions. */
4596#ifdef DEBUG_TRIEWALK
4597 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004598 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004599 int j;
4600
4601 /* print the stack of changes that brought us here */
4602 smsg("------ %s -------", fword);
4603 for (j = 0; j < depth; ++j)
4604 smsg("%s", changename[j]);
4605 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004606#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004607 if (soundfold)
4608 {
4609 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004610 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004611 add_sound_suggest(su, preword, sp->ts_score, lp);
4612 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004613 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004614 {
4615 /* Give a penalty when changing non-word char to word
4616 * char, e.g., "thes," -> "these". */
4617 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004618 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004619 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004620 {
4621 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004622 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004623 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004624 newscore += SCORE_NONWORD;
4625 }
4626
Bram Moolenaar4770d092006-01-12 23:22:24 +00004627 /* Give a bonus to words seen before. */
4628 score = score_wordcount_adj(slang,
4629 sp->ts_score + newscore,
4630 preword + sp->ts_prewordlen,
4631 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004632
Bram Moolenaar4770d092006-01-12 23:22:24 +00004633 /* Add the suggestion if the score isn't too bad. */
4634 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004635 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004636 add_suggestion(su, &su->su_ga, preword,
4637 sp->ts_fidx - repextra,
4638 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004639
4640 if (su->su_badflags & WF_MIXCAP)
4641 {
4642 /* We really don't know if the word should be
4643 * upper or lower case, add both. */
4644 c = captype(preword, NULL);
4645 if (c == 0 || c == WF_ALLCAP)
4646 {
4647 make_case_word(tword + sp->ts_splitoff,
4648 preword + sp->ts_prewordlen,
4649 c == 0 ? WF_ALLCAP : 0);
4650
4651 add_suggestion(su, &su->su_ga, preword,
4652 sp->ts_fidx - repextra,
4653 score + SCORE_ICASE, 0, FALSE,
4654 lp->lp_sallang, FALSE);
4655 }
4656 }
4657 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004658 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004659 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004660
Bram Moolenaar4770d092006-01-12 23:22:24 +00004661 /*
4662 * Try word split and/or compounding.
4663 */
4664 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004665 /* Don't split halfway a character. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004666 && (!has_mbyte || sp->ts_tcharlen == 0))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004667 {
4668 int try_compound;
4669 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004670
Bram Moolenaar4770d092006-01-12 23:22:24 +00004671 /* If past the end of the bad word don't try a split.
4672 * Otherwise try changing the next word. E.g., find
4673 * suggestions for "the the" where the second "the" is
4674 * different. It's done like a split.
4675 * TODO: word split for soundfold words */
4676 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4677 && !soundfold;
4678
4679 /* Get here in several situations:
4680 * 1. The word in the tree ends:
4681 * If the word allows compounding try that. Otherwise try
4682 * a split by inserting a space. For both check that a
4683 * valid words starts at fword[sp->ts_fidx].
4684 * For NOBREAK do like compounding to be able to check if
4685 * the next word is valid.
4686 * 2. The badword does end, but it was due to a change (e.g.,
4687 * a swap). No need to split, but do check that the
4688 * following word is valid.
4689 * 3. The badword and the word in the tree end. It may still
4690 * be possible to compound another (short) word.
4691 */
4692 try_compound = FALSE;
4693 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004694 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004695 && slang->sl_compprog != NULL
4696 && ((unsigned)flags >> 24) != 0
4697 && sp->ts_twordlen - sp->ts_splitoff
4698 >= slang->sl_compminlen
Bram Moolenaar4770d092006-01-12 23:22:24 +00004699 && (!has_mbyte
4700 || slang->sl_compminlen == 0
4701 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004702 >= slang->sl_compminlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004703 && (slang->sl_compsylmax < MAXWLEN
4704 || sp->ts_complen + 1 - sp->ts_compsplit
4705 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004706 && (can_be_compound(sp, slang,
4707 compflags, ((unsigned)flags >> 24))))
4708
Bram Moolenaar4770d092006-01-12 23:22:24 +00004709 {
4710 try_compound = TRUE;
4711 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4712 compflags[sp->ts_complen + 1] = NUL;
4713 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004714
Bram Moolenaar4770d092006-01-12 23:22:24 +00004715 /* For NOBREAK we never try splitting, it won't make any word
4716 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004717 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004718 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004719
Bram Moolenaar4770d092006-01-12 23:22:24 +00004720 /* If we could add a compound word, and it's also possible to
4721 * split at this point, do the split first and set
4722 * TSF_DIDSPLIT to avoid doing it again. */
4723 else if (!fword_ends
4724 && try_compound
4725 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4726 {
4727 try_compound = FALSE;
4728 sp->ts_flags |= TSF_DIDSPLIT;
4729 --sp->ts_curi; /* do the same NUL again */
4730 compflags[sp->ts_complen] = NUL;
4731 }
4732 else
4733 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004734
Bram Moolenaar4770d092006-01-12 23:22:24 +00004735 if (try_split || try_compound)
4736 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004737 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004738 {
4739 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004740 * words so far are valid for compounding. If there
4741 * is only one word it must not have the NEEDCOMPOUND
4742 * flag. */
4743 if (sp->ts_complen == sp->ts_compsplit
4744 && (flags & WF_NEEDCOMP))
4745 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004746 p = preword;
4747 while (*skiptowhite(p) != NUL)
4748 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004749 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004750 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004751 compflags + sp->ts_compsplit))
4752 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004753
4754 if (slang->sl_nosplitsugs)
4755 newscore += SCORE_SPLIT_NO;
4756 else
4757 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004758
4759 /* Give a bonus to words seen before. */
4760 newscore = score_wordcount_adj(slang, newscore,
4761 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004762 }
4763
Bram Moolenaar4770d092006-01-12 23:22:24 +00004764 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004766 go_deeper(stack, depth, newscore);
4767#ifdef DEBUG_TRIEWALK
4768 if (!try_compound && !fword_ends)
4769 sprintf(changename[depth], "%.*s-%s: split",
4770 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4771 else
4772 sprintf(changename[depth], "%.*s-%s: compound",
4773 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4774#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004775 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004776 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004777 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004778 sp->ts_state = STATE_SPLITUNDO;
4779
4780 ++depth;
4781 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004783 /* Append a space to preword when splitting. */
4784 if (!try_compound && !fword_ends)
4785 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004786 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004787 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004788 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004789
4790 /* If the badword has a non-word character at this
4791 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004792 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004793 * character when the word ends. But only when the
4794 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004795 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004796 + sp->ts_fidx,
4797 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004798 || fword_ends)
4799 && fword[sp->ts_fidx] != NUL
4800 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004801 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004802 int l;
4803
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004804 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004805 if (fword_ends)
4806 {
4807 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004808 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004809 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004810 sp->ts_prewordlen += l;
4811 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004812 }
4813 else
4814 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4815 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004816 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004817
Bram Moolenaard12a1322005-08-21 22:08:24 +00004818 /* When compounding include compound flag in
4819 * compflags[] (already set above). When splitting we
4820 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004821 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004822 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004823 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004824 sp->ts_compsplit = sp->ts_complen;
4825 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004826
Bram Moolenaar53805d12005-08-01 07:08:33 +00004827 /* set su->su_badflags to the caps type at this
4828 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004829 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004830 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004831 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004832 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004833 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004834 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004836 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004837 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004838
4839 /* If there are postponed prefixes, try these too. */
4840 if (pbyts != NULL)
4841 {
4842 byts = pbyts;
4843 idxs = pidxs;
4844 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004845 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004846 sp->ts_state = STATE_NOPREFIX;
4847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 }
4849 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004850 }
4851 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004852
Bram Moolenaar4770d092006-01-12 23:22:24 +00004853 case STATE_SPLITUNDO:
4854 /* Undo the changes done for word split or compound word. */
4855 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856
Bram Moolenaar4770d092006-01-12 23:22:24 +00004857 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004858 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004859 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004860
Bram Moolenaar4770d092006-01-12 23:22:24 +00004861 /* In case we went into the prefix tree. */
4862 byts = fbyts;
4863 idxs = fidxs;
4864 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865
Bram Moolenaar4770d092006-01-12 23:22:24 +00004866 case STATE_ENDNUL:
4867 /* Past the NUL bytes in the node. */
4868 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004869 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004870 {
4871 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004872 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004873 sp->ts_state = STATE_DEL;
4874 break;
4875 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004876 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004877 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004878 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004879
4880 case STATE_PLAIN:
4881 /*
4882 * Go over all possible bytes at this node, add each to tword[]
4883 * and use child node. "ts_curi" is the index.
4884 */
4885 arridx = sp->ts_arridx;
4886 if (sp->ts_curi > byts[arridx])
4887 {
4888 /* Done all bytes at this node, do next state. When still at
4889 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004890 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004891 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004892 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004893 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004894 sp->ts_state = STATE_FINAL;
4895 }
4896 else
4897 {
4898 arridx += sp->ts_curi++;
4899 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004900
Bram Moolenaar4770d092006-01-12 23:22:24 +00004901 /* Normal byte, go one level deeper. If it's not equal to the
4902 * byte in the bad word adjust the score. But don't even try
4903 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01004904 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00004905 * delete + substitute. */
4906 if (c == fword[sp->ts_fidx]
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004907 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004908 newscore = 0;
4909 else
4910 newscore = SCORE_SUBST;
4911 if ((newscore == 0
4912 || (sp->ts_fidx >= sp->ts_fidxtry
4913 && ((sp->ts_flags & TSF_DIDDEL) == 0
4914 || c != fword[sp->ts_delidx])))
4915 && TRY_DEEPER(su, stack, depth, newscore))
4916 {
4917 go_deeper(stack, depth, newscore);
4918#ifdef DEBUG_TRIEWALK
4919 if (newscore > 0)
4920 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
4921 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4922 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004923 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004924 sprintf(changename[depth], "%.*s-%s: accept %c",
4925 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4926 fword[sp->ts_fidx]);
4927#endif
4928 ++depth;
4929 sp = &stack[depth];
4930 ++sp->ts_fidx;
4931 tword[sp->ts_twordlen++] = c;
4932 sp->ts_arridx = idxs[arridx];
Bram Moolenaar4770d092006-01-12 23:22:24 +00004933 if (newscore == SCORE_SUBST)
4934 sp->ts_isdiff = DIFF_YES;
4935 if (has_mbyte)
4936 {
4937 /* Multi-byte characters are a bit complicated to
4938 * handle: They differ when any of the bytes differ
4939 * and then their length may also differ. */
4940 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004941 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004942 /* First byte. */
4943 sp->ts_tcharidx = 0;
4944 sp->ts_tcharlen = MB_BYTE2LEN(c);
4945 sp->ts_fcharstart = sp->ts_fidx - 1;
4946 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004947 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004948 }
4949 else if (sp->ts_isdiff == DIFF_INSERT)
4950 /* When inserting trail bytes don't advance in the
4951 * bad word. */
4952 --sp->ts_fidx;
4953 if (++sp->ts_tcharidx == sp->ts_tcharlen)
4954 {
4955 /* Last byte of character. */
4956 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00004957 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004958 /* Correct ts_fidx for the byte length of the
4959 * character (we didn't check that before). */
4960 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004961 + MB_PTR2LEN(
4962 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004963 /* For changing a composing character adjust
4964 * the score from SCORE_SUBST to
4965 * SCORE_SUBCOMP. */
4966 if (enc_utf8
4967 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004968 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00004969 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004970 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004971 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004972 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004973 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004974 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004975 SCORE_SUBST - SCORE_SUBCOMP;
4976
Bram Moolenaar4770d092006-01-12 23:22:24 +00004977 /* For a similar character adjust score from
4978 * SCORE_SUBST to SCORE_SIMILAR. */
4979 else if (!soundfold
4980 && slang->sl_has_map
4981 && similar_chars(slang,
4982 mb_ptr2char(tword
4983 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00004984 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00004985 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00004986 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004987 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00004988 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00004989 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004990 else if (sp->ts_isdiff == DIFF_INSERT
4991 && sp->ts_twordlen > sp->ts_tcharlen)
4992 {
4993 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
4994 c = mb_ptr2char(p);
4995 if (enc_utf8 && utf_iscomposing(c))
4996 {
4997 /* Inserting a composing char doesn't
4998 * count that much. */
4999 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5000 }
5001 else
5002 {
5003 /* If the previous character was the same,
5004 * thus doubling a character, give a bonus
5005 * to the score. Also for the soundfold
5006 * tree (might seem illogical but does
5007 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005008 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005009 if (c == mb_ptr2char(p))
5010 sp->ts_score -= SCORE_INS
5011 - SCORE_INSDUP;
5012 }
5013 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005014
Bram Moolenaar4770d092006-01-12 23:22:24 +00005015 /* Starting a new char, reset the length. */
5016 sp->ts_tcharlen = 0;
5017 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005018 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005019 else
Bram Moolenaarea408852005-06-25 22:49:46 +00005020 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005021 /* If we found a similar char adjust the score.
5022 * We do this after calling go_deeper() because
5023 * it's slow. */
5024 if (newscore != 0
5025 && !soundfold
5026 && slang->sl_has_map
5027 && similar_chars(slang,
5028 c, fword[sp->ts_fidx - 1]))
5029 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005031 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005032 }
5033 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005034
Bram Moolenaar4770d092006-01-12 23:22:24 +00005035 case STATE_DEL:
Bram Moolenaar4770d092006-01-12 23:22:24 +00005036 /* When past the first byte of a multi-byte char don't try
5037 * delete/insert/swap a character. */
5038 if (has_mbyte && sp->ts_tcharlen > 0)
5039 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005040 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005041 sp->ts_state = STATE_FINAL;
5042 break;
5043 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005044 /*
5045 * Try skipping one character in the bad word (delete it).
5046 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005047 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005048 sp->ts_state = STATE_INS_PREP;
5049 sp->ts_curi = 1;
5050 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5051 /* Deleting a vowel at the start of a word counts less, see
5052 * soundalike_score(). */
5053 newscore = 2 * SCORE_DEL / 3;
5054 else
5055 newscore = SCORE_DEL;
5056 if (fword[sp->ts_fidx] != NUL
5057 && TRY_DEEPER(su, stack, depth, newscore))
5058 {
5059 go_deeper(stack, depth, newscore);
5060#ifdef DEBUG_TRIEWALK
5061 sprintf(changename[depth], "%.*s-%s: delete %c",
5062 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5063 fword[sp->ts_fidx]);
5064#endif
5065 ++depth;
5066
5067 /* Remember what character we deleted, so that we can avoid
5068 * inserting it again. */
5069 stack[depth].ts_flags |= TSF_DIDDEL;
5070 stack[depth].ts_delidx = sp->ts_fidx;
5071
5072 /* Advance over the character in fword[]. Give a bonus to the
5073 * score if the same character is following "nn" -> "n". It's
5074 * a bit illogical for soundfold tree but it does give better
5075 * results. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005076 if (has_mbyte)
5077 {
5078 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005079 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005080 if (enc_utf8 && utf_iscomposing(c))
5081 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5082 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5083 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5084 }
5085 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005086 {
5087 ++stack[depth].ts_fidx;
5088 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5089 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5090 }
5091 break;
5092 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005093 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005094
5095 case STATE_INS_PREP:
5096 if (sp->ts_flags & TSF_DIDDEL)
5097 {
5098 /* If we just deleted a byte then inserting won't make sense,
5099 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005100 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005101 sp->ts_state = STATE_SWAP;
5102 break;
5103 }
5104
5105 /* skip over NUL bytes */
5106 n = sp->ts_arridx;
5107 for (;;)
5108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005109 if (sp->ts_curi > byts[n])
5110 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005111 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005112 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005113 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005114 break;
5115 }
5116 if (byts[n + sp->ts_curi] != NUL)
5117 {
5118 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005119 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005120 sp->ts_state = STATE_INS;
5121 break;
5122 }
5123 ++sp->ts_curi;
5124 }
5125 break;
5126
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005127 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005128
5129 case STATE_INS:
5130 /* Insert one byte. Repeat this for each possible byte at this
5131 * node. */
5132 n = sp->ts_arridx;
5133 if (sp->ts_curi > byts[n])
5134 {
5135 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005136 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005137 sp->ts_state = STATE_SWAP;
5138 break;
5139 }
5140
5141 /* Do one more byte at this node, but:
5142 * - Skip NUL bytes.
5143 * - Skip the byte if it's equal to the byte in the word,
5144 * accepting that byte is always better.
5145 */
5146 n += sp->ts_curi++;
5147 c = byts[n];
5148 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5149 /* Inserting a vowel at the start of a word counts less,
5150 * see soundalike_score(). */
5151 newscore = 2 * SCORE_INS / 3;
5152 else
5153 newscore = SCORE_INS;
5154 if (c != fword[sp->ts_fidx]
5155 && TRY_DEEPER(su, stack, depth, newscore))
5156 {
5157 go_deeper(stack, depth, newscore);
5158#ifdef DEBUG_TRIEWALK
5159 sprintf(changename[depth], "%.*s-%s: insert %c",
5160 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5161 c);
5162#endif
5163 ++depth;
5164 sp = &stack[depth];
5165 tword[sp->ts_twordlen++] = c;
5166 sp->ts_arridx = idxs[n];
Bram Moolenaar4770d092006-01-12 23:22:24 +00005167 if (has_mbyte)
5168 {
5169 fl = MB_BYTE2LEN(c);
5170 if (fl > 1)
5171 {
5172 /* There are following bytes for the same character.
5173 * We must find all bytes before trying
5174 * delete/insert/swap/etc. */
5175 sp->ts_tcharlen = fl;
5176 sp->ts_tcharidx = 1;
5177 sp->ts_isdiff = DIFF_INSERT;
5178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 }
5180 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005181 fl = 1;
5182 if (fl == 1)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005183 {
5184 /* If the previous character was the same, thus doubling a
5185 * character, give a bonus to the score. Also for
5186 * soundfold words (illogical but does give a better
5187 * score). */
5188 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005189 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005190 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005191 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005192 }
5193 break;
5194
5195 case STATE_SWAP:
5196 /*
5197 * Swap two bytes in the bad word: "12" -> "21".
5198 * We change "fword" here, it's changed back afterwards at
5199 * STATE_UNSWAP.
5200 */
5201 p = fword + sp->ts_fidx;
5202 c = *p;
5203 if (c == NUL)
5204 {
5205 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005206 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005207 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210
Bram Moolenaar4770d092006-01-12 23:22:24 +00005211 /* Don't swap if the first character is not a word character.
5212 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005213 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005214 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005215 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005216 sp->ts_state = STATE_REP_INI;
5217 break;
5218 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005219
Bram Moolenaar4770d092006-01-12 23:22:24 +00005220 if (has_mbyte)
5221 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005222 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005223 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005224 if (p[n] == NUL)
5225 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005226 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005227 c2 = c; /* don't swap non-word char */
5228 else
5229 c2 = mb_ptr2char(p + n);
5230 }
5231 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005232 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005233 if (p[1] == NUL)
5234 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005235 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005236 c2 = c; /* don't swap non-word char */
5237 else
5238 c2 = p[1];
5239 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005240
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005241 /* When the second character is NUL we can't swap. */
5242 if (c2 == NUL)
5243 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005244 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005245 sp->ts_state = STATE_REP_INI;
5246 break;
5247 }
5248
Bram Moolenaar4770d092006-01-12 23:22:24 +00005249 /* When characters are identical, swap won't do anything.
5250 * Also get here if the second char is not a word character. */
5251 if (c == c2)
5252 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005253 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005254 sp->ts_state = STATE_SWAP3;
5255 break;
5256 }
5257 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5258 {
5259 go_deeper(stack, depth, SCORE_SWAP);
5260#ifdef DEBUG_TRIEWALK
5261 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5262 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5263 c, c2);
5264#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005265 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005266 sp->ts_state = STATE_UNSWAP;
5267 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005268 if (has_mbyte)
5269 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005270 fl = mb_char2len(c2);
5271 mch_memmove(p, p + n, fl);
5272 mb_char2bytes(c, p + fl);
5273 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005274 }
5275 else
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005276 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005277 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005278 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005279 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005280 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005281 }
5282 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005283 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005284 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005285 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005286 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005287 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005288 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005289
Bram Moolenaar4770d092006-01-12 23:22:24 +00005290 case STATE_UNSWAP:
5291 /* Undo the STATE_SWAP swap: "21" -> "12". */
5292 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005293 if (has_mbyte)
5294 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005295 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005296 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005297 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005298 mb_char2bytes(c, p);
5299 }
5300 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005301 {
5302 c = *p;
5303 *p = p[1];
5304 p[1] = c;
5305 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005306 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005307
5308 case STATE_SWAP3:
5309 /* Swap two bytes, skipping one: "123" -> "321". We change
5310 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5311 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005312 if (has_mbyte)
5313 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005314 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005315 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005316 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005317 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005319 c3 = c; /* don't swap non-word char */
5320 else
5321 c3 = mb_ptr2char(p + n + fl);
5322 }
5323 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005324 {
5325 c = *p;
5326 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005327 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005328 c3 = c; /* don't swap non-word char */
5329 else
5330 c3 = p[2];
5331 }
5332
5333 /* When characters are identical: "121" then SWAP3 result is
5334 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5335 * same as SWAP on next char: "112". Thus skip all swapping.
5336 * Also skip when c3 is NUL.
5337 * Also get here when the third character is not a word character.
5338 * Second character may any char: "a.b" -> "b.a" */
5339 if (c == c3 || c3 == NUL)
5340 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005341 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005342 sp->ts_state = STATE_REP_INI;
5343 break;
5344 }
5345 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5346 {
5347 go_deeper(stack, depth, SCORE_SWAP3);
5348#ifdef DEBUG_TRIEWALK
5349 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5350 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5351 c, c3);
5352#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005353 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005354 sp->ts_state = STATE_UNSWAP3;
5355 ++depth;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005356 if (has_mbyte)
5357 {
5358 tl = mb_char2len(c3);
5359 mch_memmove(p, p + n + fl, tl);
5360 mb_char2bytes(c2, p + tl);
5361 mb_char2bytes(c, p + fl + tl);
5362 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5363 }
5364 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005365 {
5366 p[0] = p[2];
5367 p[2] = c;
5368 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5369 }
5370 }
5371 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005372 {
5373 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005374 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005375 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005376 break;
5377
5378 case STATE_UNSWAP3:
5379 /* Undo STATE_SWAP3: "321" -> "123" */
5380 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005381 if (has_mbyte)
5382 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005383 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005384 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005385 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005386 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005387 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005388 mch_memmove(p + fl + tl, p, n);
5389 mb_char2bytes(c, p);
5390 mb_char2bytes(c2, p + tl);
5391 p = p + tl;
5392 }
5393 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005394 {
5395 c = *p;
5396 *p = p[2];
5397 p[2] = c;
5398 ++p;
5399 }
5400
Bram Moolenaar860cae12010-06-05 23:22:07 +02005401 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005402 {
5403 /* Middle char is not a word char, skip the rotate. First and
5404 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005405 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005406 sp->ts_state = STATE_REP_INI;
5407 break;
5408 }
5409
5410 /* Rotate three characters left: "123" -> "231". We change
5411 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5412 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5413 {
5414 go_deeper(stack, depth, SCORE_SWAP3);
5415#ifdef DEBUG_TRIEWALK
5416 p = fword + sp->ts_fidx;
5417 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5418 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5419 p[0], p[1], p[2]);
5420#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005421 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005422 sp->ts_state = STATE_UNROT3L;
5423 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005424 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005425 if (has_mbyte)
5426 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005427 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005428 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005429 fl = MB_CPTR2LEN(p + n);
5430 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005431 mch_memmove(p, p + n, fl);
5432 mb_char2bytes(c, p + fl);
5433 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005434 }
5435 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005436 {
5437 c = *p;
5438 *p = p[1];
5439 p[1] = p[2];
5440 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005441 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005442 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005443 }
5444 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005445 {
5446 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005447 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005448 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005449 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005450
Bram Moolenaar4770d092006-01-12 23:22:24 +00005451 case STATE_UNROT3L:
5452 /* Undo ROT3L: "231" -> "123" */
5453 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005454 if (has_mbyte)
5455 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005456 n = MB_PTR2LEN(p);
5457 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005458 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005459 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005460 mch_memmove(p + tl, p, n);
5461 mb_char2bytes(c, p);
5462 }
5463 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005464 {
5465 c = p[2];
5466 p[2] = p[1];
5467 p[1] = *p;
5468 *p = c;
5469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005470
Bram Moolenaar4770d092006-01-12 23:22:24 +00005471 /* Rotate three bytes right: "123" -> "312". We change "fword"
5472 * here, it's changed back afterwards at STATE_UNROT3R. */
5473 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5474 {
5475 go_deeper(stack, depth, SCORE_SWAP3);
5476#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005477 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005478 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5479 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5480 p[0], p[1], p[2]);
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_UNROT3R;
5484 ++depth;
5485 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005486 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005487 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005488 n = MB_CPTR2LEN(p);
5489 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005490 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005491 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005492 mch_memmove(p + tl, p, n);
5493 mb_char2bytes(c, p);
5494 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005495 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005496 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005497 {
5498 c = p[2];
5499 p[2] = p[1];
5500 p[1] = *p;
5501 *p = c;
5502 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5503 }
5504 }
5505 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005506 {
5507 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005508 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005509 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005510 break;
5511
5512 case STATE_UNROT3R:
5513 /* Undo ROT3R: "312" -> "123" */
5514 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005515 if (has_mbyte)
5516 {
5517 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005518 tl = MB_PTR2LEN(p);
5519 n = MB_PTR2LEN(p + tl);
5520 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005521 mch_memmove(p, p + tl, n);
5522 mb_char2bytes(c, p + n);
5523 }
5524 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005525 {
5526 c = *p;
5527 *p = p[1];
5528 p[1] = p[2];
5529 p[2] = c;
5530 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005531 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005532
5533 case STATE_REP_INI:
5534 /* Check if matching with REP items from the .aff file would work.
5535 * Quickly skip if:
5536 * - there are no REP items and we are not in the soundfold trie
5537 * - the score is going to be too high anyway
5538 * - already applied a REP item or swapped here */
5539 if ((lp->lp_replang == NULL && !soundfold)
5540 || sp->ts_score + SCORE_REP >= su->su_maxscore
5541 || sp->ts_fidx < sp->ts_fidxtry)
5542 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005543 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005544 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005545 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547
Bram Moolenaar4770d092006-01-12 23:22:24 +00005548 /* Use the first byte to quickly find the first entry that may
5549 * match. If the index is -1 there is none. */
5550 if (soundfold)
5551 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5552 else
5553 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005554
Bram Moolenaar4770d092006-01-12 23:22:24 +00005555 if (sp->ts_curi < 0)
5556 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005557 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005558 sp->ts_state = STATE_FINAL;
5559 break;
5560 }
5561
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005562 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005563 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005564 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005565
5566 case STATE_REP:
5567 /* Try matching with REP items from the .aff file. For each match
5568 * replace the characters and check if the resulting word is
5569 * valid. */
5570 p = fword + sp->ts_fidx;
5571
5572 if (soundfold)
5573 gap = &slang->sl_repsal;
5574 else
5575 gap = &lp->lp_replang->sl_rep;
5576 while (sp->ts_curi < gap->ga_len)
5577 {
5578 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5579 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005580 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005581 /* past possible matching entries */
5582 sp->ts_curi = gap->ga_len;
5583 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005584 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005585 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5586 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5587 {
5588 go_deeper(stack, depth, SCORE_REP);
5589#ifdef DEBUG_TRIEWALK
5590 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5591 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5592 ftp->ft_from, ftp->ft_to);
5593#endif
5594 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005595 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005596 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005597
Bram Moolenaar4770d092006-01-12 23:22:24 +00005598 /* Change the "from" to the "to" string. */
5599 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005600 fl = (int)STRLEN(ftp->ft_from);
5601 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005602 if (fl != tl)
5603 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005604 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005605 repextra += tl - fl;
5606 }
5607 mch_memmove(p, ftp->ft_to, tl);
5608 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005609 stack[depth].ts_tcharlen = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005610 break;
5611 }
5612 }
5613
5614 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005615 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005616 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005617 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005618 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005619 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005620
5621 break;
5622
5623 case STATE_REP_UNDO:
5624 /* Undo a REP replacement and continue with the next one. */
5625 if (soundfold)
5626 gap = &slang->sl_repsal;
5627 else
5628 gap = &lp->lp_replang->sl_rep;
5629 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005630 fl = (int)STRLEN(ftp->ft_from);
5631 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005632 p = fword + sp->ts_fidx;
5633 if (fl != tl)
5634 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005635 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005636 repextra -= tl - fl;
5637 }
5638 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005639 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005640 sp->ts_state = STATE_REP;
5641 break;
5642
5643 default:
5644 /* Did all possible states at this level, go up one level. */
5645 --depth;
5646
5647 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5648 {
5649 /* Continue in or go back to the prefix tree. */
5650 byts = pbyts;
5651 idxs = pidxs;
5652 }
5653
5654 /* Don't check for CTRL-C too often, it takes time. */
5655 if (--breakcheckcount == 0)
5656 {
5657 ui_breakcheck();
5658 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005660 }
5661 }
5662}
5663
Bram Moolenaar4770d092006-01-12 23:22:24 +00005664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005665/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005666 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005667 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005668 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005669go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005670{
Bram Moolenaarea424162005-06-16 21:51:00 +00005671 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005672 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005673 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005674 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005675 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005676}
5677
Bram Moolenaar53805d12005-08-01 07:08:33 +00005678/*
5679 * Case-folding may change the number of bytes: Count nr of chars in
5680 * fword[flen] and return the byte length of that many chars in "word".
5681 */
5682 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005683nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005684{
5685 char_u *p;
5686 int i = 0;
5687
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005688 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005689 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005690 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005691 --i;
5692 return (int)(p - word);
5693}
Bram Moolenaar53805d12005-08-01 07:08:33 +00005694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005695/*
5696 * "fword" is a good word with case folded. Find the matching keep-case
5697 * words and put it in "kword".
5698 * Theoretically there could be several keep-case words that result in the
5699 * same case-folded word, but we only find one...
5700 */
5701 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005702find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005703{
5704 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5705 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005706 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005707
5708 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005709 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005710 int round[MAXWLEN];
5711 int fwordidx[MAXWLEN];
5712 int uwordidx[MAXWLEN];
5713 int kwordlen[MAXWLEN];
5714
5715 int flen, ulen;
5716 int l;
5717 int len;
5718 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005719 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005720 char_u *p;
5721 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005722 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005723
5724 if (byts == NULL)
5725 {
5726 /* array is empty: "cannot happen" */
5727 *kword = NUL;
5728 return;
5729 }
5730
5731 /* Make an all-cap version of "fword". */
5732 allcap_copy(fword, uword);
5733
5734 /*
5735 * Each character needs to be tried both case-folded and upper-case.
5736 * All this gets very complicated if we keep in mind that changing case
5737 * may change the byte length of a multi-byte character...
5738 */
5739 depth = 0;
5740 arridx[0] = 0;
5741 round[0] = 0;
5742 fwordidx[0] = 0;
5743 uwordidx[0] = 0;
5744 kwordlen[0] = 0;
5745 while (depth >= 0)
5746 {
5747 if (fword[fwordidx[depth]] == NUL)
5748 {
5749 /* We are at the end of "fword". If the tree allows a word to end
5750 * here we have found a match. */
5751 if (byts[arridx[depth] + 1] == 0)
5752 {
5753 kword[kwordlen[depth]] = NUL;
5754 return;
5755 }
5756
5757 /* kword is getting too long, continue one level up */
5758 --depth;
5759 }
5760 else if (++round[depth] > 2)
5761 {
5762 /* tried both fold-case and upper-case character, continue one
5763 * level up */
5764 --depth;
5765 }
5766 else
5767 {
5768 /*
5769 * round[depth] == 1: Try using the folded-case character.
5770 * round[depth] == 2: Try using the upper-case character.
5771 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005772 if (has_mbyte)
5773 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005774 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5775 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005776 }
5777 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005778 ulen = flen = 1;
5779 if (round[depth] == 1)
5780 {
5781 p = fword + fwordidx[depth];
5782 l = flen;
5783 }
5784 else
5785 {
5786 p = uword + uwordidx[depth];
5787 l = ulen;
5788 }
5789
5790 for (tryidx = arridx[depth]; l > 0; --l)
5791 {
5792 /* Perform a binary search in the list of accepted bytes. */
5793 len = byts[tryidx++];
5794 c = *p++;
5795 lo = tryidx;
5796 hi = tryidx + len - 1;
5797 while (lo < hi)
5798 {
5799 m = (lo + hi) / 2;
5800 if (byts[m] > c)
5801 hi = m - 1;
5802 else if (byts[m] < c)
5803 lo = m + 1;
5804 else
5805 {
5806 lo = hi = m;
5807 break;
5808 }
5809 }
5810
5811 /* Stop if there is no matching byte. */
5812 if (hi < lo || byts[lo] != c)
5813 break;
5814
5815 /* Continue at the child (if there is one). */
5816 tryidx = idxs[lo];
5817 }
5818
5819 if (l == 0)
5820 {
5821 /*
5822 * Found the matching char. Copy it to "kword" and go a
5823 * level deeper.
5824 */
5825 if (round[depth] == 1)
5826 {
5827 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5828 flen);
5829 kwordlen[depth + 1] = kwordlen[depth] + flen;
5830 }
5831 else
5832 {
5833 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5834 ulen);
5835 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5836 }
5837 fwordidx[depth + 1] = fwordidx[depth] + flen;
5838 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5839
5840 ++depth;
5841 arridx[depth] = tryidx;
5842 round[depth] = 0;
5843 }
5844 }
5845 }
5846
5847 /* Didn't find it: "cannot happen". */
5848 *kword = NUL;
5849}
5850
5851/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005852 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
5853 * su->su_sga.
5854 */
5855 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005856score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005857{
5858 langp_T *lp;
5859 char_u badsound[MAXWLEN];
5860 int i;
5861 suggest_T *stp;
5862 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005863 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005864 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005865
5866 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
5867 return;
5868
5869 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005870 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005871 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005872 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005873 if (lp->lp_slang->sl_sal.ga_len > 0)
5874 {
5875 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005876 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005877
5878 for (i = 0; i < su->su_ga.ga_len; ++i)
5879 {
5880 stp = &SUG(su->su_ga, i);
5881
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005882 /* Case-fold the suggested word, sound-fold it and compute the
5883 * sound-a-like score. */
5884 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005885 if (score < SCORE_MAXMAX)
5886 {
5887 /* Add the suggestion. */
5888 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
5889 sstp->st_word = vim_strsave(stp->st_word);
5890 if (sstp->st_word != NULL)
5891 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005892 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005893 sstp->st_score = score;
5894 sstp->st_altscore = 0;
5895 sstp->st_orglen = stp->st_orglen;
5896 ++su->su_sga.ga_len;
5897 }
5898 }
5899 }
5900 break;
5901 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005902 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005903}
5904
5905/*
5906 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005907 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005908 */
5909 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005910score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005911{
5912 int i;
5913 int j;
5914 garray_T ga;
5915 garray_T *gap;
5916 langp_T *lp;
5917 suggest_T *stp;
5918 char_u *p;
5919 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005920 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005921 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005922 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005923
5924 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005925 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005926 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005927 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005928 if (lp->lp_slang->sl_sal.ga_len > 0)
5929 {
5930 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005931 slang = lp->lp_slang;
5932 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005933
5934 for (i = 0; i < su->su_ga.ga_len; ++i)
5935 {
5936 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005937 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005938 if (stp->st_altscore == SCORE_MAXMAX)
5939 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
5940 else
5941 stp->st_score = (stp->st_score * 3
5942 + stp->st_altscore) / 4;
5943 stp->st_salscore = FALSE;
5944 }
5945 break;
5946 }
5947 }
5948
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005949 if (slang == NULL) /* Using "double" without sound folding. */
5950 {
5951 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
5952 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005953 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005954 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005955
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005956 /* Add the alternate score to su_sga. */
5957 for (i = 0; i < su->su_sga.ga_len; ++i)
5958 {
5959 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005960 stp->st_altscore = spell_edit_score(slang,
5961 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005962 if (stp->st_score == SCORE_MAXMAX)
5963 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
5964 else
5965 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
5966 stp->st_salscore = TRUE;
5967 }
5968
Bram Moolenaar4770d092006-01-12 23:22:24 +00005969 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
5970 * for both lists. */
5971 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005972 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005973 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005974 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
5975
5976 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
5977 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
5978 return;
5979
5980 stp = &SUG(ga, 0);
5981 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
5982 {
5983 /* round 1: get a suggestion from su_ga
5984 * round 2: get a suggestion from su_sga */
5985 for (round = 1; round <= 2; ++round)
5986 {
5987 gap = round == 1 ? &su->su_ga : &su->su_sga;
5988 if (i < gap->ga_len)
5989 {
5990 /* Don't add a word if it's already there. */
5991 p = SUG(*gap, i).st_word;
5992 for (j = 0; j < ga.ga_len; ++j)
5993 if (STRCMP(stp[j].st_word, p) == 0)
5994 break;
5995 if (j == ga.ga_len)
5996 stp[ga.ga_len++] = SUG(*gap, i);
5997 else
5998 vim_free(p);
5999 }
6000 }
6001 }
6002
6003 ga_clear(&su->su_ga);
6004 ga_clear(&su->su_sga);
6005
6006 /* Truncate the list to the number of suggestions that will be displayed. */
6007 if (ga.ga_len > su->su_maxcount)
6008 {
6009 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6010 vim_free(stp[i].st_word);
6011 ga.ga_len = su->su_maxcount;
6012 }
6013
6014 su->su_ga = ga;
6015}
6016
6017/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006018 * For the goodword in "stp" compute the soundalike score compared to the
6019 * badword.
6020 */
6021 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006022stp_sal_score(
6023 suggest_T *stp,
6024 suginfo_T *su,
6025 slang_T *slang,
6026 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006027{
6028 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006029 char_u *pbad;
6030 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006031 char_u badsound2[MAXWLEN];
6032 char_u fword[MAXWLEN];
6033 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006034 char_u goodword[MAXWLEN];
6035 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006036
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006037 lendiff = (int)(su->su_badlen - stp->st_orglen);
6038 if (lendiff >= 0)
6039 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006040 else
6041 {
6042 /* soundfold the bad word with more characters following */
6043 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6044
6045 /* When joining two words the sound often changes a lot. E.g., "t he"
6046 * sounds like "t h" while "the" sounds like "@". Avoid that by
6047 * removing the space. Don't do it when the good word also contains a
6048 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006049 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006050 && *skiptowhite(stp->st_word) == NUL)
6051 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006052 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006053
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006054 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006055 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006056 }
6057
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006058 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006059 {
6060 /* Add part of the bad word to the good word, so that we soundfold
6061 * what replaces the bad word. */
6062 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006063 vim_strncpy(goodword + stp->st_wordlen,
6064 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006065 pgood = goodword;
6066 }
6067 else
6068 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006069
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006070 /* Sound-fold the word and compute the score for the difference. */
6071 spell_soundfold(slang, pgood, FALSE, goodsound);
6072
6073 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006074}
6075
Bram Moolenaar4770d092006-01-12 23:22:24 +00006076/* structure used to store soundfolded words that add_sound_suggest() has
6077 * handled already. */
6078typedef struct
6079{
6080 short sft_score; /* lowest score used */
6081 char_u sft_word[1]; /* soundfolded word, actually longer */
6082} sftword_T;
6083
6084static sftword_T dumsft;
6085#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6086#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6087
6088/*
6089 * Prepare for calling suggest_try_soundalike().
6090 */
6091 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006092suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006093{
6094 langp_T *lp;
6095 int lpi;
6096 slang_T *slang;
6097
6098 /* Do this for all languages that support sound folding and for which a
6099 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006100 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006101 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006102 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006103 slang = lp->lp_slang;
6104 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6105 /* prepare the hashtable used by add_sound_suggest() */
6106 hash_init(&slang->sl_sounddone);
6107 }
6108}
6109
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006110/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006111 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006112 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006113 */
6114 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006115suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006116{
6117 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006118 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006119 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006120 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006121
Bram Moolenaar4770d092006-01-12 23:22:24 +00006122 /* Do this for all languages that support sound folding and for which a
6123 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006124 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006125 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006126 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006127 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006128 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006129 {
6130 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006131 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006132
Bram Moolenaar4770d092006-01-12 23:22:24 +00006133 /* try all kinds of inserts/deletes/swaps/etc. */
6134 /* TODO: also soundfold the next words, so that we can try joining
6135 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006136#ifdef SUGGEST_PROFILE
6137 prof_init();
6138#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006139 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006140#ifdef SUGGEST_PROFILE
6141 prof_report("soundalike");
6142#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006143 }
6144 }
6145}
6146
6147/*
6148 * Finish up after calling suggest_try_soundalike().
6149 */
6150 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006151suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006152{
6153 langp_T *lp;
6154 int lpi;
6155 slang_T *slang;
6156 int todo;
6157 hashitem_T *hi;
6158
6159 /* Do this for all languages that support sound folding and for which a
6160 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006161 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006162 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006163 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006164 slang = lp->lp_slang;
6165 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6166 {
6167 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006168 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006169 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6170 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006171 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006172 vim_free(HI2SFT(hi));
6173 --todo;
6174 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006175
6176 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006177 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006178 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006179 }
6180 }
6181}
6182
6183/*
6184 * A match with a soundfolded word is found. Add the good word(s) that
6185 * produce this soundfolded word.
6186 */
6187 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006188add_sound_suggest(
6189 suginfo_T *su,
6190 char_u *goodword,
6191 int score, /* soundfold score */
6192 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006193{
6194 slang_T *slang = lp->lp_slang; /* language for sound folding */
6195 int sfwordnr;
6196 char_u *nrline;
6197 int orgnr;
6198 char_u theword[MAXWLEN];
6199 int i;
6200 int wlen;
6201 char_u *byts;
6202 idx_T *idxs;
6203 int n;
6204 int wordcount;
6205 int wc;
6206 int goodscore;
6207 hash_T hash;
6208 hashitem_T *hi;
6209 sftword_T *sft;
6210 int bc, gc;
6211 int limit;
6212
6213 /*
6214 * It's very well possible that the same soundfold word is found several
6215 * times with different scores. Since the following is quite slow only do
6216 * the words that have a better score than before. Use a hashtable to
6217 * remember the words that have been done.
6218 */
6219 hash = hash_hash(goodword);
6220 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6221 if (HASHITEM_EMPTY(hi))
6222 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006223 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6224 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006225 if (sft != NULL)
6226 {
6227 sft->sft_score = score;
6228 STRCPY(sft->sft_word, goodword);
6229 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6230 }
6231 }
6232 else
6233 {
6234 sft = HI2SFT(hi);
6235 if (score >= sft->sft_score)
6236 return;
6237 sft->sft_score = score;
6238 }
6239
6240 /*
6241 * Find the word nr in the soundfold tree.
6242 */
6243 sfwordnr = soundfold_find(slang, goodword);
6244 if (sfwordnr < 0)
6245 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006246 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006247 return;
6248 }
6249
6250 /*
6251 * go over the list of good words that produce this soundfold word
6252 */
6253 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6254 orgnr = 0;
6255 while (*nrline != NUL)
6256 {
6257 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6258 * previous wordnr. */
6259 orgnr += bytes2offset(&nrline);
6260
6261 byts = slang->sl_fbyts;
6262 idxs = slang->sl_fidxs;
6263
6264 /* Lookup the word "orgnr" one of the two tries. */
6265 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006266 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006267 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006268 {
6269 i = 1;
6270 if (wordcount == orgnr && byts[n + 1] == NUL)
6271 break; /* found end of word */
6272
6273 if (byts[n + 1] == NUL)
6274 ++wordcount;
6275
6276 /* skip over the NUL bytes */
6277 for ( ; byts[n + i] == NUL; ++i)
6278 if (i > byts[n]) /* safety check */
6279 {
6280 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006281 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006282 goto badword;
6283 }
6284
6285 /* One of the siblings must have the word. */
6286 for ( ; i < byts[n]; ++i)
6287 {
6288 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6289 if (wordcount + wc > orgnr)
6290 break;
6291 wordcount += wc;
6292 }
6293
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006294 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006295 n = idxs[n + i];
6296 }
6297badword:
6298 theword[wlen] = NUL;
6299
6300 /* Go over the possible flags and regions. */
6301 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6302 {
6303 char_u cword[MAXWLEN];
6304 char_u *p;
6305 int flags = (int)idxs[n + i];
6306
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006307 /* Skip words with the NOSUGGEST flag */
6308 if (flags & WF_NOSUGGEST)
6309 continue;
6310
Bram Moolenaar4770d092006-01-12 23:22:24 +00006311 if (flags & WF_KEEPCAP)
6312 {
6313 /* Must find the word in the keep-case tree. */
6314 find_keepcap_word(slang, theword, cword);
6315 p = cword;
6316 }
6317 else
6318 {
6319 flags |= su->su_badflags;
6320 if ((flags & WF_CAPMASK) != 0)
6321 {
6322 /* Need to fix case according to "flags". */
6323 make_case_word(theword, cword, flags);
6324 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006325 }
6326 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006327 p = theword;
6328 }
6329
6330 /* Add the suggestion. */
6331 if (sps_flags & SPS_DOUBLE)
6332 {
6333 /* Add the suggestion if the score isn't too bad. */
6334 if (score <= su->su_maxscore)
6335 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6336 score, 0, FALSE, slang, FALSE);
6337 }
6338 else
6339 {
6340 /* Add a penalty for words in another region. */
6341 if ((flags & WF_REGION)
6342 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6343 goodscore = SCORE_REGION;
6344 else
6345 goodscore = 0;
6346
6347 /* Add a small penalty for changing the first letter from
6348 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006349 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006350 * letter is the same, that has already been counted. */
6351 gc = PTR2CHAR(p);
6352 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006353 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006354 bc = PTR2CHAR(su->su_badword);
6355 if (!SPELL_ISUPPER(bc)
6356 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6357 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006358 }
6359
Bram Moolenaar4770d092006-01-12 23:22:24 +00006360 /* Compute the score for the good word. This only does letter
6361 * insert/delete/swap/replace. REP items are not considered,
6362 * which may make the score a bit higher.
6363 * Use a limit for the score to make it work faster. Use
6364 * MAXSCORE(), because RESCORE() will change the score.
6365 * If the limit is very high then the iterative method is
6366 * inefficient, using an array is quicker. */
6367 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6368 if (limit > SCORE_LIMITMAX)
6369 goodscore += spell_edit_score(slang, su->su_badword, p);
6370 else
6371 goodscore += spell_edit_score_limit(slang, su->su_badword,
6372 p, limit);
6373
6374 /* When going over the limit don't bother to do the rest. */
6375 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006376 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006377 /* Give a bonus to words seen before. */
6378 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006379
Bram Moolenaar4770d092006-01-12 23:22:24 +00006380 /* Add the suggestion if the score isn't too bad. */
6381 goodscore = RESCORE(goodscore, score);
6382 if (goodscore <= su->su_sfmaxscore)
6383 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6384 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006386 }
6387 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006388 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006389 }
6390}
6391
6392/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006393 * Find word "word" in fold-case tree for "slang" and return the word number.
6394 */
6395 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006396soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006397{
6398 idx_T arridx = 0;
6399 int len;
6400 int wlen = 0;
6401 int c;
6402 char_u *ptr = word;
6403 char_u *byts;
6404 idx_T *idxs;
6405 int wordnr = 0;
6406
6407 byts = slang->sl_sbyts;
6408 idxs = slang->sl_sidxs;
6409
6410 for (;;)
6411 {
6412 /* First byte is the number of possible bytes. */
6413 len = byts[arridx++];
6414
6415 /* If the first possible byte is a zero the word could end here.
6416 * If the word ends we found the word. If not skip the NUL bytes. */
6417 c = ptr[wlen];
6418 if (byts[arridx] == NUL)
6419 {
6420 if (c == NUL)
6421 break;
6422
6423 /* Skip over the zeros, there can be several. */
6424 while (len > 0 && byts[arridx] == NUL)
6425 {
6426 ++arridx;
6427 --len;
6428 }
6429 if (len == 0)
6430 return -1; /* no children, word should have ended here */
6431 ++wordnr;
6432 }
6433
6434 /* If the word ends we didn't find it. */
6435 if (c == NUL)
6436 return -1;
6437
6438 /* Perform a binary search in the list of accepted bytes. */
6439 if (c == TAB) /* <Tab> is handled like <Space> */
6440 c = ' ';
6441 while (byts[arridx] < c)
6442 {
6443 /* The word count is in the first idxs[] entry of the child. */
6444 wordnr += idxs[idxs[arridx]];
6445 ++arridx;
6446 if (--len == 0) /* end of the bytes, didn't find it */
6447 return -1;
6448 }
6449 if (byts[arridx] != c) /* didn't find the byte */
6450 return -1;
6451
6452 /* Continue at the child (if there is one). */
6453 arridx = idxs[arridx];
6454 ++wlen;
6455
6456 /* One space in the good word may stand for several spaces in the
6457 * checked word. */
6458 if (c == ' ')
6459 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6460 ++wlen;
6461 }
6462
6463 return wordnr;
6464}
6465
6466/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006467 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006468 */
6469 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006470make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006471{
6472 if (flags & WF_ALLCAP)
6473 /* Make it all upper-case */
6474 allcap_copy(fword, cword);
6475 else if (flags & WF_ONECAP)
6476 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006477 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006478 else
6479 /* Use goodword as-is. */
6480 STRCPY(cword, fword);
6481}
6482
Bram Moolenaarea424162005-06-16 21:51:00 +00006483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006484/*
6485 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6486 * lines in the .aff file.
6487 */
6488 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006489similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006490{
Bram Moolenaarea424162005-06-16 21:51:00 +00006491 int m1, m2;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006492 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006493 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006494
Bram Moolenaarea424162005-06-16 21:51:00 +00006495 if (c1 >= 256)
6496 {
6497 buf[mb_char2bytes(c1, buf)] = 0;
6498 hi = hash_find(&slang->sl_map_hash, buf);
6499 if (HASHITEM_EMPTY(hi))
6500 m1 = 0;
6501 else
6502 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6503 }
6504 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006505 m1 = slang->sl_map_array[c1];
6506 if (m1 == 0)
6507 return FALSE;
6508
6509
Bram Moolenaarea424162005-06-16 21:51:00 +00006510 if (c2 >= 256)
6511 {
6512 buf[mb_char2bytes(c2, buf)] = 0;
6513 hi = hash_find(&slang->sl_map_hash, buf);
6514 if (HASHITEM_EMPTY(hi))
6515 m2 = 0;
6516 else
6517 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6518 }
6519 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006520 m2 = slang->sl_map_array[c2];
6521
6522 return m1 == m2;
6523}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006524
6525/*
6526 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006527 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006528 */
6529 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006530add_suggestion(
6531 suginfo_T *su,
6532 garray_T *gap, /* either su_ga or su_sga */
6533 char_u *goodword,
6534 int badlenarg, /* len of bad word replaced with "goodword" */
6535 int score,
6536 int altscore,
6537 int had_bonus, /* value for st_had_bonus */
6538 slang_T *slang, /* language for sound folding */
6539 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006540 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006541{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006542 int goodlen; /* len of goodword changed */
6543 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006544 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006545 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006546 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006547 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006548
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006549 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6550 * "thee the" is added next to changing the first "the" the "thee". */
6551 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006552 pbad = su->su_badptr + badlenarg;
6553 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006554 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006555 goodlen = (int)(pgood - goodword);
6556 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006557 if (goodlen <= 0 || badlen <= 0)
6558 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006559 MB_PTR_BACK(goodword, pgood);
6560 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006561 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006562 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006563 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6564 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006565 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006566 else if (*pgood != *pbad)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006567 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006568 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006569
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006570 if (badlen == 0 && goodlen == 0)
6571 /* goodword doesn't change anything; may happen for "the the" changing
6572 * the first "the" to itself. */
6573 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006574
Bram Moolenaar89d40322006-08-29 15:30:07 +00006575 if (gap->ga_len == 0)
6576 i = -1;
6577 else
6578 {
6579 /* Check if the word is already there. Also check the length that is
6580 * being replaced "thes," -> "these" is a different suggestion from
6581 * "thes" -> "these". */
6582 stp = &SUG(*gap, 0);
6583 for (i = gap->ga_len; --i >= 0; ++stp)
6584 if (stp->st_wordlen == goodlen
6585 && stp->st_orglen == badlen
6586 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006587 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006588 /*
6589 * Found it. Remember the word with the lowest score.
6590 */
6591 if (stp->st_slang == NULL)
6592 stp->st_slang = slang;
6593
6594 new_sug.st_score = score;
6595 new_sug.st_altscore = altscore;
6596 new_sug.st_had_bonus = had_bonus;
6597
6598 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006599 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006600 /* Only one of the two had the soundalike score computed.
6601 * Need to do that for the other one now, otherwise the
6602 * scores can't be compared. This happens because
6603 * suggest_try_change() doesn't compute the soundalike
6604 * word to keep it fast, while some special methods set
6605 * the soundalike score to zero. */
6606 if (had_bonus)
6607 rescore_one(su, stp);
6608 else
6609 {
6610 new_sug.st_word = stp->st_word;
6611 new_sug.st_wordlen = stp->st_wordlen;
6612 new_sug.st_slang = stp->st_slang;
6613 new_sug.st_orglen = badlen;
6614 rescore_one(su, &new_sug);
6615 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006616 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006617
Bram Moolenaar89d40322006-08-29 15:30:07 +00006618 if (stp->st_score > new_sug.st_score)
6619 {
6620 stp->st_score = new_sug.st_score;
6621 stp->st_altscore = new_sug.st_altscore;
6622 stp->st_had_bonus = new_sug.st_had_bonus;
6623 }
6624 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006625 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006627
Bram Moolenaar4770d092006-01-12 23:22:24 +00006628 if (i < 0 && ga_grow(gap, 1) == OK)
6629 {
6630 /* Add a suggestion. */
6631 stp = &SUG(*gap, gap->ga_len);
6632 stp->st_word = vim_strnsave(goodword, goodlen);
6633 if (stp->st_word != NULL)
6634 {
6635 stp->st_wordlen = goodlen;
6636 stp->st_score = score;
6637 stp->st_altscore = altscore;
6638 stp->st_had_bonus = had_bonus;
6639 stp->st_orglen = badlen;
6640 stp->st_slang = slang;
6641 ++gap->ga_len;
6642
6643 /* If we have too many suggestions now, sort the list and keep
6644 * the best suggestions. */
6645 if (gap->ga_len > SUG_MAX_COUNT(su))
6646 {
6647 if (maxsf)
6648 su->su_sfmaxscore = cleanup_suggestions(gap,
6649 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6650 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006651 su->su_maxscore = cleanup_suggestions(gap,
6652 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006653 }
6654 }
6655 }
6656}
6657
6658/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006659 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6660 * for split words, such as "the the". Remove these from the list here.
6661 */
6662 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006663check_suggestions(
6664 suginfo_T *su,
6665 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006666{
6667 suggest_T *stp;
6668 int i;
6669 char_u longword[MAXWLEN + 1];
6670 int len;
6671 hlf_T attr;
6672
6673 stp = &SUG(*gap, 0);
6674 for (i = gap->ga_len - 1; i >= 0; --i)
6675 {
6676 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006677 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006678 len = stp[i].st_wordlen;
6679 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6680 MAXWLEN - len);
6681 attr = HLF_COUNT;
6682 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6683 if (attr != HLF_COUNT)
6684 {
6685 /* Remove this entry. */
6686 vim_free(stp[i].st_word);
6687 --gap->ga_len;
6688 if (i < gap->ga_len)
6689 mch_memmove(stp + i, stp + i + 1,
6690 sizeof(suggest_T) * (gap->ga_len - i));
6691 }
6692 }
6693}
6694
6695
6696/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006697 * Add a word to be banned.
6698 */
6699 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006700add_banned(
6701 suginfo_T *su,
6702 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006703{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006704 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705 hash_T hash;
6706 hashitem_T *hi;
6707
Bram Moolenaar4770d092006-01-12 23:22:24 +00006708 hash = hash_hash(word);
6709 hi = hash_lookup(&su->su_banned, word, hash);
6710 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006711 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006712 s = vim_strsave(word);
6713 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006714 hash_add_item(&su->su_banned, hi, s, hash);
6715 }
6716}
6717
6718/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006719 * Recompute the score for all suggestions if sound-folding is possible. This
6720 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006721 */
6722 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006723rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006724{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006725 int i;
6726
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006727 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006728 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006729 rescore_one(su, &SUG(su->su_ga, i));
6730}
6731
6732/*
6733 * Recompute the score for one suggestion if sound-folding is possible.
6734 */
6735 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006736rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006737{
6738 slang_T *slang = stp->st_slang;
6739 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006740 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006741
6742 /* Only rescore suggestions that have no sal score yet and do have a
6743 * language. */
6744 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6745 {
6746 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006747 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006748 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006749 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006750 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006751 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006752 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006753
6754 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006755 if (stp->st_altscore == SCORE_MAXMAX)
6756 stp->st_altscore = SCORE_BIG;
6757 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6758 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006759 }
6760}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006762static int
6763#ifdef __BORLANDC__
6764_RTLENTRYF
6765#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006766sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006767
6768/*
6769 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006770 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006771 */
6772 static int
6773#ifdef __BORLANDC__
6774_RTLENTRYF
6775#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006776sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006777{
6778 suggest_T *p1 = (suggest_T *)s1;
6779 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006780 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006781
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006782 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006783 {
6784 n = p1->st_altscore - p2->st_altscore;
6785 if (n == 0)
6786 n = STRICMP(p1->st_word, p2->st_word);
6787 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006788 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006789}
6790
6791/*
6792 * Cleanup the suggestions:
6793 * - Sort on score.
6794 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006795 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006796 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006797 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006798cleanup_suggestions(
6799 garray_T *gap,
6800 int maxscore,
6801 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006802{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006803 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006804 int i;
6805
6806 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006807 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006808
6809 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006810 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006811 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006812 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006813 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006814 gap->ga_len = keep;
6815 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006816 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006817 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006818}
6819
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006820#if defined(FEAT_EVAL) || defined(PROTO)
6821/*
6822 * Soundfold a string, for soundfold().
6823 * Result is in allocated memory, NULL for an error.
6824 */
6825 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006826eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006827{
6828 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006829 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006830 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006831
Bram Moolenaar860cae12010-06-05 23:22:07 +02006832 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006833 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006834 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006835 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006836 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006837 if (lp->lp_slang->sl_sal.ga_len > 0)
6838 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006839 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006840 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006841 return vim_strsave(sound);
6842 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006843 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006844
6845 /* No language with sound folding, return word as-is. */
6846 return vim_strsave(word);
6847}
6848#endif
6849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006850/*
6851 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00006852 *
6853 * There are many ways to turn a word into a sound-a-like representation. The
6854 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
6855 * swedish name matching - survey and test of different algorithms" by Klas
6856 * Erikson.
6857 *
6858 * We support two methods:
6859 * 1. SOFOFROM/SOFOTO do a simple character mapping.
6860 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006861 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006862 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006863spell_soundfold(
6864 slang_T *slang,
6865 char_u *inword,
6866 int folded, /* "inword" is already case-folded */
6867 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006868{
6869 char_u fword[MAXWLEN];
6870 char_u *word;
6871
6872 if (slang->sl_sofo)
6873 /* SOFOFROM and SOFOTO used */
6874 spell_soundfold_sofo(slang, inword, res);
6875 else
6876 {
6877 /* SAL items used. Requires the word to be case-folded. */
6878 if (folded)
6879 word = inword;
6880 else
6881 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006882 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006883 word = fword;
6884 }
6885
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006886 if (has_mbyte)
6887 spell_soundfold_wsal(slang, word, res);
6888 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006889 spell_soundfold_sal(slang, word, res);
6890 }
6891}
6892
6893/*
6894 * Perform sound folding of "inword" into "res" according to SOFOFROM and
6895 * SOFOTO lines.
6896 */
6897 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006898spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006899{
6900 char_u *s;
6901 int ri = 0;
6902 int c;
6903
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006904 if (has_mbyte)
6905 {
6906 int prevc = 0;
6907 int *ip;
6908
6909 /* The sl_sal_first[] table contains the translation for chars up to
6910 * 255, sl_sal the rest. */
6911 for (s = inword; *s != NUL; )
6912 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006913 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006914 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006915 c = ' ';
6916 else if (c < 256)
6917 c = slang->sl_sal_first[c];
6918 else
6919 {
6920 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
6921 if (ip == NULL) /* empty list, can't match */
6922 c = NUL;
6923 else
6924 for (;;) /* find "c" in the list */
6925 {
6926 if (*ip == 0) /* not found */
6927 {
6928 c = NUL;
6929 break;
6930 }
6931 if (*ip == c) /* match! */
6932 {
6933 c = ip[1];
6934 break;
6935 }
6936 ip += 2;
6937 }
6938 }
6939
6940 if (c != NUL && c != prevc)
6941 {
6942 ri += mb_char2bytes(c, res + ri);
6943 if (ri + MB_MAXBYTES > MAXWLEN)
6944 break;
6945 prevc = c;
6946 }
6947 }
6948 }
6949 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006950 {
6951 /* The sl_sal_first[] table contains the translation. */
6952 for (s = inword; (c = *s) != NUL; ++s)
6953 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006954 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006955 c = ' ';
6956 else
6957 c = slang->sl_sal_first[c];
6958 if (c != NUL && (ri == 0 || res[ri - 1] != c))
6959 res[ri++] = c;
6960 }
6961 }
6962
6963 res[ri] = NUL;
6964}
6965
6966 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006967spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006968{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006969 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006970 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006971 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006972 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006973 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006974 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006975 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006976 int n, k = 0;
6977 int z0;
6978 int k0;
6979 int n0;
6980 int c;
6981 int pri;
6982 int p0 = -333;
6983 int c0;
6984
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006985 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006986 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006987 if (slang->sl_rem_accents)
6988 {
6989 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006990 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006991 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006992 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006993 {
6994 *t++ = ' ';
6995 s = skipwhite(s);
6996 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006997 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006998 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01006999 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007000 *t++ = *s;
7001 ++s;
7002 }
7003 }
7004 *t = NUL;
7005 }
7006 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007007 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007008
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007009 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007010
7011 /*
7012 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007013 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007014 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007015 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007016 while ((c = word[i]) != NUL)
7017 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007018 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007019 n = slang->sl_sal_first[c];
7020 z0 = 0;
7021
7022 if (n >= 0)
7023 {
7024 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007025 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007026 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007027 /* Quickly skip entries that don't match the word. Most
7028 * entries are less then three chars, optimize for that. */
7029 k = smp[n].sm_leadlen;
7030 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007031 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007032 if (word[i + 1] != s[1])
7033 continue;
7034 if (k > 2)
7035 {
7036 for (j = 2; j < k; ++j)
7037 if (word[i + j] != s[j])
7038 break;
7039 if (j < k)
7040 continue;
7041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007042 }
7043
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007044 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007045 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007046 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007047 while (*pf != NUL && *pf != word[i + k])
7048 ++pf;
7049 if (*pf == NUL)
7050 continue;
7051 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007052 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007053 s = smp[n].sm_rules;
7054 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007055
7056 p0 = *s;
7057 k0 = k;
7058 while (*s == '-' && k > 1)
7059 {
7060 k--;
7061 s++;
7062 }
7063 if (*s == '<')
7064 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007065 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007066 {
7067 /* determine priority */
7068 pri = *s - '0';
7069 s++;
7070 }
7071 if (*s == '^' && *(s + 1) == '^')
7072 s++;
7073
7074 if (*s == NUL
7075 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007076 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007077 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007078 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007079 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007080 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007081 && spell_iswordp(word + i - 1, curwin)
7082 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007083 {
7084 /* search for followup rules, if: */
7085 /* followup and k > 1 and NO '-' in searchstring */
7086 c0 = word[i + k - 1];
7087 n0 = slang->sl_sal_first[c0];
7088
7089 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007090 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007091 {
7092 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007093 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007095 /* Quickly skip entries that don't match the word.
7096 * */
7097 k0 = smp[n0].sm_leadlen;
7098 if (k0 > 1)
7099 {
7100 if (word[i + k] != s[1])
7101 continue;
7102 if (k0 > 2)
7103 {
7104 pf = word + i + k + 1;
7105 for (j = 2; j < k0; ++j)
7106 if (*pf++ != s[j])
7107 break;
7108 if (j < k0)
7109 continue;
7110 }
7111 }
7112 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007113
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007114 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007115 {
7116 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007117 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007118 while (*pf != NUL && *pf != word[i + k0])
7119 ++pf;
7120 if (*pf == NUL)
7121 continue;
7122 ++k0;
7123 }
7124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007125 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007126 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007127 while (*s == '-')
7128 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007129 /* "k0" gets NOT reduced because
7130 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007131 s++;
7132 }
7133 if (*s == '<')
7134 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007135 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007136 {
7137 p0 = *s - '0';
7138 s++;
7139 }
7140
7141 if (*s == NUL
7142 /* *s == '^' cuts */
7143 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007144 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007145 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007146 {
7147 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007148 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007149 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150
7151 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007152 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007153 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007154 /* rule fits; stop search */
7155 break;
7156 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157 }
7158
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007159 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007160 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007161 }
7162
7163 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007164 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007165 if (s == NULL)
7166 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007167 pf = smp[n].sm_rules;
7168 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007169 if (p0 == 1 && z == 0)
7170 {
7171 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007172 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7173 || res[reslen - 1] == *s))
7174 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007175 z0 = 1;
7176 z = 1;
7177 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007178 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007179 {
7180 word[i + k0] = *s;
7181 k0++;
7182 s++;
7183 }
7184 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007185 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007186
7187 /* new "actual letter" */
7188 c = word[i];
7189 }
7190 else
7191 {
7192 /* no '<' rule used */
7193 i += k - 1;
7194 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007195 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007196 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007197 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007198 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007199 s++;
7200 }
7201 /* new "actual letter" */
7202 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007203 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007204 {
7205 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007206 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007207 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007208 i = 0;
7209 z0 = 1;
7210 }
7211 }
7212 break;
7213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007214 }
7215 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007216 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007217 {
7218 c = ' ';
7219 k = 1;
7220 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007221
7222 if (z0 == 0)
7223 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007224 if (k && !p0 && reslen < MAXWLEN && c != NUL
7225 && (!slang->sl_collapse || reslen == 0
7226 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007228 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007229
7230 i++;
7231 z = 0;
7232 k = 0;
7233 }
7234 }
7235
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007236 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007237}
7238
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007239/*
7240 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7241 * Multi-byte version of spell_soundfold().
7242 */
7243 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007244spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007245{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007246 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007247 int word[MAXWLEN];
7248 int wres[MAXWLEN];
7249 int l;
7250 char_u *s;
7251 int *ws;
7252 char_u *t;
7253 int *pf;
7254 int i, j, z;
7255 int reslen;
7256 int n, k = 0;
7257 int z0;
7258 int k0;
7259 int n0;
7260 int c;
7261 int pri;
7262 int p0 = -333;
7263 int c0;
7264 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007265 int wordlen;
7266
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007267
7268 /*
7269 * Convert the multi-byte string to a wide-character string.
7270 * Remove accents, if wanted. We actually remove all non-word characters.
7271 * But keep white space.
7272 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007273 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007274 for (s = inword; *s != NUL; )
7275 {
7276 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007277 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007278 if (slang->sl_rem_accents)
7279 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007280 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007281 {
7282 if (did_white)
7283 continue;
7284 c = ' ';
7285 did_white = TRUE;
7286 }
7287 else
7288 {
7289 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007290 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007291 continue;
7292 }
7293 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007294 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007295 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007296 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007297
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007298 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007299 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007300 * Converted from C++ to C. Added support for multi-byte chars.
7301 * Changed to keep spaces.
7302 */
7303 i = reslen = z = 0;
7304 while ((c = word[i]) != NUL)
7305 {
7306 /* Start with the first rule that has the character in the word. */
7307 n = slang->sl_sal_first[c & 0xff];
7308 z0 = 0;
7309
7310 if (n >= 0)
7311 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007312 /* Check all rules for the same index byte.
7313 * If c is 0x300 need extra check for the end of the array, as
7314 * (c & 0xff) is NUL. */
7315 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7316 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007317 {
7318 /* Quickly skip entries that don't match the word. Most
7319 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007320 if (c != ws[0])
7321 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007322 k = smp[n].sm_leadlen;
7323 if (k > 1)
7324 {
7325 if (word[i + 1] != ws[1])
7326 continue;
7327 if (k > 2)
7328 {
7329 for (j = 2; j < k; ++j)
7330 if (word[i + j] != ws[j])
7331 break;
7332 if (j < k)
7333 continue;
7334 }
7335 }
7336
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007337 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007338 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007339 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007340 while (*pf != NUL && *pf != word[i + k])
7341 ++pf;
7342 if (*pf == NUL)
7343 continue;
7344 ++k;
7345 }
7346 s = smp[n].sm_rules;
7347 pri = 5; /* default priority */
7348
7349 p0 = *s;
7350 k0 = k;
7351 while (*s == '-' && k > 1)
7352 {
7353 k--;
7354 s++;
7355 }
7356 if (*s == '<')
7357 s++;
7358 if (VIM_ISDIGIT(*s))
7359 {
7360 /* determine priority */
7361 pri = *s - '0';
7362 s++;
7363 }
7364 if (*s == '^' && *(s + 1) == '^')
7365 s++;
7366
7367 if (*s == NUL
7368 || (*s == '^'
7369 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007370 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007371 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007372 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007373 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007374 && spell_iswordp_w(word + i - 1, curwin)
7375 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007376 {
7377 /* search for followup rules, if: */
7378 /* followup and k > 1 and NO '-' in searchstring */
7379 c0 = word[i + k - 1];
7380 n0 = slang->sl_sal_first[c0 & 0xff];
7381
7382 if (slang->sl_followup && k > 1 && n0 >= 0
7383 && p0 != '-' && word[i + k] != NUL)
7384 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007385 /* Test follow-up rule for "word[i + k]"; loop over
7386 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007387 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7388 == (c0 & 0xff); ++n0)
7389 {
7390 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007391 */
7392 if (c0 != ws[0])
7393 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007394 k0 = smp[n0].sm_leadlen;
7395 if (k0 > 1)
7396 {
7397 if (word[i + k] != ws[1])
7398 continue;
7399 if (k0 > 2)
7400 {
7401 pf = word + i + k + 1;
7402 for (j = 2; j < k0; ++j)
7403 if (*pf++ != ws[j])
7404 break;
7405 if (j < k0)
7406 continue;
7407 }
7408 }
7409 k0 += k - 1;
7410
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007411 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007412 {
7413 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007414 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007415 while (*pf != NUL && *pf != word[i + k0])
7416 ++pf;
7417 if (*pf == NUL)
7418 continue;
7419 ++k0;
7420 }
7421
7422 p0 = 5;
7423 s = smp[n0].sm_rules;
7424 while (*s == '-')
7425 {
7426 /* "k0" gets NOT reduced because
7427 * "if (k0 == k)" */
7428 s++;
7429 }
7430 if (*s == '<')
7431 s++;
7432 if (VIM_ISDIGIT(*s))
7433 {
7434 p0 = *s - '0';
7435 s++;
7436 }
7437
7438 if (*s == NUL
7439 /* *s == '^' cuts */
7440 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007441 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007442 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007443 {
7444 if (k0 == k)
7445 /* this is just a piece of the string */
7446 continue;
7447
7448 if (p0 < pri)
7449 /* priority too low */
7450 continue;
7451 /* rule fits; stop search */
7452 break;
7453 }
7454 }
7455
7456 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7457 == (c0 & 0xff))
7458 continue;
7459 }
7460
7461 /* replace string */
7462 ws = smp[n].sm_to_w;
7463 s = smp[n].sm_rules;
7464 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7465 if (p0 == 1 && z == 0)
7466 {
7467 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007468 if (reslen > 0 && ws != NULL && *ws != NUL
7469 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007470 || wres[reslen - 1] == *ws))
7471 reslen--;
7472 z0 = 1;
7473 z = 1;
7474 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007475 if (ws != NULL)
7476 while (*ws != NUL && word[i + k0] != NUL)
7477 {
7478 word[i + k0] = *ws;
7479 k0++;
7480 ws++;
7481 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007482 if (k > k0)
7483 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007484 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007485
7486 /* new "actual letter" */
7487 c = word[i];
7488 }
7489 else
7490 {
7491 /* no '<' rule used */
7492 i += k - 1;
7493 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007494 if (ws != NULL)
7495 while (*ws != NUL && ws[1] != NUL
7496 && reslen < MAXWLEN)
7497 {
7498 if (reslen == 0 || wres[reslen - 1] != *ws)
7499 wres[reslen++] = *ws;
7500 ws++;
7501 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007502 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007503 if (ws == NULL)
7504 c = NUL;
7505 else
7506 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007507 if (strstr((char *)s, "^^") != NULL)
7508 {
7509 if (c != NUL)
7510 wres[reslen++] = c;
7511 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007512 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007513 i = 0;
7514 z0 = 1;
7515 }
7516 }
7517 break;
7518 }
7519 }
7520 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007521 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007522 {
7523 c = ' ';
7524 k = 1;
7525 }
7526
7527 if (z0 == 0)
7528 {
7529 if (k && !p0 && reslen < MAXWLEN && c != NUL
7530 && (!slang->sl_collapse || reslen == 0
7531 || wres[reslen - 1] != c))
7532 /* condense only double letters */
7533 wres[reslen++] = c;
7534
7535 i++;
7536 z = 0;
7537 k = 0;
7538 }
7539 }
7540
7541 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7542 l = 0;
7543 for (n = 0; n < reslen; ++n)
7544 {
7545 l += mb_char2bytes(wres[n], res + l);
7546 if (l + MB_MAXBYTES > MAXWLEN)
7547 break;
7548 }
7549 res[l] = NUL;
7550}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007551
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007552/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007553 * Compute a score for two sound-a-like words.
7554 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7555 * Instead of a generic loop we write out the code. That keeps it fast by
7556 * avoiding checks that will not be possible.
7557 */
7558 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007559soundalike_score(
7560 char_u *goodstart, /* sound-folded good word */
7561 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007562{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007563 char_u *goodsound = goodstart;
7564 char_u *badsound = badstart;
7565 int goodlen;
7566 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007567 int n;
7568 char_u *pl, *ps;
7569 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007570 int score = 0;
7571
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007572 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007573 * counted so much, vowels halfway the word aren't counted at all. */
7574 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7575 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007576 if ((badsound[0] == NUL && goodsound[1] == NUL)
7577 || (goodsound[0] == NUL && badsound[1] == NUL))
7578 /* changing word with vowel to word without a sound */
7579 return SCORE_DEL;
7580 if (badsound[0] == NUL || goodsound[0] == NUL)
7581 /* more than two changes */
7582 return SCORE_MAXMAX;
7583
Bram Moolenaar4770d092006-01-12 23:22:24 +00007584 if (badsound[1] == goodsound[1]
7585 || (badsound[1] != NUL
7586 && goodsound[1] != NUL
7587 && badsound[2] == goodsound[2]))
7588 {
7589 /* handle like a substitute */
7590 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007591 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007592 {
7593 score = 2 * SCORE_DEL / 3;
7594 if (*badsound == '*')
7595 ++badsound;
7596 else
7597 ++goodsound;
7598 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007599 }
7600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007601 goodlen = (int)STRLEN(goodsound);
7602 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007603
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007604 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007605 * changes. */
7606 n = goodlen - badlen;
7607 if (n < -2 || n > 2)
7608 return SCORE_MAXMAX;
7609
7610 if (n > 0)
7611 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007612 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007613 ps = badsound;
7614 }
7615 else
7616 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007617 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007618 ps = goodsound;
7619 }
7620
7621 /* Skip over the identical part. */
7622 while (*pl == *ps && *pl != NUL)
7623 {
7624 ++pl;
7625 ++ps;
7626 }
7627
7628 switch (n)
7629 {
7630 case -2:
7631 case 2:
7632 /*
7633 * Must delete two characters from "pl".
7634 */
7635 ++pl; /* first delete */
7636 while (*pl == *ps)
7637 {
7638 ++pl;
7639 ++ps;
7640 }
7641 /* strings must be equal after second delete */
7642 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007643 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007644
7645 /* Failed to compare. */
7646 break;
7647
7648 case -1:
7649 case 1:
7650 /*
7651 * Minimal one delete from "pl" required.
7652 */
7653
7654 /* 1: delete */
7655 pl2 = pl + 1;
7656 ps2 = ps;
7657 while (*pl2 == *ps2)
7658 {
7659 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007660 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007661 ++pl2;
7662 ++ps2;
7663 }
7664
7665 /* 2: delete then swap, then rest must be equal */
7666 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7667 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007668 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007669
7670 /* 3: delete then substitute, then the rest must be equal */
7671 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007672 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007673
7674 /* 4: first swap then delete */
7675 if (pl[0] == ps[1] && pl[1] == ps[0])
7676 {
7677 pl2 = pl + 2; /* swap, skip two chars */
7678 ps2 = ps + 2;
7679 while (*pl2 == *ps2)
7680 {
7681 ++pl2;
7682 ++ps2;
7683 }
7684 /* delete a char and then strings must be equal */
7685 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007686 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007687 }
7688
7689 /* 5: first substitute then delete */
7690 pl2 = pl + 1; /* substitute, skip one char */
7691 ps2 = ps + 1;
7692 while (*pl2 == *ps2)
7693 {
7694 ++pl2;
7695 ++ps2;
7696 }
7697 /* delete a char and then strings must be equal */
7698 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007699 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007700
7701 /* Failed to compare. */
7702 break;
7703
7704 case 0:
7705 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007706 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007707 * insert is only possible in combination with a delete.
7708 * 1: check if for identical strings
7709 */
7710 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007711 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007712
7713 /* 2: swap */
7714 if (pl[0] == ps[1] && pl[1] == ps[0])
7715 {
7716 pl2 = pl + 2; /* swap, skip two chars */
7717 ps2 = ps + 2;
7718 while (*pl2 == *ps2)
7719 {
7720 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007721 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007722 ++pl2;
7723 ++ps2;
7724 }
7725 /* 3: swap and swap again */
7726 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7727 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007728 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007729
7730 /* 4: swap and substitute */
7731 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007732 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007733 }
7734
7735 /* 5: substitute */
7736 pl2 = pl + 1;
7737 ps2 = ps + 1;
7738 while (*pl2 == *ps2)
7739 {
7740 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007741 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007742 ++pl2;
7743 ++ps2;
7744 }
7745
7746 /* 6: substitute and swap */
7747 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7748 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007749 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007750
7751 /* 7: substitute and substitute */
7752 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007753 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007754
7755 /* 8: insert then delete */
7756 pl2 = pl;
7757 ps2 = ps + 1;
7758 while (*pl2 == *ps2)
7759 {
7760 ++pl2;
7761 ++ps2;
7762 }
7763 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007764 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007765
7766 /* 9: delete then insert */
7767 pl2 = pl + 1;
7768 ps2 = ps;
7769 while (*pl2 == *ps2)
7770 {
7771 ++pl2;
7772 ++ps2;
7773 }
7774 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007775 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007776
7777 /* Failed to compare. */
7778 break;
7779 }
7780
7781 return SCORE_MAXMAX;
7782}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007784/*
7785 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007786 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007787 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007788 * The algorithm is described by Du and Chang, 1992.
7789 * The implementation of the algorithm comes from Aspell editdist.cpp,
7790 * edit_distance(). It has been converted from C++ to C and modified to
7791 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007792 */
7793 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007794spell_edit_score(
7795 slang_T *slang,
7796 char_u *badword,
7797 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007798{
7799 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007800 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 int j, i;
7802 int t;
7803 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007804 int pbc, pgc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007805 char_u *p;
7806 int wbadword[MAXWLEN];
7807 int wgoodword[MAXWLEN];
7808
7809 if (has_mbyte)
7810 {
7811 /* Get the characters from the multi-byte strings and put them in an
7812 * int array for easy access. */
7813 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007814 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007815 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007816 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007817 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007818 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007819 }
7820 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007821 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007822 badlen = (int)STRLEN(badword) + 1;
7823 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007824 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007825
7826 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7827#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007828 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7829 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007830 if (cnt == NULL)
7831 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832
7833 CNT(0, 0) = 0;
7834 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007835 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007836
7837 for (i = 1; i <= badlen; ++i)
7838 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007839 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007840 for (j = 1; j <= goodlen; ++j)
7841 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007842 if (has_mbyte)
7843 {
7844 bc = wbadword[i - 1];
7845 gc = wgoodword[j - 1];
7846 }
7847 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007848 {
7849 bc = badword[i - 1];
7850 gc = goodword[j - 1];
7851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007852 if (bc == gc)
7853 CNT(i, j) = CNT(i - 1, j - 1);
7854 else
7855 {
7856 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007857 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007858 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7859 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007860 {
7861 /* For a similar character use SCORE_SIMILAR. */
7862 if (slang != NULL
7863 && slang->sl_has_map
7864 && similar_chars(slang, gc, bc))
7865 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
7866 else
7867 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7868 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007869
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007870 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007871 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007872 if (has_mbyte)
7873 {
7874 pbc = wbadword[i - 2];
7875 pgc = wgoodword[j - 2];
7876 }
7877 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007878 {
7879 pbc = badword[i - 2];
7880 pgc = goodword[j - 2];
7881 }
7882 if (bc == pgc && pbc == gc)
7883 {
7884 t = SCORE_SWAP + CNT(i - 2, j - 2);
7885 if (t < CNT(i, j))
7886 CNT(i, j) = t;
7887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007888 }
7889 t = SCORE_DEL + CNT(i - 1, j);
7890 if (t < CNT(i, j))
7891 CNT(i, j) = t;
7892 t = SCORE_INS + CNT(i, j - 1);
7893 if (t < CNT(i, j))
7894 CNT(i, j) = t;
7895 }
7896 }
7897 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007898
7899 i = CNT(badlen - 1, goodlen - 1);
7900 vim_free(cnt);
7901 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007902}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007903
Bram Moolenaar4770d092006-01-12 23:22:24 +00007904typedef struct
7905{
7906 int badi;
7907 int goodi;
7908 int score;
7909} limitscore_T;
7910
7911/*
7912 * Like spell_edit_score(), but with a limit on the score to make it faster.
7913 * May return SCORE_MAXMAX when the score is higher than "limit".
7914 *
7915 * This uses a stack for the edits still to be tried.
7916 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
7917 * for multi-byte characters.
7918 */
7919 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007920spell_edit_score_limit(
7921 slang_T *slang,
7922 char_u *badword,
7923 char_u *goodword,
7924 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007925{
7926 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
7927 int stackidx;
7928 int bi, gi;
7929 int bi2, gi2;
7930 int bc, gc;
7931 int score;
7932 int score_off;
7933 int minscore;
7934 int round;
7935
Bram Moolenaar4770d092006-01-12 23:22:24 +00007936 /* Multi-byte characters require a bit more work, use a different function
7937 * to avoid testing "has_mbyte" quite often. */
7938 if (has_mbyte)
7939 return spell_edit_score_limit_w(slang, badword, goodword, limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007940
7941 /*
7942 * The idea is to go from start to end over the words. So long as
7943 * characters are equal just continue, this always gives the lowest score.
7944 * When there is a difference try several alternatives. Each alternative
7945 * increases "score" for the edit distance. Some of the alternatives are
7946 * pushed unto a stack and tried later, some are tried right away. At the
7947 * end of the word the score for one alternative is known. The lowest
7948 * possible score is stored in "minscore".
7949 */
7950 stackidx = 0;
7951 bi = 0;
7952 gi = 0;
7953 score = 0;
7954 minscore = limit + 1;
7955
7956 for (;;)
7957 {
7958 /* Skip over an equal part, score remains the same. */
7959 for (;;)
7960 {
7961 bc = badword[bi];
7962 gc = goodword[gi];
7963 if (bc != gc) /* stop at a char that's different */
7964 break;
7965 if (bc == NUL) /* both words end */
7966 {
7967 if (score < minscore)
7968 minscore = score;
7969 goto pop; /* do next alternative */
7970 }
7971 ++bi;
7972 ++gi;
7973 }
7974
7975 if (gc == NUL) /* goodword ends, delete badword chars */
7976 {
7977 do
7978 {
7979 if ((score += SCORE_DEL) >= minscore)
7980 goto pop; /* do next alternative */
7981 } while (badword[++bi] != NUL);
7982 minscore = score;
7983 }
7984 else if (bc == NUL) /* badword ends, insert badword chars */
7985 {
7986 do
7987 {
7988 if ((score += SCORE_INS) >= minscore)
7989 goto pop; /* do next alternative */
7990 } while (goodword[++gi] != NUL);
7991 minscore = score;
7992 }
7993 else /* both words continue */
7994 {
7995 /* If not close to the limit, perform a change. Only try changes
7996 * that may lead to a lower score than "minscore".
7997 * round 0: try deleting a char from badword
7998 * round 1: try inserting a char in badword */
7999 for (round = 0; round <= 1; ++round)
8000 {
8001 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8002 if (score_off < minscore)
8003 {
8004 if (score_off + SCORE_EDIT_MIN >= minscore)
8005 {
8006 /* Near the limit, rest of the words must match. We
8007 * can check that right now, no need to push an item
8008 * onto the stack. */
8009 bi2 = bi + 1 - round;
8010 gi2 = gi + round;
8011 while (goodword[gi2] == badword[bi2])
8012 {
8013 if (goodword[gi2] == NUL)
8014 {
8015 minscore = score_off;
8016 break;
8017 }
8018 ++bi2;
8019 ++gi2;
8020 }
8021 }
8022 else
8023 {
8024 /* try deleting/inserting a character later */
8025 stack[stackidx].badi = bi + 1 - round;
8026 stack[stackidx].goodi = gi + round;
8027 stack[stackidx].score = score_off;
8028 ++stackidx;
8029 }
8030 }
8031 }
8032
8033 if (score + SCORE_SWAP < minscore)
8034 {
8035 /* If swapping two characters makes a match then the
8036 * substitution is more expensive, thus there is no need to
8037 * try both. */
8038 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8039 {
8040 /* Swap two characters, that is: skip them. */
8041 gi += 2;
8042 bi += 2;
8043 score += SCORE_SWAP;
8044 continue;
8045 }
8046 }
8047
8048 /* Substitute one character for another which is the same
8049 * thing as deleting a character from both goodword and badword.
8050 * Use a better score when there is only a case difference. */
8051 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8052 score += SCORE_ICASE;
8053 else
8054 {
8055 /* For a similar character use SCORE_SIMILAR. */
8056 if (slang != NULL
8057 && slang->sl_has_map
8058 && similar_chars(slang, gc, bc))
8059 score += SCORE_SIMILAR;
8060 else
8061 score += SCORE_SUBST;
8062 }
8063
8064 if (score < minscore)
8065 {
8066 /* Do the substitution. */
8067 ++gi;
8068 ++bi;
8069 continue;
8070 }
8071 }
8072pop:
8073 /*
8074 * Get here to try the next alternative, pop it from the stack.
8075 */
8076 if (stackidx == 0) /* stack is empty, finished */
8077 break;
8078
8079 /* pop an item from the stack */
8080 --stackidx;
8081 gi = stack[stackidx].goodi;
8082 bi = stack[stackidx].badi;
8083 score = stack[stackidx].score;
8084 }
8085
8086 /* When the score goes over "limit" it may actually be much higher.
8087 * Return a very large number to avoid going below the limit when giving a
8088 * bonus. */
8089 if (minscore > limit)
8090 return SCORE_MAXMAX;
8091 return minscore;
8092}
8093
Bram Moolenaar4770d092006-01-12 23:22:24 +00008094/*
8095 * Multi-byte version of spell_edit_score_limit().
8096 * Keep it in sync with the above!
8097 */
8098 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008099spell_edit_score_limit_w(
8100 slang_T *slang,
8101 char_u *badword,
8102 char_u *goodword,
8103 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008104{
8105 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8106 int stackidx;
8107 int bi, gi;
8108 int bi2, gi2;
8109 int bc, gc;
8110 int score;
8111 int score_off;
8112 int minscore;
8113 int round;
8114 char_u *p;
8115 int wbadword[MAXWLEN];
8116 int wgoodword[MAXWLEN];
8117
8118 /* Get the characters from the multi-byte strings and put them in an
8119 * int array for easy access. */
8120 bi = 0;
8121 for (p = badword; *p != NUL; )
8122 wbadword[bi++] = mb_cptr2char_adv(&p);
8123 wbadword[bi++] = 0;
8124 gi = 0;
8125 for (p = goodword; *p != NUL; )
8126 wgoodword[gi++] = mb_cptr2char_adv(&p);
8127 wgoodword[gi++] = 0;
8128
8129 /*
8130 * The idea is to go from start to end over the words. So long as
8131 * characters are equal just continue, this always gives the lowest score.
8132 * When there is a difference try several alternatives. Each alternative
8133 * increases "score" for the edit distance. Some of the alternatives are
8134 * pushed unto a stack and tried later, some are tried right away. At the
8135 * end of the word the score for one alternative is known. The lowest
8136 * possible score is stored in "minscore".
8137 */
8138 stackidx = 0;
8139 bi = 0;
8140 gi = 0;
8141 score = 0;
8142 minscore = limit + 1;
8143
8144 for (;;)
8145 {
8146 /* Skip over an equal part, score remains the same. */
8147 for (;;)
8148 {
8149 bc = wbadword[bi];
8150 gc = wgoodword[gi];
8151
8152 if (bc != gc) /* stop at a char that's different */
8153 break;
8154 if (bc == NUL) /* both words end */
8155 {
8156 if (score < minscore)
8157 minscore = score;
8158 goto pop; /* do next alternative */
8159 }
8160 ++bi;
8161 ++gi;
8162 }
8163
8164 if (gc == NUL) /* goodword ends, delete badword chars */
8165 {
8166 do
8167 {
8168 if ((score += SCORE_DEL) >= minscore)
8169 goto pop; /* do next alternative */
8170 } while (wbadword[++bi] != NUL);
8171 minscore = score;
8172 }
8173 else if (bc == NUL) /* badword ends, insert badword chars */
8174 {
8175 do
8176 {
8177 if ((score += SCORE_INS) >= minscore)
8178 goto pop; /* do next alternative */
8179 } while (wgoodword[++gi] != NUL);
8180 minscore = score;
8181 }
8182 else /* both words continue */
8183 {
8184 /* If not close to the limit, perform a change. Only try changes
8185 * that may lead to a lower score than "minscore".
8186 * round 0: try deleting a char from badword
8187 * round 1: try inserting a char in badword */
8188 for (round = 0; round <= 1; ++round)
8189 {
8190 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8191 if (score_off < minscore)
8192 {
8193 if (score_off + SCORE_EDIT_MIN >= minscore)
8194 {
8195 /* Near the limit, rest of the words must match. We
8196 * can check that right now, no need to push an item
8197 * onto the stack. */
8198 bi2 = bi + 1 - round;
8199 gi2 = gi + round;
8200 while (wgoodword[gi2] == wbadword[bi2])
8201 {
8202 if (wgoodword[gi2] == NUL)
8203 {
8204 minscore = score_off;
8205 break;
8206 }
8207 ++bi2;
8208 ++gi2;
8209 }
8210 }
8211 else
8212 {
8213 /* try deleting a character from badword later */
8214 stack[stackidx].badi = bi + 1 - round;
8215 stack[stackidx].goodi = gi + round;
8216 stack[stackidx].score = score_off;
8217 ++stackidx;
8218 }
8219 }
8220 }
8221
8222 if (score + SCORE_SWAP < minscore)
8223 {
8224 /* If swapping two characters makes a match then the
8225 * substitution is more expensive, thus there is no need to
8226 * try both. */
8227 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8228 {
8229 /* Swap two characters, that is: skip them. */
8230 gi += 2;
8231 bi += 2;
8232 score += SCORE_SWAP;
8233 continue;
8234 }
8235 }
8236
8237 /* Substitute one character for another which is the same
8238 * thing as deleting a character from both goodword and badword.
8239 * Use a better score when there is only a case difference. */
8240 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8241 score += SCORE_ICASE;
8242 else
8243 {
8244 /* For a similar character use SCORE_SIMILAR. */
8245 if (slang != NULL
8246 && slang->sl_has_map
8247 && similar_chars(slang, gc, bc))
8248 score += SCORE_SIMILAR;
8249 else
8250 score += SCORE_SUBST;
8251 }
8252
8253 if (score < minscore)
8254 {
8255 /* Do the substitution. */
8256 ++gi;
8257 ++bi;
8258 continue;
8259 }
8260 }
8261pop:
8262 /*
8263 * Get here to try the next alternative, pop it from the stack.
8264 */
8265 if (stackidx == 0) /* stack is empty, finished */
8266 break;
8267
8268 /* pop an item from the stack */
8269 --stackidx;
8270 gi = stack[stackidx].goodi;
8271 bi = stack[stackidx].badi;
8272 score = stack[stackidx].score;
8273 }
8274
8275 /* When the score goes over "limit" it may actually be much higher.
8276 * Return a very large number to avoid going below the limit when giving a
8277 * bonus. */
8278 if (minscore > limit)
8279 return SCORE_MAXMAX;
8280 return minscore;
8281}
Bram Moolenaar4770d092006-01-12 23:22:24 +00008282
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008283/*
8284 * ":spellinfo"
8285 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008286 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008287ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008288{
8289 int lpi;
8290 langp_T *lp;
8291 char_u *p;
8292
8293 if (no_spell_checking(curwin))
8294 return;
8295
8296 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008297 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008298 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008299 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008300 msg_puts("file: ");
8301 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008302 msg_putchar('\n');
8303 p = lp->lp_slang->sl_info;
8304 if (p != NULL)
8305 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008306 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008307 msg_putchar('\n');
8308 }
8309 }
8310 msg_end();
8311}
8312
Bram Moolenaar4770d092006-01-12 23:22:24 +00008313#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8314#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008315#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008316#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8317#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008318
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008319/*
8320 * ":spelldump"
8321 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008322 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008323ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008324{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008325 char_u *spl;
8326 long dummy;
8327
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008328 if (no_spell_checking(curwin))
8329 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008330 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008331
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008332 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008333 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008334
8335 /* enable spelling locally in the new window */
8336 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008337 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008338 vim_free(spl);
8339
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008340 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008341 return;
8342
Bram Moolenaar860cae12010-06-05 23:22:07 +02008343 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008344
8345 /* Delete the empty line that we started with. */
8346 if (curbuf->b_ml.ml_line_count > 1)
8347 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8348
8349 redraw_later(NOT_VALID);
8350}
8351
8352/*
8353 * Go through all possible words and:
8354 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8355 * "ic" and "dir" are not used.
8356 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8357 */
8358 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008359spell_dump_compl(
8360 char_u *pat, /* leading part of the word */
8361 int ic, /* ignore case */
8362 int *dir, /* direction for adding matches */
8363 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008364{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008365 langp_T *lp;
8366 slang_T *slang;
8367 idx_T arridx[MAXWLEN];
8368 int curi[MAXWLEN];
8369 char_u word[MAXWLEN];
8370 int c;
8371 char_u *byts;
8372 idx_T *idxs;
8373 linenr_T lnum = 0;
8374 int round;
8375 int depth;
8376 int n;
8377 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008378 char_u *region_names = NULL; /* region names being used */
8379 int do_region = TRUE; /* dump region names and numbers */
8380 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008381 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008382 int dumpflags = dumpflags_arg;
8383 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008384
Bram Moolenaard0131a82006-03-04 21:46:13 +00008385 /* When ignoring case or when the pattern starts with capital pass this on
8386 * to dump_word(). */
8387 if (pat != NULL)
8388 {
8389 if (ic)
8390 dumpflags |= DUMPFLAG_ICASE;
8391 else
8392 {
8393 n = captype(pat, NULL);
8394 if (n == WF_ONECAP)
8395 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01008396 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00008397 dumpflags |= DUMPFLAG_ALLCAP;
8398 }
8399 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008400
Bram Moolenaar7887d882005-07-01 22:33:52 +00008401 /* Find out if we can support regions: All languages must support the same
8402 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008403 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008404 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008405 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008406 p = lp->lp_slang->sl_regions;
8407 if (p[0] != 0)
8408 {
8409 if (region_names == NULL) /* first language with regions */
8410 region_names = p;
8411 else if (STRCMP(region_names, p) != 0)
8412 {
8413 do_region = FALSE; /* region names are different */
8414 break;
8415 }
8416 }
8417 }
8418
8419 if (do_region && region_names != NULL)
8420 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008421 if (pat == NULL)
8422 {
8423 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8424 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8425 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008426 }
8427 else
8428 do_region = FALSE;
8429
8430 /*
8431 * Loop over all files loaded for the entries in 'spelllang'.
8432 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008433 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008434 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008435 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008436 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008437 if (slang->sl_fbyts == NULL) /* reloading failed */
8438 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008439
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008440 if (pat == NULL)
8441 {
8442 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8443 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8444 }
8445
8446 /* When matching with a pattern and there are no prefixes only use
8447 * parts of the tree that match "pat". */
8448 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008449 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008450 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008451 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008452
8453 /* round 1: case-folded tree
8454 * round 2: keep-case tree */
8455 for (round = 1; round <= 2; ++round)
8456 {
8457 if (round == 1)
8458 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008459 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008460 byts = slang->sl_fbyts;
8461 idxs = slang->sl_fidxs;
8462 }
8463 else
8464 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008465 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008466 byts = slang->sl_kbyts;
8467 idxs = slang->sl_kidxs;
8468 }
8469 if (byts == NULL)
8470 continue; /* array is empty */
8471
8472 depth = 0;
8473 arridx[0] = 0;
8474 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008475 while (depth >= 0 && !got_int
8476 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008477 {
8478 if (curi[depth] > byts[arridx[depth]])
8479 {
8480 /* Done all bytes at this node, go up one level. */
8481 --depth;
8482 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008483 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008484 }
8485 else
8486 {
8487 /* Do one more byte at this node. */
8488 n = arridx[depth] + curi[depth];
8489 ++curi[depth];
8490 c = byts[n];
8491 if (c == 0)
8492 {
8493 /* End of word, deal with the word.
8494 * Don't use keep-case words in the fold-case tree,
8495 * they will appear in the keep-case tree.
8496 * Only use the word when the region matches. */
8497 flags = (int)idxs[n];
8498 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008499 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008500 && (do_region
8501 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008502 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008503 & lp->lp_region) != 0))
8504 {
8505 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008506 if (!do_region)
8507 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008508
8509 /* Dump the basic word if there is no prefix or
8510 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008511 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008512 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008513 {
8514 dump_word(slang, word, pat, dir,
8515 dumpflags, flags, lnum);
8516 if (pat == NULL)
8517 ++lnum;
8518 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008519
8520 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008521 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008522 lnum = dump_prefixes(slang, word, pat, dir,
8523 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008524 }
8525 }
8526 else
8527 {
8528 /* Normal char, go one level deeper. */
8529 word[depth++] = c;
8530 arridx[depth] = idxs[n];
8531 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008532
8533 /* Check if this characters matches with the pattern.
8534 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008535 * Always ignore case here, dump_word() will check
8536 * proper case later. This isn't exactly right when
8537 * length changes for multi-byte characters with
8538 * ignore case... */
8539 if (depth <= patlen
8540 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008541 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008542 }
8543 }
8544 }
8545 }
8546 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008547}
8548
8549/*
8550 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008551 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008552 */
8553 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008554dump_word(
8555 slang_T *slang,
8556 char_u *word,
8557 char_u *pat,
8558 int *dir,
8559 int dumpflags,
8560 int wordflags,
8561 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008562{
8563 int keepcap = FALSE;
8564 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008565 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008566 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008567 char_u badword[MAXWLEN + 10];
8568 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008569 int flags = wordflags;
8570
8571 if (dumpflags & DUMPFLAG_ONECAP)
8572 flags |= WF_ONECAP;
8573 if (dumpflags & DUMPFLAG_ALLCAP)
8574 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008575
Bram Moolenaar4770d092006-01-12 23:22:24 +00008576 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008577 {
8578 /* Need to fix case according to "flags". */
8579 make_case_word(word, cword, flags);
8580 p = cword;
8581 }
8582 else
8583 {
8584 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008585 if ((dumpflags & DUMPFLAG_KEEPCASE)
8586 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008587 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008588 keepcap = TRUE;
8589 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008590 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008591
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008592 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008593 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008594 /* Add flags and regions after a slash. */
8595 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008596 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008597 STRCPY(badword, p);
8598 STRCAT(badword, "/");
8599 if (keepcap)
8600 STRCAT(badword, "=");
8601 if (flags & WF_BANNED)
8602 STRCAT(badword, "!");
8603 else if (flags & WF_RARE)
8604 STRCAT(badword, "?");
8605 if (flags & WF_REGION)
8606 for (i = 0; i < 7; ++i)
8607 if (flags & (0x10000 << i))
8608 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8609 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008610 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008611
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008612 if (dumpflags & DUMPFLAG_COUNT)
8613 {
8614 hashitem_T *hi;
8615
8616 /* Include the word count for ":spelldump!". */
8617 hi = hash_find(&slang->sl_wordcount, tw);
8618 if (!HASHITEM_EMPTY(hi))
8619 {
8620 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8621 tw, HI2WC(hi)->wc_count);
8622 p = IObuff;
8623 }
8624 }
8625
8626 ml_append(lnum, p, (colnr_T)0, FALSE);
8627 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008628 else if (((dumpflags & DUMPFLAG_ICASE)
8629 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8630 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008631 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008632 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008633 /* if dir was BACKWARD then honor it just once */
8634 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008635}
8636
8637/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008638 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8639 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008640 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008641 * Return the updated line number.
8642 */
8643 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008644dump_prefixes(
8645 slang_T *slang,
8646 char_u *word, /* case-folded word */
8647 char_u *pat,
8648 int *dir,
8649 int dumpflags,
8650 int flags, /* flags with prefix ID */
8651 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008652{
8653 idx_T arridx[MAXWLEN];
8654 int curi[MAXWLEN];
8655 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008656 char_u word_up[MAXWLEN];
8657 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008658 int c;
8659 char_u *byts;
8660 idx_T *idxs;
8661 linenr_T lnum = startlnum;
8662 int depth;
8663 int n;
8664 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008665 int i;
8666
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008667 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008668 * upper-case letter in word_up[]. */
8669 c = PTR2CHAR(word);
8670 if (SPELL_TOUPPER(c) != c)
8671 {
8672 onecap_copy(word, word_up, TRUE);
8673 has_word_up = TRUE;
8674 }
8675
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008676 byts = slang->sl_pbyts;
8677 idxs = slang->sl_pidxs;
8678 if (byts != NULL) /* array not is empty */
8679 {
8680 /*
8681 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008682 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008683 */
8684 depth = 0;
8685 arridx[0] = 0;
8686 curi[0] = 1;
8687 while (depth >= 0 && !got_int)
8688 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008689 n = arridx[depth];
8690 len = byts[n];
8691 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008692 {
8693 /* Done all bytes at this node, go up one level. */
8694 --depth;
8695 line_breakcheck();
8696 }
8697 else
8698 {
8699 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008700 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008701 ++curi[depth];
8702 c = byts[n];
8703 if (c == 0)
8704 {
8705 /* End of prefix, find out how many IDs there are. */
8706 for (i = 1; i < len; ++i)
8707 if (byts[n + i] != 0)
8708 break;
8709 curi[depth] += i - 1;
8710
Bram Moolenaar53805d12005-08-01 07:08:33 +00008711 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8712 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008713 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008714 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008715 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008716 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008717 : flags, lnum);
8718 if (lnum != 0)
8719 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008720 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008721
8722 /* Check for prefix that matches the word when the
8723 * first letter is upper-case, but only if the prefix has
8724 * a condition. */
8725 if (has_word_up)
8726 {
8727 c = valid_word_prefix(i, n, flags, word_up, slang,
8728 TRUE);
8729 if (c != 0)
8730 {
8731 vim_strncpy(prefix + depth, word_up,
8732 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008733 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008734 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008735 : flags, lnum);
8736 if (lnum != 0)
8737 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008738 }
8739 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008740 }
8741 else
8742 {
8743 /* Normal char, go one level deeper. */
8744 prefix[depth++] = c;
8745 arridx[depth] = idxs[n];
8746 curi[depth] = 1;
8747 }
8748 }
8749 }
8750 }
8751
8752 return lnum;
8753}
8754
Bram Moolenaar95529562005-08-25 21:21:38 +00008755/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008756 * Move "p" to the end of word "start".
8757 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008758 */
8759 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008760spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008761{
8762 char_u *p = start;
8763
Bram Moolenaar860cae12010-06-05 23:22:07 +02008764 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008765 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008766 return p;
8767}
8768
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008769#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008770/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008771 * For Insert mode completion CTRL-X s:
8772 * Find start of the word in front of column "startcol".
8773 * We don't check if it is badly spelled, with completion we can only change
8774 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008775 * Returns the column number of the word.
8776 */
8777 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008778spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008779{
8780 char_u *line;
8781 char_u *p;
8782 int col = 0;
8783
Bram Moolenaar95529562005-08-25 21:21:38 +00008784 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008785 return startcol;
8786
8787 /* Find a word character before "startcol". */
8788 line = ml_get_curline();
8789 for (p = line + startcol; p > line; )
8790 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008791 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008792 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008793 break;
8794 }
8795
8796 /* Go back to start of the word. */
8797 while (p > line)
8798 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008799 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008800 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008801 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008802 break;
8803 col = 0;
8804 }
8805
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008806 return col;
8807}
8808
8809/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008810 * Need to check for 'spellcapcheck' now, the word is removed before
8811 * expand_spelling() is called. Therefore the ugly global variable.
8812 */
8813static int spell_expand_need_cap;
8814
8815 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008816spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00008817{
8818 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
8819}
8820
8821/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008822 * Get list of spelling suggestions.
8823 * Used for Insert mode completion CTRL-X ?.
8824 * Returns the number of matches. The matches are in "matchp[]", array of
8825 * allocated strings.
8826 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008827 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008828expand_spelling(
8829 linenr_T lnum UNUSED,
8830 char_u *pat,
8831 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008832{
8833 garray_T ga;
8834
Bram Moolenaar4770d092006-01-12 23:22:24 +00008835 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008836 *matchp = ga.ga_data;
8837 return ga.ga_len;
8838}
8839#endif
8840
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008841#endif /* FEAT_SPELL */