Mercurial > vim
annotate src/spell.c @ 17940:079e10a49ea1 v8.1.1966
patch 8.1.1966: some code in options.c fits better elsewhere
Commit: https://github.com/vim/vim/commit/e677df8d93772a705f40a94f3c871aee78fe4d99
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Sep 2 22:31:11 2019 +0200
patch 8.1.1966: some code in options.c fits better elsewhere
Problem: Some code in options.c fits better elsewhere.
Solution: Move functions from options.c to other files. (Yegappan
Lakshmanan, closes #4889)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 02 Sep 2019 22:45:05 +0200 |
parents | 59f8948b7590 |
children | 6e53d83e021d |
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); |
17789
0f7ae8010787
patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents:
17653
diff
changeset
|
341 static int spell_iswordp_nmw(char_u *p, win_T *wp); |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
342 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
|
343 static void spell_find_suggest(char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive); |
372 | 344 #ifdef FEAT_EVAL |
7805
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
345 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
|
346 #endif |
0b6c37dd858d
commit https://github.com/vim/vim/commit/baaa7e9ec7398a813e21285c272fa99792642077
Christian Brabandt <cb@256bit.org>
parents:
7526
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 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
|
351 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
|
352 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
|
353 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
|
354 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
|
355 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
|
356 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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 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
|
366 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 |
236 | 382 |
626 | 383 /* Remember what "z?" replaced. */ |
384 static char_u *repl_from = NULL; | |
385 static char_u *repl_to = NULL; | |
386 | |
236 | 387 /* |
388 * Main spell-checking function. | |
300 | 389 * "ptr" points to a character that could be the start of a word. |
534 | 390 * "*attrp" is set to the highlight index for a badly spelled word. For a |
391 * non-word or when it's OK it remains unchanged. | |
236 | 392 * This must only be called when 'spelllang' is not empty. |
323 | 393 * |
385 | 394 * "capcol" is used to check for a Capitalised word after the end of a |
395 * sentence. If it's zero then perform the check. Return the column where to | |
396 * check next, or -1 when no sentence end was found. If it's NULL then don't | |
397 * worry. | |
323 | 398 * |
236 | 399 * Returns the length of the word in bytes, also when it's OK, so that the |
400 * caller can skip over the word. | |
401 */ | |
402 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
403 spell_check( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
404 win_T *wp, /* current window */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
405 char_u *ptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
406 hlf_T *attrp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
407 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
|
408 int docount) /* count good words */ |
236 | 409 { |
410 matchinf_T mi; /* Most things are put in "mi" so that it can | |
411 be passed to functions quickly. */ | |
344 | 412 int nrlen = 0; /* found a number first */ |
385 | 413 int c; |
483 | 414 int wrongcaplen = 0; |
500 | 415 int lpi; |
625 | 416 int count_word = docount; |
236 | 417 |
307 | 418 /* A word never starts at a space or a control character. Return quickly |
419 * then, skipping over the character. */ | |
420 if (*ptr <= ' ') | |
421 return 1; | |
690 | 422 |
423 /* 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
|
424 if (wp->w_s->b_langp.ga_len == 0) |
690 | 425 return 1; |
426 | |
483 | 427 vim_memset(&mi, 0, sizeof(matchinf_T)); |
236 | 428 |
344 | 429 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and |
584 | 430 * 0X99FF. But always do check spelling to find "3GPP" and "11 |
431 * julifeest". */ | |
300 | 432 if (*ptr >= '0' && *ptr <= '9') |
433 { | |
7447
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
434 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
|
435 mi.mi_end = skipbin(ptr + 2); |
ad432f8f68fb
commit https://github.com/vim/vim/commit/887c1fea4a114e7170091942d0446c8882701b5b
Christian Brabandt <cb@256bit.org>
parents:
6949
diff
changeset
|
436 else if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) |
316 | 437 mi.mi_end = skiphex(ptr + 2); |
300 | 438 else |
344 | 439 mi.mi_end = skipdigits(ptr); |
835 | 440 nrlen = (int)(mi.mi_end - ptr); |
584 | 441 } |
346 | 442 |
443 /* Find the normal end of the word (until the next non-word character). */ | |
344 | 444 mi.mi_word = ptr; |
584 | 445 mi.mi_fend = ptr; |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
446 if (spell_iswordp(mi.mi_fend, wp)) |
344 | 447 { |
448 do | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
449 MB_PTR_ADV(mi.mi_fend); |
16162
cd5c83115ec6
patch 8.1.1086: too many curly braces
Bram Moolenaar <Bram@vim.org>
parents:
16142
diff
changeset
|
450 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
|
451 |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
452 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL) |
385 | 453 { |
454 /* Check word starting with capital letter. */ | |
455 | 455 c = PTR2CHAR(ptr); |
385 | 456 if (!SPELL_ISUPPER(c)) |
483 | 457 wrongcaplen = (int)(mi.mi_fend - ptr); |
385 | 458 } |
459 } | |
460 if (capcol != NULL) | |
461 *capcol = -1; | |
344 | 462 |
463 /* We always use the characters up to the next non-word character, | |
464 * also for bad words. */ | |
465 mi.mi_end = mi.mi_fend; | |
466 | |
467 /* Check caps type later. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
468 mi.mi_capflags = 0; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
469 mi.mi_cend = NULL; |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
470 mi.mi_win = wp; |
344 | 471 |
483 | 472 /* case-fold the word with one non-word character, so that we can check |
473 * for the word end. */ | |
344 | 474 if (*mi.mi_fend != NUL) |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
475 MB_PTR_ADV(mi.mi_fend); |
344 | 476 |
477 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword, | |
478 MAXWLEN + 1); | |
835 | 479 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword); |
344 | 480 |
481 /* The word is bad unless we recognize it. */ | |
482 mi.mi_result = SP_BAD; | |
492 | 483 mi.mi_result2 = SP_BAD; |
344 | 484 |
485 /* | |
486 * Loop over the languages specified in 'spelllang'. | |
625 | 487 * We check them all, because a word may be matched longer in another |
488 * language. | |
344 | 489 */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
490 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
491 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
492 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi); |
500 | 493 |
494 /* If reloading fails the language is still in the list but everything | |
495 * has been cleared. */ | |
496 if (mi.mi_lp->lp_slang->sl_fidxs == NULL) | |
497 continue; | |
498 | |
344 | 499 /* Check for a matching word in case-folded words. */ |
500 find_word(&mi, FIND_FOLDWORD); | |
501 | |
502 /* Check for a matching word in keep-case words. */ | |
503 find_word(&mi, FIND_KEEPWORD); | |
504 | |
505 /* Check for matching prefixes. */ | |
485 | 506 find_prefix(&mi, FIND_FOLDWORD); |
492 | 507 |
508 /* For a NOBREAK language, may want to use a word without a following | |
509 * word as a backup. */ | |
510 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD | |
511 && mi.mi_result2 != SP_BAD) | |
512 { | |
513 mi.mi_result = mi.mi_result2; | |
514 mi.mi_end = mi.mi_end2; | |
515 } | |
625 | 516 |
517 /* Count the word in the first language where it's found to be OK. */ | |
518 if (count_word && mi.mi_result == SP_OK) | |
519 { | |
520 count_common_word(mi.mi_lp->lp_slang, ptr, | |
521 (int)(mi.mi_end - ptr), 1); | |
522 count_word = FALSE; | |
523 } | |
344 | 524 } |
525 | |
526 if (mi.mi_result != SP_OK) | |
527 { | |
346 | 528 /* If we found a number skip over it. Allows for "42nd". Do flag |
529 * rare and local words, e.g., "3GPP". */ | |
344 | 530 if (nrlen > 0) |
346 | 531 { |
532 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) | |
533 return nrlen; | |
534 } | |
344 | 535 |
536 /* When we are at a non-word character there is no error, just | |
537 * skip over the character (try looking for a word after it). */ | |
5477 | 538 else if (!spell_iswordp_nmw(ptr, wp)) |
243 | 539 { |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
540 if (capcol != NULL && wp->w_s->b_cap_prog != NULL) |
385 | 541 { |
542 regmatch_T regmatch; | |
6375 | 543 int r; |
385 | 544 |
545 /* Check for end of sentence. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
546 regmatch.regprog = wp->w_s->b_cap_prog; |
385 | 547 regmatch.rm_ic = FALSE; |
6375 | 548 r = vim_regexec(®match, ptr, 0); |
549 wp->w_s->b_cap_prog = regmatch.regprog; | |
550 if (r) | |
385 | 551 *capcol = (int)(regmatch.endp[0] - ptr); |
552 } | |
553 | |
344 | 554 if (has_mbyte) |
474 | 555 return (*mb_ptr2len)(ptr); |
344 | 556 return 1; |
300 | 557 } |
483 | 558 else if (mi.mi_end == ptr) |
559 /* Always include at least one character. Required for when there | |
560 * is a mixup in "midword". */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
561 MB_PTR_ADV(mi.mi_end); |
492 | 562 else if (mi.mi_result == SP_BAD |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
563 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak) |
492 | 564 { |
565 char_u *p, *fp; | |
566 int save_result = mi.mi_result; | |
567 | |
568 /* First language in 'spelllang' is NOBREAK. Find first position | |
569 * at which any word would be valid. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
570 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0); |
500 | 571 if (mi.mi_lp->lp_slang->sl_fidxs != NULL) |
572 { | |
573 p = mi.mi_word; | |
574 fp = mi.mi_fword; | |
575 for (;;) | |
576 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
577 MB_PTR_ADV(p); |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
578 MB_PTR_ADV(fp); |
500 | 579 if (p >= mi.mi_end) |
580 break; | |
835 | 581 mi.mi_compoff = (int)(fp - mi.mi_fword); |
500 | 582 find_word(&mi, FIND_COMPOUND); |
583 if (mi.mi_result != SP_BAD) | |
584 { | |
585 mi.mi_end = p; | |
586 break; | |
587 } | |
588 } | |
589 mi.mi_result = save_result; | |
590 } | |
492 | 591 } |
243 | 592 |
344 | 593 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED) |
534 | 594 *attrp = HLF_SPB; |
344 | 595 else if (mi.mi_result == SP_RARE) |
534 | 596 *attrp = HLF_SPR; |
344 | 597 else |
534 | 598 *attrp = HLF_SPL; |
243 | 599 } |
600 | |
483 | 601 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE)) |
602 { | |
603 /* Report SpellCap only when the word isn't badly spelled. */ | |
534 | 604 *attrp = HLF_SPC; |
483 | 605 return wrongcaplen; |
606 } | |
607 | |
300 | 608 return (int)(mi.mi_end - ptr); |
236 | 609 } |
610 | |
611 /* | |
300 | 612 * Check if the word at "mip->mi_word" is in the tree. |
339 | 613 * When "mode" is FIND_FOLDWORD check in fold-case word tree. |
614 * When "mode" is FIND_KEEPWORD check in keep-case word tree. | |
615 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word | |
616 * tree. | |
300 | 617 * |
618 * For a match mip->mi_result is updated. | |
243 | 619 */ |
620 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
621 find_word(matchinf_T *mip, int mode) |
243 | 622 { |
324 | 623 idx_T arridx = 0; |
300 | 624 int endlen[MAXWLEN]; /* length at possible word endings */ |
324 | 625 idx_T endidx[MAXWLEN]; /* possible word endings */ |
300 | 626 int endidxcnt = 0; |
627 int len; | |
628 int wlen = 0; | |
629 int flen; | |
630 int c; | |
631 char_u *ptr; | |
324 | 632 idx_T lo, hi, m; |
300 | 633 char_u *s; |
339 | 634 char_u *p; |
307 | 635 int res = SP_BAD; |
300 | 636 slang_T *slang = mip->mi_lp->lp_slang; |
637 unsigned flags; | |
638 char_u *byts; | |
324 | 639 idx_T *idxs; |
481 | 640 int word_ends; |
485 | 641 int prefix_found; |
492 | 642 int nobreak_result; |
481 | 643 |
644 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND) | |
236 | 645 { |
300 | 646 /* Check for word with matching case in keep-case tree. */ |
647 ptr = mip->mi_word; | |
648 flen = 9999; /* no case folding, always enough bytes */ | |
649 byts = slang->sl_kbyts; | |
650 idxs = slang->sl_kidxs; | |
481 | 651 |
652 if (mode == FIND_KEEPCOMPOUND) | |
653 /* Skip over the previously found word(s). */ | |
654 wlen += mip->mi_compoff; | |
236 | 655 } |
656 else | |
657 { | |
300 | 658 /* Check for case-folded in case-folded tree. */ |
659 ptr = mip->mi_fword; | |
660 flen = mip->mi_fwordlen; /* available case-folded bytes */ | |
661 byts = slang->sl_fbyts; | |
662 idxs = slang->sl_fidxs; | |
339 | 663 |
664 if (mode == FIND_PREFIX) | |
665 { | |
666 /* Skip over the prefix. */ | |
667 wlen = mip->mi_prefixlen; | |
668 flen -= mip->mi_prefixlen; | |
669 } | |
481 | 670 else if (mode == FIND_COMPOUND) |
671 { | |
672 /* Skip over the previously found word(s). */ | |
673 wlen = mip->mi_compoff; | |
674 flen -= mip->mi_compoff; | |
675 } | |
676 | |
243 | 677 } |
678 | |
300 | 679 if (byts == NULL) |
680 return; /* array is empty */ | |
236 | 681 |
682 /* | |
307 | 683 * Repeat advancing in the tree until: |
684 * - there is a byte that doesn't match, | |
685 * - we reach the end of the tree, | |
686 * - or we reach the end of the line. | |
236 | 687 */ |
300 | 688 for (;;) |
236 | 689 { |
346 | 690 if (flen <= 0 && *mip->mi_fend != NUL) |
339 | 691 flen = fold_more(mip); |
300 | 692 |
693 len = byts[arridx++]; | |
694 | |
695 /* If the first possible byte is a zero the word could end here. | |
696 * Remember this index, we first check for the longest word. */ | |
697 if (byts[arridx] == 0) | |
698 { | |
307 | 699 if (endidxcnt == MAXWLEN) |
700 { | |
701 /* Must be a corrupted spell file. */ | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
702 emsg(_(e_format)); |
307 | 703 return; |
704 } | |
300 | 705 endlen[endidxcnt] = wlen; |
706 endidx[endidxcnt++] = arridx++; | |
707 --len; | |
708 | |
709 /* Skip over the zeros, there can be several flag/region | |
710 * combinations. */ | |
711 while (len > 0 && byts[arridx] == 0) | |
712 { | |
713 ++arridx; | |
714 --len; | |
715 } | |
716 if (len == 0) | |
717 break; /* no children, word must end here */ | |
718 } | |
719 | |
720 /* Stop looking at end of the line. */ | |
721 if (ptr[wlen] == NUL) | |
722 break; | |
723 | |
724 /* Perform a binary search in the list of accepted bytes. */ | |
725 c = ptr[wlen]; | |
346 | 726 if (c == TAB) /* <Tab> is handled like <Space> */ |
727 c = ' '; | |
300 | 728 lo = arridx; |
729 hi = arridx + len - 1; | |
730 while (lo < hi) | |
731 { | |
732 m = (lo + hi) / 2; | |
733 if (byts[m] > c) | |
734 hi = m - 1; | |
735 else if (byts[m] < c) | |
736 lo = m + 1; | |
737 else | |
738 { | |
739 lo = hi = m; | |
740 break; | |
236 | 741 } |
742 } | |
300 | 743 |
744 /* Stop if there is no matching byte. */ | |
745 if (hi < lo || byts[lo] != c) | |
746 break; | |
747 | |
748 /* Continue at the child (if there is one). */ | |
749 arridx = idxs[lo]; | |
750 ++wlen; | |
751 --flen; | |
346 | 752 |
753 /* One space in the good word may stand for several spaces in the | |
754 * checked word. */ | |
755 if (c == ' ') | |
756 { | |
757 for (;;) | |
758 { | |
759 if (flen <= 0 && *mip->mi_fend != NUL) | |
760 flen = fold_more(mip); | |
761 if (ptr[wlen] != ' ' && ptr[wlen] != TAB) | |
762 break; | |
763 ++wlen; | |
764 --flen; | |
765 } | |
766 } | |
236 | 767 } |
768 | |
300 | 769 /* |
770 * Verify that one of the possible endings is valid. Try the longest | |
771 * first. | |
772 */ | |
773 while (endidxcnt > 0) | |
774 { | |
775 --endidxcnt; | |
776 arridx = endidx[endidxcnt]; | |
777 wlen = endlen[endidxcnt]; | |
236 | 778 |
300 | 779 if ((*mb_head_off)(ptr, ptr + wlen) > 0) |
780 continue; /* not at first byte of character */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
781 if (spell_iswordp(ptr + wlen, mip->mi_win)) |
481 | 782 { |
492 | 783 if (slang->sl_compprog == NULL && !slang->sl_nobreak) |
481 | 784 continue; /* next char is a word character */ |
785 word_ends = FALSE; | |
786 } | |
787 else | |
788 word_ends = TRUE; | |
485 | 789 /* The prefix flag is before compound flags. Once a valid prefix flag |
790 * has been found we try compound flags. */ | |
791 prefix_found = FALSE; | |
300 | 792 |
339 | 793 if (mode != FIND_KEEPWORD && has_mbyte) |
300 | 794 { |
795 /* Compute byte length in original word, length may change | |
339 | 796 * when folding case. This can be slow, take a shortcut when the |
797 * case-folded word is equal to the keep-case word. */ | |
300 | 798 p = mip->mi_word; |
339 | 799 if (STRNCMP(ptr, p, wlen) != 0) |
800 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
801 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s)) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
802 MB_PTR_ADV(p); |
835 | 803 wlen = (int)(p - mip->mi_word); |
339 | 804 } |
300 | 805 } |
236 | 806 |
339 | 807 /* Check flags and region. For FIND_PREFIX check the condition and |
808 * prefix ID. | |
809 * Repeat this if there are more flags/region alternatives until there | |
810 * is a match. */ | |
811 res = SP_BAD; | |
812 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; | |
813 --len, ++arridx) | |
300 | 814 { |
815 flags = idxs[arridx]; | |
324 | 816 |
339 | 817 /* For the fold-case tree check that the case of the checked word |
818 * matches with what the word in the tree requires. | |
819 * For keep-case tree the case is always right. For prefixes we | |
820 * don't bother to check. */ | |
821 if (mode == FIND_FOLDWORD) | |
300 | 822 { |
823 if (mip->mi_cend != mip->mi_word + wlen) | |
824 { | |
323 | 825 /* mi_capflags was set for a different word length, need |
826 * to do it again. */ | |
300 | 827 mip->mi_cend = mip->mi_word + wlen; |
323 | 828 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend); |
300 | 829 } |
830 | |
346 | 831 if (mip->mi_capflags == WF_KEEPCAP |
832 || !spell_valid_case(mip->mi_capflags, flags)) | |
339 | 833 continue; |
300 | 834 } |
236 | 835 |
339 | 836 /* When mode is FIND_PREFIX the word must support the prefix: |
837 * check the prefix ID and the condition. Do that for the list at | |
366 | 838 * mip->mi_prefarridx that find_prefix() filled. */ |
485 | 839 else if (mode == FIND_PREFIX && !prefix_found) |
481 | 840 { |
366 | 841 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx, |
390 | 842 flags, |
455 | 843 mip->mi_word + mip->mi_cprefixlen, slang, |
844 FALSE); | |
366 | 845 if (c == 0) |
339 | 846 continue; |
366 | 847 |
848 /* Use the WF_RARE flag for a rare prefix. */ | |
849 if (c & WF_RAREPFX) | |
850 flags |= WF_RARE; | |
485 | 851 prefix_found = TRUE; |
339 | 852 } |
853 | |
492 | 854 if (slang->sl_nobreak) |
855 { | |
856 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND) | |
857 && (flags & WF_BANNED) == 0) | |
858 { | |
859 /* NOBREAK: found a valid following word. That's all we | |
860 * need to know, so return. */ | |
861 mip->mi_result = SP_OK; | |
862 break; | |
863 } | |
864 } | |
865 | |
866 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND | |
867 || !word_ends)) | |
481 | 868 { |
974 | 869 /* If there is no compound flag or the word is shorter than |
483 | 870 * COMPOUNDMIN reject it quickly. |
871 * Makes you wonder why someone puts a compound flag on a word | |
481 | 872 * that's too short... Myspell compatibility requires this |
873 * anyway. */ | |
490 | 874 if (((unsigned)flags >> 24) == 0 |
875 || wlen - mip->mi_compoff < slang->sl_compminlen) | |
483 | 876 continue; |
500 | 877 /* For multi-byte chars check character length against |
878 * COMPOUNDMIN. */ | |
879 if (has_mbyte | |
501 | 880 && slang->sl_compminlen > 0 |
500 | 881 && mb_charlen_len(mip->mi_word + mip->mi_compoff, |
882 wlen - mip->mi_compoff) < slang->sl_compminlen) | |
883 continue; | |
483 | 884 |
809 | 885 /* Limit the number of compound words to COMPOUNDWORDMAX if no |
490 | 886 * maximum for syllables is specified. */ |
809 | 887 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2 |
888 > slang->sl_compmax | |
490 | 889 && slang->sl_compsylmax == MAXWLEN) |
483 | 890 continue; |
891 | |
819 | 892 /* Don't allow compounding on a side where an affix was added, |
893 * unless COMPOUNDPERMITFLAG was used. */ | |
894 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF)) | |
895 continue; | |
896 if (!word_ends && (flags & WF_NOCOMPAFT)) | |
897 continue; | |
898 | |
485 | 899 /* Quickly check if compounding is possible with this flag. */ |
495 | 900 if (!byte_in_str(mip->mi_complen == 0 |
485 | 901 ? slang->sl_compstartflags |
902 : slang->sl_compallflags, | |
495 | 903 ((unsigned)flags >> 24))) |
481 | 904 continue; |
905 | |
1762 | 906 /* If there is a match with a CHECKCOMPOUNDPATTERN rule |
907 * discard the compound word. */ | |
908 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat)) | |
909 continue; | |
910 | |
490 | 911 if (mode == FIND_COMPOUND) |
912 { | |
913 int capflags; | |
914 | |
915 /* Need to check the caps type of the appended compound | |
916 * word. */ | |
917 if (has_mbyte && STRNCMP(ptr, mip->mi_word, | |
918 mip->mi_compoff) != 0) | |
919 { | |
920 /* case folding may have changed the length */ | |
921 p = mip->mi_word; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
922 for (s = ptr; s < ptr + mip->mi_compoff; MB_PTR_ADV(s)) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
923 MB_PTR_ADV(p); |
490 | 924 } |
925 else | |
926 p = mip->mi_word + mip->mi_compoff; | |
927 capflags = captype(p, mip->mi_word + wlen); | |
928 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP | |
929 && (flags & WF_FIXCAP) != 0)) | |
930 continue; | |
931 | |
932 if (capflags != WF_ALLCAP) | |
933 { | |
934 /* When the character before the word is a word | |
935 * character we do not accept a Onecap word. We do | |
936 * accept a no-caps word, even when the dictionary | |
937 * word specifies ONECAP. */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
938 MB_PTR_BACK(mip->mi_word, p); |
5477 | 939 if (spell_iswordp_nmw(p, mip->mi_win) |
490 | 940 ? capflags == WF_ONECAP |
941 : (flags & WF_ONECAP) != 0 | |
942 && capflags != WF_ONECAP) | |
943 continue; | |
944 } | |
945 } | |
946 | |
483 | 947 /* If the word ends the sequence of compound flags of the |
714 | 948 * words must match with one of the COMPOUNDRULE items and |
483 | 949 * the number of syllables must not be too large. */ |
950 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24); | |
951 mip->mi_compflags[mip->mi_complen + 1] = NUL; | |
952 if (word_ends) | |
953 { | |
954 char_u fword[MAXWLEN]; | |
955 | |
956 if (slang->sl_compsylmax < MAXWLEN) | |
957 { | |
958 /* "fword" is only needed for checking syllables. */ | |
959 if (ptr == mip->mi_word) | |
960 (void)spell_casefold(ptr, wlen, fword, MAXWLEN); | |
961 else | |
962 vim_strncpy(fword, ptr, endlen[endidxcnt]); | |
963 } | |
964 if (!can_compound(slang, fword, mip->mi_compflags)) | |
965 continue; | |
966 } | |
1762 | 967 else if (slang->sl_comprules != NULL |
968 && !match_compoundrule(slang, mip->mi_compflags)) | |
969 /* The compound flags collected so far do not match any | |
970 * COMPOUNDRULE, discard the compounded word. */ | |
971 continue; | |
481 | 972 } |
973 | |
500 | 974 /* Check NEEDCOMPOUND: can't use word without compounding. */ |
975 else if (flags & WF_NEEDCOMP) | |
976 continue; | |
977 | |
492 | 978 nobreak_result = SP_OK; |
979 | |
481 | 980 if (!word_ends) |
981 { | |
492 | 982 int save_result = mip->mi_result; |
983 char_u *save_end = mip->mi_end; | |
501 | 984 langp_T *save_lp = mip->mi_lp; |
985 int lpi; | |
492 | 986 |
987 /* Check that a valid word follows. If there is one and we | |
988 * are compounding, it will set "mi_result", thus we are | |
989 * always finished here. For NOBREAK we only check that a | |
990 * valid word follows. | |
481 | 991 * Recursive! */ |
492 | 992 if (slang->sl_nobreak) |
993 mip->mi_result = SP_BAD; | |
481 | 994 |
995 /* Find following word in case-folded tree. */ | |
996 mip->mi_compoff = endlen[endidxcnt]; | |
997 if (has_mbyte && mode == FIND_KEEPWORD) | |
998 { | |
999 /* Compute byte length in case-folded word from "wlen": | |
1000 * byte length in keep-case word. Length may change when | |
1001 * folding case. This can be slow, take a shortcut when | |
1002 * the case-folded word is equal to the keep-case word. */ | |
1003 p = mip->mi_fword; | |
1004 if (STRNCMP(ptr, p, wlen) != 0) | |
1005 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1006 for (s = ptr; s < ptr + wlen; MB_PTR_ADV(s)) |
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1007 MB_PTR_ADV(p); |
835 | 1008 mip->mi_compoff = (int)(p - mip->mi_fword); |
481 | 1009 } |
1010 } | |
8956
d9e671c5afe6
commit https://github.com/vim/vim/commit/ba53435144f46eaaa53c63a62e748b3feee9742c
Christian Brabandt <cb@256bit.org>
parents:
8524
diff
changeset
|
1011 #if 0 /* Disabled, see below */ |
485 | 1012 c = mip->mi_compoff; |
8956
d9e671c5afe6
commit https://github.com/vim/vim/commit/ba53435144f46eaaa53c63a62e748b3feee9742c
Christian Brabandt <cb@256bit.org>
parents:
8524
diff
changeset
|
1013 #endif |
483 | 1014 ++mip->mi_complen; |
809 | 1015 if (flags & WF_COMPROOT) |
1016 ++mip->mi_compextra; | |
501 | 1017 |
1018 /* For NOBREAK we need to try all NOBREAK languages, at least | |
1019 * to find the ".add" file(s). */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1020 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi) |
501 | 1021 { |
1022 if (slang->sl_nobreak) | |
1023 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1024 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi); |
501 | 1025 if (mip->mi_lp->lp_slang->sl_fidxs == NULL |
1026 || !mip->mi_lp->lp_slang->sl_nobreak) | |
1027 continue; | |
1028 } | |
1029 | |
1030 find_word(mip, FIND_COMPOUND); | |
1031 | |
1032 /* When NOBREAK any word that matches is OK. Otherwise we | |
1033 * need to find the longest match, thus try with keep-case | |
1034 * and prefix too. */ | |
492 | 1035 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
1036 { | |
501 | 1037 /* Find following word in keep-case tree. */ |
1038 mip->mi_compoff = wlen; | |
1039 find_word(mip, FIND_KEEPCOMPOUND); | |
1040 | |
819 | 1041 #if 0 /* Disabled, a prefix must not appear halfway a compound word, |
1042 unless the COMPOUNDPERMITFLAG is used and then it can't be a | |
1043 postponed prefix. */ | |
501 | 1044 if (!slang->sl_nobreak || mip->mi_result == SP_BAD) |
1045 { | |
1046 /* Check for following word with prefix. */ | |
1047 mip->mi_compoff = c; | |
1048 find_prefix(mip, FIND_COMPOUND); | |
1049 } | |
819 | 1050 #endif |
492 | 1051 } |
501 | 1052 |
1053 if (!slang->sl_nobreak) | |
1054 break; | |
492 | 1055 } |
483 | 1056 --mip->mi_complen; |
809 | 1057 if (flags & WF_COMPROOT) |
1058 --mip->mi_compextra; | |
501 | 1059 mip->mi_lp = save_lp; |
485 | 1060 |
492 | 1061 if (slang->sl_nobreak) |
1062 { | |
1063 nobreak_result = mip->mi_result; | |
1064 mip->mi_result = save_result; | |
1065 mip->mi_end = save_end; | |
1066 } | |
1067 else | |
1068 { | |
1069 if (mip->mi_result == SP_OK) | |
1070 break; | |
1071 continue; | |
1072 } | |
481 | 1073 } |
1074 | |
339 | 1075 if (flags & WF_BANNED) |
1076 res = SP_BANNED; | |
1077 else if (flags & WF_REGION) | |
1078 { | |
1079 /* Check region. */ | |
390 | 1080 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0) |
300 | 1081 res = SP_OK; |
339 | 1082 else |
1083 res = SP_LOCAL; | |
300 | 1084 } |
339 | 1085 else if (flags & WF_RARE) |
1086 res = SP_RARE; | |
307 | 1087 else |
339 | 1088 res = SP_OK; |
1089 | |
492 | 1090 /* Always use the longest match and the best result. For NOBREAK |
1091 * we separately keep the longest match without a following good | |
1092 * word as a fall-back. */ | |
1093 if (nobreak_result == SP_BAD) | |
1094 { | |
1095 if (mip->mi_result2 > res) | |
1096 { | |
1097 mip->mi_result2 = res; | |
1098 mip->mi_end2 = mip->mi_word + wlen; | |
1099 } | |
1100 else if (mip->mi_result2 == res | |
1101 && mip->mi_end2 < mip->mi_word + wlen) | |
1102 mip->mi_end2 = mip->mi_word + wlen; | |
1103 } | |
1104 else if (mip->mi_result > res) | |
339 | 1105 { |
1106 mip->mi_result = res; | |
1107 mip->mi_end = mip->mi_word + wlen; | |
1108 } | |
351 | 1109 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen) |
339 | 1110 mip->mi_end = mip->mi_word + wlen; |
1111 | |
492 | 1112 if (mip->mi_result == SP_OK) |
339 | 1113 break; |
300 | 1114 } |
1115 | |
492 | 1116 if (mip->mi_result == SP_OK) |
300 | 1117 break; |
1118 } | |
236 | 1119 } |
1120 | |
323 | 1121 /* |
1762 | 1122 * Return TRUE if there is a match between the word ptr[wlen] and |
1123 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another | |
1124 * word. | |
1125 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the | |
1126 * end of ptr[wlen] and the second part matches after it. | |
1127 */ | |
1128 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1129 match_checkcompoundpattern( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1130 char_u *ptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1131 int wlen, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1132 garray_T *gap) /* &sl_comppat */ |
1762 | 1133 { |
1134 int i; | |
1135 char_u *p; | |
1136 int len; | |
1137 | |
1138 for (i = 0; i + 1 < gap->ga_len; i += 2) | |
1139 { | |
1140 p = ((char_u **)gap->ga_data)[i + 1]; | |
1141 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0) | |
1142 { | |
1143 /* Second part matches at start of following compound word, now | |
1144 * check if first part matches at end of previous word. */ | |
1145 p = ((char_u **)gap->ga_data)[i]; | |
1771 | 1146 len = (int)STRLEN(p); |
1762 | 1147 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0) |
1148 return TRUE; | |
1149 } | |
1150 } | |
1151 return FALSE; | |
1152 } | |
1153 | |
1154 /* | |
626 | 1155 * Return TRUE if "flags" is a valid sequence of compound flags and "word" |
1156 * does not have too many syllables. | |
482 | 1157 */ |
1158 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1159 can_compound(slang_T *slang, char_u *word, char_u *flags) |
483 | 1160 { |
495 | 1161 char_u uflags[MAXWLEN * 2]; |
1162 int i; | |
1163 char_u *p; | |
483 | 1164 |
1165 if (slang->sl_compprog == NULL) | |
1166 return FALSE; | |
495 | 1167 if (enc_utf8) |
1168 { | |
1169 /* Need to convert the single byte flags to utf8 characters. */ | |
1170 p = uflags; | |
1171 for (i = 0; flags[i] != NUL; ++i) | |
11269
121d29004998
patch 8.0.0520: using a function pointer while the function is known
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
1172 p += utf_char2bytes(flags[i], p); |
495 | 1173 *p = NUL; |
1174 p = uflags; | |
1175 } | |
1176 else | |
1177 p = flags; | |
6375 | 1178 if (!vim_regexec_prog(&slang->sl_compprog, FALSE, p, 0)) |
483 | 1179 return FALSE; |
1180 | |
490 | 1181 /* Count the number of syllables. This may be slow, do it last. If there |
1182 * are too many syllables AND the number of compound words is above | |
809 | 1183 * COMPOUNDWORDMAX then compounding is not allowed. */ |
483 | 1184 if (slang->sl_compsylmax < MAXWLEN |
1185 && count_syllables(slang, word) > slang->sl_compsylmax) | |
495 | 1186 return (int)STRLEN(flags) < slang->sl_compmax; |
483 | 1187 return TRUE; |
482 | 1188 } |
1189 | |
1190 /* | |
1762 | 1191 * Return TRUE when the sequence of flags in "compflags" plus "flag" can |
1192 * possibly form a valid compounded word. This also checks the COMPOUNDRULE | |
1193 * lines if they don't contain wildcards. | |
1194 */ | |
1195 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1196 can_be_compound( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1197 trystate_T *sp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1198 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1199 char_u *compflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1200 int flag) |
1762 | 1201 { |
1202 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags | |
1203 * then it can't possibly compound. */ | |
1204 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit | |
1205 ? slang->sl_compstartflags : slang->sl_compallflags, flag)) | |
1206 return FALSE; | |
1207 | |
1208 /* If there are no wildcards, we can check if the flags collected so far | |
1209 * possibly can form a match with COMPOUNDRULE patterns. This only | |
1210 * makes sense when we have two or more words. */ | |
1211 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit) | |
1212 { | |
1213 int v; | |
1214 | |
1215 compflags[sp->ts_complen] = flag; | |
1216 compflags[sp->ts_complen + 1] = NUL; | |
1217 v = match_compoundrule(slang, compflags + sp->ts_compsplit); | |
1218 compflags[sp->ts_complen] = NUL; | |
1219 return v; | |
1220 } | |
1221 | |
1222 return TRUE; | |
1223 } | |
1224 | |
1225 | |
1226 /* | |
1227 * Return TRUE if the compound flags in compflags[] match the start of any | |
1228 * compound rule. This is used to stop trying a compound if the flags | |
1229 * collected so far can't possibly match any compound rule. | |
1230 * Caller must check that slang->sl_comprules is not NULL. | |
1231 */ | |
1232 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1233 match_compoundrule(slang_T *slang, char_u *compflags) |
1762 | 1234 { |
1235 char_u *p; | |
1236 int i; | |
1237 int c; | |
1238 | |
1239 /* loop over all the COMPOUNDRULE entries */ | |
1240 for (p = slang->sl_comprules; *p != NUL; ++p) | |
1241 { | |
1242 /* loop over the flags in the compound word we have made, match | |
1243 * them against the current rule entry */ | |
1244 for (i = 0; ; ++i) | |
1245 { | |
1246 c = compflags[i]; | |
1247 if (c == NUL) | |
1248 /* found a rule that matches for the flags we have so far */ | |
1249 return TRUE; | |
1250 if (*p == '/' || *p == NUL) | |
1251 break; /* end of rule, it's too short */ | |
1252 if (*p == '[') | |
1253 { | |
1254 int match = FALSE; | |
1255 | |
1256 /* compare against all the flags in [] */ | |
1257 ++p; | |
1258 while (*p != ']' && *p != NUL) | |
1259 if (*p++ == c) | |
1260 match = TRUE; | |
1261 if (!match) | |
1262 break; /* none matches */ | |
1263 } | |
1264 else if (*p != c) | |
1265 break; /* flag of word doesn't match flag in pattern */ | |
1266 ++p; | |
1267 } | |
1268 | |
1269 /* Skip to the next "/", where the next pattern starts. */ | |
1270 p = vim_strchr(p, '/'); | |
1271 if (p == NULL) | |
1272 break; | |
1273 } | |
1274 | |
1275 /* Checked all the rules and none of them match the flags, so there | |
1276 * can't possibly be a compound starting with these flags. */ | |
1277 return FALSE; | |
1278 } | |
1279 | |
1280 /* | |
390 | 1281 * Return non-zero if the prefix indicated by "arridx" matches with the prefix |
1282 * ID in "flags" for the word "word". | |
366 | 1283 * The WF_RAREPFX flag is included in the return value for a rare prefix. |
351 | 1284 */ |
1285 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1286 valid_word_prefix( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1287 int totprefcnt, /* nr of prefix IDs */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1288 int arridx, /* idx in sl_pidxs[] */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1289 int flags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1290 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1291 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1292 int cond_req) /* only use prefixes with a condition */ |
351 | 1293 { |
1294 int prefcnt; | |
1295 int pidx; | |
6375 | 1296 regprog_T **rp; |
390 | 1297 int prefid; |
1298 | |
1299 prefid = (unsigned)flags >> 24; | |
351 | 1300 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt) |
1301 { | |
1302 pidx = slang->sl_pidxs[arridx + prefcnt]; | |
1303 | |
1304 /* Check the prefix ID. */ | |
1305 if (prefid != (pidx & 0xff)) | |
1306 continue; | |
1307 | |
390 | 1308 /* Check if the prefix doesn't combine and the word already has a |
1309 * suffix. */ | |
1310 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC)) | |
1311 continue; | |
1312 | |
351 | 1313 /* Check the condition, if there is one. The condition index is |
366 | 1314 * stored in the two bytes above the prefix ID byte. */ |
6375 | 1315 rp = &slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff]; |
1316 if (*rp != NULL) | |
1317 { | |
1318 if (!vim_regexec_prog(rp, FALSE, word, 0)) | |
351 | 1319 continue; |
1320 } | |
455 | 1321 else if (cond_req) |
1322 continue; | |
1323 | |
1324 /* It's a match! Return the WF_ flags. */ | |
366 | 1325 return pidx; |
1326 } | |
1327 return 0; | |
351 | 1328 } |
1329 | |
1330 /* | |
339 | 1331 * Check if the word at "mip->mi_word" has a matching prefix. |
1332 * If it does, then check the following word. | |
1333 * | |
485 | 1334 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a |
1335 * prefix in a compound word. | |
1336 * | |
339 | 1337 * For a match mip->mi_result is updated. |
1338 */ | |
1339 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1340 find_prefix(matchinf_T *mip, int mode) |
339 | 1341 { |
1342 idx_T arridx = 0; | |
1343 int len; | |
1344 int wlen = 0; | |
1345 int flen; | |
1346 int c; | |
1347 char_u *ptr; | |
1348 idx_T lo, hi, m; | |
1349 slang_T *slang = mip->mi_lp->lp_slang; | |
1350 char_u *byts; | |
1351 idx_T *idxs; | |
1352 | |
375 | 1353 byts = slang->sl_pbyts; |
1354 if (byts == NULL) | |
1355 return; /* array is empty */ | |
1356 | |
339 | 1357 /* We use the case-folded word here, since prefixes are always |
1358 * case-folded. */ | |
1359 ptr = mip->mi_fword; | |
1360 flen = mip->mi_fwordlen; /* available case-folded bytes */ | |
485 | 1361 if (mode == FIND_COMPOUND) |
1362 { | |
1363 /* Skip over the previously found word(s). */ | |
1364 ptr += mip->mi_compoff; | |
1365 flen -= mip->mi_compoff; | |
1366 } | |
339 | 1367 idxs = slang->sl_pidxs; |
1368 | |
1369 /* | |
1370 * Repeat advancing in the tree until: | |
1371 * - there is a byte that doesn't match, | |
1372 * - we reach the end of the tree, | |
1373 * - or we reach the end of the line. | |
1374 */ | |
1375 for (;;) | |
1376 { | |
1377 if (flen == 0 && *mip->mi_fend != NUL) | |
1378 flen = fold_more(mip); | |
1379 | |
1380 len = byts[arridx++]; | |
1381 | |
1382 /* If the first possible byte is a zero the prefix could end here. | |
1383 * Check if the following word matches and supports the prefix. */ | |
1384 if (byts[arridx] == 0) | |
1385 { | |
1386 /* There can be several prefixes with different conditions. We | |
1387 * try them all, since we don't know which one will give the | |
1388 * longest match. The word is the same each time, pass the list | |
1389 * of possible prefixes to find_word(). */ | |
1390 mip->mi_prefarridx = arridx; | |
1391 mip->mi_prefcnt = len; | |
1392 while (len > 0 && byts[arridx] == 0) | |
1393 { | |
1394 ++arridx; | |
1395 --len; | |
1396 } | |
1397 mip->mi_prefcnt -= len; | |
1398 | |
1399 /* Find the word that comes after the prefix. */ | |
1400 mip->mi_prefixlen = wlen; | |
485 | 1401 if (mode == FIND_COMPOUND) |
1402 /* Skip over the previously found word(s). */ | |
1403 mip->mi_prefixlen += mip->mi_compoff; | |
1404 | |
455 | 1405 if (has_mbyte) |
1406 { | |
1407 /* Case-folded length may differ from original length. */ | |
485 | 1408 mip->mi_cprefixlen = nofold_len(mip->mi_fword, |
1409 mip->mi_prefixlen, mip->mi_word); | |
455 | 1410 } |
1411 else | |
485 | 1412 mip->mi_cprefixlen = mip->mi_prefixlen; |
339 | 1413 find_word(mip, FIND_PREFIX); |
1414 | |
1415 | |
1416 if (len == 0) | |
1417 break; /* no children, word must end here */ | |
1418 } | |
1419 | |
1420 /* Stop looking at end of the line. */ | |
1421 if (ptr[wlen] == NUL) | |
1422 break; | |
1423 | |
1424 /* Perform a binary search in the list of accepted bytes. */ | |
1425 c = ptr[wlen]; | |
1426 lo = arridx; | |
1427 hi = arridx + len - 1; | |
1428 while (lo < hi) | |
1429 { | |
1430 m = (lo + hi) / 2; | |
1431 if (byts[m] > c) | |
1432 hi = m - 1; | |
1433 else if (byts[m] < c) | |
1434 lo = m + 1; | |
1435 else | |
1436 { | |
1437 lo = hi = m; | |
1438 break; | |
1439 } | |
1440 } | |
1441 | |
1442 /* Stop if there is no matching byte. */ | |
1443 if (hi < lo || byts[lo] != c) | |
1444 break; | |
1445 | |
1446 /* Continue at the child (if there is one). */ | |
1447 arridx = idxs[lo]; | |
1448 ++wlen; | |
1449 --flen; | |
1450 } | |
1451 } | |
1452 | |
1453 /* | |
1454 * Need to fold at least one more character. Do until next non-word character | |
626 | 1455 * for efficiency. Include the non-word character too. |
339 | 1456 * Return the length of the folded chars in bytes. |
1457 */ | |
1458 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1459 fold_more(matchinf_T *mip) |
339 | 1460 { |
1461 int flen; | |
1462 char_u *p; | |
1463 | |
1464 p = mip->mi_fend; | |
1465 do | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
1466 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
|
1467 while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win)); |
339 | 1468 |
626 | 1469 /* Include the non-word character so that we can check for the word end. */ |
339 | 1470 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
|
1471 MB_PTR_ADV(mip->mi_fend); |
339 | 1472 |
1473 (void)spell_casefold(p, (int)(mip->mi_fend - p), | |
1474 mip->mi_fword + mip->mi_fwordlen, | |
1475 MAXWLEN - mip->mi_fwordlen); | |
835 | 1476 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen); |
339 | 1477 mip->mi_fwordlen += flen; |
1478 return flen; | |
1479 } | |
1480 | |
1481 /* | |
323 | 1482 * Check case flags for a word. Return TRUE if the word has the requested |
1483 * case. | |
1484 */ | |
1485 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1486 spell_valid_case( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1487 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
|
1488 int treeflags) /* flags for the word in the spell tree */ |
323 | 1489 { |
389 | 1490 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0) |
323 | 1491 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0 |
474 | 1492 && ((treeflags & WF_ONECAP) == 0 |
1493 || (wordflags & WF_ONECAP) != 0))); | |
323 | 1494 } |
1495 | |
351 | 1496 /* |
1497 * Return TRUE if spell checking is not enabled. | |
1498 */ | |
1499 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1500 no_spell_checking(win_T *wp) |
498 | 1501 { |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1502 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
|
1503 || wp->w_s->b_langp.ga_len == 0) |
351 | 1504 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1505 emsg(_("E756: Spell checking is not enabled")); |
351 | 1506 return TRUE; |
1507 } | |
1508 return FALSE; | |
1509 } | |
300 | 1510 |
236 | 1511 /* |
1512 * Move to next spell error. | |
500 | 1513 * "curline" is FALSE for "[s", "]s", "[S" and "]S". |
1514 * "curline" is TRUE to find word under/after cursor in the same line. | |
483 | 1515 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move |
1516 * to after badly spelled word before the cursor. | |
495 | 1517 * Return 0 if not found, length of the badly spelled word otherwise. |
236 | 1518 */ |
1519 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1520 spell_move_to( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1521 win_T *wp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1522 int dir, /* FORWARD or BACKWARD */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1523 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
|
1524 int curline, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1525 hlf_T *attrp) /* return: attributes of bad word or NULL |
534 | 1526 (only when "dir" is FORWARD) */ |
236 | 1527 { |
249 | 1528 linenr_T lnum; |
1529 pos_T found_pos; | |
495 | 1530 int found_len = 0; |
236 | 1531 char_u *line; |
1532 char_u *p; | |
346 | 1533 char_u *endp; |
534 | 1534 hlf_T attr; |
236 | 1535 int len; |
5519 | 1536 #ifdef FEAT_SYN_HL |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
1537 int has_syntax = syntax_present(wp); |
5519 | 1538 #endif |
249 | 1539 int col; |
1540 int can_spell; | |
346 | 1541 char_u *buf = NULL; |
1542 int buflen = 0; | |
1543 int skip = 0; | |
385 | 1544 int capcol = -1; |
500 | 1545 int found_one = FALSE; |
1546 int wrapped = FALSE; | |
236 | 1547 |
498 | 1548 if (no_spell_checking(wp)) |
495 | 1549 return 0; |
236 | 1550 |
249 | 1551 /* |
1552 * Start looking for bad word at the start of the line, because we can't | |
817 | 1553 * start halfway a word, we don't know where it starts or ends. |
249 | 1554 * |
1555 * When searching backwards, we continue in the line to find the last | |
1556 * bad word (in the cursor line: before the cursor). | |
346 | 1557 * |
1558 * We concatenate the start of the next line, so that wrapped words work | |
1559 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards | |
1560 * though... | |
249 | 1561 */ |
498 | 1562 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
|
1563 CLEAR_POS(&found_pos); |
236 | 1564 |
1565 while (!got_int) | |
1566 { | |
498 | 1567 line = ml_get_buf(wp->w_buffer, lnum, FALSE); |
346 | 1568 |
835 | 1569 len = (int)STRLEN(line); |
346 | 1570 if (buflen < len + MAXWLEN + 2) |
1571 { | |
1572 vim_free(buf); | |
1573 buflen = len + MAXWLEN + 2; | |
1574 buf = alloc(buflen); | |
1575 if (buf == NULL) | |
1576 break; | |
1577 } | |
1578 | |
385 | 1579 /* In first line check first word for Capital. */ |
1580 if (lnum == 1) | |
1581 capcol = 0; | |
1582 | |
1583 /* For checking first word with a capital skip white space. */ | |
1584 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
|
1585 capcol = getwhitecols(line); |
835 | 1586 else if (curline && wp == curwin) |
1587 { | |
1588 /* 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
|
1589 col = getwhitecols(line); |
835 | 1590 if (check_need_cap(lnum, col)) |
1591 capcol = col; | |
1592 | |
1593 /* Need to get the line again, may have looked at the previous | |
1594 * one. */ | |
1595 line = ml_get_buf(wp->w_buffer, lnum, FALSE); | |
1596 } | |
385 | 1597 |
346 | 1598 /* Copy the line into "buf" and append the start of the next line if |
1599 * possible. */ | |
1600 STRCPY(buf, line); | |
498 | 1601 if (lnum < wp->w_buffer->b_ml.ml_line_count) |
883 | 1602 spell_cat_line(buf + STRLEN(buf), |
1603 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN); | |
346 | 1604 |
1605 p = buf + skip; | |
1606 endp = buf + len; | |
1607 while (p < endp) | |
236 | 1608 { |
500 | 1609 /* When searching backward don't search after the cursor. Unless |
1610 * we wrapped around the end of the buffer. */ | |
300 | 1611 if (dir == BACKWARD |
498 | 1612 && lnum == wp->w_cursor.lnum |
500 | 1613 && !wrapped |
498 | 1614 && (colnr_T)(p - buf) >= wp->w_cursor.col) |
300 | 1615 break; |
249 | 1616 |
300 | 1617 /* start of word */ |
534 | 1618 attr = HLF_COUNT; |
625 | 1619 len = spell_check(wp, p, &attr, &capcol, FALSE); |
249 | 1620 |
534 | 1621 if (attr != HLF_COUNT) |
300 | 1622 { |
1623 /* We found a bad word. Check the attribute. */ | |
534 | 1624 if (allwords || attr == HLF_SPB) |
236 | 1625 { |
300 | 1626 /* When searching forward only accept a bad word after |
1627 * the cursor. */ | |
1628 if (dir == BACKWARD | |
500 | 1629 || lnum != wp->w_cursor.lnum |
498 | 1630 || (lnum == wp->w_cursor.lnum |
500 | 1631 && (wrapped |
1632 || (colnr_T)(curline ? p - buf + len | |
346 | 1633 : p - buf) |
500 | 1634 > wp->w_cursor.col))) |
236 | 1635 { |
5519 | 1636 #ifdef FEAT_SYN_HL |
300 | 1637 if (has_syntax) |
249 | 1638 { |
835 | 1639 col = (int)(p - buf); |
498 | 1640 (void)syn_get_id(wp, lnum, (colnr_T)col, |
1504 | 1641 FALSE, &can_spell, FALSE); |
857 | 1642 if (!can_spell) |
1643 attr = HLF_COUNT; | |
300 | 1644 } |
1645 else | |
737 | 1646 #endif |
300 | 1647 can_spell = TRUE; |
249 | 1648 |
300 | 1649 if (can_spell) |
1650 { | |
857 | 1651 found_one = TRUE; |
300 | 1652 found_pos.lnum = lnum; |
835 | 1653 found_pos.col = (int)(p - buf); |
300 | 1654 found_pos.coladd = 0; |
1655 if (dir == FORWARD) | |
1656 { | |
1657 /* No need to search further. */ | |
498 | 1658 wp->w_cursor = found_pos; |
346 | 1659 vim_free(buf); |
498 | 1660 if (attrp != NULL) |
1661 *attrp = attr; | |
495 | 1662 return len; |
249 | 1663 } |
483 | 1664 else if (curline) |
1665 /* Insert mode completion: put cursor after | |
1666 * the bad word. */ | |
1667 found_pos.col += len; | |
495 | 1668 found_len = len; |
249 | 1669 } |
236 | 1670 } |
857 | 1671 else |
1672 found_one = TRUE; | |
236 | 1673 } |
1674 } | |
1675 | |
300 | 1676 /* advance to character after the word */ |
1677 p += len; | |
385 | 1678 capcol -= len; |
236 | 1679 } |
1680 | |
483 | 1681 if (dir == BACKWARD && found_pos.lnum != 0) |
1682 { | |
500 | 1683 /* Use the last match in the line (before the cursor). */ |
498 | 1684 wp->w_cursor = found_pos; |
483 | 1685 vim_free(buf); |
495 | 1686 return found_len; |
483 | 1687 } |
1688 | |
323 | 1689 if (curline) |
346 | 1690 break; /* only check cursor line */ |
323 | 1691 |
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
|
1692 /* 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
|
1693 * 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
|
1694 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
|
1695 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
|
1696 |
236 | 1697 /* Advance to next line. */ |
249 | 1698 if (dir == BACKWARD) |
1699 { | |
500 | 1700 if (lnum > 1) |
1701 --lnum; | |
1702 else if (!p_ws) | |
1703 break; /* at first line and 'nowrapscan' */ | |
1704 else | |
1705 { | |
1706 /* Wrap around to the end of the buffer. May search the | |
1707 * starting line again and accept the last match. */ | |
1708 lnum = wp->w_buffer->b_ml.ml_line_count; | |
1709 wrapped = TRUE; | |
503 | 1710 if (!shortmess(SHM_SEARCH)) |
1711 give_warning((char_u *)_(top_bot_msg), TRUE); | |
500 | 1712 } |
385 | 1713 capcol = -1; |
249 | 1714 } |
1715 else | |
1716 { | |
500 | 1717 if (lnum < wp->w_buffer->b_ml.ml_line_count) |
1718 ++lnum; | |
1719 else if (!p_ws) | |
1720 break; /* at first line and 'nowrapscan' */ | |
1721 else | |
1722 { | |
1723 /* Wrap around to the start of the buffer. May search the | |
1724 * starting line again and accept the first match. */ | |
1725 lnum = 1; | |
1726 wrapped = TRUE; | |
503 | 1727 if (!shortmess(SHM_SEARCH)) |
1728 give_warning((char_u *)_(bot_top_msg), TRUE); | |
500 | 1729 } |
1730 | |
1731 /* If we are back at the starting line and there is no match then | |
1732 * 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
|
1733 if (lnum == wp->w_cursor.lnum && !found_one) |
346 | 1734 break; |
1735 | |
1736 /* Skip the characters at the start of the next line that were | |
1737 * included in a match crossing line boundaries. */ | |
534 | 1738 if (attr == HLF_COUNT) |
835 | 1739 skip = (int)(p - endp); |
346 | 1740 else |
1741 skip = 0; | |
385 | 1742 |
835 | 1743 /* Capcol skips over the inserted space. */ |
385 | 1744 --capcol; |
1745 | |
1746 /* But after empty line check first word in next line */ | |
1747 if (*skipwhite(line) == NUL) | |
1748 capcol = 0; | |
249 | 1749 } |
236 | 1750 |
1751 line_breakcheck(); | |
1752 } | |
1753 | |
346 | 1754 vim_free(buf); |
495 | 1755 return 0; |
346 | 1756 } |
1757 | |
1758 /* | |
1759 * For spell checking: concatenate the start of the following line "line" into | |
1760 * "buf", blanking-out special characters. Copy less then "maxlen" bytes. | |
1577 | 1761 * Keep the blanks at the start of the next line, this is used in win_line() |
1762 * to skip those bytes if the word was OK. | |
346 | 1763 */ |
1764 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1765 spell_cat_line(char_u *buf, char_u *line, int maxlen) |
346 | 1766 { |
1767 char_u *p; | |
1768 int n; | |
1769 | |
1770 p = skipwhite(line); | |
1771 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL) | |
1772 p = skipwhite(p + 1); | |
1773 | |
1774 if (*p != NUL) | |
1775 { | |
1577 | 1776 /* Only worth concatenating if there is something else than spaces to |
1777 * concatenate. */ | |
1778 n = (int)(p - line) + 1; | |
1779 if (n < maxlen - 1) | |
1780 { | |
1781 vim_memset(buf, ' ', n); | |
1782 vim_strncpy(buf + n, p, maxlen - 1 - n); | |
1783 } | |
346 | 1784 } |
236 | 1785 } |
1786 | |
626 | 1787 /* |
1788 * Structure used for the cookie argument of do_in_runtimepath(). | |
1789 */ | |
501 | 1790 typedef struct spelload_S |
1791 { | |
1792 char_u sl_lang[MAXWLEN + 1]; /* language name */ | |
1793 slang_T *sl_slang; /* resulting slang_T struct */ | |
1794 int sl_nobreak; /* NOBREAK language found */ | |
1795 } spelload_T; | |
1796 | |
236 | 1797 /* |
307 | 1798 * Load word list(s) for "lang" from Vim spell file(s). |
310 | 1799 * "lang" must be the language without the region: e.g., "en". |
236 | 1800 */ |
307 | 1801 static void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1802 spell_load_lang(char_u *lang) |
236 | 1803 { |
310 | 1804 char_u fname_enc[85]; |
236 | 1805 int r; |
501 | 1806 spelload_T sl; |
649 | 1807 int round; |
307 | 1808 |
310 | 1809 /* Copy the language name to pass it to spell_load_cb() as a cookie. |
307 | 1810 * It's truncated when an error is detected. */ |
501 | 1811 STRCPY(sl.sl_lang, lang); |
1812 sl.sl_slang = NULL; | |
1813 sl.sl_nobreak = FALSE; | |
307 | 1814 |
649 | 1815 /* We may retry when no spell file is found for the language, an |
1816 * autocommand may load it then. */ | |
1817 for (round = 1; round <= 2; ++round) | |
1818 { | |
1819 /* | |
1820 * Find the first spell file for "lang" in 'runtimepath' and load it. | |
1821 */ | |
1822 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, | |
2660 | 1823 #ifdef VMS |
1824 "spell/%s_%s.spl", | |
1825 #else | |
1826 "spell/%s.%s.spl", | |
1827 #endif | |
1828 lang, spell_enc()); | |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1829 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); |
649 | 1830 |
1831 if (r == FAIL && *sl.sl_lang != NUL) | |
1832 { | |
1833 /* Try loading the ASCII version. */ | |
1834 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5, | |
2660 | 1835 #ifdef VMS |
1836 "spell/%s_ascii.spl", | |
1837 #else | |
1838 "spell/%s.ascii.spl", | |
1839 #endif | |
1840 lang); | |
8524
2f57bbe870ea
commit https://github.com/vim/vim/commit/7f8989dd8a627af2185df381195351a913f3777f
Christian Brabandt <cb@256bit.org>
parents:
7835
diff
changeset
|
1841 r = do_in_runtimepath(fname_enc, 0, spell_load_cb, &sl); |
649 | 1842 |
1843 if (r == FAIL && *sl.sl_lang != NUL && round == 1 | |
1844 && apply_autocmds(EVENT_SPELLFILEMISSING, lang, | |
1845 curbuf->b_fname, FALSE, curbuf)) | |
1846 continue; | |
1847 break; | |
13380
69517d67421f
patch 8.0.1564: too many #ifdefs
Christian Brabandt <cb@256bit.org>
parents:
13308
diff
changeset
|
1848 } |
714 | 1849 break; |
307 | 1850 } |
1851 | |
1852 if (r == FAIL) | |
649 | 1853 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
1854 smsg( |
2660 | 1855 #ifdef VMS |
1856 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""), | |
1857 #else | |
1858 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""), | |
1859 #endif | |
483 | 1860 lang, spell_enc(), lang); |
649 | 1861 } |
501 | 1862 else if (sl.sl_slang != NULL) |
1863 { | |
626 | 1864 /* At least one file was loaded, now load ALL the additions. */ |
310 | 1865 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
|
1866 do_in_runtimepath(fname_enc, DIP_ALL, spell_load_cb, &sl); |
310 | 1867 } |
1868 } | |
1869 | |
1870 /* | |
1871 * Return the encoding used for spell checking: Use 'encoding', except that we | |
1872 * use "latin1" for "latin9". And limit to 60 characters (just in case). | |
1873 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1874 char_u * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1875 spell_enc(void) |
310 | 1876 { |
1877 | |
1878 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0) | |
1879 return p_enc; | |
1880 return (char_u *)"latin1"; | |
236 | 1881 } |
1882 | |
1883 /* | |
385 | 1884 * Get the name of the .spl file for the internal wordlist into |
1885 * "fname[MAXPATHL]". | |
1886 */ | |
1887 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1888 int_wordlist_spl(char_u *fname) |
385 | 1889 { |
2660 | 1890 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL, |
385 | 1891 int_wordlist, spell_enc()); |
1892 } | |
1893 | |
1894 /* | |
625 | 1895 * Allocate a new slang_T for language "lang". "lang" can be NULL. |
236 | 1896 * Caller must fill "sl_next". |
1897 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1898 slang_T * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1899 slang_alloc(char_u *lang) |
236 | 1900 { |
1901 slang_T *lp; | |
1902 | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
1903 lp = ALLOC_CLEAR_ONE(slang_T); |
236 | 1904 if (lp != NULL) |
1905 { | |
625 | 1906 if (lang != NULL) |
1907 lp->sl_name = vim_strsave(lang); | |
323 | 1908 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10); |
625 | 1909 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10); |
483 | 1910 lp->sl_compmax = MAXWLEN; |
1911 lp->sl_compsylmax = MAXWLEN; | |
625 | 1912 hash_init(&lp->sl_wordcount); |
1913 } | |
1914 | |
236 | 1915 return lp; |
1916 } | |
1917 | |
1918 /* | |
1919 * Free the contents of an slang_T and the structure itself. | |
1920 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1921 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1922 slang_free(slang_T *lp) |
236 | 1923 { |
1924 vim_free(lp->sl_name); | |
310 | 1925 vim_free(lp->sl_fname); |
1926 slang_clear(lp); | |
1927 vim_free(lp); | |
1928 } | |
1929 | |
1930 /* | |
1931 * Clear an slang_T so that the file can be reloaded. | |
1932 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
1933 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
1934 slang_clear(slang_T *lp) |
310 | 1935 { |
339 | 1936 garray_T *gap; |
1937 fromto_T *ftp; | |
344 | 1938 salitem_T *smp; |
339 | 1939 int i; |
625 | 1940 int round; |
323 | 1941 |
13244
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_fbyts); |
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_kbyts); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1944 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
|
1945 |
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_fidxs); |
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_kidxs); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1948 VIM_CLEAR(lp->sl_pidxs); |
323 | 1949 |
625 | 1950 for (round = 1; round <= 2; ++round) |
1951 { | |
1952 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal; | |
1953 while (gap->ga_len > 0) | |
1954 { | |
1955 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len]; | |
1956 vim_free(ftp->ft_from); | |
1957 vim_free(ftp->ft_to); | |
1958 } | |
1959 ga_clear(gap); | |
1960 } | |
344 | 1961 |
1962 gap = &lp->sl_sal; | |
375 | 1963 if (lp->sl_sofo) |
376 | 1964 { |
1965 /* "ga_len" is set to 1 without adding an item for latin1 */ | |
1966 if (gap->ga_data != NULL) | |
1967 /* SOFOFROM and SOFOTO items: free lists of wide characters. */ | |
1968 for (i = 0; i < gap->ga_len; ++i) | |
1969 vim_free(((int **)gap->ga_data)[i]); | |
1970 } | |
375 | 1971 else |
1972 /* SAL items: free salitem_T items */ | |
1973 while (gap->ga_len > 0) | |
1974 { | |
1975 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len]; | |
1976 vim_free(smp->sm_lead); | |
1977 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */ | |
1978 vim_free(smp->sm_to); | |
1979 vim_free(smp->sm_lead_w); | |
1980 vim_free(smp->sm_oneof_w); | |
1981 vim_free(smp->sm_to_w); | |
1982 } | |
344 | 1983 ga_clear(gap); |
323 | 1984 |
339 | 1985 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
|
1986 vim_regfree(lp->sl_prefprog[i]); |
376 | 1987 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
|
1988 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
|
1989 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1990 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
|
1991 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
1992 VIM_CLEAR(lp->sl_midword); |
339 | 1993 |
4805
66803af09906
updated for version 7.3.1149
Bram Moolenaar <bram@vim.org>
parents:
4352
diff
changeset
|
1994 vim_regfree(lp->sl_compprog); |
483 | 1995 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
|
1996 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
|
1997 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
|
1998 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
|
1999 |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2000 VIM_CLEAR(lp->sl_syllable); |
483 | 2001 ga_clear(&lp->sl_syl_items); |
481 | 2002 |
809 | 2003 ga_clear_strings(&lp->sl_comppat); |
2004 | |
625 | 2005 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF); |
2006 hash_init(&lp->sl_wordcount); | |
2007 | |
2008 hash_clear_all(&lp->sl_map_hash, 0); | |
2009 | |
2010 /* Clear info from .sug file. */ | |
2011 slang_clear_sug(lp); | |
483 | 2012 |
2013 lp->sl_compmax = MAXWLEN; | |
501 | 2014 lp->sl_compminlen = 0; |
483 | 2015 lp->sl_compsylmax = MAXWLEN; |
2016 lp->sl_regions[0] = NUL; | |
236 | 2017 } |
2018 | |
2019 /* | |
625 | 2020 * Clear the info from the .sug file in "lp". |
2021 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2022 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2023 slang_clear_sug(slang_T *lp) |
625 | 2024 { |
13244
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_sbyts); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2026 VIM_CLEAR(lp->sl_sidxs); |
625 | 2027 close_spellbuf(lp->sl_sugbuf); |
2028 lp->sl_sugbuf = NULL; | |
2029 lp->sl_sugloaded = FALSE; | |
2030 lp->sl_sugtime = 0; | |
2031 } | |
2032 | |
2033 /* | |
307 | 2034 * Load one spell file and store the info into a slang_T. |
236 | 2035 * Invoked through do_in_runtimepath(). |
2036 */ | |
2037 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2038 spell_load_cb(char_u *fname, void *cookie) |
501 | 2039 { |
2040 spelload_T *slp = (spelload_T *)cookie; | |
2041 slang_T *slang; | |
2042 | |
2043 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE); | |
2044 if (slang != NULL) | |
2045 { | |
2046 /* When a previously loaded file has NOBREAK also use it for the | |
2047 * ".add" files. */ | |
2048 if (slp->sl_nobreak && slang->sl_add) | |
2049 slang->sl_nobreak = TRUE; | |
2050 else if (slang->sl_nobreak) | |
2051 slp->sl_nobreak = TRUE; | |
2052 | |
2053 slp->sl_slang = slang; | |
2054 } | |
310 | 2055 } |
2056 | |
625 | 2057 |
2058 /* | |
2059 * Add a word to the hashtable of common words. | |
2060 * If it's already there then the counter is increased. | |
2061 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2062 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2063 count_common_word( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2064 slang_T *lp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2065 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2066 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
|
2067 int count) /* 1 to count once, 10 to init */ |
625 | 2068 { |
2069 hash_T hash; | |
2070 hashitem_T *hi; | |
2071 wordcount_T *wc; | |
2072 char_u buf[MAXWLEN]; | |
2073 char_u *p; | |
2074 | |
2075 if (len == -1) | |
2076 p = word; | |
17653
cc68aca87c17
patch 8.1.1824: crash when correctly spelled word is very long
Bram Moolenaar <Bram@vim.org>
parents:
17434
diff
changeset
|
2077 else if (len >= MAXWLEN) |
cc68aca87c17
patch 8.1.1824: crash when correctly spelled word is very long
Bram Moolenaar <Bram@vim.org>
parents:
17434
diff
changeset
|
2078 return; |
625 | 2079 else |
2080 { | |
2081 vim_strncpy(buf, word, len); | |
2082 p = buf; | |
2083 } | |
2084 | |
2085 hash = hash_hash(p); | |
2086 hi = hash_lookup(&lp->sl_wordcount, p, hash); | |
2087 if (HASHITEM_EMPTY(hi)) | |
2088 { | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
2089 wc = alloc(sizeof(wordcount_T) + STRLEN(p)); |
625 | 2090 if (wc == NULL) |
2091 return; | |
2092 STRCPY(wc->wc_word, p); | |
2093 wc->wc_count = count; | |
2094 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash); | |
2095 } | |
2096 else | |
2097 { | |
2098 wc = HI2WC(hi); | |
2099 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */ | |
2100 wc->wc_count = MAXWORDCOUNT; | |
2101 } | |
2102 } | |
2103 | |
2104 /* | |
2105 * Adjust the score of common words. | |
2106 */ | |
2107 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2108 score_wordcount_adj( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2109 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2110 int score, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2111 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2112 int split) /* word was split, less bonus */ |
625 | 2113 { |
2114 hashitem_T *hi; | |
2115 wordcount_T *wc; | |
2116 int bonus; | |
2117 int newscore; | |
2118 | |
2119 hi = hash_find(&slang->sl_wordcount, word); | |
2120 if (!HASHITEM_EMPTY(hi)) | |
2121 { | |
2122 wc = HI2WC(hi); | |
2123 if (wc->wc_count < SCORE_THRES2) | |
2124 bonus = SCORE_COMMON1; | |
2125 else if (wc->wc_count < SCORE_THRES3) | |
2126 bonus = SCORE_COMMON2; | |
2127 else | |
2128 bonus = SCORE_COMMON3; | |
2129 if (split) | |
2130 newscore = score - bonus / 2; | |
2131 else | |
2132 newscore = score - bonus; | |
2133 if (newscore < 0) | |
2134 return 0; | |
2135 return newscore; | |
2136 } | |
2137 return score; | |
2138 } | |
2139 | |
483 | 2140 |
495 | 2141 /* |
498 | 2142 * Return TRUE if byte "n" appears in "str". |
495 | 2143 * Like strchr() but independent of locale. |
2144 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2145 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2146 byte_in_str(char_u *str, int n) |
495 | 2147 { |
2148 char_u *p; | |
2149 | |
2150 for (p = str; *p != NUL; ++p) | |
498 | 2151 if (*p == n) |
495 | 2152 return TRUE; |
2153 return FALSE; | |
2154 } | |
2155 | |
483 | 2156 #define SY_MAXLEN 30 |
2157 typedef struct syl_item_S | |
2158 { | |
2159 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */ | |
2160 int sy_len; | |
2161 } syl_item_T; | |
2162 | |
2163 /* | |
2164 * Truncate "slang->sl_syllable" at the first slash and put the following items | |
2165 * in "slang->sl_syl_items". | |
2166 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2167 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2168 init_syl_tab(slang_T *slang) |
483 | 2169 { |
2170 char_u *p; | |
2171 char_u *s; | |
2172 int l; | |
2173 syl_item_T *syl; | |
2174 | |
2175 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4); | |
2176 p = vim_strchr(slang->sl_syllable, '/'); | |
2177 while (p != NULL) | |
2178 { | |
2179 *p++ = NUL; | |
495 | 2180 if (*p == NUL) /* trailing slash */ |
483 | 2181 break; |
2182 s = p; | |
2183 p = vim_strchr(p, '/'); | |
2184 if (p == NULL) | |
835 | 2185 l = (int)STRLEN(s); |
483 | 2186 else |
835 | 2187 l = (int)(p - s); |
483 | 2188 if (l >= SY_MAXLEN) |
2189 return SP_FORMERROR; | |
2190 if (ga_grow(&slang->sl_syl_items, 1) == FAIL) | |
495 | 2191 return SP_OTHERERROR; |
483 | 2192 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) |
2193 + slang->sl_syl_items.ga_len++; | |
2194 vim_strncpy(syl->sy_chars, s, l); | |
2195 syl->sy_len = l; | |
2196 } | |
2197 return OK; | |
2198 } | |
2199 | |
2200 /* | |
2201 * Count the number of syllables in "word". | |
2202 * When "word" contains spaces the syllables after the last space are counted. | |
2203 * Returns zero if syllables are not defines. | |
2204 */ | |
2205 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2206 count_syllables(slang_T *slang, char_u *word) |
483 | 2207 { |
2208 int cnt = 0; | |
2209 int skip = FALSE; | |
2210 char_u *p; | |
2211 int len; | |
2212 int i; | |
2213 syl_item_T *syl; | |
2214 int c; | |
2215 | |
2216 if (slang->sl_syllable == NULL) | |
2217 return 0; | |
2218 | |
2219 for (p = word; *p != NUL; p += len) | |
2220 { | |
2221 /* When running into a space reset counter. */ | |
2222 if (*p == ' ') | |
2223 { | |
2224 len = 1; | |
2225 cnt = 0; | |
2226 continue; | |
2227 } | |
2228 | |
2229 /* Find longest match of syllable items. */ | |
2230 len = 0; | |
2231 for (i = 0; i < slang->sl_syl_items.ga_len; ++i) | |
2232 { | |
2233 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i; | |
2234 if (syl->sy_len > len | |
2235 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0) | |
2236 len = syl->sy_len; | |
2237 } | |
2238 if (len != 0) /* found a match, count syllable */ | |
2239 { | |
2240 ++cnt; | |
2241 skip = FALSE; | |
2242 } | |
2243 else | |
2244 { | |
2245 /* No recognized syllable item, at least a syllable char then? */ | |
2246 c = mb_ptr2char(p); | |
2247 len = (*mb_ptr2len)(p); | |
2248 if (vim_strchr(slang->sl_syllable, c) == NULL) | |
2249 skip = FALSE; /* No, search for next syllable */ | |
2250 else if (!skip) | |
2251 { | |
2252 ++cnt; /* Yes, count it */ | |
2253 skip = TRUE; /* don't count following syllable chars */ | |
2254 } | |
2255 } | |
2256 } | |
2257 return cnt; | |
376 | 2258 } |
2259 | |
381 | 2260 /* |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2261 * Parse 'spelllang' and set w_s->b_langp accordingly. |
351 | 2262 * Returns NULL if it's OK, an error message otherwise. |
236 | 2263 */ |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2264 char * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2265 did_set_spelllang(win_T *wp) |
236 | 2266 { |
2267 garray_T ga; | |
351 | 2268 char_u *splp; |
236 | 2269 char_u *region; |
416 | 2270 char_u region_cp[3]; |
355 | 2271 int filename; |
236 | 2272 int region_mask; |
503 | 2273 slang_T *slang; |
236 | 2274 int c; |
351 | 2275 char_u lang[MAXWLEN + 1]; |
323 | 2276 char_u spf_name[MAXPATHL]; |
351 | 2277 int len; |
2278 char_u *p; | |
381 | 2279 int round; |
385 | 2280 char_u *spf; |
389 | 2281 char_u *use_region = NULL; |
2282 int dont_use_region = FALSE; | |
501 | 2283 int nobreak = FALSE; |
503 | 2284 int i, j; |
2285 langp_T *lp, *lp2; | |
1185 | 2286 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
|
2287 char *ret_msg = NULL; |
1185 | 2288 char_u *spl_copy; |
9487
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2289 bufref_T bufref; |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2290 |
69ed2c9d34a6
commit https://github.com/vim/vim/commit/7c0a2f367f2507669560b1a66423155c70d2e75b
Christian Brabandt <cb@256bit.org>
parents:
9387
diff
changeset
|
2291 set_bufref(&bufref, wp->w_buffer); |
1185 | 2292 |
2293 /* We don't want to do this recursively. May happen when a language is | |
2294 * not available and the SpellFileMissing autocommand opens a new buffer | |
2295 * in which 'spell' is set. */ | |
2296 if (recursive) | |
2297 return NULL; | |
2298 recursive = TRUE; | |
236 | 2299 |
2300 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
|
2301 clear_midword(wp); |
236 | 2302 |
4352 | 2303 /* Make a copy of 'spelllang', the SpellFileMissing autocommands may change |
1185 | 2304 * it under our fingers. */ |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2305 spl_copy = vim_strsave(wp->w_s->b_p_spl); |
1185 | 2306 if (spl_copy == NULL) |
2307 goto theend; | |
2308 | |
5477 | 2309 wp->w_s->b_cjk = 0; |
2310 | |
2311 /* Loop over comma separated language names. */ | |
1185 | 2312 for (splp = spl_copy; *splp != NUL; ) |
351 | 2313 { |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2314 // Get one language name. |
351 | 2315 copy_option_part(&splp, lang, MAXWLEN, ","); |
240 | 2316 region = NULL; |
835 | 2317 len = (int)STRLEN(lang); |
355 | 2318 |
16277
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2319 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
|
2320 continue; |
5ef25fa57f71
patch 8.1.1143: may pass weird strings to file name expansion
Bram Moolenaar <Bram@vim.org>
parents:
16239
diff
changeset
|
2321 |
5477 | 2322 if (STRCMP(lang, "cjk") == 0) |
2323 { | |
2324 wp->w_s->b_cjk = 1; | |
2325 continue; | |
2326 } | |
2327 | |
355 | 2328 /* If the name ends in ".spl" use it as the name of the spell file. |
2329 * If there is a region name let "region" point to it and remove it | |
2330 * from the name. */ | |
2331 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0) | |
236 | 2332 { |
355 | 2333 filename = TRUE; |
2334 | |
416 | 2335 /* Locate a region and remove it from the file name. */ |
2336 p = vim_strchr(gettail(lang), '_'); | |
2337 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2]) | |
2338 && !ASCII_ISALPHA(p[3])) | |
2339 { | |
2340 vim_strncpy(region_cp, p + 1, 2); | |
2341 mch_memmove(p, p + 3, len - (p - lang) - 2); | |
2342 region = region_cp; | |
2343 } | |
2344 else | |
2345 dont_use_region = TRUE; | |
2346 | |
355 | 2347 /* Check if we loaded this language before. */ |
503 | 2348 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2349 if (fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) == FPC_SAME) |
355 | 2350 break; |
236 | 2351 } |
355 | 2352 else |
2353 { | |
2354 filename = FALSE; | |
2355 if (len > 3 && lang[len - 3] == '_') | |
2356 { | |
2357 region = lang + len - 2; | |
2358 len -= 3; | |
2359 lang[len] = NUL; | |
389 | 2360 } |
2361 else | |
2362 dont_use_region = TRUE; | |
355 | 2363 |
2364 /* Check if we loaded this language before. */ | |
503 | 2365 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
2366 if (STRICMP(lang, slang->sl_name) == 0) | |
355 | 2367 break; |
2368 } | |
236 | 2369 |
416 | 2370 if (region != NULL) |
2371 { | |
2372 /* If the region differs from what was used before then don't | |
2373 * use it for 'spellfile'. */ | |
2374 if (use_region != NULL && STRCMP(region, use_region) != 0) | |
2375 dont_use_region = TRUE; | |
2376 use_region = region; | |
2377 } | |
2378 | |
351 | 2379 /* If not found try loading the language now. */ |
503 | 2380 if (slang == NULL) |
355 | 2381 { |
2382 if (filename) | |
2383 (void)spell_load_file(lang, lang, NULL, FALSE); | |
2384 else | |
1185 | 2385 { |
355 | 2386 spell_load_lang(lang); |
1185 | 2387 /* SpellFileMissing autocommands may do anything, including |
2388 * 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
|
2389 if (!bufref_valid(&bufref)) |
1185 | 2390 { |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
2391 ret_msg = N_("E797: SpellFileMissing autocommand deleted buffer"); |
1185 | 2392 goto theend; |
2393 } | |
2394 } | |
355 | 2395 } |
236 | 2396 |
307 | 2397 /* |
351 | 2398 * Loop over the languages, there can be several files for "lang". |
307 | 2399 */ |
503 | 2400 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2401 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE, TRUE) |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2402 == FPC_SAME |
503 | 2403 : STRICMP(lang, slang->sl_name) == 0) |
236 | 2404 { |
316 | 2405 region_mask = REGION_ALL; |
355 | 2406 if (!filename && region != NULL) |
236 | 2407 { |
307 | 2408 /* find region in sl_regions */ |
503 | 2409 c = find_region(slang->sl_regions, region); |
307 | 2410 if (c == REGION_ALL) |
2411 { | |
503 | 2412 if (slang->sl_add) |
389 | 2413 { |
503 | 2414 if (*slang->sl_regions != NUL) |
389 | 2415 /* This addition file is for other regions. */ |
2416 region_mask = 0; | |
2417 } | |
2418 else | |
2419 /* This is probably an error. Give a warning and | |
2420 * 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
|
2421 smsg(_("Warning: region %s not supported"), |
351 | 2422 region); |
307 | 2423 } |
2424 else | |
2425 region_mask = 1 << c; | |
236 | 2426 } |
307 | 2427 |
389 | 2428 if (region_mask != 0) |
307 | 2429 { |
389 | 2430 if (ga_grow(&ga, 1) == FAIL) |
2431 { | |
2432 ga_clear(&ga); | |
1185 | 2433 ret_msg = e_outofmem; |
2434 goto theend; | |
389 | 2435 } |
503 | 2436 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
389 | 2437 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
2438 ++ga.ga_len; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2439 use_midword(slang, wp); |
503 | 2440 if (slang->sl_nobreak) |
501 | 2441 nobreak = TRUE; |
307 | 2442 } |
385 | 2443 } |
2444 } | |
2445 | |
2446 /* round 0: load int_wordlist, if possible. | |
2447 * round 1: load first name in 'spellfile'. | |
2448 * round 2: load second name in 'spellfile. | |
2449 * etc. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2450 spf = curwin->w_s->b_p_spf; |
385 | 2451 for (round = 0; round == 0 || *spf != NUL; ++round) |
2452 { | |
2453 if (round == 0) | |
2454 { | |
2455 /* Internal wordlist, if there is one. */ | |
2456 if (int_wordlist == NULL) | |
381 | 2457 continue; |
385 | 2458 int_wordlist_spl(spf_name); |
381 | 2459 } |
2460 else | |
2461 { | |
385 | 2462 /* One entry in 'spellfile'. */ |
2463 copy_option_part(&spf, spf_name, MAXPATHL - 5, ","); | |
2464 STRCAT(spf_name, ".spl"); | |
2465 | |
2466 /* If it was already found above then skip it. */ | |
2467 for (c = 0; c < ga.ga_len; ++c) | |
500 | 2468 { |
2469 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname; | |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2470 if (p != NULL && fullpathcmp(spf_name, p, FALSE, TRUE) |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2471 == FPC_SAME) |
385 | 2472 break; |
500 | 2473 } |
385 | 2474 if (c < ga.ga_len) |
381 | 2475 continue; |
2476 } | |
2477 | |
351 | 2478 /* Check if it was loaded already. */ |
503 | 2479 for (slang = first_lang; slang != NULL; slang = slang->sl_next) |
16738
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2480 if (fullpathcmp(spf_name, slang->sl_fname, FALSE, TRUE) |
b52ea9c5f1db
patch 8.1.1371: cannot recover from a swap file
Bram Moolenaar <Bram@vim.org>
parents:
16606
diff
changeset
|
2481 == FPC_SAME) |
323 | 2482 break; |
503 | 2483 if (slang == NULL) |
323 | 2484 { |
351 | 2485 /* Not loaded, try loading it now. The language name includes the |
385 | 2486 * region name, the region is ignored otherwise. for int_wordlist |
2487 * use an arbitrary name. */ | |
2488 if (round == 0) | |
2489 STRCPY(lang, "internal wordlist"); | |
2490 else | |
2491 { | |
2492 vim_strncpy(lang, gettail(spf_name), MAXWLEN); | |
381 | 2493 p = vim_strchr(lang, '.'); |
2494 if (p != NULL) | |
2495 *p = NUL; /* truncate at ".encoding.add" */ | |
2496 } | |
503 | 2497 slang = spell_load_file(spf_name, lang, NULL, TRUE); |
501 | 2498 |
2499 /* If one of the languages has NOBREAK we assume the addition | |
2500 * files also have this. */ | |
503 | 2501 if (slang != NULL && nobreak) |
2502 slang->sl_nobreak = TRUE; | |
2503 } | |
2504 if (slang != NULL && ga_grow(&ga, 1) == OK) | |
323 | 2505 { |
389 | 2506 region_mask = REGION_ALL; |
2507 if (use_region != NULL && !dont_use_region) | |
2508 { | |
2509 /* find region in sl_regions */ | |
503 | 2510 c = find_region(slang->sl_regions, use_region); |
389 | 2511 if (c != REGION_ALL) |
2512 region_mask = 1 << c; | |
503 | 2513 else if (*slang->sl_regions != NUL) |
389 | 2514 /* This spell file is for other regions. */ |
2515 region_mask = 0; | |
2516 } | |
2517 | |
2518 if (region_mask != 0) | |
2519 { | |
503 | 2520 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang; |
2521 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL; | |
2522 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL; | |
389 | 2523 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask; |
2524 ++ga.ga_len; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2525 use_midword(slang, wp); |
389 | 2526 } |
323 | 2527 } |
2528 } | |
2529 | |
236 | 2530 /* 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
|
2531 ga_clear(&wp->w_s->b_langp); |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2532 wp->w_s->b_langp = ga; |
236 | 2533 |
503 | 2534 /* For each language figure out what language to use for sound folding and |
2535 * REP items. If the language doesn't support it itself use another one | |
2536 * with the same name. E.g. for "en-math" use "en". */ | |
2537 for (i = 0; i < ga.ga_len; ++i) | |
2538 { | |
2539 lp = LANGP_ENTRY(ga, i); | |
2540 | |
2541 /* sound folding */ | |
2542 if (lp->lp_slang->sl_sal.ga_len > 0) | |
2543 /* language does sound folding itself */ | |
2544 lp->lp_sallang = lp->lp_slang; | |
2545 else | |
2546 /* find first similar language that does sound folding */ | |
2547 for (j = 0; j < ga.ga_len; ++j) | |
2548 { | |
2549 lp2 = LANGP_ENTRY(ga, j); | |
2550 if (lp2->lp_slang->sl_sal.ga_len > 0 | |
2551 && STRNCMP(lp->lp_slang->sl_name, | |
2552 lp2->lp_slang->sl_name, 2) == 0) | |
2553 { | |
2554 lp->lp_sallang = lp2->lp_slang; | |
2555 break; | |
2556 } | |
2557 } | |
2558 | |
2559 /* REP items */ | |
2560 if (lp->lp_slang->sl_rep.ga_len > 0) | |
2561 /* language has REP items itself */ | |
2562 lp->lp_replang = lp->lp_slang; | |
2563 else | |
625 | 2564 /* find first similar language that has REP items */ |
503 | 2565 for (j = 0; j < ga.ga_len; ++j) |
2566 { | |
2567 lp2 = LANGP_ENTRY(ga, j); | |
2568 if (lp2->lp_slang->sl_rep.ga_len > 0 | |
2569 && STRNCMP(lp->lp_slang->sl_name, | |
2570 lp2->lp_slang->sl_name, 2) == 0) | |
2571 { | |
2572 lp->lp_replang = lp2->lp_slang; | |
2573 break; | |
2574 } | |
2575 } | |
2576 } | |
2577 | |
1185 | 2578 theend: |
2579 vim_free(spl_copy); | |
2580 recursive = FALSE; | |
5891 | 2581 redraw_win_later(wp, NOT_VALID); |
1185 | 2582 return ret_msg; |
236 | 2583 } |
2584 | |
2585 /* | |
376 | 2586 * Clear the midword characters for buffer "buf". |
2587 */ | |
2588 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2589 clear_midword(win_T *wp) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2590 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2591 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
|
2592 VIM_CLEAR(wp->w_s->b_spell_ismw_mb); |
376 | 2593 } |
2594 | |
2595 /* | |
2596 * Use the "sl_midword" field of language "lp" for buffer "buf". | |
2597 * They add up to any currently used midword characters. | |
2598 */ | |
2599 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2600 use_midword(slang_T *lp, win_T *wp) |
376 | 2601 { |
2602 char_u *p; | |
2603 | |
389 | 2604 if (lp->sl_midword == NULL) /* there aren't any */ |
2605 return; | |
2606 | |
376 | 2607 for (p = lp->sl_midword; *p != NUL; ) |
2608 if (has_mbyte) | |
2609 { | |
2610 int c, l, n; | |
2611 char_u *bp; | |
2612 | |
2613 c = mb_ptr2char(p); | |
474 | 2614 l = (*mb_ptr2len)(p); |
2615 if (c < 256 && l <= 2) | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2616 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
|
2617 else if (wp->w_s->b_spell_ismw_mb == NULL) |
376 | 2618 /* 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
|
2619 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l); |
376 | 2620 else |
2621 { | |
2622 /* 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
|
2623 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
|
2624 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l); |
376 | 2625 if (bp != NULL) |
2626 { | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2627 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
|
2628 wp->w_s->b_spell_ismw_mb = bp; |
376 | 2629 vim_strncpy(bp + n, p, l); |
2630 } | |
2631 } | |
2632 p += l; | |
2633 } | |
2634 else | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2635 wp->w_s->b_spell_ismw[*p++] = TRUE; |
376 | 2636 } |
2637 | |
2638 /* | |
236 | 2639 * 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
|
2640 * Each region is simply stored as the two characters of its name. |
381 | 2641 * Returns the index if found (first is 0), REGION_ALL if not found. |
236 | 2642 */ |
2643 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2644 find_region(char_u *rp, char_u *region) |
236 | 2645 { |
2646 int i; | |
2647 | |
2648 for (i = 0; ; i += 2) | |
2649 { | |
2650 if (rp[i] == NUL) | |
2651 return REGION_ALL; | |
2652 if (rp[i] == region[0] && rp[i + 1] == region[1]) | |
2653 break; | |
2654 } | |
2655 return i / 2; | |
2656 } | |
2657 | |
2658 /* | |
323 | 2659 * Return case type of word: |
236 | 2660 * w word 0 |
300 | 2661 * Word WF_ONECAP |
2662 * W WORD WF_ALLCAP | |
2663 * WoRd wOrd WF_KEEPCAP | |
236 | 2664 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2665 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2666 captype( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2667 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2668 char_u *end) /* When NULL use up to NUL byte. */ |
236 | 2669 { |
2670 char_u *p; | |
2671 int c; | |
2672 int firstcap; | |
2673 int allcap; | |
2674 int past_second = FALSE; /* past second word char */ | |
2675 | |
2676 /* find first letter */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2677 for (p = word; !spell_iswordp_nmw(p, curwin); MB_PTR_ADV(p)) |
323 | 2678 if (end == NULL ? *p == NUL : p >= end) |
236 | 2679 return 0; /* only non-word characters, illegal word */ |
310 | 2680 if (has_mbyte) |
2681 c = mb_ptr2char_adv(&p); | |
2682 else | |
2683 c = *p++; | |
324 | 2684 firstcap = allcap = SPELL_ISUPPER(c); |
236 | 2685 |
2686 /* | |
2687 * Need to check all letters to find a word with mixed upper/lower. | |
2688 * But a word with an upper char only at start is a ONECAP. | |
2689 */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2690 for ( ; end == NULL ? *p != NUL : p < end; MB_PTR_ADV(p)) |
5477 | 2691 if (spell_iswordp_nmw(p, curwin)) |
236 | 2692 { |
455 | 2693 c = PTR2CHAR(p); |
324 | 2694 if (!SPELL_ISUPPER(c)) |
236 | 2695 { |
2696 /* UUl -> KEEPCAP */ | |
2697 if (past_second && allcap) | |
300 | 2698 return WF_KEEPCAP; |
236 | 2699 allcap = FALSE; |
2700 } | |
2701 else if (!allcap) | |
2702 /* UlU -> KEEPCAP */ | |
300 | 2703 return WF_KEEPCAP; |
236 | 2704 past_second = TRUE; |
2705 } | |
2706 | |
2707 if (allcap) | |
300 | 2708 return WF_ALLCAP; |
236 | 2709 if (firstcap) |
300 | 2710 return WF_ONECAP; |
236 | 2711 return 0; |
2712 } | |
2713 | |
474 | 2714 /* |
2715 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a | |
2716 * capital. So that make_case_word() can turn WOrd into Word. | |
2717 * Add ALLCAP for "WOrD". | |
2718 */ | |
2719 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2720 badword_captype(char_u *word, char_u *end) |
474 | 2721 { |
2722 int flags = captype(word, end); | |
475 | 2723 int c; |
474 | 2724 int l, u; |
2725 int first; | |
2726 char_u *p; | |
2727 | |
2728 if (flags & WF_KEEPCAP) | |
2729 { | |
2730 /* Count the number of UPPER and lower case letters. */ | |
2731 l = u = 0; | |
2732 first = FALSE; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
2733 for (p = word; p < end; MB_PTR_ADV(p)) |
474 | 2734 { |
475 | 2735 c = PTR2CHAR(p); |
2736 if (SPELL_ISUPPER(c)) | |
474 | 2737 { |
2738 ++u; | |
2739 if (p == word) | |
2740 first = TRUE; | |
2741 } | |
2742 else | |
2743 ++l; | |
2744 } | |
2745 | |
2746 /* If there are more UPPER than lower case letters suggest an | |
2747 * ALLCAP word. Otherwise, if the first letter is UPPER then | |
2748 * suggest ONECAP. Exception: "ALl" most likely should be "All", | |
2749 * require three upper case letters. */ | |
2750 if (u > l && u > 2) | |
2751 flags |= WF_ALLCAP; | |
2752 else if (first) | |
2753 flags |= WF_ONECAP; | |
633 | 2754 |
2755 if (u >= 2 && l >= 2) /* maCARONI maCAroni */ | |
2756 flags |= WF_MIXCAP; | |
474 | 2757 } |
2758 return flags; | |
2759 } | |
2760 | |
5519 | 2761 /* |
2762 * Delete the internal wordlist and its .spl file. | |
2763 */ | |
2764 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2765 spell_delete_wordlist(void) |
5519 | 2766 { |
2767 char_u fname[MAXPATHL]; | |
2768 | |
2769 if (int_wordlist != NULL) | |
2770 { | |
2771 mch_remove(int_wordlist); | |
2772 int_wordlist_spl(fname); | |
2773 mch_remove(fname); | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2774 VIM_CLEAR(int_wordlist); |
5519 | 2775 } |
2776 } | |
2777 | |
355 | 2778 /* |
2779 * Free all languages. | |
2780 */ | |
2781 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2782 spell_free_all(void) |
355 | 2783 { |
503 | 2784 slang_T *slang; |
355 | 2785 buf_T *buf; |
2786 | |
2580 | 2787 /* 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
|
2788 FOR_ALL_BUFFERS(buf) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2789 ga_clear(&buf->b_s.b_langp); |
355 | 2790 |
2791 while (first_lang != NULL) | |
2792 { | |
503 | 2793 slang = first_lang; |
2794 first_lang = slang->sl_next; | |
2795 slang_free(slang); | |
355 | 2796 } |
366 | 2797 |
5519 | 2798 spell_delete_wordlist(); |
381 | 2799 |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2800 VIM_CLEAR(repl_to); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
2801 VIM_CLEAR(repl_from); |
355 | 2802 } |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
2803 |
236 | 2804 /* |
2805 * Clear all spelling tables and reload them. | |
307 | 2806 * Used after 'encoding' is set and when ":mkspell" was used. |
236 | 2807 */ |
2808 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2809 spell_reload(void) |
236 | 2810 { |
316 | 2811 win_T *wp; |
236 | 2812 |
358 | 2813 /* Initialize the table for spell_iswordp(). */ |
236 | 2814 init_spell_chartab(); |
2815 | |
2816 /* Unload all allocated memory. */ | |
355 | 2817 spell_free_all(); |
236 | 2818 |
2819 /* 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
|
2820 FOR_ALL_WINDOWS(wp) |
236 | 2821 { |
316 | 2822 /* Only load the wordlists when 'spelllang' is set and there is a |
2823 * 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
|
2824 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
|
2825 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2826 if (wp->w_p_spell) |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2827 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
2828 (void)did_set_spelllang(wp); |
316 | 2829 break; |
2830 } | |
2831 } | |
236 | 2832 } |
2833 } | |
2834 | |
310 | 2835 /* |
625 | 2836 * Opposite of offset2bytes(). |
2837 * "pp" points to the bytes and is advanced over it. | |
2838 * Returns the offset. | |
2839 */ | |
2840 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2841 bytes2offset(char_u **pp) |
625 | 2842 { |
2843 char_u *p = *pp; | |
2844 int nr; | |
2845 int c; | |
2846 | |
2847 c = *p++; | |
2848 if ((c & 0x80) == 0x00) /* 1 byte */ | |
2849 { | |
2850 nr = c - 1; | |
2851 } | |
2852 else if ((c & 0xc0) == 0x80) /* 2 bytes */ | |
2853 { | |
2854 nr = (c & 0x3f) - 1; | |
2855 nr = nr * 255 + (*p++ - 1); | |
2856 } | |
2857 else if ((c & 0xe0) == 0xc0) /* 3 bytes */ | |
2858 { | |
2859 nr = (c & 0x1f) - 1; | |
2860 nr = nr * 255 + (*p++ - 1); | |
2861 nr = nr * 255 + (*p++ - 1); | |
2862 } | |
2863 else /* 4 bytes */ | |
2864 { | |
2865 nr = (c & 0x0f) - 1; | |
2866 nr = nr * 255 + (*p++ - 1); | |
2867 nr = nr * 255 + (*p++ - 1); | |
2868 nr = nr * 255 + (*p++ - 1); | |
2869 } | |
2870 | |
2871 *pp = p; | |
2872 return nr; | |
2873 } | |
2874 | |
2875 | |
2876 /* | |
2877 * Open a spell buffer. This is a nameless buffer that is not in the buffer | |
2878 * list and only contains text lines. Can use a swapfile to reduce memory | |
2879 * use. | |
2880 * Most other fields are invalid! Esp. watch out for string options being | |
2881 * NULL and there is no undo info. | |
2882 * Returns NULL when out of memory. | |
2883 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2884 buf_T * |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2885 open_spellbuf(void) |
625 | 2886 { |
2887 buf_T *buf; | |
2888 | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
2889 buf = ALLOC_CLEAR_ONE(buf_T); |
625 | 2890 if (buf != NULL) |
2891 { | |
2892 buf->b_spell = TRUE; | |
2893 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
|
2894 #ifdef FEAT_CRYPT |
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2895 buf->b_p_key = empty_option; |
7aca84c0cd37
updated for version 7.4a.028
Bram Moolenaar <bram@vim.org>
parents:
4805
diff
changeset
|
2896 #endif |
625 | 2897 ml_open(buf); |
2898 ml_open_file(buf); /* create swap file now */ | |
2899 } | |
2900 return buf; | |
2901 } | |
2902 | |
2903 /* | |
2904 * Close the buffer used for spell info. | |
2905 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2906 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2907 close_spellbuf(buf_T *buf) |
625 | 2908 { |
2909 if (buf != NULL) | |
2910 { | |
2911 ml_close(buf, TRUE); | |
2912 vim_free(buf); | |
2913 } | |
2914 } | |
2915 | |
307 | 2916 /* |
2917 * Init the chartab used for spelling for ASCII. | |
2918 * EBCDIC is not supported! | |
2919 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
2920 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2921 clear_spell_chartab(spelltab_T *sp) |
307 | 2922 { |
324 | 2923 int i; |
307 | 2924 |
2925 /* Init everything to FALSE. */ | |
2926 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw)); | |
2927 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu)); | |
2928 for (i = 0; i < 256; ++i) | |
324 | 2929 { |
307 | 2930 sp->st_fold[i] = i; |
324 | 2931 sp->st_upper[i] = i; |
2932 } | |
307 | 2933 |
2934 /* We include digits. A word shouldn't start with a digit, but handling | |
2935 * that is done separately. */ | |
2936 for (i = '0'; i <= '9'; ++i) | |
2937 sp->st_isw[i] = TRUE; | |
2938 for (i = 'A'; i <= 'Z'; ++i) | |
2939 { | |
2940 sp->st_isw[i] = TRUE; | |
2941 sp->st_isu[i] = TRUE; | |
2942 sp->st_fold[i] = i + 0x20; | |
2943 } | |
2944 for (i = 'a'; i <= 'z'; ++i) | |
324 | 2945 { |
307 | 2946 sp->st_isw[i] = TRUE; |
324 | 2947 sp->st_upper[i] = i - 0x20; |
2948 } | |
307 | 2949 } |
2950 | |
2951 /* | |
2952 * Init the chartab used for spelling. Only depends on 'encoding'. | |
2953 * Called once while starting up and when 'encoding' changes. | |
2954 * The default is to use isalpha(), but the spell file should define the word | |
2955 * characters to make it possible that 'encoding' differs from the current | |
390 | 2956 * locale. For utf-8 we don't use isalpha() but our own functions. |
307 | 2957 */ |
2958 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
2959 init_spell_chartab(void) |
307 | 2960 { |
2961 int i; | |
2962 | |
2963 did_set_spelltab = FALSE; | |
2964 clear_spell_chartab(&spelltab); | |
2965 if (enc_dbcs) | |
2966 { | |
2967 /* DBCS: assume double-wide characters are word characters. */ | |
2968 for (i = 128; i <= 255; ++i) | |
2969 if (MB_BYTE2LEN(i) == 2) | |
2970 spelltab.st_isw[i] = TRUE; | |
2971 } | |
324 | 2972 else if (enc_utf8) |
2973 { | |
2974 for (i = 128; i < 256; ++i) | |
2975 { | |
2140
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2976 int f = utf_fold(i); |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2977 int u = utf_toupper(i); |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2978 |
324 | 2979 spelltab.st_isu[i] = utf_isupper(i); |
2980 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
|
2981 /* 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
|
2982 * 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
|
2983 * value for utf-8 to avoid this. */ |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2984 spelltab.st_fold[i] = (f < 256) ? f : i; |
12aba62fa7c6
updated for version 7.2.422
Bram Moolenaar <bram@zimbu.org>
parents:
2046
diff
changeset
|
2985 spelltab.st_upper[i] = (u < 256) ? u : i; |
324 | 2986 } |
2987 } | |
307 | 2988 else |
2989 { | |
324 | 2990 /* Rough guess: use locale-dependent library functions. */ |
307 | 2991 for (i = 128; i < 256; ++i) |
2992 { | |
2993 if (MB_ISUPPER(i)) | |
2994 { | |
324 | 2995 spelltab.st_isw[i] = TRUE; |
307 | 2996 spelltab.st_isu[i] = TRUE; |
2997 spelltab.st_fold[i] = MB_TOLOWER(i); | |
2998 } | |
324 | 2999 else if (MB_ISLOWER(i)) |
3000 { | |
3001 spelltab.st_isw[i] = TRUE; | |
3002 spelltab.st_upper[i] = MB_TOUPPER(i); | |
3003 } | |
307 | 3004 } |
3005 } | |
3006 } | |
3007 | |
3008 | |
3009 /* | |
358 | 3010 * Return TRUE if "p" points to a word character. |
366 | 3011 * As a special case we see "midword" characters as word character when it is |
358 | 3012 * followed by a word character. This finds they'there but not 'they there'. |
366 | 3013 * Thus this only works properly when past the first character of the word. |
358 | 3014 */ |
3015 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3016 spell_iswordp( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3017 char_u *p, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3018 win_T *wp) /* buffer used */ |
358 | 3019 { |
3020 char_u *s; | |
366 | 3021 int l; |
3022 int c; | |
3023 | |
3024 if (has_mbyte) | |
3025 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
3026 l = MB_PTR2LEN(p); |
358 | 3027 s = p; |
366 | 3028 if (l == 1) |
3029 { | |
3030 /* be quick for ASCII */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3031 if (wp->w_s->b_spell_ismw[*p]) |
366 | 3032 s = p + 1; /* skip a mid-word character */ |
3033 } | |
3034 else | |
3035 { | |
3036 c = mb_ptr2char(p); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3037 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
|
3038 : (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
|
3039 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL)) |
366 | 3040 s = p + l; |
3041 } | |
3042 | |
390 | 3043 c = mb_ptr2char(s); |
3044 if (c > 255) | |
5477 | 3045 return spell_mb_isword_class(mb_get_class(s), wp); |
390 | 3046 return spelltab.st_isw[c]; |
366 | 3047 } |
3048 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3049 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]]; |
376 | 3050 } |
3051 | |
3052 /* | |
3053 * Return TRUE if "p" points to a word character. | |
3054 * Unlike spell_iswordp() this doesn't check for "midword" characters. | |
3055 */ | |
17789
0f7ae8010787
patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents:
17653
diff
changeset
|
3056 static int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3057 spell_iswordp_nmw(char_u *p, win_T *wp) |
376 | 3058 { |
390 | 3059 int c; |
3060 | |
3061 if (has_mbyte) | |
3062 { | |
3063 c = mb_ptr2char(p); | |
3064 if (c > 255) | |
5477 | 3065 return spell_mb_isword_class(mb_get_class(p), wp); |
390 | 3066 return spelltab.st_isw[c]; |
3067 } | |
376 | 3068 return spelltab.st_isw[*p]; |
358 | 3069 } |
3070 | |
372 | 3071 /* |
1580 | 3072 * Return TRUE if word class indicates a word character. |
3073 * Only for characters above 255. | |
3074 * Unicode subscript and superscript are not considered word characters. | |
5477 | 3075 * See also dbcs_class() and utf_class() in mbyte.c. |
3076 */ | |
3077 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3078 spell_mb_isword_class(int cl, win_T *wp) |
5477 | 3079 { |
3080 if (wp->w_s->b_cjk) | |
3081 /* East Asian characters are not considered word characters. */ | |
3082 return cl == 2 || cl == 0x2800; | |
17434
26e8d42987ca
patch 8.1.1715: emoji characters are seen as word characters for spelling
Bram Moolenaar <Bram@vim.org>
parents:
16825
diff
changeset
|
3083 return cl >= 2 && cl != 0x2070 && cl != 0x2080 && cl != 3; |
1580 | 3084 } |
3085 | |
3086 /* | |
372 | 3087 * Return TRUE if "p" points to a word character. |
3088 * Wide version of spell_iswordp(). | |
3089 */ | |
3090 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3091 spell_iswordp_w(int *p, win_T *wp) |
372 | 3092 { |
3093 int *s; | |
3094 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3095 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
|
3096 : (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
|
3097 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL)) |
372 | 3098 s = p + 1; |
3099 else | |
3100 s = p; | |
3101 | |
390 | 3102 if (*s > 255) |
372 | 3103 { |
3104 if (enc_utf8) | |
5477 | 3105 return spell_mb_isword_class(utf_class(*s), wp); |
372 | 3106 if (enc_dbcs) |
5477 | 3107 return spell_mb_isword_class( |
3108 dbcs_class((unsigned)*s >> 8, *s & 0xff), wp); | |
372 | 3109 return 0; |
3110 } | |
3111 return spelltab.st_isw[*s]; | |
3112 } | |
3113 | |
358 | 3114 /* |
324 | 3115 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated. |
3116 * Uses the character definitions from the .spl file. | |
307 | 3117 * When using a multi-byte 'encoding' the length may change! |
3118 * Returns FAIL when something wrong. | |
3119 */ | |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3120 int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3121 spell_casefold( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3122 char_u *str, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3123 int len, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3124 char_u *buf, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3125 int buflen) |
307 | 3126 { |
3127 int i; | |
3128 | |
3129 if (len >= buflen) | |
3130 { | |
3131 buf[0] = NUL; | |
3132 return FAIL; /* result will not fit */ | |
3133 } | |
3134 | |
3135 if (has_mbyte) | |
3136 { | |
324 | 3137 int outi = 0; |
3138 char_u *p; | |
307 | 3139 int c; |
3140 | |
3141 /* Fold one character at a time. */ | |
324 | 3142 for (p = str; p < str + len; ) |
307 | 3143 { |
3144 if (outi + MB_MAXBYTES > buflen) | |
3145 { | |
3146 buf[outi] = NUL; | |
3147 return FAIL; | |
3148 } | |
474 | 3149 c = mb_cptr2char_adv(&p); |
324 | 3150 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi); |
307 | 3151 } |
3152 buf[outi] = NUL; | |
3153 } | |
3154 else | |
3155 { | |
3156 /* Be quick for non-multibyte encodings. */ | |
3157 for (i = 0; i < len; ++i) | |
324 | 3158 buf[i] = spelltab.st_fold[str[i]]; |
307 | 3159 buf[i] = NUL; |
3160 } | |
3161 | |
3162 return OK; | |
3163 } | |
3164 | |
625 | 3165 /* values for sps_flags */ |
372 | 3166 #define SPS_BEST 1 |
3167 #define SPS_FAST 2 | |
3168 #define SPS_DOUBLE 4 | |
3169 | |
625 | 3170 static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */ |
3171 static int sps_limit = 9999; /* max nr of suggestions given */ | |
372 | 3172 |
3173 /* | |
3174 * Check the 'spellsuggest' option. Return FAIL if it's wrong. | |
483 | 3175 * Sets "sps_flags" and "sps_limit". |
372 | 3176 */ |
3177 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3178 spell_check_sps(void) |
372 | 3179 { |
3180 char_u *p; | |
483 | 3181 char_u *s; |
372 | 3182 char_u buf[MAXPATHL]; |
3183 int f; | |
3184 | |
3185 sps_flags = 0; | |
483 | 3186 sps_limit = 9999; |
372 | 3187 |
3188 for (p = p_sps; *p != NUL; ) | |
3189 { | |
3190 copy_option_part(&p, buf, MAXPATHL, ","); | |
3191 | |
3192 f = 0; | |
483 | 3193 if (VIM_ISDIGIT(*buf)) |
3194 { | |
3195 s = buf; | |
3196 sps_limit = getdigits(&s); | |
3197 if (*s != NUL && !VIM_ISDIGIT(*s)) | |
3198 f = -1; | |
3199 } | |
3200 else if (STRCMP(buf, "best") == 0) | |
372 | 3201 f = SPS_BEST; |
3202 else if (STRCMP(buf, "fast") == 0) | |
3203 f = SPS_FAST; | |
3204 else if (STRCMP(buf, "double") == 0) | |
3205 f = SPS_DOUBLE; | |
3206 else if (STRNCMP(buf, "expr:", 5) != 0 | |
3207 && STRNCMP(buf, "file:", 5) != 0) | |
3208 f = -1; | |
3209 | |
3210 if (f == -1 || (sps_flags != 0 && f != 0)) | |
3211 { | |
3212 sps_flags = SPS_BEST; | |
483 | 3213 sps_limit = 9999; |
372 | 3214 return FAIL; |
3215 } | |
3216 if (f != 0) | |
3217 sps_flags = f; | |
3218 } | |
3219 | |
3220 if (sps_flags == 0) | |
3221 sps_flags = SPS_BEST; | |
3222 | |
3223 return OK; | |
3224 } | |
3225 | |
323 | 3226 /* |
5374 | 3227 * "z=": Find badly spelled word under or after the cursor. |
323 | 3228 * Give suggestions for the properly spelled word. |
638 | 3229 * In Visual mode use the highlighted word as the bad word. |
485 | 3230 * When "count" is non-zero use that suggestion. |
323 | 3231 */ |
3232 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3233 spell_suggest(int count) |
323 | 3234 { |
3235 char_u *line; | |
3236 pos_T prev_cursor = curwin->w_cursor; | |
3237 char_u wcopy[MAXWLEN + 2]; | |
3238 char_u *p; | |
3239 int i; | |
3240 int c; | |
3241 suginfo_T sug; | |
3242 suggest_T *stp; | |
372 | 3243 int mouse_used; |
388 | 3244 int need_cap; |
483 | 3245 int limit; |
485 | 3246 int selected = count; |
638 | 3247 int badlen = 0; |
1938 | 3248 int msg_scroll_save = msg_scroll; |
638 | 3249 |
3250 if (no_spell_checking(curwin)) | |
3251 return; | |
3252 | |
3253 if (VIsual_active) | |
3254 { | |
3255 /* Use the Visually selected text as the bad word. But reject | |
3256 * a multi-line selection. */ | |
3257 if (curwin->w_cursor.lnum != VIsual.lnum) | |
3258 { | |
6949 | 3259 vim_beep(BO_SPELL); |
638 | 3260 return; |
3261 } | |
3262 badlen = (int)curwin->w_cursor.col - (int)VIsual.col; | |
3263 if (badlen < 0) | |
3264 badlen = -badlen; | |
3265 else | |
3266 curwin->w_cursor.col = VIsual.col; | |
3267 ++badlen; | |
3268 end_visual_mode(); | |
3269 } | |
5735 | 3270 /* Find the start of the badly spelled word. */ |
3271 else if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0 | |
346 | 3272 || curwin->w_cursor.col > prev_cursor.col) |
3273 { | |
3274 /* No bad word or it starts after the cursor: use the word under the | |
3275 * cursor. */ | |
3276 curwin->w_cursor = prev_cursor; | |
3277 line = ml_get_curline(); | |
3278 p = line + curwin->w_cursor.col; | |
3279 /* Backup to before start of word. */ | |
5477 | 3280 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
|
3281 MB_PTR_BACK(line, p); |
346 | 3282 /* Forward to start of word. */ |
5477 | 3283 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
|
3284 MB_PTR_ADV(p); |
346 | 3285 |
5477 | 3286 if (!spell_iswordp_nmw(p, curwin)) /* No word found. */ |
346 | 3287 { |
3288 beep_flush(); | |
3289 return; | |
3290 } | |
835 | 3291 curwin->w_cursor.col = (colnr_T)(p - line); |
323 | 3292 } |
3293 | |
344 | 3294 /* Get the word and its length. */ |
3295 | |
388 | 3296 /* Figure out if the word should be capitalised. */ |
475 | 3297 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col); |
3298 | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3299 /* 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
|
3300 line = vim_strsave(ml_get_curline()); |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3301 if (line == NULL) |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3302 goto skip; |
388 | 3303 |
483 | 3304 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in |
3305 * 'spellsuggest', whatever is smaller. */ | |
3306 if (sps_limit > (int)Rows - 2) | |
3307 limit = (int)Rows - 2; | |
3308 else | |
3309 limit = sps_limit; | |
638 | 3310 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit, |
625 | 3311 TRUE, need_cap, TRUE); |
323 | 3312 |
3313 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
|
3314 msg(_("Sorry, no suggestions")); |
485 | 3315 else if (count > 0) |
3316 { | |
3317 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
|
3318 smsg(_("Sorry, only %ld suggestions"), |
485 | 3319 (long)sug.su_ga.ga_len); |
3320 } | |
323 | 3321 else |
3322 { | |
13244
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
3323 VIM_CLEAR(repl_from); |
ac42c4b11dbc
patch 8.0.1496: clearing a pointer takes two lines
Christian Brabandt <cb@256bit.org>
parents:
12674
diff
changeset
|
3324 VIM_CLEAR(repl_to); |
372 | 3325 |
474 | 3326 #ifdef FEAT_RIGHTLEFT |
3327 /* When 'rightleft' is set the list is drawn right-left. */ | |
3328 cmdmsg_rl = curwin->w_p_rl; | |
3329 if (cmdmsg_rl) | |
3330 msg_col = Columns - 1; | |
3331 #endif | |
3332 | |
323 | 3333 /* List the suggestions. */ |
3334 msg_start(); | |
914 | 3335 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */ |
372 | 3336 lines_left = Rows; /* avoid more prompt */ |
323 | 3337 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"), |
3338 sug.su_badlen, sug.su_badptr); | |
474 | 3339 #ifdef FEAT_RIGHTLEFT |
3340 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0) | |
3341 { | |
3342 /* And now the rabbit from the high hat: Avoid showing the | |
3343 * untranslated message rightleft. */ | |
3344 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC", | |
3345 sug.su_badlen, sug.su_badptr); | |
3346 } | |
3347 #endif | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3348 msg_puts((char *)IObuff); |
323 | 3349 msg_clr_eos(); |
3350 msg_putchar('\n'); | |
346 | 3351 |
323 | 3352 msg_scroll = TRUE; |
3353 for (i = 0; i < sug.su_ga.ga_len; ++i) | |
3354 { | |
344 | 3355 stp = &SUG(sug.su_ga, i); |
323 | 3356 |
3357 /* The suggested word may replace only part of the bad word, add | |
3358 * the not replaced part. */ | |
2768 | 3359 vim_strncpy(wcopy, stp->st_word, MAXWLEN); |
323 | 3360 if (sug.su_badlen > stp->st_orglen) |
625 | 3361 vim_strncpy(wcopy + stp->st_wordlen, |
323 | 3362 sug.su_badptr + stp->st_orglen, |
3363 sug.su_badlen - stp->st_orglen); | |
474 | 3364 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1); |
3365 #ifdef FEAT_RIGHTLEFT | |
3366 if (cmdmsg_rl) | |
3367 rl_mirror(IObuff); | |
3368 #endif | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3369 msg_puts((char *)IObuff); |
474 | 3370 |
3371 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
|
3372 msg_puts((char *)IObuff); |
346 | 3373 |
3374 /* The word may replace more than "su_badlen". */ | |
3375 if (sug.su_badlen < stp->st_orglen) | |
3376 { | |
3377 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""), | |
3378 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
|
3379 msg_puts((char *)IObuff); |
346 | 3380 } |
3381 | |
324 | 3382 if (p_verbose > 0) |
344 | 3383 { |
346 | 3384 /* Add the score. */ |
351 | 3385 if (sps_flags & (SPS_DOUBLE | SPS_BEST)) |
474 | 3386 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)", |
344 | 3387 stp->st_salscore ? "s " : "", |
3388 stp->st_score, stp->st_altscore); | |
3389 else | |
474 | 3390 vim_snprintf((char *)IObuff, IOSIZE, " (%d)", |
346 | 3391 stp->st_score); |
474 | 3392 #ifdef FEAT_RIGHTLEFT |
3393 if (cmdmsg_rl) | |
3394 /* Mirror the numbers, but keep the leading space. */ | |
3395 rl_mirror(IObuff + 1); | |
3396 #endif | |
346 | 3397 msg_advance(30); |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
3398 msg_puts((char *)IObuff); |
344 | 3399 } |
323 | 3400 msg_putchar('\n'); |
3401 } | |
3402 | |
474 | 3403 #ifdef FEAT_RIGHTLEFT |
3404 cmdmsg_rl = FALSE; | |
3405 msg_col = 0; | |
3406 #endif | |
323 | 3407 /* Ask for choice. */ |
485 | 3408 selected = prompt_for_number(&mouse_used); |
372 | 3409 if (mouse_used) |
485 | 3410 selected -= lines_left; |
1938 | 3411 lines_left = Rows; /* avoid more prompt */ |
3412 /* don't delay for 'smd' in normal_cmd() */ | |
3413 msg_scroll = msg_scroll_save; | |
485 | 3414 } |
3415 | |
3416 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK) | |
3417 { | |
3418 /* Save the from and to text for :spellrepall. */ | |
3419 stp = &SUG(sug.su_ga, selected - 1); | |
548 | 3420 if (sug.su_badlen > stp->st_orglen) |
3421 { | |
3422 /* Replacing less than "su_badlen", append the remainder to | |
3423 * repl_to. */ | |
3424 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen); | |
3425 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word, | |
3426 sug.su_badlen - stp->st_orglen, | |
3427 sug.su_badptr + stp->st_orglen); | |
3428 repl_to = vim_strsave(IObuff); | |
3429 } | |
3430 else | |
3431 { | |
3432 /* Replacing su_badlen or more, use the whole word. */ | |
3433 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen); | |
3434 repl_to = vim_strsave(stp->st_word); | |
3435 } | |
485 | 3436 |
3437 /* Replace the word. */ | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
3438 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1); |
485 | 3439 if (p != NULL) |
3440 { | |
835 | 3441 c = (int)(sug.su_badptr - line); |
485 | 3442 mch_memmove(p, line, c); |
3443 STRCPY(p + c, stp->st_word); | |
3444 STRCAT(p, sug.su_badptr + stp->st_orglen); | |
3445 ml_replace(curwin->w_cursor.lnum, p, FALSE); | |
3446 curwin->w_cursor.col = c; | |
3447 | |
3448 /* For redo we use a change-word command. */ | |
3449 ResetRedobuff(); | |
3450 AppendToRedobuff((char_u *)"ciw"); | |
620 | 3451 AppendToRedobuffLit(p + c, |
625 | 3452 stp->st_wordlen + sug.su_badlen - stp->st_orglen); |
485 | 3453 AppendCharToRedobuff(ESC); |
819 | 3454 |
3455 /* After this "p" may be invalid. */ | |
3456 changed_bytes(curwin->w_cursor.lnum, c); | |
485 | 3457 } |
3458 } | |
3459 else | |
3460 curwin->w_cursor = prev_cursor; | |
323 | 3461 |
344 | 3462 spell_find_cleanup(&sug); |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3463 skip: |
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3464 vim_free(line); |
344 | 3465 } |
3466 | |
3467 /* | |
475 | 3468 * Check if the word at line "lnum" column "col" is required to start with a |
3469 * capital. This uses 'spellcapcheck' of the current buffer. | |
3470 */ | |
3471 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3472 check_need_cap(linenr_T lnum, colnr_T col) |
475 | 3473 { |
3474 int need_cap = FALSE; | |
3475 char_u *line; | |
3476 char_u *line_copy = NULL; | |
3477 char_u *p; | |
3478 colnr_T endcol; | |
3479 regmatch_T regmatch; | |
3480 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3481 if (curwin->w_s->b_cap_prog == NULL) |
475 | 3482 return FALSE; |
3483 | |
3484 line = ml_get_curline(); | |
3485 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
|
3486 if (getwhitecols(line) >= (int)col) |
475 | 3487 { |
3488 /* At start of line, check if previous line is empty or sentence | |
3489 * ends there. */ | |
3490 if (lnum == 1) | |
3491 need_cap = TRUE; | |
3492 else | |
3493 { | |
3494 line = ml_get(lnum - 1); | |
3495 if (*skipwhite(line) == NUL) | |
3496 need_cap = TRUE; | |
3497 else | |
3498 { | |
3499 /* Append a space in place of the line break. */ | |
3500 line_copy = concat_str(line, (char_u *)" "); | |
3501 line = line_copy; | |
835 | 3502 endcol = (colnr_T)STRLEN(line); |
475 | 3503 } |
3504 } | |
3505 } | |
3506 else | |
3507 endcol = col; | |
3508 | |
3509 if (endcol > 0) | |
3510 { | |
3511 /* 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
|
3512 regmatch.regprog = curwin->w_s->b_cap_prog; |
475 | 3513 regmatch.rm_ic = FALSE; |
3514 p = line + endcol; | |
3515 for (;;) | |
3516 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
3517 MB_PTR_BACK(line, p); |
5477 | 3518 if (p == line || spell_iswordp_nmw(p, curwin)) |
475 | 3519 break; |
3520 if (vim_regexec(®match, p, 0) | |
3521 && regmatch.endp[0] == line + endcol) | |
3522 { | |
3523 need_cap = TRUE; | |
3524 break; | |
3525 } | |
3526 } | |
6375 | 3527 curwin->w_s->b_cap_prog = regmatch.regprog; |
475 | 3528 } |
3529 | |
3530 vim_free(line_copy); | |
3531 | |
3532 return need_cap; | |
3533 } | |
3534 | |
3535 | |
3536 /* | |
372 | 3537 * ":spellrepall" |
3538 */ | |
3539 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3540 ex_spellrepall(exarg_T *eap UNUSED) |
372 | 3541 { |
3542 pos_T pos = curwin->w_cursor; | |
3543 char_u *frompat; | |
3544 int addlen; | |
3545 char_u *line; | |
3546 char_u *p; | |
3547 int save_ws = p_ws; | |
483 | 3548 linenr_T prev_lnum = 0; |
372 | 3549 |
3550 if (repl_from == NULL || repl_to == NULL) | |
3551 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3552 emsg(_("E752: No previous spell replacement")); |
372 | 3553 return; |
3554 } | |
835 | 3555 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from)); |
3556 | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
3557 frompat = alloc(STRLEN(repl_from) + 7); |
372 | 3558 if (frompat == NULL) |
3559 return; | |
3560 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from); | |
3561 p_ws = FALSE; | |
3562 | |
483 | 3563 sub_nsubs = 0; |
3564 sub_nlines = 0; | |
372 | 3565 curwin->w_cursor.lnum = 0; |
3566 while (!got_int) | |
3567 { | |
11521
578df034735d
patch 8.0.0643: when a pattern search is slow Vim becomes unusable
Christian Brabandt <cb@256bit.org>
parents:
11394
diff
changeset
|
3568 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL, NULL) == 0 |
372 | 3569 || u_save_cursor() == FAIL) |
3570 break; | |
3571 | |
3572 /* Only replace when the right word isn't there yet. This happens | |
3573 * when changing "etc" to "etc.". */ | |
3574 line = ml_get_curline(); | |
3575 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col, | |
3576 repl_to, STRLEN(repl_to)) != 0) | |
3577 { | |
16764
ef00b6bc186b
patch 8.1.1384: using "int" for alloc() often results in compiler warnings
Bram Moolenaar <Bram@vim.org>
parents:
16738
diff
changeset
|
3578 p = alloc(STRLEN(line) + addlen + 1); |
372 | 3579 if (p == NULL) |
3580 break; | |
3581 mch_memmove(p, line, curwin->w_cursor.col); | |
3582 STRCPY(p + curwin->w_cursor.col, repl_to); | |
3583 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from)); | |
3584 ml_replace(curwin->w_cursor.lnum, p, FALSE); | |
3585 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); | |
483 | 3586 |
3587 if (curwin->w_cursor.lnum != prev_lnum) | |
3588 { | |
3589 ++sub_nlines; | |
3590 prev_lnum = curwin->w_cursor.lnum; | |
3591 } | |
3592 ++sub_nsubs; | |
372 | 3593 } |
835 | 3594 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to); |
372 | 3595 } |
3596 | |
3597 p_ws = save_ws; | |
3598 curwin->w_cursor = pos; | |
3599 vim_free(frompat); | |
3600 | |
483 | 3601 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
|
3602 semsg(_("E753: Not found: %s"), repl_from); |
483 | 3603 else |
3604 do_sub_msg(FALSE); | |
372 | 3605 } |
3606 | |
3607 /* | |
344 | 3608 * Find spell suggestions for "word". Return them in the growarray "*gap" as |
3609 * a list of allocated strings. | |
3610 */ | |
3611 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3612 spell_suggest_list( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3613 garray_T *gap, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3614 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3615 int maxcount, /* maximum nr of suggestions */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3616 int need_cap, /* 'spellcapcheck' matched */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3617 int interactive) |
344 | 3618 { |
3619 suginfo_T sug; | |
3620 int i; | |
3621 suggest_T *stp; | |
3622 char_u *wcopy; | |
3623 | |
638 | 3624 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive); |
344 | 3625 |
3626 /* Make room in "gap". */ | |
3627 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1); | |
626 | 3628 if (ga_grow(gap, sug.su_ga.ga_len) == OK) |
3629 { | |
3630 for (i = 0; i < sug.su_ga.ga_len; ++i) | |
3631 { | |
3632 stp = &SUG(sug.su_ga, i); | |
3633 | |
3634 /* The suggested word may replace only part of "word", add the not | |
3635 * replaced part. */ | |
3636 wcopy = alloc(stp->st_wordlen | |
835 | 3637 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1); |
626 | 3638 if (wcopy == NULL) |
3639 break; | |
3640 STRCPY(wcopy, stp->st_word); | |
3641 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen); | |
3642 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy; | |
3643 } | |
344 | 3644 } |
3645 | |
3646 spell_find_cleanup(&sug); | |
3647 } | |
3648 | |
3649 /* | |
3650 * Find spell suggestions for the word at the start of "badptr". | |
3651 * Return the suggestions in "su->su_ga". | |
3652 * The maximum number of suggestions is "maxcount". | |
3653 * Note: does use info for the current window. | |
3654 * This is based on the mechanisms of Aspell, but completely reimplemented. | |
3655 */ | |
3656 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3657 spell_find_suggest( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3658 char_u *badptr, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3659 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
|
3660 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3661 int maxcount, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3662 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
|
3663 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
|
3664 int interactive) |
344 | 3665 { |
534 | 3666 hlf_T attr = HLF_COUNT; |
372 | 3667 char_u buf[MAXPATHL]; |
3668 char_u *p; | |
3669 int do_combine = FALSE; | |
3670 char_u *sps_copy; | |
3671 #ifdef FEAT_EVAL | |
3672 static int expr_busy = FALSE; | |
3673 #endif | |
385 | 3674 int c; |
503 | 3675 int i; |
3676 langp_T *lp; | |
344 | 3677 |
3678 /* | |
3679 * Set the info in "*su". | |
3680 */ | |
3681 vim_memset(su, 0, sizeof(suginfo_T)); | |
3682 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10); | |
3683 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10); | |
355 | 3684 if (*badptr == NUL) |
3685 return; | |
344 | 3686 hash_init(&su->su_banned); |
3687 | |
3688 su->su_badptr = badptr; | |
638 | 3689 if (badlen != 0) |
3690 su->su_badlen = badlen; | |
3691 else | |
3692 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE); | |
344 | 3693 su->su_maxcount = maxcount; |
372 | 3694 su->su_maxscore = SCORE_MAXINIT; |
344 | 3695 |
3696 if (su->su_badlen >= MAXWLEN) | |
3697 su->su_badlen = MAXWLEN - 1; /* just in case */ | |
3698 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen); | |
3699 (void)spell_casefold(su->su_badptr, su->su_badlen, | |
3700 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
|
3701 /* 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
|
3702 * 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
|
3703 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
|
3704 |
346 | 3705 /* get caps flags for bad word */ |
474 | 3706 su->su_badflags = badword_captype(su->su_badptr, |
3707 su->su_badptr + su->su_badlen); | |
388 | 3708 if (need_cap) |
3709 su->su_badflags |= WF_ONECAP; | |
344 | 3710 |
503 | 3711 /* Find the default language for sound folding. We simply use the first |
3712 * one in 'spelllang' that supports sound folding. That's good for when | |
3713 * using multiple files for one language, it's not that bad when mixing | |
3714 * languages (e.g., "pl,en"). */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3715 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
|
3716 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
3717 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i); |
503 | 3718 if (lp->lp_sallang != NULL) |
3719 { | |
3720 su->su_sallang = lp->lp_sallang; | |
3721 break; | |
3722 } | |
3723 } | |
3724 | |
534 | 3725 /* Soundfold the bad word with the default sound folding, so that we don't |
3726 * have to do this many times. */ | |
3727 if (su->su_sallang != NULL) | |
3728 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE, | |
3729 su->su_sal_badword); | |
3730 | |
385 | 3731 /* If the word is not capitalised and spell_check() doesn't consider the |
3732 * word to be bad then it might need to be capitalised. Add a suggestion | |
3733 * for that. */ | |
455 | 3734 c = PTR2CHAR(su->su_badptr); |
534 | 3735 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT) |
385 | 3736 { |
3737 make_case_word(su->su_badword, buf, WF_ONECAP); | |
3738 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE, | |
625 | 3739 0, TRUE, su->su_sallang, FALSE); |
385 | 3740 } |
3741 | |
344 | 3742 /* Ban the bad word itself. It may appear in another region. */ |
358 | 3743 if (banbadword) |
3744 add_banned(su, su->su_badword); | |
344 | 3745 |
372 | 3746 /* Make a copy of 'spellsuggest', because the expression may change it. */ |
3747 sps_copy = vim_strsave(p_sps); | |
3748 if (sps_copy == NULL) | |
3749 return; | |
3750 | |
3751 /* Loop over the items in 'spellsuggest'. */ | |
3752 for (p = sps_copy; *p != NUL; ) | |
3753 { | |
3754 copy_option_part(&p, buf, MAXPATHL, ","); | |
3755 | |
3756 if (STRNCMP(buf, "expr:", 5) == 0) | |
3757 { | |
3758 #ifdef FEAT_EVAL | |
375 | 3759 /* Evaluate an expression. Skip this when called recursively, |
3760 * when using spellsuggest() in the expression. */ | |
372 | 3761 if (!expr_busy) |
3762 { | |
3763 expr_busy = TRUE; | |
3764 spell_suggest_expr(su, buf + 5); | |
3765 expr_busy = FALSE; | |
3766 } | |
3767 #endif | |
3768 } | |
3769 else if (STRNCMP(buf, "file:", 5) == 0) | |
3770 /* Use list of suggestions in a file. */ | |
3771 spell_suggest_file(su, buf + 5); | |
3772 else | |
3773 { | |
3774 /* Use internal method. */ | |
625 | 3775 spell_suggest_intern(su, interactive); |
372 | 3776 if (sps_flags & SPS_DOUBLE) |
3777 do_combine = TRUE; | |
3778 } | |
3779 } | |
3780 | |
3781 vim_free(sps_copy); | |
3782 | |
3783 if (do_combine) | |
3784 /* Combine the two list of suggestions. This must be done last, | |
3785 * because sorting changes the order again. */ | |
3786 score_combine(su); | |
3787 } | |
3788 | |
3789 #ifdef FEAT_EVAL | |
3790 /* | |
3791 * Find suggestions by evaluating expression "expr". | |
3792 */ | |
3793 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3794 spell_suggest_expr(suginfo_T *su, char_u *expr) |
372 | 3795 { |
3796 list_T *list; | |
3797 listitem_T *li; | |
3798 int score; | |
3799 char_u *p; | |
3800 | |
3801 /* The work is split up in a few parts to avoid having to export | |
3802 * suginfo_T. | |
3803 * First evaluate the expression and get the resulting list. */ | |
3804 list = eval_spell_expr(su->su_badword, expr); | |
3805 if (list != NULL) | |
3806 { | |
3807 /* Loop over the items in the list. */ | |
3808 for (li = list->lv_first; li != NULL; li = li->li_next) | |
3809 if (li->li_tv.v_type == VAR_LIST) | |
3810 { | |
3811 /* Get the word and the score from the items. */ | |
3812 score = get_spellword(li->li_tv.vval.v_list, &p); | |
625 | 3813 if (score >= 0 && score <= su->su_maxscore) |
3814 add_suggestion(su, &su->su_ga, p, su->su_badlen, | |
3815 score, 0, TRUE, su->su_sallang, FALSE); | |
372 | 3816 } |
3817 list_unref(list); | |
3818 } | |
3819 | |
625 | 3820 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3821 check_suggestions(su, &su->su_ga); | |
372 | 3822 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
3823 } | |
3824 #endif | |
3825 | |
3826 /* | |
474 | 3827 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'. |
372 | 3828 */ |
3829 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3830 spell_suggest_file(suginfo_T *su, char_u *fname) |
372 | 3831 { |
3832 FILE *fd; | |
3833 char_u line[MAXWLEN * 2]; | |
3834 char_u *p; | |
3835 int len; | |
3836 char_u cword[MAXWLEN]; | |
3837 | |
3838 /* Open the file. */ | |
3839 fd = mch_fopen((char *)fname, "r"); | |
3840 if (fd == NULL) | |
3841 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15034
diff
changeset
|
3842 semsg(_(e_notopen), fname); |
372 | 3843 return; |
3844 } | |
3845 | |
3846 /* Read it line by line. */ | |
3847 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int) | |
3848 { | |
3849 line_breakcheck(); | |
3850 | |
3851 p = vim_strchr(line, '/'); | |
3852 if (p == NULL) | |
3853 continue; /* No Tab found, just skip the line. */ | |
3854 *p++ = NUL; | |
3855 if (STRICMP(su->su_badword, line) == 0) | |
3856 { | |
3857 /* Match! Isolate the good word, until CR or NL. */ | |
3858 for (len = 0; p[len] >= ' '; ++len) | |
3859 ; | |
3860 p[len] = NUL; | |
3861 | |
3862 /* If the suggestion doesn't have specific case duplicate the case | |
3863 * of the bad word. */ | |
3864 if (captype(p, NULL) == 0) | |
3865 { | |
3866 make_case_word(p, cword, su->su_badflags); | |
3867 p = cword; | |
3868 } | |
3869 | |
3870 add_suggestion(su, &su->su_ga, p, su->su_badlen, | |
625 | 3871 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE); |
372 | 3872 } |
3873 } | |
3874 | |
3875 fclose(fd); | |
3876 | |
625 | 3877 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3878 check_suggestions(su, &su->su_ga); | |
372 | 3879 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
3880 } | |
3881 | |
3882 /* | |
3883 * Find suggestions for the internal method indicated by "sps_flags". | |
3884 */ | |
3885 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3886 spell_suggest_intern(suginfo_T *su, int interactive) |
625 | 3887 { |
3888 /* | |
3889 * Load the .sug file(s) that are available and not done yet. | |
3890 */ | |
3891 suggest_load_files(); | |
3892 | |
344 | 3893 /* |
346 | 3894 * 1. Try special cases, such as repeating a word: "the the" -> "the". |
344 | 3895 * |
3896 * Set a maximum score to limit the combination of operations that is | |
3897 * tried. | |
3898 */ | |
346 | 3899 suggest_try_special(su); |
3900 | |
3901 /* | |
3902 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries | |
3903 * from the .aff file and inserting a space (split the word). | |
3904 */ | |
3905 suggest_try_change(su); | |
344 | 3906 |
3907 /* For the resulting top-scorers compute the sound-a-like score. */ | |
3908 if (sps_flags & SPS_DOUBLE) | |
3909 score_comp_sal(su); | |
3910 | |
3911 /* | |
346 | 3912 * 3. Try finding sound-a-like words. |
344 | 3913 */ |
625 | 3914 if ((sps_flags & SPS_FAST) == 0) |
3915 { | |
3916 if (sps_flags & SPS_BEST) | |
3917 /* Adjust the word score for the suggestions found so far for how | |
3918 * they sounds like. */ | |
3919 rescore_suggestions(su); | |
3920 | |
3921 /* | |
2046
d433f43849ba
updated for version 7.2.332
Bram Moolenaar <bram@zimbu.org>
parents:
1938
diff
changeset
|
3922 * While going through the soundfold tree "su_maxscore" is the score |
625 | 3923 * for the soundfold word, limits the changes that are being tried, |
3924 * and "su_sfmaxscore" the rescored score, which is set by | |
3925 * cleanup_suggestions(). | |
3926 * First find words with a small edit distance, because this is much | |
3927 * faster and often already finds the top-N suggestions. If we didn't | |
3928 * find many suggestions try again with a higher edit distance. | |
3929 * "sl_sounddone" is used to avoid doing the same word twice. | |
3930 */ | |
3931 suggest_try_soundalike_prep(); | |
3932 su->su_maxscore = SCORE_SFMAX1; | |
3933 su->su_sfmaxscore = SCORE_MAXINIT * 3; | |
346 | 3934 suggest_try_soundalike(su); |
625 | 3935 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su)) |
3936 { | |
3937 /* We didn't find enough matches, try again, allowing more | |
3938 * changes to the soundfold word. */ | |
3939 su->su_maxscore = SCORE_SFMAX2; | |
3940 suggest_try_soundalike(su); | |
3941 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su)) | |
3942 { | |
3943 /* Still didn't find enough matches, try again, allowing even | |
3944 * more changes to the soundfold word. */ | |
3945 su->su_maxscore = SCORE_SFMAX3; | |
3946 suggest_try_soundalike(su); | |
3947 } | |
3948 } | |
3949 su->su_maxscore = su->su_sfmaxscore; | |
3950 suggest_try_soundalike_finish(); | |
3951 } | |
3952 | |
3953 /* When CTRL-C was hit while searching do show the results. Only clear | |
3954 * got_int when using a command, not for spellsuggest(). */ | |
344 | 3955 ui_breakcheck(); |
625 | 3956 if (interactive && got_int) |
344 | 3957 { |
3958 (void)vgetc(); | |
3959 got_int = FALSE; | |
3960 } | |
3961 | |
372 | 3962 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0) |
344 | 3963 { |
3964 if (sps_flags & SPS_BEST) | |
3965 /* Adjust the word score for how it sounds like. */ | |
3966 rescore_suggestions(su); | |
3967 | |
625 | 3968 /* Remove bogus suggestions, sort and truncate at "maxcount". */ |
3969 check_suggestions(su, &su->su_ga); | |
372 | 3970 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
344 | 3971 } |
3972 } | |
3973 | |
3974 /* | |
3975 * Free the info put in "*su" by spell_find_suggest(). | |
3976 */ | |
3977 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
3978 spell_find_cleanup(suginfo_T *su) |
344 | 3979 { |
3980 int i; | |
3981 | |
3982 /* Free the suggestions. */ | |
3983 for (i = 0; i < su->su_ga.ga_len; ++i) | |
3984 vim_free(SUG(su->su_ga, i).st_word); | |
3985 ga_clear(&su->su_ga); | |
3986 for (i = 0; i < su->su_sga.ga_len; ++i) | |
3987 vim_free(SUG(su->su_sga, i).st_word); | |
3988 ga_clear(&su->su_sga); | |
323 | 3989 |
3990 /* Free the banned words. */ | |
625 | 3991 hash_clear_all(&su->su_banned, 0); |
323 | 3992 } |
3993 | |
3994 /* | |
324 | 3995 * Make a copy of "word", with the first letter upper or lower cased, to |
3996 * "wcopy[MAXWLEN]". "word" must not be empty. | |
3997 * The result is NUL terminated. | |
323 | 3998 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
3999 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4000 onecap_copy( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4001 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4002 char_u *wcopy, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4003 int upper) /* TRUE: first letter made upper case */ |
323 | 4004 { |
4005 char_u *p; | |
4006 int c; | |
4007 int l; | |
4008 | |
4009 p = word; | |
4010 if (has_mbyte) | |
474 | 4011 c = mb_cptr2char_adv(&p); |
323 | 4012 else |
4013 c = *p++; | |
4014 if (upper) | |
324 | 4015 c = SPELL_TOUPPER(c); |
323 | 4016 else |
324 | 4017 c = SPELL_TOFOLD(c); |
323 | 4018 if (has_mbyte) |
4019 l = mb_char2bytes(c, wcopy); | |
4020 else | |
4021 { | |
4022 l = 1; | |
4023 wcopy[0] = c; | |
4024 } | |
376 | 4025 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1); |
323 | 4026 } |
4027 | |
4028 /* | |
324 | 4029 * Make a copy of "word" with all the letters upper cased into |
4030 * "wcopy[MAXWLEN]". The result is NUL terminated. | |
323 | 4031 */ |
4032 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4033 allcap_copy(char_u *word, char_u *wcopy) |
323 | 4034 { |
4035 char_u *s; | |
4036 char_u *d; | |
4037 int c; | |
4038 | |
4039 d = wcopy; | |
4040 for (s = word; *s != NUL; ) | |
4041 { | |
4042 if (has_mbyte) | |
474 | 4043 c = mb_cptr2char_adv(&s); |
323 | 4044 else |
4045 c = *s++; | |
492 | 4046 |
3042 | 4047 /* We only change 0xdf to SS when we are certain latin1 is used. It |
492 | 4048 * would cause weird errors in other 8-bit encodings. */ |
4049 if (enc_latin1like && c == 0xdf) | |
4050 { | |
4051 c = 'S'; | |
4052 if (d - wcopy >= MAXWLEN - 1) | |
4053 break; | |
4054 *d++ = c; | |
4055 } | |
4056 else | |
4057 c = SPELL_TOUPPER(c); | |
323 | 4058 |
4059 if (has_mbyte) | |
4060 { | |
4061 if (d - wcopy >= MAXWLEN - MB_MAXBYTES) | |
4062 break; | |
4063 d += mb_char2bytes(c, d); | |
4064 } | |
4065 else | |
4066 { | |
4067 if (d - wcopy >= MAXWLEN - 1) | |
4068 break; | |
4069 *d++ = c; | |
4070 } | |
4071 } | |
4072 *d = NUL; | |
4073 } | |
4074 | |
4075 /* | |
346 | 4076 * Try finding suggestions by recognizing specific situations. |
4077 */ | |
4078 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4079 suggest_try_special(suginfo_T *su) |
346 | 4080 { |
4081 char_u *p; | |
376 | 4082 size_t len; |
346 | 4083 int c; |
4084 char_u word[MAXWLEN]; | |
4085 | |
4086 /* | |
4087 * Recognize a word that is repeated: "the the". | |
4088 */ | |
4089 p = skiptowhite(su->su_fbadword); | |
4090 len = p - su->su_fbadword; | |
4091 p = skipwhite(p); | |
4092 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0) | |
4093 { | |
4094 /* Include badflags: if the badword is onecap or allcap | |
4095 * use that for the goodword too: "The the" -> "The". */ | |
4096 c = su->su_fbadword[len]; | |
4097 su->su_fbadword[len] = NUL; | |
4098 make_case_word(su->su_fbadword, word, su->su_badflags); | |
4099 su->su_fbadword[len] = c; | |
534 | 4100 |
4101 /* Give a soundalike score of 0, compute the score as if deleting one | |
4102 * character. */ | |
4103 add_suggestion(su, &su->su_ga, word, su->su_badlen, | |
625 | 4104 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE); |
346 | 4105 } |
4106 } | |
4107 | |
4108 /* | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4109 * 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
|
4110 * Output is dumped in "suggestprof". |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4111 */ |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4112 #if 0 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4113 # define SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4114 proftime_T current; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4115 proftime_T total; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4116 proftime_T times[STATE_FINAL + 1]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4117 long counts[STATE_FINAL + 1]; |
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 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4120 prof_init(void) |
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 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
|
4123 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4124 profile_zero(×[i]); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4125 counts[i] = 0; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4126 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4127 profile_start(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4128 profile_start(&total); |
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 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4131 /* call before changing state */ |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4132 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4133 prof_store(state_T state) |
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 profile_end(¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4136 profile_add(×[state], ¤t); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4137 ++counts[state]; |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4138 profile_start(¤t); |
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 # 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
|
4141 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4142 static void |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4143 prof_report(char *name) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4144 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4145 FILE *fd = fopen("suggestprof", "a"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4146 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4147 profile_end(&total); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4148 fprintf(fd, "-----------------------\n"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4149 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
|
4150 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
|
4151 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
|
4152 fclose(fd); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4153 } |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4154 #else |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4155 # define PROF_STORE(state) |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4156 #endif |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4157 |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4158 /* |
323 | 4159 * Try finding suggestions by adding/removing/swapping letters. |
4160 */ | |
4161 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4162 suggest_try_change(suginfo_T *su) |
323 | 4163 { |
4164 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */ | |
625 | 4165 int n; |
4166 char_u *p; | |
4167 int lpi; | |
323 | 4168 langp_T *lp; |
4169 | |
4170 /* We make a copy of the case-folded bad word, so that we can modify it | |
346 | 4171 * to find matches (esp. REP items). Append some more text, changing |
4172 * chars after the bad word may help. */ | |
323 | 4173 STRCPY(fword, su->su_fbadword); |
835 | 4174 n = (int)STRLEN(fword); |
346 | 4175 p = su->su_badptr + su->su_badlen; |
835 | 4176 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n); |
323 | 4177 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4178 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
|
4179 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4180 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
482 | 4181 |
500 | 4182 /* If reloading a spell file fails it's still in the list but |
4183 * everything has been cleared. */ | |
625 | 4184 if (lp->lp_slang->sl_fbyts == NULL) |
500 | 4185 continue; |
4186 | |
625 | 4187 /* 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
|
4188 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4189 prof_init(); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4190 #endif |
625 | 4191 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
|
4192 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4193 prof_report("try_change"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4194 #endif |
625 | 4195 } |
4196 } | |
4197 | |
4198 /* Check the maximum score, if we go over it we won't try this change. */ | |
4199 #define TRY_DEEPER(su, stack, depth, add) \ | |
4200 (stack[depth].ts_score + (add) < su->su_maxscore) | |
4201 | |
4202 /* | |
4203 * Try finding suggestions by adding/removing/swapping letters. | |
4204 * | |
4205 * This uses a state machine. At each node in the tree we try various | |
4206 * operations. When trying if an operation works "depth" is increased and the | |
4207 * stack[] is used to store info. This allows combinations, thus insert one | |
4208 * character, replace one and delete another. The number of changes is | |
4209 * limited by su->su_maxscore. | |
4210 * | |
4211 * After implementing this I noticed an article by Kemal Oflazer that | |
4212 * describes something similar: "Error-tolerant Finite State Recognition with | |
4213 * Applications to Morphological Analysis and Spelling Correction" (1996). | |
4214 * The implementation in the article is simplified and requires a stack of | |
4215 * unknown depth. The implementation here only needs a stack depth equal to | |
4216 * the length of the word. | |
4217 * | |
4218 * This is also used for the sound-folded word, "soundfold" is TRUE then. | |
4219 * The mechanism is the same, but we find a match with a sound-folded word | |
4220 * that comes from one or more original words. Each of these words may be | |
4221 * added, this is done by add_sound_suggest(). | |
4222 * Don't use: | |
4223 * the prefix tree or the keep-case tree | |
4224 * "su->su_badlen" | |
4225 * anything to do with upper and lower case | |
4226 * anything to do with word or non-word characters ("spell_iswordp()") | |
4227 * banned words | |
4228 * word flags (rare, region, compounding) | |
4229 * word splitting for now | |
4230 * "similar_chars()" | |
4231 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep" | |
4232 */ | |
4233 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4234 suggest_trie_walk( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4235 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4236 langp_T *lp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4237 char_u *fword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
4238 int soundfold) |
625 | 4239 { |
4240 char_u tword[MAXWLEN]; /* good word collected so far */ | |
4241 trystate_T stack[MAXWLEN]; | |
4242 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
|
4243 * concatenation of prefix compound |
625 | 4244 * words and split word. NUL terminated |
4245 * when going deeper but not when coming | |
4246 * back. */ | |
4247 char_u compflags[MAXWLEN]; /* compound flags, one for each word */ | |
4248 trystate_T *sp; | |
4249 int newscore; | |
4250 int score; | |
4251 char_u *byts, *fbyts, *pbyts; | |
4252 idx_T *idxs, *fidxs, *pidxs; | |
4253 int depth; | |
4254 int c, c2, c3; | |
4255 int n = 0; | |
4256 int flags; | |
4257 garray_T *gap; | |
4258 idx_T arridx; | |
4259 int len; | |
4260 char_u *p; | |
4261 fromto_T *ftp; | |
4262 int fl = 0, tl; | |
4263 int repextra = 0; /* extra bytes in fword[] from REP item */ | |
4264 slang_T *slang = lp->lp_slang; | |
4265 int fword_ends; | |
4266 int goodword_ends; | |
4267 #ifdef DEBUG_TRIEWALK | |
4268 /* Stores the name of the change made at each level. */ | |
4269 char_u changename[MAXWLEN][80]; | |
4270 #endif | |
4271 int breakcheckcount = 1000; | |
4272 int compound_ok; | |
4273 | |
4274 /* | |
4275 * Go through the whole case-fold tree, try changes at each node. | |
4276 * "tword[]" contains the word collected from nodes in the tree. | |
4277 * "fword[]" the word we are trying to match with (initially the bad | |
4278 * word). | |
4279 */ | |
4280 depth = 0; | |
4281 sp = &stack[0]; | |
4282 vim_memset(sp, 0, sizeof(trystate_T)); | |
4283 sp->ts_curi = 1; | |
4284 | |
4285 if (soundfold) | |
4286 { | |
4287 /* Going through the soundfold tree. */ | |
4288 byts = fbyts = slang->sl_sbyts; | |
4289 idxs = fidxs = slang->sl_sidxs; | |
4290 pbyts = NULL; | |
4291 pidxs = NULL; | |
4292 sp->ts_prefixdepth = PFD_NOPREFIX; | |
4293 sp->ts_state = STATE_START; | |
4294 } | |
4295 else | |
4296 { | |
330 | 4297 /* |
375 | 4298 * When there are postponed prefixes we need to use these first. At |
4299 * the end of the prefix we continue in the case-fold tree. | |
4300 */ | |
482 | 4301 fbyts = slang->sl_fbyts; |
4302 fidxs = slang->sl_fidxs; | |
4303 pbyts = slang->sl_pbyts; | |
4304 pidxs = slang->sl_pidxs; | |
375 | 4305 if (pbyts != NULL) |
4306 { | |
4307 byts = pbyts; | |
4308 idxs = pidxs; | |
482 | 4309 sp->ts_prefixdepth = PFD_PREFIXTREE; |
375 | 4310 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */ |
4311 } | |
4312 else | |
4313 { | |
4314 byts = fbyts; | |
4315 idxs = fidxs; | |
482 | 4316 sp->ts_prefixdepth = PFD_NOPREFIX; |
485 | 4317 sp->ts_state = STATE_START; |
375 | 4318 } |
625 | 4319 } |
4320 | |
4321 /* | |
4322 * Loop to find all suggestions. At each round we either: | |
4323 * - For the current state try one operation, advance "ts_curi", | |
4324 * increase "depth". | |
4325 * - When a state is done go to the next, set "ts_state". | |
4326 * - When all states are tried decrease "depth". | |
4327 */ | |
4328 while (depth >= 0 && !got_int) | |
4329 { | |
4330 sp = &stack[depth]; | |
4331 switch (sp->ts_state) | |
4332 { | |
4333 case STATE_START: | |
4334 case STATE_NOPREFIX: | |
4335 /* | |
4336 * Start of node: Deal with NUL bytes, which means | |
4337 * tword[] may end here. | |
4338 */ | |
4339 arridx = sp->ts_arridx; /* current node in the tree */ | |
4340 len = byts[arridx]; /* bytes in this node */ | |
4341 arridx += sp->ts_curi; /* index of current byte */ | |
4342 | |
4343 if (sp->ts_prefixdepth == PFD_PREFIXTREE) | |
4344 { | |
4345 /* Skip over the NUL bytes, we use them later. */ | |
4346 for (n = 0; n < len && byts[arridx + n] == 0; ++n) | |
4347 ; | |
4348 sp->ts_curi += n; | |
4349 | |
4350 /* Always past NUL bytes now. */ | |
4351 n = (int)sp->ts_state; | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4352 PROF_STORE(sp->ts_state) |
625 | 4353 sp->ts_state = STATE_ENDNUL; |
4354 sp->ts_save_badflags = su->su_badflags; | |
4355 | |
4356 /* At end of a prefix or at start of prefixtree: check for | |
4357 * following word. */ | |
4358 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX) | |
4359 { | |
4360 /* Set su->su_badflags to the caps type at this position. | |
4361 * Use the caps type until here for the prefix itself. */ | |
4362 if (has_mbyte) | |
4363 n = nofold_len(fword, sp->ts_fidx, su->su_badptr); | |
4364 else | |
4365 n = sp->ts_fidx; | |
4366 flags = badword_captype(su->su_badptr, su->su_badptr + n); | |
4367 su->su_badflags = badword_captype(su->su_badptr + n, | |
455 | 4368 su->su_badptr + su->su_badlen); |
625 | 4369 #ifdef DEBUG_TRIEWALK |
4370 sprintf(changename[depth], "prefix"); | |
4371 #endif | |
4372 go_deeper(stack, depth, 0); | |
4373 ++depth; | |
4374 sp = &stack[depth]; | |
4375 sp->ts_prefixdepth = depth - 1; | |
4376 byts = fbyts; | |
4377 idxs = fidxs; | |
4378 sp->ts_arridx = 0; | |
4379 | |
4380 /* Move the prefix to preword[] with the right case | |
4381 * and make find_keepcap_word() works. */ | |
4382 tword[sp->ts_twordlen] = NUL; | |
4383 make_case_word(tword + sp->ts_splitoff, | |
4384 preword + sp->ts_prewordlen, flags); | |
835 | 4385 sp->ts_prewordlen = (char_u)STRLEN(preword); |
625 | 4386 sp->ts_splitoff = sp->ts_twordlen; |
4387 } | |
4388 break; | |
4389 } | |
4390 | |
4391 if (sp->ts_curi > len || byts[arridx] != 0) | |
4392 { | |
4393 /* 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
|
4394 PROF_STORE(sp->ts_state) |
625 | 4395 sp->ts_state = STATE_ENDNUL; |
4396 sp->ts_save_badflags = su->su_badflags; | |
4397 break; | |
4398 } | |
4399 | |
4400 /* | |
4401 * End of word in tree. | |
4402 */ | |
4403 ++sp->ts_curi; /* eat one NUL byte */ | |
4404 | |
4405 flags = (int)idxs[arridx]; | |
698 | 4406 |
4407 /* Skip words with the NOSUGGEST flag. */ | |
4408 if (flags & WF_NOSUGGEST) | |
4409 break; | |
4410 | |
625 | 4411 fword_ends = (fword[sp->ts_fidx] == NUL |
4412 || (soundfold | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
4413 ? VIM_ISWHITE(fword[sp->ts_fidx]) |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4414 : !spell_iswordp(fword + sp->ts_fidx, curwin))); |
625 | 4415 tword[sp->ts_twordlen] = NUL; |
4416 | |
4417 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL | |
485 | 4418 && (sp->ts_flags & TSF_PREFIXOK) == 0) |
625 | 4419 { |
4420 /* There was a prefix before the word. Check that the prefix | |
4421 * can be used with this word. */ | |
4422 /* Count the length of the NULs in the prefix. If there are | |
4423 * none this must be the first try without a prefix. */ | |
4424 n = stack[sp->ts_prefixdepth].ts_arridx; | |
4425 len = pbyts[n++]; | |
4426 for (c = 0; c < len && pbyts[n + c] == 0; ++c) | |
4427 ; | |
4428 if (c > 0) | |
4429 { | |
4430 c = valid_word_prefix(c, n, flags, | |
483 | 4431 tword + sp->ts_splitoff, slang, FALSE); |
625 | 4432 if (c == 0) |
4433 break; | |
4434 | |
4435 /* Use the WF_RARE flag for a rare prefix. */ | |
4436 if (c & WF_RAREPFX) | |
4437 flags |= WF_RARE; | |
4438 | |
4439 /* Tricky: when checking for both prefix and compounding | |
4440 * we run into the prefix flag first. | |
4441 * Remember that it's OK, so that we accept the prefix | |
4442 * when arriving at a compound flag. */ | |
4443 sp->ts_flags |= TSF_PREFIXOK; | |
4444 } | |
4445 } | |
4446 | |
4447 /* Check NEEDCOMPOUND: can't use word without compounding. Do try | |
4448 * appending another compound word below. */ | |
4449 if (sp->ts_complen == sp->ts_compsplit && fword_ends | |
500 | 4450 && (flags & WF_NEEDCOMP)) |
625 | 4451 goodword_ends = FALSE; |
4452 else | |
4453 goodword_ends = TRUE; | |
4454 | |
4455 p = NULL; | |
4456 compound_ok = TRUE; | |
4457 if (sp->ts_complen > sp->ts_compsplit) | |
4458 { | |
4459 if (slang->sl_nobreak) | |
4460 { | |
4461 /* There was a word before this word. When there was no | |
4462 * change in this word (it was correct) add the first word | |
4463 * as a suggestion. If this word was corrected too, we | |
4464 * need to check if a correct word follows. */ | |
4465 if (sp->ts_fidx - sp->ts_splitfidx | |
4466 == sp->ts_twordlen - sp->ts_splitoff | |
4467 && STRNCMP(fword + sp->ts_splitfidx, | |
4468 tword + sp->ts_splitoff, | |
4469 sp->ts_fidx - sp->ts_splitfidx) == 0) | |
492 | 4470 { |
625 | 4471 preword[sp->ts_prewordlen] = NUL; |
4472 newscore = score_wordcount_adj(slang, sp->ts_score, | |
4473 preword + sp->ts_prewordlen, | |
4474 sp->ts_prewordlen > 0); | |
4475 /* Add the suggestion if the score isn't too bad. */ | |
4476 if (newscore <= su->su_maxscore) | |
492 | 4477 add_suggestion(su, &su->su_ga, preword, |
503 | 4478 sp->ts_splitfidx - repextra, |
625 | 4479 newscore, 0, FALSE, |
4480 lp->lp_sallang, FALSE); | |
4481 break; | |
492 | 4482 } |
490 | 4483 } |
4484 else | |
625 | 4485 { |
4486 /* There was a compound word before this word. If this | |
4487 * word does not support compounding then give up | |
4488 * (splitting is tried for the word without compound | |
4489 * flag). */ | |
4490 if (((unsigned)flags >> 24) == 0 | |
4491 || sp->ts_twordlen - sp->ts_splitoff | |
4492 < slang->sl_compminlen) | |
4493 break; | |
4494 /* For multi-byte chars check character length against | |
4495 * COMPOUNDMIN. */ | |
4496 if (has_mbyte | |
4497 && slang->sl_compminlen > 0 | |
4498 && mb_charlen(tword + sp->ts_splitoff) | |
4499 < slang->sl_compminlen) | |
4500 break; | |
4501 | |
4502 compflags[sp->ts_complen] = ((unsigned)flags >> 24); | |
4503 compflags[sp->ts_complen + 1] = NUL; | |
4504 vim_strncpy(preword + sp->ts_prewordlen, | |
4505 tword + sp->ts_splitoff, | |
4506 sp->ts_twordlen - sp->ts_splitoff); | |
1762 | 4507 |
4508 /* Verify CHECKCOMPOUNDPATTERN rules. */ | |
4509 if (match_checkcompoundpattern(preword, sp->ts_prewordlen, | |
4510 &slang->sl_comppat)) | |
4511 compound_ok = FALSE; | |
4512 | |
4513 if (compound_ok) | |
4514 { | |
4515 p = preword; | |
4516 while (*skiptowhite(p) != NUL) | |
4517 p = skipwhite(skiptowhite(p)); | |
4518 if (fword_ends && !can_compound(slang, p, | |
625 | 4519 compflags + sp->ts_compsplit)) |
1762 | 4520 /* Compound is not allowed. But it may still be |
4521 * possible if we add another (short) word. */ | |
4522 compound_ok = FALSE; | |
4523 } | |
625 | 4524 |
4525 /* Get pointer to last char of previous word. */ | |
4526 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
|
4527 MB_PTR_BACK(preword, p); |
625 | 4528 } |
4529 } | |
4530 | |
4531 /* | |
4532 * Form the word with proper case in preword. | |
4533 * If there is a word from a previous split, append. | |
4534 * For the soundfold tree don't change the case, simply append. | |
4535 */ | |
4536 if (soundfold) | |
4537 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff); | |
4538 else if (flags & WF_KEEPCAP) | |
4539 /* Must find the word in the keep-case tree. */ | |
4540 find_keepcap_word(slang, tword + sp->ts_splitoff, | |
483 | 4541 preword + sp->ts_prewordlen); |
625 | 4542 else |
4543 { | |
4544 /* Include badflags: If the badword is onecap or allcap | |
4545 * use that for the goodword too. But if the badword is | |
4546 * allcap and it's only one char long use onecap. */ | |
4547 c = su->su_badflags; | |
4548 if ((c & WF_ALLCAP) | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4549 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)) |
625 | 4550 c = WF_ONECAP; |
4551 c |= flags; | |
4552 | |
4553 /* When appending a compound word after a word character don't | |
4554 * use Onecap. */ | |
5477 | 4555 if (p != NULL && spell_iswordp_nmw(p, curwin)) |
625 | 4556 c &= ~WF_ONECAP; |
4557 make_case_word(tword + sp->ts_splitoff, | |
490 | 4558 preword + sp->ts_prewordlen, c); |
625 | 4559 } |
4560 | |
4561 if (!soundfold) | |
4562 { | |
323 | 4563 /* Don't use a banned word. It may appear again as a good |
4564 * word, thus remember it. */ | |
4565 if (flags & WF_BANNED) | |
4566 { | |
483 | 4567 add_banned(su, preword + sp->ts_prewordlen); |
323 | 4568 break; |
4569 } | |
534 | 4570 if ((sp->ts_complen == sp->ts_compsplit |
625 | 4571 && WAS_BANNED(su, preword + sp->ts_prewordlen)) |
4572 || WAS_BANNED(su, preword)) | |
534 | 4573 { |
4574 if (slang->sl_compprog == NULL) | |
4575 break; | |
4576 /* the word so far was banned but we may try compounding */ | |
4577 goodword_ends = FALSE; | |
4578 } | |
625 | 4579 } |
4580 | |
4581 newscore = 0; | |
4582 if (!soundfold) /* soundfold words don't have flags */ | |
4583 { | |
323 | 4584 if ((flags & WF_REGION) |
390 | 4585 && (((unsigned)flags >> 16) & lp->lp_region) == 0) |
323 | 4586 newscore += SCORE_REGION; |
4587 if (flags & WF_RARE) | |
4588 newscore += SCORE_RARE; | |
4589 | |
346 | 4590 if (!spell_valid_case(su->su_badflags, |
483 | 4591 captype(preword + sp->ts_prewordlen, NULL))) |
323 | 4592 newscore += SCORE_ICASE; |
625 | 4593 } |
4594 | |
4595 /* TODO: how about splitting in the soundfold tree? */ | |
4596 if (fword_ends | |
4597 && goodword_ends | |
4598 && sp->ts_fidx >= sp->ts_fidxtry | |
4599 && compound_ok) | |
4600 { | |
4601 /* The badword also ends: add suggestions. */ | |
4602 #ifdef DEBUG_TRIEWALK | |
4603 if (soundfold && STRCMP(preword, "smwrd") == 0) | |
4604 { | |
4605 int j; | |
4606 | |
4607 /* print the stack of changes that brought us here */ | |
4608 smsg("------ %s -------", fword); | |
4609 for (j = 0; j < depth; ++j) | |
4610 smsg("%s", changename[j]); | |
4611 } | |
4612 #endif | |
4613 if (soundfold) | |
4614 { | |
4615 /* For soundfolded words we need to find the original | |
1188 | 4616 * words, the edit distance and then add them. */ |
625 | 4617 add_sound_suggest(su, preword, sp->ts_score, lp); |
4618 } | |
2457
6c1d10a85398
Avoid illegal memory access in spell suggestion. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2455
diff
changeset
|
4619 else if (sp->ts_fidx > 0) |
625 | 4620 { |
4621 /* Give a penalty when changing non-word char to word | |
4622 * char, e.g., "thes," -> "these". */ | |
366 | 4623 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
|
4624 MB_PTR_BACK(fword, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4625 if (!spell_iswordp(p, curwin)) |
366 | 4626 { |
4627 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
|
4628 MB_PTR_BACK(preword, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
4629 if (spell_iswordp(p, curwin)) |
366 | 4630 newscore += SCORE_NONWORD; |
4631 } | |
4632 | |
625 | 4633 /* Give a bonus to words seen before. */ |
4634 score = score_wordcount_adj(slang, | |
4635 sp->ts_score + newscore, | |
4636 preword + sp->ts_prewordlen, | |
4637 sp->ts_prewordlen > 0); | |
4638 | |
4639 /* Add the suggestion if the score isn't too bad. */ | |
4640 if (score <= su->su_maxscore) | |
633 | 4641 { |
625 | 4642 add_suggestion(su, &su->su_ga, preword, |
4643 sp->ts_fidx - repextra, | |
4644 score, 0, FALSE, lp->lp_sallang, FALSE); | |
633 | 4645 |
4646 if (su->su_badflags & WF_MIXCAP) | |
4647 { | |
4648 /* We really don't know if the word should be | |
4649 * upper or lower case, add both. */ | |
4650 c = captype(preword, NULL); | |
4651 if (c == 0 || c == WF_ALLCAP) | |
4652 { | |
4653 make_case_word(tword + sp->ts_splitoff, | |
4654 preword + sp->ts_prewordlen, | |
4655 c == 0 ? WF_ALLCAP : 0); | |
4656 | |
4657 add_suggestion(su, &su->su_ga, preword, | |
4658 sp->ts_fidx - repextra, | |
4659 score + SCORE_ICASE, 0, FALSE, | |
4660 lp->lp_sallang, FALSE); | |
4661 } | |
4662 } | |
4663 } | |
625 | 4664 } |
4665 } | |
4666 | |
4667 /* | |
4668 * Try word split and/or compounding. | |
4669 */ | |
4670 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends) | |
4671 /* Don't split halfway a character. */ | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4672 && (!has_mbyte || sp->ts_tcharlen == 0)) |
625 | 4673 { |
4674 int try_compound; | |
4675 int try_split; | |
4676 | |
4677 /* If past the end of the bad word don't try a split. | |
4678 * Otherwise try changing the next word. E.g., find | |
4679 * suggestions for "the the" where the second "the" is | |
4680 * different. It's done like a split. | |
4681 * TODO: word split for soundfold words */ | |
4682 try_split = (sp->ts_fidx - repextra < su->su_badlen) | |
4683 && !soundfold; | |
4684 | |
4685 /* Get here in several situations: | |
4686 * 1. The word in the tree ends: | |
4687 * If the word allows compounding try that. Otherwise try | |
4688 * a split by inserting a space. For both check that a | |
4689 * valid words starts at fword[sp->ts_fidx]. | |
4690 * For NOBREAK do like compounding to be able to check if | |
4691 * the next word is valid. | |
4692 * 2. The badword does end, but it was due to a change (e.g., | |
4693 * a swap). No need to split, but do check that the | |
4694 * following word is valid. | |
4695 * 3. The badword and the word in the tree end. It may still | |
4696 * be possible to compound another (short) word. | |
4697 */ | |
4698 try_compound = FALSE; | |
4699 if (!soundfold | |
7526
08c1f73efcde
commit https://github.com/vim/vim/commit/7b877b360532713dc21a0ff3d55a76ac02eaf573
Christian Brabandt <cb@256bit.org>
parents:
7509
diff
changeset
|
4700 && !slang->sl_nocompoundsugs |
625 | 4701 && slang->sl_compprog != NULL |
4702 && ((unsigned)flags >> 24) != 0 | |
4703 && sp->ts_twordlen - sp->ts_splitoff | |
4704 >= slang->sl_compminlen | |
4705 && (!has_mbyte | |
4706 || slang->sl_compminlen == 0 | |
4707 || mb_charlen(tword + sp->ts_splitoff) | |
4708 >= slang->sl_compminlen) | |
4709 && (slang->sl_compsylmax < MAXWLEN | |
4710 || sp->ts_complen + 1 - sp->ts_compsplit | |
4711 < slang->sl_compmax) | |
1762 | 4712 && (can_be_compound(sp, slang, |
4713 compflags, ((unsigned)flags >> 24)))) | |
4714 | |
625 | 4715 { |
4716 try_compound = TRUE; | |
4717 compflags[sp->ts_complen] = ((unsigned)flags >> 24); | |
4718 compflags[sp->ts_complen + 1] = NUL; | |
4719 } | |
4720 | |
4721 /* For NOBREAK we never try splitting, it won't make any word | |
4722 * valid. */ | |
7526
08c1f73efcde
commit https://github.com/vim/vim/commit/7b877b360532713dc21a0ff3d55a76ac02eaf573
Christian Brabandt <cb@256bit.org>
parents:
7509
diff
changeset
|
4723 if (slang->sl_nobreak && !slang->sl_nocompoundsugs) |
625 | 4724 try_compound = TRUE; |
4725 | |
4726 /* If we could add a compound word, and it's also possible to | |
4727 * split at this point, do the split first and set | |
4728 * TSF_DIDSPLIT to avoid doing it again. */ | |
4729 else if (!fword_ends | |
4730 && try_compound | |
4731 && (sp->ts_flags & TSF_DIDSPLIT) == 0) | |
4732 { | |
485 | 4733 try_compound = FALSE; |
625 | 4734 sp->ts_flags |= TSF_DIDSPLIT; |
4735 --sp->ts_curi; /* do the same NUL again */ | |
4736 compflags[sp->ts_complen] = NUL; | |
4737 } | |
4738 else | |
4739 sp->ts_flags &= ~TSF_DIDSPLIT; | |
4740 | |
4741 if (try_split || try_compound) | |
4742 { | |
534 | 4743 if (!try_compound && (!fword_ends || !goodword_ends)) |
485 | 4744 { |
4745 /* If we're going to split need to check that the | |
501 | 4746 * words so far are valid for compounding. If there |
4747 * is only one word it must not have the NEEDCOMPOUND | |
4748 * flag. */ | |
4749 if (sp->ts_complen == sp->ts_compsplit | |
4750 && (flags & WF_NEEDCOMP)) | |
4751 break; | |
490 | 4752 p = preword; |
4753 while (*skiptowhite(p) != NUL) | |
4754 p = skipwhite(skiptowhite(p)); | |
485 | 4755 if (sp->ts_complen > sp->ts_compsplit |
490 | 4756 && !can_compound(slang, p, |
485 | 4757 compflags + sp->ts_compsplit)) |
4758 break; | |
698 | 4759 |
4760 if (slang->sl_nosplitsugs) | |
4761 newscore += SCORE_SPLIT_NO; | |
4762 else | |
4763 newscore += SCORE_SPLIT; | |
625 | 4764 |
4765 /* Give a bonus to words seen before. */ | |
4766 newscore = score_wordcount_adj(slang, newscore, | |
4767 preword + sp->ts_prewordlen, TRUE); | |
482 | 4768 } |
4769 | |
625 | 4770 if (TRY_DEEPER(su, stack, depth, newscore)) |
323 | 4771 { |
625 | 4772 go_deeper(stack, depth, newscore); |
4773 #ifdef DEBUG_TRIEWALK | |
4774 if (!try_compound && !fword_ends) | |
4775 sprintf(changename[depth], "%.*s-%s: split", | |
4776 sp->ts_twordlen, tword, fword + sp->ts_fidx); | |
4777 else | |
4778 sprintf(changename[depth], "%.*s-%s: compound", | |
4779 sp->ts_twordlen, tword, fword + sp->ts_fidx); | |
4780 #endif | |
323 | 4781 /* Save things to be restored at STATE_SPLITUNDO. */ |
346 | 4782 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
|
4783 PROF_STORE(sp->ts_state) |
376 | 4784 sp->ts_state = STATE_SPLITUNDO; |
4785 | |
4786 ++depth; | |
4787 sp = &stack[depth]; | |
323 | 4788 |
482 | 4789 /* Append a space to preword when splitting. */ |
4790 if (!try_compound && !fword_ends) | |
4791 STRCAT(preword, " "); | |
835 | 4792 sp->ts_prewordlen = (char_u)STRLEN(preword); |
483 | 4793 sp->ts_splitoff = sp->ts_twordlen; |
492 | 4794 sp->ts_splitfidx = sp->ts_fidx; |
376 | 4795 |
4796 /* If the badword has a non-word character at this | |
4797 * position skip it. That means replacing the | |
482 | 4798 * non-word character with a space. Always skip a |
534 | 4799 * character when the word ends. But only when the |
4800 * good word can end. */ | |
625 | 4801 if (((!try_compound && !spell_iswordp_nmw(fword |
5477 | 4802 + sp->ts_fidx, |
4803 curwin)) | |
625 | 4804 || fword_ends) |
4805 && fword[sp->ts_fidx] != NUL | |
4806 && goodword_ends) | |
376 | 4807 { |
482 | 4808 int l; |
4809 | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4810 l = MB_PTR2LEN(fword + sp->ts_fidx); |
482 | 4811 if (fword_ends) |
4812 { | |
4813 /* Copy the skipped character to preword. */ | |
483 | 4814 mch_memmove(preword + sp->ts_prewordlen, |
482 | 4815 fword + sp->ts_fidx, l); |
483 | 4816 sp->ts_prewordlen += l; |
4817 preword[sp->ts_prewordlen] = NUL; | |
482 | 4818 } |
4819 else | |
4820 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST; | |
4821 sp->ts_fidx += l; | |
376 | 4822 } |
455 | 4823 |
485 | 4824 /* When compounding include compound flag in |
4825 * compflags[] (already set above). When splitting we | |
4826 * may start compounding over again. */ | |
482 | 4827 if (try_compound) |
483 | 4828 ++sp->ts_complen; |
482 | 4829 else |
485 | 4830 sp->ts_compsplit = sp->ts_complen; |
4831 sp->ts_prefixdepth = PFD_NOPREFIX; | |
482 | 4832 |
455 | 4833 /* set su->su_badflags to the caps type at this |
4834 * position */ | |
324 | 4835 if (has_mbyte) |
455 | 4836 n = nofold_len(fword, sp->ts_fidx, su->su_badptr); |
324 | 4837 else |
455 | 4838 n = sp->ts_fidx; |
474 | 4839 su->su_badflags = badword_captype(su->su_badptr + n, |
455 | 4840 su->su_badptr + su->su_badlen); |
323 | 4841 |
4842 /* Restart at top of the tree. */ | |
376 | 4843 sp->ts_arridx = 0; |
485 | 4844 |
4845 /* If there are postponed prefixes, try these too. */ | |
4846 if (pbyts != NULL) | |
4847 { | |
4848 byts = pbyts; | |
4849 idxs = pidxs; | |
4850 sp->ts_prefixdepth = PFD_PREFIXTREE; | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4851 PROF_STORE(sp->ts_state) |
485 | 4852 sp->ts_state = STATE_NOPREFIX; |
4853 } | |
323 | 4854 } |
4855 } | |
625 | 4856 } |
4857 break; | |
4858 | |
4859 case STATE_SPLITUNDO: | |
4860 /* Undo the changes done for word split or compound word. */ | |
4861 su->su_badflags = sp->ts_save_badflags; | |
4862 | |
4863 /* Continue looking for NUL bytes. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4864 PROF_STORE(sp->ts_state) |
625 | 4865 sp->ts_state = STATE_START; |
4866 | |
4867 /* In case we went into the prefix tree. */ | |
4868 byts = fbyts; | |
4869 idxs = fidxs; | |
4870 break; | |
4871 | |
4872 case STATE_ENDNUL: | |
4873 /* Past the NUL bytes in the node. */ | |
4874 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
|
4875 if (fword[sp->ts_fidx] == NUL && sp->ts_tcharlen == 0) |
625 | 4876 { |
4877 /* 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
|
4878 PROF_STORE(sp->ts_state) |
625 | 4879 sp->ts_state = STATE_DEL; |
323 | 4880 break; |
625 | 4881 } |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
4882 PROF_STORE(sp->ts_state) |
625 | 4883 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
|
4884 /* FALLTHROUGH */ |
625 | 4885 |
4886 case STATE_PLAIN: | |
4887 /* | |
4888 * Go over all possible bytes at this node, add each to tword[] | |
4889 * and use child node. "ts_curi" is the index. | |
4890 */ | |
4891 arridx = sp->ts_arridx; | |
4892 if (sp->ts_curi > byts[arridx]) | |
4893 { | |
4894 /* Done all bytes at this node, do next state. When still at | |
4895 * 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
|
4896 PROF_STORE(sp->ts_state) |
625 | 4897 if (sp->ts_fidx >= sp->ts_fidxtry) |
323 | 4898 sp->ts_state = STATE_DEL; |
4899 else | |
625 | 4900 sp->ts_state = STATE_FINAL; |
4901 } | |
4902 else | |
4903 { | |
4904 arridx += sp->ts_curi++; | |
4905 c = byts[arridx]; | |
4906 | |
4907 /* Normal byte, go one level deeper. If it's not equal to the | |
4908 * byte in the bad word adjust the score. But don't even try | |
4909 * when the byte was already changed. And don't try when we | |
5695 | 4910 * just deleted this byte, accepting it is always cheaper than |
625 | 4911 * delete + substitute. */ |
4912 if (c == fword[sp->ts_fidx] | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
4913 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)) |
625 | 4914 newscore = 0; |
4915 else | |
4916 newscore = SCORE_SUBST; | |
4917 if ((newscore == 0 | |
4918 || (sp->ts_fidx >= sp->ts_fidxtry | |
4919 && ((sp->ts_flags & TSF_DIDDEL) == 0 | |
4920 || c != fword[sp->ts_delidx]))) | |
4921 && TRY_DEEPER(su, stack, depth, newscore)) | |
4922 { | |
4923 go_deeper(stack, depth, newscore); | |
4924 #ifdef DEBUG_TRIEWALK | |
4925 if (newscore > 0) | |
4926 sprintf(changename[depth], "%.*s-%s: subst %c to %c", | |
4927 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
4928 fword[sp->ts_fidx], c); | |
323 | 4929 else |
625 | 4930 sprintf(changename[depth], "%.*s-%s: accept %c", |
4931 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
4932 fword[sp->ts_fidx]); | |
4933 #endif | |
323 | 4934 ++depth; |
625 | 4935 sp = &stack[depth]; |
4936 ++sp->ts_fidx; | |
4937 tword[sp->ts_twordlen++] = c; | |
4938 sp->ts_arridx = idxs[arridx]; | |
4939 if (newscore == SCORE_SUBST) | |
4940 sp->ts_isdiff = DIFF_YES; | |
330 | 4941 if (has_mbyte) |
358 | 4942 { |
625 | 4943 /* Multi-byte characters are a bit complicated to |
4944 * handle: They differ when any of the bytes differ | |
4945 * and then their length may also differ. */ | |
4946 if (sp->ts_tcharlen == 0) | |
4947 { | |
4948 /* First byte. */ | |
4949 sp->ts_tcharidx = 0; | |
4950 sp->ts_tcharlen = MB_BYTE2LEN(c); | |
4951 sp->ts_fcharstart = sp->ts_fidx - 1; | |
4952 sp->ts_isdiff = (newscore != 0) | |
4953 ? DIFF_YES : DIFF_NONE; | |
4954 } | |
4955 else if (sp->ts_isdiff == DIFF_INSERT) | |
4956 /* When inserting trail bytes don't advance in the | |
4957 * bad word. */ | |
4958 --sp->ts_fidx; | |
4959 if (++sp->ts_tcharidx == sp->ts_tcharlen) | |
4960 { | |
4961 /* Last byte of character. */ | |
4962 if (sp->ts_isdiff == DIFF_YES) | |
4963 { | |
4964 /* Correct ts_fidx for the byte length of the | |
4965 * character (we didn't check that before). */ | |
4966 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
|
4967 + MB_PTR2LEN( |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
4968 fword + sp->ts_fcharstart); |
625 | 4969 /* For changing a composing character adjust |
4970 * the score from SCORE_SUBST to | |
4971 * SCORE_SUBCOMP. */ | |
4972 if (enc_utf8 | |
4973 && utf_iscomposing( | |
11269
121d29004998
patch 8.0.0520: using a function pointer while the function is known
Christian Brabandt <cb@256bit.org>
parents:
11129
diff
changeset
|
4974 utf_ptr2char(tword |
625 | 4975 + sp->ts_twordlen |
4976 - sp->ts_tcharlen)) | |
4977 && 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
|
4978 utf_ptr2char(fword |
625 | 4979 + sp->ts_fcharstart))) |
4980 sp->ts_score -= | |
4981 SCORE_SUBST - SCORE_SUBCOMP; | |
4982 | |
4983 /* For a similar character adjust score from | |
4984 * SCORE_SUBST to SCORE_SIMILAR. */ | |
4985 else if (!soundfold | |
4986 && slang->sl_has_map | |
4987 && similar_chars(slang, | |
4988 mb_ptr2char(tword | |
4989 + sp->ts_twordlen | |
4990 - sp->ts_tcharlen), | |
4991 mb_ptr2char(fword | |
4992 + sp->ts_fcharstart))) | |
4993 sp->ts_score -= | |
4994 SCORE_SUBST - SCORE_SIMILAR; | |
4995 } | |
4996 else if (sp->ts_isdiff == DIFF_INSERT | |
4997 && sp->ts_twordlen > sp->ts_tcharlen) | |
4998 { | |
4999 p = tword + sp->ts_twordlen - sp->ts_tcharlen; | |
5000 c = mb_ptr2char(p); | |
5001 if (enc_utf8 && utf_iscomposing(c)) | |
5002 { | |
5003 /* Inserting a composing char doesn't | |
5004 * count that much. */ | |
5005 sp->ts_score -= SCORE_INS - SCORE_INSCOMP; | |
5006 } | |
5007 else | |
5008 { | |
5009 /* If the previous character was the same, | |
5010 * thus doubling a character, give a bonus | |
5011 * to the score. Also for the soundfold | |
5012 * tree (might seem illogical but does | |
5013 * give better scores). */ | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5014 MB_PTR_BACK(tword, p); |
625 | 5015 if (c == mb_ptr2char(p)) |
5016 sp->ts_score -= SCORE_INS | |
5017 - SCORE_INSDUP; | |
5018 } | |
5019 } | |
5020 | |
5021 /* Starting a new char, reset the length. */ | |
5022 sp->ts_tcharlen = 0; | |
5023 } | |
358 | 5024 } |
330 | 5025 else |
358 | 5026 { |
625 | 5027 /* If we found a similar char adjust the score. |
5028 * We do this after calling go_deeper() because | |
5029 * it's slow. */ | |
5030 if (newscore != 0 | |
5031 && !soundfold | |
5032 && slang->sl_has_map | |
5033 && similar_chars(slang, | |
5034 c, fword[sp->ts_fidx - 1])) | |
5035 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR; | |
358 | 5036 } |
625 | 5037 } |
5038 } | |
5039 break; | |
5040 | |
5041 case STATE_DEL: | |
5042 /* When past the first byte of a multi-byte char don't try | |
5043 * delete/insert/swap a character. */ | |
5044 if (has_mbyte && sp->ts_tcharlen > 0) | |
5045 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5046 PROF_STORE(sp->ts_state) |
625 | 5047 sp->ts_state = STATE_FINAL; |
5048 break; | |
5049 } | |
5050 /* | |
5051 * Try skipping one character in the bad word (delete it). | |
5052 */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5053 PROF_STORE(sp->ts_state) |
625 | 5054 sp->ts_state = STATE_INS_PREP; |
5055 sp->ts_curi = 1; | |
5056 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*') | |
5057 /* Deleting a vowel at the start of a word counts less, see | |
5058 * soundalike_score(). */ | |
5059 newscore = 2 * SCORE_DEL / 3; | |
5060 else | |
5061 newscore = SCORE_DEL; | |
5062 if (fword[sp->ts_fidx] != NUL | |
5063 && TRY_DEEPER(su, stack, depth, newscore)) | |
5064 { | |
5065 go_deeper(stack, depth, newscore); | |
5066 #ifdef DEBUG_TRIEWALK | |
5067 sprintf(changename[depth], "%.*s-%s: delete %c", | |
5068 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5069 fword[sp->ts_fidx]); | |
5070 #endif | |
5071 ++depth; | |
5072 | |
5073 /* Remember what character we deleted, so that we can avoid | |
5074 * inserting it again. */ | |
5075 stack[depth].ts_flags |= TSF_DIDDEL; | |
5076 stack[depth].ts_delidx = sp->ts_fidx; | |
5077 | |
5078 /* Advance over the character in fword[]. Give a bonus to the | |
5079 * score if the same character is following "nn" -> "n". It's | |
5080 * a bit illogical for soundfold tree but it does give better | |
5081 * results. */ | |
5082 if (has_mbyte) | |
5083 { | |
5084 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
|
5085 stack[depth].ts_fidx += MB_PTR2LEN(fword + sp->ts_fidx); |
625 | 5086 if (enc_utf8 && utf_iscomposing(c)) |
5087 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP; | |
5088 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx)) | |
5089 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; | |
323 | 5090 } |
5091 else | |
625 | 5092 { |
5093 ++stack[depth].ts_fidx; | |
5094 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1]) | |
5095 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP; | |
323 | 5096 } |
5097 break; | |
625 | 5098 } |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5099 /* FALLTHROUGH */ |
625 | 5100 |
5101 case STATE_INS_PREP: | |
5102 if (sp->ts_flags & TSF_DIDDEL) | |
5103 { | |
5104 /* If we just deleted a byte then inserting won't make sense, | |
5105 * a substitute is always cheaper. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5106 PROF_STORE(sp->ts_state) |
625 | 5107 sp->ts_state = STATE_SWAP; |
5108 break; | |
5109 } | |
5110 | |
5111 /* skip over NUL bytes */ | |
5112 n = sp->ts_arridx; | |
5113 for (;;) | |
5114 { | |
5115 if (sp->ts_curi > byts[n]) | |
5116 { | |
5117 /* 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
|
5118 PROF_STORE(sp->ts_state) |
625 | 5119 sp->ts_state = STATE_SWAP; |
5120 break; | |
5121 } | |
5122 if (byts[n + sp->ts_curi] != NUL) | |
5123 { | |
5124 /* Found a byte to insert. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5125 PROF_STORE(sp->ts_state) |
625 | 5126 sp->ts_state = STATE_INS; |
330 | 5127 break; |
5128 } | |
625 | 5129 ++sp->ts_curi; |
5130 } | |
5131 break; | |
5132 | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5133 /* FALLTHROUGH */ |
625 | 5134 |
5135 case STATE_INS: | |
5136 /* Insert one byte. Repeat this for each possible byte at this | |
5137 * node. */ | |
5138 n = sp->ts_arridx; | |
5139 if (sp->ts_curi > byts[n]) | |
5140 { | |
5141 /* 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
|
5142 PROF_STORE(sp->ts_state) |
625 | 5143 sp->ts_state = STATE_SWAP; |
5144 break; | |
5145 } | |
5146 | |
5147 /* Do one more byte at this node, but: | |
5148 * - Skip NUL bytes. | |
5149 * - Skip the byte if it's equal to the byte in the word, | |
5150 * accepting that byte is always better. | |
5151 */ | |
5152 n += sp->ts_curi++; | |
5153 c = byts[n]; | |
5154 if (soundfold && sp->ts_twordlen == 0 && c == '*') | |
5155 /* Inserting a vowel at the start of a word counts less, | |
5156 * see soundalike_score(). */ | |
5157 newscore = 2 * SCORE_INS / 3; | |
5158 else | |
5159 newscore = SCORE_INS; | |
5160 if (c != fword[sp->ts_fidx] | |
5161 && TRY_DEEPER(su, stack, depth, newscore)) | |
5162 { | |
5163 go_deeper(stack, depth, newscore); | |
5164 #ifdef DEBUG_TRIEWALK | |
5165 sprintf(changename[depth], "%.*s-%s: insert %c", | |
5166 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5167 c); | |
5168 #endif | |
5169 ++depth; | |
5170 sp = &stack[depth]; | |
5171 tword[sp->ts_twordlen++] = c; | |
5172 sp->ts_arridx = idxs[n]; | |
330 | 5173 if (has_mbyte) |
5174 { | |
625 | 5175 fl = MB_BYTE2LEN(c); |
5176 if (fl > 1) | |
330 | 5177 { |
625 | 5178 /* There are following bytes for the same character. |
5179 * We must find all bytes before trying | |
5180 * delete/insert/swap/etc. */ | |
5181 sp->ts_tcharlen = fl; | |
5182 sp->ts_tcharidx = 1; | |
5183 sp->ts_isdiff = DIFF_INSERT; | |
330 | 5184 } |
323 | 5185 } |
5186 else | |
625 | 5187 fl = 1; |
5188 if (fl == 1) | |
5189 { | |
5190 /* If the previous character was the same, thus doubling a | |
5191 * character, give a bonus to the score. Also for | |
5192 * soundfold words (illogical but does give a better | |
5193 * score). */ | |
5194 if (sp->ts_twordlen >= 2 | |
5195 && tword[sp->ts_twordlen - 2] == c) | |
5196 sp->ts_score -= SCORE_INS - SCORE_INSDUP; | |
5197 } | |
5198 } | |
5199 break; | |
5200 | |
5201 case STATE_SWAP: | |
5202 /* | |
5203 * Swap two bytes in the bad word: "12" -> "21". | |
5204 * We change "fword" here, it's changed back afterwards at | |
5205 * STATE_UNSWAP. | |
5206 */ | |
5207 p = fword + sp->ts_fidx; | |
5208 c = *p; | |
5209 if (c == NUL) | |
5210 { | |
5211 /* 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
|
5212 PROF_STORE(sp->ts_state) |
625 | 5213 sp->ts_state = STATE_FINAL; |
5214 break; | |
5215 } | |
5216 | |
5217 /* Don't swap if the first character is not a word character. | |
5218 * 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
|
5219 if (!soundfold && !spell_iswordp(p, curwin)) |
625 | 5220 { |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5221 PROF_STORE(sp->ts_state) |
625 | 5222 sp->ts_state = STATE_REP_INI; |
323 | 5223 break; |
625 | 5224 } |
5225 | |
5226 if (has_mbyte) | |
5227 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5228 n = MB_CPTR2LEN(p); |
625 | 5229 c = mb_ptr2char(p); |
1337 | 5230 if (p[n] == NUL) |
5231 c2 = NUL; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5232 else if (!soundfold && !spell_iswordp(p + n, curwin)) |
625 | 5233 c2 = c; /* don't swap non-word char */ |
5234 else | |
5235 c2 = mb_ptr2char(p + n); | |
5236 } | |
5237 else | |
5238 { | |
1337 | 5239 if (p[1] == NUL) |
5240 c2 = NUL; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5241 else if (!soundfold && !spell_iswordp(p + 1, curwin)) |
625 | 5242 c2 = c; /* don't swap non-word char */ |
5243 else | |
5244 c2 = p[1]; | |
5245 } | |
5246 | |
1337 | 5247 /* When the second character is NUL we can't swap. */ |
5248 if (c2 == NUL) | |
5249 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5250 PROF_STORE(sp->ts_state) |
1337 | 5251 sp->ts_state = STATE_REP_INI; |
5252 break; | |
5253 } | |
5254 | |
625 | 5255 /* When characters are identical, swap won't do anything. |
5256 * Also get here if the second char is not a word character. */ | |
5257 if (c == c2) | |
5258 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5259 PROF_STORE(sp->ts_state) |
625 | 5260 sp->ts_state = STATE_SWAP3; |
5261 break; | |
5262 } | |
5263 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP)) | |
5264 { | |
5265 go_deeper(stack, depth, SCORE_SWAP); | |
5266 #ifdef DEBUG_TRIEWALK | |
5267 sprintf(changename[depth], "%.*s-%s: swap %c and %c", | |
5268 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5269 c, c2); | |
5270 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5271 PROF_STORE(sp->ts_state) |
625 | 5272 sp->ts_state = STATE_UNSWAP; |
5273 ++depth; | |
330 | 5274 if (has_mbyte) |
5275 { | |
625 | 5276 fl = mb_char2len(c2); |
5277 mch_memmove(p, p + n, fl); | |
5278 mb_char2bytes(c, p + fl); | |
5279 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; | |
330 | 5280 } |
5281 else | |
5282 { | |
625 | 5283 p[0] = c2; |
330 | 5284 p[1] = c; |
625 | 5285 stack[depth].ts_fidxtry = sp->ts_fidx + 2; |
5286 } | |
5287 } | |
5288 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5289 { |
625 | 5290 /* 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
|
5291 PROF_STORE(sp->ts_state) |
625 | 5292 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
|
5293 } |
625 | 5294 break; |
5295 | |
5296 case STATE_UNSWAP: | |
5297 /* Undo the STATE_SWAP swap: "21" -> "12". */ | |
5298 p = fword + sp->ts_fidx; | |
5299 if (has_mbyte) | |
5300 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5301 n = MB_PTR2LEN(p); |
625 | 5302 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
|
5303 mch_memmove(p + MB_PTR2LEN(p + n), p, n); |
625 | 5304 mb_char2bytes(c, p); |
5305 } | |
5306 else | |
5307 { | |
5308 c = *p; | |
5309 *p = p[1]; | |
5310 p[1] = c; | |
5311 } | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5312 /* FALLTHROUGH */ |
625 | 5313 |
5314 case STATE_SWAP3: | |
5315 /* Swap two bytes, skipping one: "123" -> "321". We change | |
5316 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */ | |
5317 p = fword + sp->ts_fidx; | |
5318 if (has_mbyte) | |
5319 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5320 n = MB_CPTR2LEN(p); |
625 | 5321 c = mb_ptr2char(p); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5322 fl = MB_CPTR2LEN(p + n); |
625 | 5323 c2 = mb_ptr2char(p + n); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5324 if (!soundfold && !spell_iswordp(p + n + fl, curwin)) |
625 | 5325 c3 = c; /* don't swap non-word char */ |
5326 else | |
5327 c3 = mb_ptr2char(p + n + fl); | |
5328 } | |
5329 else | |
5330 { | |
5331 c = *p; | |
5332 c2 = p[1]; | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5333 if (!soundfold && !spell_iswordp(p + 2, curwin)) |
625 | 5334 c3 = c; /* don't swap non-word char */ |
5335 else | |
5336 c3 = p[2]; | |
5337 } | |
5338 | |
5339 /* When characters are identical: "121" then SWAP3 result is | |
5340 * identical, ROT3L result is same as SWAP: "211", ROT3L result is | |
5341 * same as SWAP on next char: "112". Thus skip all swapping. | |
5342 * Also skip when c3 is NUL. | |
5343 * Also get here when the third character is not a word character. | |
5344 * Second character may any char: "a.b" -> "b.a" */ | |
5345 if (c == c3 || c3 == NUL) | |
5346 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5347 PROF_STORE(sp->ts_state) |
625 | 5348 sp->ts_state = STATE_REP_INI; |
5349 break; | |
5350 } | |
5351 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) | |
5352 { | |
5353 go_deeper(stack, depth, SCORE_SWAP3); | |
5354 #ifdef DEBUG_TRIEWALK | |
5355 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c", | |
5356 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5357 c, c3); | |
5358 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5359 PROF_STORE(sp->ts_state) |
625 | 5360 sp->ts_state = STATE_UNSWAP3; |
5361 ++depth; | |
5362 if (has_mbyte) | |
5363 { | |
5364 tl = mb_char2len(c3); | |
5365 mch_memmove(p, p + n + fl, tl); | |
5366 mb_char2bytes(c2, p + tl); | |
5367 mb_char2bytes(c, p + fl + tl); | |
5368 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl; | |
5369 } | |
5370 else | |
5371 { | |
5372 p[0] = p[2]; | |
5373 p[2] = c; | |
5374 stack[depth].ts_fidxtry = sp->ts_fidx + 3; | |
5375 } | |
5376 } | |
5377 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5378 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5379 PROF_STORE(sp->ts_state) |
625 | 5380 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
|
5381 } |
625 | 5382 break; |
5383 | |
5384 case STATE_UNSWAP3: | |
5385 /* Undo STATE_SWAP3: "321" -> "123" */ | |
5386 p = fword + sp->ts_fidx; | |
5387 if (has_mbyte) | |
5388 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5389 n = MB_PTR2LEN(p); |
625 | 5390 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
|
5391 fl = MB_PTR2LEN(p + n); |
625 | 5392 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
|
5393 tl = MB_PTR2LEN(p + n + fl); |
625 | 5394 mch_memmove(p + fl + tl, p, n); |
5395 mb_char2bytes(c, p); | |
5396 mb_char2bytes(c2, p + tl); | |
5397 p = p + tl; | |
5398 } | |
5399 else | |
5400 { | |
5401 c = *p; | |
5402 *p = p[2]; | |
5403 p[2] = c; | |
5404 ++p; | |
5405 } | |
5406 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5407 if (!soundfold && !spell_iswordp(p, curwin)) |
625 | 5408 { |
5409 /* Middle char is not a word char, skip the rotate. First and | |
5410 * 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
|
5411 PROF_STORE(sp->ts_state) |
625 | 5412 sp->ts_state = STATE_REP_INI; |
5413 break; | |
5414 } | |
5415 | |
5416 /* Rotate three characters left: "123" -> "231". We change | |
5417 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */ | |
5418 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) | |
5419 { | |
5420 go_deeper(stack, depth, SCORE_SWAP3); | |
5421 #ifdef DEBUG_TRIEWALK | |
5422 p = fword + sp->ts_fidx; | |
5423 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c", | |
5424 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5425 p[0], p[1], p[2]); | |
5426 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5427 PROF_STORE(sp->ts_state) |
625 | 5428 sp->ts_state = STATE_UNROT3L; |
5429 ++depth; | |
330 | 5430 p = fword + sp->ts_fidx; |
5431 if (has_mbyte) | |
5432 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5433 n = MB_CPTR2LEN(p); |
330 | 5434 c = mb_ptr2char(p); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5435 fl = MB_CPTR2LEN(p + n); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5436 fl += MB_CPTR2LEN(p + n + fl); |
625 | 5437 mch_memmove(p, p + n, fl); |
5438 mb_char2bytes(c, p + fl); | |
5439 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl; | |
330 | 5440 } |
5441 else | |
323 | 5442 { |
330 | 5443 c = *p; |
625 | 5444 *p = p[1]; |
5445 p[1] = p[2]; | |
5446 p[2] = c; | |
5447 stack[depth].ts_fidxtry = sp->ts_fidx + 3; | |
5448 } | |
5449 } | |
5450 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5451 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5452 PROF_STORE(sp->ts_state) |
625 | 5453 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
|
5454 } |
625 | 5455 break; |
5456 | |
5457 case STATE_UNROT3L: | |
5458 /* Undo ROT3L: "231" -> "123" */ | |
5459 p = fword + sp->ts_fidx; | |
5460 if (has_mbyte) | |
5461 { | |
11394
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5462 n = MB_PTR2LEN(p); |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5463 n += MB_PTR2LEN(p + n); |
625 | 5464 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
|
5465 tl = MB_PTR2LEN(p + n); |
625 | 5466 mch_memmove(p + tl, p, n); |
5467 mb_char2bytes(c, p); | |
5468 } | |
5469 else | |
5470 { | |
5471 c = p[2]; | |
5472 p[2] = p[1]; | |
5473 p[1] = *p; | |
5474 *p = c; | |
5475 } | |
5476 | |
5477 /* Rotate three bytes right: "123" -> "312". We change "fword" | |
5478 * here, it's changed back afterwards at STATE_UNROT3R. */ | |
5479 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3)) | |
5480 { | |
5481 go_deeper(stack, depth, SCORE_SWAP3); | |
5482 #ifdef DEBUG_TRIEWALK | |
5483 p = fword + sp->ts_fidx; | |
5484 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c", | |
5485 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5486 p[0], p[1], p[2]); | |
5487 #endif | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5488 PROF_STORE(sp->ts_state) |
625 | 5489 sp->ts_state = STATE_UNROT3R; |
5490 ++depth; | |
330 | 5491 p = fword + sp->ts_fidx; |
5492 if (has_mbyte) | |
5493 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5494 n = MB_CPTR2LEN(p); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5495 n += MB_CPTR2LEN(p + n); |
330 | 5496 c = mb_ptr2char(p + n); |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5497 tl = MB_CPTR2LEN(p + n); |
330 | 5498 mch_memmove(p + tl, p, n); |
5499 mb_char2bytes(c, p); | |
625 | 5500 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl; |
330 | 5501 } |
5502 else | |
5503 { | |
5504 c = p[2]; | |
5505 p[2] = p[1]; | |
5506 p[1] = *p; | |
5507 *p = c; | |
625 | 5508 stack[depth].ts_fidxtry = sp->ts_fidx + 3; |
5509 } | |
5510 } | |
5511 else | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5512 { |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5513 PROF_STORE(sp->ts_state) |
625 | 5514 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
|
5515 } |
625 | 5516 break; |
5517 | |
5518 case STATE_UNROT3R: | |
5519 /* Undo ROT3R: "312" -> "123" */ | |
5520 p = fword + sp->ts_fidx; | |
5521 if (has_mbyte) | |
5522 { | |
5523 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
|
5524 tl = MB_PTR2LEN(p); |
a2124e9bbb6a
patch 8.0.0582: illegal memory access with z= command
Christian Brabandt <cb@256bit.org>
parents:
11269
diff
changeset
|
5525 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
|
5526 n += MB_PTR2LEN(p + tl + n); |
625 | 5527 mch_memmove(p, p + tl, n); |
5528 mb_char2bytes(c, p + n); | |
5529 } | |
5530 else | |
5531 { | |
5532 c = *p; | |
5533 *p = p[1]; | |
5534 p[1] = p[2]; | |
5535 p[2] = c; | |
5536 } | |
12674
e769c912fcd9
patch 8.0.1215: newer gcc warns for implicit fallthrough
Christian Brabandt <cb@256bit.org>
parents:
12477
diff
changeset
|
5537 /* FALLTHROUGH */ |
625 | 5538 |
5539 case STATE_REP_INI: | |
5540 /* Check if matching with REP items from the .aff file would work. | |
5541 * Quickly skip if: | |
5542 * - there are no REP items and we are not in the soundfold trie | |
5543 * - the score is going to be too high anyway | |
5544 * - already applied a REP item or swapped here */ | |
5545 if ((lp->lp_replang == NULL && !soundfold) | |
5546 || sp->ts_score + SCORE_REP >= su->su_maxscore | |
5547 || sp->ts_fidx < sp->ts_fidxtry) | |
5548 { | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5549 PROF_STORE(sp->ts_state) |
625 | 5550 sp->ts_state = STATE_FINAL; |
323 | 5551 break; |
625 | 5552 } |
5553 | |
5554 /* Use the first byte to quickly find the first entry that may | |
5555 * match. If the index is -1 there is none. */ | |
5556 if (soundfold) | |
5557 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]]; | |
5558 else | |
5559 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]]; | |
5560 | |
5561 if (sp->ts_curi < 0) | |
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_FINAL; |
5565 break; | |
5566 } | |
5567 | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5568 PROF_STORE(sp->ts_state) |
625 | 5569 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
|
5570 /* FALLTHROUGH */ |
625 | 5571 |
5572 case STATE_REP: | |
5573 /* Try matching with REP items from the .aff file. For each match | |
5574 * replace the characters and check if the resulting word is | |
5575 * valid. */ | |
5576 p = fword + sp->ts_fidx; | |
5577 | |
5578 if (soundfold) | |
5579 gap = &slang->sl_repsal; | |
5580 else | |
503 | 5581 gap = &lp->lp_replang->sl_rep; |
625 | 5582 while (sp->ts_curi < gap->ga_len) |
5583 { | |
5584 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; | |
5585 if (*ftp->ft_from != *p) | |
5586 { | |
5587 /* past possible matching entries */ | |
5588 sp->ts_curi = gap->ga_len; | |
323 | 5589 break; |
5590 } | |
625 | 5591 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0 |
5592 && TRY_DEEPER(su, stack, depth, SCORE_REP)) | |
5593 { | |
5594 go_deeper(stack, depth, SCORE_REP); | |
5595 #ifdef DEBUG_TRIEWALK | |
5596 sprintf(changename[depth], "%.*s-%s: replace %s with %s", | |
5597 sp->ts_twordlen, tword, fword + sp->ts_fidx, | |
5598 ftp->ft_from, ftp->ft_to); | |
5599 #endif | |
5600 /* Need to undo this afterwards. */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5601 PROF_STORE(sp->ts_state) |
625 | 5602 sp->ts_state = STATE_REP_UNDO; |
5603 | |
5604 /* Change the "from" to the "to" string. */ | |
5605 ++depth; | |
835 | 5606 fl = (int)STRLEN(ftp->ft_from); |
5607 tl = (int)STRLEN(ftp->ft_to); | |
625 | 5608 if (fl != tl) |
323 | 5609 { |
1619 | 5610 STRMOVE(p + tl, p + fl); |
625 | 5611 repextra += tl - fl; |
323 | 5612 } |
625 | 5613 mch_memmove(p, ftp->ft_to, tl); |
5614 stack[depth].ts_fidxtry = sp->ts_fidx + tl; | |
5615 stack[depth].ts_tcharlen = 0; | |
5616 break; | |
5617 } | |
5618 } | |
5619 | |
5620 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
|
5621 { |
625 | 5622 /* No (more) matches. */ |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5623 PROF_STORE(sp->ts_state) |
625 | 5624 sp->ts_state = STATE_FINAL; |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
5625 } |
625 | 5626 |
5627 break; | |
5628 | |
5629 case STATE_REP_UNDO: | |
5630 /* Undo a REP replacement and continue with the next one. */ | |
5631 if (soundfold) | |
5632 gap = &slang->sl_repsal; | |
5633 else | |
5634 gap = &lp->lp_replang->sl_rep; | |
5635 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1; | |
835 | 5636 fl = (int)STRLEN(ftp->ft_from); |
5637 tl = (int)STRLEN(ftp->ft_to); | |
625 | 5638 p = fword + sp->ts_fidx; |
5639 if (fl != tl) | |
5640 { | |
1619 | 5641 STRMOVE(p + fl, p + tl); |
625 | 5642 repextra -= tl - fl; |
5643 } | |
5644 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
|
5645 PROF_STORE(sp->ts_state) |
625 | 5646 sp->ts_state = STATE_REP; |
5647 break; | |
5648 | |
5649 default: | |
5650 /* Did all possible states at this level, go up one level. */ | |
5651 --depth; | |
5652 | |
5653 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE) | |
5654 { | |
5655 /* Continue in or go back to the prefix tree. */ | |
5656 byts = pbyts; | |
5657 idxs = pidxs; | |
5658 } | |
5659 | |
5660 /* Don't check for CTRL-C too often, it takes time. */ | |
5661 if (--breakcheckcount == 0) | |
5662 { | |
5663 ui_breakcheck(); | |
5664 breakcheckcount = 1000; | |
5665 } | |
5666 } | |
5667 } | |
5668 } | |
5669 | |
5670 | |
5671 /* | |
5672 * Go one level deeper in the tree. | |
5673 */ | |
5674 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5675 go_deeper(trystate_T *stack, int depth, int score_add) |
323 | 5676 { |
330 | 5677 stack[depth + 1] = stack[depth]; |
323 | 5678 stack[depth + 1].ts_state = STATE_START; |
625 | 5679 stack[depth + 1].ts_score = stack[depth].ts_score + score_add; |
323 | 5680 stack[depth + 1].ts_curi = 1; /* start just after length byte */ |
485 | 5681 stack[depth + 1].ts_flags = 0; |
323 | 5682 } |
5683 | |
455 | 5684 /* |
5685 * Case-folding may change the number of bytes: Count nr of chars in | |
5686 * fword[flen] and return the byte length of that many chars in "word". | |
5687 */ | |
5688 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5689 nofold_len(char_u *fword, int flen, char_u *word) |
455 | 5690 { |
5691 char_u *p; | |
5692 int i = 0; | |
5693 | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5694 for (p = fword; p < fword + flen; MB_PTR_ADV(p)) |
455 | 5695 ++i; |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
5696 for (p = word; i > 0; MB_PTR_ADV(p)) |
455 | 5697 --i; |
5698 return (int)(p - word); | |
5699 } | |
5700 | |
323 | 5701 /* |
5702 * "fword" is a good word with case folded. Find the matching keep-case | |
5703 * words and put it in "kword". | |
5704 * Theoretically there could be several keep-case words that result in the | |
5705 * same case-folded word, but we only find one... | |
5706 */ | |
5707 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5708 find_keepcap_word(slang_T *slang, char_u *fword, char_u *kword) |
323 | 5709 { |
5710 char_u uword[MAXWLEN]; /* "fword" in upper-case */ | |
5711 int depth; | |
324 | 5712 idx_T tryidx; |
323 | 5713 |
5714 /* The following arrays are used at each depth in the tree. */ | |
324 | 5715 idx_T arridx[MAXWLEN]; |
323 | 5716 int round[MAXWLEN]; |
5717 int fwordidx[MAXWLEN]; | |
5718 int uwordidx[MAXWLEN]; | |
5719 int kwordlen[MAXWLEN]; | |
5720 | |
5721 int flen, ulen; | |
5722 int l; | |
5723 int len; | |
5724 int c; | |
324 | 5725 idx_T lo, hi, m; |
323 | 5726 char_u *p; |
5727 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */ | |
324 | 5728 idx_T *idxs = slang->sl_kidxs; /* array with indexes */ |
323 | 5729 |
5730 if (byts == NULL) | |
5731 { | |
5732 /* array is empty: "cannot happen" */ | |
5733 *kword = NUL; | |
5734 return; | |
5735 } | |
5736 | |
5737 /* Make an all-cap version of "fword". */ | |
5738 allcap_copy(fword, uword); | |
5739 | |
5740 /* | |
5741 * Each character needs to be tried both case-folded and upper-case. | |
5742 * All this gets very complicated if we keep in mind that changing case | |
5743 * may change the byte length of a multi-byte character... | |
5744 */ | |
5745 depth = 0; | |
5746 arridx[0] = 0; | |
5747 round[0] = 0; | |
5748 fwordidx[0] = 0; | |
5749 uwordidx[0] = 0; | |
5750 kwordlen[0] = 0; | |
5751 while (depth >= 0) | |
5752 { | |
5753 if (fword[fwordidx[depth]] == NUL) | |
5754 { | |
5755 /* We are at the end of "fword". If the tree allows a word to end | |
5756 * here we have found a match. */ | |
5757 if (byts[arridx[depth] + 1] == 0) | |
5758 { | |
5759 kword[kwordlen[depth]] = NUL; | |
5760 return; | |
5761 } | |
5762 | |
5763 /* kword is getting too long, continue one level up */ | |
5764 --depth; | |
5765 } | |
5766 else if (++round[depth] > 2) | |
5767 { | |
5768 /* tried both fold-case and upper-case character, continue one | |
5769 * level up */ | |
5770 --depth; | |
5771 } | |
5772 else | |
5773 { | |
5774 /* | |
5775 * round[depth] == 1: Try using the folded-case character. | |
5776 * round[depth] == 2: Try using the upper-case character. | |
5777 */ | |
5778 if (has_mbyte) | |
5779 { | |
9898
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5780 flen = MB_CPTR2LEN(fword + fwordidx[depth]); |
bff8a09016a5
commit https://github.com/vim/vim/commit/d3c907b5d2b352482b580a0cf687cbbea4c19ea1
Christian Brabandt <cb@256bit.org>
parents:
9649
diff
changeset
|
5781 ulen = MB_CPTR2LEN(uword + uwordidx[depth]); |
323 | 5782 } |
5783 else | |
5784 ulen = flen = 1; | |
5785 if (round[depth] == 1) | |
5786 { | |
5787 p = fword + fwordidx[depth]; | |
5788 l = flen; | |
5789 } | |
5790 else | |
5791 { | |
5792 p = uword + uwordidx[depth]; | |
5793 l = ulen; | |
5794 } | |
5795 | |
5796 for (tryidx = arridx[depth]; l > 0; --l) | |
5797 { | |
5798 /* Perform a binary search in the list of accepted bytes. */ | |
5799 len = byts[tryidx++]; | |
5800 c = *p++; | |
5801 lo = tryidx; | |
5802 hi = tryidx + len - 1; | |
5803 while (lo < hi) | |
5804 { | |
5805 m = (lo + hi) / 2; | |
5806 if (byts[m] > c) | |
5807 hi = m - 1; | |
5808 else if (byts[m] < c) | |
5809 lo = m + 1; | |
5810 else | |
5811 { | |
5812 lo = hi = m; | |
5813 break; | |
5814 } | |
5815 } | |
5816 | |
5817 /* Stop if there is no matching byte. */ | |
5818 if (hi < lo || byts[lo] != c) | |
5819 break; | |
5820 | |
5821 /* Continue at the child (if there is one). */ | |
5822 tryidx = idxs[lo]; | |
5823 } | |
5824 | |
5825 if (l == 0) | |
5826 { | |
5827 /* | |
5828 * Found the matching char. Copy it to "kword" and go a | |
5829 * level deeper. | |
5830 */ | |
5831 if (round[depth] == 1) | |
5832 { | |
5833 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth], | |
5834 flen); | |
5835 kwordlen[depth + 1] = kwordlen[depth] + flen; | |
5836 } | |
5837 else | |
5838 { | |
5839 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth], | |
5840 ulen); | |
5841 kwordlen[depth + 1] = kwordlen[depth] + ulen; | |
5842 } | |
5843 fwordidx[depth + 1] = fwordidx[depth] + flen; | |
5844 uwordidx[depth + 1] = uwordidx[depth] + ulen; | |
5845 | |
5846 ++depth; | |
5847 arridx[depth] = tryidx; | |
5848 round[depth] = 0; | |
5849 } | |
5850 } | |
5851 } | |
5852 | |
5853 /* Didn't find it: "cannot happen". */ | |
5854 *kword = NUL; | |
5855 } | |
5856 | |
5857 /* | |
344 | 5858 * Compute the sound-a-like score for suggestions in su->su_ga and add them to |
5859 * su->su_sga. | |
5860 */ | |
5861 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5862 score_comp_sal(suginfo_T *su) |
344 | 5863 { |
5864 langp_T *lp; | |
5865 char_u badsound[MAXWLEN]; | |
5866 int i; | |
5867 suggest_T *stp; | |
5868 suggest_T *sstp; | |
5869 int score; | |
500 | 5870 int lpi; |
344 | 5871 |
5872 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL) | |
5873 return; | |
5874 | |
5875 /* 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
|
5876 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
|
5877 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5878 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
344 | 5879 if (lp->lp_slang->sl_sal.ga_len > 0) |
5880 { | |
5881 /* soundfold the bad word */ | |
375 | 5882 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound); |
344 | 5883 |
5884 for (i = 0; i < su->su_ga.ga_len; ++i) | |
5885 { | |
5886 stp = &SUG(su->su_ga, i); | |
5887 | |
351 | 5888 /* Case-fold the suggested word, sound-fold it and compute the |
5889 * sound-a-like score. */ | |
5890 score = stp_sal_score(stp, su, lp->lp_slang, badsound); | |
344 | 5891 if (score < SCORE_MAXMAX) |
5892 { | |
5893 /* Add the suggestion. */ | |
5894 sstp = &SUG(su->su_sga, su->su_sga.ga_len); | |
5895 sstp->st_word = vim_strsave(stp->st_word); | |
5896 if (sstp->st_word != NULL) | |
5897 { | |
625 | 5898 sstp->st_wordlen = stp->st_wordlen; |
344 | 5899 sstp->st_score = score; |
5900 sstp->st_altscore = 0; | |
5901 sstp->st_orglen = stp->st_orglen; | |
5902 ++su->su_sga.ga_len; | |
5903 } | |
5904 } | |
5905 } | |
5906 break; | |
5907 } | |
500 | 5908 } |
344 | 5909 } |
5910 | |
5911 /* | |
5912 * Combine the list of suggestions in su->su_ga and su->su_sga. | |
4352 | 5913 * They are entwined. |
344 | 5914 */ |
5915 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
5916 score_combine(suginfo_T *su) |
344 | 5917 { |
5918 int i; | |
5919 int j; | |
5920 garray_T ga; | |
5921 garray_T *gap; | |
5922 langp_T *lp; | |
5923 suggest_T *stp; | |
5924 char_u *p; | |
5925 char_u badsound[MAXWLEN]; | |
5926 int round; | |
500 | 5927 int lpi; |
625 | 5928 slang_T *slang = NULL; |
344 | 5929 |
5930 /* 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
|
5931 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
|
5932 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
5933 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
344 | 5934 if (lp->lp_slang->sl_sal.ga_len > 0) |
5935 { | |
5936 /* soundfold the bad word */ | |
625 | 5937 slang = lp->lp_slang; |
5938 spell_soundfold(slang, su->su_fbadword, TRUE, badsound); | |
344 | 5939 |
5940 for (i = 0; i < su->su_ga.ga_len; ++i) | |
5941 { | |
5942 stp = &SUG(su->su_ga, i); | |
625 | 5943 stp->st_altscore = stp_sal_score(stp, su, slang, badsound); |
344 | 5944 if (stp->st_altscore == SCORE_MAXMAX) |
5945 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4; | |
5946 else | |
5947 stp->st_score = (stp->st_score * 3 | |
5948 + stp->st_altscore) / 4; | |
5949 stp->st_salscore = FALSE; | |
5950 } | |
5951 break; | |
5952 } | |
5953 } | |
5954 | |
844 | 5955 if (slang == NULL) /* Using "double" without sound folding. */ |
5956 { | |
5957 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, | |
5958 su->su_maxcount); | |
625 | 5959 return; |
844 | 5960 } |
625 | 5961 |
344 | 5962 /* Add the alternate score to su_sga. */ |
5963 for (i = 0; i < su->su_sga.ga_len; ++i) | |
5964 { | |
5965 stp = &SUG(su->su_sga, i); | |
625 | 5966 stp->st_altscore = spell_edit_score(slang, |
5967 su->su_badword, stp->st_word); | |
344 | 5968 if (stp->st_score == SCORE_MAXMAX) |
5969 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8; | |
5970 else | |
5971 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8; | |
5972 stp->st_salscore = TRUE; | |
5973 } | |
5974 | |
625 | 5975 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount" |
5976 * for both lists. */ | |
5977 check_suggestions(su, &su->su_ga); | |
344 | 5978 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount); |
625 | 5979 check_suggestions(su, &su->su_sga); |
344 | 5980 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount); |
5981 | |
5982 ga_init2(&ga, (int)sizeof(suginfo_T), 1); | |
5983 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL) | |
5984 return; | |
5985 | |
5986 stp = &SUG(ga, 0); | |
5987 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i) | |
5988 { | |
5989 /* round 1: get a suggestion from su_ga | |
5990 * round 2: get a suggestion from su_sga */ | |
5991 for (round = 1; round <= 2; ++round) | |
5992 { | |
5993 gap = round == 1 ? &su->su_ga : &su->su_sga; | |
5994 if (i < gap->ga_len) | |
5995 { | |
5996 /* Don't add a word if it's already there. */ | |
5997 p = SUG(*gap, i).st_word; | |
5998 for (j = 0; j < ga.ga_len; ++j) | |
5999 if (STRCMP(stp[j].st_word, p) == 0) | |
6000 break; | |
6001 if (j == ga.ga_len) | |
6002 stp[ga.ga_len++] = SUG(*gap, i); | |
6003 else | |
6004 vim_free(p); | |
6005 } | |
6006 } | |
6007 } | |
6008 | |
6009 ga_clear(&su->su_ga); | |
6010 ga_clear(&su->su_sga); | |
6011 | |
6012 /* Truncate the list to the number of suggestions that will be displayed. */ | |
6013 if (ga.ga_len > su->su_maxcount) | |
6014 { | |
6015 for (i = su->su_maxcount; i < ga.ga_len; ++i) | |
6016 vim_free(stp[i].st_word); | |
6017 ga.ga_len = su->su_maxcount; | |
6018 } | |
6019 | |
6020 su->su_ga = ga; | |
6021 } | |
6022 | |
6023 /* | |
351 | 6024 * For the goodword in "stp" compute the soundalike score compared to the |
6025 * badword. | |
6026 */ | |
6027 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6028 stp_sal_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6029 suggest_T *stp, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6030 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6031 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6032 char_u *badsound) /* sound-folded badword */ |
351 | 6033 { |
6034 char_u *p; | |
534 | 6035 char_u *pbad; |
6036 char_u *pgood; | |
351 | 6037 char_u badsound2[MAXWLEN]; |
6038 char_u fword[MAXWLEN]; | |
6039 char_u goodsound[MAXWLEN]; | |
534 | 6040 char_u goodword[MAXWLEN]; |
6041 int lendiff; | |
6042 | |
6043 lendiff = (int)(su->su_badlen - stp->st_orglen); | |
6044 if (lendiff >= 0) | |
6045 pbad = badsound; | |
351 | 6046 else |
6047 { | |
6048 /* soundfold the bad word with more characters following */ | |
6049 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN); | |
6050 | |
6051 /* When joining two words the sound often changes a lot. E.g., "t he" | |
6052 * sounds like "t h" while "the" sounds like "@". Avoid that by | |
6053 * removing the space. Don't do it when the good word also contains a | |
6054 * space. */ | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6055 if (VIM_ISWHITE(su->su_badptr[su->su_badlen]) |
351 | 6056 && *skiptowhite(stp->st_word) == NUL) |
6057 for (p = fword; *(p = skiptowhite(p)) != NUL; ) | |
1619 | 6058 STRMOVE(p, p + 1); |
351 | 6059 |
375 | 6060 spell_soundfold(slang, fword, TRUE, badsound2); |
534 | 6061 pbad = badsound2; |
6062 } | |
6063 | |
2768 | 6064 if (lendiff > 0 && stp->st_wordlen + lendiff < MAXWLEN) |
534 | 6065 { |
6066 /* Add part of the bad word to the good word, so that we soundfold | |
6067 * what replaces the bad word. */ | |
6068 STRCPY(goodword, stp->st_word); | |
625 | 6069 vim_strncpy(goodword + stp->st_wordlen, |
6070 su->su_badptr + su->su_badlen - lendiff, lendiff); | |
534 | 6071 pgood = goodword; |
6072 } | |
6073 else | |
6074 pgood = stp->st_word; | |
351 | 6075 |
375 | 6076 /* Sound-fold the word and compute the score for the difference. */ |
534 | 6077 spell_soundfold(slang, pgood, FALSE, goodsound); |
6078 | |
6079 return soundalike_score(goodsound, pbad); | |
351 | 6080 } |
6081 | |
625 | 6082 /* structure used to store soundfolded words that add_sound_suggest() has |
6083 * handled already. */ | |
6084 typedef struct | |
6085 { | |
6086 short sft_score; /* lowest score used */ | |
6087 char_u sft_word[1]; /* soundfolded word, actually longer */ | |
6088 } sftword_T; | |
6089 | |
6090 static sftword_T dumsft; | |
6091 #define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft))) | |
6092 #define HI2SFT(hi) HIKEY2SFT((hi)->hi_key) | |
6093 | |
6094 /* | |
6095 * Prepare for calling suggest_try_soundalike(). | |
6096 */ | |
6097 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6098 suggest_try_soundalike_prep(void) |
625 | 6099 { |
6100 langp_T *lp; | |
6101 int lpi; | |
6102 slang_T *slang; | |
6103 | |
6104 /* Do this for all languages that support sound folding and for which a | |
6105 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6106 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
|
6107 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6108 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
625 | 6109 slang = lp->lp_slang; |
6110 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) | |
6111 /* prepare the hashtable used by add_sound_suggest() */ | |
6112 hash_init(&slang->sl_sounddone); | |
6113 } | |
6114 } | |
6115 | |
351 | 6116 /* |
323 | 6117 * Find suggestions by comparing the word in a sound-a-like form. |
503 | 6118 * Note: This doesn't support postponed prefixes. |
323 | 6119 */ |
6120 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6121 suggest_try_soundalike(suginfo_T *su) |
323 | 6122 { |
6123 char_u salword[MAXWLEN]; | |
6124 langp_T *lp; | |
500 | 6125 int lpi; |
503 | 6126 slang_T *slang; |
344 | 6127 |
625 | 6128 /* Do this for all languages that support sound folding and for which a |
6129 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6130 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
|
6131 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6132 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
625 | 6133 slang = lp->lp_slang; |
6134 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) | |
6135 { | |
6136 /* soundfold the bad word */ | |
6137 spell_soundfold(slang, su->su_fbadword, TRUE, salword); | |
6138 | |
6139 /* try all kinds of inserts/deletes/swaps/etc. */ | |
6140 /* TODO: also soundfold the next words, so that we can try joining | |
6141 * and splitting */ | |
7509
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6142 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6143 prof_init(); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6144 #endif |
625 | 6145 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
|
6146 #ifdef SUGGEST_PROFILE |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6147 prof_report("soundalike"); |
a64793340689
commit https://github.com/vim/vim/commit/ca1fe985175385c609f8e06672a1014729aba05c
Christian Brabandt <cb@256bit.org>
parents:
7447
diff
changeset
|
6148 #endif |
625 | 6149 } |
6150 } | |
6151 } | |
6152 | |
6153 /* | |
6154 * Finish up after calling suggest_try_soundalike(). | |
6155 */ | |
6156 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6157 suggest_try_soundalike_finish(void) |
625 | 6158 { |
6159 langp_T *lp; | |
6160 int lpi; | |
6161 slang_T *slang; | |
6162 int todo; | |
6163 hashitem_T *hi; | |
6164 | |
6165 /* Do this for all languages that support sound folding and for which a | |
6166 * .sug file has been loaded. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6167 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
|
6168 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6169 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
503 | 6170 slang = lp->lp_slang; |
625 | 6171 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL) |
6172 { | |
6173 /* Free the info about handled words. */ | |
835 | 6174 todo = (int)slang->sl_sounddone.ht_used; |
625 | 6175 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi) |
6176 if (!HASHITEM_EMPTY(hi)) | |
6177 { | |
6178 vim_free(HI2SFT(hi)); | |
6179 --todo; | |
6180 } | |
1087 | 6181 |
6182 /* Clear the hashtable, it may also be used by another region. */ | |
625 | 6183 hash_clear(&slang->sl_sounddone); |
1087 | 6184 hash_init(&slang->sl_sounddone); |
625 | 6185 } |
6186 } | |
6187 } | |
6188 | |
6189 /* | |
6190 * A match with a soundfolded word is found. Add the good word(s) that | |
6191 * produce this soundfolded word. | |
6192 */ | |
6193 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6194 add_sound_suggest( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6195 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6196 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6197 int score, /* soundfold score */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6198 langp_T *lp) |
625 | 6199 { |
6200 slang_T *slang = lp->lp_slang; /* language for sound folding */ | |
6201 int sfwordnr; | |
6202 char_u *nrline; | |
6203 int orgnr; | |
6204 char_u theword[MAXWLEN]; | |
6205 int i; | |
6206 int wlen; | |
6207 char_u *byts; | |
6208 idx_T *idxs; | |
6209 int n; | |
6210 int wordcount; | |
6211 int wc; | |
6212 int goodscore; | |
6213 hash_T hash; | |
6214 hashitem_T *hi; | |
6215 sftword_T *sft; | |
6216 int bc, gc; | |
6217 int limit; | |
6218 | |
6219 /* | |
6220 * It's very well possible that the same soundfold word is found several | |
6221 * times with different scores. Since the following is quite slow only do | |
6222 * the words that have a better score than before. Use a hashtable to | |
6223 * remember the words that have been done. | |
6224 */ | |
6225 hash = hash_hash(goodword); | |
6226 hi = hash_lookup(&slang->sl_sounddone, goodword, hash); | |
6227 if (HASHITEM_EMPTY(hi)) | |
6228 { | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
6229 sft = alloc(sizeof(sftword_T) + STRLEN(goodword)); |
625 | 6230 if (sft != NULL) |
6231 { | |
6232 sft->sft_score = score; | |
6233 STRCPY(sft->sft_word, goodword); | |
6234 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash); | |
6235 } | |
6236 } | |
6237 else | |
6238 { | |
6239 sft = HI2SFT(hi); | |
6240 if (score >= sft->sft_score) | |
6241 return; | |
6242 sft->sft_score = score; | |
6243 } | |
6244 | |
6245 /* | |
6246 * Find the word nr in the soundfold tree. | |
6247 */ | |
6248 sfwordnr = soundfold_find(slang, goodword); | |
6249 if (sfwordnr < 0) | |
6250 { | |
10359
66f1b5bf3fa6
commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents:
10277
diff
changeset
|
6251 internal_error("add_sound_suggest()"); |
625 | 6252 return; |
6253 } | |
6254 | |
6255 /* | |
6256 * go over the list of good words that produce this soundfold word | |
6257 */ | |
6258 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE); | |
6259 orgnr = 0; | |
6260 while (*nrline != NUL) | |
6261 { | |
6262 /* The wordnr was stored in a minimal nr of bytes as an offset to the | |
6263 * previous wordnr. */ | |
6264 orgnr += bytes2offset(&nrline); | |
6265 | |
6266 byts = slang->sl_fbyts; | |
6267 idxs = slang->sl_fidxs; | |
6268 | |
6269 /* Lookup the word "orgnr" one of the two tries. */ | |
6270 n = 0; | |
6271 wordcount = 0; | |
5506 | 6272 for (wlen = 0; wlen < MAXWLEN - 3; ++wlen) |
625 | 6273 { |
6274 i = 1; | |
6275 if (wordcount == orgnr && byts[n + 1] == NUL) | |
6276 break; /* found end of word */ | |
6277 | |
6278 if (byts[n + 1] == NUL) | |
6279 ++wordcount; | |
6280 | |
6281 /* skip over the NUL bytes */ | |
6282 for ( ; byts[n + i] == NUL; ++i) | |
6283 if (i > byts[n]) /* safety check */ | |
6284 { | |
6285 STRCPY(theword + wlen, "BAD"); | |
5506 | 6286 wlen += 3; |
625 | 6287 goto badword; |
6288 } | |
6289 | |
6290 /* One of the siblings must have the word. */ | |
6291 for ( ; i < byts[n]; ++i) | |
6292 { | |
6293 wc = idxs[idxs[n + i]]; /* nr of words under this byte */ | |
6294 if (wordcount + wc > orgnr) | |
6295 break; | |
6296 wordcount += wc; | |
6297 } | |
6298 | |
5506 | 6299 theword[wlen] = byts[n + i]; |
625 | 6300 n = idxs[n + i]; |
6301 } | |
6302 badword: | |
6303 theword[wlen] = NUL; | |
6304 | |
6305 /* Go over the possible flags and regions. */ | |
6306 for (; i <= byts[n] && byts[n + i] == NUL; ++i) | |
6307 { | |
6308 char_u cword[MAXWLEN]; | |
6309 char_u *p; | |
6310 int flags = (int)idxs[n + i]; | |
6311 | |
698 | 6312 /* Skip words with the NOSUGGEST flag */ |
6313 if (flags & WF_NOSUGGEST) | |
6314 continue; | |
6315 | |
625 | 6316 if (flags & WF_KEEPCAP) |
6317 { | |
6318 /* Must find the word in the keep-case tree. */ | |
6319 find_keepcap_word(slang, theword, cword); | |
6320 p = cword; | |
6321 } | |
6322 else | |
6323 { | |
6324 flags |= su->su_badflags; | |
6325 if ((flags & WF_CAPMASK) != 0) | |
6326 { | |
6327 /* Need to fix case according to "flags". */ | |
6328 make_case_word(theword, cword, flags); | |
6329 p = cword; | |
323 | 6330 } |
6331 else | |
625 | 6332 p = theword; |
6333 } | |
6334 | |
6335 /* Add the suggestion. */ | |
6336 if (sps_flags & SPS_DOUBLE) | |
6337 { | |
6338 /* Add the suggestion if the score isn't too bad. */ | |
6339 if (score <= su->su_maxscore) | |
6340 add_suggestion(su, &su->su_sga, p, su->su_badlen, | |
6341 score, 0, FALSE, slang, FALSE); | |
6342 } | |
6343 else | |
6344 { | |
6345 /* Add a penalty for words in another region. */ | |
6346 if ((flags & WF_REGION) | |
6347 && (((unsigned)flags >> 16) & lp->lp_region) == 0) | |
6348 goodscore = SCORE_REGION; | |
6349 else | |
6350 goodscore = 0; | |
6351 | |
6352 /* Add a small penalty for changing the first letter from | |
6353 * lower to upper case. Helps for "tath" -> "Kath", which is | |
4352 | 6354 * less common than "tath" -> "path". Don't do it when the |
625 | 6355 * letter is the same, that has already been counted. */ |
6356 gc = PTR2CHAR(p); | |
6357 if (SPELL_ISUPPER(gc)) | |
6358 { | |
6359 bc = PTR2CHAR(su->su_badword); | |
6360 if (!SPELL_ISUPPER(bc) | |
6361 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc)) | |
6362 goodscore += SCORE_ICASE / 2; | |
6363 } | |
6364 | |
6365 /* Compute the score for the good word. This only does letter | |
6366 * insert/delete/swap/replace. REP items are not considered, | |
6367 * which may make the score a bit higher. | |
6368 * Use a limit for the score to make it work faster. Use | |
6369 * MAXSCORE(), because RESCORE() will change the score. | |
6370 * If the limit is very high then the iterative method is | |
6371 * inefficient, using an array is quicker. */ | |
6372 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score); | |
6373 if (limit > SCORE_LIMITMAX) | |
6374 goodscore += spell_edit_score(slang, su->su_badword, p); | |
6375 else | |
6376 goodscore += spell_edit_score_limit(slang, su->su_badword, | |
6377 p, limit); | |
6378 | |
6379 /* When going over the limit don't bother to do the rest. */ | |
6380 if (goodscore < SCORE_MAXMAX) | |
6381 { | |
6382 /* Give a bonus to words seen before. */ | |
6383 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE); | |
6384 | |
6385 /* Add the suggestion if the score isn't too bad. */ | |
6386 goodscore = RESCORE(goodscore, score); | |
6387 if (goodscore <= su->su_sfmaxscore) | |
6388 add_suggestion(su, &su->su_ga, p, su->su_badlen, | |
6389 goodscore, score, TRUE, slang, TRUE); | |
6390 } | |
6391 } | |
6392 } | |
6393 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */ | |
6394 } | |
6395 } | |
6396 | |
6397 /* | |
6398 * Find word "word" in fold-case tree for "slang" and return the word number. | |
6399 */ | |
6400 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6401 soundfold_find(slang_T *slang, char_u *word) |
625 | 6402 { |
6403 idx_T arridx = 0; | |
6404 int len; | |
6405 int wlen = 0; | |
6406 int c; | |
6407 char_u *ptr = word; | |
6408 char_u *byts; | |
6409 idx_T *idxs; | |
6410 int wordnr = 0; | |
6411 | |
6412 byts = slang->sl_sbyts; | |
6413 idxs = slang->sl_sidxs; | |
6414 | |
6415 for (;;) | |
6416 { | |
6417 /* First byte is the number of possible bytes. */ | |
6418 len = byts[arridx++]; | |
6419 | |
6420 /* If the first possible byte is a zero the word could end here. | |
6421 * If the word ends we found the word. If not skip the NUL bytes. */ | |
6422 c = ptr[wlen]; | |
6423 if (byts[arridx] == NUL) | |
6424 { | |
6425 if (c == NUL) | |
6426 break; | |
6427 | |
6428 /* Skip over the zeros, there can be several. */ | |
6429 while (len > 0 && byts[arridx] == NUL) | |
6430 { | |
6431 ++arridx; | |
6432 --len; | |
6433 } | |
6434 if (len == 0) | |
6435 return -1; /* no children, word should have ended here */ | |
6436 ++wordnr; | |
6437 } | |
6438 | |
6439 /* If the word ends we didn't find it. */ | |
6440 if (c == NUL) | |
6441 return -1; | |
6442 | |
6443 /* Perform a binary search in the list of accepted bytes. */ | |
6444 if (c == TAB) /* <Tab> is handled like <Space> */ | |
6445 c = ' '; | |
6446 while (byts[arridx] < c) | |
6447 { | |
6448 /* The word count is in the first idxs[] entry of the child. */ | |
6449 wordnr += idxs[idxs[arridx]]; | |
6450 ++arridx; | |
6451 if (--len == 0) /* end of the bytes, didn't find it */ | |
6452 return -1; | |
6453 } | |
6454 if (byts[arridx] != c) /* didn't find the byte */ | |
6455 return -1; | |
6456 | |
6457 /* Continue at the child (if there is one). */ | |
6458 arridx = idxs[arridx]; | |
6459 ++wlen; | |
6460 | |
6461 /* One space in the good word may stand for several spaces in the | |
6462 * checked word. */ | |
6463 if (c == ' ') | |
6464 while (ptr[wlen] == ' ' || ptr[wlen] == TAB) | |
6465 ++wlen; | |
6466 } | |
6467 | |
6468 return wordnr; | |
323 | 6469 } |
6470 | |
6471 /* | |
324 | 6472 * Copy "fword" to "cword", fixing case according to "flags". |
323 | 6473 */ |
6474 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6475 make_case_word(char_u *fword, char_u *cword, int flags) |
323 | 6476 { |
6477 if (flags & WF_ALLCAP) | |
6478 /* Make it all upper-case */ | |
6479 allcap_copy(fword, cword); | |
6480 else if (flags & WF_ONECAP) | |
6481 /* Make the first letter upper-case */ | |
324 | 6482 onecap_copy(fword, cword, TRUE); |
323 | 6483 else |
6484 /* Use goodword as-is. */ | |
6485 STRCPY(cword, fword); | |
6486 } | |
6487 | |
330 | 6488 |
323 | 6489 /* |
6490 * Return TRUE if "c1" and "c2" are similar characters according to the MAP | |
6491 * lines in the .aff file. | |
6492 */ | |
6493 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6494 similar_chars(slang_T *slang, int c1, int c2) |
323 | 6495 { |
330 | 6496 int m1, m2; |
3549 | 6497 char_u buf[MB_MAXBYTES + 1]; |
330 | 6498 hashitem_T *hi; |
6499 | |
6500 if (c1 >= 256) | |
6501 { | |
6502 buf[mb_char2bytes(c1, buf)] = 0; | |
6503 hi = hash_find(&slang->sl_map_hash, buf); | |
6504 if (HASHITEM_EMPTY(hi)) | |
6505 m1 = 0; | |
6506 else | |
6507 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); | |
6508 } | |
6509 else | |
6510 m1 = slang->sl_map_array[c1]; | |
6511 if (m1 == 0) | |
323 | 6512 return FALSE; |
330 | 6513 |
6514 | |
6515 if (c2 >= 256) | |
6516 { | |
6517 buf[mb_char2bytes(c2, buf)] = 0; | |
6518 hi = hash_find(&slang->sl_map_hash, buf); | |
6519 if (HASHITEM_EMPTY(hi)) | |
6520 m2 = 0; | |
6521 else | |
6522 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1); | |
6523 } | |
6524 else | |
6525 m2 = slang->sl_map_array[c2]; | |
6526 | |
6527 return m1 == m2; | |
323 | 6528 } |
6529 | |
6530 /* | |
6531 * Add a suggestion to the list of suggestions. | |
625 | 6532 * For a suggestion that is already in the list the lowest score is remembered. |
6533 */ | |
6534 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6535 add_suggestion( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6536 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6537 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
|
6538 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6539 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
|
6540 int score, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6541 int altscore, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6542 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
|
6543 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
|
6544 int maxsf) /* su_maxscore applies to soundfold score, |
625 | 6545 su_sfmaxscore to the total score. */ |
6546 { | |
6547 int goodlen; /* len of goodword changed */ | |
6548 int badlen; /* len of bad word changed */ | |
323 | 6549 suggest_T *stp; |
534 | 6550 suggest_T new_sug; |
323 | 6551 int i; |
534 | 6552 char_u *pgood, *pbad; |
532 | 6553 |
534 | 6554 /* Minimize "badlen" for consistency. Avoids that changing "the the" to |
6555 * "thee the" is added next to changing the first "the" the "thee". */ | |
6556 pgood = goodword + STRLEN(goodword); | |
625 | 6557 pbad = su->su_badptr + badlenarg; |
6558 for (;;) | |
6559 { | |
835 | 6560 goodlen = (int)(pgood - goodword); |
6561 badlen = (int)(pbad - su->su_badptr); | |
625 | 6562 if (goodlen <= 0 || badlen <= 0) |
6563 break; | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
6564 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
|
6565 MB_PTR_BACK(su->su_badptr, pbad); |
534 | 6566 if (has_mbyte) |
6567 { | |
6568 if (mb_ptr2char(pgood) != mb_ptr2char(pbad)) | |
6569 break; | |
346 | 6570 } |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
6571 else if (*pgood != *pbad) |
534 | 6572 break; |
625 | 6573 } |
6574 | |
534 | 6575 if (badlen == 0 && goodlen == 0) |
6576 /* goodword doesn't change anything; may happen for "the the" changing | |
6577 * the first "the" to itself. */ | |
6578 return; | |
346 | 6579 |
944 | 6580 if (gap->ga_len == 0) |
6581 i = -1; | |
6582 else | |
6583 { | |
6584 /* Check if the word is already there. Also check the length that is | |
6585 * being replaced "thes," -> "these" is a different suggestion from | |
6586 * "thes" -> "these". */ | |
6587 stp = &SUG(*gap, 0); | |
6588 for (i = gap->ga_len; --i >= 0; ++stp) | |
6589 if (stp->st_wordlen == goodlen | |
6590 && stp->st_orglen == badlen | |
6591 && STRNCMP(stp->st_word, goodword, goodlen) == 0) | |
6592 { | |
6593 /* | |
6594 * Found it. Remember the word with the lowest score. | |
6595 */ | |
6596 if (stp->st_slang == NULL) | |
6597 stp->st_slang = slang; | |
6598 | |
6599 new_sug.st_score = score; | |
6600 new_sug.st_altscore = altscore; | |
6601 new_sug.st_had_bonus = had_bonus; | |
6602 | |
6603 if (stp->st_had_bonus != had_bonus) | |
6604 { | |
6605 /* Only one of the two had the soundalike score computed. | |
6606 * Need to do that for the other one now, otherwise the | |
6607 * scores can't be compared. This happens because | |
6608 * suggest_try_change() doesn't compute the soundalike | |
6609 * word to keep it fast, while some special methods set | |
6610 * the soundalike score to zero. */ | |
6611 if (had_bonus) | |
6612 rescore_one(su, stp); | |
6613 else | |
6614 { | |
6615 new_sug.st_word = stp->st_word; | |
6616 new_sug.st_wordlen = stp->st_wordlen; | |
6617 new_sug.st_slang = stp->st_slang; | |
6618 new_sug.st_orglen = badlen; | |
6619 rescore_one(su, &new_sug); | |
6620 } | |
6621 } | |
6622 | |
6623 if (stp->st_score > new_sug.st_score) | |
6624 { | |
6625 stp->st_score = new_sug.st_score; | |
6626 stp->st_altscore = new_sug.st_altscore; | |
6627 stp->st_had_bonus = new_sug.st_had_bonus; | |
6628 } | |
6629 break; | |
6630 } | |
6631 } | |
625 | 6632 |
6633 if (i < 0 && ga_grow(gap, 1) == OK) | |
6634 { | |
6635 /* Add a suggestion. */ | |
6636 stp = &SUG(*gap, gap->ga_len); | |
6637 stp->st_word = vim_strnsave(goodword, goodlen); | |
6638 if (stp->st_word != NULL) | |
6639 { | |
6640 stp->st_wordlen = goodlen; | |
6641 stp->st_score = score; | |
6642 stp->st_altscore = altscore; | |
6643 stp->st_had_bonus = had_bonus; | |
6644 stp->st_orglen = badlen; | |
6645 stp->st_slang = slang; | |
6646 ++gap->ga_len; | |
6647 | |
6648 /* If we have too many suggestions now, sort the list and keep | |
6649 * the best suggestions. */ | |
6650 if (gap->ga_len > SUG_MAX_COUNT(su)) | |
6651 { | |
6652 if (maxsf) | |
6653 su->su_sfmaxscore = cleanup_suggestions(gap, | |
6654 su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); | |
6655 else | |
6656 su->su_maxscore = cleanup_suggestions(gap, | |
6657 su->su_maxscore, SUG_CLEAN_COUNT(su)); | |
6658 } | |
6659 } | |
6660 } | |
6661 } | |
6662 | |
6663 /* | |
6664 * Suggestions may in fact be flagged as errors. Esp. for banned words and | |
6665 * for split words, such as "the the". Remove these from the list here. | |
6666 */ | |
6667 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6668 check_suggestions( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6669 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6670 garray_T *gap) /* either su_ga or su_sga */ |
625 | 6671 { |
6672 suggest_T *stp; | |
6673 int i; | |
6674 char_u longword[MAXWLEN + 1]; | |
6675 int len; | |
6676 hlf_T attr; | |
6677 | |
6678 stp = &SUG(*gap, 0); | |
6679 for (i = gap->ga_len - 1; i >= 0; --i) | |
6680 { | |
6681 /* Need to append what follows to check for "the the". */ | |
2768 | 6682 vim_strncpy(longword, stp[i].st_word, MAXWLEN); |
625 | 6683 len = stp[i].st_wordlen; |
6684 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen, | |
6685 MAXWLEN - len); | |
6686 attr = HLF_COUNT; | |
6687 (void)spell_check(curwin, longword, &attr, NULL, FALSE); | |
6688 if (attr != HLF_COUNT) | |
6689 { | |
6690 /* Remove this entry. */ | |
6691 vim_free(stp[i].st_word); | |
6692 --gap->ga_len; | |
6693 if (i < gap->ga_len) | |
6694 mch_memmove(stp + i, stp + i + 1, | |
6695 sizeof(suggest_T) * (gap->ga_len - i)); | |
6696 } | |
6697 } | |
6698 } | |
6699 | |
323 | 6700 |
6701 /* | |
6702 * Add a word to be banned. | |
6703 */ | |
6704 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6705 add_banned( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6706 suginfo_T *su, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6707 char_u *word) |
323 | 6708 { |
626 | 6709 char_u *s; |
323 | 6710 hash_T hash; |
6711 hashitem_T *hi; | |
6712 | |
625 | 6713 hash = hash_hash(word); |
6714 hi = hash_lookup(&su->su_banned, word, hash); | |
6715 if (HASHITEM_EMPTY(hi)) | |
6716 { | |
6717 s = vim_strsave(word); | |
6718 if (s != NULL) | |
323 | 6719 hash_add_item(&su->su_banned, hi, s, hash); |
625 | 6720 } |
323 | 6721 } |
6722 | |
324 | 6723 /* |
534 | 6724 * Recompute the score for all suggestions if sound-folding is possible. This |
6725 * is slow, thus only done for the final results. | |
324 | 6726 */ |
6727 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6728 rescore_suggestions(suginfo_T *su) |
324 | 6729 { |
6730 int i; | |
534 | 6731 |
6732 if (su->su_sallang != NULL) | |
503 | 6733 for (i = 0; i < su->su_ga.ga_len; ++i) |
534 | 6734 rescore_one(su, &SUG(su->su_ga, i)); |
6735 } | |
6736 | |
6737 /* | |
6738 * Recompute the score for one suggestion if sound-folding is possible. | |
6739 */ | |
6740 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6741 rescore_one(suginfo_T *su, suggest_T *stp) |
534 | 6742 { |
6743 slang_T *slang = stp->st_slang; | |
6744 char_u sal_badword[MAXWLEN]; | |
535 | 6745 char_u *p; |
534 | 6746 |
6747 /* Only rescore suggestions that have no sal score yet and do have a | |
6748 * language. */ | |
6749 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus) | |
6750 { | |
6751 if (slang == su->su_sallang) | |
535 | 6752 p = su->su_sal_badword; |
534 | 6753 else |
6754 { | |
6755 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword); | |
535 | 6756 p = sal_badword; |
6757 } | |
6758 | |
6759 stp->st_altscore = stp_sal_score(stp, su, slang, p); | |
534 | 6760 if (stp->st_altscore == SCORE_MAXMAX) |
6761 stp->st_altscore = SCORE_BIG; | |
6762 stp->st_score = RESCORE(stp->st_score, stp->st_altscore); | |
6763 stp->st_had_bonus = TRUE; | |
324 | 6764 } |
6765 } | |
6766 | |
16606
7e733046db1d
patch 8.1.1306: Borland support is outdated and doesn't work
Bram Moolenaar <Bram@vim.org>
parents:
16277
diff
changeset
|
6767 static int 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 | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6774 sug_compare(const void *s1, const void *s2) |
323 | 6775 { |
6776 suggest_T *p1 = (suggest_T *)s1; | |
6777 suggest_T *p2 = (suggest_T *)s2; | |
344 | 6778 int n = p1->st_score - p2->st_score; |
6779 | |
6780 if (n == 0) | |
529 | 6781 { |
6782 n = p1->st_altscore - p2->st_altscore; | |
6783 if (n == 0) | |
6784 n = STRICMP(p1->st_word, p2->st_word); | |
6785 } | |
344 | 6786 return n; |
323 | 6787 } |
6788 | |
6789 /* | |
6790 * Cleanup the suggestions: | |
6791 * - Sort on score. | |
6792 * - Remove words that won't be displayed. | |
344 | 6793 * Returns the maximum score in the list or "maxscore" unmodified. |
323 | 6794 */ |
344 | 6795 static int |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6796 cleanup_suggestions( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6797 garray_T *gap, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6798 int maxscore, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6799 int keep) /* nr of suggestions to keep */ |
323 | 6800 { |
344 | 6801 suggest_T *stp = &SUG(*gap, 0); |
323 | 6802 int i; |
6803 | |
6804 /* Sort the list. */ | |
344 | 6805 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare); |
323 | 6806 |
6807 /* Truncate the list to the number of suggestions that will be displayed. */ | |
344 | 6808 if (gap->ga_len > keep) |
323 | 6809 { |
344 | 6810 for (i = keep; i < gap->ga_len; ++i) |
323 | 6811 vim_free(stp[i].st_word); |
344 | 6812 gap->ga_len = keep; |
6813 return stp[keep - 1].st_score; | |
323 | 6814 } |
344 | 6815 return maxscore; |
323 | 6816 } |
6817 | |
372 | 6818 #if defined(FEAT_EVAL) || defined(PROTO) |
6819 /* | |
6820 * Soundfold a string, for soundfold(). | |
6821 * Result is in allocated memory, NULL for an error. | |
6822 */ | |
6823 char_u * | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6824 eval_soundfold(char_u *word) |
372 | 6825 { |
6826 langp_T *lp; | |
6827 char_u sound[MAXWLEN]; | |
500 | 6828 int lpi; |
372 | 6829 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6830 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) |
372 | 6831 /* 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
|
6832 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
|
6833 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
6834 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
372 | 6835 if (lp->lp_slang->sl_sal.ga_len > 0) |
6836 { | |
6837 /* soundfold the word */ | |
375 | 6838 spell_soundfold(lp->lp_slang, word, FALSE, sound); |
372 | 6839 return vim_strsave(sound); |
6840 } | |
500 | 6841 } |
372 | 6842 |
6843 /* No language with sound folding, return word as-is. */ | |
6844 return vim_strsave(word); | |
6845 } | |
6846 #endif | |
6847 | |
323 | 6848 /* |
6849 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". | |
485 | 6850 * |
6851 * There are many ways to turn a word into a sound-a-like representation. The | |
6852 * oldest is Soundex (1918!). A nice overview can be found in "Approximate | |
6853 * swedish name matching - survey and test of different algorithms" by Klas | |
6854 * Erikson. | |
6855 * | |
6856 * We support two methods: | |
6857 * 1. SOFOFROM/SOFOTO do a simple character mapping. | |
6858 * 2. SAL items define a more advanced sound-folding (and much slower). | |
323 | 6859 */ |
9583
b0c7061d6439
commit https://github.com/vim/vim/commit/9ccfebddc3ff2a3c2853cf706fd4c26f639bf381
Christian Brabandt <cb@256bit.org>
parents:
9570
diff
changeset
|
6860 void |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6861 spell_soundfold( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6862 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6863 char_u *inword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6864 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
|
6865 char_u *res) |
375 | 6866 { |
6867 char_u fword[MAXWLEN]; | |
6868 char_u *word; | |
6869 | |
6870 if (slang->sl_sofo) | |
6871 /* SOFOFROM and SOFOTO used */ | |
6872 spell_soundfold_sofo(slang, inword, res); | |
6873 else | |
6874 { | |
6875 /* SAL items used. Requires the word to be case-folded. */ | |
6876 if (folded) | |
6877 word = inword; | |
6878 else | |
6879 { | |
835 | 6880 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN); |
375 | 6881 word = fword; |
6882 } | |
6883 | |
6884 if (has_mbyte) | |
6885 spell_soundfold_wsal(slang, word, res); | |
6886 else | |
6887 spell_soundfold_sal(slang, word, res); | |
6888 } | |
6889 } | |
6890 | |
6891 /* | |
6892 * Perform sound folding of "inword" into "res" according to SOFOFROM and | |
6893 * SOFOTO lines. | |
6894 */ | |
6895 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6896 spell_soundfold_sofo(slang_T *slang, char_u *inword, char_u *res) |
375 | 6897 { |
6898 char_u *s; | |
6899 int ri = 0; | |
6900 int c; | |
6901 | |
6902 if (has_mbyte) | |
6903 { | |
6904 int prevc = 0; | |
6905 int *ip; | |
6906 | |
6907 /* The sl_sal_first[] table contains the translation for chars up to | |
6908 * 255, sl_sal the rest. */ | |
6909 for (s = inword; *s != NUL; ) | |
6910 { | |
474 | 6911 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
|
6912 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) |
375 | 6913 c = ' '; |
6914 else if (c < 256) | |
6915 c = slang->sl_sal_first[c]; | |
6916 else | |
6917 { | |
6918 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff]; | |
6919 if (ip == NULL) /* empty list, can't match */ | |
6920 c = NUL; | |
6921 else | |
6922 for (;;) /* find "c" in the list */ | |
6923 { | |
6924 if (*ip == 0) /* not found */ | |
6925 { | |
6926 c = NUL; | |
6927 break; | |
6928 } | |
6929 if (*ip == c) /* match! */ | |
6930 { | |
6931 c = ip[1]; | |
6932 break; | |
6933 } | |
6934 ip += 2; | |
6935 } | |
6936 } | |
6937 | |
6938 if (c != NUL && c != prevc) | |
6939 { | |
6940 ri += mb_char2bytes(c, res + ri); | |
6941 if (ri + MB_MAXBYTES > MAXWLEN) | |
6942 break; | |
6943 prevc = c; | |
6944 } | |
6945 } | |
6946 } | |
6947 else | |
6948 { | |
6949 /* The sl_sal_first[] table contains the translation. */ | |
6950 for (s = inword; (c = *s) != NUL; ++s) | |
6951 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6952 if (VIM_ISWHITE(c)) |
375 | 6953 c = ' '; |
6954 else | |
6955 c = slang->sl_sal_first[c]; | |
6956 if (c != NUL && (ri == 0 || res[ri - 1] != c)) | |
6957 res[ri++] = c; | |
6958 } | |
6959 } | |
6960 | |
6961 res[ri] = NUL; | |
6962 } | |
6963 | |
6964 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
6965 spell_soundfold_sal(slang_T *slang, char_u *inword, char_u *res) |
323 | 6966 { |
344 | 6967 salitem_T *smp; |
323 | 6968 char_u word[MAXWLEN]; |
375 | 6969 char_u *s = inword; |
323 | 6970 char_u *t; |
344 | 6971 char_u *pf; |
323 | 6972 int i, j, z; |
344 | 6973 int reslen; |
323 | 6974 int n, k = 0; |
6975 int z0; | |
6976 int k0; | |
6977 int n0; | |
6978 int c; | |
6979 int pri; | |
6980 int p0 = -333; | |
6981 int c0; | |
6982 | |
324 | 6983 /* Remove accents, if wanted. We actually remove all non-word characters. |
375 | 6984 * But keep white space. We need a copy, the word may be changed here. */ |
323 | 6985 if (slang->sl_rem_accents) |
6986 { | |
6987 t = word; | |
375 | 6988 while (*s != NUL) |
323 | 6989 { |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
6990 if (VIM_ISWHITE(*s)) |
344 | 6991 { |
6992 *t++ = ' '; | |
6993 s = skipwhite(s); | |
6994 } | |
323 | 6995 else |
6996 { | |
5477 | 6997 if (spell_iswordp_nmw(s, curwin)) |
323 | 6998 *t++ = *s; |
6999 ++s; | |
7000 } | |
7001 } | |
7002 *t = NUL; | |
7003 } | |
7004 else | |
2768 | 7005 vim_strncpy(word, s, MAXWLEN - 1); |
323 | 7006 |
344 | 7007 smp = (salitem_T *)slang->sl_sal.ga_data; |
323 | 7008 |
7009 /* | |
7010 * This comes from Aspell phonet.cpp. Converted from C++ to C. | |
324 | 7011 * Changed to keep spaces. |
323 | 7012 */ |
344 | 7013 i = reslen = z = 0; |
323 | 7014 while ((c = word[i]) != NUL) |
7015 { | |
344 | 7016 /* Start with the first rule that has the character in the word. */ |
323 | 7017 n = slang->sl_sal_first[c]; |
7018 z0 = 0; | |
7019 | |
7020 if (n >= 0) | |
7021 { | |
7022 /* check all rules for the same letter */ | |
344 | 7023 for (; (s = smp[n].sm_lead)[0] == c; ++n) |
323 | 7024 { |
344 | 7025 /* Quickly skip entries that don't match the word. Most |
7026 * entries are less then three chars, optimize for that. */ | |
7027 k = smp[n].sm_leadlen; | |
7028 if (k > 1) | |
323 | 7029 { |
344 | 7030 if (word[i + 1] != s[1]) |
7031 continue; | |
7032 if (k > 2) | |
7033 { | |
7034 for (j = 2; j < k; ++j) | |
7035 if (word[i + j] != s[j]) | |
7036 break; | |
7037 if (j < k) | |
7038 continue; | |
7039 } | |
323 | 7040 } |
7041 | |
375 | 7042 if ((pf = smp[n].sm_oneof) != NULL) |
323 | 7043 { |
375 | 7044 /* Check for match with one of the chars in "sm_oneof". */ |
344 | 7045 while (*pf != NUL && *pf != word[i + k]) |
7046 ++pf; | |
7047 if (*pf == NUL) | |
7048 continue; | |
7049 ++k; | |
323 | 7050 } |
344 | 7051 s = smp[n].sm_rules; |
7052 pri = 5; /* default priority */ | |
323 | 7053 |
7054 p0 = *s; | |
7055 k0 = k; | |
7056 while (*s == '-' && k > 1) | |
7057 { | |
7058 k--; | |
7059 s++; | |
7060 } | |
7061 if (*s == '<') | |
7062 s++; | |
344 | 7063 if (VIM_ISDIGIT(*s)) |
323 | 7064 { |
7065 /* determine priority */ | |
7066 pri = *s - '0'; | |
7067 s++; | |
7068 } | |
7069 if (*s == '^' && *(s + 1) == '^') | |
7070 s++; | |
7071 | |
7072 if (*s == NUL | |
7073 || (*s == '^' | |
324 | 7074 && (i == 0 || !(word[i - 1] == ' ' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7075 || spell_iswordp(word + i - 1, curwin))) |
323 | 7076 && (*(s + 1) != '$' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7077 || (!spell_iswordp(word + i + k0, curwin)))) |
323 | 7078 || (*s == '$' && i > 0 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7079 && spell_iswordp(word + i - 1, curwin) |
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 { |
7082 /* search for followup rules, if: */ | |
7083 /* followup and k > 1 and NO '-' in searchstring */ | |
7084 c0 = word[i + k - 1]; | |
7085 n0 = slang->sl_sal_first[c0]; | |
7086 | |
7087 if (slang->sl_followup && k > 1 && n0 >= 0 | |
344 | 7088 && p0 != '-' && word[i + k] != NUL) |
323 | 7089 { |
7090 /* test follow-up rule for "word[i + k]" */ | |
344 | 7091 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0) |
323 | 7092 { |
344 | 7093 /* Quickly skip entries that don't match the word. |
7094 * */ | |
7095 k0 = smp[n0].sm_leadlen; | |
7096 if (k0 > 1) | |
323 | 7097 { |
344 | 7098 if (word[i + k] != s[1]) |
7099 continue; | |
7100 if (k0 > 2) | |
7101 { | |
7102 pf = word + i + k + 1; | |
7103 for (j = 2; j < k0; ++j) | |
7104 if (*pf++ != s[j]) | |
7105 break; | |
7106 if (j < k0) | |
7107 continue; | |
7108 } | |
323 | 7109 } |
344 | 7110 k0 += k - 1; |
7111 | |
375 | 7112 if ((pf = smp[n0].sm_oneof) != NULL) |
323 | 7113 { |
344 | 7114 /* Check for match with one of the chars in |
375 | 7115 * "sm_oneof". */ |
344 | 7116 while (*pf != NUL && *pf != word[i + k0]) |
7117 ++pf; | |
7118 if (*pf == NUL) | |
7119 continue; | |
7120 ++k0; | |
323 | 7121 } |
344 | 7122 |
7123 p0 = 5; | |
7124 s = smp[n0].sm_rules; | |
323 | 7125 while (*s == '-') |
7126 { | |
344 | 7127 /* "k0" gets NOT reduced because |
7128 * "if (k0 == k)" */ | |
323 | 7129 s++; |
7130 } | |
7131 if (*s == '<') | |
7132 s++; | |
344 | 7133 if (VIM_ISDIGIT(*s)) |
323 | 7134 { |
7135 p0 = *s - '0'; | |
7136 s++; | |
7137 } | |
7138 | |
7139 if (*s == NUL | |
7140 /* *s == '^' cuts */ | |
7141 || (*s == '$' | |
376 | 7142 && !spell_iswordp(word + i + k0, |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7143 curwin))) |
323 | 7144 { |
7145 if (k0 == k) | |
7146 /* this is just a piece of the string */ | |
7147 continue; | |
7148 | |
7149 if (p0 < pri) | |
7150 /* priority too low */ | |
7151 continue; | |
7152 /* rule fits; stop search */ | |
7153 break; | |
7154 } | |
7155 } | |
7156 | |
344 | 7157 if (p0 >= pri && smp[n0].sm_lead[0] == c0) |
323 | 7158 continue; |
7159 } | |
7160 | |
7161 /* replace string */ | |
344 | 7162 s = smp[n].sm_to; |
389 | 7163 if (s == NULL) |
7164 s = (char_u *)""; | |
344 | 7165 pf = smp[n].sm_rules; |
7166 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0; | |
323 | 7167 if (p0 == 1 && z == 0) |
7168 { | |
7169 /* rule with '<' is used */ | |
344 | 7170 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c |
7171 || res[reslen - 1] == *s)) | |
7172 reslen--; | |
323 | 7173 z0 = 1; |
7174 z = 1; | |
7175 k0 = 0; | |
372 | 7176 while (*s != NUL && word[i + k0] != NUL) |
323 | 7177 { |
7178 word[i + k0] = *s; | |
7179 k0++; | |
7180 s++; | |
7181 } | |
7182 if (k > k0) | |
1619 | 7183 STRMOVE(word + i + k0, word + i + k); |
323 | 7184 |
7185 /* new "actual letter" */ | |
7186 c = word[i]; | |
7187 } | |
7188 else | |
7189 { | |
7190 /* no '<' rule used */ | |
7191 i += k - 1; | |
7192 z = 0; | |
344 | 7193 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN) |
323 | 7194 { |
344 | 7195 if (reslen == 0 || res[reslen - 1] != *s) |
372 | 7196 res[reslen++] = *s; |
323 | 7197 s++; |
7198 } | |
7199 /* new "actual letter" */ | |
7200 c = *s; | |
344 | 7201 if (strstr((char *)pf, "^^") != NULL) |
323 | 7202 { |
7203 if (c != NUL) | |
372 | 7204 res[reslen++] = c; |
1619 | 7205 STRMOVE(word, word + i + 1); |
323 | 7206 i = 0; |
7207 z0 = 1; | |
7208 } | |
7209 } | |
7210 break; | |
7211 } | |
7212 } | |
7213 } | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7214 else if (VIM_ISWHITE(c)) |
324 | 7215 { |
7216 c = ' '; | |
7217 k = 1; | |
7218 } | |
323 | 7219 |
7220 if (z0 == 0) | |
7221 { | |
344 | 7222 if (k && !p0 && reslen < MAXWLEN && c != NUL |
7223 && (!slang->sl_collapse || reslen == 0 | |
7224 || res[reslen - 1] != c)) | |
323 | 7225 /* condense only double letters */ |
372 | 7226 res[reslen++] = c; |
323 | 7227 |
7228 i++; | |
7229 z = 0; | |
7230 k = 0; | |
7231 } | |
7232 } | |
7233 | |
344 | 7234 res[reslen] = NUL; |
323 | 7235 } |
7236 | |
372 | 7237 /* |
7238 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]". | |
7239 * Multi-byte version of spell_soundfold(). | |
7240 */ | |
7241 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7242 spell_soundfold_wsal(slang_T *slang, char_u *inword, char_u *res) |
372 | 7243 { |
375 | 7244 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data; |
372 | 7245 int word[MAXWLEN]; |
7246 int wres[MAXWLEN]; | |
7247 int l; | |
7248 char_u *s; | |
7249 int *ws; | |
7250 char_u *t; | |
7251 int *pf; | |
7252 int i, j, z; | |
7253 int reslen; | |
7254 int n, k = 0; | |
7255 int z0; | |
7256 int k0; | |
7257 int n0; | |
7258 int c; | |
7259 int pri; | |
7260 int p0 = -333; | |
7261 int c0; | |
7262 int did_white = FALSE; | |
3520 | 7263 int wordlen; |
7264 | |
372 | 7265 |
7266 /* | |
7267 * Convert the multi-byte string to a wide-character string. | |
7268 * Remove accents, if wanted. We actually remove all non-word characters. | |
7269 * But keep white space. | |
7270 */ | |
3520 | 7271 wordlen = 0; |
372 | 7272 for (s = inword; *s != NUL; ) |
7273 { | |
7274 t = s; | |
474 | 7275 c = mb_cptr2char_adv(&s); |
372 | 7276 if (slang->sl_rem_accents) |
7277 { | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7278 if (enc_utf8 ? utf_class(c) == 0 : VIM_ISWHITE(c)) |
372 | 7279 { |
7280 if (did_white) | |
7281 continue; | |
7282 c = ' '; | |
7283 did_white = TRUE; | |
7284 } | |
7285 else | |
7286 { | |
7287 did_white = FALSE; | |
5477 | 7288 if (!spell_iswordp_nmw(t, curwin)) |
372 | 7289 continue; |
7290 } | |
7291 } | |
3520 | 7292 word[wordlen++] = c; |
7293 } | |
7294 word[wordlen] = NUL; | |
372 | 7295 |
7296 /* | |
3520 | 7297 * This algorithm comes from Aspell phonet.cpp. |
372 | 7298 * Converted from C++ to C. Added support for multi-byte chars. |
7299 * Changed to keep spaces. | |
7300 */ | |
7301 i = reslen = z = 0; | |
7302 while ((c = word[i]) != NUL) | |
7303 { | |
7304 /* Start with the first rule that has the character in the word. */ | |
7305 n = slang->sl_sal_first[c & 0xff]; | |
7306 z0 = 0; | |
7307 | |
7308 if (n >= 0) | |
7309 { | |
2455
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7310 /* 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
|
7311 * 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
|
7312 * (c & 0xff) is NUL. */ |
9367de3e2e1b
Fix: crash in spell checking with a 0x300 character.
Bram Moolenaar <bram@vim.org>
parents:
2454
diff
changeset
|
7313 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
|
7314 && ws[0] != NUL; ++n) |
372 | 7315 { |
7316 /* Quickly skip entries that don't match the word. Most | |
7317 * entries are less then three chars, optimize for that. */ | |
375 | 7318 if (c != ws[0]) |
7319 continue; | |
372 | 7320 k = smp[n].sm_leadlen; |
7321 if (k > 1) | |
7322 { | |
7323 if (word[i + 1] != ws[1]) | |
7324 continue; | |
7325 if (k > 2) | |
7326 { | |
7327 for (j = 2; j < k; ++j) | |
7328 if (word[i + j] != ws[j]) | |
7329 break; | |
7330 if (j < k) | |
7331 continue; | |
7332 } | |
7333 } | |
7334 | |
375 | 7335 if ((pf = smp[n].sm_oneof_w) != NULL) |
372 | 7336 { |
375 | 7337 /* Check for match with one of the chars in "sm_oneof". */ |
372 | 7338 while (*pf != NUL && *pf != word[i + k]) |
7339 ++pf; | |
7340 if (*pf == NUL) | |
7341 continue; | |
7342 ++k; | |
7343 } | |
7344 s = smp[n].sm_rules; | |
7345 pri = 5; /* default priority */ | |
7346 | |
7347 p0 = *s; | |
7348 k0 = k; | |
7349 while (*s == '-' && k > 1) | |
7350 { | |
7351 k--; | |
7352 s++; | |
7353 } | |
7354 if (*s == '<') | |
7355 s++; | |
7356 if (VIM_ISDIGIT(*s)) | |
7357 { | |
7358 /* determine priority */ | |
7359 pri = *s - '0'; | |
7360 s++; | |
7361 } | |
7362 if (*s == '^' && *(s + 1) == '^') | |
7363 s++; | |
7364 | |
7365 if (*s == NUL | |
7366 || (*s == '^' | |
7367 && (i == 0 || !(word[i - 1] == ' ' | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7368 || spell_iswordp_w(word + i - 1, curwin))) |
372 | 7369 && (*(s + 1) != '$' |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7370 || (!spell_iswordp_w(word + i + k0, curwin)))) |
372 | 7371 || (*s == '$' && i > 0 |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7372 && spell_iswordp_w(word + i - 1, curwin) |
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 { |
7375 /* search for followup rules, if: */ | |
7376 /* followup and k > 1 and NO '-' in searchstring */ | |
7377 c0 = word[i + k - 1]; | |
7378 n0 = slang->sl_sal_first[c0 & 0xff]; | |
7379 | |
7380 if (slang->sl_followup && k > 1 && n0 >= 0 | |
7381 && p0 != '-' && word[i + k] != NUL) | |
7382 { | |
375 | 7383 /* Test follow-up rule for "word[i + k]"; loop over |
7384 * all entries with the same index byte. */ | |
372 | 7385 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff) |
7386 == (c0 & 0xff); ++n0) | |
7387 { | |
7388 /* Quickly skip entries that don't match the word. | |
375 | 7389 */ |
7390 if (c0 != ws[0]) | |
7391 continue; | |
372 | 7392 k0 = smp[n0].sm_leadlen; |
7393 if (k0 > 1) | |
7394 { | |
7395 if (word[i + k] != ws[1]) | |
7396 continue; | |
7397 if (k0 > 2) | |
7398 { | |
7399 pf = word + i + k + 1; | |
7400 for (j = 2; j < k0; ++j) | |
7401 if (*pf++ != ws[j]) | |
7402 break; | |
7403 if (j < k0) | |
7404 continue; | |
7405 } | |
7406 } | |
7407 k0 += k - 1; | |
7408 | |
375 | 7409 if ((pf = smp[n0].sm_oneof_w) != NULL) |
372 | 7410 { |
7411 /* Check for match with one of the chars in | |
375 | 7412 * "sm_oneof". */ |
372 | 7413 while (*pf != NUL && *pf != word[i + k0]) |
7414 ++pf; | |
7415 if (*pf == NUL) | |
7416 continue; | |
7417 ++k0; | |
7418 } | |
7419 | |
7420 p0 = 5; | |
7421 s = smp[n0].sm_rules; | |
7422 while (*s == '-') | |
7423 { | |
7424 /* "k0" gets NOT reduced because | |
7425 * "if (k0 == k)" */ | |
7426 s++; | |
7427 } | |
7428 if (*s == '<') | |
7429 s++; | |
7430 if (VIM_ISDIGIT(*s)) | |
7431 { | |
7432 p0 = *s - '0'; | |
7433 s++; | |
7434 } | |
7435 | |
7436 if (*s == NUL | |
7437 /* *s == '^' cuts */ | |
7438 || (*s == '$' | |
376 | 7439 && !spell_iswordp_w(word + i + k0, |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
7440 curwin))) |
372 | 7441 { |
7442 if (k0 == k) | |
7443 /* this is just a piece of the string */ | |
7444 continue; | |
7445 | |
7446 if (p0 < pri) | |
7447 /* priority too low */ | |
7448 continue; | |
7449 /* rule fits; stop search */ | |
7450 break; | |
7451 } | |
7452 } | |
7453 | |
7454 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff) | |
7455 == (c0 & 0xff)) | |
7456 continue; | |
7457 } | |
7458 | |
7459 /* replace string */ | |
7460 ws = smp[n].sm_to_w; | |
7461 s = smp[n].sm_rules; | |
7462 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0; | |
7463 if (p0 == 1 && z == 0) | |
7464 { | |
7465 /* rule with '<' is used */ | |
389 | 7466 if (reslen > 0 && ws != NULL && *ws != NUL |
7467 && (wres[reslen - 1] == c | |
372 | 7468 || wres[reslen - 1] == *ws)) |
7469 reslen--; | |
7470 z0 = 1; | |
7471 z = 1; | |
7472 k0 = 0; | |
389 | 7473 if (ws != NULL) |
7474 while (*ws != NUL && word[i + k0] != NUL) | |
7475 { | |
7476 word[i + k0] = *ws; | |
7477 k0++; | |
7478 ws++; | |
7479 } | |
372 | 7480 if (k > k0) |
7481 mch_memmove(word + i + k0, word + i + k, | |
3520 | 7482 sizeof(int) * (wordlen - (i + k) + 1)); |
372 | 7483 |
7484 /* new "actual letter" */ | |
7485 c = word[i]; | |
7486 } | |
7487 else | |
7488 { | |
7489 /* no '<' rule used */ | |
7490 i += k - 1; | |
7491 z = 0; | |
389 | 7492 if (ws != NULL) |
7493 while (*ws != NUL && ws[1] != NUL | |
7494 && reslen < MAXWLEN) | |
7495 { | |
7496 if (reslen == 0 || wres[reslen - 1] != *ws) | |
7497 wres[reslen++] = *ws; | |
7498 ws++; | |
7499 } | |
372 | 7500 /* new "actual letter" */ |
389 | 7501 if (ws == NULL) |
7502 c = NUL; | |
7503 else | |
7504 c = *ws; | |
372 | 7505 if (strstr((char *)s, "^^") != NULL) |
7506 { | |
7507 if (c != NUL) | |
7508 wres[reslen++] = c; | |
7509 mch_memmove(word, word + i + 1, | |
3520 | 7510 sizeof(int) * (wordlen - (i + 1) + 1)); |
372 | 7511 i = 0; |
7512 z0 = 1; | |
7513 } | |
7514 } | |
7515 break; | |
7516 } | |
7517 } | |
7518 } | |
11129
f4ea50924c6d
patch 8.0.0452: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11127
diff
changeset
|
7519 else if (VIM_ISWHITE(c)) |
372 | 7520 { |
7521 c = ' '; | |
7522 k = 1; | |
7523 } | |
7524 | |
7525 if (z0 == 0) | |
7526 { | |
7527 if (k && !p0 && reslen < MAXWLEN && c != NUL | |
7528 && (!slang->sl_collapse || reslen == 0 | |
7529 || wres[reslen - 1] != c)) | |
7530 /* condense only double letters */ | |
7531 wres[reslen++] = c; | |
7532 | |
7533 i++; | |
7534 z = 0; | |
7535 k = 0; | |
7536 } | |
7537 } | |
7538 | |
7539 /* Convert wide characters in "wres" to a multi-byte string in "res". */ | |
7540 l = 0; | |
7541 for (n = 0; n < reslen; ++n) | |
7542 { | |
7543 l += mb_char2bytes(wres[n], res + l); | |
7544 if (l + MB_MAXBYTES > MAXWLEN) | |
7545 break; | |
7546 } | |
7547 res[l] = NUL; | |
7548 } | |
7549 | |
324 | 7550 /* |
344 | 7551 * Compute a score for two sound-a-like words. |
7552 * This permits up to two inserts/deletes/swaps/etc. to keep things fast. | |
7553 * Instead of a generic loop we write out the code. That keeps it fast by | |
7554 * avoiding checks that will not be possible. | |
7555 */ | |
7556 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7557 soundalike_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7558 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
|
7559 char_u *badstart) /* sound-folded bad word */ |
344 | 7560 { |
351 | 7561 char_u *goodsound = goodstart; |
7562 char_u *badsound = badstart; | |
7563 int goodlen; | |
7564 int badlen; | |
344 | 7565 int n; |
7566 char_u *pl, *ps; | |
7567 char_u *pl2, *ps2; | |
351 | 7568 int score = 0; |
7569 | |
2454
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7570 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be |
351 | 7571 * counted so much, vowels halfway the word aren't counted at all. */ |
7572 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound) | |
7573 { | |
2454
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7574 if ((badsound[0] == NUL && goodsound[1] == NUL) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7575 || (goodsound[0] == NUL && badsound[1] == NUL)) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7576 /* 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
|
7577 return SCORE_DEL; |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7578 if (badsound[0] == NUL || goodsound[0] == NUL) |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7579 /* more than two changes */ |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7580 return SCORE_MAXMAX; |
91fec91a522c
Fix: uninitialized memory access. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
7581 |
625 | 7582 if (badsound[1] == goodsound[1] |
7583 || (badsound[1] != NUL | |
7584 && goodsound[1] != NUL | |
7585 && badsound[2] == goodsound[2])) | |
7586 { | |
7587 /* handle like a substitute */ | |
7588 } | |
351 | 7589 else |
625 | 7590 { |
7591 score = 2 * SCORE_DEL / 3; | |
7592 if (*badsound == '*') | |
7593 ++badsound; | |
7594 else | |
7595 ++goodsound; | |
7596 } | |
351 | 7597 } |
7598 | |
835 | 7599 goodlen = (int)STRLEN(goodsound); |
7600 badlen = (int)STRLEN(badsound); | |
344 | 7601 |
1188 | 7602 /* Return quickly if the lengths are too different to be fixed by two |
344 | 7603 * changes. */ |
7604 n = goodlen - badlen; | |
7605 if (n < -2 || n > 2) | |
7606 return SCORE_MAXMAX; | |
7607 | |
7608 if (n > 0) | |
7609 { | |
351 | 7610 pl = goodsound; /* goodsound is longest */ |
344 | 7611 ps = badsound; |
7612 } | |
7613 else | |
7614 { | |
351 | 7615 pl = badsound; /* badsound is longest */ |
344 | 7616 ps = goodsound; |
7617 } | |
7618 | |
7619 /* Skip over the identical part. */ | |
7620 while (*pl == *ps && *pl != NUL) | |
7621 { | |
7622 ++pl; | |
7623 ++ps; | |
7624 } | |
7625 | |
7626 switch (n) | |
7627 { | |
7628 case -2: | |
7629 case 2: | |
7630 /* | |
7631 * Must delete two characters from "pl". | |
7632 */ | |
7633 ++pl; /* first delete */ | |
7634 while (*pl == *ps) | |
7635 { | |
7636 ++pl; | |
7637 ++ps; | |
7638 } | |
7639 /* strings must be equal after second delete */ | |
7640 if (STRCMP(pl + 1, ps) == 0) | |
351 | 7641 return score + SCORE_DEL * 2; |
344 | 7642 |
7643 /* Failed to compare. */ | |
7644 break; | |
7645 | |
7646 case -1: | |
7647 case 1: | |
7648 /* | |
7649 * Minimal one delete from "pl" required. | |
7650 */ | |
7651 | |
7652 /* 1: delete */ | |
7653 pl2 = pl + 1; | |
7654 ps2 = ps; | |
7655 while (*pl2 == *ps2) | |
7656 { | |
7657 if (*pl2 == NUL) /* reached the end */ | |
351 | 7658 return score + SCORE_DEL; |
344 | 7659 ++pl2; |
7660 ++ps2; | |
7661 } | |
7662 | |
7663 /* 2: delete then swap, then rest must be equal */ | |
7664 if (pl2[0] == ps2[1] && pl2[1] == ps2[0] | |
7665 && STRCMP(pl2 + 2, ps2 + 2) == 0) | |
351 | 7666 return score + SCORE_DEL + SCORE_SWAP; |
344 | 7667 |
7668 /* 3: delete then substitute, then the rest must be equal */ | |
7669 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7670 return score + SCORE_DEL + SCORE_SUBST; |
344 | 7671 |
7672 /* 4: first swap then delete */ | |
7673 if (pl[0] == ps[1] && pl[1] == ps[0]) | |
7674 { | |
7675 pl2 = pl + 2; /* swap, skip two chars */ | |
7676 ps2 = ps + 2; | |
7677 while (*pl2 == *ps2) | |
7678 { | |
7679 ++pl2; | |
7680 ++ps2; | |
7681 } | |
7682 /* delete a char and then strings must be equal */ | |
7683 if (STRCMP(pl2 + 1, ps2) == 0) | |
351 | 7684 return score + SCORE_SWAP + SCORE_DEL; |
344 | 7685 } |
7686 | |
7687 /* 5: first substitute then delete */ | |
7688 pl2 = pl + 1; /* substitute, skip one char */ | |
7689 ps2 = ps + 1; | |
7690 while (*pl2 == *ps2) | |
7691 { | |
7692 ++pl2; | |
7693 ++ps2; | |
7694 } | |
7695 /* delete a char and then strings must be equal */ | |
7696 if (STRCMP(pl2 + 1, ps2) == 0) | |
351 | 7697 return score + SCORE_SUBST + SCORE_DEL; |
344 | 7698 |
7699 /* Failed to compare. */ | |
7700 break; | |
7701 | |
7702 case 0: | |
7703 /* | |
1808 | 7704 * Lengths are equal, thus changes must result in same length: An |
344 | 7705 * insert is only possible in combination with a delete. |
7706 * 1: check if for identical strings | |
7707 */ | |
7708 if (*pl == NUL) | |
351 | 7709 return score; |
344 | 7710 |
7711 /* 2: swap */ | |
7712 if (pl[0] == ps[1] && pl[1] == ps[0]) | |
7713 { | |
7714 pl2 = pl + 2; /* swap, skip two chars */ | |
7715 ps2 = ps + 2; | |
7716 while (*pl2 == *ps2) | |
7717 { | |
7718 if (*pl2 == NUL) /* reached the end */ | |
351 | 7719 return score + SCORE_SWAP; |
344 | 7720 ++pl2; |
7721 ++ps2; | |
7722 } | |
7723 /* 3: swap and swap again */ | |
7724 if (pl2[0] == ps2[1] && pl2[1] == ps2[0] | |
7725 && STRCMP(pl2 + 2, ps2 + 2) == 0) | |
351 | 7726 return score + SCORE_SWAP + SCORE_SWAP; |
344 | 7727 |
7728 /* 4: swap and substitute */ | |
7729 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7730 return score + SCORE_SWAP + SCORE_SUBST; |
344 | 7731 } |
7732 | |
7733 /* 5: substitute */ | |
7734 pl2 = pl + 1; | |
7735 ps2 = ps + 1; | |
7736 while (*pl2 == *ps2) | |
7737 { | |
7738 if (*pl2 == NUL) /* reached the end */ | |
351 | 7739 return score + SCORE_SUBST; |
344 | 7740 ++pl2; |
7741 ++ps2; | |
7742 } | |
7743 | |
7744 /* 6: substitute and swap */ | |
7745 if (pl2[0] == ps2[1] && pl2[1] == ps2[0] | |
7746 && STRCMP(pl2 + 2, ps2 + 2) == 0) | |
351 | 7747 return score + SCORE_SUBST + SCORE_SWAP; |
344 | 7748 |
7749 /* 7: substitute and substitute */ | |
7750 if (STRCMP(pl2 + 1, ps2 + 1) == 0) | |
351 | 7751 return score + SCORE_SUBST + SCORE_SUBST; |
344 | 7752 |
7753 /* 8: insert then delete */ | |
7754 pl2 = pl; | |
7755 ps2 = ps + 1; | |
7756 while (*pl2 == *ps2) | |
7757 { | |
7758 ++pl2; | |
7759 ++ps2; | |
7760 } | |
7761 if (STRCMP(pl2 + 1, ps2) == 0) | |
351 | 7762 return score + SCORE_INS + SCORE_DEL; |
344 | 7763 |
7764 /* 9: delete then insert */ | |
7765 pl2 = pl + 1; | |
7766 ps2 = ps; | |
7767 while (*pl2 == *ps2) | |
7768 { | |
7769 ++pl2; | |
7770 ++ps2; | |
7771 } | |
7772 if (STRCMP(pl2, ps2 + 1) == 0) | |
351 | 7773 return score + SCORE_INS + SCORE_DEL; |
344 | 7774 |
7775 /* Failed to compare. */ | |
7776 break; | |
7777 } | |
7778 | |
7779 return SCORE_MAXMAX; | |
7780 } | |
324 | 7781 |
323 | 7782 /* |
7783 * Compute the "edit distance" to turn "badword" into "goodword". The less | |
344 | 7784 * deletes/inserts/substitutes/swaps are required the lower the score. |
324 | 7785 * |
485 | 7786 * The algorithm is described by Du and Chang, 1992. |
7787 * The implementation of the algorithm comes from Aspell editdist.cpp, | |
7788 * edit_distance(). It has been converted from C++ to C and modified to | |
7789 * support multi-byte characters. | |
323 | 7790 */ |
7791 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7792 spell_edit_score( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7793 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7794 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7795 char_u *goodword) |
323 | 7796 { |
7797 int *cnt; | |
1188 | 7798 int badlen, goodlen; /* lengths including NUL */ |
323 | 7799 int j, i; |
7800 int t; | |
7801 int bc, gc; | |
324 | 7802 int pbc, pgc; |
7803 char_u *p; | |
7804 int wbadword[MAXWLEN]; | |
7805 int wgoodword[MAXWLEN]; | |
7806 | |
7807 if (has_mbyte) | |
7808 { | |
7809 /* Get the characters from the multi-byte strings and put them in an | |
7810 * int array for easy access. */ | |
7811 for (p = badword, badlen = 0; *p != NUL; ) | |
474 | 7812 wbadword[badlen++] = mb_cptr2char_adv(&p); |
398 | 7813 wbadword[badlen++] = 0; |
324 | 7814 for (p = goodword, goodlen = 0; *p != NUL; ) |
474 | 7815 wgoodword[goodlen++] = mb_cptr2char_adv(&p); |
398 | 7816 wgoodword[goodlen++] = 0; |
324 | 7817 } |
7818 else | |
7819 { | |
835 | 7820 badlen = (int)STRLEN(badword) + 1; |
7821 goodlen = (int)STRLEN(goodword) + 1; | |
324 | 7822 } |
323 | 7823 |
7824 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */ | |
7825 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)] | |
16825
ce04ebdf26b8
patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents:
16768
diff
changeset
|
7826 cnt = ALLOC_MULT(int, (badlen + 1) * (goodlen + 1)); |
324 | 7827 if (cnt == NULL) |
7828 return 0; /* out of memory */ | |
323 | 7829 |
7830 CNT(0, 0) = 0; | |
7831 for (j = 1; j <= goodlen; ++j) | |
625 | 7832 CNT(0, j) = CNT(0, j - 1) + SCORE_INS; |
323 | 7833 |
7834 for (i = 1; i <= badlen; ++i) | |
7835 { | |
625 | 7836 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL; |
323 | 7837 for (j = 1; j <= goodlen; ++j) |
7838 { | |
324 | 7839 if (has_mbyte) |
7840 { | |
7841 bc = wbadword[i - 1]; | |
7842 gc = wgoodword[j - 1]; | |
7843 } | |
7844 else | |
7845 { | |
7846 bc = badword[i - 1]; | |
7847 gc = goodword[j - 1]; | |
7848 } | |
323 | 7849 if (bc == gc) |
7850 CNT(i, j) = CNT(i - 1, j - 1); | |
7851 else | |
7852 { | |
7853 /* Use a better score when there is only a case difference. */ | |
324 | 7854 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) |
323 | 7855 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1); |
7856 else | |
625 | 7857 { |
7858 /* For a similar character use SCORE_SIMILAR. */ | |
7859 if (slang != NULL | |
7860 && slang->sl_has_map | |
7861 && similar_chars(slang, gc, bc)) | |
7862 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1); | |
7863 else | |
7864 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1); | |
7865 } | |
323 | 7866 |
324 | 7867 if (i > 1 && j > 1) |
323 | 7868 { |
324 | 7869 if (has_mbyte) |
7870 { | |
7871 pbc = wbadword[i - 2]; | |
7872 pgc = wgoodword[j - 2]; | |
7873 } | |
7874 else | |
7875 { | |
7876 pbc = badword[i - 2]; | |
7877 pgc = goodword[j - 2]; | |
7878 } | |
7879 if (bc == pgc && pbc == gc) | |
7880 { | |
7881 t = SCORE_SWAP + CNT(i - 2, j - 2); | |
7882 if (t < CNT(i, j)) | |
7883 CNT(i, j) = t; | |
7884 } | |
323 | 7885 } |
7886 t = SCORE_DEL + CNT(i - 1, j); | |
7887 if (t < CNT(i, j)) | |
7888 CNT(i, j) = t; | |
7889 t = SCORE_INS + CNT(i, j - 1); | |
7890 if (t < CNT(i, j)) | |
7891 CNT(i, j) = t; | |
7892 } | |
7893 } | |
7894 } | |
344 | 7895 |
7896 i = CNT(badlen - 1, goodlen - 1); | |
7897 vim_free(cnt); | |
7898 return i; | |
323 | 7899 } |
307 | 7900 |
625 | 7901 typedef struct |
7902 { | |
7903 int badi; | |
7904 int goodi; | |
7905 int score; | |
7906 } limitscore_T; | |
7907 | |
7908 /* | |
7909 * Like spell_edit_score(), but with a limit on the score to make it faster. | |
7910 * May return SCORE_MAXMAX when the score is higher than "limit". | |
7911 * | |
7912 * This uses a stack for the edits still to be tried. | |
7913 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support | |
7914 * for multi-byte characters. | |
7915 */ | |
7916 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7917 spell_edit_score_limit( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7918 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7919 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7920 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
7921 int limit) |
625 | 7922 { |
7923 limitscore_T stack[10]; /* allow for over 3 * 2 edits */ | |
7924 int stackidx; | |
7925 int bi, gi; | |
7926 int bi2, gi2; | |
7927 int bc, gc; | |
7928 int score; | |
7929 int score_off; | |
7930 int minscore; | |
7931 int round; | |
7932 | |
7933 /* Multi-byte characters require a bit more work, use a different function | |
7934 * to avoid testing "has_mbyte" quite often. */ | |
7935 if (has_mbyte) | |
7936 return spell_edit_score_limit_w(slang, badword, goodword, limit); | |
7937 | |
7938 /* | |
7939 * The idea is to go from start to end over the words. So long as | |
7940 * characters are equal just continue, this always gives the lowest score. | |
7941 * When there is a difference try several alternatives. Each alternative | |
7942 * increases "score" for the edit distance. Some of the alternatives are | |
7943 * pushed unto a stack and tried later, some are tried right away. At the | |
7944 * end of the word the score for one alternative is known. The lowest | |
7945 * possible score is stored in "minscore". | |
7946 */ | |
7947 stackidx = 0; | |
7948 bi = 0; | |
7949 gi = 0; | |
7950 score = 0; | |
7951 minscore = limit + 1; | |
7952 | |
7953 for (;;) | |
7954 { | |
7955 /* Skip over an equal part, score remains the same. */ | |
7956 for (;;) | |
7957 { | |
7958 bc = badword[bi]; | |
7959 gc = goodword[gi]; | |
7960 if (bc != gc) /* stop at a char that's different */ | |
7961 break; | |
7962 if (bc == NUL) /* both words end */ | |
7963 { | |
7964 if (score < minscore) | |
7965 minscore = score; | |
7966 goto pop; /* do next alternative */ | |
7967 } | |
7968 ++bi; | |
7969 ++gi; | |
7970 } | |
7971 | |
7972 if (gc == NUL) /* goodword ends, delete badword chars */ | |
7973 { | |
7974 do | |
7975 { | |
7976 if ((score += SCORE_DEL) >= minscore) | |
7977 goto pop; /* do next alternative */ | |
7978 } while (badword[++bi] != NUL); | |
7979 minscore = score; | |
7980 } | |
7981 else if (bc == NUL) /* badword ends, insert badword chars */ | |
7982 { | |
7983 do | |
7984 { | |
7985 if ((score += SCORE_INS) >= minscore) | |
7986 goto pop; /* do next alternative */ | |
7987 } while (goodword[++gi] != NUL); | |
7988 minscore = score; | |
7989 } | |
7990 else /* both words continue */ | |
7991 { | |
7992 /* If not close to the limit, perform a change. Only try changes | |
7993 * that may lead to a lower score than "minscore". | |
7994 * round 0: try deleting a char from badword | |
7995 * round 1: try inserting a char in badword */ | |
7996 for (round = 0; round <= 1; ++round) | |
7997 { | |
7998 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS); | |
7999 if (score_off < minscore) | |
8000 { | |
8001 if (score_off + SCORE_EDIT_MIN >= minscore) | |
8002 { | |
8003 /* Near the limit, rest of the words must match. We | |
8004 * can check that right now, no need to push an item | |
8005 * onto the stack. */ | |
8006 bi2 = bi + 1 - round; | |
8007 gi2 = gi + round; | |
8008 while (goodword[gi2] == badword[bi2]) | |
8009 { | |
8010 if (goodword[gi2] == NUL) | |
8011 { | |
8012 minscore = score_off; | |
8013 break; | |
8014 } | |
8015 ++bi2; | |
8016 ++gi2; | |
8017 } | |
8018 } | |
8019 else | |
8020 { | |
8021 /* try deleting/inserting a character later */ | |
8022 stack[stackidx].badi = bi + 1 - round; | |
8023 stack[stackidx].goodi = gi + round; | |
8024 stack[stackidx].score = score_off; | |
8025 ++stackidx; | |
8026 } | |
8027 } | |
8028 } | |
8029 | |
8030 if (score + SCORE_SWAP < minscore) | |
8031 { | |
8032 /* If swapping two characters makes a match then the | |
8033 * substitution is more expensive, thus there is no need to | |
8034 * try both. */ | |
8035 if (gc == badword[bi + 1] && bc == goodword[gi + 1]) | |
8036 { | |
8037 /* Swap two characters, that is: skip them. */ | |
8038 gi += 2; | |
8039 bi += 2; | |
8040 score += SCORE_SWAP; | |
8041 continue; | |
8042 } | |
8043 } | |
8044 | |
8045 /* Substitute one character for another which is the same | |
8046 * thing as deleting a character from both goodword and badword. | |
8047 * Use a better score when there is only a case difference. */ | |
8048 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) | |
8049 score += SCORE_ICASE; | |
8050 else | |
8051 { | |
8052 /* For a similar character use SCORE_SIMILAR. */ | |
8053 if (slang != NULL | |
8054 && slang->sl_has_map | |
8055 && similar_chars(slang, gc, bc)) | |
8056 score += SCORE_SIMILAR; | |
8057 else | |
8058 score += SCORE_SUBST; | |
8059 } | |
8060 | |
8061 if (score < minscore) | |
8062 { | |
8063 /* Do the substitution. */ | |
8064 ++gi; | |
8065 ++bi; | |
8066 continue; | |
8067 } | |
8068 } | |
8069 pop: | |
8070 /* | |
8071 * Get here to try the next alternative, pop it from the stack. | |
8072 */ | |
8073 if (stackidx == 0) /* stack is empty, finished */ | |
8074 break; | |
8075 | |
8076 /* pop an item from the stack */ | |
8077 --stackidx; | |
8078 gi = stack[stackidx].goodi; | |
8079 bi = stack[stackidx].badi; | |
8080 score = stack[stackidx].score; | |
8081 } | |
8082 | |
8083 /* When the score goes over "limit" it may actually be much higher. | |
8084 * Return a very large number to avoid going below the limit when giving a | |
8085 * bonus. */ | |
8086 if (minscore > limit) | |
8087 return SCORE_MAXMAX; | |
8088 return minscore; | |
8089 } | |
8090 | |
8091 /* | |
8092 * Multi-byte version of spell_edit_score_limit(). | |
8093 * Keep it in sync with the above! | |
8094 */ | |
8095 static int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8096 spell_edit_score_limit_w( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8097 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8098 char_u *badword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8099 char_u *goodword, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8100 int limit) |
625 | 8101 { |
8102 limitscore_T stack[10]; /* allow for over 3 * 2 edits */ | |
8103 int stackidx; | |
8104 int bi, gi; | |
8105 int bi2, gi2; | |
8106 int bc, gc; | |
8107 int score; | |
8108 int score_off; | |
8109 int minscore; | |
8110 int round; | |
8111 char_u *p; | |
8112 int wbadword[MAXWLEN]; | |
8113 int wgoodword[MAXWLEN]; | |
8114 | |
8115 /* Get the characters from the multi-byte strings and put them in an | |
8116 * int array for easy access. */ | |
8117 bi = 0; | |
8118 for (p = badword; *p != NUL; ) | |
8119 wbadword[bi++] = mb_cptr2char_adv(&p); | |
8120 wbadword[bi++] = 0; | |
8121 gi = 0; | |
8122 for (p = goodword; *p != NUL; ) | |
8123 wgoodword[gi++] = mb_cptr2char_adv(&p); | |
8124 wgoodword[gi++] = 0; | |
8125 | |
8126 /* | |
8127 * The idea is to go from start to end over the words. So long as | |
8128 * characters are equal just continue, this always gives the lowest score. | |
8129 * When there is a difference try several alternatives. Each alternative | |
8130 * increases "score" for the edit distance. Some of the alternatives are | |
8131 * pushed unto a stack and tried later, some are tried right away. At the | |
8132 * end of the word the score for one alternative is known. The lowest | |
8133 * possible score is stored in "minscore". | |
8134 */ | |
8135 stackidx = 0; | |
8136 bi = 0; | |
8137 gi = 0; | |
8138 score = 0; | |
8139 minscore = limit + 1; | |
8140 | |
8141 for (;;) | |
8142 { | |
8143 /* Skip over an equal part, score remains the same. */ | |
8144 for (;;) | |
8145 { | |
8146 bc = wbadword[bi]; | |
8147 gc = wgoodword[gi]; | |
8148 | |
8149 if (bc != gc) /* stop at a char that's different */ | |
8150 break; | |
8151 if (bc == NUL) /* both words end */ | |
8152 { | |
8153 if (score < minscore) | |
8154 minscore = score; | |
8155 goto pop; /* do next alternative */ | |
8156 } | |
8157 ++bi; | |
8158 ++gi; | |
8159 } | |
8160 | |
8161 if (gc == NUL) /* goodword ends, delete badword chars */ | |
8162 { | |
8163 do | |
8164 { | |
8165 if ((score += SCORE_DEL) >= minscore) | |
8166 goto pop; /* do next alternative */ | |
8167 } while (wbadword[++bi] != NUL); | |
8168 minscore = score; | |
8169 } | |
8170 else if (bc == NUL) /* badword ends, insert badword chars */ | |
8171 { | |
8172 do | |
8173 { | |
8174 if ((score += SCORE_INS) >= minscore) | |
8175 goto pop; /* do next alternative */ | |
8176 } while (wgoodword[++gi] != NUL); | |
8177 minscore = score; | |
8178 } | |
8179 else /* both words continue */ | |
8180 { | |
8181 /* If not close to the limit, perform a change. Only try changes | |
8182 * that may lead to a lower score than "minscore". | |
8183 * round 0: try deleting a char from badword | |
8184 * round 1: try inserting a char in badword */ | |
8185 for (round = 0; round <= 1; ++round) | |
8186 { | |
8187 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS); | |
8188 if (score_off < minscore) | |
8189 { | |
8190 if (score_off + SCORE_EDIT_MIN >= minscore) | |
8191 { | |
8192 /* Near the limit, rest of the words must match. We | |
8193 * can check that right now, no need to push an item | |
8194 * onto the stack. */ | |
8195 bi2 = bi + 1 - round; | |
8196 gi2 = gi + round; | |
8197 while (wgoodword[gi2] == wbadword[bi2]) | |
8198 { | |
8199 if (wgoodword[gi2] == NUL) | |
8200 { | |
8201 minscore = score_off; | |
8202 break; | |
8203 } | |
8204 ++bi2; | |
8205 ++gi2; | |
8206 } | |
8207 } | |
8208 else | |
8209 { | |
8210 /* try deleting a character from badword later */ | |
8211 stack[stackidx].badi = bi + 1 - round; | |
8212 stack[stackidx].goodi = gi + round; | |
8213 stack[stackidx].score = score_off; | |
8214 ++stackidx; | |
8215 } | |
8216 } | |
8217 } | |
8218 | |
8219 if (score + SCORE_SWAP < minscore) | |
8220 { | |
8221 /* If swapping two characters makes a match then the | |
8222 * substitution is more expensive, thus there is no need to | |
8223 * try both. */ | |
8224 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1]) | |
8225 { | |
8226 /* Swap two characters, that is: skip them. */ | |
8227 gi += 2; | |
8228 bi += 2; | |
8229 score += SCORE_SWAP; | |
8230 continue; | |
8231 } | |
8232 } | |
8233 | |
8234 /* Substitute one character for another which is the same | |
8235 * thing as deleting a character from both goodword and badword. | |
8236 * Use a better score when there is only a case difference. */ | |
8237 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc)) | |
8238 score += SCORE_ICASE; | |
8239 else | |
8240 { | |
8241 /* For a similar character use SCORE_SIMILAR. */ | |
8242 if (slang != NULL | |
8243 && slang->sl_has_map | |
8244 && similar_chars(slang, gc, bc)) | |
8245 score += SCORE_SIMILAR; | |
8246 else | |
8247 score += SCORE_SUBST; | |
8248 } | |
8249 | |
8250 if (score < minscore) | |
8251 { | |
8252 /* Do the substitution. */ | |
8253 ++gi; | |
8254 ++bi; | |
8255 continue; | |
8256 } | |
8257 } | |
8258 pop: | |
8259 /* | |
8260 * Get here to try the next alternative, pop it from the stack. | |
8261 */ | |
8262 if (stackidx == 0) /* stack is empty, finished */ | |
8263 break; | |
8264 | |
8265 /* pop an item from the stack */ | |
8266 --stackidx; | |
8267 gi = stack[stackidx].goodi; | |
8268 bi = stack[stackidx].badi; | |
8269 score = stack[stackidx].score; | |
8270 } | |
8271 | |
8272 /* When the score goes over "limit" it may actually be much higher. | |
8273 * Return a very large number to avoid going below the limit when giving a | |
8274 * bonus. */ | |
8275 if (minscore > limit) | |
8276 return SCORE_MAXMAX; | |
8277 return minscore; | |
8278 } | |
8279 | |
714 | 8280 /* |
8281 * ":spellinfo" | |
8282 */ | |
8283 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8284 ex_spellinfo(exarg_T *eap UNUSED) |
714 | 8285 { |
8286 int lpi; | |
8287 langp_T *lp; | |
8288 char_u *p; | |
8289 | |
8290 if (no_spell_checking(curwin)) | |
8291 return; | |
8292 | |
8293 msg_start(); | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8294 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
|
8295 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8296 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
|
8297 msg_puts("file: "); |
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8298 msg_puts((char *)lp->lp_slang->sl_fname); |
714 | 8299 msg_putchar('\n'); |
8300 p = lp->lp_slang->sl_info; | |
8301 if (p != NULL) | |
8302 { | |
15543
dd725a8ab112
patch 8.1.0779: argument for message functions is inconsistent
Bram Moolenaar <Bram@vim.org>
parents:
15470
diff
changeset
|
8303 msg_puts((char *)p); |
714 | 8304 msg_putchar('\n'); |
8305 } | |
8306 } | |
8307 msg_end(); | |
8308 } | |
8309 | |
625 | 8310 #define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */ |
8311 #define DUMPFLAG_COUNT 2 /* include word count */ | |
701 | 8312 #define DUMPFLAG_ICASE 4 /* ignore case when finding matches */ |
710 | 8313 #define DUMPFLAG_ONECAP 8 /* pattern starts with capital */ |
8314 #define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */ | |
625 | 8315 |
351 | 8316 /* |
8317 * ":spelldump" | |
8318 */ | |
8319 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8320 ex_spelldump(exarg_T *eap) |
351 | 8321 { |
5382 | 8322 char_u *spl; |
8323 long dummy; | |
8324 | |
701 | 8325 if (no_spell_checking(curwin)) |
8326 return; | |
5382 | 8327 get_option_value((char_u*)"spl", &dummy, &spl, OPT_LOCAL); |
8328 | |
8329 /* Create a new empty buffer in a new window. */ | |
701 | 8330 do_cmdline_cmd((char_u *)"new"); |
5382 | 8331 |
8332 /* enable spelling locally in the new window */ | |
8333 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
|
8334 set_option_value((char_u*)"spl", dummy, spl, OPT_LOCAL); |
5382 | 8335 vim_free(spl); |
8336 | |
11121
778c10516955
patch 8.0.0448: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
10950
diff
changeset
|
8337 if (!BUFEMPTY()) |
701 | 8338 return; |
8339 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8340 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0); |
701 | 8341 |
8342 /* Delete the empty line that we started with. */ | |
8343 if (curbuf->b_ml.ml_line_count > 1) | |
8344 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
8345 | |
8346 redraw_later(NOT_VALID); | |
8347 } | |
8348 | |
8349 /* | |
8350 * Go through all possible words and: | |
8351 * 1. When "pat" is NULL: dump a list of all words in the current buffer. | |
8352 * "ic" and "dir" are not used. | |
8353 * 2. When "pat" is not NULL: add matching words to insert mode completion. | |
8354 */ | |
8355 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8356 spell_dump_compl( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8357 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
|
8358 int ic, /* ignore case */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8359 int *dir, /* direction for adding matches */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8360 int dumpflags_arg) /* DUMPFLAG_* */ |
701 | 8361 { |
351 | 8362 langp_T *lp; |
8363 slang_T *slang; | |
8364 idx_T arridx[MAXWLEN]; | |
8365 int curi[MAXWLEN]; | |
8366 char_u word[MAXWLEN]; | |
8367 int c; | |
8368 char_u *byts; | |
8369 idx_T *idxs; | |
8370 linenr_T lnum = 0; | |
8371 int round; | |
8372 int depth; | |
8373 int n; | |
8374 int flags; | |
381 | 8375 char_u *region_names = NULL; /* region names being used */ |
8376 int do_region = TRUE; /* dump region names and numbers */ | |
8377 char_u *p; | |
500 | 8378 int lpi; |
701 | 8379 int dumpflags = dumpflags_arg; |
8380 int patlen; | |
8381 | |
710 | 8382 /* When ignoring case or when the pattern starts with capital pass this on |
8383 * to dump_word(). */ | |
8384 if (pat != NULL) | |
8385 { | |
8386 if (ic) | |
8387 dumpflags |= DUMPFLAG_ICASE; | |
8388 else | |
8389 { | |
8390 n = captype(pat, NULL); | |
8391 if (n == WF_ONECAP) | |
8392 dumpflags |= DUMPFLAG_ONECAP; | |
15605
62b3805506b3
patch 8.1.0810: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents:
15543
diff
changeset
|
8393 else if (n == WF_ALLCAP && (int)STRLEN(pat) > mb_ptr2len(pat)) |
710 | 8394 dumpflags |= DUMPFLAG_ALLCAP; |
8395 } | |
8396 } | |
351 | 8397 |
381 | 8398 /* Find out if we can support regions: All languages must support the same |
8399 * regions or none at all. */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8400 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
|
8401 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8402 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
381 | 8403 p = lp->lp_slang->sl_regions; |
8404 if (p[0] != 0) | |
8405 { | |
8406 if (region_names == NULL) /* first language with regions */ | |
8407 region_names = p; | |
8408 else if (STRCMP(region_names, p) != 0) | |
8409 { | |
8410 do_region = FALSE; /* region names are different */ | |
8411 break; | |
8412 } | |
8413 } | |
8414 } | |
8415 | |
8416 if (do_region && region_names != NULL) | |
8417 { | |
701 | 8418 if (pat == NULL) |
8419 { | |
8420 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names); | |
8421 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); | |
8422 } | |
381 | 8423 } |
8424 else | |
8425 do_region = FALSE; | |
8426 | |
8427 /* | |
8428 * Loop over all files loaded for the entries in 'spelllang'. | |
8429 */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8430 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
|
8431 { |
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8432 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi); |
351 | 8433 slang = lp->lp_slang; |
500 | 8434 if (slang->sl_fbyts == NULL) /* reloading failed */ |
8435 continue; | |
351 | 8436 |
701 | 8437 if (pat == NULL) |
8438 { | |
8439 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname); | |
8440 ml_append(lnum++, IObuff, (colnr_T)0, FALSE); | |
8441 } | |
8442 | |
8443 /* When matching with a pattern and there are no prefixes only use | |
8444 * parts of the tree that match "pat". */ | |
8445 if (pat != NULL && slang->sl_pbyts == NULL) | |
835 | 8446 patlen = (int)STRLEN(pat); |
701 | 8447 else |
840 | 8448 patlen = -1; |
351 | 8449 |
8450 /* round 1: case-folded tree | |
8451 * round 2: keep-case tree */ | |
8452 for (round = 1; round <= 2; ++round) | |
8453 { | |
8454 if (round == 1) | |
8455 { | |
701 | 8456 dumpflags &= ~DUMPFLAG_KEEPCASE; |
351 | 8457 byts = slang->sl_fbyts; |
8458 idxs = slang->sl_fidxs; | |
8459 } | |
8460 else | |
8461 { | |
701 | 8462 dumpflags |= DUMPFLAG_KEEPCASE; |
351 | 8463 byts = slang->sl_kbyts; |
8464 idxs = slang->sl_kidxs; | |
8465 } | |
8466 if (byts == NULL) | |
8467 continue; /* array is empty */ | |
8468 | |
8469 depth = 0; | |
8470 arridx[0] = 0; | |
8471 curi[0] = 1; | |
701 | 8472 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
|
8473 && (pat == NULL || !ins_compl_interrupted())) |
351 | 8474 { |
8475 if (curi[depth] > byts[arridx[depth]]) | |
8476 { | |
8477 /* Done all bytes at this node, go up one level. */ | |
8478 --depth; | |
8479 line_breakcheck(); | |
10277
154d5a2e7395
commit https://github.com/vim/vim/commit/472e85970ee3a80abd824bef510df12e9cfe9e96
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
8480 ins_compl_check_keys(50, FALSE); |
351 | 8481 } |
8482 else | |
8483 { | |
8484 /* Do one more byte at this node. */ | |
8485 n = arridx[depth] + curi[depth]; | |
8486 ++curi[depth]; | |
8487 c = byts[n]; | |
8488 if (c == 0) | |
8489 { | |
8490 /* End of word, deal with the word. | |
8491 * Don't use keep-case words in the fold-case tree, | |
8492 * they will appear in the keep-case tree. | |
8493 * Only use the word when the region matches. */ | |
8494 flags = (int)idxs[n]; | |
8495 if ((round == 2 || (flags & WF_KEEPCAP) == 0) | |
500 | 8496 && (flags & WF_NEEDCOMP) == 0 |
381 | 8497 && (do_region |
8498 || (flags & WF_REGION) == 0 | |
390 | 8499 || (((unsigned)flags >> 16) |
351 | 8500 & lp->lp_region) != 0)) |
8501 { | |
8502 word[depth] = NUL; | |
381 | 8503 if (!do_region) |
8504 flags &= ~WF_REGION; | |
355 | 8505 |
8506 /* Dump the basic word if there is no prefix or | |
8507 * when it's the first one. */ | |
390 | 8508 c = (unsigned)flags >> 24; |
355 | 8509 if (c == 0 || curi[depth] == 2) |
701 | 8510 { |
8511 dump_word(slang, word, pat, dir, | |
8512 dumpflags, flags, lnum); | |
8513 if (pat == NULL) | |
8514 ++lnum; | |
8515 } | |
351 | 8516 |
8517 /* Apply the prefix, if there is one. */ | |
355 | 8518 if (c != 0) |
701 | 8519 lnum = dump_prefixes(slang, word, pat, dir, |
8520 dumpflags, flags, lnum); | |
351 | 8521 } |
8522 } | |
8523 else | |
8524 { | |
8525 /* Normal char, go one level deeper. */ | |
8526 word[depth++] = c; | |
8527 arridx[depth] = idxs[n]; | |
8528 curi[depth] = 1; | |
701 | 8529 |
8530 /* Check if this characters matches with the pattern. | |
8531 * If not skip the whole tree below it. | |
710 | 8532 * Always ignore case here, dump_word() will check |
8533 * proper case later. This isn't exactly right when | |
8534 * length changes for multi-byte characters with | |
8535 * ignore case... */ | |
8536 if (depth <= patlen | |
8537 && MB_STRNICMP(word, pat, depth) != 0) | |
701 | 8538 --depth; |
351 | 8539 } |
8540 } | |
8541 } | |
8542 } | |
8543 } | |
8544 } | |
8545 | |
8546 /* | |
8547 * Dump one word: apply case modifications and append a line to the buffer. | |
701 | 8548 * When "lnum" is zero add insert mode completion. |
8549 */ | |
8550 static void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8551 dump_word( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8552 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8553 char_u *word, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8554 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8555 int *dir, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8556 int dumpflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8557 int wordflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8558 linenr_T lnum) |
351 | 8559 { |
8560 int keepcap = FALSE; | |
8561 char_u *p; | |
625 | 8562 char_u *tw; |
351 | 8563 char_u cword[MAXWLEN]; |
381 | 8564 char_u badword[MAXWLEN + 10]; |
8565 int i; | |
710 | 8566 int flags = wordflags; |
8567 | |
8568 if (dumpflags & DUMPFLAG_ONECAP) | |
8569 flags |= WF_ONECAP; | |
8570 if (dumpflags & DUMPFLAG_ALLCAP) | |
8571 flags |= WF_ALLCAP; | |
351 | 8572 |
625 | 8573 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0) |
351 | 8574 { |
8575 /* Need to fix case according to "flags". */ | |
8576 make_case_word(word, cword, flags); | |
8577 p = cword; | |
8578 } | |
8579 else | |
8580 { | |
8581 p = word; | |
625 | 8582 if ((dumpflags & DUMPFLAG_KEEPCASE) |
8583 && ((captype(word, NULL) & WF_KEEPCAP) == 0 | |
389 | 8584 || (flags & WF_FIXCAP) != 0)) |
351 | 8585 keepcap = TRUE; |
8586 } | |
625 | 8587 tw = p; |
351 | 8588 |
701 | 8589 if (pat == NULL) |
8590 { | |
8591 /* Add flags and regions after a slash. */ | |
8592 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap) | |
8593 { | |
8594 STRCPY(badword, p); | |
8595 STRCAT(badword, "/"); | |
8596 if (keepcap) | |
8597 STRCAT(badword, "="); | |
8598 if (flags & WF_BANNED) | |
8599 STRCAT(badword, "!"); | |
8600 else if (flags & WF_RARE) | |
8601 STRCAT(badword, "?"); | |
8602 if (flags & WF_REGION) | |
8603 for (i = 0; i < 7; ++i) | |
8604 if (flags & (0x10000 << i)) | |
8605 sprintf((char *)badword + STRLEN(badword), "%d", i + 1); | |
8606 p = badword; | |
8607 } | |
8608 | |
8609 if (dumpflags & DUMPFLAG_COUNT) | |
8610 { | |
8611 hashitem_T *hi; | |
8612 | |
8613 /* Include the word count for ":spelldump!". */ | |
8614 hi = hash_find(&slang->sl_wordcount, tw); | |
8615 if (!HASHITEM_EMPTY(hi)) | |
8616 { | |
8617 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d", | |
625 | 8618 tw, HI2WC(hi)->wc_count); |
701 | 8619 p = IObuff; |
8620 } | |
8621 } | |
8622 | |
8623 ml_append(lnum, p, (colnr_T)0, FALSE); | |
8624 } | |
710 | 8625 else if (((dumpflags & DUMPFLAG_ICASE) |
8626 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0 | |
8627 : STRNCMP(p, pat, STRLEN(pat)) == 0) | |
701 | 8628 && 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
|
8629 p_ic, NULL, *dir, FALSE) == OK) |
710 | 8630 /* if dir was BACKWARD then honor it just once */ |
8631 *dir = FORWARD; | |
351 | 8632 } |
8633 | |
8634 /* | |
372 | 8635 * For ":spelldump": Find matching prefixes for "word". Prepend each to |
8636 * "word" and append a line to the buffer. | |
701 | 8637 * When "lnum" is zero add insert mode completion. |
351 | 8638 * Return the updated line number. |
8639 */ | |
8640 static linenr_T | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8641 dump_prefixes( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8642 slang_T *slang, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8643 char_u *word, /* case-folded word */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8644 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8645 int *dir, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8646 int dumpflags, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8647 int flags, /* flags with prefix ID */ |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8648 linenr_T startlnum) |
351 | 8649 { |
8650 idx_T arridx[MAXWLEN]; | |
8651 int curi[MAXWLEN]; | |
8652 char_u prefix[MAXWLEN]; | |
455 | 8653 char_u word_up[MAXWLEN]; |
8654 int has_word_up = FALSE; | |
351 | 8655 int c; |
8656 char_u *byts; | |
8657 idx_T *idxs; | |
8658 linenr_T lnum = startlnum; | |
8659 int depth; | |
8660 int n; | |
8661 int len; | |
8662 int i; | |
8663 | |
626 | 8664 /* If the word starts with a lower-case letter make the word with an |
455 | 8665 * upper-case letter in word_up[]. */ |
8666 c = PTR2CHAR(word); | |
8667 if (SPELL_TOUPPER(c) != c) | |
8668 { | |
8669 onecap_copy(word, word_up, TRUE); | |
8670 has_word_up = TRUE; | |
8671 } | |
8672 | |
351 | 8673 byts = slang->sl_pbyts; |
8674 idxs = slang->sl_pidxs; | |
8675 if (byts != NULL) /* array not is empty */ | |
8676 { | |
8677 /* | |
8678 * Loop over all prefixes, building them byte-by-byte in prefix[]. | |
390 | 8679 * When at the end of a prefix check that it supports "flags". |
351 | 8680 */ |
8681 depth = 0; | |
8682 arridx[0] = 0; | |
8683 curi[0] = 1; | |
8684 while (depth >= 0 && !got_int) | |
8685 { | |
390 | 8686 n = arridx[depth]; |
8687 len = byts[n]; | |
8688 if (curi[depth] > len) | |
351 | 8689 { |
8690 /* Done all bytes at this node, go up one level. */ | |
8691 --depth; | |
8692 line_breakcheck(); | |
8693 } | |
8694 else | |
8695 { | |
8696 /* Do one more byte at this node. */ | |
390 | 8697 n += curi[depth]; |
351 | 8698 ++curi[depth]; |
8699 c = byts[n]; | |
8700 if (c == 0) | |
8701 { | |
8702 /* End of prefix, find out how many IDs there are. */ | |
8703 for (i = 1; i < len; ++i) | |
8704 if (byts[n + i] != 0) | |
8705 break; | |
8706 curi[depth] += i - 1; | |
8707 | |
455 | 8708 c = valid_word_prefix(i, n, flags, word, slang, FALSE); |
8709 if (c != 0) | |
351 | 8710 { |
376 | 8711 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1); |
701 | 8712 dump_word(slang, prefix, pat, dir, dumpflags, |
455 | 8713 (c & WF_RAREPFX) ? (flags | WF_RARE) |
701 | 8714 : flags, lnum); |
8715 if (lnum != 0) | |
8716 ++lnum; | |
351 | 8717 } |
455 | 8718 |
8719 /* Check for prefix that matches the word when the | |
8720 * first letter is upper-case, but only if the prefix has | |
8721 * a condition. */ | |
8722 if (has_word_up) | |
8723 { | |
8724 c = valid_word_prefix(i, n, flags, word_up, slang, | |
8725 TRUE); | |
8726 if (c != 0) | |
8727 { | |
8728 vim_strncpy(prefix + depth, word_up, | |
8729 MAXWLEN - depth - 1); | |
701 | 8730 dump_word(slang, prefix, pat, dir, dumpflags, |
455 | 8731 (c & WF_RAREPFX) ? (flags | WF_RARE) |
701 | 8732 : flags, lnum); |
8733 if (lnum != 0) | |
8734 ++lnum; | |
455 | 8735 } |
8736 } | |
351 | 8737 } |
8738 else | |
8739 { | |
8740 /* Normal char, go one level deeper. */ | |
8741 prefix[depth++] = c; | |
8742 arridx[depth] = idxs[n]; | |
8743 curi[depth] = 1; | |
8744 } | |
8745 } | |
8746 } | |
8747 } | |
8748 | |
8749 return lnum; | |
8750 } | |
8751 | |
498 | 8752 /* |
626 | 8753 * Move "p" to the end of word "start". |
8754 * Uses the spell-checking word characters. | |
498 | 8755 */ |
8756 char_u * | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8757 spell_to_word_end(char_u *start, win_T *win) |
498 | 8758 { |
8759 char_u *p = start; | |
8760 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8761 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
|
8762 MB_PTR_ADV(p); |
498 | 8763 return p; |
8764 } | |
8765 | |
475 | 8766 /* |
626 | 8767 * For Insert mode completion CTRL-X s: |
8768 * Find start of the word in front of column "startcol". | |
8769 * We don't check if it is badly spelled, with completion we can only change | |
8770 * the word in front of the cursor. | |
475 | 8771 * Returns the column number of the word. |
8772 */ | |
8773 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8774 spell_word_start(int startcol) |
475 | 8775 { |
8776 char_u *line; | |
8777 char_u *p; | |
8778 int col = 0; | |
8779 | |
498 | 8780 if (no_spell_checking(curwin)) |
475 | 8781 return startcol; |
8782 | |
8783 /* Find a word character before "startcol". */ | |
8784 line = ml_get_curline(); | |
8785 for (p = line + startcol; p > line; ) | |
8786 { | |
11127
506f5d8b7d8b
patch 8.0.0451: some macros are in lower case
Christian Brabandt <cb@256bit.org>
parents:
11121
diff
changeset
|
8787 MB_PTR_BACK(line, p); |
5477 | 8788 if (spell_iswordp_nmw(p, curwin)) |
475 | 8789 break; |
8790 } | |
8791 | |
8792 /* Go back to start of the word. */ | |
8793 while (p > line) | |
8794 { | |
835 | 8795 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
|
8796 MB_PTR_BACK(line, p); |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2229
diff
changeset
|
8797 if (!spell_iswordp(p, curwin)) |
475 | 8798 break; |
8799 col = 0; | |
8800 } | |
8801 | |
535 | 8802 return col; |
8803 } | |
8804 | |
8805 /* | |
8806 * Need to check for 'spellcapcheck' now, the word is removed before | |
8807 * expand_spelling() is called. Therefore the ugly global variable. | |
8808 */ | |
8809 static int spell_expand_need_cap; | |
8810 | |
8811 void | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8812 spell_expand_check_cap(colnr_T col) |
535 | 8813 { |
475 | 8814 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col); |
8815 } | |
8816 | |
8817 /* | |
8818 * Get list of spelling suggestions. | |
8819 * Used for Insert mode completion CTRL-X ?. | |
8820 * Returns the number of matches. The matches are in "matchp[]", array of | |
8821 * allocated strings. | |
8822 */ | |
8823 int | |
7835
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8824 expand_spelling( |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8825 linenr_T lnum UNUSED, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8826 char_u *pat, |
4d7ce6c03fda
commit https://github.com/vim/vim/commit/764b23c8fd3369cb05ae9122abf3ca16fec539d7
Christian Brabandt <cb@256bit.org>
parents:
7805
diff
changeset
|
8827 char_u ***matchp) |
475 | 8828 { |
8829 garray_T ga; | |
8830 | |
625 | 8831 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE); |
475 | 8832 *matchp = ga.ga_data; |
8833 return ga.ga_len; | |
8834 } | |
8835 | |
17940
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8836 /* |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8837 * Return TRUE if "val" is a valid 'spellang' value. |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8838 */ |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8839 int |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8840 valid_spellang(char_u *val) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8841 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8842 return valid_name(val, ".-_,@"); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8843 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8844 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8845 /* |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8846 * Return TRUE if "val" is a valid 'spellfile' value. |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8847 */ |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8848 int |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8849 valid_spellfile(char_u *val) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8850 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8851 char_u *s; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8852 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8853 for (s = val; *s != NUL; ++s) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8854 if (!vim_isfilec(*s) && *s != ',') |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8855 return FALSE; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8856 return TRUE; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8857 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8858 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8859 /* |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8860 * Handle side effects of setting 'spell'. |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8861 * Return an error message or NULL for success. |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8862 */ |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8863 char * |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8864 did_set_spell_option(int is_spellfile) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8865 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8866 char *errmsg = NULL; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8867 win_T *wp; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8868 int l; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8869 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8870 if (is_spellfile) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8871 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8872 l = (int)STRLEN(curwin->w_s->b_p_spf); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8873 if (l > 0 && (l < 4 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8874 || STRCMP(curwin->w_s->b_p_spf + l - 4, ".add") != 0)) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8875 errmsg = e_invarg; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8876 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8877 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8878 if (errmsg == NULL) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8879 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8880 FOR_ALL_WINDOWS(wp) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8881 if (wp->w_buffer == curbuf && wp->w_p_spell) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8882 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8883 errmsg = did_set_spelllang(wp); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8884 break; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8885 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8886 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8887 return errmsg; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8888 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8889 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8890 /* |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8891 * Set curbuf->b_cap_prog to the regexp program for 'spellcapcheck'. |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8892 * Return error message when failed, NULL when OK. |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8893 */ |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8894 char * |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8895 compile_cap_prog(synblock_T *synblock) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8896 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8897 regprog_T *rp = synblock->b_cap_prog; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8898 char_u *re; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8899 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8900 if (*synblock->b_p_spc == NUL) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8901 synblock->b_cap_prog = NULL; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8902 else |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8903 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8904 // Prepend a ^ so that we only match at one column |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8905 re = concat_str((char_u *)"^", synblock->b_p_spc); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8906 if (re != NULL) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8907 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8908 synblock->b_cap_prog = vim_regcomp(re, RE_MAGIC); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8909 vim_free(re); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8910 if (synblock->b_cap_prog == NULL) |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8911 { |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8912 synblock->b_cap_prog = rp; // restore the previous program |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8913 return e_invarg; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8914 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8915 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8916 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8917 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8918 vim_regfree(rp); |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8919 return NULL; |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8920 } |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8921 |
079e10a49ea1
patch 8.1.1966: some code in options.c fits better elsewhere
Bram Moolenaar <Bram@vim.org>
parents:
17809
diff
changeset
|
8922 #endif // FEAT_SPELL |