Mercurial > vim
annotate src/spell.c @ 15605:62b3805506b3 v8.1.0810
patch 8.1.0810: too many #ifdefs
commit https://github.com/vim/vim/commit/264b74fa545edfb92c0d7d08a02c26331cc5b168
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jan 24 17:18:42 2019 +0100
patch 8.1.0810: too many #ifdefs
Problem: Too many #ifdefs.
Solution: Graduate FEAT_MBYTE, part 4.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 24 Jan 2019 17:30:08 +0100 |
parents | dd725a8ab112 |
children | 6f1c7e9a6393 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
9953
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
223 | 2 * |
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 | |
226 | 12 * |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
13 * See spellfile.c for the Vim spell file format. |
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
14 * |
300 | 15 * 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). | |
324 | 18 * |
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". | |
300 | 35 * |
339 | 36 * There are two word trees: one with case-folded words and one with words in |
300 | 37 * original case. The second one is only used for keep-case words and is |
38 * usually small. | |
39 * | |
481 | 40 * There is one additional tree for when not all prefixes are applied when |
339 | 41 * 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 * | |
300 | 46 * Thanks to Olaf Seibert for providing an example implementation of this tree |
47 * and the compression mechanism. | |
625 | 48 * 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 | |
243 | 51 * |
52 * Matching involves checking the caps type: Onecap ALLCAP KeepCap. | |
53 * | |
236 | 54 * Why doesn't Vim use aspell/ispell/myspell/etc.? |
55 * See ":help develop-spell". | |
56 */ | |
57 | |
300 | 58 /* |
324 | 59 * 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. | |
625 | 62 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@" |
63 * vs "ht") and goes down in the list. | |
344 | 64 * Used when 'spellsuggest' is set to "best". |
65 */ | |
66 #define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4) | |
67 | |
68 /* | |
625 | 69 * Do the opposite: based on a maximum end score and a known sound score, |
1705 | 70 * compute the maximum word score that can be used. |
625 | 71 */ |
72 #define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3) | |
73 | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
74 #define IN_SPELL_C |
223 | 75 #include "vim.h" |
76 | |
737 | 77 #if defined(FEAT_SPELL) || defined(PROTO) |
223 | 78 |
625 | 79 #ifndef UNIX /* it's in os_unix.h for Unix */ |
80 # include <time.h> /* for time_t */ | |
81 #endif | |
82 | |
633 | 83 /* only used for su_badflags */ |
84 #define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */ | |
85 | |
389 | 86 #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP) |
300 | 87 |
307 | 88 #define REGION_ALL 0xff /* word valid in all regions */ |
89 | |
625 | 90 #define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */ |
91 #define VIMSUGMAGICL 6 | |
92 #define VIMSUGVERSION 1 | |
93 | |
307 | 94 /* Result values. Lower number is accepted over higher one. */ |
95 #define SP_BANNED -1 | |
236 | 96 #define SP_OK 0 |
307 | 97 #define SP_RARE 1 |
98 #define SP_LOCAL 2 | |
99 #define SP_BAD 3 | |
236 | 100 |
625 | 101 typedef 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 | |
9570
695186e11daa
commit https://github.com/vim/vim/commit/840268400dc8fda62a14f8a084e8b1ea46619454
Christian Brabandt <cb@256bit.org>
parents:
9487
diff
changeset
|
107 #define WC_KEY_OFF offsetof(wordcount_T, wc_word) |
625 | 108 #define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF)) |
109 #define MAXWORDCOUNT 0xffff | |
110 | |
236 | 111 /* |
323 | 112 * Information used when looking for suggestions. |
113 */ | |
114 typedef struct suginfo_S | |
115 { | |
116 garray_T su_ga; /* suggestions, contains "suggest_T" */ | |
344 | 117 int su_maxcount; /* max. number of suggestions displayed */ |
323 | 118 int su_maxscore; /* maximum score for adding to su_ga */ |
625 | 119 int su_sfmaxscore; /* idem, for when doing soundfold words */ |
344 | 120 garray_T su_sga; /* like su_ga, sound-folded scoring */ |
323 | 121 char_u *su_badptr; /* start of bad word in line */ |
122 int su_badlen; /* length of detected bad word in line */ | |
346 | 123 int su_badflags; /* caps flags for bad word */ |
323 | 124 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */ |
125 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */ | |
534 | 126 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */ |
323 | 127 hashtab_T su_banned; /* table with banned words */ |
503 | 128 slang_T *su_sallang; /* default language for sound folding */ |
323 | 129 } suginfo_T; |
130 | |
131 /* One word suggestion. Used in "si_ga". */ | |
132 typedef struct suggest_S | |
133 { | |
134 char_u *st_word; /* suggested word, allocated string */ | |
625 | 135 int st_wordlen; /* STRLEN(st_word) */ |
323 | 136 int st_orglen; /* length of replaced text */ |
137 int st_score; /* lower is better */ | |
344 | 138 int st_altscore; /* used when st_score compares equal */ |
139 int st_salscore; /* st_score is for soundalike */ | |
324 | 140 int st_had_bonus; /* bonus already included in score */ |
503 | 141 slang_T *st_slang; /* language used for sound folding */ |
323 | 142 } suggest_T; |
143 | |
344 | 144 #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i]) |
323 | 145 |
625 | 146 /* 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 | |
1705 | 149 /* Number of suggestions kept when cleaning up. We need to keep more than |
625 | 150 * 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) | |
324 | 153 |
154 /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots | |
155 * of suggestions that are not going to be displayed. */ | |
625 | 156 #define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50) |
323 | 157 |
158 /* score for various changes */ | |
344 | 159 #define SCORE_SPLIT 149 /* split bad word */ |
698 | 160 #define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */ |
323 | 161 #define SCORE_ICASE 52 /* slightly different case */ |
483 | 162 #define SCORE_REGION 200 /* word is for different region */ |
323 | 163 #define SCORE_RARE 180 /* rare word */ |
625 | 164 #define SCORE_SWAP 75 /* swap two characters */ |
323 | 165 #define SCORE_SWAP3 110 /* swap two characters in three */ |
532 | 166 #define SCORE_REP 65 /* REP replacement */ |
323 | 167 #define SCORE_SUBST 93 /* substitute a character */ |
168 #define SCORE_SIMILAR 33 /* substitute a similar character */ | |
479 | 169 #define SCORE_SUBCOMP 33 /* substitute a composing character */ |
324 | 170 #define SCORE_DEL 94 /* delete a character */ |
532 | 171 #define SCORE_DELDUP 66 /* delete a duplicated character */ |
479 | 172 #define SCORE_DELCOMP 28 /* delete a composing character */ |
324 | 173 #define SCORE_INS 96 /* insert a character */ |
532 | 174 #define SCORE_INSDUP 67 /* insert a duplicate character */ |
475 | 175 #define SCORE_INSCOMP 30 /* insert a composing character */ |
366 | 176 #define SCORE_NONWORD 103 /* change non-word to word char */ |
323 | 177 |
372 | 178 #define SCORE_FILE 30 /* suggestion from a file */ |
323 | 179 #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower. |
180 * 350 allows for about three changes. */ | |
344 | 181 |
625 | 182 #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 | |
344 | 196 #define SCORE_BIG SCORE_INS * 3 /* big difference */ |
625 | 197 #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 | |
323 | 203 |
204 /* | |
236 | 205 * Structure to store info for word matching. |
206 */ | |
207 typedef struct matchinf_S | |
208 { | |
209 langp_T *mi_lp; /* info for language and region */ | |
243 | 210 |
211 /* pointers to original text to be checked */ | |
236 | 212 char_u *mi_word; /* start of word being checked */ |
339 | 213 char_u *mi_end; /* end of matching word so far */ |
243 | 214 char_u *mi_fend; /* next char to be added to mi_fword */ |
300 | 215 char_u *mi_cend; /* char after what was used for |
216 mi_capflags */ | |
243 | 217 |
218 /* case-folded text */ | |
219 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */ | |
300 | 220 int mi_fwordlen; /* nr of valid bytes in mi_fword */ |
243 | 221 |
339 | 222 /* for when checking word after a prefix */ |
223 int mi_prefarridx; /* index in sl_pidxs with list of | |
481 | 224 affixID/condition */ |
339 | 225 int mi_prefcnt; /* number of entries at mi_prefarridx */ |
226 int mi_prefixlen; /* byte length of prefix */ | |
455 | 227 int mi_cprefixlen; /* byte length of prefix in original |
228 case */ | |
339 | 229 |
481 | 230 /* for when checking a compound word */ |
231 int mi_compoff; /* start of following word offset */ | |
483 | 232 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */ |
233 int mi_complen; /* nr of compound words used */ | |
809 | 234 int mi_compextra; /* nr of COMPOUNDROOT words */ |
481 | 235 |
243 | 236 /* others */ |
236 | 237 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */ |
300 | 238 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
239 win_T *mi_win; /* buffer being checked */ |
492 | 240 |
241 /* for NOBREAK */ | |
242 int mi_result2; /* "mi_resul" without following word */ | |
243 char_u *mi_end2; /* "mi_end" without following word */ | |
236 | 244 } matchinf_T; |
245 | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
246 |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
247 static int spell_iswordp(char_u *p, win_T *wp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
248 static int spell_mb_isword_class(int cl, win_T *wp); |
307 | 249 |
250 /* | |
344 | 251 * For finding suggestions: At each node in the tree these states are tried: |
330 | 252 */ |
253 typedef enum | |
254 { | |
344 | 255 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. */ | |
375 | 258 STATE_NOPREFIX, /* try without prefix */ |
344 | 259 STATE_SPLITUNDO, /* Undo splitting. */ |
330 | 260 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. */ | |
625 | 263 STATE_INS_PREP, /* Prepare for inserting bytes. */ |
330 | 264 STATE_INS, /* Insert a byte in the bad word. */ |
265 STATE_SWAP, /* Swap two bytes. */ | |
344 | 266 STATE_UNSWAP, /* Undo swap two characters. */ |
267 STATE_SWAP3, /* Swap two characters over three. */ | |
268 STATE_UNSWAP3, /* Undo Swap two characters over three. */ | |
269 STATE_UNROT3L, /* Undo rotate three characters left */ | |
270 STATE_UNROT3R, /* Undo rotate three characters right */ | |
330 | 271 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 /* | |
346 | 278 * Struct to keep the state at each level in suggest_try_change(). |
323 | 279 */ |
280 typedef struct trystate_S | |
281 { | |
330 | 282 state_T ts_state; /* state at this level, STATE_ */ |
323 | 283 int ts_score; /* score */ |
344 | 284 idx_T ts_arridx; /* index in tree array, start of node */ |
330 | 285 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[] */ | |
482 | 289 char_u ts_prefixdepth; /* stack depth for end of prefix or |
485 | 290 * PFD_PREFIXTREE or PFD_NOPREFIX */ |
291 char_u ts_flags; /* TSF_ flags */ | |
330 | 292 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 */ | |
483 | 296 char_u ts_prewordlen; /* length of word in "preword[]" */ |
297 char_u ts_splitoff; /* index in "tword" after last split */ | |
492 | 298 char_u ts_splitfidx; /* "ts_fidx" at word split */ |
483 | 299 char_u ts_complen; /* nr of compound words used */ |
485 | 300 char_u ts_compsplit; /* index for "compflags" where word was spit */ |
346 | 301 char_u ts_save_badflags; /* su_badflags saved here */ |
625 | 302 char_u ts_delidx; /* index in fword for char that was deleted, |
303 valid when "ts_flags" has TSF_DIDDEL */ | |
323 | 304 } trystate_T; |
305 | |
330 | 306 /* 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 | |
485 | 311 /* 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 */ | |
625 | 314 #define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */ |
485 | 315 |
375 | 316 /* special values ts_prefixdepth */ |
485 | 317 #define PFD_NOPREFIX 0xff /* not using prefixes */ |
482 | 318 #define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */ |
625 | 319 #define PFD_NOTSPECIAL 0xfd /* highest value that's not special */ |
375 | 320 |
339 | 321 /* mode values for find_word */ |
481 | 322 #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 */ | |
339 | 327 |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
328 static void find_word(matchinf_T *mip, int mode); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
329 static int match_checkcompoundpattern(char_u *ptr, int wlen, garray_T *gap); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
330 static int can_compound(slang_T *slang, char_u *word, char_u *flags); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
331 static int match_compoundrule(slang_T *slang, char_u *compflags); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
332 static int valid_word_prefix(int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
333 static void find_prefix(matchinf_T *mip, int mode); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
334 static int fold_more(matchinf_T *mip); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
335 static int spell_valid_case(int wordflags, int treeflags); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
336 static void spell_load_cb(char_u *fname, void *cookie); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
337 static int count_syllables(slang_T *slang, char_u *word); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
338 static void clear_midword(win_T *buf); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
339 static void use_midword(slang_T *lp, win_T *buf); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
340 static int find_region(char_u *rp, char_u *region); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
341 static int check_need_cap(linenr_T lnum, colnr_T col); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
342 static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive); |
372 | 343 #ifdef FEAT_EVAL |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
344 static void spell_suggest_expr(suginfo_T *su, char_u *expr); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
345 #endif |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
346 static void spell_suggest_file(suginfo_T *su, char_u *fname); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
347 static void spell_suggest_intern(suginfo_T *su, int interactive); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
348 static void spell_find_cleanup(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
349 static void suggest_try_special(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
350 static void suggest_try_change(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
351 static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char_u *fword, int soundfold); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
352 static void go_deeper(trystate_T *stack, int depth, int score_add); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
353 static int nofold_len(char_u *fword, int flen, char_u *word); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
354 static void find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
355 static void score_comp_sal(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
356 static void score_combine(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
357 static int stp_sal_score(suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
358 static void suggest_try_soundalike_prep(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
359 static void suggest_try_soundalike(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
360 static void suggest_try_soundalike_finish(void); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
361 static void add_sound_suggest(suginfo_T *su, char_u *goodword, int score, langp_T *lp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
362 static int soundfold_find(slang_T *slang, char_u *word); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
363 static void make_case_word(char_u *fword, char_u *cword, int flags); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
364 static int similar_chars(slang_T *slang, int c1, int c2); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
365 static 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); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
366 static void check_suggestions(suginfo_T *su, garray_T *gap); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
367 static void add_banned(suginfo_T *su, char_u *word); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
368 static void rescore_suggestions(suginfo_T *su); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
369 static void rescore_one(suginfo_T *su, suggest_T *stp); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
370 static int cleanup_suggestions(garray_T *gap, int maxscore, int keep); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
371 static void spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
372 static void spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
373 static void spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
374 static int soundalike_score(char_u *goodsound, char_u *badsound); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
375 static int spell_edit_score(slang_T *slang, char_u *badword, char_u *goodword); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
376 static int spell_edit_score_limit(slang_T *slang, char_u *badword, char_u *goodword, int limit); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
377 static int spell_edit_score_limit_w(slang_T *slang, char_u *badword, char_u *goodword, int limit); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
378 static void dump_word(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum); |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
379 static linenr_T dump_prefixes(slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum); |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
380 |
236 | 381 |
626 | 382 /* Remember what "z?" replaced. */ |
383 static char_u *repl_from = NULL; | |
384 static char_u *repl_to = NULL; | |
385 | |
236 | 386 /* |
387 * Main spell-checking function. | |
300 | 388 * "ptr" points to a character that could be the start of a word. |
534 | 389 * "*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. | |
236 | 391 * This must only be called when 'spelllang' is not empty. |
323 | 392 * |
385 | 393 * "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. | |
323 | 397 * |
236 | 398 * 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
402 spell_check( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
403 win_T *wp, /* current window */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
404 char_u *ptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
405 hlf_T *attrp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
406 int *capcol, /* column to check for Capital */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
407 int docount) /* count good words */ |
236 | 408 { |
409 matchinf_T mi; /* Most things are put in "mi" so that it can | |
410 be passed to functions quickly. */ | |
344 | 411 int nrlen = 0; /* found a number first */ |
385 | 412 int c; |
483 | 413 int wrongcaplen = 0; |
500 | 414 int lpi; |
625 | 415 int count_word = docount; |
236 | 416 |
307 | 417 /* 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; | |
690 | 421 |
422 /* Return here when loading language files failed. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
423 if (wp->w_s->b_langp.ga_len == 0) |
690 | 424 return 1; |
425 | |
483 | 426 vim_memset(&mi, 0, sizeof(matchinf_T)); |
236 | 427 |
344 | 428 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
584 | 429 * 0X99FF. But always do check spelling to find "3GPP" and "11 |
430 * julifeest". */ | |
300 | 431 if (*ptr >= '0' && *ptr <= '9') |
432 { | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
433 if (*ptr == '0' && (ptr[1] == 'b' || ptr[1] == 'B')) |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
434 mi.mi_end = skipbin(ptr + 2); |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
435 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
316 | 436 mi.mi_end = skiphex(ptr + 2); |
300 | 437 else |
344 | 438 mi.mi_end = skipdigits(ptr); |
835 | 439 nrlen = (int)(mi.mi_end - ptr); |
584 | 440 } |
346 | 441 |
442 /* Find the normal end of the word (until the next non-word character). */ | |
344 | 443 mi.mi_word = ptr; |
584 | 444 mi.mi_fend = ptr; |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
445 if (spell_iswordp(mi.mi_fend, wp)) |
344 | 446 { |
447 do | |
448 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
449 MB_PTR_ADV(mi.mi_fend); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
450 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp)); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
451 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
452 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL) |
385 | 453 { |
454 /* Check word starting with capital letter. */ | |
455 | 455 c = PTR2CHAR(ptr); |
385 | 456 if (!SPELL_ISUPPER(c)) |
483 | 457 wrongcaplen = (int)(mi.mi_fend - ptr); |
385 | 458 } |
459 } | |
460 if (capcol != NULL) | |
461 *capcol = -1; | |
344 | 462 |
463 /* We always use the characters up to the next non-word character, | |
464 * also for bad words. */ | |
465 mi.mi_end = mi.mi_fend; | |
466 | |
467 /* Check caps type later. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
468 mi.mi_capflags = 0; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
469 mi.mi_cend = NULL; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
470 mi.mi_win = wp; |
344 | 471 |
483 | 472 /* case-fold the word with one non-word character, so that we can check |
473 * for the word end. */ | |
344 | 474 if (*mi.mi_fend != NUL) |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
475 MB_PTR_ADV(mi.mi_fend); |
344 | 476 |
477 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, | |
478 MAXWLEN + 1); | |
835 | 479 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword); |
344 | 480 |
481 /* The word is bad unless we recognize it. */ | |
482 mi.mi_result = SP_BAD; | |
492 | 483 mi.mi_result2 = SP_BAD; |
344 | 484 |
485 /* | |
486 * Loop over the languages specified in 'spelllang'. | |
625 | 487 * We check them all, because a word may be matched longer in another |
488 * language. | |
344 | 489 */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
490 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
491 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
492 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi); |
500 | 493 |
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 | |
344 | 499 /* 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. */ | |
485 | 506 find_prefix(&mi, FIND_FOLDWORD); |
492 | 507 |
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 } | |
625 | 516 |
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 } | |
344 | 524 } |
525 | |
526 if (mi.mi_result != SP_OK) | |
527 { | |
346 | 528 /* If we found a number skip over it. Allows for "42nd". Do flag |
529 * rare and local words, e.g., "3GPP". */ | |
344 | 530 if (nrlen > 0) |
346 | 531 { |
532 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) | |
533 return nrlen; | |
534 } | |
344 | 535 |
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). */ | |
5477 | 538 else if (!spell_iswordp_nmw(ptr, wp)) |
243 | 539 { |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
540 if (capcol != NULL && wp->w_s->b_cap_prog != NULL) |
385 | 541 { |
542 regmatch_T regmatch; | |
6375 | 543 int r; |
385 | 544 |
545 /* Check for end of sentence. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
546 regmatch.regprog = wp->w_s->b_cap_prog; |
385 | 547 regmatch.rm_ic = FALSE; |
6375 | 548 r = vim_regexec(®match, ptr, 0); |
549 wp->w_s->b_cap_prog = regmatch.regprog; | |
550 if (r) | |
385 | 551 *capcol = (int)(regmatch.endp[0] - ptr); |
552 } | |
553 | |
344 | 554 if (has_mbyte) |
474 | 555 return (*mb_ptr2len)(ptr); |
344 | 556 return 1; |
300 | 557 } |
483 | 558 else if (mi.mi_end == ptr) |
559 /* Always include at least one character. Required for when there | |
560 * is a mixup in "midword". */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
561 MB_PTR_ADV(mi.mi_end); |
492 | 562 else if (mi.mi_result == SP_BAD |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
563 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak) |
492 | 564 { |
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. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
570 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0); |
500 | 571 if (mi.mi_lp->lp_slang->sl_fidxs != NULL) |
572 { | |
573 p = mi.mi_word; | |
574 fp = mi.mi_fword; | |
575 for (;;) | |
576 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
577 MB_PTR_ADV(p); |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
578 MB_PTR_ADV(fp); |
500 | 579 if (p >= mi.mi_end) |
580 break; | |
835 | 581 mi.mi_compoff = (int)(fp - mi.mi_fword); |
500 | 582 find_word(&mi, FIND_COMPOUND); |
583 if (mi.mi_result != SP_BAD) | |
584 { | |
585 mi.mi_end = p; | |
586 break; | |
587 } | |
588 } | |
589 mi.mi_result = save_result; | |
590 } | |
492 | 591 } |
243 | 592 |
344 | 593 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
534 | 594 *attrp = HLF_SPB; |
344 | 595 else if (mi.mi_result == SP_RARE) |
534 | 596 *attrp = HLF_SPR; |
344 | 597 else |
534 | 598 *attrp = HLF_SPL; |
243 | 599 } |
600 | |
483 | 601 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. */ | |
534 | 604 *attrp = HLF_SPC; |
483 | 605 return wrongcaplen; |
606 } | |
607 | |
300 | 608 return (int)(mi.mi_end - ptr); |
236 | 609 } |
610 | |
611 /* | |
300 | 612 * Check if the word at "mip->mi_word" is in the tree. |
339 | 613 * 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. | |
300 | 617 * |
618 * For a match mip->mi_result is updated. | |
243 | 619 */ |
620 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
621 find_word(matchinf_T *mip, int mode) |
243 | 622 { |
324 | 623 idx_T arridx = 0; |
300 | 624 int endlen[MAXWLEN]; /* length at possible word endings */ |
324 | 625 idx_T endidx[MAXWLEN]; /* possible word endings */ |
300 | 626 int endidxcnt = 0; |
627 int len; | |
628 int wlen = 0; | |
629 int flen; | |
630 int c; | |
631 char_u *ptr; | |
324 | 632 idx_T lo, hi, m; |
300 | 633 char_u *s; |
339 | 634 char_u *p; |
307 | 635 int res = SP_BAD; |
300 | 636 slang_T *slang = mip->mi_lp->lp_slang; |
637 unsigned flags; | |
638 char_u *byts; | |
324 | 639 idx_T *idxs; |
481 | 640 int word_ends; |
485 | 641 int prefix_found; |
492 | 642 int nobreak_result; |
481 | 643 |
644 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND) | |
236 | 645 { |
300 | 646 /* 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; | |
481 | 651 |
652 if (mode == FIND_KEEPCOMPOUND) | |
653 /* Skip over the previously found word(s). */ | |
654 wlen += mip->mi_compoff; | |
236 | 655 } |
656 else | |
657 { | |
300 | 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; | |
339 | 663 |
664 if (mode == FIND_PREFIX) | |
665 { | |
666 /* Skip over the prefix. */ | |
667 wlen = mip->mi_prefixlen; | |
668 flen -= mip->mi_prefixlen; | |
669 } | |
481 | 670 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 | |
243 | 677 } |
678 | |
300 | 679 if (byts == NULL) |
680 return; /* array is empty */ | |
236 | 681 |
682 /* | |
307 | 683 * 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. | |
236 | 687 */ |
300 | 688 for (;;) |
236 | 689 { |
346 | 690 if (flen <= 0 && *mip->mi_fend != NUL) |
339 | 691 flen = fold_more(mip); |
300 | 692 |
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 { | |
307 | 699 if (endidxcnt == MAXWLEN) |
700 { | |
701 /* Must be a corrupted spell file. */ | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
702 emsg(_(e_format)); |
307 | 703 return; |
704 } | |
300 | 705 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]; | |
346 | 726 if (c == TAB) /* <Tab> is handled like <Space> */ |
727 c = ' '; | |
300 | 728 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; | |
236 | 741 } |
742 } | |
300 | 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; | |
346 | 752 |
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 } | |
236 | 767 } |
768 | |
300 | 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]; | |
236 | 778 |
300 | 779 if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
780 continue; /* not at first byte of character */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
781 if (spell_iswordp(ptr + wlen, mip->mi_win)) |
481 | 782 { |
492 | 783 if (slang->sl_compprog == NULL && !slang->sl_nobreak) |
481 | 784 continue; /* next char is a word character */ |
785 word_ends = FALSE; | |
786 } | |
787 else | |
788 word_ends = TRUE; | |
485 | 789 /* The prefix flag is before compound flags. Once a valid prefix flag |
790 * has been found we try compound flags. */ | |
791 prefix_found = FALSE; | |
300 | 792 |
339 | 793 if (mode != FIND_KEEPWORD && has_mbyte) |
300 | 794 { |
795 /* Compute byte length in original word, length may change | |
339 | 796 * when folding case. This can be slow, take a shortcut when the |
797 * case-folded word is equal to the keep-case word. */ | |
300 | 798 p = mip->mi_word; |
339 | 799 if (STRNCMP(ptr, p, wlen) != 0) |
800 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
801 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s)) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
802 MB_PTR_ADV(p); |
835 | 803 wlen = (int)(p - mip->mi_word); |
339 | 804 } |
300 | 805 } |
236 | 806 |
339 | 807 /* 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) | |
300 | 814 { |
815 flags = idxs[arridx]; | |
324 | 816 |
339 | 817 /* 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) | |
300 | 822 { |
823 if (mip->mi_cend != mip->mi_word + wlen) | |
824 { | |
323 | 825 /* mi_capflags was set for a different word length, need |
826 * to do it again. */ | |
300 | 827 mip->mi_cend = mip->mi_word + wlen; |
323 | 828 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
300 | 829 } |
830 | |
346 | 831 if (mip->mi_capflags == WF_KEEPCAP |
832 || !spell_valid_case(mip->mi_capflags, flags)) | |
339 | 833 continue; |
300 | 834 } |
236 | 835 |
339 | 836 /* 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 | |
366 | 838 * mip->mi_prefarridx that find_prefix() filled. */ |
485 | 839 else if (mode == FIND_PREFIX && !prefix_found) |
481 | 840 { |
366 | 841 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
390 | 842 flags, |
455 | 843 mip->mi_word + mip->mi_cprefixlen, slang, |
844 FALSE); | |
366 | 845 if (c == 0) |
339 | 846 continue; |
366 | 847 |
848 /* Use the WF_RARE flag for a rare prefix. */ | |
849 if (c & WF_RAREPFX) | |
850 flags |= WF_RARE; | |
485 | 851 prefix_found = TRUE; |
339 | 852 } |
853 | |
492 | 854 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)) | |
481 | 868 { |
974 | 869 /* If there is no compound flag or the word is shorter than |
483 | 870 * COMPOUNDMIN reject it quickly. |
871 * Makes you wonder why someone puts a compound flag on a word | |
481 | 872 * that's too short... Myspell compatibility requires this |
873 * anyway. */ | |
490 | 874 if (((unsigned)flags >> 24) == 0 |
875 || wlen - mip->mi_compoff < slang->sl_compminlen) | |
483 | 876 continue; |
500 | 877 /* For multi-byte chars check character length against |
878 * COMPOUNDMIN. */ | |
879 if (has_mbyte | |
501 | 880 && slang->sl_compminlen > 0 |
500 | 881 && mb_charlen_len(mip->mi_word + mip->mi_compoff, |
882 wlen - mip->mi_compoff) < slang->sl_compminlen) | |
883 continue; | |
483 | 884 |
809 | 885 /* Limit the number of compound words to COMPOUNDWORDMAX if no |
490 | 886 * maximum for syllables is specified. */ |
809 | 887 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2 |
888 > slang->sl_compmax | |
490 | 889 && slang->sl_compsylmax == MAXWLEN) |
483 | 890 continue; |
891 | |
819 | 892 /* 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 | |
485 | 899 /* Quickly check if compounding is possible with this flag. */ |
495 | 900 if (!byte_in_str(mip->mi_complen == 0 |
485 | 901 ? slang->sl_compstartflags |
902 : slang->sl_compallflags, | |
495 | 903 ((unsigned)flags >> 24))) |
481 | 904 continue; |
905 | |
1762 | 906 /* 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 | |
490 | 911 if (mode == FIND_COMPOUND) |
912 { | |
913 int capflags; | |
914 | |
915 /* Need to check the caps type of the appended compound | |
916 * word. */ | |
917 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; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
922 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s)) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
923 MB_PTR_ADV(p); |
490 | 924 } |
925 else | |
926 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. */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
938 MB_PTR_BACK(mip->mi_word, p); |
5477 | 939 if (spell_iswordp_nmw(p, mip->mi_win) |
490 | 940 ? capflags == WF_ONECAP |
941 : (flags & WF_ONECAP) != 0 | |
942 && capflags != WF_ONECAP) | |
943 continue; | |
944 } | |
945 } | |
946 | |
483 | 947 /* If the word ends the sequence of compound flags of the |
714 | 948 * words must match with one of the COMPOUNDRULE items and |
483 | 949 * 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 } | |
1762 | 967 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; | |
481 | 972 } |
973 | |
500 | 974 /* Check NEEDCOMPOUND: can't use word without compounding. */ |
975 else if (flags & WF_NEEDCOMP) | |
976 continue; | |
977 | |
492 | 978 nobreak_result = SP_OK; |
979 | |
481 | 980 if (!word_ends) |
981 { | |
492 | 982 int save_result = mip->mi_result; |
983 char_u *save_end = mip->mi_end; | |
501 | 984 langp_T *save_lp = mip->mi_lp; |
985 int lpi; | |
492 | 986 |
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. | |
481 | 991 * Recursive! */ |
492 | 992 if (slang->sl_nobreak) |
993 mip->mi_result = SP_BAD; | |
481 | 994 |
995 /* Find following word in case-folded tree. */ | |
996 mip->mi_compoff = endlen[endidxcnt]; | |
997 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 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1006 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s)) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1007 MB_PTR_ADV(p); |
835 | 1008 mip->mi_compoff = (int)(p - mip->mi_fword); |
481 | 1009 } |
1010 } | |
8956
d9e671c5afe6
commit https://github.com/vim/vim/commit/ba53435144f46eaaa53c63a62e748b3feee9742c
Christian Brabandt <cb@256bit.org>
parents:
8524
diff
changeset
|
1011 #if 0 /* Disabled, see below */ |
485 | 1012 c = mip->mi_compoff; |
8956
d9e671c5afe6
commit https://github.com/vim/vim/commit/ba53435144f46eaaa53c63a62e748b3feee9742c
Christian Brabandt <cb@256bit.org>
parents:
8524
diff
changeset
|
1013 #endif |
483 | 1014 ++mip->mi_complen; |
809 | 1015 if (flags & WF_COMPROOT) |
1016 ++mip->mi_compextra; | |
501 | 1017 |
1018 /* For NOBREAK we need to try all NOBREAK languages, at least | |
1019 * to find the ".add" file(s). */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1020 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi) |
501 | 1021 { |
1022 if (slang->sl_nobreak) | |
1023 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1024 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi); |
501 | 1025 if (mip->mi_lp->lp_slang->sl_fidxs == NULL |
1026 || !mip->mi_lp->lp_slang->sl_nobreak) | |
1027 continue; | |
1028 } | |
1029 | |
1030 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. */ | |
492 | 1035 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
1036 { | |
501 | 1037 /* Find following word in keep-case tree. */ |
1038 mip->mi_compoff = wlen; | |
1039 find_word(mip, FIND_KEEPCOMPOUND); | |
1040 | |
819 | 1041 #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. */ | |
501 | 1044 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 } | |
819 | 1050 #endif |
492 | 1051 } |
501 | 1052 |
1053 if (!slang->sl_nobreak) | |
1054 break; | |
492 | 1055 } |
483 | 1056 --mip->mi_complen; |
809 | 1057 if (flags & WF_COMPROOT) |
1058 --mip->mi_compextra; | |
501 | 1059 mip->mi_lp = save_lp; |
485 | 1060 |
492 | 1061 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 } | |
481 | 1073 } |
1074 | |
339 | 1075 if (flags & WF_BANNED) |
1076 res = SP_BANNED; | |
1077 else if (flags & WF_REGION) | |
1078 { | |
1079 /* Check region. */ | |
390 | 1080 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) |
300 | 1081 res = SP_OK; |
339 | 1082 else |
1083 res = SP_LOCAL; | |
300 | 1084 } |
339 | 1085 else if (flags & WF_RARE) |
1086 res = SP_RARE; | |
307 | 1087 else |
339 | 1088 res = SP_OK; |
1089 | |
492 | 1090 /* 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) | |
339 | 1105 { |
1106 mip->mi_result = res; | |
1107 mip->mi_end = mip->mi_word + wlen; | |
1108 } | |
351 | 1109 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) |
339 | 1110 mip->mi_end = mip->mi_word + wlen; |
1111 | |
492 | 1112 if (mip->mi_result == SP_OK) |
339 | 1113 break; |
300 | 1114 } |
1115 | |
492 | 1116 if (mip->mi_result == SP_OK) |
300 | 1117 break; |
1118 } | |
236 | 1119 } |
1120 | |
323 | 1121 /* |
1762 | 1122 * 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1129 match_checkcompoundpattern( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1130 char_u *ptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1131 int wlen, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1132 garray_T *gap) /* &sl_comppat */ |
1762 | 1133 { |
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]; | |
1771 | 1146 len = (int)STRLEN(p); |
1762 | 1147 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) |
1148 return TRUE; | |
1149 } | |
1150 } | |
1151 return FALSE; | |
1152 } | |
1153 | |
1154 /* | |
626 | 1155 * Return TRUE if "flags" is a valid sequence of compound flags and "word" |
1156 * does not have too many syllables. | |
482 | 1157 */ |
1158 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1159 can_compound(slang_T *slang, char_u *word, char_u *flags) |
483 | 1160 { |
495 | 1161 char_u uflags[MAXWLEN * 2]; |
1162 int i; | |
1163 char_u *p; | |
483 | 1164 |
1165 if (slang->sl_compprog == NULL) | |
1166 return FALSE; | |
495 | 1167 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) | |
11269
121d29004998
patch 8.0.0520: using a function pointer while the function is known
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1172 p += utf_char2bytes(flags[i], p); |
495 | 1173 *p = NUL; |
1174 p = uflags; | |
1175 } | |
1176 else | |
1177 p = flags; | |
6375 | 1178 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0)) |
483 | 1179 return FALSE; |
1180 | |
490 | 1181 /* 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 | |
809 | 1183 * COMPOUNDWORDMAX then compounding is not allowed. */ |
483 | 1184 if (slang->sl_compsylmax < MAXWLEN |
1185 && count_syllables(slang, word) > slang->sl_compsylmax) | |
495 | 1186 return (int)STRLEN(flags) < slang->sl_compmax; |
483 | 1187 return TRUE; |
482 | 1188 } |
1189 | |
1190 /* | |
1762 | 1191 * 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1196 can_be_compound( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1197 trystate_T *sp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1198 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1199 char_u *compflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1200 int flag) |
1762 | 1201 { |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1233 match_compoundrule(slang_T *slang, char_u *compflags) |
1762 | 1234 { |
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 /* | |
390 | 1281 * Return non-zero if the prefix indicated by "arridx" matches with the prefix |
1282 * ID in "flags" for the word "word". | |
366 | 1283 * The WF_RAREPFX flag is included in the return value for a rare prefix. |
351 | 1284 */ |
1285 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1286 valid_word_prefix( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1287 int totprefcnt, /* nr of prefix IDs */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1288 int arridx, /* idx in sl_pidxs[] */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1289 int flags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1290 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1291 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1292 int cond_req) /* only use prefixes with a condition */ |
351 | 1293 { |
1294 int prefcnt; | |
1295 int pidx; | |
6375 | 1296 regprog_T **rp; |
390 | 1297 int prefid; |
1298 | |
1299 prefid = (unsigned)flags >> 24; | |
351 | 1300 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 | |
390 | 1308 /* 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 | |
351 | 1313 /* Check the condition, if there is one. The condition index is |
366 | 1314 * stored in the two bytes above the prefix ID byte. */ |
6375 | 1315 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
1316 if (*rp != NULL) | |
1317 { | |
1318 if (!vim_regexec_prog(rp, FALSE, word, 0)) | |
351 | 1319 continue; |
1320 } | |
455 | 1321 else if (cond_req) |
1322 continue; | |
1323 | |
1324 /* It's a match! Return the WF_ flags. */ | |
366 | 1325 return pidx; |
1326 } | |
1327 return 0; | |
351 | 1328 } |
1329 | |
1330 /* | |
339 | 1331 * Check if the word at "mip->mi_word" has a matching prefix. |
1332 * If it does, then check the following word. | |
1333 * | |
485 | 1334 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a |
1335 * prefix in a compound word. | |
1336 * | |
339 | 1337 * For a match mip->mi_result is updated. |
1338 */ | |
1339 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1340 find_prefix(matchinf_T *mip, int mode) |
339 | 1341 { |
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 | |
375 | 1353 byts = slang->sl_pbyts; |
1354 if (byts == NULL) | |
1355 return; /* array is empty */ | |
1356 | |
339 | 1357 /* 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 */ | |
485 | 1361 if (mode == FIND_COMPOUND) |
1362 { | |
1363 /* Skip over the previously found word(s). */ | |
1364 ptr += mip->mi_compoff; | |
1365 flen -= mip->mi_compoff; | |
1366 } | |
339 | 1367 idxs = slang->sl_pidxs; |
1368 | |
1369 /* | |
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; | |
485 | 1401 if (mode == FIND_COMPOUND) |
1402 /* Skip over the previously found word(s). */ | |
1403 mip->mi_prefixlen += mip->mi_compoff; | |
1404 | |
455 | 1405 if (has_mbyte) |
1406 { | |
1407 /* Case-folded length may differ from original length. */ | |
485 | 1408 mip->mi_cprefixlen = nofold_len(mip->mi_fword, |
1409 mip->mi_prefixlen, mip->mi_word); | |
455 | 1410 } |
1411 else | |
485 | 1412 mip->mi_cprefixlen = mip->mi_prefixlen; |
339 | 1413 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 | |
626 | 1455 * for efficiency. Include the non-word character too. |
339 | 1456 * Return the length of the folded chars in bytes. |
1457 */ | |
1458 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1459 fold_more(matchinf_T *mip) |
339 | 1460 { |
1461 int flen; | |
1462 char_u *p; | |
1463 | |
1464 p = mip->mi_fend; | |
1465 do | |
1466 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1467 MB_PTR_ADV(mip->mi_fend); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1468 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win)); |
339 | 1469 |
626 | 1470 /* Include the non-word character so that we can check for the word end. */ |
339 | 1471 if (*mip->mi_fend != NUL) |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1472 MB_PTR_ADV(mip->mi_fend); |
339 | 1473 |
1474 (void)spell_casefold(p, (int)(mip->mi_fend - p), | |
1475 mip->mi_fword + mip->mi_fwordlen, | |
1476 MAXWLEN - mip->mi_fwordlen); | |
835 | 1477 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen); |
339 | 1478 mip->mi_fwordlen += flen; |
1479 return flen; | |
1480 } | |
1481 | |
1482 /* | |
323 | 1483 * Check case flags for a word. Return TRUE if the word has the requested |
1484 * case. | |
1485 */ | |
1486 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1487 spell_valid_case( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1488 int wordflags, /* flags for the checked word. */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1489 int treeflags) /* flags for the word in the spell tree */ |
323 | 1490 { |
389 | 1491 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) |
323 | 1492 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
474 | 1493 && ((treeflags & WF_ONECAP) == 0 |
1494 || (wordflags & WF_ONECAP) != 0))); | |
323 | 1495 } |
1496 | |
351 | 1497 /* |
1498 * Return TRUE if spell checking is not enabled. | |
1499 */ | |
1500 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1501 no_spell_checking(win_T *wp) |
498 | 1502 { |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1503 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1504 || wp->w_s->b_langp.ga_len == 0) |
351 | 1505 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1506 emsg(_("E756: Spell checking is not enabled")); |
351 | 1507 return TRUE; |
1508 } | |
1509 return FALSE; | |
1510 } | |
300 | 1511 |
236 | 1512 /* |
1513 * Move to next spell error. | |
500 | 1514 * "curline" is FALSE for "[s", "]s", "[S" and "]S". |
1515 * "curline" is TRUE to find word under/after cursor in the same line. | |
483 | 1516 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move |
1517 * to after badly spelled word before the cursor. | |
495 | 1518 * Return 0 if not found, length of the badly spelled word otherwise. |
236 | 1519 */ |
1520 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1521 spell_move_to( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1522 win_T *wp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1523 int dir, /* FORWARD or BACKWARD */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1524 int allwords, /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1525 int curline, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1526 hlf_T *attrp) /* return: attributes of bad word or NULL |
534 | 1527 (only when "dir" is FORWARD) */ |
236 | 1528 { |
249 | 1529 linenr_T lnum; |
1530 pos_T found_pos; | |
495 | 1531 int found_len = 0; |
236 | 1532 char_u *line; |
1533 char_u *p; | |
346 | 1534 char_u *endp; |
534 | 1535 hlf_T attr; |
236 | 1536 int len; |
5519 | 1537 #ifdef FEAT_SYN_HL |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1538 int has_syntax = syntax_present(wp); |
5519 | 1539 #endif |
249 | 1540 int col; |
1541 int can_spell; | |
346 | 1542 char_u *buf = NULL; |
1543 int buflen = 0; | |
1544 int skip = 0; | |
385 | 1545 int capcol = -1; |
500 | 1546 int found_one = FALSE; |
1547 int wrapped = FALSE; | |
236 | 1548 |
498 | 1549 if (no_spell_checking(wp)) |
495 | 1550 return 0; |
236 | 1551 |
249 | 1552 /* |
1553 * Start looking for bad word at the start of the line, because we can't | |
817 | 1554 * start halfway a word, we don't know where it starts or ends. |
249 | 1555 * |
1556 * When searching backwards, we continue in the line to find the last | |
1557 * bad word (in the cursor line: before the cursor). | |
346 | 1558 * |
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... | |
249 | 1562 */ |
498 | 1563 lnum = wp->w_cursor.lnum; |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10950
diff
changeset
|
1564 CLEAR_POS(&found_pos); |
236 | 1565 |
1566 while (!got_int) | |
1567 { | |
498 | 1568 line = ml_get_buf(wp->w_buffer, lnum, FALSE); |
346 | 1569 |
835 | 1570 len = (int)STRLEN(line); |
346 | 1571 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 | |
385 | 1580 /* 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) | |
12323
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11521
diff
changeset
|
1586 capcol = getwhitecols(line); |
835 | 1587 else if (curline && wp == curwin) |
1588 { | |
1589 /* For spellbadword(): check if first word needs a capital. */ | |
12323
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11521
diff
changeset
|
1590 col = getwhitecols(line); |
835 | 1591 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 } | |
385 | 1598 |
346 | 1599 /* Copy the line into "buf" and append the start of the next line if |
1600 * possible. */ | |
1601 STRCPY(buf, line); | |
498 | 1602 if (lnum < wp->w_buffer->b_ml.ml_line_count) |
883 | 1603 spell_cat_line(buf + STRLEN(buf), |
1604 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN); | |
346 | 1605 |
1606 p = buf + skip; | |
1607 endp = buf + len; | |
1608 while (p < endp) | |
236 | 1609 { |
500 | 1610 /* When searching backward don't search after the cursor. Unless |
1611 * we wrapped around the end of the buffer. */ | |
300 | 1612 if (dir == BACKWARD |
498 | 1613 && lnum == wp->w_cursor.lnum |
500 | 1614 && !wrapped |
498 | 1615 && (colnr_T)(p - buf) >= wp->w_cursor.col) |
300 | 1616 break; |
249 | 1617 |
300 | 1618 /* start of word */ |
534 | 1619 attr = HLF_COUNT; |
625 | 1620 len = spell_check(wp, p, &attr, &capcol, FALSE); |
249 | 1621 |
534 | 1622 if (attr != HLF_COUNT) |
300 | 1623 { |
1624 /* We found a bad word. Check the attribute. */ | |
534 | 1625 if (allwords || attr == HLF_SPB) |
236 | 1626 { |
300 | 1627 /* When searching forward only accept a bad word after |
1628 * the cursor. */ | |
1629 if (dir == BACKWARD | |
500 | 1630 || lnum != wp->w_cursor.lnum |
498 | 1631 || (lnum == wp->w_cursor.lnum |
500 | 1632 && (wrapped |
1633 || (colnr_T)(curline ? p - buf + len | |
346 | 1634 : p - buf) |
500 | 1635 > wp->w_cursor.col))) |
236 | 1636 { |
5519 | 1637 #ifdef FEAT_SYN_HL |
300 | 1638 if (has_syntax) |
249 | 1639 { |
835 | 1640 col = (int)(p - buf); |
498 | 1641 (void)syn_get_id(wp, lnum, (colnr_T)col, |
1504 | 1642 FALSE, &can_spell, FALSE); |
857 | 1643 if (!can_spell) |
1644 attr = HLF_COUNT; | |
300 | 1645 } |
1646 else | |
737 | 1647 #endif |
300 | 1648 can_spell = TRUE; |
249 | 1649 |
300 | 1650 if (can_spell) |
1651 { | |
857 | 1652 found_one = TRUE; |
300 | 1653 found_pos.lnum = lnum; |
835 | 1654 found_pos.col = (int)(p - buf); |
249 | 1655 #ifdef FEAT_VIRTUALEDIT |
300 | 1656 found_pos.coladd = 0; |
249 | 1657 #endif |
300 | 1658 if (dir == FORWARD) |
1659 { | |
1660 /* No need to search further. */ | |
498 | 1661 wp->w_cursor = found_pos; |
346 | 1662 vim_free(buf); |
498 | 1663 if (attrp != NULL) |
1664 *attrp = attr; | |
495 | 1665 return len; |
249 | 1666 } |
483 | 1667 else if (curline) |
1668 /* Insert mode completion: put cursor after | |
1669 * the bad word. */ | |
1670 found_pos.col += len; | |
495 | 1671 found_len = len; |
249 | 1672 } |
236 | 1673 } |
857 | 1674 else |
1675 found_one = TRUE; | |
236 | 1676 } |
1677 } | |
1678 | |
300 | 1679 /* advance to character after the word */ |
1680 p += len; | |
385 | 1681 capcol -= len; |
236 | 1682 } |
1683 | |
483 | 1684 if (dir == BACKWARD && found_pos.lnum != 0) |
1685 { | |
500 | 1686 /* Use the last match in the line (before the cursor). */ |
498 | 1687 wp->w_cursor = found_pos; |
483 | 1688 vim_free(buf); |
495 | 1689 return found_len; |
483 | 1690 } |
1691 | |
323 | 1692 if (curline) |
346 | 1693 break; /* only check cursor line */ |
323 | 1694 |
10950
2297aae8e127
patch 8.0.0364: ]s does not move cursor with two spell errors in one line
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1695 /* If we are back at the starting line and searched it again there |
2297aae8e127
patch 8.0.0364: ]s does not move cursor with two spell errors in one line
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1696 * is no match, give up. */ |
2297aae8e127
patch 8.0.0364: ]s does not move cursor with two spell errors in one line
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1697 if (lnum == wp->w_cursor.lnum && wrapped) |
2297aae8e127
patch 8.0.0364: ]s does not move cursor with two spell errors in one line
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1698 break; |
2297aae8e127
patch 8.0.0364: ]s does not move cursor with two spell errors in one line
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1699 |
236 | 1700 /* Advance to next line. */ |
249 | 1701 if (dir == BACKWARD) |
1702 { | |
500 | 1703 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; | |
503 | 1713 if (!shortmess(SHM_SEARCH)) |
1714 give_warning((char_u *)_(top_bot_msg), TRUE); | |
500 | 1715 } |
385 | 1716 capcol = -1; |
249 | 1717 } |
1718 else | |
1719 { | |
500 | 1720 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; | |
503 | 1730 if (!shortmess(SHM_SEARCH)) |
1731 give_warning((char_u *)_(bot_top_msg), TRUE); | |
500 | 1732 } |
1733 | |
1734 /* If we are back at the starting line and there is no match then | |
1735 * give up. */ | |
10950
2297aae8e127
patch 8.0.0364: ]s does not move cursor with two spell errors in one line
Christian Brabandt <cb@256bit.org>
parents:
10359
diff
changeset
|
1736 if (lnum == wp->w_cursor.lnum && !found_one) |
346 | 1737 break; |
1738 | |
1739 /* Skip the characters at the start of the next line that were | |
1740 * included in a match crossing line boundaries. */ | |
534 | 1741 if (attr == HLF_COUNT) |
835 | 1742 skip = (int)(p - endp); |
346 | 1743 else |
1744 skip = 0; | |
385 | 1745 |
835 | 1746 /* Capcol skips over the inserted space. */ |
385 | 1747 --capcol; |
1748 | |
1749 /* But after empty line check first word in next line */ | |
1750 if (*skipwhite(line) == NUL) | |
1751 capcol = 0; | |
249 | 1752 } |
236 | 1753 |
1754 line_breakcheck(); | |
1755 } | |
1756 | |
346 | 1757 vim_free(buf); |
495 | 1758 return 0; |
346 | 1759 } |
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. | |
1577 | 1764 * 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. | |
346 | 1766 */ |
1767 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1768 spell_cat_line(char_u *buf, char_u *line, int maxlen) |
346 | 1769 { |
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 { | |
1577 | 1779 /* 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 } | |
346 | 1787 } |
236 | 1788 } |
1789 | |
626 | 1790 /* |
1791 * Structure used for the cookie argument of do_in_runtimepath(). | |
1792 */ | |
501 | 1793 typedef 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 | |
236 | 1800 /* |
307 | 1801 * Load word list(s) for "lang" from Vim spell file(s). |
310 | 1802 * "lang" must be the language without the region: e.g., "en". |
236 | 1803 */ |
307 | 1804 static void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1805 spell_load_lang(char_u *lang) |
236 | 1806 { |
310 | 1807 char_u fname_enc[85]; |
236 | 1808 int r; |
501 | 1809 spelload_T sl; |
649 | 1810 int round; |
307 | 1811 |
310 | 1812 /* Copy the language name to pass it to spell_load_cb() as a cookie. |
307 | 1813 * It's truncated when an error is detected. */ |
501 | 1814 STRCPY(sl.sl_lang, lang); |
1815 sl.sl_slang = NULL; | |
1816 sl.sl_nobreak = FALSE; | |
307 | 1817 |
649 | 1818 /* 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) | |
1821 { | |
1822 /* | |
1823 * Find the first spell file for "lang" in 'runtimepath' and load it. | |
1824 */ | |
1825 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, | |
2660 | 1826 #ifdef VMS |
1827 "spell/%s_%s.spl", | |
1828 #else | |
1829 "spell/%s.%s.spl", | |
1830 #endif | |
1831 lang, spell_enc()); | |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1832 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); |
649 | 1833 |
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, | |
2660 | 1838 #ifdef VMS |
1839 "spell/%s_ascii.spl", | |
1840 #else | |
1841 "spell/%s.ascii.spl", | |
1842 #endif | |
1843 lang); | |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1844 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); |
649 | 1845 |
1846 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; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13308
diff
changeset
|
1851 } |
714 | 1852 break; |
307 | 1853 } |
1854 | |
1855 if (r == FAIL) | |
649 | 1856 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1857 smsg( |
2660 | 1858 #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 | |
483 | 1863 lang, spell_enc(), lang); |
649 | 1864 } |
501 | 1865 else if (sl.sl_slang != NULL) |
1866 { | |
626 | 1867 /* At least one file was loaded, now load ALL the additions. */ |
310 | 1868 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl"); |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1869 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl); |
310 | 1870 } |
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 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1877 char_u * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1878 spell_enc(void) |
310 | 1879 { |
1880 | |
1881 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) | |
1882 return p_enc; | |
1883 return (char_u *)"latin1"; | |
236 | 1884 } |
1885 | |
1886 /* | |
385 | 1887 * Get the name of the .spl file for the internal wordlist into |
1888 * "fname[MAXPATHL]". | |
1889 */ | |
1890 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1891 int_wordlist_spl(char_u *fname) |
385 | 1892 { |
2660 | 1893 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL, |
385 | 1894 int_wordlist, spell_enc()); |
1895 } | |
1896 | |
1897 /* | |
625 | 1898 * Allocate a new slang_T for language "lang". "lang" can be NULL. |
236 | 1899 * Caller must fill "sl_next". |
1900 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1901 slang_T * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1902 slang_alloc(char_u *lang) |
236 | 1903 { |
1904 slang_T *lp; | |
1905 | |
300 | 1906 lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
236 | 1907 if (lp != NULL) |
1908 { | |
625 | 1909 if (lang != NULL) |
1910 lp->sl_name = vim_strsave(lang); | |
323 | 1911 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
625 | 1912 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10); |
483 | 1913 lp->sl_compmax = MAXWLEN; |
1914 lp->sl_compsylmax = MAXWLEN; | |
625 | 1915 hash_init(&lp->sl_wordcount); |
1916 } | |
1917 | |
236 | 1918 return lp; |
1919 } | |
1920 | |
1921 /* | |
1922 * Free the contents of an slang_T and the structure itself. | |
1923 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1924 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1925 slang_free(slang_T *lp) |
236 | 1926 { |
1927 vim_free(lp->sl_name); | |
310 | 1928 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 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1936 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1937 slang_clear(slang_T *lp) |
310 | 1938 { |
339 | 1939 garray_T *gap; |
1940 fromto_T *ftp; | |
344 | 1941 salitem_T *smp; |
339 | 1942 int i; |
625 | 1943 int round; |
323 | 1944 |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1945 VIM_CLEAR(lp->sl_fbyts); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1946 VIM_CLEAR(lp->sl_kbyts); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1947 VIM_CLEAR(lp->sl_pbyts); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1948 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1949 VIM_CLEAR(lp->sl_fidxs); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1950 VIM_CLEAR(lp->sl_kidxs); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1951 VIM_CLEAR(lp->sl_pidxs); |
323 | 1952 |
625 | 1953 for (round = 1; round <= 2; ++round) |
1954 { | |
1955 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); | |
1963 } | |
344 | 1964 |
1965 gap = &lp->sl_sal; | |
375 | 1966 if (lp->sl_sofo) |
376 | 1967 { |
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 } | |
375 | 1974 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); | |
1982 vim_free(smp->sm_lead_w); | |
1983 vim_free(smp->sm_oneof_w); | |
1984 vim_free(smp->sm_to_w); | |
1985 } | |
344 | 1986 ga_clear(gap); |
323 | 1987 |
339 | 1988 for (i = 0; i < lp->sl_prefixcnt; ++i) |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1989 vim_regfree(lp->sl_prefprog[i]); |
376 | 1990 lp->sl_prefixcnt = 0; |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1991 VIM_CLEAR(lp->sl_prefprog); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1992 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1993 VIM_CLEAR(lp->sl_info); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1994 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1995 VIM_CLEAR(lp->sl_midword); |
339 | 1996 |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1997 vim_regfree(lp->sl_compprog); |
483 | 1998 lp->sl_compprog = NULL; |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1999 VIM_CLEAR(lp->sl_comprules); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2000 VIM_CLEAR(lp->sl_compstartflags); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2001 VIM_CLEAR(lp->sl_compallflags); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2002 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2003 VIM_CLEAR(lp->sl_syllable); |
483 | 2004 ga_clear(&lp->sl_syl_items); |
481 | 2005 |
809 | 2006 ga_clear_strings(&lp->sl_comppat); |
2007 | |
625 | 2008 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF); |
2009 hash_init(&lp->sl_wordcount); | |
2010 | |
2011 hash_clear_all(&lp->sl_map_hash, 0); | |
2012 | |
2013 /* Clear info from .sug file. */ | |
2014 slang_clear_sug(lp); | |
483 | 2015 |
2016 lp->sl_compmax = MAXWLEN; | |
501 | 2017 lp->sl_compminlen = 0; |
483 | 2018 lp->sl_compsylmax = MAXWLEN; |
2019 lp->sl_regions[0] = NUL; | |
236 | 2020 } |
2021 | |
2022 /* | |
625 | 2023 * Clear the info from the .sug file in "lp". |
2024 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2025 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2026 slang_clear_sug(slang_T *lp) |
625 | 2027 { |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2028 VIM_CLEAR(lp->sl_sbyts); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2029 VIM_CLEAR(lp->sl_sidxs); |
625 | 2030 close_spellbuf(lp->sl_sugbuf); |
2031 lp->sl_sugbuf = NULL; | |
2032 lp->sl_sugloaded = FALSE; | |
2033 lp->sl_sugtime = 0; | |
2034 } | |
2035 | |
2036 /* | |
307 | 2037 * Load one spell file and store the info into a slang_T. |
236 | 2038 * Invoked through do_in_runtimepath(). |
2039 */ | |
2040 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2041 spell_load_cb(char_u *fname, void *cookie) |
501 | 2042 { |
2043 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 } | |
310 | 2058 } |
2059 | |
625 | 2060 |
2061 /* | |
2062 * Add a word to the hashtable of common words. | |
2063 * If it's already there then the counter is increased. | |
2064 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2065 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2066 count_common_word( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2067 slang_T *lp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2068 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2069 int len, /* word length, -1 for upto NUL */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2070 int count) /* 1 to count once, 10 to init */ |
625 | 2071 { |
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 { | |
835 | 2090 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p))); |
625 | 2091 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2109 score_wordcount_adj( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2110 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2111 int score, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2112 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2113 int split) /* word was split, less bonus */ |
625 | 2114 { |
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 | |
483 | 2141 |
495 | 2142 /* |
498 | 2143 * Return TRUE if byte "n" appears in "str". |
495 | 2144 * Like strchr() but independent of locale. |
2145 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2146 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2147 byte_in_str(char_u *str, int n) |
495 | 2148 { |
2149 char_u *p; | |
2150 | |
2151 for (p = str; *p != NUL; ++p) | |
498 | 2152 if (*p == n) |
495 | 2153 return TRUE; |
2154 return FALSE; | |
2155 } | |
2156 | |
483 | 2157 #define SY_MAXLEN 30 |
2158 typedef 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 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2168 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2169 init_syl_tab(slang_T *slang) |
483 | 2170 { |
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; | |
495 | 2181 if (*p == NUL) /* trailing slash */ |
483 | 2182 break; |
2183 s = p; | |
2184 p = vim_strchr(p, '/'); | |
2185 if (p == NULL) | |
835 | 2186 l = (int)STRLEN(s); |
483 | 2187 else |
835 | 2188 l = (int)(p - s); |
483 | 2189 if (l >= SY_MAXLEN) |
2190 return SP_FORMERROR; | |
2191 if (ga_grow(&slang->sl_syl_items, 1) == FAIL) | |
495 | 2192 return SP_OTHERERROR; |
483 | 2193 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2207 count_syllables(slang_T *slang, char_u *word) |
483 | 2208 { |
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? */ | |
2247 c = mb_ptr2char(p); | |
2248 len = (*mb_ptr2len)(p); | |
2249 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; | |
376 | 2259 } |
2260 | |
381 | 2261 /* |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2262 * Parse 'spelllang' and set w_s->b_langp accordingly. |
351 | 2263 * Returns NULL if it's OK, an error message otherwise. |
236 | 2264 */ |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2265 char * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2266 did_set_spelllang(win_T *wp) |
236 | 2267 { |
2268 garray_T ga; | |
351 | 2269 char_u *splp; |
236 | 2270 char_u *region; |
416 | 2271 char_u region_cp[3]; |
355 | 2272 int filename; |
236 | 2273 int region_mask; |
503 | 2274 slang_T *slang; |
236 | 2275 int c; |
351 | 2276 char_u lang[MAXWLEN + 1]; |
323 | 2277 char_u spf_name[MAXPATHL]; |
351 | 2278 int len; |
2279 char_u *p; | |
381 | 2280 int round; |
385 | 2281 char_u *spf; |
389 | 2282 char_u *use_region = NULL; |
2283 int dont_use_region = FALSE; | |
501 | 2284 int nobreak = FALSE; |
503 | 2285 int i, j; |
2286 langp_T *lp, *lp2; | |
1185 | 2287 static int recursive = FALSE; |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2288 char *ret_msg = NULL; |
1185 | 2289 char_u *spl_copy; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2290 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2291 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2292 set_bufref(&bufref, wp->w_buffer); |
1185 | 2293 |
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; | |
236 | 2300 |
2301 ga_init2(&ga, sizeof(langp_T), 2); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2302 clear_midword(wp); |
236 | 2303 |
4352 | 2304 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change |
1185 | 2305 * it under our fingers. */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2306 spl_copy = vim_strsave(wp->w_s->b_p_spl); |
1185 | 2307 if (spl_copy == NULL) |
2308 goto theend; | |
2309 | |
5477 | 2310 wp->w_s->b_cjk = 0; |
2311 | |
2312 /* Loop over comma separated language names. */ | |
1185 | 2313 for (splp = spl_copy; *splp != NUL; ) |
351 | 2314 { |
2315 /* Get one language name. */ | |
2316 copy_option_part(&splp, lang, MAXWLEN, ","); | |
240 | 2317 region = NULL; |
835 | 2318 len = (int)STRLEN(lang); |
355 | 2319 |
5477 | 2320 if (STRCMP(lang, "cjk") == 0) |
2321 { | |
2322 wp->w_s->b_cjk = 1; | |
2323 continue; | |
2324 } | |
2325 | |
355 | 2326 /* 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) | |
236 | 2330 { |
355 | 2331 filename = TRUE; |
2332 | |
416 | 2333 /* 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); | |
2340 region = region_cp; | |
2341 } | |
2342 else | |
2343 dont_use_region = TRUE; | |
2344 | |
355 | 2345 /* Check if we loaded this language before. */ |
503 | 2346 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2347 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME) | |
355 | 2348 break; |
236 | 2349 } |
355 | 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; | |
389 | 2358 } |
2359 else | |
2360 dont_use_region = TRUE; | |
355 | 2361 |
2362 /* Check if we loaded this language before. */ | |
503 | 2363 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2364 if (STRICMP(lang, slang->sl_name) == 0) | |
355 | 2365 break; |
2366 } | |
236 | 2367 |
416 | 2368 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 | |
351 | 2377 /* If not found try loading the language now. */ |
503 | 2378 if (slang == NULL) |
355 | 2379 { |
2380 if (filename) | |
2381 (void)spell_load_file(lang, lang, NULL, FALSE); | |
2382 else | |
1185 | 2383 { |
355 | 2384 spell_load_lang(lang); |
1185 | 2385 /* SpellFileMissing autocommands may do anything, including |
2386 * destroying the buffer we are using... */ | |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2387 if (!bufref_valid(&bufref)) |
1185 | 2388 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2389 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer"); |
1185 | 2390 goto theend; |
2391 } | |
2392 } | |
355 | 2393 } |
236 | 2394 |
307 | 2395 /* |
351 | 2396 * Loop over the languages, there can be several files for "lang". |
307 | 2397 */ |
503 | 2398 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) | |
236 | 2401 { |
316 | 2402 region_mask = REGION_ALL; |
355 | 2403 if (!filename && region != NULL) |
236 | 2404 { |
307 | 2405 /* find region in sl_regions */ |
503 | 2406 c = find_region(slang->sl_regions, region); |
307 | 2407 if (c == REGION_ALL) |
2408 { | |
503 | 2409 if (slang->sl_add) |
389 | 2410 { |
503 | 2411 if (*slang->sl_regions != NUL) |
389 | 2412 /* 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. */ | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2418 smsg(_("Warning: region %s not supported"), |
351 | 2419 region); |
307 | 2420 } |
2421 else | |
2422 region_mask = 1 << c; | |
236 | 2423 } |
307 | 2424 |
389 | 2425 if (region_mask != 0) |
307 | 2426 { |
389 | 2427 if (ga_grow(&ga, 1) == FAIL) |
2428 { | |
2429 ga_clear(&ga); | |
1185 | 2430 ret_msg = e_outofmem; |
2431 goto theend; | |
389 | 2432 } |
503 | 2433 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
389 | 2434 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
2435 ++ga.ga_len; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2436 use_midword(slang, wp); |
503 | 2437 if (slang->sl_nobreak) |
501 | 2438 nobreak = TRUE; |
307 | 2439 } |
385 | 2440 } |
2441 } | |
2442 | |
2443 /* 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. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2447 spf = curwin->w_s->b_p_spf; |
385 | 2448 for (round = 0; round == 0 || *spf != NUL; ++round) |
2449 { | |
2450 if (round == 0) | |
2451 { | |
2452 /* Internal wordlist, if there is one. */ | |
2453 if (int_wordlist == NULL) | |
381 | 2454 continue; |
385 | 2455 int_wordlist_spl(spf_name); |
381 | 2456 } |
2457 else | |
2458 { | |
385 | 2459 /* 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) | |
500 | 2465 { |
2466 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname; | |
2467 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME) | |
385 | 2468 break; |
500 | 2469 } |
385 | 2470 if (c < ga.ga_len) |
381 | 2471 continue; |
2472 } | |
2473 | |
351 | 2474 /* Check if it was loaded already. */ |
503 | 2475 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2476 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME) | |
323 | 2477 break; |
503 | 2478 if (slang == NULL) |
323 | 2479 { |
351 | 2480 /* Not loaded, try loading it now. The language name includes the |
385 | 2481 * 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 | |
2486 { | |
2487 vim_strncpy(lang, gettail(spf_name), MAXWLEN); | |
381 | 2488 p = vim_strchr(lang, '.'); |
2489 if (p != NULL) | |
2490 *p = NUL; /* truncate at ".encoding.add" */ | |
2491 } | |
503 | 2492 slang = spell_load_file(spf_name, lang, NULL, TRUE); |
501 | 2493 |
2494 /* If one of the languages has NOBREAK we assume the addition | |
2495 * files also have this. */ | |
503 | 2496 if (slang != NULL && nobreak) |
2497 slang->sl_nobreak = TRUE; | |
2498 } | |
2499 if (slang != NULL && ga_grow(&ga, 1) == OK) | |
323 | 2500 { |
389 | 2501 region_mask = REGION_ALL; |
2502 if (use_region != NULL && !dont_use_region) | |
2503 { | |
2504 /* find region in sl_regions */ | |
503 | 2505 c = find_region(slang->sl_regions, use_region); |
389 | 2506 if (c != REGION_ALL) |
2507 region_mask = 1 << c; | |
503 | 2508 else if (*slang->sl_regions != NUL) |
389 | 2509 /* This spell file is for other regions. */ |
2510 region_mask = 0; | |
2511 } | |
2512 | |
2513 if (region_mask != 0) | |
2514 { | |
503 | 2515 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; | |
389 | 2518 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
2519 ++ga.ga_len; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2520 use_midword(slang, wp); |
389 | 2521 } |
323 | 2522 } |
2523 } | |
2524 | |
236 | 2525 /* Everything is fine, store the new b_langp value. */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2526 ga_clear(&wp->w_s->b_langp); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2527 wp->w_s->b_langp = ga; |
236 | 2528 |
503 | 2529 /* 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 | |
625 | 2559 /* find first similar language that has REP items */ |
503 | 2560 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 | |
1185 | 2573 theend: |
2574 vim_free(spl_copy); | |
2575 recursive = FALSE; | |
5891 | 2576 redraw_win_later(wp, NOT_VALID); |
1185 | 2577 return ret_msg; |
236 | 2578 } |
2579 | |
2580 /* | |
376 | 2581 * Clear the midword characters for buffer "buf". |
2582 */ | |
2583 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2584 clear_midword(win_T *wp) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2585 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2586 vim_memset(wp->w_s->b_spell_ismw, 0, 256); |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2587 VIM_CLEAR(wp->w_s->b_spell_ismw_mb); |
376 | 2588 } |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2595 use_midword(slang_T *lp, win_T *wp) |
376 | 2596 { |
2597 char_u *p; | |
2598 | |
389 | 2599 if (lp->sl_midword == NULL) /* there aren't any */ |
2600 return; | |
2601 | |
376 | 2602 for (p = lp->sl_midword; *p != NUL; ) |
2603 if (has_mbyte) | |
2604 { | |
2605 int c, l, n; | |
2606 char_u *bp; | |
2607 | |
2608 c = mb_ptr2char(p); | |
474 | 2609 l = (*mb_ptr2len)(p); |
2610 if (c < 256 && l <= 2) | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2611 wp->w_s->b_spell_ismw[c] = TRUE; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2612 else if (wp->w_s->b_spell_ismw_mb == NULL) |
376 | 2613 /* First multi-byte char in "b_spell_ismw_mb". */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2614 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l); |
376 | 2615 else |
2616 { | |
2617 /* Append multi-byte chars to "b_spell_ismw_mb". */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2618 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2619 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l); |
376 | 2620 if (bp != NULL) |
2621 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2622 vim_free(wp->w_s->b_spell_ismw_mb); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2623 wp->w_s->b_spell_ismw_mb = bp; |
376 | 2624 vim_strncpy(bp + n, p, l); |
2625 } | |
2626 } | |
2627 p += l; | |
2628 } | |
2629 else | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2630 wp->w_s->b_spell_ismw[*p++] = TRUE; |
376 | 2631 } |
2632 | |
2633 /* | |
236 | 2634 * Find the region "region[2]" in "rp" (points to "sl_regions"). |
15034
6e4e0d43b20b
patch 8.1.0528: various typos in comments
Bram Moolenaar <Bram@vim.org>
parents:
14862
diff
changeset
|
2635 * Each region is simply stored as the two characters of its name. |
381 | 2636 * Returns the index if found (first is 0), REGION_ALL if not found. |
236 | 2637 */ |
2638 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2639 find_region(char_u *rp, char_u *region) |
236 | 2640 { |
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 /* | |
323 | 2654 * Return case type of word: |
236 | 2655 * w word 0 |
300 | 2656 * Word WF_ONECAP |
2657 * W WORD WF_ALLCAP | |
2658 * WoRd wOrd WF_KEEPCAP | |
236 | 2659 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2660 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2661 captype( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2662 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2663 char_u *end) /* When NULL use up to NUL byte. */ |
236 | 2664 { |
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 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2672 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p)) |
323 | 2673 if (end == NULL ? *p == NUL : p >= end) |
236 | 2674 return 0; /* only non-word characters, illegal word */ |
310 | 2675 if (has_mbyte) |
2676 c = mb_ptr2char_adv(&p); | |
2677 else | |
2678 c = *p++; | |
324 | 2679 firstcap = allcap = SPELL_ISUPPER(c); |
236 | 2680 |
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 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2685 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p)) |
5477 | 2686 if (spell_iswordp_nmw(p, curwin)) |
236 | 2687 { |
455 | 2688 c = PTR2CHAR(p); |
324 | 2689 if (!SPELL_ISUPPER(c)) |
236 | 2690 { |
2691 /* UUl -> KEEPCAP */ | |
2692 if (past_second && allcap) | |
300 | 2693 return WF_KEEPCAP; |
236 | 2694 allcap = FALSE; |
2695 } | |
2696 else if (!allcap) | |
2697 /* UlU -> KEEPCAP */ | |
300 | 2698 return WF_KEEPCAP; |
236 | 2699 past_second = TRUE; |
2700 } | |
2701 | |
2702 if (allcap) | |
300 | 2703 return WF_ALLCAP; |
236 | 2704 if (firstcap) |
300 | 2705 return WF_ONECAP; |
236 | 2706 return 0; |
2707 } | |
2708 | |
474 | 2709 /* |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2715 badword_captype(char_u *word, char_u *end) |
474 | 2716 { |
2717 int flags = captype(word, end); | |
475 | 2718 int c; |
474 | 2719 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; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2728 for (p = word; p < end; MB_PTR_ADV(p)) |
474 | 2729 { |
475 | 2730 c = PTR2CHAR(p); |
2731 if (SPELL_ISUPPER(c)) | |
474 | 2732 { |
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; | |
633 | 2749 |
2750 if (u >= 2 && l >= 2) /* maCARONI maCAroni */ | |
2751 flags |= WF_MIXCAP; | |
474 | 2752 } |
2753 return flags; | |
2754 } | |
2755 | |
5519 | 2756 /* |
2757 * Delete the internal wordlist and its .spl file. | |
2758 */ | |
2759 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2760 spell_delete_wordlist(void) |
5519 | 2761 { |
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); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2769 VIM_CLEAR(int_wordlist); |
5519 | 2770 } |
2771 } | |
2772 | |
355 | 2773 /* |
2774 * Free all languages. | |
2775 */ | |
2776 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2777 spell_free_all(void) |
355 | 2778 { |
503 | 2779 slang_T *slang; |
355 | 2780 buf_T *buf; |
2781 | |
2580 | 2782 /* Go through all buffers and handle 'spelllang'. <VN> */ |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9583
diff
changeset
|
2783 FOR_ALL_BUFFERS(buf) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2784 ga_clear(&buf->b_s.b_langp); |
355 | 2785 |
2786 while (first_lang != NULL) | |
2787 { | |
503 | 2788 slang = first_lang; |
2789 first_lang = slang->sl_next; | |
2790 slang_free(slang); | |
355 | 2791 } |
366 | 2792 |
5519 | 2793 spell_delete_wordlist(); |
381 | 2794 |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2795 VIM_CLEAR(repl_to); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2796 VIM_CLEAR(repl_from); |
355 | 2797 } |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
2798 |
236 | 2799 /* |
2800 * Clear all spelling tables and reload them. | |
307 | 2801 * Used after 'encoding' is set and when ":mkspell" was used. |
236 | 2802 */ |
2803 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2804 spell_reload(void) |
236 | 2805 { |
316 | 2806 win_T *wp; |
236 | 2807 |
358 | 2808 /* Initialize the table for spell_iswordp(). */ |
236 | 2809 init_spell_chartab(); |
2810 | |
2811 /* Unload all allocated memory. */ | |
355 | 2812 spell_free_all(); |
236 | 2813 |
2814 /* Go through all buffers and handle 'spelllang'. */ | |
9649
fd9727ae3c49
commit https://github.com/vim/vim/commit/2932359000b2f918d5fade79ea4d124d5943cd07
Christian Brabandt <cb@256bit.org>
parents:
9583
diff
changeset
|
2815 FOR_ALL_WINDOWS(wp) |
236 | 2816 { |
316 | 2817 /* Only load the wordlists when 'spelllang' is set and there is a |
2818 * window for this buffer in which 'spell' is set. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2819 if (*wp->w_s->b_p_spl != NUL) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2820 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2821 if (wp->w_p_spell) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2822 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2823 (void)did_set_spelllang(wp); |
316 | 2824 break; |
2825 } | |
2826 } | |
236 | 2827 } |
2828 } | |
2829 | |
310 | 2830 /* |
625 | 2831 * Opposite of offset2bytes(). |
2832 * "pp" points to the bytes and is advanced over it. | |
2833 * Returns the offset. | |
2834 */ | |
2835 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2836 bytes2offset(char_u **pp) |
625 | 2837 { |
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 | |
2870 | |
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 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2879 buf_T * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2880 open_spellbuf(void) |
625 | 2881 { |
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 */ | |
5204
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2889 #ifdef FEAT_CRYPT |
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2890 buf->b_p_key = empty_option; |
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2891 #endif |
625 | 2892 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 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2901 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2902 close_spellbuf(buf_T *buf) |
625 | 2903 { |
2904 if (buf != NULL) | |
2905 { | |
2906 ml_close(buf, TRUE); | |
2907 vim_free(buf); | |
2908 } | |
2909 } | |
2910 | |
307 | 2911 /* |
2912 * Init the chartab used for spelling for ASCII. | |
2913 * EBCDIC is not supported! | |
2914 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2915 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2916 clear_spell_chartab(spelltab_T *sp) |
307 | 2917 { |
324 | 2918 int i; |
307 | 2919 |
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) | |
324 | 2924 { |
307 | 2925 sp->st_fold[i] = i; |
324 | 2926 sp->st_upper[i] = i; |
2927 } | |
307 | 2928 |
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) | |
324 | 2940 { |
307 | 2941 sp->st_isw[i] = TRUE; |
324 | 2942 sp->st_upper[i] = i - 0x20; |
2943 } | |
307 | 2944 } |
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 | |
390 | 2951 * locale. For utf-8 we don't use isalpha() but our own functions. |
307 | 2952 */ |
2953 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2954 init_spell_chartab(void) |
307 | 2955 { |
2956 int i; | |
2957 | |
2958 did_set_spelltab = FALSE; | |
2959 clear_spell_chartab(&spelltab); | |
2960 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 } | |
324 | 2967 else if (enc_utf8) |
2968 { | |
2969 for (i = 128; i < 256; ++i) | |
2970 { | |
2140
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2971 int f = utf_fold(i); |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2972 int u = utf_toupper(i); |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2973 |
324 | 2974 spelltab.st_isu[i] = utf_isupper(i); |
2975 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i); | |
2140
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2976 /* The folded/upper-cased value is different between latin1 and |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2977 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1 |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2978 * value for utf-8 to avoid this. */ |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2979 spelltab.st_fold[i] = (f < 256) ? f : i; |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2980 spelltab.st_upper[i] = (u < 256) ? u : i; |
324 | 2981 } |
2982 } | |
307 | 2983 else |
2984 { | |
324 | 2985 /* Rough guess: use locale-dependent library functions. */ |
307 | 2986 for (i = 128; i < 256; ++i) |
2987 { | |
2988 if (MB_ISUPPER(i)) | |
2989 { | |
324 | 2990 spelltab.st_isw[i] = TRUE; |
307 | 2991 spelltab.st_isu[i] = TRUE; |
2992 spelltab.st_fold[i] = MB_TOLOWER(i); | |
2993 } | |
324 | 2994 else if (MB_ISLOWER(i)) |
2995 { | |
2996 spelltab.st_isw[i] = TRUE; | |
2997 spelltab.st_upper[i] = MB_TOUPPER(i); | |
2998 } | |
307 | 2999 } |
3000 } | |
3001 } | |
3002 | |
3003 | |
3004 /* | |
358 | 3005 * Return TRUE if "p" points to a word character. |
366 | 3006 * As a special case we see "midword" characters as word character when it is |
358 | 3007 * followed by a word character. This finds they'there but not 'they there'. |
366 | 3008 * Thus this only works properly when past the first character of the word. |
358 | 3009 */ |
3010 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3011 spell_iswordp( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3012 char_u *p, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3013 win_T *wp) /* buffer used */ |
358 | 3014 { |
3015 char_u *s; | |
366 | 3016 int l; |
3017 int c; | |
3018 | |
3019 if (has_mbyte) | |
3020 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3021 l = MB_PTR2LEN(p); |
358 | 3022 s = p; |
366 | 3023 if (l == 1) |
3024 { | |
3025 /* be quick for ASCII */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3026 if (wp->w_s->b_spell_ismw[*p]) |
366 | 3027 s = p + 1; /* skip a mid-word character */ |
3028 } | |
3029 else | |
3030 { | |
3031 c = mb_ptr2char(p); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3032 if (c < 256 ? wp->w_s->b_spell_ismw[c] |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3033 : (wp->w_s->b_spell_ismw_mb != NULL |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3034 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL)) |
366 | 3035 s = p + l; |
3036 } | |
3037 | |
390 | 3038 c = mb_ptr2char(s); |
3039 if (c > 255) | |
5477 | 3040 return spell_mb_isword_class(mb_get_class(s), wp); |
390 | 3041 return spelltab.st_isw[c]; |
366 | 3042 } |
3043 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3044 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]]; |
376 | 3045 } |
3046 | |
3047 /* | |
3048 * Return TRUE if "p" points to a word character. | |
3049 * Unlike spell_iswordp() this doesn't check for "midword" characters. | |
3050 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3051 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3052 spell_iswordp_nmw(char_u *p, win_T *wp) |
376 | 3053 { |
390 | 3054 int c; |
3055 | |
3056 if (has_mbyte) | |
3057 { | |
3058 c = mb_ptr2char(p); | |
3059 if (c > 255) | |
5477 | 3060 return spell_mb_isword_class(mb_get_class(p), wp); |
390 | 3061 return spelltab.st_isw[c]; |
3062 } | |
376 | 3063 return spelltab.st_isw[*p]; |
358 | 3064 } |
3065 | |
372 | 3066 /* |
1580 | 3067 * 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. | |
5477 | 3070 * See also dbcs_class() and utf_class() in mbyte.c. |
3071 */ | |
3072 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3073 spell_mb_isword_class(int cl, win_T *wp) |
5477 | 3074 { |
3075 if (wp->w_s->b_cjk) | |
3076 /* East Asian characters are not considered word characters. */ | |
3077 return cl == 2 || cl == 0x2800; | |
1580 | 3078 return cl >= 2 && cl != 0x2070 && cl != 0x2080; |
3079 } | |
3080 | |
3081 /* | |
372 | 3082 * Return TRUE if "p" points to a word character. |
3083 * Wide version of spell_iswordp(). | |
3084 */ | |
3085 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3086 spell_iswordp_w(int *p, win_T *wp) |
372 | 3087 { |
3088 int *s; | |
3089 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3090 if (*p < 256 ? wp->w_s->b_spell_ismw[*p] |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3091 : (wp->w_s->b_spell_ismw_mb != NULL |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3092 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL)) |
372 | 3093 s = p + 1; |
3094 else | |
3095 s = p; | |
3096 | |
390 | 3097 if (*s > 255) |
372 | 3098 { |
3099 if (enc_utf8) | |
5477 | 3100 return spell_mb_isword_class(utf_class(*s), wp); |
372 | 3101 if (enc_dbcs) |
5477 | 3102 return spell_mb_isword_class( |
3103 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp); | |
372 | 3104 return 0; |
3105 } | |
3106 return spelltab.st_isw[*s]; | |
3107 } | |
3108 | |
358 | 3109 /* |
324 | 3110 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
3111 * Uses the character definitions from the .spl file. | |
307 | 3112 * When using a multi-byte 'encoding' the length may change! |
3113 * Returns FAIL when something wrong. | |
3114 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3115 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3116 spell_casefold( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3117 char_u *str, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3118 int len, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3119 char_u *buf, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3120 int buflen) |
307 | 3121 { |
3122 int i; | |
3123 | |
3124 if (len >= buflen) | |
3125 { | |
3126 buf[0] = NUL; | |
3127 return FAIL; /* result will not fit */ | |
3128 } | |
3129 | |
3130 if (has_mbyte) | |
3131 { | |
324 | 3132 int outi = 0; |
3133 char_u *p; | |
307 | 3134 int c; |
3135 | |
3136 /* Fold one character at a time. */ | |
324 | 3137 for (p = str; p < str + len; ) |
307 | 3138 { |
3139 if (outi + MB_MAXBYTES > buflen) | |
3140 { | |
3141 buf[outi] = NUL; | |
3142 return FAIL; | |
3143 } | |
474 | 3144 c = mb_cptr2char_adv(&p); |
324 | 3145 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
307 | 3146 } |
3147 buf[outi] = NUL; | |
3148 } | |
3149 else | |
3150 { | |
3151 /* Be quick for non-multibyte encodings. */ | |
3152 for (i = 0; i < len; ++i) | |
324 | 3153 buf[i] = spelltab.st_fold[str[i]]; |
307 | 3154 buf[i] = NUL; |
3155 } | |
3156 | |
3157 return OK; | |
3158 } | |
3159 | |
625 | 3160 /* values for sps_flags */ |
372 | 3161 #define SPS_BEST 1 |
3162 #define SPS_FAST 2 | |
3163 #define SPS_DOUBLE 4 | |
3164 | |
625 | 3165 static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */ |
3166 static int sps_limit = 9999; /* max nr of suggestions given */ | |
372 | 3167 |
3168 /* | |
3169 * Check the 'spellsuggest' option. Return FAIL if it's wrong. | |
483 | 3170 * Sets "sps_flags" and "sps_limit". |
372 | 3171 */ |
3172 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3173 spell_check_sps(void) |
372 | 3174 { |
3175 char_u *p; | |
483 | 3176 char_u *s; |
372 | 3177 char_u buf[MAXPATHL]; |
3178 int f; | |
3179 | |
3180 sps_flags = 0; | |
483 | 3181 sps_limit = 9999; |
372 | 3182 |
3183 for (p = p_sps; *p != NUL; ) | |
3184 { | |
3185 copy_option_part(&p, buf, MAXPATHL, ","); | |
3186 | |
3187 f = 0; | |
483 | 3188 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) | |
372 | 3196 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; | |
483 | 3208 sps_limit = 9999; |
372 | 3209 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 | |
323 | 3221 /* |
5374 | 3222 * "z=": Find badly spelled word under or after the cursor. |
323 | 3223 * Give suggestions for the properly spelled word. |
638 | 3224 * In Visual mode use the highlighted word as the bad word. |
485 | 3225 * When "count" is non-zero use that suggestion. |
323 | 3226 */ |
3227 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3228 spell_suggest(int count) |
323 | 3229 { |
3230 char_u *line; | |
3231 pos_T prev_cursor = curwin->w_cursor; | |
3232 char_u wcopy[MAXWLEN + 2]; | |
3233 char_u *p; | |
3234 int i; | |
3235 int c; | |
3236 suginfo_T sug; | |
3237 suggest_T *stp; | |
372 | 3238 int mouse_used; |
388 | 3239 int need_cap; |
483 | 3240 int limit; |
485 | 3241 int selected = count; |
638 | 3242 int badlen = 0; |
1938 | 3243 int msg_scroll_save = msg_scroll; |
638 | 3244 |
3245 if (no_spell_checking(curwin)) | |
3246 return; | |
3247 | |
3248 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 { | |
6949 | 3254 vim_beep(BO_SPELL); |
638 | 3255 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 } | |
5735 | 3265 /* Find the start of the badly spelled word. */ |
3266 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0 | |
346 | 3267 || curwin->w_cursor.col > prev_cursor.col) |
3268 { | |
3269 /* 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. */ | |
5477 | 3275 while (p > line && spell_iswordp_nmw(p, curwin)) |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3276 MB_PTR_BACK(line, p); |
346 | 3277 /* Forward to start of word. */ |
5477 | 3278 while (*p != NUL && !spell_iswordp_nmw(p, curwin)) |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3279 MB_PTR_ADV(p); |
346 | 3280 |
5477 | 3281 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */ |
346 | 3282 { |
3283 beep_flush(); | |
3284 return; | |
3285 } | |
835 | 3286 curwin->w_cursor.col = (colnr_T)(p - line); |
323 | 3287 } |
3288 | |
344 | 3289 /* Get the word and its length. */ |
3290 | |
388 | 3291 /* Figure out if the word should be capitalised. */ |
475 | 3292 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col); |
3293 | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3294 /* Make a copy of current line since autocommands may free the line. */ |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3295 line = vim_strsave(ml_get_curline()); |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3296 if (line == NULL) |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3297 goto skip; |
388 | 3298 |
483 | 3299 /* 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; | |
638 | 3305 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit, |
625 | 3306 TRUE, need_cap, TRUE); |
323 | 3307 |
3308 if (sug.su_ga.ga_len == 0) | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3309 msg(_("Sorry, no suggestions")); |
485 | 3310 else if (count > 0) |
3311 { | |
3312 if (count > sug.su_ga.ga_len) | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3313 smsg(_("Sorry, only %ld suggestions"), |
485 | 3314 (long)sug.su_ga.ga_len); |
3315 } | |
323 | 3316 else |
3317 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
3318 VIM_CLEAR(repl_from); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
3319 VIM_CLEAR(repl_to); |
372 | 3320 |
474 | 3321 #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 | |
323 | 3328 /* List the suggestions. */ |
3329 msg_start(); | |
914 | 3330 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */ |
372 | 3331 lines_left = Rows; /* avoid more prompt */ |
323 | 3332 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
3333 sug.su_badlen, sug.su_badptr); | |
474 | 3334 #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 | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3343 msg_puts((char *)IObuff); |
323 | 3344 msg_clr_eos(); |
3345 msg_putchar('\n'); | |
346 | 3346 |
323 | 3347 msg_scroll = TRUE; |
3348 for (i = 0; i < sug.su_ga.ga_len; ++i) | |
3349 { | |
344 | 3350 stp = &SUG(sug.su_ga, i); |
323 | 3351 |
3352 /* The suggested word may replace only part of the bad word, add | |
3353 * the not replaced part. */ | |
2768 | 3354 vim_strncpy(wcopy, stp->st_word, MAXWLEN); |
323 | 3355 if (sug.su_badlen > stp->st_orglen) |
625 | 3356 vim_strncpy(wcopy + stp->st_wordlen, |
323 | 3357 sug.su_badptr + stp->st_orglen, |
3358 sug.su_badlen - stp->st_orglen); | |
474 | 3359 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1); |
3360 #ifdef FEAT_RIGHTLEFT | |
3361 if (cmdmsg_rl) | |
3362 rl_mirror(IObuff); | |
3363 #endif | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3364 msg_puts((char *)IObuff); |
474 | 3365 |
3366 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy); | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3367 msg_puts((char *)IObuff); |
346 | 3368 |
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); | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3374 msg_puts((char *)IObuff); |
346 | 3375 } |
3376 | |
324 | 3377 if (p_verbose > 0) |
344 | 3378 { |
346 | 3379 /* Add the score. */ |
351 | 3380 if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
474 | 3381 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)", |
344 | 3382 stp->st_salscore ? "s " : "", |
3383 stp->st_score, stp->st_altscore); | |
3384 else | |
474 | 3385 vim_snprintf((char *)IObuff, IOSIZE, " (%d)", |
346 | 3386 stp->st_score); |
474 | 3387 #ifdef FEAT_RIGHTLEFT |
3388 if (cmdmsg_rl) | |
3389 /* Mirror the numbers, but keep the leading space. */ | |
3390 rl_mirror(IObuff + 1); | |
3391 #endif | |
346 | 3392 msg_advance(30); |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3393 msg_puts((char *)IObuff); |
344 | 3394 } |
323 | 3395 msg_putchar('\n'); |
3396 } | |
3397 | |
474 | 3398 #ifdef FEAT_RIGHTLEFT |
3399 cmdmsg_rl = FALSE; | |
3400 msg_col = 0; | |
3401 #endif | |
323 | 3402 /* Ask for choice. */ |
485 | 3403 selected = prompt_for_number(&mouse_used); |
372 | 3404 if (mouse_used) |
485 | 3405 selected -= lines_left; |
1938 | 3406 lines_left = Rows; /* avoid more prompt */ |
3407 /* don't delay for 'smd' in normal_cmd() */ | |
3408 msg_scroll = msg_scroll_save; | |
485 | 3409 } |
3410 | |
3411 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); | |
548 | 3415 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 } | |
485 | 3431 |
3432 /* Replace the word. */ | |
1938 | 3433 p = alloc((unsigned)STRLEN(line) - stp->st_orglen |
3434 + stp->st_wordlen + 1); | |
485 | 3435 if (p != NULL) |
3436 { | |
835 | 3437 c = (int)(sug.su_badptr - line); |
485 | 3438 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; | |
3443 | |
3444 /* For redo we use a change-word command. */ | |
3445 ResetRedobuff(); | |
3446 AppendToRedobuff((char_u *)"ciw"); | |
620 | 3447 AppendToRedobuffLit(p + c, |
625 | 3448 stp->st_wordlen + sug.su_badlen - stp->st_orglen); |
485 | 3449 AppendCharToRedobuff(ESC); |
819 | 3450 |
3451 /* After this "p" may be invalid. */ | |
3452 changed_bytes(curwin->w_cursor.lnum, c); | |
485 | 3453 } |
3454 } | |
3455 else | |
3456 curwin->w_cursor = prev_cursor; | |
323 | 3457 |
344 | 3458 spell_find_cleanup(&sug); |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3459 skip: |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3460 vim_free(line); |
344 | 3461 } |
3462 | |
3463 /* | |
475 | 3464 * 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3468 check_need_cap(linenr_T lnum, colnr_T col) |
475 | 3469 { |
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 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3477 if (curwin->w_s->b_cap_prog == NULL) |
475 | 3478 return FALSE; |
3479 | |
3480 line = ml_get_curline(); | |
3481 endcol = 0; | |
12323
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11521
diff
changeset
|
3482 if (getwhitecols(line) >= (int)col) |
475 | 3483 { |
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; | |
835 | 3498 endcol = (colnr_T)STRLEN(line); |
475 | 3499 } |
3500 } | |
3501 } | |
3502 else | |
3503 endcol = col; | |
3504 | |
3505 if (endcol > 0) | |
3506 { | |
3507 /* Check if sentence ends before the bad word. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3508 regmatch.regprog = curwin->w_s->b_cap_prog; |
475 | 3509 regmatch.rm_ic = FALSE; |
3510 p = line + endcol; | |
3511 for (;;) | |
3512 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3513 MB_PTR_BACK(line, p); |
5477 | 3514 if (p == line || spell_iswordp_nmw(p, curwin)) |
475 | 3515 break; |
3516 if (vim_regexec(®match, p, 0) | |
3517 && regmatch.endp[0] == line + endcol) | |
3518 { | |
3519 need_cap = TRUE; | |
3520 break; | |
3521 } | |
3522 } | |
6375 | 3523 curwin->w_s->b_cap_prog = regmatch.regprog; |
475 | 3524 } |
3525 | |
3526 vim_free(line_copy); | |
3527 | |
3528 return need_cap; | |
3529 } | |
3530 | |
3531 | |
3532 /* | |
372 | 3533 * ":spellrepall" |
3534 */ | |
3535 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3536 ex_spellrepall(exarg_T *eap UNUSED) |
372 | 3537 { |
3538 pos_T pos = curwin->w_cursor; | |
3539 char_u *frompat; | |
3540 int addlen; | |
3541 char_u *line; | |
3542 char_u *p; | |
3543 int save_ws = p_ws; | |
483 | 3544 linenr_T prev_lnum = 0; |
372 | 3545 |
3546 if (repl_from == NULL || repl_to == NULL) | |
3547 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3548 emsg(_("E752: No previous spell replacement")); |
372 | 3549 return; |
3550 } | |
835 | 3551 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from)); |
3552 | |
3553 frompat = alloc((unsigned)STRLEN(repl_from) + 7); | |
372 | 3554 if (frompat == NULL) |
3555 return; | |
3556 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); | |
3557 p_ws = FALSE; | |
3558 | |
483 | 3559 sub_nsubs = 0; |
3560 sub_nlines = 0; | |
372 | 3561 curwin->w_cursor.lnum = 0; |
3562 while (!got_int) | |
3563 { | |
11521
578df034735d
patch 8.0.0643: when a pattern search is slow Vim becomes unusable
Christian Brabandt <cb@256bit.org>
parents:
11394
diff
changeset
|
3564 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0 |
372 | 3565 || 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 { | |
835 | 3574 p = alloc((unsigned)STRLEN(line) + addlen + 1); |
372 | 3575 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); | |
483 | 3582 |
3583 if (curwin->w_cursor.lnum != prev_lnum) | |
3584 { | |
3585 ++sub_nlines; | |
3586 prev_lnum = curwin->w_cursor.lnum; | |
3587 } | |
3588 ++sub_nsubs; | |
372 | 3589 } |
835 | 3590 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to); |
372 | 3591 } |
3592 | |
3593 p_ws = save_ws; | |
3594 curwin->w_cursor = pos; | |
3595 vim_free(frompat); | |
3596 | |
483 | 3597 if (sub_nsubs == 0) |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3598 semsg(_("E753: Not found: %s"), repl_from); |
483 | 3599 else |
3600 do_sub_msg(FALSE); | |
372 | 3601 } |
3602 | |
3603 /* | |
344 | 3604 * Find spell suggestions for "word". Return them in the growarray "*gap" as |
3605 * a list of allocated strings. | |
3606 */ | |
3607 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3608 spell_suggest_list( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3609 garray_T *gap, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3610 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3611 int maxcount, /* maximum nr of suggestions */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3612 int need_cap, /* 'spellcapcheck' matched */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3613 int interactive) |
344 | 3614 { |
3615 suginfo_T sug; | |
3616 int i; | |
3617 suggest_T *stp; | |
3618 char_u *wcopy; | |
3619 | |
638 | 3620 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive); |
344 | 3621 |
3622 /* Make room in "gap". */ | |
3623 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); | |
626 | 3624 if (ga_grow(gap, sug.su_ga.ga_len) == OK) |
3625 { | |
3626 for (i = 0; i < sug.su_ga.ga_len; ++i) | |
3627 { | |
3628 stp = &SUG(sug.su_ga, i); | |
3629 | |
3630 /* The suggested word may replace only part of "word", add the not | |
3631 * replaced part. */ | |
3632 wcopy = alloc(stp->st_wordlen | |
835 | 3633 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
626 | 3634 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 } | |
344 | 3640 } |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3653 spell_find_suggest( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3654 char_u *badptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3655 int badlen, /* length of bad word or 0 if unknown */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3656 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3657 int maxcount, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3658 int banbadword, /* don't include badword in suggestions */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3659 int need_cap, /* word should start with capital */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3660 int interactive) |
344 | 3661 { |
534 | 3662 hlf_T attr = HLF_COUNT; |
372 | 3663 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 | |
385 | 3670 int c; |
503 | 3671 int i; |
3672 langp_T *lp; | |
344 | 3673 |
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); | |
355 | 3680 if (*badptr == NUL) |
3681 return; | |
344 | 3682 hash_init(&su->su_banned); |
3683 | |
3684 su->su_badptr = badptr; | |
638 | 3685 if (badlen != 0) |
3686 su->su_badlen = badlen; | |
3687 else | |
3688 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE); | |
344 | 3689 su->su_maxcount = maxcount; |
372 | 3690 su->su_maxscore = SCORE_MAXINIT; |
344 | 3691 |
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); | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3697 /* TODO: make this work if the case-folded text is longer than the original |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3698 * text. Currently an illegal byte causes wrong pointer computations. */ |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3699 su->su_fbadword[su->su_badlen] = NUL; |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3700 |
346 | 3701 /* get caps flags for bad word */ |
474 | 3702 su->su_badflags = badword_captype(su->su_badptr, |
3703 su->su_badptr + su->su_badlen); | |
388 | 3704 if (need_cap) |
3705 su->su_badflags |= WF_ONECAP; | |
344 | 3706 |
503 | 3707 /* 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"). */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3711 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3712 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3713 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i); |
503 | 3714 if (lp->lp_sallang != NULL) |
3715 { | |
3716 su->su_sallang = lp->lp_sallang; | |
3717 break; | |
3718 } | |
3719 } | |
3720 | |
534 | 3721 /* 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 | |
385 | 3727 /* 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. */ | |
455 | 3730 c = PTR2CHAR(su->su_badptr); |
534 | 3731 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT) |
385 | 3732 { |
3733 make_case_word(su->su_badword, buf, WF_ONECAP); | |
3734 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, | |
625 | 3735 0, TRUE, su->su_sallang, FALSE); |
385 | 3736 } |
3737 | |
344 | 3738 /* Ban the bad word itself. It may appear in another region. */ |
358 | 3739 if (banbadword) |
3740 add_banned(su, su->su_badword); | |
344 | 3741 |
372 | 3742 /* 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 | |
375 | 3755 /* Evaluate an expression. Skip this when called recursively, |
3756 * when using spellsuggest() in the expression. */ | |
372 | 3757 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. */ | |
625 | 3771 spell_suggest_intern(su, interactive); |
372 | 3772 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3790 spell_suggest_expr(suginfo_T *su, char_u *expr) |
372 | 3791 { |
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); | |
625 | 3809 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); | |
372 | 3812 } |
3813 list_unref(list); | |
3814 } | |
3815 | |
625 | 3816 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3817 check_suggestions(su, &su->su_ga); | |
372 | 3818 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
3819 } | |
3820 #endif | |
3821 | |
3822 /* | |
474 | 3823 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'. |
372 | 3824 */ |
3825 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3826 spell_suggest_file(suginfo_T *su, char_u *fname) |
372 | 3827 { |
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 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3838 semsg(_(e_notopen), fname); |
372 | 3839 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, | |
625 | 3867 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE); |
372 | 3868 } |
3869 } | |
3870 | |
3871 fclose(fd); | |
3872 | |
625 | 3873 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3874 check_suggestions(su, &su->su_ga); | |
372 | 3875 (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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3882 spell_suggest_intern(suginfo_T *su, int interactive) |
625 | 3883 { |
3884 /* | |
3885 * Load the .sug file(s) that are available and not done yet. | |
3886 */ | |
3887 suggest_load_files(); | |
3888 | |
344 | 3889 /* |
346 | 3890 * 1. Try special cases, such as repeating a word: "the the" -> "the". |
344 | 3891 * |
3892 * Set a maximum score to limit the combination of operations that is | |
3893 * tried. | |
3894 */ | |
346 | 3895 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); | |
344 | 3902 |
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 /* | |
346 | 3908 * 3. Try finding sound-a-like words. |
344 | 3909 */ |
625 | 3910 if ((sps_flags & SPS_FAST) == 0) |
3911 { | |
3912 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 /* | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3918 * While going through the soundfold tree "su_maxscore" is the score |
625 | 3919 * 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; | |
346 | 3930 suggest_try_soundalike(su); |
625 | 3931 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(); | |
3947 } | |
3948 | |
3949 /* When CTRL-C was hit while searching do show the results. Only clear | |
3950 * got_int when using a command, not for spellsuggest(). */ | |
344 | 3951 ui_breakcheck(); |
625 | 3952 if (interactive && got_int) |
344 | 3953 { |
3954 (void)vgetc(); | |
3955 got_int = FALSE; | |
3956 } | |
3957 | |
372 | 3958 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
344 | 3959 { |
3960 if (sps_flags & SPS_BEST) | |
3961 /* Adjust the word score for how it sounds like. */ | |
3962 rescore_suggestions(su); | |
3963 | |
625 | 3964 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3965 check_suggestions(su, &su->su_ga); | |
372 | 3966 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
344 | 3967 } |
3968 } | |
3969 | |
3970 /* | |
3971 * Free the info put in "*su" by spell_find_suggest(). | |
3972 */ | |
3973 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3974 spell_find_cleanup(suginfo_T *su) |
344 | 3975 { |
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); | |
323 | 3985 |
3986 /* Free the banned words. */ | |
625 | 3987 hash_clear_all(&su->su_banned, 0); |
323 | 3988 } |
3989 | |
3990 /* | |
324 | 3991 * 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. | |
323 | 3994 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3995 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3996 onecap_copy( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3997 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3998 char_u *wcopy, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3999 int upper) /* TRUE: first letter made upper case */ |
323 | 4000 { |
4001 char_u *p; | |
4002 int c; | |
4003 int l; | |
4004 | |
4005 p = word; | |
4006 if (has_mbyte) | |
474 | 4007 c = mb_cptr2char_adv(&p); |
323 | 4008 else |
4009 c = *p++; | |
4010 if (upper) | |
324 | 4011 c = SPELL_TOUPPER(c); |
323 | 4012 else |
324 | 4013 c = SPELL_TOFOLD(c); |
323 | 4014 if (has_mbyte) |
4015 l = mb_char2bytes(c, wcopy); | |
4016 else | |
4017 { | |
4018 l = 1; | |
4019 wcopy[0] = c; | |
4020 } | |
376 | 4021 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
323 | 4022 } |
4023 | |
4024 /* | |
324 | 4025 * Make a copy of "word" with all the letters upper cased into |
4026 * "wcopy[MAXWLEN]". The result is NUL terminated. | |
323 | 4027 */ |
4028 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4029 allcap_copy(char_u *word, char_u *wcopy) |
323 | 4030 { |
4031 char_u *s; | |
4032 char_u *d; | |
4033 int c; | |
4034 | |
4035 d = wcopy; | |
4036 for (s = word; *s != NUL; ) | |
4037 { | |
4038 if (has_mbyte) | |
474 | 4039 c = mb_cptr2char_adv(&s); |
323 | 4040 else |
4041 c = *s++; | |
492 | 4042 |
3042 | 4043 /* We only change 0xdf to SS when we are certain latin1 is used. It |
492 | 4044 * 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 | |
4053 c = SPELL_TOUPPER(c); | |
323 | 4054 |
4055 if (has_mbyte) | |
4056 { | |
4057 if (d - wcopy >= MAXWLEN - MB_MAXBYTES) | |
4058 break; | |
4059 d += mb_char2bytes(c, d); | |
4060 } | |
4061 else | |
4062 { | |
4063 if (d - wcopy >= MAXWLEN - 1) | |
4064 break; | |
4065 *d++ = c; | |
4066 } | |
4067 } | |
4068 *d = NUL; | |
4069 } | |
4070 | |
4071 /* | |
346 | 4072 * Try finding suggestions by recognizing specific situations. |
4073 */ | |
4074 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4075 suggest_try_special(suginfo_T *su) |
346 | 4076 { |
4077 char_u *p; | |
376 | 4078 size_t len; |
346 | 4079 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; | |
534 | 4096 |
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, | |
625 | 4100 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE); |
346 | 4101 } |
4102 } | |
4103 | |
4104 /* | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4105 * Change the 0 to 1 to measure how much time is spent in each state. |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4106 * Output is dumped in "suggestprof". |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4107 */ |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4108 #if 0 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4109 # define SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4110 proftime_T current; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4111 proftime_T total; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4112 proftime_T times[STATE_FINAL + 1]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4113 long counts[STATE_FINAL + 1]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4114 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4115 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4116 prof_init(void) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4117 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4118 for (int i = 0; i <= STATE_FINAL; ++i) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4119 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4120 profile_zero(×[i]); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4121 counts[i] = 0; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4122 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4123 profile_start(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4124 profile_start(&total); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4125 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4126 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4127 /* call before changing state */ |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4128 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4129 prof_store(state_T state) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4130 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4131 profile_end(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4132 profile_add(×[state], ¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4133 ++counts[state]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4134 profile_start(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4135 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4136 # define PROF_STORE(state) prof_store(state); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4137 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4138 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4139 prof_report(char *name) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4140 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4141 FILE *fd = fopen("suggestprof", "a"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4142 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4143 profile_end(&total); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4144 fprintf(fd, "-----------------------\n"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4145 fprintf(fd, "%s: %s\n", name, profile_msg(&total)); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4146 for (int i = 0; i <= STATE_FINAL; ++i) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4147 fprintf(fd, "%d: %s (%ld)\n", i, profile_msg(×[i]), counts[i]); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4148 fclose(fd); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4149 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4150 #else |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4151 # define PROF_STORE(state) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4152 #endif |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4153 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4154 /* |
323 | 4155 * Try finding suggestions by adding/removing/swapping letters. |
4156 */ | |
4157 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4158 suggest_try_change(suginfo_T *su) |
323 | 4159 { |
4160 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ | |
625 | 4161 int n; |
4162 char_u *p; | |
4163 int lpi; | |
323 | 4164 langp_T *lp; |
4165 | |
4166 /* We make a copy of the case-folded bad word, so that we can modify it | |
346 | 4167 * to find matches (esp. REP items). Append some more text, changing |
4168 * chars after the bad word may help. */ | |
323 | 4169 STRCPY(fword, su->su_fbadword); |
835 | 4170 n = (int)STRLEN(fword); |
346 | 4171 p = su->su_badptr + su->su_badlen; |
835 | 4172 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n); |
323 | 4173 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4174 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4175 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4176 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
482 | 4177 |
500 | 4178 /* If reloading a spell file fails it's still in the list but |
4179 * everything has been cleared. */ | |
625 | 4180 if (lp->lp_slang->sl_fbyts == NULL) |
500 | 4181 continue; |
4182 | |
625 | 4183 /* Try it for this language. Will add possible suggestions. */ |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4184 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4185 prof_init(); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4186 #endif |
625 | 4187 suggest_trie_walk(su, lp, fword, FALSE); |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4188 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4189 prof_report("try_change"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4190 #endif |
625 | 4191 } |
4192 } | |
4193 | |
4194 /* 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4230 suggest_trie_walk( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4231 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4232 langp_T *lp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4233 char_u *fword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4234 int soundfold) |
625 | 4235 { |
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; | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
4239 * concatenation of prefix compound |
625 | 4240 * 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 { | |
330 | 4293 /* |
375 | 4294 * 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 */ | |
482 | 4297 fbyts = slang->sl_fbyts; |
4298 fidxs = slang->sl_fidxs; | |
4299 pbyts = slang->sl_pbyts; | |
4300 pidxs = slang->sl_pidxs; | |
375 | 4301 if (pbyts != NULL) |
4302 { | |
4303 byts = pbyts; | |
4304 idxs = pidxs; | |
482 | 4305 sp->ts_prefixdepth = PFD_PREFIXTREE; |
375 | 4306 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
4307 } | |
4308 else | |
4309 { | |
4310 byts = fbyts; | |
4311 idxs = fidxs; | |
482 | 4312 sp->ts_prefixdepth = PFD_NOPREFIX; |
485 | 4313 sp->ts_state = STATE_START; |
375 | 4314 } |
625 | 4315 } |
4316 | |
4317 /* | |
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) | |
4328 { | |
4329 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) | |
4340 { | |
4341 /* 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; | |
4345 | |
4346 /* Always past NUL bytes now. */ | |
4347 n = (int)sp->ts_state; | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4348 PROF_STORE(sp->ts_state) |
625 | 4349 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) | |
4355 { | |
4356 /* Set su->su_badflags to the caps type at this position. | |
4357 * Use the caps type until here for the prefix itself. */ | |
4358 if (has_mbyte) | |
4359 n = nofold_len(fword, sp->ts_fidx, su->su_badptr); | |
4360 else | |
4361 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, | |
455 | 4364 su->su_badptr + su->su_badlen); |
625 | 4365 #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; | |
4375 | |
4376 /* 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); | |
835 | 4381 sp->ts_prewordlen = (char_u)STRLEN(preword); |
625 | 4382 sp->ts_splitoff = sp->ts_twordlen; |
4383 } | |
4384 break; | |
4385 } | |
4386 | |
4387 if (sp->ts_curi > len || byts[arridx] != 0) | |
4388 { | |
4389 /* Past bytes in node and/or past NUL bytes. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4390 PROF_STORE(sp->ts_state) |
625 | 4391 sp->ts_state = STATE_ENDNUL; |
4392 sp->ts_save_badflags = su->su_badflags; | |
4393 break; | |
4394 } | |
4395 | |
4396 /* | |
4397 * End of word in tree. | |
4398 */ | |
4399 ++sp->ts_curi; /* eat one NUL byte */ | |
4400 | |
4401 flags = (int)idxs[arridx]; | |
698 | 4402 |
4403 /* Skip words with the NOSUGGEST flag. */ | |
4404 if (flags & WF_NOSUGGEST) | |
4405 break; | |
4406 | |
625 | 4407 fword_ends = (fword[sp->ts_fidx] == NUL |
4408 || (soundfold | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
4409 ? VIM_ISWHITE(fword[sp->ts_fidx]) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4410 : !spell_iswordp(fword + sp->ts_fidx, curwin))); |
625 | 4411 tword[sp->ts_twordlen] = NUL; |
4412 | |
4413 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL | |
485 | 4414 && (sp->ts_flags & TSF_PREFIXOK) == 0) |
625 | 4415 { |
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) | |
4425 { | |
4426 c = valid_word_prefix(c, n, flags, | |
483 | 4427 tword + sp->ts_splitoff, slang, FALSE); |
625 | 4428 if (c == 0) |
4429 break; | |
4430 | |
4431 /* Use the WF_RARE flag for a rare prefix. */ | |
4432 if (c & WF_RAREPFX) | |
4433 flags |= WF_RARE; | |
4434 | |
4435 /* 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; | |
4440 } | |
4441 } | |
4442 | |
4443 /* 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 | |
500 | 4446 && (flags & WF_NEEDCOMP)) |
625 | 4447 goodword_ends = FALSE; |
4448 else | |
4449 goodword_ends = TRUE; | |
4450 | |
4451 p = NULL; | |
4452 compound_ok = TRUE; | |
4453 if (sp->ts_complen > sp->ts_compsplit) | |
4454 { | |
4455 if (slang->sl_nobreak) | |
4456 { | |
4457 /* 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 | |
4462 == sp->ts_twordlen - sp->ts_splitoff | |
4463 && STRNCMP(fword + sp->ts_splitfidx, | |
4464 tword + sp->ts_splitoff, | |
4465 sp->ts_fidx - sp->ts_splitfidx) == 0) | |
492 | 4466 { |
625 | 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) | |
492 | 4473 add_suggestion(su, &su->su_ga, preword, |
503 | 4474 sp->ts_splitfidx - repextra, |
625 | 4475 newscore, 0, FALSE, |
4476 lp->lp_sallang, FALSE); | |
4477 break; | |
492 | 4478 } |
490 | 4479 } |
4480 else | |
625 | 4481 { |
4482 /* 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; | |
4490 /* 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; | |
4497 | |
4498 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); | |
1762 | 4503 |
4504 /* Verify CHECKCOMPOUNDPATTERN rules. */ | |
4505 if (match_checkcompoundpattern(preword, sp->ts_prewordlen, | |
4506 &slang->sl_comppat)) | |
4507 compound_ok = FALSE; | |
4508 | |
4509 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, | |
625 | 4515 compflags + sp->ts_compsplit)) |
1762 | 4516 /* Compound is not allowed. But it may still be |
4517 * possible if we add another (short) word. */ | |
4518 compound_ok = FALSE; | |
4519 } | |
625 | 4520 |
4521 /* Get pointer to last char of previous word. */ | |
4522 p = preword + sp->ts_prewordlen; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
4523 MB_PTR_BACK(preword, p); |
625 | 4524 } |
4525 } | |
4526 | |
4527 /* | |
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, | |
483 | 4537 preword + sp->ts_prewordlen); |
625 | 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) | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4545 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)) |
625 | 4546 c = WF_ONECAP; |
4547 c |= flags; | |
4548 | |
4549 /* When appending a compound word after a word character don't | |
4550 * use Onecap. */ | |
5477 | 4551 if (p != NULL && spell_iswordp_nmw(p, curwin)) |
625 | 4552 c &= ~WF_ONECAP; |
4553 make_case_word(tword + sp->ts_splitoff, | |
490 | 4554 preword + sp->ts_prewordlen, c); |
625 | 4555 } |
4556 | |
4557 if (!soundfold) | |
4558 { | |
323 | 4559 /* Don't use a banned word. It may appear again as a good |
4560 * word, thus remember it. */ | |
4561 if (flags & WF_BANNED) | |
4562 { | |
483 | 4563 add_banned(su, preword + sp->ts_prewordlen); |
323 | 4564 break; |
4565 } | |
534 | 4566 if ((sp->ts_complen == sp->ts_compsplit |
625 | 4567 && WAS_BANNED(su, preword + sp->ts_prewordlen)) |
4568 || WAS_BANNED(su, preword)) | |
534 | 4569 { |
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 } | |
625 | 4575 } |
4576 | |
4577 newscore = 0; | |
4578 if (!soundfold) /* soundfold words don't have flags */ | |
4579 { | |
323 | 4580 if ((flags & WF_REGION) |
390 | 4581 && (((unsigned)flags >> 16) & lp->lp_region) == 0) |
323 | 4582 newscore += SCORE_REGION; |
4583 if (flags & WF_RARE) | |
4584 newscore += SCORE_RARE; | |
4585 | |
346 | 4586 if (!spell_valid_case(su->su_badflags, |
483 | 4587 captype(preword + sp->ts_prewordlen, NULL))) |
323 | 4588 newscore += SCORE_ICASE; |
625 | 4589 } |
4590 | |
4591 /* 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) | |
4600 { | |
4601 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 } | |
4608 #endif | |
4609 if (soundfold) | |
4610 { | |
4611 /* For soundfolded words we need to find the original | |
1188 | 4612 * words, the edit distance and then add them. */ |
625 | 4613 add_sound_suggest(su, preword, sp->ts_score, lp); |
4614 } | |
2457
6c1d10a85398
Avoid illegal memory access in spell suggestion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2455
diff
changeset
|
4615 else if (sp->ts_fidx > 0) |
625 | 4616 { |
4617 /* Give a penalty when changing non-word char to word | |
4618 * char, e.g., "thes," -> "these". */ | |
366 | 4619 p = fword + sp->ts_fidx; |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
4620 MB_PTR_BACK(fword, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4621 if (!spell_iswordp(p, curwin)) |
366 | 4622 { |
4623 p = preword + STRLEN(preword); | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
4624 MB_PTR_BACK(preword, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4625 if (spell_iswordp(p, curwin)) |
366 | 4626 newscore += SCORE_NONWORD; |
4627 } | |
4628 | |
625 | 4629 /* 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); | |
4634 | |
4635 /* Add the suggestion if the score isn't too bad. */ | |
4636 if (score <= su->su_maxscore) | |
633 | 4637 { |
625 | 4638 add_suggestion(su, &su->su_ga, preword, |
4639 sp->ts_fidx - repextra, | |
4640 score, 0, FALSE, lp->lp_sallang, FALSE); | |
633 | 4641 |
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 } | |
625 | 4660 } |
4661 } | |
4662 | |
4663 /* | |
4664 * Try word split and/or compounding. | |
4665 */ | |
4666 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends) | |
4667 /* Don't split halfway a character. */ | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4668 && (!has_mbyte || sp->ts_tcharlen == 0)) |
625 | 4669 { |
4670 int try_compound; | |
4671 int try_split; | |
4672 | |
4673 /* 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 | |
7526
08c1f73efcde
commit https://github.com/vim/vim/commit/7b877b360532713dc21a0ff3d55a76ac02eaf573
Christian Brabandt <cb@256bit.org>
parents:
7509
diff
changeset
|
4696 && !slang->sl_nocompoundsugs |
625 | 4697 && slang->sl_compprog != NULL |
4698 && ((unsigned)flags >> 24) != 0 | |
4699 && sp->ts_twordlen - sp->ts_splitoff | |
4700 >= slang->sl_compminlen | |
4701 && (!has_mbyte | |
4702 || slang->sl_compminlen == 0 | |
4703 || mb_charlen(tword + sp->ts_splitoff) | |
4704 >= slang->sl_compminlen) | |
4705 && (slang->sl_compsylmax < MAXWLEN | |
4706 || sp->ts_complen + 1 - sp->ts_compsplit | |
4707 < slang->sl_compmax) | |
1762 | 4708 && (can_be_compound(sp, slang, |
4709 compflags, ((unsigned)flags >> 24)))) | |
4710 | |
625 | 4711 { |
4712 try_compound = TRUE; | |
4713 compflags[sp->ts_complen] = ((unsigned)flags >> 24); | |
4714 compflags[sp->ts_complen + 1] = NUL; | |
4715 } | |
4716 | |
4717 /* For NOBREAK we never try splitting, it won't make any word | |
4718 * valid. */ | |
7526
08c1f73efcde
commit https://github.com/vim/vim/commit/7b877b360532713dc21a0ff3d55a76ac02eaf573
Christian Brabandt <cb@256bit.org>
parents:
7509
diff
changeset
|
4719 if (slang->sl_nobreak && !slang->sl_nocompoundsugs) |
625 | 4720 try_compound = TRUE; |
4721 | |
4722 /* 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 { | |
485 | 4729 try_compound = FALSE; |
625 | 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; | |
4736 | |
4737 if (try_split || try_compound) | |
4738 { | |
534 | 4739 if (!try_compound && (!fword_ends || !goodword_ends)) |
485 | 4740 { |
4741 /* If we're going to split need to check that the | |
501 | 4742 * 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; | |
490 | 4748 p = preword; |
4749 while (*skiptowhite(p) != NUL) | |
4750 p = skipwhite(skiptowhite(p)); | |
485 | 4751 if (sp->ts_complen > sp->ts_compsplit |
490 | 4752 && !can_compound(slang, p, |
485 | 4753 compflags + sp->ts_compsplit)) |
4754 break; | |
698 | 4755 |
4756 if (slang->sl_nosplitsugs) | |
4757 newscore += SCORE_SPLIT_NO; | |
4758 else | |
4759 newscore += SCORE_SPLIT; | |
625 | 4760 |
4761 /* Give a bonus to words seen before. */ | |
4762 newscore = score_wordcount_adj(slang, newscore, | |
4763 preword + sp->ts_prewordlen, TRUE); | |
482 | 4764 } |
4765 | |
625 | 4766 if (TRY_DEEPER(su, stack, depth, newscore)) |
323 | 4767 { |
625 | 4768 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 | |
323 | 4777 /* Save things to be restored at STATE_SPLITUNDO. */ |
346 | 4778 sp->ts_save_badflags = su->su_badflags; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4779 PROF_STORE(sp->ts_state) |
376 | 4780 sp->ts_state = STATE_SPLITUNDO; |
4781 | |
4782 ++depth; | |
4783 sp = &stack[depth]; | |
323 | 4784 |
482 | 4785 /* Append a space to preword when splitting. */ |
4786 if (!try_compound && !fword_ends) | |
4787 STRCAT(preword, " "); | |
835 | 4788 sp->ts_prewordlen = (char_u)STRLEN(preword); |
483 | 4789 sp->ts_splitoff = sp->ts_twordlen; |
492 | 4790 sp->ts_splitfidx = sp->ts_fidx; |
376 | 4791 |
4792 /* If the badword has a non-word character at this | |
4793 * position skip it. That means replacing the | |
482 | 4794 * non-word character with a space. Always skip a |
534 | 4795 * character when the word ends. But only when the |
4796 * good word can end. */ | |
625 | 4797 if (((!try_compound && !spell_iswordp_nmw(fword |
5477 | 4798 + sp->ts_fidx, |
4799 curwin)) | |
625 | 4800 || fword_ends) |
4801 && fword[sp->ts_fidx] != NUL | |
4802 && goodword_ends) | |
376 | 4803 { |
482 | 4804 int l; |
4805 | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4806 l = MB_PTR2LEN(fword + sp->ts_fidx); |
482 | 4807 if (fword_ends) |
4808 { | |
4809 /* Copy the skipped character to preword. */ | |
483 | 4810 mch_memmove(preword + sp->ts_prewordlen, |
482 | 4811 fword + sp->ts_fidx, l); |
483 | 4812 sp->ts_prewordlen += l; |
4813 preword[sp->ts_prewordlen] = NUL; | |
482 | 4814 } |
4815 else | |
4816 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; | |
4817 sp->ts_fidx += l; | |
376 | 4818 } |
455 | 4819 |
485 | 4820 /* When compounding include compound flag in |
4821 * compflags[] (already set above). When splitting we | |
4822 * may start compounding over again. */ | |
482 | 4823 if (try_compound) |
483 | 4824 ++sp->ts_complen; |
482 | 4825 else |
485 | 4826 sp->ts_compsplit = sp->ts_complen; |
4827 sp->ts_prefixdepth = PFD_NOPREFIX; | |
482 | 4828 |
455 | 4829 /* set su->su_badflags to the caps type at this |
4830 * position */ | |
324 | 4831 if (has_mbyte) |
455 | 4832 n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
324 | 4833 else |
455 | 4834 n = sp->ts_fidx; |
474 | 4835 su->su_badflags = badword_captype(su->su_badptr + n, |
455 | 4836 su->su_badptr + su->su_badlen); |
323 | 4837 |
4838 /* Restart at top of the tree. */ | |
376 | 4839 sp->ts_arridx = 0; |
485 | 4840 |
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; | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4847 PROF_STORE(sp->ts_state) |
485 | 4848 sp->ts_state = STATE_NOPREFIX; |
4849 } | |
323 | 4850 } |
4851 } | |
625 | 4852 } |
4853 break; | |
4854 | |
4855 case STATE_SPLITUNDO: | |
4856 /* Undo the changes done for word split or compound word. */ | |
4857 su->su_badflags = sp->ts_save_badflags; | |
4858 | |
4859 /* Continue looking for NUL bytes. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4860 PROF_STORE(sp->ts_state) |
625 | 4861 sp->ts_state = STATE_START; |
4862 | |
4863 /* In case we went into the prefix tree. */ | |
4864 byts = fbyts; | |
4865 idxs = fidxs; | |
4866 break; | |
4867 | |
4868 case STATE_ENDNUL: | |
4869 /* Past the NUL bytes in the node. */ | |
4870 su->su_badflags = sp->ts_save_badflags; | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4871 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0) |
625 | 4872 { |
4873 /* The badword ends, can't use STATE_PLAIN. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4874 PROF_STORE(sp->ts_state) |
625 | 4875 sp->ts_state = STATE_DEL; |
323 | 4876 break; |
625 | 4877 } |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4878 PROF_STORE(sp->ts_state) |
625 | 4879 sp->ts_state = STATE_PLAIN; |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
4880 /* FALLTHROUGH */ |
625 | 4881 |
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. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4892 PROF_STORE(sp->ts_state) |
625 | 4893 if (sp->ts_fidx >= sp->ts_fidxtry) |
323 | 4894 sp->ts_state = STATE_DEL; |
4895 else | |
625 | 4896 sp->ts_state = STATE_FINAL; |
4897 } | |
4898 else | |
4899 { | |
4900 arridx += sp->ts_curi++; | |
4901 c = byts[arridx]; | |
4902 | |
4903 /* 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 | |
5695 | 4906 * just deleted this byte, accepting it is always cheaper than |
625 | 4907 * delete + substitute. */ |
4908 if (c == fword[sp->ts_fidx] | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4909 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)) |
625 | 4910 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); | |
323 | 4925 else |
625 | 4926 sprintf(changename[depth], "%.*s-%s: accept %c", |
4927 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
4928 fword[sp->ts_fidx]); | |
4929 #endif | |
323 | 4930 ++depth; |
625 | 4931 sp = &stack[depth]; |
4932 ++sp->ts_fidx; | |
4933 tword[sp->ts_twordlen++] = c; | |
4934 sp->ts_arridx = idxs[arridx]; | |
4935 if (newscore == SCORE_SUBST) | |
4936 sp->ts_isdiff = DIFF_YES; | |
330 | 4937 if (has_mbyte) |
358 | 4938 { |
625 | 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) | |
4943 { | |
4944 /* 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) | |
4949 ? DIFF_YES : DIFF_NONE; | |
4950 } | |
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) | |
4959 { | |
4960 /* Correct ts_fidx for the byte length of the | |
4961 * character (we didn't check that before). */ | |
4962 sp->ts_fidx = sp->ts_fcharstart | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4963 + MB_PTR2LEN( |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4964 fword + sp->ts_fcharstart); |
625 | 4965 /* For changing a composing character adjust |
4966 * the score from SCORE_SUBST to | |
4967 * SCORE_SUBCOMP. */ | |
4968 if (enc_utf8 | |
4969 && utf_iscomposing( | |
11269
121d29004998
patch 8.0.0520: using a function pointer while the function is known
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
4970 utf_ptr2char(tword |
625 | 4971 + sp->ts_twordlen |
4972 - sp->ts_tcharlen)) | |
4973 && utf_iscomposing( | |
11269
121d29004998
patch 8.0.0520: using a function pointer while the function is known
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
4974 utf_ptr2char(fword |
625 | 4975 + sp->ts_fcharstart))) |
4976 sp->ts_score -= | |
4977 SCORE_SUBST - SCORE_SUBCOMP; | |
4978 | |
4979 /* 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 | |
4986 - sp->ts_tcharlen), | |
4987 mb_ptr2char(fword | |
4988 + sp->ts_fcharstart))) | |
4989 sp->ts_score -= | |
4990 SCORE_SUBST - SCORE_SIMILAR; | |
4991 } | |
4992 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). */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5010 MB_PTR_BACK(tword, p); |
625 | 5011 if (c == mb_ptr2char(p)) |
5012 sp->ts_score -= SCORE_INS | |
5013 - SCORE_INSDUP; | |
5014 } | |
5015 } | |
5016 | |
5017 /* Starting a new char, reset the length. */ | |
5018 sp->ts_tcharlen = 0; | |
5019 } | |
358 | 5020 } |
330 | 5021 else |
358 | 5022 { |
625 | 5023 /* 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; | |
358 | 5032 } |
625 | 5033 } |
5034 } | |
5035 break; | |
5036 | |
5037 case STATE_DEL: | |
5038 /* 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 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5042 PROF_STORE(sp->ts_state) |
625 | 5043 sp->ts_state = STATE_FINAL; |
5044 break; | |
5045 } | |
5046 /* | |
5047 * Try skipping one character in the bad word (delete it). | |
5048 */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5049 PROF_STORE(sp->ts_state) |
625 | 5050 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. */ | |
5078 if (has_mbyte) | |
5079 { | |
5080 c = mb_ptr2char(fword + sp->ts_fidx); | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5081 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx); |
625 | 5082 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; | |
323 | 5086 } |
5087 else | |
625 | 5088 { |
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; | |
323 | 5092 } |
5093 break; | |
625 | 5094 } |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5095 /* FALLTHROUGH */ |
625 | 5096 |
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. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5102 PROF_STORE(sp->ts_state) |
625 | 5103 sp->ts_state = STATE_SWAP; |
5104 break; | |
5105 } | |
5106 | |
5107 /* skip over NUL bytes */ | |
5108 n = sp->ts_arridx; | |
5109 for (;;) | |
5110 { | |
5111 if (sp->ts_curi > byts[n]) | |
5112 { | |
5113 /* Only NUL bytes at this node, go to next state. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5114 PROF_STORE(sp->ts_state) |
625 | 5115 sp->ts_state = STATE_SWAP; |
5116 break; | |
5117 } | |
5118 if (byts[n + sp->ts_curi] != NUL) | |
5119 { | |
5120 /* Found a byte to insert. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5121 PROF_STORE(sp->ts_state) |
625 | 5122 sp->ts_state = STATE_INS; |
330 | 5123 break; |
5124 } | |
625 | 5125 ++sp->ts_curi; |
5126 } | |
5127 break; | |
5128 | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5129 /* FALLTHROUGH */ |
625 | 5130 |
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. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5138 PROF_STORE(sp->ts_state) |
625 | 5139 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]; | |
330 | 5169 if (has_mbyte) |
5170 { | |
625 | 5171 fl = MB_BYTE2LEN(c); |
5172 if (fl > 1) | |
330 | 5173 { |
625 | 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; | |
330 | 5180 } |
323 | 5181 } |
5182 else | |
625 | 5183 fl = 1; |
5184 if (fl == 1) | |
5185 { | |
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 | |
5191 && tword[sp->ts_twordlen - 2] == c) | |
5192 sp->ts_score -= SCORE_INS - SCORE_INSDUP; | |
5193 } | |
5194 } | |
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. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5208 PROF_STORE(sp->ts_state) |
625 | 5209 sp->ts_state = STATE_FINAL; |
5210 break; | |
5211 } | |
5212 | |
5213 /* Don't swap if the first character is not a word character. | |
5214 * SWAP3 etc. also don't make sense then. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5215 if (!soundfold && !spell_iswordp(p, curwin)) |
625 | 5216 { |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5217 PROF_STORE(sp->ts_state) |
625 | 5218 sp->ts_state = STATE_REP_INI; |
323 | 5219 break; |
625 | 5220 } |
5221 | |
5222 if (has_mbyte) | |
5223 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5224 n = MB_CPTR2LEN(p); |
625 | 5225 c = mb_ptr2char(p); |
1337 | 5226 if (p[n] == NUL) |
5227 c2 = NUL; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5228 else if (!soundfold && !spell_iswordp(p + n, curwin)) |
625 | 5229 c2 = c; /* don't swap non-word char */ |
5230 else | |
5231 c2 = mb_ptr2char(p + n); | |
5232 } | |
5233 else | |
5234 { | |
1337 | 5235 if (p[1] == NUL) |
5236 c2 = NUL; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5237 else if (!soundfold && !spell_iswordp(p + 1, curwin)) |
625 | 5238 c2 = c; /* don't swap non-word char */ |
5239 else | |
5240 c2 = p[1]; | |
5241 } | |
5242 | |
1337 | 5243 /* When the second character is NUL we can't swap. */ |
5244 if (c2 == NUL) | |
5245 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5246 PROF_STORE(sp->ts_state) |
1337 | 5247 sp->ts_state = STATE_REP_INI; |
5248 break; | |
5249 } | |
5250 | |
625 | 5251 /* 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 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5255 PROF_STORE(sp->ts_state) |
625 | 5256 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 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5267 PROF_STORE(sp->ts_state) |
625 | 5268 sp->ts_state = STATE_UNSWAP; |
5269 ++depth; | |
330 | 5270 if (has_mbyte) |
5271 { | |
625 | 5272 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; | |
330 | 5276 } |
5277 else | |
5278 { | |
625 | 5279 p[0] = c2; |
330 | 5280 p[1] = c; |
625 | 5281 stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
5282 } | |
5283 } | |
5284 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5285 { |
625 | 5286 /* If this swap doesn't work then SWAP3 won't either. */ |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5287 PROF_STORE(sp->ts_state) |
625 | 5288 sp->ts_state = STATE_REP_INI; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5289 } |
625 | 5290 break; |
5291 | |
5292 case STATE_UNSWAP: | |
5293 /* Undo the STATE_SWAP swap: "21" -> "12". */ | |
5294 p = fword + sp->ts_fidx; | |
5295 if (has_mbyte) | |
5296 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5297 n = MB_PTR2LEN(p); |
625 | 5298 c = mb_ptr2char(p + n); |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5299 mch_memmove(p + MB_PTR2LEN(p + n), p, n); |
625 | 5300 mb_char2bytes(c, p); |
5301 } | |
5302 else | |
5303 { | |
5304 c = *p; | |
5305 *p = p[1]; | |
5306 p[1] = c; | |
5307 } | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5308 /* FALLTHROUGH */ |
625 | 5309 |
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; | |
5314 if (has_mbyte) | |
5315 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5316 n = MB_CPTR2LEN(p); |
625 | 5317 c = mb_ptr2char(p); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5318 fl = MB_CPTR2LEN(p + n); |
625 | 5319 c2 = mb_ptr2char(p + n); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5320 if (!soundfold && !spell_iswordp(p + n + fl, curwin)) |
625 | 5321 c3 = c; /* don't swap non-word char */ |
5322 else | |
5323 c3 = mb_ptr2char(p + n + fl); | |
5324 } | |
5325 else | |
5326 { | |
5327 c = *p; | |
5328 c2 = p[1]; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5329 if (!soundfold && !spell_iswordp(p + 2, curwin)) |
625 | 5330 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 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5343 PROF_STORE(sp->ts_state) |
625 | 5344 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 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5355 PROF_STORE(sp->ts_state) |
625 | 5356 sp->ts_state = STATE_UNSWAP3; |
5357 ++depth; | |
5358 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 | |
5367 { | |
5368 p[0] = p[2]; | |
5369 p[2] = c; | |
5370 stack[depth].ts_fidxtry = sp->ts_fidx + 3; | |
5371 } | |
5372 } | |
5373 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5374 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5375 PROF_STORE(sp->ts_state) |
625 | 5376 sp->ts_state = STATE_REP_INI; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5377 } |
625 | 5378 break; |
5379 | |
5380 case STATE_UNSWAP3: | |
5381 /* Undo STATE_SWAP3: "321" -> "123" */ | |
5382 p = fword + sp->ts_fidx; | |
5383 if (has_mbyte) | |
5384 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5385 n = MB_PTR2LEN(p); |
625 | 5386 c2 = mb_ptr2char(p + n); |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5387 fl = MB_PTR2LEN(p + n); |
625 | 5388 c = mb_ptr2char(p + n + fl); |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5389 tl = MB_PTR2LEN(p + n + fl); |
625 | 5390 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 | |
5396 { | |
5397 c = *p; | |
5398 *p = p[2]; | |
5399 p[2] = c; | |
5400 ++p; | |
5401 } | |
5402 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5403 if (!soundfold && !spell_iswordp(p, curwin)) |
625 | 5404 { |
5405 /* Middle char is not a word char, skip the rotate. First and | |
5406 * third char were already checked at swap and swap3. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5407 PROF_STORE(sp->ts_state) |
625 | 5408 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 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5423 PROF_STORE(sp->ts_state) |
625 | 5424 sp->ts_state = STATE_UNROT3L; |
5425 ++depth; | |
330 | 5426 p = fword + sp->ts_fidx; |
5427 if (has_mbyte) | |
5428 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5429 n = MB_CPTR2LEN(p); |
330 | 5430 c = mb_ptr2char(p); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5431 fl = MB_CPTR2LEN(p + n); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5432 fl += MB_CPTR2LEN(p + n + fl); |
625 | 5433 mch_memmove(p, p + n, fl); |
5434 mb_char2bytes(c, p + fl); | |
5435 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; | |
330 | 5436 } |
5437 else | |
323 | 5438 { |
330 | 5439 c = *p; |
625 | 5440 *p = p[1]; |
5441 p[1] = p[2]; | |
5442 p[2] = c; | |
5443 stack[depth].ts_fidxtry = sp->ts_fidx + 3; | |
5444 } | |
5445 } | |
5446 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5447 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5448 PROF_STORE(sp->ts_state) |
625 | 5449 sp->ts_state = STATE_REP_INI; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5450 } |
625 | 5451 break; |
5452 | |
5453 case STATE_UNROT3L: | |
5454 /* Undo ROT3L: "231" -> "123" */ | |
5455 p = fword + sp->ts_fidx; | |
5456 if (has_mbyte) | |
5457 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5458 n = MB_PTR2LEN(p); |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5459 n += MB_PTR2LEN(p + n); |
625 | 5460 c = mb_ptr2char(p + n); |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5461 tl = MB_PTR2LEN(p + n); |
625 | 5462 mch_memmove(p + tl, p, n); |
5463 mb_char2bytes(c, p); | |
5464 } | |
5465 else | |
5466 { | |
5467 c = p[2]; | |
5468 p[2] = p[1]; | |
5469 p[1] = *p; | |
5470 *p = c; | |
5471 } | |
5472 | |
5473 /* 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 | |
5479 p = fword + sp->ts_fidx; | |
5480 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 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5484 PROF_STORE(sp->ts_state) |
625 | 5485 sp->ts_state = STATE_UNROT3R; |
5486 ++depth; | |
330 | 5487 p = fword + sp->ts_fidx; |
5488 if (has_mbyte) | |
5489 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5490 n = MB_CPTR2LEN(p); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5491 n += MB_CPTR2LEN(p + n); |
330 | 5492 c = mb_ptr2char(p + n); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5493 tl = MB_CPTR2LEN(p + n); |
330 | 5494 mch_memmove(p + tl, p, n); |
5495 mb_char2bytes(c, p); | |
625 | 5496 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
330 | 5497 } |
5498 else | |
5499 { | |
5500 c = p[2]; | |
5501 p[2] = p[1]; | |
5502 p[1] = *p; | |
5503 *p = c; | |
625 | 5504 stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
5505 } | |
5506 } | |
5507 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5508 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5509 PROF_STORE(sp->ts_state) |
625 | 5510 sp->ts_state = STATE_REP_INI; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5511 } |
625 | 5512 break; |
5513 | |
5514 case STATE_UNROT3R: | |
5515 /* Undo ROT3R: "312" -> "123" */ | |
5516 p = fword + sp->ts_fidx; | |
5517 if (has_mbyte) | |
5518 { | |
5519 c = mb_ptr2char(p); | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5520 tl = MB_PTR2LEN(p); |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5521 n = MB_PTR2LEN(p + tl); |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5522 n += MB_PTR2LEN(p + tl + n); |
625 | 5523 mch_memmove(p, p + tl, n); |
5524 mb_char2bytes(c, p + n); | |
5525 } | |
5526 else | |
5527 { | |
5528 c = *p; | |
5529 *p = p[1]; | |
5530 p[1] = p[2]; | |
5531 p[2] = c; | |
5532 } | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5533 /* FALLTHROUGH */ |
625 | 5534 |
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 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5545 PROF_STORE(sp->ts_state) |
625 | 5546 sp->ts_state = STATE_FINAL; |
323 | 5547 break; |
625 | 5548 } |
5549 | |
5550 /* 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]]; | |
5556 | |
5557 if (sp->ts_curi < 0) | |
5558 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5559 PROF_STORE(sp->ts_state) |
625 | 5560 sp->ts_state = STATE_FINAL; |
5561 break; | |
5562 } | |
5563 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5564 PROF_STORE(sp->ts_state) |
625 | 5565 sp->ts_state = STATE_REP; |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5566 /* FALLTHROUGH */ |
625 | 5567 |
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 | |
503 | 5577 gap = &lp->lp_replang->sl_rep; |
625 | 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) | |
5582 { | |
5583 /* past possible matching entries */ | |
5584 sp->ts_curi = gap->ga_len; | |
323 | 5585 break; |
5586 } | |
625 | 5587 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. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5597 PROF_STORE(sp->ts_state) |
625 | 5598 sp->ts_state = STATE_REP_UNDO; |
5599 | |
5600 /* Change the "from" to the "to" string. */ | |
5601 ++depth; | |
835 | 5602 fl = (int)STRLEN(ftp->ft_from); |
5603 tl = (int)STRLEN(ftp->ft_to); | |
625 | 5604 if (fl != tl) |
323 | 5605 { |
1619 | 5606 STRMOVE(p + tl, p + fl); |
625 | 5607 repextra += tl - fl; |
323 | 5608 } |
625 | 5609 mch_memmove(p, ftp->ft_to, tl); |
5610 stack[depth].ts_fidxtry = sp->ts_fidx + tl; | |
5611 stack[depth].ts_tcharlen = 0; | |
5612 break; | |
5613 } | |
5614 } | |
5615 | |
5616 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP) | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5617 { |
625 | 5618 /* No (more) matches. */ |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5619 PROF_STORE(sp->ts_state) |
625 | 5620 sp->ts_state = STATE_FINAL; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5621 } |
625 | 5622 |
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; | |
835 | 5632 fl = (int)STRLEN(ftp->ft_from); |
5633 tl = (int)STRLEN(ftp->ft_to); | |
625 | 5634 p = fword + sp->ts_fidx; |
5635 if (fl != tl) | |
5636 { | |
1619 | 5637 STRMOVE(p + fl, p + tl); |
625 | 5638 repextra -= tl - fl; |
5639 } | |
5640 mch_memmove(p, ftp->ft_from, fl); | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5641 PROF_STORE(sp->ts_state) |
625 | 5642 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; | |
5661 } | |
5662 } | |
5663 } | |
5664 } | |
5665 | |
5666 | |
5667 /* | |
5668 * Go one level deeper in the tree. | |
5669 */ | |
5670 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5671 go_deeper(trystate_T *stack, int depth, int score_add) |
323 | 5672 { |
330 | 5673 stack[depth + 1] = stack[depth]; |
323 | 5674 stack[depth + 1].ts_state = STATE_START; |
625 | 5675 stack[depth + 1].ts_score = stack[depth].ts_score + score_add; |
323 | 5676 stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
485 | 5677 stack[depth + 1].ts_flags = 0; |
323 | 5678 } |
5679 | |
455 | 5680 /* |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5685 nofold_len(char_u *fword, int flen, char_u *word) |
455 | 5686 { |
5687 char_u *p; | |
5688 int i = 0; | |
5689 | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5690 for (p = fword; p < fword + flen; MB_PTR_ADV(p)) |
455 | 5691 ++i; |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5692 for (p = word; i > 0; MB_PTR_ADV(p)) |
455 | 5693 --i; |
5694 return (int)(p - word); | |
5695 } | |
5696 | |
323 | 5697 /* |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5704 find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) |
323 | 5705 { |
5706 char_u uword[MAXWLEN]; /* "fword" in upper-case */ | |
5707 int depth; | |
324 | 5708 idx_T tryidx; |
323 | 5709 |
5710 /* The following arrays are used at each depth in the tree. */ | |
324 | 5711 idx_T arridx[MAXWLEN]; |
323 | 5712 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; | |
324 | 5721 idx_T lo, hi, m; |
323 | 5722 char_u *p; |
5723 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ | |
324 | 5724 idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
323 | 5725 |
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 */ | |
5774 if (has_mbyte) | |
5775 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5776 flen = MB_CPTR2LEN(fword + fwordidx[depth]); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5777 ulen = MB_CPTR2LEN(uword + uwordidx[depth]); |
323 | 5778 } |
5779 else | |
5780 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 /* | |
344 | 5854 * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
5855 * su->su_sga. | |
5856 */ | |
5857 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5858 score_comp_sal(suginfo_T *su) |
344 | 5859 { |
5860 langp_T *lp; | |
5861 char_u badsound[MAXWLEN]; | |
5862 int i; | |
5863 suggest_T *stp; | |
5864 suggest_T *sstp; | |
5865 int score; | |
500 | 5866 int lpi; |
344 | 5867 |
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. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5872 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5873 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5874 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
344 | 5875 if (lp->lp_slang->sl_sal.ga_len > 0) |
5876 { | |
5877 /* soundfold the bad word */ | |
375 | 5878 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
344 | 5879 |
5880 for (i = 0; i < su->su_ga.ga_len; ++i) | |
5881 { | |
5882 stp = &SUG(su->su_ga, i); | |
5883 | |
351 | 5884 /* 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); | |
344 | 5887 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 { | |
625 | 5894 sstp->st_wordlen = stp->st_wordlen; |
344 | 5895 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 } | |
500 | 5904 } |
344 | 5905 } |
5906 | |
5907 /* | |
5908 * Combine the list of suggestions in su->su_ga and su->su_sga. | |
4352 | 5909 * They are entwined. |
344 | 5910 */ |
5911 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5912 score_combine(suginfo_T *su) |
344 | 5913 { |
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]; | |
5922 int round; | |
500 | 5923 int lpi; |
625 | 5924 slang_T *slang = NULL; |
344 | 5925 |
5926 /* Add the alternate score to su_ga. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5927 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5928 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5929 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
344 | 5930 if (lp->lp_slang->sl_sal.ga_len > 0) |
5931 { | |
5932 /* soundfold the bad word */ | |
625 | 5933 slang = lp->lp_slang; |
5934 spell_soundfold(slang, su->su_fbadword, TRUE, badsound); | |
344 | 5935 |
5936 for (i = 0; i < su->su_ga.ga_len; ++i) | |
5937 { | |
5938 stp = &SUG(su->su_ga, i); | |
625 | 5939 stp->st_altscore = stp_sal_score(stp, su, slang, badsound); |
344 | 5940 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 | |
844 | 5951 if (slang == NULL) /* Using "double" without sound folding. */ |
5952 { | |
5953 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, | |
5954 su->su_maxcount); | |
625 | 5955 return; |
844 | 5956 } |
625 | 5957 |
344 | 5958 /* 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); | |
625 | 5962 stp->st_altscore = spell_edit_score(slang, |
5963 su->su_badword, stp->st_word); | |
344 | 5964 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 | |
625 | 5971 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount" |
5972 * for both lists. */ | |
5973 check_suggestions(su, &su->su_ga); | |
344 | 5974 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
625 | 5975 check_suggestions(su, &su->su_sga); |
344 | 5976 (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 /* | |
351 | 6020 * For the goodword in "stp" compute the soundalike score compared to the |
6021 * badword. | |
6022 */ | |
6023 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6024 stp_sal_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6025 suggest_T *stp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6026 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6027 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6028 char_u *badsound) /* sound-folded badword */ |
351 | 6029 { |
6030 char_u *p; | |
534 | 6031 char_u *pbad; |
6032 char_u *pgood; | |
351 | 6033 char_u badsound2[MAXWLEN]; |
6034 char_u fword[MAXWLEN]; | |
6035 char_u goodsound[MAXWLEN]; | |
534 | 6036 char_u goodword[MAXWLEN]; |
6037 int lendiff; | |
6038 | |
6039 lendiff = (int)(su->su_badlen - stp->st_orglen); | |
6040 if (lendiff >= 0) | |
6041 pbad = badsound; | |
351 | 6042 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. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6051 if (VIM_ISWHITE(su->su_badptr[su->su_badlen]) |
351 | 6052 && *skiptowhite(stp->st_word) == NUL) |
6053 for (p = fword; *(p = skiptowhite(p)) != NUL; ) | |
1619 | 6054 STRMOVE(p, p + 1); |
351 | 6055 |
375 | 6056 spell_soundfold(slang, fword, TRUE, badsound2); |
534 | 6057 pbad = badsound2; |
6058 } | |
6059 | |
2768 | 6060 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN) |
534 | 6061 { |
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); | |
625 | 6065 vim_strncpy(goodword + stp->st_wordlen, |
6066 su->su_badptr + su->su_badlen - lendiff, lendiff); | |
534 | 6067 pgood = goodword; |
6068 } | |
6069 else | |
6070 pgood = stp->st_word; | |
351 | 6071 |
375 | 6072 /* Sound-fold the word and compute the score for the difference. */ |
534 | 6073 spell_soundfold(slang, pgood, FALSE, goodsound); |
6074 | |
6075 return soundalike_score(goodsound, pbad); | |
351 | 6076 } |
6077 | |
625 | 6078 /* structure used to store soundfolded words that add_sound_suggest() has |
6079 * handled already. */ | |
6080 typedef struct | |
6081 { | |
6082 short sft_score; /* lowest score used */ | |
6083 char_u sft_word[1]; /* soundfolded word, actually longer */ | |
6084 } sftword_T; | |
6085 | |
6086 static 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6094 suggest_try_soundalike_prep(void) |
625 | 6095 { |
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. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6102 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6103 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6104 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
625 | 6105 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 | |
351 | 6112 /* |
323 | 6113 * Find suggestions by comparing the word in a sound-a-like form. |
503 | 6114 * Note: This doesn't support postponed prefixes. |
323 | 6115 */ |
6116 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6117 suggest_try_soundalike(suginfo_T *su) |
323 | 6118 { |
6119 char_u salword[MAXWLEN]; | |
6120 langp_T *lp; | |
500 | 6121 int lpi; |
503 | 6122 slang_T *slang; |
344 | 6123 |
625 | 6124 /* Do this for all languages that support sound folding and for which a |
6125 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6126 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6127 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6128 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
625 | 6129 slang = lp->lp_slang; |
6130 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) | |
6131 { | |
6132 /* soundfold the bad word */ | |
6133 spell_soundfold(slang, su->su_fbadword, TRUE, salword); | |
6134 | |
6135 /* try all kinds of inserts/deletes/swaps/etc. */ | |
6136 /* TODO: also soundfold the next words, so that we can try joining | |
6137 * and splitting */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6138 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6139 prof_init(); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6140 #endif |
625 | 6141 suggest_trie_walk(su, lp, salword, TRUE); |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6142 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6143 prof_report("soundalike"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6144 #endif |
625 | 6145 } |
6146 } | |
6147 } | |
6148 | |
6149 /* | |
6150 * Finish up after calling suggest_try_soundalike(). | |
6151 */ | |
6152 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6153 suggest_try_soundalike_finish(void) |
625 | 6154 { |
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. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6163 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6164 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6165 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
503 | 6166 slang = lp->lp_slang; |
625 | 6167 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) |
6168 { | |
6169 /* Free the info about handled words. */ | |
835 | 6170 todo = (int)slang->sl_sounddone.ht_used; |
625 | 6171 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi) |
6172 if (!HASHITEM_EMPTY(hi)) | |
6173 { | |
6174 vim_free(HI2SFT(hi)); | |
6175 --todo; | |
6176 } | |
1087 | 6177 |
6178 /* Clear the hashtable, it may also be used by another region. */ | |
625 | 6179 hash_clear(&slang->sl_sounddone); |
1087 | 6180 hash_init(&slang->sl_sounddone); |
625 | 6181 } |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6190 add_sound_suggest( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6191 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6192 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6193 int score, /* soundfold score */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6194 langp_T *lp) |
625 | 6195 { |
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 { | |
844 | 6225 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T) |
6226 + STRLEN(goodword))); | |
625 | 6227 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 { | |
10359
66f1b5bf3fa6
commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents:
10277
diff
changeset
|
6248 internal_error("add_sound_suggest()"); |
625 | 6249 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; | |
6268 wordcount = 0; | |
5506 | 6269 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen) |
625 | 6270 { |
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"); | |
5506 | 6283 wlen += 3; |
625 | 6284 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 | |
5506 | 6296 theword[wlen] = byts[n + i]; |
625 | 6297 n = idxs[n + i]; |
6298 } | |
6299 badword: | |
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 | |
698 | 6309 /* Skip words with the NOSUGGEST flag */ |
6310 if (flags & WF_NOSUGGEST) | |
6311 continue; | |
6312 | |
625 | 6313 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; | |
323 | 6327 } |
6328 else | |
625 | 6329 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 | |
4352 | 6351 * less common than "tath" -> "path". Don't do it when the |
625 | 6352 * letter is the same, that has already been counted. */ |
6353 gc = PTR2CHAR(p); | |
6354 if (SPELL_ISUPPER(gc)) | |
6355 { | |
6356 bc = PTR2CHAR(su->su_badword); | |
6357 if (!SPELL_ISUPPER(bc) | |
6358 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc)) | |
6359 goodscore += SCORE_ICASE / 2; | |
6360 } | |
6361 | |
6362 /* 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) | |
6378 { | |
6379 /* Give a bonus to words seen before. */ | |
6380 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE); | |
6381 | |
6382 /* 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); | |
6387 } | |
6388 } | |
6389 } | |
6390 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */ | |
6391 } | |
6392 } | |
6393 | |
6394 /* | |
6395 * Find word "word" in fold-case tree for "slang" and return the word number. | |
6396 */ | |
6397 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6398 soundfold_find(slang_T *slang, char_u *word) |
625 | 6399 { |
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; | |
323 | 6466 } |
6467 | |
6468 /* | |
324 | 6469 * Copy "fword" to "cword", fixing case according to "flags". |
323 | 6470 */ |
6471 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6472 make_case_word(char_u *fword, char_u *cword, int flags) |
323 | 6473 { |
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 */ | |
324 | 6479 onecap_copy(fword, cword, TRUE); |
323 | 6480 else |
6481 /* Use goodword as-is. */ | |
6482 STRCPY(cword, fword); | |
6483 } | |
6484 | |
330 | 6485 |
323 | 6486 /* |
6487 * Return TRUE if "c1" and "c2" are similar characters according to the MAP | |
6488 * lines in the .aff file. | |
6489 */ | |
6490 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6491 similar_chars(slang_T *slang, int c1, int c2) |
323 | 6492 { |
330 | 6493 int m1, m2; |
3549 | 6494 char_u buf[MB_MAXBYTES + 1]; |
330 | 6495 hashitem_T *hi; |
6496 | |
6497 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 | |
6507 m1 = slang->sl_map_array[c1]; | |
6508 if (m1 == 0) | |
323 | 6509 return FALSE; |
330 | 6510 |
6511 | |
6512 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 | |
6522 m2 = slang->sl_map_array[c2]; | |
6523 | |
6524 return m1 == m2; | |
323 | 6525 } |
6526 | |
6527 /* | |
6528 * Add a suggestion to the list of suggestions. | |
625 | 6529 * For a suggestion that is already in the list the lowest score is remembered. |
6530 */ | |
6531 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6532 add_suggestion( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6533 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6534 garray_T *gap, /* either su_ga or su_sga */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6535 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6536 int badlenarg, /* len of bad word replaced with "goodword" */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6537 int score, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6538 int altscore, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6539 int had_bonus, /* value for st_had_bonus */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6540 slang_T *slang, /* language for sound folding */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6541 int maxsf) /* su_maxscore applies to soundfold score, |
625 | 6542 su_sfmaxscore to the total score. */ |
6543 { | |
6544 int goodlen; /* len of goodword changed */ | |
6545 int badlen; /* len of bad word changed */ | |
323 | 6546 suggest_T *stp; |
534 | 6547 suggest_T new_sug; |
323 | 6548 int i; |
534 | 6549 char_u *pgood, *pbad; |
532 | 6550 |
534 | 6551 /* 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); | |
625 | 6554 pbad = su->su_badptr + badlenarg; |
6555 for (;;) | |
6556 { | |
835 | 6557 goodlen = (int)(pgood - goodword); |
6558 badlen = (int)(pbad - su->su_badptr); | |
625 | 6559 if (goodlen <= 0 || badlen <= 0) |
6560 break; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6561 MB_PTR_BACK(goodword, pgood); |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6562 MB_PTR_BACK(su->su_badptr, pbad); |
534 | 6563 if (has_mbyte) |
6564 { | |
6565 if (mb_ptr2char(pgood) != mb_ptr2char(pbad)) | |
6566 break; | |
346 | 6567 } |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
6568 else if (*pgood != *pbad) |
534 | 6569 break; |
625 | 6570 } |
6571 | |
534 | 6572 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; | |
346 | 6576 |
944 | 6577 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) | |
6589 { | |
6590 /* | |
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) | |
6601 { | |
6602 /* 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 } | |
6618 } | |
6619 | |
6620 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; | |
6627 } | |
6628 } | |
625 | 6629 |
6630 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 | |
6653 su->su_maxscore = cleanup_suggestions(gap, | |
6654 su->su_maxscore, SUG_CLEAN_COUNT(su)); | |
6655 } | |
6656 } | |
6657 } | |
6658 } | |
6659 | |
6660 /* | |
6661 * 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6665 check_suggestions( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6666 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6667 garray_T *gap) /* either su_ga or su_sga */ |
625 | 6668 { |
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". */ | |
2768 | 6679 vim_strncpy(longword, stp[i].st_word, MAXWLEN); |
625 | 6680 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 | |
323 | 6697 |
6698 /* | |
6699 * Add a word to be banned. | |
6700 */ | |
6701 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6702 add_banned( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6703 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6704 char_u *word) |
323 | 6705 { |
626 | 6706 char_u *s; |
323 | 6707 hash_T hash; |
6708 hashitem_T *hi; | |
6709 | |
625 | 6710 hash = hash_hash(word); |
6711 hi = hash_lookup(&su->su_banned, word, hash); | |
6712 if (HASHITEM_EMPTY(hi)) | |
6713 { | |
6714 s = vim_strsave(word); | |
6715 if (s != NULL) | |
323 | 6716 hash_add_item(&su->su_banned, hi, s, hash); |
625 | 6717 } |
323 | 6718 } |
6719 | |
324 | 6720 /* |
534 | 6721 * Recompute the score for all suggestions if sound-folding is possible. This |
6722 * is slow, thus only done for the final results. | |
324 | 6723 */ |
6724 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6725 rescore_suggestions(suginfo_T *su) |
324 | 6726 { |
6727 int i; | |
534 | 6728 |
6729 if (su->su_sallang != NULL) | |
503 | 6730 for (i = 0; i < su->su_ga.ga_len; ++i) |
534 | 6731 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6738 rescore_one(suginfo_T *su, suggest_T *stp) |
534 | 6739 { |
6740 slang_T *slang = stp->st_slang; | |
6741 char_u sal_badword[MAXWLEN]; | |
535 | 6742 char_u *p; |
534 | 6743 |
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) | |
535 | 6749 p = su->su_sal_badword; |
534 | 6750 else |
6751 { | |
6752 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword); | |
535 | 6753 p = sal_badword; |
6754 } | |
6755 | |
6756 stp->st_altscore = stp_sal_score(stp, su, slang, p); | |
534 | 6757 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; | |
324 | 6761 } |
6762 } | |
6763 | |
323 | 6764 static int |
6765 #ifdef __BORLANDC__ | |
6766 _RTLENTRYF | |
6767 #endif | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
6768 sug_compare(const void *s1, const void *s2); |
323 | 6769 |
6770 /* | |
6771 * Function given to qsort() to sort the suggestions on st_score. | |
529 | 6772 * First on "st_score", then "st_altscore" then alphabetically. |
323 | 6773 */ |
6774 static int | |
6775 #ifdef __BORLANDC__ | |
6776 _RTLENTRYF | |
6777 #endif | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6778 sug_compare(const void *s1, const void *s2) |
323 | 6779 { |
6780 suggest_T *p1 = (suggest_T *)s1; | |
6781 suggest_T *p2 = (suggest_T *)s2; | |
344 | 6782 int n = p1->st_score - p2->st_score; |
6783 | |
6784 if (n == 0) | |
529 | 6785 { |
6786 n = p1->st_altscore - p2->st_altscore; | |
6787 if (n == 0) | |
6788 n = STRICMP(p1->st_word, p2->st_word); | |
6789 } | |
344 | 6790 return n; |
323 | 6791 } |
6792 | |
6793 /* | |
6794 * Cleanup the suggestions: | |
6795 * - Sort on score. | |
6796 * - Remove words that won't be displayed. | |
344 | 6797 * Returns the maximum score in the list or "maxscore" unmodified. |
323 | 6798 */ |
344 | 6799 static int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6800 cleanup_suggestions( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6801 garray_T *gap, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6802 int maxscore, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6803 int keep) /* nr of suggestions to keep */ |
323 | 6804 { |
344 | 6805 suggest_T *stp = &SUG(*gap, 0); |
323 | 6806 int i; |
6807 | |
6808 /* Sort the list. */ | |
344 | 6809 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare); |
323 | 6810 |
6811 /* Truncate the list to the number of suggestions that will be displayed. */ | |
344 | 6812 if (gap->ga_len > keep) |
323 | 6813 { |
344 | 6814 for (i = keep; i < gap->ga_len; ++i) |
323 | 6815 vim_free(stp[i].st_word); |
344 | 6816 gap->ga_len = keep; |
6817 return stp[keep - 1].st_score; | |
323 | 6818 } |
344 | 6819 return maxscore; |
323 | 6820 } |
6821 | |
372 | 6822 #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 * | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6828 eval_soundfold(char_u *word) |
372 | 6829 { |
6830 langp_T *lp; | |
6831 char_u sound[MAXWLEN]; | |
500 | 6832 int lpi; |
372 | 6833 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6834 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
372 | 6835 /* Use the sound-folding of the first language that supports it. */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6836 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6837 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6838 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
372 | 6839 if (lp->lp_slang->sl_sal.ga_len > 0) |
6840 { | |
6841 /* soundfold the word */ | |
375 | 6842 spell_soundfold(lp->lp_slang, word, FALSE, sound); |
372 | 6843 return vim_strsave(sound); |
6844 } | |
500 | 6845 } |
372 | 6846 |
6847 /* No language with sound folding, return word as-is. */ | |
6848 return vim_strsave(word); | |
6849 } | |
6850 #endif | |
6851 | |
323 | 6852 /* |
6853 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". | |
485 | 6854 * |
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). | |
323 | 6863 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
6864 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6865 spell_soundfold( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6866 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6867 char_u *inword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6868 int folded, /* "inword" is already case-folded */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6869 char_u *res) |
375 | 6870 { |
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 { | |
835 | 6884 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN); |
375 | 6885 word = fword; |
6886 } | |
6887 | |
6888 if (has_mbyte) | |
6889 spell_soundfold_wsal(slang, word, res); | |
6890 else | |
6891 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6900 spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res) |
375 | 6901 { |
6902 char_u *s; | |
6903 int ri = 0; | |
6904 int c; | |
6905 | |
6906 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 { | |
474 | 6915 c = mb_cptr2char_adv(&s); |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6916 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) |
375 | 6917 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 | |
6952 { | |
6953 /* The sl_sal_first[] table contains the translation. */ | |
6954 for (s = inword; (c = *s) != NUL; ++s) | |
6955 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6956 if (VIM_ISWHITE(c)) |
375 | 6957 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6969 spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res) |
323 | 6970 { |
344 | 6971 salitem_T *smp; |
323 | 6972 char_u word[MAXWLEN]; |
375 | 6973 char_u *s = inword; |
323 | 6974 char_u *t; |
344 | 6975 char_u *pf; |
323 | 6976 int i, j, z; |
344 | 6977 int reslen; |
323 | 6978 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 | |
324 | 6987 /* Remove accents, if wanted. We actually remove all non-word characters. |
375 | 6988 * But keep white space. We need a copy, the word may be changed here. */ |
323 | 6989 if (slang->sl_rem_accents) |
6990 { | |
6991 t = word; | |
375 | 6992 while (*s != NUL) |
323 | 6993 { |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6994 if (VIM_ISWHITE(*s)) |
344 | 6995 { |
6996 *t++ = ' '; | |
6997 s = skipwhite(s); | |
6998 } | |
323 | 6999 else |
7000 { | |
5477 | 7001 if (spell_iswordp_nmw(s, curwin)) |
323 | 7002 *t++ = *s; |
7003 ++s; | |
7004 } | |
7005 } | |
7006 *t = NUL; | |
7007 } | |
7008 else | |
2768 | 7009 vim_strncpy(word, s, MAXWLEN - 1); |
323 | 7010 |
344 | 7011 smp = (salitem_T *)slang->sl_sal.ga_data; |
323 | 7012 |
7013 /* | |
7014 * This comes from Aspell phonet.cpp. Converted from C++ to C. | |
324 | 7015 * Changed to keep spaces. |
323 | 7016 */ |
344 | 7017 i = reslen = z = 0; |
323 | 7018 while ((c = word[i]) != NUL) |
7019 { | |
344 | 7020 /* Start with the first rule that has the character in the word. */ |
323 | 7021 n = slang->sl_sal_first[c]; |
7022 z0 = 0; | |
7023 | |
7024 if (n >= 0) | |
7025 { | |
7026 /* check all rules for the same letter */ | |
344 | 7027 for (; (s = smp[n].sm_lead)[0] == c; ++n) |
323 | 7028 { |
344 | 7029 /* 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) | |
323 | 7033 { |
344 | 7034 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 } | |
323 | 7044 } |
7045 | |
375 | 7046 if ((pf = smp[n].sm_oneof) != NULL) |
323 | 7047 { |
375 | 7048 /* Check for match with one of the chars in "sm_oneof". */ |
344 | 7049 while (*pf != NUL && *pf != word[i + k]) |
7050 ++pf; | |
7051 if (*pf == NUL) | |
7052 continue; | |
7053 ++k; | |
323 | 7054 } |
344 | 7055 s = smp[n].sm_rules; |
7056 pri = 5; /* default priority */ | |
323 | 7057 |
7058 p0 = *s; | |
7059 k0 = k; | |
7060 while (*s == '-' && k > 1) | |
7061 { | |
7062 k--; | |
7063 s++; | |
7064 } | |
7065 if (*s == '<') | |
7066 s++; | |
344 | 7067 if (VIM_ISDIGIT(*s)) |
323 | 7068 { |
7069 /* determine priority */ | |
7070 pri = *s - '0'; | |
7071 s++; | |
7072 } | |
7073 if (*s == '^' && *(s + 1) == '^') | |
7074 s++; | |
7075 | |
7076 if (*s == NUL | |
7077 || (*s == '^' | |
324 | 7078 && (i == 0 || !(word[i - 1] == ' ' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7079 || spell_iswordp(word + i - 1, curwin))) |
323 | 7080 && (*(s + 1) != '$' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7081 || (!spell_iswordp(word + i + k0, curwin)))) |
323 | 7082 || (*s == '$' && i > 0 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7083 && spell_iswordp(word + i - 1, curwin) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7084 && (!spell_iswordp(word + i + k0, curwin)))) |
323 | 7085 { |
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 | |
344 | 7092 && p0 != '-' && word[i + k] != NUL) |
323 | 7093 { |
7094 /* test follow-up rule for "word[i + k]" */ | |
344 | 7095 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
323 | 7096 { |
344 | 7097 /* Quickly skip entries that don't match the word. |
7098 * */ | |
7099 k0 = smp[n0].sm_leadlen; | |
7100 if (k0 > 1) | |
323 | 7101 { |
344 | 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 } | |
323 | 7113 } |
344 | 7114 k0 += k - 1; |
7115 | |
375 | 7116 if ((pf = smp[n0].sm_oneof) != NULL) |
323 | 7117 { |
344 | 7118 /* Check for match with one of the chars in |
375 | 7119 * "sm_oneof". */ |
344 | 7120 while (*pf != NUL && *pf != word[i + k0]) |
7121 ++pf; | |
7122 if (*pf == NUL) | |
7123 continue; | |
7124 ++k0; | |
323 | 7125 } |
344 | 7126 |
7127 p0 = 5; | |
7128 s = smp[n0].sm_rules; | |
323 | 7129 while (*s == '-') |
7130 { | |
344 | 7131 /* "k0" gets NOT reduced because |
7132 * "if (k0 == k)" */ | |
323 | 7133 s++; |
7134 } | |
7135 if (*s == '<') | |
7136 s++; | |
344 | 7137 if (VIM_ISDIGIT(*s)) |
323 | 7138 { |
7139 p0 = *s - '0'; | |
7140 s++; | |
7141 } | |
7142 | |
7143 if (*s == NUL | |
7144 /* *s == '^' cuts */ | |
7145 || (*s == '$' | |
376 | 7146 && !spell_iswordp(word + i + k0, |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7147 curwin))) |
323 | 7148 { |
7149 if (k0 == k) | |
7150 /* this is just a piece of the string */ | |
7151 continue; | |
7152 | |
7153 if (p0 < pri) | |
7154 /* priority too low */ | |
7155 continue; | |
7156 /* rule fits; stop search */ | |
7157 break; | |
7158 } | |
7159 } | |
7160 | |
344 | 7161 if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
323 | 7162 continue; |
7163 } | |
7164 | |
7165 /* replace string */ | |
344 | 7166 s = smp[n].sm_to; |
389 | 7167 if (s == NULL) |
7168 s = (char_u *)""; | |
344 | 7169 pf = smp[n].sm_rules; |
7170 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; | |
323 | 7171 if (p0 == 1 && z == 0) |
7172 { | |
7173 /* rule with '<' is used */ | |
344 | 7174 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
7175 || res[reslen - 1] == *s)) | |
7176 reslen--; | |
323 | 7177 z0 = 1; |
7178 z = 1; | |
7179 k0 = 0; | |
372 | 7180 while (*s != NUL && word[i + k0] != NUL) |
323 | 7181 { |
7182 word[i + k0] = *s; | |
7183 k0++; | |
7184 s++; | |
7185 } | |
7186 if (k > k0) | |
1619 | 7187 STRMOVE(word + i + k0, word + i + k); |
323 | 7188 |
7189 /* new "actual letter" */ | |
7190 c = word[i]; | |
7191 } | |
7192 else | |
7193 { | |
7194 /* no '<' rule used */ | |
7195 i += k - 1; | |
7196 z = 0; | |
344 | 7197 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
323 | 7198 { |
344 | 7199 if (reslen == 0 || res[reslen - 1] != *s) |
372 | 7200 res[reslen++] = *s; |
323 | 7201 s++; |
7202 } | |
7203 /* new "actual letter" */ | |
7204 c = *s; | |
344 | 7205 if (strstr((char *)pf, "^^") != NULL) |
323 | 7206 { |
7207 if (c != NUL) | |
372 | 7208 res[reslen++] = c; |
1619 | 7209 STRMOVE(word, word + i + 1); |
323 | 7210 i = 0; |
7211 z0 = 1; | |
7212 } | |
7213 } | |
7214 break; | |
7215 } | |
7216 } | |
7217 } | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7218 else if (VIM_ISWHITE(c)) |
324 | 7219 { |
7220 c = ' '; | |
7221 k = 1; | |
7222 } | |
323 | 7223 |
7224 if (z0 == 0) | |
7225 { | |
344 | 7226 if (k && !p0 && reslen < MAXWLEN && c != NUL |
7227 && (!slang->sl_collapse || reslen == 0 | |
7228 || res[reslen - 1] != c)) | |
323 | 7229 /* condense only double letters */ |
372 | 7230 res[reslen++] = c; |
323 | 7231 |
7232 i++; | |
7233 z = 0; | |
7234 k = 0; | |
7235 } | |
7236 } | |
7237 | |
344 | 7238 res[reslen] = NUL; |
323 | 7239 } |
7240 | |
372 | 7241 /* |
7242 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". | |
7243 * Multi-byte version of spell_soundfold(). | |
7244 */ | |
7245 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7246 spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) |
372 | 7247 { |
375 | 7248 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
372 | 7249 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; | |
3520 | 7267 int wordlen; |
7268 | |
372 | 7269 |
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 */ | |
3520 | 7275 wordlen = 0; |
372 | 7276 for (s = inword; *s != NUL; ) |
7277 { | |
7278 t = s; | |
474 | 7279 c = mb_cptr2char_adv(&s); |
372 | 7280 if (slang->sl_rem_accents) |
7281 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7282 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) |
372 | 7283 { |
7284 if (did_white) | |
7285 continue; | |
7286 c = ' '; | |
7287 did_white = TRUE; | |
7288 } | |
7289 else | |
7290 { | |
7291 did_white = FALSE; | |
5477 | 7292 if (!spell_iswordp_nmw(t, curwin)) |
372 | 7293 continue; |
7294 } | |
7295 } | |
3520 | 7296 word[wordlen++] = c; |
7297 } | |
7298 word[wordlen] = NUL; | |
372 | 7299 |
7300 /* | |
3520 | 7301 * This algorithm comes from Aspell phonet.cpp. |
372 | 7302 * 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 { | |
2455
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7314 /* Check all rules for the same index byte. |
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7315 * If c is 0x300 need extra check for the end of the array, as |
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7316 * (c & 0xff) is NUL. */ |
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7317 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff) |
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7318 && ws[0] != NUL; ++n) |
372 | 7319 { |
7320 /* Quickly skip entries that don't match the word. Most | |
7321 * entries are less then three chars, optimize for that. */ | |
375 | 7322 if (c != ws[0]) |
7323 continue; | |
372 | 7324 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 | |
375 | 7339 if ((pf = smp[n].sm_oneof_w) != NULL) |
372 | 7340 { |
375 | 7341 /* Check for match with one of the chars in "sm_oneof". */ |
372 | 7342 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] == ' ' | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7372 || spell_iswordp_w(word + i - 1, curwin))) |
372 | 7373 && (*(s + 1) != '$' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7374 || (!spell_iswordp_w(word + i + k0, curwin)))) |
372 | 7375 || (*s == '$' && i > 0 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7376 && spell_iswordp_w(word + i - 1, curwin) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7377 && (!spell_iswordp_w(word + i + k0, curwin)))) |
372 | 7378 { |
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 { | |
375 | 7387 /* Test follow-up rule for "word[i + k]"; loop over |
7388 * all entries with the same index byte. */ | |
372 | 7389 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
7390 == (c0 & 0xff); ++n0) | |
7391 { | |
7392 /* Quickly skip entries that don't match the word. | |
375 | 7393 */ |
7394 if (c0 != ws[0]) | |
7395 continue; | |
372 | 7396 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 | |
375 | 7413 if ((pf = smp[n0].sm_oneof_w) != NULL) |
372 | 7414 { |
7415 /* Check for match with one of the chars in | |
375 | 7416 * "sm_oneof". */ |
372 | 7417 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 == '$' | |
376 | 7443 && !spell_iswordp_w(word + i + k0, |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7444 curwin))) |
372 | 7445 { |
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 */ | |
389 | 7470 if (reslen > 0 && ws != NULL && *ws != NUL |
7471 && (wres[reslen - 1] == c | |
372 | 7472 || wres[reslen - 1] == *ws)) |
7473 reslen--; | |
7474 z0 = 1; | |
7475 z = 1; | |
7476 k0 = 0; | |
389 | 7477 if (ws != NULL) |
7478 while (*ws != NUL && word[i + k0] != NUL) | |
7479 { | |
7480 word[i + k0] = *ws; | |
7481 k0++; | |
7482 ws++; | |
7483 } | |
372 | 7484 if (k > k0) |
7485 mch_memmove(word + i + k0, word + i + k, | |
3520 | 7486 sizeof(int) * (wordlen - (i + k) + 1)); |
372 | 7487 |
7488 /* new "actual letter" */ | |
7489 c = word[i]; | |
7490 } | |
7491 else | |
7492 { | |
7493 /* no '<' rule used */ | |
7494 i += k - 1; | |
7495 z = 0; | |
389 | 7496 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 } | |
372 | 7504 /* new "actual letter" */ |
389 | 7505 if (ws == NULL) |
7506 c = NUL; | |
7507 else | |
7508 c = *ws; | |
372 | 7509 if (strstr((char *)s, "^^") != NULL) |
7510 { | |
7511 if (c != NUL) | |
7512 wres[reslen++] = c; | |
7513 mch_memmove(word, word + i + 1, | |
3520 | 7514 sizeof(int) * (wordlen - (i + 1) + 1)); |
372 | 7515 i = 0; |
7516 z0 = 1; | |
7517 } | |
7518 } | |
7519 break; | |
7520 } | |
7521 } | |
7522 } | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7523 else if (VIM_ISWHITE(c)) |
372 | 7524 { |
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 } | |
7553 | |
324 | 7554 /* |
344 | 7555 * 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7561 soundalike_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7562 char_u *goodstart, /* sound-folded good word */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7563 char_u *badstart) /* sound-folded bad word */ |
344 | 7564 { |
351 | 7565 char_u *goodsound = goodstart; |
7566 char_u *badsound = badstart; | |
7567 int goodlen; | |
7568 int badlen; | |
344 | 7569 int n; |
7570 char_u *pl, *ps; | |
7571 char_u *pl2, *ps2; | |
351 | 7572 int score = 0; |
7573 | |
2454
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7574 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be |
351 | 7575 * counted so much, vowels halfway the word aren't counted at all. */ |
7576 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) | |
7577 { | |
2454
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7578 if ((badsound[0] == NUL && goodsound[1] == NUL) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7579 || (goodsound[0] == NUL && badsound[1] == NUL)) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7580 /* changing word with vowel to word without a sound */ |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7581 return SCORE_DEL; |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7582 if (badsound[0] == NUL || goodsound[0] == NUL) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7583 /* more than two changes */ |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7584 return SCORE_MAXMAX; |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7585 |
625 | 7586 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 } | |
351 | 7593 else |
625 | 7594 { |
7595 score = 2 * SCORE_DEL / 3; | |
7596 if (*badsound == '*') | |
7597 ++badsound; | |
7598 else | |
7599 ++goodsound; | |
7600 } | |
351 | 7601 } |
7602 | |
835 | 7603 goodlen = (int)STRLEN(goodsound); |
7604 badlen = (int)STRLEN(badsound); | |
344 | 7605 |
1188 | 7606 /* Return quickly if the lengths are too different to be fixed by two |
344 | 7607 * changes. */ |
7608 n = goodlen - badlen; | |
7609 if (n < -2 || n > 2) | |
7610 return SCORE_MAXMAX; | |
7611 | |
7612 if (n > 0) | |
7613 { | |
351 | 7614 pl = goodsound; /* goodsound is longest */ |
344 | 7615 ps = badsound; |
7616 } | |
7617 else | |
7618 { | |
351 | 7619 pl = badsound; /* badsound is longest */ |
344 | 7620 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) | |
351 | 7645 return score + SCORE_DEL * 2; |
344 | 7646 |
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 */ | |
351 | 7662 return score + SCORE_DEL; |
344 | 7663 ++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) | |
351 | 7670 return score + SCORE_DEL + SCORE_SWAP; |
344 | 7671 |
7672 /* 3: delete then substitute, then the rest must be equal */ | |
7673 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7674 return score + SCORE_DEL + SCORE_SUBST; |
344 | 7675 |
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) | |
351 | 7688 return score + SCORE_SWAP + SCORE_DEL; |
344 | 7689 } |
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) | |
351 | 7701 return score + SCORE_SUBST + SCORE_DEL; |
344 | 7702 |
7703 /* Failed to compare. */ | |
7704 break; | |
7705 | |
7706 case 0: | |
7707 /* | |
1808 | 7708 * Lengths are equal, thus changes must result in same length: An |
344 | 7709 * insert is only possible in combination with a delete. |
7710 * 1: check if for identical strings | |
7711 */ | |
7712 if (*pl == NUL) | |
351 | 7713 return score; |
344 | 7714 |
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 */ | |
351 | 7723 return score + SCORE_SWAP; |
344 | 7724 ++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) | |
351 | 7730 return score + SCORE_SWAP + SCORE_SWAP; |
344 | 7731 |
7732 /* 4: swap and substitute */ | |
7733 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7734 return score + SCORE_SWAP + SCORE_SUBST; |
344 | 7735 } |
7736 | |
7737 /* 5: substitute */ | |
7738 pl2 = pl + 1; | |
7739 ps2 = ps + 1; | |
7740 while (*pl2 == *ps2) | |
7741 { | |
7742 if (*pl2 == NUL) /* reached the end */ | |
351 | 7743 return score + SCORE_SUBST; |
344 | 7744 ++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) | |
351 | 7751 return score + SCORE_SUBST + SCORE_SWAP; |
344 | 7752 |
7753 /* 7: substitute and substitute */ | |
7754 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7755 return score + SCORE_SUBST + SCORE_SUBST; |
344 | 7756 |
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) | |
351 | 7766 return score + SCORE_INS + SCORE_DEL; |
344 | 7767 |
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) | |
351 | 7777 return score + SCORE_INS + SCORE_DEL; |
344 | 7778 |
7779 /* Failed to compare. */ | |
7780 break; | |
7781 } | |
7782 | |
7783 return SCORE_MAXMAX; | |
7784 } | |
324 | 7785 |
323 | 7786 /* |
7787 * Compute the "edit distance" to turn "badword" into "goodword". The less | |
344 | 7788 * deletes/inserts/substitutes/swaps are required the lower the score. |
324 | 7789 * |
485 | 7790 * 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. | |
323 | 7794 */ |
7795 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7796 spell_edit_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7797 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7798 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7799 char_u *goodword) |
323 | 7800 { |
7801 int *cnt; | |
1188 | 7802 int badlen, goodlen; /* lengths including NUL */ |
323 | 7803 int j, i; |
7804 int t; | |
7805 int bc, gc; | |
324 | 7806 int pbc, pgc; |
7807 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; ) | |
474 | 7816 wbadword[badlen++] = mb_cptr2char_adv(&p); |
398 | 7817 wbadword[badlen++] = 0; |
324 | 7818 for (p = goodword, goodlen = 0; *p != NUL; ) |
474 | 7819 wgoodword[goodlen++] = mb_cptr2char_adv(&p); |
398 | 7820 wgoodword[goodlen++] = 0; |
324 | 7821 } |
7822 else | |
7823 { | |
835 | 7824 badlen = (int)STRLEN(badword) + 1; |
7825 goodlen = (int)STRLEN(goodword) + 1; | |
324 | 7826 } |
323 | 7827 |
7828 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ | |
7829 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] | |
7830 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), | |
7831 TRUE); | |
324 | 7832 if (cnt == NULL) |
7833 return 0; /* out of memory */ | |
323 | 7834 |
7835 CNT(0, 0) = 0; | |
7836 for (j = 1; j <= goodlen; ++j) | |
625 | 7837 CNT(0, j) = CNT(0, j - 1) + SCORE_INS; |
323 | 7838 |
7839 for (i = 1; i <= badlen; ++i) | |
7840 { | |
625 | 7841 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL; |
323 | 7842 for (j = 1; j <= goodlen; ++j) |
7843 { | |
324 | 7844 if (has_mbyte) |
7845 { | |
7846 bc = wbadword[i - 1]; | |
7847 gc = wgoodword[j - 1]; | |
7848 } | |
7849 else | |
7850 { | |
7851 bc = badword[i - 1]; | |
7852 gc = goodword[j - 1]; | |
7853 } | |
323 | 7854 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. */ | |
324 | 7859 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
323 | 7860 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
7861 else | |
625 | 7862 { |
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 } | |
323 | 7871 |
324 | 7872 if (i > 1 && j > 1) |
323 | 7873 { |
324 | 7874 if (has_mbyte) |
7875 { | |
7876 pbc = wbadword[i - 2]; | |
7877 pgc = wgoodword[j - 2]; | |
7878 } | |
7879 else | |
7880 { | |
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 } | |
323 | 7890 } |
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 } | |
344 | 7900 |
7901 i = CNT(badlen - 1, goodlen - 1); | |
7902 vim_free(cnt); | |
7903 return i; | |
323 | 7904 } |
307 | 7905 |
625 | 7906 typedef 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7922 spell_edit_score_limit( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7923 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7924 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7925 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7926 int limit) |
625 | 7927 { |
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 | |
7938 /* 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); | |
7942 | |
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 } | |
8074 pop: | |
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 | |
8096 /* | |
8097 * Multi-byte version of spell_edit_score_limit(). | |
8098 * Keep it in sync with the above! | |
8099 */ | |
8100 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8101 spell_edit_score_limit_w( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8102 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8103 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8104 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8105 int limit) |
625 | 8106 { |
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 } | |
8263 pop: | |
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 } | |
8284 | |
714 | 8285 /* |
8286 * ":spellinfo" | |
8287 */ | |
8288 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8289 ex_spellinfo(exarg_T *eap UNUSED) |
714 | 8290 { |
8291 int lpi; | |
8292 langp_T *lp; | |
8293 char_u *p; | |
8294 | |
8295 if (no_spell_checking(curwin)) | |
8296 return; | |
8297 | |
8298 msg_start(); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8299 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8300 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8301 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8302 msg_puts("file: "); |
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8303 msg_puts((char *)lp->lp_slang->sl_fname); |
714 | 8304 msg_putchar('\n'); |
8305 p = lp->lp_slang->sl_info; | |
8306 if (p != NULL) | |
8307 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8308 msg_puts((char *)p); |
714 | 8309 msg_putchar('\n'); |
8310 } | |
8311 } | |
8312 msg_end(); | |
8313 } | |
8314 | |
625 | 8315 #define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */ |
8316 #define DUMPFLAG_COUNT 2 /* include word count */ | |
701 | 8317 #define DUMPFLAG_ICASE 4 /* ignore case when finding matches */ |
710 | 8318 #define DUMPFLAG_ONECAP 8 /* pattern starts with capital */ |
8319 #define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */ | |
625 | 8320 |
351 | 8321 /* |
8322 * ":spelldump" | |
8323 */ | |
8324 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8325 ex_spelldump(exarg_T *eap) |
351 | 8326 { |
5382 | 8327 char_u *spl; |
8328 long dummy; | |
8329 | |
701 | 8330 if (no_spell_checking(curwin)) |
8331 return; | |
5382 | 8332 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL); |
8333 | |
8334 /* Create a new empty buffer in a new window. */ | |
701 | 8335 do_cmdline_cmd((char_u *)"new"); |
5382 | 8336 |
8337 /* enable spelling locally in the new window */ | |
8338 set_option_value((char_u*)"spell", TRUE, (char_u*)"", OPT_LOCAL); | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
8339 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); |
5382 | 8340 vim_free(spl); |
8341 | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10950
diff
changeset
|
8342 if (!BUFEMPTY()) |
701 | 8343 return; |
8344 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8345 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); |
701 | 8346 |
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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8361 spell_dump_compl( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8362 char_u *pat, /* leading part of the word */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8363 int ic, /* ignore case */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8364 int *dir, /* direction for adding matches */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8365 int dumpflags_arg) /* DUMPFLAG_* */ |
701 | 8366 { |
351 | 8367 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; | |
381 | 8380 char_u *region_names = NULL; /* region names being used */ |
8381 int do_region = TRUE; /* dump region names and numbers */ | |
8382 char_u *p; | |
500 | 8383 int lpi; |
701 | 8384 int dumpflags = dumpflags_arg; |
8385 int patlen; | |
8386 | |
710 | 8387 /* 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; | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
8398 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat)) |
710 | 8399 dumpflags |= DUMPFLAG_ALLCAP; |
8400 } | |
8401 } | |
351 | 8402 |
381 | 8403 /* Find out if we can support regions: All languages must support the same |
8404 * regions or none at all. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8405 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8406 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8407 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
381 | 8408 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 { | |
701 | 8423 if (pat == NULL) |
8424 { | |
8425 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); | |
8426 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); | |
8427 } | |
381 | 8428 } |
8429 else | |
8430 do_region = FALSE; | |
8431 | |
8432 /* | |
8433 * Loop over all files loaded for the entries in 'spelllang'. | |
8434 */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8435 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8436 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8437 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
351 | 8438 slang = lp->lp_slang; |
500 | 8439 if (slang->sl_fbyts == NULL) /* reloading failed */ |
8440 continue; | |
351 | 8441 |
701 | 8442 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) | |
835 | 8451 patlen = (int)STRLEN(pat); |
701 | 8452 else |
840 | 8453 patlen = -1; |
351 | 8454 |
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 { | |
701 | 8461 dumpflags &= ~DUMPFLAG_KEEPCASE; |
351 | 8462 byts = slang->sl_fbyts; |
8463 idxs = slang->sl_fidxs; | |
8464 } | |
8465 else | |
8466 { | |
701 | 8467 dumpflags |= DUMPFLAG_KEEPCASE; |
351 | 8468 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; | |
701 | 8477 while (depth >= 0 && !got_int |
8478 && (pat == NULL || !compl_interrupted)) | |
351 | 8479 { |
8480 if (curi[depth] > byts[arridx[depth]]) | |
8481 { | |
8482 /* Done all bytes at this node, go up one level. */ | |
8483 --depth; | |
8484 line_breakcheck(); | |
10277
154d5a2e7395
commit https://github.com/vim/vim/commit/472e85970ee3a80abd824bef510df12e9cfe9e96
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
8485 ins_compl_check_keys(50, FALSE); |
351 | 8486 } |
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) | |
500 | 8501 && (flags & WF_NEEDCOMP) == 0 |
381 | 8502 && (do_region |
8503 || (flags & WF_REGION) == 0 | |
390 | 8504 || (((unsigned)flags >> 16) |
351 | 8505 & lp->lp_region) != 0)) |
8506 { | |
8507 word[depth] = NUL; | |
381 | 8508 if (!do_region) |
8509 flags &= ~WF_REGION; | |
355 | 8510 |
8511 /* Dump the basic word if there is no prefix or | |
8512 * when it's the first one. */ | |
390 | 8513 c = (unsigned)flags >> 24; |
355 | 8514 if (c == 0 || curi[depth] == 2) |
701 | 8515 { |
8516 dump_word(slang, word, pat, dir, | |
8517 dumpflags, flags, lnum); | |
8518 if (pat == NULL) | |
8519 ++lnum; | |
8520 } | |
351 | 8521 |
8522 /* Apply the prefix, if there is one. */ | |
355 | 8523 if (c != 0) |
701 | 8524 lnum = dump_prefixes(slang, word, pat, dir, |
8525 dumpflags, flags, lnum); | |
351 | 8526 } |
8527 } | |
8528 else | |
8529 { | |
8530 /* Normal char, go one level deeper. */ | |
8531 word[depth++] = c; | |
8532 arridx[depth] = idxs[n]; | |
8533 curi[depth] = 1; | |
701 | 8534 |
8535 /* Check if this characters matches with the pattern. | |
8536 * If not skip the whole tree below it. | |
710 | 8537 * 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) | |
701 | 8543 --depth; |
351 | 8544 } |
8545 } | |
8546 } | |
8547 } | |
8548 } | |
8549 } | |
8550 | |
8551 /* | |
8552 * Dump one word: apply case modifications and append a line to the buffer. | |
701 | 8553 * When "lnum" is zero add insert mode completion. |
8554 */ | |
8555 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8556 dump_word( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8557 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8558 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8559 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8560 int *dir, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8561 int dumpflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8562 int wordflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8563 linenr_T lnum) |
351 | 8564 { |
8565 int keepcap = FALSE; | |
8566 char_u *p; | |
625 | 8567 char_u *tw; |
351 | 8568 char_u cword[MAXWLEN]; |
381 | 8569 char_u badword[MAXWLEN + 10]; |
8570 int i; | |
710 | 8571 int flags = wordflags; |
8572 | |
8573 if (dumpflags & DUMPFLAG_ONECAP) | |
8574 flags |= WF_ONECAP; | |
8575 if (dumpflags & DUMPFLAG_ALLCAP) | |
8576 flags |= WF_ALLCAP; | |
351 | 8577 |
625 | 8578 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0) |
351 | 8579 { |
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; | |
625 | 8587 if ((dumpflags & DUMPFLAG_KEEPCASE) |
8588 && ((captype(word, NULL) & WF_KEEPCAP) == 0 | |
389 | 8589 || (flags & WF_FIXCAP) != 0)) |
351 | 8590 keepcap = TRUE; |
8591 } | |
625 | 8592 tw = p; |
351 | 8593 |
701 | 8594 if (pat == NULL) |
8595 { | |
8596 /* Add flags and regions after a slash. */ | |
8597 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) | |
8598 { | |
8599 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; | |
8612 } | |
8613 | |
8614 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", | |
625 | 8623 tw, HI2WC(hi)->wc_count); |
701 | 8624 p = IObuff; |
8625 } | |
8626 } | |
8627 | |
8628 ml_append(lnum, p, (colnr_T)0, FALSE); | |
8629 } | |
710 | 8630 else if (((dumpflags & DUMPFLAG_ICASE) |
8631 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0 | |
8632 : STRNCMP(p, pat, STRLEN(pat)) == 0) | |
701 | 8633 && ins_compl_add_infercase(p, (int)STRLEN(p), |
942 | 8634 p_ic, NULL, *dir, 0) == OK) |
710 | 8635 /* if dir was BACKWARD then honor it just once */ |
8636 *dir = FORWARD; | |
351 | 8637 } |
8638 | |
8639 /* | |
372 | 8640 * For ":spelldump": Find matching prefixes for "word". Prepend each to |
8641 * "word" and append a line to the buffer. | |
701 | 8642 * When "lnum" is zero add insert mode completion. |
351 | 8643 * Return the updated line number. |
8644 */ | |
8645 static linenr_T | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8646 dump_prefixes( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8647 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8648 char_u *word, /* case-folded word */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8649 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8650 int *dir, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8651 int dumpflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8652 int flags, /* flags with prefix ID */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8653 linenr_T startlnum) |
351 | 8654 { |
8655 idx_T arridx[MAXWLEN]; | |
8656 int curi[MAXWLEN]; | |
8657 char_u prefix[MAXWLEN]; | |
455 | 8658 char_u word_up[MAXWLEN]; |
8659 int has_word_up = FALSE; | |
351 | 8660 int c; |
8661 char_u *byts; | |
8662 idx_T *idxs; | |
8663 linenr_T lnum = startlnum; | |
8664 int depth; | |
8665 int n; | |
8666 int len; | |
8667 int i; | |
8668 | |
626 | 8669 /* If the word starts with a lower-case letter make the word with an |
455 | 8670 * 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 | |
351 | 8678 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[]. | |
390 | 8684 * When at the end of a prefix check that it supports "flags". |
351 | 8685 */ |
8686 depth = 0; | |
8687 arridx[0] = 0; | |
8688 curi[0] = 1; | |
8689 while (depth >= 0 && !got_int) | |
8690 { | |
390 | 8691 n = arridx[depth]; |
8692 len = byts[n]; | |
8693 if (curi[depth] > len) | |
351 | 8694 { |
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. */ | |
390 | 8702 n += curi[depth]; |
351 | 8703 ++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 | |
455 | 8713 c = valid_word_prefix(i, n, flags, word, slang, FALSE); |
8714 if (c != 0) | |
351 | 8715 { |
376 | 8716 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
701 | 8717 dump_word(slang, prefix, pat, dir, dumpflags, |
455 | 8718 (c & WF_RAREPFX) ? (flags | WF_RARE) |
701 | 8719 : flags, lnum); |
8720 if (lnum != 0) | |
8721 ++lnum; | |
351 | 8722 } |
455 | 8723 |
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); | |
701 | 8735 dump_word(slang, prefix, pat, dir, dumpflags, |
455 | 8736 (c & WF_RAREPFX) ? (flags | WF_RARE) |
701 | 8737 : flags, lnum); |
8738 if (lnum != 0) | |
8739 ++lnum; | |
455 | 8740 } |
8741 } | |
351 | 8742 } |
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 | |
498 | 8757 /* |
626 | 8758 * Move "p" to the end of word "start". |
8759 * Uses the spell-checking word characters. | |
498 | 8760 */ |
8761 char_u * | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8762 spell_to_word_end(char_u *start, win_T *win) |
498 | 8763 { |
8764 char_u *p = start; | |
8765 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8766 while (*p != NUL && spell_iswordp(p, win)) |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
8767 MB_PTR_ADV(p); |
498 | 8768 return p; |
8769 } | |
8770 | |
475 | 8771 #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
8772 /* | |
626 | 8773 * 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. | |
475 | 8777 * Returns the column number of the word. |
8778 */ | |
8779 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8780 spell_word_start(int startcol) |
475 | 8781 { |
8782 char_u *line; | |
8783 char_u *p; | |
8784 int col = 0; | |
8785 | |
498 | 8786 if (no_spell_checking(curwin)) |
475 | 8787 return startcol; |
8788 | |
8789 /* Find a word character before "startcol". */ | |
8790 line = ml_get_curline(); | |
8791 for (p = line + startcol; p > line; ) | |
8792 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
8793 MB_PTR_BACK(line, p); |
5477 | 8794 if (spell_iswordp_nmw(p, curwin)) |
475 | 8795 break; |
8796 } | |
8797 | |
8798 /* Go back to start of the word. */ | |
8799 while (p > line) | |
8800 { | |
835 | 8801 col = (int)(p - line); |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
8802 MB_PTR_BACK(line, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8803 if (!spell_iswordp(p, curwin)) |
475 | 8804 break; |
8805 col = 0; | |
8806 } | |
8807 | |
535 | 8808 return col; |
8809 } | |
8810 | |
8811 /* | |
8812 * Need to check for 'spellcapcheck' now, the word is removed before | |
8813 * expand_spelling() is called. Therefore the ugly global variable. | |
8814 */ | |
8815 static int spell_expand_need_cap; | |
8816 | |
8817 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8818 spell_expand_check_cap(colnr_T col) |
535 | 8819 { |
475 | 8820 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col); |
8821 } | |
8822 | |
8823 /* | |
8824 * 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 */ | |
8829 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8830 expand_spelling( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8831 linenr_T lnum UNUSED, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8832 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8833 char_u ***matchp) |
475 | 8834 { |
8835 garray_T ga; | |
8836 | |
625 | 8837 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE); |
475 | 8838 *matchp = ga.ga_data; |
8839 return ga.ga_len; | |
8840 } | |
8841 #endif | |
8842 | |
737 | 8843 #endif /* FEAT_SPELL */ |