Mercurial > vim
annotate src/spell.c @ 16277:5ef25fa57f71 v8.1.1143
patch 8.1.1143: may pass weird strings to file name expansion
commit https://github.com/vim/vim/commit/8f130eda4747e4a4d68353cdb650f359fd01469b
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Apr 10 22:15:19 2019 +0200
patch 8.1.1143: may pass weird strings to file name expansion
Problem: May pass weird strings to file name expansion.
Solution: Check for matching characters. Disallow control characters.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 10 Apr 2019 22:30:06 +0200 |
parents | 5df26b29e809 |
children | 7e733046db1d |
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 | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
448 MB_PTR_ADV(mi.mi_fend); |
16162
cd5c83115ec6
patch 8.1.1086: too many curly braces
Bram Moolenaar <Bram@vim.org>
parents:
16142
diff
changeset
|
449 while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp)); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
450 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
451 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL) |
385 | 452 { |
453 /* Check word starting with capital letter. */ | |
455 | 454 c = PTR2CHAR(ptr); |
385 | 455 if (!SPELL_ISUPPER(c)) |
483 | 456 wrongcaplen = (int)(mi.mi_fend - ptr); |
385 | 457 } |
458 } | |
459 if (capcol != NULL) | |
460 *capcol = -1; | |
344 | 461 |
462 /* We always use the characters up to the next non-word character, | |
463 * also for bad words. */ | |
464 mi.mi_end = mi.mi_fend; | |
465 | |
466 /* Check caps type later. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
467 mi.mi_capflags = 0; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
468 mi.mi_cend = NULL; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
469 mi.mi_win = wp; |
344 | 470 |
483 | 471 /* case-fold the word with one non-word character, so that we can check |
472 * for the word end. */ | |
344 | 473 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
|
474 MB_PTR_ADV(mi.mi_fend); |
344 | 475 |
476 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, | |
477 MAXWLEN + 1); | |
835 | 478 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword); |
344 | 479 |
480 /* The word is bad unless we recognize it. */ | |
481 mi.mi_result = SP_BAD; | |
492 | 482 mi.mi_result2 = SP_BAD; |
344 | 483 |
484 /* | |
485 * Loop over the languages specified in 'spelllang'. | |
625 | 486 * We check them all, because a word may be matched longer in another |
487 * language. | |
344 | 488 */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
489 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
|
490 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
491 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi); |
500 | 492 |
493 /* If reloading fails the language is still in the list but everything | |
494 * has been cleared. */ | |
495 if (mi.mi_lp->lp_slang->sl_fidxs == NULL) | |
496 continue; | |
497 | |
344 | 498 /* Check for a matching word in case-folded words. */ |
499 find_word(&mi, FIND_FOLDWORD); | |
500 | |
501 /* Check for a matching word in keep-case words. */ | |
502 find_word(&mi, FIND_KEEPWORD); | |
503 | |
504 /* Check for matching prefixes. */ | |
485 | 505 find_prefix(&mi, FIND_FOLDWORD); |
492 | 506 |
507 /* For a NOBREAK language, may want to use a word without a following | |
508 * word as a backup. */ | |
509 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD | |
510 && mi.mi_result2 != SP_BAD) | |
511 { | |
512 mi.mi_result = mi.mi_result2; | |
513 mi.mi_end = mi.mi_end2; | |
514 } | |
625 | 515 |
516 /* Count the word in the first language where it's found to be OK. */ | |
517 if (count_word && mi.mi_result == SP_OK) | |
518 { | |
519 count_common_word(mi.mi_lp->lp_slang, ptr, | |
520 (int)(mi.mi_end - ptr), 1); | |
521 count_word = FALSE; | |
522 } | |
344 | 523 } |
524 | |
525 if (mi.mi_result != SP_OK) | |
526 { | |
346 | 527 /* If we found a number skip over it. Allows for "42nd". Do flag |
528 * rare and local words, e.g., "3GPP". */ | |
344 | 529 if (nrlen > 0) |
346 | 530 { |
531 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) | |
532 return nrlen; | |
533 } | |
344 | 534 |
535 /* When we are at a non-word character there is no error, just | |
536 * skip over the character (try looking for a word after it). */ | |
5477 | 537 else if (!spell_iswordp_nmw(ptr, wp)) |
243 | 538 { |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
539 if (capcol != NULL && wp->w_s->b_cap_prog != NULL) |
385 | 540 { |
541 regmatch_T regmatch; | |
6375 | 542 int r; |
385 | 543 |
544 /* Check for end of sentence. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
545 regmatch.regprog = wp->w_s->b_cap_prog; |
385 | 546 regmatch.rm_ic = FALSE; |
6375 | 547 r = vim_regexec(®match, ptr, 0); |
548 wp->w_s->b_cap_prog = regmatch.regprog; | |
549 if (r) | |
385 | 550 *capcol = (int)(regmatch.endp[0] - ptr); |
551 } | |
552 | |
344 | 553 if (has_mbyte) |
474 | 554 return (*mb_ptr2len)(ptr); |
344 | 555 return 1; |
300 | 556 } |
483 | 557 else if (mi.mi_end == ptr) |
558 /* Always include at least one character. Required for when there | |
559 * 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
|
560 MB_PTR_ADV(mi.mi_end); |
492 | 561 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
|
562 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak) |
492 | 563 { |
564 char_u *p, *fp; | |
565 int save_result = mi.mi_result; | |
566 | |
567 /* First language in 'spelllang' is NOBREAK. Find first position | |
568 * at which any word would be valid. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
569 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0); |
500 | 570 if (mi.mi_lp->lp_slang->sl_fidxs != NULL) |
571 { | |
572 p = mi.mi_word; | |
573 fp = mi.mi_fword; | |
574 for (;;) | |
575 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
576 MB_PTR_ADV(p); |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
577 MB_PTR_ADV(fp); |
500 | 578 if (p >= mi.mi_end) |
579 break; | |
835 | 580 mi.mi_compoff = (int)(fp - mi.mi_fword); |
500 | 581 find_word(&mi, FIND_COMPOUND); |
582 if (mi.mi_result != SP_BAD) | |
583 { | |
584 mi.mi_end = p; | |
585 break; | |
586 } | |
587 } | |
588 mi.mi_result = save_result; | |
589 } | |
492 | 590 } |
243 | 591 |
344 | 592 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
534 | 593 *attrp = HLF_SPB; |
344 | 594 else if (mi.mi_result == SP_RARE) |
534 | 595 *attrp = HLF_SPR; |
344 | 596 else |
534 | 597 *attrp = HLF_SPL; |
243 | 598 } |
599 | |
483 | 600 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE)) |
601 { | |
602 /* Report SpellCap only when the word isn't badly spelled. */ | |
534 | 603 *attrp = HLF_SPC; |
483 | 604 return wrongcaplen; |
605 } | |
606 | |
300 | 607 return (int)(mi.mi_end - ptr); |
236 | 608 } |
609 | |
610 /* | |
300 | 611 * Check if the word at "mip->mi_word" is in the tree. |
339 | 612 * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
613 * When "mode" is FIND_KEEPWORD check in keep-case word tree. | |
614 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word | |
615 * tree. | |
300 | 616 * |
617 * For a match mip->mi_result is updated. | |
243 | 618 */ |
619 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
620 find_word(matchinf_T *mip, int mode) |
243 | 621 { |
324 | 622 idx_T arridx = 0; |
300 | 623 int endlen[MAXWLEN]; /* length at possible word endings */ |
324 | 624 idx_T endidx[MAXWLEN]; /* possible word endings */ |
300 | 625 int endidxcnt = 0; |
626 int len; | |
627 int wlen = 0; | |
628 int flen; | |
629 int c; | |
630 char_u *ptr; | |
324 | 631 idx_T lo, hi, m; |
300 | 632 char_u *s; |
339 | 633 char_u *p; |
307 | 634 int res = SP_BAD; |
300 | 635 slang_T *slang = mip->mi_lp->lp_slang; |
636 unsigned flags; | |
637 char_u *byts; | |
324 | 638 idx_T *idxs; |
481 | 639 int word_ends; |
485 | 640 int prefix_found; |
492 | 641 int nobreak_result; |
481 | 642 |
643 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND) | |
236 | 644 { |
300 | 645 /* Check for word with matching case in keep-case tree. */ |
646 ptr = mip->mi_word; | |
647 flen = 9999; /* no case folding, always enough bytes */ | |
648 byts = slang->sl_kbyts; | |
649 idxs = slang->sl_kidxs; | |
481 | 650 |
651 if (mode == FIND_KEEPCOMPOUND) | |
652 /* Skip over the previously found word(s). */ | |
653 wlen += mip->mi_compoff; | |
236 | 654 } |
655 else | |
656 { | |
300 | 657 /* Check for case-folded in case-folded tree. */ |
658 ptr = mip->mi_fword; | |
659 flen = mip->mi_fwordlen; /* available case-folded bytes */ | |
660 byts = slang->sl_fbyts; | |
661 idxs = slang->sl_fidxs; | |
339 | 662 |
663 if (mode == FIND_PREFIX) | |
664 { | |
665 /* Skip over the prefix. */ | |
666 wlen = mip->mi_prefixlen; | |
667 flen -= mip->mi_prefixlen; | |
668 } | |
481 | 669 else if (mode == FIND_COMPOUND) |
670 { | |
671 /* Skip over the previously found word(s). */ | |
672 wlen = mip->mi_compoff; | |
673 flen -= mip->mi_compoff; | |
674 } | |
675 | |
243 | 676 } |
677 | |
300 | 678 if (byts == NULL) |
679 return; /* array is empty */ | |
236 | 680 |
681 /* | |
307 | 682 * Repeat advancing in the tree until: |
683 * - there is a byte that doesn't match, | |
684 * - we reach the end of the tree, | |
685 * - or we reach the end of the line. | |
236 | 686 */ |
300 | 687 for (;;) |
236 | 688 { |
346 | 689 if (flen <= 0 && *mip->mi_fend != NUL) |
339 | 690 flen = fold_more(mip); |
300 | 691 |
692 len = byts[arridx++]; | |
693 | |
694 /* If the first possible byte is a zero the word could end here. | |
695 * Remember this index, we first check for the longest word. */ | |
696 if (byts[arridx] == 0) | |
697 { | |
307 | 698 if (endidxcnt == MAXWLEN) |
699 { | |
700 /* 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
|
701 emsg(_(e_format)); |
307 | 702 return; |
703 } | |
300 | 704 endlen[endidxcnt] = wlen; |
705 endidx[endidxcnt++] = arridx++; | |
706 --len; | |
707 | |
708 /* Skip over the zeros, there can be several flag/region | |
709 * combinations. */ | |
710 while (len > 0 && byts[arridx] == 0) | |
711 { | |
712 ++arridx; | |
713 --len; | |
714 } | |
715 if (len == 0) | |
716 break; /* no children, word must end here */ | |
717 } | |
718 | |
719 /* Stop looking at end of the line. */ | |
720 if (ptr[wlen] == NUL) | |
721 break; | |
722 | |
723 /* Perform a binary search in the list of accepted bytes. */ | |
724 c = ptr[wlen]; | |
346 | 725 if (c == TAB) /* <Tab> is handled like <Space> */ |
726 c = ' '; | |
300 | 727 lo = arridx; |
728 hi = arridx + len - 1; | |
729 while (lo < hi) | |
730 { | |
731 m = (lo + hi) / 2; | |
732 if (byts[m] > c) | |
733 hi = m - 1; | |
734 else if (byts[m] < c) | |
735 lo = m + 1; | |
736 else | |
737 { | |
738 lo = hi = m; | |
739 break; | |
236 | 740 } |
741 } | |
300 | 742 |
743 /* Stop if there is no matching byte. */ | |
744 if (hi < lo || byts[lo] != c) | |
745 break; | |
746 | |
747 /* Continue at the child (if there is one). */ | |
748 arridx = idxs[lo]; | |
749 ++wlen; | |
750 --flen; | |
346 | 751 |
752 /* One space in the good word may stand for several spaces in the | |
753 * checked word. */ | |
754 if (c == ' ') | |
755 { | |
756 for (;;) | |
757 { | |
758 if (flen <= 0 && *mip->mi_fend != NUL) | |
759 flen = fold_more(mip); | |
760 if (ptr[wlen] != ' ' && ptr[wlen] != TAB) | |
761 break; | |
762 ++wlen; | |
763 --flen; | |
764 } | |
765 } | |
236 | 766 } |
767 | |
300 | 768 /* |
769 * Verify that one of the possible endings is valid. Try the longest | |
770 * first. | |
771 */ | |
772 while (endidxcnt > 0) | |
773 { | |
774 --endidxcnt; | |
775 arridx = endidx[endidxcnt]; | |
776 wlen = endlen[endidxcnt]; | |
236 | 777 |
300 | 778 if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
779 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
|
780 if (spell_iswordp(ptr + wlen, mip->mi_win)) |
481 | 781 { |
492 | 782 if (slang->sl_compprog == NULL && !slang->sl_nobreak) |
481 | 783 continue; /* next char is a word character */ |
784 word_ends = FALSE; | |
785 } | |
786 else | |
787 word_ends = TRUE; | |
485 | 788 /* The prefix flag is before compound flags. Once a valid prefix flag |
789 * has been found we try compound flags. */ | |
790 prefix_found = FALSE; | |
300 | 791 |
339 | 792 if (mode != FIND_KEEPWORD && has_mbyte) |
300 | 793 { |
794 /* Compute byte length in original word, length may change | |
339 | 795 * when folding case. This can be slow, take a shortcut when the |
796 * case-folded word is equal to the keep-case word. */ | |
300 | 797 p = mip->mi_word; |
339 | 798 if (STRNCMP(ptr, p, wlen) != 0) |
799 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
800 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
|
801 MB_PTR_ADV(p); |
835 | 802 wlen = (int)(p - mip->mi_word); |
339 | 803 } |
300 | 804 } |
236 | 805 |
339 | 806 /* Check flags and region. For FIND_PREFIX check the condition and |
807 * prefix ID. | |
808 * Repeat this if there are more flags/region alternatives until there | |
809 * is a match. */ | |
810 res = SP_BAD; | |
811 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; | |
812 --len, ++arridx) | |
300 | 813 { |
814 flags = idxs[arridx]; | |
324 | 815 |
339 | 816 /* For the fold-case tree check that the case of the checked word |
817 * matches with what the word in the tree requires. | |
818 * For keep-case tree the case is always right. For prefixes we | |
819 * don't bother to check. */ | |
820 if (mode == FIND_FOLDWORD) | |
300 | 821 { |
822 if (mip->mi_cend != mip->mi_word + wlen) | |
823 { | |
323 | 824 /* mi_capflags was set for a different word length, need |
825 * to do it again. */ | |
300 | 826 mip->mi_cend = mip->mi_word + wlen; |
323 | 827 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
300 | 828 } |
829 | |
346 | 830 if (mip->mi_capflags == WF_KEEPCAP |
831 || !spell_valid_case(mip->mi_capflags, flags)) | |
339 | 832 continue; |
300 | 833 } |
236 | 834 |
339 | 835 /* When mode is FIND_PREFIX the word must support the prefix: |
836 * check the prefix ID and the condition. Do that for the list at | |
366 | 837 * mip->mi_prefarridx that find_prefix() filled. */ |
485 | 838 else if (mode == FIND_PREFIX && !prefix_found) |
481 | 839 { |
366 | 840 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
390 | 841 flags, |
455 | 842 mip->mi_word + mip->mi_cprefixlen, slang, |
843 FALSE); | |
366 | 844 if (c == 0) |
339 | 845 continue; |
366 | 846 |
847 /* Use the WF_RARE flag for a rare prefix. */ | |
848 if (c & WF_RAREPFX) | |
849 flags |= WF_RARE; | |
485 | 850 prefix_found = TRUE; |
339 | 851 } |
852 | |
492 | 853 if (slang->sl_nobreak) |
854 { | |
855 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND) | |
856 && (flags & WF_BANNED) == 0) | |
857 { | |
858 /* NOBREAK: found a valid following word. That's all we | |
859 * need to know, so return. */ | |
860 mip->mi_result = SP_OK; | |
861 break; | |
862 } | |
863 } | |
864 | |
865 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND | |
866 || !word_ends)) | |
481 | 867 { |
974 | 868 /* If there is no compound flag or the word is shorter than |
483 | 869 * COMPOUNDMIN reject it quickly. |
870 * Makes you wonder why someone puts a compound flag on a word | |
481 | 871 * that's too short... Myspell compatibility requires this |
872 * anyway. */ | |
490 | 873 if (((unsigned)flags >> 24) == 0 |
874 || wlen - mip->mi_compoff < slang->sl_compminlen) | |
483 | 875 continue; |
500 | 876 /* For multi-byte chars check character length against |
877 * COMPOUNDMIN. */ | |
878 if (has_mbyte | |
501 | 879 && slang->sl_compminlen > 0 |
500 | 880 && mb_charlen_len(mip->mi_word + mip->mi_compoff, |
881 wlen - mip->mi_compoff) < slang->sl_compminlen) | |
882 continue; | |
483 | 883 |
809 | 884 /* Limit the number of compound words to COMPOUNDWORDMAX if no |
490 | 885 * maximum for syllables is specified. */ |
809 | 886 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2 |
887 > slang->sl_compmax | |
490 | 888 && slang->sl_compsylmax == MAXWLEN) |
483 | 889 continue; |
890 | |
819 | 891 /* Don't allow compounding on a side where an affix was added, |
892 * unless COMPOUNDPERMITFLAG was used. */ | |
893 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF)) | |
894 continue; | |
895 if (!word_ends && (flags & WF_NOCOMPAFT)) | |
896 continue; | |
897 | |
485 | 898 /* Quickly check if compounding is possible with this flag. */ |
495 | 899 if (!byte_in_str(mip->mi_complen == 0 |
485 | 900 ? slang->sl_compstartflags |
901 : slang->sl_compallflags, | |
495 | 902 ((unsigned)flags >> 24))) |
481 | 903 continue; |
904 | |
1762 | 905 /* If there is a match with a CHECKCOMPOUNDPATTERN rule |
906 * discard the compound word. */ | |
907 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat)) | |
908 continue; | |
909 | |
490 | 910 if (mode == FIND_COMPOUND) |
911 { | |
912 int capflags; | |
913 | |
914 /* Need to check the caps type of the appended compound | |
915 * word. */ | |
916 if (has_mbyte && STRNCMP(ptr, mip->mi_word, | |
917 mip->mi_compoff) != 0) | |
918 { | |
919 /* case folding may have changed the length */ | |
920 p = mip->mi_word; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
921 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
|
922 MB_PTR_ADV(p); |
490 | 923 } |
924 else | |
925 p = mip->mi_word + mip->mi_compoff; | |
926 capflags = captype(p, mip->mi_word + wlen); | |
927 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP | |
928 && (flags & WF_FIXCAP) != 0)) | |
929 continue; | |
930 | |
931 if (capflags != WF_ALLCAP) | |
932 { | |
933 /* When the character before the word is a word | |
934 * character we do not accept a Onecap word. We do | |
935 * accept a no-caps word, even when the dictionary | |
936 * word specifies ONECAP. */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
937 MB_PTR_BACK(mip->mi_word, p); |
5477 | 938 if (spell_iswordp_nmw(p, mip->mi_win) |
490 | 939 ? capflags == WF_ONECAP |
940 : (flags & WF_ONECAP) != 0 | |
941 && capflags != WF_ONECAP) | |
942 continue; | |
943 } | |
944 } | |
945 | |
483 | 946 /* If the word ends the sequence of compound flags of the |
714 | 947 * words must match with one of the COMPOUNDRULE items and |
483 | 948 * the number of syllables must not be too large. */ |
949 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24); | |
950 mip->mi_compflags[mip->mi_complen + 1] = NUL; | |
951 if (word_ends) | |
952 { | |
953 char_u fword[MAXWLEN]; | |
954 | |
955 if (slang->sl_compsylmax < MAXWLEN) | |
956 { | |
957 /* "fword" is only needed for checking syllables. */ | |
958 if (ptr == mip->mi_word) | |
959 (void)spell_casefold(ptr, wlen, fword, MAXWLEN); | |
960 else | |
961 vim_strncpy(fword, ptr, endlen[endidxcnt]); | |
962 } | |
963 if (!can_compound(slang, fword, mip->mi_compflags)) | |
964 continue; | |
965 } | |
1762 | 966 else if (slang->sl_comprules != NULL |
967 && !match_compoundrule(slang, mip->mi_compflags)) | |
968 /* The compound flags collected so far do not match any | |
969 * COMPOUNDRULE, discard the compounded word. */ | |
970 continue; | |
481 | 971 } |
972 | |
500 | 973 /* Check NEEDCOMPOUND: can't use word without compounding. */ |
974 else if (flags & WF_NEEDCOMP) | |
975 continue; | |
976 | |
492 | 977 nobreak_result = SP_OK; |
978 | |
481 | 979 if (!word_ends) |
980 { | |
492 | 981 int save_result = mip->mi_result; |
982 char_u *save_end = mip->mi_end; | |
501 | 983 langp_T *save_lp = mip->mi_lp; |
984 int lpi; | |
492 | 985 |
986 /* Check that a valid word follows. If there is one and we | |
987 * are compounding, it will set "mi_result", thus we are | |
988 * always finished here. For NOBREAK we only check that a | |
989 * valid word follows. | |
481 | 990 * Recursive! */ |
492 | 991 if (slang->sl_nobreak) |
992 mip->mi_result = SP_BAD; | |
481 | 993 |
994 /* Find following word in case-folded tree. */ | |
995 mip->mi_compoff = endlen[endidxcnt]; | |
996 if (has_mbyte && mode == FIND_KEEPWORD) | |
997 { | |
998 /* Compute byte length in case-folded word from "wlen": | |
999 * byte length in keep-case word. Length may change when | |
1000 * folding case. This can be slow, take a shortcut when | |
1001 * the case-folded word is equal to the keep-case word. */ | |
1002 p = mip->mi_fword; | |
1003 if (STRNCMP(ptr, p, wlen) != 0) | |
1004 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1005 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
|
1006 MB_PTR_ADV(p); |
835 | 1007 mip->mi_compoff = (int)(p - mip->mi_fword); |
481 | 1008 } |
1009 } | |
8956
d9e671c5afe6
commit https://github.com/vim/vim/commit/ba53435144f46eaaa53c63a62e748b3feee9742c
Christian Brabandt <cb@256bit.org>
parents:
8524
diff
changeset
|
1010 #if 0 /* Disabled, see below */ |
485 | 1011 c = mip->mi_compoff; |
8956
d9e671c5afe6
commit https://github.com/vim/vim/commit/ba53435144f46eaaa53c63a62e748b3feee9742c
Christian Brabandt <cb@256bit.org>
parents:
8524
diff
changeset
|
1012 #endif |
483 | 1013 ++mip->mi_complen; |
809 | 1014 if (flags & WF_COMPROOT) |
1015 ++mip->mi_compextra; | |
501 | 1016 |
1017 /* For NOBREAK we need to try all NOBREAK languages, at least | |
1018 * to find the ".add" file(s). */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1019 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi) |
501 | 1020 { |
1021 if (slang->sl_nobreak) | |
1022 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1023 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi); |
501 | 1024 if (mip->mi_lp->lp_slang->sl_fidxs == NULL |
1025 || !mip->mi_lp->lp_slang->sl_nobreak) | |
1026 continue; | |
1027 } | |
1028 | |
1029 find_word(mip, FIND_COMPOUND); | |
1030 | |
1031 /* When NOBREAK any word that matches is OK. Otherwise we | |
1032 * need to find the longest match, thus try with keep-case | |
1033 * and prefix too. */ | |
492 | 1034 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
1035 { | |
501 | 1036 /* Find following word in keep-case tree. */ |
1037 mip->mi_compoff = wlen; | |
1038 find_word(mip, FIND_KEEPCOMPOUND); | |
1039 | |
819 | 1040 #if 0 /* Disabled, a prefix must not appear halfway a compound word, |
1041 unless the COMPOUNDPERMITFLAG is used and then it can't be a | |
1042 postponed prefix. */ | |
501 | 1043 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
1044 { | |
1045 /* Check for following word with prefix. */ | |
1046 mip->mi_compoff = c; | |
1047 find_prefix(mip, FIND_COMPOUND); | |
1048 } | |
819 | 1049 #endif |
492 | 1050 } |
501 | 1051 |
1052 if (!slang->sl_nobreak) | |
1053 break; | |
492 | 1054 } |
483 | 1055 --mip->mi_complen; |
809 | 1056 if (flags & WF_COMPROOT) |
1057 --mip->mi_compextra; | |
501 | 1058 mip->mi_lp = save_lp; |
485 | 1059 |
492 | 1060 if (slang->sl_nobreak) |
1061 { | |
1062 nobreak_result = mip->mi_result; | |
1063 mip->mi_result = save_result; | |
1064 mip->mi_end = save_end; | |
1065 } | |
1066 else | |
1067 { | |
1068 if (mip->mi_result == SP_OK) | |
1069 break; | |
1070 continue; | |
1071 } | |
481 | 1072 } |
1073 | |
339 | 1074 if (flags & WF_BANNED) |
1075 res = SP_BANNED; | |
1076 else if (flags & WF_REGION) | |
1077 { | |
1078 /* Check region. */ | |
390 | 1079 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) |
300 | 1080 res = SP_OK; |
339 | 1081 else |
1082 res = SP_LOCAL; | |
300 | 1083 } |
339 | 1084 else if (flags & WF_RARE) |
1085 res = SP_RARE; | |
307 | 1086 else |
339 | 1087 res = SP_OK; |
1088 | |
492 | 1089 /* Always use the longest match and the best result. For NOBREAK |
1090 * we separately keep the longest match without a following good | |
1091 * word as a fall-back. */ | |
1092 if (nobreak_result == SP_BAD) | |
1093 { | |
1094 if (mip->mi_result2 > res) | |
1095 { | |
1096 mip->mi_result2 = res; | |
1097 mip->mi_end2 = mip->mi_word + wlen; | |
1098 } | |
1099 else if (mip->mi_result2 == res | |
1100 && mip->mi_end2 < mip->mi_word + wlen) | |
1101 mip->mi_end2 = mip->mi_word + wlen; | |
1102 } | |
1103 else if (mip->mi_result > res) | |
339 | 1104 { |
1105 mip->mi_result = res; | |
1106 mip->mi_end = mip->mi_word + wlen; | |
1107 } | |
351 | 1108 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) |
339 | 1109 mip->mi_end = mip->mi_word + wlen; |
1110 | |
492 | 1111 if (mip->mi_result == SP_OK) |
339 | 1112 break; |
300 | 1113 } |
1114 | |
492 | 1115 if (mip->mi_result == SP_OK) |
300 | 1116 break; |
1117 } | |
236 | 1118 } |
1119 | |
323 | 1120 /* |
1762 | 1121 * Return TRUE if there is a match between the word ptr[wlen] and |
1122 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another | |
1123 * word. | |
1124 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the | |
1125 * end of ptr[wlen] and the second part matches after it. | |
1126 */ | |
1127 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1128 match_checkcompoundpattern( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1129 char_u *ptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1130 int wlen, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1131 garray_T *gap) /* &sl_comppat */ |
1762 | 1132 { |
1133 int i; | |
1134 char_u *p; | |
1135 int len; | |
1136 | |
1137 for (i = 0; i + 1 < gap->ga_len; i += 2) | |
1138 { | |
1139 p = ((char_u **)gap->ga_data)[i + 1]; | |
1140 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0) | |
1141 { | |
1142 /* Second part matches at start of following compound word, now | |
1143 * check if first part matches at end of previous word. */ | |
1144 p = ((char_u **)gap->ga_data)[i]; | |
1771 | 1145 len = (int)STRLEN(p); |
1762 | 1146 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) |
1147 return TRUE; | |
1148 } | |
1149 } | |
1150 return FALSE; | |
1151 } | |
1152 | |
1153 /* | |
626 | 1154 * Return TRUE if "flags" is a valid sequence of compound flags and "word" |
1155 * does not have too many syllables. | |
482 | 1156 */ |
1157 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1158 can_compound(slang_T *slang, char_u *word, char_u *flags) |
483 | 1159 { |
495 | 1160 char_u uflags[MAXWLEN * 2]; |
1161 int i; | |
1162 char_u *p; | |
483 | 1163 |
1164 if (slang->sl_compprog == NULL) | |
1165 return FALSE; | |
495 | 1166 if (enc_utf8) |
1167 { | |
1168 /* Need to convert the single byte flags to utf8 characters. */ | |
1169 p = uflags; | |
1170 for (i = 0; flags[i] != NUL; ++i) | |
11269
121d29004998
patch 8.0.0520: using a function pointer while the function is known
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1171 p += utf_char2bytes(flags[i], p); |
495 | 1172 *p = NUL; |
1173 p = uflags; | |
1174 } | |
1175 else | |
1176 p = flags; | |
6375 | 1177 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0)) |
483 | 1178 return FALSE; |
1179 | |
490 | 1180 /* Count the number of syllables. This may be slow, do it last. If there |
1181 * are too many syllables AND the number of compound words is above | |
809 | 1182 * COMPOUNDWORDMAX then compounding is not allowed. */ |
483 | 1183 if (slang->sl_compsylmax < MAXWLEN |
1184 && count_syllables(slang, word) > slang->sl_compsylmax) | |
495 | 1185 return (int)STRLEN(flags) < slang->sl_compmax; |
483 | 1186 return TRUE; |
482 | 1187 } |
1188 | |
1189 /* | |
1762 | 1190 * Return TRUE when the sequence of flags in "compflags" plus "flag" can |
1191 * possibly form a valid compounded word. This also checks the COMPOUNDRULE | |
1192 * lines if they don't contain wildcards. | |
1193 */ | |
1194 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1195 can_be_compound( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1196 trystate_T *sp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1197 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1198 char_u *compflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1199 int flag) |
1762 | 1200 { |
1201 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags | |
1202 * then it can't possibly compound. */ | |
1203 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit | |
1204 ? slang->sl_compstartflags : slang->sl_compallflags, flag)) | |
1205 return FALSE; | |
1206 | |
1207 /* If there are no wildcards, we can check if the flags collected so far | |
1208 * possibly can form a match with COMPOUNDRULE patterns. This only | |
1209 * makes sense when we have two or more words. */ | |
1210 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit) | |
1211 { | |
1212 int v; | |
1213 | |
1214 compflags[sp->ts_complen] = flag; | |
1215 compflags[sp->ts_complen + 1] = NUL; | |
1216 v = match_compoundrule(slang, compflags + sp->ts_compsplit); | |
1217 compflags[sp->ts_complen] = NUL; | |
1218 return v; | |
1219 } | |
1220 | |
1221 return TRUE; | |
1222 } | |
1223 | |
1224 | |
1225 /* | |
1226 * Return TRUE if the compound flags in compflags[] match the start of any | |
1227 * compound rule. This is used to stop trying a compound if the flags | |
1228 * collected so far can't possibly match any compound rule. | |
1229 * Caller must check that slang->sl_comprules is not NULL. | |
1230 */ | |
1231 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1232 match_compoundrule(slang_T *slang, char_u *compflags) |
1762 | 1233 { |
1234 char_u *p; | |
1235 int i; | |
1236 int c; | |
1237 | |
1238 /* loop over all the COMPOUNDRULE entries */ | |
1239 for (p = slang->sl_comprules; *p != NUL; ++p) | |
1240 { | |
1241 /* loop over the flags in the compound word we have made, match | |
1242 * them against the current rule entry */ | |
1243 for (i = 0; ; ++i) | |
1244 { | |
1245 c = compflags[i]; | |
1246 if (c == NUL) | |
1247 /* found a rule that matches for the flags we have so far */ | |
1248 return TRUE; | |
1249 if (*p == '/' || *p == NUL) | |
1250 break; /* end of rule, it's too short */ | |
1251 if (*p == '[') | |
1252 { | |
1253 int match = FALSE; | |
1254 | |
1255 /* compare against all the flags in [] */ | |
1256 ++p; | |
1257 while (*p != ']' && *p != NUL) | |
1258 if (*p++ == c) | |
1259 match = TRUE; | |
1260 if (!match) | |
1261 break; /* none matches */ | |
1262 } | |
1263 else if (*p != c) | |
1264 break; /* flag of word doesn't match flag in pattern */ | |
1265 ++p; | |
1266 } | |
1267 | |
1268 /* Skip to the next "/", where the next pattern starts. */ | |
1269 p = vim_strchr(p, '/'); | |
1270 if (p == NULL) | |
1271 break; | |
1272 } | |
1273 | |
1274 /* Checked all the rules and none of them match the flags, so there | |
1275 * can't possibly be a compound starting with these flags. */ | |
1276 return FALSE; | |
1277 } | |
1278 | |
1279 /* | |
390 | 1280 * Return non-zero if the prefix indicated by "arridx" matches with the prefix |
1281 * ID in "flags" for the word "word". | |
366 | 1282 * The WF_RAREPFX flag is included in the return value for a rare prefix. |
351 | 1283 */ |
1284 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1285 valid_word_prefix( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1286 int totprefcnt, /* nr of prefix IDs */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1287 int arridx, /* idx in sl_pidxs[] */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1288 int flags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1289 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1290 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1291 int cond_req) /* only use prefixes with a condition */ |
351 | 1292 { |
1293 int prefcnt; | |
1294 int pidx; | |
6375 | 1295 regprog_T **rp; |
390 | 1296 int prefid; |
1297 | |
1298 prefid = (unsigned)flags >> 24; | |
351 | 1299 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
1300 { | |
1301 pidx = slang->sl_pidxs[arridx + prefcnt]; | |
1302 | |
1303 /* Check the prefix ID. */ | |
1304 if (prefid != (pidx & 0xff)) | |
1305 continue; | |
1306 | |
390 | 1307 /* Check if the prefix doesn't combine and the word already has a |
1308 * suffix. */ | |
1309 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) | |
1310 continue; | |
1311 | |
351 | 1312 /* Check the condition, if there is one. The condition index is |
366 | 1313 * stored in the two bytes above the prefix ID byte. */ |
6375 | 1314 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
1315 if (*rp != NULL) | |
1316 { | |
1317 if (!vim_regexec_prog(rp, FALSE, word, 0)) | |
351 | 1318 continue; |
1319 } | |
455 | 1320 else if (cond_req) |
1321 continue; | |
1322 | |
1323 /* It's a match! Return the WF_ flags. */ | |
366 | 1324 return pidx; |
1325 } | |
1326 return 0; | |
351 | 1327 } |
1328 | |
1329 /* | |
339 | 1330 * Check if the word at "mip->mi_word" has a matching prefix. |
1331 * If it does, then check the following word. | |
1332 * | |
485 | 1333 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a |
1334 * prefix in a compound word. | |
1335 * | |
339 | 1336 * For a match mip->mi_result is updated. |
1337 */ | |
1338 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1339 find_prefix(matchinf_T *mip, int mode) |
339 | 1340 { |
1341 idx_T arridx = 0; | |
1342 int len; | |
1343 int wlen = 0; | |
1344 int flen; | |
1345 int c; | |
1346 char_u *ptr; | |
1347 idx_T lo, hi, m; | |
1348 slang_T *slang = mip->mi_lp->lp_slang; | |
1349 char_u *byts; | |
1350 idx_T *idxs; | |
1351 | |
375 | 1352 byts = slang->sl_pbyts; |
1353 if (byts == NULL) | |
1354 return; /* array is empty */ | |
1355 | |
339 | 1356 /* We use the case-folded word here, since prefixes are always |
1357 * case-folded. */ | |
1358 ptr = mip->mi_fword; | |
1359 flen = mip->mi_fwordlen; /* available case-folded bytes */ | |
485 | 1360 if (mode == FIND_COMPOUND) |
1361 { | |
1362 /* Skip over the previously found word(s). */ | |
1363 ptr += mip->mi_compoff; | |
1364 flen -= mip->mi_compoff; | |
1365 } | |
339 | 1366 idxs = slang->sl_pidxs; |
1367 | |
1368 /* | |
1369 * Repeat advancing in the tree until: | |
1370 * - there is a byte that doesn't match, | |
1371 * - we reach the end of the tree, | |
1372 * - or we reach the end of the line. | |
1373 */ | |
1374 for (;;) | |
1375 { | |
1376 if (flen == 0 && *mip->mi_fend != NUL) | |
1377 flen = fold_more(mip); | |
1378 | |
1379 len = byts[arridx++]; | |
1380 | |
1381 /* If the first possible byte is a zero the prefix could end here. | |
1382 * Check if the following word matches and supports the prefix. */ | |
1383 if (byts[arridx] == 0) | |
1384 { | |
1385 /* There can be several prefixes with different conditions. We | |
1386 * try them all, since we don't know which one will give the | |
1387 * longest match. The word is the same each time, pass the list | |
1388 * of possible prefixes to find_word(). */ | |
1389 mip->mi_prefarridx = arridx; | |
1390 mip->mi_prefcnt = len; | |
1391 while (len > 0 && byts[arridx] == 0) | |
1392 { | |
1393 ++arridx; | |
1394 --len; | |
1395 } | |
1396 mip->mi_prefcnt -= len; | |
1397 | |
1398 /* Find the word that comes after the prefix. */ | |
1399 mip->mi_prefixlen = wlen; | |
485 | 1400 if (mode == FIND_COMPOUND) |
1401 /* Skip over the previously found word(s). */ | |
1402 mip->mi_prefixlen += mip->mi_compoff; | |
1403 | |
455 | 1404 if (has_mbyte) |
1405 { | |
1406 /* Case-folded length may differ from original length. */ | |
485 | 1407 mip->mi_cprefixlen = nofold_len(mip->mi_fword, |
1408 mip->mi_prefixlen, mip->mi_word); | |
455 | 1409 } |
1410 else | |
485 | 1411 mip->mi_cprefixlen = mip->mi_prefixlen; |
339 | 1412 find_word(mip, FIND_PREFIX); |
1413 | |
1414 | |
1415 if (len == 0) | |
1416 break; /* no children, word must end here */ | |
1417 } | |
1418 | |
1419 /* Stop looking at end of the line. */ | |
1420 if (ptr[wlen] == NUL) | |
1421 break; | |
1422 | |
1423 /* Perform a binary search in the list of accepted bytes. */ | |
1424 c = ptr[wlen]; | |
1425 lo = arridx; | |
1426 hi = arridx + len - 1; | |
1427 while (lo < hi) | |
1428 { | |
1429 m = (lo + hi) / 2; | |
1430 if (byts[m] > c) | |
1431 hi = m - 1; | |
1432 else if (byts[m] < c) | |
1433 lo = m + 1; | |
1434 else | |
1435 { | |
1436 lo = hi = m; | |
1437 break; | |
1438 } | |
1439 } | |
1440 | |
1441 /* Stop if there is no matching byte. */ | |
1442 if (hi < lo || byts[lo] != c) | |
1443 break; | |
1444 | |
1445 /* Continue at the child (if there is one). */ | |
1446 arridx = idxs[lo]; | |
1447 ++wlen; | |
1448 --flen; | |
1449 } | |
1450 } | |
1451 | |
1452 /* | |
1453 * Need to fold at least one more character. Do until next non-word character | |
626 | 1454 * for efficiency. Include the non-word character too. |
339 | 1455 * Return the length of the folded chars in bytes. |
1456 */ | |
1457 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1458 fold_more(matchinf_T *mip) |
339 | 1459 { |
1460 int flen; | |
1461 char_u *p; | |
1462 | |
1463 p = mip->mi_fend; | |
1464 do | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1465 MB_PTR_ADV(mip->mi_fend); |
16162
cd5c83115ec6
patch 8.1.1086: too many curly braces
Bram Moolenaar <Bram@vim.org>
parents:
16142
diff
changeset
|
1466 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win)); |
339 | 1467 |
626 | 1468 /* Include the non-word character so that we can check for the word end. */ |
339 | 1469 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
|
1470 MB_PTR_ADV(mip->mi_fend); |
339 | 1471 |
1472 (void)spell_casefold(p, (int)(mip->mi_fend - p), | |
1473 mip->mi_fword + mip->mi_fwordlen, | |
1474 MAXWLEN - mip->mi_fwordlen); | |
835 | 1475 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen); |
339 | 1476 mip->mi_fwordlen += flen; |
1477 return flen; | |
1478 } | |
1479 | |
1480 /* | |
323 | 1481 * Check case flags for a word. Return TRUE if the word has the requested |
1482 * case. | |
1483 */ | |
1484 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1485 spell_valid_case( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1486 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
|
1487 int treeflags) /* flags for the word in the spell tree */ |
323 | 1488 { |
389 | 1489 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) |
323 | 1490 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
474 | 1491 && ((treeflags & WF_ONECAP) == 0 |
1492 || (wordflags & WF_ONECAP) != 0))); | |
323 | 1493 } |
1494 | |
351 | 1495 /* |
1496 * Return TRUE if spell checking is not enabled. | |
1497 */ | |
1498 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1499 no_spell_checking(win_T *wp) |
498 | 1500 { |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1501 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
|
1502 || wp->w_s->b_langp.ga_len == 0) |
351 | 1503 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1504 emsg(_("E756: Spell checking is not enabled")); |
351 | 1505 return TRUE; |
1506 } | |
1507 return FALSE; | |
1508 } | |
300 | 1509 |
236 | 1510 /* |
1511 * Move to next spell error. | |
500 | 1512 * "curline" is FALSE for "[s", "]s", "[S" and "]S". |
1513 * "curline" is TRUE to find word under/after cursor in the same line. | |
483 | 1514 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move |
1515 * to after badly spelled word before the cursor. | |
495 | 1516 * Return 0 if not found, length of the badly spelled word otherwise. |
236 | 1517 */ |
1518 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1519 spell_move_to( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1520 win_T *wp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1521 int dir, /* FORWARD or BACKWARD */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1522 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
|
1523 int curline, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1524 hlf_T *attrp) /* return: attributes of bad word or NULL |
534 | 1525 (only when "dir" is FORWARD) */ |
236 | 1526 { |
249 | 1527 linenr_T lnum; |
1528 pos_T found_pos; | |
495 | 1529 int found_len = 0; |
236 | 1530 char_u *line; |
1531 char_u *p; | |
346 | 1532 char_u *endp; |
534 | 1533 hlf_T attr; |
236 | 1534 int len; |
5519 | 1535 #ifdef FEAT_SYN_HL |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1536 int has_syntax = syntax_present(wp); |
5519 | 1537 #endif |
249 | 1538 int col; |
1539 int can_spell; | |
346 | 1540 char_u *buf = NULL; |
1541 int buflen = 0; | |
1542 int skip = 0; | |
385 | 1543 int capcol = -1; |
500 | 1544 int found_one = FALSE; |
1545 int wrapped = FALSE; | |
236 | 1546 |
498 | 1547 if (no_spell_checking(wp)) |
495 | 1548 return 0; |
236 | 1549 |
249 | 1550 /* |
1551 * Start looking for bad word at the start of the line, because we can't | |
817 | 1552 * start halfway a word, we don't know where it starts or ends. |
249 | 1553 * |
1554 * When searching backwards, we continue in the line to find the last | |
1555 * bad word (in the cursor line: before the cursor). | |
346 | 1556 * |
1557 * We concatenate the start of the next line, so that wrapped words work | |
1558 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards | |
1559 * though... | |
249 | 1560 */ |
498 | 1561 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
|
1562 CLEAR_POS(&found_pos); |
236 | 1563 |
1564 while (!got_int) | |
1565 { | |
498 | 1566 line = ml_get_buf(wp->w_buffer, lnum, FALSE); |
346 | 1567 |
835 | 1568 len = (int)STRLEN(line); |
346 | 1569 if (buflen < len + MAXWLEN + 2) |
1570 { | |
1571 vim_free(buf); | |
1572 buflen = len + MAXWLEN + 2; | |
1573 buf = alloc(buflen); | |
1574 if (buf == NULL) | |
1575 break; | |
1576 } | |
1577 | |
385 | 1578 /* In first line check first word for Capital. */ |
1579 if (lnum == 1) | |
1580 capcol = 0; | |
1581 | |
1582 /* For checking first word with a capital skip white space. */ | |
1583 if (capcol == 0) | |
12323
4dba3e4f3b01
patch 8.0.1041: bogus characters when indenting during visual-block append
Christian Brabandt <cb@256bit.org>
parents:
11521
diff
changeset
|
1584 capcol = getwhitecols(line); |
835 | 1585 else if (curline && wp == curwin) |
1586 { | |
1587 /* 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
|
1588 col = getwhitecols(line); |
835 | 1589 if (check_need_cap(lnum, col)) |
1590 capcol = col; | |
1591 | |
1592 /* Need to get the line again, may have looked at the previous | |
1593 * one. */ | |
1594 line = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
1595 } | |
385 | 1596 |
346 | 1597 /* Copy the line into "buf" and append the start of the next line if |
1598 * possible. */ | |
1599 STRCPY(buf, line); | |
498 | 1600 if (lnum < wp->w_buffer->b_ml.ml_line_count) |
883 | 1601 spell_cat_line(buf + STRLEN(buf), |
1602 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN); | |
346 | 1603 |
1604 p = buf + skip; | |
1605 endp = buf + len; | |
1606 while (p < endp) | |
236 | 1607 { |
500 | 1608 /* When searching backward don't search after the cursor. Unless |
1609 * we wrapped around the end of the buffer. */ | |
300 | 1610 if (dir == BACKWARD |
498 | 1611 && lnum == wp->w_cursor.lnum |
500 | 1612 && !wrapped |
498 | 1613 && (colnr_T)(p - buf) >= wp->w_cursor.col) |
300 | 1614 break; |
249 | 1615 |
300 | 1616 /* start of word */ |
534 | 1617 attr = HLF_COUNT; |
625 | 1618 len = spell_check(wp, p, &attr, &capcol, FALSE); |
249 | 1619 |
534 | 1620 if (attr != HLF_COUNT) |
300 | 1621 { |
1622 /* We found a bad word. Check the attribute. */ | |
534 | 1623 if (allwords || attr == HLF_SPB) |
236 | 1624 { |
300 | 1625 /* When searching forward only accept a bad word after |
1626 * the cursor. */ | |
1627 if (dir == BACKWARD | |
500 | 1628 || lnum != wp->w_cursor.lnum |
498 | 1629 || (lnum == wp->w_cursor.lnum |
500 | 1630 && (wrapped |
1631 || (colnr_T)(curline ? p - buf + len | |
346 | 1632 : p - buf) |
500 | 1633 > wp->w_cursor.col))) |
236 | 1634 { |
5519 | 1635 #ifdef FEAT_SYN_HL |
300 | 1636 if (has_syntax) |
249 | 1637 { |
835 | 1638 col = (int)(p - buf); |
498 | 1639 (void)syn_get_id(wp, lnum, (colnr_T)col, |
1504 | 1640 FALSE, &can_spell, FALSE); |
857 | 1641 if (!can_spell) |
1642 attr = HLF_COUNT; | |
300 | 1643 } |
1644 else | |
737 | 1645 #endif |
300 | 1646 can_spell = TRUE; |
249 | 1647 |
300 | 1648 if (can_spell) |
1649 { | |
857 | 1650 found_one = TRUE; |
300 | 1651 found_pos.lnum = lnum; |
835 | 1652 found_pos.col = (int)(p - buf); |
300 | 1653 found_pos.coladd = 0; |
1654 if (dir == FORWARD) | |
1655 { | |
1656 /* No need to search further. */ | |
498 | 1657 wp->w_cursor = found_pos; |
346 | 1658 vim_free(buf); |
498 | 1659 if (attrp != NULL) |
1660 *attrp = attr; | |
495 | 1661 return len; |
249 | 1662 } |
483 | 1663 else if (curline) |
1664 /* Insert mode completion: put cursor after | |
1665 * the bad word. */ | |
1666 found_pos.col += len; | |
495 | 1667 found_len = len; |
249 | 1668 } |
236 | 1669 } |
857 | 1670 else |
1671 found_one = TRUE; | |
236 | 1672 } |
1673 } | |
1674 | |
300 | 1675 /* advance to character after the word */ |
1676 p += len; | |
385 | 1677 capcol -= len; |
236 | 1678 } |
1679 | |
483 | 1680 if (dir == BACKWARD && found_pos.lnum != 0) |
1681 { | |
500 | 1682 /* Use the last match in the line (before the cursor). */ |
498 | 1683 wp->w_cursor = found_pos; |
483 | 1684 vim_free(buf); |
495 | 1685 return found_len; |
483 | 1686 } |
1687 | |
323 | 1688 if (curline) |
346 | 1689 break; /* only check cursor line */ |
323 | 1690 |
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
|
1691 /* 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
|
1692 * 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
|
1693 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
|
1694 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
|
1695 |
236 | 1696 /* Advance to next line. */ |
249 | 1697 if (dir == BACKWARD) |
1698 { | |
500 | 1699 if (lnum > 1) |
1700 --lnum; | |
1701 else if (!p_ws) | |
1702 break; /* at first line and 'nowrapscan' */ | |
1703 else | |
1704 { | |
1705 /* Wrap around to the end of the buffer. May search the | |
1706 * starting line again and accept the last match. */ | |
1707 lnum = wp->w_buffer->b_ml.ml_line_count; | |
1708 wrapped = TRUE; | |
503 | 1709 if (!shortmess(SHM_SEARCH)) |
1710 give_warning((char_u *)_(top_bot_msg), TRUE); | |
500 | 1711 } |
385 | 1712 capcol = -1; |
249 | 1713 } |
1714 else | |
1715 { | |
500 | 1716 if (lnum < wp->w_buffer->b_ml.ml_line_count) |
1717 ++lnum; | |
1718 else if (!p_ws) | |
1719 break; /* at first line and 'nowrapscan' */ | |
1720 else | |
1721 { | |
1722 /* Wrap around to the start of the buffer. May search the | |
1723 * starting line again and accept the first match. */ | |
1724 lnum = 1; | |
1725 wrapped = TRUE; | |
503 | 1726 if (!shortmess(SHM_SEARCH)) |
1727 give_warning((char_u *)_(bot_top_msg), TRUE); | |
500 | 1728 } |
1729 | |
1730 /* If we are back at the starting line and there is no match then | |
1731 * 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
|
1732 if (lnum == wp->w_cursor.lnum && !found_one) |
346 | 1733 break; |
1734 | |
1735 /* Skip the characters at the start of the next line that were | |
1736 * included in a match crossing line boundaries. */ | |
534 | 1737 if (attr == HLF_COUNT) |
835 | 1738 skip = (int)(p - endp); |
346 | 1739 else |
1740 skip = 0; | |
385 | 1741 |
835 | 1742 /* Capcol skips over the inserted space. */ |
385 | 1743 --capcol; |
1744 | |
1745 /* But after empty line check first word in next line */ | |
1746 if (*skipwhite(line) == NUL) | |
1747 capcol = 0; | |
249 | 1748 } |
236 | 1749 |
1750 line_breakcheck(); | |
1751 } | |
1752 | |
346 | 1753 vim_free(buf); |
495 | 1754 return 0; |
346 | 1755 } |
1756 | |
1757 /* | |
1758 * For spell checking: concatenate the start of the following line "line" into | |
1759 * "buf", blanking-out special characters. Copy less then "maxlen" bytes. | |
1577 | 1760 * Keep the blanks at the start of the next line, this is used in win_line() |
1761 * to skip those bytes if the word was OK. | |
346 | 1762 */ |
1763 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1764 spell_cat_line(char_u *buf, char_u *line, int maxlen) |
346 | 1765 { |
1766 char_u *p; | |
1767 int n; | |
1768 | |
1769 p = skipwhite(line); | |
1770 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) | |
1771 p = skipwhite(p + 1); | |
1772 | |
1773 if (*p != NUL) | |
1774 { | |
1577 | 1775 /* Only worth concatenating if there is something else than spaces to |
1776 * concatenate. */ | |
1777 n = (int)(p - line) + 1; | |
1778 if (n < maxlen - 1) | |
1779 { | |
1780 vim_memset(buf, ' ', n); | |
1781 vim_strncpy(buf + n, p, maxlen - 1 - n); | |
1782 } | |
346 | 1783 } |
236 | 1784 } |
1785 | |
626 | 1786 /* |
1787 * Structure used for the cookie argument of do_in_runtimepath(). | |
1788 */ | |
501 | 1789 typedef struct spelload_S |
1790 { | |
1791 char_u sl_lang[MAXWLEN + 1]; /* language name */ | |
1792 slang_T *sl_slang; /* resulting slang_T struct */ | |
1793 int sl_nobreak; /* NOBREAK language found */ | |
1794 } spelload_T; | |
1795 | |
236 | 1796 /* |
307 | 1797 * Load word list(s) for "lang" from Vim spell file(s). |
310 | 1798 * "lang" must be the language without the region: e.g., "en". |
236 | 1799 */ |
307 | 1800 static void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1801 spell_load_lang(char_u *lang) |
236 | 1802 { |
310 | 1803 char_u fname_enc[85]; |
236 | 1804 int r; |
501 | 1805 spelload_T sl; |
649 | 1806 int round; |
307 | 1807 |
310 | 1808 /* Copy the language name to pass it to spell_load_cb() as a cookie. |
307 | 1809 * It's truncated when an error is detected. */ |
501 | 1810 STRCPY(sl.sl_lang, lang); |
1811 sl.sl_slang = NULL; | |
1812 sl.sl_nobreak = FALSE; | |
307 | 1813 |
649 | 1814 /* We may retry when no spell file is found for the language, an |
1815 * autocommand may load it then. */ | |
1816 for (round = 1; round <= 2; ++round) | |
1817 { | |
1818 /* | |
1819 * Find the first spell file for "lang" in 'runtimepath' and load it. | |
1820 */ | |
1821 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, | |
2660 | 1822 #ifdef VMS |
1823 "spell/%s_%s.spl", | |
1824 #else | |
1825 "spell/%s.%s.spl", | |
1826 #endif | |
1827 lang, spell_enc()); | |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1828 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); |
649 | 1829 |
1830 if (r == FAIL && *sl.sl_lang != NUL) | |
1831 { | |
1832 /* Try loading the ASCII version. */ | |
1833 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, | |
2660 | 1834 #ifdef VMS |
1835 "spell/%s_ascii.spl", | |
1836 #else | |
1837 "spell/%s.ascii.spl", | |
1838 #endif | |
1839 lang); | |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1840 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); |
649 | 1841 |
1842 if (r == FAIL && *sl.sl_lang != NUL && round == 1 | |
1843 && apply_autocmds(EVENT_SPELLFILEMISSING, lang, | |
1844 curbuf->b_fname, FALSE, curbuf)) | |
1845 continue; | |
1846 break; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13308
diff
changeset
|
1847 } |
714 | 1848 break; |
307 | 1849 } |
1850 | |
1851 if (r == FAIL) | |
649 | 1852 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1853 smsg( |
2660 | 1854 #ifdef VMS |
1855 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""), | |
1856 #else | |
1857 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""), | |
1858 #endif | |
483 | 1859 lang, spell_enc(), lang); |
649 | 1860 } |
501 | 1861 else if (sl.sl_slang != NULL) |
1862 { | |
626 | 1863 /* At least one file was loaded, now load ALL the additions. */ |
310 | 1864 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
|
1865 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl); |
310 | 1866 } |
1867 } | |
1868 | |
1869 /* | |
1870 * Return the encoding used for spell checking: Use 'encoding', except that we | |
1871 * use "latin1" for "latin9". And limit to 60 characters (just in case). | |
1872 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1873 char_u * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1874 spell_enc(void) |
310 | 1875 { |
1876 | |
1877 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) | |
1878 return p_enc; | |
1879 return (char_u *)"latin1"; | |
236 | 1880 } |
1881 | |
1882 /* | |
385 | 1883 * Get the name of the .spl file for the internal wordlist into |
1884 * "fname[MAXPATHL]". | |
1885 */ | |
1886 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1887 int_wordlist_spl(char_u *fname) |
385 | 1888 { |
2660 | 1889 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL, |
385 | 1890 int_wordlist, spell_enc()); |
1891 } | |
1892 | |
1893 /* | |
625 | 1894 * Allocate a new slang_T for language "lang". "lang" can be NULL. |
236 | 1895 * Caller must fill "sl_next". |
1896 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1897 slang_T * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1898 slang_alloc(char_u *lang) |
236 | 1899 { |
1900 slang_T *lp; | |
1901 | |
300 | 1902 lp = (slang_T *)alloc_clear(sizeof(slang_T)); |
236 | 1903 if (lp != NULL) |
1904 { | |
625 | 1905 if (lang != NULL) |
1906 lp->sl_name = vim_strsave(lang); | |
323 | 1907 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
625 | 1908 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10); |
483 | 1909 lp->sl_compmax = MAXWLEN; |
1910 lp->sl_compsylmax = MAXWLEN; | |
625 | 1911 hash_init(&lp->sl_wordcount); |
1912 } | |
1913 | |
236 | 1914 return lp; |
1915 } | |
1916 | |
1917 /* | |
1918 * Free the contents of an slang_T and the structure itself. | |
1919 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1920 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1921 slang_free(slang_T *lp) |
236 | 1922 { |
1923 vim_free(lp->sl_name); | |
310 | 1924 vim_free(lp->sl_fname); |
1925 slang_clear(lp); | |
1926 vim_free(lp); | |
1927 } | |
1928 | |
1929 /* | |
1930 * Clear an slang_T so that the file can be reloaded. | |
1931 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1932 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1933 slang_clear(slang_T *lp) |
310 | 1934 { |
339 | 1935 garray_T *gap; |
1936 fromto_T *ftp; | |
344 | 1937 salitem_T *smp; |
339 | 1938 int i; |
625 | 1939 int round; |
323 | 1940 |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1941 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
|
1942 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
|
1943 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
|
1944 |
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_fidxs); |
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_kidxs); |
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_pidxs); |
323 | 1948 |
625 | 1949 for (round = 1; round <= 2; ++round) |
1950 { | |
1951 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal; | |
1952 while (gap->ga_len > 0) | |
1953 { | |
1954 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; | |
1955 vim_free(ftp->ft_from); | |
1956 vim_free(ftp->ft_to); | |
1957 } | |
1958 ga_clear(gap); | |
1959 } | |
344 | 1960 |
1961 gap = &lp->sl_sal; | |
375 | 1962 if (lp->sl_sofo) |
376 | 1963 { |
1964 /* "ga_len" is set to 1 without adding an item for latin1 */ | |
1965 if (gap->ga_data != NULL) | |
1966 /* SOFOFROM and SOFOTO items: free lists of wide characters. */ | |
1967 for (i = 0; i < gap->ga_len; ++i) | |
1968 vim_free(((int **)gap->ga_data)[i]); | |
1969 } | |
375 | 1970 else |
1971 /* SAL items: free salitem_T items */ | |
1972 while (gap->ga_len > 0) | |
1973 { | |
1974 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; | |
1975 vim_free(smp->sm_lead); | |
1976 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */ | |
1977 vim_free(smp->sm_to); | |
1978 vim_free(smp->sm_lead_w); | |
1979 vim_free(smp->sm_oneof_w); | |
1980 vim_free(smp->sm_to_w); | |
1981 } | |
344 | 1982 ga_clear(gap); |
323 | 1983 |
339 | 1984 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
|
1985 vim_regfree(lp->sl_prefprog[i]); |
376 | 1986 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
|
1987 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
|
1988 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1989 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
|
1990 |
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_midword); |
339 | 1992 |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1993 vim_regfree(lp->sl_compprog); |
483 | 1994 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
|
1995 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
|
1996 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
|
1997 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
|
1998 |
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_syllable); |
483 | 2000 ga_clear(&lp->sl_syl_items); |
481 | 2001 |
809 | 2002 ga_clear_strings(&lp->sl_comppat); |
2003 | |
625 | 2004 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF); |
2005 hash_init(&lp->sl_wordcount); | |
2006 | |
2007 hash_clear_all(&lp->sl_map_hash, 0); | |
2008 | |
2009 /* Clear info from .sug file. */ | |
2010 slang_clear_sug(lp); | |
483 | 2011 |
2012 lp->sl_compmax = MAXWLEN; | |
501 | 2013 lp->sl_compminlen = 0; |
483 | 2014 lp->sl_compsylmax = MAXWLEN; |
2015 lp->sl_regions[0] = NUL; | |
236 | 2016 } |
2017 | |
2018 /* | |
625 | 2019 * Clear the info from the .sug file in "lp". |
2020 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2021 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2022 slang_clear_sug(slang_T *lp) |
625 | 2023 { |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2024 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
|
2025 VIM_CLEAR(lp->sl_sidxs); |
625 | 2026 close_spellbuf(lp->sl_sugbuf); |
2027 lp->sl_sugbuf = NULL; | |
2028 lp->sl_sugloaded = FALSE; | |
2029 lp->sl_sugtime = 0; | |
2030 } | |
2031 | |
2032 /* | |
307 | 2033 * Load one spell file and store the info into a slang_T. |
236 | 2034 * Invoked through do_in_runtimepath(). |
2035 */ | |
2036 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2037 spell_load_cb(char_u *fname, void *cookie) |
501 | 2038 { |
2039 spelload_T *slp = (spelload_T *)cookie; | |
2040 slang_T *slang; | |
2041 | |
2042 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE); | |
2043 if (slang != NULL) | |
2044 { | |
2045 /* When a previously loaded file has NOBREAK also use it for the | |
2046 * ".add" files. */ | |
2047 if (slp->sl_nobreak && slang->sl_add) | |
2048 slang->sl_nobreak = TRUE; | |
2049 else if (slang->sl_nobreak) | |
2050 slp->sl_nobreak = TRUE; | |
2051 | |
2052 slp->sl_slang = slang; | |
2053 } | |
310 | 2054 } |
2055 | |
625 | 2056 |
2057 /* | |
2058 * Add a word to the hashtable of common words. | |
2059 * If it's already there then the counter is increased. | |
2060 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2061 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2062 count_common_word( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2063 slang_T *lp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2064 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2065 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
|
2066 int count) /* 1 to count once, 10 to init */ |
625 | 2067 { |
2068 hash_T hash; | |
2069 hashitem_T *hi; | |
2070 wordcount_T *wc; | |
2071 char_u buf[MAXWLEN]; | |
2072 char_u *p; | |
2073 | |
2074 if (len == -1) | |
2075 p = word; | |
2076 else | |
2077 { | |
2078 vim_strncpy(buf, word, len); | |
2079 p = buf; | |
2080 } | |
2081 | |
2082 hash = hash_hash(p); | |
2083 hi = hash_lookup(&lp->sl_wordcount, p, hash); | |
2084 if (HASHITEM_EMPTY(hi)) | |
2085 { | |
835 | 2086 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p))); |
625 | 2087 if (wc == NULL) |
2088 return; | |
2089 STRCPY(wc->wc_word, p); | |
2090 wc->wc_count = count; | |
2091 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash); | |
2092 } | |
2093 else | |
2094 { | |
2095 wc = HI2WC(hi); | |
2096 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */ | |
2097 wc->wc_count = MAXWORDCOUNT; | |
2098 } | |
2099 } | |
2100 | |
2101 /* | |
2102 * Adjust the score of common words. | |
2103 */ | |
2104 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2105 score_wordcount_adj( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2106 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2107 int score, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2108 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2109 int split) /* word was split, less bonus */ |
625 | 2110 { |
2111 hashitem_T *hi; | |
2112 wordcount_T *wc; | |
2113 int bonus; | |
2114 int newscore; | |
2115 | |
2116 hi = hash_find(&slang->sl_wordcount, word); | |
2117 if (!HASHITEM_EMPTY(hi)) | |
2118 { | |
2119 wc = HI2WC(hi); | |
2120 if (wc->wc_count < SCORE_THRES2) | |
2121 bonus = SCORE_COMMON1; | |
2122 else if (wc->wc_count < SCORE_THRES3) | |
2123 bonus = SCORE_COMMON2; | |
2124 else | |
2125 bonus = SCORE_COMMON3; | |
2126 if (split) | |
2127 newscore = score - bonus / 2; | |
2128 else | |
2129 newscore = score - bonus; | |
2130 if (newscore < 0) | |
2131 return 0; | |
2132 return newscore; | |
2133 } | |
2134 return score; | |
2135 } | |
2136 | |
483 | 2137 |
495 | 2138 /* |
498 | 2139 * Return TRUE if byte "n" appears in "str". |
495 | 2140 * Like strchr() but independent of locale. |
2141 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2142 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2143 byte_in_str(char_u *str, int n) |
495 | 2144 { |
2145 char_u *p; | |
2146 | |
2147 for (p = str; *p != NUL; ++p) | |
498 | 2148 if (*p == n) |
495 | 2149 return TRUE; |
2150 return FALSE; | |
2151 } | |
2152 | |
483 | 2153 #define SY_MAXLEN 30 |
2154 typedef struct syl_item_S | |
2155 { | |
2156 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */ | |
2157 int sy_len; | |
2158 } syl_item_T; | |
2159 | |
2160 /* | |
2161 * Truncate "slang->sl_syllable" at the first slash and put the following items | |
2162 * in "slang->sl_syl_items". | |
2163 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2164 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2165 init_syl_tab(slang_T *slang) |
483 | 2166 { |
2167 char_u *p; | |
2168 char_u *s; | |
2169 int l; | |
2170 syl_item_T *syl; | |
2171 | |
2172 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4); | |
2173 p = vim_strchr(slang->sl_syllable, '/'); | |
2174 while (p != NULL) | |
2175 { | |
2176 *p++ = NUL; | |
495 | 2177 if (*p == NUL) /* trailing slash */ |
483 | 2178 break; |
2179 s = p; | |
2180 p = vim_strchr(p, '/'); | |
2181 if (p == NULL) | |
835 | 2182 l = (int)STRLEN(s); |
483 | 2183 else |
835 | 2184 l = (int)(p - s); |
483 | 2185 if (l >= SY_MAXLEN) |
2186 return SP_FORMERROR; | |
2187 if (ga_grow(&slang->sl_syl_items, 1) == FAIL) | |
495 | 2188 return SP_OTHERERROR; |
483 | 2189 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) |
2190 + slang->sl_syl_items.ga_len++; | |
2191 vim_strncpy(syl->sy_chars, s, l); | |
2192 syl->sy_len = l; | |
2193 } | |
2194 return OK; | |
2195 } | |
2196 | |
2197 /* | |
2198 * Count the number of syllables in "word". | |
2199 * When "word" contains spaces the syllables after the last space are counted. | |
2200 * Returns zero if syllables are not defines. | |
2201 */ | |
2202 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2203 count_syllables(slang_T *slang, char_u *word) |
483 | 2204 { |
2205 int cnt = 0; | |
2206 int skip = FALSE; | |
2207 char_u *p; | |
2208 int len; | |
2209 int i; | |
2210 syl_item_T *syl; | |
2211 int c; | |
2212 | |
2213 if (slang->sl_syllable == NULL) | |
2214 return 0; | |
2215 | |
2216 for (p = word; *p != NUL; p += len) | |
2217 { | |
2218 /* When running into a space reset counter. */ | |
2219 if (*p == ' ') | |
2220 { | |
2221 len = 1; | |
2222 cnt = 0; | |
2223 continue; | |
2224 } | |
2225 | |
2226 /* Find longest match of syllable items. */ | |
2227 len = 0; | |
2228 for (i = 0; i < slang->sl_syl_items.ga_len; ++i) | |
2229 { | |
2230 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i; | |
2231 if (syl->sy_len > len | |
2232 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0) | |
2233 len = syl->sy_len; | |
2234 } | |
2235 if (len != 0) /* found a match, count syllable */ | |
2236 { | |
2237 ++cnt; | |
2238 skip = FALSE; | |
2239 } | |
2240 else | |
2241 { | |
2242 /* No recognized syllable item, at least a syllable char then? */ | |
2243 c = mb_ptr2char(p); | |
2244 len = (*mb_ptr2len)(p); | |
2245 if (vim_strchr(slang->sl_syllable, c) == NULL) | |
2246 skip = FALSE; /* No, search for next syllable */ | |
2247 else if (!skip) | |
2248 { | |
2249 ++cnt; /* Yes, count it */ | |
2250 skip = TRUE; /* don't count following syllable chars */ | |
2251 } | |
2252 } | |
2253 } | |
2254 return cnt; | |
376 | 2255 } |
2256 | |
381 | 2257 /* |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2258 * Parse 'spelllang' and set w_s->b_langp accordingly. |
351 | 2259 * Returns NULL if it's OK, an error message otherwise. |
236 | 2260 */ |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2261 char * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2262 did_set_spelllang(win_T *wp) |
236 | 2263 { |
2264 garray_T ga; | |
351 | 2265 char_u *splp; |
236 | 2266 char_u *region; |
416 | 2267 char_u region_cp[3]; |
355 | 2268 int filename; |
236 | 2269 int region_mask; |
503 | 2270 slang_T *slang; |
236 | 2271 int c; |
351 | 2272 char_u lang[MAXWLEN + 1]; |
323 | 2273 char_u spf_name[MAXPATHL]; |
351 | 2274 int len; |
2275 char_u *p; | |
381 | 2276 int round; |
385 | 2277 char_u *spf; |
389 | 2278 char_u *use_region = NULL; |
2279 int dont_use_region = FALSE; | |
501 | 2280 int nobreak = FALSE; |
503 | 2281 int i, j; |
2282 langp_T *lp, *lp2; | |
1185 | 2283 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
|
2284 char *ret_msg = NULL; |
1185 | 2285 char_u *spl_copy; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2286 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2287 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2288 set_bufref(&bufref, wp->w_buffer); |
1185 | 2289 |
2290 /* We don't want to do this recursively. May happen when a language is | |
2291 * not available and the SpellFileMissing autocommand opens a new buffer | |
2292 * in which 'spell' is set. */ | |
2293 if (recursive) | |
2294 return NULL; | |
2295 recursive = TRUE; | |
236 | 2296 |
2297 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
|
2298 clear_midword(wp); |
236 | 2299 |
4352 | 2300 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change |
1185 | 2301 * it under our fingers. */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2302 spl_copy = vim_strsave(wp->w_s->b_p_spl); |
1185 | 2303 if (spl_copy == NULL) |
2304 goto theend; | |
2305 | |
5477 | 2306 wp->w_s->b_cjk = 0; |
2307 | |
2308 /* Loop over comma separated language names. */ | |
1185 | 2309 for (splp = spl_copy; *splp != NUL; ) |
351 | 2310 { |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2311 // Get one language name. |
351 | 2312 copy_option_part(&splp, lang, MAXWLEN, ","); |
240 | 2313 region = NULL; |
835 | 2314 len = (int)STRLEN(lang); |
355 | 2315 |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2316 if (!valid_spellang(lang)) |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2317 continue; |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2318 |
5477 | 2319 if (STRCMP(lang, "cjk") == 0) |
2320 { | |
2321 wp->w_s->b_cjk = 1; | |
2322 continue; | |
2323 } | |
2324 | |
355 | 2325 /* If the name ends in ".spl" use it as the name of the spell file. |
2326 * If there is a region name let "region" point to it and remove it | |
2327 * from the name. */ | |
2328 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) | |
236 | 2329 { |
355 | 2330 filename = TRUE; |
2331 | |
416 | 2332 /* Locate a region and remove it from the file name. */ |
2333 p = vim_strchr(gettail(lang), '_'); | |
2334 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2]) | |
2335 && !ASCII_ISALPHA(p[3])) | |
2336 { | |
2337 vim_strncpy(region_cp, p + 1, 2); | |
2338 mch_memmove(p, p + 3, len - (p - lang) - 2); | |
2339 region = region_cp; | |
2340 } | |
2341 else | |
2342 dont_use_region = TRUE; | |
2343 | |
355 | 2344 /* Check if we loaded this language before. */ |
503 | 2345 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2346 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME) | |
355 | 2347 break; |
236 | 2348 } |
355 | 2349 else |
2350 { | |
2351 filename = FALSE; | |
2352 if (len > 3 && lang[len - 3] == '_') | |
2353 { | |
2354 region = lang + len - 2; | |
2355 len -= 3; | |
2356 lang[len] = NUL; | |
389 | 2357 } |
2358 else | |
2359 dont_use_region = TRUE; | |
355 | 2360 |
2361 /* Check if we loaded this language before. */ | |
503 | 2362 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2363 if (STRICMP(lang, slang->sl_name) == 0) | |
355 | 2364 break; |
2365 } | |
236 | 2366 |
416 | 2367 if (region != NULL) |
2368 { | |
2369 /* If the region differs from what was used before then don't | |
2370 * use it for 'spellfile'. */ | |
2371 if (use_region != NULL && STRCMP(region, use_region) != 0) | |
2372 dont_use_region = TRUE; | |
2373 use_region = region; | |
2374 } | |
2375 | |
351 | 2376 /* If not found try loading the language now. */ |
503 | 2377 if (slang == NULL) |
355 | 2378 { |
2379 if (filename) | |
2380 (void)spell_load_file(lang, lang, NULL, FALSE); | |
2381 else | |
1185 | 2382 { |
355 | 2383 spell_load_lang(lang); |
1185 | 2384 /* SpellFileMissing autocommands may do anything, including |
2385 * 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
|
2386 if (!bufref_valid(&bufref)) |
1185 | 2387 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2388 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer"); |
1185 | 2389 goto theend; |
2390 } | |
2391 } | |
355 | 2392 } |
236 | 2393 |
307 | 2394 /* |
351 | 2395 * Loop over the languages, there can be several files for "lang". |
307 | 2396 */ |
503 | 2397 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2398 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME | |
2399 : STRICMP(lang, slang->sl_name) == 0) | |
236 | 2400 { |
316 | 2401 region_mask = REGION_ALL; |
355 | 2402 if (!filename && region != NULL) |
236 | 2403 { |
307 | 2404 /* find region in sl_regions */ |
503 | 2405 c = find_region(slang->sl_regions, region); |
307 | 2406 if (c == REGION_ALL) |
2407 { | |
503 | 2408 if (slang->sl_add) |
389 | 2409 { |
503 | 2410 if (*slang->sl_regions != NUL) |
389 | 2411 /* This addition file is for other regions. */ |
2412 region_mask = 0; | |
2413 } | |
2414 else | |
2415 /* This is probably an error. Give a warning and | |
2416 * 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
|
2417 smsg(_("Warning: region %s not supported"), |
351 | 2418 region); |
307 | 2419 } |
2420 else | |
2421 region_mask = 1 << c; | |
236 | 2422 } |
307 | 2423 |
389 | 2424 if (region_mask != 0) |
307 | 2425 { |
389 | 2426 if (ga_grow(&ga, 1) == FAIL) |
2427 { | |
2428 ga_clear(&ga); | |
1185 | 2429 ret_msg = e_outofmem; |
2430 goto theend; | |
389 | 2431 } |
503 | 2432 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
389 | 2433 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
2434 ++ga.ga_len; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2435 use_midword(slang, wp); |
503 | 2436 if (slang->sl_nobreak) |
501 | 2437 nobreak = TRUE; |
307 | 2438 } |
385 | 2439 } |
2440 } | |
2441 | |
2442 /* round 0: load int_wordlist, if possible. | |
2443 * round 1: load first name in 'spellfile'. | |
2444 * round 2: load second name in 'spellfile. | |
2445 * etc. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2446 spf = curwin->w_s->b_p_spf; |
385 | 2447 for (round = 0; round == 0 || *spf != NUL; ++round) |
2448 { | |
2449 if (round == 0) | |
2450 { | |
2451 /* Internal wordlist, if there is one. */ | |
2452 if (int_wordlist == NULL) | |
381 | 2453 continue; |
385 | 2454 int_wordlist_spl(spf_name); |
381 | 2455 } |
2456 else | |
2457 { | |
385 | 2458 /* One entry in 'spellfile'. */ |
2459 copy_option_part(&spf, spf_name, MAXPATHL - 5, ","); | |
2460 STRCAT(spf_name, ".spl"); | |
2461 | |
2462 /* If it was already found above then skip it. */ | |
2463 for (c = 0; c < ga.ga_len; ++c) | |
500 | 2464 { |
2465 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname; | |
2466 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME) | |
385 | 2467 break; |
500 | 2468 } |
385 | 2469 if (c < ga.ga_len) |
381 | 2470 continue; |
2471 } | |
2472 | |
351 | 2473 /* Check if it was loaded already. */ |
503 | 2474 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2475 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME) | |
323 | 2476 break; |
503 | 2477 if (slang == NULL) |
323 | 2478 { |
351 | 2479 /* Not loaded, try loading it now. The language name includes the |
385 | 2480 * region name, the region is ignored otherwise. for int_wordlist |
2481 * use an arbitrary name. */ | |
2482 if (round == 0) | |
2483 STRCPY(lang, "internal wordlist"); | |
2484 else | |
2485 { | |
2486 vim_strncpy(lang, gettail(spf_name), MAXWLEN); | |
381 | 2487 p = vim_strchr(lang, '.'); |
2488 if (p != NULL) | |
2489 *p = NUL; /* truncate at ".encoding.add" */ | |
2490 } | |
503 | 2491 slang = spell_load_file(spf_name, lang, NULL, TRUE); |
501 | 2492 |
2493 /* If one of the languages has NOBREAK we assume the addition | |
2494 * files also have this. */ | |
503 | 2495 if (slang != NULL && nobreak) |
2496 slang->sl_nobreak = TRUE; | |
2497 } | |
2498 if (slang != NULL && ga_grow(&ga, 1) == OK) | |
323 | 2499 { |
389 | 2500 region_mask = REGION_ALL; |
2501 if (use_region != NULL && !dont_use_region) | |
2502 { | |
2503 /* find region in sl_regions */ | |
503 | 2504 c = find_region(slang->sl_regions, use_region); |
389 | 2505 if (c != REGION_ALL) |
2506 region_mask = 1 << c; | |
503 | 2507 else if (*slang->sl_regions != NUL) |
389 | 2508 /* This spell file is for other regions. */ |
2509 region_mask = 0; | |
2510 } | |
2511 | |
2512 if (region_mask != 0) | |
2513 { | |
503 | 2514 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
2515 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL; | |
2516 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL; | |
389 | 2517 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
2518 ++ga.ga_len; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2519 use_midword(slang, wp); |
389 | 2520 } |
323 | 2521 } |
2522 } | |
2523 | |
236 | 2524 /* 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
|
2525 ga_clear(&wp->w_s->b_langp); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2526 wp->w_s->b_langp = ga; |
236 | 2527 |
503 | 2528 /* For each language figure out what language to use for sound folding and |
2529 * REP items. If the language doesn't support it itself use another one | |
2530 * with the same name. E.g. for "en-math" use "en". */ | |
2531 for (i = 0; i < ga.ga_len; ++i) | |
2532 { | |
2533 lp = LANGP_ENTRY(ga, i); | |
2534 | |
2535 /* sound folding */ | |
2536 if (lp->lp_slang->sl_sal.ga_len > 0) | |
2537 /* language does sound folding itself */ | |
2538 lp->lp_sallang = lp->lp_slang; | |
2539 else | |
2540 /* find first similar language that does sound folding */ | |
2541 for (j = 0; j < ga.ga_len; ++j) | |
2542 { | |
2543 lp2 = LANGP_ENTRY(ga, j); | |
2544 if (lp2->lp_slang->sl_sal.ga_len > 0 | |
2545 && STRNCMP(lp->lp_slang->sl_name, | |
2546 lp2->lp_slang->sl_name, 2) == 0) | |
2547 { | |
2548 lp->lp_sallang = lp2->lp_slang; | |
2549 break; | |
2550 } | |
2551 } | |
2552 | |
2553 /* REP items */ | |
2554 if (lp->lp_slang->sl_rep.ga_len > 0) | |
2555 /* language has REP items itself */ | |
2556 lp->lp_replang = lp->lp_slang; | |
2557 else | |
625 | 2558 /* find first similar language that has REP items */ |
503 | 2559 for (j = 0; j < ga.ga_len; ++j) |
2560 { | |
2561 lp2 = LANGP_ENTRY(ga, j); | |
2562 if (lp2->lp_slang->sl_rep.ga_len > 0 | |
2563 && STRNCMP(lp->lp_slang->sl_name, | |
2564 lp2->lp_slang->sl_name, 2) == 0) | |
2565 { | |
2566 lp->lp_replang = lp2->lp_slang; | |
2567 break; | |
2568 } | |
2569 } | |
2570 } | |
2571 | |
1185 | 2572 theend: |
2573 vim_free(spl_copy); | |
2574 recursive = FALSE; | |
5891 | 2575 redraw_win_later(wp, NOT_VALID); |
1185 | 2576 return ret_msg; |
236 | 2577 } |
2578 | |
2579 /* | |
376 | 2580 * Clear the midword characters for buffer "buf". |
2581 */ | |
2582 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2583 clear_midword(win_T *wp) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2584 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2585 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
|
2586 VIM_CLEAR(wp->w_s->b_spell_ismw_mb); |
376 | 2587 } |
2588 | |
2589 /* | |
2590 * Use the "sl_midword" field of language "lp" for buffer "buf". | |
2591 * They add up to any currently used midword characters. | |
2592 */ | |
2593 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2594 use_midword(slang_T *lp, win_T *wp) |
376 | 2595 { |
2596 char_u *p; | |
2597 | |
389 | 2598 if (lp->sl_midword == NULL) /* there aren't any */ |
2599 return; | |
2600 | |
376 | 2601 for (p = lp->sl_midword; *p != NUL; ) |
2602 if (has_mbyte) | |
2603 { | |
2604 int c, l, n; | |
2605 char_u *bp; | |
2606 | |
2607 c = mb_ptr2char(p); | |
474 | 2608 l = (*mb_ptr2len)(p); |
2609 if (c < 256 && l <= 2) | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2610 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
|
2611 else if (wp->w_s->b_spell_ismw_mb == NULL) |
376 | 2612 /* 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
|
2613 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l); |
376 | 2614 else |
2615 { | |
2616 /* 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
|
2617 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
|
2618 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l); |
376 | 2619 if (bp != NULL) |
2620 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2621 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
|
2622 wp->w_s->b_spell_ismw_mb = bp; |
376 | 2623 vim_strncpy(bp + n, p, l); |
2624 } | |
2625 } | |
2626 p += l; | |
2627 } | |
2628 else | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2629 wp->w_s->b_spell_ismw[*p++] = TRUE; |
376 | 2630 } |
2631 | |
2632 /* | |
236 | 2633 * 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
|
2634 * Each region is simply stored as the two characters of its name. |
381 | 2635 * Returns the index if found (first is 0), REGION_ALL if not found. |
236 | 2636 */ |
2637 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2638 find_region(char_u *rp, char_u *region) |
236 | 2639 { |
2640 int i; | |
2641 | |
2642 for (i = 0; ; i += 2) | |
2643 { | |
2644 if (rp[i] == NUL) | |
2645 return REGION_ALL; | |
2646 if (rp[i] == region[0] && rp[i + 1] == region[1]) | |
2647 break; | |
2648 } | |
2649 return i / 2; | |
2650 } | |
2651 | |
2652 /* | |
323 | 2653 * Return case type of word: |
236 | 2654 * w word 0 |
300 | 2655 * Word WF_ONECAP |
2656 * W WORD WF_ALLCAP | |
2657 * WoRd wOrd WF_KEEPCAP | |
236 | 2658 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2659 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2660 captype( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2661 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2662 char_u *end) /* When NULL use up to NUL byte. */ |
236 | 2663 { |
2664 char_u *p; | |
2665 int c; | |
2666 int firstcap; | |
2667 int allcap; | |
2668 int past_second = FALSE; /* past second word char */ | |
2669 | |
2670 /* find first letter */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2671 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p)) |
323 | 2672 if (end == NULL ? *p == NUL : p >= end) |
236 | 2673 return 0; /* only non-word characters, illegal word */ |
310 | 2674 if (has_mbyte) |
2675 c = mb_ptr2char_adv(&p); | |
2676 else | |
2677 c = *p++; | |
324 | 2678 firstcap = allcap = SPELL_ISUPPER(c); |
236 | 2679 |
2680 /* | |
2681 * Need to check all letters to find a word with mixed upper/lower. | |
2682 * But a word with an upper char only at start is a ONECAP. | |
2683 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2684 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p)) |
5477 | 2685 if (spell_iswordp_nmw(p, curwin)) |
236 | 2686 { |
455 | 2687 c = PTR2CHAR(p); |
324 | 2688 if (!SPELL_ISUPPER(c)) |
236 | 2689 { |
2690 /* UUl -> KEEPCAP */ | |
2691 if (past_second && allcap) | |
300 | 2692 return WF_KEEPCAP; |
236 | 2693 allcap = FALSE; |
2694 } | |
2695 else if (!allcap) | |
2696 /* UlU -> KEEPCAP */ | |
300 | 2697 return WF_KEEPCAP; |
236 | 2698 past_second = TRUE; |
2699 } | |
2700 | |
2701 if (allcap) | |
300 | 2702 return WF_ALLCAP; |
236 | 2703 if (firstcap) |
300 | 2704 return WF_ONECAP; |
236 | 2705 return 0; |
2706 } | |
2707 | |
474 | 2708 /* |
2709 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a | |
2710 * capital. So that make_case_word() can turn WOrd into Word. | |
2711 * Add ALLCAP for "WOrD". | |
2712 */ | |
2713 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2714 badword_captype(char_u *word, char_u *end) |
474 | 2715 { |
2716 int flags = captype(word, end); | |
475 | 2717 int c; |
474 | 2718 int l, u; |
2719 int first; | |
2720 char_u *p; | |
2721 | |
2722 if (flags & WF_KEEPCAP) | |
2723 { | |
2724 /* Count the number of UPPER and lower case letters. */ | |
2725 l = u = 0; | |
2726 first = FALSE; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2727 for (p = word; p < end; MB_PTR_ADV(p)) |
474 | 2728 { |
475 | 2729 c = PTR2CHAR(p); |
2730 if (SPELL_ISUPPER(c)) | |
474 | 2731 { |
2732 ++u; | |
2733 if (p == word) | |
2734 first = TRUE; | |
2735 } | |
2736 else | |
2737 ++l; | |
2738 } | |
2739 | |
2740 /* If there are more UPPER than lower case letters suggest an | |
2741 * ALLCAP word. Otherwise, if the first letter is UPPER then | |
2742 * suggest ONECAP. Exception: "ALl" most likely should be "All", | |
2743 * require three upper case letters. */ | |
2744 if (u > l && u > 2) | |
2745 flags |= WF_ALLCAP; | |
2746 else if (first) | |
2747 flags |= WF_ONECAP; | |
633 | 2748 |
2749 if (u >= 2 && l >= 2) /* maCARONI maCAroni */ | |
2750 flags |= WF_MIXCAP; | |
474 | 2751 } |
2752 return flags; | |
2753 } | |
2754 | |
5519 | 2755 /* |
2756 * Delete the internal wordlist and its .spl file. | |
2757 */ | |
2758 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2759 spell_delete_wordlist(void) |
5519 | 2760 { |
2761 char_u fname[MAXPATHL]; | |
2762 | |
2763 if (int_wordlist != NULL) | |
2764 { | |
2765 mch_remove(int_wordlist); | |
2766 int_wordlist_spl(fname); | |
2767 mch_remove(fname); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2768 VIM_CLEAR(int_wordlist); |
5519 | 2769 } |
2770 } | |
2771 | |
355 | 2772 /* |
2773 * Free all languages. | |
2774 */ | |
2775 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2776 spell_free_all(void) |
355 | 2777 { |
503 | 2778 slang_T *slang; |
355 | 2779 buf_T *buf; |
2780 | |
2580 | 2781 /* 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
|
2782 FOR_ALL_BUFFERS(buf) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2783 ga_clear(&buf->b_s.b_langp); |
355 | 2784 |
2785 while (first_lang != NULL) | |
2786 { | |
503 | 2787 slang = first_lang; |
2788 first_lang = slang->sl_next; | |
2789 slang_free(slang); | |
355 | 2790 } |
366 | 2791 |
5519 | 2792 spell_delete_wordlist(); |
381 | 2793 |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2794 VIM_CLEAR(repl_to); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2795 VIM_CLEAR(repl_from); |
355 | 2796 } |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
2797 |
236 | 2798 /* |
2799 * Clear all spelling tables and reload them. | |
307 | 2800 * Used after 'encoding' is set and when ":mkspell" was used. |
236 | 2801 */ |
2802 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2803 spell_reload(void) |
236 | 2804 { |
316 | 2805 win_T *wp; |
236 | 2806 |
358 | 2807 /* Initialize the table for spell_iswordp(). */ |
236 | 2808 init_spell_chartab(); |
2809 | |
2810 /* Unload all allocated memory. */ | |
355 | 2811 spell_free_all(); |
236 | 2812 |
2813 /* 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
|
2814 FOR_ALL_WINDOWS(wp) |
236 | 2815 { |
316 | 2816 /* Only load the wordlists when 'spelllang' is set and there is a |
2817 * 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
|
2818 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
|
2819 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2820 if (wp->w_p_spell) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2821 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2822 (void)did_set_spelllang(wp); |
316 | 2823 break; |
2824 } | |
2825 } | |
236 | 2826 } |
2827 } | |
2828 | |
310 | 2829 /* |
625 | 2830 * Opposite of offset2bytes(). |
2831 * "pp" points to the bytes and is advanced over it. | |
2832 * Returns the offset. | |
2833 */ | |
2834 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2835 bytes2offset(char_u **pp) |
625 | 2836 { |
2837 char_u *p = *pp; | |
2838 int nr; | |
2839 int c; | |
2840 | |
2841 c = *p++; | |
2842 if ((c & 0x80) == 0x00) /* 1 byte */ | |
2843 { | |
2844 nr = c - 1; | |
2845 } | |
2846 else if ((c & 0xc0) == 0x80) /* 2 bytes */ | |
2847 { | |
2848 nr = (c & 0x3f) - 1; | |
2849 nr = nr * 255 + (*p++ - 1); | |
2850 } | |
2851 else if ((c & 0xe0) == 0xc0) /* 3 bytes */ | |
2852 { | |
2853 nr = (c & 0x1f) - 1; | |
2854 nr = nr * 255 + (*p++ - 1); | |
2855 nr = nr * 255 + (*p++ - 1); | |
2856 } | |
2857 else /* 4 bytes */ | |
2858 { | |
2859 nr = (c & 0x0f) - 1; | |
2860 nr = nr * 255 + (*p++ - 1); | |
2861 nr = nr * 255 + (*p++ - 1); | |
2862 nr = nr * 255 + (*p++ - 1); | |
2863 } | |
2864 | |
2865 *pp = p; | |
2866 return nr; | |
2867 } | |
2868 | |
2869 | |
2870 /* | |
2871 * Open a spell buffer. This is a nameless buffer that is not in the buffer | |
2872 * list and only contains text lines. Can use a swapfile to reduce memory | |
2873 * use. | |
2874 * Most other fields are invalid! Esp. watch out for string options being | |
2875 * NULL and there is no undo info. | |
2876 * Returns NULL when out of memory. | |
2877 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2878 buf_T * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2879 open_spellbuf(void) |
625 | 2880 { |
2881 buf_T *buf; | |
2882 | |
2883 buf = (buf_T *)alloc_clear(sizeof(buf_T)); | |
2884 if (buf != NULL) | |
2885 { | |
2886 buf->b_spell = TRUE; | |
2887 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
|
2888 #ifdef FEAT_CRYPT |
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2889 buf->b_p_key = empty_option; |
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2890 #endif |
625 | 2891 ml_open(buf); |
2892 ml_open_file(buf); /* create swap file now */ | |
2893 } | |
2894 return buf; | |
2895 } | |
2896 | |
2897 /* | |
2898 * Close the buffer used for spell info. | |
2899 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2900 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2901 close_spellbuf(buf_T *buf) |
625 | 2902 { |
2903 if (buf != NULL) | |
2904 { | |
2905 ml_close(buf, TRUE); | |
2906 vim_free(buf); | |
2907 } | |
2908 } | |
2909 | |
307 | 2910 /* |
2911 * Init the chartab used for spelling for ASCII. | |
2912 * EBCDIC is not supported! | |
2913 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2914 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2915 clear_spell_chartab(spelltab_T *sp) |
307 | 2916 { |
324 | 2917 int i; |
307 | 2918 |
2919 /* Init everything to FALSE. */ | |
2920 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); | |
2921 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); | |
2922 for (i = 0; i < 256; ++i) | |
324 | 2923 { |
307 | 2924 sp->st_fold[i] = i; |
324 | 2925 sp->st_upper[i] = i; |
2926 } | |
307 | 2927 |
2928 /* We include digits. A word shouldn't start with a digit, but handling | |
2929 * that is done separately. */ | |
2930 for (i = '0'; i <= '9'; ++i) | |
2931 sp->st_isw[i] = TRUE; | |
2932 for (i = 'A'; i <= 'Z'; ++i) | |
2933 { | |
2934 sp->st_isw[i] = TRUE; | |
2935 sp->st_isu[i] = TRUE; | |
2936 sp->st_fold[i] = i + 0x20; | |
2937 } | |
2938 for (i = 'a'; i <= 'z'; ++i) | |
324 | 2939 { |
307 | 2940 sp->st_isw[i] = TRUE; |
324 | 2941 sp->st_upper[i] = i - 0x20; |
2942 } | |
307 | 2943 } |
2944 | |
2945 /* | |
2946 * Init the chartab used for spelling. Only depends on 'encoding'. | |
2947 * Called once while starting up and when 'encoding' changes. | |
2948 * The default is to use isalpha(), but the spell file should define the word | |
2949 * characters to make it possible that 'encoding' differs from the current | |
390 | 2950 * locale. For utf-8 we don't use isalpha() but our own functions. |
307 | 2951 */ |
2952 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2953 init_spell_chartab(void) |
307 | 2954 { |
2955 int i; | |
2956 | |
2957 did_set_spelltab = FALSE; | |
2958 clear_spell_chartab(&spelltab); | |
2959 if (enc_dbcs) | |
2960 { | |
2961 /* DBCS: assume double-wide characters are word characters. */ | |
2962 for (i = 128; i <= 255; ++i) | |
2963 if (MB_BYTE2LEN(i) == 2) | |
2964 spelltab.st_isw[i] = TRUE; | |
2965 } | |
324 | 2966 else if (enc_utf8) |
2967 { | |
2968 for (i = 128; i < 256; ++i) | |
2969 { | |
2140
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2970 int f = utf_fold(i); |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2971 int u = utf_toupper(i); |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2972 |
324 | 2973 spelltab.st_isu[i] = utf_isupper(i); |
2974 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
|
2975 /* 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
|
2976 * 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
|
2977 * value for utf-8 to avoid this. */ |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2978 spelltab.st_fold[i] = (f < 256) ? f : i; |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2979 spelltab.st_upper[i] = (u < 256) ? u : i; |
324 | 2980 } |
2981 } | |
307 | 2982 else |
2983 { | |
324 | 2984 /* Rough guess: use locale-dependent library functions. */ |
307 | 2985 for (i = 128; i < 256; ++i) |
2986 { | |
2987 if (MB_ISUPPER(i)) | |
2988 { | |
324 | 2989 spelltab.st_isw[i] = TRUE; |
307 | 2990 spelltab.st_isu[i] = TRUE; |
2991 spelltab.st_fold[i] = MB_TOLOWER(i); | |
2992 } | |
324 | 2993 else if (MB_ISLOWER(i)) |
2994 { | |
2995 spelltab.st_isw[i] = TRUE; | |
2996 spelltab.st_upper[i] = MB_TOUPPER(i); | |
2997 } | |
307 | 2998 } |
2999 } | |
3000 } | |
3001 | |
3002 | |
3003 /* | |
358 | 3004 * Return TRUE if "p" points to a word character. |
366 | 3005 * As a special case we see "midword" characters as word character when it is |
358 | 3006 * followed by a word character. This finds they'there but not 'they there'. |
366 | 3007 * Thus this only works properly when past the first character of the word. |
358 | 3008 */ |
3009 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3010 spell_iswordp( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3011 char_u *p, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3012 win_T *wp) /* buffer used */ |
358 | 3013 { |
3014 char_u *s; | |
366 | 3015 int l; |
3016 int c; | |
3017 | |
3018 if (has_mbyte) | |
3019 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3020 l = MB_PTR2LEN(p); |
358 | 3021 s = p; |
366 | 3022 if (l == 1) |
3023 { | |
3024 /* be quick for ASCII */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3025 if (wp->w_s->b_spell_ismw[*p]) |
366 | 3026 s = p + 1; /* skip a mid-word character */ |
3027 } | |
3028 else | |
3029 { | |
3030 c = mb_ptr2char(p); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3031 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
|
3032 : (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
|
3033 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL)) |
366 | 3034 s = p + l; |
3035 } | |
3036 | |
390 | 3037 c = mb_ptr2char(s); |
3038 if (c > 255) | |
5477 | 3039 return spell_mb_isword_class(mb_get_class(s), wp); |
390 | 3040 return spelltab.st_isw[c]; |
366 | 3041 } |
3042 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3043 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]]; |
376 | 3044 } |
3045 | |
3046 /* | |
3047 * Return TRUE if "p" points to a word character. | |
3048 * Unlike spell_iswordp() this doesn't check for "midword" characters. | |
3049 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3050 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3051 spell_iswordp_nmw(char_u *p, win_T *wp) |
376 | 3052 { |
390 | 3053 int c; |
3054 | |
3055 if (has_mbyte) | |
3056 { | |
3057 c = mb_ptr2char(p); | |
3058 if (c > 255) | |
5477 | 3059 return spell_mb_isword_class(mb_get_class(p), wp); |
390 | 3060 return spelltab.st_isw[c]; |
3061 } | |
376 | 3062 return spelltab.st_isw[*p]; |
358 | 3063 } |
3064 | |
372 | 3065 /* |
1580 | 3066 * Return TRUE if word class indicates a word character. |
3067 * Only for characters above 255. | |
3068 * Unicode subscript and superscript are not considered word characters. | |
5477 | 3069 * See also dbcs_class() and utf_class() in mbyte.c. |
3070 */ | |
3071 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3072 spell_mb_isword_class(int cl, win_T *wp) |
5477 | 3073 { |
3074 if (wp->w_s->b_cjk) | |
3075 /* East Asian characters are not considered word characters. */ | |
3076 return cl == 2 || cl == 0x2800; | |
1580 | 3077 return cl >= 2 && cl != 0x2070 && cl != 0x2080; |
3078 } | |
3079 | |
3080 /* | |
372 | 3081 * Return TRUE if "p" points to a word character. |
3082 * Wide version of spell_iswordp(). | |
3083 */ | |
3084 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3085 spell_iswordp_w(int *p, win_T *wp) |
372 | 3086 { |
3087 int *s; | |
3088 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3089 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
|
3090 : (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
|
3091 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL)) |
372 | 3092 s = p + 1; |
3093 else | |
3094 s = p; | |
3095 | |
390 | 3096 if (*s > 255) |
372 | 3097 { |
3098 if (enc_utf8) | |
5477 | 3099 return spell_mb_isword_class(utf_class(*s), wp); |
372 | 3100 if (enc_dbcs) |
5477 | 3101 return spell_mb_isword_class( |
3102 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp); | |
372 | 3103 return 0; |
3104 } | |
3105 return spelltab.st_isw[*s]; | |
3106 } | |
3107 | |
358 | 3108 /* |
324 | 3109 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
3110 * Uses the character definitions from the .spl file. | |
307 | 3111 * When using a multi-byte 'encoding' the length may change! |
3112 * Returns FAIL when something wrong. | |
3113 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3114 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3115 spell_casefold( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3116 char_u *str, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3117 int len, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3118 char_u *buf, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3119 int buflen) |
307 | 3120 { |
3121 int i; | |
3122 | |
3123 if (len >= buflen) | |
3124 { | |
3125 buf[0] = NUL; | |
3126 return FAIL; /* result will not fit */ | |
3127 } | |
3128 | |
3129 if (has_mbyte) | |
3130 { | |
324 | 3131 int outi = 0; |
3132 char_u *p; | |
307 | 3133 int c; |
3134 | |
3135 /* Fold one character at a time. */ | |
324 | 3136 for (p = str; p < str + len; ) |
307 | 3137 { |
3138 if (outi + MB_MAXBYTES > buflen) | |
3139 { | |
3140 buf[outi] = NUL; | |
3141 return FAIL; | |
3142 } | |
474 | 3143 c = mb_cptr2char_adv(&p); |
324 | 3144 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
307 | 3145 } |
3146 buf[outi] = NUL; | |
3147 } | |
3148 else | |
3149 { | |
3150 /* Be quick for non-multibyte encodings. */ | |
3151 for (i = 0; i < len; ++i) | |
324 | 3152 buf[i] = spelltab.st_fold[str[i]]; |
307 | 3153 buf[i] = NUL; |
3154 } | |
3155 | |
3156 return OK; | |
3157 } | |
3158 | |
625 | 3159 /* values for sps_flags */ |
372 | 3160 #define SPS_BEST 1 |
3161 #define SPS_FAST 2 | |
3162 #define SPS_DOUBLE 4 | |
3163 | |
625 | 3164 static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */ |
3165 static int sps_limit = 9999; /* max nr of suggestions given */ | |
372 | 3166 |
3167 /* | |
3168 * Check the 'spellsuggest' option. Return FAIL if it's wrong. | |
483 | 3169 * Sets "sps_flags" and "sps_limit". |
372 | 3170 */ |
3171 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3172 spell_check_sps(void) |
372 | 3173 { |
3174 char_u *p; | |
483 | 3175 char_u *s; |
372 | 3176 char_u buf[MAXPATHL]; |
3177 int f; | |
3178 | |
3179 sps_flags = 0; | |
483 | 3180 sps_limit = 9999; |
372 | 3181 |
3182 for (p = p_sps; *p != NUL; ) | |
3183 { | |
3184 copy_option_part(&p, buf, MAXPATHL, ","); | |
3185 | |
3186 f = 0; | |
483 | 3187 if (VIM_ISDIGIT(*buf)) |
3188 { | |
3189 s = buf; | |
3190 sps_limit = getdigits(&s); | |
3191 if (*s != NUL && !VIM_ISDIGIT(*s)) | |
3192 f = -1; | |
3193 } | |
3194 else if (STRCMP(buf, "best") == 0) | |
372 | 3195 f = SPS_BEST; |
3196 else if (STRCMP(buf, "fast") == 0) | |
3197 f = SPS_FAST; | |
3198 else if (STRCMP(buf, "double") == 0) | |
3199 f = SPS_DOUBLE; | |
3200 else if (STRNCMP(buf, "expr:", 5) != 0 | |
3201 && STRNCMP(buf, "file:", 5) != 0) | |
3202 f = -1; | |
3203 | |
3204 if (f == -1 || (sps_flags != 0 && f != 0)) | |
3205 { | |
3206 sps_flags = SPS_BEST; | |
483 | 3207 sps_limit = 9999; |
372 | 3208 return FAIL; |
3209 } | |
3210 if (f != 0) | |
3211 sps_flags = f; | |
3212 } | |
3213 | |
3214 if (sps_flags == 0) | |
3215 sps_flags = SPS_BEST; | |
3216 | |
3217 return OK; | |
3218 } | |
3219 | |
323 | 3220 /* |
5374 | 3221 * "z=": Find badly spelled word under or after the cursor. |
323 | 3222 * Give suggestions for the properly spelled word. |
638 | 3223 * In Visual mode use the highlighted word as the bad word. |
485 | 3224 * When "count" is non-zero use that suggestion. |
323 | 3225 */ |
3226 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3227 spell_suggest(int count) |
323 | 3228 { |
3229 char_u *line; | |
3230 pos_T prev_cursor = curwin->w_cursor; | |
3231 char_u wcopy[MAXWLEN + 2]; | |
3232 char_u *p; | |
3233 int i; | |
3234 int c; | |
3235 suginfo_T sug; | |
3236 suggest_T *stp; | |
372 | 3237 int mouse_used; |
388 | 3238 int need_cap; |
483 | 3239 int limit; |
485 | 3240 int selected = count; |
638 | 3241 int badlen = 0; |
1938 | 3242 int msg_scroll_save = msg_scroll; |
638 | 3243 |
3244 if (no_spell_checking(curwin)) | |
3245 return; | |
3246 | |
3247 if (VIsual_active) | |
3248 { | |
3249 /* Use the Visually selected text as the bad word. But reject | |
3250 * a multi-line selection. */ | |
3251 if (curwin->w_cursor.lnum != VIsual.lnum) | |
3252 { | |
6949 | 3253 vim_beep(BO_SPELL); |
638 | 3254 return; |
3255 } | |
3256 badlen = (int)curwin->w_cursor.col - (int)VIsual.col; | |
3257 if (badlen < 0) | |
3258 badlen = -badlen; | |
3259 else | |
3260 curwin->w_cursor.col = VIsual.col; | |
3261 ++badlen; | |
3262 end_visual_mode(); | |
3263 } | |
5735 | 3264 /* Find the start of the badly spelled word. */ |
3265 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0 | |
346 | 3266 || curwin->w_cursor.col > prev_cursor.col) |
3267 { | |
3268 /* No bad word or it starts after the cursor: use the word under the | |
3269 * cursor. */ | |
3270 curwin->w_cursor = prev_cursor; | |
3271 line = ml_get_curline(); | |
3272 p = line + curwin->w_cursor.col; | |
3273 /* Backup to before start of word. */ | |
5477 | 3274 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
|
3275 MB_PTR_BACK(line, p); |
346 | 3276 /* Forward to start of word. */ |
5477 | 3277 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
|
3278 MB_PTR_ADV(p); |
346 | 3279 |
5477 | 3280 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */ |
346 | 3281 { |
3282 beep_flush(); | |
3283 return; | |
3284 } | |
835 | 3285 curwin->w_cursor.col = (colnr_T)(p - line); |
323 | 3286 } |
3287 | |
344 | 3288 /* Get the word and its length. */ |
3289 | |
388 | 3290 /* Figure out if the word should be capitalised. */ |
475 | 3291 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col); |
3292 | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3293 /* 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
|
3294 line = vim_strsave(ml_get_curline()); |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3295 if (line == NULL) |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3296 goto skip; |
388 | 3297 |
483 | 3298 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in |
3299 * 'spellsuggest', whatever is smaller. */ | |
3300 if (sps_limit > (int)Rows - 2) | |
3301 limit = (int)Rows - 2; | |
3302 else | |
3303 limit = sps_limit; | |
638 | 3304 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit, |
625 | 3305 TRUE, need_cap, TRUE); |
323 | 3306 |
3307 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
|
3308 msg(_("Sorry, no suggestions")); |
485 | 3309 else if (count > 0) |
3310 { | |
3311 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
|
3312 smsg(_("Sorry, only %ld suggestions"), |
485 | 3313 (long)sug.su_ga.ga_len); |
3314 } | |
323 | 3315 else |
3316 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
3317 VIM_CLEAR(repl_from); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
3318 VIM_CLEAR(repl_to); |
372 | 3319 |
474 | 3320 #ifdef FEAT_RIGHTLEFT |
3321 /* When 'rightleft' is set the list is drawn right-left. */ | |
3322 cmdmsg_rl = curwin->w_p_rl; | |
3323 if (cmdmsg_rl) | |
3324 msg_col = Columns - 1; | |
3325 #endif | |
3326 | |
323 | 3327 /* List the suggestions. */ |
3328 msg_start(); | |
914 | 3329 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */ |
372 | 3330 lines_left = Rows; /* avoid more prompt */ |
323 | 3331 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
3332 sug.su_badlen, sug.su_badptr); | |
474 | 3333 #ifdef FEAT_RIGHTLEFT |
3334 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0) | |
3335 { | |
3336 /* And now the rabbit from the high hat: Avoid showing the | |
3337 * untranslated message rightleft. */ | |
3338 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC", | |
3339 sug.su_badlen, sug.su_badptr); | |
3340 } | |
3341 #endif | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3342 msg_puts((char *)IObuff); |
323 | 3343 msg_clr_eos(); |
3344 msg_putchar('\n'); | |
346 | 3345 |
323 | 3346 msg_scroll = TRUE; |
3347 for (i = 0; i < sug.su_ga.ga_len; ++i) | |
3348 { | |
344 | 3349 stp = &SUG(sug.su_ga, i); |
323 | 3350 |
3351 /* The suggested word may replace only part of the bad word, add | |
3352 * the not replaced part. */ | |
2768 | 3353 vim_strncpy(wcopy, stp->st_word, MAXWLEN); |
323 | 3354 if (sug.su_badlen > stp->st_orglen) |
625 | 3355 vim_strncpy(wcopy + stp->st_wordlen, |
323 | 3356 sug.su_badptr + stp->st_orglen, |
3357 sug.su_badlen - stp->st_orglen); | |
474 | 3358 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1); |
3359 #ifdef FEAT_RIGHTLEFT | |
3360 if (cmdmsg_rl) | |
3361 rl_mirror(IObuff); | |
3362 #endif | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3363 msg_puts((char *)IObuff); |
474 | 3364 |
3365 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
|
3366 msg_puts((char *)IObuff); |
346 | 3367 |
3368 /* The word may replace more than "su_badlen". */ | |
3369 if (sug.su_badlen < stp->st_orglen) | |
3370 { | |
3371 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), | |
3372 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
|
3373 msg_puts((char *)IObuff); |
346 | 3374 } |
3375 | |
324 | 3376 if (p_verbose > 0) |
344 | 3377 { |
346 | 3378 /* Add the score. */ |
351 | 3379 if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
474 | 3380 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)", |
344 | 3381 stp->st_salscore ? "s " : "", |
3382 stp->st_score, stp->st_altscore); | |
3383 else | |
474 | 3384 vim_snprintf((char *)IObuff, IOSIZE, " (%d)", |
346 | 3385 stp->st_score); |
474 | 3386 #ifdef FEAT_RIGHTLEFT |
3387 if (cmdmsg_rl) | |
3388 /* Mirror the numbers, but keep the leading space. */ | |
3389 rl_mirror(IObuff + 1); | |
3390 #endif | |
346 | 3391 msg_advance(30); |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3392 msg_puts((char *)IObuff); |
344 | 3393 } |
323 | 3394 msg_putchar('\n'); |
3395 } | |
3396 | |
474 | 3397 #ifdef FEAT_RIGHTLEFT |
3398 cmdmsg_rl = FALSE; | |
3399 msg_col = 0; | |
3400 #endif | |
323 | 3401 /* Ask for choice. */ |
485 | 3402 selected = prompt_for_number(&mouse_used); |
372 | 3403 if (mouse_used) |
485 | 3404 selected -= lines_left; |
1938 | 3405 lines_left = Rows; /* avoid more prompt */ |
3406 /* don't delay for 'smd' in normal_cmd() */ | |
3407 msg_scroll = msg_scroll_save; | |
485 | 3408 } |
3409 | |
3410 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK) | |
3411 { | |
3412 /* Save the from and to text for :spellrepall. */ | |
3413 stp = &SUG(sug.su_ga, selected - 1); | |
548 | 3414 if (sug.su_badlen > stp->st_orglen) |
3415 { | |
3416 /* Replacing less than "su_badlen", append the remainder to | |
3417 * repl_to. */ | |
3418 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen); | |
3419 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word, | |
3420 sug.su_badlen - stp->st_orglen, | |
3421 sug.su_badptr + stp->st_orglen); | |
3422 repl_to = vim_strsave(IObuff); | |
3423 } | |
3424 else | |
3425 { | |
3426 /* Replacing su_badlen or more, use the whole word. */ | |
3427 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); | |
3428 repl_to = vim_strsave(stp->st_word); | |
3429 } | |
485 | 3430 |
3431 /* Replace the word. */ | |
1938 | 3432 p = alloc((unsigned)STRLEN(line) - stp->st_orglen |
3433 + stp->st_wordlen + 1); | |
485 | 3434 if (p != NULL) |
3435 { | |
835 | 3436 c = (int)(sug.su_badptr - line); |
485 | 3437 mch_memmove(p, line, c); |
3438 STRCPY(p + c, stp->st_word); | |
3439 STRCAT(p, sug.su_badptr + stp->st_orglen); | |
3440 ml_replace(curwin->w_cursor.lnum, p, FALSE); | |
3441 curwin->w_cursor.col = c; | |
3442 | |
3443 /* For redo we use a change-word command. */ | |
3444 ResetRedobuff(); | |
3445 AppendToRedobuff((char_u *)"ciw"); | |
620 | 3446 AppendToRedobuffLit(p + c, |
625 | 3447 stp->st_wordlen + sug.su_badlen - stp->st_orglen); |
485 | 3448 AppendCharToRedobuff(ESC); |
819 | 3449 |
3450 /* After this "p" may be invalid. */ | |
3451 changed_bytes(curwin->w_cursor.lnum, c); | |
485 | 3452 } |
3453 } | |
3454 else | |
3455 curwin->w_cursor = prev_cursor; | |
323 | 3456 |
344 | 3457 spell_find_cleanup(&sug); |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3458 skip: |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3459 vim_free(line); |
344 | 3460 } |
3461 | |
3462 /* | |
475 | 3463 * Check if the word at line "lnum" column "col" is required to start with a |
3464 * capital. This uses 'spellcapcheck' of the current buffer. | |
3465 */ | |
3466 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3467 check_need_cap(linenr_T lnum, colnr_T col) |
475 | 3468 { |
3469 int need_cap = FALSE; | |
3470 char_u *line; | |
3471 char_u *line_copy = NULL; | |
3472 char_u *p; | |
3473 colnr_T endcol; | |
3474 regmatch_T regmatch; | |
3475 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3476 if (curwin->w_s->b_cap_prog == NULL) |
475 | 3477 return FALSE; |
3478 | |
3479 line = ml_get_curline(); | |
3480 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
|
3481 if (getwhitecols(line) >= (int)col) |
475 | 3482 { |
3483 /* At start of line, check if previous line is empty or sentence | |
3484 * ends there. */ | |
3485 if (lnum == 1) | |
3486 need_cap = TRUE; | |
3487 else | |
3488 { | |
3489 line = ml_get(lnum - 1); | |
3490 if (*skipwhite(line) == NUL) | |
3491 need_cap = TRUE; | |
3492 else | |
3493 { | |
3494 /* Append a space in place of the line break. */ | |
3495 line_copy = concat_str(line, (char_u *)" "); | |
3496 line = line_copy; | |
835 | 3497 endcol = (colnr_T)STRLEN(line); |
475 | 3498 } |
3499 } | |
3500 } | |
3501 else | |
3502 endcol = col; | |
3503 | |
3504 if (endcol > 0) | |
3505 { | |
3506 /* 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
|
3507 regmatch.regprog = curwin->w_s->b_cap_prog; |
475 | 3508 regmatch.rm_ic = FALSE; |
3509 p = line + endcol; | |
3510 for (;;) | |
3511 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3512 MB_PTR_BACK(line, p); |
5477 | 3513 if (p == line || spell_iswordp_nmw(p, curwin)) |
475 | 3514 break; |
3515 if (vim_regexec(®match, p, 0) | |
3516 && regmatch.endp[0] == line + endcol) | |
3517 { | |
3518 need_cap = TRUE; | |
3519 break; | |
3520 } | |
3521 } | |
6375 | 3522 curwin->w_s->b_cap_prog = regmatch.regprog; |
475 | 3523 } |
3524 | |
3525 vim_free(line_copy); | |
3526 | |
3527 return need_cap; | |
3528 } | |
3529 | |
3530 | |
3531 /* | |
372 | 3532 * ":spellrepall" |
3533 */ | |
3534 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3535 ex_spellrepall(exarg_T *eap UNUSED) |
372 | 3536 { |
3537 pos_T pos = curwin->w_cursor; | |
3538 char_u *frompat; | |
3539 int addlen; | |
3540 char_u *line; | |
3541 char_u *p; | |
3542 int save_ws = p_ws; | |
483 | 3543 linenr_T prev_lnum = 0; |
372 | 3544 |
3545 if (repl_from == NULL || repl_to == NULL) | |
3546 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3547 emsg(_("E752: No previous spell replacement")); |
372 | 3548 return; |
3549 } | |
835 | 3550 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from)); |
3551 | |
3552 frompat = alloc((unsigned)STRLEN(repl_from) + 7); | |
372 | 3553 if (frompat == NULL) |
3554 return; | |
3555 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); | |
3556 p_ws = FALSE; | |
3557 | |
483 | 3558 sub_nsubs = 0; |
3559 sub_nlines = 0; | |
372 | 3560 curwin->w_cursor.lnum = 0; |
3561 while (!got_int) | |
3562 { | |
11521
578df034735d
patch 8.0.0643: when a pattern search is slow Vim becomes unusable
Christian Brabandt <cb@256bit.org>
parents:
11394
diff
changeset
|
3563 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0 |
372 | 3564 || u_save_cursor() == FAIL) |
3565 break; | |
3566 | |
3567 /* Only replace when the right word isn't there yet. This happens | |
3568 * when changing "etc" to "etc.". */ | |
3569 line = ml_get_curline(); | |
3570 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, | |
3571 repl_to, STRLEN(repl_to)) != 0) | |
3572 { | |
835 | 3573 p = alloc((unsigned)STRLEN(line) + addlen + 1); |
372 | 3574 if (p == NULL) |
3575 break; | |
3576 mch_memmove(p, line, curwin->w_cursor.col); | |
3577 STRCPY(p + curwin->w_cursor.col, repl_to); | |
3578 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); | |
3579 ml_replace(curwin->w_cursor.lnum, p, FALSE); | |
3580 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); | |
483 | 3581 |
3582 if (curwin->w_cursor.lnum != prev_lnum) | |
3583 { | |
3584 ++sub_nlines; | |
3585 prev_lnum = curwin->w_cursor.lnum; | |
3586 } | |
3587 ++sub_nsubs; | |
372 | 3588 } |
835 | 3589 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to); |
372 | 3590 } |
3591 | |
3592 p_ws = save_ws; | |
3593 curwin->w_cursor = pos; | |
3594 vim_free(frompat); | |
3595 | |
483 | 3596 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
|
3597 semsg(_("E753: Not found: %s"), repl_from); |
483 | 3598 else |
3599 do_sub_msg(FALSE); | |
372 | 3600 } |
3601 | |
3602 /* | |
344 | 3603 * Find spell suggestions for "word". Return them in the growarray "*gap" as |
3604 * a list of allocated strings. | |
3605 */ | |
3606 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3607 spell_suggest_list( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3608 garray_T *gap, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3609 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3610 int maxcount, /* maximum nr of suggestions */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3611 int need_cap, /* 'spellcapcheck' matched */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3612 int interactive) |
344 | 3613 { |
3614 suginfo_T sug; | |
3615 int i; | |
3616 suggest_T *stp; | |
3617 char_u *wcopy; | |
3618 | |
638 | 3619 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive); |
344 | 3620 |
3621 /* Make room in "gap". */ | |
3622 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); | |
626 | 3623 if (ga_grow(gap, sug.su_ga.ga_len) == OK) |
3624 { | |
3625 for (i = 0; i < sug.su_ga.ga_len; ++i) | |
3626 { | |
3627 stp = &SUG(sug.su_ga, i); | |
3628 | |
3629 /* The suggested word may replace only part of "word", add the not | |
3630 * replaced part. */ | |
3631 wcopy = alloc(stp->st_wordlen | |
835 | 3632 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
626 | 3633 if (wcopy == NULL) |
3634 break; | |
3635 STRCPY(wcopy, stp->st_word); | |
3636 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen); | |
3637 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; | |
3638 } | |
344 | 3639 } |
3640 | |
3641 spell_find_cleanup(&sug); | |
3642 } | |
3643 | |
3644 /* | |
3645 * Find spell suggestions for the word at the start of "badptr". | |
3646 * Return the suggestions in "su->su_ga". | |
3647 * The maximum number of suggestions is "maxcount". | |
3648 * Note: does use info for the current window. | |
3649 * This is based on the mechanisms of Aspell, but completely reimplemented. | |
3650 */ | |
3651 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3652 spell_find_suggest( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3653 char_u *badptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3654 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
|
3655 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3656 int maxcount, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3657 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
|
3658 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
|
3659 int interactive) |
344 | 3660 { |
534 | 3661 hlf_T attr = HLF_COUNT; |
372 | 3662 char_u buf[MAXPATHL]; |
3663 char_u *p; | |
3664 int do_combine = FALSE; | |
3665 char_u *sps_copy; | |
3666 #ifdef FEAT_EVAL | |
3667 static int expr_busy = FALSE; | |
3668 #endif | |
385 | 3669 int c; |
503 | 3670 int i; |
3671 langp_T *lp; | |
344 | 3672 |
3673 /* | |
3674 * Set the info in "*su". | |
3675 */ | |
3676 vim_memset(su, 0, sizeof(suginfo_T)); | |
3677 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); | |
3678 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); | |
355 | 3679 if (*badptr == NUL) |
3680 return; | |
344 | 3681 hash_init(&su->su_banned); |
3682 | |
3683 su->su_badptr = badptr; | |
638 | 3684 if (badlen != 0) |
3685 su->su_badlen = badlen; | |
3686 else | |
3687 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE); | |
344 | 3688 su->su_maxcount = maxcount; |
372 | 3689 su->su_maxscore = SCORE_MAXINIT; |
344 | 3690 |
3691 if (su->su_badlen >= MAXWLEN) | |
3692 su->su_badlen = MAXWLEN - 1; /* just in case */ | |
3693 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); | |
3694 (void)spell_casefold(su->su_badptr, su->su_badlen, | |
3695 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
|
3696 /* 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
|
3697 * 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
|
3698 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
|
3699 |
346 | 3700 /* get caps flags for bad word */ |
474 | 3701 su->su_badflags = badword_captype(su->su_badptr, |
3702 su->su_badptr + su->su_badlen); | |
388 | 3703 if (need_cap) |
3704 su->su_badflags |= WF_ONECAP; | |
344 | 3705 |
503 | 3706 /* Find the default language for sound folding. We simply use the first |
3707 * one in 'spelllang' that supports sound folding. That's good for when | |
3708 * using multiple files for one language, it's not that bad when mixing | |
3709 * languages (e.g., "pl,en"). */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3710 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
|
3711 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3712 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i); |
503 | 3713 if (lp->lp_sallang != NULL) |
3714 { | |
3715 su->su_sallang = lp->lp_sallang; | |
3716 break; | |
3717 } | |
3718 } | |
3719 | |
534 | 3720 /* Soundfold the bad word with the default sound folding, so that we don't |
3721 * have to do this many times. */ | |
3722 if (su->su_sallang != NULL) | |
3723 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE, | |
3724 su->su_sal_badword); | |
3725 | |
385 | 3726 /* If the word is not capitalised and spell_check() doesn't consider the |
3727 * word to be bad then it might need to be capitalised. Add a suggestion | |
3728 * for that. */ | |
455 | 3729 c = PTR2CHAR(su->su_badptr); |
534 | 3730 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT) |
385 | 3731 { |
3732 make_case_word(su->su_badword, buf, WF_ONECAP); | |
3733 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, | |
625 | 3734 0, TRUE, su->su_sallang, FALSE); |
385 | 3735 } |
3736 | |
344 | 3737 /* Ban the bad word itself. It may appear in another region. */ |
358 | 3738 if (banbadword) |
3739 add_banned(su, su->su_badword); | |
344 | 3740 |
372 | 3741 /* Make a copy of 'spellsuggest', because the expression may change it. */ |
3742 sps_copy = vim_strsave(p_sps); | |
3743 if (sps_copy == NULL) | |
3744 return; | |
3745 | |
3746 /* Loop over the items in 'spellsuggest'. */ | |
3747 for (p = sps_copy; *p != NUL; ) | |
3748 { | |
3749 copy_option_part(&p, buf, MAXPATHL, ","); | |
3750 | |
3751 if (STRNCMP(buf, "expr:", 5) == 0) | |
3752 { | |
3753 #ifdef FEAT_EVAL | |
375 | 3754 /* Evaluate an expression. Skip this when called recursively, |
3755 * when using spellsuggest() in the expression. */ | |
372 | 3756 if (!expr_busy) |
3757 { | |
3758 expr_busy = TRUE; | |
3759 spell_suggest_expr(su, buf + 5); | |
3760 expr_busy = FALSE; | |
3761 } | |
3762 #endif | |
3763 } | |
3764 else if (STRNCMP(buf, "file:", 5) == 0) | |
3765 /* Use list of suggestions in a file. */ | |
3766 spell_suggest_file(su, buf + 5); | |
3767 else | |
3768 { | |
3769 /* Use internal method. */ | |
625 | 3770 spell_suggest_intern(su, interactive); |
372 | 3771 if (sps_flags & SPS_DOUBLE) |
3772 do_combine = TRUE; | |
3773 } | |
3774 } | |
3775 | |
3776 vim_free(sps_copy); | |
3777 | |
3778 if (do_combine) | |
3779 /* Combine the two list of suggestions. This must be done last, | |
3780 * because sorting changes the order again. */ | |
3781 score_combine(su); | |
3782 } | |
3783 | |
3784 #ifdef FEAT_EVAL | |
3785 /* | |
3786 * Find suggestions by evaluating expression "expr". | |
3787 */ | |
3788 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3789 spell_suggest_expr(suginfo_T *su, char_u *expr) |
372 | 3790 { |
3791 list_T *list; | |
3792 listitem_T *li; | |
3793 int score; | |
3794 char_u *p; | |
3795 | |
3796 /* The work is split up in a few parts to avoid having to export | |
3797 * suginfo_T. | |
3798 * First evaluate the expression and get the resulting list. */ | |
3799 list = eval_spell_expr(su->su_badword, expr); | |
3800 if (list != NULL) | |
3801 { | |
3802 /* Loop over the items in the list. */ | |
3803 for (li = list->lv_first; li != NULL; li = li->li_next) | |
3804 if (li->li_tv.v_type == VAR_LIST) | |
3805 { | |
3806 /* Get the word and the score from the items. */ | |
3807 score = get_spellword(li->li_tv.vval.v_list, &p); | |
625 | 3808 if (score >= 0 && score <= su->su_maxscore) |
3809 add_suggestion(su, &su->su_ga, p, su->su_badlen, | |
3810 score, 0, TRUE, su->su_sallang, FALSE); | |
372 | 3811 } |
3812 list_unref(list); | |
3813 } | |
3814 | |
625 | 3815 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3816 check_suggestions(su, &su->su_ga); | |
372 | 3817 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
3818 } | |
3819 #endif | |
3820 | |
3821 /* | |
474 | 3822 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'. |
372 | 3823 */ |
3824 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3825 spell_suggest_file(suginfo_T *su, char_u *fname) |
372 | 3826 { |
3827 FILE *fd; | |
3828 char_u line[MAXWLEN * 2]; | |
3829 char_u *p; | |
3830 int len; | |
3831 char_u cword[MAXWLEN]; | |
3832 | |
3833 /* Open the file. */ | |
3834 fd = mch_fopen((char *)fname, "r"); | |
3835 if (fd == NULL) | |
3836 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3837 semsg(_(e_notopen), fname); |
372 | 3838 return; |
3839 } | |
3840 | |
3841 /* Read it line by line. */ | |
3842 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) | |
3843 { | |
3844 line_breakcheck(); | |
3845 | |
3846 p = vim_strchr(line, '/'); | |
3847 if (p == NULL) | |
3848 continue; /* No Tab found, just skip the line. */ | |
3849 *p++ = NUL; | |
3850 if (STRICMP(su->su_badword, line) == 0) | |
3851 { | |
3852 /* Match! Isolate the good word, until CR or NL. */ | |
3853 for (len = 0; p[len] >= ' '; ++len) | |
3854 ; | |
3855 p[len] = NUL; | |
3856 | |
3857 /* If the suggestion doesn't have specific case duplicate the case | |
3858 * of the bad word. */ | |
3859 if (captype(p, NULL) == 0) | |
3860 { | |
3861 make_case_word(p, cword, su->su_badflags); | |
3862 p = cword; | |
3863 } | |
3864 | |
3865 add_suggestion(su, &su->su_ga, p, su->su_badlen, | |
625 | 3866 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE); |
372 | 3867 } |
3868 } | |
3869 | |
3870 fclose(fd); | |
3871 | |
625 | 3872 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3873 check_suggestions(su, &su->su_ga); | |
372 | 3874 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
3875 } | |
3876 | |
3877 /* | |
3878 * Find suggestions for the internal method indicated by "sps_flags". | |
3879 */ | |
3880 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3881 spell_suggest_intern(suginfo_T *su, int interactive) |
625 | 3882 { |
3883 /* | |
3884 * Load the .sug file(s) that are available and not done yet. | |
3885 */ | |
3886 suggest_load_files(); | |
3887 | |
344 | 3888 /* |
346 | 3889 * 1. Try special cases, such as repeating a word: "the the" -> "the". |
344 | 3890 * |
3891 * Set a maximum score to limit the combination of operations that is | |
3892 * tried. | |
3893 */ | |
346 | 3894 suggest_try_special(su); |
3895 | |
3896 /* | |
3897 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries | |
3898 * from the .aff file and inserting a space (split the word). | |
3899 */ | |
3900 suggest_try_change(su); | |
344 | 3901 |
3902 /* For the resulting top-scorers compute the sound-a-like score. */ | |
3903 if (sps_flags & SPS_DOUBLE) | |
3904 score_comp_sal(su); | |
3905 | |
3906 /* | |
346 | 3907 * 3. Try finding sound-a-like words. |
344 | 3908 */ |
625 | 3909 if ((sps_flags & SPS_FAST) == 0) |
3910 { | |
3911 if (sps_flags & SPS_BEST) | |
3912 /* Adjust the word score for the suggestions found so far for how | |
3913 * they sounds like. */ | |
3914 rescore_suggestions(su); | |
3915 | |
3916 /* | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3917 * While going through the soundfold tree "su_maxscore" is the score |
625 | 3918 * for the soundfold word, limits the changes that are being tried, |
3919 * and "su_sfmaxscore" the rescored score, which is set by | |
3920 * cleanup_suggestions(). | |
3921 * First find words with a small edit distance, because this is much | |
3922 * faster and often already finds the top-N suggestions. If we didn't | |
3923 * find many suggestions try again with a higher edit distance. | |
3924 * "sl_sounddone" is used to avoid doing the same word twice. | |
3925 */ | |
3926 suggest_try_soundalike_prep(); | |
3927 su->su_maxscore = SCORE_SFMAX1; | |
3928 su->su_sfmaxscore = SCORE_MAXINIT * 3; | |
346 | 3929 suggest_try_soundalike(su); |
625 | 3930 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su)) |
3931 { | |
3932 /* We didn't find enough matches, try again, allowing more | |
3933 * changes to the soundfold word. */ | |
3934 su->su_maxscore = SCORE_SFMAX2; | |
3935 suggest_try_soundalike(su); | |
3936 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su)) | |
3937 { | |
3938 /* Still didn't find enough matches, try again, allowing even | |
3939 * more changes to the soundfold word. */ | |
3940 su->su_maxscore = SCORE_SFMAX3; | |
3941 suggest_try_soundalike(su); | |
3942 } | |
3943 } | |
3944 su->su_maxscore = su->su_sfmaxscore; | |
3945 suggest_try_soundalike_finish(); | |
3946 } | |
3947 | |
3948 /* When CTRL-C was hit while searching do show the results. Only clear | |
3949 * got_int when using a command, not for spellsuggest(). */ | |
344 | 3950 ui_breakcheck(); |
625 | 3951 if (interactive && got_int) |
344 | 3952 { |
3953 (void)vgetc(); | |
3954 got_int = FALSE; | |
3955 } | |
3956 | |
372 | 3957 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
344 | 3958 { |
3959 if (sps_flags & SPS_BEST) | |
3960 /* Adjust the word score for how it sounds like. */ | |
3961 rescore_suggestions(su); | |
3962 | |
625 | 3963 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3964 check_suggestions(su, &su->su_ga); | |
372 | 3965 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
344 | 3966 } |
3967 } | |
3968 | |
3969 /* | |
3970 * Free the info put in "*su" by spell_find_suggest(). | |
3971 */ | |
3972 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3973 spell_find_cleanup(suginfo_T *su) |
344 | 3974 { |
3975 int i; | |
3976 | |
3977 /* Free the suggestions. */ | |
3978 for (i = 0; i < su->su_ga.ga_len; ++i) | |
3979 vim_free(SUG(su->su_ga, i).st_word); | |
3980 ga_clear(&su->su_ga); | |
3981 for (i = 0; i < su->su_sga.ga_len; ++i) | |
3982 vim_free(SUG(su->su_sga, i).st_word); | |
3983 ga_clear(&su->su_sga); | |
323 | 3984 |
3985 /* Free the banned words. */ | |
625 | 3986 hash_clear_all(&su->su_banned, 0); |
323 | 3987 } |
3988 | |
3989 /* | |
324 | 3990 * Make a copy of "word", with the first letter upper or lower cased, to |
3991 * "wcopy[MAXWLEN]". "word" must not be empty. | |
3992 * The result is NUL terminated. | |
323 | 3993 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3994 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3995 onecap_copy( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3996 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3997 char_u *wcopy, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3998 int upper) /* TRUE: first letter made upper case */ |
323 | 3999 { |
4000 char_u *p; | |
4001 int c; | |
4002 int l; | |
4003 | |
4004 p = word; | |
4005 if (has_mbyte) | |
474 | 4006 c = mb_cptr2char_adv(&p); |
323 | 4007 else |
4008 c = *p++; | |
4009 if (upper) | |
324 | 4010 c = SPELL_TOUPPER(c); |
323 | 4011 else |
324 | 4012 c = SPELL_TOFOLD(c); |
323 | 4013 if (has_mbyte) |
4014 l = mb_char2bytes(c, wcopy); | |
4015 else | |
4016 { | |
4017 l = 1; | |
4018 wcopy[0] = c; | |
4019 } | |
376 | 4020 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
323 | 4021 } |
4022 | |
4023 /* | |
324 | 4024 * Make a copy of "word" with all the letters upper cased into |
4025 * "wcopy[MAXWLEN]". The result is NUL terminated. | |
323 | 4026 */ |
4027 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4028 allcap_copy(char_u *word, char_u *wcopy) |
323 | 4029 { |
4030 char_u *s; | |
4031 char_u *d; | |
4032 int c; | |
4033 | |
4034 d = wcopy; | |
4035 for (s = word; *s != NUL; ) | |
4036 { | |
4037 if (has_mbyte) | |
474 | 4038 c = mb_cptr2char_adv(&s); |
323 | 4039 else |
4040 c = *s++; | |
492 | 4041 |
3042 | 4042 /* We only change 0xdf to SS when we are certain latin1 is used. It |
492 | 4043 * would cause weird errors in other 8-bit encodings. */ |
4044 if (enc_latin1like && c == 0xdf) | |
4045 { | |
4046 c = 'S'; | |
4047 if (d - wcopy >= MAXWLEN - 1) | |
4048 break; | |
4049 *d++ = c; | |
4050 } | |
4051 else | |
4052 c = SPELL_TOUPPER(c); | |
323 | 4053 |
4054 if (has_mbyte) | |
4055 { | |
4056 if (d - wcopy >= MAXWLEN - MB_MAXBYTES) | |
4057 break; | |
4058 d += mb_char2bytes(c, d); | |
4059 } | |
4060 else | |
4061 { | |
4062 if (d - wcopy >= MAXWLEN - 1) | |
4063 break; | |
4064 *d++ = c; | |
4065 } | |
4066 } | |
4067 *d = NUL; | |
4068 } | |
4069 | |
4070 /* | |
346 | 4071 * Try finding suggestions by recognizing specific situations. |
4072 */ | |
4073 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4074 suggest_try_special(suginfo_T *su) |
346 | 4075 { |
4076 char_u *p; | |
376 | 4077 size_t len; |
346 | 4078 int c; |
4079 char_u word[MAXWLEN]; | |
4080 | |
4081 /* | |
4082 * Recognize a word that is repeated: "the the". | |
4083 */ | |
4084 p = skiptowhite(su->su_fbadword); | |
4085 len = p - su->su_fbadword; | |
4086 p = skipwhite(p); | |
4087 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) | |
4088 { | |
4089 /* Include badflags: if the badword is onecap or allcap | |
4090 * use that for the goodword too: "The the" -> "The". */ | |
4091 c = su->su_fbadword[len]; | |
4092 su->su_fbadword[len] = NUL; | |
4093 make_case_word(su->su_fbadword, word, su->su_badflags); | |
4094 su->su_fbadword[len] = c; | |
534 | 4095 |
4096 /* Give a soundalike score of 0, compute the score as if deleting one | |
4097 * character. */ | |
4098 add_suggestion(su, &su->su_ga, word, su->su_badlen, | |
625 | 4099 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE); |
346 | 4100 } |
4101 } | |
4102 | |
4103 /* | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4104 * 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
|
4105 * Output is dumped in "suggestprof". |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4106 */ |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4107 #if 0 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4108 # define SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4109 proftime_T current; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4110 proftime_T total; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4111 proftime_T times[STATE_FINAL + 1]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4112 long counts[STATE_FINAL + 1]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4113 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4114 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4115 prof_init(void) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4116 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4117 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
|
4118 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4119 profile_zero(×[i]); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4120 counts[i] = 0; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4121 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4122 profile_start(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4123 profile_start(&total); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4124 } |
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 /* call before changing state */ |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4127 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4128 prof_store(state_T state) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4129 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4130 profile_end(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4131 profile_add(×[state], ¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4132 ++counts[state]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4133 profile_start(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4134 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4135 # 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
|
4136 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4137 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4138 prof_report(char *name) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4139 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4140 FILE *fd = fopen("suggestprof", "a"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4141 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4142 profile_end(&total); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4143 fprintf(fd, "-----------------------\n"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4144 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
|
4145 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
|
4146 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
|
4147 fclose(fd); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4148 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4149 #else |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4150 # define PROF_STORE(state) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4151 #endif |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4152 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4153 /* |
323 | 4154 * Try finding suggestions by adding/removing/swapping letters. |
4155 */ | |
4156 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4157 suggest_try_change(suginfo_T *su) |
323 | 4158 { |
4159 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ | |
625 | 4160 int n; |
4161 char_u *p; | |
4162 int lpi; | |
323 | 4163 langp_T *lp; |
4164 | |
4165 /* We make a copy of the case-folded bad word, so that we can modify it | |
346 | 4166 * to find matches (esp. REP items). Append some more text, changing |
4167 * chars after the bad word may help. */ | |
323 | 4168 STRCPY(fword, su->su_fbadword); |
835 | 4169 n = (int)STRLEN(fword); |
346 | 4170 p = su->su_badptr + su->su_badlen; |
835 | 4171 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n); |
323 | 4172 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4173 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
|
4174 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4175 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
482 | 4176 |
500 | 4177 /* If reloading a spell file fails it's still in the list but |
4178 * everything has been cleared. */ | |
625 | 4179 if (lp->lp_slang->sl_fbyts == NULL) |
500 | 4180 continue; |
4181 | |
625 | 4182 /* 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
|
4183 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4184 prof_init(); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4185 #endif |
625 | 4186 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
|
4187 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4188 prof_report("try_change"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4189 #endif |
625 | 4190 } |
4191 } | |
4192 | |
4193 /* Check the maximum score, if we go over it we won't try this change. */ | |
4194 #define TRY_DEEPER(su, stack, depth, add) \ | |
4195 (stack[depth].ts_score + (add) < su->su_maxscore) | |
4196 | |
4197 /* | |
4198 * Try finding suggestions by adding/removing/swapping letters. | |
4199 * | |
4200 * This uses a state machine. At each node in the tree we try various | |
4201 * operations. When trying if an operation works "depth" is increased and the | |
4202 * stack[] is used to store info. This allows combinations, thus insert one | |
4203 * character, replace one and delete another. The number of changes is | |
4204 * limited by su->su_maxscore. | |
4205 * | |
4206 * After implementing this I noticed an article by Kemal Oflazer that | |
4207 * describes something similar: "Error-tolerant Finite State Recognition with | |
4208 * Applications to Morphological Analysis and Spelling Correction" (1996). | |
4209 * The implementation in the article is simplified and requires a stack of | |
4210 * unknown depth. The implementation here only needs a stack depth equal to | |
4211 * the length of the word. | |
4212 * | |
4213 * This is also used for the sound-folded word, "soundfold" is TRUE then. | |
4214 * The mechanism is the same, but we find a match with a sound-folded word | |
4215 * that comes from one or more original words. Each of these words may be | |
4216 * added, this is done by add_sound_suggest(). | |
4217 * Don't use: | |
4218 * the prefix tree or the keep-case tree | |
4219 * "su->su_badlen" | |
4220 * anything to do with upper and lower case | |
4221 * anything to do with word or non-word characters ("spell_iswordp()") | |
4222 * banned words | |
4223 * word flags (rare, region, compounding) | |
4224 * word splitting for now | |
4225 * "similar_chars()" | |
4226 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep" | |
4227 */ | |
4228 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4229 suggest_trie_walk( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4230 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4231 langp_T *lp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4232 char_u *fword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4233 int soundfold) |
625 | 4234 { |
4235 char_u tword[MAXWLEN]; /* good word collected so far */ | |
4236 trystate_T stack[MAXWLEN]; | |
4237 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
|
4238 * concatenation of prefix compound |
625 | 4239 * words and split word. NUL terminated |
4240 * when going deeper but not when coming | |
4241 * back. */ | |
4242 char_u compflags[MAXWLEN]; /* compound flags, one for each word */ | |
4243 trystate_T *sp; | |
4244 int newscore; | |
4245 int score; | |
4246 char_u *byts, *fbyts, *pbyts; | |
4247 idx_T *idxs, *fidxs, *pidxs; | |
4248 int depth; | |
4249 int c, c2, c3; | |
4250 int n = 0; | |
4251 int flags; | |
4252 garray_T *gap; | |
4253 idx_T arridx; | |
4254 int len; | |
4255 char_u *p; | |
4256 fromto_T *ftp; | |
4257 int fl = 0, tl; | |
4258 int repextra = 0; /* extra bytes in fword[] from REP item */ | |
4259 slang_T *slang = lp->lp_slang; | |
4260 int fword_ends; | |
4261 int goodword_ends; | |
4262 #ifdef DEBUG_TRIEWALK | |
4263 /* Stores the name of the change made at each level. */ | |
4264 char_u changename[MAXWLEN][80]; | |
4265 #endif | |
4266 int breakcheckcount = 1000; | |
4267 int compound_ok; | |
4268 | |
4269 /* | |
4270 * Go through the whole case-fold tree, try changes at each node. | |
4271 * "tword[]" contains the word collected from nodes in the tree. | |
4272 * "fword[]" the word we are trying to match with (initially the bad | |
4273 * word). | |
4274 */ | |
4275 depth = 0; | |
4276 sp = &stack[0]; | |
4277 vim_memset(sp, 0, sizeof(trystate_T)); | |
4278 sp->ts_curi = 1; | |
4279 | |
4280 if (soundfold) | |
4281 { | |
4282 /* Going through the soundfold tree. */ | |
4283 byts = fbyts = slang->sl_sbyts; | |
4284 idxs = fidxs = slang->sl_sidxs; | |
4285 pbyts = NULL; | |
4286 pidxs = NULL; | |
4287 sp->ts_prefixdepth = PFD_NOPREFIX; | |
4288 sp->ts_state = STATE_START; | |
4289 } | |
4290 else | |
4291 { | |
330 | 4292 /* |
375 | 4293 * When there are postponed prefixes we need to use these first. At |
4294 * the end of the prefix we continue in the case-fold tree. | |
4295 */ | |
482 | 4296 fbyts = slang->sl_fbyts; |
4297 fidxs = slang->sl_fidxs; | |
4298 pbyts = slang->sl_pbyts; | |
4299 pidxs = slang->sl_pidxs; | |
375 | 4300 if (pbyts != NULL) |
4301 { | |
4302 byts = pbyts; | |
4303 idxs = pidxs; | |
482 | 4304 sp->ts_prefixdepth = PFD_PREFIXTREE; |
375 | 4305 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
4306 } | |
4307 else | |
4308 { | |
4309 byts = fbyts; | |
4310 idxs = fidxs; | |
482 | 4311 sp->ts_prefixdepth = PFD_NOPREFIX; |
485 | 4312 sp->ts_state = STATE_START; |
375 | 4313 } |
625 | 4314 } |
4315 | |
4316 /* | |
4317 * Loop to find all suggestions. At each round we either: | |
4318 * - For the current state try one operation, advance "ts_curi", | |
4319 * increase "depth". | |
4320 * - When a state is done go to the next, set "ts_state". | |
4321 * - When all states are tried decrease "depth". | |
4322 */ | |
4323 while (depth >= 0 && !got_int) | |
4324 { | |
4325 sp = &stack[depth]; | |
4326 switch (sp->ts_state) | |
4327 { | |
4328 case STATE_START: | |
4329 case STATE_NOPREFIX: | |
4330 /* | |
4331 * Start of node: Deal with NUL bytes, which means | |
4332 * tword[] may end here. | |
4333 */ | |
4334 arridx = sp->ts_arridx; /* current node in the tree */ | |
4335 len = byts[arridx]; /* bytes in this node */ | |
4336 arridx += sp->ts_curi; /* index of current byte */ | |
4337 | |
4338 if (sp->ts_prefixdepth == PFD_PREFIXTREE) | |
4339 { | |
4340 /* Skip over the NUL bytes, we use them later. */ | |
4341 for (n = 0; n < len && byts[arridx + n] == 0; ++n) | |
4342 ; | |
4343 sp->ts_curi += n; | |
4344 | |
4345 /* Always past NUL bytes now. */ | |
4346 n = (int)sp->ts_state; | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4347 PROF_STORE(sp->ts_state) |
625 | 4348 sp->ts_state = STATE_ENDNUL; |
4349 sp->ts_save_badflags = su->su_badflags; | |
4350 | |
4351 /* At end of a prefix or at start of prefixtree: check for | |
4352 * following word. */ | |
4353 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX) | |
4354 { | |
4355 /* Set su->su_badflags to the caps type at this position. | |
4356 * Use the caps type until here for the prefix itself. */ | |
4357 if (has_mbyte) | |
4358 n = nofold_len(fword, sp->ts_fidx, su->su_badptr); | |
4359 else | |
4360 n = sp->ts_fidx; | |
4361 flags = badword_captype(su->su_badptr, su->su_badptr + n); | |
4362 su->su_badflags = badword_captype(su->su_badptr + n, | |
455 | 4363 su->su_badptr + su->su_badlen); |
625 | 4364 #ifdef DEBUG_TRIEWALK |
4365 sprintf(changename[depth], "prefix"); | |
4366 #endif | |
4367 go_deeper(stack, depth, 0); | |
4368 ++depth; | |
4369 sp = &stack[depth]; | |
4370 sp->ts_prefixdepth = depth - 1; | |
4371 byts = fbyts; | |
4372 idxs = fidxs; | |
4373 sp->ts_arridx = 0; | |
4374 | |
4375 /* Move the prefix to preword[] with the right case | |
4376 * and make find_keepcap_word() works. */ | |
4377 tword[sp->ts_twordlen] = NUL; | |
4378 make_case_word(tword + sp->ts_splitoff, | |
4379 preword + sp->ts_prewordlen, flags); | |
835 | 4380 sp->ts_prewordlen = (char_u)STRLEN(preword); |
625 | 4381 sp->ts_splitoff = sp->ts_twordlen; |
4382 } | |
4383 break; | |
4384 } | |
4385 | |
4386 if (sp->ts_curi > len || byts[arridx] != 0) | |
4387 { | |
4388 /* 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
|
4389 PROF_STORE(sp->ts_state) |
625 | 4390 sp->ts_state = STATE_ENDNUL; |
4391 sp->ts_save_badflags = su->su_badflags; | |
4392 break; | |
4393 } | |
4394 | |
4395 /* | |
4396 * End of word in tree. | |
4397 */ | |
4398 ++sp->ts_curi; /* eat one NUL byte */ | |
4399 | |
4400 flags = (int)idxs[arridx]; | |
698 | 4401 |
4402 /* Skip words with the NOSUGGEST flag. */ | |
4403 if (flags & WF_NOSUGGEST) | |
4404 break; | |
4405 | |
625 | 4406 fword_ends = (fword[sp->ts_fidx] == NUL |
4407 || (soundfold | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
4408 ? VIM_ISWHITE(fword[sp->ts_fidx]) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4409 : !spell_iswordp(fword + sp->ts_fidx, curwin))); |
625 | 4410 tword[sp->ts_twordlen] = NUL; |
4411 | |
4412 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL | |
485 | 4413 && (sp->ts_flags & TSF_PREFIXOK) == 0) |
625 | 4414 { |
4415 /* There was a prefix before the word. Check that the prefix | |
4416 * can be used with this word. */ | |
4417 /* Count the length of the NULs in the prefix. If there are | |
4418 * none this must be the first try without a prefix. */ | |
4419 n = stack[sp->ts_prefixdepth].ts_arridx; | |
4420 len = pbyts[n++]; | |
4421 for (c = 0; c < len && pbyts[n + c] == 0; ++c) | |
4422 ; | |
4423 if (c > 0) | |
4424 { | |
4425 c = valid_word_prefix(c, n, flags, | |
483 | 4426 tword + sp->ts_splitoff, slang, FALSE); |
625 | 4427 if (c == 0) |
4428 break; | |
4429 | |
4430 /* Use the WF_RARE flag for a rare prefix. */ | |
4431 if (c & WF_RAREPFX) | |
4432 flags |= WF_RARE; | |
4433 | |
4434 /* Tricky: when checking for both prefix and compounding | |
4435 * we run into the prefix flag first. | |
4436 * Remember that it's OK, so that we accept the prefix | |
4437 * when arriving at a compound flag. */ | |
4438 sp->ts_flags |= TSF_PREFIXOK; | |
4439 } | |
4440 } | |
4441 | |
4442 /* Check NEEDCOMPOUND: can't use word without compounding. Do try | |
4443 * appending another compound word below. */ | |
4444 if (sp->ts_complen == sp->ts_compsplit && fword_ends | |
500 | 4445 && (flags & WF_NEEDCOMP)) |
625 | 4446 goodword_ends = FALSE; |
4447 else | |
4448 goodword_ends = TRUE; | |
4449 | |
4450 p = NULL; | |
4451 compound_ok = TRUE; | |
4452 if (sp->ts_complen > sp->ts_compsplit) | |
4453 { | |
4454 if (slang->sl_nobreak) | |
4455 { | |
4456 /* There was a word before this word. When there was no | |
4457 * change in this word (it was correct) add the first word | |
4458 * as a suggestion. If this word was corrected too, we | |
4459 * need to check if a correct word follows. */ | |
4460 if (sp->ts_fidx - sp->ts_splitfidx | |
4461 == sp->ts_twordlen - sp->ts_splitoff | |
4462 && STRNCMP(fword + sp->ts_splitfidx, | |
4463 tword + sp->ts_splitoff, | |
4464 sp->ts_fidx - sp->ts_splitfidx) == 0) | |
492 | 4465 { |
625 | 4466 preword[sp->ts_prewordlen] = NUL; |
4467 newscore = score_wordcount_adj(slang, sp->ts_score, | |
4468 preword + sp->ts_prewordlen, | |
4469 sp->ts_prewordlen > 0); | |
4470 /* Add the suggestion if the score isn't too bad. */ | |
4471 if (newscore <= su->su_maxscore) | |
492 | 4472 add_suggestion(su, &su->su_ga, preword, |
503 | 4473 sp->ts_splitfidx - repextra, |
625 | 4474 newscore, 0, FALSE, |
4475 lp->lp_sallang, FALSE); | |
4476 break; | |
492 | 4477 } |
490 | 4478 } |
4479 else | |
625 | 4480 { |
4481 /* There was a compound word before this word. If this | |
4482 * word does not support compounding then give up | |
4483 * (splitting is tried for the word without compound | |
4484 * flag). */ | |
4485 if (((unsigned)flags >> 24) == 0 | |
4486 || sp->ts_twordlen - sp->ts_splitoff | |
4487 < slang->sl_compminlen) | |
4488 break; | |
4489 /* For multi-byte chars check character length against | |
4490 * COMPOUNDMIN. */ | |
4491 if (has_mbyte | |
4492 && slang->sl_compminlen > 0 | |
4493 && mb_charlen(tword + sp->ts_splitoff) | |
4494 < slang->sl_compminlen) | |
4495 break; | |
4496 | |
4497 compflags[sp->ts_complen] = ((unsigned)flags >> 24); | |
4498 compflags[sp->ts_complen + 1] = NUL; | |
4499 vim_strncpy(preword + sp->ts_prewordlen, | |
4500 tword + sp->ts_splitoff, | |
4501 sp->ts_twordlen - sp->ts_splitoff); | |
1762 | 4502 |
4503 /* Verify CHECKCOMPOUNDPATTERN rules. */ | |
4504 if (match_checkcompoundpattern(preword, sp->ts_prewordlen, | |
4505 &slang->sl_comppat)) | |
4506 compound_ok = FALSE; | |
4507 | |
4508 if (compound_ok) | |
4509 { | |
4510 p = preword; | |
4511 while (*skiptowhite(p) != NUL) | |
4512 p = skipwhite(skiptowhite(p)); | |
4513 if (fword_ends && !can_compound(slang, p, | |
625 | 4514 compflags + sp->ts_compsplit)) |
1762 | 4515 /* Compound is not allowed. But it may still be |
4516 * possible if we add another (short) word. */ | |
4517 compound_ok = FALSE; | |
4518 } | |
625 | 4519 |
4520 /* Get pointer to last char of previous word. */ | |
4521 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
|
4522 MB_PTR_BACK(preword, p); |
625 | 4523 } |
4524 } | |
4525 | |
4526 /* | |
4527 * Form the word with proper case in preword. | |
4528 * If there is a word from a previous split, append. | |
4529 * For the soundfold tree don't change the case, simply append. | |
4530 */ | |
4531 if (soundfold) | |
4532 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff); | |
4533 else if (flags & WF_KEEPCAP) | |
4534 /* Must find the word in the keep-case tree. */ | |
4535 find_keepcap_word(slang, tword + sp->ts_splitoff, | |
483 | 4536 preword + sp->ts_prewordlen); |
625 | 4537 else |
4538 { | |
4539 /* Include badflags: If the badword is onecap or allcap | |
4540 * use that for the goodword too. But if the badword is | |
4541 * allcap and it's only one char long use onecap. */ | |
4542 c = su->su_badflags; | |
4543 if ((c & WF_ALLCAP) | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4544 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)) |
625 | 4545 c = WF_ONECAP; |
4546 c |= flags; | |
4547 | |
4548 /* When appending a compound word after a word character don't | |
4549 * use Onecap. */ | |
5477 | 4550 if (p != NULL && spell_iswordp_nmw(p, curwin)) |
625 | 4551 c &= ~WF_ONECAP; |
4552 make_case_word(tword + sp->ts_splitoff, | |
490 | 4553 preword + sp->ts_prewordlen, c); |
625 | 4554 } |
4555 | |
4556 if (!soundfold) | |
4557 { | |
323 | 4558 /* Don't use a banned word. It may appear again as a good |
4559 * word, thus remember it. */ | |
4560 if (flags & WF_BANNED) | |
4561 { | |
483 | 4562 add_banned(su, preword + sp->ts_prewordlen); |
323 | 4563 break; |
4564 } | |
534 | 4565 if ((sp->ts_complen == sp->ts_compsplit |
625 | 4566 && WAS_BANNED(su, preword + sp->ts_prewordlen)) |
4567 || WAS_BANNED(su, preword)) | |
534 | 4568 { |
4569 if (slang->sl_compprog == NULL) | |
4570 break; | |
4571 /* the word so far was banned but we may try compounding */ | |
4572 goodword_ends = FALSE; | |
4573 } | |
625 | 4574 } |
4575 | |
4576 newscore = 0; | |
4577 if (!soundfold) /* soundfold words don't have flags */ | |
4578 { | |
323 | 4579 if ((flags & WF_REGION) |
390 | 4580 && (((unsigned)flags >> 16) & lp->lp_region) == 0) |
323 | 4581 newscore += SCORE_REGION; |
4582 if (flags & WF_RARE) | |
4583 newscore += SCORE_RARE; | |
4584 | |
346 | 4585 if (!spell_valid_case(su->su_badflags, |
483 | 4586 captype(preword + sp->ts_prewordlen, NULL))) |
323 | 4587 newscore += SCORE_ICASE; |
625 | 4588 } |
4589 | |
4590 /* TODO: how about splitting in the soundfold tree? */ | |
4591 if (fword_ends | |
4592 && goodword_ends | |
4593 && sp->ts_fidx >= sp->ts_fidxtry | |
4594 && compound_ok) | |
4595 { | |
4596 /* The badword also ends: add suggestions. */ | |
4597 #ifdef DEBUG_TRIEWALK | |
4598 if (soundfold && STRCMP(preword, "smwrd") == 0) | |
4599 { | |
4600 int j; | |
4601 | |
4602 /* print the stack of changes that brought us here */ | |
4603 smsg("------ %s -------", fword); | |
4604 for (j = 0; j < depth; ++j) | |
4605 smsg("%s", changename[j]); | |
4606 } | |
4607 #endif | |
4608 if (soundfold) | |
4609 { | |
4610 /* For soundfolded words we need to find the original | |
1188 | 4611 * words, the edit distance and then add them. */ |
625 | 4612 add_sound_suggest(su, preword, sp->ts_score, lp); |
4613 } | |
2457
6c1d10a85398
Avoid illegal memory access in spell suggestion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2455
diff
changeset
|
4614 else if (sp->ts_fidx > 0) |
625 | 4615 { |
4616 /* Give a penalty when changing non-word char to word | |
4617 * char, e.g., "thes," -> "these". */ | |
366 | 4618 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
|
4619 MB_PTR_BACK(fword, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4620 if (!spell_iswordp(p, curwin)) |
366 | 4621 { |
4622 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
|
4623 MB_PTR_BACK(preword, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4624 if (spell_iswordp(p, curwin)) |
366 | 4625 newscore += SCORE_NONWORD; |
4626 } | |
4627 | |
625 | 4628 /* Give a bonus to words seen before. */ |
4629 score = score_wordcount_adj(slang, | |
4630 sp->ts_score + newscore, | |
4631 preword + sp->ts_prewordlen, | |
4632 sp->ts_prewordlen > 0); | |
4633 | |
4634 /* Add the suggestion if the score isn't too bad. */ | |
4635 if (score <= su->su_maxscore) | |
633 | 4636 { |
625 | 4637 add_suggestion(su, &su->su_ga, preword, |
4638 sp->ts_fidx - repextra, | |
4639 score, 0, FALSE, lp->lp_sallang, FALSE); | |
633 | 4640 |
4641 if (su->su_badflags & WF_MIXCAP) | |
4642 { | |
4643 /* We really don't know if the word should be | |
4644 * upper or lower case, add both. */ | |
4645 c = captype(preword, NULL); | |
4646 if (c == 0 || c == WF_ALLCAP) | |
4647 { | |
4648 make_case_word(tword + sp->ts_splitoff, | |
4649 preword + sp->ts_prewordlen, | |
4650 c == 0 ? WF_ALLCAP : 0); | |
4651 | |
4652 add_suggestion(su, &su->su_ga, preword, | |
4653 sp->ts_fidx - repextra, | |
4654 score + SCORE_ICASE, 0, FALSE, | |
4655 lp->lp_sallang, FALSE); | |
4656 } | |
4657 } | |
4658 } | |
625 | 4659 } |
4660 } | |
4661 | |
4662 /* | |
4663 * Try word split and/or compounding. | |
4664 */ | |
4665 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends) | |
4666 /* Don't split halfway a character. */ | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4667 && (!has_mbyte || sp->ts_tcharlen == 0)) |
625 | 4668 { |
4669 int try_compound; | |
4670 int try_split; | |
4671 | |
4672 /* If past the end of the bad word don't try a split. | |
4673 * Otherwise try changing the next word. E.g., find | |
4674 * suggestions for "the the" where the second "the" is | |
4675 * different. It's done like a split. | |
4676 * TODO: word split for soundfold words */ | |
4677 try_split = (sp->ts_fidx - repextra < su->su_badlen) | |
4678 && !soundfold; | |
4679 | |
4680 /* Get here in several situations: | |
4681 * 1. The word in the tree ends: | |
4682 * If the word allows compounding try that. Otherwise try | |
4683 * a split by inserting a space. For both check that a | |
4684 * valid words starts at fword[sp->ts_fidx]. | |
4685 * For NOBREAK do like compounding to be able to check if | |
4686 * the next word is valid. | |
4687 * 2. The badword does end, but it was due to a change (e.g., | |
4688 * a swap). No need to split, but do check that the | |
4689 * following word is valid. | |
4690 * 3. The badword and the word in the tree end. It may still | |
4691 * be possible to compound another (short) word. | |
4692 */ | |
4693 try_compound = FALSE; | |
4694 if (!soundfold | |
7526
08c1f73efcde
commit https://github.com/vim/vim/commit/7b877b360532713dc21a0ff3d55a76ac02eaf573
Christian Brabandt <cb@256bit.org>
parents:
7509
diff
changeset
|
4695 && !slang->sl_nocompoundsugs |
625 | 4696 && slang->sl_compprog != NULL |
4697 && ((unsigned)flags >> 24) != 0 | |
4698 && sp->ts_twordlen - sp->ts_splitoff | |
4699 >= slang->sl_compminlen | |
4700 && (!has_mbyte | |
4701 || slang->sl_compminlen == 0 | |
4702 || mb_charlen(tword + sp->ts_splitoff) | |
4703 >= slang->sl_compminlen) | |
4704 && (slang->sl_compsylmax < MAXWLEN | |
4705 || sp->ts_complen + 1 - sp->ts_compsplit | |
4706 < slang->sl_compmax) | |
1762 | 4707 && (can_be_compound(sp, slang, |
4708 compflags, ((unsigned)flags >> 24)))) | |
4709 | |
625 | 4710 { |
4711 try_compound = TRUE; | |
4712 compflags[sp->ts_complen] = ((unsigned)flags >> 24); | |
4713 compflags[sp->ts_complen + 1] = NUL; | |
4714 } | |
4715 | |
4716 /* For NOBREAK we never try splitting, it won't make any word | |
4717 * valid. */ | |
7526
08c1f73efcde
commit https://github.com/vim/vim/commit/7b877b360532713dc21a0ff3d55a76ac02eaf573
Christian Brabandt <cb@256bit.org>
parents:
7509
diff
changeset
|
4718 if (slang->sl_nobreak && !slang->sl_nocompoundsugs) |
625 | 4719 try_compound = TRUE; |
4720 | |
4721 /* If we could add a compound word, and it's also possible to | |
4722 * split at this point, do the split first and set | |
4723 * TSF_DIDSPLIT to avoid doing it again. */ | |
4724 else if (!fword_ends | |
4725 && try_compound | |
4726 && (sp->ts_flags & TSF_DIDSPLIT) == 0) | |
4727 { | |
485 | 4728 try_compound = FALSE; |
625 | 4729 sp->ts_flags |= TSF_DIDSPLIT; |
4730 --sp->ts_curi; /* do the same NUL again */ | |
4731 compflags[sp->ts_complen] = NUL; | |
4732 } | |
4733 else | |
4734 sp->ts_flags &= ~TSF_DIDSPLIT; | |
4735 | |
4736 if (try_split || try_compound) | |
4737 { | |
534 | 4738 if (!try_compound && (!fword_ends || !goodword_ends)) |
485 | 4739 { |
4740 /* If we're going to split need to check that the | |
501 | 4741 * words so far are valid for compounding. If there |
4742 * is only one word it must not have the NEEDCOMPOUND | |
4743 * flag. */ | |
4744 if (sp->ts_complen == sp->ts_compsplit | |
4745 && (flags & WF_NEEDCOMP)) | |
4746 break; | |
490 | 4747 p = preword; |
4748 while (*skiptowhite(p) != NUL) | |
4749 p = skipwhite(skiptowhite(p)); | |
485 | 4750 if (sp->ts_complen > sp->ts_compsplit |
490 | 4751 && !can_compound(slang, p, |
485 | 4752 compflags + sp->ts_compsplit)) |
4753 break; | |
698 | 4754 |
4755 if (slang->sl_nosplitsugs) | |
4756 newscore += SCORE_SPLIT_NO; | |
4757 else | |
4758 newscore += SCORE_SPLIT; | |
625 | 4759 |
4760 /* Give a bonus to words seen before. */ | |
4761 newscore = score_wordcount_adj(slang, newscore, | |
4762 preword + sp->ts_prewordlen, TRUE); | |
482 | 4763 } |
4764 | |
625 | 4765 if (TRY_DEEPER(su, stack, depth, newscore)) |
323 | 4766 { |
625 | 4767 go_deeper(stack, depth, newscore); |
4768 #ifdef DEBUG_TRIEWALK | |
4769 if (!try_compound && !fword_ends) | |
4770 sprintf(changename[depth], "%.*s-%s: split", | |
4771 sp->ts_twordlen, tword, fword + sp->ts_fidx); | |
4772 else | |
4773 sprintf(changename[depth], "%.*s-%s: compound", | |
4774 sp->ts_twordlen, tword, fword + sp->ts_fidx); | |
4775 #endif | |
323 | 4776 /* Save things to be restored at STATE_SPLITUNDO. */ |
346 | 4777 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
|
4778 PROF_STORE(sp->ts_state) |
376 | 4779 sp->ts_state = STATE_SPLITUNDO; |
4780 | |
4781 ++depth; | |
4782 sp = &stack[depth]; | |
323 | 4783 |
482 | 4784 /* Append a space to preword when splitting. */ |
4785 if (!try_compound && !fword_ends) | |
4786 STRCAT(preword, " "); | |
835 | 4787 sp->ts_prewordlen = (char_u)STRLEN(preword); |
483 | 4788 sp->ts_splitoff = sp->ts_twordlen; |
492 | 4789 sp->ts_splitfidx = sp->ts_fidx; |
376 | 4790 |
4791 /* If the badword has a non-word character at this | |
4792 * position skip it. That means replacing the | |
482 | 4793 * non-word character with a space. Always skip a |
534 | 4794 * character when the word ends. But only when the |
4795 * good word can end. */ | |
625 | 4796 if (((!try_compound && !spell_iswordp_nmw(fword |
5477 | 4797 + sp->ts_fidx, |
4798 curwin)) | |
625 | 4799 || fword_ends) |
4800 && fword[sp->ts_fidx] != NUL | |
4801 && goodword_ends) | |
376 | 4802 { |
482 | 4803 int l; |
4804 | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4805 l = MB_PTR2LEN(fword + sp->ts_fidx); |
482 | 4806 if (fword_ends) |
4807 { | |
4808 /* Copy the skipped character to preword. */ | |
483 | 4809 mch_memmove(preword + sp->ts_prewordlen, |
482 | 4810 fword + sp->ts_fidx, l); |
483 | 4811 sp->ts_prewordlen += l; |
4812 preword[sp->ts_prewordlen] = NUL; | |
482 | 4813 } |
4814 else | |
4815 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; | |
4816 sp->ts_fidx += l; | |
376 | 4817 } |
455 | 4818 |
485 | 4819 /* When compounding include compound flag in |
4820 * compflags[] (already set above). When splitting we | |
4821 * may start compounding over again. */ | |
482 | 4822 if (try_compound) |
483 | 4823 ++sp->ts_complen; |
482 | 4824 else |
485 | 4825 sp->ts_compsplit = sp->ts_complen; |
4826 sp->ts_prefixdepth = PFD_NOPREFIX; | |
482 | 4827 |
455 | 4828 /* set su->su_badflags to the caps type at this |
4829 * position */ | |
324 | 4830 if (has_mbyte) |
455 | 4831 n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
324 | 4832 else |
455 | 4833 n = sp->ts_fidx; |
474 | 4834 su->su_badflags = badword_captype(su->su_badptr + n, |
455 | 4835 su->su_badptr + su->su_badlen); |
323 | 4836 |
4837 /* Restart at top of the tree. */ | |
376 | 4838 sp->ts_arridx = 0; |
485 | 4839 |
4840 /* If there are postponed prefixes, try these too. */ | |
4841 if (pbyts != NULL) | |
4842 { | |
4843 byts = pbyts; | |
4844 idxs = pidxs; | |
4845 sp->ts_prefixdepth = PFD_PREFIXTREE; | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4846 PROF_STORE(sp->ts_state) |
485 | 4847 sp->ts_state = STATE_NOPREFIX; |
4848 } | |
323 | 4849 } |
4850 } | |
625 | 4851 } |
4852 break; | |
4853 | |
4854 case STATE_SPLITUNDO: | |
4855 /* Undo the changes done for word split or compound word. */ | |
4856 su->su_badflags = sp->ts_save_badflags; | |
4857 | |
4858 /* Continue looking for NUL bytes. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4859 PROF_STORE(sp->ts_state) |
625 | 4860 sp->ts_state = STATE_START; |
4861 | |
4862 /* In case we went into the prefix tree. */ | |
4863 byts = fbyts; | |
4864 idxs = fidxs; | |
4865 break; | |
4866 | |
4867 case STATE_ENDNUL: | |
4868 /* Past the NUL bytes in the node. */ | |
4869 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
|
4870 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0) |
625 | 4871 { |
4872 /* 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
|
4873 PROF_STORE(sp->ts_state) |
625 | 4874 sp->ts_state = STATE_DEL; |
323 | 4875 break; |
625 | 4876 } |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4877 PROF_STORE(sp->ts_state) |
625 | 4878 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
|
4879 /* FALLTHROUGH */ |
625 | 4880 |
4881 case STATE_PLAIN: | |
4882 /* | |
4883 * Go over all possible bytes at this node, add each to tword[] | |
4884 * and use child node. "ts_curi" is the index. | |
4885 */ | |
4886 arridx = sp->ts_arridx; | |
4887 if (sp->ts_curi > byts[arridx]) | |
4888 { | |
4889 /* Done all bytes at this node, do next state. When still at | |
4890 * 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
|
4891 PROF_STORE(sp->ts_state) |
625 | 4892 if (sp->ts_fidx >= sp->ts_fidxtry) |
323 | 4893 sp->ts_state = STATE_DEL; |
4894 else | |
625 | 4895 sp->ts_state = STATE_FINAL; |
4896 } | |
4897 else | |
4898 { | |
4899 arridx += sp->ts_curi++; | |
4900 c = byts[arridx]; | |
4901 | |
4902 /* Normal byte, go one level deeper. If it's not equal to the | |
4903 * byte in the bad word adjust the score. But don't even try | |
4904 * when the byte was already changed. And don't try when we | |
5695 | 4905 * just deleted this byte, accepting it is always cheaper than |
625 | 4906 * delete + substitute. */ |
4907 if (c == fword[sp->ts_fidx] | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4908 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)) |
625 | 4909 newscore = 0; |
4910 else | |
4911 newscore = SCORE_SUBST; | |
4912 if ((newscore == 0 | |
4913 || (sp->ts_fidx >= sp->ts_fidxtry | |
4914 && ((sp->ts_flags & TSF_DIDDEL) == 0 | |
4915 || c != fword[sp->ts_delidx]))) | |
4916 && TRY_DEEPER(su, stack, depth, newscore)) | |
4917 { | |
4918 go_deeper(stack, depth, newscore); | |
4919 #ifdef DEBUG_TRIEWALK | |
4920 if (newscore > 0) | |
4921 sprintf(changename[depth], "%.*s-%s: subst %c to %c", | |
4922 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
4923 fword[sp->ts_fidx], c); | |
323 | 4924 else |
625 | 4925 sprintf(changename[depth], "%.*s-%s: accept %c", |
4926 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
4927 fword[sp->ts_fidx]); | |
4928 #endif | |
323 | 4929 ++depth; |
625 | 4930 sp = &stack[depth]; |
4931 ++sp->ts_fidx; | |
4932 tword[sp->ts_twordlen++] = c; | |
4933 sp->ts_arridx = idxs[arridx]; | |
4934 if (newscore == SCORE_SUBST) | |
4935 sp->ts_isdiff = DIFF_YES; | |
330 | 4936 if (has_mbyte) |
358 | 4937 { |
625 | 4938 /* Multi-byte characters are a bit complicated to |
4939 * handle: They differ when any of the bytes differ | |
4940 * and then their length may also differ. */ | |
4941 if (sp->ts_tcharlen == 0) | |
4942 { | |
4943 /* First byte. */ | |
4944 sp->ts_tcharidx = 0; | |
4945 sp->ts_tcharlen = MB_BYTE2LEN(c); | |
4946 sp->ts_fcharstart = sp->ts_fidx - 1; | |
4947 sp->ts_isdiff = (newscore != 0) | |
4948 ? DIFF_YES : DIFF_NONE; | |
4949 } | |
4950 else if (sp->ts_isdiff == DIFF_INSERT) | |
4951 /* When inserting trail bytes don't advance in the | |
4952 * bad word. */ | |
4953 --sp->ts_fidx; | |
4954 if (++sp->ts_tcharidx == sp->ts_tcharlen) | |
4955 { | |
4956 /* Last byte of character. */ | |
4957 if (sp->ts_isdiff == DIFF_YES) | |
4958 { | |
4959 /* Correct ts_fidx for the byte length of the | |
4960 * character (we didn't check that before). */ | |
4961 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
|
4962 + MB_PTR2LEN( |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4963 fword + sp->ts_fcharstart); |
625 | 4964 /* For changing a composing character adjust |
4965 * the score from SCORE_SUBST to | |
4966 * SCORE_SUBCOMP. */ | |
4967 if (enc_utf8 | |
4968 && 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
|
4969 utf_ptr2char(tword |
625 | 4970 + sp->ts_twordlen |
4971 - sp->ts_tcharlen)) | |
4972 && 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
|
4973 utf_ptr2char(fword |
625 | 4974 + sp->ts_fcharstart))) |
4975 sp->ts_score -= | |
4976 SCORE_SUBST - SCORE_SUBCOMP; | |
4977 | |
4978 /* For a similar character adjust score from | |
4979 * SCORE_SUBST to SCORE_SIMILAR. */ | |
4980 else if (!soundfold | |
4981 && slang->sl_has_map | |
4982 && similar_chars(slang, | |
4983 mb_ptr2char(tword | |
4984 + sp->ts_twordlen | |
4985 - sp->ts_tcharlen), | |
4986 mb_ptr2char(fword | |
4987 + sp->ts_fcharstart))) | |
4988 sp->ts_score -= | |
4989 SCORE_SUBST - SCORE_SIMILAR; | |
4990 } | |
4991 else if (sp->ts_isdiff == DIFF_INSERT | |
4992 && sp->ts_twordlen > sp->ts_tcharlen) | |
4993 { | |
4994 p = tword + sp->ts_twordlen - sp->ts_tcharlen; | |
4995 c = mb_ptr2char(p); | |
4996 if (enc_utf8 && utf_iscomposing(c)) | |
4997 { | |
4998 /* Inserting a composing char doesn't | |
4999 * count that much. */ | |
5000 sp->ts_score -= SCORE_INS - SCORE_INSCOMP; | |
5001 } | |
5002 else | |
5003 { | |
5004 /* If the previous character was the same, | |
5005 * thus doubling a character, give a bonus | |
5006 * to the score. Also for the soundfold | |
5007 * tree (might seem illogical but does | |
5008 * give better scores). */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5009 MB_PTR_BACK(tword, p); |
625 | 5010 if (c == mb_ptr2char(p)) |
5011 sp->ts_score -= SCORE_INS | |
5012 - SCORE_INSDUP; | |
5013 } | |
5014 } | |
5015 | |
5016 /* Starting a new char, reset the length. */ | |
5017 sp->ts_tcharlen = 0; | |
5018 } | |
358 | 5019 } |
330 | 5020 else |
358 | 5021 { |
625 | 5022 /* If we found a similar char adjust the score. |
5023 * We do this after calling go_deeper() because | |
5024 * it's slow. */ | |
5025 if (newscore != 0 | |
5026 && !soundfold | |
5027 && slang->sl_has_map | |
5028 && similar_chars(slang, | |
5029 c, fword[sp->ts_fidx - 1])) | |
5030 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; | |
358 | 5031 } |
625 | 5032 } |
5033 } | |
5034 break; | |
5035 | |
5036 case STATE_DEL: | |
5037 /* When past the first byte of a multi-byte char don't try | |
5038 * delete/insert/swap a character. */ | |
5039 if (has_mbyte && sp->ts_tcharlen > 0) | |
5040 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5041 PROF_STORE(sp->ts_state) |
625 | 5042 sp->ts_state = STATE_FINAL; |
5043 break; | |
5044 } | |
5045 /* | |
5046 * Try skipping one character in the bad word (delete it). | |
5047 */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5048 PROF_STORE(sp->ts_state) |
625 | 5049 sp->ts_state = STATE_INS_PREP; |
5050 sp->ts_curi = 1; | |
5051 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*') | |
5052 /* Deleting a vowel at the start of a word counts less, see | |
5053 * soundalike_score(). */ | |
5054 newscore = 2 * SCORE_DEL / 3; | |
5055 else | |
5056 newscore = SCORE_DEL; | |
5057 if (fword[sp->ts_fidx] != NUL | |
5058 && TRY_DEEPER(su, stack, depth, newscore)) | |
5059 { | |
5060 go_deeper(stack, depth, newscore); | |
5061 #ifdef DEBUG_TRIEWALK | |
5062 sprintf(changename[depth], "%.*s-%s: delete %c", | |
5063 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5064 fword[sp->ts_fidx]); | |
5065 #endif | |
5066 ++depth; | |
5067 | |
5068 /* Remember what character we deleted, so that we can avoid | |
5069 * inserting it again. */ | |
5070 stack[depth].ts_flags |= TSF_DIDDEL; | |
5071 stack[depth].ts_delidx = sp->ts_fidx; | |
5072 | |
5073 /* Advance over the character in fword[]. Give a bonus to the | |
5074 * score if the same character is following "nn" -> "n". It's | |
5075 * a bit illogical for soundfold tree but it does give better | |
5076 * results. */ | |
5077 if (has_mbyte) | |
5078 { | |
5079 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
|
5080 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx); |
625 | 5081 if (enc_utf8 && utf_iscomposing(c)) |
5082 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP; | |
5083 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) | |
5084 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; | |
323 | 5085 } |
5086 else | |
625 | 5087 { |
5088 ++stack[depth].ts_fidx; | |
5089 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) | |
5090 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; | |
323 | 5091 } |
5092 break; | |
625 | 5093 } |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5094 /* FALLTHROUGH */ |
625 | 5095 |
5096 case STATE_INS_PREP: | |
5097 if (sp->ts_flags & TSF_DIDDEL) | |
5098 { | |
5099 /* If we just deleted a byte then inserting won't make sense, | |
5100 * a substitute is always cheaper. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5101 PROF_STORE(sp->ts_state) |
625 | 5102 sp->ts_state = STATE_SWAP; |
5103 break; | |
5104 } | |
5105 | |
5106 /* skip over NUL bytes */ | |
5107 n = sp->ts_arridx; | |
5108 for (;;) | |
5109 { | |
5110 if (sp->ts_curi > byts[n]) | |
5111 { | |
5112 /* 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
|
5113 PROF_STORE(sp->ts_state) |
625 | 5114 sp->ts_state = STATE_SWAP; |
5115 break; | |
5116 } | |
5117 if (byts[n + sp->ts_curi] != NUL) | |
5118 { | |
5119 /* Found a byte to insert. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5120 PROF_STORE(sp->ts_state) |
625 | 5121 sp->ts_state = STATE_INS; |
330 | 5122 break; |
5123 } | |
625 | 5124 ++sp->ts_curi; |
5125 } | |
5126 break; | |
5127 | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5128 /* FALLTHROUGH */ |
625 | 5129 |
5130 case STATE_INS: | |
5131 /* Insert one byte. Repeat this for each possible byte at this | |
5132 * node. */ | |
5133 n = sp->ts_arridx; | |
5134 if (sp->ts_curi > byts[n]) | |
5135 { | |
5136 /* 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
|
5137 PROF_STORE(sp->ts_state) |
625 | 5138 sp->ts_state = STATE_SWAP; |
5139 break; | |
5140 } | |
5141 | |
5142 /* Do one more byte at this node, but: | |
5143 * - Skip NUL bytes. | |
5144 * - Skip the byte if it's equal to the byte in the word, | |
5145 * accepting that byte is always better. | |
5146 */ | |
5147 n += sp->ts_curi++; | |
5148 c = byts[n]; | |
5149 if (soundfold && sp->ts_twordlen == 0 && c == '*') | |
5150 /* Inserting a vowel at the start of a word counts less, | |
5151 * see soundalike_score(). */ | |
5152 newscore = 2 * SCORE_INS / 3; | |
5153 else | |
5154 newscore = SCORE_INS; | |
5155 if (c != fword[sp->ts_fidx] | |
5156 && TRY_DEEPER(su, stack, depth, newscore)) | |
5157 { | |
5158 go_deeper(stack, depth, newscore); | |
5159 #ifdef DEBUG_TRIEWALK | |
5160 sprintf(changename[depth], "%.*s-%s: insert %c", | |
5161 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5162 c); | |
5163 #endif | |
5164 ++depth; | |
5165 sp = &stack[depth]; | |
5166 tword[sp->ts_twordlen++] = c; | |
5167 sp->ts_arridx = idxs[n]; | |
330 | 5168 if (has_mbyte) |
5169 { | |
625 | 5170 fl = MB_BYTE2LEN(c); |
5171 if (fl > 1) | |
330 | 5172 { |
625 | 5173 /* There are following bytes for the same character. |
5174 * We must find all bytes before trying | |
5175 * delete/insert/swap/etc. */ | |
5176 sp->ts_tcharlen = fl; | |
5177 sp->ts_tcharidx = 1; | |
5178 sp->ts_isdiff = DIFF_INSERT; | |
330 | 5179 } |
323 | 5180 } |
5181 else | |
625 | 5182 fl = 1; |
5183 if (fl == 1) | |
5184 { | |
5185 /* If the previous character was the same, thus doubling a | |
5186 * character, give a bonus to the score. Also for | |
5187 * soundfold words (illogical but does give a better | |
5188 * score). */ | |
5189 if (sp->ts_twordlen >= 2 | |
5190 && tword[sp->ts_twordlen - 2] == c) | |
5191 sp->ts_score -= SCORE_INS - SCORE_INSDUP; | |
5192 } | |
5193 } | |
5194 break; | |
5195 | |
5196 case STATE_SWAP: | |
5197 /* | |
5198 * Swap two bytes in the bad word: "12" -> "21". | |
5199 * We change "fword" here, it's changed back afterwards at | |
5200 * STATE_UNSWAP. | |
5201 */ | |
5202 p = fword + sp->ts_fidx; | |
5203 c = *p; | |
5204 if (c == NUL) | |
5205 { | |
5206 /* 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
|
5207 PROF_STORE(sp->ts_state) |
625 | 5208 sp->ts_state = STATE_FINAL; |
5209 break; | |
5210 } | |
5211 | |
5212 /* Don't swap if the first character is not a word character. | |
5213 * 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
|
5214 if (!soundfold && !spell_iswordp(p, curwin)) |
625 | 5215 { |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5216 PROF_STORE(sp->ts_state) |
625 | 5217 sp->ts_state = STATE_REP_INI; |
323 | 5218 break; |
625 | 5219 } |
5220 | |
5221 if (has_mbyte) | |
5222 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5223 n = MB_CPTR2LEN(p); |
625 | 5224 c = mb_ptr2char(p); |
1337 | 5225 if (p[n] == NUL) |
5226 c2 = NUL; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5227 else if (!soundfold && !spell_iswordp(p + n, curwin)) |
625 | 5228 c2 = c; /* don't swap non-word char */ |
5229 else | |
5230 c2 = mb_ptr2char(p + n); | |
5231 } | |
5232 else | |
5233 { | |
1337 | 5234 if (p[1] == NUL) |
5235 c2 = NUL; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5236 else if (!soundfold && !spell_iswordp(p + 1, curwin)) |
625 | 5237 c2 = c; /* don't swap non-word char */ |
5238 else | |
5239 c2 = p[1]; | |
5240 } | |
5241 | |
1337 | 5242 /* When the second character is NUL we can't swap. */ |
5243 if (c2 == NUL) | |
5244 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5245 PROF_STORE(sp->ts_state) |
1337 | 5246 sp->ts_state = STATE_REP_INI; |
5247 break; | |
5248 } | |
5249 | |
625 | 5250 /* When characters are identical, swap won't do anything. |
5251 * Also get here if the second char is not a word character. */ | |
5252 if (c == c2) | |
5253 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5254 PROF_STORE(sp->ts_state) |
625 | 5255 sp->ts_state = STATE_SWAP3; |
5256 break; | |
5257 } | |
5258 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP)) | |
5259 { | |
5260 go_deeper(stack, depth, SCORE_SWAP); | |
5261 #ifdef DEBUG_TRIEWALK | |
5262 sprintf(changename[depth], "%.*s-%s: swap %c and %c", | |
5263 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5264 c, c2); | |
5265 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5266 PROF_STORE(sp->ts_state) |
625 | 5267 sp->ts_state = STATE_UNSWAP; |
5268 ++depth; | |
330 | 5269 if (has_mbyte) |
5270 { | |
625 | 5271 fl = mb_char2len(c2); |
5272 mch_memmove(p, p + n, fl); | |
5273 mb_char2bytes(c, p + fl); | |
5274 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; | |
330 | 5275 } |
5276 else | |
5277 { | |
625 | 5278 p[0] = c2; |
330 | 5279 p[1] = c; |
625 | 5280 stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
5281 } | |
5282 } | |
5283 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5284 { |
625 | 5285 /* 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
|
5286 PROF_STORE(sp->ts_state) |
625 | 5287 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
|
5288 } |
625 | 5289 break; |
5290 | |
5291 case STATE_UNSWAP: | |
5292 /* Undo the STATE_SWAP swap: "21" -> "12". */ | |
5293 p = fword + sp->ts_fidx; | |
5294 if (has_mbyte) | |
5295 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5296 n = MB_PTR2LEN(p); |
625 | 5297 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
|
5298 mch_memmove(p + MB_PTR2LEN(p + n), p, n); |
625 | 5299 mb_char2bytes(c, p); |
5300 } | |
5301 else | |
5302 { | |
5303 c = *p; | |
5304 *p = p[1]; | |
5305 p[1] = c; | |
5306 } | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5307 /* FALLTHROUGH */ |
625 | 5308 |
5309 case STATE_SWAP3: | |
5310 /* Swap two bytes, skipping one: "123" -> "321". We change | |
5311 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */ | |
5312 p = fword + sp->ts_fidx; | |
5313 if (has_mbyte) | |
5314 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5315 n = MB_CPTR2LEN(p); |
625 | 5316 c = mb_ptr2char(p); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5317 fl = MB_CPTR2LEN(p + n); |
625 | 5318 c2 = mb_ptr2char(p + n); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5319 if (!soundfold && !spell_iswordp(p + n + fl, curwin)) |
625 | 5320 c3 = c; /* don't swap non-word char */ |
5321 else | |
5322 c3 = mb_ptr2char(p + n + fl); | |
5323 } | |
5324 else | |
5325 { | |
5326 c = *p; | |
5327 c2 = p[1]; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5328 if (!soundfold && !spell_iswordp(p + 2, curwin)) |
625 | 5329 c3 = c; /* don't swap non-word char */ |
5330 else | |
5331 c3 = p[2]; | |
5332 } | |
5333 | |
5334 /* When characters are identical: "121" then SWAP3 result is | |
5335 * identical, ROT3L result is same as SWAP: "211", ROT3L result is | |
5336 * same as SWAP on next char: "112". Thus skip all swapping. | |
5337 * Also skip when c3 is NUL. | |
5338 * Also get here when the third character is not a word character. | |
5339 * Second character may any char: "a.b" -> "b.a" */ | |
5340 if (c == c3 || c3 == NUL) | |
5341 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5342 PROF_STORE(sp->ts_state) |
625 | 5343 sp->ts_state = STATE_REP_INI; |
5344 break; | |
5345 } | |
5346 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) | |
5347 { | |
5348 go_deeper(stack, depth, SCORE_SWAP3); | |
5349 #ifdef DEBUG_TRIEWALK | |
5350 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c", | |
5351 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5352 c, c3); | |
5353 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5354 PROF_STORE(sp->ts_state) |
625 | 5355 sp->ts_state = STATE_UNSWAP3; |
5356 ++depth; | |
5357 if (has_mbyte) | |
5358 { | |
5359 tl = mb_char2len(c3); | |
5360 mch_memmove(p, p + n + fl, tl); | |
5361 mb_char2bytes(c2, p + tl); | |
5362 mb_char2bytes(c, p + fl + tl); | |
5363 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; | |
5364 } | |
5365 else | |
5366 { | |
5367 p[0] = p[2]; | |
5368 p[2] = c; | |
5369 stack[depth].ts_fidxtry = sp->ts_fidx + 3; | |
5370 } | |
5371 } | |
5372 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5373 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5374 PROF_STORE(sp->ts_state) |
625 | 5375 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
|
5376 } |
625 | 5377 break; |
5378 | |
5379 case STATE_UNSWAP3: | |
5380 /* Undo STATE_SWAP3: "321" -> "123" */ | |
5381 p = fword + sp->ts_fidx; | |
5382 if (has_mbyte) | |
5383 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5384 n = MB_PTR2LEN(p); |
625 | 5385 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
|
5386 fl = MB_PTR2LEN(p + n); |
625 | 5387 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
|
5388 tl = MB_PTR2LEN(p + n + fl); |
625 | 5389 mch_memmove(p + fl + tl, p, n); |
5390 mb_char2bytes(c, p); | |
5391 mb_char2bytes(c2, p + tl); | |
5392 p = p + tl; | |
5393 } | |
5394 else | |
5395 { | |
5396 c = *p; | |
5397 *p = p[2]; | |
5398 p[2] = c; | |
5399 ++p; | |
5400 } | |
5401 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5402 if (!soundfold && !spell_iswordp(p, curwin)) |
625 | 5403 { |
5404 /* Middle char is not a word char, skip the rotate. First and | |
5405 * 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
|
5406 PROF_STORE(sp->ts_state) |
625 | 5407 sp->ts_state = STATE_REP_INI; |
5408 break; | |
5409 } | |
5410 | |
5411 /* Rotate three characters left: "123" -> "231". We change | |
5412 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */ | |
5413 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) | |
5414 { | |
5415 go_deeper(stack, depth, SCORE_SWAP3); | |
5416 #ifdef DEBUG_TRIEWALK | |
5417 p = fword + sp->ts_fidx; | |
5418 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c", | |
5419 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5420 p[0], p[1], p[2]); | |
5421 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5422 PROF_STORE(sp->ts_state) |
625 | 5423 sp->ts_state = STATE_UNROT3L; |
5424 ++depth; | |
330 | 5425 p = fword + sp->ts_fidx; |
5426 if (has_mbyte) | |
5427 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5428 n = MB_CPTR2LEN(p); |
330 | 5429 c = mb_ptr2char(p); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5430 fl = MB_CPTR2LEN(p + n); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5431 fl += MB_CPTR2LEN(p + n + fl); |
625 | 5432 mch_memmove(p, p + n, fl); |
5433 mb_char2bytes(c, p + fl); | |
5434 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; | |
330 | 5435 } |
5436 else | |
323 | 5437 { |
330 | 5438 c = *p; |
625 | 5439 *p = p[1]; |
5440 p[1] = p[2]; | |
5441 p[2] = c; | |
5442 stack[depth].ts_fidxtry = sp->ts_fidx + 3; | |
5443 } | |
5444 } | |
5445 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5446 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5447 PROF_STORE(sp->ts_state) |
625 | 5448 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
|
5449 } |
625 | 5450 break; |
5451 | |
5452 case STATE_UNROT3L: | |
5453 /* Undo ROT3L: "231" -> "123" */ | |
5454 p = fword + sp->ts_fidx; | |
5455 if (has_mbyte) | |
5456 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5457 n = MB_PTR2LEN(p); |
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 + n); |
625 | 5459 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
|
5460 tl = MB_PTR2LEN(p + n); |
625 | 5461 mch_memmove(p + tl, p, n); |
5462 mb_char2bytes(c, p); | |
5463 } | |
5464 else | |
5465 { | |
5466 c = p[2]; | |
5467 p[2] = p[1]; | |
5468 p[1] = *p; | |
5469 *p = c; | |
5470 } | |
5471 | |
5472 /* Rotate three bytes right: "123" -> "312". We change "fword" | |
5473 * here, it's changed back afterwards at STATE_UNROT3R. */ | |
5474 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) | |
5475 { | |
5476 go_deeper(stack, depth, SCORE_SWAP3); | |
5477 #ifdef DEBUG_TRIEWALK | |
5478 p = fword + sp->ts_fidx; | |
5479 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c", | |
5480 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5481 p[0], p[1], p[2]); | |
5482 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5483 PROF_STORE(sp->ts_state) |
625 | 5484 sp->ts_state = STATE_UNROT3R; |
5485 ++depth; | |
330 | 5486 p = fword + sp->ts_fidx; |
5487 if (has_mbyte) | |
5488 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5489 n = MB_CPTR2LEN(p); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5490 n += MB_CPTR2LEN(p + n); |
330 | 5491 c = mb_ptr2char(p + n); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5492 tl = MB_CPTR2LEN(p + n); |
330 | 5493 mch_memmove(p + tl, p, n); |
5494 mb_char2bytes(c, p); | |
625 | 5495 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
330 | 5496 } |
5497 else | |
5498 { | |
5499 c = p[2]; | |
5500 p[2] = p[1]; | |
5501 p[1] = *p; | |
5502 *p = c; | |
625 | 5503 stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
5504 } | |
5505 } | |
5506 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5507 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5508 PROF_STORE(sp->ts_state) |
625 | 5509 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
|
5510 } |
625 | 5511 break; |
5512 | |
5513 case STATE_UNROT3R: | |
5514 /* Undo ROT3R: "312" -> "123" */ | |
5515 p = fword + sp->ts_fidx; | |
5516 if (has_mbyte) | |
5517 { | |
5518 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
|
5519 tl = MB_PTR2LEN(p); |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5520 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
|
5521 n += MB_PTR2LEN(p + tl + n); |
625 | 5522 mch_memmove(p, p + tl, n); |
5523 mb_char2bytes(c, p + n); | |
5524 } | |
5525 else | |
5526 { | |
5527 c = *p; | |
5528 *p = p[1]; | |
5529 p[1] = p[2]; | |
5530 p[2] = c; | |
5531 } | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5532 /* FALLTHROUGH */ |
625 | 5533 |
5534 case STATE_REP_INI: | |
5535 /* Check if matching with REP items from the .aff file would work. | |
5536 * Quickly skip if: | |
5537 * - there are no REP items and we are not in the soundfold trie | |
5538 * - the score is going to be too high anyway | |
5539 * - already applied a REP item or swapped here */ | |
5540 if ((lp->lp_replang == NULL && !soundfold) | |
5541 || sp->ts_score + SCORE_REP >= su->su_maxscore | |
5542 || sp->ts_fidx < sp->ts_fidxtry) | |
5543 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5544 PROF_STORE(sp->ts_state) |
625 | 5545 sp->ts_state = STATE_FINAL; |
323 | 5546 break; |
625 | 5547 } |
5548 | |
5549 /* Use the first byte to quickly find the first entry that may | |
5550 * match. If the index is -1 there is none. */ | |
5551 if (soundfold) | |
5552 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]]; | |
5553 else | |
5554 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]]; | |
5555 | |
5556 if (sp->ts_curi < 0) | |
5557 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5558 PROF_STORE(sp->ts_state) |
625 | 5559 sp->ts_state = STATE_FINAL; |
5560 break; | |
5561 } | |
5562 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5563 PROF_STORE(sp->ts_state) |
625 | 5564 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
|
5565 /* FALLTHROUGH */ |
625 | 5566 |
5567 case STATE_REP: | |
5568 /* Try matching with REP items from the .aff file. For each match | |
5569 * replace the characters and check if the resulting word is | |
5570 * valid. */ | |
5571 p = fword + sp->ts_fidx; | |
5572 | |
5573 if (soundfold) | |
5574 gap = &slang->sl_repsal; | |
5575 else | |
503 | 5576 gap = &lp->lp_replang->sl_rep; |
625 | 5577 while (sp->ts_curi < gap->ga_len) |
5578 { | |
5579 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; | |
5580 if (*ftp->ft_from != *p) | |
5581 { | |
5582 /* past possible matching entries */ | |
5583 sp->ts_curi = gap->ga_len; | |
323 | 5584 break; |
5585 } | |
625 | 5586 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
5587 && TRY_DEEPER(su, stack, depth, SCORE_REP)) | |
5588 { | |
5589 go_deeper(stack, depth, SCORE_REP); | |
5590 #ifdef DEBUG_TRIEWALK | |
5591 sprintf(changename[depth], "%.*s-%s: replace %s with %s", | |
5592 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5593 ftp->ft_from, ftp->ft_to); | |
5594 #endif | |
5595 /* Need to undo this afterwards. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5596 PROF_STORE(sp->ts_state) |
625 | 5597 sp->ts_state = STATE_REP_UNDO; |
5598 | |
5599 /* Change the "from" to the "to" string. */ | |
5600 ++depth; | |
835 | 5601 fl = (int)STRLEN(ftp->ft_from); |
5602 tl = (int)STRLEN(ftp->ft_to); | |
625 | 5603 if (fl != tl) |
323 | 5604 { |
1619 | 5605 STRMOVE(p + tl, p + fl); |
625 | 5606 repextra += tl - fl; |
323 | 5607 } |
625 | 5608 mch_memmove(p, ftp->ft_to, tl); |
5609 stack[depth].ts_fidxtry = sp->ts_fidx + tl; | |
5610 stack[depth].ts_tcharlen = 0; | |
5611 break; | |
5612 } | |
5613 } | |
5614 | |
5615 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
|
5616 { |
625 | 5617 /* No (more) matches. */ |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5618 PROF_STORE(sp->ts_state) |
625 | 5619 sp->ts_state = STATE_FINAL; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5620 } |
625 | 5621 |
5622 break; | |
5623 | |
5624 case STATE_REP_UNDO: | |
5625 /* Undo a REP replacement and continue with the next one. */ | |
5626 if (soundfold) | |
5627 gap = &slang->sl_repsal; | |
5628 else | |
5629 gap = &lp->lp_replang->sl_rep; | |
5630 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1; | |
835 | 5631 fl = (int)STRLEN(ftp->ft_from); |
5632 tl = (int)STRLEN(ftp->ft_to); | |
625 | 5633 p = fword + sp->ts_fidx; |
5634 if (fl != tl) | |
5635 { | |
1619 | 5636 STRMOVE(p + fl, p + tl); |
625 | 5637 repextra -= tl - fl; |
5638 } | |
5639 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
|
5640 PROF_STORE(sp->ts_state) |
625 | 5641 sp->ts_state = STATE_REP; |
5642 break; | |
5643 | |
5644 default: | |
5645 /* Did all possible states at this level, go up one level. */ | |
5646 --depth; | |
5647 | |
5648 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE) | |
5649 { | |
5650 /* Continue in or go back to the prefix tree. */ | |
5651 byts = pbyts; | |
5652 idxs = pidxs; | |
5653 } | |
5654 | |
5655 /* Don't check for CTRL-C too often, it takes time. */ | |
5656 if (--breakcheckcount == 0) | |
5657 { | |
5658 ui_breakcheck(); | |
5659 breakcheckcount = 1000; | |
5660 } | |
5661 } | |
5662 } | |
5663 } | |
5664 | |
5665 | |
5666 /* | |
5667 * Go one level deeper in the tree. | |
5668 */ | |
5669 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5670 go_deeper(trystate_T *stack, int depth, int score_add) |
323 | 5671 { |
330 | 5672 stack[depth + 1] = stack[depth]; |
323 | 5673 stack[depth + 1].ts_state = STATE_START; |
625 | 5674 stack[depth + 1].ts_score = stack[depth].ts_score + score_add; |
323 | 5675 stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
485 | 5676 stack[depth + 1].ts_flags = 0; |
323 | 5677 } |
5678 | |
455 | 5679 /* |
5680 * Case-folding may change the number of bytes: Count nr of chars in | |
5681 * fword[flen] and return the byte length of that many chars in "word". | |
5682 */ | |
5683 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5684 nofold_len(char_u *fword, int flen, char_u *word) |
455 | 5685 { |
5686 char_u *p; | |
5687 int i = 0; | |
5688 | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5689 for (p = fword; p < fword + flen; MB_PTR_ADV(p)) |
455 | 5690 ++i; |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5691 for (p = word; i > 0; MB_PTR_ADV(p)) |
455 | 5692 --i; |
5693 return (int)(p - word); | |
5694 } | |
5695 | |
323 | 5696 /* |
5697 * "fword" is a good word with case folded. Find the matching keep-case | |
5698 * words and put it in "kword". | |
5699 * Theoretically there could be several keep-case words that result in the | |
5700 * same case-folded word, but we only find one... | |
5701 */ | |
5702 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5703 find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) |
323 | 5704 { |
5705 char_u uword[MAXWLEN]; /* "fword" in upper-case */ | |
5706 int depth; | |
324 | 5707 idx_T tryidx; |
323 | 5708 |
5709 /* The following arrays are used at each depth in the tree. */ | |
324 | 5710 idx_T arridx[MAXWLEN]; |
323 | 5711 int round[MAXWLEN]; |
5712 int fwordidx[MAXWLEN]; | |
5713 int uwordidx[MAXWLEN]; | |
5714 int kwordlen[MAXWLEN]; | |
5715 | |
5716 int flen, ulen; | |
5717 int l; | |
5718 int len; | |
5719 int c; | |
324 | 5720 idx_T lo, hi, m; |
323 | 5721 char_u *p; |
5722 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ | |
324 | 5723 idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
323 | 5724 |
5725 if (byts == NULL) | |
5726 { | |
5727 /* array is empty: "cannot happen" */ | |
5728 *kword = NUL; | |
5729 return; | |
5730 } | |
5731 | |
5732 /* Make an all-cap version of "fword". */ | |
5733 allcap_copy(fword, uword); | |
5734 | |
5735 /* | |
5736 * Each character needs to be tried both case-folded and upper-case. | |
5737 * All this gets very complicated if we keep in mind that changing case | |
5738 * may change the byte length of a multi-byte character... | |
5739 */ | |
5740 depth = 0; | |
5741 arridx[0] = 0; | |
5742 round[0] = 0; | |
5743 fwordidx[0] = 0; | |
5744 uwordidx[0] = 0; | |
5745 kwordlen[0] = 0; | |
5746 while (depth >= 0) | |
5747 { | |
5748 if (fword[fwordidx[depth]] == NUL) | |
5749 { | |
5750 /* We are at the end of "fword". If the tree allows a word to end | |
5751 * here we have found a match. */ | |
5752 if (byts[arridx[depth] + 1] == 0) | |
5753 { | |
5754 kword[kwordlen[depth]] = NUL; | |
5755 return; | |
5756 } | |
5757 | |
5758 /* kword is getting too long, continue one level up */ | |
5759 --depth; | |
5760 } | |
5761 else if (++round[depth] > 2) | |
5762 { | |
5763 /* tried both fold-case and upper-case character, continue one | |
5764 * level up */ | |
5765 --depth; | |
5766 } | |
5767 else | |
5768 { | |
5769 /* | |
5770 * round[depth] == 1: Try using the folded-case character. | |
5771 * round[depth] == 2: Try using the upper-case character. | |
5772 */ | |
5773 if (has_mbyte) | |
5774 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5775 flen = MB_CPTR2LEN(fword + fwordidx[depth]); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5776 ulen = MB_CPTR2LEN(uword + uwordidx[depth]); |
323 | 5777 } |
5778 else | |
5779 ulen = flen = 1; | |
5780 if (round[depth] == 1) | |
5781 { | |
5782 p = fword + fwordidx[depth]; | |
5783 l = flen; | |
5784 } | |
5785 else | |
5786 { | |
5787 p = uword + uwordidx[depth]; | |
5788 l = ulen; | |
5789 } | |
5790 | |
5791 for (tryidx = arridx[depth]; l > 0; --l) | |
5792 { | |
5793 /* Perform a binary search in the list of accepted bytes. */ | |
5794 len = byts[tryidx++]; | |
5795 c = *p++; | |
5796 lo = tryidx; | |
5797 hi = tryidx + len - 1; | |
5798 while (lo < hi) | |
5799 { | |
5800 m = (lo + hi) / 2; | |
5801 if (byts[m] > c) | |
5802 hi = m - 1; | |
5803 else if (byts[m] < c) | |
5804 lo = m + 1; | |
5805 else | |
5806 { | |
5807 lo = hi = m; | |
5808 break; | |
5809 } | |
5810 } | |
5811 | |
5812 /* Stop if there is no matching byte. */ | |
5813 if (hi < lo || byts[lo] != c) | |
5814 break; | |
5815 | |
5816 /* Continue at the child (if there is one). */ | |
5817 tryidx = idxs[lo]; | |
5818 } | |
5819 | |
5820 if (l == 0) | |
5821 { | |
5822 /* | |
5823 * Found the matching char. Copy it to "kword" and go a | |
5824 * level deeper. | |
5825 */ | |
5826 if (round[depth] == 1) | |
5827 { | |
5828 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], | |
5829 flen); | |
5830 kwordlen[depth + 1] = kwordlen[depth] + flen; | |
5831 } | |
5832 else | |
5833 { | |
5834 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], | |
5835 ulen); | |
5836 kwordlen[depth + 1] = kwordlen[depth] + ulen; | |
5837 } | |
5838 fwordidx[depth + 1] = fwordidx[depth] + flen; | |
5839 uwordidx[depth + 1] = uwordidx[depth] + ulen; | |
5840 | |
5841 ++depth; | |
5842 arridx[depth] = tryidx; | |
5843 round[depth] = 0; | |
5844 } | |
5845 } | |
5846 } | |
5847 | |
5848 /* Didn't find it: "cannot happen". */ | |
5849 *kword = NUL; | |
5850 } | |
5851 | |
5852 /* | |
344 | 5853 * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
5854 * su->su_sga. | |
5855 */ | |
5856 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5857 score_comp_sal(suginfo_T *su) |
344 | 5858 { |
5859 langp_T *lp; | |
5860 char_u badsound[MAXWLEN]; | |
5861 int i; | |
5862 suggest_T *stp; | |
5863 suggest_T *sstp; | |
5864 int score; | |
500 | 5865 int lpi; |
344 | 5866 |
5867 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) | |
5868 return; | |
5869 | |
5870 /* 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
|
5871 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
|
5872 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5873 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
344 | 5874 if (lp->lp_slang->sl_sal.ga_len > 0) |
5875 { | |
5876 /* soundfold the bad word */ | |
375 | 5877 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
344 | 5878 |
5879 for (i = 0; i < su->su_ga.ga_len; ++i) | |
5880 { | |
5881 stp = &SUG(su->su_ga, i); | |
5882 | |
351 | 5883 /* Case-fold the suggested word, sound-fold it and compute the |
5884 * sound-a-like score. */ | |
5885 score = stp_sal_score(stp, su, lp->lp_slang, badsound); | |
344 | 5886 if (score < SCORE_MAXMAX) |
5887 { | |
5888 /* Add the suggestion. */ | |
5889 sstp = &SUG(su->su_sga, su->su_sga.ga_len); | |
5890 sstp->st_word = vim_strsave(stp->st_word); | |
5891 if (sstp->st_word != NULL) | |
5892 { | |
625 | 5893 sstp->st_wordlen = stp->st_wordlen; |
344 | 5894 sstp->st_score = score; |
5895 sstp->st_altscore = 0; | |
5896 sstp->st_orglen = stp->st_orglen; | |
5897 ++su->su_sga.ga_len; | |
5898 } | |
5899 } | |
5900 } | |
5901 break; | |
5902 } | |
500 | 5903 } |
344 | 5904 } |
5905 | |
5906 /* | |
5907 * Combine the list of suggestions in su->su_ga and su->su_sga. | |
4352 | 5908 * They are entwined. |
344 | 5909 */ |
5910 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5911 score_combine(suginfo_T *su) |
344 | 5912 { |
5913 int i; | |
5914 int j; | |
5915 garray_T ga; | |
5916 garray_T *gap; | |
5917 langp_T *lp; | |
5918 suggest_T *stp; | |
5919 char_u *p; | |
5920 char_u badsound[MAXWLEN]; | |
5921 int round; | |
500 | 5922 int lpi; |
625 | 5923 slang_T *slang = NULL; |
344 | 5924 |
5925 /* 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
|
5926 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
|
5927 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5928 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
344 | 5929 if (lp->lp_slang->sl_sal.ga_len > 0) |
5930 { | |
5931 /* soundfold the bad word */ | |
625 | 5932 slang = lp->lp_slang; |
5933 spell_soundfold(slang, su->su_fbadword, TRUE, badsound); | |
344 | 5934 |
5935 for (i = 0; i < su->su_ga.ga_len; ++i) | |
5936 { | |
5937 stp = &SUG(su->su_ga, i); | |
625 | 5938 stp->st_altscore = stp_sal_score(stp, su, slang, badsound); |
344 | 5939 if (stp->st_altscore == SCORE_MAXMAX) |
5940 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; | |
5941 else | |
5942 stp->st_score = (stp->st_score * 3 | |
5943 + stp->st_altscore) / 4; | |
5944 stp->st_salscore = FALSE; | |
5945 } | |
5946 break; | |
5947 } | |
5948 } | |
5949 | |
844 | 5950 if (slang == NULL) /* Using "double" without sound folding. */ |
5951 { | |
5952 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, | |
5953 su->su_maxcount); | |
625 | 5954 return; |
844 | 5955 } |
625 | 5956 |
344 | 5957 /* Add the alternate score to su_sga. */ |
5958 for (i = 0; i < su->su_sga.ga_len; ++i) | |
5959 { | |
5960 stp = &SUG(su->su_sga, i); | |
625 | 5961 stp->st_altscore = spell_edit_score(slang, |
5962 su->su_badword, stp->st_word); | |
344 | 5963 if (stp->st_score == SCORE_MAXMAX) |
5964 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; | |
5965 else | |
5966 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; | |
5967 stp->st_salscore = TRUE; | |
5968 } | |
5969 | |
625 | 5970 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount" |
5971 * for both lists. */ | |
5972 check_suggestions(su, &su->su_ga); | |
344 | 5973 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
625 | 5974 check_suggestions(su, &su->su_sga); |
344 | 5975 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
5976 | |
5977 ga_init2(&ga, (int)sizeof(suginfo_T), 1); | |
5978 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) | |
5979 return; | |
5980 | |
5981 stp = &SUG(ga, 0); | |
5982 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) | |
5983 { | |
5984 /* round 1: get a suggestion from su_ga | |
5985 * round 2: get a suggestion from su_sga */ | |
5986 for (round = 1; round <= 2; ++round) | |
5987 { | |
5988 gap = round == 1 ? &su->su_ga : &su->su_sga; | |
5989 if (i < gap->ga_len) | |
5990 { | |
5991 /* Don't add a word if it's already there. */ | |
5992 p = SUG(*gap, i).st_word; | |
5993 for (j = 0; j < ga.ga_len; ++j) | |
5994 if (STRCMP(stp[j].st_word, p) == 0) | |
5995 break; | |
5996 if (j == ga.ga_len) | |
5997 stp[ga.ga_len++] = SUG(*gap, i); | |
5998 else | |
5999 vim_free(p); | |
6000 } | |
6001 } | |
6002 } | |
6003 | |
6004 ga_clear(&su->su_ga); | |
6005 ga_clear(&su->su_sga); | |
6006 | |
6007 /* Truncate the list to the number of suggestions that will be displayed. */ | |
6008 if (ga.ga_len > su->su_maxcount) | |
6009 { | |
6010 for (i = su->su_maxcount; i < ga.ga_len; ++i) | |
6011 vim_free(stp[i].st_word); | |
6012 ga.ga_len = su->su_maxcount; | |
6013 } | |
6014 | |
6015 su->su_ga = ga; | |
6016 } | |
6017 | |
6018 /* | |
351 | 6019 * For the goodword in "stp" compute the soundalike score compared to the |
6020 * badword. | |
6021 */ | |
6022 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6023 stp_sal_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6024 suggest_T *stp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6025 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6026 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6027 char_u *badsound) /* sound-folded badword */ |
351 | 6028 { |
6029 char_u *p; | |
534 | 6030 char_u *pbad; |
6031 char_u *pgood; | |
351 | 6032 char_u badsound2[MAXWLEN]; |
6033 char_u fword[MAXWLEN]; | |
6034 char_u goodsound[MAXWLEN]; | |
534 | 6035 char_u goodword[MAXWLEN]; |
6036 int lendiff; | |
6037 | |
6038 lendiff = (int)(su->su_badlen - stp->st_orglen); | |
6039 if (lendiff >= 0) | |
6040 pbad = badsound; | |
351 | 6041 else |
6042 { | |
6043 /* soundfold the bad word with more characters following */ | |
6044 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); | |
6045 | |
6046 /* When joining two words the sound often changes a lot. E.g., "t he" | |
6047 * sounds like "t h" while "the" sounds like "@". Avoid that by | |
6048 * removing the space. Don't do it when the good word also contains a | |
6049 * space. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6050 if (VIM_ISWHITE(su->su_badptr[su->su_badlen]) |
351 | 6051 && *skiptowhite(stp->st_word) == NUL) |
6052 for (p = fword; *(p = skiptowhite(p)) != NUL; ) | |
1619 | 6053 STRMOVE(p, p + 1); |
351 | 6054 |
375 | 6055 spell_soundfold(slang, fword, TRUE, badsound2); |
534 | 6056 pbad = badsound2; |
6057 } | |
6058 | |
2768 | 6059 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN) |
534 | 6060 { |
6061 /* Add part of the bad word to the good word, so that we soundfold | |
6062 * what replaces the bad word. */ | |
6063 STRCPY(goodword, stp->st_word); | |
625 | 6064 vim_strncpy(goodword + stp->st_wordlen, |
6065 su->su_badptr + su->su_badlen - lendiff, lendiff); | |
534 | 6066 pgood = goodword; |
6067 } | |
6068 else | |
6069 pgood = stp->st_word; | |
351 | 6070 |
375 | 6071 /* Sound-fold the word and compute the score for the difference. */ |
534 | 6072 spell_soundfold(slang, pgood, FALSE, goodsound); |
6073 | |
6074 return soundalike_score(goodsound, pbad); | |
351 | 6075 } |
6076 | |
625 | 6077 /* structure used to store soundfolded words that add_sound_suggest() has |
6078 * handled already. */ | |
6079 typedef struct | |
6080 { | |
6081 short sft_score; /* lowest score used */ | |
6082 char_u sft_word[1]; /* soundfolded word, actually longer */ | |
6083 } sftword_T; | |
6084 | |
6085 static sftword_T dumsft; | |
6086 #define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft))) | |
6087 #define HI2SFT(hi) HIKEY2SFT((hi)->hi_key) | |
6088 | |
6089 /* | |
6090 * Prepare for calling suggest_try_soundalike(). | |
6091 */ | |
6092 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6093 suggest_try_soundalike_prep(void) |
625 | 6094 { |
6095 langp_T *lp; | |
6096 int lpi; | |
6097 slang_T *slang; | |
6098 | |
6099 /* Do this for all languages that support sound folding and for which a | |
6100 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6101 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
|
6102 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6103 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
625 | 6104 slang = lp->lp_slang; |
6105 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) | |
6106 /* prepare the hashtable used by add_sound_suggest() */ | |
6107 hash_init(&slang->sl_sounddone); | |
6108 } | |
6109 } | |
6110 | |
351 | 6111 /* |
323 | 6112 * Find suggestions by comparing the word in a sound-a-like form. |
503 | 6113 * Note: This doesn't support postponed prefixes. |
323 | 6114 */ |
6115 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6116 suggest_try_soundalike(suginfo_T *su) |
323 | 6117 { |
6118 char_u salword[MAXWLEN]; | |
6119 langp_T *lp; | |
500 | 6120 int lpi; |
503 | 6121 slang_T *slang; |
344 | 6122 |
625 | 6123 /* Do this for all languages that support sound folding and for which a |
6124 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6125 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
|
6126 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6127 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
625 | 6128 slang = lp->lp_slang; |
6129 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) | |
6130 { | |
6131 /* soundfold the bad word */ | |
6132 spell_soundfold(slang, su->su_fbadword, TRUE, salword); | |
6133 | |
6134 /* try all kinds of inserts/deletes/swaps/etc. */ | |
6135 /* TODO: also soundfold the next words, so that we can try joining | |
6136 * and splitting */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6137 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6138 prof_init(); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6139 #endif |
625 | 6140 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
|
6141 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6142 prof_report("soundalike"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6143 #endif |
625 | 6144 } |
6145 } | |
6146 } | |
6147 | |
6148 /* | |
6149 * Finish up after calling suggest_try_soundalike(). | |
6150 */ | |
6151 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6152 suggest_try_soundalike_finish(void) |
625 | 6153 { |
6154 langp_T *lp; | |
6155 int lpi; | |
6156 slang_T *slang; | |
6157 int todo; | |
6158 hashitem_T *hi; | |
6159 | |
6160 /* Do this for all languages that support sound folding and for which a | |
6161 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6162 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
|
6163 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6164 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
503 | 6165 slang = lp->lp_slang; |
625 | 6166 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) |
6167 { | |
6168 /* Free the info about handled words. */ | |
835 | 6169 todo = (int)slang->sl_sounddone.ht_used; |
625 | 6170 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi) |
6171 if (!HASHITEM_EMPTY(hi)) | |
6172 { | |
6173 vim_free(HI2SFT(hi)); | |
6174 --todo; | |
6175 } | |
1087 | 6176 |
6177 /* Clear the hashtable, it may also be used by another region. */ | |
625 | 6178 hash_clear(&slang->sl_sounddone); |
1087 | 6179 hash_init(&slang->sl_sounddone); |
625 | 6180 } |
6181 } | |
6182 } | |
6183 | |
6184 /* | |
6185 * A match with a soundfolded word is found. Add the good word(s) that | |
6186 * produce this soundfolded word. | |
6187 */ | |
6188 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6189 add_sound_suggest( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6190 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6191 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6192 int score, /* soundfold score */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6193 langp_T *lp) |
625 | 6194 { |
6195 slang_T *slang = lp->lp_slang; /* language for sound folding */ | |
6196 int sfwordnr; | |
6197 char_u *nrline; | |
6198 int orgnr; | |
6199 char_u theword[MAXWLEN]; | |
6200 int i; | |
6201 int wlen; | |
6202 char_u *byts; | |
6203 idx_T *idxs; | |
6204 int n; | |
6205 int wordcount; | |
6206 int wc; | |
6207 int goodscore; | |
6208 hash_T hash; | |
6209 hashitem_T *hi; | |
6210 sftword_T *sft; | |
6211 int bc, gc; | |
6212 int limit; | |
6213 | |
6214 /* | |
6215 * It's very well possible that the same soundfold word is found several | |
6216 * times with different scores. Since the following is quite slow only do | |
6217 * the words that have a better score than before. Use a hashtable to | |
6218 * remember the words that have been done. | |
6219 */ | |
6220 hash = hash_hash(goodword); | |
6221 hi = hash_lookup(&slang->sl_sounddone, goodword, hash); | |
6222 if (HASHITEM_EMPTY(hi)) | |
6223 { | |
844 | 6224 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T) |
6225 + STRLEN(goodword))); | |
625 | 6226 if (sft != NULL) |
6227 { | |
6228 sft->sft_score = score; | |
6229 STRCPY(sft->sft_word, goodword); | |
6230 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash); | |
6231 } | |
6232 } | |
6233 else | |
6234 { | |
6235 sft = HI2SFT(hi); | |
6236 if (score >= sft->sft_score) | |
6237 return; | |
6238 sft->sft_score = score; | |
6239 } | |
6240 | |
6241 /* | |
6242 * Find the word nr in the soundfold tree. | |
6243 */ | |
6244 sfwordnr = soundfold_find(slang, goodword); | |
6245 if (sfwordnr < 0) | |
6246 { | |
10359
66f1b5bf3fa6
commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents:
10277
diff
changeset
|
6247 internal_error("add_sound_suggest()"); |
625 | 6248 return; |
6249 } | |
6250 | |
6251 /* | |
6252 * go over the list of good words that produce this soundfold word | |
6253 */ | |
6254 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE); | |
6255 orgnr = 0; | |
6256 while (*nrline != NUL) | |
6257 { | |
6258 /* The wordnr was stored in a minimal nr of bytes as an offset to the | |
6259 * previous wordnr. */ | |
6260 orgnr += bytes2offset(&nrline); | |
6261 | |
6262 byts = slang->sl_fbyts; | |
6263 idxs = slang->sl_fidxs; | |
6264 | |
6265 /* Lookup the word "orgnr" one of the two tries. */ | |
6266 n = 0; | |
6267 wordcount = 0; | |
5506 | 6268 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen) |
625 | 6269 { |
6270 i = 1; | |
6271 if (wordcount == orgnr && byts[n + 1] == NUL) | |
6272 break; /* found end of word */ | |
6273 | |
6274 if (byts[n + 1] == NUL) | |
6275 ++wordcount; | |
6276 | |
6277 /* skip over the NUL bytes */ | |
6278 for ( ; byts[n + i] == NUL; ++i) | |
6279 if (i > byts[n]) /* safety check */ | |
6280 { | |
6281 STRCPY(theword + wlen, "BAD"); | |
5506 | 6282 wlen += 3; |
625 | 6283 goto badword; |
6284 } | |
6285 | |
6286 /* One of the siblings must have the word. */ | |
6287 for ( ; i < byts[n]; ++i) | |
6288 { | |
6289 wc = idxs[idxs[n + i]]; /* nr of words under this byte */ | |
6290 if (wordcount + wc > orgnr) | |
6291 break; | |
6292 wordcount += wc; | |
6293 } | |
6294 | |
5506 | 6295 theword[wlen] = byts[n + i]; |
625 | 6296 n = idxs[n + i]; |
6297 } | |
6298 badword: | |
6299 theword[wlen] = NUL; | |
6300 | |
6301 /* Go over the possible flags and regions. */ | |
6302 for (; i <= byts[n] && byts[n + i] == NUL; ++i) | |
6303 { | |
6304 char_u cword[MAXWLEN]; | |
6305 char_u *p; | |
6306 int flags = (int)idxs[n + i]; | |
6307 | |
698 | 6308 /* Skip words with the NOSUGGEST flag */ |
6309 if (flags & WF_NOSUGGEST) | |
6310 continue; | |
6311 | |
625 | 6312 if (flags & WF_KEEPCAP) |
6313 { | |
6314 /* Must find the word in the keep-case tree. */ | |
6315 find_keepcap_word(slang, theword, cword); | |
6316 p = cword; | |
6317 } | |
6318 else | |
6319 { | |
6320 flags |= su->su_badflags; | |
6321 if ((flags & WF_CAPMASK) != 0) | |
6322 { | |
6323 /* Need to fix case according to "flags". */ | |
6324 make_case_word(theword, cword, flags); | |
6325 p = cword; | |
323 | 6326 } |
6327 else | |
625 | 6328 p = theword; |
6329 } | |
6330 | |
6331 /* Add the suggestion. */ | |
6332 if (sps_flags & SPS_DOUBLE) | |
6333 { | |
6334 /* Add the suggestion if the score isn't too bad. */ | |
6335 if (score <= su->su_maxscore) | |
6336 add_suggestion(su, &su->su_sga, p, su->su_badlen, | |
6337 score, 0, FALSE, slang, FALSE); | |
6338 } | |
6339 else | |
6340 { | |
6341 /* Add a penalty for words in another region. */ | |
6342 if ((flags & WF_REGION) | |
6343 && (((unsigned)flags >> 16) & lp->lp_region) == 0) | |
6344 goodscore = SCORE_REGION; | |
6345 else | |
6346 goodscore = 0; | |
6347 | |
6348 /* Add a small penalty for changing the first letter from | |
6349 * lower to upper case. Helps for "tath" -> "Kath", which is | |
4352 | 6350 * less common than "tath" -> "path". Don't do it when the |
625 | 6351 * letter is the same, that has already been counted. */ |
6352 gc = PTR2CHAR(p); | |
6353 if (SPELL_ISUPPER(gc)) | |
6354 { | |
6355 bc = PTR2CHAR(su->su_badword); | |
6356 if (!SPELL_ISUPPER(bc) | |
6357 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc)) | |
6358 goodscore += SCORE_ICASE / 2; | |
6359 } | |
6360 | |
6361 /* Compute the score for the good word. This only does letter | |
6362 * insert/delete/swap/replace. REP items are not considered, | |
6363 * which may make the score a bit higher. | |
6364 * Use a limit for the score to make it work faster. Use | |
6365 * MAXSCORE(), because RESCORE() will change the score. | |
6366 * If the limit is very high then the iterative method is | |
6367 * inefficient, using an array is quicker. */ | |
6368 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score); | |
6369 if (limit > SCORE_LIMITMAX) | |
6370 goodscore += spell_edit_score(slang, su->su_badword, p); | |
6371 else | |
6372 goodscore += spell_edit_score_limit(slang, su->su_badword, | |
6373 p, limit); | |
6374 | |
6375 /* When going over the limit don't bother to do the rest. */ | |
6376 if (goodscore < SCORE_MAXMAX) | |
6377 { | |
6378 /* Give a bonus to words seen before. */ | |
6379 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE); | |
6380 | |
6381 /* Add the suggestion if the score isn't too bad. */ | |
6382 goodscore = RESCORE(goodscore, score); | |
6383 if (goodscore <= su->su_sfmaxscore) | |
6384 add_suggestion(su, &su->su_ga, p, su->su_badlen, | |
6385 goodscore, score, TRUE, slang, TRUE); | |
6386 } | |
6387 } | |
6388 } | |
6389 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */ | |
6390 } | |
6391 } | |
6392 | |
6393 /* | |
6394 * Find word "word" in fold-case tree for "slang" and return the word number. | |
6395 */ | |
6396 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6397 soundfold_find(slang_T *slang, char_u *word) |
625 | 6398 { |
6399 idx_T arridx = 0; | |
6400 int len; | |
6401 int wlen = 0; | |
6402 int c; | |
6403 char_u *ptr = word; | |
6404 char_u *byts; | |
6405 idx_T *idxs; | |
6406 int wordnr = 0; | |
6407 | |
6408 byts = slang->sl_sbyts; | |
6409 idxs = slang->sl_sidxs; | |
6410 | |
6411 for (;;) | |
6412 { | |
6413 /* First byte is the number of possible bytes. */ | |
6414 len = byts[arridx++]; | |
6415 | |
6416 /* If the first possible byte is a zero the word could end here. | |
6417 * If the word ends we found the word. If not skip the NUL bytes. */ | |
6418 c = ptr[wlen]; | |
6419 if (byts[arridx] == NUL) | |
6420 { | |
6421 if (c == NUL) | |
6422 break; | |
6423 | |
6424 /* Skip over the zeros, there can be several. */ | |
6425 while (len > 0 && byts[arridx] == NUL) | |
6426 { | |
6427 ++arridx; | |
6428 --len; | |
6429 } | |
6430 if (len == 0) | |
6431 return -1; /* no children, word should have ended here */ | |
6432 ++wordnr; | |
6433 } | |
6434 | |
6435 /* If the word ends we didn't find it. */ | |
6436 if (c == NUL) | |
6437 return -1; | |
6438 | |
6439 /* Perform a binary search in the list of accepted bytes. */ | |
6440 if (c == TAB) /* <Tab> is handled like <Space> */ | |
6441 c = ' '; | |
6442 while (byts[arridx] < c) | |
6443 { | |
6444 /* The word count is in the first idxs[] entry of the child. */ | |
6445 wordnr += idxs[idxs[arridx]]; | |
6446 ++arridx; | |
6447 if (--len == 0) /* end of the bytes, didn't find it */ | |
6448 return -1; | |
6449 } | |
6450 if (byts[arridx] != c) /* didn't find the byte */ | |
6451 return -1; | |
6452 | |
6453 /* Continue at the child (if there is one). */ | |
6454 arridx = idxs[arridx]; | |
6455 ++wlen; | |
6456 | |
6457 /* One space in the good word may stand for several spaces in the | |
6458 * checked word. */ | |
6459 if (c == ' ') | |
6460 while (ptr[wlen] == ' ' || ptr[wlen] == TAB) | |
6461 ++wlen; | |
6462 } | |
6463 | |
6464 return wordnr; | |
323 | 6465 } |
6466 | |
6467 /* | |
324 | 6468 * Copy "fword" to "cword", fixing case according to "flags". |
323 | 6469 */ |
6470 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6471 make_case_word(char_u *fword, char_u *cword, int flags) |
323 | 6472 { |
6473 if (flags & WF_ALLCAP) | |
6474 /* Make it all upper-case */ | |
6475 allcap_copy(fword, cword); | |
6476 else if (flags & WF_ONECAP) | |
6477 /* Make the first letter upper-case */ | |
324 | 6478 onecap_copy(fword, cword, TRUE); |
323 | 6479 else |
6480 /* Use goodword as-is. */ | |
6481 STRCPY(cword, fword); | |
6482 } | |
6483 | |
330 | 6484 |
323 | 6485 /* |
6486 * Return TRUE if "c1" and "c2" are similar characters according to the MAP | |
6487 * lines in the .aff file. | |
6488 */ | |
6489 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6490 similar_chars(slang_T *slang, int c1, int c2) |
323 | 6491 { |
330 | 6492 int m1, m2; |
3549 | 6493 char_u buf[MB_MAXBYTES + 1]; |
330 | 6494 hashitem_T *hi; |
6495 | |
6496 if (c1 >= 256) | |
6497 { | |
6498 buf[mb_char2bytes(c1, buf)] = 0; | |
6499 hi = hash_find(&slang->sl_map_hash, buf); | |
6500 if (HASHITEM_EMPTY(hi)) | |
6501 m1 = 0; | |
6502 else | |
6503 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); | |
6504 } | |
6505 else | |
6506 m1 = slang->sl_map_array[c1]; | |
6507 if (m1 == 0) | |
323 | 6508 return FALSE; |
330 | 6509 |
6510 | |
6511 if (c2 >= 256) | |
6512 { | |
6513 buf[mb_char2bytes(c2, buf)] = 0; | |
6514 hi = hash_find(&slang->sl_map_hash, buf); | |
6515 if (HASHITEM_EMPTY(hi)) | |
6516 m2 = 0; | |
6517 else | |
6518 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); | |
6519 } | |
6520 else | |
6521 m2 = slang->sl_map_array[c2]; | |
6522 | |
6523 return m1 == m2; | |
323 | 6524 } |
6525 | |
6526 /* | |
6527 * Add a suggestion to the list of suggestions. | |
625 | 6528 * For a suggestion that is already in the list the lowest score is remembered. |
6529 */ | |
6530 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6531 add_suggestion( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6532 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6533 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
|
6534 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6535 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
|
6536 int score, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6537 int altscore, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6538 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
|
6539 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
|
6540 int maxsf) /* su_maxscore applies to soundfold score, |
625 | 6541 su_sfmaxscore to the total score. */ |
6542 { | |
6543 int goodlen; /* len of goodword changed */ | |
6544 int badlen; /* len of bad word changed */ | |
323 | 6545 suggest_T *stp; |
534 | 6546 suggest_T new_sug; |
323 | 6547 int i; |
534 | 6548 char_u *pgood, *pbad; |
532 | 6549 |
534 | 6550 /* Minimize "badlen" for consistency. Avoids that changing "the the" to |
6551 * "thee the" is added next to changing the first "the" the "thee". */ | |
6552 pgood = goodword + STRLEN(goodword); | |
625 | 6553 pbad = su->su_badptr + badlenarg; |
6554 for (;;) | |
6555 { | |
835 | 6556 goodlen = (int)(pgood - goodword); |
6557 badlen = (int)(pbad - su->su_badptr); | |
625 | 6558 if (goodlen <= 0 || badlen <= 0) |
6559 break; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6560 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
|
6561 MB_PTR_BACK(su->su_badptr, pbad); |
534 | 6562 if (has_mbyte) |
6563 { | |
6564 if (mb_ptr2char(pgood) != mb_ptr2char(pbad)) | |
6565 break; | |
346 | 6566 } |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
6567 else if (*pgood != *pbad) |
534 | 6568 break; |
625 | 6569 } |
6570 | |
534 | 6571 if (badlen == 0 && goodlen == 0) |
6572 /* goodword doesn't change anything; may happen for "the the" changing | |
6573 * the first "the" to itself. */ | |
6574 return; | |
346 | 6575 |
944 | 6576 if (gap->ga_len == 0) |
6577 i = -1; | |
6578 else | |
6579 { | |
6580 /* Check if the word is already there. Also check the length that is | |
6581 * being replaced "thes," -> "these" is a different suggestion from | |
6582 * "thes" -> "these". */ | |
6583 stp = &SUG(*gap, 0); | |
6584 for (i = gap->ga_len; --i >= 0; ++stp) | |
6585 if (stp->st_wordlen == goodlen | |
6586 && stp->st_orglen == badlen | |
6587 && STRNCMP(stp->st_word, goodword, goodlen) == 0) | |
6588 { | |
6589 /* | |
6590 * Found it. Remember the word with the lowest score. | |
6591 */ | |
6592 if (stp->st_slang == NULL) | |
6593 stp->st_slang = slang; | |
6594 | |
6595 new_sug.st_score = score; | |
6596 new_sug.st_altscore = altscore; | |
6597 new_sug.st_had_bonus = had_bonus; | |
6598 | |
6599 if (stp->st_had_bonus != had_bonus) | |
6600 { | |
6601 /* Only one of the two had the soundalike score computed. | |
6602 * Need to do that for the other one now, otherwise the | |
6603 * scores can't be compared. This happens because | |
6604 * suggest_try_change() doesn't compute the soundalike | |
6605 * word to keep it fast, while some special methods set | |
6606 * the soundalike score to zero. */ | |
6607 if (had_bonus) | |
6608 rescore_one(su, stp); | |
6609 else | |
6610 { | |
6611 new_sug.st_word = stp->st_word; | |
6612 new_sug.st_wordlen = stp->st_wordlen; | |
6613 new_sug.st_slang = stp->st_slang; | |
6614 new_sug.st_orglen = badlen; | |
6615 rescore_one(su, &new_sug); | |
6616 } | |
6617 } | |
6618 | |
6619 if (stp->st_score > new_sug.st_score) | |
6620 { | |
6621 stp->st_score = new_sug.st_score; | |
6622 stp->st_altscore = new_sug.st_altscore; | |
6623 stp->st_had_bonus = new_sug.st_had_bonus; | |
6624 } | |
6625 break; | |
6626 } | |
6627 } | |
625 | 6628 |
6629 if (i < 0 && ga_grow(gap, 1) == OK) | |
6630 { | |
6631 /* Add a suggestion. */ | |
6632 stp = &SUG(*gap, gap->ga_len); | |
6633 stp->st_word = vim_strnsave(goodword, goodlen); | |
6634 if (stp->st_word != NULL) | |
6635 { | |
6636 stp->st_wordlen = goodlen; | |
6637 stp->st_score = score; | |
6638 stp->st_altscore = altscore; | |
6639 stp->st_had_bonus = had_bonus; | |
6640 stp->st_orglen = badlen; | |
6641 stp->st_slang = slang; | |
6642 ++gap->ga_len; | |
6643 | |
6644 /* If we have too many suggestions now, sort the list and keep | |
6645 * the best suggestions. */ | |
6646 if (gap->ga_len > SUG_MAX_COUNT(su)) | |
6647 { | |
6648 if (maxsf) | |
6649 su->su_sfmaxscore = cleanup_suggestions(gap, | |
6650 su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); | |
6651 else | |
6652 su->su_maxscore = cleanup_suggestions(gap, | |
6653 su->su_maxscore, SUG_CLEAN_COUNT(su)); | |
6654 } | |
6655 } | |
6656 } | |
6657 } | |
6658 | |
6659 /* | |
6660 * Suggestions may in fact be flagged as errors. Esp. for banned words and | |
6661 * for split words, such as "the the". Remove these from the list here. | |
6662 */ | |
6663 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6664 check_suggestions( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6665 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6666 garray_T *gap) /* either su_ga or su_sga */ |
625 | 6667 { |
6668 suggest_T *stp; | |
6669 int i; | |
6670 char_u longword[MAXWLEN + 1]; | |
6671 int len; | |
6672 hlf_T attr; | |
6673 | |
6674 stp = &SUG(*gap, 0); | |
6675 for (i = gap->ga_len - 1; i >= 0; --i) | |
6676 { | |
6677 /* Need to append what follows to check for "the the". */ | |
2768 | 6678 vim_strncpy(longword, stp[i].st_word, MAXWLEN); |
625 | 6679 len = stp[i].st_wordlen; |
6680 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen, | |
6681 MAXWLEN - len); | |
6682 attr = HLF_COUNT; | |
6683 (void)spell_check(curwin, longword, &attr, NULL, FALSE); | |
6684 if (attr != HLF_COUNT) | |
6685 { | |
6686 /* Remove this entry. */ | |
6687 vim_free(stp[i].st_word); | |
6688 --gap->ga_len; | |
6689 if (i < gap->ga_len) | |
6690 mch_memmove(stp + i, stp + i + 1, | |
6691 sizeof(suggest_T) * (gap->ga_len - i)); | |
6692 } | |
6693 } | |
6694 } | |
6695 | |
323 | 6696 |
6697 /* | |
6698 * Add a word to be banned. | |
6699 */ | |
6700 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6701 add_banned( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6702 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6703 char_u *word) |
323 | 6704 { |
626 | 6705 char_u *s; |
323 | 6706 hash_T hash; |
6707 hashitem_T *hi; | |
6708 | |
625 | 6709 hash = hash_hash(word); |
6710 hi = hash_lookup(&su->su_banned, word, hash); | |
6711 if (HASHITEM_EMPTY(hi)) | |
6712 { | |
6713 s = vim_strsave(word); | |
6714 if (s != NULL) | |
323 | 6715 hash_add_item(&su->su_banned, hi, s, hash); |
625 | 6716 } |
323 | 6717 } |
6718 | |
324 | 6719 /* |
534 | 6720 * Recompute the score for all suggestions if sound-folding is possible. This |
6721 * is slow, thus only done for the final results. | |
324 | 6722 */ |
6723 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6724 rescore_suggestions(suginfo_T *su) |
324 | 6725 { |
6726 int i; | |
534 | 6727 |
6728 if (su->su_sallang != NULL) | |
503 | 6729 for (i = 0; i < su->su_ga.ga_len; ++i) |
534 | 6730 rescore_one(su, &SUG(su->su_ga, i)); |
6731 } | |
6732 | |
6733 /* | |
6734 * Recompute the score for one suggestion if sound-folding is possible. | |
6735 */ | |
6736 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6737 rescore_one(suginfo_T *su, suggest_T *stp) |
534 | 6738 { |
6739 slang_T *slang = stp->st_slang; | |
6740 char_u sal_badword[MAXWLEN]; | |
535 | 6741 char_u *p; |
534 | 6742 |
6743 /* Only rescore suggestions that have no sal score yet and do have a | |
6744 * language. */ | |
6745 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus) | |
6746 { | |
6747 if (slang == su->su_sallang) | |
535 | 6748 p = su->su_sal_badword; |
534 | 6749 else |
6750 { | |
6751 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword); | |
535 | 6752 p = sal_badword; |
6753 } | |
6754 | |
6755 stp->st_altscore = stp_sal_score(stp, su, slang, p); | |
534 | 6756 if (stp->st_altscore == SCORE_MAXMAX) |
6757 stp->st_altscore = SCORE_BIG; | |
6758 stp->st_score = RESCORE(stp->st_score, stp->st_altscore); | |
6759 stp->st_had_bonus = TRUE; | |
324 | 6760 } |
6761 } | |
6762 | |
323 | 6763 static int |
6764 #ifdef __BORLANDC__ | |
6765 _RTLENTRYF | |
6766 #endif | |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
6767 sug_compare(const void *s1, const void *s2); |
323 | 6768 |
6769 /* | |
6770 * Function given to qsort() to sort the suggestions on st_score. | |
529 | 6771 * First on "st_score", then "st_altscore" then alphabetically. |
323 | 6772 */ |
6773 static int | |
6774 #ifdef __BORLANDC__ | |
6775 _RTLENTRYF | |
6776 #endif | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6777 sug_compare(const void *s1, const void *s2) |
323 | 6778 { |
6779 suggest_T *p1 = (suggest_T *)s1; | |
6780 suggest_T *p2 = (suggest_T *)s2; | |
344 | 6781 int n = p1->st_score - p2->st_score; |
6782 | |
6783 if (n == 0) | |
529 | 6784 { |
6785 n = p1->st_altscore - p2->st_altscore; | |
6786 if (n == 0) | |
6787 n = STRICMP(p1->st_word, p2->st_word); | |
6788 } | |
344 | 6789 return n; |
323 | 6790 } |
6791 | |
6792 /* | |
6793 * Cleanup the suggestions: | |
6794 * - Sort on score. | |
6795 * - Remove words that won't be displayed. | |
344 | 6796 * Returns the maximum score in the list or "maxscore" unmodified. |
323 | 6797 */ |
344 | 6798 static int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6799 cleanup_suggestions( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6800 garray_T *gap, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6801 int maxscore, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6802 int keep) /* nr of suggestions to keep */ |
323 | 6803 { |
344 | 6804 suggest_T *stp = &SUG(*gap, 0); |
323 | 6805 int i; |
6806 | |
6807 /* Sort the list. */ | |
344 | 6808 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare); |
323 | 6809 |
6810 /* Truncate the list to the number of suggestions that will be displayed. */ | |
344 | 6811 if (gap->ga_len > keep) |
323 | 6812 { |
344 | 6813 for (i = keep; i < gap->ga_len; ++i) |
323 | 6814 vim_free(stp[i].st_word); |
344 | 6815 gap->ga_len = keep; |
6816 return stp[keep - 1].st_score; | |
323 | 6817 } |
344 | 6818 return maxscore; |
323 | 6819 } |
6820 | |
372 | 6821 #if defined(FEAT_EVAL) || defined(PROTO) |
6822 /* | |
6823 * Soundfold a string, for soundfold(). | |
6824 * Result is in allocated memory, NULL for an error. | |
6825 */ | |
6826 char_u * | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6827 eval_soundfold(char_u *word) |
372 | 6828 { |
6829 langp_T *lp; | |
6830 char_u sound[MAXWLEN]; | |
500 | 6831 int lpi; |
372 | 6832 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6833 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
372 | 6834 /* 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
|
6835 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
|
6836 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6837 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
372 | 6838 if (lp->lp_slang->sl_sal.ga_len > 0) |
6839 { | |
6840 /* soundfold the word */ | |
375 | 6841 spell_soundfold(lp->lp_slang, word, FALSE, sound); |
372 | 6842 return vim_strsave(sound); |
6843 } | |
500 | 6844 } |
372 | 6845 |
6846 /* No language with sound folding, return word as-is. */ | |
6847 return vim_strsave(word); | |
6848 } | |
6849 #endif | |
6850 | |
323 | 6851 /* |
6852 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". | |
485 | 6853 * |
6854 * There are many ways to turn a word into a sound-a-like representation. The | |
6855 * oldest is Soundex (1918!). A nice overview can be found in "Approximate | |
6856 * swedish name matching - survey and test of different algorithms" by Klas | |
6857 * Erikson. | |
6858 * | |
6859 * We support two methods: | |
6860 * 1. SOFOFROM/SOFOTO do a simple character mapping. | |
6861 * 2. SAL items define a more advanced sound-folding (and much slower). | |
323 | 6862 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
6863 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6864 spell_soundfold( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6865 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6866 char_u *inword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6867 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
|
6868 char_u *res) |
375 | 6869 { |
6870 char_u fword[MAXWLEN]; | |
6871 char_u *word; | |
6872 | |
6873 if (slang->sl_sofo) | |
6874 /* SOFOFROM and SOFOTO used */ | |
6875 spell_soundfold_sofo(slang, inword, res); | |
6876 else | |
6877 { | |
6878 /* SAL items used. Requires the word to be case-folded. */ | |
6879 if (folded) | |
6880 word = inword; | |
6881 else | |
6882 { | |
835 | 6883 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN); |
375 | 6884 word = fword; |
6885 } | |
6886 | |
6887 if (has_mbyte) | |
6888 spell_soundfold_wsal(slang, word, res); | |
6889 else | |
6890 spell_soundfold_sal(slang, word, res); | |
6891 } | |
6892 } | |
6893 | |
6894 /* | |
6895 * Perform sound folding of "inword" into "res" according to SOFOFROM and | |
6896 * SOFOTO lines. | |
6897 */ | |
6898 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6899 spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res) |
375 | 6900 { |
6901 char_u *s; | |
6902 int ri = 0; | |
6903 int c; | |
6904 | |
6905 if (has_mbyte) | |
6906 { | |
6907 int prevc = 0; | |
6908 int *ip; | |
6909 | |
6910 /* The sl_sal_first[] table contains the translation for chars up to | |
6911 * 255, sl_sal the rest. */ | |
6912 for (s = inword; *s != NUL; ) | |
6913 { | |
474 | 6914 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
|
6915 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) |
375 | 6916 c = ' '; |
6917 else if (c < 256) | |
6918 c = slang->sl_sal_first[c]; | |
6919 else | |
6920 { | |
6921 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; | |
6922 if (ip == NULL) /* empty list, can't match */ | |
6923 c = NUL; | |
6924 else | |
6925 for (;;) /* find "c" in the list */ | |
6926 { | |
6927 if (*ip == 0) /* not found */ | |
6928 { | |
6929 c = NUL; | |
6930 break; | |
6931 } | |
6932 if (*ip == c) /* match! */ | |
6933 { | |
6934 c = ip[1]; | |
6935 break; | |
6936 } | |
6937 ip += 2; | |
6938 } | |
6939 } | |
6940 | |
6941 if (c != NUL && c != prevc) | |
6942 { | |
6943 ri += mb_char2bytes(c, res + ri); | |
6944 if (ri + MB_MAXBYTES > MAXWLEN) | |
6945 break; | |
6946 prevc = c; | |
6947 } | |
6948 } | |
6949 } | |
6950 else | |
6951 { | |
6952 /* The sl_sal_first[] table contains the translation. */ | |
6953 for (s = inword; (c = *s) != NUL; ++s) | |
6954 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6955 if (VIM_ISWHITE(c)) |
375 | 6956 c = ' '; |
6957 else | |
6958 c = slang->sl_sal_first[c]; | |
6959 if (c != NUL && (ri == 0 || res[ri - 1] != c)) | |
6960 res[ri++] = c; | |
6961 } | |
6962 } | |
6963 | |
6964 res[ri] = NUL; | |
6965 } | |
6966 | |
6967 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6968 spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res) |
323 | 6969 { |
344 | 6970 salitem_T *smp; |
323 | 6971 char_u word[MAXWLEN]; |
375 | 6972 char_u *s = inword; |
323 | 6973 char_u *t; |
344 | 6974 char_u *pf; |
323 | 6975 int i, j, z; |
344 | 6976 int reslen; |
323 | 6977 int n, k = 0; |
6978 int z0; | |
6979 int k0; | |
6980 int n0; | |
6981 int c; | |
6982 int pri; | |
6983 int p0 = -333; | |
6984 int c0; | |
6985 | |
324 | 6986 /* Remove accents, if wanted. We actually remove all non-word characters. |
375 | 6987 * But keep white space. We need a copy, the word may be changed here. */ |
323 | 6988 if (slang->sl_rem_accents) |
6989 { | |
6990 t = word; | |
375 | 6991 while (*s != NUL) |
323 | 6992 { |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6993 if (VIM_ISWHITE(*s)) |
344 | 6994 { |
6995 *t++ = ' '; | |
6996 s = skipwhite(s); | |
6997 } | |
323 | 6998 else |
6999 { | |
5477 | 7000 if (spell_iswordp_nmw(s, curwin)) |
323 | 7001 *t++ = *s; |
7002 ++s; | |
7003 } | |
7004 } | |
7005 *t = NUL; | |
7006 } | |
7007 else | |
2768 | 7008 vim_strncpy(word, s, MAXWLEN - 1); |
323 | 7009 |
344 | 7010 smp = (salitem_T *)slang->sl_sal.ga_data; |
323 | 7011 |
7012 /* | |
7013 * This comes from Aspell phonet.cpp. Converted from C++ to C. | |
324 | 7014 * Changed to keep spaces. |
323 | 7015 */ |
344 | 7016 i = reslen = z = 0; |
323 | 7017 while ((c = word[i]) != NUL) |
7018 { | |
344 | 7019 /* Start with the first rule that has the character in the word. */ |
323 | 7020 n = slang->sl_sal_first[c]; |
7021 z0 = 0; | |
7022 | |
7023 if (n >= 0) | |
7024 { | |
7025 /* check all rules for the same letter */ | |
344 | 7026 for (; (s = smp[n].sm_lead)[0] == c; ++n) |
323 | 7027 { |
344 | 7028 /* Quickly skip entries that don't match the word. Most |
7029 * entries are less then three chars, optimize for that. */ | |
7030 k = smp[n].sm_leadlen; | |
7031 if (k > 1) | |
323 | 7032 { |
344 | 7033 if (word[i + 1] != s[1]) |
7034 continue; | |
7035 if (k > 2) | |
7036 { | |
7037 for (j = 2; j < k; ++j) | |
7038 if (word[i + j] != s[j]) | |
7039 break; | |
7040 if (j < k) | |
7041 continue; | |
7042 } | |
323 | 7043 } |
7044 | |
375 | 7045 if ((pf = smp[n].sm_oneof) != NULL) |
323 | 7046 { |
375 | 7047 /* Check for match with one of the chars in "sm_oneof". */ |
344 | 7048 while (*pf != NUL && *pf != word[i + k]) |
7049 ++pf; | |
7050 if (*pf == NUL) | |
7051 continue; | |
7052 ++k; | |
323 | 7053 } |
344 | 7054 s = smp[n].sm_rules; |
7055 pri = 5; /* default priority */ | |
323 | 7056 |
7057 p0 = *s; | |
7058 k0 = k; | |
7059 while (*s == '-' && k > 1) | |
7060 { | |
7061 k--; | |
7062 s++; | |
7063 } | |
7064 if (*s == '<') | |
7065 s++; | |
344 | 7066 if (VIM_ISDIGIT(*s)) |
323 | 7067 { |
7068 /* determine priority */ | |
7069 pri = *s - '0'; | |
7070 s++; | |
7071 } | |
7072 if (*s == '^' && *(s + 1) == '^') | |
7073 s++; | |
7074 | |
7075 if (*s == NUL | |
7076 || (*s == '^' | |
324 | 7077 && (i == 0 || !(word[i - 1] == ' ' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7078 || spell_iswordp(word + i - 1, curwin))) |
323 | 7079 && (*(s + 1) != '$' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7080 || (!spell_iswordp(word + i + k0, curwin)))) |
323 | 7081 || (*s == '$' && i > 0 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7082 && spell_iswordp(word + i - 1, curwin) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7083 && (!spell_iswordp(word + i + k0, curwin)))) |
323 | 7084 { |
7085 /* search for followup rules, if: */ | |
7086 /* followup and k > 1 and NO '-' in searchstring */ | |
7087 c0 = word[i + k - 1]; | |
7088 n0 = slang->sl_sal_first[c0]; | |
7089 | |
7090 if (slang->sl_followup && k > 1 && n0 >= 0 | |
344 | 7091 && p0 != '-' && word[i + k] != NUL) |
323 | 7092 { |
7093 /* test follow-up rule for "word[i + k]" */ | |
344 | 7094 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
323 | 7095 { |
344 | 7096 /* Quickly skip entries that don't match the word. |
7097 * */ | |
7098 k0 = smp[n0].sm_leadlen; | |
7099 if (k0 > 1) | |
323 | 7100 { |
344 | 7101 if (word[i + k] != s[1]) |
7102 continue; | |
7103 if (k0 > 2) | |
7104 { | |
7105 pf = word + i + k + 1; | |
7106 for (j = 2; j < k0; ++j) | |
7107 if (*pf++ != s[j]) | |
7108 break; | |
7109 if (j < k0) | |
7110 continue; | |
7111 } | |
323 | 7112 } |
344 | 7113 k0 += k - 1; |
7114 | |
375 | 7115 if ((pf = smp[n0].sm_oneof) != NULL) |
323 | 7116 { |
344 | 7117 /* Check for match with one of the chars in |
375 | 7118 * "sm_oneof". */ |
344 | 7119 while (*pf != NUL && *pf != word[i + k0]) |
7120 ++pf; | |
7121 if (*pf == NUL) | |
7122 continue; | |
7123 ++k0; | |
323 | 7124 } |
344 | 7125 |
7126 p0 = 5; | |
7127 s = smp[n0].sm_rules; | |
323 | 7128 while (*s == '-') |
7129 { | |
344 | 7130 /* "k0" gets NOT reduced because |
7131 * "if (k0 == k)" */ | |
323 | 7132 s++; |
7133 } | |
7134 if (*s == '<') | |
7135 s++; | |
344 | 7136 if (VIM_ISDIGIT(*s)) |
323 | 7137 { |
7138 p0 = *s - '0'; | |
7139 s++; | |
7140 } | |
7141 | |
7142 if (*s == NUL | |
7143 /* *s == '^' cuts */ | |
7144 || (*s == '$' | |
376 | 7145 && !spell_iswordp(word + i + k0, |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7146 curwin))) |
323 | 7147 { |
7148 if (k0 == k) | |
7149 /* this is just a piece of the string */ | |
7150 continue; | |
7151 | |
7152 if (p0 < pri) | |
7153 /* priority too low */ | |
7154 continue; | |
7155 /* rule fits; stop search */ | |
7156 break; | |
7157 } | |
7158 } | |
7159 | |
344 | 7160 if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
323 | 7161 continue; |
7162 } | |
7163 | |
7164 /* replace string */ | |
344 | 7165 s = smp[n].sm_to; |
389 | 7166 if (s == NULL) |
7167 s = (char_u *)""; | |
344 | 7168 pf = smp[n].sm_rules; |
7169 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; | |
323 | 7170 if (p0 == 1 && z == 0) |
7171 { | |
7172 /* rule with '<' is used */ | |
344 | 7173 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
7174 || res[reslen - 1] == *s)) | |
7175 reslen--; | |
323 | 7176 z0 = 1; |
7177 z = 1; | |
7178 k0 = 0; | |
372 | 7179 while (*s != NUL && word[i + k0] != NUL) |
323 | 7180 { |
7181 word[i + k0] = *s; | |
7182 k0++; | |
7183 s++; | |
7184 } | |
7185 if (k > k0) | |
1619 | 7186 STRMOVE(word + i + k0, word + i + k); |
323 | 7187 |
7188 /* new "actual letter" */ | |
7189 c = word[i]; | |
7190 } | |
7191 else | |
7192 { | |
7193 /* no '<' rule used */ | |
7194 i += k - 1; | |
7195 z = 0; | |
344 | 7196 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
323 | 7197 { |
344 | 7198 if (reslen == 0 || res[reslen - 1] != *s) |
372 | 7199 res[reslen++] = *s; |
323 | 7200 s++; |
7201 } | |
7202 /* new "actual letter" */ | |
7203 c = *s; | |
344 | 7204 if (strstr((char *)pf, "^^") != NULL) |
323 | 7205 { |
7206 if (c != NUL) | |
372 | 7207 res[reslen++] = c; |
1619 | 7208 STRMOVE(word, word + i + 1); |
323 | 7209 i = 0; |
7210 z0 = 1; | |
7211 } | |
7212 } | |
7213 break; | |
7214 } | |
7215 } | |
7216 } | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7217 else if (VIM_ISWHITE(c)) |
324 | 7218 { |
7219 c = ' '; | |
7220 k = 1; | |
7221 } | |
323 | 7222 |
7223 if (z0 == 0) | |
7224 { | |
344 | 7225 if (k && !p0 && reslen < MAXWLEN && c != NUL |
7226 && (!slang->sl_collapse || reslen == 0 | |
7227 || res[reslen - 1] != c)) | |
323 | 7228 /* condense only double letters */ |
372 | 7229 res[reslen++] = c; |
323 | 7230 |
7231 i++; | |
7232 z = 0; | |
7233 k = 0; | |
7234 } | |
7235 } | |
7236 | |
344 | 7237 res[reslen] = NUL; |
323 | 7238 } |
7239 | |
372 | 7240 /* |
7241 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". | |
7242 * Multi-byte version of spell_soundfold(). | |
7243 */ | |
7244 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7245 spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) |
372 | 7246 { |
375 | 7247 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
372 | 7248 int word[MAXWLEN]; |
7249 int wres[MAXWLEN]; | |
7250 int l; | |
7251 char_u *s; | |
7252 int *ws; | |
7253 char_u *t; | |
7254 int *pf; | |
7255 int i, j, z; | |
7256 int reslen; | |
7257 int n, k = 0; | |
7258 int z0; | |
7259 int k0; | |
7260 int n0; | |
7261 int c; | |
7262 int pri; | |
7263 int p0 = -333; | |
7264 int c0; | |
7265 int did_white = FALSE; | |
3520 | 7266 int wordlen; |
7267 | |
372 | 7268 |
7269 /* | |
7270 * Convert the multi-byte string to a wide-character string. | |
7271 * Remove accents, if wanted. We actually remove all non-word characters. | |
7272 * But keep white space. | |
7273 */ | |
3520 | 7274 wordlen = 0; |
372 | 7275 for (s = inword; *s != NUL; ) |
7276 { | |
7277 t = s; | |
474 | 7278 c = mb_cptr2char_adv(&s); |
372 | 7279 if (slang->sl_rem_accents) |
7280 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7281 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) |
372 | 7282 { |
7283 if (did_white) | |
7284 continue; | |
7285 c = ' '; | |
7286 did_white = TRUE; | |
7287 } | |
7288 else | |
7289 { | |
7290 did_white = FALSE; | |
5477 | 7291 if (!spell_iswordp_nmw(t, curwin)) |
372 | 7292 continue; |
7293 } | |
7294 } | |
3520 | 7295 word[wordlen++] = c; |
7296 } | |
7297 word[wordlen] = NUL; | |
372 | 7298 |
7299 /* | |
3520 | 7300 * This algorithm comes from Aspell phonet.cpp. |
372 | 7301 * Converted from C++ to C. Added support for multi-byte chars. |
7302 * Changed to keep spaces. | |
7303 */ | |
7304 i = reslen = z = 0; | |
7305 while ((c = word[i]) != NUL) | |
7306 { | |
7307 /* Start with the first rule that has the character in the word. */ | |
7308 n = slang->sl_sal_first[c & 0xff]; | |
7309 z0 = 0; | |
7310 | |
7311 if (n >= 0) | |
7312 { | |
2455
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7313 /* 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
|
7314 * 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
|
7315 * (c & 0xff) is NUL. */ |
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7316 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
|
7317 && ws[0] != NUL; ++n) |
372 | 7318 { |
7319 /* Quickly skip entries that don't match the word. Most | |
7320 * entries are less then three chars, optimize for that. */ | |
375 | 7321 if (c != ws[0]) |
7322 continue; | |
372 | 7323 k = smp[n].sm_leadlen; |
7324 if (k > 1) | |
7325 { | |
7326 if (word[i + 1] != ws[1]) | |
7327 continue; | |
7328 if (k > 2) | |
7329 { | |
7330 for (j = 2; j < k; ++j) | |
7331 if (word[i + j] != ws[j]) | |
7332 break; | |
7333 if (j < k) | |
7334 continue; | |
7335 } | |
7336 } | |
7337 | |
375 | 7338 if ((pf = smp[n].sm_oneof_w) != NULL) |
372 | 7339 { |
375 | 7340 /* Check for match with one of the chars in "sm_oneof". */ |
372 | 7341 while (*pf != NUL && *pf != word[i + k]) |
7342 ++pf; | |
7343 if (*pf == NUL) | |
7344 continue; | |
7345 ++k; | |
7346 } | |
7347 s = smp[n].sm_rules; | |
7348 pri = 5; /* default priority */ | |
7349 | |
7350 p0 = *s; | |
7351 k0 = k; | |
7352 while (*s == '-' && k > 1) | |
7353 { | |
7354 k--; | |
7355 s++; | |
7356 } | |
7357 if (*s == '<') | |
7358 s++; | |
7359 if (VIM_ISDIGIT(*s)) | |
7360 { | |
7361 /* determine priority */ | |
7362 pri = *s - '0'; | |
7363 s++; | |
7364 } | |
7365 if (*s == '^' && *(s + 1) == '^') | |
7366 s++; | |
7367 | |
7368 if (*s == NUL | |
7369 || (*s == '^' | |
7370 && (i == 0 || !(word[i - 1] == ' ' | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7371 || spell_iswordp_w(word + i - 1, curwin))) |
372 | 7372 && (*(s + 1) != '$' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7373 || (!spell_iswordp_w(word + i + k0, curwin)))) |
372 | 7374 || (*s == '$' && i > 0 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7375 && spell_iswordp_w(word + i - 1, curwin) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7376 && (!spell_iswordp_w(word + i + k0, curwin)))) |
372 | 7377 { |
7378 /* search for followup rules, if: */ | |
7379 /* followup and k > 1 and NO '-' in searchstring */ | |
7380 c0 = word[i + k - 1]; | |
7381 n0 = slang->sl_sal_first[c0 & 0xff]; | |
7382 | |
7383 if (slang->sl_followup && k > 1 && n0 >= 0 | |
7384 && p0 != '-' && word[i + k] != NUL) | |
7385 { | |
375 | 7386 /* Test follow-up rule for "word[i + k]"; loop over |
7387 * all entries with the same index byte. */ | |
372 | 7388 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
7389 == (c0 & 0xff); ++n0) | |
7390 { | |
7391 /* Quickly skip entries that don't match the word. | |
375 | 7392 */ |
7393 if (c0 != ws[0]) | |
7394 continue; | |
372 | 7395 k0 = smp[n0].sm_leadlen; |
7396 if (k0 > 1) | |
7397 { | |
7398 if (word[i + k] != ws[1]) | |
7399 continue; | |
7400 if (k0 > 2) | |
7401 { | |
7402 pf = word + i + k + 1; | |
7403 for (j = 2; j < k0; ++j) | |
7404 if (*pf++ != ws[j]) | |
7405 break; | |
7406 if (j < k0) | |
7407 continue; | |
7408 } | |
7409 } | |
7410 k0 += k - 1; | |
7411 | |
375 | 7412 if ((pf = smp[n0].sm_oneof_w) != NULL) |
372 | 7413 { |
7414 /* Check for match with one of the chars in | |
375 | 7415 * "sm_oneof". */ |
372 | 7416 while (*pf != NUL && *pf != word[i + k0]) |
7417 ++pf; | |
7418 if (*pf == NUL) | |
7419 continue; | |
7420 ++k0; | |
7421 } | |
7422 | |
7423 p0 = 5; | |
7424 s = smp[n0].sm_rules; | |
7425 while (*s == '-') | |
7426 { | |
7427 /* "k0" gets NOT reduced because | |
7428 * "if (k0 == k)" */ | |
7429 s++; | |
7430 } | |
7431 if (*s == '<') | |
7432 s++; | |
7433 if (VIM_ISDIGIT(*s)) | |
7434 { | |
7435 p0 = *s - '0'; | |
7436 s++; | |
7437 } | |
7438 | |
7439 if (*s == NUL | |
7440 /* *s == '^' cuts */ | |
7441 || (*s == '$' | |
376 | 7442 && !spell_iswordp_w(word + i + k0, |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7443 curwin))) |
372 | 7444 { |
7445 if (k0 == k) | |
7446 /* this is just a piece of the string */ | |
7447 continue; | |
7448 | |
7449 if (p0 < pri) | |
7450 /* priority too low */ | |
7451 continue; | |
7452 /* rule fits; stop search */ | |
7453 break; | |
7454 } | |
7455 } | |
7456 | |
7457 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) | |
7458 == (c0 & 0xff)) | |
7459 continue; | |
7460 } | |
7461 | |
7462 /* replace string */ | |
7463 ws = smp[n].sm_to_w; | |
7464 s = smp[n].sm_rules; | |
7465 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; | |
7466 if (p0 == 1 && z == 0) | |
7467 { | |
7468 /* rule with '<' is used */ | |
389 | 7469 if (reslen > 0 && ws != NULL && *ws != NUL |
7470 && (wres[reslen - 1] == c | |
372 | 7471 || wres[reslen - 1] == *ws)) |
7472 reslen--; | |
7473 z0 = 1; | |
7474 z = 1; | |
7475 k0 = 0; | |
389 | 7476 if (ws != NULL) |
7477 while (*ws != NUL && word[i + k0] != NUL) | |
7478 { | |
7479 word[i + k0] = *ws; | |
7480 k0++; | |
7481 ws++; | |
7482 } | |
372 | 7483 if (k > k0) |
7484 mch_memmove(word + i + k0, word + i + k, | |
3520 | 7485 sizeof(int) * (wordlen - (i + k) + 1)); |
372 | 7486 |
7487 /* new "actual letter" */ | |
7488 c = word[i]; | |
7489 } | |
7490 else | |
7491 { | |
7492 /* no '<' rule used */ | |
7493 i += k - 1; | |
7494 z = 0; | |
389 | 7495 if (ws != NULL) |
7496 while (*ws != NUL && ws[1] != NUL | |
7497 && reslen < MAXWLEN) | |
7498 { | |
7499 if (reslen == 0 || wres[reslen - 1] != *ws) | |
7500 wres[reslen++] = *ws; | |
7501 ws++; | |
7502 } | |
372 | 7503 /* new "actual letter" */ |
389 | 7504 if (ws == NULL) |
7505 c = NUL; | |
7506 else | |
7507 c = *ws; | |
372 | 7508 if (strstr((char *)s, "^^") != NULL) |
7509 { | |
7510 if (c != NUL) | |
7511 wres[reslen++] = c; | |
7512 mch_memmove(word, word + i + 1, | |
3520 | 7513 sizeof(int) * (wordlen - (i + 1) + 1)); |
372 | 7514 i = 0; |
7515 z0 = 1; | |
7516 } | |
7517 } | |
7518 break; | |
7519 } | |
7520 } | |
7521 } | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7522 else if (VIM_ISWHITE(c)) |
372 | 7523 { |
7524 c = ' '; | |
7525 k = 1; | |
7526 } | |
7527 | |
7528 if (z0 == 0) | |
7529 { | |
7530 if (k && !p0 && reslen < MAXWLEN && c != NUL | |
7531 && (!slang->sl_collapse || reslen == 0 | |
7532 || wres[reslen - 1] != c)) | |
7533 /* condense only double letters */ | |
7534 wres[reslen++] = c; | |
7535 | |
7536 i++; | |
7537 z = 0; | |
7538 k = 0; | |
7539 } | |
7540 } | |
7541 | |
7542 /* Convert wide characters in "wres" to a multi-byte string in "res". */ | |
7543 l = 0; | |
7544 for (n = 0; n < reslen; ++n) | |
7545 { | |
7546 l += mb_char2bytes(wres[n], res + l); | |
7547 if (l + MB_MAXBYTES > MAXWLEN) | |
7548 break; | |
7549 } | |
7550 res[l] = NUL; | |
7551 } | |
7552 | |
324 | 7553 /* |
344 | 7554 * Compute a score for two sound-a-like words. |
7555 * This permits up to two inserts/deletes/swaps/etc. to keep things fast. | |
7556 * Instead of a generic loop we write out the code. That keeps it fast by | |
7557 * avoiding checks that will not be possible. | |
7558 */ | |
7559 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7560 soundalike_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7561 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
|
7562 char_u *badstart) /* sound-folded bad word */ |
344 | 7563 { |
351 | 7564 char_u *goodsound = goodstart; |
7565 char_u *badsound = badstart; | |
7566 int goodlen; | |
7567 int badlen; | |
344 | 7568 int n; |
7569 char_u *pl, *ps; | |
7570 char_u *pl2, *ps2; | |
351 | 7571 int score = 0; |
7572 | |
2454
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7573 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be |
351 | 7574 * counted so much, vowels halfway the word aren't counted at all. */ |
7575 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) | |
7576 { | |
2454
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7577 if ((badsound[0] == NUL && goodsound[1] == NUL) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7578 || (goodsound[0] == NUL && badsound[1] == NUL)) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7579 /* 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
|
7580 return SCORE_DEL; |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7581 if (badsound[0] == NUL || goodsound[0] == NUL) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7582 /* more than two changes */ |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7583 return SCORE_MAXMAX; |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7584 |
625 | 7585 if (badsound[1] == goodsound[1] |
7586 || (badsound[1] != NUL | |
7587 && goodsound[1] != NUL | |
7588 && badsound[2] == goodsound[2])) | |
7589 { | |
7590 /* handle like a substitute */ | |
7591 } | |
351 | 7592 else |
625 | 7593 { |
7594 score = 2 * SCORE_DEL / 3; | |
7595 if (*badsound == '*') | |
7596 ++badsound; | |
7597 else | |
7598 ++goodsound; | |
7599 } | |
351 | 7600 } |
7601 | |
835 | 7602 goodlen = (int)STRLEN(goodsound); |
7603 badlen = (int)STRLEN(badsound); | |
344 | 7604 |
1188 | 7605 /* Return quickly if the lengths are too different to be fixed by two |
344 | 7606 * changes. */ |
7607 n = goodlen - badlen; | |
7608 if (n < -2 || n > 2) | |
7609 return SCORE_MAXMAX; | |
7610 | |
7611 if (n > 0) | |
7612 { | |
351 | 7613 pl = goodsound; /* goodsound is longest */ |
344 | 7614 ps = badsound; |
7615 } | |
7616 else | |
7617 { | |
351 | 7618 pl = badsound; /* badsound is longest */ |
344 | 7619 ps = goodsound; |
7620 } | |
7621 | |
7622 /* Skip over the identical part. */ | |
7623 while (*pl == *ps && *pl != NUL) | |
7624 { | |
7625 ++pl; | |
7626 ++ps; | |
7627 } | |
7628 | |
7629 switch (n) | |
7630 { | |
7631 case -2: | |
7632 case 2: | |
7633 /* | |
7634 * Must delete two characters from "pl". | |
7635 */ | |
7636 ++pl; /* first delete */ | |
7637 while (*pl == *ps) | |
7638 { | |
7639 ++pl; | |
7640 ++ps; | |
7641 } | |
7642 /* strings must be equal after second delete */ | |
7643 if (STRCMP(pl + 1, ps) == 0) | |
351 | 7644 return score + SCORE_DEL * 2; |
344 | 7645 |
7646 /* Failed to compare. */ | |
7647 break; | |
7648 | |
7649 case -1: | |
7650 case 1: | |
7651 /* | |
7652 * Minimal one delete from "pl" required. | |
7653 */ | |
7654 | |
7655 /* 1: delete */ | |
7656 pl2 = pl + 1; | |
7657 ps2 = ps; | |
7658 while (*pl2 == *ps2) | |
7659 { | |
7660 if (*pl2 == NUL) /* reached the end */ | |
351 | 7661 return score + SCORE_DEL; |
344 | 7662 ++pl2; |
7663 ++ps2; | |
7664 } | |
7665 | |
7666 /* 2: delete then swap, then rest must be equal */ | |
7667 if (pl2[0] == ps2[1] && pl2[1] == ps2[0] | |
7668 && STRCMP(pl2 + 2, ps2 + 2) == 0) | |
351 | 7669 return score + SCORE_DEL + SCORE_SWAP; |
344 | 7670 |
7671 /* 3: delete then substitute, then the rest must be equal */ | |
7672 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7673 return score + SCORE_DEL + SCORE_SUBST; |
344 | 7674 |
7675 /* 4: first swap then delete */ | |
7676 if (pl[0] == ps[1] && pl[1] == ps[0]) | |
7677 { | |
7678 pl2 = pl + 2; /* swap, skip two chars */ | |
7679 ps2 = ps + 2; | |
7680 while (*pl2 == *ps2) | |
7681 { | |
7682 ++pl2; | |
7683 ++ps2; | |
7684 } | |
7685 /* delete a char and then strings must be equal */ | |
7686 if (STRCMP(pl2 + 1, ps2) == 0) | |
351 | 7687 return score + SCORE_SWAP + SCORE_DEL; |
344 | 7688 } |
7689 | |
7690 /* 5: first substitute then delete */ | |
7691 pl2 = pl + 1; /* substitute, skip one char */ | |
7692 ps2 = ps + 1; | |
7693 while (*pl2 == *ps2) | |
7694 { | |
7695 ++pl2; | |
7696 ++ps2; | |
7697 } | |
7698 /* delete a char and then strings must be equal */ | |
7699 if (STRCMP(pl2 + 1, ps2) == 0) | |
351 | 7700 return score + SCORE_SUBST + SCORE_DEL; |
344 | 7701 |
7702 /* Failed to compare. */ | |
7703 break; | |
7704 | |
7705 case 0: | |
7706 /* | |
1808 | 7707 * Lengths are equal, thus changes must result in same length: An |
344 | 7708 * insert is only possible in combination with a delete. |
7709 * 1: check if for identical strings | |
7710 */ | |
7711 if (*pl == NUL) | |
351 | 7712 return score; |
344 | 7713 |
7714 /* 2: swap */ | |
7715 if (pl[0] == ps[1] && pl[1] == ps[0]) | |
7716 { | |
7717 pl2 = pl + 2; /* swap, skip two chars */ | |
7718 ps2 = ps + 2; | |
7719 while (*pl2 == *ps2) | |
7720 { | |
7721 if (*pl2 == NUL) /* reached the end */ | |
351 | 7722 return score + SCORE_SWAP; |
344 | 7723 ++pl2; |
7724 ++ps2; | |
7725 } | |
7726 /* 3: swap and swap again */ | |
7727 if (pl2[0] == ps2[1] && pl2[1] == ps2[0] | |
7728 && STRCMP(pl2 + 2, ps2 + 2) == 0) | |
351 | 7729 return score + SCORE_SWAP + SCORE_SWAP; |
344 | 7730 |
7731 /* 4: swap and substitute */ | |
7732 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7733 return score + SCORE_SWAP + SCORE_SUBST; |
344 | 7734 } |
7735 | |
7736 /* 5: substitute */ | |
7737 pl2 = pl + 1; | |
7738 ps2 = ps + 1; | |
7739 while (*pl2 == *ps2) | |
7740 { | |
7741 if (*pl2 == NUL) /* reached the end */ | |
351 | 7742 return score + SCORE_SUBST; |
344 | 7743 ++pl2; |
7744 ++ps2; | |
7745 } | |
7746 | |
7747 /* 6: substitute and swap */ | |
7748 if (pl2[0] == ps2[1] && pl2[1] == ps2[0] | |
7749 && STRCMP(pl2 + 2, ps2 + 2) == 0) | |
351 | 7750 return score + SCORE_SUBST + SCORE_SWAP; |
344 | 7751 |
7752 /* 7: substitute and substitute */ | |
7753 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7754 return score + SCORE_SUBST + SCORE_SUBST; |
344 | 7755 |
7756 /* 8: insert then delete */ | |
7757 pl2 = pl; | |
7758 ps2 = ps + 1; | |
7759 while (*pl2 == *ps2) | |
7760 { | |
7761 ++pl2; | |
7762 ++ps2; | |
7763 } | |
7764 if (STRCMP(pl2 + 1, ps2) == 0) | |
351 | 7765 return score + SCORE_INS + SCORE_DEL; |
344 | 7766 |
7767 /* 9: delete then insert */ | |
7768 pl2 = pl + 1; | |
7769 ps2 = ps; | |
7770 while (*pl2 == *ps2) | |
7771 { | |
7772 ++pl2; | |
7773 ++ps2; | |
7774 } | |
7775 if (STRCMP(pl2, ps2 + 1) == 0) | |
351 | 7776 return score + SCORE_INS + SCORE_DEL; |
344 | 7777 |
7778 /* Failed to compare. */ | |
7779 break; | |
7780 } | |
7781 | |
7782 return SCORE_MAXMAX; | |
7783 } | |
324 | 7784 |
323 | 7785 /* |
7786 * Compute the "edit distance" to turn "badword" into "goodword". The less | |
344 | 7787 * deletes/inserts/substitutes/swaps are required the lower the score. |
324 | 7788 * |
485 | 7789 * The algorithm is described by Du and Chang, 1992. |
7790 * The implementation of the algorithm comes from Aspell editdist.cpp, | |
7791 * edit_distance(). It has been converted from C++ to C and modified to | |
7792 * support multi-byte characters. | |
323 | 7793 */ |
7794 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7795 spell_edit_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7796 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7797 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7798 char_u *goodword) |
323 | 7799 { |
7800 int *cnt; | |
1188 | 7801 int badlen, goodlen; /* lengths including NUL */ |
323 | 7802 int j, i; |
7803 int t; | |
7804 int bc, gc; | |
324 | 7805 int pbc, pgc; |
7806 char_u *p; | |
7807 int wbadword[MAXWLEN]; | |
7808 int wgoodword[MAXWLEN]; | |
7809 | |
7810 if (has_mbyte) | |
7811 { | |
7812 /* Get the characters from the multi-byte strings and put them in an | |
7813 * int array for easy access. */ | |
7814 for (p = badword, badlen = 0; *p != NUL; ) | |
474 | 7815 wbadword[badlen++] = mb_cptr2char_adv(&p); |
398 | 7816 wbadword[badlen++] = 0; |
324 | 7817 for (p = goodword, goodlen = 0; *p != NUL; ) |
474 | 7818 wgoodword[goodlen++] = mb_cptr2char_adv(&p); |
398 | 7819 wgoodword[goodlen++] = 0; |
324 | 7820 } |
7821 else | |
7822 { | |
835 | 7823 badlen = (int)STRLEN(badword) + 1; |
7824 goodlen = (int)STRLEN(goodword) + 1; | |
324 | 7825 } |
323 | 7826 |
7827 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ | |
7828 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] | |
7829 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)), | |
7830 TRUE); | |
324 | 7831 if (cnt == NULL) |
7832 return 0; /* out of memory */ | |
323 | 7833 |
7834 CNT(0, 0) = 0; | |
7835 for (j = 1; j <= goodlen; ++j) | |
625 | 7836 CNT(0, j) = CNT(0, j - 1) + SCORE_INS; |
323 | 7837 |
7838 for (i = 1; i <= badlen; ++i) | |
7839 { | |
625 | 7840 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL; |
323 | 7841 for (j = 1; j <= goodlen; ++j) |
7842 { | |
324 | 7843 if (has_mbyte) |
7844 { | |
7845 bc = wbadword[i - 1]; | |
7846 gc = wgoodword[j - 1]; | |
7847 } | |
7848 else | |
7849 { | |
7850 bc = badword[i - 1]; | |
7851 gc = goodword[j - 1]; | |
7852 } | |
323 | 7853 if (bc == gc) |
7854 CNT(i, j) = CNT(i - 1, j - 1); | |
7855 else | |
7856 { | |
7857 /* Use a better score when there is only a case difference. */ | |
324 | 7858 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
323 | 7859 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
7860 else | |
625 | 7861 { |
7862 /* For a similar character use SCORE_SIMILAR. */ | |
7863 if (slang != NULL | |
7864 && slang->sl_has_map | |
7865 && similar_chars(slang, gc, bc)) | |
7866 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1); | |
7867 else | |
7868 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); | |
7869 } | |
323 | 7870 |
324 | 7871 if (i > 1 && j > 1) |
323 | 7872 { |
324 | 7873 if (has_mbyte) |
7874 { | |
7875 pbc = wbadword[i - 2]; | |
7876 pgc = wgoodword[j - 2]; | |
7877 } | |
7878 else | |
7879 { | |
7880 pbc = badword[i - 2]; | |
7881 pgc = goodword[j - 2]; | |
7882 } | |
7883 if (bc == pgc && pbc == gc) | |
7884 { | |
7885 t = SCORE_SWAP + CNT(i - 2, j - 2); | |
7886 if (t < CNT(i, j)) | |
7887 CNT(i, j) = t; | |
7888 } | |
323 | 7889 } |
7890 t = SCORE_DEL + CNT(i - 1, j); | |
7891 if (t < CNT(i, j)) | |
7892 CNT(i, j) = t; | |
7893 t = SCORE_INS + CNT(i, j - 1); | |
7894 if (t < CNT(i, j)) | |
7895 CNT(i, j) = t; | |
7896 } | |
7897 } | |
7898 } | |
344 | 7899 |
7900 i = CNT(badlen - 1, goodlen - 1); | |
7901 vim_free(cnt); | |
7902 return i; | |
323 | 7903 } |
307 | 7904 |
625 | 7905 typedef struct |
7906 { | |
7907 int badi; | |
7908 int goodi; | |
7909 int score; | |
7910 } limitscore_T; | |
7911 | |
7912 /* | |
7913 * Like spell_edit_score(), but with a limit on the score to make it faster. | |
7914 * May return SCORE_MAXMAX when the score is higher than "limit". | |
7915 * | |
7916 * This uses a stack for the edits still to be tried. | |
7917 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support | |
7918 * for multi-byte characters. | |
7919 */ | |
7920 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7921 spell_edit_score_limit( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7922 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7923 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7924 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7925 int limit) |
625 | 7926 { |
7927 limitscore_T stack[10]; /* allow for over 3 * 2 edits */ | |
7928 int stackidx; | |
7929 int bi, gi; | |
7930 int bi2, gi2; | |
7931 int bc, gc; | |
7932 int score; | |
7933 int score_off; | |
7934 int minscore; | |
7935 int round; | |
7936 | |
7937 /* Multi-byte characters require a bit more work, use a different function | |
7938 * to avoid testing "has_mbyte" quite often. */ | |
7939 if (has_mbyte) | |
7940 return spell_edit_score_limit_w(slang, badword, goodword, limit); | |
7941 | |
7942 /* | |
7943 * The idea is to go from start to end over the words. So long as | |
7944 * characters are equal just continue, this always gives the lowest score. | |
7945 * When there is a difference try several alternatives. Each alternative | |
7946 * increases "score" for the edit distance. Some of the alternatives are | |
7947 * pushed unto a stack and tried later, some are tried right away. At the | |
7948 * end of the word the score for one alternative is known. The lowest | |
7949 * possible score is stored in "minscore". | |
7950 */ | |
7951 stackidx = 0; | |
7952 bi = 0; | |
7953 gi = 0; | |
7954 score = 0; | |
7955 minscore = limit + 1; | |
7956 | |
7957 for (;;) | |
7958 { | |
7959 /* Skip over an equal part, score remains the same. */ | |
7960 for (;;) | |
7961 { | |
7962 bc = badword[bi]; | |
7963 gc = goodword[gi]; | |
7964 if (bc != gc) /* stop at a char that's different */ | |
7965 break; | |
7966 if (bc == NUL) /* both words end */ | |
7967 { | |
7968 if (score < minscore) | |
7969 minscore = score; | |
7970 goto pop; /* do next alternative */ | |
7971 } | |
7972 ++bi; | |
7973 ++gi; | |
7974 } | |
7975 | |
7976 if (gc == NUL) /* goodword ends, delete badword chars */ | |
7977 { | |
7978 do | |
7979 { | |
7980 if ((score += SCORE_DEL) >= minscore) | |
7981 goto pop; /* do next alternative */ | |
7982 } while (badword[++bi] != NUL); | |
7983 minscore = score; | |
7984 } | |
7985 else if (bc == NUL) /* badword ends, insert badword chars */ | |
7986 { | |
7987 do | |
7988 { | |
7989 if ((score += SCORE_INS) >= minscore) | |
7990 goto pop; /* do next alternative */ | |
7991 } while (goodword[++gi] != NUL); | |
7992 minscore = score; | |
7993 } | |
7994 else /* both words continue */ | |
7995 { | |
7996 /* If not close to the limit, perform a change. Only try changes | |
7997 * that may lead to a lower score than "minscore". | |
7998 * round 0: try deleting a char from badword | |
7999 * round 1: try inserting a char in badword */ | |
8000 for (round = 0; round <= 1; ++round) | |
8001 { | |
8002 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS); | |
8003 if (score_off < minscore) | |
8004 { | |
8005 if (score_off + SCORE_EDIT_MIN >= minscore) | |
8006 { | |
8007 /* Near the limit, rest of the words must match. We | |
8008 * can check that right now, no need to push an item | |
8009 * onto the stack. */ | |
8010 bi2 = bi + 1 - round; | |
8011 gi2 = gi + round; | |
8012 while (goodword[gi2] == badword[bi2]) | |
8013 { | |
8014 if (goodword[gi2] == NUL) | |
8015 { | |
8016 minscore = score_off; | |
8017 break; | |
8018 } | |
8019 ++bi2; | |
8020 ++gi2; | |
8021 } | |
8022 } | |
8023 else | |
8024 { | |
8025 /* try deleting/inserting a character later */ | |
8026 stack[stackidx].badi = bi + 1 - round; | |
8027 stack[stackidx].goodi = gi + round; | |
8028 stack[stackidx].score = score_off; | |
8029 ++stackidx; | |
8030 } | |
8031 } | |
8032 } | |
8033 | |
8034 if (score + SCORE_SWAP < minscore) | |
8035 { | |
8036 /* If swapping two characters makes a match then the | |
8037 * substitution is more expensive, thus there is no need to | |
8038 * try both. */ | |
8039 if (gc == badword[bi + 1] && bc == goodword[gi + 1]) | |
8040 { | |
8041 /* Swap two characters, that is: skip them. */ | |
8042 gi += 2; | |
8043 bi += 2; | |
8044 score += SCORE_SWAP; | |
8045 continue; | |
8046 } | |
8047 } | |
8048 | |
8049 /* Substitute one character for another which is the same | |
8050 * thing as deleting a character from both goodword and badword. | |
8051 * Use a better score when there is only a case difference. */ | |
8052 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) | |
8053 score += SCORE_ICASE; | |
8054 else | |
8055 { | |
8056 /* For a similar character use SCORE_SIMILAR. */ | |
8057 if (slang != NULL | |
8058 && slang->sl_has_map | |
8059 && similar_chars(slang, gc, bc)) | |
8060 score += SCORE_SIMILAR; | |
8061 else | |
8062 score += SCORE_SUBST; | |
8063 } | |
8064 | |
8065 if (score < minscore) | |
8066 { | |
8067 /* Do the substitution. */ | |
8068 ++gi; | |
8069 ++bi; | |
8070 continue; | |
8071 } | |
8072 } | |
8073 pop: | |
8074 /* | |
8075 * Get here to try the next alternative, pop it from the stack. | |
8076 */ | |
8077 if (stackidx == 0) /* stack is empty, finished */ | |
8078 break; | |
8079 | |
8080 /* pop an item from the stack */ | |
8081 --stackidx; | |
8082 gi = stack[stackidx].goodi; | |
8083 bi = stack[stackidx].badi; | |
8084 score = stack[stackidx].score; | |
8085 } | |
8086 | |
8087 /* When the score goes over "limit" it may actually be much higher. | |
8088 * Return a very large number to avoid going below the limit when giving a | |
8089 * bonus. */ | |
8090 if (minscore > limit) | |
8091 return SCORE_MAXMAX; | |
8092 return minscore; | |
8093 } | |
8094 | |
8095 /* | |
8096 * Multi-byte version of spell_edit_score_limit(). | |
8097 * Keep it in sync with the above! | |
8098 */ | |
8099 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8100 spell_edit_score_limit_w( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8101 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8102 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8103 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8104 int limit) |
625 | 8105 { |
8106 limitscore_T stack[10]; /* allow for over 3 * 2 edits */ | |
8107 int stackidx; | |
8108 int bi, gi; | |
8109 int bi2, gi2; | |
8110 int bc, gc; | |
8111 int score; | |
8112 int score_off; | |
8113 int minscore; | |
8114 int round; | |
8115 char_u *p; | |
8116 int wbadword[MAXWLEN]; | |
8117 int wgoodword[MAXWLEN]; | |
8118 | |
8119 /* Get the characters from the multi-byte strings and put them in an | |
8120 * int array for easy access. */ | |
8121 bi = 0; | |
8122 for (p = badword; *p != NUL; ) | |
8123 wbadword[bi++] = mb_cptr2char_adv(&p); | |
8124 wbadword[bi++] = 0; | |
8125 gi = 0; | |
8126 for (p = goodword; *p != NUL; ) | |
8127 wgoodword[gi++] = mb_cptr2char_adv(&p); | |
8128 wgoodword[gi++] = 0; | |
8129 | |
8130 /* | |
8131 * The idea is to go from start to end over the words. So long as | |
8132 * characters are equal just continue, this always gives the lowest score. | |
8133 * When there is a difference try several alternatives. Each alternative | |
8134 * increases "score" for the edit distance. Some of the alternatives are | |
8135 * pushed unto a stack and tried later, some are tried right away. At the | |
8136 * end of the word the score for one alternative is known. The lowest | |
8137 * possible score is stored in "minscore". | |
8138 */ | |
8139 stackidx = 0; | |
8140 bi = 0; | |
8141 gi = 0; | |
8142 score = 0; | |
8143 minscore = limit + 1; | |
8144 | |
8145 for (;;) | |
8146 { | |
8147 /* Skip over an equal part, score remains the same. */ | |
8148 for (;;) | |
8149 { | |
8150 bc = wbadword[bi]; | |
8151 gc = wgoodword[gi]; | |
8152 | |
8153 if (bc != gc) /* stop at a char that's different */ | |
8154 break; | |
8155 if (bc == NUL) /* both words end */ | |
8156 { | |
8157 if (score < minscore) | |
8158 minscore = score; | |
8159 goto pop; /* do next alternative */ | |
8160 } | |
8161 ++bi; | |
8162 ++gi; | |
8163 } | |
8164 | |
8165 if (gc == NUL) /* goodword ends, delete badword chars */ | |
8166 { | |
8167 do | |
8168 { | |
8169 if ((score += SCORE_DEL) >= minscore) | |
8170 goto pop; /* do next alternative */ | |
8171 } while (wbadword[++bi] != NUL); | |
8172 minscore = score; | |
8173 } | |
8174 else if (bc == NUL) /* badword ends, insert badword chars */ | |
8175 { | |
8176 do | |
8177 { | |
8178 if ((score += SCORE_INS) >= minscore) | |
8179 goto pop; /* do next alternative */ | |
8180 } while (wgoodword[++gi] != NUL); | |
8181 minscore = score; | |
8182 } | |
8183 else /* both words continue */ | |
8184 { | |
8185 /* If not close to the limit, perform a change. Only try changes | |
8186 * that may lead to a lower score than "minscore". | |
8187 * round 0: try deleting a char from badword | |
8188 * round 1: try inserting a char in badword */ | |
8189 for (round = 0; round <= 1; ++round) | |
8190 { | |
8191 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS); | |
8192 if (score_off < minscore) | |
8193 { | |
8194 if (score_off + SCORE_EDIT_MIN >= minscore) | |
8195 { | |
8196 /* Near the limit, rest of the words must match. We | |
8197 * can check that right now, no need to push an item | |
8198 * onto the stack. */ | |
8199 bi2 = bi + 1 - round; | |
8200 gi2 = gi + round; | |
8201 while (wgoodword[gi2] == wbadword[bi2]) | |
8202 { | |
8203 if (wgoodword[gi2] == NUL) | |
8204 { | |
8205 minscore = score_off; | |
8206 break; | |
8207 } | |
8208 ++bi2; | |
8209 ++gi2; | |
8210 } | |
8211 } | |
8212 else | |
8213 { | |
8214 /* try deleting a character from badword later */ | |
8215 stack[stackidx].badi = bi + 1 - round; | |
8216 stack[stackidx].goodi = gi + round; | |
8217 stack[stackidx].score = score_off; | |
8218 ++stackidx; | |
8219 } | |
8220 } | |
8221 } | |
8222 | |
8223 if (score + SCORE_SWAP < minscore) | |
8224 { | |
8225 /* If swapping two characters makes a match then the | |
8226 * substitution is more expensive, thus there is no need to | |
8227 * try both. */ | |
8228 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1]) | |
8229 { | |
8230 /* Swap two characters, that is: skip them. */ | |
8231 gi += 2; | |
8232 bi += 2; | |
8233 score += SCORE_SWAP; | |
8234 continue; | |
8235 } | |
8236 } | |
8237 | |
8238 /* Substitute one character for another which is the same | |
8239 * thing as deleting a character from both goodword and badword. | |
8240 * Use a better score when there is only a case difference. */ | |
8241 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) | |
8242 score += SCORE_ICASE; | |
8243 else | |
8244 { | |
8245 /* For a similar character use SCORE_SIMILAR. */ | |
8246 if (slang != NULL | |
8247 && slang->sl_has_map | |
8248 && similar_chars(slang, gc, bc)) | |
8249 score += SCORE_SIMILAR; | |
8250 else | |
8251 score += SCORE_SUBST; | |
8252 } | |
8253 | |
8254 if (score < minscore) | |
8255 { | |
8256 /* Do the substitution. */ | |
8257 ++gi; | |
8258 ++bi; | |
8259 continue; | |
8260 } | |
8261 } | |
8262 pop: | |
8263 /* | |
8264 * Get here to try the next alternative, pop it from the stack. | |
8265 */ | |
8266 if (stackidx == 0) /* stack is empty, finished */ | |
8267 break; | |
8268 | |
8269 /* pop an item from the stack */ | |
8270 --stackidx; | |
8271 gi = stack[stackidx].goodi; | |
8272 bi = stack[stackidx].badi; | |
8273 score = stack[stackidx].score; | |
8274 } | |
8275 | |
8276 /* When the score goes over "limit" it may actually be much higher. | |
8277 * Return a very large number to avoid going below the limit when giving a | |
8278 * bonus. */ | |
8279 if (minscore > limit) | |
8280 return SCORE_MAXMAX; | |
8281 return minscore; | |
8282 } | |
8283 | |
714 | 8284 /* |
8285 * ":spellinfo" | |
8286 */ | |
8287 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8288 ex_spellinfo(exarg_T *eap UNUSED) |
714 | 8289 { |
8290 int lpi; | |
8291 langp_T *lp; | |
8292 char_u *p; | |
8293 | |
8294 if (no_spell_checking(curwin)) | |
8295 return; | |
8296 | |
8297 msg_start(); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8298 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
|
8299 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8300 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
|
8301 msg_puts("file: "); |
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8302 msg_puts((char *)lp->lp_slang->sl_fname); |
714 | 8303 msg_putchar('\n'); |
8304 p = lp->lp_slang->sl_info; | |
8305 if (p != NULL) | |
8306 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8307 msg_puts((char *)p); |
714 | 8308 msg_putchar('\n'); |
8309 } | |
8310 } | |
8311 msg_end(); | |
8312 } | |
8313 | |
625 | 8314 #define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */ |
8315 #define DUMPFLAG_COUNT 2 /* include word count */ | |
701 | 8316 #define DUMPFLAG_ICASE 4 /* ignore case when finding matches */ |
710 | 8317 #define DUMPFLAG_ONECAP 8 /* pattern starts with capital */ |
8318 #define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */ | |
625 | 8319 |
351 | 8320 /* |
8321 * ":spelldump" | |
8322 */ | |
8323 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8324 ex_spelldump(exarg_T *eap) |
351 | 8325 { |
5382 | 8326 char_u *spl; |
8327 long dummy; | |
8328 | |
701 | 8329 if (no_spell_checking(curwin)) |
8330 return; | |
5382 | 8331 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL); |
8332 | |
8333 /* Create a new empty buffer in a new window. */ | |
701 | 8334 do_cmdline_cmd((char_u *)"new"); |
5382 | 8335 |
8336 /* enable spelling locally in the new window */ | |
8337 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
|
8338 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); |
5382 | 8339 vim_free(spl); |
8340 | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10950
diff
changeset
|
8341 if (!BUFEMPTY()) |
701 | 8342 return; |
8343 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8344 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); |
701 | 8345 |
8346 /* Delete the empty line that we started with. */ | |
8347 if (curbuf->b_ml.ml_line_count > 1) | |
8348 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
8349 | |
8350 redraw_later(NOT_VALID); | |
8351 } | |
8352 | |
8353 /* | |
8354 * Go through all possible words and: | |
8355 * 1. When "pat" is NULL: dump a list of all words in the current buffer. | |
8356 * "ic" and "dir" are not used. | |
8357 * 2. When "pat" is not NULL: add matching words to insert mode completion. | |
8358 */ | |
8359 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8360 spell_dump_compl( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8361 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
|
8362 int ic, /* ignore case */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8363 int *dir, /* direction for adding matches */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8364 int dumpflags_arg) /* DUMPFLAG_* */ |
701 | 8365 { |
351 | 8366 langp_T *lp; |
8367 slang_T *slang; | |
8368 idx_T arridx[MAXWLEN]; | |
8369 int curi[MAXWLEN]; | |
8370 char_u word[MAXWLEN]; | |
8371 int c; | |
8372 char_u *byts; | |
8373 idx_T *idxs; | |
8374 linenr_T lnum = 0; | |
8375 int round; | |
8376 int depth; | |
8377 int n; | |
8378 int flags; | |
381 | 8379 char_u *region_names = NULL; /* region names being used */ |
8380 int do_region = TRUE; /* dump region names and numbers */ | |
8381 char_u *p; | |
500 | 8382 int lpi; |
701 | 8383 int dumpflags = dumpflags_arg; |
8384 int patlen; | |
8385 | |
710 | 8386 /* When ignoring case or when the pattern starts with capital pass this on |
8387 * to dump_word(). */ | |
8388 if (pat != NULL) | |
8389 { | |
8390 if (ic) | |
8391 dumpflags |= DUMPFLAG_ICASE; | |
8392 else | |
8393 { | |
8394 n = captype(pat, NULL); | |
8395 if (n == WF_ONECAP) | |
8396 dumpflags |= DUMPFLAG_ONECAP; | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
8397 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat)) |
710 | 8398 dumpflags |= DUMPFLAG_ALLCAP; |
8399 } | |
8400 } | |
351 | 8401 |
381 | 8402 /* Find out if we can support regions: All languages must support the same |
8403 * regions or none at all. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8404 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
|
8405 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8406 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
381 | 8407 p = lp->lp_slang->sl_regions; |
8408 if (p[0] != 0) | |
8409 { | |
8410 if (region_names == NULL) /* first language with regions */ | |
8411 region_names = p; | |
8412 else if (STRCMP(region_names, p) != 0) | |
8413 { | |
8414 do_region = FALSE; /* region names are different */ | |
8415 break; | |
8416 } | |
8417 } | |
8418 } | |
8419 | |
8420 if (do_region && region_names != NULL) | |
8421 { | |
701 | 8422 if (pat == NULL) |
8423 { | |
8424 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); | |
8425 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); | |
8426 } | |
381 | 8427 } |
8428 else | |
8429 do_region = FALSE; | |
8430 | |
8431 /* | |
8432 * Loop over all files loaded for the entries in 'spelllang'. | |
8433 */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8434 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
|
8435 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8436 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
351 | 8437 slang = lp->lp_slang; |
500 | 8438 if (slang->sl_fbyts == NULL) /* reloading failed */ |
8439 continue; | |
351 | 8440 |
701 | 8441 if (pat == NULL) |
8442 { | |
8443 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); | |
8444 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); | |
8445 } | |
8446 | |
8447 /* When matching with a pattern and there are no prefixes only use | |
8448 * parts of the tree that match "pat". */ | |
8449 if (pat != NULL && slang->sl_pbyts == NULL) | |
835 | 8450 patlen = (int)STRLEN(pat); |
701 | 8451 else |
840 | 8452 patlen = -1; |
351 | 8453 |
8454 /* round 1: case-folded tree | |
8455 * round 2: keep-case tree */ | |
8456 for (round = 1; round <= 2; ++round) | |
8457 { | |
8458 if (round == 1) | |
8459 { | |
701 | 8460 dumpflags &= ~DUMPFLAG_KEEPCASE; |
351 | 8461 byts = slang->sl_fbyts; |
8462 idxs = slang->sl_fidxs; | |
8463 } | |
8464 else | |
8465 { | |
701 | 8466 dumpflags |= DUMPFLAG_KEEPCASE; |
351 | 8467 byts = slang->sl_kbyts; |
8468 idxs = slang->sl_kidxs; | |
8469 } | |
8470 if (byts == NULL) | |
8471 continue; /* array is empty */ | |
8472 | |
8473 depth = 0; | |
8474 arridx[0] = 0; | |
8475 curi[0] = 1; | |
701 | 8476 while (depth >= 0 && !got_int |
16142
570a296aa0b4
patch 8.1.1076: file for Insert mode is much too big
Bram Moolenaar <Bram@vim.org>
parents:
15636
diff
changeset
|
8477 && (pat == NULL || !ins_compl_interrupted())) |
351 | 8478 { |
8479 if (curi[depth] > byts[arridx[depth]]) | |
8480 { | |
8481 /* Done all bytes at this node, go up one level. */ | |
8482 --depth; | |
8483 line_breakcheck(); | |
10277
154d5a2e7395
commit https://github.com/vim/vim/commit/472e85970ee3a80abd824bef510df12e9cfe9e96
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
8484 ins_compl_check_keys(50, FALSE); |
351 | 8485 } |
8486 else | |
8487 { | |
8488 /* Do one more byte at this node. */ | |
8489 n = arridx[depth] + curi[depth]; | |
8490 ++curi[depth]; | |
8491 c = byts[n]; | |
8492 if (c == 0) | |
8493 { | |
8494 /* End of word, deal with the word. | |
8495 * Don't use keep-case words in the fold-case tree, | |
8496 * they will appear in the keep-case tree. | |
8497 * Only use the word when the region matches. */ | |
8498 flags = (int)idxs[n]; | |
8499 if ((round == 2 || (flags & WF_KEEPCAP) == 0) | |
500 | 8500 && (flags & WF_NEEDCOMP) == 0 |
381 | 8501 && (do_region |
8502 || (flags & WF_REGION) == 0 | |
390 | 8503 || (((unsigned)flags >> 16) |
351 | 8504 & lp->lp_region) != 0)) |
8505 { | |
8506 word[depth] = NUL; | |
381 | 8507 if (!do_region) |
8508 flags &= ~WF_REGION; | |
355 | 8509 |
8510 /* Dump the basic word if there is no prefix or | |
8511 * when it's the first one. */ | |
390 | 8512 c = (unsigned)flags >> 24; |
355 | 8513 if (c == 0 || curi[depth] == 2) |
701 | 8514 { |
8515 dump_word(slang, word, pat, dir, | |
8516 dumpflags, flags, lnum); | |
8517 if (pat == NULL) | |
8518 ++lnum; | |
8519 } | |
351 | 8520 |
8521 /* Apply the prefix, if there is one. */ | |
355 | 8522 if (c != 0) |
701 | 8523 lnum = dump_prefixes(slang, word, pat, dir, |
8524 dumpflags, flags, lnum); | |
351 | 8525 } |
8526 } | |
8527 else | |
8528 { | |
8529 /* Normal char, go one level deeper. */ | |
8530 word[depth++] = c; | |
8531 arridx[depth] = idxs[n]; | |
8532 curi[depth] = 1; | |
701 | 8533 |
8534 /* Check if this characters matches with the pattern. | |
8535 * If not skip the whole tree below it. | |
710 | 8536 * Always ignore case here, dump_word() will check |
8537 * proper case later. This isn't exactly right when | |
8538 * length changes for multi-byte characters with | |
8539 * ignore case... */ | |
8540 if (depth <= patlen | |
8541 && MB_STRNICMP(word, pat, depth) != 0) | |
701 | 8542 --depth; |
351 | 8543 } |
8544 } | |
8545 } | |
8546 } | |
8547 } | |
8548 } | |
8549 | |
8550 /* | |
8551 * Dump one word: apply case modifications and append a line to the buffer. | |
701 | 8552 * When "lnum" is zero add insert mode completion. |
8553 */ | |
8554 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8555 dump_word( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8556 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8557 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8558 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8559 int *dir, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8560 int dumpflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8561 int wordflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8562 linenr_T lnum) |
351 | 8563 { |
8564 int keepcap = FALSE; | |
8565 char_u *p; | |
625 | 8566 char_u *tw; |
351 | 8567 char_u cword[MAXWLEN]; |
381 | 8568 char_u badword[MAXWLEN + 10]; |
8569 int i; | |
710 | 8570 int flags = wordflags; |
8571 | |
8572 if (dumpflags & DUMPFLAG_ONECAP) | |
8573 flags |= WF_ONECAP; | |
8574 if (dumpflags & DUMPFLAG_ALLCAP) | |
8575 flags |= WF_ALLCAP; | |
351 | 8576 |
625 | 8577 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0) |
351 | 8578 { |
8579 /* Need to fix case according to "flags". */ | |
8580 make_case_word(word, cword, flags); | |
8581 p = cword; | |
8582 } | |
8583 else | |
8584 { | |
8585 p = word; | |
625 | 8586 if ((dumpflags & DUMPFLAG_KEEPCASE) |
8587 && ((captype(word, NULL) & WF_KEEPCAP) == 0 | |
389 | 8588 || (flags & WF_FIXCAP) != 0)) |
351 | 8589 keepcap = TRUE; |
8590 } | |
625 | 8591 tw = p; |
351 | 8592 |
701 | 8593 if (pat == NULL) |
8594 { | |
8595 /* Add flags and regions after a slash. */ | |
8596 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) | |
8597 { | |
8598 STRCPY(badword, p); | |
8599 STRCAT(badword, "/"); | |
8600 if (keepcap) | |
8601 STRCAT(badword, "="); | |
8602 if (flags & WF_BANNED) | |
8603 STRCAT(badword, "!"); | |
8604 else if (flags & WF_RARE) | |
8605 STRCAT(badword, "?"); | |
8606 if (flags & WF_REGION) | |
8607 for (i = 0; i < 7; ++i) | |
8608 if (flags & (0x10000 << i)) | |
8609 sprintf((char *)badword + STRLEN(badword), "%d", i + 1); | |
8610 p = badword; | |
8611 } | |
8612 | |
8613 if (dumpflags & DUMPFLAG_COUNT) | |
8614 { | |
8615 hashitem_T *hi; | |
8616 | |
8617 /* Include the word count for ":spelldump!". */ | |
8618 hi = hash_find(&slang->sl_wordcount, tw); | |
8619 if (!HASHITEM_EMPTY(hi)) | |
8620 { | |
8621 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d", | |
625 | 8622 tw, HI2WC(hi)->wc_count); |
701 | 8623 p = IObuff; |
8624 } | |
8625 } | |
8626 | |
8627 ml_append(lnum, p, (colnr_T)0, FALSE); | |
8628 } | |
710 | 8629 else if (((dumpflags & DUMPFLAG_ICASE) |
8630 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0 | |
8631 : STRNCMP(p, pat, STRLEN(pat)) == 0) | |
701 | 8632 && ins_compl_add_infercase(p, (int)STRLEN(p), |
16239
5df26b29e809
patch 8.1.1124: insert completion flags are mixed up
Bram Moolenaar <Bram@vim.org>
parents:
16162
diff
changeset
|
8633 p_ic, NULL, *dir, FALSE) == OK) |
710 | 8634 /* if dir was BACKWARD then honor it just once */ |
8635 *dir = FORWARD; | |
351 | 8636 } |
8637 | |
8638 /* | |
372 | 8639 * For ":spelldump": Find matching prefixes for "word". Prepend each to |
8640 * "word" and append a line to the buffer. | |
701 | 8641 * When "lnum" is zero add insert mode completion. |
351 | 8642 * Return the updated line number. |
8643 */ | |
8644 static linenr_T | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8645 dump_prefixes( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8646 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8647 char_u *word, /* case-folded word */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8648 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8649 int *dir, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8650 int dumpflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8651 int flags, /* flags with prefix ID */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8652 linenr_T startlnum) |
351 | 8653 { |
8654 idx_T arridx[MAXWLEN]; | |
8655 int curi[MAXWLEN]; | |
8656 char_u prefix[MAXWLEN]; | |
455 | 8657 char_u word_up[MAXWLEN]; |
8658 int has_word_up = FALSE; | |
351 | 8659 int c; |
8660 char_u *byts; | |
8661 idx_T *idxs; | |
8662 linenr_T lnum = startlnum; | |
8663 int depth; | |
8664 int n; | |
8665 int len; | |
8666 int i; | |
8667 | |
626 | 8668 /* If the word starts with a lower-case letter make the word with an |
455 | 8669 * upper-case letter in word_up[]. */ |
8670 c = PTR2CHAR(word); | |
8671 if (SPELL_TOUPPER(c) != c) | |
8672 { | |
8673 onecap_copy(word, word_up, TRUE); | |
8674 has_word_up = TRUE; | |
8675 } | |
8676 | |
351 | 8677 byts = slang->sl_pbyts; |
8678 idxs = slang->sl_pidxs; | |
8679 if (byts != NULL) /* array not is empty */ | |
8680 { | |
8681 /* | |
8682 * Loop over all prefixes, building them byte-by-byte in prefix[]. | |
390 | 8683 * When at the end of a prefix check that it supports "flags". |
351 | 8684 */ |
8685 depth = 0; | |
8686 arridx[0] = 0; | |
8687 curi[0] = 1; | |
8688 while (depth >= 0 && !got_int) | |
8689 { | |
390 | 8690 n = arridx[depth]; |
8691 len = byts[n]; | |
8692 if (curi[depth] > len) | |
351 | 8693 { |
8694 /* Done all bytes at this node, go up one level. */ | |
8695 --depth; | |
8696 line_breakcheck(); | |
8697 } | |
8698 else | |
8699 { | |
8700 /* Do one more byte at this node. */ | |
390 | 8701 n += curi[depth]; |
351 | 8702 ++curi[depth]; |
8703 c = byts[n]; | |
8704 if (c == 0) | |
8705 { | |
8706 /* End of prefix, find out how many IDs there are. */ | |
8707 for (i = 1; i < len; ++i) | |
8708 if (byts[n + i] != 0) | |
8709 break; | |
8710 curi[depth] += i - 1; | |
8711 | |
455 | 8712 c = valid_word_prefix(i, n, flags, word, slang, FALSE); |
8713 if (c != 0) | |
351 | 8714 { |
376 | 8715 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
701 | 8716 dump_word(slang, prefix, pat, dir, dumpflags, |
455 | 8717 (c & WF_RAREPFX) ? (flags | WF_RARE) |
701 | 8718 : flags, lnum); |
8719 if (lnum != 0) | |
8720 ++lnum; | |
351 | 8721 } |
455 | 8722 |
8723 /* Check for prefix that matches the word when the | |
8724 * first letter is upper-case, but only if the prefix has | |
8725 * a condition. */ | |
8726 if (has_word_up) | |
8727 { | |
8728 c = valid_word_prefix(i, n, flags, word_up, slang, | |
8729 TRUE); | |
8730 if (c != 0) | |
8731 { | |
8732 vim_strncpy(prefix + depth, word_up, | |
8733 MAXWLEN - depth - 1); | |
701 | 8734 dump_word(slang, prefix, pat, dir, dumpflags, |
455 | 8735 (c & WF_RAREPFX) ? (flags | WF_RARE) |
701 | 8736 : flags, lnum); |
8737 if (lnum != 0) | |
8738 ++lnum; | |
455 | 8739 } |
8740 } | |
351 | 8741 } |
8742 else | |
8743 { | |
8744 /* Normal char, go one level deeper. */ | |
8745 prefix[depth++] = c; | |
8746 arridx[depth] = idxs[n]; | |
8747 curi[depth] = 1; | |
8748 } | |
8749 } | |
8750 } | |
8751 } | |
8752 | |
8753 return lnum; | |
8754 } | |
8755 | |
498 | 8756 /* |
626 | 8757 * Move "p" to the end of word "start". |
8758 * Uses the spell-checking word characters. | |
498 | 8759 */ |
8760 char_u * | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8761 spell_to_word_end(char_u *start, win_T *win) |
498 | 8762 { |
8763 char_u *p = start; | |
8764 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8765 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
|
8766 MB_PTR_ADV(p); |
498 | 8767 return p; |
8768 } | |
8769 | |
475 | 8770 #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
8771 /* | |
626 | 8772 * For Insert mode completion CTRL-X s: |
8773 * Find start of the word in front of column "startcol". | |
8774 * We don't check if it is badly spelled, with completion we can only change | |
8775 * the word in front of the cursor. | |
475 | 8776 * Returns the column number of the word. |
8777 */ | |
8778 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8779 spell_word_start(int startcol) |
475 | 8780 { |
8781 char_u *line; | |
8782 char_u *p; | |
8783 int col = 0; | |
8784 | |
498 | 8785 if (no_spell_checking(curwin)) |
475 | 8786 return startcol; |
8787 | |
8788 /* Find a word character before "startcol". */ | |
8789 line = ml_get_curline(); | |
8790 for (p = line + startcol; p > line; ) | |
8791 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
8792 MB_PTR_BACK(line, p); |
5477 | 8793 if (spell_iswordp_nmw(p, curwin)) |
475 | 8794 break; |
8795 } | |
8796 | |
8797 /* Go back to start of the word. */ | |
8798 while (p > line) | |
8799 { | |
835 | 8800 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
|
8801 MB_PTR_BACK(line, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8802 if (!spell_iswordp(p, curwin)) |
475 | 8803 break; |
8804 col = 0; | |
8805 } | |
8806 | |
535 | 8807 return col; |
8808 } | |
8809 | |
8810 /* | |
8811 * Need to check for 'spellcapcheck' now, the word is removed before | |
8812 * expand_spelling() is called. Therefore the ugly global variable. | |
8813 */ | |
8814 static int spell_expand_need_cap; | |
8815 | |
8816 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8817 spell_expand_check_cap(colnr_T col) |
535 | 8818 { |
475 | 8819 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col); |
8820 } | |
8821 | |
8822 /* | |
8823 * Get list of spelling suggestions. | |
8824 * Used for Insert mode completion CTRL-X ?. | |
8825 * Returns the number of matches. The matches are in "matchp[]", array of | |
8826 * allocated strings. | |
8827 */ | |
8828 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8829 expand_spelling( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8830 linenr_T lnum UNUSED, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8831 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8832 char_u ***matchp) |
475 | 8833 { |
8834 garray_T ga; | |
8835 | |
625 | 8836 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE); |
475 | 8837 *matchp = ga.ga_data; |
8838 return ga.ga_len; | |
8839 } | |
8840 #endif | |
8841 | |
737 | 8842 #endif /* FEAT_SPELL */ |