blob: 59e73682b555c4e1447cadddea410c3b5ac426c3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaare19defe2005-03-21 08:23:33 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020013 * See spellfile.c for the Vim spell file format.
14 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000015 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
16 * has a list of bytes that can appear (siblings). For each byte there is a
17 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000018 *
19 * A NUL byte is used where the word may end. The bytes are sorted, so that
20 * binary searching can be used and the NUL bytes are at the start. The
21 * number of possible bytes is stored before the list of bytes.
22 *
23 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
24 * either the next index or flags. The tree starts at index 0. For example,
25 * to lookup "vi" this sequence is followed:
26 * i = 0
27 * len = byts[i]
28 * n = where "v" appears in byts[i + 1] to byts[i + len]
29 * i = idxs[n]
30 * len = byts[i]
31 * n = where "i" appears in byts[i + 1] to byts[i + len]
32 * i = idxs[n]
33 * len = byts[i]
34 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000036 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000037 * original case. The second one is only used for keep-case words and is
38 * usually small.
39 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000040 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000041 * generating the .spl file. This tree stores all the possible prefixes, as
42 * if they were words. At each word (prefix) end the prefix nr is stored, the
43 * following word must support this prefix nr. And the condition nr is
44 * stored, used to lookup the condition that the word must match with.
45 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000046 * Thanks to Olaf Seibert for providing an example implementation of this tree
47 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000048 * LZ trie ideas:
49 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
50 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
52 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000053 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000054 * Why doesn't Vim use aspell/ispell/myspell/etc.?
55 * See ":help develop-spell".
56 */
57
Bram Moolenaar51485f02005-06-04 21:55:20 +000058/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000059 * Use this to adjust the score after finding suggestions, based on the
60 * suggested word sounding like the bad word. This is much faster than doing
61 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000062 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
63 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000064 * Used when 'spellsuggest' is set to "best".
65 */
66#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
67
68/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000069 * Do the opposite: based on a maximum end score and a known sound score,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000070 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000071 */
72#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
73
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020074#define IN_SPELL_C
Bram Moolenaare19defe2005-03-21 08:23:33 +000075#include "vim.h"
76
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000077#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +000078
Bram Moolenaar4770d092006-01-12 23:22:24 +000079#ifndef UNIX /* it's in os_unix.h for Unix */
80# include <time.h> /* for time_t */
81#endif
82
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000083/* only used for su_badflags */
84#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
85
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000086#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +000087
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000088#define REGION_ALL 0xff /* word valid in all regions */
89
Bram Moolenaar4770d092006-01-12 23:22:24 +000090#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
91#define VIMSUGMAGICL 6
92#define VIMSUGVERSION 1
93
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000094/* Result values. Lower number is accepted over higher one. */
95#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000096#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000097#define SP_RARE 1
98#define SP_LOCAL 2
99#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000100
Bram Moolenaar4770d092006-01-12 23:22:24 +0000101typedef struct wordcount_S
102{
103 short_u wc_count; /* nr of times word was seen */
104 char_u wc_word[1]; /* word, actually longer */
105} wordcount_T;
106
Bram Moolenaar84026842016-07-17 20:37:43 +0200107#define WC_KEY_OFF offsetof(wordcount_T, wc_word)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000108#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
109#define MAXWORDCOUNT 0xffff
110
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000111/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000112 * Information used when looking for suggestions.
113 */
114typedef struct suginfo_S
115{
116 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000117 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000118 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000119 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000120 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000121 char_u *su_badptr; /* start of bad word in line */
122 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000123 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000124 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
125 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000126 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000127 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000128 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000129} suginfo_T;
130
131/* One word suggestion. Used in "si_ga". */
132typedef struct suggest_S
133{
134 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000135 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000136 int st_orglen; /* length of replaced text */
137 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000138 int st_altscore; /* used when st_score compares equal */
139 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000140 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000141 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000142} suggest_T;
143
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000144#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000145
Bram Moolenaar4770d092006-01-12 23:22:24 +0000146/* TRUE if a word appears in the list of banned words. */
147#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
148
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000149/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000150 * what is displayed, because when rescore_suggestions() is called the score
151 * may change and wrong suggestions may be removed later. */
152#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000153
154/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
155 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000156#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000157
158/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000159#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000160#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000161#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000162#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000163#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000164#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000165#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000166#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000167#define SCORE_SUBST 93 /* substitute a character */
168#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000169#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000170#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000171#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000172#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000173#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000174#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000175#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000176#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000177
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000178#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000179#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
180 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000181
Bram Moolenaar4770d092006-01-12 23:22:24 +0000182#define SCORE_COMMON1 30 /* subtracted for words seen before */
183#define SCORE_COMMON2 40 /* subtracted for words often seen */
184#define SCORE_COMMON3 50 /* subtracted for words very often seen */
185#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
186#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
187
188/* When trying changed soundfold words it becomes slow when trying more than
189 * two changes. With less then two changes it's slightly faster but we miss a
190 * few good suggestions. In rare cases we need to try three of four changes.
191 */
192#define SCORE_SFMAX1 200 /* maximum score for first try */
193#define SCORE_SFMAX2 300 /* maximum score for second try */
194#define SCORE_SFMAX3 400 /* maximum score for third try */
195
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000196#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000197#define SCORE_MAXMAX 999999 /* accept any score */
198#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
199
200/* for spell_edit_score_limit() we need to know the minimum value of
201 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
202#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000203
204/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000205 * Structure to store info for word matching.
206 */
207typedef struct matchinf_S
208{
209 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000210
211 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000212 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000213 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000214 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000215 char_u *mi_cend; /* char after what was used for
216 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000217
218 /* case-folded text */
219 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000220 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000221
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000222 /* for when checking word after a prefix */
223 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000225 int mi_prefcnt; /* number of entries at mi_prefarridx */
226 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000227 int mi_cprefixlen; /* byte length of prefix in original
228 case */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000229
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 /* for when checking a compound word */
231 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000232 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
233 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000234 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000235
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000236 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000237 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200239 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000240
241 /* for NOBREAK */
242 int mi_result2; /* "mi_resul" without following word */
243 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000244} matchinf_T;
245
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000246
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100247static int spell_iswordp(char_u *p, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100248static int spell_mb_isword_class(int cl, win_T *wp);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000249
250/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000251 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000252 */
253typedef enum
254{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000255 STATE_START = 0, /* At start of node check for NUL bytes (goodword
256 * ends); if badword ends there is a match, otherwise
257 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000258 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000259 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000260 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
261 STATE_PLAIN, /* Use each byte of the node. */
262 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000263 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000264 STATE_INS, /* Insert a byte in the bad word. */
265 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000266 STATE_UNSWAP, /* Undo swap two characters. */
267 STATE_SWAP3, /* Swap two characters over three. */
268 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000269 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000270 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000271 STATE_REP_INI, /* Prepare for using REP items. */
272 STATE_REP, /* Use matching REP items from the .aff file. */
273 STATE_REP_UNDO, /* Undo a REP item replacement. */
274 STATE_FINAL /* End of this node. */
275} state_T;
276
277/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000278 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000279 */
280typedef struct trystate_S
281{
Bram Moolenaarea424162005-06-16 21:51:00 +0000282 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000283 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000284 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000285 short ts_curi; /* index in list of child nodes */
286 char_u ts_fidx; /* index in fword[], case-folded bad word */
287 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
288 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000289 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000290 * PFD_PREFIXTREE or PFD_NOPREFIX */
291 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000292 char_u ts_tcharlen; /* number of bytes in tword character */
293 char_u ts_tcharidx; /* current byte index in tword character */
294 char_u ts_isdiff; /* DIFF_ values */
295 char_u ts_fcharstart; /* index in fword where badword char started */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000296 char_u ts_prewordlen; /* length of word in "preword[]" */
297 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000298 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000299 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000300 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000301 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000302 char_u ts_delidx; /* index in fword for char that was deleted,
303 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304} trystate_T;
305
Bram Moolenaarea424162005-06-16 21:51:00 +0000306/* values for ts_isdiff */
307#define DIFF_NONE 0 /* no different byte (yet) */
308#define DIFF_YES 1 /* different byte found */
309#define DIFF_INSERT 2 /* inserting character */
310
Bram Moolenaard12a1322005-08-21 22:08:24 +0000311/* values for ts_flags */
312#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
313#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000314#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000315
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000316/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000317#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000318#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000319#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000321/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000322#define FIND_FOLDWORD 0 /* find word case-folded */
323#define FIND_KEEPWORD 1 /* find keep-case word */
324#define FIND_PREFIX 2 /* find word after prefix */
325#define FIND_COMPOUND 3 /* find case-folded compound word */
326#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000327
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100328static void find_word(matchinf_T *mip, int mode);
329static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap);
330static int can_compound(slang_T *slang, char_u *word, char_u *flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100331static int match_compoundrule(slang_T *slang, char_u *compflags);
332static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req);
333static void find_prefix(matchinf_T *mip, int mode);
334static int fold_more(matchinf_T *mip);
335static int spell_valid_case(int wordflags, int treeflags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100336static void spell_load_cb(char_u *fname, void *cookie);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100337static int count_syllables(slang_T *slang, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100338static void clear_midword(win_T *buf);
339static void use_midword(slang_T *lp, win_T *buf);
340static int find_region(char_u *rp, char_u *region);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100341static int check_need_cap(linenr_T lnum, colnr_T col);
342static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000343#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100344static void spell_suggest_expr(suginfo_T *su, char_u *expr);
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000345#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100346static void spell_suggest_file(suginfo_T *su, char_u *fname);
347static void spell_suggest_intern(suginfo_T *su, int interactive);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100348static void spell_find_cleanup(suginfo_T *su);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100349static void suggest_try_special(suginfo_T *su);
350static void suggest_try_change(suginfo_T *su);
351static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold);
352static void go_deeper(trystate_T *stack, int depth, int score_add);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100353static int nofold_len(char_u *fword, int flen, char_u *word);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100354static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword);
355static void score_comp_sal(suginfo_T *su);
356static void score_combine(suginfo_T *su);
357static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound);
358static void suggest_try_soundalike_prep(void);
359static void suggest_try_soundalike(suginfo_T *su);
360static void suggest_try_soundalike_finish(void);
361static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp);
362static int soundfold_find(slang_T *slang, char_u *word);
363static void make_case_word(char_u *fword, char_u *cword, int flags);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100364static int similar_chars(slang_T *slang, int c1, int c2);
365static void add_suggestion(suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf);
366static void check_suggestions(suginfo_T *su, garray_T *gap);
367static void add_banned(suginfo_T *su, char_u *word);
368static void rescore_suggestions(suginfo_T *su);
369static void rescore_one(suginfo_T *su, suggest_T *stp);
370static int cleanup_suggestions(garray_T *gap, int maxscore, int keep);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100371static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res);
372static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100373static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100374static int soundalike_score(char_u *goodsound, char_u *badsound);
375static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword);
376static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100377static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100378static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum);
379static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000381
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000382/* Remember what "z?" replaced. */
383static char_u *repl_from = NULL;
384static char_u *repl_to = NULL;
385
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000386/*
387 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000388 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000389 * "*attrp" is set to the highlight index for a badly spelled word. For a
390 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000391 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000393 * "capcol" is used to check for a Capitalised word after the end of a
394 * sentence. If it's zero then perform the check. Return the column where to
395 * check next, or -1 when no sentence end was found. If it's NULL then don't
396 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000397 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000398 * Returns the length of the word in bytes, also when it's OK, so that the
399 * caller can skip over the word.
400 */
401 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100402spell_check(
403 win_T *wp, /* current window */
404 char_u *ptr,
405 hlf_T *attrp,
406 int *capcol, /* column to check for Capital */
407 int docount) /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000408{
409 matchinf_T mi; /* Most things are put in "mi" so that it can
410 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000411 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000412 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000413 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000414 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +0000415 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000416
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000417 /* A word never starts at a space or a control character. Return quickly
418 * then, skipping over the character. */
419 if (*ptr <= ' ')
420 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000421
422 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200423 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000424 return 1;
425
Bram Moolenaar5195e452005-08-19 20:32:47 +0000426 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000428 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000429 * 0X99FF. But always do check spelling to find "3GPP" and "11
430 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000432 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +0100433 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
434 mi.mi_end = skipbin(ptr + 2);
435 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
Bram Moolenaar3982c542005-06-08 21:56:31 +0000436 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000437 else
438 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000439 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000440 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000441
Bram Moolenaar0c405862005-06-22 22:26:26 +0000442 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000443 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000444 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +0200445 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000446 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000447 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000448 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100449 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200450 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000451
Bram Moolenaar860cae12010-06-05 23:22:07 +0200452 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000453 {
454 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000455 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000456 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000457 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000458 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000459 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000460 if (capcol != NULL)
461 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000462
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463 /* We always use the characters up to the next non-word character,
464 * also for bad words. */
465 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000466
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200468 mi.mi_capflags = 0;
469 mi.mi_cend = NULL;
470 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471
Bram Moolenaar5195e452005-08-19 20:32:47 +0000472 /* case-fold the word with one non-word character, so that we can check
473 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474 if (*mi.mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100475 MB_PTR_ADV(mi.mi_fend);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000476
477 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
478 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000479 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000480
481 /* The word is bad unless we recognize it. */
482 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000483 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000484
485 /*
486 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000487 * We check them all, because a word may be matched longer in another
488 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000489 */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200490 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200492 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000493
494 /* If reloading fails the language is still in the list but everything
495 * has been cleared. */
496 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
497 continue;
498
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000499 /* Check for a matching word in case-folded words. */
500 find_word(&mi, FIND_FOLDWORD);
501
502 /* Check for a matching word in keep-case words. */
503 find_word(&mi, FIND_KEEPWORD);
504
505 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000506 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000507
508 /* For a NOBREAK language, may want to use a word without a following
509 * word as a backup. */
510 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
511 && mi.mi_result2 != SP_BAD)
512 {
513 mi.mi_result = mi.mi_result2;
514 mi.mi_end = mi.mi_end2;
515 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516
517 /* Count the word in the first language where it's found to be OK. */
518 if (count_word && mi.mi_result == SP_OK)
519 {
520 count_common_word(mi.mi_lp->lp_slang, ptr,
521 (int)(mi.mi_end - ptr), 1);
522 count_word = FALSE;
523 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524 }
525
526 if (mi.mi_result != SP_OK)
527 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000528 /* If we found a number skip over it. Allows for "42nd". Do flag
529 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000530 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000531 {
532 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
533 return nrlen;
534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000535
536 /* When we are at a non-word character there is no error, just
537 * skip over the character (try looking for a word after it). */
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100538 else if (!spell_iswordp_nmw(ptr, wp))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539 {
Bram Moolenaar860cae12010-06-05 23:22:07 +0200540 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000541 {
542 regmatch_T regmatch;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100543 int r;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000544
545 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200546 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000547 regmatch.rm_ic = FALSE;
Bram Moolenaardffa5b82014-11-19 16:38:07 +0100548 r = vim_regexec(&regmatch, ptr, 0);
549 wp->w_s->b_cap_prog = regmatch.regprog;
550 if (r)
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000551 *capcol = (int)(regmatch.endp[0] - ptr);
552 }
553
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000554 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000555 return (*mb_ptr2len)(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000556 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000557 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000558 else if (mi.mi_end == ptr)
559 /* Always include at least one character. Required for when there
560 * is a mixup in "midword". */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100561 MB_PTR_ADV(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000562 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +0200563 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +0000564 {
565 char_u *p, *fp;
566 int save_result = mi.mi_result;
567
568 /* First language in 'spelllang' is NOBREAK. Find first position
569 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200570 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000571 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000572 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000573 p = mi.mi_word;
574 fp = mi.mi_fword;
575 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000576 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100577 MB_PTR_ADV(p);
578 MB_PTR_ADV(fp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000579 if (p >= mi.mi_end)
580 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000581 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000582 find_word(&mi, FIND_COMPOUND);
583 if (mi.mi_result != SP_BAD)
584 {
585 mi.mi_end = p;
586 break;
587 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000588 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000589 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +0000590 }
Bram Moolenaar78622822005-08-23 21:00:13 +0000591 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592
593 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000594 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000595 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000596 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000598 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000599 }
600
Bram Moolenaar5195e452005-08-19 20:32:47 +0000601 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
602 {
603 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000604 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000605 return wrongcaplen;
606 }
607
Bram Moolenaar51485f02005-06-04 21:55:20 +0000608 return (int)(mi.mi_end - ptr);
609}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000610
Bram Moolenaar51485f02005-06-04 21:55:20 +0000611/*
612 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000613 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
614 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
615 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
616 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000617 *
618 * For a match mip->mi_result is updated.
619 */
620 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100621find_word(matchinf_T *mip, int mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000622{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000624 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000625 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000626 int endidxcnt = 0;
627 int len;
628 int wlen = 0;
629 int flen;
630 int c;
631 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000632 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000633 char_u *s;
Bram Moolenaare52325c2005-08-22 22:54:29 +0000634 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000635 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000636 slang_T *slang = mip->mi_lp->lp_slang;
637 unsigned flags;
638 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000639 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000640 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000641 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +0000642 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000643
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000644 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000645 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000646 /* Check for word with matching case in keep-case tree. */
647 ptr = mip->mi_word;
648 flen = 9999; /* no case folding, always enough bytes */
649 byts = slang->sl_kbyts;
650 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000651
652 if (mode == FIND_KEEPCOMPOUND)
653 /* Skip over the previously found word(s). */
654 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000655 }
656 else
657 {
658 /* Check for case-folded in case-folded tree. */
659 ptr = mip->mi_fword;
660 flen = mip->mi_fwordlen; /* available case-folded bytes */
661 byts = slang->sl_fbyts;
662 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000663
664 if (mode == FIND_PREFIX)
665 {
666 /* Skip over the prefix. */
667 wlen = mip->mi_prefixlen;
668 flen -= mip->mi_prefixlen;
669 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670 else if (mode == FIND_COMPOUND)
671 {
672 /* Skip over the previously found word(s). */
673 wlen = mip->mi_compoff;
674 flen -= mip->mi_compoff;
675 }
676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677 }
678
Bram Moolenaar51485f02005-06-04 21:55:20 +0000679 if (byts == NULL)
680 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000681
Bram Moolenaar51485f02005-06-04 21:55:20 +0000682 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000683 * Repeat advancing in the tree until:
684 * - there is a byte that doesn't match,
685 * - we reach the end of the tree,
686 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000687 */
688 for (;;)
689 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000690 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000691 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692
693 len = byts[arridx++];
694
695 /* If the first possible byte is a zero the word could end here.
696 * Remember this index, we first check for the longest word. */
697 if (byts[arridx] == 0)
698 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000699 if (endidxcnt == MAXWLEN)
700 {
701 /* Must be a corrupted spell file. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100702 emsg(_(e_format));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000703 return;
704 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 endlen[endidxcnt] = wlen;
706 endidx[endidxcnt++] = arridx++;
707 --len;
708
709 /* Skip over the zeros, there can be several flag/region
710 * combinations. */
711 while (len > 0 && byts[arridx] == 0)
712 {
713 ++arridx;
714 --len;
715 }
716 if (len == 0)
717 break; /* no children, word must end here */
718 }
719
720 /* Stop looking at end of the line. */
721 if (ptr[wlen] == NUL)
722 break;
723
724 /* Perform a binary search in the list of accepted bytes. */
725 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000726 if (c == TAB) /* <Tab> is handled like <Space> */
727 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000728 lo = arridx;
729 hi = arridx + len - 1;
730 while (lo < hi)
731 {
732 m = (lo + hi) / 2;
733 if (byts[m] > c)
734 hi = m - 1;
735 else if (byts[m] < c)
736 lo = m + 1;
737 else
738 {
739 lo = hi = m;
740 break;
741 }
742 }
743
744 /* Stop if there is no matching byte. */
745 if (hi < lo || byts[lo] != c)
746 break;
747
748 /* Continue at the child (if there is one). */
749 arridx = idxs[lo];
750 ++wlen;
751 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +0000752
753 /* One space in the good word may stand for several spaces in the
754 * checked word. */
755 if (c == ' ')
756 {
757 for (;;)
758 {
759 if (flen <= 0 && *mip->mi_fend != NUL)
760 flen = fold_more(mip);
761 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
762 break;
763 ++wlen;
764 --flen;
765 }
766 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000767 }
768
769 /*
770 * Verify that one of the possible endings is valid. Try the longest
771 * first.
772 */
773 while (endidxcnt > 0)
774 {
775 --endidxcnt;
776 arridx = endidx[endidxcnt];
777 wlen = endlen[endidxcnt];
778
Bram Moolenaar51485f02005-06-04 21:55:20 +0000779 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
780 continue; /* not at first byte of character */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200781 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000782 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000783 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000784 continue; /* next char is a word character */
785 word_ends = FALSE;
786 }
787 else
788 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000789 /* The prefix flag is before compound flags. Once a valid prefix flag
790 * has been found we try compound flags. */
791 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000792
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000793 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000794 {
795 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000796 * when folding case. This can be slow, take a shortcut when the
797 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000798 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000799 if (STRNCMP(ptr, p, wlen) != 0)
800 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100801 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
802 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000803 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000804 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000805 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000806
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000807 /* Check flags and region. For FIND_PREFIX check the condition and
808 * prefix ID.
809 * Repeat this if there are more flags/region alternatives until there
810 * is a match. */
811 res = SP_BAD;
812 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
813 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000814 {
815 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000816
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000817 /* For the fold-case tree check that the case of the checked word
818 * matches with what the word in the tree requires.
819 * For keep-case tree the case is always right. For prefixes we
820 * don't bother to check. */
821 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000823 if (mip->mi_cend != mip->mi_word + wlen)
824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000825 /* mi_capflags was set for a different word length, need
826 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000827 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000828 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000829 }
830
Bram Moolenaar0c405862005-06-22 22:26:26 +0000831 if (mip->mi_capflags == WF_KEEPCAP
832 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 }
835
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000836 /* When mode is FIND_PREFIX the word must support the prefix:
837 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000838 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000839 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000840 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000841 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000842 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +0000843 mip->mi_word + mip->mi_cprefixlen, slang,
844 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000845 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000846 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000847
848 /* Use the WF_RARE flag for a rare prefix. */
849 if (c & WF_RAREPFX)
850 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000851 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000852 }
853
Bram Moolenaar78622822005-08-23 21:00:13 +0000854 if (slang->sl_nobreak)
855 {
856 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
857 && (flags & WF_BANNED) == 0)
858 {
859 /* NOBREAK: found a valid following word. That's all we
860 * need to know, so return. */
861 mip->mi_result = SP_OK;
862 break;
863 }
864 }
865
866 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
867 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000868 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +0000869 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870 * COMPOUNDMIN reject it quickly.
871 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000872 * that's too short... Myspell compatibility requires this
873 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000874 if (((unsigned)flags >> 24) == 0
875 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000876 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000877 /* For multi-byte chars check character length against
878 * COMPOUNDMIN. */
879 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000880 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000881 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
882 wlen - mip->mi_compoff) < slang->sl_compminlen)
883 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000884
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000885 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +0000886 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000887 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
888 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +0000889 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000890 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000891
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000892 /* Don't allow compounding on a side where an affix was added,
893 * unless COMPOUNDPERMITFLAG was used. */
894 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
895 continue;
896 if (!word_ends && (flags & WF_NOCOMPAFT))
897 continue;
898
Bram Moolenaard12a1322005-08-21 22:08:24 +0000899 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000900 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +0000901 ? slang->sl_compstartflags
902 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +0000903 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000904 continue;
905
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000906 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
907 * discard the compound word. */
908 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
909 continue;
910
Bram Moolenaare52325c2005-08-22 22:54:29 +0000911 if (mode == FIND_COMPOUND)
912 {
913 int capflags;
914
915 /* Need to check the caps type of the appended compound
916 * word. */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000917 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
918 mip->mi_compoff) != 0)
919 {
920 /* case folding may have changed the length */
921 p = mip->mi_word;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100922 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s))
923 MB_PTR_ADV(p);
Bram Moolenaare52325c2005-08-22 22:54:29 +0000924 }
925 else
Bram Moolenaare52325c2005-08-22 22:54:29 +0000926 p = mip->mi_word + mip->mi_compoff;
927 capflags = captype(p, mip->mi_word + wlen);
928 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
929 && (flags & WF_FIXCAP) != 0))
930 continue;
931
932 if (capflags != WF_ALLCAP)
933 {
934 /* When the character before the word is a word
935 * character we do not accept a Onecap word. We do
936 * accept a no-caps word, even when the dictionary
937 * word specifies ONECAP. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100938 MB_PTR_BACK(mip->mi_word, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +0100939 if (spell_iswordp_nmw(p, mip->mi_win)
Bram Moolenaare52325c2005-08-22 22:54:29 +0000940 ? capflags == WF_ONECAP
941 : (flags & WF_ONECAP) != 0
942 && capflags != WF_ONECAP)
943 continue;
944 }
945 }
946
Bram Moolenaar5195e452005-08-19 20:32:47 +0000947 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000948 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +0000949 * the number of syllables must not be too large. */
950 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
951 mip->mi_compflags[mip->mi_complen + 1] = NUL;
952 if (word_ends)
953 {
954 char_u fword[MAXWLEN];
955
956 if (slang->sl_compsylmax < MAXWLEN)
957 {
958 /* "fword" is only needed for checking syllables. */
959 if (ptr == mip->mi_word)
960 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
961 else
962 vim_strncpy(fword, ptr, endlen[endidxcnt]);
963 }
964 if (!can_compound(slang, fword, mip->mi_compflags))
965 continue;
966 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000967 else if (slang->sl_comprules != NULL
968 && !match_compoundrule(slang, mip->mi_compflags))
969 /* The compound flags collected so far do not match any
970 * COMPOUNDRULE, discard the compounded word. */
971 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000972 }
973
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000974 /* Check NEEDCOMPOUND: can't use word without compounding. */
975 else if (flags & WF_NEEDCOMP)
976 continue;
977
Bram Moolenaar78622822005-08-23 21:00:13 +0000978 nobreak_result = SP_OK;
979
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000980 if (!word_ends)
981 {
Bram Moolenaar78622822005-08-23 21:00:13 +0000982 int save_result = mip->mi_result;
983 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000984 langp_T *save_lp = mip->mi_lp;
985 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +0000986
987 /* Check that a valid word follows. If there is one and we
988 * are compounding, it will set "mi_result", thus we are
989 * always finished here. For NOBREAK we only check that a
990 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000991 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +0000992 if (slang->sl_nobreak)
993 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000994
995 /* Find following word in case-folded tree. */
996 mip->mi_compoff = endlen[endidxcnt];
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000997 if (has_mbyte && mode == FIND_KEEPWORD)
998 {
999 /* Compute byte length in case-folded word from "wlen":
1000 * byte length in keep-case word. Length may change when
1001 * folding case. This can be slow, take a shortcut when
1002 * the case-folded word is equal to the keep-case word. */
1003 p = mip->mi_fword;
1004 if (STRNCMP(ptr, p, wlen) != 0)
1005 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001006 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s))
1007 MB_PTR_ADV(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001008 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001009 }
1010 }
Bram Moolenaarba534352016-04-21 09:20:26 +02001011#if 0 /* Disabled, see below */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001012 c = mip->mi_compoff;
Bram Moolenaarba534352016-04-21 09:20:26 +02001013#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001014 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001015 if (flags & WF_COMPROOT)
1016 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001017
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001018 /* For NOBREAK we need to try all NOBREAK languages, at least
1019 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001020 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001021 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001022 if (slang->sl_nobreak)
1023 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001024 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001025 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1026 || !mip->mi_lp->lp_slang->sl_nobreak)
1027 continue;
1028 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001029
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001030 find_word(mip, FIND_COMPOUND);
1031
1032 /* When NOBREAK any word that matches is OK. Otherwise we
1033 * need to find the longest match, thus try with keep-case
1034 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001035 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1036 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001037 /* Find following word in keep-case tree. */
1038 mip->mi_compoff = wlen;
1039 find_word(mip, FIND_KEEPCOMPOUND);
1040
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001041#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1042 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1043 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001044 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1045 {
1046 /* Check for following word with prefix. */
1047 mip->mi_compoff = c;
1048 find_prefix(mip, FIND_COMPOUND);
1049 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001050#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001051 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001052
1053 if (!slang->sl_nobreak)
1054 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001055 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001056 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001057 if (flags & WF_COMPROOT)
1058 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001059 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001060
Bram Moolenaar78622822005-08-23 21:00:13 +00001061 if (slang->sl_nobreak)
1062 {
1063 nobreak_result = mip->mi_result;
1064 mip->mi_result = save_result;
1065 mip->mi_end = save_end;
1066 }
1067 else
1068 {
1069 if (mip->mi_result == SP_OK)
1070 break;
1071 continue;
1072 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001073 }
1074
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001075 if (flags & WF_BANNED)
1076 res = SP_BANNED;
1077 else if (flags & WF_REGION)
1078 {
1079 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001080 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001081 res = SP_OK;
1082 else
1083 res = SP_LOCAL;
1084 }
1085 else if (flags & WF_RARE)
1086 res = SP_RARE;
1087 else
1088 res = SP_OK;
1089
Bram Moolenaar78622822005-08-23 21:00:13 +00001090 /* Always use the longest match and the best result. For NOBREAK
1091 * we separately keep the longest match without a following good
1092 * word as a fall-back. */
1093 if (nobreak_result == SP_BAD)
1094 {
1095 if (mip->mi_result2 > res)
1096 {
1097 mip->mi_result2 = res;
1098 mip->mi_end2 = mip->mi_word + wlen;
1099 }
1100 else if (mip->mi_result2 == res
1101 && mip->mi_end2 < mip->mi_word + wlen)
1102 mip->mi_end2 = mip->mi_word + wlen;
1103 }
1104 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001105 {
1106 mip->mi_result = res;
1107 mip->mi_end = mip->mi_word + wlen;
1108 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001109 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001110 mip->mi_end = mip->mi_word + wlen;
1111
Bram Moolenaar78622822005-08-23 21:00:13 +00001112 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001114 }
1115
Bram Moolenaar78622822005-08-23 21:00:13 +00001116 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001117 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001119}
1120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001121/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001122 * Return TRUE if there is a match between the word ptr[wlen] and
1123 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1124 * word.
1125 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1126 * end of ptr[wlen] and the second part matches after it.
1127 */
1128 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001129match_checkcompoundpattern(
1130 char_u *ptr,
1131 int wlen,
1132 garray_T *gap) /* &sl_comppat */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001133{
1134 int i;
1135 char_u *p;
1136 int len;
1137
1138 for (i = 0; i + 1 < gap->ga_len; i += 2)
1139 {
1140 p = ((char_u **)gap->ga_data)[i + 1];
1141 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1142 {
1143 /* Second part matches at start of following compound word, now
1144 * check if first part matches at end of previous word. */
1145 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001146 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001147 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1148 return TRUE;
1149 }
1150 }
1151 return FALSE;
1152}
1153
1154/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001155 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1156 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001157 */
1158 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001159can_compound(slang_T *slang, char_u *word, char_u *flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001160{
Bram Moolenaar6de68532005-08-24 22:08:48 +00001161 char_u uflags[MAXWLEN * 2];
1162 int i;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001163 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001164
1165 if (slang->sl_compprog == NULL)
1166 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001167 if (enc_utf8)
1168 {
1169 /* Need to convert the single byte flags to utf8 characters. */
1170 p = uflags;
1171 for (i = 0; flags[i] != NUL; ++i)
Bram Moolenaarace95982017-03-29 17:30:27 +02001172 p += utf_char2bytes(flags[i], p);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001173 *p = NUL;
1174 p = uflags;
1175 }
1176 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00001177 p = flags;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001178 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001179 return FALSE;
1180
Bram Moolenaare52325c2005-08-22 22:54:29 +00001181 /* Count the number of syllables. This may be slow, do it last. If there
1182 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001183 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001184 if (slang->sl_compsylmax < MAXWLEN
1185 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001186 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001187 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001188}
1189
1190/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001191 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1192 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1193 * lines if they don't contain wildcards.
1194 */
1195 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001196can_be_compound(
1197 trystate_T *sp,
1198 slang_T *slang,
1199 char_u *compflags,
1200 int flag)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001201{
1202 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1203 * then it can't possibly compound. */
1204 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1205 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1206 return FALSE;
1207
1208 /* If there are no wildcards, we can check if the flags collected so far
1209 * possibly can form a match with COMPOUNDRULE patterns. This only
1210 * makes sense when we have two or more words. */
1211 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1212 {
1213 int v;
1214
1215 compflags[sp->ts_complen] = flag;
1216 compflags[sp->ts_complen + 1] = NUL;
1217 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1218 compflags[sp->ts_complen] = NUL;
1219 return v;
1220 }
1221
1222 return TRUE;
1223}
1224
1225
1226/*
1227 * Return TRUE if the compound flags in compflags[] match the start of any
1228 * compound rule. This is used to stop trying a compound if the flags
1229 * collected so far can't possibly match any compound rule.
1230 * Caller must check that slang->sl_comprules is not NULL.
1231 */
1232 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001233match_compoundrule(slang_T *slang, char_u *compflags)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001234{
1235 char_u *p;
1236 int i;
1237 int c;
1238
1239 /* loop over all the COMPOUNDRULE entries */
1240 for (p = slang->sl_comprules; *p != NUL; ++p)
1241 {
1242 /* loop over the flags in the compound word we have made, match
1243 * them against the current rule entry */
1244 for (i = 0; ; ++i)
1245 {
1246 c = compflags[i];
1247 if (c == NUL)
1248 /* found a rule that matches for the flags we have so far */
1249 return TRUE;
1250 if (*p == '/' || *p == NUL)
1251 break; /* end of rule, it's too short */
1252 if (*p == '[')
1253 {
1254 int match = FALSE;
1255
1256 /* compare against all the flags in [] */
1257 ++p;
1258 while (*p != ']' && *p != NUL)
1259 if (*p++ == c)
1260 match = TRUE;
1261 if (!match)
1262 break; /* none matches */
1263 }
1264 else if (*p != c)
1265 break; /* flag of word doesn't match flag in pattern */
1266 ++p;
1267 }
1268
1269 /* Skip to the next "/", where the next pattern starts. */
1270 p = vim_strchr(p, '/');
1271 if (p == NULL)
1272 break;
1273 }
1274
1275 /* Checked all the rules and none of them match the flags, so there
1276 * can't possibly be a compound starting with these flags. */
1277 return FALSE;
1278}
1279
1280/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001281 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1282 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001283 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001284 */
1285 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001286valid_word_prefix(
1287 int totprefcnt, /* nr of prefix IDs */
1288 int arridx, /* idx in sl_pidxs[] */
1289 int flags,
1290 char_u *word,
1291 slang_T *slang,
1292 int cond_req) /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001293{
1294 int prefcnt;
1295 int pidx;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001296 regprog_T **rp;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001297 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001298
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001299 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001300 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1301 {
1302 pidx = slang->sl_pidxs[arridx + prefcnt];
1303
1304 /* Check the prefix ID. */
1305 if (prefid != (pidx & 0xff))
1306 continue;
1307
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001308 /* Check if the prefix doesn't combine and the word already has a
1309 * suffix. */
1310 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1311 continue;
1312
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001313 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001314 * stored in the two bytes above the prefix ID byte. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001315 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
1316 if (*rp != NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001317 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01001318 if (!vim_regexec_prog(rp, FALSE, word, 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001319 continue;
1320 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001321 else if (cond_req)
1322 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001323
Bram Moolenaar53805d12005-08-01 07:08:33 +00001324 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001325 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001326 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001327 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001328}
1329
1330/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001331 * Check if the word at "mip->mi_word" has a matching prefix.
1332 * If it does, then check the following word.
1333 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001334 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1335 * prefix in a compound word.
1336 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001337 * For a match mip->mi_result is updated.
1338 */
1339 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001340find_prefix(matchinf_T *mip, int mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001341{
1342 idx_T arridx = 0;
1343 int len;
1344 int wlen = 0;
1345 int flen;
1346 int c;
1347 char_u *ptr;
1348 idx_T lo, hi, m;
1349 slang_T *slang = mip->mi_lp->lp_slang;
1350 char_u *byts;
1351 idx_T *idxs;
1352
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001353 byts = slang->sl_pbyts;
1354 if (byts == NULL)
1355 return; /* array is empty */
1356
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001357 /* We use the case-folded word here, since prefixes are always
1358 * case-folded. */
1359 ptr = mip->mi_fword;
1360 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001361 if (mode == FIND_COMPOUND)
1362 {
1363 /* Skip over the previously found word(s). */
1364 ptr += mip->mi_compoff;
1365 flen -= mip->mi_compoff;
1366 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001367 idxs = slang->sl_pidxs;
1368
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001369 /*
1370 * Repeat advancing in the tree until:
1371 * - there is a byte that doesn't match,
1372 * - we reach the end of the tree,
1373 * - or we reach the end of the line.
1374 */
1375 for (;;)
1376 {
1377 if (flen == 0 && *mip->mi_fend != NUL)
1378 flen = fold_more(mip);
1379
1380 len = byts[arridx++];
1381
1382 /* If the first possible byte is a zero the prefix could end here.
1383 * Check if the following word matches and supports the prefix. */
1384 if (byts[arridx] == 0)
1385 {
1386 /* There can be several prefixes with different conditions. We
1387 * try them all, since we don't know which one will give the
1388 * longest match. The word is the same each time, pass the list
1389 * of possible prefixes to find_word(). */
1390 mip->mi_prefarridx = arridx;
1391 mip->mi_prefcnt = len;
1392 while (len > 0 && byts[arridx] == 0)
1393 {
1394 ++arridx;
1395 --len;
1396 }
1397 mip->mi_prefcnt -= len;
1398
1399 /* Find the word that comes after the prefix. */
1400 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001401 if (mode == FIND_COMPOUND)
1402 /* Skip over the previously found word(s). */
1403 mip->mi_prefixlen += mip->mi_compoff;
1404
Bram Moolenaar53805d12005-08-01 07:08:33 +00001405 if (has_mbyte)
1406 {
1407 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001408 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1409 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001410 }
1411 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001412 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 find_word(mip, FIND_PREFIX);
1414
1415
1416 if (len == 0)
1417 break; /* no children, word must end here */
1418 }
1419
1420 /* Stop looking at end of the line. */
1421 if (ptr[wlen] == NUL)
1422 break;
1423
1424 /* Perform a binary search in the list of accepted bytes. */
1425 c = ptr[wlen];
1426 lo = arridx;
1427 hi = arridx + len - 1;
1428 while (lo < hi)
1429 {
1430 m = (lo + hi) / 2;
1431 if (byts[m] > c)
1432 hi = m - 1;
1433 else if (byts[m] < c)
1434 lo = m + 1;
1435 else
1436 {
1437 lo = hi = m;
1438 break;
1439 }
1440 }
1441
1442 /* Stop if there is no matching byte. */
1443 if (hi < lo || byts[lo] != c)
1444 break;
1445
1446 /* Continue at the child (if there is one). */
1447 arridx = idxs[lo];
1448 ++wlen;
1449 --flen;
1450 }
1451}
1452
1453/*
1454 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001455 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001456 * Return the length of the folded chars in bytes.
1457 */
1458 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001459fold_more(matchinf_T *mip)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460{
1461 int flen;
1462 char_u *p;
1463
1464 p = mip->mi_fend;
1465 do
1466 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001467 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001468 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001470 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001471 if (*mip->mi_fend != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001472 MB_PTR_ADV(mip->mi_fend);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473
1474 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1475 mip->mi_fword + mip->mi_fwordlen,
1476 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001477 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001478 mip->mi_fwordlen += flen;
1479 return flen;
1480}
1481
1482/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001483 * Check case flags for a word. Return TRUE if the word has the requested
1484 * case.
1485 */
1486 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001487spell_valid_case(
1488 int wordflags, /* flags for the checked word. */
1489 int treeflags) /* flags for the word in the spell tree */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001491 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001492 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001493 && ((treeflags & WF_ONECAP) == 0
1494 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495}
1496
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001497/*
1498 * Return TRUE if spell checking is not enabled.
1499 */
1500 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001501no_spell_checking(win_T *wp)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001502{
Bram Moolenaar860cae12010-06-05 23:22:07 +02001503 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
1504 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001506 emsg(_("E756: Spell checking is not enabled"));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001507 return TRUE;
1508 }
1509 return FALSE;
1510}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001511
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001512/*
1513 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001514 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1515 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001516 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1517 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001518 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001519 */
1520 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001521spell_move_to(
1522 win_T *wp,
1523 int dir, /* FORWARD or BACKWARD */
1524 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
1525 int curline,
1526 hlf_T *attrp) /* return: attributes of bad word or NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001527 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001528{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001529 linenr_T lnum;
1530 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001531 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001532 char_u *line;
1533 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001534 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001535 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001536 int len;
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001537#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001538 int has_syntax = syntax_present(wp);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001539#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001540 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001541 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001542 char_u *buf = NULL;
1543 int buflen = 0;
1544 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001545 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001546 int found_one = FALSE;
1547 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001548
Bram Moolenaar95529562005-08-25 21:21:38 +00001549 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001550 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001551
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001552 /*
1553 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00001554 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001555 *
1556 * When searching backwards, we continue in the line to find the last
1557 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001558 *
1559 * We concatenate the start of the next line, so that wrapped words work
1560 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1561 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001562 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001563 lnum = wp->w_cursor.lnum;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001564 CLEAR_POS(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001565
1566 while (!got_int)
1567 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001568 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001569
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001570 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001571 if (buflen < len + MAXWLEN + 2)
1572 {
1573 vim_free(buf);
1574 buflen = len + MAXWLEN + 2;
1575 buf = alloc(buflen);
1576 if (buf == NULL)
1577 break;
1578 }
1579
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001580 /* In first line check first word for Capital. */
1581 if (lnum == 1)
1582 capcol = 0;
1583
1584 /* For checking first word with a capital skip white space. */
1585 if (capcol == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001586 capcol = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001587 else if (curline && wp == curwin)
1588 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001589 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001590 col = getwhitecols(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001591 if (check_need_cap(lnum, col))
1592 capcol = col;
1593
1594 /* Need to get the line again, may have looked at the previous
1595 * one. */
1596 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
1597 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001598
Bram Moolenaar0c405862005-06-22 22:26:26 +00001599 /* Copy the line into "buf" and append the start of the next line if
1600 * possible. */
1601 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001602 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00001603 spell_cat_line(buf + STRLEN(buf),
1604 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001605
1606 p = buf + skip;
1607 endp = buf + len;
1608 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001609 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001610 /* When searching backward don't search after the cursor. Unless
1611 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001612 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001613 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001614 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001615 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001616 break;
1617
1618 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001619 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001620 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001621
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001622 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001623 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001624 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001625 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001626 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001627 /* When searching forward only accept a bad word after
1628 * the cursor. */
1629 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001630 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001631 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001632 && (wrapped
1633 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001634 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001635 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636 {
Bram Moolenaar34b466e2013-11-28 17:41:46 +01001637#ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00001638 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001639 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001640 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001641 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00001642 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001643 if (!can_spell)
1644 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001645 }
1646 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001647#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001648 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001649
Bram Moolenaar51485f02005-06-04 21:55:20 +00001650 if (can_spell)
1651 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00001652 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001653 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001654 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001655#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001656 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001657#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001658 if (dir == FORWARD)
1659 {
1660 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001661 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001662 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001663 if (attrp != NULL)
1664 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001665 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001666 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001667 else if (curline)
1668 /* Insert mode completion: put cursor after
1669 * the bad word. */
1670 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001671 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001672 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001673 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00001674 else
1675 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001677 }
1678
Bram Moolenaar51485f02005-06-04 21:55:20 +00001679 /* advance to character after the word */
1680 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001681 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001682 }
1683
Bram Moolenaar5195e452005-08-19 20:32:47 +00001684 if (dir == BACKWARD && found_pos.lnum != 0)
1685 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001686 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001687 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001688 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001689 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001690 }
1691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001692 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001693 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001694
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001695 /* If we are back at the starting line and searched it again there
1696 * is no match, give up. */
1697 if (lnum == wp->w_cursor.lnum && wrapped)
1698 break;
1699
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001700 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001701 if (dir == BACKWARD)
1702 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001703 if (lnum > 1)
1704 --lnum;
1705 else if (!p_ws)
1706 break; /* at first line and 'nowrapscan' */
1707 else
1708 {
1709 /* Wrap around to the end of the buffer. May search the
1710 * starting line again and accept the last match. */
1711 lnum = wp->w_buffer->b_ml.ml_line_count;
1712 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001713 if (!shortmess(SHM_SEARCH))
1714 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001715 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001716 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001717 }
1718 else
1719 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001720 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1721 ++lnum;
1722 else if (!p_ws)
1723 break; /* at first line and 'nowrapscan' */
1724 else
1725 {
1726 /* Wrap around to the start of the buffer. May search the
1727 * starting line again and accept the first match. */
1728 lnum = 1;
1729 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001730 if (!shortmess(SHM_SEARCH))
1731 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001732 }
1733
1734 /* If we are back at the starting line and there is no match then
1735 * give up. */
Bram Moolenaard3f78dc2017-02-25 14:21:10 +01001736 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001737 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001738
1739 /* Skip the characters at the start of the next line that were
1740 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001741 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001742 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001743 else
1744 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001745
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001746 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001747 --capcol;
1748
1749 /* But after empty line check first word in next line */
1750 if (*skipwhite(line) == NUL)
1751 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001752 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001753
1754 line_breakcheck();
1755 }
1756
Bram Moolenaar0c405862005-06-22 22:26:26 +00001757 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001758 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001759}
1760
1761/*
1762 * For spell checking: concatenate the start of the following line "line" into
1763 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001764 * Keep the blanks at the start of the next line, this is used in win_line()
1765 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00001766 */
1767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001768spell_cat_line(char_u *buf, char_u *line, int maxlen)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001769{
1770 char_u *p;
1771 int n;
1772
1773 p = skipwhite(line);
1774 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1775 p = skipwhite(p + 1);
1776
1777 if (*p != NUL)
1778 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00001779 /* Only worth concatenating if there is something else than spaces to
1780 * concatenate. */
1781 n = (int)(p - line) + 1;
1782 if (n < maxlen - 1)
1783 {
1784 vim_memset(buf, ' ', n);
1785 vim_strncpy(buf + n, p, maxlen - 1 - n);
1786 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00001787 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001788}
1789
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001790/*
1791 * Structure used for the cookie argument of do_in_runtimepath().
1792 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001793typedef struct spelload_S
1794{
1795 char_u sl_lang[MAXWLEN + 1]; /* language name */
1796 slang_T *sl_slang; /* resulting slang_T struct */
1797 int sl_nobreak; /* NOBREAK language found */
1798} spelload_T;
1799
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001800/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001801 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001802 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001803 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001804 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001805spell_load_lang(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001807 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001808 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001809 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001810 int round;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811
Bram Moolenaarb765d632005-06-07 21:00:02 +00001812 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001813 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001814 STRCPY(sl.sl_lang, lang);
1815 sl.sl_slang = NULL;
1816 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001817
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001818 /* We may retry when no spell file is found for the language, an
1819 * autocommand may load it then. */
1820 for (round = 1; round <= 2; ++round)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001821 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001822 /*
1823 * Find the first spell file for "lang" in 'runtimepath' and load it.
1824 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001825 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001826#ifdef VMS
1827 "spell/%s_%s.spl",
1828#else
1829 "spell/%s.%s.spl",
1830#endif
1831 lang, spell_enc());
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001832 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001833
1834 if (r == FAIL && *sl.sl_lang != NUL)
1835 {
1836 /* Try loading the ASCII version. */
1837 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01001838#ifdef VMS
1839 "spell/%s_ascii.spl",
1840#else
1841 "spell/%s.ascii.spl",
1842#endif
1843 lang);
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001844 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001845
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001846 if (r == FAIL && *sl.sl_lang != NUL && round == 1
1847 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
1848 curbuf->b_fname, FALSE, curbuf))
1849 continue;
1850 break;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001851 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001852 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001853 }
1854
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001855 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001857 smsg(
Bram Moolenaar56f78042010-12-08 17:09:32 +01001858#ifdef VMS
1859 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
1860#else
1861 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1862#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001863 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00001864 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001865 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001866 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001867 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001868 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01001869 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001870 }
1871}
1872
1873/*
1874 * Return the encoding used for spell checking: Use 'encoding', except that we
1875 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1876 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001877 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001878spell_enc(void)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001879{
1880
Bram Moolenaarb765d632005-06-07 21:00:02 +00001881 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1882 return p_enc;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001883 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001884}
1885
1886/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001887 * Get the name of the .spl file for the internal wordlist into
1888 * "fname[MAXPATHL]".
1889 */
1890 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001891int_wordlist_spl(char_u *fname)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001892{
Bram Moolenaar56f78042010-12-08 17:09:32 +01001893 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001894 int_wordlist, spell_enc());
1895}
1896
1897/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001898 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001899 * Caller must fill "sl_next".
1900 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001901 slang_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001902slang_alloc(char_u *lang)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903{
1904 slang_T *lp;
1905
Bram Moolenaar51485f02005-06-04 21:55:20 +00001906 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001907 if (lp != NULL)
1908 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001909 if (lang != NULL)
1910 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001911 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001912 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001913 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001914 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001915 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001916 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001917
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001918 return lp;
1919}
1920
1921/*
1922 * Free the contents of an slang_T and the structure itself.
1923 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001924 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001925slang_free(slang_T *lp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001926{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001927 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001928 vim_free(lp->sl_fname);
1929 slang_clear(lp);
1930 vim_free(lp);
1931}
1932
1933/*
1934 * Clear an slang_T so that the file can be reloaded.
1935 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001936 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001937slang_clear(slang_T *lp)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001938{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001939 garray_T *gap;
1940 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001941 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001942 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001943 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001944
Bram Moolenaard23a8232018-02-10 18:45:26 +01001945 VIM_CLEAR(lp->sl_fbyts);
1946 VIM_CLEAR(lp->sl_kbyts);
1947 VIM_CLEAR(lp->sl_pbyts);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001948
Bram Moolenaard23a8232018-02-10 18:45:26 +01001949 VIM_CLEAR(lp->sl_fidxs);
1950 VIM_CLEAR(lp->sl_kidxs);
1951 VIM_CLEAR(lp->sl_pidxs);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001952
Bram Moolenaar4770d092006-01-12 23:22:24 +00001953 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001954 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00001955 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
1956 while (gap->ga_len > 0)
1957 {
1958 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1959 vim_free(ftp->ft_from);
1960 vim_free(ftp->ft_to);
1961 }
1962 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001963 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001964
1965 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001966 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001967 {
1968 /* "ga_len" is set to 1 without adding an item for latin1 */
1969 if (gap->ga_data != NULL)
1970 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1971 for (i = 0; i < gap->ga_len; ++i)
1972 vim_free(((int **)gap->ga_data)[i]);
1973 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001974 else
1975 /* SAL items: free salitem_T items */
1976 while (gap->ga_len > 0)
1977 {
1978 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1979 vim_free(smp->sm_lead);
1980 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1981 vim_free(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001982 vim_free(smp->sm_lead_w);
1983 vim_free(smp->sm_oneof_w);
1984 vim_free(smp->sm_to_w);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001985 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001986 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001987
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001988 for (i = 0; i < lp->sl_prefixcnt; ++i)
Bram Moolenaar473de612013-06-08 18:19:48 +02001989 vim_regfree(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001990 lp->sl_prefixcnt = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001991 VIM_CLEAR(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001992
Bram Moolenaard23a8232018-02-10 18:45:26 +01001993 VIM_CLEAR(lp->sl_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001994
Bram Moolenaard23a8232018-02-10 18:45:26 +01001995 VIM_CLEAR(lp->sl_midword);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001996
Bram Moolenaar473de612013-06-08 18:19:48 +02001997 vim_regfree(lp->sl_compprog);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001998 lp->sl_compprog = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001999 VIM_CLEAR(lp->sl_comprules);
2000 VIM_CLEAR(lp->sl_compstartflags);
2001 VIM_CLEAR(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002002
Bram Moolenaard23a8232018-02-10 18:45:26 +01002003 VIM_CLEAR(lp->sl_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002004 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002005
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002006 ga_clear_strings(&lp->sl_comppat);
2007
Bram Moolenaar4770d092006-01-12 23:22:24 +00002008 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2009 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002010
Bram Moolenaar4770d092006-01-12 23:22:24 +00002011 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002012
Bram Moolenaar4770d092006-01-12 23:22:24 +00002013 /* Clear info from .sug file. */
2014 slang_clear_sug(lp);
2015
Bram Moolenaar5195e452005-08-19 20:32:47 +00002016 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002017 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002018 lp->sl_compsylmax = MAXWLEN;
2019 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002020}
2021
2022/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002023 * Clear the info from the .sug file in "lp".
2024 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002025 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002026slang_clear_sug(slang_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002027{
Bram Moolenaard23a8232018-02-10 18:45:26 +01002028 VIM_CLEAR(lp->sl_sbyts);
2029 VIM_CLEAR(lp->sl_sidxs);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002030 close_spellbuf(lp->sl_sugbuf);
2031 lp->sl_sugbuf = NULL;
2032 lp->sl_sugloaded = FALSE;
2033 lp->sl_sugtime = 0;
2034}
2035
2036/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002037 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002038 * Invoked through do_in_runtimepath().
2039 */
2040 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002041spell_load_cb(char_u *fname, void *cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002042{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002043 spelload_T *slp = (spelload_T *)cookie;
2044 slang_T *slang;
2045
2046 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2047 if (slang != NULL)
2048 {
2049 /* When a previously loaded file has NOBREAK also use it for the
2050 * ".add" files. */
2051 if (slp->sl_nobreak && slang->sl_add)
2052 slang->sl_nobreak = TRUE;
2053 else if (slang->sl_nobreak)
2054 slp->sl_nobreak = TRUE;
2055
2056 slp->sl_slang = slang;
2057 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002058}
2059
Bram Moolenaar4770d092006-01-12 23:22:24 +00002060
2061/*
2062 * Add a word to the hashtable of common words.
2063 * If it's already there then the counter is increased.
2064 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002065 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002066count_common_word(
2067 slang_T *lp,
2068 char_u *word,
2069 int len, /* word length, -1 for upto NUL */
2070 int count) /* 1 to count once, 10 to init */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002071{
2072 hash_T hash;
2073 hashitem_T *hi;
2074 wordcount_T *wc;
2075 char_u buf[MAXWLEN];
2076 char_u *p;
2077
2078 if (len == -1)
2079 p = word;
2080 else
2081 {
2082 vim_strncpy(buf, word, len);
2083 p = buf;
2084 }
2085
2086 hash = hash_hash(p);
2087 hi = hash_lookup(&lp->sl_wordcount, p, hash);
2088 if (HASHITEM_EMPTY(hi))
2089 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002090 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00002091 if (wc == NULL)
2092 return;
2093 STRCPY(wc->wc_word, p);
2094 wc->wc_count = count;
2095 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
2096 }
2097 else
2098 {
2099 wc = HI2WC(hi);
2100 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
2101 wc->wc_count = MAXWORDCOUNT;
2102 }
2103}
2104
2105/*
2106 * Adjust the score of common words.
2107 */
2108 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002109score_wordcount_adj(
2110 slang_T *slang,
2111 int score,
2112 char_u *word,
2113 int split) /* word was split, less bonus */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002114{
2115 hashitem_T *hi;
2116 wordcount_T *wc;
2117 int bonus;
2118 int newscore;
2119
2120 hi = hash_find(&slang->sl_wordcount, word);
2121 if (!HASHITEM_EMPTY(hi))
2122 {
2123 wc = HI2WC(hi);
2124 if (wc->wc_count < SCORE_THRES2)
2125 bonus = SCORE_COMMON1;
2126 else if (wc->wc_count < SCORE_THRES3)
2127 bonus = SCORE_COMMON2;
2128 else
2129 bonus = SCORE_COMMON3;
2130 if (split)
2131 newscore = score - bonus / 2;
2132 else
2133 newscore = score - bonus;
2134 if (newscore < 0)
2135 return 0;
2136 return newscore;
2137 }
2138 return score;
2139}
2140
Bram Moolenaar5195e452005-08-19 20:32:47 +00002141
Bram Moolenaar6de68532005-08-24 22:08:48 +00002142/*
Bram Moolenaar95529562005-08-25 21:21:38 +00002143 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002144 * Like strchr() but independent of locale.
2145 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002146 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002147byte_in_str(char_u *str, int n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002148{
2149 char_u *p;
2150
2151 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00002152 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002153 return TRUE;
2154 return FALSE;
2155}
2156
Bram Moolenaar5195e452005-08-19 20:32:47 +00002157#define SY_MAXLEN 30
2158typedef struct syl_item_S
2159{
2160 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2161 int sy_len;
2162} syl_item_T;
2163
2164/*
2165 * Truncate "slang->sl_syllable" at the first slash and put the following items
2166 * in "slang->sl_syl_items".
2167 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002168 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002169init_syl_tab(slang_T *slang)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002170{
2171 char_u *p;
2172 char_u *s;
2173 int l;
2174 syl_item_T *syl;
2175
2176 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2177 p = vim_strchr(slang->sl_syllable, '/');
2178 while (p != NULL)
2179 {
2180 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002181 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002182 break;
2183 s = p;
2184 p = vim_strchr(p, '/');
2185 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002187 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002188 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002189 if (l >= SY_MAXLEN)
2190 return SP_FORMERROR;
2191 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002192 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002193 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2194 + slang->sl_syl_items.ga_len++;
2195 vim_strncpy(syl->sy_chars, s, l);
2196 syl->sy_len = l;
2197 }
2198 return OK;
2199}
2200
2201/*
2202 * Count the number of syllables in "word".
2203 * When "word" contains spaces the syllables after the last space are counted.
2204 * Returns zero if syllables are not defines.
2205 */
2206 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002207count_syllables(slang_T *slang, char_u *word)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002208{
2209 int cnt = 0;
2210 int skip = FALSE;
2211 char_u *p;
2212 int len;
2213 int i;
2214 syl_item_T *syl;
2215 int c;
2216
2217 if (slang->sl_syllable == NULL)
2218 return 0;
2219
2220 for (p = word; *p != NUL; p += len)
2221 {
2222 /* When running into a space reset counter. */
2223 if (*p == ' ')
2224 {
2225 len = 1;
2226 cnt = 0;
2227 continue;
2228 }
2229
2230 /* Find longest match of syllable items. */
2231 len = 0;
2232 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2233 {
2234 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2235 if (syl->sy_len > len
2236 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2237 len = syl->sy_len;
2238 }
2239 if (len != 0) /* found a match, count syllable */
2240 {
2241 ++cnt;
2242 skip = FALSE;
2243 }
2244 else
2245 {
2246 /* No recognized syllable item, at least a syllable char then? */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002247 c = mb_ptr2char(p);
2248 len = (*mb_ptr2len)(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002249 if (vim_strchr(slang->sl_syllable, c) == NULL)
2250 skip = FALSE; /* No, search for next syllable */
2251 else if (!skip)
2252 {
2253 ++cnt; /* Yes, count it */
2254 skip = TRUE; /* don't count following syllable chars */
2255 }
2256 }
2257 }
2258 return cnt;
2259}
2260
2261/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02002262 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002263 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002264 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002265 char *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002266did_set_spelllang(win_T *wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002267{
2268 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002269 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002270 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002271 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002272 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002273 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002274 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002276 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002278 int len;
2279 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002280 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002281 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002282 char_u *use_region = NULL;
2283 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002284 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002285 int i, j;
2286 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002287 static int recursive = FALSE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002288 char *ret_msg = NULL;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002289 char_u *spl_copy;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002290 bufref_T bufref;
2291
2292 set_bufref(&bufref, wp->w_buffer);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002293
2294 /* We don't want to do this recursively. May happen when a language is
2295 * not available and the SpellFileMissing autocommand opens a new buffer
2296 * in which 'spell' is set. */
2297 if (recursive)
2298 return NULL;
2299 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002300
2301 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002302 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002303
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002304 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002305 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002306 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002307 if (spl_copy == NULL)
2308 goto theend;
2309
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002310 wp->w_s->b_cjk = 0;
2311
2312 /* Loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002313 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002314 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002315 /* Get one language name. */
2316 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00002317 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002318 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002320 if (STRCMP(lang, "cjk") == 0)
2321 {
2322 wp->w_s->b_cjk = 1;
2323 continue;
2324 }
2325
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002326 /* If the name ends in ".spl" use it as the name of the spell file.
2327 * If there is a region name let "region" point to it and remove it
2328 * from the name. */
2329 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2330 {
2331 filename = TRUE;
2332
Bram Moolenaarb6356332005-07-18 21:40:44 +00002333 /* Locate a region and remove it from the file name. */
2334 p = vim_strchr(gettail(lang), '_');
2335 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2336 && !ASCII_ISALPHA(p[3]))
2337 {
2338 vim_strncpy(region_cp, p + 1, 2);
2339 mch_memmove(p, p + 3, len - (p - lang) - 2);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002340 region = region_cp;
2341 }
2342 else
2343 dont_use_region = TRUE;
2344
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002345 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002346 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2347 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002348 break;
2349 }
2350 else
2351 {
2352 filename = FALSE;
2353 if (len > 3 && lang[len - 3] == '_')
2354 {
2355 region = lang + len - 2;
2356 len -= 3;
2357 lang[len] = NUL;
2358 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002359 else
2360 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002361
2362 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002363 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2364 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002365 break;
2366 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002367
Bram Moolenaarb6356332005-07-18 21:40:44 +00002368 if (region != NULL)
2369 {
2370 /* If the region differs from what was used before then don't
2371 * use it for 'spellfile'. */
2372 if (use_region != NULL && STRCMP(region, use_region) != 0)
2373 dont_use_region = TRUE;
2374 use_region = region;
2375 }
2376
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002377 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002378 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002379 {
2380 if (filename)
2381 (void)spell_load_file(lang, lang, NULL, FALSE);
2382 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002383 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002384 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002385 /* SpellFileMissing autocommands may do anything, including
2386 * destroying the buffer we are using... */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002387 if (!bufref_valid(&bufref))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002389 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer");
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002390 goto theend;
2391 }
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002392 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002393 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002394
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002395 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002396 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002397 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002398 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2399 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
2400 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002401 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002402 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002403 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002404 {
2405 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002406 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002407 if (c == REGION_ALL)
2408 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002409 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002410 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002411 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002412 /* This addition file is for other regions. */
2413 region_mask = 0;
2414 }
2415 else
2416 /* This is probably an error. Give a warning and
2417 * accept the words anyway. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002418 smsg(_("Warning: region %s not supported"),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002419 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002420 }
2421 else
2422 region_mask = 1 << c;
2423 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002424
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002425 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002426 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002427 if (ga_grow(&ga, 1) == FAIL)
2428 {
2429 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002430 ret_msg = e_outofmem;
2431 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002432 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002433 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002434 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2435 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002436 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002437 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002438 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002439 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002440 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002441 }
2442
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002443 /* round 0: load int_wordlist, if possible.
2444 * round 1: load first name in 'spellfile'.
2445 * round 2: load second name in 'spellfile.
2446 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002447 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002448 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002450 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002451 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002452 /* Internal wordlist, if there is one. */
2453 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002454 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002455 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002456 }
2457 else
2458 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002459 /* One entry in 'spellfile'. */
2460 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2461 STRCAT(spf_name, ".spl");
2462
2463 /* If it was already found above then skip it. */
2464 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002465 {
2466 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
2467 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002468 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002469 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002470 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002471 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002472 }
2473
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002474 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002475 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
2476 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002478 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002480 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002481 * region name, the region is ignored otherwise. for int_wordlist
2482 * use an arbitrary name. */
2483 if (round == 0)
2484 STRCPY(lang, "internal wordlist");
2485 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002486 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002487 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002488 p = vim_strchr(lang, '.');
2489 if (p != NULL)
2490 *p = NUL; /* truncate at ".encoding.add" */
2491 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002492 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002493
2494 /* If one of the languages has NOBREAK we assume the addition
2495 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 if (slang != NULL && nobreak)
2497 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002499 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002500 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002501 region_mask = REGION_ALL;
2502 if (use_region != NULL && !dont_use_region)
2503 {
2504 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002505 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002506 if (c != REGION_ALL)
2507 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002508 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002509 /* This spell file is for other regions. */
2510 region_mask = 0;
2511 }
2512
2513 if (region_mask != 0)
2514 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002515 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
2516 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
2517 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002518 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2519 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002520 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002522 }
2523 }
2524
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002525 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002526 ga_clear(&wp->w_s->b_langp);
2527 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002528
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002529 /* For each language figure out what language to use for sound folding and
2530 * REP items. If the language doesn't support it itself use another one
2531 * with the same name. E.g. for "en-math" use "en". */
2532 for (i = 0; i < ga.ga_len; ++i)
2533 {
2534 lp = LANGP_ENTRY(ga, i);
2535
2536 /* sound folding */
2537 if (lp->lp_slang->sl_sal.ga_len > 0)
2538 /* language does sound folding itself */
2539 lp->lp_sallang = lp->lp_slang;
2540 else
2541 /* find first similar language that does sound folding */
2542 for (j = 0; j < ga.ga_len; ++j)
2543 {
2544 lp2 = LANGP_ENTRY(ga, j);
2545 if (lp2->lp_slang->sl_sal.ga_len > 0
2546 && STRNCMP(lp->lp_slang->sl_name,
2547 lp2->lp_slang->sl_name, 2) == 0)
2548 {
2549 lp->lp_sallang = lp2->lp_slang;
2550 break;
2551 }
2552 }
2553
2554 /* REP items */
2555 if (lp->lp_slang->sl_rep.ga_len > 0)
2556 /* language has REP items itself */
2557 lp->lp_replang = lp->lp_slang;
2558 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00002559 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002560 for (j = 0; j < ga.ga_len; ++j)
2561 {
2562 lp2 = LANGP_ENTRY(ga, j);
2563 if (lp2->lp_slang->sl_rep.ga_len > 0
2564 && STRNCMP(lp->lp_slang->sl_name,
2565 lp2->lp_slang->sl_name, 2) == 0)
2566 {
2567 lp->lp_replang = lp2->lp_slang;
2568 break;
2569 }
2570 }
2571 }
2572
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002573theend:
2574 vim_free(spl_copy);
2575 recursive = FALSE;
Bram Moolenaarbe578ed2014-05-13 14:03:40 +02002576 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002577 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002578}
2579
2580/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002581 * Clear the midword characters for buffer "buf".
2582 */
2583 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002584clear_midword(win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002585{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002586 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002587 VIM_CLEAR(wp->w_s->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002588}
2589
2590/*
2591 * Use the "sl_midword" field of language "lp" for buffer "buf".
2592 * They add up to any currently used midword characters.
2593 */
2594 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002595use_midword(slang_T *lp, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002596{
2597 char_u *p;
2598
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002599 if (lp->sl_midword == NULL) /* there aren't any */
2600 return;
2601
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002602 for (p = lp->sl_midword; *p != NUL; )
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002603 if (has_mbyte)
2604 {
2605 int c, l, n;
2606 char_u *bp;
2607
2608 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002609 l = (*mb_ptr2len)(p);
2610 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002611 wp->w_s->b_spell_ismw[c] = TRUE;
2612 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002613 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002614 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002615 else
2616 {
2617 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002618 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
2619 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002620 if (bp != NULL)
2621 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002622 vim_free(wp->w_s->b_spell_ismw_mb);
2623 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002624 vim_strncpy(bp + n, p, l);
2625 }
2626 }
2627 p += l;
2628 }
2629 else
Bram Moolenaar860cae12010-06-05 23:22:07 +02002630 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002631}
2632
2633/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 * Find the region "region[2]" in "rp" (points to "sl_regions").
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002635 * Each region is simply stored as the two characters of its name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002636 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002637 */
2638 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002639find_region(char_u *rp, char_u *region)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002640{
2641 int i;
2642
2643 for (i = 0; ; i += 2)
2644 {
2645 if (rp[i] == NUL)
2646 return REGION_ALL;
2647 if (rp[i] == region[0] && rp[i + 1] == region[1])
2648 break;
2649 }
2650 return i / 2;
2651}
2652
2653/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002655 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002656 * Word WF_ONECAP
2657 * W WORD WF_ALLCAP
2658 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002660 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002661captype(
2662 char_u *word,
2663 char_u *end) /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002664{
2665 char_u *p;
2666 int c;
2667 int firstcap;
2668 int allcap;
2669 int past_second = FALSE; /* past second word char */
2670
2671 /* find first letter */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002672 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002673 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002674 return 0; /* only non-word characters, illegal word */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002675 if (has_mbyte)
2676 c = mb_ptr2char_adv(&p);
2677 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002678 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002679 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680
2681 /*
2682 * Need to check all letters to find a word with mixed upper/lower.
2683 * But a word with an upper char only at start is a ONECAP.
2684 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002685 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p))
Bram Moolenaarcc63c642013-11-12 04:44:01 +01002686 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002687 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002688 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002689 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002690 {
2691 /* UUl -> KEEPCAP */
2692 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002693 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002694 allcap = FALSE;
2695 }
2696 else if (!allcap)
2697 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002698 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002699 past_second = TRUE;
2700 }
2701
2702 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002703 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002704 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002705 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002706 return 0;
2707}
2708
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002709/*
2710 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
2711 * capital. So that make_case_word() can turn WOrd into Word.
2712 * Add ALLCAP for "WOrD".
2713 */
2714 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002715badword_captype(char_u *word, char_u *end)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002716{
2717 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002718 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002719 int l, u;
2720 int first;
2721 char_u *p;
2722
2723 if (flags & WF_KEEPCAP)
2724 {
2725 /* Count the number of UPPER and lower case letters. */
2726 l = u = 0;
2727 first = FALSE;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002728 for (p = word; p < end; MB_PTR_ADV(p))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002729 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00002730 c = PTR2CHAR(p);
2731 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002732 {
2733 ++u;
2734 if (p == word)
2735 first = TRUE;
2736 }
2737 else
2738 ++l;
2739 }
2740
2741 /* If there are more UPPER than lower case letters suggest an
2742 * ALLCAP word. Otherwise, if the first letter is UPPER then
2743 * suggest ONECAP. Exception: "ALl" most likely should be "All",
2744 * require three upper case letters. */
2745 if (u > l && u > 2)
2746 flags |= WF_ALLCAP;
2747 else if (first)
2748 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002749
2750 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
2751 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002752 }
2753 return flags;
2754}
2755
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002756/*
2757 * Delete the internal wordlist and its .spl file.
2758 */
2759 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002760spell_delete_wordlist(void)
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002761{
2762 char_u fname[MAXPATHL];
2763
2764 if (int_wordlist != NULL)
2765 {
2766 mch_remove(int_wordlist);
2767 int_wordlist_spl(fname);
2768 mch_remove(fname);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002769 VIM_CLEAR(int_wordlist);
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002770 }
2771}
2772
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002773/*
2774 * Free all languages.
2775 */
2776 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002777spell_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002778{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002779 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002780 buf_T *buf;
2781
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02002782 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar29323592016-07-24 22:04:11 +02002783 FOR_ALL_BUFFERS(buf)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002784 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002785
2786 while (first_lang != NULL)
2787 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002788 slang = first_lang;
2789 first_lang = slang->sl_next;
2790 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002791 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002792
Bram Moolenaar34b466e2013-11-28 17:41:46 +01002793 spell_delete_wordlist();
Bram Moolenaar7887d882005-07-01 22:33:52 +00002794
Bram Moolenaard23a8232018-02-10 18:45:26 +01002795 VIM_CLEAR(repl_to);
2796 VIM_CLEAR(repl_from);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002797}
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002798
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002799/*
2800 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002801 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002802 */
2803 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002804spell_reload(void)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002805{
Bram Moolenaar3982c542005-06-08 21:56:31 +00002806 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807
Bram Moolenaarea408852005-06-25 22:49:46 +00002808 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002809 init_spell_chartab();
2810
2811 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002812 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813
2814 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar29323592016-07-24 22:04:11 +02002815 FOR_ALL_WINDOWS(wp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002816 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002817 /* Only load the wordlists when 'spelllang' is set and there is a
2818 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002819 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002820 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002821 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002822 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02002823 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00002824 break;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002825 }
2826 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002827 }
2828}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002829
Bram Moolenaarb765d632005-06-07 21:00:02 +00002830/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002831 * Opposite of offset2bytes().
2832 * "pp" points to the bytes and is advanced over it.
2833 * Returns the offset.
2834 */
2835 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002836bytes2offset(char_u **pp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002837{
2838 char_u *p = *pp;
2839 int nr;
2840 int c;
2841
2842 c = *p++;
2843 if ((c & 0x80) == 0x00) /* 1 byte */
2844 {
2845 nr = c - 1;
2846 }
2847 else if ((c & 0xc0) == 0x80) /* 2 bytes */
2848 {
2849 nr = (c & 0x3f) - 1;
2850 nr = nr * 255 + (*p++ - 1);
2851 }
2852 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
2853 {
2854 nr = (c & 0x1f) - 1;
2855 nr = nr * 255 + (*p++ - 1);
2856 nr = nr * 255 + (*p++ - 1);
2857 }
2858 else /* 4 bytes */
2859 {
2860 nr = (c & 0x0f) - 1;
2861 nr = nr * 255 + (*p++ - 1);
2862 nr = nr * 255 + (*p++ - 1);
2863 nr = nr * 255 + (*p++ - 1);
2864 }
2865
2866 *pp = p;
2867 return nr;
2868}
2869
Bram Moolenaar4770d092006-01-12 23:22:24 +00002870
2871/*
2872 * Open a spell buffer. This is a nameless buffer that is not in the buffer
2873 * list and only contains text lines. Can use a swapfile to reduce memory
2874 * use.
2875 * Most other fields are invalid! Esp. watch out for string options being
2876 * NULL and there is no undo info.
2877 * Returns NULL when out of memory.
2878 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002879 buf_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002880open_spellbuf(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002881{
2882 buf_T *buf;
2883
2884 buf = (buf_T *)alloc_clear(sizeof(buf_T));
2885 if (buf != NULL)
2886 {
2887 buf->b_spell = TRUE;
2888 buf->b_p_swf = TRUE; /* may create a swap file */
Bram Moolenaar706d2de2013-07-17 17:35:13 +02002889#ifdef FEAT_CRYPT
2890 buf->b_p_key = empty_option;
2891#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00002892 ml_open(buf);
2893 ml_open_file(buf); /* create swap file now */
2894 }
2895 return buf;
2896}
2897
2898/*
2899 * Close the buffer used for spell info.
2900 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002901 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002902close_spellbuf(buf_T *buf)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002903{
2904 if (buf != NULL)
2905 {
2906 ml_close(buf, TRUE);
2907 vim_free(buf);
2908 }
2909}
2910
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002911/*
2912 * Init the chartab used for spelling for ASCII.
2913 * EBCDIC is not supported!
2914 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002915 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002916clear_spell_chartab(spelltab_T *sp)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002917{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002918 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002919
2920 /* Init everything to FALSE. */
2921 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2922 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2923 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002924 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002925 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002926 sp->st_upper[i] = i;
2927 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002928
2929 /* We include digits. A word shouldn't start with a digit, but handling
2930 * that is done separately. */
2931 for (i = '0'; i <= '9'; ++i)
2932 sp->st_isw[i] = TRUE;
2933 for (i = 'A'; i <= 'Z'; ++i)
2934 {
2935 sp->st_isw[i] = TRUE;
2936 sp->st_isu[i] = TRUE;
2937 sp->st_fold[i] = i + 0x20;
2938 }
2939 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002940 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002941 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002942 sp->st_upper[i] = i - 0x20;
2943 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002944}
2945
2946/*
2947 * Init the chartab used for spelling. Only depends on 'encoding'.
2948 * Called once while starting up and when 'encoding' changes.
2949 * The default is to use isalpha(), but the spell file should define the word
2950 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002951 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002952 */
2953 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002954init_spell_chartab(void)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002955{
2956 int i;
2957
2958 did_set_spelltab = FALSE;
2959 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002960 if (enc_dbcs)
2961 {
2962 /* DBCS: assume double-wide characters are word characters. */
2963 for (i = 128; i <= 255; ++i)
2964 if (MB_BYTE2LEN(i) == 2)
2965 spelltab.st_isw[i] = TRUE;
2966 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002967 else if (enc_utf8)
2968 {
2969 for (i = 128; i < 256; ++i)
2970 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002971 int f = utf_fold(i);
2972 int u = utf_toupper(i);
2973
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002974 spelltab.st_isu[i] = utf_isupper(i);
2975 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02002976 /* The folded/upper-cased value is different between latin1 and
2977 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
2978 * value for utf-8 to avoid this. */
2979 spelltab.st_fold[i] = (f < 256) ? f : i;
2980 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002981 }
2982 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002983 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002984 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002985 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002986 for (i = 128; i < 256; ++i)
2987 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002988 if (MB_ISUPPER(i))
2989 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002990 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002991 spelltab.st_isu[i] = TRUE;
2992 spelltab.st_fold[i] = MB_TOLOWER(i);
2993 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002994 else if (MB_ISLOWER(i))
2995 {
2996 spelltab.st_isw[i] = TRUE;
2997 spelltab.st_upper[i] = MB_TOUPPER(i);
2998 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002999 }
3000 }
3001}
3002
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003003
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003004/*
Bram Moolenaarea408852005-06-25 22:49:46 +00003005 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003006 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00003007 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003008 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00003009 */
3010 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003011spell_iswordp(
3012 char_u *p,
3013 win_T *wp) /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00003014{
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003015 char_u *s;
3016 int l;
3017 int c;
3018
3019 if (has_mbyte)
3020 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003021 l = MB_PTR2LEN(p);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003022 s = p;
3023 if (l == 1)
3024 {
3025 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003026 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003027 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003028 }
3029 else
3030 {
3031 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003032 if (c < 256 ? wp->w_s->b_spell_ismw[c]
3033 : (wp->w_s->b_spell_ismw_mb != NULL
3034 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003035 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003036 }
3037
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003038 c = mb_ptr2char(s);
3039 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003040 return spell_mb_isword_class(mb_get_class(s), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003041 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003042 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003043
Bram Moolenaar860cae12010-06-05 23:22:07 +02003044 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003045}
3046
3047/*
3048 * Return TRUE if "p" points to a word character.
3049 * Unlike spell_iswordp() this doesn't check for "midword" characters.
3050 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003051 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003052spell_iswordp_nmw(char_u *p, win_T *wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003053{
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003054 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003055
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003056 if (has_mbyte)
3057 {
3058 c = mb_ptr2char(p);
3059 if (c > 255)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003060 return spell_mb_isword_class(mb_get_class(p), wp);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003061 return spelltab.st_isw[c];
3062 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003063 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00003064}
3065
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003066/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003067 * Return TRUE if word class indicates a word character.
3068 * Only for characters above 255.
3069 * Unicode subscript and superscript are not considered word characters.
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003070 * See also dbcs_class() and utf_class() in mbyte.c.
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003071 */
3072 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003073spell_mb_isword_class(int cl, win_T *wp)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003074{
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003075 if (wp->w_s->b_cjk)
3076 /* East Asian characters are not considered word characters. */
3077 return cl == 2 || cl == 0x2800;
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00003078 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
3079}
3080
3081/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003082 * Return TRUE if "p" points to a word character.
3083 * Wide version of spell_iswordp().
3084 */
3085 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003086spell_iswordp_w(int *p, win_T *wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003087{
3088 int *s;
3089
Bram Moolenaar860cae12010-06-05 23:22:07 +02003090 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
3091 : (wp->w_s->b_spell_ismw_mb != NULL
3092 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003093 s = p + 1;
3094 else
3095 s = p;
3096
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003097 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003098 {
3099 if (enc_utf8)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003100 return spell_mb_isword_class(utf_class(*s), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003101 if (enc_dbcs)
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003102 return spell_mb_isword_class(
3103 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003104 return 0;
3105 }
3106 return spelltab.st_isw[*s];
3107}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003108
Bram Moolenaarea408852005-06-25 22:49:46 +00003109/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003110 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
3111 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003112 * When using a multi-byte 'encoding' the length may change!
3113 * Returns FAIL when something wrong.
3114 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003115 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003116spell_casefold(
3117 char_u *str,
3118 int len,
3119 char_u *buf,
3120 int buflen)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003121{
3122 int i;
3123
3124 if (len >= buflen)
3125 {
3126 buf[0] = NUL;
3127 return FAIL; /* result will not fit */
3128 }
3129
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003130 if (has_mbyte)
3131 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003132 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003133 char_u *p;
3134 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003135
3136 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003137 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003138 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003139 if (outi + MB_MAXBYTES > buflen)
3140 {
3141 buf[outi] = NUL;
3142 return FAIL;
3143 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003144 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003145 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003146 }
3147 buf[outi] = NUL;
3148 }
3149 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003150 {
3151 /* Be quick for non-multibyte encodings. */
3152 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003153 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003154 buf[i] = NUL;
3155 }
3156
3157 return OK;
3158}
3159
Bram Moolenaar4770d092006-01-12 23:22:24 +00003160/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003161#define SPS_BEST 1
3162#define SPS_FAST 2
3163#define SPS_DOUBLE 4
3164
Bram Moolenaar4770d092006-01-12 23:22:24 +00003165static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
3166static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003167
3168/*
3169 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003170 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003171 */
3172 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003173spell_check_sps(void)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003174{
3175 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003176 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003177 char_u buf[MAXPATHL];
3178 int f;
3179
3180 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003181 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003182
3183 for (p = p_sps; *p != NUL; )
3184 {
3185 copy_option_part(&p, buf, MAXPATHL, ",");
3186
3187 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003188 if (VIM_ISDIGIT(*buf))
3189 {
3190 s = buf;
3191 sps_limit = getdigits(&s);
3192 if (*s != NUL && !VIM_ISDIGIT(*s))
3193 f = -1;
3194 }
3195 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003196 f = SPS_BEST;
3197 else if (STRCMP(buf, "fast") == 0)
3198 f = SPS_FAST;
3199 else if (STRCMP(buf, "double") == 0)
3200 f = SPS_DOUBLE;
3201 else if (STRNCMP(buf, "expr:", 5) != 0
3202 && STRNCMP(buf, "file:", 5) != 0)
3203 f = -1;
3204
3205 if (f == -1 || (sps_flags != 0 && f != 0))
3206 {
3207 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003208 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003209 return FAIL;
3210 }
3211 if (f != 0)
3212 sps_flags = f;
3213 }
3214
3215 if (sps_flags == 0)
3216 sps_flags = SPS_BEST;
3217
3218 return OK;
3219}
3220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003221/*
Bram Moolenaar134bf072013-09-25 18:54:24 +02003222 * "z=": Find badly spelled word under or after the cursor.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003223 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003224 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00003225 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003226 */
3227 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003228spell_suggest(int count)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003229{
3230 char_u *line;
3231 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003232 char_u wcopy[MAXWLEN + 2];
3233 char_u *p;
3234 int i;
3235 int c;
3236 suginfo_T sug;
3237 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003238 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003239 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003240 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003241 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003242 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003243 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003244
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003245 if (no_spell_checking(curwin))
3246 return;
3247
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003248 if (VIsual_active)
3249 {
3250 /* Use the Visually selected text as the bad word. But reject
3251 * a multi-line selection. */
3252 if (curwin->w_cursor.lnum != VIsual.lnum)
3253 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003254 vim_beep(BO_SPELL);
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003255 return;
3256 }
3257 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
3258 if (badlen < 0)
3259 badlen = -badlen;
3260 else
3261 curwin->w_cursor.col = VIsual.col;
3262 ++badlen;
3263 end_visual_mode();
3264 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003265 /* Find the start of the badly spelled word. */
3266 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00003267 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003268 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003269 /* No bad word or it starts after the cursor: use the word under the
3270 * cursor. */
3271 curwin->w_cursor = prev_cursor;
3272 line = ml_get_curline();
3273 p = line + curwin->w_cursor.col;
3274 /* Backup to before start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003275 while (p > line && spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003276 MB_PTR_BACK(line, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003277 /* Forward to start of word. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003278 while (*p != NUL && !spell_iswordp_nmw(p, curwin))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003279 MB_PTR_ADV(p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003280
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003281 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003282 {
3283 beep_flush();
3284 return;
3285 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003286 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003287 }
3288
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003289 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003290
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003291 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003292 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003293
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003294 /* Make a copy of current line since autocommands may free the line. */
3295 line = vim_strsave(ml_get_curline());
3296 if (line == NULL)
3297 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003298
Bram Moolenaar5195e452005-08-19 20:32:47 +00003299 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
3300 * 'spellsuggest', whatever is smaller. */
3301 if (sps_limit > (int)Rows - 2)
3302 limit = (int)Rows - 2;
3303 else
3304 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003305 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003306 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307
3308 if (sug.su_ga.ga_len == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003309 msg(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00003310 else if (count > 0)
3311 {
3312 if (count > sug.su_ga.ga_len)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003313 smsg(_("Sorry, only %ld suggestions"),
Bram Moolenaard12a1322005-08-21 22:08:24 +00003314 (long)sug.su_ga.ga_len);
3315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003316 else
3317 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01003318 VIM_CLEAR(repl_from);
3319 VIM_CLEAR(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003320
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003321#ifdef FEAT_RIGHTLEFT
3322 /* When 'rightleft' is set the list is drawn right-left. */
3323 cmdmsg_rl = curwin->w_p_rl;
3324 if (cmdmsg_rl)
3325 msg_col = Columns - 1;
3326#endif
3327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003328 /* List the suggestions. */
3329 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +00003330 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003331 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003332 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
3333 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003334#ifdef FEAT_RIGHTLEFT
3335 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
3336 {
3337 /* And now the rabbit from the high hat: Avoid showing the
3338 * untranslated message rightleft. */
3339 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
3340 sug.su_badlen, sug.su_badptr);
3341 }
3342#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003343 msg_puts((char *)IObuff);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003344 msg_clr_eos();
3345 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00003346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 msg_scroll = TRUE;
3348 for (i = 0; i < sug.su_ga.ga_len; ++i)
3349 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003350 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351
3352 /* The suggested word may replace only part of the bad word, add
3353 * the not replaced part. */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003354 vim_strncpy(wcopy, stp->st_word, MAXWLEN);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003356 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003357 sug.su_badptr + stp->st_orglen,
3358 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003359 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
3360#ifdef FEAT_RIGHTLEFT
3361 if (cmdmsg_rl)
3362 rl_mirror(IObuff);
3363#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01003364 msg_puts((char *)IObuff);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003365
3366 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003367 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003368
3369 /* The word may replace more than "su_badlen". */
3370 if (sug.su_badlen < stp->st_orglen)
3371 {
3372 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
3373 stp->st_orglen, sug.su_badptr);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003374 msg_puts((char *)IObuff);
Bram Moolenaar0c405862005-06-22 22:26:26 +00003375 }
3376
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003377 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003378 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003379 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003380 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003381 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003382 stp->st_salscore ? "s " : "",
3383 stp->st_score, stp->st_altscore);
3384 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003385 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00003386 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003387#ifdef FEAT_RIGHTLEFT
3388 if (cmdmsg_rl)
3389 /* Mirror the numbers, but keep the leading space. */
3390 rl_mirror(IObuff + 1);
3391#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00003392 msg_advance(30);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003393 msg_puts((char *)IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003395 msg_putchar('\n');
3396 }
3397
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003398#ifdef FEAT_RIGHTLEFT
3399 cmdmsg_rl = FALSE;
3400 msg_col = 0;
3401#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003402 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003403 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003404 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00003405 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003406 lines_left = Rows; /* avoid more prompt */
3407 /* don't delay for 'smd' in normal_cmd() */
3408 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003409 }
3410
Bram Moolenaard12a1322005-08-21 22:08:24 +00003411 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
3412 {
3413 /* Save the from and to text for :spellrepall. */
3414 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003415 if (sug.su_badlen > stp->st_orglen)
3416 {
3417 /* Replacing less than "su_badlen", append the remainder to
3418 * repl_to. */
3419 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
3420 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
3421 sug.su_badlen - stp->st_orglen,
3422 sug.su_badptr + stp->st_orglen);
3423 repl_to = vim_strsave(IObuff);
3424 }
3425 else
3426 {
3427 /* Replacing su_badlen or more, use the whole word. */
3428 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
3429 repl_to = vim_strsave(stp->st_word);
3430 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003431
3432 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +00003433 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
3434 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003435 if (p != NULL)
3436 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003437 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003438 mch_memmove(p, line, c);
3439 STRCPY(p + c, stp->st_word);
3440 STRCAT(p, sug.su_badptr + stp->st_orglen);
3441 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3442 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003443
3444 /* For redo we use a change-word command. */
3445 ResetRedobuff();
3446 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00003447 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003448 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003449 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003450
3451 /* After this "p" may be invalid. */
3452 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +00003453 }
3454 }
3455 else
3456 curwin->w_cursor = prev_cursor;
3457
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003458 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003459skip:
3460 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003461}
3462
3463/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003464 * Check if the word at line "lnum" column "col" is required to start with a
3465 * capital. This uses 'spellcapcheck' of the current buffer.
3466 */
3467 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003468check_need_cap(linenr_T lnum, colnr_T col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003469{
3470 int need_cap = FALSE;
3471 char_u *line;
3472 char_u *line_copy = NULL;
3473 char_u *p;
3474 colnr_T endcol;
3475 regmatch_T regmatch;
3476
Bram Moolenaar860cae12010-06-05 23:22:07 +02003477 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003478 return FALSE;
3479
3480 line = ml_get_curline();
3481 endcol = 0;
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003482 if (getwhitecols(line) >= (int)col)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003483 {
3484 /* At start of line, check if previous line is empty or sentence
3485 * ends there. */
3486 if (lnum == 1)
3487 need_cap = TRUE;
3488 else
3489 {
3490 line = ml_get(lnum - 1);
3491 if (*skipwhite(line) == NUL)
3492 need_cap = TRUE;
3493 else
3494 {
3495 /* Append a space in place of the line break. */
3496 line_copy = concat_str(line, (char_u *)" ");
3497 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003499 }
3500 }
3501 }
3502 else
3503 endcol = col;
3504
3505 if (endcol > 0)
3506 {
3507 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003508 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003509 regmatch.rm_ic = FALSE;
3510 p = line + endcol;
3511 for (;;)
3512 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003513 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01003514 if (p == line || spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003515 break;
3516 if (vim_regexec(&regmatch, p, 0)
3517 && regmatch.endp[0] == line + endcol)
3518 {
3519 need_cap = TRUE;
3520 break;
3521 }
3522 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003523 curwin->w_s->b_cap_prog = regmatch.regprog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003524 }
3525
3526 vim_free(line_copy);
3527
3528 return need_cap;
3529}
3530
3531
3532/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003533 * ":spellrepall"
3534 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003535 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003536ex_spellrepall(exarg_T *eap UNUSED)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003537{
3538 pos_T pos = curwin->w_cursor;
3539 char_u *frompat;
3540 int addlen;
3541 char_u *line;
3542 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003543 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003544 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003545
3546 if (repl_from == NULL || repl_to == NULL)
3547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003548 emsg(_("E752: No previous spell replacement"));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003549 return;
3550 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003552
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003554 if (frompat == NULL)
3555 return;
3556 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
3557 p_ws = FALSE;
3558
Bram Moolenaar5195e452005-08-19 20:32:47 +00003559 sub_nsubs = 0;
3560 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003561 curwin->w_cursor.lnum = 0;
3562 while (!got_int)
3563 {
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003564 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003565 || u_save_cursor() == FAIL)
3566 break;
3567
3568 /* Only replace when the right word isn't there yet. This happens
3569 * when changing "etc" to "etc.". */
3570 line = ml_get_curline();
3571 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
3572 repl_to, STRLEN(repl_to)) != 0)
3573 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003574 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003575 if (p == NULL)
3576 break;
3577 mch_memmove(p, line, curwin->w_cursor.col);
3578 STRCPY(p + curwin->w_cursor.col, repl_to);
3579 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
3580 ml_replace(curwin->w_cursor.lnum, p, FALSE);
3581 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003582
3583 if (curwin->w_cursor.lnum != prev_lnum)
3584 {
3585 ++sub_nlines;
3586 prev_lnum = curwin->w_cursor.lnum;
3587 }
3588 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003589 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003590 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003591 }
3592
3593 p_ws = save_ws;
3594 curwin->w_cursor = pos;
3595 vim_free(frompat);
3596
Bram Moolenaar5195e452005-08-19 20:32:47 +00003597 if (sub_nsubs == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003598 semsg(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003599 else
3600 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003601}
3602
3603/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003604 * Find spell suggestions for "word". Return them in the growarray "*gap" as
3605 * a list of allocated strings.
3606 */
3607 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003608spell_suggest_list(
3609 garray_T *gap,
3610 char_u *word,
3611 int maxcount, /* maximum nr of suggestions */
3612 int need_cap, /* 'spellcapcheck' matched */
3613 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003614{
3615 suginfo_T sug;
3616 int i;
3617 suggest_T *stp;
3618 char_u *wcopy;
3619
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003620 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003621
3622 /* Make room in "gap". */
3623 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003624 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003625 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003626 for (i = 0; i < sug.su_ga.ga_len; ++i)
3627 {
3628 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003629
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003630 /* The suggested word may replace only part of "word", add the not
3631 * replaced part. */
3632 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003633 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00003634 if (wcopy == NULL)
3635 break;
3636 STRCPY(wcopy, stp->st_word);
3637 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
3638 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
3639 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003640 }
3641
3642 spell_find_cleanup(&sug);
3643}
3644
3645/*
3646 * Find spell suggestions for the word at the start of "badptr".
3647 * Return the suggestions in "su->su_ga".
3648 * The maximum number of suggestions is "maxcount".
3649 * Note: does use info for the current window.
3650 * This is based on the mechanisms of Aspell, but completely reimplemented.
3651 */
3652 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003653spell_find_suggest(
3654 char_u *badptr,
3655 int badlen, /* length of bad word or 0 if unknown */
3656 suginfo_T *su,
3657 int maxcount,
3658 int banbadword, /* don't include badword in suggestions */
3659 int need_cap, /* word should start with capital */
3660 int interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003661{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003662 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003663 char_u buf[MAXPATHL];
3664 char_u *p;
3665 int do_combine = FALSE;
3666 char_u *sps_copy;
3667#ifdef FEAT_EVAL
3668 static int expr_busy = FALSE;
3669#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003670 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003671 int i;
3672 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003673
3674 /*
3675 * Set the info in "*su".
3676 */
3677 vim_memset(su, 0, sizeof(suginfo_T));
3678 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
3679 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003680 if (*badptr == NUL)
3681 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003682 hash_init(&su->su_banned);
3683
3684 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003685 if (badlen != 0)
3686 su->su_badlen = badlen;
3687 else
3688 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003689 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003690 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003691
3692 if (su->su_badlen >= MAXWLEN)
3693 su->su_badlen = MAXWLEN - 1; /* just in case */
3694 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
3695 (void)spell_casefold(su->su_badptr, su->su_badlen,
3696 su->su_fbadword, MAXWLEN);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02003697 /* TODO: make this work if the case-folded text is longer than the original
3698 * text. Currently an illegal byte causes wrong pointer computations. */
3699 su->su_fbadword[su->su_badlen] = NUL;
3700
Bram Moolenaar0c405862005-06-22 22:26:26 +00003701 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003702 su->su_badflags = badword_captype(su->su_badptr,
3703 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003704 if (need_cap)
3705 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003706
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003707 /* Find the default language for sound folding. We simply use the first
3708 * one in 'spelllang' that supports sound folding. That's good for when
3709 * using multiple files for one language, it's not that bad when mixing
3710 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003711 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003712 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02003713 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003714 if (lp->lp_sallang != NULL)
3715 {
3716 su->su_sallang = lp->lp_sallang;
3717 break;
3718 }
3719 }
3720
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003721 /* Soundfold the bad word with the default sound folding, so that we don't
3722 * have to do this many times. */
3723 if (su->su_sallang != NULL)
3724 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
3725 su->su_sal_badword);
3726
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003727 /* If the word is not capitalised and spell_check() doesn't consider the
3728 * word to be bad then it might need to be capitalised. Add a suggestion
3729 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003730 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003731 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003732 {
3733 make_case_word(su->su_badword, buf, WF_ONECAP);
3734 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003735 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003736 }
3737
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003738 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00003739 if (banbadword)
3740 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003741
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003742 /* Make a copy of 'spellsuggest', because the expression may change it. */
3743 sps_copy = vim_strsave(p_sps);
3744 if (sps_copy == NULL)
3745 return;
3746
3747 /* Loop over the items in 'spellsuggest'. */
3748 for (p = sps_copy; *p != NUL; )
3749 {
3750 copy_option_part(&p, buf, MAXPATHL, ",");
3751
3752 if (STRNCMP(buf, "expr:", 5) == 0)
3753 {
3754#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003755 /* Evaluate an expression. Skip this when called recursively,
3756 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003757 if (!expr_busy)
3758 {
3759 expr_busy = TRUE;
3760 spell_suggest_expr(su, buf + 5);
3761 expr_busy = FALSE;
3762 }
3763#endif
3764 }
3765 else if (STRNCMP(buf, "file:", 5) == 0)
3766 /* Use list of suggestions in a file. */
3767 spell_suggest_file(su, buf + 5);
3768 else
3769 {
3770 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003771 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003772 if (sps_flags & SPS_DOUBLE)
3773 do_combine = TRUE;
3774 }
3775 }
3776
3777 vim_free(sps_copy);
3778
3779 if (do_combine)
3780 /* Combine the two list of suggestions. This must be done last,
3781 * because sorting changes the order again. */
3782 score_combine(su);
3783}
3784
3785#ifdef FEAT_EVAL
3786/*
3787 * Find suggestions by evaluating expression "expr".
3788 */
3789 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003790spell_suggest_expr(suginfo_T *su, char_u *expr)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003791{
3792 list_T *list;
3793 listitem_T *li;
3794 int score;
3795 char_u *p;
3796
3797 /* The work is split up in a few parts to avoid having to export
3798 * suginfo_T.
3799 * First evaluate the expression and get the resulting list. */
3800 list = eval_spell_expr(su->su_badword, expr);
3801 if (list != NULL)
3802 {
3803 /* Loop over the items in the list. */
3804 for (li = list->lv_first; li != NULL; li = li->li_next)
3805 if (li->li_tv.v_type == VAR_LIST)
3806 {
3807 /* Get the word and the score from the items. */
3808 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003809 if (score >= 0 && score <= su->su_maxscore)
3810 add_suggestion(su, &su->su_ga, p, su->su_badlen,
3811 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003812 }
3813 list_unref(list);
3814 }
3815
Bram Moolenaar4770d092006-01-12 23:22:24 +00003816 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3817 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003818 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3819}
3820#endif
3821
3822/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003823 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003824 */
3825 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003826spell_suggest_file(suginfo_T *su, char_u *fname)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003827{
3828 FILE *fd;
3829 char_u line[MAXWLEN * 2];
3830 char_u *p;
3831 int len;
3832 char_u cword[MAXWLEN];
3833
3834 /* Open the file. */
3835 fd = mch_fopen((char *)fname, "r");
3836 if (fd == NULL)
3837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003838 semsg(_(e_notopen), fname);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003839 return;
3840 }
3841
3842 /* Read it line by line. */
3843 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
3844 {
3845 line_breakcheck();
3846
3847 p = vim_strchr(line, '/');
3848 if (p == NULL)
3849 continue; /* No Tab found, just skip the line. */
3850 *p++ = NUL;
3851 if (STRICMP(su->su_badword, line) == 0)
3852 {
3853 /* Match! Isolate the good word, until CR or NL. */
3854 for (len = 0; p[len] >= ' '; ++len)
3855 ;
3856 p[len] = NUL;
3857
3858 /* If the suggestion doesn't have specific case duplicate the case
3859 * of the bad word. */
3860 if (captype(p, NULL) == 0)
3861 {
3862 make_case_word(p, cword, su->su_badflags);
3863 p = cword;
3864 }
3865
3866 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00003867 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003868 }
3869 }
3870
3871 fclose(fd);
3872
Bram Moolenaar4770d092006-01-12 23:22:24 +00003873 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3874 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003875 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
3876}
3877
3878/*
3879 * Find suggestions for the internal method indicated by "sps_flags".
3880 */
3881 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003882spell_suggest_intern(suginfo_T *su, int interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003883{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003884 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003885 * Load the .sug file(s) that are available and not done yet.
3886 */
3887 suggest_load_files();
3888
3889 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003890 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003891 *
3892 * Set a maximum score to limit the combination of operations that is
3893 * tried.
3894 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003895 suggest_try_special(su);
3896
3897 /*
3898 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
3899 * from the .aff file and inserting a space (split the word).
3900 */
3901 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003902
3903 /* For the resulting top-scorers compute the sound-a-like score. */
3904 if (sps_flags & SPS_DOUBLE)
3905 score_comp_sal(su);
3906
3907 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00003908 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003909 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003910 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003911 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00003912 if (sps_flags & SPS_BEST)
3913 /* Adjust the word score for the suggestions found so far for how
3914 * they sounds like. */
3915 rescore_suggestions(su);
3916
3917 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01003918 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +00003919 * for the soundfold word, limits the changes that are being tried,
3920 * and "su_sfmaxscore" the rescored score, which is set by
3921 * cleanup_suggestions().
3922 * First find words with a small edit distance, because this is much
3923 * faster and often already finds the top-N suggestions. If we didn't
3924 * find many suggestions try again with a higher edit distance.
3925 * "sl_sounddone" is used to avoid doing the same word twice.
3926 */
3927 suggest_try_soundalike_prep();
3928 su->su_maxscore = SCORE_SFMAX1;
3929 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +00003930 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003931 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3932 {
3933 /* We didn't find enough matches, try again, allowing more
3934 * changes to the soundfold word. */
3935 su->su_maxscore = SCORE_SFMAX2;
3936 suggest_try_soundalike(su);
3937 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
3938 {
3939 /* Still didn't find enough matches, try again, allowing even
3940 * more changes to the soundfold word. */
3941 su->su_maxscore = SCORE_SFMAX3;
3942 suggest_try_soundalike(su);
3943 }
3944 }
3945 su->su_maxscore = su->su_sfmaxscore;
3946 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003947 }
3948
Bram Moolenaar4770d092006-01-12 23:22:24 +00003949 /* When CTRL-C was hit while searching do show the results. Only clear
3950 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003951 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +00003952 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003953 {
3954 (void)vgetc();
3955 got_int = FALSE;
3956 }
3957
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003958 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003959 {
3960 if (sps_flags & SPS_BEST)
3961 /* Adjust the word score for how it sounds like. */
3962 rescore_suggestions(su);
3963
Bram Moolenaar4770d092006-01-12 23:22:24 +00003964 /* Remove bogus suggestions, sort and truncate at "maxcount". */
3965 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003966 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003967 }
3968}
3969
3970/*
3971 * Free the info put in "*su" by spell_find_suggest().
3972 */
3973 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003974spell_find_cleanup(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003975{
3976 int i;
3977
3978 /* Free the suggestions. */
3979 for (i = 0; i < su->su_ga.ga_len; ++i)
3980 vim_free(SUG(su->su_ga, i).st_word);
3981 ga_clear(&su->su_ga);
3982 for (i = 0; i < su->su_sga.ga_len; ++i)
3983 vim_free(SUG(su->su_sga, i).st_word);
3984 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003985
3986 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00003987 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003988}
3989
3990/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003991 * Make a copy of "word", with the first letter upper or lower cased, to
3992 * "wcopy[MAXWLEN]". "word" must not be empty.
3993 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003994 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003995 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003996onecap_copy(
3997 char_u *word,
3998 char_u *wcopy,
3999 int upper) /* TRUE: first letter made upper case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004000{
4001 char_u *p;
4002 int c;
4003 int l;
4004
4005 p = word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004006 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004007 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004008 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004009 c = *p++;
4010 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004011 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004013 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 if (has_mbyte)
4015 l = mb_char2bytes(c, wcopy);
4016 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004017 {
4018 l = 1;
4019 wcopy[0] = c;
4020 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004021 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004022}
4023
4024/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004025 * Make a copy of "word" with all the letters upper cased into
4026 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004027 */
4028 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004029allcap_copy(char_u *word, char_u *wcopy)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004030{
4031 char_u *s;
4032 char_u *d;
4033 int c;
4034
4035 d = wcopy;
4036 for (s = word; *s != NUL; )
4037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004039 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004040 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004041 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00004042
Bram Moolenaard3184b52011-09-02 14:18:20 +02004043 /* We only change 0xdf to SS when we are certain latin1 is used. It
Bram Moolenaar78622822005-08-23 21:00:13 +00004044 * would cause weird errors in other 8-bit encodings. */
4045 if (enc_latin1like && c == 0xdf)
4046 {
4047 c = 'S';
4048 if (d - wcopy >= MAXWLEN - 1)
4049 break;
4050 *d++ = c;
4051 }
4052 else
Bram Moolenaar78622822005-08-23 21:00:13 +00004053 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 if (has_mbyte)
4056 {
4057 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4058 break;
4059 d += mb_char2bytes(c, d);
4060 }
4061 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004062 {
4063 if (d - wcopy >= MAXWLEN - 1)
4064 break;
4065 *d++ = c;
4066 }
4067 }
4068 *d = NUL;
4069}
4070
4071/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00004072 * Try finding suggestions by recognizing specific situations.
4073 */
4074 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004075suggest_try_special(suginfo_T *su)
Bram Moolenaar0c405862005-06-22 22:26:26 +00004076{
4077 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004078 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004079 int c;
4080 char_u word[MAXWLEN];
4081
4082 /*
4083 * Recognize a word that is repeated: "the the".
4084 */
4085 p = skiptowhite(su->su_fbadword);
4086 len = p - su->su_fbadword;
4087 p = skipwhite(p);
4088 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
4089 {
4090 /* Include badflags: if the badword is onecap or allcap
4091 * use that for the goodword too: "The the" -> "The". */
4092 c = su->su_fbadword[len];
4093 su->su_fbadword[len] = NUL;
4094 make_case_word(su->su_fbadword, word, su->su_badflags);
4095 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004096
4097 /* Give a soundalike score of 0, compute the score as if deleting one
4098 * character. */
4099 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004100 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004101 }
4102}
4103
4104/*
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004105 * Change the 0 to 1 to measure how much time is spent in each state.
4106 * Output is dumped in "suggestprof".
4107 */
4108#if 0
4109# define SUGGEST_PROFILE
4110proftime_T current;
4111proftime_T total;
4112proftime_T times[STATE_FINAL + 1];
4113long counts[STATE_FINAL + 1];
4114
4115 static void
4116prof_init(void)
4117{
4118 for (int i = 0; i <= STATE_FINAL; ++i)
4119 {
4120 profile_zero(&times[i]);
4121 counts[i] = 0;
4122 }
4123 profile_start(&current);
4124 profile_start(&total);
4125}
4126
4127/* call before changing state */
4128 static void
4129prof_store(state_T state)
4130{
4131 profile_end(&current);
4132 profile_add(&times[state], &current);
4133 ++counts[state];
4134 profile_start(&current);
4135}
4136# define PROF_STORE(state) prof_store(state);
4137
4138 static void
4139prof_report(char *name)
4140{
4141 FILE *fd = fopen("suggestprof", "a");
4142
4143 profile_end(&total);
4144 fprintf(fd, "-----------------------\n");
4145 fprintf(fd, "%s: %s\n", name, profile_msg(&total));
4146 for (int i = 0; i <= STATE_FINAL; ++i)
4147 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(&times[i]), counts[i]);
4148 fclose(fd);
4149}
4150#else
4151# define PROF_STORE(state)
4152#endif
4153
4154/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 * Try finding suggestions by adding/removing/swapping letters.
4156 */
4157 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004158suggest_try_change(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004159{
4160 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004161 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004163 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004164 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004165
4166 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00004167 * to find matches (esp. REP items). Append some more text, changing
4168 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004170 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004171 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004172 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173
Bram Moolenaar860cae12010-06-05 23:22:07 +02004174 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004175 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004176 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004177
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004178 /* If reloading a spell file fails it's still in the list but
4179 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004180 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004181 continue;
4182
Bram Moolenaar4770d092006-01-12 23:22:24 +00004183 /* Try it for this language. Will add possible suggestions. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004184#ifdef SUGGEST_PROFILE
4185 prof_init();
4186#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004187 suggest_trie_walk(su, lp, fword, FALSE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004188#ifdef SUGGEST_PROFILE
4189 prof_report("try_change");
4190#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004191 }
4192}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193
Bram Moolenaar4770d092006-01-12 23:22:24 +00004194/* Check the maximum score, if we go over it we won't try this change. */
4195#define TRY_DEEPER(su, stack, depth, add) \
4196 (stack[depth].ts_score + (add) < su->su_maxscore)
4197
4198/*
4199 * Try finding suggestions by adding/removing/swapping letters.
4200 *
4201 * This uses a state machine. At each node in the tree we try various
4202 * operations. When trying if an operation works "depth" is increased and the
4203 * stack[] is used to store info. This allows combinations, thus insert one
4204 * character, replace one and delete another. The number of changes is
4205 * limited by su->su_maxscore.
4206 *
4207 * After implementing this I noticed an article by Kemal Oflazer that
4208 * describes something similar: "Error-tolerant Finite State Recognition with
4209 * Applications to Morphological Analysis and Spelling Correction" (1996).
4210 * The implementation in the article is simplified and requires a stack of
4211 * unknown depth. The implementation here only needs a stack depth equal to
4212 * the length of the word.
4213 *
4214 * This is also used for the sound-folded word, "soundfold" is TRUE then.
4215 * The mechanism is the same, but we find a match with a sound-folded word
4216 * that comes from one or more original words. Each of these words may be
4217 * added, this is done by add_sound_suggest().
4218 * Don't use:
4219 * the prefix tree or the keep-case tree
4220 * "su->su_badlen"
4221 * anything to do with upper and lower case
4222 * anything to do with word or non-word characters ("spell_iswordp()")
4223 * banned words
4224 * word flags (rare, region, compounding)
4225 * word splitting for now
4226 * "similar_chars()"
4227 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
4228 */
4229 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004230suggest_trie_walk(
4231 suginfo_T *su,
4232 langp_T *lp,
4233 char_u *fword,
4234 int soundfold)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004235{
4236 char_u tword[MAXWLEN]; /* good word collected so far */
4237 trystate_T stack[MAXWLEN];
4238 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +01004239 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +00004240 * words and split word. NUL terminated
4241 * when going deeper but not when coming
4242 * back. */
4243 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
4244 trystate_T *sp;
4245 int newscore;
4246 int score;
4247 char_u *byts, *fbyts, *pbyts;
4248 idx_T *idxs, *fidxs, *pidxs;
4249 int depth;
4250 int c, c2, c3;
4251 int n = 0;
4252 int flags;
4253 garray_T *gap;
4254 idx_T arridx;
4255 int len;
4256 char_u *p;
4257 fromto_T *ftp;
4258 int fl = 0, tl;
4259 int repextra = 0; /* extra bytes in fword[] from REP item */
4260 slang_T *slang = lp->lp_slang;
4261 int fword_ends;
4262 int goodword_ends;
4263#ifdef DEBUG_TRIEWALK
4264 /* Stores the name of the change made at each level. */
4265 char_u changename[MAXWLEN][80];
4266#endif
4267 int breakcheckcount = 1000;
4268 int compound_ok;
4269
4270 /*
4271 * Go through the whole case-fold tree, try changes at each node.
4272 * "tword[]" contains the word collected from nodes in the tree.
4273 * "fword[]" the word we are trying to match with (initially the bad
4274 * word).
4275 */
4276 depth = 0;
4277 sp = &stack[0];
4278 vim_memset(sp, 0, sizeof(trystate_T));
4279 sp->ts_curi = 1;
4280
4281 if (soundfold)
4282 {
4283 /* Going through the soundfold tree. */
4284 byts = fbyts = slang->sl_sbyts;
4285 idxs = fidxs = slang->sl_sidxs;
4286 pbyts = NULL;
4287 pidxs = NULL;
4288 sp->ts_prefixdepth = PFD_NOPREFIX;
4289 sp->ts_state = STATE_START;
4290 }
4291 else
4292 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004293 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004294 * When there are postponed prefixes we need to use these first. At
4295 * the end of the prefix we continue in the case-fold tree.
4296 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004297 fbyts = slang->sl_fbyts;
4298 fidxs = slang->sl_fidxs;
4299 pbyts = slang->sl_pbyts;
4300 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004301 if (pbyts != NULL)
4302 {
4303 byts = pbyts;
4304 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004305 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004306 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
4307 }
4308 else
4309 {
4310 byts = fbyts;
4311 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004312 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004313 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004314 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004315 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004316
Bram Moolenaar4770d092006-01-12 23:22:24 +00004317 /*
4318 * Loop to find all suggestions. At each round we either:
4319 * - For the current state try one operation, advance "ts_curi",
4320 * increase "depth".
4321 * - When a state is done go to the next, set "ts_state".
4322 * - When all states are tried decrease "depth".
4323 */
4324 while (depth >= 0 && !got_int)
4325 {
4326 sp = &stack[depth];
4327 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004329 case STATE_START:
4330 case STATE_NOPREFIX:
4331 /*
4332 * Start of node: Deal with NUL bytes, which means
4333 * tword[] may end here.
4334 */
4335 arridx = sp->ts_arridx; /* current node in the tree */
4336 len = byts[arridx]; /* bytes in this node */
4337 arridx += sp->ts_curi; /* index of current byte */
4338
4339 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004341 /* Skip over the NUL bytes, we use them later. */
4342 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
4343 ;
4344 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345
Bram Moolenaar4770d092006-01-12 23:22:24 +00004346 /* Always past NUL bytes now. */
4347 n = (int)sp->ts_state;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004348 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004349 sp->ts_state = STATE_ENDNUL;
4350 sp->ts_save_badflags = su->su_badflags;
4351
4352 /* At end of a prefix or at start of prefixtree: check for
4353 * following word. */
4354 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004355 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004356 /* Set su->su_badflags to the caps type at this position.
4357 * Use the caps type until here for the prefix itself. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004358 if (has_mbyte)
4359 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
4360 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004361 n = sp->ts_fidx;
4362 flags = badword_captype(su->su_badptr, su->su_badptr + n);
4363 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004364 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004365#ifdef DEBUG_TRIEWALK
4366 sprintf(changename[depth], "prefix");
4367#endif
4368 go_deeper(stack, depth, 0);
4369 ++depth;
4370 sp = &stack[depth];
4371 sp->ts_prefixdepth = depth - 1;
4372 byts = fbyts;
4373 idxs = fidxs;
4374 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004375
Bram Moolenaar4770d092006-01-12 23:22:24 +00004376 /* Move the prefix to preword[] with the right case
4377 * and make find_keepcap_word() works. */
4378 tword[sp->ts_twordlen] = NUL;
4379 make_case_word(tword + sp->ts_splitoff,
4380 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004381 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004382 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004383 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004384 break;
4385 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004386
Bram Moolenaar4770d092006-01-12 23:22:24 +00004387 if (sp->ts_curi > len || byts[arridx] != 0)
4388 {
4389 /* Past bytes in node and/or past NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004390 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004391 sp->ts_state = STATE_ENDNUL;
4392 sp->ts_save_badflags = su->su_badflags;
4393 break;
4394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395
Bram Moolenaar4770d092006-01-12 23:22:24 +00004396 /*
4397 * End of word in tree.
4398 */
4399 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400
Bram Moolenaar4770d092006-01-12 23:22:24 +00004401 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004402
4403 /* Skip words with the NOSUGGEST flag. */
4404 if (flags & WF_NOSUGGEST)
4405 break;
4406
Bram Moolenaar4770d092006-01-12 23:22:24 +00004407 fword_ends = (fword[sp->ts_fidx] == NUL
4408 || (soundfold
Bram Moolenaar1c465442017-03-12 20:10:05 +01004409 ? VIM_ISWHITE(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +02004410 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004411 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412
Bram Moolenaar4770d092006-01-12 23:22:24 +00004413 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +00004414 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004415 {
4416 /* There was a prefix before the word. Check that the prefix
4417 * can be used with this word. */
4418 /* Count the length of the NULs in the prefix. If there are
4419 * none this must be the first try without a prefix. */
4420 n = stack[sp->ts_prefixdepth].ts_arridx;
4421 len = pbyts[n++];
4422 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
4423 ;
4424 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004425 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004426 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004427 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004428 if (c == 0)
4429 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004430
Bram Moolenaar4770d092006-01-12 23:22:24 +00004431 /* Use the WF_RARE flag for a rare prefix. */
4432 if (c & WF_RAREPFX)
4433 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004434
Bram Moolenaar4770d092006-01-12 23:22:24 +00004435 /* Tricky: when checking for both prefix and compounding
4436 * we run into the prefix flag first.
4437 * Remember that it's OK, so that we accept the prefix
4438 * when arriving at a compound flag. */
4439 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004440 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004441 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004442
Bram Moolenaar4770d092006-01-12 23:22:24 +00004443 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
4444 * appending another compound word below. */
4445 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004446 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004447 goodword_ends = FALSE;
4448 else
4449 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004450
Bram Moolenaar4770d092006-01-12 23:22:24 +00004451 p = NULL;
4452 compound_ok = TRUE;
4453 if (sp->ts_complen > sp->ts_compsplit)
4454 {
4455 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004456 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004457 /* There was a word before this word. When there was no
4458 * change in this word (it was correct) add the first word
4459 * as a suggestion. If this word was corrected too, we
4460 * need to check if a correct word follows. */
4461 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +00004462 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +00004463 && STRNCMP(fword + sp->ts_splitfidx,
4464 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +00004465 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004466 {
4467 preword[sp->ts_prewordlen] = NUL;
4468 newscore = score_wordcount_adj(slang, sp->ts_score,
4469 preword + sp->ts_prewordlen,
4470 sp->ts_prewordlen > 0);
4471 /* Add the suggestion if the score isn't too bad. */
4472 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +00004473 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004474 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +00004475 newscore, 0, FALSE,
4476 lp->lp_sallang, FALSE);
4477 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00004478 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004479 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00004480 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00004481 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004482 /* There was a compound word before this word. If this
4483 * word does not support compounding then give up
4484 * (splitting is tried for the word without compound
4485 * flag). */
4486 if (((unsigned)flags >> 24) == 0
4487 || sp->ts_twordlen - sp->ts_splitoff
4488 < slang->sl_compminlen)
4489 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004490 /* For multi-byte chars check character length against
4491 * COMPOUNDMIN. */
4492 if (has_mbyte
4493 && slang->sl_compminlen > 0
4494 && mb_charlen(tword + sp->ts_splitoff)
4495 < slang->sl_compminlen)
4496 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004497
Bram Moolenaar4770d092006-01-12 23:22:24 +00004498 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4499 compflags[sp->ts_complen + 1] = NUL;
4500 vim_strncpy(preword + sp->ts_prewordlen,
4501 tword + sp->ts_splitoff,
4502 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004503
4504 /* Verify CHECKCOMPOUNDPATTERN rules. */
4505 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
4506 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004507 compound_ok = FALSE;
4508
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004509 if (compound_ok)
4510 {
4511 p = preword;
4512 while (*skiptowhite(p) != NUL)
4513 p = skipwhite(skiptowhite(p));
4514 if (fword_ends && !can_compound(slang, p,
4515 compflags + sp->ts_compsplit))
4516 /* Compound is not allowed. But it may still be
4517 * possible if we add another (short) word. */
4518 compound_ok = FALSE;
4519 }
4520
Bram Moolenaar4770d092006-01-12 23:22:24 +00004521 /* Get pointer to last char of previous word. */
4522 p = preword + sp->ts_prewordlen;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004523 MB_PTR_BACK(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004524 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526
Bram Moolenaar4770d092006-01-12 23:22:24 +00004527 /*
4528 * Form the word with proper case in preword.
4529 * If there is a word from a previous split, append.
4530 * For the soundfold tree don't change the case, simply append.
4531 */
4532 if (soundfold)
4533 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
4534 else if (flags & WF_KEEPCAP)
4535 /* Must find the word in the keep-case tree. */
4536 find_keepcap_word(slang, tword + sp->ts_splitoff,
4537 preword + sp->ts_prewordlen);
4538 else
4539 {
4540 /* Include badflags: If the badword is onecap or allcap
4541 * use that for the goodword too. But if the badword is
4542 * allcap and it's only one char long use onecap. */
4543 c = su->su_badflags;
4544 if ((c & WF_ALLCAP)
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004545 && su->su_badlen == (*mb_ptr2len)(su->su_badptr))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004546 c = WF_ONECAP;
4547 c |= flags;
4548
4549 /* When appending a compound word after a word character don't
4550 * use Onecap. */
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004551 if (p != NULL && spell_iswordp_nmw(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004552 c &= ~WF_ONECAP;
4553 make_case_word(tword + sp->ts_splitoff,
4554 preword + sp->ts_prewordlen, c);
4555 }
4556
4557 if (!soundfold)
4558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 /* Don't use a banned word. It may appear again as a good
4560 * word, thus remember it. */
4561 if (flags & WF_BANNED)
4562 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004563 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004564 break;
4565 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004566 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +00004567 && WAS_BANNED(su, preword + sp->ts_prewordlen))
4568 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004569 {
4570 if (slang->sl_compprog == NULL)
4571 break;
4572 /* the word so far was banned but we may try compounding */
4573 goodword_ends = FALSE;
4574 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004575 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576
Bram Moolenaar4770d092006-01-12 23:22:24 +00004577 newscore = 0;
4578 if (!soundfold) /* soundfold words don't have flags */
4579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004580 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004581 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004582 newscore += SCORE_REGION;
4583 if (flags & WF_RARE)
4584 newscore += SCORE_RARE;
4585
Bram Moolenaar0c405862005-06-22 22:26:26 +00004586 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00004587 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004588 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004589 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590
Bram Moolenaar4770d092006-01-12 23:22:24 +00004591 /* TODO: how about splitting in the soundfold tree? */
4592 if (fword_ends
4593 && goodword_ends
4594 && sp->ts_fidx >= sp->ts_fidxtry
4595 && compound_ok)
4596 {
4597 /* The badword also ends: add suggestions. */
4598#ifdef DEBUG_TRIEWALK
4599 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004601 int j;
4602
4603 /* print the stack of changes that brought us here */
4604 smsg("------ %s -------", fword);
4605 for (j = 0; j < depth; ++j)
4606 smsg("%s", changename[j]);
4607 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004608#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004609 if (soundfold)
4610 {
4611 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004612 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004613 add_sound_suggest(su, preword, sp->ts_score, lp);
4614 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +02004615 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004616 {
4617 /* Give a penalty when changing non-word char to word
4618 * char, e.g., "thes," -> "these". */
4619 p = fword + sp->ts_fidx;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004620 MB_PTR_BACK(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004621 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004622 {
4623 p = preword + STRLEN(preword);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004624 MB_PTR_BACK(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004625 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004626 newscore += SCORE_NONWORD;
4627 }
4628
Bram Moolenaar4770d092006-01-12 23:22:24 +00004629 /* Give a bonus to words seen before. */
4630 score = score_wordcount_adj(slang,
4631 sp->ts_score + newscore,
4632 preword + sp->ts_prewordlen,
4633 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004634
Bram Moolenaar4770d092006-01-12 23:22:24 +00004635 /* Add the suggestion if the score isn't too bad. */
4636 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004637 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004638 add_suggestion(su, &su->su_ga, preword,
4639 sp->ts_fidx - repextra,
4640 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004641
4642 if (su->su_badflags & WF_MIXCAP)
4643 {
4644 /* We really don't know if the word should be
4645 * upper or lower case, add both. */
4646 c = captype(preword, NULL);
4647 if (c == 0 || c == WF_ALLCAP)
4648 {
4649 make_case_word(tword + sp->ts_splitoff,
4650 preword + sp->ts_prewordlen,
4651 c == 0 ? WF_ALLCAP : 0);
4652
4653 add_suggestion(su, &su->su_ga, preword,
4654 sp->ts_fidx - repextra,
4655 score + SCORE_ICASE, 0, FALSE,
4656 lp->lp_sallang, FALSE);
4657 }
4658 }
4659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004660 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004661 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004662
Bram Moolenaar4770d092006-01-12 23:22:24 +00004663 /*
4664 * Try word split and/or compounding.
4665 */
4666 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004667 /* Don't split halfway a character. */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004668 && (!has_mbyte || sp->ts_tcharlen == 0))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004669 {
4670 int try_compound;
4671 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004672
Bram Moolenaar4770d092006-01-12 23:22:24 +00004673 /* If past the end of the bad word don't try a split.
4674 * Otherwise try changing the next word. E.g., find
4675 * suggestions for "the the" where the second "the" is
4676 * different. It's done like a split.
4677 * TODO: word split for soundfold words */
4678 try_split = (sp->ts_fidx - repextra < su->su_badlen)
4679 && !soundfold;
4680
4681 /* Get here in several situations:
4682 * 1. The word in the tree ends:
4683 * If the word allows compounding try that. Otherwise try
4684 * a split by inserting a space. For both check that a
4685 * valid words starts at fword[sp->ts_fidx].
4686 * For NOBREAK do like compounding to be able to check if
4687 * the next word is valid.
4688 * 2. The badword does end, but it was due to a change (e.g.,
4689 * a swap). No need to split, but do check that the
4690 * following word is valid.
4691 * 3. The badword and the word in the tree end. It may still
4692 * be possible to compound another (short) word.
4693 */
4694 try_compound = FALSE;
4695 if (!soundfold
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004696 && !slang->sl_nocompoundsugs
Bram Moolenaar4770d092006-01-12 23:22:24 +00004697 && slang->sl_compprog != NULL
4698 && ((unsigned)flags >> 24) != 0
4699 && sp->ts_twordlen - sp->ts_splitoff
4700 >= slang->sl_compminlen
Bram Moolenaar4770d092006-01-12 23:22:24 +00004701 && (!has_mbyte
4702 || slang->sl_compminlen == 0
4703 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004704 >= slang->sl_compminlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004705 && (slang->sl_compsylmax < MAXWLEN
4706 || sp->ts_complen + 1 - sp->ts_compsplit
4707 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004708 && (can_be_compound(sp, slang,
4709 compflags, ((unsigned)flags >> 24))))
4710
Bram Moolenaar4770d092006-01-12 23:22:24 +00004711 {
4712 try_compound = TRUE;
4713 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
4714 compflags[sp->ts_complen + 1] = NUL;
4715 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00004716
Bram Moolenaar4770d092006-01-12 23:22:24 +00004717 /* For NOBREAK we never try splitting, it won't make any word
4718 * valid. */
Bram Moolenaar7b877b32016-01-09 13:51:34 +01004719 if (slang->sl_nobreak && !slang->sl_nocompoundsugs)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004720 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +00004721
Bram Moolenaar4770d092006-01-12 23:22:24 +00004722 /* If we could add a compound word, and it's also possible to
4723 * split at this point, do the split first and set
4724 * TSF_DIDSPLIT to avoid doing it again. */
4725 else if (!fword_ends
4726 && try_compound
4727 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
4728 {
4729 try_compound = FALSE;
4730 sp->ts_flags |= TSF_DIDSPLIT;
4731 --sp->ts_curi; /* do the same NUL again */
4732 compflags[sp->ts_complen] = NUL;
4733 }
4734 else
4735 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004736
Bram Moolenaar4770d092006-01-12 23:22:24 +00004737 if (try_split || try_compound)
4738 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004739 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004740 {
4741 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004742 * words so far are valid for compounding. If there
4743 * is only one word it must not have the NEEDCOMPOUND
4744 * flag. */
4745 if (sp->ts_complen == sp->ts_compsplit
4746 && (flags & WF_NEEDCOMP))
4747 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00004748 p = preword;
4749 while (*skiptowhite(p) != NUL)
4750 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00004751 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00004752 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00004753 compflags + sp->ts_compsplit))
4754 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004755
4756 if (slang->sl_nosplitsugs)
4757 newscore += SCORE_SPLIT_NO;
4758 else
4759 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004760
4761 /* Give a bonus to words seen before. */
4762 newscore = score_wordcount_adj(slang, newscore,
4763 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004764 }
4765
Bram Moolenaar4770d092006-01-12 23:22:24 +00004766 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004768 go_deeper(stack, depth, newscore);
4769#ifdef DEBUG_TRIEWALK
4770 if (!try_compound && !fword_ends)
4771 sprintf(changename[depth], "%.*s-%s: split",
4772 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4773 else
4774 sprintf(changename[depth], "%.*s-%s: compound",
4775 sp->ts_twordlen, tword, fword + sp->ts_fidx);
4776#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004778 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004779 PROF_STORE(sp->ts_state)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004780 sp->ts_state = STATE_SPLITUNDO;
4781
4782 ++depth;
4783 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004785 /* Append a space to preword when splitting. */
4786 if (!try_compound && !fword_ends)
4787 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004788 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004789 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00004790 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004791
4792 /* If the badword has a non-word character at this
4793 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004794 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004795 * character when the word ends. But only when the
4796 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004797 if (((!try_compound && !spell_iswordp_nmw(fword
Bram Moolenaarcc63c642013-11-12 04:44:01 +01004798 + sp->ts_fidx,
4799 curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004800 || fword_ends)
4801 && fword[sp->ts_fidx] != NUL
4802 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004803 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004804 int l;
4805
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004806 l = MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004807 if (fword_ends)
4808 {
4809 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004810 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004811 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004812 sp->ts_prewordlen += l;
4813 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004814 }
4815 else
4816 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
4817 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004818 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00004819
Bram Moolenaard12a1322005-08-21 22:08:24 +00004820 /* When compounding include compound flag in
4821 * compflags[] (already set above). When splitting we
4822 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004823 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004824 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004825 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00004826 sp->ts_compsplit = sp->ts_complen;
4827 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004828
Bram Moolenaar53805d12005-08-01 07:08:33 +00004829 /* set su->su_badflags to the caps type at this
4830 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004831 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00004832 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004833 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004834 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004835 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004836 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004839 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004840
4841 /* If there are postponed prefixes, try these too. */
4842 if (pbyts != NULL)
4843 {
4844 byts = pbyts;
4845 idxs = pidxs;
4846 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004847 PROF_STORE(sp->ts_state)
Bram Moolenaard12a1322005-08-21 22:08:24 +00004848 sp->ts_state = STATE_NOPREFIX;
4849 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 }
4851 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004852 }
4853 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854
Bram Moolenaar4770d092006-01-12 23:22:24 +00004855 case STATE_SPLITUNDO:
4856 /* Undo the changes done for word split or compound word. */
4857 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858
Bram Moolenaar4770d092006-01-12 23:22:24 +00004859 /* Continue looking for NUL bytes. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004860 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004861 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00004862
Bram Moolenaar4770d092006-01-12 23:22:24 +00004863 /* In case we went into the prefix tree. */
4864 byts = fbyts;
4865 idxs = fidxs;
4866 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867
Bram Moolenaar4770d092006-01-12 23:22:24 +00004868 case STATE_ENDNUL:
4869 /* Past the NUL bytes in the node. */
4870 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004871 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004872 {
4873 /* The badword ends, can't use STATE_PLAIN. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004874 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004875 sp->ts_state = STATE_DEL;
4876 break;
4877 }
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004878 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004879 sp->ts_state = STATE_PLAIN;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004880 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004881
4882 case STATE_PLAIN:
4883 /*
4884 * Go over all possible bytes at this node, add each to tword[]
4885 * and use child node. "ts_curi" is the index.
4886 */
4887 arridx = sp->ts_arridx;
4888 if (sp->ts_curi > byts[arridx])
4889 {
4890 /* Done all bytes at this node, do next state. When still at
4891 * already changed bytes skip the other tricks. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01004892 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004893 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004896 sp->ts_state = STATE_FINAL;
4897 }
4898 else
4899 {
4900 arridx += sp->ts_curi++;
4901 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004902
Bram Moolenaar4770d092006-01-12 23:22:24 +00004903 /* Normal byte, go one level deeper. If it's not equal to the
4904 * byte in the bad word adjust the score. But don't even try
4905 * when the byte was already changed. And don't try when we
Bram Moolenaar4de6a212014-03-08 16:13:44 +01004906 * just deleted this byte, accepting it is always cheaper than
Bram Moolenaar4770d092006-01-12 23:22:24 +00004907 * delete + substitute. */
4908 if (c == fword[sp->ts_fidx]
Bram Moolenaar264b74f2019-01-24 17:18:42 +01004909 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004910 newscore = 0;
4911 else
4912 newscore = SCORE_SUBST;
4913 if ((newscore == 0
4914 || (sp->ts_fidx >= sp->ts_fidxtry
4915 && ((sp->ts_flags & TSF_DIDDEL) == 0
4916 || c != fword[sp->ts_delidx])))
4917 && TRY_DEEPER(su, stack, depth, newscore))
4918 {
4919 go_deeper(stack, depth, newscore);
4920#ifdef DEBUG_TRIEWALK
4921 if (newscore > 0)
4922 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
4923 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4924 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004925 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004926 sprintf(changename[depth], "%.*s-%s: accept %c",
4927 sp->ts_twordlen, tword, fword + sp->ts_fidx,
4928 fword[sp->ts_fidx]);
4929#endif
4930 ++depth;
4931 sp = &stack[depth];
4932 ++sp->ts_fidx;
4933 tword[sp->ts_twordlen++] = c;
4934 sp->ts_arridx = idxs[arridx];
Bram Moolenaar4770d092006-01-12 23:22:24 +00004935 if (newscore == SCORE_SUBST)
4936 sp->ts_isdiff = DIFF_YES;
4937 if (has_mbyte)
4938 {
4939 /* Multi-byte characters are a bit complicated to
4940 * handle: They differ when any of the bytes differ
4941 * and then their length may also differ. */
4942 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004943 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004944 /* First byte. */
4945 sp->ts_tcharidx = 0;
4946 sp->ts_tcharlen = MB_BYTE2LEN(c);
4947 sp->ts_fcharstart = sp->ts_fidx - 1;
4948 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +00004949 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004950 }
4951 else if (sp->ts_isdiff == DIFF_INSERT)
4952 /* When inserting trail bytes don't advance in the
4953 * bad word. */
4954 --sp->ts_fidx;
4955 if (++sp->ts_tcharidx == sp->ts_tcharlen)
4956 {
4957 /* Last byte of character. */
4958 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +00004959 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00004960 /* Correct ts_fidx for the byte length of the
4961 * character (we didn't check that before). */
4962 sp->ts_fidx = sp->ts_fcharstart
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02004963 + MB_PTR2LEN(
4964 fword + sp->ts_fcharstart);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004965 /* For changing a composing character adjust
4966 * the score from SCORE_SUBST to
4967 * SCORE_SUBCOMP. */
4968 if (enc_utf8
4969 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004970 utf_ptr2char(tword
Bram Moolenaar4770d092006-01-12 23:22:24 +00004971 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004972 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004973 && utf_iscomposing(
Bram Moolenaarace95982017-03-29 17:30:27 +02004974 utf_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004975 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004976 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00004977 SCORE_SUBST - SCORE_SUBCOMP;
4978
Bram Moolenaar4770d092006-01-12 23:22:24 +00004979 /* For a similar character adjust score from
4980 * SCORE_SUBST to SCORE_SIMILAR. */
4981 else if (!soundfold
4982 && slang->sl_has_map
4983 && similar_chars(slang,
4984 mb_ptr2char(tword
4985 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +00004986 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +00004987 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +00004988 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +00004989 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +00004990 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +00004991 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00004992 else if (sp->ts_isdiff == DIFF_INSERT
4993 && sp->ts_twordlen > sp->ts_tcharlen)
4994 {
4995 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
4996 c = mb_ptr2char(p);
4997 if (enc_utf8 && utf_iscomposing(c))
4998 {
4999 /* Inserting a composing char doesn't
5000 * count that much. */
5001 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
5002 }
5003 else
5004 {
5005 /* If the previous character was the same,
5006 * thus doubling a character, give a bonus
5007 * to the score. Also for the soundfold
5008 * tree (might seem illogical but does
5009 * give better scores). */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005010 MB_PTR_BACK(tword, p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005011 if (c == mb_ptr2char(p))
5012 sp->ts_score -= SCORE_INS
5013 - SCORE_INSDUP;
5014 }
5015 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005016
Bram Moolenaar4770d092006-01-12 23:22:24 +00005017 /* Starting a new char, reset the length. */
5018 sp->ts_tcharlen = 0;
5019 }
Bram Moolenaarea408852005-06-25 22:49:46 +00005020 }
Bram Moolenaarea424162005-06-16 21:51:00 +00005021 else
Bram Moolenaarea408852005-06-25 22:49:46 +00005022 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005023 /* If we found a similar char adjust the score.
5024 * We do this after calling go_deeper() because
5025 * it's slow. */
5026 if (newscore != 0
5027 && !soundfold
5028 && slang->sl_has_map
5029 && similar_chars(slang,
5030 c, fword[sp->ts_fidx - 1]))
5031 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +00005032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005033 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005034 }
5035 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036
Bram Moolenaar4770d092006-01-12 23:22:24 +00005037 case STATE_DEL:
Bram Moolenaar4770d092006-01-12 23:22:24 +00005038 /* When past the first byte of a multi-byte char don't try
5039 * delete/insert/swap a character. */
5040 if (has_mbyte && sp->ts_tcharlen > 0)
5041 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005042 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005043 sp->ts_state = STATE_FINAL;
5044 break;
5045 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005046 /*
5047 * Try skipping one character in the bad word (delete it).
5048 */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005049 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005050 sp->ts_state = STATE_INS_PREP;
5051 sp->ts_curi = 1;
5052 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
5053 /* Deleting a vowel at the start of a word counts less, see
5054 * soundalike_score(). */
5055 newscore = 2 * SCORE_DEL / 3;
5056 else
5057 newscore = SCORE_DEL;
5058 if (fword[sp->ts_fidx] != NUL
5059 && TRY_DEEPER(su, stack, depth, newscore))
5060 {
5061 go_deeper(stack, depth, newscore);
5062#ifdef DEBUG_TRIEWALK
5063 sprintf(changename[depth], "%.*s-%s: delete %c",
5064 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5065 fword[sp->ts_fidx]);
5066#endif
5067 ++depth;
5068
5069 /* Remember what character we deleted, so that we can avoid
5070 * inserting it again. */
5071 stack[depth].ts_flags |= TSF_DIDDEL;
5072 stack[depth].ts_delidx = sp->ts_fidx;
5073
5074 /* Advance over the character in fword[]. Give a bonus to the
5075 * score if the same character is following "nn" -> "n". It's
5076 * a bit illogical for soundfold tree but it does give better
5077 * results. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005078 if (has_mbyte)
5079 {
5080 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005081 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005082 if (enc_utf8 && utf_iscomposing(c))
5083 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
5084 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
5085 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5086 }
5087 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005088 {
5089 ++stack[depth].ts_fidx;
5090 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
5091 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
5092 }
5093 break;
5094 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005095 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005096
5097 case STATE_INS_PREP:
5098 if (sp->ts_flags & TSF_DIDDEL)
5099 {
5100 /* If we just deleted a byte then inserting won't make sense,
5101 * a substitute is always cheaper. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005102 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005103 sp->ts_state = STATE_SWAP;
5104 break;
5105 }
5106
5107 /* skip over NUL bytes */
5108 n = sp->ts_arridx;
5109 for (;;)
5110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005111 if (sp->ts_curi > byts[n])
5112 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005113 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005114 PROF_STORE(sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005115 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005116 break;
5117 }
5118 if (byts[n + sp->ts_curi] != NUL)
5119 {
5120 /* Found a byte to insert. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005121 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005122 sp->ts_state = STATE_INS;
5123 break;
5124 }
5125 ++sp->ts_curi;
5126 }
5127 break;
5128
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005129 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005130
5131 case STATE_INS:
5132 /* Insert one byte. Repeat this for each possible byte at this
5133 * node. */
5134 n = sp->ts_arridx;
5135 if (sp->ts_curi > byts[n])
5136 {
5137 /* Done all bytes at this node, go to next state. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005138 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005139 sp->ts_state = STATE_SWAP;
5140 break;
5141 }
5142
5143 /* Do one more byte at this node, but:
5144 * - Skip NUL bytes.
5145 * - Skip the byte if it's equal to the byte in the word,
5146 * accepting that byte is always better.
5147 */
5148 n += sp->ts_curi++;
5149 c = byts[n];
5150 if (soundfold && sp->ts_twordlen == 0 && c == '*')
5151 /* Inserting a vowel at the start of a word counts less,
5152 * see soundalike_score(). */
5153 newscore = 2 * SCORE_INS / 3;
5154 else
5155 newscore = SCORE_INS;
5156 if (c != fword[sp->ts_fidx]
5157 && TRY_DEEPER(su, stack, depth, newscore))
5158 {
5159 go_deeper(stack, depth, newscore);
5160#ifdef DEBUG_TRIEWALK
5161 sprintf(changename[depth], "%.*s-%s: insert %c",
5162 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5163 c);
5164#endif
5165 ++depth;
5166 sp = &stack[depth];
5167 tword[sp->ts_twordlen++] = c;
5168 sp->ts_arridx = idxs[n];
Bram Moolenaar4770d092006-01-12 23:22:24 +00005169 if (has_mbyte)
5170 {
5171 fl = MB_BYTE2LEN(c);
5172 if (fl > 1)
5173 {
5174 /* There are following bytes for the same character.
5175 * We must find all bytes before trying
5176 * delete/insert/swap/etc. */
5177 sp->ts_tcharlen = fl;
5178 sp->ts_tcharidx = 1;
5179 sp->ts_isdiff = DIFF_INSERT;
5180 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005181 }
5182 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005183 fl = 1;
5184 if (fl == 1)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005185 {
5186 /* If the previous character was the same, thus doubling a
5187 * character, give a bonus to the score. Also for
5188 * soundfold words (illogical but does give a better
5189 * score). */
5190 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +00005191 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005192 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005194 }
5195 break;
5196
5197 case STATE_SWAP:
5198 /*
5199 * Swap two bytes in the bad word: "12" -> "21".
5200 * We change "fword" here, it's changed back afterwards at
5201 * STATE_UNSWAP.
5202 */
5203 p = fword + sp->ts_fidx;
5204 c = *p;
5205 if (c == NUL)
5206 {
5207 /* End of word, can't swap or replace. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005208 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005209 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212
Bram Moolenaar4770d092006-01-12 23:22:24 +00005213 /* Don't swap if the first character is not a word character.
5214 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005216 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005217 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005218 sp->ts_state = STATE_REP_INI;
5219 break;
5220 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005221
Bram Moolenaar4770d092006-01-12 23:22:24 +00005222 if (has_mbyte)
5223 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005224 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005225 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005226 if (p[n] == NUL)
5227 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005228 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005229 c2 = c; /* don't swap non-word char */
5230 else
5231 c2 = mb_ptr2char(p + n);
5232 }
5233 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005234 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005235 if (p[1] == NUL)
5236 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005237 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005238 c2 = c; /* don't swap non-word char */
5239 else
5240 c2 = p[1];
5241 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005242
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005243 /* When the second character is NUL we can't swap. */
5244 if (c2 == NUL)
5245 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005246 PROF_STORE(sp->ts_state)
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +00005247 sp->ts_state = STATE_REP_INI;
5248 break;
5249 }
5250
Bram Moolenaar4770d092006-01-12 23:22:24 +00005251 /* When characters are identical, swap won't do anything.
5252 * Also get here if the second char is not a word character. */
5253 if (c == c2)
5254 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005255 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005256 sp->ts_state = STATE_SWAP3;
5257 break;
5258 }
5259 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
5260 {
5261 go_deeper(stack, depth, SCORE_SWAP);
5262#ifdef DEBUG_TRIEWALK
5263 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
5264 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5265 c, c2);
5266#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005267 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005268 sp->ts_state = STATE_UNSWAP;
5269 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005270 if (has_mbyte)
5271 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005272 fl = mb_char2len(c2);
5273 mch_memmove(p, p + n, fl);
5274 mb_char2bytes(c, p + fl);
5275 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005276 }
5277 else
Bram Moolenaarbb15b652005-10-03 21:52:09 +00005278 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005279 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005280 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005281 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +00005282 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005283 }
5284 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005285 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005286 /* If this swap doesn't work then SWAP3 won't either. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005287 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005288 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005289 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005290 break;
Bram Moolenaarea424162005-06-16 21:51:00 +00005291
Bram Moolenaar4770d092006-01-12 23:22:24 +00005292 case STATE_UNSWAP:
5293 /* Undo the STATE_SWAP swap: "21" -> "12". */
5294 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005295 if (has_mbyte)
5296 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005297 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005298 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005299 mch_memmove(p + MB_PTR2LEN(p + n), p, n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005300 mb_char2bytes(c, p);
5301 }
5302 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005303 {
5304 c = *p;
5305 *p = p[1];
5306 p[1] = c;
5307 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005308 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005309
5310 case STATE_SWAP3:
5311 /* Swap two bytes, skipping one: "123" -> "321". We change
5312 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
5313 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005314 if (has_mbyte)
5315 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005316 n = MB_CPTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005317 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005318 fl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005319 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005320 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005321 c3 = c; /* don't swap non-word char */
5322 else
5323 c3 = mb_ptr2char(p + n + fl);
5324 }
5325 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005326 {
5327 c = *p;
5328 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +02005329 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005330 c3 = c; /* don't swap non-word char */
5331 else
5332 c3 = p[2];
5333 }
5334
5335 /* When characters are identical: "121" then SWAP3 result is
5336 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
5337 * same as SWAP on next char: "112". Thus skip all swapping.
5338 * Also skip when c3 is NUL.
5339 * Also get here when the third character is not a word character.
5340 * Second character may any char: "a.b" -> "b.a" */
5341 if (c == c3 || c3 == NUL)
5342 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005343 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005344 sp->ts_state = STATE_REP_INI;
5345 break;
5346 }
5347 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5348 {
5349 go_deeper(stack, depth, SCORE_SWAP3);
5350#ifdef DEBUG_TRIEWALK
5351 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
5352 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5353 c, c3);
5354#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005355 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005356 sp->ts_state = STATE_UNSWAP3;
5357 ++depth;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005358 if (has_mbyte)
5359 {
5360 tl = mb_char2len(c3);
5361 mch_memmove(p, p + n + fl, tl);
5362 mb_char2bytes(c2, p + tl);
5363 mb_char2bytes(c, p + fl + tl);
5364 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5365 }
5366 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005367 {
5368 p[0] = p[2];
5369 p[2] = c;
5370 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5371 }
5372 }
5373 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005374 {
5375 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005376 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005377 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005378 break;
5379
5380 case STATE_UNSWAP3:
5381 /* Undo STATE_SWAP3: "321" -> "123" */
5382 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005383 if (has_mbyte)
5384 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005385 n = MB_PTR2LEN(p);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005386 c2 = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005387 fl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005388 c = mb_ptr2char(p + n + fl);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005389 tl = MB_PTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005390 mch_memmove(p + fl + tl, p, n);
5391 mb_char2bytes(c, p);
5392 mb_char2bytes(c2, p + tl);
5393 p = p + tl;
5394 }
5395 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005396 {
5397 c = *p;
5398 *p = p[2];
5399 p[2] = c;
5400 ++p;
5401 }
5402
Bram Moolenaar860cae12010-06-05 23:22:07 +02005403 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005404 {
5405 /* Middle char is not a word char, skip the rotate. First and
5406 * third char were already checked at swap and swap3. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005407 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005408 sp->ts_state = STATE_REP_INI;
5409 break;
5410 }
5411
5412 /* Rotate three characters left: "123" -> "231". We change
5413 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
5414 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5415 {
5416 go_deeper(stack, depth, SCORE_SWAP3);
5417#ifdef DEBUG_TRIEWALK
5418 p = fword + sp->ts_fidx;
5419 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
5420 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5421 p[0], p[1], p[2]);
5422#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005423 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005424 sp->ts_state = STATE_UNROT3L;
5425 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005426 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +00005427 if (has_mbyte)
5428 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005429 n = MB_CPTR2LEN(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00005430 c = mb_ptr2char(p);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005431 fl = MB_CPTR2LEN(p + n);
5432 fl += MB_CPTR2LEN(p + n + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005433 mch_memmove(p, p + n, fl);
5434 mb_char2bytes(c, p + fl);
5435 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005436 }
5437 else
Bram Moolenaarea424162005-06-16 21:51:00 +00005438 {
5439 c = *p;
5440 *p = p[1];
5441 p[1] = p[2];
5442 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005443 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +00005444 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005445 }
5446 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005447 {
5448 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005449 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005450 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005451 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005452
Bram Moolenaar4770d092006-01-12 23:22:24 +00005453 case STATE_UNROT3L:
5454 /* Undo ROT3L: "231" -> "123" */
5455 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005456 if (has_mbyte)
5457 {
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005458 n = MB_PTR2LEN(p);
5459 n += MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005460 c = mb_ptr2char(p + n);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005461 tl = MB_PTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005462 mch_memmove(p + tl, p, n);
5463 mb_char2bytes(c, p);
5464 }
5465 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005466 {
5467 c = p[2];
5468 p[2] = p[1];
5469 p[1] = *p;
5470 *p = c;
5471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005472
Bram Moolenaar4770d092006-01-12 23:22:24 +00005473 /* Rotate three bytes right: "123" -> "312". We change "fword"
5474 * here, it's changed back afterwards at STATE_UNROT3R. */
5475 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
5476 {
5477 go_deeper(stack, depth, SCORE_SWAP3);
5478#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005479 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005480 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
5481 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5482 p[0], p[1], p[2]);
5483#endif
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005484 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005485 sp->ts_state = STATE_UNROT3R;
5486 ++depth;
5487 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005488 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005489 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005490 n = MB_CPTR2LEN(p);
5491 n += MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005492 c = mb_ptr2char(p + n);
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005493 tl = MB_CPTR2LEN(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005494 mch_memmove(p + tl, p, n);
5495 mb_char2bytes(c, p);
5496 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005497 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005498 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005499 {
5500 c = p[2];
5501 p[2] = p[1];
5502 p[1] = *p;
5503 *p = c;
5504 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5505 }
5506 }
5507 else
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005508 {
5509 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005510 sp->ts_state = STATE_REP_INI;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005511 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005512 break;
5513
5514 case STATE_UNROT3R:
5515 /* Undo ROT3R: "312" -> "123" */
5516 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005517 if (has_mbyte)
5518 {
5519 c = mb_ptr2char(p);
Bram Moolenaar5b276aa2017-04-22 23:49:52 +02005520 tl = MB_PTR2LEN(p);
5521 n = MB_PTR2LEN(p + tl);
5522 n += MB_PTR2LEN(p + tl + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005523 mch_memmove(p, p + tl, n);
5524 mb_char2bytes(c, p + n);
5525 }
5526 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00005527 {
5528 c = *p;
5529 *p = p[1];
5530 p[1] = p[2];
5531 p[2] = c;
5532 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005533 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005534
5535 case STATE_REP_INI:
5536 /* Check if matching with REP items from the .aff file would work.
5537 * Quickly skip if:
5538 * - there are no REP items and we are not in the soundfold trie
5539 * - the score is going to be too high anyway
5540 * - already applied a REP item or swapped here */
5541 if ((lp->lp_replang == NULL && !soundfold)
5542 || sp->ts_score + SCORE_REP >= su->su_maxscore
5543 || sp->ts_fidx < sp->ts_fidxtry)
5544 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005545 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005546 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005548 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005549
Bram Moolenaar4770d092006-01-12 23:22:24 +00005550 /* Use the first byte to quickly find the first entry that may
5551 * match. If the index is -1 there is none. */
5552 if (soundfold)
5553 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
5554 else
5555 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005556
Bram Moolenaar4770d092006-01-12 23:22:24 +00005557 if (sp->ts_curi < 0)
5558 {
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005559 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005560 sp->ts_state = STATE_FINAL;
5561 break;
5562 }
5563
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005564 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005565 sp->ts_state = STATE_REP;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005566 /* FALLTHROUGH */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005567
5568 case STATE_REP:
5569 /* Try matching with REP items from the .aff file. For each match
5570 * replace the characters and check if the resulting word is
5571 * valid. */
5572 p = fword + sp->ts_fidx;
5573
5574 if (soundfold)
5575 gap = &slang->sl_repsal;
5576 else
5577 gap = &lp->lp_replang->sl_rep;
5578 while (sp->ts_curi < gap->ga_len)
5579 {
5580 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5581 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005582 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005583 /* past possible matching entries */
5584 sp->ts_curi = gap->ga_len;
5585 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005586 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005587 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5588 && TRY_DEEPER(su, stack, depth, SCORE_REP))
5589 {
5590 go_deeper(stack, depth, SCORE_REP);
5591#ifdef DEBUG_TRIEWALK
5592 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
5593 sp->ts_twordlen, tword, fword + sp->ts_fidx,
5594 ftp->ft_from, ftp->ft_to);
5595#endif
5596 /* Need to undo this afterwards. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005597 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005598 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005599
Bram Moolenaar4770d092006-01-12 23:22:24 +00005600 /* Change the "from" to the "to" string. */
5601 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005602 fl = (int)STRLEN(ftp->ft_from);
5603 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005604 if (fl != tl)
5605 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005606 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005607 repextra += tl - fl;
5608 }
5609 mch_memmove(p, ftp->ft_to, tl);
5610 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005611 stack[depth].ts_tcharlen = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005612 break;
5613 }
5614 }
5615
5616 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005617 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005618 /* No (more) matches. */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005619 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005620 sp->ts_state = STATE_FINAL;
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005621 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005622
5623 break;
5624
5625 case STATE_REP_UNDO:
5626 /* Undo a REP replacement and continue with the next one. */
5627 if (soundfold)
5628 gap = &slang->sl_repsal;
5629 else
5630 gap = &lp->lp_replang->sl_rep;
5631 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005632 fl = (int)STRLEN(ftp->ft_from);
5633 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005634 p = fword + sp->ts_fidx;
5635 if (fl != tl)
5636 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005637 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005638 repextra -= tl - fl;
5639 }
5640 mch_memmove(p, ftp->ft_from, fl);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01005641 PROF_STORE(sp->ts_state)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005642 sp->ts_state = STATE_REP;
5643 break;
5644
5645 default:
5646 /* Did all possible states at this level, go up one level. */
5647 --depth;
5648
5649 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
5650 {
5651 /* Continue in or go back to the prefix tree. */
5652 byts = pbyts;
5653 idxs = pidxs;
5654 }
5655
5656 /* Don't check for CTRL-C too often, it takes time. */
5657 if (--breakcheckcount == 0)
5658 {
5659 ui_breakcheck();
5660 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005661 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005662 }
5663 }
5664}
5665
Bram Moolenaar4770d092006-01-12 23:22:24 +00005666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005667/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00005668 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005669 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005670 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005671go_deeper(trystate_T *stack, int depth, int score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005672{
Bram Moolenaarea424162005-06-16 21:51:00 +00005673 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005674 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005675 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005676 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00005677 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005678}
5679
Bram Moolenaar53805d12005-08-01 07:08:33 +00005680/*
5681 * Case-folding may change the number of bytes: Count nr of chars in
5682 * fword[flen] and return the byte length of that many chars in "word".
5683 */
5684 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005685nofold_len(char_u *fword, int flen, char_u *word)
Bram Moolenaar53805d12005-08-01 07:08:33 +00005686{
5687 char_u *p;
5688 int i = 0;
5689
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005690 for (p = fword; p < fword + flen; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005691 ++i;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005692 for (p = word; i > 0; MB_PTR_ADV(p))
Bram Moolenaar53805d12005-08-01 07:08:33 +00005693 --i;
5694 return (int)(p - word);
5695}
Bram Moolenaar53805d12005-08-01 07:08:33 +00005696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005697/*
5698 * "fword" is a good word with case folded. Find the matching keep-case
5699 * words and put it in "kword".
5700 * Theoretically there could be several keep-case words that result in the
5701 * same case-folded word, but we only find one...
5702 */
5703 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005704find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005705{
5706 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5707 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005708 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005709
5710 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005711 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005712 int round[MAXWLEN];
5713 int fwordidx[MAXWLEN];
5714 int uwordidx[MAXWLEN];
5715 int kwordlen[MAXWLEN];
5716
5717 int flen, ulen;
5718 int l;
5719 int len;
5720 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005721 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005722 char_u *p;
5723 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005724 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005725
5726 if (byts == NULL)
5727 {
5728 /* array is empty: "cannot happen" */
5729 *kword = NUL;
5730 return;
5731 }
5732
5733 /* Make an all-cap version of "fword". */
5734 allcap_copy(fword, uword);
5735
5736 /*
5737 * Each character needs to be tried both case-folded and upper-case.
5738 * All this gets very complicated if we keep in mind that changing case
5739 * may change the byte length of a multi-byte character...
5740 */
5741 depth = 0;
5742 arridx[0] = 0;
5743 round[0] = 0;
5744 fwordidx[0] = 0;
5745 uwordidx[0] = 0;
5746 kwordlen[0] = 0;
5747 while (depth >= 0)
5748 {
5749 if (fword[fwordidx[depth]] == NUL)
5750 {
5751 /* We are at the end of "fword". If the tree allows a word to end
5752 * here we have found a match. */
5753 if (byts[arridx[depth] + 1] == 0)
5754 {
5755 kword[kwordlen[depth]] = NUL;
5756 return;
5757 }
5758
5759 /* kword is getting too long, continue one level up */
5760 --depth;
5761 }
5762 else if (++round[depth] > 2)
5763 {
5764 /* tried both fold-case and upper-case character, continue one
5765 * level up */
5766 --depth;
5767 }
5768 else
5769 {
5770 /*
5771 * round[depth] == 1: Try using the folded-case character.
5772 * round[depth] == 2: Try using the upper-case character.
5773 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005774 if (has_mbyte)
5775 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02005776 flen = MB_CPTR2LEN(fword + fwordidx[depth]);
5777 ulen = MB_CPTR2LEN(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005778 }
5779 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005780 ulen = flen = 1;
5781 if (round[depth] == 1)
5782 {
5783 p = fword + fwordidx[depth];
5784 l = flen;
5785 }
5786 else
5787 {
5788 p = uword + uwordidx[depth];
5789 l = ulen;
5790 }
5791
5792 for (tryidx = arridx[depth]; l > 0; --l)
5793 {
5794 /* Perform a binary search in the list of accepted bytes. */
5795 len = byts[tryidx++];
5796 c = *p++;
5797 lo = tryidx;
5798 hi = tryidx + len - 1;
5799 while (lo < hi)
5800 {
5801 m = (lo + hi) / 2;
5802 if (byts[m] > c)
5803 hi = m - 1;
5804 else if (byts[m] < c)
5805 lo = m + 1;
5806 else
5807 {
5808 lo = hi = m;
5809 break;
5810 }
5811 }
5812
5813 /* Stop if there is no matching byte. */
5814 if (hi < lo || byts[lo] != c)
5815 break;
5816
5817 /* Continue at the child (if there is one). */
5818 tryidx = idxs[lo];
5819 }
5820
5821 if (l == 0)
5822 {
5823 /*
5824 * Found the matching char. Copy it to "kword" and go a
5825 * level deeper.
5826 */
5827 if (round[depth] == 1)
5828 {
5829 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5830 flen);
5831 kwordlen[depth + 1] = kwordlen[depth] + flen;
5832 }
5833 else
5834 {
5835 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5836 ulen);
5837 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5838 }
5839 fwordidx[depth + 1] = fwordidx[depth] + flen;
5840 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5841
5842 ++depth;
5843 arridx[depth] = tryidx;
5844 round[depth] = 0;
5845 }
5846 }
5847 }
5848
5849 /* Didn't find it: "cannot happen". */
5850 *kword = NUL;
5851}
5852
5853/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005854 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
5855 * su->su_sga.
5856 */
5857 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005858score_comp_sal(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005859{
5860 langp_T *lp;
5861 char_u badsound[MAXWLEN];
5862 int i;
5863 suggest_T *stp;
5864 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005865 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005866 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005867
5868 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
5869 return;
5870
5871 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005872 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005873 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005875 if (lp->lp_slang->sl_sal.ga_len > 0)
5876 {
5877 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005878 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005879
5880 for (i = 0; i < su->su_ga.ga_len; ++i)
5881 {
5882 stp = &SUG(su->su_ga, i);
5883
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005884 /* Case-fold the suggested word, sound-fold it and compute the
5885 * sound-a-like score. */
5886 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005887 if (score < SCORE_MAXMAX)
5888 {
5889 /* Add the suggestion. */
5890 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
5891 sstp->st_word = vim_strsave(stp->st_word);
5892 if (sstp->st_word != NULL)
5893 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005894 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005895 sstp->st_score = score;
5896 sstp->st_altscore = 0;
5897 sstp->st_orglen = stp->st_orglen;
5898 ++su->su_sga.ga_len;
5899 }
5900 }
5901 }
5902 break;
5903 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005904 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005905}
5906
5907/*
5908 * Combine the list of suggestions in su->su_ga and su->su_sga.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005909 * They are entwined.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005910 */
5911 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005912score_combine(suginfo_T *su)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005913{
5914 int i;
5915 int j;
5916 garray_T ga;
5917 garray_T *gap;
5918 langp_T *lp;
5919 suggest_T *stp;
5920 char_u *p;
5921 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005922 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005923 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005924 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005925
5926 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005927 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005928 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005929 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005930 if (lp->lp_slang->sl_sal.ga_len > 0)
5931 {
5932 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005933 slang = lp->lp_slang;
5934 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005935
5936 for (i = 0; i < su->su_ga.ga_len; ++i)
5937 {
5938 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005939 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005940 if (stp->st_altscore == SCORE_MAXMAX)
5941 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
5942 else
5943 stp->st_score = (stp->st_score * 3
5944 + stp->st_altscore) / 4;
5945 stp->st_salscore = FALSE;
5946 }
5947 break;
5948 }
5949 }
5950
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005951 if (slang == NULL) /* Using "double" without sound folding. */
5952 {
5953 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
5954 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005955 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005956 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005957
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005958 /* Add the alternate score to su_sga. */
5959 for (i = 0; i < su->su_sga.ga_len; ++i)
5960 {
5961 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005962 stp->st_altscore = spell_edit_score(slang,
5963 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005964 if (stp->st_score == SCORE_MAXMAX)
5965 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
5966 else
5967 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
5968 stp->st_salscore = TRUE;
5969 }
5970
Bram Moolenaar4770d092006-01-12 23:22:24 +00005971 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
5972 * for both lists. */
5973 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005974 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005975 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005976 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
5977
5978 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
5979 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
5980 return;
5981
5982 stp = &SUG(ga, 0);
5983 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
5984 {
5985 /* round 1: get a suggestion from su_ga
5986 * round 2: get a suggestion from su_sga */
5987 for (round = 1; round <= 2; ++round)
5988 {
5989 gap = round == 1 ? &su->su_ga : &su->su_sga;
5990 if (i < gap->ga_len)
5991 {
5992 /* Don't add a word if it's already there. */
5993 p = SUG(*gap, i).st_word;
5994 for (j = 0; j < ga.ga_len; ++j)
5995 if (STRCMP(stp[j].st_word, p) == 0)
5996 break;
5997 if (j == ga.ga_len)
5998 stp[ga.ga_len++] = SUG(*gap, i);
5999 else
6000 vim_free(p);
6001 }
6002 }
6003 }
6004
6005 ga_clear(&su->su_ga);
6006 ga_clear(&su->su_sga);
6007
6008 /* Truncate the list to the number of suggestions that will be displayed. */
6009 if (ga.ga_len > su->su_maxcount)
6010 {
6011 for (i = su->su_maxcount; i < ga.ga_len; ++i)
6012 vim_free(stp[i].st_word);
6013 ga.ga_len = su->su_maxcount;
6014 }
6015
6016 su->su_ga = ga;
6017}
6018
6019/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006020 * For the goodword in "stp" compute the soundalike score compared to the
6021 * badword.
6022 */
6023 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006024stp_sal_score(
6025 suggest_T *stp,
6026 suginfo_T *su,
6027 slang_T *slang,
6028 char_u *badsound) /* sound-folded badword */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006029{
6030 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006031 char_u *pbad;
6032 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006033 char_u badsound2[MAXWLEN];
6034 char_u fword[MAXWLEN];
6035 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006036 char_u goodword[MAXWLEN];
6037 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006038
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006039 lendiff = (int)(su->su_badlen - stp->st_orglen);
6040 if (lendiff >= 0)
6041 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006042 else
6043 {
6044 /* soundfold the bad word with more characters following */
6045 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
6046
6047 /* When joining two words the sound often changes a lot. E.g., "t he"
6048 * sounds like "t h" while "the" sounds like "@". Avoid that by
6049 * removing the space. Don't do it when the good word also contains a
6050 * space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01006051 if (VIM_ISWHITE(su->su_badptr[su->su_badlen])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006052 && *skiptowhite(stp->st_word) == NUL)
6053 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +00006054 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006055
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006056 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006057 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006058 }
6059
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006060 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006061 {
6062 /* Add part of the bad word to the good word, so that we soundfold
6063 * what replaces the bad word. */
6064 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006065 vim_strncpy(goodword + stp->st_wordlen,
6066 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006067 pgood = goodword;
6068 }
6069 else
6070 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006071
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006072 /* Sound-fold the word and compute the score for the difference. */
6073 spell_soundfold(slang, pgood, FALSE, goodsound);
6074
6075 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006076}
6077
Bram Moolenaar4770d092006-01-12 23:22:24 +00006078/* structure used to store soundfolded words that add_sound_suggest() has
6079 * handled already. */
6080typedef struct
6081{
6082 short sft_score; /* lowest score used */
6083 char_u sft_word[1]; /* soundfolded word, actually longer */
6084} sftword_T;
6085
6086static sftword_T dumsft;
6087#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
6088#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
6089
6090/*
6091 * Prepare for calling suggest_try_soundalike().
6092 */
6093 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006094suggest_try_soundalike_prep(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006095{
6096 langp_T *lp;
6097 int lpi;
6098 slang_T *slang;
6099
6100 /* Do this for all languages that support sound folding and for which a
6101 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006102 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006103 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006104 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006105 slang = lp->lp_slang;
6106 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6107 /* prepare the hashtable used by add_sound_suggest() */
6108 hash_init(&slang->sl_sounddone);
6109 }
6110}
6111
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006112/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006113 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006114 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006115 */
6116 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006117suggest_try_soundalike(suginfo_T *su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006118{
6119 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006120 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006121 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006122 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006123
Bram Moolenaar4770d092006-01-12 23:22:24 +00006124 /* Do this for all languages that support sound folding and for which a
6125 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006126 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006127 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006128 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006129 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006130 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006131 {
6132 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006133 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006134
Bram Moolenaar4770d092006-01-12 23:22:24 +00006135 /* try all kinds of inserts/deletes/swaps/etc. */
6136 /* TODO: also soundfold the next words, so that we can try joining
6137 * and splitting */
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006138#ifdef SUGGEST_PROFILE
6139 prof_init();
6140#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006141 suggest_trie_walk(su, lp, salword, TRUE);
Bram Moolenaarca1fe982016-01-07 16:22:06 +01006142#ifdef SUGGEST_PROFILE
6143 prof_report("soundalike");
6144#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00006145 }
6146 }
6147}
6148
6149/*
6150 * Finish up after calling suggest_try_soundalike().
6151 */
6152 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006153suggest_try_soundalike_finish(void)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006154{
6155 langp_T *lp;
6156 int lpi;
6157 slang_T *slang;
6158 int todo;
6159 hashitem_T *hi;
6160
6161 /* Do this for all languages that support sound folding and for which a
6162 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006163 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006164 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006165 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006166 slang = lp->lp_slang;
6167 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
6168 {
6169 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006170 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006171 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
6172 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006173 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006174 vim_free(HI2SFT(hi));
6175 --todo;
6176 }
Bram Moolenaar6417da62007-03-08 13:49:53 +00006177
6178 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006179 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +00006180 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006181 }
6182 }
6183}
6184
6185/*
6186 * A match with a soundfolded word is found. Add the good word(s) that
6187 * produce this soundfolded word.
6188 */
6189 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006190add_sound_suggest(
6191 suginfo_T *su,
6192 char_u *goodword,
6193 int score, /* soundfold score */
6194 langp_T *lp)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006195{
6196 slang_T *slang = lp->lp_slang; /* language for sound folding */
6197 int sfwordnr;
6198 char_u *nrline;
6199 int orgnr;
6200 char_u theword[MAXWLEN];
6201 int i;
6202 int wlen;
6203 char_u *byts;
6204 idx_T *idxs;
6205 int n;
6206 int wordcount;
6207 int wc;
6208 int goodscore;
6209 hash_T hash;
6210 hashitem_T *hi;
6211 sftword_T *sft;
6212 int bc, gc;
6213 int limit;
6214
6215 /*
6216 * It's very well possible that the same soundfold word is found several
6217 * times with different scores. Since the following is quite slow only do
6218 * the words that have a better score than before. Use a hashtable to
6219 * remember the words that have been done.
6220 */
6221 hash = hash_hash(goodword);
6222 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
6223 if (HASHITEM_EMPTY(hi))
6224 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00006225 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
6226 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00006227 if (sft != NULL)
6228 {
6229 sft->sft_score = score;
6230 STRCPY(sft->sft_word, goodword);
6231 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
6232 }
6233 }
6234 else
6235 {
6236 sft = HI2SFT(hi);
6237 if (score >= sft->sft_score)
6238 return;
6239 sft->sft_score = score;
6240 }
6241
6242 /*
6243 * Find the word nr in the soundfold tree.
6244 */
6245 sfwordnr = soundfold_find(slang, goodword);
6246 if (sfwordnr < 0)
6247 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01006248 internal_error("add_sound_suggest()");
Bram Moolenaar4770d092006-01-12 23:22:24 +00006249 return;
6250 }
6251
6252 /*
6253 * go over the list of good words that produce this soundfold word
6254 */
6255 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
6256 orgnr = 0;
6257 while (*nrline != NUL)
6258 {
6259 /* The wordnr was stored in a minimal nr of bytes as an offset to the
6260 * previous wordnr. */
6261 orgnr += bytes2offset(&nrline);
6262
6263 byts = slang->sl_fbyts;
6264 idxs = slang->sl_fidxs;
6265
6266 /* Lookup the word "orgnr" one of the two tries. */
6267 n = 0;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006268 wordcount = 0;
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006269 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006270 {
6271 i = 1;
6272 if (wordcount == orgnr && byts[n + 1] == NUL)
6273 break; /* found end of word */
6274
6275 if (byts[n + 1] == NUL)
6276 ++wordcount;
6277
6278 /* skip over the NUL bytes */
6279 for ( ; byts[n + i] == NUL; ++i)
6280 if (i > byts[n]) /* safety check */
6281 {
6282 STRCPY(theword + wlen, "BAD");
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006283 wlen += 3;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006284 goto badword;
6285 }
6286
6287 /* One of the siblings must have the word. */
6288 for ( ; i < byts[n]; ++i)
6289 {
6290 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
6291 if (wordcount + wc > orgnr)
6292 break;
6293 wordcount += wc;
6294 }
6295
Bram Moolenaarace8d8e2013-11-21 17:42:31 +01006296 theword[wlen] = byts[n + i];
Bram Moolenaar4770d092006-01-12 23:22:24 +00006297 n = idxs[n + i];
6298 }
6299badword:
6300 theword[wlen] = NUL;
6301
6302 /* Go over the possible flags and regions. */
6303 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
6304 {
6305 char_u cword[MAXWLEN];
6306 char_u *p;
6307 int flags = (int)idxs[n + i];
6308
Bram Moolenaare1438bb2006-03-01 22:01:55 +00006309 /* Skip words with the NOSUGGEST flag */
6310 if (flags & WF_NOSUGGEST)
6311 continue;
6312
Bram Moolenaar4770d092006-01-12 23:22:24 +00006313 if (flags & WF_KEEPCAP)
6314 {
6315 /* Must find the word in the keep-case tree. */
6316 find_keepcap_word(slang, theword, cword);
6317 p = cword;
6318 }
6319 else
6320 {
6321 flags |= su->su_badflags;
6322 if ((flags & WF_CAPMASK) != 0)
6323 {
6324 /* Need to fix case according to "flags". */
6325 make_case_word(theword, cword, flags);
6326 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006327 }
6328 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006329 p = theword;
6330 }
6331
6332 /* Add the suggestion. */
6333 if (sps_flags & SPS_DOUBLE)
6334 {
6335 /* Add the suggestion if the score isn't too bad. */
6336 if (score <= su->su_maxscore)
6337 add_suggestion(su, &su->su_sga, p, su->su_badlen,
6338 score, 0, FALSE, slang, FALSE);
6339 }
6340 else
6341 {
6342 /* Add a penalty for words in another region. */
6343 if ((flags & WF_REGION)
6344 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
6345 goodscore = SCORE_REGION;
6346 else
6347 goodscore = 0;
6348
6349 /* Add a small penalty for changing the first letter from
6350 * lower to upper case. Helps for "tath" -> "Kath", which is
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006351 * less common than "tath" -> "path". Don't do it when the
Bram Moolenaar4770d092006-01-12 23:22:24 +00006352 * letter is the same, that has already been counted. */
6353 gc = PTR2CHAR(p);
6354 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006355 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006356 bc = PTR2CHAR(su->su_badword);
6357 if (!SPELL_ISUPPER(bc)
6358 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
6359 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006360 }
6361
Bram Moolenaar4770d092006-01-12 23:22:24 +00006362 /* Compute the score for the good word. This only does letter
6363 * insert/delete/swap/replace. REP items are not considered,
6364 * which may make the score a bit higher.
6365 * Use a limit for the score to make it work faster. Use
6366 * MAXSCORE(), because RESCORE() will change the score.
6367 * If the limit is very high then the iterative method is
6368 * inefficient, using an array is quicker. */
6369 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
6370 if (limit > SCORE_LIMITMAX)
6371 goodscore += spell_edit_score(slang, su->su_badword, p);
6372 else
6373 goodscore += spell_edit_score_limit(slang, su->su_badword,
6374 p, limit);
6375
6376 /* When going over the limit don't bother to do the rest. */
6377 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006378 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006379 /* Give a bonus to words seen before. */
6380 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006381
Bram Moolenaar4770d092006-01-12 23:22:24 +00006382 /* Add the suggestion if the score isn't too bad. */
6383 goodscore = RESCORE(goodscore, score);
6384 if (goodscore <= su->su_sfmaxscore)
6385 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6386 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006388 }
6389 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006390 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006391 }
6392}
6393
6394/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006395 * Find word "word" in fold-case tree for "slang" and return the word number.
6396 */
6397 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006398soundfold_find(slang_T *slang, char_u *word)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006399{
6400 idx_T arridx = 0;
6401 int len;
6402 int wlen = 0;
6403 int c;
6404 char_u *ptr = word;
6405 char_u *byts;
6406 idx_T *idxs;
6407 int wordnr = 0;
6408
6409 byts = slang->sl_sbyts;
6410 idxs = slang->sl_sidxs;
6411
6412 for (;;)
6413 {
6414 /* First byte is the number of possible bytes. */
6415 len = byts[arridx++];
6416
6417 /* If the first possible byte is a zero the word could end here.
6418 * If the word ends we found the word. If not skip the NUL bytes. */
6419 c = ptr[wlen];
6420 if (byts[arridx] == NUL)
6421 {
6422 if (c == NUL)
6423 break;
6424
6425 /* Skip over the zeros, there can be several. */
6426 while (len > 0 && byts[arridx] == NUL)
6427 {
6428 ++arridx;
6429 --len;
6430 }
6431 if (len == 0)
6432 return -1; /* no children, word should have ended here */
6433 ++wordnr;
6434 }
6435
6436 /* If the word ends we didn't find it. */
6437 if (c == NUL)
6438 return -1;
6439
6440 /* Perform a binary search in the list of accepted bytes. */
6441 if (c == TAB) /* <Tab> is handled like <Space> */
6442 c = ' ';
6443 while (byts[arridx] < c)
6444 {
6445 /* The word count is in the first idxs[] entry of the child. */
6446 wordnr += idxs[idxs[arridx]];
6447 ++arridx;
6448 if (--len == 0) /* end of the bytes, didn't find it */
6449 return -1;
6450 }
6451 if (byts[arridx] != c) /* didn't find the byte */
6452 return -1;
6453
6454 /* Continue at the child (if there is one). */
6455 arridx = idxs[arridx];
6456 ++wlen;
6457
6458 /* One space in the good word may stand for several spaces in the
6459 * checked word. */
6460 if (c == ' ')
6461 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
6462 ++wlen;
6463 }
6464
6465 return wordnr;
6466}
6467
6468/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006469 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006470 */
6471 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006472make_case_word(char_u *fword, char_u *cword, int flags)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006473{
6474 if (flags & WF_ALLCAP)
6475 /* Make it all upper-case */
6476 allcap_copy(fword, cword);
6477 else if (flags & WF_ONECAP)
6478 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006479 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006480 else
6481 /* Use goodword as-is. */
6482 STRCPY(cword, fword);
6483}
6484
Bram Moolenaarea424162005-06-16 21:51:00 +00006485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006486/*
6487 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
6488 * lines in the .aff file.
6489 */
6490 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006491similar_chars(slang_T *slang, int c1, int c2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006492{
Bram Moolenaarea424162005-06-16 21:51:00 +00006493 int m1, m2;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006494 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarea424162005-06-16 21:51:00 +00006495 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006496
Bram Moolenaarea424162005-06-16 21:51:00 +00006497 if (c1 >= 256)
6498 {
6499 buf[mb_char2bytes(c1, buf)] = 0;
6500 hi = hash_find(&slang->sl_map_hash, buf);
6501 if (HASHITEM_EMPTY(hi))
6502 m1 = 0;
6503 else
6504 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6505 }
6506 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006507 m1 = slang->sl_map_array[c1];
6508 if (m1 == 0)
6509 return FALSE;
6510
6511
Bram Moolenaarea424162005-06-16 21:51:00 +00006512 if (c2 >= 256)
6513 {
6514 buf[mb_char2bytes(c2, buf)] = 0;
6515 hi = hash_find(&slang->sl_map_hash, buf);
6516 if (HASHITEM_EMPTY(hi))
6517 m2 = 0;
6518 else
6519 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
6520 }
6521 else
Bram Moolenaarea424162005-06-16 21:51:00 +00006522 m2 = slang->sl_map_array[c2];
6523
6524 return m1 == m2;
6525}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006526
6527/*
6528 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +00006529 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006530 */
6531 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006532add_suggestion(
6533 suginfo_T *su,
6534 garray_T *gap, /* either su_ga or su_sga */
6535 char_u *goodword,
6536 int badlenarg, /* len of bad word replaced with "goodword" */
6537 int score,
6538 int altscore,
6539 int had_bonus, /* value for st_had_bonus */
6540 slang_T *slang, /* language for sound folding */
6541 int maxsf) /* su_maxscore applies to soundfold score,
Bram Moolenaar4770d092006-01-12 23:22:24 +00006542 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006543{
Bram Moolenaar4770d092006-01-12 23:22:24 +00006544 int goodlen; /* len of goodword changed */
6545 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006546 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006547 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006548 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006549 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006550
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006551 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
6552 * "thee the" is added next to changing the first "the" the "thee". */
6553 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006554 pbad = su->su_badptr + badlenarg;
6555 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006556 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006557 goodlen = (int)(pgood - goodword);
6558 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006559 if (goodlen <= 0 || badlen <= 0)
6560 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006561 MB_PTR_BACK(goodword, pgood);
6562 MB_PTR_BACK(su->su_badptr, pbad);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006563 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006564 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006565 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
6566 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006567 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006568 else if (*pgood != *pbad)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006569 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006570 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00006571
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006572 if (badlen == 0 && goodlen == 0)
6573 /* goodword doesn't change anything; may happen for "the the" changing
6574 * the first "the" to itself. */
6575 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006576
Bram Moolenaar89d40322006-08-29 15:30:07 +00006577 if (gap->ga_len == 0)
6578 i = -1;
6579 else
6580 {
6581 /* Check if the word is already there. Also check the length that is
6582 * being replaced "thes," -> "these" is a different suggestion from
6583 * "thes" -> "these". */
6584 stp = &SUG(*gap, 0);
6585 for (i = gap->ga_len; --i >= 0; ++stp)
6586 if (stp->st_wordlen == goodlen
6587 && stp->st_orglen == badlen
6588 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006589 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006590 /*
6591 * Found it. Remember the word with the lowest score.
6592 */
6593 if (stp->st_slang == NULL)
6594 stp->st_slang = slang;
6595
6596 new_sug.st_score = score;
6597 new_sug.st_altscore = altscore;
6598 new_sug.st_had_bonus = had_bonus;
6599
6600 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006601 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00006602 /* Only one of the two had the soundalike score computed.
6603 * Need to do that for the other one now, otherwise the
6604 * scores can't be compared. This happens because
6605 * suggest_try_change() doesn't compute the soundalike
6606 * word to keep it fast, while some special methods set
6607 * the soundalike score to zero. */
6608 if (had_bonus)
6609 rescore_one(su, stp);
6610 else
6611 {
6612 new_sug.st_word = stp->st_word;
6613 new_sug.st_wordlen = stp->st_wordlen;
6614 new_sug.st_slang = stp->st_slang;
6615 new_sug.st_orglen = badlen;
6616 rescore_one(su, &new_sug);
6617 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006618 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006619
Bram Moolenaar89d40322006-08-29 15:30:07 +00006620 if (stp->st_score > new_sug.st_score)
6621 {
6622 stp->st_score = new_sug.st_score;
6623 stp->st_altscore = new_sug.st_altscore;
6624 stp->st_had_bonus = new_sug.st_had_bonus;
6625 }
6626 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +00006627 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00006628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006629
Bram Moolenaar4770d092006-01-12 23:22:24 +00006630 if (i < 0 && ga_grow(gap, 1) == OK)
6631 {
6632 /* Add a suggestion. */
6633 stp = &SUG(*gap, gap->ga_len);
6634 stp->st_word = vim_strnsave(goodword, goodlen);
6635 if (stp->st_word != NULL)
6636 {
6637 stp->st_wordlen = goodlen;
6638 stp->st_score = score;
6639 stp->st_altscore = altscore;
6640 stp->st_had_bonus = had_bonus;
6641 stp->st_orglen = badlen;
6642 stp->st_slang = slang;
6643 ++gap->ga_len;
6644
6645 /* If we have too many suggestions now, sort the list and keep
6646 * the best suggestions. */
6647 if (gap->ga_len > SUG_MAX_COUNT(su))
6648 {
6649 if (maxsf)
6650 su->su_sfmaxscore = cleanup_suggestions(gap,
6651 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
6652 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00006653 su->su_maxscore = cleanup_suggestions(gap,
6654 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006655 }
6656 }
6657 }
6658}
6659
6660/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00006661 * Suggestions may in fact be flagged as errors. Esp. for banned words and
6662 * for split words, such as "the the". Remove these from the list here.
6663 */
6664 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006665check_suggestions(
6666 suginfo_T *su,
6667 garray_T *gap) /* either su_ga or su_sga */
Bram Moolenaar4770d092006-01-12 23:22:24 +00006668{
6669 suggest_T *stp;
6670 int i;
6671 char_u longword[MAXWLEN + 1];
6672 int len;
6673 hlf_T attr;
6674
6675 stp = &SUG(*gap, 0);
6676 for (i = gap->ga_len - 1; i >= 0; --i)
6677 {
6678 /* Need to append what follows to check for "the the". */
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02006679 vim_strncpy(longword, stp[i].st_word, MAXWLEN);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006680 len = stp[i].st_wordlen;
6681 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
6682 MAXWLEN - len);
6683 attr = HLF_COUNT;
6684 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
6685 if (attr != HLF_COUNT)
6686 {
6687 /* Remove this entry. */
6688 vim_free(stp[i].st_word);
6689 --gap->ga_len;
6690 if (i < gap->ga_len)
6691 mch_memmove(stp + i, stp + i + 1,
6692 sizeof(suggest_T) * (gap->ga_len - i));
6693 }
6694 }
6695}
6696
6697
6698/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006699 * Add a word to be banned.
6700 */
6701 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006702add_banned(
6703 suginfo_T *su,
6704 char_u *word)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006705{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00006706 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006707 hash_T hash;
6708 hashitem_T *hi;
6709
Bram Moolenaar4770d092006-01-12 23:22:24 +00006710 hash = hash_hash(word);
6711 hi = hash_lookup(&su->su_banned, word, hash);
6712 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006713 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00006714 s = vim_strsave(word);
6715 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006716 hash_add_item(&su->su_banned, hi, s, hash);
6717 }
6718}
6719
6720/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006721 * Recompute the score for all suggestions if sound-folding is possible. This
6722 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006723 */
6724 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006725rescore_suggestions(suginfo_T *su)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006726{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006727 int i;
6728
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006729 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006730 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006731 rescore_one(su, &SUG(su->su_ga, i));
6732}
6733
6734/*
6735 * Recompute the score for one suggestion if sound-folding is possible.
6736 */
6737 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006738rescore_one(suginfo_T *su, suggest_T *stp)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006739{
6740 slang_T *slang = stp->st_slang;
6741 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +00006742 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006743
6744 /* Only rescore suggestions that have no sal score yet and do have a
6745 * language. */
6746 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
6747 {
6748 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +00006749 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006750 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006751 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006752 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +00006753 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006754 }
Bram Moolenaar4effc802005-09-30 21:12:02 +00006755
6756 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006757 if (stp->st_altscore == SCORE_MAXMAX)
6758 stp->st_altscore = SCORE_BIG;
6759 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
6760 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006761 }
6762}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006764static int
6765#ifdef __BORLANDC__
6766_RTLENTRYF
6767#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006768sug_compare(const void *s1, const void *s2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006769
6770/*
6771 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006772 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006773 */
6774 static int
6775#ifdef __BORLANDC__
6776_RTLENTRYF
6777#endif
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006778sug_compare(const void *s1, const void *s2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006779{
6780 suggest_T *p1 = (suggest_T *)s1;
6781 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006782 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006783
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006784 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +00006785 {
6786 n = p1->st_altscore - p2->st_altscore;
6787 if (n == 0)
6788 n = STRICMP(p1->st_word, p2->st_word);
6789 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006790 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006791}
6792
6793/*
6794 * Cleanup the suggestions:
6795 * - Sort on score.
6796 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006797 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006798 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006799 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006800cleanup_suggestions(
6801 garray_T *gap,
6802 int maxscore,
6803 int keep) /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006804{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006805 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006806 int i;
6807
6808 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006809 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006810
6811 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006812 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006813 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006814 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006815 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006816 gap->ga_len = keep;
6817 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006818 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006819 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006820}
6821
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006822#if defined(FEAT_EVAL) || defined(PROTO)
6823/*
6824 * Soundfold a string, for soundfold().
6825 * Result is in allocated memory, NULL for an error.
6826 */
6827 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006828eval_soundfold(char_u *word)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006829{
6830 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006831 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006832 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006833
Bram Moolenaar860cae12010-06-05 23:22:07 +02006834 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006835 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02006836 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006837 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006838 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006839 if (lp->lp_slang->sl_sal.ga_len > 0)
6840 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006841 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006842 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006843 return vim_strsave(sound);
6844 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006845 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006846
6847 /* No language with sound folding, return word as-is. */
6848 return vim_strsave(word);
6849}
6850#endif
6851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006852/*
6853 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +00006854 *
6855 * There are many ways to turn a word into a sound-a-like representation. The
6856 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
6857 * swedish name matching - survey and test of different algorithms" by Klas
6858 * Erikson.
6859 *
6860 * We support two methods:
6861 * 1. SOFOFROM/SOFOTO do a simple character mapping.
6862 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006863 */
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006864 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006865spell_soundfold(
6866 slang_T *slang,
6867 char_u *inword,
6868 int folded, /* "inword" is already case-folded */
6869 char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006870{
6871 char_u fword[MAXWLEN];
6872 char_u *word;
6873
6874 if (slang->sl_sofo)
6875 /* SOFOFROM and SOFOTO used */
6876 spell_soundfold_sofo(slang, inword, res);
6877 else
6878 {
6879 /* SAL items used. Requires the word to be case-folded. */
6880 if (folded)
6881 word = inword;
6882 else
6883 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006884 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006885 word = fword;
6886 }
6887
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006888 if (has_mbyte)
6889 spell_soundfold_wsal(slang, word, res);
6890 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006891 spell_soundfold_sal(slang, word, res);
6892 }
6893}
6894
6895/*
6896 * Perform sound folding of "inword" into "res" according to SOFOFROM and
6897 * SOFOTO lines.
6898 */
6899 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006900spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006901{
6902 char_u *s;
6903 int ri = 0;
6904 int c;
6905
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006906 if (has_mbyte)
6907 {
6908 int prevc = 0;
6909 int *ip;
6910
6911 /* The sl_sal_first[] table contains the translation for chars up to
6912 * 255, sl_sal the rest. */
6913 for (s = inword; *s != NUL; )
6914 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006915 c = mb_cptr2char_adv(&s);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006916 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006917 c = ' ';
6918 else if (c < 256)
6919 c = slang->sl_sal_first[c];
6920 else
6921 {
6922 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
6923 if (ip == NULL) /* empty list, can't match */
6924 c = NUL;
6925 else
6926 for (;;) /* find "c" in the list */
6927 {
6928 if (*ip == 0) /* not found */
6929 {
6930 c = NUL;
6931 break;
6932 }
6933 if (*ip == c) /* match! */
6934 {
6935 c = ip[1];
6936 break;
6937 }
6938 ip += 2;
6939 }
6940 }
6941
6942 if (c != NUL && c != prevc)
6943 {
6944 ri += mb_char2bytes(c, res + ri);
6945 if (ri + MB_MAXBYTES > MAXWLEN)
6946 break;
6947 prevc = c;
6948 }
6949 }
6950 }
6951 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006952 {
6953 /* The sl_sal_first[] table contains the translation. */
6954 for (s = inword; (c = *s) != NUL; ++s)
6955 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006956 if (VIM_ISWHITE(c))
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006957 c = ' ';
6958 else
6959 c = slang->sl_sal_first[c];
6960 if (c != NUL && (ri == 0 || res[ri - 1] != c))
6961 res[ri++] = c;
6962 }
6963 }
6964
6965 res[ri] = NUL;
6966}
6967
6968 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01006969spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006970{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006971 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006972 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006973 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006974 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006975 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006976 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006977 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006978 int n, k = 0;
6979 int z0;
6980 int k0;
6981 int n0;
6982 int c;
6983 int pri;
6984 int p0 = -333;
6985 int c0;
6986
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006987 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006988 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006989 if (slang->sl_rem_accents)
6990 {
6991 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006992 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006993 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006994 if (VIM_ISWHITE(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006995 {
6996 *t++ = ' ';
6997 s = skipwhite(s);
6998 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006999 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007000 {
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007001 if (spell_iswordp_nmw(s, curwin))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007002 *t++ = *s;
7003 ++s;
7004 }
7005 }
7006 *t = NUL;
7007 }
7008 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02007009 vim_strncpy(word, s, MAXWLEN - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007010
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007011 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007012
7013 /*
7014 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007015 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007016 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007017 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007018 while ((c = word[i]) != NUL)
7019 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007020 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007021 n = slang->sl_sal_first[c];
7022 z0 = 0;
7023
7024 if (n >= 0)
7025 {
7026 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007027 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007028 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007029 /* Quickly skip entries that don't match the word. Most
7030 * entries are less then three chars, optimize for that. */
7031 k = smp[n].sm_leadlen;
7032 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007033 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007034 if (word[i + 1] != s[1])
7035 continue;
7036 if (k > 2)
7037 {
7038 for (j = 2; j < k; ++j)
7039 if (word[i + j] != s[j])
7040 break;
7041 if (j < k)
7042 continue;
7043 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007044 }
7045
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007046 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007047 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007048 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007049 while (*pf != NUL && *pf != word[i + k])
7050 ++pf;
7051 if (*pf == NUL)
7052 continue;
7053 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007054 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007055 s = smp[n].sm_rules;
7056 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007057
7058 p0 = *s;
7059 k0 = k;
7060 while (*s == '-' && k > 1)
7061 {
7062 k--;
7063 s++;
7064 }
7065 if (*s == '<')
7066 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007067 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007068 {
7069 /* determine priority */
7070 pri = *s - '0';
7071 s++;
7072 }
7073 if (*s == '^' && *(s + 1) == '^')
7074 s++;
7075
7076 if (*s == NUL
7077 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007078 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007079 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007080 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007081 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007082 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007083 && spell_iswordp(word + i - 1, curwin)
7084 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007085 {
7086 /* search for followup rules, if: */
7087 /* followup and k > 1 and NO '-' in searchstring */
7088 c0 = word[i + k - 1];
7089 n0 = slang->sl_sal_first[c0];
7090
7091 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007092 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007093 {
7094 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007095 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007097 /* Quickly skip entries that don't match the word.
7098 * */
7099 k0 = smp[n0].sm_leadlen;
7100 if (k0 > 1)
7101 {
7102 if (word[i + k] != s[1])
7103 continue;
7104 if (k0 > 2)
7105 {
7106 pf = word + i + k + 1;
7107 for (j = 2; j < k0; ++j)
7108 if (*pf++ != s[j])
7109 break;
7110 if (j < k0)
7111 continue;
7112 }
7113 }
7114 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007115
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007116 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007117 {
7118 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007119 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007120 while (*pf != NUL && *pf != word[i + k0])
7121 ++pf;
7122 if (*pf == NUL)
7123 continue;
7124 ++k0;
7125 }
7126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007127 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007128 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007129 while (*s == '-')
7130 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007131 /* "k0" gets NOT reduced because
7132 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007133 s++;
7134 }
7135 if (*s == '<')
7136 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007137 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007138 {
7139 p0 = *s - '0';
7140 s++;
7141 }
7142
7143 if (*s == NUL
7144 /* *s == '^' cuts */
7145 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007146 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007147 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007148 {
7149 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007150 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007151 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007152
7153 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007154 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007155 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007156 /* rule fits; stop search */
7157 break;
7158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007159 }
7160
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007161 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007162 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007163 }
7164
7165 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007166 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007167 if (s == NULL)
7168 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007169 pf = smp[n].sm_rules;
7170 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007171 if (p0 == 1 && z == 0)
7172 {
7173 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007174 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
7175 || res[reslen - 1] == *s))
7176 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007177 z0 = 1;
7178 z = 1;
7179 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007180 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007181 {
7182 word[i + k0] = *s;
7183 k0++;
7184 s++;
7185 }
7186 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +00007187 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007188
7189 /* new "actual letter" */
7190 c = word[i];
7191 }
7192 else
7193 {
7194 /* no '<' rule used */
7195 i += k - 1;
7196 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007197 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007198 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007199 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007200 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007201 s++;
7202 }
7203 /* new "actual letter" */
7204 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007205 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007206 {
7207 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007208 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +00007209 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007210 i = 0;
7211 z0 = 1;
7212 }
7213 }
7214 break;
7215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007216 }
7217 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007218 else if (VIM_ISWHITE(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007219 {
7220 c = ' ';
7221 k = 1;
7222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007223
7224 if (z0 == 0)
7225 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007226 if (k && !p0 && reslen < MAXWLEN && c != NUL
7227 && (!slang->sl_collapse || reslen == 0
7228 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007229 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007230 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007231
7232 i++;
7233 z = 0;
7234 k = 0;
7235 }
7236 }
7237
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007238 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007239}
7240
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007241/*
7242 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
7243 * Multi-byte version of spell_soundfold().
7244 */
7245 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007246spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007247{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007248 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007249 int word[MAXWLEN];
7250 int wres[MAXWLEN];
7251 int l;
7252 char_u *s;
7253 int *ws;
7254 char_u *t;
7255 int *pf;
7256 int i, j, z;
7257 int reslen;
7258 int n, k = 0;
7259 int z0;
7260 int k0;
7261 int n0;
7262 int c;
7263 int pri;
7264 int p0 = -333;
7265 int c0;
7266 int did_white = FALSE;
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007267 int wordlen;
7268
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007269
7270 /*
7271 * Convert the multi-byte string to a wide-character string.
7272 * Remove accents, if wanted. We actually remove all non-word characters.
7273 * But keep white space.
7274 */
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007275 wordlen = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007276 for (s = inword; *s != NUL; )
7277 {
7278 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007279 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007280 if (slang->sl_rem_accents)
7281 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007282 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007283 {
7284 if (did_white)
7285 continue;
7286 c = ' ';
7287 did_white = TRUE;
7288 }
7289 else
7290 {
7291 did_white = FALSE;
Bram Moolenaarcc63c642013-11-12 04:44:01 +01007292 if (!spell_iswordp_nmw(t, curwin))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007293 continue;
7294 }
7295 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007296 word[wordlen++] = c;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007297 }
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007298 word[wordlen] = NUL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007299
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007300 /*
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007301 * This algorithm comes from Aspell phonet.cpp.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007302 * Converted from C++ to C. Added support for multi-byte chars.
7303 * Changed to keep spaces.
7304 */
7305 i = reslen = z = 0;
7306 while ((c = word[i]) != NUL)
7307 {
7308 /* Start with the first rule that has the character in the word. */
7309 n = slang->sl_sal_first[c & 0xff];
7310 z0 = 0;
7311
7312 if (n >= 0)
7313 {
Bram Moolenaar95e85792010-08-01 15:37:02 +02007314 /* Check all rules for the same index byte.
7315 * If c is 0x300 need extra check for the end of the array, as
7316 * (c & 0xff) is NUL. */
7317 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
7318 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007319 {
7320 /* Quickly skip entries that don't match the word. Most
7321 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007322 if (c != ws[0])
7323 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007324 k = smp[n].sm_leadlen;
7325 if (k > 1)
7326 {
7327 if (word[i + 1] != ws[1])
7328 continue;
7329 if (k > 2)
7330 {
7331 for (j = 2; j < k; ++j)
7332 if (word[i + j] != ws[j])
7333 break;
7334 if (j < k)
7335 continue;
7336 }
7337 }
7338
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007339 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007340 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007341 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007342 while (*pf != NUL && *pf != word[i + k])
7343 ++pf;
7344 if (*pf == NUL)
7345 continue;
7346 ++k;
7347 }
7348 s = smp[n].sm_rules;
7349 pri = 5; /* default priority */
7350
7351 p0 = *s;
7352 k0 = k;
7353 while (*s == '-' && k > 1)
7354 {
7355 k--;
7356 s++;
7357 }
7358 if (*s == '<')
7359 s++;
7360 if (VIM_ISDIGIT(*s))
7361 {
7362 /* determine priority */
7363 pri = *s - '0';
7364 s++;
7365 }
7366 if (*s == '^' && *(s + 1) == '^')
7367 s++;
7368
7369 if (*s == NUL
7370 || (*s == '^'
7371 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +02007372 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007373 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +02007374 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007375 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +02007376 && spell_iswordp_w(word + i - 1, curwin)
7377 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007378 {
7379 /* search for followup rules, if: */
7380 /* followup and k > 1 and NO '-' in searchstring */
7381 c0 = word[i + k - 1];
7382 n0 = slang->sl_sal_first[c0 & 0xff];
7383
7384 if (slang->sl_followup && k > 1 && n0 >= 0
7385 && p0 != '-' && word[i + k] != NUL)
7386 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007387 /* Test follow-up rule for "word[i + k]"; loop over
7388 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007389 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
7390 == (c0 & 0xff); ++n0)
7391 {
7392 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007393 */
7394 if (c0 != ws[0])
7395 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007396 k0 = smp[n0].sm_leadlen;
7397 if (k0 > 1)
7398 {
7399 if (word[i + k] != ws[1])
7400 continue;
7401 if (k0 > 2)
7402 {
7403 pf = word + i + k + 1;
7404 for (j = 2; j < k0; ++j)
7405 if (*pf++ != ws[j])
7406 break;
7407 if (j < k0)
7408 continue;
7409 }
7410 }
7411 k0 += k - 1;
7412
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007413 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007414 {
7415 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007416 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007417 while (*pf != NUL && *pf != word[i + k0])
7418 ++pf;
7419 if (*pf == NUL)
7420 continue;
7421 ++k0;
7422 }
7423
7424 p0 = 5;
7425 s = smp[n0].sm_rules;
7426 while (*s == '-')
7427 {
7428 /* "k0" gets NOT reduced because
7429 * "if (k0 == k)" */
7430 s++;
7431 }
7432 if (*s == '<')
7433 s++;
7434 if (VIM_ISDIGIT(*s))
7435 {
7436 p0 = *s - '0';
7437 s++;
7438 }
7439
7440 if (*s == NUL
7441 /* *s == '^' cuts */
7442 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007443 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +02007444 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007445 {
7446 if (k0 == k)
7447 /* this is just a piece of the string */
7448 continue;
7449
7450 if (p0 < pri)
7451 /* priority too low */
7452 continue;
7453 /* rule fits; stop search */
7454 break;
7455 }
7456 }
7457
7458 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
7459 == (c0 & 0xff))
7460 continue;
7461 }
7462
7463 /* replace string */
7464 ws = smp[n].sm_to_w;
7465 s = smp[n].sm_rules;
7466 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
7467 if (p0 == 1 && z == 0)
7468 {
7469 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007470 if (reslen > 0 && ws != NULL && *ws != NUL
7471 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007472 || wres[reslen - 1] == *ws))
7473 reslen--;
7474 z0 = 1;
7475 z = 1;
7476 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007477 if (ws != NULL)
7478 while (*ws != NUL && word[i + k0] != NUL)
7479 {
7480 word[i + k0] = *ws;
7481 k0++;
7482 ws++;
7483 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007484 if (k > k0)
7485 mch_memmove(word + i + k0, word + i + k,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007486 sizeof(int) * (wordlen - (i + k) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007487
7488 /* new "actual letter" */
7489 c = word[i];
7490 }
7491 else
7492 {
7493 /* no '<' rule used */
7494 i += k - 1;
7495 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007496 if (ws != NULL)
7497 while (*ws != NUL && ws[1] != NUL
7498 && reslen < MAXWLEN)
7499 {
7500 if (reslen == 0 || wres[reslen - 1] != *ws)
7501 wres[reslen++] = *ws;
7502 ws++;
7503 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007504 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007505 if (ws == NULL)
7506 c = NUL;
7507 else
7508 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007509 if (strstr((char *)s, "^^") != NULL)
7510 {
7511 if (c != NUL)
7512 wres[reslen++] = c;
7513 mch_memmove(word, word + i + 1,
Bram Moolenaarf9de1402012-05-18 18:08:01 +02007514 sizeof(int) * (wordlen - (i + 1) + 1));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007515 i = 0;
7516 z0 = 1;
7517 }
7518 }
7519 break;
7520 }
7521 }
7522 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01007523 else if (VIM_ISWHITE(c))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007524 {
7525 c = ' ';
7526 k = 1;
7527 }
7528
7529 if (z0 == 0)
7530 {
7531 if (k && !p0 && reslen < MAXWLEN && c != NUL
7532 && (!slang->sl_collapse || reslen == 0
7533 || wres[reslen - 1] != c))
7534 /* condense only double letters */
7535 wres[reslen++] = c;
7536
7537 i++;
7538 z = 0;
7539 k = 0;
7540 }
7541 }
7542
7543 /* Convert wide characters in "wres" to a multi-byte string in "res". */
7544 l = 0;
7545 for (n = 0; n < reslen; ++n)
7546 {
7547 l += mb_char2bytes(wres[n], res + l);
7548 if (l + MB_MAXBYTES > MAXWLEN)
7549 break;
7550 }
7551 res[l] = NUL;
7552}
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007553
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007554/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007555 * Compute a score for two sound-a-like words.
7556 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
7557 * Instead of a generic loop we write out the code. That keeps it fast by
7558 * avoiding checks that will not be possible.
7559 */
7560 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007561soundalike_score(
7562 char_u *goodstart, /* sound-folded good word */
7563 char_u *badstart) /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007564{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007565 char_u *goodsound = goodstart;
7566 char_u *badsound = badstart;
7567 int goodlen;
7568 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007569 int n;
7570 char_u *pl, *ps;
7571 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007572 int score = 0;
7573
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007574 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007575 * counted so much, vowels halfway the word aren't counted at all. */
7576 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
7577 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +02007578 if ((badsound[0] == NUL && goodsound[1] == NUL)
7579 || (goodsound[0] == NUL && badsound[1] == NUL))
7580 /* changing word with vowel to word without a sound */
7581 return SCORE_DEL;
7582 if (badsound[0] == NUL || goodsound[0] == NUL)
7583 /* more than two changes */
7584 return SCORE_MAXMAX;
7585
Bram Moolenaar4770d092006-01-12 23:22:24 +00007586 if (badsound[1] == goodsound[1]
7587 || (badsound[1] != NUL
7588 && goodsound[1] != NUL
7589 && badsound[2] == goodsound[2]))
7590 {
7591 /* handle like a substitute */
7592 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007593 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007594 {
7595 score = 2 * SCORE_DEL / 3;
7596 if (*badsound == '*')
7597 ++badsound;
7598 else
7599 ++goodsound;
7600 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007601 }
7602
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 goodlen = (int)STRLEN(goodsound);
7604 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007605
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007606 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007607 * changes. */
7608 n = goodlen - badlen;
7609 if (n < -2 || n > 2)
7610 return SCORE_MAXMAX;
7611
7612 if (n > 0)
7613 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007614 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007615 ps = badsound;
7616 }
7617 else
7618 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007619 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007620 ps = goodsound;
7621 }
7622
7623 /* Skip over the identical part. */
7624 while (*pl == *ps && *pl != NUL)
7625 {
7626 ++pl;
7627 ++ps;
7628 }
7629
7630 switch (n)
7631 {
7632 case -2:
7633 case 2:
7634 /*
7635 * Must delete two characters from "pl".
7636 */
7637 ++pl; /* first delete */
7638 while (*pl == *ps)
7639 {
7640 ++pl;
7641 ++ps;
7642 }
7643 /* strings must be equal after second delete */
7644 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007645 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007646
7647 /* Failed to compare. */
7648 break;
7649
7650 case -1:
7651 case 1:
7652 /*
7653 * Minimal one delete from "pl" required.
7654 */
7655
7656 /* 1: delete */
7657 pl2 = pl + 1;
7658 ps2 = ps;
7659 while (*pl2 == *ps2)
7660 {
7661 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007662 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007663 ++pl2;
7664 ++ps2;
7665 }
7666
7667 /* 2: delete then swap, then rest must be equal */
7668 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7669 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007670 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007671
7672 /* 3: delete then substitute, then the rest must be equal */
7673 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007674 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007675
7676 /* 4: first swap then delete */
7677 if (pl[0] == ps[1] && pl[1] == ps[0])
7678 {
7679 pl2 = pl + 2; /* swap, skip two chars */
7680 ps2 = ps + 2;
7681 while (*pl2 == *ps2)
7682 {
7683 ++pl2;
7684 ++ps2;
7685 }
7686 /* delete a char and then strings must be equal */
7687 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007688 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007689 }
7690
7691 /* 5: first substitute then delete */
7692 pl2 = pl + 1; /* substitute, skip one char */
7693 ps2 = ps + 1;
7694 while (*pl2 == *ps2)
7695 {
7696 ++pl2;
7697 ++ps2;
7698 }
7699 /* delete a char and then strings must be equal */
7700 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007701 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007702
7703 /* Failed to compare. */
7704 break;
7705
7706 case 0:
7707 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00007708 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007709 * insert is only possible in combination with a delete.
7710 * 1: check if for identical strings
7711 */
7712 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007713 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007714
7715 /* 2: swap */
7716 if (pl[0] == ps[1] && pl[1] == ps[0])
7717 {
7718 pl2 = pl + 2; /* swap, skip two chars */
7719 ps2 = ps + 2;
7720 while (*pl2 == *ps2)
7721 {
7722 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007723 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007724 ++pl2;
7725 ++ps2;
7726 }
7727 /* 3: swap and swap again */
7728 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7729 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007730 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007731
7732 /* 4: swap and substitute */
7733 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007734 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007735 }
7736
7737 /* 5: substitute */
7738 pl2 = pl + 1;
7739 ps2 = ps + 1;
7740 while (*pl2 == *ps2)
7741 {
7742 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007743 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007744 ++pl2;
7745 ++ps2;
7746 }
7747
7748 /* 6: substitute and swap */
7749 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
7750 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007751 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007752
7753 /* 7: substitute and substitute */
7754 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007755 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007756
7757 /* 8: insert then delete */
7758 pl2 = pl;
7759 ps2 = ps + 1;
7760 while (*pl2 == *ps2)
7761 {
7762 ++pl2;
7763 ++ps2;
7764 }
7765 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007766 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007767
7768 /* 9: delete then insert */
7769 pl2 = pl + 1;
7770 ps2 = ps;
7771 while (*pl2 == *ps2)
7772 {
7773 ++pl2;
7774 ++ps2;
7775 }
7776 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007777 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007778
7779 /* Failed to compare. */
7780 break;
7781 }
7782
7783 return SCORE_MAXMAX;
7784}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007786/*
7787 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007788 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007789 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00007790 * The algorithm is described by Du and Chang, 1992.
7791 * The implementation of the algorithm comes from Aspell editdist.cpp,
7792 * edit_distance(). It has been converted from C++ to C and modified to
7793 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007794 */
7795 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007796spell_edit_score(
7797 slang_T *slang,
7798 char_u *badword,
7799 char_u *goodword)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007800{
7801 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007802 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007803 int j, i;
7804 int t;
7805 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007806 int pbc, pgc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007807 char_u *p;
7808 int wbadword[MAXWLEN];
7809 int wgoodword[MAXWLEN];
7810
7811 if (has_mbyte)
7812 {
7813 /* Get the characters from the multi-byte strings and put them in an
7814 * int array for easy access. */
7815 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007816 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007817 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007818 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007819 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +00007820 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007821 }
7822 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007823 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007824 badlen = (int)STRLEN(badword) + 1;
7825 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007826 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007827
7828 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
7829#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007830 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
7831 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007832 if (cnt == NULL)
7833 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007834
7835 CNT(0, 0) = 0;
7836 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007837 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007838
7839 for (i = 1; i <= badlen; ++i)
7840 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007841 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007842 for (j = 1; j <= goodlen; ++j)
7843 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007844 if (has_mbyte)
7845 {
7846 bc = wbadword[i - 1];
7847 gc = wgoodword[j - 1];
7848 }
7849 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007850 {
7851 bc = badword[i - 1];
7852 gc = goodword[j - 1];
7853 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 if (bc == gc)
7855 CNT(i, j) = CNT(i - 1, j - 1);
7856 else
7857 {
7858 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007859 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007860 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
7861 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007862 {
7863 /* For a similar character use SCORE_SIMILAR. */
7864 if (slang != NULL
7865 && slang->sl_has_map
7866 && similar_chars(slang, gc, bc))
7867 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
7868 else
7869 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
7870 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007871
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007872 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007873 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007874 if (has_mbyte)
7875 {
7876 pbc = wbadword[i - 2];
7877 pgc = wgoodword[j - 2];
7878 }
7879 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007880 {
7881 pbc = badword[i - 2];
7882 pgc = goodword[j - 2];
7883 }
7884 if (bc == pgc && pbc == gc)
7885 {
7886 t = SCORE_SWAP + CNT(i - 2, j - 2);
7887 if (t < CNT(i, j))
7888 CNT(i, j) = t;
7889 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007890 }
7891 t = SCORE_DEL + CNT(i - 1, j);
7892 if (t < CNT(i, j))
7893 CNT(i, j) = t;
7894 t = SCORE_INS + CNT(i, j - 1);
7895 if (t < CNT(i, j))
7896 CNT(i, j) = t;
7897 }
7898 }
7899 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007900
7901 i = CNT(badlen - 1, goodlen - 1);
7902 vim_free(cnt);
7903 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007904}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007905
Bram Moolenaar4770d092006-01-12 23:22:24 +00007906typedef struct
7907{
7908 int badi;
7909 int goodi;
7910 int score;
7911} limitscore_T;
7912
7913/*
7914 * Like spell_edit_score(), but with a limit on the score to make it faster.
7915 * May return SCORE_MAXMAX when the score is higher than "limit".
7916 *
7917 * This uses a stack for the edits still to be tried.
7918 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
7919 * for multi-byte characters.
7920 */
7921 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01007922spell_edit_score_limit(
7923 slang_T *slang,
7924 char_u *badword,
7925 char_u *goodword,
7926 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007927{
7928 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
7929 int stackidx;
7930 int bi, gi;
7931 int bi2, gi2;
7932 int bc, gc;
7933 int score;
7934 int score_off;
7935 int minscore;
7936 int round;
7937
Bram Moolenaar4770d092006-01-12 23:22:24 +00007938 /* Multi-byte characters require a bit more work, use a different function
7939 * to avoid testing "has_mbyte" quite often. */
7940 if (has_mbyte)
7941 return spell_edit_score_limit_w(slang, badword, goodword, limit);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007942
7943 /*
7944 * The idea is to go from start to end over the words. So long as
7945 * characters are equal just continue, this always gives the lowest score.
7946 * When there is a difference try several alternatives. Each alternative
7947 * increases "score" for the edit distance. Some of the alternatives are
7948 * pushed unto a stack and tried later, some are tried right away. At the
7949 * end of the word the score for one alternative is known. The lowest
7950 * possible score is stored in "minscore".
7951 */
7952 stackidx = 0;
7953 bi = 0;
7954 gi = 0;
7955 score = 0;
7956 minscore = limit + 1;
7957
7958 for (;;)
7959 {
7960 /* Skip over an equal part, score remains the same. */
7961 for (;;)
7962 {
7963 bc = badword[bi];
7964 gc = goodword[gi];
7965 if (bc != gc) /* stop at a char that's different */
7966 break;
7967 if (bc == NUL) /* both words end */
7968 {
7969 if (score < minscore)
7970 minscore = score;
7971 goto pop; /* do next alternative */
7972 }
7973 ++bi;
7974 ++gi;
7975 }
7976
7977 if (gc == NUL) /* goodword ends, delete badword chars */
7978 {
7979 do
7980 {
7981 if ((score += SCORE_DEL) >= minscore)
7982 goto pop; /* do next alternative */
7983 } while (badword[++bi] != NUL);
7984 minscore = score;
7985 }
7986 else if (bc == NUL) /* badword ends, insert badword chars */
7987 {
7988 do
7989 {
7990 if ((score += SCORE_INS) >= minscore)
7991 goto pop; /* do next alternative */
7992 } while (goodword[++gi] != NUL);
7993 minscore = score;
7994 }
7995 else /* both words continue */
7996 {
7997 /* If not close to the limit, perform a change. Only try changes
7998 * that may lead to a lower score than "minscore".
7999 * round 0: try deleting a char from badword
8000 * round 1: try inserting a char in badword */
8001 for (round = 0; round <= 1; ++round)
8002 {
8003 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8004 if (score_off < minscore)
8005 {
8006 if (score_off + SCORE_EDIT_MIN >= minscore)
8007 {
8008 /* Near the limit, rest of the words must match. We
8009 * can check that right now, no need to push an item
8010 * onto the stack. */
8011 bi2 = bi + 1 - round;
8012 gi2 = gi + round;
8013 while (goodword[gi2] == badword[bi2])
8014 {
8015 if (goodword[gi2] == NUL)
8016 {
8017 minscore = score_off;
8018 break;
8019 }
8020 ++bi2;
8021 ++gi2;
8022 }
8023 }
8024 else
8025 {
8026 /* try deleting/inserting a character later */
8027 stack[stackidx].badi = bi + 1 - round;
8028 stack[stackidx].goodi = gi + round;
8029 stack[stackidx].score = score_off;
8030 ++stackidx;
8031 }
8032 }
8033 }
8034
8035 if (score + SCORE_SWAP < minscore)
8036 {
8037 /* If swapping two characters makes a match then the
8038 * substitution is more expensive, thus there is no need to
8039 * try both. */
8040 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
8041 {
8042 /* Swap two characters, that is: skip them. */
8043 gi += 2;
8044 bi += 2;
8045 score += SCORE_SWAP;
8046 continue;
8047 }
8048 }
8049
8050 /* Substitute one character for another which is the same
8051 * thing as deleting a character from both goodword and badword.
8052 * Use a better score when there is only a case difference. */
8053 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8054 score += SCORE_ICASE;
8055 else
8056 {
8057 /* For a similar character use SCORE_SIMILAR. */
8058 if (slang != NULL
8059 && slang->sl_has_map
8060 && similar_chars(slang, gc, bc))
8061 score += SCORE_SIMILAR;
8062 else
8063 score += SCORE_SUBST;
8064 }
8065
8066 if (score < minscore)
8067 {
8068 /* Do the substitution. */
8069 ++gi;
8070 ++bi;
8071 continue;
8072 }
8073 }
8074pop:
8075 /*
8076 * Get here to try the next alternative, pop it from the stack.
8077 */
8078 if (stackidx == 0) /* stack is empty, finished */
8079 break;
8080
8081 /* pop an item from the stack */
8082 --stackidx;
8083 gi = stack[stackidx].goodi;
8084 bi = stack[stackidx].badi;
8085 score = stack[stackidx].score;
8086 }
8087
8088 /* When the score goes over "limit" it may actually be much higher.
8089 * Return a very large number to avoid going below the limit when giving a
8090 * bonus. */
8091 if (minscore > limit)
8092 return SCORE_MAXMAX;
8093 return minscore;
8094}
8095
Bram Moolenaar4770d092006-01-12 23:22:24 +00008096/*
8097 * Multi-byte version of spell_edit_score_limit().
8098 * Keep it in sync with the above!
8099 */
8100 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008101spell_edit_score_limit_w(
8102 slang_T *slang,
8103 char_u *badword,
8104 char_u *goodword,
8105 int limit)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008106{
8107 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
8108 int stackidx;
8109 int bi, gi;
8110 int bi2, gi2;
8111 int bc, gc;
8112 int score;
8113 int score_off;
8114 int minscore;
8115 int round;
8116 char_u *p;
8117 int wbadword[MAXWLEN];
8118 int wgoodword[MAXWLEN];
8119
8120 /* Get the characters from the multi-byte strings and put them in an
8121 * int array for easy access. */
8122 bi = 0;
8123 for (p = badword; *p != NUL; )
8124 wbadword[bi++] = mb_cptr2char_adv(&p);
8125 wbadword[bi++] = 0;
8126 gi = 0;
8127 for (p = goodword; *p != NUL; )
8128 wgoodword[gi++] = mb_cptr2char_adv(&p);
8129 wgoodword[gi++] = 0;
8130
8131 /*
8132 * The idea is to go from start to end over the words. So long as
8133 * characters are equal just continue, this always gives the lowest score.
8134 * When there is a difference try several alternatives. Each alternative
8135 * increases "score" for the edit distance. Some of the alternatives are
8136 * pushed unto a stack and tried later, some are tried right away. At the
8137 * end of the word the score for one alternative is known. The lowest
8138 * possible score is stored in "minscore".
8139 */
8140 stackidx = 0;
8141 bi = 0;
8142 gi = 0;
8143 score = 0;
8144 minscore = limit + 1;
8145
8146 for (;;)
8147 {
8148 /* Skip over an equal part, score remains the same. */
8149 for (;;)
8150 {
8151 bc = wbadword[bi];
8152 gc = wgoodword[gi];
8153
8154 if (bc != gc) /* stop at a char that's different */
8155 break;
8156 if (bc == NUL) /* both words end */
8157 {
8158 if (score < minscore)
8159 minscore = score;
8160 goto pop; /* do next alternative */
8161 }
8162 ++bi;
8163 ++gi;
8164 }
8165
8166 if (gc == NUL) /* goodword ends, delete badword chars */
8167 {
8168 do
8169 {
8170 if ((score += SCORE_DEL) >= minscore)
8171 goto pop; /* do next alternative */
8172 } while (wbadword[++bi] != NUL);
8173 minscore = score;
8174 }
8175 else if (bc == NUL) /* badword ends, insert badword chars */
8176 {
8177 do
8178 {
8179 if ((score += SCORE_INS) >= minscore)
8180 goto pop; /* do next alternative */
8181 } while (wgoodword[++gi] != NUL);
8182 minscore = score;
8183 }
8184 else /* both words continue */
8185 {
8186 /* If not close to the limit, perform a change. Only try changes
8187 * that may lead to a lower score than "minscore".
8188 * round 0: try deleting a char from badword
8189 * round 1: try inserting a char in badword */
8190 for (round = 0; round <= 1; ++round)
8191 {
8192 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
8193 if (score_off < minscore)
8194 {
8195 if (score_off + SCORE_EDIT_MIN >= minscore)
8196 {
8197 /* Near the limit, rest of the words must match. We
8198 * can check that right now, no need to push an item
8199 * onto the stack. */
8200 bi2 = bi + 1 - round;
8201 gi2 = gi + round;
8202 while (wgoodword[gi2] == wbadword[bi2])
8203 {
8204 if (wgoodword[gi2] == NUL)
8205 {
8206 minscore = score_off;
8207 break;
8208 }
8209 ++bi2;
8210 ++gi2;
8211 }
8212 }
8213 else
8214 {
8215 /* try deleting a character from badword later */
8216 stack[stackidx].badi = bi + 1 - round;
8217 stack[stackidx].goodi = gi + round;
8218 stack[stackidx].score = score_off;
8219 ++stackidx;
8220 }
8221 }
8222 }
8223
8224 if (score + SCORE_SWAP < minscore)
8225 {
8226 /* If swapping two characters makes a match then the
8227 * substitution is more expensive, thus there is no need to
8228 * try both. */
8229 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
8230 {
8231 /* Swap two characters, that is: skip them. */
8232 gi += 2;
8233 bi += 2;
8234 score += SCORE_SWAP;
8235 continue;
8236 }
8237 }
8238
8239 /* Substitute one character for another which is the same
8240 * thing as deleting a character from both goodword and badword.
8241 * Use a better score when there is only a case difference. */
8242 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
8243 score += SCORE_ICASE;
8244 else
8245 {
8246 /* For a similar character use SCORE_SIMILAR. */
8247 if (slang != NULL
8248 && slang->sl_has_map
8249 && similar_chars(slang, gc, bc))
8250 score += SCORE_SIMILAR;
8251 else
8252 score += SCORE_SUBST;
8253 }
8254
8255 if (score < minscore)
8256 {
8257 /* Do the substitution. */
8258 ++gi;
8259 ++bi;
8260 continue;
8261 }
8262 }
8263pop:
8264 /*
8265 * Get here to try the next alternative, pop it from the stack.
8266 */
8267 if (stackidx == 0) /* stack is empty, finished */
8268 break;
8269
8270 /* pop an item from the stack */
8271 --stackidx;
8272 gi = stack[stackidx].goodi;
8273 bi = stack[stackidx].badi;
8274 score = stack[stackidx].score;
8275 }
8276
8277 /* When the score goes over "limit" it may actually be much higher.
8278 * Return a very large number to avoid going below the limit when giving a
8279 * bonus. */
8280 if (minscore > limit)
8281 return SCORE_MAXMAX;
8282 return minscore;
8283}
Bram Moolenaar4770d092006-01-12 23:22:24 +00008284
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008285/*
8286 * ":spellinfo"
8287 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008288 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008289ex_spellinfo(exarg_T *eap UNUSED)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008290{
8291 int lpi;
8292 langp_T *lp;
8293 char_u *p;
8294
8295 if (no_spell_checking(curwin))
8296 return;
8297
8298 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +02008299 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008300 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008301 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar32526b32019-01-19 17:43:09 +01008302 msg_puts("file: ");
8303 msg_puts((char *)lp->lp_slang->sl_fname);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008304 msg_putchar('\n');
8305 p = lp->lp_slang->sl_info;
8306 if (p != NULL)
8307 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008308 msg_puts((char *)p);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008309 msg_putchar('\n');
8310 }
8311 }
8312 msg_end();
8313}
8314
Bram Moolenaar4770d092006-01-12 23:22:24 +00008315#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
8316#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008317#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +00008318#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
8319#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008320
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008321/*
8322 * ":spelldump"
8323 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008324 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008325ex_spelldump(exarg_T *eap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008326{
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008327 char_u *spl;
8328 long dummy;
8329
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008330 if (no_spell_checking(curwin))
8331 return;
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008332 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008333
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008334 /* Create a new empty buffer in a new window. */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008335 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008336
8337 /* enable spelling locally in the new window */
8338 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01008339 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL);
Bram Moolenaar7a18fdc2013-09-29 13:38:29 +02008340 vim_free(spl);
8341
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008342 if (!BUFEMPTY())
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008343 return;
8344
Bram Moolenaar860cae12010-06-05 23:22:07 +02008345 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008346
8347 /* Delete the empty line that we started with. */
8348 if (curbuf->b_ml.ml_line_count > 1)
8349 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
8350
8351 redraw_later(NOT_VALID);
8352}
8353
8354/*
8355 * Go through all possible words and:
8356 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
8357 * "ic" and "dir" are not used.
8358 * 2. When "pat" is not NULL: add matching words to insert mode completion.
8359 */
8360 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008361spell_dump_compl(
8362 char_u *pat, /* leading part of the word */
8363 int ic, /* ignore case */
8364 int *dir, /* direction for adding matches */
8365 int dumpflags_arg) /* DUMPFLAG_* */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008366{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008367 langp_T *lp;
8368 slang_T *slang;
8369 idx_T arridx[MAXWLEN];
8370 int curi[MAXWLEN];
8371 char_u word[MAXWLEN];
8372 int c;
8373 char_u *byts;
8374 idx_T *idxs;
8375 linenr_T lnum = 0;
8376 int round;
8377 int depth;
8378 int n;
8379 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008380 char_u *region_names = NULL; /* region names being used */
8381 int do_region = TRUE; /* dump region names and numbers */
8382 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008383 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008384 int dumpflags = dumpflags_arg;
8385 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008386
Bram Moolenaard0131a82006-03-04 21:46:13 +00008387 /* When ignoring case or when the pattern starts with capital pass this on
8388 * to dump_word(). */
8389 if (pat != NULL)
8390 {
8391 if (ic)
8392 dumpflags |= DUMPFLAG_ICASE;
8393 else
8394 {
8395 n = captype(pat, NULL);
8396 if (n == WF_ONECAP)
8397 dumpflags |= DUMPFLAG_ONECAP;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01008398 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat))
Bram Moolenaard0131a82006-03-04 21:46:13 +00008399 dumpflags |= DUMPFLAG_ALLCAP;
8400 }
8401 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008402
Bram Moolenaar7887d882005-07-01 22:33:52 +00008403 /* Find out if we can support regions: All languages must support the same
8404 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008405 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +00008406 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008407 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +00008408 p = lp->lp_slang->sl_regions;
8409 if (p[0] != 0)
8410 {
8411 if (region_names == NULL) /* first language with regions */
8412 region_names = p;
8413 else if (STRCMP(region_names, p) != 0)
8414 {
8415 do_region = FALSE; /* region names are different */
8416 break;
8417 }
8418 }
8419 }
8420
8421 if (do_region && region_names != NULL)
8422 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008423 if (pat == NULL)
8424 {
8425 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
8426 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8427 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00008428 }
8429 else
8430 do_region = FALSE;
8431
8432 /*
8433 * Loop over all files loaded for the entries in 'spelllang'.
8434 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02008435 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008436 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008437 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008438 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008439 if (slang->sl_fbyts == NULL) /* reloading failed */
8440 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008441
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008442 if (pat == NULL)
8443 {
8444 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
8445 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
8446 }
8447
8448 /* When matching with a pattern and there are no prefixes only use
8449 * parts of the tree that match "pat". */
8450 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008451 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008452 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008453 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008454
8455 /* round 1: case-folded tree
8456 * round 2: keep-case tree */
8457 for (round = 1; round <= 2; ++round)
8458 {
8459 if (round == 1)
8460 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008461 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008462 byts = slang->sl_fbyts;
8463 idxs = slang->sl_fidxs;
8464 }
8465 else
8466 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008467 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008468 byts = slang->sl_kbyts;
8469 idxs = slang->sl_kidxs;
8470 }
8471 if (byts == NULL)
8472 continue; /* array is empty */
8473
8474 depth = 0;
8475 arridx[0] = 0;
8476 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008477 while (depth >= 0 && !got_int
8478 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008479 {
8480 if (curi[depth] > byts[arridx[depth]])
8481 {
8482 /* Done all bytes at this node, go up one level. */
8483 --depth;
8484 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02008485 ins_compl_check_keys(50, FALSE);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008486 }
8487 else
8488 {
8489 /* Do one more byte at this node. */
8490 n = arridx[depth] + curi[depth];
8491 ++curi[depth];
8492 c = byts[n];
8493 if (c == 0)
8494 {
8495 /* End of word, deal with the word.
8496 * Don't use keep-case words in the fold-case tree,
8497 * they will appear in the keep-case tree.
8498 * Only use the word when the region matches. */
8499 flags = (int)idxs[n];
8500 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008501 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +00008502 && (do_region
8503 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008504 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008505 & lp->lp_region) != 0))
8506 {
8507 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00008508 if (!do_region)
8509 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008510
8511 /* Dump the basic word if there is no prefix or
8512 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008513 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008514 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008515 {
8516 dump_word(slang, word, pat, dir,
8517 dumpflags, flags, lnum);
8518 if (pat == NULL)
8519 ++lnum;
8520 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008521
8522 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008523 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008524 lnum = dump_prefixes(slang, word, pat, dir,
8525 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008526 }
8527 }
8528 else
8529 {
8530 /* Normal char, go one level deeper. */
8531 word[depth++] = c;
8532 arridx[depth] = idxs[n];
8533 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008534
8535 /* Check if this characters matches with the pattern.
8536 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +00008537 * Always ignore case here, dump_word() will check
8538 * proper case later. This isn't exactly right when
8539 * length changes for multi-byte characters with
8540 * ignore case... */
8541 if (depth <= patlen
8542 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008543 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008544 }
8545 }
8546 }
8547 }
8548 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008549}
8550
8551/*
8552 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008553 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008554 */
8555 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008556dump_word(
8557 slang_T *slang,
8558 char_u *word,
8559 char_u *pat,
8560 int *dir,
8561 int dumpflags,
8562 int wordflags,
8563 linenr_T lnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008564{
8565 int keepcap = FALSE;
8566 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008567 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008568 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +00008569 char_u badword[MAXWLEN + 10];
8570 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +00008571 int flags = wordflags;
8572
8573 if (dumpflags & DUMPFLAG_ONECAP)
8574 flags |= WF_ONECAP;
8575 if (dumpflags & DUMPFLAG_ALLCAP)
8576 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008577
Bram Moolenaar4770d092006-01-12 23:22:24 +00008578 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008579 {
8580 /* Need to fix case according to "flags". */
8581 make_case_word(word, cword, flags);
8582 p = cword;
8583 }
8584 else
8585 {
8586 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008587 if ((dumpflags & DUMPFLAG_KEEPCASE)
8588 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008589 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008590 keepcap = TRUE;
8591 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008592 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008593
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008594 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008595 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008596 /* Add flags and regions after a slash. */
8597 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008598 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008599 STRCPY(badword, p);
8600 STRCAT(badword, "/");
8601 if (keepcap)
8602 STRCAT(badword, "=");
8603 if (flags & WF_BANNED)
8604 STRCAT(badword, "!");
8605 else if (flags & WF_RARE)
8606 STRCAT(badword, "?");
8607 if (flags & WF_REGION)
8608 for (i = 0; i < 7; ++i)
8609 if (flags & (0x10000 << i))
8610 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
8611 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008612 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00008613
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008614 if (dumpflags & DUMPFLAG_COUNT)
8615 {
8616 hashitem_T *hi;
8617
8618 /* Include the word count for ":spelldump!". */
8619 hi = hash_find(&slang->sl_wordcount, tw);
8620 if (!HASHITEM_EMPTY(hi))
8621 {
8622 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
8623 tw, HI2WC(hi)->wc_count);
8624 p = IObuff;
8625 }
8626 }
8627
8628 ml_append(lnum, p, (colnr_T)0, FALSE);
8629 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00008630 else if (((dumpflags & DUMPFLAG_ICASE)
8631 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
8632 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008633 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00008634 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +00008635 /* if dir was BACKWARD then honor it just once */
8636 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008637}
8638
8639/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008640 * For ":spelldump": Find matching prefixes for "word". Prepend each to
8641 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008642 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008643 * Return the updated line number.
8644 */
8645 static linenr_T
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008646dump_prefixes(
8647 slang_T *slang,
8648 char_u *word, /* case-folded word */
8649 char_u *pat,
8650 int *dir,
8651 int dumpflags,
8652 int flags, /* flags with prefix ID */
8653 linenr_T startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008654{
8655 idx_T arridx[MAXWLEN];
8656 int curi[MAXWLEN];
8657 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00008658 char_u word_up[MAXWLEN];
8659 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008660 int c;
8661 char_u *byts;
8662 idx_T *idxs;
8663 linenr_T lnum = startlnum;
8664 int depth;
8665 int n;
8666 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008667 int i;
8668
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008669 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +00008670 * upper-case letter in word_up[]. */
8671 c = PTR2CHAR(word);
8672 if (SPELL_TOUPPER(c) != c)
8673 {
8674 onecap_copy(word, word_up, TRUE);
8675 has_word_up = TRUE;
8676 }
8677
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008678 byts = slang->sl_pbyts;
8679 idxs = slang->sl_pidxs;
8680 if (byts != NULL) /* array not is empty */
8681 {
8682 /*
8683 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008684 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008685 */
8686 depth = 0;
8687 arridx[0] = 0;
8688 curi[0] = 1;
8689 while (depth >= 0 && !got_int)
8690 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008691 n = arridx[depth];
8692 len = byts[n];
8693 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008694 {
8695 /* Done all bytes at this node, go up one level. */
8696 --depth;
8697 line_breakcheck();
8698 }
8699 else
8700 {
8701 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008702 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008703 ++curi[depth];
8704 c = byts[n];
8705 if (c == 0)
8706 {
8707 /* End of prefix, find out how many IDs there are. */
8708 for (i = 1; i < len; ++i)
8709 if (byts[n + i] != 0)
8710 break;
8711 curi[depth] += i - 1;
8712
Bram Moolenaar53805d12005-08-01 07:08:33 +00008713 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
8714 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008715 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008716 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008717 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008718 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008719 : flags, lnum);
8720 if (lnum != 0)
8721 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008722 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008723
8724 /* Check for prefix that matches the word when the
8725 * first letter is upper-case, but only if the prefix has
8726 * a condition. */
8727 if (has_word_up)
8728 {
8729 c = valid_word_prefix(i, n, flags, word_up, slang,
8730 TRUE);
8731 if (c != 0)
8732 {
8733 vim_strncpy(prefix + depth, word_up,
8734 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008735 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008736 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00008737 : flags, lnum);
8738 if (lnum != 0)
8739 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008740 }
8741 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008742 }
8743 else
8744 {
8745 /* Normal char, go one level deeper. */
8746 prefix[depth++] = c;
8747 arridx[depth] = idxs[n];
8748 curi[depth] = 1;
8749 }
8750 }
8751 }
8752 }
8753
8754 return lnum;
8755}
8756
Bram Moolenaar95529562005-08-25 21:21:38 +00008757/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008758 * Move "p" to the end of word "start".
8759 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +00008760 */
8761 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008762spell_to_word_end(char_u *start, win_T *win)
Bram Moolenaar95529562005-08-25 21:21:38 +00008763{
8764 char_u *p = start;
8765
Bram Moolenaar860cae12010-06-05 23:22:07 +02008766 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008767 MB_PTR_ADV(p);
Bram Moolenaar95529562005-08-25 21:21:38 +00008768 return p;
8769}
8770
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008771#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008772/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008773 * For Insert mode completion CTRL-X s:
8774 * Find start of the word in front of column "startcol".
8775 * We don't check if it is badly spelled, with completion we can only change
8776 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008777 * Returns the column number of the word.
8778 */
8779 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008780spell_word_start(int startcol)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008781{
8782 char_u *line;
8783 char_u *p;
8784 int col = 0;
8785
Bram Moolenaar95529562005-08-25 21:21:38 +00008786 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008787 return startcol;
8788
8789 /* Find a word character before "startcol". */
8790 line = ml_get_curline();
8791 for (p = line + startcol; p > line; )
8792 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008793 MB_PTR_BACK(line, p);
Bram Moolenaarcc63c642013-11-12 04:44:01 +01008794 if (spell_iswordp_nmw(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008795 break;
8796 }
8797
8798 /* Go back to start of the word. */
8799 while (p > line)
8800 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008801 col = (int)(p - line);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01008802 MB_PTR_BACK(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008803 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008804 break;
8805 col = 0;
8806 }
8807
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008808 return col;
8809}
8810
8811/*
Bram Moolenaar4effc802005-09-30 21:12:02 +00008812 * Need to check for 'spellcapcheck' now, the word is removed before
8813 * expand_spelling() is called. Therefore the ugly global variable.
8814 */
8815static int spell_expand_need_cap;
8816
8817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008818spell_expand_check_cap(colnr_T col)
Bram Moolenaar4effc802005-09-30 21:12:02 +00008819{
8820 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
8821}
8822
8823/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008824 * Get list of spelling suggestions.
8825 * Used for Insert mode completion CTRL-X ?.
8826 * Returns the number of matches. The matches are in "matchp[]", array of
8827 * allocated strings.
8828 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008829 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01008830expand_spelling(
8831 linenr_T lnum UNUSED,
8832 char_u *pat,
8833 char_u ***matchp)
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008834{
8835 garray_T ga;
8836
Bram Moolenaar4770d092006-01-12 23:22:24 +00008837 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008838 *matchp = ga.ga_data;
8839 return ga.ga_len;
8840}
8841#endif
8842
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008843#endif /* FEAT_SPELL */