blob: 83c232c524b03f766d8f94190cd66c4b01e495f9 [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 Moolenaar91acfff2017-03-12 19:22:36 +0100448 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +0100449 while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000450
Bram Moolenaar860cae12010-06-05 23:22:07 +0200451 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000452 {
453 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000454 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000455 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000456 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000457 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000458 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000459 if (capcol != NULL)
460 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000461
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000462 /* We always use the characters up to the next non-word character,
463 * also for bad words. */
464 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000465
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000466 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200467 mi.mi_capflags = 0;
468 mi.mi_cend = NULL;
469 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000470
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 /* case-fold the word with one non-word character, so that we can check
472 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000473 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100474 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000475
476 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
477 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000478 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000479
480 /* The word is bad unless we recognize it. */
481 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000482 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000483
484 /*
485 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000486 * We check them all, because a word may be matched longer in another
487 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200489 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000490 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200491 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000492
493 /* If reloading fails the language is still in the list but everything
494 * has been cleared. */
495 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
496 continue;
497
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000498 /* Check for a matching word in case-folded words. */
499 find_word(&mi, FIND_FOLDWORD);
500
501 /* Check for a matching word in keep-case words. */
502 find_word(&mi, FIND_KEEPWORD);
503
504 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000505 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000506
507 /* For a NOBREAK language, may want to use a word without a following
508 * word as a backup. */
509 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
510 && mi.mi_result2 != SP_BAD)
511 {
512 mi.mi_result = mi.mi_result2;
513 mi.mi_end = mi.mi_end2;
514 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000515
516 /* Count the word in the first language where it's found to be OK. */
517 if (count_word && mi.mi_result == SP_OK)
518 {
519 count_common_word(mi.mi_lp->lp_slang, ptr,
520 (int)(mi.mi_end - ptr), 1);
521 count_word = FALSE;
522 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000523 }
524
525 if (mi.mi_result != SP_OK)
526 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000527 /* If we found a number skip over it. Allows for "42nd". Do flag
528 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000529 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000530 {
531 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
532 return nrlen;
533 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000534
535 /* When we are at a non-word character there is no error, just
536 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100537 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000538 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200539 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000540 {
541 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100542 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000543
544 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200545 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000546 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100547 r = vim_regexec(&regmatch, ptr, 0);
548 wp->w_s->b_cap_prog = regmatch.regprog;
549 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000550 *capcol = (int)(regmatch.endp[0] - ptr);
551 }
552
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000553 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000554 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000555 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000556 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000557 else if (mi.mi_end == ptr)
558 /* Always include at least one character. Required for when there
559 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100560 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000561 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200562 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000563 {
564 char_u *p, *fp;
565 int save_result = mi.mi_result;
566
567 /* First language in 'spelllang' is NOBREAK. Find first position
568 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200569 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000570 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000571 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000572 p = mi.mi_word;
573 fp = mi.mi_fword;
574 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000575 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100576 MB_PTR_ADV(p);
577 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000578 if (p >= mi.mi_end)
579 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000580 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000581 find_word(&mi, FIND_COMPOUND);
582 if (mi.mi_result != SP_BAD)
583 {
584 mi.mi_end = p;
585 break;
586 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000587 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000588 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000589 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000590 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000591
592 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000593 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000594 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000595 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000596 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000597 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000598 }
599
Bram Moolenaar5195e452005-08-19 20:32:47 +0000600 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
601 {
602 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000603 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000604 return wrongcaplen;
605 }
606
Bram Moolenaar51485f02005-06-04 21:55:20 +0000607 return (int)(mi.mi_end - ptr);
608}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000609
Bram Moolenaar51485f02005-06-04 21:55:20 +0000610/*
611 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000612 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
613 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
614 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
615 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000616 *
617 * For a match mip->mi_result is updated.
618 */
619 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100620find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000621{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000622 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000623 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000624 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000625 int endidxcnt = 0;
626 int len;
627 int wlen = 0;
628 int flen;
629 int c;
630 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000631 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000632 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000633 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000634 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000635 slang_T *slang = mip->mi_lp->lp_slang;
636 unsigned flags;
637 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000638 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000639 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000640 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000641 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000642
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000643 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000644 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000645 /* Check for word with matching case in keep-case tree. */
646 ptr = mip->mi_word;
647 flen = 9999; /* no case folding, always enough bytes */
648 byts = slang->sl_kbyts;
649 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000650
651 if (mode == FIND_KEEPCOMPOUND)
652 /* Skip over the previously found word(s). */
653 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000654 }
655 else
656 {
657 /* Check for case-folded in case-folded tree. */
658 ptr = mip->mi_fword;
659 flen = mip->mi_fwordlen; /* available case-folded bytes */
660 byts = slang->sl_fbyts;
661 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000662
663 if (mode == FIND_PREFIX)
664 {
665 /* Skip over the prefix. */
666 wlen = mip->mi_prefixlen;
667 flen -= mip->mi_prefixlen;
668 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000669 else if (mode == FIND_COMPOUND)
670 {
671 /* Skip over the previously found word(s). */
672 wlen = mip->mi_compoff;
673 flen -= mip->mi_compoff;
674 }
675
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000676 }
677
Bram Moolenaar51485f02005-06-04 21:55:20 +0000678 if (byts == NULL)
679 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000680
Bram Moolenaar51485f02005-06-04 21:55:20 +0000681 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000682 * Repeat advancing in the tree until:
683 * - there is a byte that doesn't match,
684 * - we reach the end of the tree,
685 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000686 */
687 for (;;)
688 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000689 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000690 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000691
692 len = byts[arridx++];
693
694 /* If the first possible byte is a zero the word could end here.
695 * Remember this index, we first check for the longest word. */
696 if (byts[arridx] == 0)
697 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000698 if (endidxcnt == MAXWLEN)
699 {
700 /* Must be a corrupted spell file. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100701 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000702 return;
703 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000704 endlen[endidxcnt] = wlen;
705 endidx[endidxcnt++] = arridx++;
706 --len;
707
708 /* Skip over the zeros, there can be several flag/region
709 * combinations. */
710 while (len > 0 && byts[arridx] == 0)
711 {
712 ++arridx;
713 --len;
714 }
715 if (len == 0)
716 break; /* no children, word must end here */
717 }
718
719 /* Stop looking at end of the line. */
720 if (ptr[wlen] == NUL)
721 break;
722
723 /* Perform a binary search in the list of accepted bytes. */
724 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000725 if (c == TAB) /* <Tab> is handled like <Space> */
726 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000727 lo = arridx;
728 hi = arridx + len - 1;
729 while (lo < hi)
730 {
731 m = (lo + hi) / 2;
732 if (byts[m] > c)
733 hi = m - 1;
734 else if (byts[m] < c)
735 lo = m + 1;
736 else
737 {
738 lo = hi = m;
739 break;
740 }
741 }
742
743 /* Stop if there is no matching byte. */
744 if (hi < lo || byts[lo] != c)
745 break;
746
747 /* Continue at the child (if there is one). */
748 arridx = idxs[lo];
749 ++wlen;
750 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000751
752 /* One space in the good word may stand for several spaces in the
753 * checked word. */
754 if (c == ' ')
755 {
756 for (;;)
757 {
758 if (flen <= 0 && *mip->mi_fend != NUL)
759 flen = fold_more(mip);
760 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
761 break;
762 ++wlen;
763 --flen;
764 }
765 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000766 }
767
768 /*
769 * Verify that one of the possible endings is valid. Try the longest
770 * first.
771 */
772 while (endidxcnt > 0)
773 {
774 --endidxcnt;
775 arridx = endidx[endidxcnt];
776 wlen = endlen[endidxcnt];
777
Bram Moolenaar51485f02005-06-04 21:55:20 +0000778 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
779 continue; /* not at first byte of character */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200780 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000781 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000782 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000783 continue; /* next char is a word character */
784 word_ends = FALSE;
785 }
786 else
787 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000788 /* The prefix flag is before compound flags. Once a valid prefix flag
789 * has been found we try compound flags. */
790 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000791
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000792 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000793 {
794 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000795 * when folding case. This can be slow, take a shortcut when the
796 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000797 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000798 if (STRNCMP(ptr, p, wlen) != 0)
799 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100800 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
801 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000802 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000803 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000804 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000805
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000806 /* Check flags and region. For FIND_PREFIX check the condition and
807 * prefix ID.
808 * Repeat this if there are more flags/region alternatives until there
809 * is a match. */
810 res = SP_BAD;
811 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
812 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000813 {
814 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000815
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000816 /* For the fold-case tree check that the case of the checked word
817 * matches with what the word in the tree requires.
818 * For keep-case tree the case is always right. For prefixes we
819 * don't bother to check. */
820 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000821 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000822 if (mip->mi_cend != mip->mi_word + wlen)
823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000824 /* mi_capflags was set for a different word length, need
825 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000826 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000827 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000828 }
829
Bram Moolenaar0c405862005-06-22 22:26:26 +0000830 if (mip->mi_capflags == WF_KEEPCAP
831 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000832 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000833 }
834
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000835 /* When mode is FIND_PREFIX the word must support the prefix:
836 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000837 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000838 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000839 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000840 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000841 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000842 mip->mi_word + mip->mi_cprefixlen, slang,
843 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000844 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000845 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000846
847 /* Use the WF_RARE flag for a rare prefix. */
848 if (c & WF_RAREPFX)
849 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000850 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000851 }
852
Bram Moolenaar78622822005-08-23 21:00:13 +0000853 if (slang->sl_nobreak)
854 {
855 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
856 && (flags & WF_BANNED) == 0)
857 {
858 /* NOBREAK: found a valid following word. That's all we
859 * need to know, so return. */
860 mip->mi_result = SP_OK;
861 break;
862 }
863 }
864
865 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
866 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000867 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000868 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000869 * COMPOUNDMIN reject it quickly.
870 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000871 * that's too short... Myspell compatibility requires this
872 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000873 if (((unsigned)flags >> 24) == 0
874 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000875 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000876 /* For multi-byte chars check character length against
877 * COMPOUNDMIN. */
878 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000879 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000880 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
881 wlen - mip->mi_compoff) < slang->sl_compminlen)
882 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000883
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000884 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000885 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000886 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
887 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000888 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000889 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000890
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000891 /* Don't allow compounding on a side where an affix was added,
892 * unless COMPOUNDPERMITFLAG was used. */
893 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
894 continue;
895 if (!word_ends && (flags & WF_NOCOMPAFT))
896 continue;
897
Bram Moolenaard12a1322005-08-21 22:08:24 +0000898 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000899 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000900 ? slang->sl_compstartflags
901 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000902 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000903 continue;
904
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000905 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
906 * discard the compound word. */
907 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
908 continue;
909
Bram Moolenaare52325c2005-08-22 22:54:29 +0000910 if (mode == FIND_COMPOUND)
911 {
912 int capflags;
913
914 /* Need to check the caps type of the appended compound
915 * word. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000916 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
917 mip->mi_compoff) != 0)
918 {
919 /* case folding may have changed the length */
920 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100921 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
922 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000923 }
924 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000925 p = mip->mi_word + mip->mi_compoff;
926 capflags = captype(p, mip->mi_word + wlen);
927 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
928 && (flags & WF_FIXCAP) != 0))
929 continue;
930
931 if (capflags != WF_ALLCAP)
932 {
933 /* When the character before the word is a word
934 * character we do not accept a Onecap word. We do
935 * accept a no-caps word, even when the dictionary
936 * word specifies ONECAP. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100937 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100938 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000939 ? capflags == WF_ONECAP
940 : (flags & WF_ONECAP) != 0
941 && capflags != WF_ONECAP)
942 continue;
943 }
944 }
945
Bram Moolenaar5195e452005-08-19 20:32:47 +0000946 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000947 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000948 * the number of syllables must not be too large. */
949 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
950 mip->mi_compflags[mip->mi_complen + 1] = NUL;
951 if (word_ends)
952 {
953 char_u fword[MAXWLEN];
954
955 if (slang->sl_compsylmax < MAXWLEN)
956 {
957 /* "fword" is only needed for checking syllables. */
958 if (ptr == mip->mi_word)
959 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
960 else
961 vim_strncpy(fword, ptr, endlen[endidxcnt]);
962 }
963 if (!can_compound(slang, fword, mip->mi_compflags))
964 continue;
965 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000966 else if (slang->sl_comprules != NULL
967 && !match_compoundrule(slang, mip->mi_compflags))
968 /* The compound flags collected so far do not match any
969 * COMPOUNDRULE, discard the compounded word. */
970 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000971 }
972
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000973 /* Check NEEDCOMPOUND: can't use word without compounding. */
974 else if (flags & WF_NEEDCOMP)
975 continue;
976
Bram Moolenaar78622822005-08-23 21:00:13 +0000977 nobreak_result = SP_OK;
978
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000979 if (!word_ends)
980 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000981 int save_result = mip->mi_result;
982 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000983 langp_T *save_lp = mip->mi_lp;
984 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000985
986 /* Check that a valid word follows. If there is one and we
987 * are compounding, it will set "mi_result", thus we are
988 * always finished here. For NOBREAK we only check that a
989 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000990 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +0000991 if (slang->sl_nobreak)
992 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000993
994 /* Find following word in case-folded tree. */
995 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000996 if (has_mbyte && mode == FIND_KEEPWORD)
997 {
998 /* Compute byte length in case-folded word from "wlen":
999 * byte length in keep-case word. Length may change when
1000 * folding case. This can be slow, take a shortcut when
1001 * the case-folded word is equal to the keep-case word. */
1002 p = mip->mi_fword;
1003 if (STRNCMP(ptr, p, wlen) != 0)
1004 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001005 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
1006 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001007 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001008 }
1009 }
Bram Moolenaarba534352016-04-21 09:20:26 +02001010#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001011 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001012#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001013 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001014 if (flags & WF_COMPROOT)
1015 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001016
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001017 /* For NOBREAK we need to try all NOBREAK languages, at least
1018 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001019 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001020 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001021 if (slang->sl_nobreak)
1022 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001023 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001024 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1025 || !mip->mi_lp->lp_slang->sl_nobreak)
1026 continue;
1027 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001028
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001029 find_word(mip, FIND_COMPOUND);
1030
1031 /* When NOBREAK any word that matches is OK. Otherwise we
1032 * need to find the longest match, thus try with keep-case
1033 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001034 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1035 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001036 /* Find following word in keep-case tree. */
1037 mip->mi_compoff = wlen;
1038 find_word(mip, FIND_KEEPCOMPOUND);
1039
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001040#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1041 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1042 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001043 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1044 {
1045 /* Check for following word with prefix. */
1046 mip->mi_compoff = c;
1047 find_prefix(mip, FIND_COMPOUND);
1048 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001049#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001050 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001051
1052 if (!slang->sl_nobreak)
1053 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001054 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001055 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001056 if (flags & WF_COMPROOT)
1057 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001058 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001059
Bram Moolenaar78622822005-08-23 21:00:13 +00001060 if (slang->sl_nobreak)
1061 {
1062 nobreak_result = mip->mi_result;
1063 mip->mi_result = save_result;
1064 mip->mi_end = save_end;
1065 }
1066 else
1067 {
1068 if (mip->mi_result == SP_OK)
1069 break;
1070 continue;
1071 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001072 }
1073
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001074 if (flags & WF_BANNED)
1075 res = SP_BANNED;
1076 else if (flags & WF_REGION)
1077 {
1078 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001079 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001080 res = SP_OK;
1081 else
1082 res = SP_LOCAL;
1083 }
1084 else if (flags & WF_RARE)
1085 res = SP_RARE;
1086 else
1087 res = SP_OK;
1088
Bram Moolenaar78622822005-08-23 21:00:13 +00001089 /* Always use the longest match and the best result. For NOBREAK
1090 * we separately keep the longest match without a following good
1091 * word as a fall-back. */
1092 if (nobreak_result == SP_BAD)
1093 {
1094 if (mip->mi_result2 > res)
1095 {
1096 mip->mi_result2 = res;
1097 mip->mi_end2 = mip->mi_word + wlen;
1098 }
1099 else if (mip->mi_result2 == res
1100 && mip->mi_end2 < mip->mi_word + wlen)
1101 mip->mi_end2 = mip->mi_word + wlen;
1102 }
1103 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001104 {
1105 mip->mi_result = res;
1106 mip->mi_end = mip->mi_word + wlen;
1107 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001108 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001109 mip->mi_end = mip->mi_word + wlen;
1110
Bram Moolenaar78622822005-08-23 21:00:13 +00001111 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001112 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001113 }
1114
Bram Moolenaar78622822005-08-23 21:00:13 +00001115 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001116 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001117 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001118}
1119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001120/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001121 * Return TRUE if there is a match between the word ptr[wlen] and
1122 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1123 * word.
1124 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1125 * end of ptr[wlen] and the second part matches after it.
1126 */
1127 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001128match_checkcompoundpattern(
1129 char_u *ptr,
1130 int wlen,
1131 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001132{
1133 int i;
1134 char_u *p;
1135 int len;
1136
1137 for (i = 0; i + 1 < gap->ga_len; i += 2)
1138 {
1139 p = ((char_u **)gap->ga_data)[i + 1];
1140 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1141 {
1142 /* Second part matches at start of following compound word, now
1143 * check if first part matches at end of previous word. */
1144 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001145 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001146 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1147 return TRUE;
1148 }
1149 }
1150 return FALSE;
1151}
1152
1153/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001154 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1155 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001156 */
1157 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001158can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001159{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001160 char_u uflags[MAXWLEN * 2];
1161 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001162 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001163
1164 if (slang->sl_compprog == NULL)
1165 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001166 if (enc_utf8)
1167 {
1168 /* Need to convert the single byte flags to utf8 characters. */
1169 p = uflags;
1170 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001171 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001172 *p = NUL;
1173 p = uflags;
1174 }
1175 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00001176 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001177 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001178 return FALSE;
1179
Bram Moolenaare52325c2005-08-22 22:54:29 +00001180 /* Count the number of syllables. This may be slow, do it last. If there
1181 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001182 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001183 if (slang->sl_compsylmax < MAXWLEN
1184 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001185 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001186 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001187}
1188
1189/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001190 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1191 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1192 * lines if they don't contain wildcards.
1193 */
1194 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001195can_be_compound(
1196 trystate_T *sp,
1197 slang_T *slang,
1198 char_u *compflags,
1199 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001200{
1201 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1202 * then it can't possibly compound. */
1203 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1204 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1205 return FALSE;
1206
1207 /* If there are no wildcards, we can check if the flags collected so far
1208 * possibly can form a match with COMPOUNDRULE patterns. This only
1209 * makes sense when we have two or more words. */
1210 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1211 {
1212 int v;
1213
1214 compflags[sp->ts_complen] = flag;
1215 compflags[sp->ts_complen + 1] = NUL;
1216 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1217 compflags[sp->ts_complen] = NUL;
1218 return v;
1219 }
1220
1221 return TRUE;
1222}
1223
1224
1225/*
1226 * Return TRUE if the compound flags in compflags[] match the start of any
1227 * compound rule. This is used to stop trying a compound if the flags
1228 * collected so far can't possibly match any compound rule.
1229 * Caller must check that slang->sl_comprules is not NULL.
1230 */
1231 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001232match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001233{
1234 char_u *p;
1235 int i;
1236 int c;
1237
1238 /* loop over all the COMPOUNDRULE entries */
1239 for (p = slang->sl_comprules; *p != NUL; ++p)
1240 {
1241 /* loop over the flags in the compound word we have made, match
1242 * them against the current rule entry */
1243 for (i = 0; ; ++i)
1244 {
1245 c = compflags[i];
1246 if (c == NUL)
1247 /* found a rule that matches for the flags we have so far */
1248 return TRUE;
1249 if (*p == '/' || *p == NUL)
1250 break; /* end of rule, it's too short */
1251 if (*p == '[')
1252 {
1253 int match = FALSE;
1254
1255 /* compare against all the flags in [] */
1256 ++p;
1257 while (*p != ']' && *p != NUL)
1258 if (*p++ == c)
1259 match = TRUE;
1260 if (!match)
1261 break; /* none matches */
1262 }
1263 else if (*p != c)
1264 break; /* flag of word doesn't match flag in pattern */
1265 ++p;
1266 }
1267
1268 /* Skip to the next "/", where the next pattern starts. */
1269 p = vim_strchr(p, '/');
1270 if (p == NULL)
1271 break;
1272 }
1273
1274 /* Checked all the rules and none of them match the flags, so there
1275 * can't possibly be a compound starting with these flags. */
1276 return FALSE;
1277}
1278
1279/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001280 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1281 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001282 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001283 */
1284 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001285valid_word_prefix(
1286 int totprefcnt, /* nr of prefix IDs */
1287 int arridx, /* idx in sl_pidxs[] */
1288 int flags,
1289 char_u *word,
1290 slang_T *slang,
1291 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001292{
1293 int prefcnt;
1294 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001295 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001296 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001297
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001298 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001299 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1300 {
1301 pidx = slang->sl_pidxs[arridx + prefcnt];
1302
1303 /* Check the prefix ID. */
1304 if (prefid != (pidx & 0xff))
1305 continue;
1306
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001307 /* Check if the prefix doesn't combine and the word already has a
1308 * suffix. */
1309 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1310 continue;
1311
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001312 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001313 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001314 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1315 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001316 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001317 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001318 continue;
1319 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001320 else if (cond_req)
1321 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001322
Bram Moolenaar53805d12005-08-01 07:08:33 +00001323 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001324 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001325 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001326 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001327}
1328
1329/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001330 * Check if the word at "mip->mi_word" has a matching prefix.
1331 * If it does, then check the following word.
1332 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001333 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1334 * prefix in a compound word.
1335 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001336 * For a match mip->mi_result is updated.
1337 */
1338 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001339find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001340{
1341 idx_T arridx = 0;
1342 int len;
1343 int wlen = 0;
1344 int flen;
1345 int c;
1346 char_u *ptr;
1347 idx_T lo, hi, m;
1348 slang_T *slang = mip->mi_lp->lp_slang;
1349 char_u *byts;
1350 idx_T *idxs;
1351
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001352 byts = slang->sl_pbyts;
1353 if (byts == NULL)
1354 return; /* array is empty */
1355
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001356 /* We use the case-folded word here, since prefixes are always
1357 * case-folded. */
1358 ptr = mip->mi_fword;
1359 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001360 if (mode == FIND_COMPOUND)
1361 {
1362 /* Skip over the previously found word(s). */
1363 ptr += mip->mi_compoff;
1364 flen -= mip->mi_compoff;
1365 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001366 idxs = slang->sl_pidxs;
1367
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001368 /*
1369 * Repeat advancing in the tree until:
1370 * - there is a byte that doesn't match,
1371 * - we reach the end of the tree,
1372 * - or we reach the end of the line.
1373 */
1374 for (;;)
1375 {
1376 if (flen == 0 && *mip->mi_fend != NUL)
1377 flen = fold_more(mip);
1378
1379 len = byts[arridx++];
1380
1381 /* If the first possible byte is a zero the prefix could end here.
1382 * Check if the following word matches and supports the prefix. */
1383 if (byts[arridx] == 0)
1384 {
1385 /* There can be several prefixes with different conditions. We
1386 * try them all, since we don't know which one will give the
1387 * longest match. The word is the same each time, pass the list
1388 * of possible prefixes to find_word(). */
1389 mip->mi_prefarridx = arridx;
1390 mip->mi_prefcnt = len;
1391 while (len > 0 && byts[arridx] == 0)
1392 {
1393 ++arridx;
1394 --len;
1395 }
1396 mip->mi_prefcnt -= len;
1397
1398 /* Find the word that comes after the prefix. */
1399 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001400 if (mode == FIND_COMPOUND)
1401 /* Skip over the previously found word(s). */
1402 mip->mi_prefixlen += mip->mi_compoff;
1403
Bram Moolenaar53805d12005-08-01 07:08:33 +00001404 if (has_mbyte)
1405 {
1406 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001407 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1408 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001409 }
1410 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001411 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 find_word(mip, FIND_PREFIX);
1413
1414
1415 if (len == 0)
1416 break; /* no children, word must end here */
1417 }
1418
1419 /* Stop looking at end of the line. */
1420 if (ptr[wlen] == NUL)
1421 break;
1422
1423 /* Perform a binary search in the list of accepted bytes. */
1424 c = ptr[wlen];
1425 lo = arridx;
1426 hi = arridx + len - 1;
1427 while (lo < hi)
1428 {
1429 m = (lo + hi) / 2;
1430 if (byts[m] > c)
1431 hi = m - 1;
1432 else if (byts[m] < c)
1433 lo = m + 1;
1434 else
1435 {
1436 lo = hi = m;
1437 break;
1438 }
1439 }
1440
1441 /* Stop if there is no matching byte. */
1442 if (hi < lo || byts[lo] != c)
1443 break;
1444
1445 /* Continue at the child (if there is one). */
1446 arridx = idxs[lo];
1447 ++wlen;
1448 --flen;
1449 }
1450}
1451
1452/*
1453 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001454 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001455 * Return the length of the folded chars in bytes.
1456 */
1457 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001458fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001459{
1460 int flen;
1461 char_u *p;
1462
1463 p = mip->mi_fend;
1464 do
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001465 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001466 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001467
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001468 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001470 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001471
1472 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1473 mip->mi_fword + mip->mi_fwordlen,
1474 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001475 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001476 mip->mi_fwordlen += flen;
1477 return flen;
1478}
1479
1480/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001481 * Check case flags for a word. Return TRUE if the word has the requested
1482 * case.
1483 */
1484 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001485spell_valid_case(
1486 int wordflags, /* flags for the checked word. */
1487 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001488{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001489 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001491 && ((treeflags & WF_ONECAP) == 0
1492 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001493}
1494
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001495/*
1496 * Return TRUE if spell checking is not enabled.
1497 */
1498 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001499no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001500{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001501 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1502 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001504 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001505 return TRUE;
1506 }
1507 return FALSE;
1508}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001509
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001510/*
1511 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001512 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1513 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001514 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1515 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001516 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001517 */
1518 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001519spell_move_to(
1520 win_T *wp,
1521 int dir, /* FORWARD or BACKWARD */
1522 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
1523 int curline,
1524 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001525 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001526{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001527 linenr_T lnum;
1528 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001529 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001530 char_u *line;
1531 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001532 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001533 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001534 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001535#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001536 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001537#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001538 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001539 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001540 char_u *buf = NULL;
1541 int buflen = 0;
1542 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001543 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001544 int found_one = FALSE;
1545 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001546
Bram Moolenaar95529562005-08-25 21:21:38 +00001547 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001548 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001549
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001550 /*
1551 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001552 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001553 *
1554 * When searching backwards, we continue in the line to find the last
1555 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001556 *
1557 * We concatenate the start of the next line, so that wrapped words work
1558 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1559 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001560 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001561 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001562 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001563
1564 while (!got_int)
1565 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001566 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001568 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001569 if (buflen < len + MAXWLEN + 2)
1570 {
1571 vim_free(buf);
1572 buflen = len + MAXWLEN + 2;
1573 buf = alloc(buflen);
1574 if (buf == NULL)
1575 break;
1576 }
1577
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001578 /* In first line check first word for Capital. */
1579 if (lnum == 1)
1580 capcol = 0;
1581
1582 /* For checking first word with a capital skip white space. */
1583 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001584 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001585 else if (curline && wp == curwin)
1586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001587 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001588 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001589 if (check_need_cap(lnum, col))
1590 capcol = col;
1591
1592 /* Need to get the line again, may have looked at the previous
1593 * one. */
1594 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1595 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001596
Bram Moolenaar0c405862005-06-22 22:26:26 +00001597 /* Copy the line into "buf" and append the start of the next line if
1598 * possible. */
1599 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001600 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001601 spell_cat_line(buf + STRLEN(buf),
1602 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001603
1604 p = buf + skip;
1605 endp = buf + len;
1606 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001607 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001608 /* When searching backward don't search after the cursor. Unless
1609 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001610 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001611 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001612 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001613 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001614 break;
1615
1616 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001617 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001618 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001619
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001620 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001621 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001622 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001623 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001624 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001625 /* When searching forward only accept a bad word after
1626 * the cursor. */
1627 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001628 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001629 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001630 && (wrapped
1631 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001632 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001633 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001634 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001635#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001636 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001637 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001638 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001639 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001640 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001641 if (!can_spell)
1642 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001643 }
1644 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001645#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001646 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001647
Bram Moolenaar51485f02005-06-04 21:55:20 +00001648 if (can_spell)
1649 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001650 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001651 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001652 found_pos.col = (int)(p - buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001653 found_pos.coladd = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001654 if (dir == FORWARD)
1655 {
1656 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001657 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001658 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001659 if (attrp != NULL)
1660 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001661 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001662 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001663 else if (curline)
1664 /* Insert mode completion: put cursor after
1665 * the bad word. */
1666 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001667 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001668 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001669 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001670 else
1671 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001672 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001673 }
1674
Bram Moolenaar51485f02005-06-04 21:55:20 +00001675 /* advance to character after the word */
1676 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001677 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001678 }
1679
Bram Moolenaar5195e452005-08-19 20:32:47 +00001680 if (dir == BACKWARD && found_pos.lnum != 0)
1681 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001682 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001683 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001684 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001685 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001686 }
1687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001688 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001689 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001690
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001691 /* If we are back at the starting line and searched it again there
1692 * is no match, give up. */
1693 if (lnum == wp->w_cursor.lnum && wrapped)
1694 break;
1695
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001696 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001697 if (dir == BACKWARD)
1698 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001699 if (lnum > 1)
1700 --lnum;
1701 else if (!p_ws)
1702 break; /* at first line and 'nowrapscan' */
1703 else
1704 {
1705 /* Wrap around to the end of the buffer. May search the
1706 * starting line again and accept the last match. */
1707 lnum = wp->w_buffer->b_ml.ml_line_count;
1708 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001709 if (!shortmess(SHM_SEARCH))
1710 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001711 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001712 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001713 }
1714 else
1715 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001716 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1717 ++lnum;
1718 else if (!p_ws)
1719 break; /* at first line and 'nowrapscan' */
1720 else
1721 {
1722 /* Wrap around to the start of the buffer. May search the
1723 * starting line again and accept the first match. */
1724 lnum = 1;
1725 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001726 if (!shortmess(SHM_SEARCH))
1727 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001728 }
1729
1730 /* If we are back at the starting line and there is no match then
1731 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001732 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001733 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001734
1735 /* Skip the characters at the start of the next line that were
1736 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001737 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001738 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001739 else
1740 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001741
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001742 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001743 --capcol;
1744
1745 /* But after empty line check first word in next line */
1746 if (*skipwhite(line) == NUL)
1747 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001748 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001749
1750 line_breakcheck();
1751 }
1752
Bram Moolenaar0c405862005-06-22 22:26:26 +00001753 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001754 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001755}
1756
1757/*
1758 * For spell checking: concatenate the start of the following line "line" into
1759 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001760 * Keep the blanks at the start of the next line, this is used in win_line()
1761 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001762 */
1763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001764spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001765{
1766 char_u *p;
1767 int n;
1768
1769 p = skipwhite(line);
1770 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1771 p = skipwhite(p + 1);
1772
1773 if (*p != NUL)
1774 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001775 /* Only worth concatenating if there is something else than spaces to
1776 * concatenate. */
1777 n = (int)(p - line) + 1;
1778 if (n < maxlen - 1)
1779 {
1780 vim_memset(buf, ' ', n);
1781 vim_strncpy(buf + n, p, maxlen - 1 - n);
1782 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001784}
1785
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001786/*
1787 * Structure used for the cookie argument of do_in_runtimepath().
1788 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001789typedef struct spelload_S
1790{
1791 char_u sl_lang[MAXWLEN + 1]; /* language name */
1792 slang_T *sl_slang; /* resulting slang_T struct */
1793 int sl_nobreak; /* NOBREAK language found */
1794} spelload_T;
1795
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001796/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001797 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001798 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001799 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001800 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001801spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001803 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001805 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001806 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001807
Bram Moolenaarb765d632005-06-07 21:00:02 +00001808 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001809 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001810 STRCPY(sl.sl_lang, lang);
1811 sl.sl_slang = NULL;
1812 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001813
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001814 /* We may retry when no spell file is found for the language, an
1815 * autocommand may load it then. */
1816 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001817 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001818 /*
1819 * Find the first spell file for "lang" in 'runtimepath' and load it.
1820 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001821 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001822#ifdef VMS
1823 "spell/%s_%s.spl",
1824#else
1825 "spell/%s.%s.spl",
1826#endif
1827 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001828 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001829
1830 if (r == FAIL && *sl.sl_lang != NUL)
1831 {
1832 /* Try loading the ASCII version. */
1833 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001834#ifdef VMS
1835 "spell/%s_ascii.spl",
1836#else
1837 "spell/%s.ascii.spl",
1838#endif
1839 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001840 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001841
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001842 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1843 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1844 curbuf->b_fname, FALSE, curbuf))
1845 continue;
1846 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001847 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001848 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001849 }
1850
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001851 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001852 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001853 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001854#ifdef VMS
1855 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1856#else
1857 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1858#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001859 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001860 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001861 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001862 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001863 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001864 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001865 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001866 }
1867}
1868
1869/*
1870 * Return the encoding used for spell checking: Use 'encoding', except that we
1871 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1872 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001873 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001874spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001875{
1876
Bram Moolenaarb765d632005-06-07 21:00:02 +00001877 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1878 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001879 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001880}
1881
1882/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001883 * Get the name of the .spl file for the internal wordlist into
1884 * "fname[MAXPATHL]".
1885 */
1886 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001887int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001888{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001889 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001890 int_wordlist, spell_enc());
1891}
1892
1893/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001894 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001895 * Caller must fill "sl_next".
1896 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001897 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001898slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001899{
1900 slang_T *lp;
1901
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001902 lp = ALLOC_CLEAR_ONE(slang_T);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903 if (lp != NULL)
1904 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001905 if (lang != NULL)
1906 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001907 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001908 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001909 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001910 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001911 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001912 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001913
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001914 return lp;
1915}
1916
1917/*
1918 * Free the contents of an slang_T and the structure itself.
1919 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001920 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001921slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001922{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001923 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001924 vim_free(lp->sl_fname);
1925 slang_clear(lp);
1926 vim_free(lp);
1927}
1928
1929/*
1930 * Clear an slang_T so that the file can be reloaded.
1931 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001932 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001933slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001934{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001935 garray_T *gap;
1936 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001937 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001938 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001939 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940
Bram Moolenaard23a8232018-02-10 18:45:26 +01001941 VIM_CLEAR(lp->sl_fbyts);
1942 VIM_CLEAR(lp->sl_kbyts);
1943 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001944
Bram Moolenaard23a8232018-02-10 18:45:26 +01001945 VIM_CLEAR(lp->sl_fidxs);
1946 VIM_CLEAR(lp->sl_kidxs);
1947 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001948
Bram Moolenaar4770d092006-01-12 23:22:24 +00001949 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001950 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001951 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1952 while (gap->ga_len > 0)
1953 {
1954 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1955 vim_free(ftp->ft_from);
1956 vim_free(ftp->ft_to);
1957 }
1958 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001959 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001960
1961 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001962 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001963 {
1964 /* "ga_len" is set to 1 without adding an item for latin1 */
1965 if (gap->ga_data != NULL)
1966 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1967 for (i = 0; i < gap->ga_len; ++i)
1968 vim_free(((int **)gap->ga_data)[i]);
1969 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001970 else
1971 /* SAL items: free salitem_T items */
1972 while (gap->ga_len > 0)
1973 {
1974 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1975 vim_free(smp->sm_lead);
1976 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1977 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001978 vim_free(smp->sm_lead_w);
1979 vim_free(smp->sm_oneof_w);
1980 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001981 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001982 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001983
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001984 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001985 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001986 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001987 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001988
Bram Moolenaard23a8232018-02-10 18:45:26 +01001989 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001990
Bram Moolenaard23a8232018-02-10 18:45:26 +01001991 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001992
Bram Moolenaar473de612013-06-08 18:19:48 +02001993 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001994 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001995 VIM_CLEAR(lp->sl_comprules);
1996 VIM_CLEAR(lp->sl_compstartflags);
1997 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001998
Bram Moolenaard23a8232018-02-10 18:45:26 +01001999 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002000 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002001
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002002 ga_clear_strings(&lp->sl_comppat);
2003
Bram Moolenaar4770d092006-01-12 23:22:24 +00002004 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2005 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002006
Bram Moolenaar4770d092006-01-12 23:22:24 +00002007 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002008
Bram Moolenaar4770d092006-01-12 23:22:24 +00002009 /* Clear info from .sug file. */
2010 slang_clear_sug(lp);
2011
Bram Moolenaar5195e452005-08-19 20:32:47 +00002012 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002013 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002014 lp->sl_compsylmax = MAXWLEN;
2015 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002016}
2017
2018/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002019 * Clear the info from the .sug file in "lp".
2020 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002021 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002022slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002023{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002024 VIM_CLEAR(lp->sl_sbyts);
2025 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002026 close_spellbuf(lp->sl_sugbuf);
2027 lp->sl_sugbuf = NULL;
2028 lp->sl_sugloaded = FALSE;
2029 lp->sl_sugtime = 0;
2030}
2031
2032/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002033 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002034 * Invoked through do_in_runtimepath().
2035 */
2036 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002037spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002038{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002039 spelload_T *slp = (spelload_T *)cookie;
2040 slang_T *slang;
2041
2042 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2043 if (slang != NULL)
2044 {
2045 /* When a previously loaded file has NOBREAK also use it for the
2046 * ".add" files. */
2047 if (slp->sl_nobreak && slang->sl_add)
2048 slang->sl_nobreak = TRUE;
2049 else if (slang->sl_nobreak)
2050 slp->sl_nobreak = TRUE;
2051
2052 slp->sl_slang = slang;
2053 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002054}
2055
Bram Moolenaar4770d092006-01-12 23:22:24 +00002056
2057/*
2058 * Add a word to the hashtable of common words.
2059 * If it's already there then the counter is increased.
2060 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002061 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002062count_common_word(
2063 slang_T *lp,
2064 char_u *word,
2065 int len, /* word length, -1 for upto NUL */
2066 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002067{
2068 hash_T hash;
2069 hashitem_T *hi;
2070 wordcount_T *wc;
2071 char_u buf[MAXWLEN];
2072 char_u *p;
2073
2074 if (len == -1)
2075 p = word;
Bram Moolenaar5bcc5a12019-08-06 22:48:02 +02002076 else if (len >= MAXWLEN)
2077 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002078 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 Moolenaarc799fe22019-05-28 23:08:19 +02002088 wc = alloc(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 Moolenaar8f130ed2019-04-10 22:15:19 +02002313 // Get one language name.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002314 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 Moolenaar8f130ed2019-04-10 22:15:19 +02002318 if (!valid_spellang(lang))
2319 continue;
2320
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002321 if (STRCMP(lang, "cjk") == 0)
2322 {
2323 wp->w_s->b_cjk = 1;
2324 continue;
2325 }
2326
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002327 /* If the name ends in ".spl" use it as the name of the spell file.
2328 * If there is a region name let "region" point to it and remove it
2329 * from the name. */
2330 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2331 {
2332 filename = TRUE;
2333
Bram Moolenaarb6356332005-07-18 21:40:44 +00002334 /* Locate a region and remove it from the file name. */
2335 p = vim_strchr(gettail(lang), '_');
2336 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2337 && !ASCII_ISALPHA(p[3]))
2338 {
2339 vim_strncpy(region_cp, p + 1, 2);
2340 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002341 region = region_cp;
2342 }
2343 else
2344 dont_use_region = TRUE;
2345
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002346 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002347 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002348 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002349 break;
2350 }
2351 else
2352 {
2353 filename = FALSE;
2354 if (len > 3 && lang[len - 3] == '_')
2355 {
2356 region = lang + len - 2;
2357 len -= 3;
2358 lang[len] = NUL;
2359 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002360 else
2361 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002362
2363 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002364 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2365 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002366 break;
2367 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002368
Bram Moolenaarb6356332005-07-18 21:40:44 +00002369 if (region != NULL)
2370 {
2371 /* If the region differs from what was used before then don't
2372 * use it for 'spellfile'. */
2373 if (use_region != NULL && STRCMP(region, use_region) != 0)
2374 dont_use_region = TRUE;
2375 use_region = region;
2376 }
2377
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002378 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002379 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002380 {
2381 if (filename)
2382 (void)spell_load_file(lang, lang, NULL, FALSE);
2383 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002384 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002385 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002386 /* SpellFileMissing autocommands may do anything, including
2387 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002388 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002390 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002391 goto theend;
2392 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002393 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002394 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002396 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002397 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002398 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002399 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002400 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE)
2401 == FPC_SAME
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002402 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002404 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002405 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002406 {
2407 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002408 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002409 if (c == REGION_ALL)
2410 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002411 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002412 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002414 /* This addition file is for other regions. */
2415 region_mask = 0;
2416 }
2417 else
2418 /* This is probably an error. Give a warning and
2419 * accept the words anyway. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002420 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002421 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002422 }
2423 else
2424 region_mask = 1 << c;
2425 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002426
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002427 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002428 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002429 if (ga_grow(&ga, 1) == FAIL)
2430 {
2431 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002432 ret_msg = e_outofmem;
2433 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002434 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002435 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002436 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2437 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002438 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002439 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002440 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002441 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002442 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443 }
2444
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002445 /* round 0: load int_wordlist, if possible.
2446 * round 1: load first name in 'spellfile'.
2447 * round 2: load second name in 'spellfile.
2448 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002449 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002450 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002452 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002453 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002454 /* Internal wordlist, if there is one. */
2455 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002456 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002457 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002458 }
2459 else
2460 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002461 /* One entry in 'spellfile'. */
2462 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2463 STRCAT(spf_name, ".spl");
2464
2465 /* If it was already found above then skip it. */
2466 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002467 {
2468 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
Bram Moolenaar99499b12019-05-23 21:35:48 +02002469 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE)
2470 == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002471 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002472 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002473 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002474 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002475 }
2476
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002477 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002478 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02002479 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE)
2480 == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002482 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002483 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002484 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002485 * region name, the region is ignored otherwise. for int_wordlist
2486 * use an arbitrary name. */
2487 if (round == 0)
2488 STRCPY(lang, "internal wordlist");
2489 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002490 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002491 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002492 p = vim_strchr(lang, '.');
2493 if (p != NULL)
2494 *p = NUL; /* truncate at ".encoding.add" */
2495 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002497
2498 /* If one of the languages has NOBREAK we assume the addition
2499 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002500 if (slang != NULL && nobreak)
2501 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002503 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002505 region_mask = REGION_ALL;
2506 if (use_region != NULL && !dont_use_region)
2507 {
2508 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002509 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002510 if (c != REGION_ALL)
2511 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002512 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002513 /* This spell file is for other regions. */
2514 region_mask = 0;
2515 }
2516
2517 if (region_mask != 0)
2518 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002519 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2520 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2521 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002522 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2523 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002524 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002526 }
2527 }
2528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002529 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002530 ga_clear(&wp->w_s->b_langp);
2531 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002532
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002533 /* For each language figure out what language to use for sound folding and
2534 * REP items. If the language doesn't support it itself use another one
2535 * with the same name. E.g. for "en-math" use "en". */
2536 for (i = 0; i < ga.ga_len; ++i)
2537 {
2538 lp = LANGP_ENTRY(ga, i);
2539
2540 /* sound folding */
2541 if (lp->lp_slang->sl_sal.ga_len > 0)
2542 /* language does sound folding itself */
2543 lp->lp_sallang = lp->lp_slang;
2544 else
2545 /* find first similar language that does sound folding */
2546 for (j = 0; j < ga.ga_len; ++j)
2547 {
2548 lp2 = LANGP_ENTRY(ga, j);
2549 if (lp2->lp_slang->sl_sal.ga_len > 0
2550 && STRNCMP(lp->lp_slang->sl_name,
2551 lp2->lp_slang->sl_name, 2) == 0)
2552 {
2553 lp->lp_sallang = lp2->lp_slang;
2554 break;
2555 }
2556 }
2557
2558 /* REP items */
2559 if (lp->lp_slang->sl_rep.ga_len > 0)
2560 /* language has REP items itself */
2561 lp->lp_replang = lp->lp_slang;
2562 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002563 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002564 for (j = 0; j < ga.ga_len; ++j)
2565 {
2566 lp2 = LANGP_ENTRY(ga, j);
2567 if (lp2->lp_slang->sl_rep.ga_len > 0
2568 && STRNCMP(lp->lp_slang->sl_name,
2569 lp2->lp_slang->sl_name, 2) == 0)
2570 {
2571 lp->lp_replang = lp2->lp_slang;
2572 break;
2573 }
2574 }
2575 }
2576
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002577theend:
2578 vim_free(spl_copy);
2579 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002580 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002581 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002582}
2583
2584/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002585 * Clear the midword characters for buffer "buf".
2586 */
2587 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002588clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002589{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002590 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002591 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002592}
2593
2594/*
2595 * Use the "sl_midword" field of language "lp" for buffer "buf".
2596 * They add up to any currently used midword characters.
2597 */
2598 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002599use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002600{
2601 char_u *p;
2602
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002603 if (lp->sl_midword == NULL) /* there aren't any */
2604 return;
2605
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002606 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002607 if (has_mbyte)
2608 {
2609 int c, l, n;
2610 char_u *bp;
2611
2612 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002613 l = (*mb_ptr2len)(p);
2614 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002615 wp->w_s->b_spell_ismw[c] = TRUE;
2616 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002617 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002618 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002619 else
2620 {
2621 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002622 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2623 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002624 if (bp != NULL)
2625 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002626 vim_free(wp->w_s->b_spell_ismw_mb);
2627 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002628 vim_strncpy(bp + n, p, l);
2629 }
2630 }
2631 p += l;
2632 }
2633 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002634 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002635}
2636
2637/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002638 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002639 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002640 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002641 */
2642 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002643find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002644{
2645 int i;
2646
2647 for (i = 0; ; i += 2)
2648 {
2649 if (rp[i] == NUL)
2650 return REGION_ALL;
2651 if (rp[i] == region[0] && rp[i + 1] == region[1])
2652 break;
2653 }
2654 return i / 2;
2655}
2656
2657/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002658 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002660 * Word WF_ONECAP
2661 * W WORD WF_ALLCAP
2662 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002663 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002664 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002665captype(
2666 char_u *word,
2667 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002668{
2669 char_u *p;
2670 int c;
2671 int firstcap;
2672 int allcap;
2673 int past_second = FALSE; /* past second word char */
2674
2675 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002676 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002678 return 0; /* only non-word characters, illegal word */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002679 if (has_mbyte)
2680 c = mb_ptr2char_adv(&p);
2681 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002682 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002683 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002684
2685 /*
2686 * Need to check all letters to find a word with mixed upper/lower.
2687 * But a word with an upper char only at start is a ONECAP.
2688 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002689 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002690 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002692 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002693 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002694 {
2695 /* UUl -> KEEPCAP */
2696 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002697 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002698 allcap = FALSE;
2699 }
2700 else if (!allcap)
2701 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002702 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002703 past_second = TRUE;
2704 }
2705
2706 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002707 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002708 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002709 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002710 return 0;
2711}
2712
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002713/*
2714 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2715 * capital. So that make_case_word() can turn WOrd into Word.
2716 * Add ALLCAP for "WOrD".
2717 */
2718 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002719badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002720{
2721 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002722 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002723 int l, u;
2724 int first;
2725 char_u *p;
2726
2727 if (flags & WF_KEEPCAP)
2728 {
2729 /* Count the number of UPPER and lower case letters. */
2730 l = u = 0;
2731 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002732 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002733 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002734 c = PTR2CHAR(p);
2735 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002736 {
2737 ++u;
2738 if (p == word)
2739 first = TRUE;
2740 }
2741 else
2742 ++l;
2743 }
2744
2745 /* If there are more UPPER than lower case letters suggest an
2746 * ALLCAP word. Otherwise, if the first letter is UPPER then
2747 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2748 * require three upper case letters. */
2749 if (u > l && u > 2)
2750 flags |= WF_ALLCAP;
2751 else if (first)
2752 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002753
2754 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2755 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002756 }
2757 return flags;
2758}
2759
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002760/*
2761 * Delete the internal wordlist and its .spl file.
2762 */
2763 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002764spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002765{
2766 char_u fname[MAXPATHL];
2767
2768 if (int_wordlist != NULL)
2769 {
2770 mch_remove(int_wordlist);
2771 int_wordlist_spl(fname);
2772 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002773 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002774 }
2775}
2776
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002777/*
2778 * Free all languages.
2779 */
2780 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002781spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002782{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002783 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002784 buf_T *buf;
2785
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002786 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002787 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002788 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002789
2790 while (first_lang != NULL)
2791 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002792 slang = first_lang;
2793 first_lang = slang->sl_next;
2794 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002795 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002796
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002797 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002798
Bram Moolenaard23a8232018-02-10 18:45:26 +01002799 VIM_CLEAR(repl_to);
2800 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002801}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002802
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002803/*
2804 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002805 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002806 */
2807 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002808spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002809{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002810 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002811
Bram Moolenaarea408852005-06-25 22:49:46 +00002812 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813 init_spell_chartab();
2814
2815 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002816 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002817
2818 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002819 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002820 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002821 /* Only load the wordlists when 'spelllang' is set and there is a
2822 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002823 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002824 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002825 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002826 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002827 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002828 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002829 }
2830 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002831 }
2832}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002833
Bram Moolenaarb765d632005-06-07 21:00:02 +00002834/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002835 * Opposite of offset2bytes().
2836 * "pp" points to the bytes and is advanced over it.
2837 * Returns the offset.
2838 */
2839 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002840bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002841{
2842 char_u *p = *pp;
2843 int nr;
2844 int c;
2845
2846 c = *p++;
2847 if ((c & 0x80) == 0x00) /* 1 byte */
2848 {
2849 nr = c - 1;
2850 }
2851 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2852 {
2853 nr = (c & 0x3f) - 1;
2854 nr = nr * 255 + (*p++ - 1);
2855 }
2856 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2857 {
2858 nr = (c & 0x1f) - 1;
2859 nr = nr * 255 + (*p++ - 1);
2860 nr = nr * 255 + (*p++ - 1);
2861 }
2862 else /* 4 bytes */
2863 {
2864 nr = (c & 0x0f) - 1;
2865 nr = nr * 255 + (*p++ - 1);
2866 nr = nr * 255 + (*p++ - 1);
2867 nr = nr * 255 + (*p++ - 1);
2868 }
2869
2870 *pp = p;
2871 return nr;
2872}
2873
Bram Moolenaar4770d092006-01-12 23:22:24 +00002874
2875/*
2876 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2877 * list and only contains text lines. Can use a swapfile to reduce memory
2878 * use.
2879 * Most other fields are invalid! Esp. watch out for string options being
2880 * NULL and there is no undo info.
2881 * Returns NULL when out of memory.
2882 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002883 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002884open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002885{
2886 buf_T *buf;
2887
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002888 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002889 if (buf != NULL)
2890 {
2891 buf->b_spell = TRUE;
2892 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002893#ifdef FEAT_CRYPT
2894 buf->b_p_key = empty_option;
2895#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002896 ml_open(buf);
2897 ml_open_file(buf); /* create swap file now */
2898 }
2899 return buf;
2900}
2901
2902/*
2903 * Close the buffer used for spell info.
2904 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002905 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002906close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002907{
2908 if (buf != NULL)
2909 {
2910 ml_close(buf, TRUE);
2911 vim_free(buf);
2912 }
2913}
2914
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002915/*
2916 * Init the chartab used for spelling for ASCII.
2917 * EBCDIC is not supported!
2918 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002919 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002920clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002921{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002922 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002923
2924 /* Init everything to FALSE. */
2925 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2926 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2927 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002928 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002929 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002930 sp->st_upper[i] = i;
2931 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002932
2933 /* We include digits. A word shouldn't start with a digit, but handling
2934 * that is done separately. */
2935 for (i = '0'; i <= '9'; ++i)
2936 sp->st_isw[i] = TRUE;
2937 for (i = 'A'; i <= 'Z'; ++i)
2938 {
2939 sp->st_isw[i] = TRUE;
2940 sp->st_isu[i] = TRUE;
2941 sp->st_fold[i] = i + 0x20;
2942 }
2943 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002944 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002945 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002946 sp->st_upper[i] = i - 0x20;
2947 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002948}
2949
2950/*
2951 * Init the chartab used for spelling. Only depends on 'encoding'.
2952 * Called once while starting up and when 'encoding' changes.
2953 * The default is to use isalpha(), but the spell file should define the word
2954 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002955 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002956 */
2957 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002958init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002959{
2960 int i;
2961
2962 did_set_spelltab = FALSE;
2963 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002964 if (enc_dbcs)
2965 {
2966 /* DBCS: assume double-wide characters are word characters. */
2967 for (i = 128; i <= 255; ++i)
2968 if (MB_BYTE2LEN(i) == 2)
2969 spelltab.st_isw[i] = TRUE;
2970 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002971 else if (enc_utf8)
2972 {
2973 for (i = 128; i < 256; ++i)
2974 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002975 int f = utf_fold(i);
2976 int u = utf_toupper(i);
2977
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002978 spelltab.st_isu[i] = utf_isupper(i);
2979 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002980 /* The folded/upper-cased value is different between latin1 and
2981 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2982 * value for utf-8 to avoid this. */
2983 spelltab.st_fold[i] = (f < 256) ? f : i;
2984 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002985 }
2986 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002987 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002988 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002989 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002990 for (i = 128; i < 256; ++i)
2991 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002992 if (MB_ISUPPER(i))
2993 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002994 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002995 spelltab.st_isu[i] = TRUE;
2996 spelltab.st_fold[i] = MB_TOLOWER(i);
2997 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002998 else if (MB_ISLOWER(i))
2999 {
3000 spelltab.st_isw[i] = TRUE;
3001 spelltab.st_upper[i] = MB_TOUPPER(i);
3002 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003003 }
3004 }
3005}
3006
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003007
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003008/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003009 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003010 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003011 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003012 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003013 */
3014 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003015spell_iswordp(
3016 char_u *p,
3017 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003018{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003019 char_u *s;
3020 int l;
3021 int c;
3022
3023 if (has_mbyte)
3024 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003025 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003026 s = p;
3027 if (l == 1)
3028 {
3029 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003030 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003031 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003032 }
3033 else
3034 {
3035 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003036 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3037 : (wp->w_s->b_spell_ismw_mb != NULL
3038 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003039 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003040 }
3041
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003042 c = mb_ptr2char(s);
3043 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003044 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003045 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003046 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003047
Bram Moolenaar860cae12010-06-05 23:22:07 +02003048 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003049}
3050
3051/*
3052 * Return TRUE if "p" points to a word character.
3053 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3054 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003055 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003056spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003057{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003058 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003059
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003060 if (has_mbyte)
3061 {
3062 c = mb_ptr2char(p);
3063 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003064 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003065 return spelltab.st_isw[c];
3066 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003067 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003068}
3069
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003070/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003071 * Return TRUE if word class indicates a word character.
3072 * Only for characters above 255.
3073 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003074 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003075 */
3076 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003078{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003079 if (wp->w_s->b_cjk)
3080 /* East Asian characters are not considered word characters. */
3081 return cl == 2 || cl == 0x2800;
Bram Moolenaar06e63772019-07-19 23:04:34 +02003082 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003083}
3084
3085/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003086 * Return TRUE if "p" points to a word character.
3087 * Wide version of spell_iswordp().
3088 */
3089 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003090spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003091{
3092 int *s;
3093
Bram Moolenaar860cae12010-06-05 23:22:07 +02003094 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3095 : (wp->w_s->b_spell_ismw_mb != NULL
3096 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003097 s = p + 1;
3098 else
3099 s = p;
3100
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003101 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003102 {
3103 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003104 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003105 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003106 return spell_mb_isword_class(
3107 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003108 return 0;
3109 }
3110 return spelltab.st_isw[*s];
3111}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003112
Bram Moolenaarea408852005-06-25 22:49:46 +00003113/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003114 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3115 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003116 * When using a multi-byte 'encoding' the length may change!
3117 * Returns FAIL when something wrong.
3118 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003119 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003120spell_casefold(
3121 char_u *str,
3122 int len,
3123 char_u *buf,
3124 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003125{
3126 int i;
3127
3128 if (len >= buflen)
3129 {
3130 buf[0] = NUL;
3131 return FAIL; /* result will not fit */
3132 }
3133
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003134 if (has_mbyte)
3135 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003136 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003137 char_u *p;
3138 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003139
3140 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003141 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003142 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003143 if (outi + MB_MAXBYTES > buflen)
3144 {
3145 buf[outi] = NUL;
3146 return FAIL;
3147 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003148 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003149 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003150 }
3151 buf[outi] = NUL;
3152 }
3153 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003154 {
3155 /* Be quick for non-multibyte encodings. */
3156 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003157 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003158 buf[i] = NUL;
3159 }
3160
3161 return OK;
3162}
3163
Bram Moolenaar4770d092006-01-12 23:22:24 +00003164/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003165#define SPS_BEST 1
3166#define SPS_FAST 2
3167#define SPS_DOUBLE 4
3168
Bram Moolenaar4770d092006-01-12 23:22:24 +00003169static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3170static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003171
3172/*
3173 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003174 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003175 */
3176 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003177spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003178{
3179 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003180 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003181 char_u buf[MAXPATHL];
3182 int f;
3183
3184 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003185 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003186
3187 for (p = p_sps; *p != NUL; )
3188 {
3189 copy_option_part(&p, buf, MAXPATHL, ",");
3190
3191 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003192 if (VIM_ISDIGIT(*buf))
3193 {
3194 s = buf;
3195 sps_limit = getdigits(&s);
3196 if (*s != NUL && !VIM_ISDIGIT(*s))
3197 f = -1;
3198 }
3199 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003200 f = SPS_BEST;
3201 else if (STRCMP(buf, "fast") == 0)
3202 f = SPS_FAST;
3203 else if (STRCMP(buf, "double") == 0)
3204 f = SPS_DOUBLE;
3205 else if (STRNCMP(buf, "expr:", 5) != 0
3206 && STRNCMP(buf, "file:", 5) != 0)
3207 f = -1;
3208
3209 if (f == -1 || (sps_flags != 0 && f != 0))
3210 {
3211 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003212 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003213 return FAIL;
3214 }
3215 if (f != 0)
3216 sps_flags = f;
3217 }
3218
3219 if (sps_flags == 0)
3220 sps_flags = SPS_BEST;
3221
3222 return OK;
3223}
3224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003225/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003226 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003227 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003228 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003229 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003230 */
3231 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003232spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003233{
3234 char_u *line;
3235 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003236 char_u wcopy[MAXWLEN + 2];
3237 char_u *p;
3238 int i;
3239 int c;
3240 suginfo_T sug;
3241 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003242 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003243 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003244 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003245 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003246 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003247 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003248
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003249 if (no_spell_checking(curwin))
3250 return;
3251
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003252 if (VIsual_active)
3253 {
3254 /* Use the Visually selected text as the bad word. But reject
3255 * a multi-line selection. */
3256 if (curwin->w_cursor.lnum != VIsual.lnum)
3257 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003258 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003259 return;
3260 }
3261 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3262 if (badlen < 0)
3263 badlen = -badlen;
3264 else
3265 curwin->w_cursor.col = VIsual.col;
3266 ++badlen;
3267 end_visual_mode();
3268 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003269 /* Find the start of the badly spelled word. */
3270 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003271 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003273 /* No bad word or it starts after the cursor: use the word under the
3274 * cursor. */
3275 curwin->w_cursor = prev_cursor;
3276 line = ml_get_curline();
3277 p = line + curwin->w_cursor.col;
3278 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003279 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003280 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003281 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003282 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003283 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003284
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003285 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003286 {
3287 beep_flush();
3288 return;
3289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003290 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003291 }
3292
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003293 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003294
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003295 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003296 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003297
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003298 /* Make a copy of current line since autocommands may free the line. */
3299 line = vim_strsave(ml_get_curline());
3300 if (line == NULL)
3301 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003302
Bram Moolenaar5195e452005-08-19 20:32:47 +00003303 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3304 * 'spellsuggest', whatever is smaller. */
3305 if (sps_limit > (int)Rows - 2)
3306 limit = (int)Rows - 2;
3307 else
3308 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003309 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003310 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311
3312 if (sug.su_ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003313 msg(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003314 else if (count > 0)
3315 {
3316 if (count > sug.su_ga.ga_len)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003317 smsg(_("Sorry, only %ld suggestions"),
Bram Moolenaard12a1322005-08-21 22:08:24 +00003318 (long)sug.su_ga.ga_len);
3319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003320 else
3321 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003322 VIM_CLEAR(repl_from);
3323 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003324
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003325#ifdef FEAT_RIGHTLEFT
3326 /* When 'rightleft' is set the list is drawn right-left. */
3327 cmdmsg_rl = curwin->w_p_rl;
3328 if (cmdmsg_rl)
3329 msg_col = Columns - 1;
3330#endif
3331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 /* List the suggestions. */
3333 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003334 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003335 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3337 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003338#ifdef FEAT_RIGHTLEFT
3339 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3340 {
3341 /* And now the rabbit from the high hat: Avoid showing the
3342 * untranslated message rightleft. */
3343 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3344 sug.su_badlen, sug.su_badptr);
3345 }
3346#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003347 msg_puts((char *)IObuff);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003348 msg_clr_eos();
3349 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 msg_scroll = TRUE;
3352 for (i = 0; i < sug.su_ga.ga_len; ++i)
3353 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003354 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355
3356 /* The suggested word may replace only part of the bad word, add
3357 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003358 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003359 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003360 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003361 sug.su_badptr + stp->st_orglen,
3362 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003363 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3364#ifdef FEAT_RIGHTLEFT
3365 if (cmdmsg_rl)
3366 rl_mirror(IObuff);
3367#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003368 msg_puts((char *)IObuff);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003369
3370 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003371 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003372
3373 /* The word may replace more than "su_badlen". */
3374 if (sug.su_badlen < stp->st_orglen)
3375 {
3376 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3377 stp->st_orglen, sug.su_badptr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003378 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003379 }
3380
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003381 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003382 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003383 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003384 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003385 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003386 stp->st_salscore ? "s " : "",
3387 stp->st_score, stp->st_altscore);
3388 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003389 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003390 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003391#ifdef FEAT_RIGHTLEFT
3392 if (cmdmsg_rl)
3393 /* Mirror the numbers, but keep the leading space. */
3394 rl_mirror(IObuff + 1);
3395#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003396 msg_advance(30);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003397 msg_puts((char *)IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003398 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003399 msg_putchar('\n');
3400 }
3401
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003402#ifdef FEAT_RIGHTLEFT
3403 cmdmsg_rl = FALSE;
3404 msg_col = 0;
3405#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003407 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003408 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003409 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003410 lines_left = Rows; /* avoid more prompt */
3411 /* don't delay for 'smd' in normal_cmd() */
3412 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 }
3414
Bram Moolenaard12a1322005-08-21 22:08:24 +00003415 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3416 {
3417 /* Save the from and to text for :spellrepall. */
3418 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003419 if (sug.su_badlen > stp->st_orglen)
3420 {
3421 /* Replacing less than "su_badlen", append the remainder to
3422 * repl_to. */
3423 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3424 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3425 sug.su_badlen - stp->st_orglen,
3426 sug.su_badptr + stp->st_orglen);
3427 repl_to = vim_strsave(IObuff);
3428 }
3429 else
3430 {
3431 /* Replacing su_badlen or more, use the whole word. */
3432 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3433 repl_to = vim_strsave(stp->st_word);
3434 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003435
3436 /* Replace the word. */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003437 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003438 if (p != NULL)
3439 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003440 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003441 mch_memmove(p, line, c);
3442 STRCPY(p + c, stp->st_word);
3443 STRCAT(p, sug.su_badptr + stp->st_orglen);
3444 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3445 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003446
3447 /* For redo we use a change-word command. */
3448 ResetRedobuff();
3449 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003450 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003451 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003452 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003453
3454 /* After this "p" may be invalid. */
3455 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003456 }
3457 }
3458 else
3459 curwin->w_cursor = prev_cursor;
3460
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003461 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003462skip:
3463 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003464}
3465
3466/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003467 * Check if the word at line "lnum" column "col" is required to start with a
3468 * capital. This uses 'spellcapcheck' of the current buffer.
3469 */
3470 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003471check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003472{
3473 int need_cap = FALSE;
3474 char_u *line;
3475 char_u *line_copy = NULL;
3476 char_u *p;
3477 colnr_T endcol;
3478 regmatch_T regmatch;
3479
Bram Moolenaar860cae12010-06-05 23:22:07 +02003480 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003481 return FALSE;
3482
3483 line = ml_get_curline();
3484 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003485 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003486 {
3487 /* At start of line, check if previous line is empty or sentence
3488 * ends there. */
3489 if (lnum == 1)
3490 need_cap = TRUE;
3491 else
3492 {
3493 line = ml_get(lnum - 1);
3494 if (*skipwhite(line) == NUL)
3495 need_cap = TRUE;
3496 else
3497 {
3498 /* Append a space in place of the line break. */
3499 line_copy = concat_str(line, (char_u *)" ");
3500 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003501 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003502 }
3503 }
3504 }
3505 else
3506 endcol = col;
3507
3508 if (endcol > 0)
3509 {
3510 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003511 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003512 regmatch.rm_ic = FALSE;
3513 p = line + endcol;
3514 for (;;)
3515 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003516 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003517 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003518 break;
3519 if (vim_regexec(&regmatch, p, 0)
3520 && regmatch.endp[0] == line + endcol)
3521 {
3522 need_cap = TRUE;
3523 break;
3524 }
3525 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003526 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003527 }
3528
3529 vim_free(line_copy);
3530
3531 return need_cap;
3532}
3533
3534
3535/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003536 * ":spellrepall"
3537 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003539ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003540{
3541 pos_T pos = curwin->w_cursor;
3542 char_u *frompat;
3543 int addlen;
3544 char_u *line;
3545 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003546 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003547 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003548
3549 if (repl_from == NULL || repl_to == NULL)
3550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003551 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003552 return;
3553 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003554 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003555
Bram Moolenaar964b3742019-05-24 18:54:09 +02003556 frompat = alloc(STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003557 if (frompat == NULL)
3558 return;
3559 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3560 p_ws = FALSE;
3561
Bram Moolenaar5195e452005-08-19 20:32:47 +00003562 sub_nsubs = 0;
3563 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003564 curwin->w_cursor.lnum = 0;
3565 while (!got_int)
3566 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003567 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003568 || u_save_cursor() == FAIL)
3569 break;
3570
3571 /* Only replace when the right word isn't there yet. This happens
3572 * when changing "etc" to "etc.". */
3573 line = ml_get_curline();
3574 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3575 repl_to, STRLEN(repl_to)) != 0)
3576 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02003577 p = alloc(STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003578 if (p == NULL)
3579 break;
3580 mch_memmove(p, line, curwin->w_cursor.col);
3581 STRCPY(p + curwin->w_cursor.col, repl_to);
3582 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3583 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3584 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003585
3586 if (curwin->w_cursor.lnum != prev_lnum)
3587 {
3588 ++sub_nlines;
3589 prev_lnum = curwin->w_cursor.lnum;
3590 }
3591 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003592 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003593 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003594 }
3595
3596 p_ws = save_ws;
3597 curwin->w_cursor = pos;
3598 vim_free(frompat);
3599
Bram Moolenaar5195e452005-08-19 20:32:47 +00003600 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003601 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003602 else
3603 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003604}
3605
3606/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003607 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3608 * a list of allocated strings.
3609 */
3610 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003611spell_suggest_list(
3612 garray_T *gap,
3613 char_u *word,
3614 int maxcount, /* maximum nr of suggestions */
3615 int need_cap, /* 'spellcapcheck' matched */
3616 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003617{
3618 suginfo_T sug;
3619 int i;
3620 suggest_T *stp;
3621 char_u *wcopy;
3622
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003623 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003624
3625 /* Make room in "gap". */
3626 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003627 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003628 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003629 for (i = 0; i < sug.su_ga.ga_len; ++i)
3630 {
3631 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003632
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003633 /* The suggested word may replace only part of "word", add the not
3634 * replaced part. */
3635 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003636 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003637 if (wcopy == NULL)
3638 break;
3639 STRCPY(wcopy, stp->st_word);
3640 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3641 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3642 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003643 }
3644
3645 spell_find_cleanup(&sug);
3646}
3647
3648/*
3649 * Find spell suggestions for the word at the start of "badptr".
3650 * Return the suggestions in "su->su_ga".
3651 * The maximum number of suggestions is "maxcount".
3652 * Note: does use info for the current window.
3653 * This is based on the mechanisms of Aspell, but completely reimplemented.
3654 */
3655 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003656spell_find_suggest(
3657 char_u *badptr,
3658 int badlen, /* length of bad word or 0 if unknown */
3659 suginfo_T *su,
3660 int maxcount,
3661 int banbadword, /* don't include badword in suggestions */
3662 int need_cap, /* word should start with capital */
3663 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003664{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003665 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003666 char_u buf[MAXPATHL];
3667 char_u *p;
3668 int do_combine = FALSE;
3669 char_u *sps_copy;
3670#ifdef FEAT_EVAL
3671 static int expr_busy = FALSE;
3672#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003673 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003674 int i;
3675 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003676
3677 /*
3678 * Set the info in "*su".
3679 */
3680 vim_memset(su, 0, sizeof(suginfo_T));
3681 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3682 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003683 if (*badptr == NUL)
3684 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003685 hash_init(&su->su_banned);
3686
3687 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003688 if (badlen != 0)
3689 su->su_badlen = badlen;
3690 else
3691 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003692 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003693 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003694
3695 if (su->su_badlen >= MAXWLEN)
3696 su->su_badlen = MAXWLEN - 1; /* just in case */
3697 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3698 (void)spell_casefold(su->su_badptr, su->su_badlen,
3699 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003700 /* TODO: make this work if the case-folded text is longer than the original
3701 * text. Currently an illegal byte causes wrong pointer computations. */
3702 su->su_fbadword[su->su_badlen] = NUL;
3703
Bram Moolenaar0c405862005-06-22 22:26:26 +00003704 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003705 su->su_badflags = badword_captype(su->su_badptr,
3706 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003707 if (need_cap)
3708 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003709
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003710 /* Find the default language for sound folding. We simply use the first
3711 * one in 'spelllang' that supports sound folding. That's good for when
3712 * using multiple files for one language, it's not that bad when mixing
3713 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003714 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003715 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003716 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003717 if (lp->lp_sallang != NULL)
3718 {
3719 su->su_sallang = lp->lp_sallang;
3720 break;
3721 }
3722 }
3723
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003724 /* Soundfold the bad word with the default sound folding, so that we don't
3725 * have to do this many times. */
3726 if (su->su_sallang != NULL)
3727 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3728 su->su_sal_badword);
3729
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003730 /* If the word is not capitalised and spell_check() doesn't consider the
3731 * word to be bad then it might need to be capitalised. Add a suggestion
3732 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003733 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003734 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003735 {
3736 make_case_word(su->su_badword, buf, WF_ONECAP);
3737 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003738 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003739 }
3740
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003741 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003742 if (banbadword)
3743 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003744
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003745 /* Make a copy of 'spellsuggest', because the expression may change it. */
3746 sps_copy = vim_strsave(p_sps);
3747 if (sps_copy == NULL)
3748 return;
3749
3750 /* Loop over the items in 'spellsuggest'. */
3751 for (p = sps_copy; *p != NUL; )
3752 {
3753 copy_option_part(&p, buf, MAXPATHL, ",");
3754
3755 if (STRNCMP(buf, "expr:", 5) == 0)
3756 {
3757#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003758 /* Evaluate an expression. Skip this when called recursively,
3759 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003760 if (!expr_busy)
3761 {
3762 expr_busy = TRUE;
3763 spell_suggest_expr(su, buf + 5);
3764 expr_busy = FALSE;
3765 }
3766#endif
3767 }
3768 else if (STRNCMP(buf, "file:", 5) == 0)
3769 /* Use list of suggestions in a file. */
3770 spell_suggest_file(su, buf + 5);
3771 else
3772 {
3773 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003774 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003775 if (sps_flags & SPS_DOUBLE)
3776 do_combine = TRUE;
3777 }
3778 }
3779
3780 vim_free(sps_copy);
3781
3782 if (do_combine)
3783 /* Combine the two list of suggestions. This must be done last,
3784 * because sorting changes the order again. */
3785 score_combine(su);
3786}
3787
3788#ifdef FEAT_EVAL
3789/*
3790 * Find suggestions by evaluating expression "expr".
3791 */
3792 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003793spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003794{
3795 list_T *list;
3796 listitem_T *li;
3797 int score;
3798 char_u *p;
3799
3800 /* The work is split up in a few parts to avoid having to export
3801 * suginfo_T.
3802 * First evaluate the expression and get the resulting list. */
3803 list = eval_spell_expr(su->su_badword, expr);
3804 if (list != NULL)
3805 {
3806 /* Loop over the items in the list. */
3807 for (li = list->lv_first; li != NULL; li = li->li_next)
3808 if (li->li_tv.v_type == VAR_LIST)
3809 {
3810 /* Get the word and the score from the items. */
3811 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003812 if (score >= 0 && score <= su->su_maxscore)
3813 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3814 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003815 }
3816 list_unref(list);
3817 }
3818
Bram Moolenaar4770d092006-01-12 23:22:24 +00003819 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3820 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003821 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3822}
3823#endif
3824
3825/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003826 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003827 */
3828 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003829spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003830{
3831 FILE *fd;
3832 char_u line[MAXWLEN * 2];
3833 char_u *p;
3834 int len;
3835 char_u cword[MAXWLEN];
3836
3837 /* Open the file. */
3838 fd = mch_fopen((char *)fname, "r");
3839 if (fd == NULL)
3840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003841 semsg(_(e_notopen), fname);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003842 return;
3843 }
3844
3845 /* Read it line by line. */
3846 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3847 {
3848 line_breakcheck();
3849
3850 p = vim_strchr(line, '/');
3851 if (p == NULL)
3852 continue; /* No Tab found, just skip the line. */
3853 *p++ = NUL;
3854 if (STRICMP(su->su_badword, line) == 0)
3855 {
3856 /* Match! Isolate the good word, until CR or NL. */
3857 for (len = 0; p[len] >= ' '; ++len)
3858 ;
3859 p[len] = NUL;
3860
3861 /* If the suggestion doesn't have specific case duplicate the case
3862 * of the bad word. */
3863 if (captype(p, NULL) == 0)
3864 {
3865 make_case_word(p, cword, su->su_badflags);
3866 p = cword;
3867 }
3868
3869 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003870 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003871 }
3872 }
3873
3874 fclose(fd);
3875
Bram Moolenaar4770d092006-01-12 23:22:24 +00003876 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3877 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003878 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3879}
3880
3881/*
3882 * Find suggestions for the internal method indicated by "sps_flags".
3883 */
3884 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003885spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003886{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003887 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003888 * Load the .sug file(s) that are available and not done yet.
3889 */
3890 suggest_load_files();
3891
3892 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003893 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003894 *
3895 * Set a maximum score to limit the combination of operations that is
3896 * tried.
3897 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003898 suggest_try_special(su);
3899
3900 /*
3901 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3902 * from the .aff file and inserting a space (split the word).
3903 */
3904 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003905
3906 /* For the resulting top-scorers compute the sound-a-like score. */
3907 if (sps_flags & SPS_DOUBLE)
3908 score_comp_sal(su);
3909
3910 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003911 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003912 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003913 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003914 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00003915 if (sps_flags & SPS_BEST)
3916 /* Adjust the word score for the suggestions found so far for how
3917 * they sounds like. */
3918 rescore_suggestions(su);
3919
3920 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003921 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00003922 * for the soundfold word, limits the changes that are being tried,
3923 * and "su_sfmaxscore" the rescored score, which is set by
3924 * cleanup_suggestions().
3925 * First find words with a small edit distance, because this is much
3926 * faster and often already finds the top-N suggestions. If we didn't
3927 * find many suggestions try again with a higher edit distance.
3928 * "sl_sounddone" is used to avoid doing the same word twice.
3929 */
3930 suggest_try_soundalike_prep();
3931 su->su_maxscore = SCORE_SFMAX1;
3932 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003933 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003934 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3935 {
3936 /* We didn't find enough matches, try again, allowing more
3937 * changes to the soundfold word. */
3938 su->su_maxscore = SCORE_SFMAX2;
3939 suggest_try_soundalike(su);
3940 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3941 {
3942 /* Still didn't find enough matches, try again, allowing even
3943 * more changes to the soundfold word. */
3944 su->su_maxscore = SCORE_SFMAX3;
3945 suggest_try_soundalike(su);
3946 }
3947 }
3948 su->su_maxscore = su->su_sfmaxscore;
3949 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003950 }
3951
Bram Moolenaar4770d092006-01-12 23:22:24 +00003952 /* When CTRL-C was hit while searching do show the results. Only clear
3953 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003954 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00003955 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003956 {
3957 (void)vgetc();
3958 got_int = FALSE;
3959 }
3960
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003961 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003962 {
3963 if (sps_flags & SPS_BEST)
3964 /* Adjust the word score for how it sounds like. */
3965 rescore_suggestions(su);
3966
Bram Moolenaar4770d092006-01-12 23:22:24 +00003967 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3968 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003969 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003970 }
3971}
3972
3973/*
3974 * Free the info put in "*su" by spell_find_suggest().
3975 */
3976 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003977spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003978{
3979 int i;
3980
3981 /* Free the suggestions. */
3982 for (i = 0; i < su->su_ga.ga_len; ++i)
3983 vim_free(SUG(su->su_ga, i).st_word);
3984 ga_clear(&su->su_ga);
3985 for (i = 0; i < su->su_sga.ga_len; ++i)
3986 vim_free(SUG(su->su_sga, i).st_word);
3987 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003988
3989 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003990 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003991}
3992
3993/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003994 * Make a copy of "word", with the first letter upper or lower cased, to
3995 * "wcopy[MAXWLEN]". "word" must not be empty.
3996 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003997 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003998 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003999onecap_copy(
4000 char_u *word,
4001 char_u *wcopy,
4002 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004003{
4004 char_u *p;
4005 int c;
4006 int l;
4007
4008 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004010 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 c = *p++;
4013 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004014 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004015 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004016 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004017 if (has_mbyte)
4018 l = mb_char2bytes(c, wcopy);
4019 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 {
4021 l = 1;
4022 wcopy[0] = c;
4023 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004024 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025}
4026
4027/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004028 * Make a copy of "word" with all the letters upper cased into
4029 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004030 */
4031 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004032allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004033{
4034 char_u *s;
4035 char_u *d;
4036 int c;
4037
4038 d = wcopy;
4039 for (s = word; *s != NUL; )
4040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004041 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004042 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004045
Bram Moolenaard3184b52011-09-02 14:18:20 +02004046 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004047 * would cause weird errors in other 8-bit encodings. */
4048 if (enc_latin1like && c == 0xdf)
4049 {
4050 c = 'S';
4051 if (d - wcopy >= MAXWLEN - 1)
4052 break;
4053 *d++ = c;
4054 }
4055 else
Bram Moolenaar78622822005-08-23 21:00:13 +00004056 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004057
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004058 if (has_mbyte)
4059 {
4060 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4061 break;
4062 d += mb_char2bytes(c, d);
4063 }
4064 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004065 {
4066 if (d - wcopy >= MAXWLEN - 1)
4067 break;
4068 *d++ = c;
4069 }
4070 }
4071 *d = NUL;
4072}
4073
4074/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004075 * Try finding suggestions by recognizing specific situations.
4076 */
4077 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004078suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004079{
4080 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004081 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004082 int c;
4083 char_u word[MAXWLEN];
4084
4085 /*
4086 * Recognize a word that is repeated: "the the".
4087 */
4088 p = skiptowhite(su->su_fbadword);
4089 len = p - su->su_fbadword;
4090 p = skipwhite(p);
4091 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4092 {
4093 /* Include badflags: if the badword is onecap or allcap
4094 * use that for the goodword too: "The the" -> "The". */
4095 c = su->su_fbadword[len];
4096 su->su_fbadword[len] = NUL;
4097 make_case_word(su->su_fbadword, word, su->su_badflags);
4098 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004099
4100 /* Give a soundalike score of 0, compute the score as if deleting one
4101 * character. */
4102 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004103 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004104 }
4105}
4106
4107/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004108 * Change the 0 to 1 to measure how much time is spent in each state.
4109 * Output is dumped in "suggestprof".
4110 */
4111#if 0
4112# define SUGGEST_PROFILE
4113proftime_T current;
4114proftime_T total;
4115proftime_T times[STATE_FINAL + 1];
4116long counts[STATE_FINAL + 1];
4117
4118 static void
4119prof_init(void)
4120{
4121 for (int i = 0; i <= STATE_FINAL; ++i)
4122 {
4123 profile_zero(&times[i]);
4124 counts[i] = 0;
4125 }
4126 profile_start(&current);
4127 profile_start(&total);
4128}
4129
4130/* call before changing state */
4131 static void
4132prof_store(state_T state)
4133{
4134 profile_end(&current);
4135 profile_add(&times[state], &current);
4136 ++counts[state];
4137 profile_start(&current);
4138}
4139# define PROF_STORE(state) prof_store(state);
4140
4141 static void
4142prof_report(char *name)
4143{
4144 FILE *fd = fopen("suggestprof", "a");
4145
4146 profile_end(&total);
4147 fprintf(fd, "-----------------------\n");
4148 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4149 for (int i = 0; i <= STATE_FINAL; ++i)
4150 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4151 fclose(fd);
4152}
4153#else
4154# define PROF_STORE(state)
4155#endif
4156
4157/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004158 * Try finding suggestions by adding/removing/swapping letters.
4159 */
4160 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004161suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162{
4163 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004164 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004166 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004167 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168
4169 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004170 * to find matches (esp. REP items). Append some more text, changing
4171 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004173 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004174 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004175 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004176
Bram Moolenaar860cae12010-06-05 23:22:07 +02004177 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004178 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004179 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004180
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004181 /* If reloading a spell file fails it's still in the list but
4182 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004183 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004184 continue;
4185
Bram Moolenaar4770d092006-01-12 23:22:24 +00004186 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004187#ifdef SUGGEST_PROFILE
4188 prof_init();
4189#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004190 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004191#ifdef SUGGEST_PROFILE
4192 prof_report("try_change");
4193#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004194 }
4195}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196
Bram Moolenaar4770d092006-01-12 23:22:24 +00004197/* Check the maximum score, if we go over it we won't try this change. */
4198#define TRY_DEEPER(su, stack, depth, add) \
4199 (stack[depth].ts_score + (add) < su->su_maxscore)
4200
4201/*
4202 * Try finding suggestions by adding/removing/swapping letters.
4203 *
4204 * This uses a state machine. At each node in the tree we try various
4205 * operations. When trying if an operation works "depth" is increased and the
4206 * stack[] is used to store info. This allows combinations, thus insert one
4207 * character, replace one and delete another. The number of changes is
4208 * limited by su->su_maxscore.
4209 *
4210 * After implementing this I noticed an article by Kemal Oflazer that
4211 * describes something similar: "Error-tolerant Finite State Recognition with
4212 * Applications to Morphological Analysis and Spelling Correction" (1996).
4213 * The implementation in the article is simplified and requires a stack of
4214 * unknown depth. The implementation here only needs a stack depth equal to
4215 * the length of the word.
4216 *
4217 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4218 * The mechanism is the same, but we find a match with a sound-folded word
4219 * that comes from one or more original words. Each of these words may be
4220 * added, this is done by add_sound_suggest().
4221 * Don't use:
4222 * the prefix tree or the keep-case tree
4223 * "su->su_badlen"
4224 * anything to do with upper and lower case
4225 * anything to do with word or non-word characters ("spell_iswordp()")
4226 * banned words
4227 * word flags (rare, region, compounding)
4228 * word splitting for now
4229 * "similar_chars()"
4230 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4231 */
4232 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004233suggest_trie_walk(
4234 suginfo_T *su,
4235 langp_T *lp,
4236 char_u *fword,
4237 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004238{
4239 char_u tword[MAXWLEN]; /* good word collected so far */
4240 trystate_T stack[MAXWLEN];
4241 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004242 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004243 * words and split word. NUL terminated
4244 * when going deeper but not when coming
4245 * back. */
4246 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4247 trystate_T *sp;
4248 int newscore;
4249 int score;
4250 char_u *byts, *fbyts, *pbyts;
4251 idx_T *idxs, *fidxs, *pidxs;
4252 int depth;
4253 int c, c2, c3;
4254 int n = 0;
4255 int flags;
4256 garray_T *gap;
4257 idx_T arridx;
4258 int len;
4259 char_u *p;
4260 fromto_T *ftp;
4261 int fl = 0, tl;
4262 int repextra = 0; /* extra bytes in fword[] from REP item */
4263 slang_T *slang = lp->lp_slang;
4264 int fword_ends;
4265 int goodword_ends;
4266#ifdef DEBUG_TRIEWALK
4267 /* Stores the name of the change made at each level. */
4268 char_u changename[MAXWLEN][80];
4269#endif
4270 int breakcheckcount = 1000;
4271 int compound_ok;
4272
4273 /*
4274 * Go through the whole case-fold tree, try changes at each node.
4275 * "tword[]" contains the word collected from nodes in the tree.
4276 * "fword[]" the word we are trying to match with (initially the bad
4277 * word).
4278 */
4279 depth = 0;
4280 sp = &stack[0];
4281 vim_memset(sp, 0, sizeof(trystate_T));
4282 sp->ts_curi = 1;
4283
4284 if (soundfold)
4285 {
4286 /* Going through the soundfold tree. */
4287 byts = fbyts = slang->sl_sbyts;
4288 idxs = fidxs = slang->sl_sidxs;
4289 pbyts = NULL;
4290 pidxs = NULL;
4291 sp->ts_prefixdepth = PFD_NOPREFIX;
4292 sp->ts_state = STATE_START;
4293 }
4294 else
4295 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004296 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004297 * When there are postponed prefixes we need to use these first. At
4298 * the end of the prefix we continue in the case-fold tree.
4299 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004300 fbyts = slang->sl_fbyts;
4301 fidxs = slang->sl_fidxs;
4302 pbyts = slang->sl_pbyts;
4303 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004304 if (pbyts != NULL)
4305 {
4306 byts = pbyts;
4307 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004308 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004309 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4310 }
4311 else
4312 {
4313 byts = fbyts;
4314 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004315 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004316 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004317 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004318 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004319
Bram Moolenaar4770d092006-01-12 23:22:24 +00004320 /*
4321 * Loop to find all suggestions. At each round we either:
4322 * - For the current state try one operation, advance "ts_curi",
4323 * increase "depth".
4324 * - When a state is done go to the next, set "ts_state".
4325 * - When all states are tried decrease "depth".
4326 */
4327 while (depth >= 0 && !got_int)
4328 {
4329 sp = &stack[depth];
4330 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004332 case STATE_START:
4333 case STATE_NOPREFIX:
4334 /*
4335 * Start of node: Deal with NUL bytes, which means
4336 * tword[] may end here.
4337 */
4338 arridx = sp->ts_arridx; /* current node in the tree */
4339 len = byts[arridx]; /* bytes in this node */
4340 arridx += sp->ts_curi; /* index of current byte */
4341
4342 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004344 /* Skip over the NUL bytes, we use them later. */
4345 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4346 ;
4347 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348
Bram Moolenaar4770d092006-01-12 23:22:24 +00004349 /* Always past NUL bytes now. */
4350 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004351 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004352 sp->ts_state = STATE_ENDNUL;
4353 sp->ts_save_badflags = su->su_badflags;
4354
4355 /* At end of a prefix or at start of prefixtree: check for
4356 * following word. */
4357 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004358 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004359 /* Set su->su_badflags to the caps type at this position.
4360 * Use the caps type until here for the prefix itself. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004361 if (has_mbyte)
4362 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4363 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004364 n = sp->ts_fidx;
4365 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4366 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004367 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004368#ifdef DEBUG_TRIEWALK
4369 sprintf(changename[depth], "prefix");
4370#endif
4371 go_deeper(stack, depth, 0);
4372 ++depth;
4373 sp = &stack[depth];
4374 sp->ts_prefixdepth = depth - 1;
4375 byts = fbyts;
4376 idxs = fidxs;
4377 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004378
Bram Moolenaar4770d092006-01-12 23:22:24 +00004379 /* Move the prefix to preword[] with the right case
4380 * and make find_keepcap_word() works. */
4381 tword[sp->ts_twordlen] = NUL;
4382 make_case_word(tword + sp->ts_splitoff,
4383 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004384 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004385 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004386 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004387 break;
4388 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004389
Bram Moolenaar4770d092006-01-12 23:22:24 +00004390 if (sp->ts_curi > len || byts[arridx] != 0)
4391 {
4392 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004393 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004394 sp->ts_state = STATE_ENDNUL;
4395 sp->ts_save_badflags = su->su_badflags;
4396 break;
4397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398
Bram Moolenaar4770d092006-01-12 23:22:24 +00004399 /*
4400 * End of word in tree.
4401 */
4402 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403
Bram Moolenaar4770d092006-01-12 23:22:24 +00004404 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004405
4406 /* Skip words with the NOSUGGEST flag. */
4407 if (flags & WF_NOSUGGEST)
4408 break;
4409
Bram Moolenaar4770d092006-01-12 23:22:24 +00004410 fword_ends = (fword[sp->ts_fidx] == NUL
4411 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004412 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004413 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004414 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004415
Bram Moolenaar4770d092006-01-12 23:22:24 +00004416 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004417 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004418 {
4419 /* There was a prefix before the word. Check that the prefix
4420 * can be used with this word. */
4421 /* Count the length of the NULs in the prefix. If there are
4422 * none this must be the first try without a prefix. */
4423 n = stack[sp->ts_prefixdepth].ts_arridx;
4424 len = pbyts[n++];
4425 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4426 ;
4427 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004428 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004429 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004430 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004431 if (c == 0)
4432 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004433
Bram Moolenaar4770d092006-01-12 23:22:24 +00004434 /* Use the WF_RARE flag for a rare prefix. */
4435 if (c & WF_RAREPFX)
4436 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004437
Bram Moolenaar4770d092006-01-12 23:22:24 +00004438 /* Tricky: when checking for both prefix and compounding
4439 * we run into the prefix flag first.
4440 * Remember that it's OK, so that we accept the prefix
4441 * when arriving at a compound flag. */
4442 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004443 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004444 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004445
Bram Moolenaar4770d092006-01-12 23:22:24 +00004446 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4447 * appending another compound word below. */
4448 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004449 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004450 goodword_ends = FALSE;
4451 else
4452 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004453
Bram Moolenaar4770d092006-01-12 23:22:24 +00004454 p = NULL;
4455 compound_ok = TRUE;
4456 if (sp->ts_complen > sp->ts_compsplit)
4457 {
4458 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004459 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004460 /* There was a word before this word. When there was no
4461 * change in this word (it was correct) add the first word
4462 * as a suggestion. If this word was corrected too, we
4463 * need to check if a correct word follows. */
4464 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004465 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004466 && STRNCMP(fword + sp->ts_splitfidx,
4467 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004468 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004469 {
4470 preword[sp->ts_prewordlen] = NUL;
4471 newscore = score_wordcount_adj(slang, sp->ts_score,
4472 preword + sp->ts_prewordlen,
4473 sp->ts_prewordlen > 0);
4474 /* Add the suggestion if the score isn't too bad. */
4475 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004476 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004477 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004478 newscore, 0, FALSE,
4479 lp->lp_sallang, FALSE);
4480 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004481 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004482 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004483 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004484 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004485 /* There was a compound word before this word. If this
4486 * word does not support compounding then give up
4487 * (splitting is tried for the word without compound
4488 * flag). */
4489 if (((unsigned)flags >> 24) == 0
4490 || sp->ts_twordlen - sp->ts_splitoff
4491 < slang->sl_compminlen)
4492 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004493 /* For multi-byte chars check character length against
4494 * COMPOUNDMIN. */
4495 if (has_mbyte
4496 && slang->sl_compminlen > 0
4497 && mb_charlen(tword + sp->ts_splitoff)
4498 < slang->sl_compminlen)
4499 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004500
Bram Moolenaar4770d092006-01-12 23:22:24 +00004501 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4502 compflags[sp->ts_complen + 1] = NUL;
4503 vim_strncpy(preword + sp->ts_prewordlen,
4504 tword + sp->ts_splitoff,
4505 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004506
4507 /* Verify CHECKCOMPOUNDPATTERN rules. */
4508 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4509 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004510 compound_ok = FALSE;
4511
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004512 if (compound_ok)
4513 {
4514 p = preword;
4515 while (*skiptowhite(p) != NUL)
4516 p = skipwhite(skiptowhite(p));
4517 if (fword_ends && !can_compound(slang, p,
4518 compflags + sp->ts_compsplit))
4519 /* Compound is not allowed. But it may still be
4520 * possible if we add another (short) word. */
4521 compound_ok = FALSE;
4522 }
4523
Bram Moolenaar4770d092006-01-12 23:22:24 +00004524 /* Get pointer to last char of previous word. */
4525 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004526 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004527 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004528 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004529
Bram Moolenaar4770d092006-01-12 23:22:24 +00004530 /*
4531 * Form the word with proper case in preword.
4532 * If there is a word from a previous split, append.
4533 * For the soundfold tree don't change the case, simply append.
4534 */
4535 if (soundfold)
4536 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4537 else if (flags & WF_KEEPCAP)
4538 /* Must find the word in the keep-case tree. */
4539 find_keepcap_word(slang, tword + sp->ts_splitoff,
4540 preword + sp->ts_prewordlen);
4541 else
4542 {
4543 /* Include badflags: If the badword is onecap or allcap
4544 * use that for the goodword too. But if the badword is
4545 * allcap and it's only one char long use onecap. */
4546 c = su->su_badflags;
4547 if ((c & WF_ALLCAP)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004548 && su->su_badlen == (*mb_ptr2len)(su->su_badptr))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004549 c = WF_ONECAP;
4550 c |= flags;
4551
4552 /* When appending a compound word after a word character don't
4553 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004554 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004555 c &= ~WF_ONECAP;
4556 make_case_word(tword + sp->ts_splitoff,
4557 preword + sp->ts_prewordlen, c);
4558 }
4559
4560 if (!soundfold)
4561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004562 /* Don't use a banned word. It may appear again as a good
4563 * word, thus remember it. */
4564 if (flags & WF_BANNED)
4565 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004566 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 break;
4568 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004569 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004570 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4571 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004572 {
4573 if (slang->sl_compprog == NULL)
4574 break;
4575 /* the word so far was banned but we may try compounding */
4576 goodword_ends = FALSE;
4577 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004578 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004579
Bram Moolenaar4770d092006-01-12 23:22:24 +00004580 newscore = 0;
4581 if (!soundfold) /* soundfold words don't have flags */
4582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004583 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004584 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004585 newscore += SCORE_REGION;
4586 if (flags & WF_RARE)
4587 newscore += SCORE_RARE;
4588
Bram Moolenaar0c405862005-06-22 22:26:26 +00004589 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004590 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004591 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004593
Bram Moolenaar4770d092006-01-12 23:22:24 +00004594 /* TODO: how about splitting in the soundfold tree? */
4595 if (fword_ends
4596 && goodword_ends
4597 && sp->ts_fidx >= sp->ts_fidxtry
4598 && compound_ok)
4599 {
4600 /* The badword also ends: add suggestions. */
4601#ifdef DEBUG_TRIEWALK
4602 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004603 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004604 int j;
4605
4606 /* print the stack of changes that brought us here */
4607 smsg("------ %s -------", fword);
4608 for (j = 0; j < depth; ++j)
4609 smsg("%s", changename[j]);
4610 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004611#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004612 if (soundfold)
4613 {
4614 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004615 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004616 add_sound_suggest(su, preword, sp->ts_score, lp);
4617 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004618 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004619 {
4620 /* Give a penalty when changing non-word char to word
4621 * char, e.g., "thes," -> "these". */
4622 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004623 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004624 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004625 {
4626 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004627 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004628 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004629 newscore += SCORE_NONWORD;
4630 }
4631
Bram Moolenaar4770d092006-01-12 23:22:24 +00004632 /* Give a bonus to words seen before. */
4633 score = score_wordcount_adj(slang,
4634 sp->ts_score + newscore,
4635 preword + sp->ts_prewordlen,
4636 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004637
Bram Moolenaar4770d092006-01-12 23:22:24 +00004638 /* Add the suggestion if the score isn't too bad. */
4639 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004640 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004641 add_suggestion(su, &su->su_ga, preword,
4642 sp->ts_fidx - repextra,
4643 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004644
4645 if (su->su_badflags & WF_MIXCAP)
4646 {
4647 /* We really don't know if the word should be
4648 * upper or lower case, add both. */
4649 c = captype(preword, NULL);
4650 if (c == 0 || c == WF_ALLCAP)
4651 {
4652 make_case_word(tword + sp->ts_splitoff,
4653 preword + sp->ts_prewordlen,
4654 c == 0 ? WF_ALLCAP : 0);
4655
4656 add_suggestion(su, &su->su_ga, preword,
4657 sp->ts_fidx - repextra,
4658 score + SCORE_ICASE, 0, FALSE,
4659 lp->lp_sallang, FALSE);
4660 }
4661 }
4662 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004663 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004664 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004665
Bram Moolenaar4770d092006-01-12 23:22:24 +00004666 /*
4667 * Try word split and/or compounding.
4668 */
4669 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004670 /* Don't split halfway a character. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004671 && (!has_mbyte || sp->ts_tcharlen == 0))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004672 {
4673 int try_compound;
4674 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004675
Bram Moolenaar4770d092006-01-12 23:22:24 +00004676 /* If past the end of the bad word don't try a split.
4677 * Otherwise try changing the next word. E.g., find
4678 * suggestions for "the the" where the second "the" is
4679 * different. It's done like a split.
4680 * TODO: word split for soundfold words */
4681 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4682 && !soundfold;
4683
4684 /* Get here in several situations:
4685 * 1. The word in the tree ends:
4686 * If the word allows compounding try that. Otherwise try
4687 * a split by inserting a space. For both check that a
4688 * valid words starts at fword[sp->ts_fidx].
4689 * For NOBREAK do like compounding to be able to check if
4690 * the next word is valid.
4691 * 2. The badword does end, but it was due to a change (e.g.,
4692 * a swap). No need to split, but do check that the
4693 * following word is valid.
4694 * 3. The badword and the word in the tree end. It may still
4695 * be possible to compound another (short) word.
4696 */
4697 try_compound = FALSE;
4698 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004699 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004700 && slang->sl_compprog != NULL
4701 && ((unsigned)flags >> 24) != 0
4702 && sp->ts_twordlen - sp->ts_splitoff
4703 >= slang->sl_compminlen
Bram Moolenaar4770d092006-01-12 23:22:24 +00004704 && (!has_mbyte
4705 || slang->sl_compminlen == 0
4706 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004707 >= slang->sl_compminlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004708 && (slang->sl_compsylmax < MAXWLEN
4709 || sp->ts_complen + 1 - sp->ts_compsplit
4710 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004711 && (can_be_compound(sp, slang,
4712 compflags, ((unsigned)flags >> 24))))
4713
Bram Moolenaar4770d092006-01-12 23:22:24 +00004714 {
4715 try_compound = TRUE;
4716 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4717 compflags[sp->ts_complen + 1] = NUL;
4718 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004719
Bram Moolenaar4770d092006-01-12 23:22:24 +00004720 /* For NOBREAK we never try splitting, it won't make any word
4721 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004722 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004723 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004724
Bram Moolenaar4770d092006-01-12 23:22:24 +00004725 /* If we could add a compound word, and it's also possible to
4726 * split at this point, do the split first and set
4727 * TSF_DIDSPLIT to avoid doing it again. */
4728 else if (!fword_ends
4729 && try_compound
4730 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4731 {
4732 try_compound = FALSE;
4733 sp->ts_flags |= TSF_DIDSPLIT;
4734 --sp->ts_curi; /* do the same NUL again */
4735 compflags[sp->ts_complen] = NUL;
4736 }
4737 else
4738 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004739
Bram Moolenaar4770d092006-01-12 23:22:24 +00004740 if (try_split || try_compound)
4741 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004742 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004743 {
4744 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004745 * words so far are valid for compounding. If there
4746 * is only one word it must not have the NEEDCOMPOUND
4747 * flag. */
4748 if (sp->ts_complen == sp->ts_compsplit
4749 && (flags & WF_NEEDCOMP))
4750 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004751 p = preword;
4752 while (*skiptowhite(p) != NUL)
4753 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004754 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004755 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004756 compflags + sp->ts_compsplit))
4757 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004758
4759 if (slang->sl_nosplitsugs)
4760 newscore += SCORE_SPLIT_NO;
4761 else
4762 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004763
4764 /* Give a bonus to words seen before. */
4765 newscore = score_wordcount_adj(slang, newscore,
4766 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004767 }
4768
Bram Moolenaar4770d092006-01-12 23:22:24 +00004769 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004771 go_deeper(stack, depth, newscore);
4772#ifdef DEBUG_TRIEWALK
4773 if (!try_compound && !fword_ends)
4774 sprintf(changename[depth], "%.*s-%s: split",
4775 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4776 else
4777 sprintf(changename[depth], "%.*s-%s: compound",
4778 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4779#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004781 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004782 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004783 sp->ts_state = STATE_SPLITUNDO;
4784
4785 ++depth;
4786 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004787
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004788 /* Append a space to preword when splitting. */
4789 if (!try_compound && !fword_ends)
4790 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004791 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004792 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004793 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004794
4795 /* If the badword has a non-word character at this
4796 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004797 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004798 * character when the word ends. But only when the
4799 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004800 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004801 + sp->ts_fidx,
4802 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004803 || fword_ends)
4804 && fword[sp->ts_fidx] != NUL
4805 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004806 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004807 int l;
4808
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004809 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004810 if (fword_ends)
4811 {
4812 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004813 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004814 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004815 sp->ts_prewordlen += l;
4816 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004817 }
4818 else
4819 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4820 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004821 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004822
Bram Moolenaard12a1322005-08-21 22:08:24 +00004823 /* When compounding include compound flag in
4824 * compflags[] (already set above). When splitting we
4825 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004826 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004827 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004828 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004829 sp->ts_compsplit = sp->ts_complen;
4830 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004831
Bram Moolenaar53805d12005-08-01 07:08:33 +00004832 /* set su->su_badflags to the caps type at this
4833 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004834 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004835 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004836 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004837 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004838 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004839 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004840
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004842 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004843
4844 /* If there are postponed prefixes, try these too. */
4845 if (pbyts != NULL)
4846 {
4847 byts = pbyts;
4848 idxs = pidxs;
4849 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004850 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004851 sp->ts_state = STATE_NOPREFIX;
4852 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004853 }
4854 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004855 }
4856 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857
Bram Moolenaar4770d092006-01-12 23:22:24 +00004858 case STATE_SPLITUNDO:
4859 /* Undo the changes done for word split or compound word. */
4860 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004861
Bram Moolenaar4770d092006-01-12 23:22:24 +00004862 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004863 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004864 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004865
Bram Moolenaar4770d092006-01-12 23:22:24 +00004866 /* In case we went into the prefix tree. */
4867 byts = fbyts;
4868 idxs = fidxs;
4869 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870
Bram Moolenaar4770d092006-01-12 23:22:24 +00004871 case STATE_ENDNUL:
4872 /* Past the NUL bytes in the node. */
4873 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004874 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004875 {
4876 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004877 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004878 sp->ts_state = STATE_DEL;
4879 break;
4880 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004881 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004882 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004883 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004884
4885 case STATE_PLAIN:
4886 /*
4887 * Go over all possible bytes at this node, add each to tword[]
4888 * and use child node. "ts_curi" is the index.
4889 */
4890 arridx = sp->ts_arridx;
4891 if (sp->ts_curi > byts[arridx])
4892 {
4893 /* Done all bytes at this node, do next state. When still at
4894 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004895 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004896 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004897 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004898 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004899 sp->ts_state = STATE_FINAL;
4900 }
4901 else
4902 {
4903 arridx += sp->ts_curi++;
4904 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004905
Bram Moolenaar4770d092006-01-12 23:22:24 +00004906 /* Normal byte, go one level deeper. If it's not equal to the
4907 * byte in the bad word adjust the score. But don't even try
4908 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01004909 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00004910 * delete + substitute. */
4911 if (c == fword[sp->ts_fidx]
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004912 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004913 newscore = 0;
4914 else
4915 newscore = SCORE_SUBST;
4916 if ((newscore == 0
4917 || (sp->ts_fidx >= sp->ts_fidxtry
4918 && ((sp->ts_flags & TSF_DIDDEL) == 0
4919 || c != fword[sp->ts_delidx])))
4920 && TRY_DEEPER(su, stack, depth, newscore))
4921 {
4922 go_deeper(stack, depth, newscore);
4923#ifdef DEBUG_TRIEWALK
4924 if (newscore > 0)
4925 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
4926 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4927 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004928 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004929 sprintf(changename[depth], "%.*s-%s: accept %c",
4930 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4931 fword[sp->ts_fidx]);
4932#endif
4933 ++depth;
4934 sp = &stack[depth];
4935 ++sp->ts_fidx;
4936 tword[sp->ts_twordlen++] = c;
4937 sp->ts_arridx = idxs[arridx];
Bram Moolenaar4770d092006-01-12 23:22:24 +00004938 if (newscore == SCORE_SUBST)
4939 sp->ts_isdiff = DIFF_YES;
4940 if (has_mbyte)
4941 {
4942 /* Multi-byte characters are a bit complicated to
4943 * handle: They differ when any of the bytes differ
4944 * and then their length may also differ. */
4945 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004946 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004947 /* First byte. */
4948 sp->ts_tcharidx = 0;
4949 sp->ts_tcharlen = MB_BYTE2LEN(c);
4950 sp->ts_fcharstart = sp->ts_fidx - 1;
4951 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004952 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004953 }
4954 else if (sp->ts_isdiff == DIFF_INSERT)
4955 /* When inserting trail bytes don't advance in the
4956 * bad word. */
4957 --sp->ts_fidx;
4958 if (++sp->ts_tcharidx == sp->ts_tcharlen)
4959 {
4960 /* Last byte of character. */
4961 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00004962 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004963 /* Correct ts_fidx for the byte length of the
4964 * character (we didn't check that before). */
4965 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004966 + MB_PTR2LEN(
4967 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004968 /* For changing a composing character adjust
4969 * the score from SCORE_SUBST to
4970 * SCORE_SUBCOMP. */
4971 if (enc_utf8
4972 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004973 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00004974 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004975 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004976 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004977 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004978 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004979 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004980 SCORE_SUBST - SCORE_SUBCOMP;
4981
Bram Moolenaar4770d092006-01-12 23:22:24 +00004982 /* For a similar character adjust score from
4983 * SCORE_SUBST to SCORE_SIMILAR. */
4984 else if (!soundfold
4985 && slang->sl_has_map
4986 && similar_chars(slang,
4987 mb_ptr2char(tword
4988 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00004989 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00004990 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00004991 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004992 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00004993 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00004994 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004995 else if (sp->ts_isdiff == DIFF_INSERT
4996 && sp->ts_twordlen > sp->ts_tcharlen)
4997 {
4998 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
4999 c = mb_ptr2char(p);
5000 if (enc_utf8 && utf_iscomposing(c))
5001 {
5002 /* Inserting a composing char doesn't
5003 * count that much. */
5004 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5005 }
5006 else
5007 {
5008 /* If the previous character was the same,
5009 * thus doubling a character, give a bonus
5010 * to the score. Also for the soundfold
5011 * tree (might seem illogical but does
5012 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005013 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005014 if (c == mb_ptr2char(p))
5015 sp->ts_score -= SCORE_INS
5016 - SCORE_INSDUP;
5017 }
5018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005019
Bram Moolenaar4770d092006-01-12 23:22:24 +00005020 /* Starting a new char, reset the length. */
5021 sp->ts_tcharlen = 0;
5022 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005023 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005024 else
Bram Moolenaarea408852005-06-25 22:49:46 +00005025 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005026 /* If we found a similar char adjust the score.
5027 * We do this after calling go_deeper() because
5028 * it's slow. */
5029 if (newscore != 0
5030 && !soundfold
5031 && slang->sl_has_map
5032 && similar_chars(slang,
5033 c, fword[sp->ts_fidx - 1]))
5034 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005037 }
5038 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005039
Bram Moolenaar4770d092006-01-12 23:22:24 +00005040 case STATE_DEL:
Bram Moolenaar4770d092006-01-12 23:22:24 +00005041 /* When past the first byte of a multi-byte char don't try
5042 * delete/insert/swap a character. */
5043 if (has_mbyte && sp->ts_tcharlen > 0)
5044 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005045 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005046 sp->ts_state = STATE_FINAL;
5047 break;
5048 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005049 /*
5050 * Try skipping one character in the bad word (delete it).
5051 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005052 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005053 sp->ts_state = STATE_INS_PREP;
5054 sp->ts_curi = 1;
5055 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5056 /* Deleting a vowel at the start of a word counts less, see
5057 * soundalike_score(). */
5058 newscore = 2 * SCORE_DEL / 3;
5059 else
5060 newscore = SCORE_DEL;
5061 if (fword[sp->ts_fidx] != NUL
5062 && TRY_DEEPER(su, stack, depth, newscore))
5063 {
5064 go_deeper(stack, depth, newscore);
5065#ifdef DEBUG_TRIEWALK
5066 sprintf(changename[depth], "%.*s-%s: delete %c",
5067 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5068 fword[sp->ts_fidx]);
5069#endif
5070 ++depth;
5071
5072 /* Remember what character we deleted, so that we can avoid
5073 * inserting it again. */
5074 stack[depth].ts_flags |= TSF_DIDDEL;
5075 stack[depth].ts_delidx = sp->ts_fidx;
5076
5077 /* Advance over the character in fword[]. Give a bonus to the
5078 * score if the same character is following "nn" -> "n". It's
5079 * a bit illogical for soundfold tree but it does give better
5080 * results. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005081 if (has_mbyte)
5082 {
5083 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005084 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005085 if (enc_utf8 && utf_iscomposing(c))
5086 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5087 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5088 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5089 }
5090 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005091 {
5092 ++stack[depth].ts_fidx;
5093 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5094 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5095 }
5096 break;
5097 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005098 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005099
5100 case STATE_INS_PREP:
5101 if (sp->ts_flags & TSF_DIDDEL)
5102 {
5103 /* If we just deleted a byte then inserting won't make sense,
5104 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005105 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005106 sp->ts_state = STATE_SWAP;
5107 break;
5108 }
5109
5110 /* skip over NUL bytes */
5111 n = sp->ts_arridx;
5112 for (;;)
5113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005114 if (sp->ts_curi > byts[n])
5115 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005116 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005117 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005118 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005119 break;
5120 }
5121 if (byts[n + sp->ts_curi] != NUL)
5122 {
5123 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005124 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005125 sp->ts_state = STATE_INS;
5126 break;
5127 }
5128 ++sp->ts_curi;
5129 }
5130 break;
5131
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005132 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005133
5134 case STATE_INS:
5135 /* Insert one byte. Repeat this for each possible byte at this
5136 * node. */
5137 n = sp->ts_arridx;
5138 if (sp->ts_curi > byts[n])
5139 {
5140 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005141 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005142 sp->ts_state = STATE_SWAP;
5143 break;
5144 }
5145
5146 /* Do one more byte at this node, but:
5147 * - Skip NUL bytes.
5148 * - Skip the byte if it's equal to the byte in the word,
5149 * accepting that byte is always better.
5150 */
5151 n += sp->ts_curi++;
5152 c = byts[n];
5153 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5154 /* Inserting a vowel at the start of a word counts less,
5155 * see soundalike_score(). */
5156 newscore = 2 * SCORE_INS / 3;
5157 else
5158 newscore = SCORE_INS;
5159 if (c != fword[sp->ts_fidx]
5160 && TRY_DEEPER(su, stack, depth, newscore))
5161 {
5162 go_deeper(stack, depth, newscore);
5163#ifdef DEBUG_TRIEWALK
5164 sprintf(changename[depth], "%.*s-%s: insert %c",
5165 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5166 c);
5167#endif
5168 ++depth;
5169 sp = &stack[depth];
5170 tword[sp->ts_twordlen++] = c;
5171 sp->ts_arridx = idxs[n];
Bram Moolenaar4770d092006-01-12 23:22:24 +00005172 if (has_mbyte)
5173 {
5174 fl = MB_BYTE2LEN(c);
5175 if (fl > 1)
5176 {
5177 /* There are following bytes for the same character.
5178 * We must find all bytes before trying
5179 * delete/insert/swap/etc. */
5180 sp->ts_tcharlen = fl;
5181 sp->ts_tcharidx = 1;
5182 sp->ts_isdiff = DIFF_INSERT;
5183 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005184 }
5185 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005186 fl = 1;
5187 if (fl == 1)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005188 {
5189 /* If the previous character was the same, thus doubling a
5190 * character, give a bonus to the score. Also for
5191 * soundfold words (illogical but does give a better
5192 * score). */
5193 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005194 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005195 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005197 }
5198 break;
5199
5200 case STATE_SWAP:
5201 /*
5202 * Swap two bytes in the bad word: "12" -> "21".
5203 * We change "fword" here, it's changed back afterwards at
5204 * STATE_UNSWAP.
5205 */
5206 p = fword + sp->ts_fidx;
5207 c = *p;
5208 if (c == NUL)
5209 {
5210 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005211 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005212 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005215
Bram Moolenaar4770d092006-01-12 23:22:24 +00005216 /* Don't swap if the first character is not a word character.
5217 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005218 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005219 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005220 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005221 sp->ts_state = STATE_REP_INI;
5222 break;
5223 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005224
Bram Moolenaar4770d092006-01-12 23:22:24 +00005225 if (has_mbyte)
5226 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005227 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005228 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005229 if (p[n] == NUL)
5230 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005231 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005232 c2 = c; /* don't swap non-word char */
5233 else
5234 c2 = mb_ptr2char(p + n);
5235 }
5236 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005237 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005238 if (p[1] == NUL)
5239 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005240 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005241 c2 = c; /* don't swap non-word char */
5242 else
5243 c2 = p[1];
5244 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005245
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005246 /* When the second character is NUL we can't swap. */
5247 if (c2 == NUL)
5248 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005249 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005250 sp->ts_state = STATE_REP_INI;
5251 break;
5252 }
5253
Bram Moolenaar4770d092006-01-12 23:22:24 +00005254 /* When characters are identical, swap won't do anything.
5255 * Also get here if the second char is not a word character. */
5256 if (c == c2)
5257 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005258 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005259 sp->ts_state = STATE_SWAP3;
5260 break;
5261 }
5262 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5263 {
5264 go_deeper(stack, depth, SCORE_SWAP);
5265#ifdef DEBUG_TRIEWALK
5266 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5267 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5268 c, c2);
5269#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005270 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005271 sp->ts_state = STATE_UNSWAP;
5272 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005273 if (has_mbyte)
5274 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005275 fl = mb_char2len(c2);
5276 mch_memmove(p, p + n, fl);
5277 mb_char2bytes(c, p + fl);
5278 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005279 }
5280 else
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005281 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005282 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005283 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005284 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005285 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005286 }
5287 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005288 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005289 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005290 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005291 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005292 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005293 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005294
Bram Moolenaar4770d092006-01-12 23:22:24 +00005295 case STATE_UNSWAP:
5296 /* Undo the STATE_SWAP swap: "21" -> "12". */
5297 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005298 if (has_mbyte)
5299 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005300 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005301 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005302 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005303 mb_char2bytes(c, p);
5304 }
5305 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005306 {
5307 c = *p;
5308 *p = p[1];
5309 p[1] = c;
5310 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005311 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005312
5313 case STATE_SWAP3:
5314 /* Swap two bytes, skipping one: "123" -> "321". We change
5315 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5316 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005317 if (has_mbyte)
5318 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005319 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005320 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005321 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005322 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005323 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005324 c3 = c; /* don't swap non-word char */
5325 else
5326 c3 = mb_ptr2char(p + n + fl);
5327 }
5328 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005329 {
5330 c = *p;
5331 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005332 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005333 c3 = c; /* don't swap non-word char */
5334 else
5335 c3 = p[2];
5336 }
5337
5338 /* When characters are identical: "121" then SWAP3 result is
5339 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5340 * same as SWAP on next char: "112". Thus skip all swapping.
5341 * Also skip when c3 is NUL.
5342 * Also get here when the third character is not a word character.
5343 * Second character may any char: "a.b" -> "b.a" */
5344 if (c == c3 || c3 == NUL)
5345 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005346 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005347 sp->ts_state = STATE_REP_INI;
5348 break;
5349 }
5350 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5351 {
5352 go_deeper(stack, depth, SCORE_SWAP3);
5353#ifdef DEBUG_TRIEWALK
5354 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5355 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5356 c, c3);
5357#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005358 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005359 sp->ts_state = STATE_UNSWAP3;
5360 ++depth;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005361 if (has_mbyte)
5362 {
5363 tl = mb_char2len(c3);
5364 mch_memmove(p, p + n + fl, tl);
5365 mb_char2bytes(c2, p + tl);
5366 mb_char2bytes(c, p + fl + tl);
5367 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5368 }
5369 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005370 {
5371 p[0] = p[2];
5372 p[2] = c;
5373 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5374 }
5375 }
5376 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005377 {
5378 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005379 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005380 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005381 break;
5382
5383 case STATE_UNSWAP3:
5384 /* Undo STATE_SWAP3: "321" -> "123" */
5385 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005386 if (has_mbyte)
5387 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005388 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005389 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005390 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005391 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005392 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005393 mch_memmove(p + fl + tl, p, n);
5394 mb_char2bytes(c, p);
5395 mb_char2bytes(c2, p + tl);
5396 p = p + tl;
5397 }
5398 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005399 {
5400 c = *p;
5401 *p = p[2];
5402 p[2] = c;
5403 ++p;
5404 }
5405
Bram Moolenaar860cae12010-06-05 23:22:07 +02005406 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005407 {
5408 /* Middle char is not a word char, skip the rotate. First and
5409 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005410 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005411 sp->ts_state = STATE_REP_INI;
5412 break;
5413 }
5414
5415 /* Rotate three characters left: "123" -> "231". We change
5416 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5417 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5418 {
5419 go_deeper(stack, depth, SCORE_SWAP3);
5420#ifdef DEBUG_TRIEWALK
5421 p = fword + sp->ts_fidx;
5422 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5423 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5424 p[0], p[1], p[2]);
5425#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005426 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005427 sp->ts_state = STATE_UNROT3L;
5428 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005429 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005430 if (has_mbyte)
5431 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005432 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005433 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005434 fl = MB_CPTR2LEN(p + n);
5435 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005436 mch_memmove(p, p + n, fl);
5437 mb_char2bytes(c, p + fl);
5438 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005439 }
5440 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005441 {
5442 c = *p;
5443 *p = p[1];
5444 p[1] = p[2];
5445 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005446 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005447 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005448 }
5449 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005450 {
5451 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005452 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005453 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005454 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455
Bram Moolenaar4770d092006-01-12 23:22:24 +00005456 case STATE_UNROT3L:
5457 /* Undo ROT3L: "231" -> "123" */
5458 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005459 if (has_mbyte)
5460 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005461 n = MB_PTR2LEN(p);
5462 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005463 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005464 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005465 mch_memmove(p + tl, p, n);
5466 mb_char2bytes(c, p);
5467 }
5468 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005469 {
5470 c = p[2];
5471 p[2] = p[1];
5472 p[1] = *p;
5473 *p = c;
5474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005475
Bram Moolenaar4770d092006-01-12 23:22:24 +00005476 /* Rotate three bytes right: "123" -> "312". We change "fword"
5477 * here, it's changed back afterwards at STATE_UNROT3R. */
5478 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5479 {
5480 go_deeper(stack, depth, SCORE_SWAP3);
5481#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005482 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005483 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5484 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5485 p[0], p[1], p[2]);
5486#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005487 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005488 sp->ts_state = STATE_UNROT3R;
5489 ++depth;
5490 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005491 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005492 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005493 n = MB_CPTR2LEN(p);
5494 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005495 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005496 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005497 mch_memmove(p + tl, p, n);
5498 mb_char2bytes(c, p);
5499 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005500 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005501 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005502 {
5503 c = p[2];
5504 p[2] = p[1];
5505 p[1] = *p;
5506 *p = c;
5507 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5508 }
5509 }
5510 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005511 {
5512 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005513 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005514 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005515 break;
5516
5517 case STATE_UNROT3R:
5518 /* Undo ROT3R: "312" -> "123" */
5519 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005520 if (has_mbyte)
5521 {
5522 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005523 tl = MB_PTR2LEN(p);
5524 n = MB_PTR2LEN(p + tl);
5525 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005526 mch_memmove(p, p + tl, n);
5527 mb_char2bytes(c, p + n);
5528 }
5529 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005530 {
5531 c = *p;
5532 *p = p[1];
5533 p[1] = p[2];
5534 p[2] = c;
5535 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005536 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005537
5538 case STATE_REP_INI:
5539 /* Check if matching with REP items from the .aff file would work.
5540 * Quickly skip if:
5541 * - there are no REP items and we are not in the soundfold trie
5542 * - the score is going to be too high anyway
5543 * - already applied a REP item or swapped here */
5544 if ((lp->lp_replang == NULL && !soundfold)
5545 || sp->ts_score + SCORE_REP >= su->su_maxscore
5546 || sp->ts_fidx < sp->ts_fidxtry)
5547 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005548 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005549 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005550 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005551 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005552
Bram Moolenaar4770d092006-01-12 23:22:24 +00005553 /* Use the first byte to quickly find the first entry that may
5554 * match. If the index is -1 there is none. */
5555 if (soundfold)
5556 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5557 else
5558 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005559
Bram Moolenaar4770d092006-01-12 23:22:24 +00005560 if (sp->ts_curi < 0)
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_FINAL;
5564 break;
5565 }
5566
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005567 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005568 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005569 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005570
5571 case STATE_REP:
5572 /* Try matching with REP items from the .aff file. For each match
5573 * replace the characters and check if the resulting word is
5574 * valid. */
5575 p = fword + sp->ts_fidx;
5576
5577 if (soundfold)
5578 gap = &slang->sl_repsal;
5579 else
5580 gap = &lp->lp_replang->sl_rep;
5581 while (sp->ts_curi < gap->ga_len)
5582 {
5583 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5584 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005585 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005586 /* past possible matching entries */
5587 sp->ts_curi = gap->ga_len;
5588 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005589 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005590 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5591 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5592 {
5593 go_deeper(stack, depth, SCORE_REP);
5594#ifdef DEBUG_TRIEWALK
5595 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5596 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5597 ftp->ft_from, ftp->ft_to);
5598#endif
5599 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005600 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005601 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005602
Bram Moolenaar4770d092006-01-12 23:22:24 +00005603 /* Change the "from" to the "to" string. */
5604 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005605 fl = (int)STRLEN(ftp->ft_from);
5606 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005607 if (fl != tl)
5608 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005609 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005610 repextra += tl - fl;
5611 }
5612 mch_memmove(p, ftp->ft_to, tl);
5613 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005614 stack[depth].ts_tcharlen = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005615 break;
5616 }
5617 }
5618
5619 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005620 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005621 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005622 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005623 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005624 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005625
5626 break;
5627
5628 case STATE_REP_UNDO:
5629 /* Undo a REP replacement and continue with the next one. */
5630 if (soundfold)
5631 gap = &slang->sl_repsal;
5632 else
5633 gap = &lp->lp_replang->sl_rep;
5634 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005635 fl = (int)STRLEN(ftp->ft_from);
5636 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005637 p = fword + sp->ts_fidx;
5638 if (fl != tl)
5639 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005640 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005641 repextra -= tl - fl;
5642 }
5643 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005644 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005645 sp->ts_state = STATE_REP;
5646 break;
5647
5648 default:
5649 /* Did all possible states at this level, go up one level. */
5650 --depth;
5651
5652 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5653 {
5654 /* Continue in or go back to the prefix tree. */
5655 byts = pbyts;
5656 idxs = pidxs;
5657 }
5658
5659 /* Don't check for CTRL-C too often, it takes time. */
5660 if (--breakcheckcount == 0)
5661 {
5662 ui_breakcheck();
5663 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005665 }
5666 }
5667}
5668
Bram Moolenaar4770d092006-01-12 23:22:24 +00005669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005670/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005671 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005672 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005673 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005674go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005675{
Bram Moolenaarea424162005-06-16 21:51:00 +00005676 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005677 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005678 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005679 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005680 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005681}
5682
Bram Moolenaar53805d12005-08-01 07:08:33 +00005683/*
5684 * Case-folding may change the number of bytes: Count nr of chars in
5685 * fword[flen] and return the byte length of that many chars in "word".
5686 */
5687 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005688nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005689{
5690 char_u *p;
5691 int i = 0;
5692
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005693 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005694 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005695 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005696 --i;
5697 return (int)(p - word);
5698}
Bram Moolenaar53805d12005-08-01 07:08:33 +00005699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005700/*
5701 * "fword" is a good word with case folded. Find the matching keep-case
5702 * words and put it in "kword".
5703 * Theoretically there could be several keep-case words that result in the
5704 * same case-folded word, but we only find one...
5705 */
5706 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005707find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005708{
5709 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5710 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005711 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005712
5713 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005714 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005715 int round[MAXWLEN];
5716 int fwordidx[MAXWLEN];
5717 int uwordidx[MAXWLEN];
5718 int kwordlen[MAXWLEN];
5719
5720 int flen, ulen;
5721 int l;
5722 int len;
5723 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005724 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005725 char_u *p;
5726 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005727 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005728
5729 if (byts == NULL)
5730 {
5731 /* array is empty: "cannot happen" */
5732 *kword = NUL;
5733 return;
5734 }
5735
5736 /* Make an all-cap version of "fword". */
5737 allcap_copy(fword, uword);
5738
5739 /*
5740 * Each character needs to be tried both case-folded and upper-case.
5741 * All this gets very complicated if we keep in mind that changing case
5742 * may change the byte length of a multi-byte character...
5743 */
5744 depth = 0;
5745 arridx[0] = 0;
5746 round[0] = 0;
5747 fwordidx[0] = 0;
5748 uwordidx[0] = 0;
5749 kwordlen[0] = 0;
5750 while (depth >= 0)
5751 {
5752 if (fword[fwordidx[depth]] == NUL)
5753 {
5754 /* We are at the end of "fword". If the tree allows a word to end
5755 * here we have found a match. */
5756 if (byts[arridx[depth] + 1] == 0)
5757 {
5758 kword[kwordlen[depth]] = NUL;
5759 return;
5760 }
5761
5762 /* kword is getting too long, continue one level up */
5763 --depth;
5764 }
5765 else if (++round[depth] > 2)
5766 {
5767 /* tried both fold-case and upper-case character, continue one
5768 * level up */
5769 --depth;
5770 }
5771 else
5772 {
5773 /*
5774 * round[depth] == 1: Try using the folded-case character.
5775 * round[depth] == 2: Try using the upper-case character.
5776 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005777 if (has_mbyte)
5778 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005779 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5780 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005781 }
5782 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005783 ulen = flen = 1;
5784 if (round[depth] == 1)
5785 {
5786 p = fword + fwordidx[depth];
5787 l = flen;
5788 }
5789 else
5790 {
5791 p = uword + uwordidx[depth];
5792 l = ulen;
5793 }
5794
5795 for (tryidx = arridx[depth]; l > 0; --l)
5796 {
5797 /* Perform a binary search in the list of accepted bytes. */
5798 len = byts[tryidx++];
5799 c = *p++;
5800 lo = tryidx;
5801 hi = tryidx + len - 1;
5802 while (lo < hi)
5803 {
5804 m = (lo + hi) / 2;
5805 if (byts[m] > c)
5806 hi = m - 1;
5807 else if (byts[m] < c)
5808 lo = m + 1;
5809 else
5810 {
5811 lo = hi = m;
5812 break;
5813 }
5814 }
5815
5816 /* Stop if there is no matching byte. */
5817 if (hi < lo || byts[lo] != c)
5818 break;
5819
5820 /* Continue at the child (if there is one). */
5821 tryidx = idxs[lo];
5822 }
5823
5824 if (l == 0)
5825 {
5826 /*
5827 * Found the matching char. Copy it to "kword" and go a
5828 * level deeper.
5829 */
5830 if (round[depth] == 1)
5831 {
5832 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5833 flen);
5834 kwordlen[depth + 1] = kwordlen[depth] + flen;
5835 }
5836 else
5837 {
5838 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5839 ulen);
5840 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5841 }
5842 fwordidx[depth + 1] = fwordidx[depth] + flen;
5843 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5844
5845 ++depth;
5846 arridx[depth] = tryidx;
5847 round[depth] = 0;
5848 }
5849 }
5850 }
5851
5852 /* Didn't find it: "cannot happen". */
5853 *kword = NUL;
5854}
5855
5856/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005857 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
5858 * su->su_sga.
5859 */
5860 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005861score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005862{
5863 langp_T *lp;
5864 char_u badsound[MAXWLEN];
5865 int i;
5866 suggest_T *stp;
5867 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005868 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005869 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005870
5871 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
5872 return;
5873
5874 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005875 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005876 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005877 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005878 if (lp->lp_slang->sl_sal.ga_len > 0)
5879 {
5880 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005881 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005882
5883 for (i = 0; i < su->su_ga.ga_len; ++i)
5884 {
5885 stp = &SUG(su->su_ga, i);
5886
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005887 /* Case-fold the suggested word, sound-fold it and compute the
5888 * sound-a-like score. */
5889 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005890 if (score < SCORE_MAXMAX)
5891 {
5892 /* Add the suggestion. */
5893 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
5894 sstp->st_word = vim_strsave(stp->st_word);
5895 if (sstp->st_word != NULL)
5896 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005897 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005898 sstp->st_score = score;
5899 sstp->st_altscore = 0;
5900 sstp->st_orglen = stp->st_orglen;
5901 ++su->su_sga.ga_len;
5902 }
5903 }
5904 }
5905 break;
5906 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005907 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005908}
5909
5910/*
5911 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005912 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005913 */
5914 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005915score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005916{
5917 int i;
5918 int j;
5919 garray_T ga;
5920 garray_T *gap;
5921 langp_T *lp;
5922 suggest_T *stp;
5923 char_u *p;
5924 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005925 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005926 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005927 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005928
5929 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005930 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005931 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005932 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005933 if (lp->lp_slang->sl_sal.ga_len > 0)
5934 {
5935 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005936 slang = lp->lp_slang;
5937 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005938
5939 for (i = 0; i < su->su_ga.ga_len; ++i)
5940 {
5941 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005942 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005943 if (stp->st_altscore == SCORE_MAXMAX)
5944 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
5945 else
5946 stp->st_score = (stp->st_score * 3
5947 + stp->st_altscore) / 4;
5948 stp->st_salscore = FALSE;
5949 }
5950 break;
5951 }
5952 }
5953
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005954 if (slang == NULL) /* Using "double" without sound folding. */
5955 {
5956 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
5957 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005958 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005959 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005960
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005961 /* Add the alternate score to su_sga. */
5962 for (i = 0; i < su->su_sga.ga_len; ++i)
5963 {
5964 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005965 stp->st_altscore = spell_edit_score(slang,
5966 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005967 if (stp->st_score == SCORE_MAXMAX)
5968 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
5969 else
5970 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
5971 stp->st_salscore = TRUE;
5972 }
5973
Bram Moolenaar4770d092006-01-12 23:22:24 +00005974 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
5975 * for both lists. */
5976 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005977 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005978 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005979 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
5980
5981 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
5982 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
5983 return;
5984
5985 stp = &SUG(ga, 0);
5986 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
5987 {
5988 /* round 1: get a suggestion from su_ga
5989 * round 2: get a suggestion from su_sga */
5990 for (round = 1; round <= 2; ++round)
5991 {
5992 gap = round == 1 ? &su->su_ga : &su->su_sga;
5993 if (i < gap->ga_len)
5994 {
5995 /* Don't add a word if it's already there. */
5996 p = SUG(*gap, i).st_word;
5997 for (j = 0; j < ga.ga_len; ++j)
5998 if (STRCMP(stp[j].st_word, p) == 0)
5999 break;
6000 if (j == ga.ga_len)
6001 stp[ga.ga_len++] = SUG(*gap, i);
6002 else
6003 vim_free(p);
6004 }
6005 }
6006 }
6007
6008 ga_clear(&su->su_ga);
6009 ga_clear(&su->su_sga);
6010
6011 /* Truncate the list to the number of suggestions that will be displayed. */
6012 if (ga.ga_len > su->su_maxcount)
6013 {
6014 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6015 vim_free(stp[i].st_word);
6016 ga.ga_len = su->su_maxcount;
6017 }
6018
6019 su->su_ga = ga;
6020}
6021
6022/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006023 * For the goodword in "stp" compute the soundalike score compared to the
6024 * badword.
6025 */
6026 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006027stp_sal_score(
6028 suggest_T *stp,
6029 suginfo_T *su,
6030 slang_T *slang,
6031 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006032{
6033 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006034 char_u *pbad;
6035 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006036 char_u badsound2[MAXWLEN];
6037 char_u fword[MAXWLEN];
6038 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006039 char_u goodword[MAXWLEN];
6040 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006041
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006042 lendiff = (int)(su->su_badlen - stp->st_orglen);
6043 if (lendiff >= 0)
6044 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006045 else
6046 {
6047 /* soundfold the bad word with more characters following */
6048 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6049
6050 /* When joining two words the sound often changes a lot. E.g., "t he"
6051 * sounds like "t h" while "the" sounds like "@". Avoid that by
6052 * removing the space. Don't do it when the good word also contains a
6053 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006054 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006055 && *skiptowhite(stp->st_word) == NUL)
6056 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006057 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006058
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006059 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006060 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006061 }
6062
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006063 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006064 {
6065 /* Add part of the bad word to the good word, so that we soundfold
6066 * what replaces the bad word. */
6067 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006068 vim_strncpy(goodword + stp->st_wordlen,
6069 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006070 pgood = goodword;
6071 }
6072 else
6073 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006074
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006075 /* Sound-fold the word and compute the score for the difference. */
6076 spell_soundfold(slang, pgood, FALSE, goodsound);
6077
6078 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006079}
6080
Bram Moolenaar4770d092006-01-12 23:22:24 +00006081/* structure used to store soundfolded words that add_sound_suggest() has
6082 * handled already. */
6083typedef struct
6084{
6085 short sft_score; /* lowest score used */
6086 char_u sft_word[1]; /* soundfolded word, actually longer */
6087} sftword_T;
6088
6089static sftword_T dumsft;
6090#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6091#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6092
6093/*
6094 * Prepare for calling suggest_try_soundalike().
6095 */
6096 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006097suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006098{
6099 langp_T *lp;
6100 int lpi;
6101 slang_T *slang;
6102
6103 /* Do this for all languages that support sound folding and for which a
6104 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006105 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006106 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006107 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006108 slang = lp->lp_slang;
6109 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6110 /* prepare the hashtable used by add_sound_suggest() */
6111 hash_init(&slang->sl_sounddone);
6112 }
6113}
6114
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006115/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006116 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006117 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006118 */
6119 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006120suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006121{
6122 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006123 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006124 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006125 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006126
Bram Moolenaar4770d092006-01-12 23:22:24 +00006127 /* Do this for all languages that support sound folding and for which a
6128 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006129 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006130 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006131 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006132 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006133 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006134 {
6135 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006136 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006137
Bram Moolenaar4770d092006-01-12 23:22:24 +00006138 /* try all kinds of inserts/deletes/swaps/etc. */
6139 /* TODO: also soundfold the next words, so that we can try joining
6140 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006141#ifdef SUGGEST_PROFILE
6142 prof_init();
6143#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006144 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006145#ifdef SUGGEST_PROFILE
6146 prof_report("soundalike");
6147#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006148 }
6149 }
6150}
6151
6152/*
6153 * Finish up after calling suggest_try_soundalike().
6154 */
6155 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006156suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006157{
6158 langp_T *lp;
6159 int lpi;
6160 slang_T *slang;
6161 int todo;
6162 hashitem_T *hi;
6163
6164 /* Do this for all languages that support sound folding and for which a
6165 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006166 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006167 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006168 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006169 slang = lp->lp_slang;
6170 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6171 {
6172 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006173 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006174 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6175 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006176 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006177 vim_free(HI2SFT(hi));
6178 --todo;
6179 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006180
6181 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006182 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006183 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006184 }
6185 }
6186}
6187
6188/*
6189 * A match with a soundfolded word is found. Add the good word(s) that
6190 * produce this soundfolded word.
6191 */
6192 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006193add_sound_suggest(
6194 suginfo_T *su,
6195 char_u *goodword,
6196 int score, /* soundfold score */
6197 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006198{
6199 slang_T *slang = lp->lp_slang; /* language for sound folding */
6200 int sfwordnr;
6201 char_u *nrline;
6202 int orgnr;
6203 char_u theword[MAXWLEN];
6204 int i;
6205 int wlen;
6206 char_u *byts;
6207 idx_T *idxs;
6208 int n;
6209 int wordcount;
6210 int wc;
6211 int goodscore;
6212 hash_T hash;
6213 hashitem_T *hi;
6214 sftword_T *sft;
6215 int bc, gc;
6216 int limit;
6217
6218 /*
6219 * It's very well possible that the same soundfold word is found several
6220 * times with different scores. Since the following is quite slow only do
6221 * the words that have a better score than before. Use a hashtable to
6222 * remember the words that have been done.
6223 */
6224 hash = hash_hash(goodword);
6225 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6226 if (HASHITEM_EMPTY(hi))
6227 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006228 sft = alloc(sizeof(sftword_T) + STRLEN(goodword));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006229 if (sft != NULL)
6230 {
6231 sft->sft_score = score;
6232 STRCPY(sft->sft_word, goodword);
6233 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6234 }
6235 }
6236 else
6237 {
6238 sft = HI2SFT(hi);
6239 if (score >= sft->sft_score)
6240 return;
6241 sft->sft_score = score;
6242 }
6243
6244 /*
6245 * Find the word nr in the soundfold tree.
6246 */
6247 sfwordnr = soundfold_find(slang, goodword);
6248 if (sfwordnr < 0)
6249 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006250 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006251 return;
6252 }
6253
6254 /*
6255 * go over the list of good words that produce this soundfold word
6256 */
6257 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6258 orgnr = 0;
6259 while (*nrline != NUL)
6260 {
6261 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6262 * previous wordnr. */
6263 orgnr += bytes2offset(&nrline);
6264
6265 byts = slang->sl_fbyts;
6266 idxs = slang->sl_fidxs;
6267
6268 /* Lookup the word "orgnr" one of the two tries. */
6269 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006270 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006271 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006272 {
6273 i = 1;
6274 if (wordcount == orgnr && byts[n + 1] == NUL)
6275 break; /* found end of word */
6276
6277 if (byts[n + 1] == NUL)
6278 ++wordcount;
6279
6280 /* skip over the NUL bytes */
6281 for ( ; byts[n + i] == NUL; ++i)
6282 if (i > byts[n]) /* safety check */
6283 {
6284 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006285 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006286 goto badword;
6287 }
6288
6289 /* One of the siblings must have the word. */
6290 for ( ; i < byts[n]; ++i)
6291 {
6292 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6293 if (wordcount + wc > orgnr)
6294 break;
6295 wordcount += wc;
6296 }
6297
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006298 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006299 n = idxs[n + i];
6300 }
6301badword:
6302 theword[wlen] = NUL;
6303
6304 /* Go over the possible flags and regions. */
6305 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6306 {
6307 char_u cword[MAXWLEN];
6308 char_u *p;
6309 int flags = (int)idxs[n + i];
6310
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006311 /* Skip words with the NOSUGGEST flag */
6312 if (flags & WF_NOSUGGEST)
6313 continue;
6314
Bram Moolenaar4770d092006-01-12 23:22:24 +00006315 if (flags & WF_KEEPCAP)
6316 {
6317 /* Must find the word in the keep-case tree. */
6318 find_keepcap_word(slang, theword, cword);
6319 p = cword;
6320 }
6321 else
6322 {
6323 flags |= su->su_badflags;
6324 if ((flags & WF_CAPMASK) != 0)
6325 {
6326 /* Need to fix case according to "flags". */
6327 make_case_word(theword, cword, flags);
6328 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006329 }
6330 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006331 p = theword;
6332 }
6333
6334 /* Add the suggestion. */
6335 if (sps_flags & SPS_DOUBLE)
6336 {
6337 /* Add the suggestion if the score isn't too bad. */
6338 if (score <= su->su_maxscore)
6339 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6340 score, 0, FALSE, slang, FALSE);
6341 }
6342 else
6343 {
6344 /* Add a penalty for words in another region. */
6345 if ((flags & WF_REGION)
6346 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6347 goodscore = SCORE_REGION;
6348 else
6349 goodscore = 0;
6350
6351 /* Add a small penalty for changing the first letter from
6352 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006353 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006354 * letter is the same, that has already been counted. */
6355 gc = PTR2CHAR(p);
6356 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006357 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006358 bc = PTR2CHAR(su->su_badword);
6359 if (!SPELL_ISUPPER(bc)
6360 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6361 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006362 }
6363
Bram Moolenaar4770d092006-01-12 23:22:24 +00006364 /* Compute the score for the good word. This only does letter
6365 * insert/delete/swap/replace. REP items are not considered,
6366 * which may make the score a bit higher.
6367 * Use a limit for the score to make it work faster. Use
6368 * MAXSCORE(), because RESCORE() will change the score.
6369 * If the limit is very high then the iterative method is
6370 * inefficient, using an array is quicker. */
6371 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6372 if (limit > SCORE_LIMITMAX)
6373 goodscore += spell_edit_score(slang, su->su_badword, p);
6374 else
6375 goodscore += spell_edit_score_limit(slang, su->su_badword,
6376 p, limit);
6377
6378 /* When going over the limit don't bother to do the rest. */
6379 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006380 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006381 /* Give a bonus to words seen before. */
6382 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006383
Bram Moolenaar4770d092006-01-12 23:22:24 +00006384 /* Add the suggestion if the score isn't too bad. */
6385 goodscore = RESCORE(goodscore, score);
6386 if (goodscore <= su->su_sfmaxscore)
6387 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6388 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006389 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006390 }
6391 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006392 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006393 }
6394}
6395
6396/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006397 * Find word "word" in fold-case tree for "slang" and return the word number.
6398 */
6399 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006400soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006401{
6402 idx_T arridx = 0;
6403 int len;
6404 int wlen = 0;
6405 int c;
6406 char_u *ptr = word;
6407 char_u *byts;
6408 idx_T *idxs;
6409 int wordnr = 0;
6410
6411 byts = slang->sl_sbyts;
6412 idxs = slang->sl_sidxs;
6413
6414 for (;;)
6415 {
6416 /* First byte is the number of possible bytes. */
6417 len = byts[arridx++];
6418
6419 /* If the first possible byte is a zero the word could end here.
6420 * If the word ends we found the word. If not skip the NUL bytes. */
6421 c = ptr[wlen];
6422 if (byts[arridx] == NUL)
6423 {
6424 if (c == NUL)
6425 break;
6426
6427 /* Skip over the zeros, there can be several. */
6428 while (len > 0 && byts[arridx] == NUL)
6429 {
6430 ++arridx;
6431 --len;
6432 }
6433 if (len == 0)
6434 return -1; /* no children, word should have ended here */
6435 ++wordnr;
6436 }
6437
6438 /* If the word ends we didn't find it. */
6439 if (c == NUL)
6440 return -1;
6441
6442 /* Perform a binary search in the list of accepted bytes. */
6443 if (c == TAB) /* <Tab> is handled like <Space> */
6444 c = ' ';
6445 while (byts[arridx] < c)
6446 {
6447 /* The word count is in the first idxs[] entry of the child. */
6448 wordnr += idxs[idxs[arridx]];
6449 ++arridx;
6450 if (--len == 0) /* end of the bytes, didn't find it */
6451 return -1;
6452 }
6453 if (byts[arridx] != c) /* didn't find the byte */
6454 return -1;
6455
6456 /* Continue at the child (if there is one). */
6457 arridx = idxs[arridx];
6458 ++wlen;
6459
6460 /* One space in the good word may stand for several spaces in the
6461 * checked word. */
6462 if (c == ' ')
6463 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6464 ++wlen;
6465 }
6466
6467 return wordnr;
6468}
6469
6470/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006471 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006472 */
6473 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006474make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006475{
6476 if (flags & WF_ALLCAP)
6477 /* Make it all upper-case */
6478 allcap_copy(fword, cword);
6479 else if (flags & WF_ONECAP)
6480 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006481 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006482 else
6483 /* Use goodword as-is. */
6484 STRCPY(cword, fword);
6485}
6486
Bram Moolenaarea424162005-06-16 21:51:00 +00006487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006488/*
6489 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6490 * lines in the .aff file.
6491 */
6492 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006493similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006494{
Bram Moolenaarea424162005-06-16 21:51:00 +00006495 int m1, m2;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006496 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006497 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006498
Bram Moolenaarea424162005-06-16 21:51:00 +00006499 if (c1 >= 256)
6500 {
6501 buf[mb_char2bytes(c1, buf)] = 0;
6502 hi = hash_find(&slang->sl_map_hash, buf);
6503 if (HASHITEM_EMPTY(hi))
6504 m1 = 0;
6505 else
6506 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6507 }
6508 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006509 m1 = slang->sl_map_array[c1];
6510 if (m1 == 0)
6511 return FALSE;
6512
6513
Bram Moolenaarea424162005-06-16 21:51:00 +00006514 if (c2 >= 256)
6515 {
6516 buf[mb_char2bytes(c2, buf)] = 0;
6517 hi = hash_find(&slang->sl_map_hash, buf);
6518 if (HASHITEM_EMPTY(hi))
6519 m2 = 0;
6520 else
6521 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6522 }
6523 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006524 m2 = slang->sl_map_array[c2];
6525
6526 return m1 == m2;
6527}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006528
6529/*
6530 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006531 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006532 */
6533 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006534add_suggestion(
6535 suginfo_T *su,
6536 garray_T *gap, /* either su_ga or su_sga */
6537 char_u *goodword,
6538 int badlenarg, /* len of bad word replaced with "goodword" */
6539 int score,
6540 int altscore,
6541 int had_bonus, /* value for st_had_bonus */
6542 slang_T *slang, /* language for sound folding */
6543 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006544 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006545{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006546 int goodlen; /* len of goodword changed */
6547 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006548 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006549 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006550 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006551 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006552
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006553 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6554 * "thee the" is added next to changing the first "the" the "thee". */
6555 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006556 pbad = su->su_badptr + badlenarg;
6557 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006558 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006559 goodlen = (int)(pgood - goodword);
6560 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006561 if (goodlen <= 0 || badlen <= 0)
6562 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006563 MB_PTR_BACK(goodword, pgood);
6564 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006565 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006566 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006567 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6568 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006569 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006570 else if (*pgood != *pbad)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006571 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006572 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006573
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006574 if (badlen == 0 && goodlen == 0)
6575 /* goodword doesn't change anything; may happen for "the the" changing
6576 * the first "the" to itself. */
6577 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006578
Bram Moolenaar89d40322006-08-29 15:30:07 +00006579 if (gap->ga_len == 0)
6580 i = -1;
6581 else
6582 {
6583 /* Check if the word is already there. Also check the length that is
6584 * being replaced "thes," -> "these" is a different suggestion from
6585 * "thes" -> "these". */
6586 stp = &SUG(*gap, 0);
6587 for (i = gap->ga_len; --i >= 0; ++stp)
6588 if (stp->st_wordlen == goodlen
6589 && stp->st_orglen == badlen
6590 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006591 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006592 /*
6593 * Found it. Remember the word with the lowest score.
6594 */
6595 if (stp->st_slang == NULL)
6596 stp->st_slang = slang;
6597
6598 new_sug.st_score = score;
6599 new_sug.st_altscore = altscore;
6600 new_sug.st_had_bonus = had_bonus;
6601
6602 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006603 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006604 /* Only one of the two had the soundalike score computed.
6605 * Need to do that for the other one now, otherwise the
6606 * scores can't be compared. This happens because
6607 * suggest_try_change() doesn't compute the soundalike
6608 * word to keep it fast, while some special methods set
6609 * the soundalike score to zero. */
6610 if (had_bonus)
6611 rescore_one(su, stp);
6612 else
6613 {
6614 new_sug.st_word = stp->st_word;
6615 new_sug.st_wordlen = stp->st_wordlen;
6616 new_sug.st_slang = stp->st_slang;
6617 new_sug.st_orglen = badlen;
6618 rescore_one(su, &new_sug);
6619 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006621
Bram Moolenaar89d40322006-08-29 15:30:07 +00006622 if (stp->st_score > new_sug.st_score)
6623 {
6624 stp->st_score = new_sug.st_score;
6625 stp->st_altscore = new_sug.st_altscore;
6626 stp->st_had_bonus = new_sug.st_had_bonus;
6627 }
6628 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006629 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006631
Bram Moolenaar4770d092006-01-12 23:22:24 +00006632 if (i < 0 && ga_grow(gap, 1) == OK)
6633 {
6634 /* Add a suggestion. */
6635 stp = &SUG(*gap, gap->ga_len);
6636 stp->st_word = vim_strnsave(goodword, goodlen);
6637 if (stp->st_word != NULL)
6638 {
6639 stp->st_wordlen = goodlen;
6640 stp->st_score = score;
6641 stp->st_altscore = altscore;
6642 stp->st_had_bonus = had_bonus;
6643 stp->st_orglen = badlen;
6644 stp->st_slang = slang;
6645 ++gap->ga_len;
6646
6647 /* If we have too many suggestions now, sort the list and keep
6648 * the best suggestions. */
6649 if (gap->ga_len > SUG_MAX_COUNT(su))
6650 {
6651 if (maxsf)
6652 su->su_sfmaxscore = cleanup_suggestions(gap,
6653 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6654 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006655 su->su_maxscore = cleanup_suggestions(gap,
6656 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006657 }
6658 }
6659 }
6660}
6661
6662/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006663 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6664 * for split words, such as "the the". Remove these from the list here.
6665 */
6666 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006667check_suggestions(
6668 suginfo_T *su,
6669 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006670{
6671 suggest_T *stp;
6672 int i;
6673 char_u longword[MAXWLEN + 1];
6674 int len;
6675 hlf_T attr;
6676
6677 stp = &SUG(*gap, 0);
6678 for (i = gap->ga_len - 1; i >= 0; --i)
6679 {
6680 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006681 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006682 len = stp[i].st_wordlen;
6683 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6684 MAXWLEN - len);
6685 attr = HLF_COUNT;
6686 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6687 if (attr != HLF_COUNT)
6688 {
6689 /* Remove this entry. */
6690 vim_free(stp[i].st_word);
6691 --gap->ga_len;
6692 if (i < gap->ga_len)
6693 mch_memmove(stp + i, stp + i + 1,
6694 sizeof(suggest_T) * (gap->ga_len - i));
6695 }
6696 }
6697}
6698
6699
6700/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006701 * Add a word to be banned.
6702 */
6703 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006704add_banned(
6705 suginfo_T *su,
6706 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006707{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006708 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006709 hash_T hash;
6710 hashitem_T *hi;
6711
Bram Moolenaar4770d092006-01-12 23:22:24 +00006712 hash = hash_hash(word);
6713 hi = hash_lookup(&su->su_banned, word, hash);
6714 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006715 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006716 s = vim_strsave(word);
6717 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006718 hash_add_item(&su->su_banned, hi, s, hash);
6719 }
6720}
6721
6722/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006723 * Recompute the score for all suggestions if sound-folding is possible. This
6724 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006725 */
6726 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006727rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006728{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006729 int i;
6730
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006731 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006732 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006733 rescore_one(su, &SUG(su->su_ga, i));
6734}
6735
6736/*
6737 * Recompute the score for one suggestion if sound-folding is possible.
6738 */
6739 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006740rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006741{
6742 slang_T *slang = stp->st_slang;
6743 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006744 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006745
6746 /* Only rescore suggestions that have no sal score yet and do have a
6747 * language. */
6748 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6749 {
6750 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006751 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006752 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006753 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006754 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006755 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006756 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006757
6758 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006759 if (stp->st_altscore == SCORE_MAXMAX)
6760 stp->st_altscore = SCORE_BIG;
6761 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6762 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006763 }
6764}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006765
Bram Moolenaareae1b912019-05-09 15:12:55 +02006766static int sug_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
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006773sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006774{
6775 suggest_T *p1 = (suggest_T *)s1;
6776 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006777 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006778
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006779 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006780 {
6781 n = p1->st_altscore - p2->st_altscore;
6782 if (n == 0)
6783 n = STRICMP(p1->st_word, p2->st_word);
6784 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006785 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006786}
6787
6788/*
6789 * Cleanup the suggestions:
6790 * - Sort on score.
6791 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006792 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006793 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006794 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006795cleanup_suggestions(
6796 garray_T *gap,
6797 int maxscore,
6798 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006799{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006800 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006801 int i;
6802
6803 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006804 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006805
6806 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006807 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006808 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006809 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006810 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006811 gap->ga_len = keep;
6812 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006813 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006814 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006815}
6816
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006817#if defined(FEAT_EVAL) || defined(PROTO)
6818/*
6819 * Soundfold a string, for soundfold().
6820 * Result is in allocated memory, NULL for an error.
6821 */
6822 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006823eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006824{
6825 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006826 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006827 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006828
Bram Moolenaar860cae12010-06-05 23:22:07 +02006829 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006830 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006831 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006832 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006833 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006834 if (lp->lp_slang->sl_sal.ga_len > 0)
6835 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006836 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006837 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006838 return vim_strsave(sound);
6839 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006840 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006841
6842 /* No language with sound folding, return word as-is. */
6843 return vim_strsave(word);
6844}
6845#endif
6846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006847/*
6848 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00006849 *
6850 * There are many ways to turn a word into a sound-a-like representation. The
6851 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
6852 * swedish name matching - survey and test of different algorithms" by Klas
6853 * Erikson.
6854 *
6855 * We support two methods:
6856 * 1. SOFOFROM/SOFOTO do a simple character mapping.
6857 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006858 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006859 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006860spell_soundfold(
6861 slang_T *slang,
6862 char_u *inword,
6863 int folded, /* "inword" is already case-folded */
6864 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006865{
6866 char_u fword[MAXWLEN];
6867 char_u *word;
6868
6869 if (slang->sl_sofo)
6870 /* SOFOFROM and SOFOTO used */
6871 spell_soundfold_sofo(slang, inword, res);
6872 else
6873 {
6874 /* SAL items used. Requires the word to be case-folded. */
6875 if (folded)
6876 word = inword;
6877 else
6878 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006879 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006880 word = fword;
6881 }
6882
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006883 if (has_mbyte)
6884 spell_soundfold_wsal(slang, word, res);
6885 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006886 spell_soundfold_sal(slang, word, res);
6887 }
6888}
6889
6890/*
6891 * Perform sound folding of "inword" into "res" according to SOFOFROM and
6892 * SOFOTO lines.
6893 */
6894 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006895spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006896{
6897 char_u *s;
6898 int ri = 0;
6899 int c;
6900
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006901 if (has_mbyte)
6902 {
6903 int prevc = 0;
6904 int *ip;
6905
6906 /* The sl_sal_first[] table contains the translation for chars up to
6907 * 255, sl_sal the rest. */
6908 for (s = inword; *s != NUL; )
6909 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006910 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006911 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006912 c = ' ';
6913 else if (c < 256)
6914 c = slang->sl_sal_first[c];
6915 else
6916 {
6917 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
6918 if (ip == NULL) /* empty list, can't match */
6919 c = NUL;
6920 else
6921 for (;;) /* find "c" in the list */
6922 {
6923 if (*ip == 0) /* not found */
6924 {
6925 c = NUL;
6926 break;
6927 }
6928 if (*ip == c) /* match! */
6929 {
6930 c = ip[1];
6931 break;
6932 }
6933 ip += 2;
6934 }
6935 }
6936
6937 if (c != NUL && c != prevc)
6938 {
6939 ri += mb_char2bytes(c, res + ri);
6940 if (ri + MB_MAXBYTES > MAXWLEN)
6941 break;
6942 prevc = c;
6943 }
6944 }
6945 }
6946 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006947 {
6948 /* The sl_sal_first[] table contains the translation. */
6949 for (s = inword; (c = *s) != NUL; ++s)
6950 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006951 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006952 c = ' ';
6953 else
6954 c = slang->sl_sal_first[c];
6955 if (c != NUL && (ri == 0 || res[ri - 1] != c))
6956 res[ri++] = c;
6957 }
6958 }
6959
6960 res[ri] = NUL;
6961}
6962
6963 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006964spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006965{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006966 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006967 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006968 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006969 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006970 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006971 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006972 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006973 int n, k = 0;
6974 int z0;
6975 int k0;
6976 int n0;
6977 int c;
6978 int pri;
6979 int p0 = -333;
6980 int c0;
6981
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006982 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006983 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006984 if (slang->sl_rem_accents)
6985 {
6986 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006987 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006988 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006989 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006990 {
6991 *t++ = ' ';
6992 s = skipwhite(s);
6993 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006994 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006995 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01006996 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006997 *t++ = *s;
6998 ++s;
6999 }
7000 }
7001 *t = NUL;
7002 }
7003 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007004 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007005
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007006 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007007
7008 /*
7009 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007010 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007011 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007012 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007013 while ((c = word[i]) != NUL)
7014 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007015 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007016 n = slang->sl_sal_first[c];
7017 z0 = 0;
7018
7019 if (n >= 0)
7020 {
7021 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007022 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007023 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007024 /* Quickly skip entries that don't match the word. Most
7025 * entries are less then three chars, optimize for that. */
7026 k = smp[n].sm_leadlen;
7027 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007028 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007029 if (word[i + 1] != s[1])
7030 continue;
7031 if (k > 2)
7032 {
7033 for (j = 2; j < k; ++j)
7034 if (word[i + j] != s[j])
7035 break;
7036 if (j < k)
7037 continue;
7038 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007039 }
7040
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007041 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007042 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007043 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007044 while (*pf != NUL && *pf != word[i + k])
7045 ++pf;
7046 if (*pf == NUL)
7047 continue;
7048 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007049 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007050 s = smp[n].sm_rules;
7051 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007052
7053 p0 = *s;
7054 k0 = k;
7055 while (*s == '-' && k > 1)
7056 {
7057 k--;
7058 s++;
7059 }
7060 if (*s == '<')
7061 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007062 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007063 {
7064 /* determine priority */
7065 pri = *s - '0';
7066 s++;
7067 }
7068 if (*s == '^' && *(s + 1) == '^')
7069 s++;
7070
7071 if (*s == NUL
7072 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007073 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007074 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007075 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007076 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007077 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007078 && spell_iswordp(word + i - 1, curwin)
7079 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007080 {
7081 /* search for followup rules, if: */
7082 /* followup and k > 1 and NO '-' in searchstring */
7083 c0 = word[i + k - 1];
7084 n0 = slang->sl_sal_first[c0];
7085
7086 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007087 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007088 {
7089 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007090 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007091 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007092 /* Quickly skip entries that don't match the word.
7093 * */
7094 k0 = smp[n0].sm_leadlen;
7095 if (k0 > 1)
7096 {
7097 if (word[i + k] != s[1])
7098 continue;
7099 if (k0 > 2)
7100 {
7101 pf = word + i + k + 1;
7102 for (j = 2; j < k0; ++j)
7103 if (*pf++ != s[j])
7104 break;
7105 if (j < k0)
7106 continue;
7107 }
7108 }
7109 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007110
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007111 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007112 {
7113 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007114 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007115 while (*pf != NUL && *pf != word[i + k0])
7116 ++pf;
7117 if (*pf == NUL)
7118 continue;
7119 ++k0;
7120 }
7121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007122 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007123 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007124 while (*s == '-')
7125 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007126 /* "k0" gets NOT reduced because
7127 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007128 s++;
7129 }
7130 if (*s == '<')
7131 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007132 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007133 {
7134 p0 = *s - '0';
7135 s++;
7136 }
7137
7138 if (*s == NUL
7139 /* *s == '^' cuts */
7140 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007141 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007142 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007143 {
7144 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007145 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007146 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007147
7148 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007149 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007151 /* rule fits; stop search */
7152 break;
7153 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007154 }
7155
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007156 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007157 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158 }
7159
7160 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007161 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007162 if (s == NULL)
7163 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007164 pf = smp[n].sm_rules;
7165 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007166 if (p0 == 1 && z == 0)
7167 {
7168 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007169 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7170 || res[reslen - 1] == *s))
7171 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007172 z0 = 1;
7173 z = 1;
7174 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007175 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007176 {
7177 word[i + k0] = *s;
7178 k0++;
7179 s++;
7180 }
7181 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007182 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007183
7184 /* new "actual letter" */
7185 c = word[i];
7186 }
7187 else
7188 {
7189 /* no '<' rule used */
7190 i += k - 1;
7191 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007192 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007193 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007194 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007195 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007196 s++;
7197 }
7198 /* new "actual letter" */
7199 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007200 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007201 {
7202 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007203 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007204 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 i = 0;
7206 z0 = 1;
7207 }
7208 }
7209 break;
7210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007211 }
7212 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007213 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007214 {
7215 c = ' ';
7216 k = 1;
7217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007218
7219 if (z0 == 0)
7220 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007221 if (k && !p0 && reslen < MAXWLEN && c != NUL
7222 && (!slang->sl_collapse || reslen == 0
7223 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007224 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007225 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007226
7227 i++;
7228 z = 0;
7229 k = 0;
7230 }
7231 }
7232
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007233 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007234}
7235
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007236/*
7237 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7238 * Multi-byte version of spell_soundfold().
7239 */
7240 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007241spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007242{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007243 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007244 int word[MAXWLEN];
7245 int wres[MAXWLEN];
7246 int l;
7247 char_u *s;
7248 int *ws;
7249 char_u *t;
7250 int *pf;
7251 int i, j, z;
7252 int reslen;
7253 int n, k = 0;
7254 int z0;
7255 int k0;
7256 int n0;
7257 int c;
7258 int pri;
7259 int p0 = -333;
7260 int c0;
7261 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007262 int wordlen;
7263
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007264
7265 /*
7266 * Convert the multi-byte string to a wide-character string.
7267 * Remove accents, if wanted. We actually remove all non-word characters.
7268 * But keep white space.
7269 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007270 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007271 for (s = inword; *s != NUL; )
7272 {
7273 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007274 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007275 if (slang->sl_rem_accents)
7276 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007277 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007278 {
7279 if (did_white)
7280 continue;
7281 c = ' ';
7282 did_white = TRUE;
7283 }
7284 else
7285 {
7286 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007287 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007288 continue;
7289 }
7290 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007291 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007292 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007293 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007294
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007295 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007296 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007297 * Converted from C++ to C. Added support for multi-byte chars.
7298 * Changed to keep spaces.
7299 */
7300 i = reslen = z = 0;
7301 while ((c = word[i]) != NUL)
7302 {
7303 /* Start with the first rule that has the character in the word. */
7304 n = slang->sl_sal_first[c & 0xff];
7305 z0 = 0;
7306
7307 if (n >= 0)
7308 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007309 /* Check all rules for the same index byte.
7310 * If c is 0x300 need extra check for the end of the array, as
7311 * (c & 0xff) is NUL. */
7312 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7313 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007314 {
7315 /* Quickly skip entries that don't match the word. Most
7316 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007317 if (c != ws[0])
7318 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007319 k = smp[n].sm_leadlen;
7320 if (k > 1)
7321 {
7322 if (word[i + 1] != ws[1])
7323 continue;
7324 if (k > 2)
7325 {
7326 for (j = 2; j < k; ++j)
7327 if (word[i + j] != ws[j])
7328 break;
7329 if (j < k)
7330 continue;
7331 }
7332 }
7333
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007334 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007335 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007336 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007337 while (*pf != NUL && *pf != word[i + k])
7338 ++pf;
7339 if (*pf == NUL)
7340 continue;
7341 ++k;
7342 }
7343 s = smp[n].sm_rules;
7344 pri = 5; /* default priority */
7345
7346 p0 = *s;
7347 k0 = k;
7348 while (*s == '-' && k > 1)
7349 {
7350 k--;
7351 s++;
7352 }
7353 if (*s == '<')
7354 s++;
7355 if (VIM_ISDIGIT(*s))
7356 {
7357 /* determine priority */
7358 pri = *s - '0';
7359 s++;
7360 }
7361 if (*s == '^' && *(s + 1) == '^')
7362 s++;
7363
7364 if (*s == NUL
7365 || (*s == '^'
7366 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007367 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007368 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007369 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007370 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007371 && spell_iswordp_w(word + i - 1, curwin)
7372 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007373 {
7374 /* search for followup rules, if: */
7375 /* followup and k > 1 and NO '-' in searchstring */
7376 c0 = word[i + k - 1];
7377 n0 = slang->sl_sal_first[c0 & 0xff];
7378
7379 if (slang->sl_followup && k > 1 && n0 >= 0
7380 && p0 != '-' && word[i + k] != NUL)
7381 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007382 /* Test follow-up rule for "word[i + k]"; loop over
7383 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007384 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7385 == (c0 & 0xff); ++n0)
7386 {
7387 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007388 */
7389 if (c0 != ws[0])
7390 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007391 k0 = smp[n0].sm_leadlen;
7392 if (k0 > 1)
7393 {
7394 if (word[i + k] != ws[1])
7395 continue;
7396 if (k0 > 2)
7397 {
7398 pf = word + i + k + 1;
7399 for (j = 2; j < k0; ++j)
7400 if (*pf++ != ws[j])
7401 break;
7402 if (j < k0)
7403 continue;
7404 }
7405 }
7406 k0 += k - 1;
7407
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007408 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007409 {
7410 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007411 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007412 while (*pf != NUL && *pf != word[i + k0])
7413 ++pf;
7414 if (*pf == NUL)
7415 continue;
7416 ++k0;
7417 }
7418
7419 p0 = 5;
7420 s = smp[n0].sm_rules;
7421 while (*s == '-')
7422 {
7423 /* "k0" gets NOT reduced because
7424 * "if (k0 == k)" */
7425 s++;
7426 }
7427 if (*s == '<')
7428 s++;
7429 if (VIM_ISDIGIT(*s))
7430 {
7431 p0 = *s - '0';
7432 s++;
7433 }
7434
7435 if (*s == NUL
7436 /* *s == '^' cuts */
7437 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007438 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007439 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007440 {
7441 if (k0 == k)
7442 /* this is just a piece of the string */
7443 continue;
7444
7445 if (p0 < pri)
7446 /* priority too low */
7447 continue;
7448 /* rule fits; stop search */
7449 break;
7450 }
7451 }
7452
7453 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7454 == (c0 & 0xff))
7455 continue;
7456 }
7457
7458 /* replace string */
7459 ws = smp[n].sm_to_w;
7460 s = smp[n].sm_rules;
7461 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7462 if (p0 == 1 && z == 0)
7463 {
7464 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007465 if (reslen > 0 && ws != NULL && *ws != NUL
7466 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007467 || wres[reslen - 1] == *ws))
7468 reslen--;
7469 z0 = 1;
7470 z = 1;
7471 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007472 if (ws != NULL)
7473 while (*ws != NUL && word[i + k0] != NUL)
7474 {
7475 word[i + k0] = *ws;
7476 k0++;
7477 ws++;
7478 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007479 if (k > k0)
7480 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007481 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007482
7483 /* new "actual letter" */
7484 c = word[i];
7485 }
7486 else
7487 {
7488 /* no '<' rule used */
7489 i += k - 1;
7490 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007491 if (ws != NULL)
7492 while (*ws != NUL && ws[1] != NUL
7493 && reslen < MAXWLEN)
7494 {
7495 if (reslen == 0 || wres[reslen - 1] != *ws)
7496 wres[reslen++] = *ws;
7497 ws++;
7498 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007499 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007500 if (ws == NULL)
7501 c = NUL;
7502 else
7503 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007504 if (strstr((char *)s, "^^") != NULL)
7505 {
7506 if (c != NUL)
7507 wres[reslen++] = c;
7508 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007509 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007510 i = 0;
7511 z0 = 1;
7512 }
7513 }
7514 break;
7515 }
7516 }
7517 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007518 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007519 {
7520 c = ' ';
7521 k = 1;
7522 }
7523
7524 if (z0 == 0)
7525 {
7526 if (k && !p0 && reslen < MAXWLEN && c != NUL
7527 && (!slang->sl_collapse || reslen == 0
7528 || wres[reslen - 1] != c))
7529 /* condense only double letters */
7530 wres[reslen++] = c;
7531
7532 i++;
7533 z = 0;
7534 k = 0;
7535 }
7536 }
7537
7538 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7539 l = 0;
7540 for (n = 0; n < reslen; ++n)
7541 {
7542 l += mb_char2bytes(wres[n], res + l);
7543 if (l + MB_MAXBYTES > MAXWLEN)
7544 break;
7545 }
7546 res[l] = NUL;
7547}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007548
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007549/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007550 * Compute a score for two sound-a-like words.
7551 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7552 * Instead of a generic loop we write out the code. That keeps it fast by
7553 * avoiding checks that will not be possible.
7554 */
7555 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007556soundalike_score(
7557 char_u *goodstart, /* sound-folded good word */
7558 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007559{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007560 char_u *goodsound = goodstart;
7561 char_u *badsound = badstart;
7562 int goodlen;
7563 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007564 int n;
7565 char_u *pl, *ps;
7566 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007567 int score = 0;
7568
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007569 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007570 * counted so much, vowels halfway the word aren't counted at all. */
7571 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7572 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007573 if ((badsound[0] == NUL && goodsound[1] == NUL)
7574 || (goodsound[0] == NUL && badsound[1] == NUL))
7575 /* changing word with vowel to word without a sound */
7576 return SCORE_DEL;
7577 if (badsound[0] == NUL || goodsound[0] == NUL)
7578 /* more than two changes */
7579 return SCORE_MAXMAX;
7580
Bram Moolenaar4770d092006-01-12 23:22:24 +00007581 if (badsound[1] == goodsound[1]
7582 || (badsound[1] != NUL
7583 && goodsound[1] != NUL
7584 && badsound[2] == goodsound[2]))
7585 {
7586 /* handle like a substitute */
7587 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007588 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007589 {
7590 score = 2 * SCORE_DEL / 3;
7591 if (*badsound == '*')
7592 ++badsound;
7593 else
7594 ++goodsound;
7595 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007596 }
7597
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007598 goodlen = (int)STRLEN(goodsound);
7599 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007600
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007601 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007602 * changes. */
7603 n = goodlen - badlen;
7604 if (n < -2 || n > 2)
7605 return SCORE_MAXMAX;
7606
7607 if (n > 0)
7608 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007609 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007610 ps = badsound;
7611 }
7612 else
7613 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007614 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007615 ps = goodsound;
7616 }
7617
7618 /* Skip over the identical part. */
7619 while (*pl == *ps && *pl != NUL)
7620 {
7621 ++pl;
7622 ++ps;
7623 }
7624
7625 switch (n)
7626 {
7627 case -2:
7628 case 2:
7629 /*
7630 * Must delete two characters from "pl".
7631 */
7632 ++pl; /* first delete */
7633 while (*pl == *ps)
7634 {
7635 ++pl;
7636 ++ps;
7637 }
7638 /* strings must be equal after second delete */
7639 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007640 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007641
7642 /* Failed to compare. */
7643 break;
7644
7645 case -1:
7646 case 1:
7647 /*
7648 * Minimal one delete from "pl" required.
7649 */
7650
7651 /* 1: delete */
7652 pl2 = pl + 1;
7653 ps2 = ps;
7654 while (*pl2 == *ps2)
7655 {
7656 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007657 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007658 ++pl2;
7659 ++ps2;
7660 }
7661
7662 /* 2: delete then swap, then rest must be equal */
7663 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7664 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007665 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007666
7667 /* 3: delete then substitute, then the rest must be equal */
7668 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007669 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007670
7671 /* 4: first swap then delete */
7672 if (pl[0] == ps[1] && pl[1] == ps[0])
7673 {
7674 pl2 = pl + 2; /* swap, skip two chars */
7675 ps2 = ps + 2;
7676 while (*pl2 == *ps2)
7677 {
7678 ++pl2;
7679 ++ps2;
7680 }
7681 /* delete a char and then strings must be equal */
7682 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007683 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007684 }
7685
7686 /* 5: first substitute then delete */
7687 pl2 = pl + 1; /* substitute, skip one char */
7688 ps2 = ps + 1;
7689 while (*pl2 == *ps2)
7690 {
7691 ++pl2;
7692 ++ps2;
7693 }
7694 /* delete a char and then strings must be equal */
7695 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007696 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007697
7698 /* Failed to compare. */
7699 break;
7700
7701 case 0:
7702 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007703 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007704 * insert is only possible in combination with a delete.
7705 * 1: check if for identical strings
7706 */
7707 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007708 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007709
7710 /* 2: swap */
7711 if (pl[0] == ps[1] && pl[1] == ps[0])
7712 {
7713 pl2 = pl + 2; /* swap, skip two chars */
7714 ps2 = ps + 2;
7715 while (*pl2 == *ps2)
7716 {
7717 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007718 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007719 ++pl2;
7720 ++ps2;
7721 }
7722 /* 3: swap and swap again */
7723 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7724 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007725 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007726
7727 /* 4: swap and substitute */
7728 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007729 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007730 }
7731
7732 /* 5: substitute */
7733 pl2 = pl + 1;
7734 ps2 = ps + 1;
7735 while (*pl2 == *ps2)
7736 {
7737 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007738 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007739 ++pl2;
7740 ++ps2;
7741 }
7742
7743 /* 6: substitute and swap */
7744 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7745 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007746 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007747
7748 /* 7: substitute and substitute */
7749 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007750 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007751
7752 /* 8: insert then delete */
7753 pl2 = pl;
7754 ps2 = ps + 1;
7755 while (*pl2 == *ps2)
7756 {
7757 ++pl2;
7758 ++ps2;
7759 }
7760 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007761 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007762
7763 /* 9: delete then insert */
7764 pl2 = pl + 1;
7765 ps2 = ps;
7766 while (*pl2 == *ps2)
7767 {
7768 ++pl2;
7769 ++ps2;
7770 }
7771 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007772 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007773
7774 /* Failed to compare. */
7775 break;
7776 }
7777
7778 return SCORE_MAXMAX;
7779}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007781/*
7782 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007783 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007784 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007785 * The algorithm is described by Du and Chang, 1992.
7786 * The implementation of the algorithm comes from Aspell editdist.cpp,
7787 * edit_distance(). It has been converted from C++ to C and modified to
7788 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007789 */
7790 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007791spell_edit_score(
7792 slang_T *slang,
7793 char_u *badword,
7794 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007795{
7796 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007797 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007798 int j, i;
7799 int t;
7800 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007801 int pbc, pgc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007802 char_u *p;
7803 int wbadword[MAXWLEN];
7804 int wgoodword[MAXWLEN];
7805
7806 if (has_mbyte)
7807 {
7808 /* Get the characters from the multi-byte strings and put them in an
7809 * int array for easy access. */
7810 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007811 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007812 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007813 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007814 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007815 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007816 }
7817 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007818 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007819 badlen = (int)STRLEN(badword) + 1;
7820 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007821 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007822
7823 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7824#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007825 cnt = ALLOC_MULT(int, (badlen + 1) * (goodlen + 1));
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007826 if (cnt == NULL)
7827 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007828
7829 CNT(0, 0) = 0;
7830 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007831 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832
7833 for (i = 1; i <= badlen; ++i)
7834 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007835 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007836 for (j = 1; j <= goodlen; ++j)
7837 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007838 if (has_mbyte)
7839 {
7840 bc = wbadword[i - 1];
7841 gc = wgoodword[j - 1];
7842 }
7843 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007844 {
7845 bc = badword[i - 1];
7846 gc = goodword[j - 1];
7847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 if (bc == gc)
7849 CNT(i, j) = CNT(i - 1, j - 1);
7850 else
7851 {
7852 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007853 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7855 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007856 {
7857 /* For a similar character use SCORE_SIMILAR. */
7858 if (slang != NULL
7859 && slang->sl_has_map
7860 && similar_chars(slang, gc, bc))
7861 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
7862 else
7863 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7864 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007865
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007866 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007867 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007868 if (has_mbyte)
7869 {
7870 pbc = wbadword[i - 2];
7871 pgc = wgoodword[j - 2];
7872 }
7873 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007874 {
7875 pbc = badword[i - 2];
7876 pgc = goodword[j - 2];
7877 }
7878 if (bc == pgc && pbc == gc)
7879 {
7880 t = SCORE_SWAP + CNT(i - 2, j - 2);
7881 if (t < CNT(i, j))
7882 CNT(i, j) = t;
7883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007884 }
7885 t = SCORE_DEL + CNT(i - 1, j);
7886 if (t < CNT(i, j))
7887 CNT(i, j) = t;
7888 t = SCORE_INS + CNT(i, j - 1);
7889 if (t < CNT(i, j))
7890 CNT(i, j) = t;
7891 }
7892 }
7893 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007894
7895 i = CNT(badlen - 1, goodlen - 1);
7896 vim_free(cnt);
7897 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007898}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007899
Bram Moolenaar4770d092006-01-12 23:22:24 +00007900typedef struct
7901{
7902 int badi;
7903 int goodi;
7904 int score;
7905} limitscore_T;
7906
7907/*
7908 * Like spell_edit_score(), but with a limit on the score to make it faster.
7909 * May return SCORE_MAXMAX when the score is higher than "limit".
7910 *
7911 * This uses a stack for the edits still to be tried.
7912 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
7913 * for multi-byte characters.
7914 */
7915 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007916spell_edit_score_limit(
7917 slang_T *slang,
7918 char_u *badword,
7919 char_u *goodword,
7920 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007921{
7922 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
7923 int stackidx;
7924 int bi, gi;
7925 int bi2, gi2;
7926 int bc, gc;
7927 int score;
7928 int score_off;
7929 int minscore;
7930 int round;
7931
Bram Moolenaar4770d092006-01-12 23:22:24 +00007932 /* Multi-byte characters require a bit more work, use a different function
7933 * to avoid testing "has_mbyte" quite often. */
7934 if (has_mbyte)
7935 return spell_edit_score_limit_w(slang, badword, goodword, limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007936
7937 /*
7938 * The idea is to go from start to end over the words. So long as
7939 * characters are equal just continue, this always gives the lowest score.
7940 * When there is a difference try several alternatives. Each alternative
7941 * increases "score" for the edit distance. Some of the alternatives are
7942 * pushed unto a stack and tried later, some are tried right away. At the
7943 * end of the word the score for one alternative is known. The lowest
7944 * possible score is stored in "minscore".
7945 */
7946 stackidx = 0;
7947 bi = 0;
7948 gi = 0;
7949 score = 0;
7950 minscore = limit + 1;
7951
7952 for (;;)
7953 {
7954 /* Skip over an equal part, score remains the same. */
7955 for (;;)
7956 {
7957 bc = badword[bi];
7958 gc = goodword[gi];
7959 if (bc != gc) /* stop at a char that's different */
7960 break;
7961 if (bc == NUL) /* both words end */
7962 {
7963 if (score < minscore)
7964 minscore = score;
7965 goto pop; /* do next alternative */
7966 }
7967 ++bi;
7968 ++gi;
7969 }
7970
7971 if (gc == NUL) /* goodword ends, delete badword chars */
7972 {
7973 do
7974 {
7975 if ((score += SCORE_DEL) >= minscore)
7976 goto pop; /* do next alternative */
7977 } while (badword[++bi] != NUL);
7978 minscore = score;
7979 }
7980 else if (bc == NUL) /* badword ends, insert badword chars */
7981 {
7982 do
7983 {
7984 if ((score += SCORE_INS) >= minscore)
7985 goto pop; /* do next alternative */
7986 } while (goodword[++gi] != NUL);
7987 minscore = score;
7988 }
7989 else /* both words continue */
7990 {
7991 /* If not close to the limit, perform a change. Only try changes
7992 * that may lead to a lower score than "minscore".
7993 * round 0: try deleting a char from badword
7994 * round 1: try inserting a char in badword */
7995 for (round = 0; round <= 1; ++round)
7996 {
7997 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
7998 if (score_off < minscore)
7999 {
8000 if (score_off + SCORE_EDIT_MIN >= minscore)
8001 {
8002 /* Near the limit, rest of the words must match. We
8003 * can check that right now, no need to push an item
8004 * onto the stack. */
8005 bi2 = bi + 1 - round;
8006 gi2 = gi + round;
8007 while (goodword[gi2] == badword[bi2])
8008 {
8009 if (goodword[gi2] == NUL)
8010 {
8011 minscore = score_off;
8012 break;
8013 }
8014 ++bi2;
8015 ++gi2;
8016 }
8017 }
8018 else
8019 {
8020 /* try deleting/inserting a character later */
8021 stack[stackidx].badi = bi + 1 - round;
8022 stack[stackidx].goodi = gi + round;
8023 stack[stackidx].score = score_off;
8024 ++stackidx;
8025 }
8026 }
8027 }
8028
8029 if (score + SCORE_SWAP < minscore)
8030 {
8031 /* If swapping two characters makes a match then the
8032 * substitution is more expensive, thus there is no need to
8033 * try both. */
8034 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8035 {
8036 /* Swap two characters, that is: skip them. */
8037 gi += 2;
8038 bi += 2;
8039 score += SCORE_SWAP;
8040 continue;
8041 }
8042 }
8043
8044 /* Substitute one character for another which is the same
8045 * thing as deleting a character from both goodword and badword.
8046 * Use a better score when there is only a case difference. */
8047 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8048 score += SCORE_ICASE;
8049 else
8050 {
8051 /* For a similar character use SCORE_SIMILAR. */
8052 if (slang != NULL
8053 && slang->sl_has_map
8054 && similar_chars(slang, gc, bc))
8055 score += SCORE_SIMILAR;
8056 else
8057 score += SCORE_SUBST;
8058 }
8059
8060 if (score < minscore)
8061 {
8062 /* Do the substitution. */
8063 ++gi;
8064 ++bi;
8065 continue;
8066 }
8067 }
8068pop:
8069 /*
8070 * Get here to try the next alternative, pop it from the stack.
8071 */
8072 if (stackidx == 0) /* stack is empty, finished */
8073 break;
8074
8075 /* pop an item from the stack */
8076 --stackidx;
8077 gi = stack[stackidx].goodi;
8078 bi = stack[stackidx].badi;
8079 score = stack[stackidx].score;
8080 }
8081
8082 /* When the score goes over "limit" it may actually be much higher.
8083 * Return a very large number to avoid going below the limit when giving a
8084 * bonus. */
8085 if (minscore > limit)
8086 return SCORE_MAXMAX;
8087 return minscore;
8088}
8089
Bram Moolenaar4770d092006-01-12 23:22:24 +00008090/*
8091 * Multi-byte version of spell_edit_score_limit().
8092 * Keep it in sync with the above!
8093 */
8094 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008095spell_edit_score_limit_w(
8096 slang_T *slang,
8097 char_u *badword,
8098 char_u *goodword,
8099 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008100{
8101 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8102 int stackidx;
8103 int bi, gi;
8104 int bi2, gi2;
8105 int bc, gc;
8106 int score;
8107 int score_off;
8108 int minscore;
8109 int round;
8110 char_u *p;
8111 int wbadword[MAXWLEN];
8112 int wgoodword[MAXWLEN];
8113
8114 /* Get the characters from the multi-byte strings and put them in an
8115 * int array for easy access. */
8116 bi = 0;
8117 for (p = badword; *p != NUL; )
8118 wbadword[bi++] = mb_cptr2char_adv(&p);
8119 wbadword[bi++] = 0;
8120 gi = 0;
8121 for (p = goodword; *p != NUL; )
8122 wgoodword[gi++] = mb_cptr2char_adv(&p);
8123 wgoodword[gi++] = 0;
8124
8125 /*
8126 * The idea is to go from start to end over the words. So long as
8127 * characters are equal just continue, this always gives the lowest score.
8128 * When there is a difference try several alternatives. Each alternative
8129 * increases "score" for the edit distance. Some of the alternatives are
8130 * pushed unto a stack and tried later, some are tried right away. At the
8131 * end of the word the score for one alternative is known. The lowest
8132 * possible score is stored in "minscore".
8133 */
8134 stackidx = 0;
8135 bi = 0;
8136 gi = 0;
8137 score = 0;
8138 minscore = limit + 1;
8139
8140 for (;;)
8141 {
8142 /* Skip over an equal part, score remains the same. */
8143 for (;;)
8144 {
8145 bc = wbadword[bi];
8146 gc = wgoodword[gi];
8147
8148 if (bc != gc) /* stop at a char that's different */
8149 break;
8150 if (bc == NUL) /* both words end */
8151 {
8152 if (score < minscore)
8153 minscore = score;
8154 goto pop; /* do next alternative */
8155 }
8156 ++bi;
8157 ++gi;
8158 }
8159
8160 if (gc == NUL) /* goodword ends, delete badword chars */
8161 {
8162 do
8163 {
8164 if ((score += SCORE_DEL) >= minscore)
8165 goto pop; /* do next alternative */
8166 } while (wbadword[++bi] != NUL);
8167 minscore = score;
8168 }
8169 else if (bc == NUL) /* badword ends, insert badword chars */
8170 {
8171 do
8172 {
8173 if ((score += SCORE_INS) >= minscore)
8174 goto pop; /* do next alternative */
8175 } while (wgoodword[++gi] != NUL);
8176 minscore = score;
8177 }
8178 else /* both words continue */
8179 {
8180 /* If not close to the limit, perform a change. Only try changes
8181 * that may lead to a lower score than "minscore".
8182 * round 0: try deleting a char from badword
8183 * round 1: try inserting a char in badword */
8184 for (round = 0; round <= 1; ++round)
8185 {
8186 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8187 if (score_off < minscore)
8188 {
8189 if (score_off + SCORE_EDIT_MIN >= minscore)
8190 {
8191 /* Near the limit, rest of the words must match. We
8192 * can check that right now, no need to push an item
8193 * onto the stack. */
8194 bi2 = bi + 1 - round;
8195 gi2 = gi + round;
8196 while (wgoodword[gi2] == wbadword[bi2])
8197 {
8198 if (wgoodword[gi2] == NUL)
8199 {
8200 minscore = score_off;
8201 break;
8202 }
8203 ++bi2;
8204 ++gi2;
8205 }
8206 }
8207 else
8208 {
8209 /* try deleting a character from badword later */
8210 stack[stackidx].badi = bi + 1 - round;
8211 stack[stackidx].goodi = gi + round;
8212 stack[stackidx].score = score_off;
8213 ++stackidx;
8214 }
8215 }
8216 }
8217
8218 if (score + SCORE_SWAP < minscore)
8219 {
8220 /* If swapping two characters makes a match then the
8221 * substitution is more expensive, thus there is no need to
8222 * try both. */
8223 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8224 {
8225 /* Swap two characters, that is: skip them. */
8226 gi += 2;
8227 bi += 2;
8228 score += SCORE_SWAP;
8229 continue;
8230 }
8231 }
8232
8233 /* Substitute one character for another which is the same
8234 * thing as deleting a character from both goodword and badword.
8235 * Use a better score when there is only a case difference. */
8236 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8237 score += SCORE_ICASE;
8238 else
8239 {
8240 /* For a similar character use SCORE_SIMILAR. */
8241 if (slang != NULL
8242 && slang->sl_has_map
8243 && similar_chars(slang, gc, bc))
8244 score += SCORE_SIMILAR;
8245 else
8246 score += SCORE_SUBST;
8247 }
8248
8249 if (score < minscore)
8250 {
8251 /* Do the substitution. */
8252 ++gi;
8253 ++bi;
8254 continue;
8255 }
8256 }
8257pop:
8258 /*
8259 * Get here to try the next alternative, pop it from the stack.
8260 */
8261 if (stackidx == 0) /* stack is empty, finished */
8262 break;
8263
8264 /* pop an item from the stack */
8265 --stackidx;
8266 gi = stack[stackidx].goodi;
8267 bi = stack[stackidx].badi;
8268 score = stack[stackidx].score;
8269 }
8270
8271 /* When the score goes over "limit" it may actually be much higher.
8272 * Return a very large number to avoid going below the limit when giving a
8273 * bonus. */
8274 if (minscore > limit)
8275 return SCORE_MAXMAX;
8276 return minscore;
8277}
Bram Moolenaar4770d092006-01-12 23:22:24 +00008278
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008279/*
8280 * ":spellinfo"
8281 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008282 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008283ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008284{
8285 int lpi;
8286 langp_T *lp;
8287 char_u *p;
8288
8289 if (no_spell_checking(curwin))
8290 return;
8291
8292 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008293 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008294 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008295 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008296 msg_puts("file: ");
8297 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008298 msg_putchar('\n');
8299 p = lp->lp_slang->sl_info;
8300 if (p != NULL)
8301 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008302 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008303 msg_putchar('\n');
8304 }
8305 }
8306 msg_end();
8307}
8308
Bram Moolenaar4770d092006-01-12 23:22:24 +00008309#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8310#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008311#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008312#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8313#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008314
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008315/*
8316 * ":spelldump"
8317 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008319ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008320{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008321 char_u *spl;
8322 long dummy;
8323
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008324 if (no_spell_checking(curwin))
8325 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008326 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008327
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008328 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008329 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008330
8331 /* enable spelling locally in the new window */
8332 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008333 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008334 vim_free(spl);
8335
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008336 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008337 return;
8338
Bram Moolenaar860cae12010-06-05 23:22:07 +02008339 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008340
8341 /* Delete the empty line that we started with. */
8342 if (curbuf->b_ml.ml_line_count > 1)
8343 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8344
8345 redraw_later(NOT_VALID);
8346}
8347
8348/*
8349 * Go through all possible words and:
8350 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8351 * "ic" and "dir" are not used.
8352 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8353 */
8354 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008355spell_dump_compl(
8356 char_u *pat, /* leading part of the word */
8357 int ic, /* ignore case */
8358 int *dir, /* direction for adding matches */
8359 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008360{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008361 langp_T *lp;
8362 slang_T *slang;
8363 idx_T arridx[MAXWLEN];
8364 int curi[MAXWLEN];
8365 char_u word[MAXWLEN];
8366 int c;
8367 char_u *byts;
8368 idx_T *idxs;
8369 linenr_T lnum = 0;
8370 int round;
8371 int depth;
8372 int n;
8373 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008374 char_u *region_names = NULL; /* region names being used */
8375 int do_region = TRUE; /* dump region names and numbers */
8376 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008377 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008378 int dumpflags = dumpflags_arg;
8379 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008380
Bram Moolenaard0131a82006-03-04 21:46:13 +00008381 /* When ignoring case or when the pattern starts with capital pass this on
8382 * to dump_word(). */
8383 if (pat != NULL)
8384 {
8385 if (ic)
8386 dumpflags |= DUMPFLAG_ICASE;
8387 else
8388 {
8389 n = captype(pat, NULL);
8390 if (n == WF_ONECAP)
8391 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01008392 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00008393 dumpflags |= DUMPFLAG_ALLCAP;
8394 }
8395 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008396
Bram Moolenaar7887d882005-07-01 22:33:52 +00008397 /* Find out if we can support regions: All languages must support the same
8398 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008399 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008400 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008401 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008402 p = lp->lp_slang->sl_regions;
8403 if (p[0] != 0)
8404 {
8405 if (region_names == NULL) /* first language with regions */
8406 region_names = p;
8407 else if (STRCMP(region_names, p) != 0)
8408 {
8409 do_region = FALSE; /* region names are different */
8410 break;
8411 }
8412 }
8413 }
8414
8415 if (do_region && region_names != NULL)
8416 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008417 if (pat == NULL)
8418 {
8419 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8420 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8421 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008422 }
8423 else
8424 do_region = FALSE;
8425
8426 /*
8427 * Loop over all files loaded for the entries in 'spelllang'.
8428 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008429 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008430 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008431 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008432 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008433 if (slang->sl_fbyts == NULL) /* reloading failed */
8434 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008435
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008436 if (pat == NULL)
8437 {
8438 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8439 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8440 }
8441
8442 /* When matching with a pattern and there are no prefixes only use
8443 * parts of the tree that match "pat". */
8444 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008445 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008446 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008447 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008448
8449 /* round 1: case-folded tree
8450 * round 2: keep-case tree */
8451 for (round = 1; round <= 2; ++round)
8452 {
8453 if (round == 1)
8454 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008455 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008456 byts = slang->sl_fbyts;
8457 idxs = slang->sl_fidxs;
8458 }
8459 else
8460 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008461 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008462 byts = slang->sl_kbyts;
8463 idxs = slang->sl_kidxs;
8464 }
8465 if (byts == NULL)
8466 continue; /* array is empty */
8467
8468 depth = 0;
8469 arridx[0] = 0;
8470 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008471 while (depth >= 0 && !got_int
Bram Moolenaar7591bb32019-03-30 13:53:47 +01008472 && (pat == NULL || !ins_compl_interrupted()))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008473 {
8474 if (curi[depth] > byts[arridx[depth]])
8475 {
8476 /* Done all bytes at this node, go up one level. */
8477 --depth;
8478 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008479 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008480 }
8481 else
8482 {
8483 /* Do one more byte at this node. */
8484 n = arridx[depth] + curi[depth];
8485 ++curi[depth];
8486 c = byts[n];
8487 if (c == 0)
8488 {
8489 /* End of word, deal with the word.
8490 * Don't use keep-case words in the fold-case tree,
8491 * they will appear in the keep-case tree.
8492 * Only use the word when the region matches. */
8493 flags = (int)idxs[n];
8494 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008495 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008496 && (do_region
8497 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008498 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008499 & lp->lp_region) != 0))
8500 {
8501 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008502 if (!do_region)
8503 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008504
8505 /* Dump the basic word if there is no prefix or
8506 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008507 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008508 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008509 {
8510 dump_word(slang, word, pat, dir,
8511 dumpflags, flags, lnum);
8512 if (pat == NULL)
8513 ++lnum;
8514 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008515
8516 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008517 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008518 lnum = dump_prefixes(slang, word, pat, dir,
8519 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008520 }
8521 }
8522 else
8523 {
8524 /* Normal char, go one level deeper. */
8525 word[depth++] = c;
8526 arridx[depth] = idxs[n];
8527 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008528
8529 /* Check if this characters matches with the pattern.
8530 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008531 * Always ignore case here, dump_word() will check
8532 * proper case later. This isn't exactly right when
8533 * length changes for multi-byte characters with
8534 * ignore case... */
8535 if (depth <= patlen
8536 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008537 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008538 }
8539 }
8540 }
8541 }
8542 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008543}
8544
8545/*
8546 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008547 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008548 */
8549 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008550dump_word(
8551 slang_T *slang,
8552 char_u *word,
8553 char_u *pat,
8554 int *dir,
8555 int dumpflags,
8556 int wordflags,
8557 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008558{
8559 int keepcap = FALSE;
8560 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008561 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008562 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008563 char_u badword[MAXWLEN + 10];
8564 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008565 int flags = wordflags;
8566
8567 if (dumpflags & DUMPFLAG_ONECAP)
8568 flags |= WF_ONECAP;
8569 if (dumpflags & DUMPFLAG_ALLCAP)
8570 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008571
Bram Moolenaar4770d092006-01-12 23:22:24 +00008572 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008573 {
8574 /* Need to fix case according to "flags". */
8575 make_case_word(word, cword, flags);
8576 p = cword;
8577 }
8578 else
8579 {
8580 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008581 if ((dumpflags & DUMPFLAG_KEEPCASE)
8582 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008583 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008584 keepcap = TRUE;
8585 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008586 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008587
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008588 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008589 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008590 /* Add flags and regions after a slash. */
8591 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008592 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008593 STRCPY(badword, p);
8594 STRCAT(badword, "/");
8595 if (keepcap)
8596 STRCAT(badword, "=");
8597 if (flags & WF_BANNED)
8598 STRCAT(badword, "!");
8599 else if (flags & WF_RARE)
8600 STRCAT(badword, "?");
8601 if (flags & WF_REGION)
8602 for (i = 0; i < 7; ++i)
8603 if (flags & (0x10000 << i))
8604 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8605 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008606 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008607
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008608 if (dumpflags & DUMPFLAG_COUNT)
8609 {
8610 hashitem_T *hi;
8611
8612 /* Include the word count for ":spelldump!". */
8613 hi = hash_find(&slang->sl_wordcount, tw);
8614 if (!HASHITEM_EMPTY(hi))
8615 {
8616 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8617 tw, HI2WC(hi)->wc_count);
8618 p = IObuff;
8619 }
8620 }
8621
8622 ml_append(lnum, p, (colnr_T)0, FALSE);
8623 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008624 else if (((dumpflags & DUMPFLAG_ICASE)
8625 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8626 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008627 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaard9eefe32019-04-06 14:22:21 +02008628 p_ic, NULL, *dir, FALSE) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008629 /* if dir was BACKWARD then honor it just once */
8630 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008631}
8632
8633/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008634 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8635 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008636 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008637 * Return the updated line number.
8638 */
8639 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008640dump_prefixes(
8641 slang_T *slang,
8642 char_u *word, /* case-folded word */
8643 char_u *pat,
8644 int *dir,
8645 int dumpflags,
8646 int flags, /* flags with prefix ID */
8647 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008648{
8649 idx_T arridx[MAXWLEN];
8650 int curi[MAXWLEN];
8651 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008652 char_u word_up[MAXWLEN];
8653 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008654 int c;
8655 char_u *byts;
8656 idx_T *idxs;
8657 linenr_T lnum = startlnum;
8658 int depth;
8659 int n;
8660 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008661 int i;
8662
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008663 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008664 * upper-case letter in word_up[]. */
8665 c = PTR2CHAR(word);
8666 if (SPELL_TOUPPER(c) != c)
8667 {
8668 onecap_copy(word, word_up, TRUE);
8669 has_word_up = TRUE;
8670 }
8671
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008672 byts = slang->sl_pbyts;
8673 idxs = slang->sl_pidxs;
8674 if (byts != NULL) /* array not is empty */
8675 {
8676 /*
8677 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008678 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008679 */
8680 depth = 0;
8681 arridx[0] = 0;
8682 curi[0] = 1;
8683 while (depth >= 0 && !got_int)
8684 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008685 n = arridx[depth];
8686 len = byts[n];
8687 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008688 {
8689 /* Done all bytes at this node, go up one level. */
8690 --depth;
8691 line_breakcheck();
8692 }
8693 else
8694 {
8695 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008696 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008697 ++curi[depth];
8698 c = byts[n];
8699 if (c == 0)
8700 {
8701 /* End of prefix, find out how many IDs there are. */
8702 for (i = 1; i < len; ++i)
8703 if (byts[n + i] != 0)
8704 break;
8705 curi[depth] += i - 1;
8706
Bram Moolenaar53805d12005-08-01 07:08:33 +00008707 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8708 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008709 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008710 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008711 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008712 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008713 : flags, lnum);
8714 if (lnum != 0)
8715 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008716 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008717
8718 /* Check for prefix that matches the word when the
8719 * first letter is upper-case, but only if the prefix has
8720 * a condition. */
8721 if (has_word_up)
8722 {
8723 c = valid_word_prefix(i, n, flags, word_up, slang,
8724 TRUE);
8725 if (c != 0)
8726 {
8727 vim_strncpy(prefix + depth, word_up,
8728 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008729 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008730 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008731 : flags, lnum);
8732 if (lnum != 0)
8733 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008734 }
8735 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008736 }
8737 else
8738 {
8739 /* Normal char, go one level deeper. */
8740 prefix[depth++] = c;
8741 arridx[depth] = idxs[n];
8742 curi[depth] = 1;
8743 }
8744 }
8745 }
8746 }
8747
8748 return lnum;
8749}
8750
Bram Moolenaar95529562005-08-25 21:21:38 +00008751/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008752 * Move "p" to the end of word "start".
8753 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008754 */
8755 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008756spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008757{
8758 char_u *p = start;
8759
Bram Moolenaar860cae12010-06-05 23:22:07 +02008760 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008761 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008762 return p;
8763}
8764
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008765#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008766/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008767 * For Insert mode completion CTRL-X s:
8768 * Find start of the word in front of column "startcol".
8769 * We don't check if it is badly spelled, with completion we can only change
8770 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008771 * Returns the column number of the word.
8772 */
8773 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008774spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008775{
8776 char_u *line;
8777 char_u *p;
8778 int col = 0;
8779
Bram Moolenaar95529562005-08-25 21:21:38 +00008780 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008781 return startcol;
8782
8783 /* Find a word character before "startcol". */
8784 line = ml_get_curline();
8785 for (p = line + startcol; p > line; )
8786 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008787 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008788 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008789 break;
8790 }
8791
8792 /* Go back to start of the word. */
8793 while (p > line)
8794 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008795 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008796 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008797 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008798 break;
8799 col = 0;
8800 }
8801
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008802 return col;
8803}
8804
8805/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008806 * Need to check for 'spellcapcheck' now, the word is removed before
8807 * expand_spelling() is called. Therefore the ugly global variable.
8808 */
8809static int spell_expand_need_cap;
8810
8811 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008812spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00008813{
8814 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
8815}
8816
8817/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008818 * Get list of spelling suggestions.
8819 * Used for Insert mode completion CTRL-X ?.
8820 * Returns the number of matches. The matches are in "matchp[]", array of
8821 * allocated strings.
8822 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008823 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008824expand_spelling(
8825 linenr_T lnum UNUSED,
8826 char_u *pat,
8827 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008828{
8829 garray_T ga;
8830
Bram Moolenaar4770d092006-01-12 23:22:24 +00008831 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008832 *matchp = ga.ga_data;
8833 return ga.ga_len;
8834}
8835#endif
8836
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008837#endif /* FEAT_SPELL */