223
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
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 *
|
300
|
13 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
|
|
14 * has a list of bytes that can appear (siblings). For each byte there is a
|
|
15 * pointer to the node with the byte that follows in the word (child).
|
324
|
16 *
|
|
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
|
|
18 * binary searching can be used and the NUL bytes are at the start. The
|
|
19 * number of possible bytes is stored before the list of bytes.
|
|
20 *
|
|
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
|
|
22 * either the next index or flags. The tree starts at index 0. For example,
|
|
23 * to lookup "vi" this sequence is followed:
|
|
24 * i = 0
|
|
25 * len = byts[i]
|
|
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
|
|
27 * i = idxs[n]
|
|
28 * len = byts[i]
|
|
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
|
|
30 * i = idxs[n]
|
|
31 * len = byts[i]
|
|
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
|
300
|
33 *
|
339
|
34 * There are two word trees: one with case-folded words and one with words in
|
300
|
35 * original case. The second one is only used for keep-case words and is
|
|
36 * usually small.
|
|
37 *
|
339
|
38 * There is one additional tree for when prefixes are not applied when
|
|
39 * generating the .spl file. This tree stores all the possible prefixes, as
|
|
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
|
|
41 * following word must support this prefix nr. And the condition nr is
|
|
42 * stored, used to lookup the condition that the word must match with.
|
|
43 *
|
300
|
44 * Thanks to Olaf Seibert for providing an example implementation of this tree
|
|
45 * and the compression mechanism.
|
243
|
46 *
|
|
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
|
|
48 *
|
236
|
49 * Why doesn't Vim use aspell/ispell/myspell/etc.?
|
|
50 * See ":help develop-spell".
|
|
51 */
|
|
52
|
300
|
53 /*
|
324
|
54 * Use this to adjust the score after finding suggestions, based on the
|
|
55 * suggested word sounding like the bad word. This is much faster than doing
|
|
56 * it for every possible suggestion.
|
|
57 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
|
|
58 * down in the list.
|
344
|
59 * Used when 'spellsuggest' is set to "best".
|
|
60 */
|
|
61 #define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
|
|
62
|
|
63 /*
|
|
64 * The double scoring mechanism is based on the principle that there are two
|
|
65 * kinds of spelling mistakes:
|
|
66 * 1. You know how to spell the word, but mistype something. This results in
|
|
67 * a small editing distance (character swapped/omitted/inserted) and
|
|
68 * possibly a word that sounds completely different.
|
|
69 * 2. You don't know how to spell the word and type something that sounds
|
|
70 * right. The edit distance can be big but the word is similar after
|
|
71 * sound-folding.
|
|
72 * Since scores for these two mistakes will be very different we use a list
|
|
73 * for each.
|
|
74 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
|
|
75 * "double".
|
323
|
76 */
|
|
77
|
|
78 /*
|
339
|
79 * Vim spell file format: <HEADER>
|
|
80 * <SUGGEST>
|
|
81 * <LWORDTREE>
|
|
82 * <KWORDTREE>
|
|
83 * <PREFIXTREE>
|
300
|
84 *
|
339
|
85 * <HEADER>: <fileID>
|
|
86 * <regioncnt> <regionname> ...
|
|
87 * <charflagslen> <charflags>
|
|
88 * <fcharslen> <fchars>
|
|
89 * <prefcondcnt> <prefcond> ...
|
300
|
90 *
|
339
|
91 * <fileID> 10 bytes "VIMspell07"
|
300
|
92 * <regioncnt> 1 byte number of regions following (8 supported)
|
307
|
93 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
|
300
|
94 * First <regionname> is region 1.
|
|
95 *
|
|
96 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
|
|
97 * <charflags> N bytes List of flags (first one is for character 128):
|
324
|
98 * 0x01 word character CF_WORD
|
|
99 * 0x02 upper-case character CF_UPPER
|
300
|
100 * <fcharslen> 2 bytes Number of bytes in <fchars>.
|
|
101 * <fchars> N bytes Folded characters, first one is for character 128.
|
|
102 *
|
339
|
103 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
|
|
104 *
|
|
105 * <prefcond> : <condlen> <condstr>
|
|
106 *
|
|
107 * <condlen> 1 byte Length of <condstr>.
|
|
108 *
|
|
109 * <condstr> N bytes Condition for the prefix.
|
|
110 *
|
300
|
111 *
|
323
|
112 * <SUGGEST> : <repcount> <rep> ...
|
|
113 * <salflags> <salcount> <sal> ...
|
|
114 * <maplen> <mapstr>
|
|
115 *
|
|
116 * <repcount> 2 bytes number of <rep> items, MSB first.
|
|
117 *
|
|
118 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
|
|
119 *
|
|
120 * <repfromlen> 1 byte length of <repfrom>
|
|
121 *
|
|
122 * <repfrom> N bytes "from" part of replacement
|
|
123 *
|
|
124 * <reptolen> 1 byte length of <repto>
|
|
125 *
|
|
126 * <repto> N bytes "to" part of replacement
|
300
|
127 *
|
323
|
128 * <salflags> 1 byte flags for soundsalike conversion:
|
|
129 * SAL_F0LLOWUP
|
|
130 * SAL_COLLAPSE
|
|
131 * SAL_REM_ACCENTS
|
|
132 *
|
|
133 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
|
|
134 *
|
|
135 * <salfromlen> 1 byte length of <salfrom>
|
|
136 *
|
|
137 * <salfrom> N bytes "from" part of soundsalike
|
|
138 *
|
|
139 * <saltolen> 1 byte length of <salto>
|
|
140 *
|
|
141 * <salto> N bytes "to" part of soundsalike
|
|
142 *
|
|
143 * <maplen> 2 bytes length of <mapstr>, MSB first
|
|
144 *
|
|
145 * <mapstr> N bytes String with sequences of similar characters,
|
|
146 * separated by slashes.
|
300
|
147 *
|
|
148 *
|
|
149 * <LWORDTREE>: <wordtree>
|
|
150 *
|
339
|
151 * <KWORDTREE>: <wordtree>
|
|
152 *
|
|
153 * <PREFIXTREE>: <wordtree>
|
|
154 *
|
|
155 *
|
300
|
156 * <wordtree>: <nodecount> <nodedata> ...
|
|
157 *
|
|
158 * <nodecount> 4 bytes Number of nodes following. MSB first.
|
|
159 *
|
|
160 * <nodedata>: <siblingcount> <sibling> ...
|
|
161 *
|
|
162 * <siblingcount> 1 byte Number of siblings in this node. The siblings
|
|
163 * follow in sorted order.
|
|
164 *
|
339
|
165 * <sibling>: <byte> [ <nodeidx> <xbyte>
|
|
166 * | <flags> [<region>] [<prefixID>]
|
|
167 * | <prefixID> <prefcondnr> ]
|
300
|
168 *
|
|
169 * <byte> 1 byte Byte value of the sibling. Special cases:
|
|
170 * BY_NOFLAGS: End of word without flags and for all
|
|
171 * regions.
|
339
|
172 * BY_FLAGS: End of word, <flags> follow. For
|
|
173 * PREFIXTREE <prefixID> and <prefcondnr>
|
|
174 * follow.
|
300
|
175 * BY_INDEX: Child of sibling is shared, <nodeidx>
|
|
176 * and <xbyte> follow.
|
|
177 *
|
|
178 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
|
|
179 *
|
|
180 * <xbyte> 1 byte byte value of the sibling.
|
|
181 *
|
|
182 * <flags> 1 byte bitmask of:
|
|
183 * WF_ALLCAP word must have only capitals
|
|
184 * WF_ONECAP first char of word must be capital
|
|
185 * WF_RARE rare word
|
|
186 * WF_REGION <region> follows
|
339
|
187 * WF_PFX <prefixID> follows
|
300
|
188 *
|
|
189 * <region> 1 byte Bitmask for regions in which word is valid. When
|
|
190 * omitted it's valid in all regions.
|
|
191 * Lowest bit is for region 1.
|
|
192 *
|
339
|
193 * <prefixID> 1 byte ID of prefix that can be used with this word. For
|
|
194 * PREFIXTREE used for the required prefix ID.
|
|
195 *
|
|
196 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
|
|
197 * from HEADER.
|
300
|
198 *
|
|
199 * All text characters are in 'encoding', but stored as single bytes.
|
|
200 */
|
|
201
|
223
|
202 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
|
|
203 # include <io.h> /* for lseek(), must be before vim.h */
|
|
204 #endif
|
|
205
|
|
206 #include "vim.h"
|
|
207
|
|
208 #if defined(FEAT_SYN_HL) || defined(PROTO)
|
|
209
|
|
210 #ifdef HAVE_FCNTL_H
|
|
211 # include <fcntl.h>
|
|
212 #endif
|
|
213
|
323
|
214 #define MAXWLEN 250 /* Assume max. word len is this many bytes.
|
|
215 Some places assume a word length fits in a
|
|
216 byte, thus it can't be above 255. */
|
226
|
217
|
324
|
218 /* Type used for indexes in the word tree need to be at least 3 bytes. If int
|
|
219 * is 8 bytes we could use something smaller, but what? */
|
|
220 #if SIZEOF_INT > 2
|
|
221 typedef int idx_T;
|
|
222 #else
|
|
223 typedef long idx_T;
|
|
224 #endif
|
|
225
|
|
226 /* Flags used for a word. Only the lowest byte can be used, the region byte
|
|
227 * comes above it. */
|
300
|
228 #define WF_REGION 0x01 /* region byte follows */
|
|
229 #define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
|
|
230 #define WF_ALLCAP 0x04 /* word must be all capitals */
|
|
231 #define WF_RARE 0x08 /* rare word */
|
307
|
232 #define WF_BANNED 0x10 /* bad word */
|
339
|
233 #define WF_PFX 0x20 /* prefix ID list follows */
|
323
|
234 #define WF_KEEPCAP 0x80 /* keep-case word */
|
|
235
|
|
236 #define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
|
300
|
237
|
|
238 #define BY_NOFLAGS 0 /* end of word without flags or region */
|
|
239 #define BY_FLAGS 1 /* end of word, flag byte follows */
|
|
240 #define BY_INDEX 2 /* child is shared, index follows */
|
|
241 #define BY_SPECIAL BY_INDEX /* hightest special byte value */
|
236
|
242
|
323
|
243 /* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
|
344
|
244 * and si_sal. Not for sl_sal!
|
323
|
245 * One replacement: from "ft_from" to "ft_to". */
|
|
246 typedef struct fromto_S
|
236
|
247 {
|
323
|
248 char_u *ft_from;
|
|
249 char_u *ft_to;
|
|
250 } fromto_T;
|
236
|
251
|
344
|
252 /* Info from "SAL" entries in ".aff" file used in sl_sal.
|
|
253 * The info is split for quick processing by spell_soundfold().
|
|
254 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
|
|
255 typedef struct salitem_S
|
|
256 {
|
|
257 char_u *sm_lead; /* leading letters */
|
|
258 int sm_leadlen; /* length of "sm_lead" */
|
|
259 char_u *sm_oneoff; /* letters from () or NULL */
|
|
260 char_u *sm_rules; /* rules like ^, $, priority */
|
|
261 char_u *sm_to; /* replacement. */
|
|
262 } salitem_T;
|
|
263
|
236
|
264 /*
|
243
|
265 * Structure used to store words and other info for one language, loaded from
|
|
266 * a .spl file.
|
300
|
267 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
|
|
268 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
|
|
269 *
|
|
270 * The "byts" array stores the possible bytes in each tree node, preceded by
|
|
271 * the number of possible bytes, sorted on byte value:
|
|
272 * <len> <byte1> <byte2> ...
|
|
273 * The "idxs" array stores the index of the child node corresponding to the
|
|
274 * byte in "byts".
|
|
275 * Exception: when the byte is zero, the word may end here and "idxs" holds
|
|
276 * the flags and region for the word. There may be several zeros in sequence
|
|
277 * for alternative flag/region combinations.
|
236
|
278 */
|
|
279 typedef struct slang_S slang_T;
|
|
280 struct slang_S
|
|
281 {
|
|
282 slang_T *sl_next; /* next language */
|
|
283 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
|
310
|
284 char_u *sl_fname; /* name of .spl file */
|
323
|
285 int sl_add; /* TRUE if it's a .add file. */
|
339
|
286
|
300
|
287 char_u *sl_fbyts; /* case-folded word bytes */
|
324
|
288 idx_T *sl_fidxs; /* case-folded word indexes */
|
300
|
289 char_u *sl_kbyts; /* keep-case word bytes */
|
324
|
290 idx_T *sl_kidxs; /* keep-case word indexes */
|
339
|
291 char_u *sl_pbyts; /* prefix tree word bytes */
|
|
292 idx_T *sl_pidxs; /* prefix tree word indexes */
|
|
293
|
236
|
294 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
|
323
|
295
|
339
|
296 int sl_prefixcnt; /* number of items in "sl_prefprog" */
|
|
297 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
|
|
298
|
323
|
299 garray_T sl_rep; /* list of fromto_T entries from REP lines */
|
|
300 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
|
|
301 there is none */
|
344
|
302 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
|
323
|
303 short sl_sal_first[256]; /* indexes where byte first appears, -1 if
|
|
304 there is none */
|
|
305 int sl_followup; /* SAL followup */
|
|
306 int sl_collapse; /* SAL collapse_result */
|
|
307 int sl_rem_accents; /* SAL remove_accents */
|
330
|
308 int sl_has_map; /* TRUE if there is a MAP line */
|
|
309 #ifdef FEAT_MBYTE
|
|
310 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
|
|
311 int sl_map_array[256]; /* MAP for first 256 chars */
|
|
312 #else
|
|
313 char_u sl_map_array[256]; /* MAP for first 256 chars */
|
|
314 #endif
|
236
|
315 };
|
|
316
|
243
|
317 /* First language that is loaded, start of the linked list of loaded
|
|
318 * languages. */
|
236
|
319 static slang_T *first_lang = NULL;
|
|
320
|
323
|
321 /* Flags used in .spl file for soundsalike flags. */
|
|
322 #define SAL_F0LLOWUP 1
|
|
323 #define SAL_COLLAPSE 2
|
|
324 #define SAL_REM_ACCENTS 4
|
|
325
|
236
|
326 /*
|
|
327 * Structure used in "b_langp", filled from 'spelllang'.
|
|
328 */
|
|
329 typedef struct langp_S
|
|
330 {
|
|
331 slang_T *lp_slang; /* info for this language (NULL for last one) */
|
|
332 int lp_region; /* bitmask for region or REGION_ALL */
|
|
333 } langp_T;
|
|
334
|
|
335 #define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
|
|
336
|
307
|
337 #define REGION_ALL 0xff /* word valid in all regions */
|
|
338
|
|
339 /* Result values. Lower number is accepted over higher one. */
|
|
340 #define SP_BANNED -1
|
236
|
341 #define SP_OK 0
|
307
|
342 #define SP_RARE 1
|
|
343 #define SP_LOCAL 2
|
|
344 #define SP_BAD 3
|
236
|
345
|
339
|
346 #define VIMSPELLMAGIC "VIMspell07" /* string at start of Vim spell file */
|
236
|
347 #define VIMSPELLMAGICL 10
|
|
348
|
|
349 /*
|
323
|
350 * Information used when looking for suggestions.
|
|
351 */
|
|
352 typedef struct suginfo_S
|
|
353 {
|
|
354 garray_T su_ga; /* suggestions, contains "suggest_T" */
|
344
|
355 int su_maxcount; /* max. number of suggestions displayed */
|
323
|
356 int su_maxscore; /* maximum score for adding to su_ga */
|
344
|
357 garray_T su_sga; /* like su_ga, sound-folded scoring */
|
323
|
358 char_u *su_badptr; /* start of bad word in line */
|
|
359 int su_badlen; /* length of detected bad word in line */
|
346
|
360 int su_badflags; /* caps flags for bad word */
|
323
|
361 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
|
|
362 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
|
|
363 hashtab_T su_banned; /* table with banned words */
|
|
364 } suginfo_T;
|
|
365
|
|
366 /* One word suggestion. Used in "si_ga". */
|
|
367 typedef struct suggest_S
|
|
368 {
|
|
369 char_u *st_word; /* suggested word, allocated string */
|
|
370 int st_orglen; /* length of replaced text */
|
|
371 int st_score; /* lower is better */
|
344
|
372 int st_altscore; /* used when st_score compares equal */
|
|
373 int st_salscore; /* st_score is for soundalike */
|
324
|
374 int st_had_bonus; /* bonus already included in score */
|
323
|
375 } suggest_T;
|
|
376
|
344
|
377 #define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
|
323
|
378
|
324
|
379 /* Number of suggestions kept when cleaning up. When rescore_suggestions() is
|
|
380 * called the score may change, thus we need to keep more than what is
|
|
381 * displayed. */
|
344
|
382 #define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 25 ? 25 : (su)->su_maxcount)
|
324
|
383
|
|
384 /* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
|
|
385 * of suggestions that are not going to be displayed. */
|
344
|
386 #define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
|
323
|
387
|
|
388 /* score for various changes */
|
344
|
389 #define SCORE_SPLIT 149 /* split bad word */
|
323
|
390 #define SCORE_ICASE 52 /* slightly different case */
|
|
391 #define SCORE_REGION 70 /* word is for different region */
|
|
392 #define SCORE_RARE 180 /* rare word */
|
|
393 #define SCORE_SWAP 90 /* swap two characters */
|
|
394 #define SCORE_SWAP3 110 /* swap two characters in three */
|
|
395 #define SCORE_REP 87 /* REP replacement */
|
|
396 #define SCORE_SUBST 93 /* substitute a character */
|
|
397 #define SCORE_SIMILAR 33 /* substitute a similar character */
|
324
|
398 #define SCORE_DEL 94 /* delete a character */
|
|
399 #define SCORE_INS 96 /* insert a character */
|
323
|
400
|
|
401 #define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
|
|
402 * 350 allows for about three changes. */
|
344
|
403
|
|
404 #define SCORE_BIG SCORE_INS * 3 /* big difference */
|
323
|
405 #define SCORE_MAXMAX 999999 /* accept any score */
|
|
406
|
|
407 /*
|
236
|
408 * Structure to store info for word matching.
|
|
409 */
|
|
410 typedef struct matchinf_S
|
|
411 {
|
|
412 langp_T *mi_lp; /* info for language and region */
|
243
|
413
|
|
414 /* pointers to original text to be checked */
|
236
|
415 char_u *mi_word; /* start of word being checked */
|
339
|
416 char_u *mi_end; /* end of matching word so far */
|
243
|
417 char_u *mi_fend; /* next char to be added to mi_fword */
|
300
|
418 char_u *mi_cend; /* char after what was used for
|
|
419 mi_capflags */
|
243
|
420
|
|
421 /* case-folded text */
|
|
422 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
|
300
|
423 int mi_fwordlen; /* nr of valid bytes in mi_fword */
|
243
|
424
|
339
|
425 /* for when checking word after a prefix */
|
|
426 int mi_prefarridx; /* index in sl_pidxs with list of
|
|
427 prefixID/condition */
|
|
428 int mi_prefcnt; /* number of entries at mi_prefarridx */
|
|
429 int mi_prefixlen; /* byte length of prefix */
|
|
430
|
243
|
431 /* others */
|
236
|
432 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
|
300
|
433 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
|
236
|
434 } matchinf_T;
|
|
435
|
307
|
436 /*
|
|
437 * The tables used for recognizing word characters according to spelling.
|
|
438 * These are only used for the first 256 characters of 'encoding'.
|
|
439 */
|
|
440 typedef struct spelltab_S
|
|
441 {
|
|
442 char_u st_isw[256]; /* flags: is word char */
|
|
443 char_u st_isu[256]; /* flags: is uppercase char */
|
|
444 char_u st_fold[256]; /* chars: folded case */
|
324
|
445 char_u st_upper[256]; /* chars: upper case */
|
307
|
446 } spelltab_T;
|
|
447
|
|
448 static spelltab_T spelltab;
|
|
449 static int did_set_spelltab;
|
|
450
|
324
|
451 #define CF_WORD 0x01
|
|
452 #define CF_UPPER 0x02
|
307
|
453
|
|
454 static void clear_spell_chartab __ARGS((spelltab_T *sp));
|
|
455 static int set_spell_finish __ARGS((spelltab_T *new_st));
|
339
|
456 static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
|
307
|
457
|
|
458 /*
|
|
459 * Return TRUE if "p" points to a word character or "c" is a word character
|
|
460 * for spelling.
|
|
461 * Checking for a word character is done very often, avoid the function call
|
|
462 * overhead.
|
|
463 */
|
|
464 #ifdef FEAT_MBYTE
|
|
465 # define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
|
|
466 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
|
|
467 #else
|
|
468 # define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
|
|
469 #endif
|
|
470
|
323
|
471 /*
|
344
|
472 * For finding suggestions: At each node in the tree these states are tried:
|
330
|
473 */
|
|
474 typedef enum
|
|
475 {
|
344
|
476 STATE_START = 0, /* At start of node check for NUL bytes (goodword
|
|
477 * ends); if badword ends there is a match, otherwise
|
|
478 * try splitting word. */
|
|
479 STATE_SPLITUNDO, /* Undo splitting. */
|
330
|
480 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
|
|
481 STATE_PLAIN, /* Use each byte of the node. */
|
|
482 STATE_DEL, /* Delete a byte from the bad word. */
|
|
483 STATE_INS, /* Insert a byte in the bad word. */
|
|
484 STATE_SWAP, /* Swap two bytes. */
|
344
|
485 STATE_UNSWAP, /* Undo swap two characters. */
|
|
486 STATE_SWAP3, /* Swap two characters over three. */
|
|
487 STATE_UNSWAP3, /* Undo Swap two characters over three. */
|
|
488 STATE_UNROT3L, /* Undo rotate three characters left */
|
|
489 STATE_UNROT3R, /* Undo rotate three characters right */
|
330
|
490 STATE_REP_INI, /* Prepare for using REP items. */
|
|
491 STATE_REP, /* Use matching REP items from the .aff file. */
|
|
492 STATE_REP_UNDO, /* Undo a REP item replacement. */
|
|
493 STATE_FINAL /* End of this node. */
|
|
494 } state_T;
|
|
495
|
|
496 /*
|
346
|
497 * Struct to keep the state at each level in suggest_try_change().
|
323
|
498 */
|
|
499 typedef struct trystate_S
|
|
500 {
|
330
|
501 state_T ts_state; /* state at this level, STATE_ */
|
323
|
502 int ts_score; /* score */
|
344
|
503 idx_T ts_arridx; /* index in tree array, start of node */
|
330
|
504 short ts_curi; /* index in list of child nodes */
|
|
505 char_u ts_fidx; /* index in fword[], case-folded bad word */
|
|
506 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
|
|
507 char_u ts_twordlen; /* valid length of tword[] */
|
|
508 #ifdef FEAT_MBYTE
|
|
509 char_u ts_tcharlen; /* number of bytes in tword character */
|
|
510 char_u ts_tcharidx; /* current byte index in tword character */
|
|
511 char_u ts_isdiff; /* DIFF_ values */
|
|
512 char_u ts_fcharstart; /* index in fword where badword char started */
|
|
513 #endif
|
323
|
514 char_u ts_save_prewordlen; /* saved "prewordlen" */
|
330
|
515 char_u ts_save_splitoff; /* su_splitoff saved here */
|
346
|
516 char_u ts_save_badflags; /* su_badflags saved here */
|
323
|
517 } trystate_T;
|
|
518
|
330
|
519 /* values for ts_isdiff */
|
|
520 #define DIFF_NONE 0 /* no different byte (yet) */
|
|
521 #define DIFF_YES 1 /* different byte found */
|
|
522 #define DIFF_INSERT 2 /* inserting character */
|
|
523
|
339
|
524 /* mode values for find_word */
|
|
525 #define FIND_FOLDWORD 0 /* find word case-folded */
|
|
526 #define FIND_KEEPWORD 1 /* find keep-case word */
|
|
527 #define FIND_PREFIX 2 /* find word after prefix */
|
|
528
|
236
|
529 static slang_T *slang_alloc __ARGS((char_u *lang));
|
|
530 static void slang_free __ARGS((slang_T *lp));
|
310
|
531 static void slang_clear __ARGS((slang_T *lp));
|
339
|
532 static void find_word __ARGS((matchinf_T *mip, int mode));
|
|
533 static void find_prefix __ARGS((matchinf_T *mip));
|
|
534 static int fold_more __ARGS((matchinf_T *mip));
|
323
|
535 static int spell_valid_case __ARGS((int origflags, int treeflags));
|
307
|
536 static void spell_load_lang __ARGS((char_u *lang));
|
310
|
537 static char_u *spell_enc __ARGS((void));
|
|
538 static void spell_load_cb __ARGS((char_u *fname, void *cookie));
|
323
|
539 static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
|
339
|
540 static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
|
236
|
541 static int find_region __ARGS((char_u *rp, char_u *region));
|
|
542 static int captype __ARGS((char_u *word, char_u *end));
|
323
|
543 static void spell_reload_one __ARGS((char_u *fname, int added_word));
|
307
|
544 static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
|
|
545 static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
|
|
546 static void write_spell_chartab __ARGS((FILE *fd));
|
|
547 static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
|
344
|
548 static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount));
|
|
549 static void spell_find_cleanup __ARGS((suginfo_T *su));
|
324
|
550 static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
|
344
|
551 static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
|
346
|
552 static void suggest_try_special __ARGS((suginfo_T *su));
|
|
553 static void suggest_try_change __ARGS((suginfo_T *su));
|
323
|
554 static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
|
|
555 static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
|
344
|
556 static void score_comp_sal __ARGS((suginfo_T *su));
|
|
557 static void score_combine __ARGS((suginfo_T *su));
|
346
|
558 static void suggest_try_soundalike __ARGS((suginfo_T *su));
|
323
|
559 static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
|
330
|
560 static void set_map_str __ARGS((slang_T *lp, char_u *map));
|
323
|
561 static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
|
346
|
562 static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int use_score, int had_bonus));
|
323
|
563 static void add_banned __ARGS((suginfo_T *su, char_u *word));
|
|
564 static int was_banned __ARGS((suginfo_T *su, char_u *word));
|
|
565 static void free_banned __ARGS((suginfo_T *su));
|
324
|
566 static void rescore_suggestions __ARGS((suginfo_T *su));
|
344
|
567 static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
|
323
|
568 static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
|
324
|
569 static int spell_sound_score __ARGS((slang_T *slang, char_u *goodword, char_u *badsound));
|
344
|
570 static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
|
323
|
571 static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
|
|
572
|
324
|
573 /*
|
|
574 * Use our own character-case definitions, because the current locale may
|
|
575 * differ from what the .spl file uses.
|
|
576 * These must not be called with negative number!
|
|
577 */
|
|
578 #ifndef FEAT_MBYTE
|
|
579 /* Non-multi-byte implementation. */
|
|
580 # define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
|
|
581 # define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
|
|
582 # define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
|
|
583 #else
|
|
584 /* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
|
|
585 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
|
|
586 * the "w" library function for characters above 255 if available. */
|
|
587 # ifdef HAVE_TOWLOWER
|
|
588 # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
|
|
589 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
|
|
590 # else
|
|
591 # define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
|
|
592 : (c) < 256 ? spelltab.st_fold[c] : (c))
|
|
593 # endif
|
|
594
|
|
595 # ifdef HAVE_TOWUPPER
|
|
596 # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
|
|
597 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
|
|
598 # else
|
|
599 # define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
|
|
600 : (c) < 256 ? spelltab.st_upper[c] : (c))
|
|
601 # endif
|
|
602
|
|
603 # ifdef HAVE_ISWUPPER
|
|
604 # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
|
|
605 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
|
|
606 # else
|
|
607 # define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
|
|
608 : (c) < 256 ? spelltab.st_isu[c] : (c))
|
|
609 # endif
|
|
610 #endif
|
|
611
|
307
|
612
|
|
613 static char *e_format = N_("E759: Format error in spell file");
|
236
|
614
|
|
615 /*
|
|
616 * Main spell-checking function.
|
300
|
617 * "ptr" points to a character that could be the start of a word.
|
236
|
618 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
|
|
619 * or when it's OK it remains unchanged.
|
|
620 * This must only be called when 'spelllang' is not empty.
|
323
|
621 *
|
|
622 * "sug" is normally NULL. When looking for suggestions it points to
|
|
623 * suginfo_T. It's passed as a void pointer to keep the struct local.
|
|
624 *
|
236
|
625 * Returns the length of the word in bytes, also when it's OK, so that the
|
|
626 * caller can skip over the word.
|
|
627 */
|
|
628 int
|
300
|
629 spell_check(wp, ptr, attrp)
|
236
|
630 win_T *wp; /* current window */
|
|
631 char_u *ptr;
|
|
632 int *attrp;
|
|
633 {
|
|
634 matchinf_T mi; /* Most things are put in "mi" so that it can
|
|
635 be passed to functions quickly. */
|
344
|
636 int nrlen = 0; /* found a number first */
|
236
|
637
|
307
|
638 /* A word never starts at a space or a control character. Return quickly
|
|
639 * then, skipping over the character. */
|
|
640 if (*ptr <= ' ')
|
|
641 return 1;
|
236
|
642
|
344
|
643 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
|
346
|
644 * 0X99FF. But when a word character follows do check spelling to find
|
|
645 * "3GPP". */
|
300
|
646 if (*ptr >= '0' && *ptr <= '9')
|
|
647 {
|
316
|
648 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
|
|
649 mi.mi_end = skiphex(ptr + 2);
|
300
|
650 else
|
|
651 {
|
344
|
652 mi.mi_end = skipdigits(ptr);
|
|
653 nrlen = mi.mi_end - ptr;
|
300
|
654 }
|
344
|
655 if (!SPELL_ISWORDP(mi.mi_end))
|
|
656 return (int)(mi.mi_end - ptr);
|
346
|
657
|
|
658 /* Try including the digits in the word. */
|
|
659 mi.mi_fend = ptr + nrlen;
|
|
660 }
|
|
661 else
|
|
662 mi.mi_fend = ptr;
|
|
663
|
|
664 /* Find the normal end of the word (until the next non-word character). */
|
344
|
665 mi.mi_word = ptr;
|
|
666 if (SPELL_ISWORDP(mi.mi_fend))
|
|
667 {
|
|
668 do
|
|
669 {
|
307
|
670 mb_ptr_adv(mi.mi_fend);
|
344
|
671 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
|
|
672 }
|
|
673
|
|
674 /* We always use the characters up to the next non-word character,
|
|
675 * also for bad words. */
|
|
676 mi.mi_end = mi.mi_fend;
|
|
677
|
|
678 /* Check caps type later. */
|
|
679 mi.mi_capflags = 0;
|
|
680 mi.mi_cend = NULL;
|
|
681
|
|
682 /* Include one non-word character so that we can check for the
|
|
683 * word end. */
|
|
684 if (*mi.mi_fend != NUL)
|
|
685 mb_ptr_adv(mi.mi_fend);
|
|
686
|
|
687 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
|
|
688 MAXWLEN + 1);
|
|
689 mi.mi_fwordlen = STRLEN(mi.mi_fword);
|
|
690
|
|
691 /* The word is bad unless we recognize it. */
|
|
692 mi.mi_result = SP_BAD;
|
|
693
|
|
694 /*
|
|
695 * Loop over the languages specified in 'spelllang'.
|
|
696 * We check them all, because a matching word may be longer than an
|
|
697 * already found matching word.
|
|
698 */
|
|
699 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
|
|
700 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
|
|
701 {
|
|
702 /* Check for a matching word in case-folded words. */
|
|
703 find_word(&mi, FIND_FOLDWORD);
|
|
704
|
|
705 /* Check for a matching word in keep-case words. */
|
|
706 find_word(&mi, FIND_KEEPWORD);
|
|
707
|
|
708 /* Check for matching prefixes. */
|
|
709 find_prefix(&mi);
|
|
710 }
|
|
711
|
|
712 if (mi.mi_result != SP_OK)
|
|
713 {
|
346
|
714 /* If we found a number skip over it. Allows for "42nd". Do flag
|
|
715 * rare and local words, e.g., "3GPP". */
|
344
|
716 if (nrlen > 0)
|
346
|
717 {
|
|
718 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
|
|
719 return nrlen;
|
|
720 }
|
344
|
721
|
|
722 /* When we are at a non-word character there is no error, just
|
|
723 * skip over the character (try looking for a word after it). */
|
346
|
724 else if (!SPELL_ISWORDP(ptr))
|
243
|
725 {
|
344
|
726 #ifdef FEAT_MBYTE
|
|
727 if (has_mbyte)
|
|
728 return mb_ptr2len_check(ptr);
|
|
729 #endif
|
|
730 return 1;
|
300
|
731 }
|
243
|
732
|
344
|
733 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
|
|
734 *attrp = highlight_attr[HLF_SPB];
|
|
735 else if (mi.mi_result == SP_RARE)
|
|
736 *attrp = highlight_attr[HLF_SPR];
|
|
737 else
|
|
738 *attrp = highlight_attr[HLF_SPL];
|
243
|
739 }
|
|
740
|
300
|
741 return (int)(mi.mi_end - ptr);
|
236
|
742 }
|
|
743
|
|
744 /*
|
300
|
745 * Check if the word at "mip->mi_word" is in the tree.
|
339
|
746 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
|
|
747 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
|
|
748 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
|
|
749 * tree.
|
300
|
750 *
|
|
751 * For a match mip->mi_result is updated.
|
243
|
752 */
|
|
753 static void
|
339
|
754 find_word(mip, mode)
|
243
|
755 matchinf_T *mip;
|
339
|
756 int mode;
|
243
|
757 {
|
324
|
758 idx_T arridx = 0;
|
300
|
759 int endlen[MAXWLEN]; /* length at possible word endings */
|
324
|
760 idx_T endidx[MAXWLEN]; /* possible word endings */
|
300
|
761 int endidxcnt = 0;
|
|
762 int len;
|
|
763 int wlen = 0;
|
|
764 int flen;
|
|
765 int c;
|
|
766 char_u *ptr;
|
324
|
767 idx_T lo, hi, m;
|
243
|
768 #ifdef FEAT_MBYTE
|
300
|
769 char_u *s;
|
339
|
770 char_u *p;
|
307
|
771 #endif
|
|
772 int res = SP_BAD;
|
|
773 int valid;
|
300
|
774 slang_T *slang = mip->mi_lp->lp_slang;
|
|
775 unsigned flags;
|
|
776 char_u *byts;
|
324
|
777 idx_T *idxs;
|
339
|
778 int prefcnt;
|
|
779 int pidx;
|
|
780 regmatch_T regmatch;
|
|
781 regprog_T *rp;
|
|
782 int prefid;
|
|
783
|
|
784 if (mode == FIND_KEEPWORD)
|
236
|
785 {
|
300
|
786 /* Check for word with matching case in keep-case tree. */
|
|
787 ptr = mip->mi_word;
|
|
788 flen = 9999; /* no case folding, always enough bytes */
|
|
789 byts = slang->sl_kbyts;
|
|
790 idxs = slang->sl_kidxs;
|
236
|
791 }
|
|
792 else
|
|
793 {
|
300
|
794 /* Check for case-folded in case-folded tree. */
|
|
795 ptr = mip->mi_fword;
|
|
796 flen = mip->mi_fwordlen; /* available case-folded bytes */
|
|
797 byts = slang->sl_fbyts;
|
|
798 idxs = slang->sl_fidxs;
|
339
|
799
|
|
800 if (mode == FIND_PREFIX)
|
|
801 {
|
|
802 /* Skip over the prefix. */
|
|
803 wlen = mip->mi_prefixlen;
|
|
804 flen -= mip->mi_prefixlen;
|
|
805 }
|
243
|
806 }
|
|
807
|
300
|
808 if (byts == NULL)
|
|
809 return; /* array is empty */
|
236
|
810
|
|
811 /*
|
307
|
812 * Repeat advancing in the tree until:
|
|
813 * - there is a byte that doesn't match,
|
|
814 * - we reach the end of the tree,
|
|
815 * - or we reach the end of the line.
|
236
|
816 */
|
300
|
817 for (;;)
|
236
|
818 {
|
346
|
819 if (flen <= 0 && *mip->mi_fend != NUL)
|
339
|
820 flen = fold_more(mip);
|
300
|
821
|
|
822 len = byts[arridx++];
|
|
823
|
|
824 /* If the first possible byte is a zero the word could end here.
|
|
825 * Remember this index, we first check for the longest word. */
|
|
826 if (byts[arridx] == 0)
|
|
827 {
|
307
|
828 if (endidxcnt == MAXWLEN)
|
|
829 {
|
|
830 /* Must be a corrupted spell file. */
|
|
831 EMSG(_(e_format));
|
|
832 return;
|
|
833 }
|
300
|
834 endlen[endidxcnt] = wlen;
|
|
835 endidx[endidxcnt++] = arridx++;
|
|
836 --len;
|
|
837
|
|
838 /* Skip over the zeros, there can be several flag/region
|
|
839 * combinations. */
|
|
840 while (len > 0 && byts[arridx] == 0)
|
|
841 {
|
|
842 ++arridx;
|
|
843 --len;
|
|
844 }
|
|
845 if (len == 0)
|
|
846 break; /* no children, word must end here */
|
|
847 }
|
|
848
|
|
849 /* Stop looking at end of the line. */
|
|
850 if (ptr[wlen] == NUL)
|
|
851 break;
|
|
852
|
|
853 /* Perform a binary search in the list of accepted bytes. */
|
|
854 c = ptr[wlen];
|
346
|
855 if (c == TAB) /* <Tab> is handled like <Space> */
|
|
856 c = ' ';
|
300
|
857 lo = arridx;
|
|
858 hi = arridx + len - 1;
|
|
859 while (lo < hi)
|
|
860 {
|
|
861 m = (lo + hi) / 2;
|
|
862 if (byts[m] > c)
|
|
863 hi = m - 1;
|
|
864 else if (byts[m] < c)
|
|
865 lo = m + 1;
|
|
866 else
|
|
867 {
|
|
868 lo = hi = m;
|
|
869 break;
|
236
|
870 }
|
|
871 }
|
300
|
872
|
|
873 /* Stop if there is no matching byte. */
|
|
874 if (hi < lo || byts[lo] != c)
|
|
875 break;
|
|
876
|
|
877 /* Continue at the child (if there is one). */
|
|
878 arridx = idxs[lo];
|
|
879 ++wlen;
|
|
880 --flen;
|
346
|
881
|
|
882 /* One space in the good word may stand for several spaces in the
|
|
883 * checked word. */
|
|
884 if (c == ' ')
|
|
885 {
|
|
886 for (;;)
|
|
887 {
|
|
888 if (flen <= 0 && *mip->mi_fend != NUL)
|
|
889 flen = fold_more(mip);
|
|
890 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
|
|
891 break;
|
|
892 ++wlen;
|
|
893 --flen;
|
|
894 }
|
|
895 }
|
236
|
896 }
|
|
897
|
300
|
898 /*
|
|
899 * Verify that one of the possible endings is valid. Try the longest
|
|
900 * first.
|
|
901 */
|
|
902 while (endidxcnt > 0)
|
|
903 {
|
|
904 --endidxcnt;
|
|
905 arridx = endidx[endidxcnt];
|
|
906 wlen = endlen[endidxcnt];
|
236
|
907
|
300
|
908 #ifdef FEAT_MBYTE
|
|
909 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
|
|
910 continue; /* not at first byte of character */
|
|
911 #endif
|
307
|
912 if (SPELL_ISWORDP(ptr + wlen))
|
300
|
913 continue; /* next char is a word character */
|
|
914
|
|
915 #ifdef FEAT_MBYTE
|
339
|
916 if (mode != FIND_KEEPWORD && has_mbyte)
|
300
|
917 {
|
|
918 /* Compute byte length in original word, length may change
|
339
|
919 * when folding case. This can be slow, take a shortcut when the
|
|
920 * case-folded word is equal to the keep-case word. */
|
300
|
921 p = mip->mi_word;
|
339
|
922 if (STRNCMP(ptr, p, wlen) != 0)
|
|
923 {
|
|
924 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
|
|
925 mb_ptr_adv(p);
|
|
926 wlen = p - mip->mi_word;
|
|
927 }
|
300
|
928 }
|
|
929 #endif
|
236
|
930
|
339
|
931 /* Check flags and region. For FIND_PREFIX check the condition and
|
|
932 * prefix ID.
|
|
933 * Repeat this if there are more flags/region alternatives until there
|
|
934 * is a match. */
|
|
935 res = SP_BAD;
|
|
936 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
|
|
937 --len, ++arridx)
|
300
|
938 {
|
|
939 flags = idxs[arridx];
|
324
|
940
|
339
|
941 /* For the fold-case tree check that the case of the checked word
|
|
942 * matches with what the word in the tree requires.
|
|
943 * For keep-case tree the case is always right. For prefixes we
|
|
944 * don't bother to check. */
|
|
945 if (mode == FIND_FOLDWORD)
|
300
|
946 {
|
|
947 if (mip->mi_cend != mip->mi_word + wlen)
|
|
948 {
|
323
|
949 /* mi_capflags was set for a different word length, need
|
|
950 * to do it again. */
|
300
|
951 mip->mi_cend = mip->mi_word + wlen;
|
323
|
952 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
|
300
|
953 }
|
|
954
|
346
|
955 if (mip->mi_capflags == WF_KEEPCAP
|
|
956 || !spell_valid_case(mip->mi_capflags, flags))
|
339
|
957 continue;
|
300
|
958 }
|
236
|
959
|
339
|
960 /* When mode is FIND_PREFIX the word must support the prefix:
|
|
961 * check the prefix ID and the condition. Do that for the list at
|
|
962 * mip->mi_prefarridx. */
|
|
963 if (mode == FIND_PREFIX)
|
300
|
964 {
|
339
|
965 /* The prefix ID is stored two bytes above the flags. */
|
|
966 prefid = (unsigned)flags >> 16;
|
|
967
|
|
968 valid = FALSE;
|
|
969 for (prefcnt = mip->mi_prefcnt - 1; prefcnt >= 0; --prefcnt)
|
300
|
970 {
|
339
|
971 pidx = slang->sl_pidxs[mip->mi_prefarridx + prefcnt];
|
|
972
|
|
973 /* Check the prefix ID. */
|
|
974 if (prefid != (pidx & 0xff))
|
|
975 continue;
|
|
976
|
|
977 /* Check the condition, if there is one. The
|
|
978 * condition index is stored above the prefix ID byte.
|
|
979 */
|
|
980 rp = slang->sl_prefprog[(unsigned)pidx >> 8];
|
|
981 if (rp != NULL)
|
|
982 {
|
|
983 regmatch.regprog = rp;
|
|
984 regmatch.rm_ic = FALSE;
|
344
|
985 if (!vim_regexec(®match,
|
|
986 mip->mi_fword + mip->mi_prefixlen, 0))
|
339
|
987 continue;
|
|
988 }
|
|
989
|
|
990 /* It's a match, use it. */
|
|
991 valid = TRUE;
|
|
992 break;
|
300
|
993 }
|
339
|
994
|
|
995 if (!valid)
|
|
996 continue;
|
|
997 }
|
|
998
|
|
999 if (flags & WF_BANNED)
|
|
1000 res = SP_BANNED;
|
|
1001 else if (flags & WF_REGION)
|
|
1002 {
|
|
1003 /* Check region. */
|
|
1004 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
|
300
|
1005 res = SP_OK;
|
339
|
1006 else
|
|
1007 res = SP_LOCAL;
|
300
|
1008 }
|
339
|
1009 else if (flags & WF_RARE)
|
|
1010 res = SP_RARE;
|
307
|
1011 else
|
339
|
1012 res = SP_OK;
|
|
1013
|
|
1014 /* Always use the longest match and the best result. */
|
|
1015 if (mip->mi_result > res)
|
|
1016 {
|
|
1017 mip->mi_result = res;
|
|
1018 mip->mi_end = mip->mi_word + wlen;
|
|
1019 }
|
|
1020 else if (mip->mi_result == res
|
|
1021 && mip->mi_end < mip->mi_word + wlen)
|
|
1022 mip->mi_end = mip->mi_word + wlen;
|
|
1023
|
|
1024 if (res == SP_OK)
|
|
1025 break;
|
300
|
1026 }
|
|
1027
|
307
|
1028 if (res == SP_OK)
|
300
|
1029 break;
|
|
1030 }
|
236
|
1031 }
|
|
1032
|
323
|
1033 /*
|
339
|
1034 * Check if the word at "mip->mi_word" has a matching prefix.
|
|
1035 * If it does, then check the following word.
|
|
1036 *
|
|
1037 * For a match mip->mi_result is updated.
|
|
1038 */
|
|
1039 static void
|
|
1040 find_prefix(mip)
|
|
1041 matchinf_T *mip;
|
|
1042 {
|
|
1043 idx_T arridx = 0;
|
|
1044 int len;
|
|
1045 int wlen = 0;
|
|
1046 int flen;
|
|
1047 int c;
|
|
1048 char_u *ptr;
|
|
1049 idx_T lo, hi, m;
|
|
1050 slang_T *slang = mip->mi_lp->lp_slang;
|
|
1051 char_u *byts;
|
|
1052 idx_T *idxs;
|
|
1053
|
|
1054 /* We use the case-folded word here, since prefixes are always
|
|
1055 * case-folded. */
|
|
1056 ptr = mip->mi_fword;
|
|
1057 flen = mip->mi_fwordlen; /* available case-folded bytes */
|
|
1058 byts = slang->sl_pbyts;
|
|
1059 idxs = slang->sl_pidxs;
|
|
1060
|
|
1061 if (byts == NULL)
|
|
1062 return; /* array is empty */
|
|
1063
|
|
1064 /*
|
|
1065 * Repeat advancing in the tree until:
|
|
1066 * - there is a byte that doesn't match,
|
|
1067 * - we reach the end of the tree,
|
|
1068 * - or we reach the end of the line.
|
|
1069 */
|
|
1070 for (;;)
|
|
1071 {
|
|
1072 if (flen == 0 && *mip->mi_fend != NUL)
|
|
1073 flen = fold_more(mip);
|
|
1074
|
|
1075 len = byts[arridx++];
|
|
1076
|
|
1077 /* If the first possible byte is a zero the prefix could end here.
|
|
1078 * Check if the following word matches and supports the prefix. */
|
|
1079 if (byts[arridx] == 0)
|
|
1080 {
|
|
1081 /* There can be several prefixes with different conditions. We
|
|
1082 * try them all, since we don't know which one will give the
|
|
1083 * longest match. The word is the same each time, pass the list
|
|
1084 * of possible prefixes to find_word(). */
|
|
1085 mip->mi_prefarridx = arridx;
|
|
1086 mip->mi_prefcnt = len;
|
|
1087 while (len > 0 && byts[arridx] == 0)
|
|
1088 {
|
|
1089 ++arridx;
|
|
1090 --len;
|
|
1091 }
|
|
1092 mip->mi_prefcnt -= len;
|
|
1093
|
|
1094 /* Find the word that comes after the prefix. */
|
|
1095 mip->mi_prefixlen = wlen;
|
|
1096 find_word(mip, FIND_PREFIX);
|
|
1097
|
|
1098
|
|
1099 if (len == 0)
|
|
1100 break; /* no children, word must end here */
|
|
1101 }
|
|
1102
|
|
1103 /* Stop looking at end of the line. */
|
|
1104 if (ptr[wlen] == NUL)
|
|
1105 break;
|
|
1106
|
|
1107 /* Perform a binary search in the list of accepted bytes. */
|
|
1108 c = ptr[wlen];
|
|
1109 lo = arridx;
|
|
1110 hi = arridx + len - 1;
|
|
1111 while (lo < hi)
|
|
1112 {
|
|
1113 m = (lo + hi) / 2;
|
|
1114 if (byts[m] > c)
|
|
1115 hi = m - 1;
|
|
1116 else if (byts[m] < c)
|
|
1117 lo = m + 1;
|
|
1118 else
|
|
1119 {
|
|
1120 lo = hi = m;
|
|
1121 break;
|
|
1122 }
|
|
1123 }
|
|
1124
|
|
1125 /* Stop if there is no matching byte. */
|
|
1126 if (hi < lo || byts[lo] != c)
|
|
1127 break;
|
|
1128
|
|
1129 /* Continue at the child (if there is one). */
|
|
1130 arridx = idxs[lo];
|
|
1131 ++wlen;
|
|
1132 --flen;
|
|
1133 }
|
|
1134 }
|
|
1135
|
|
1136 /*
|
|
1137 * Need to fold at least one more character. Do until next non-word character
|
|
1138 * for efficiency.
|
|
1139 * Return the length of the folded chars in bytes.
|
|
1140 */
|
|
1141 static int
|
|
1142 fold_more(mip)
|
|
1143 matchinf_T *mip;
|
|
1144 {
|
|
1145 int flen;
|
|
1146 char_u *p;
|
|
1147
|
|
1148 p = mip->mi_fend;
|
|
1149 do
|
|
1150 {
|
|
1151 mb_ptr_adv(mip->mi_fend);
|
|
1152 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
|
|
1153
|
|
1154 /* Include the non-word character so that we can check for the
|
|
1155 * word end. */
|
|
1156 if (*mip->mi_fend != NUL)
|
|
1157 mb_ptr_adv(mip->mi_fend);
|
|
1158
|
|
1159 (void)spell_casefold(p, (int)(mip->mi_fend - p),
|
|
1160 mip->mi_fword + mip->mi_fwordlen,
|
|
1161 MAXWLEN - mip->mi_fwordlen);
|
|
1162 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
|
|
1163 mip->mi_fwordlen += flen;
|
|
1164 return flen;
|
|
1165 }
|
|
1166
|
|
1167 /*
|
323
|
1168 * Check case flags for a word. Return TRUE if the word has the requested
|
|
1169 * case.
|
|
1170 */
|
|
1171 static int
|
|
1172 spell_valid_case(origflags, treeflags)
|
|
1173 int origflags; /* flags for the checked word. */
|
|
1174 int treeflags; /* flags for the word in the spell tree */
|
|
1175 {
|
|
1176 return (origflags == WF_ALLCAP
|
|
1177 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
|
|
1178 && ((treeflags & WF_ONECAP) == 0 || origflags == WF_ONECAP)));
|
|
1179 }
|
|
1180
|
300
|
1181
|
236
|
1182 /*
|
|
1183 * Move to next spell error.
|
323
|
1184 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
|
236
|
1185 * Return OK if found, FAIL otherwise.
|
|
1186 */
|
|
1187 int
|
323
|
1188 spell_move_to(dir, allwords, curline)
|
236
|
1189 int dir; /* FORWARD or BACKWARD */
|
|
1190 int allwords; /* TRUE for "[s" and "]s" */
|
323
|
1191 int curline;
|
236
|
1192 {
|
249
|
1193 linenr_T lnum;
|
|
1194 pos_T found_pos;
|
236
|
1195 char_u *line;
|
|
1196 char_u *p;
|
346
|
1197 char_u *endp;
|
|
1198 int attr;
|
236
|
1199 int len;
|
249
|
1200 int has_syntax = syntax_present(curbuf);
|
|
1201 int col;
|
|
1202 int can_spell;
|
346
|
1203 char_u *buf = NULL;
|
|
1204 int buflen = 0;
|
|
1205 int skip = 0;
|
236
|
1206
|
310
|
1207 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
|
236
|
1208 {
|
|
1209 EMSG(_("E756: Spell checking not enabled"));
|
|
1210 return FAIL;
|
|
1211 }
|
|
1212
|
249
|
1213 /*
|
|
1214 * Start looking for bad word at the start of the line, because we can't
|
346
|
1215 * start halfway a word, we don't know where the it starts or ends.
|
249
|
1216 *
|
|
1217 * When searching backwards, we continue in the line to find the last
|
|
1218 * bad word (in the cursor line: before the cursor).
|
346
|
1219 *
|
|
1220 * We concatenate the start of the next line, so that wrapped words work
|
|
1221 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
|
|
1222 * though...
|
249
|
1223 */
|
|
1224 lnum = curwin->w_cursor.lnum;
|
|
1225 found_pos.lnum = 0;
|
236
|
1226
|
|
1227 while (!got_int)
|
|
1228 {
|
249
|
1229 line = ml_get(lnum);
|
346
|
1230
|
|
1231 len = STRLEN(line);
|
|
1232 if (buflen < len + MAXWLEN + 2)
|
|
1233 {
|
|
1234 vim_free(buf);
|
|
1235 buflen = len + MAXWLEN + 2;
|
|
1236 buf = alloc(buflen);
|
|
1237 if (buf == NULL)
|
|
1238 break;
|
|
1239 }
|
|
1240
|
|
1241 /* Copy the line into "buf" and append the start of the next line if
|
|
1242 * possible. */
|
|
1243 STRCPY(buf, line);
|
|
1244 if (lnum < curbuf->b_ml.ml_line_count)
|
|
1245 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
|
|
1246
|
|
1247 p = buf + skip;
|
|
1248 endp = buf + len;
|
|
1249 while (p < endp)
|
236
|
1250 {
|
300
|
1251 /* When searching backward don't search after the cursor. */
|
|
1252 if (dir == BACKWARD
|
|
1253 && lnum == curwin->w_cursor.lnum
|
346
|
1254 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
|
300
|
1255 break;
|
249
|
1256
|
300
|
1257 /* start of word */
|
346
|
1258 attr = 0;
|
300
|
1259 len = spell_check(curwin, p, &attr);
|
249
|
1260
|
300
|
1261 if (attr != 0)
|
|
1262 {
|
|
1263 /* We found a bad word. Check the attribute. */
|
|
1264 if (allwords || attr == highlight_attr[HLF_SPB])
|
236
|
1265 {
|
300
|
1266 /* When searching forward only accept a bad word after
|
|
1267 * the cursor. */
|
|
1268 if (dir == BACKWARD
|
|
1269 || lnum > curwin->w_cursor.lnum
|
|
1270 || (lnum == curwin->w_cursor.lnum
|
346
|
1271 && (colnr_T)(curline ? p - buf + len
|
|
1272 : p - buf)
|
300
|
1273 > curwin->w_cursor.col))
|
236
|
1274 {
|
300
|
1275 if (has_syntax)
|
249
|
1276 {
|
346
|
1277 col = p - buf;
|
300
|
1278 (void)syn_get_id(lnum, (colnr_T)col,
|
|
1279 FALSE, &can_spell);
|
|
1280 }
|
|
1281 else
|
|
1282 can_spell = TRUE;
|
249
|
1283
|
300
|
1284 if (can_spell)
|
|
1285 {
|
|
1286 found_pos.lnum = lnum;
|
346
|
1287 found_pos.col = p - buf;
|
249
|
1288 #ifdef FEAT_VIRTUALEDIT
|
300
|
1289 found_pos.coladd = 0;
|
249
|
1290 #endif
|
300
|
1291 if (dir == FORWARD)
|
|
1292 {
|
|
1293 /* No need to search further. */
|
|
1294 curwin->w_cursor = found_pos;
|
346
|
1295 vim_free(buf);
|
300
|
1296 return OK;
|
249
|
1297 }
|
|
1298 }
|
236
|
1299 }
|
|
1300 }
|
|
1301 }
|
|
1302
|
300
|
1303 /* advance to character after the word */
|
|
1304 p += len;
|
236
|
1305 }
|
|
1306
|
323
|
1307 if (curline)
|
346
|
1308 break; /* only check cursor line */
|
323
|
1309
|
236
|
1310 /* Advance to next line. */
|
249
|
1311 if (dir == BACKWARD)
|
|
1312 {
|
|
1313 if (found_pos.lnum != 0)
|
|
1314 {
|
|
1315 /* Use the last match in the line. */
|
|
1316 curwin->w_cursor = found_pos;
|
346
|
1317 vim_free(buf);
|
249
|
1318 return OK;
|
|
1319 }
|
|
1320 if (lnum == 1)
|
346
|
1321 break;
|
249
|
1322 --lnum;
|
|
1323 }
|
|
1324 else
|
|
1325 {
|
|
1326 if (lnum == curbuf->b_ml.ml_line_count)
|
346
|
1327 break;
|
249
|
1328 ++lnum;
|
346
|
1329
|
|
1330 /* Skip the characters at the start of the next line that were
|
|
1331 * included in a match crossing line boundaries. */
|
|
1332 if (attr == 0)
|
|
1333 skip = p - endp;
|
|
1334 else
|
|
1335 skip = 0;
|
249
|
1336 }
|
236
|
1337
|
|
1338 line_breakcheck();
|
|
1339 }
|
|
1340
|
346
|
1341 vim_free(buf);
|
|
1342 return FAIL;
|
|
1343 }
|
|
1344
|
|
1345 /*
|
|
1346 * For spell checking: concatenate the start of the following line "line" into
|
|
1347 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
|
|
1348 */
|
|
1349 void
|
|
1350 spell_cat_line(buf, line, maxlen)
|
|
1351 char_u *buf;
|
|
1352 char_u *line;
|
|
1353 int maxlen;
|
|
1354 {
|
|
1355 char_u *p;
|
|
1356 int n;
|
|
1357
|
|
1358 p = skipwhite(line);
|
|
1359 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
|
|
1360 p = skipwhite(p + 1);
|
|
1361
|
|
1362 if (*p != NUL)
|
|
1363 {
|
|
1364 *buf = ' ';
|
|
1365 vim_strncpy(buf + 1, line, maxlen - 1);
|
|
1366 n = p - line;
|
|
1367 if (n >= maxlen)
|
|
1368 n = maxlen - 1;
|
|
1369 vim_memset(buf + 1, ' ', n);
|
|
1370 }
|
236
|
1371 }
|
|
1372
|
|
1373 /*
|
307
|
1374 * Load word list(s) for "lang" from Vim spell file(s).
|
310
|
1375 * "lang" must be the language without the region: e.g., "en".
|
236
|
1376 */
|
307
|
1377 static void
|
236
|
1378 spell_load_lang(lang)
|
|
1379 char_u *lang;
|
|
1380 {
|
310
|
1381 char_u fname_enc[85];
|
236
|
1382 int r;
|
307
|
1383 char_u langcp[MAXWLEN + 1];
|
|
1384
|
310
|
1385 /* Copy the language name to pass it to spell_load_cb() as a cookie.
|
307
|
1386 * It's truncated when an error is detected. */
|
|
1387 STRCPY(langcp, lang);
|
|
1388
|
310
|
1389 /*
|
|
1390 * Find the first spell file for "lang" in 'runtimepath' and load it.
|
|
1391 */
|
|
1392 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
|
|
1393 "spell/%s.%s.spl", lang, spell_enc());
|
|
1394 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
|
307
|
1395
|
|
1396 if (r == FAIL && *langcp != NUL)
|
|
1397 {
|
|
1398 /* Try loading the ASCII version. */
|
310
|
1399 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
|
272
|
1400 "spell/%s.ascii.spl", lang);
|
310
|
1401 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
|
307
|
1402 }
|
|
1403
|
|
1404 if (r == FAIL)
|
|
1405 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
|
236
|
1406 fname_enc + 6);
|
310
|
1407 else if (*langcp != NUL)
|
|
1408 {
|
|
1409 /* Load all the additions. */
|
|
1410 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
|
|
1411 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
|
|
1412 }
|
|
1413 }
|
|
1414
|
|
1415 /*
|
|
1416 * Return the encoding used for spell checking: Use 'encoding', except that we
|
|
1417 * use "latin1" for "latin9". And limit to 60 characters (just in case).
|
|
1418 */
|
|
1419 static char_u *
|
|
1420 spell_enc()
|
|
1421 {
|
|
1422
|
|
1423 #ifdef FEAT_MBYTE
|
|
1424 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
|
|
1425 return p_enc;
|
|
1426 #endif
|
|
1427 return (char_u *)"latin1";
|
236
|
1428 }
|
|
1429
|
|
1430 /*
|
|
1431 * Allocate a new slang_T.
|
|
1432 * Caller must fill "sl_next".
|
|
1433 */
|
|
1434 static slang_T *
|
|
1435 slang_alloc(lang)
|
|
1436 char_u *lang;
|
|
1437 {
|
|
1438 slang_T *lp;
|
|
1439
|
300
|
1440 lp = (slang_T *)alloc_clear(sizeof(slang_T));
|
236
|
1441 if (lp != NULL)
|
|
1442 {
|
|
1443 lp->sl_name = vim_strsave(lang);
|
323
|
1444 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
|
344
|
1445 ga_init2(&lp->sl_sal, sizeof(salitem_T), 10);
|
236
|
1446 }
|
|
1447 return lp;
|
|
1448 }
|
|
1449
|
|
1450 /*
|
|
1451 * Free the contents of an slang_T and the structure itself.
|
|
1452 */
|
|
1453 static void
|
|
1454 slang_free(lp)
|
|
1455 slang_T *lp;
|
|
1456 {
|
|
1457 vim_free(lp->sl_name);
|
310
|
1458 vim_free(lp->sl_fname);
|
|
1459 slang_clear(lp);
|
|
1460 vim_free(lp);
|
|
1461 }
|
|
1462
|
|
1463 /*
|
|
1464 * Clear an slang_T so that the file can be reloaded.
|
|
1465 */
|
|
1466 static void
|
|
1467 slang_clear(lp)
|
|
1468 slang_T *lp;
|
|
1469 {
|
339
|
1470 garray_T *gap;
|
|
1471 fromto_T *ftp;
|
344
|
1472 salitem_T *smp;
|
339
|
1473 int i;
|
323
|
1474
|
300
|
1475 vim_free(lp->sl_fbyts);
|
310
|
1476 lp->sl_fbyts = NULL;
|
300
|
1477 vim_free(lp->sl_kbyts);
|
310
|
1478 lp->sl_kbyts = NULL;
|
339
|
1479 vim_free(lp->sl_pbyts);
|
|
1480 lp->sl_pbyts = NULL;
|
|
1481
|
300
|
1482 vim_free(lp->sl_fidxs);
|
310
|
1483 lp->sl_fidxs = NULL;
|
300
|
1484 vim_free(lp->sl_kidxs);
|
310
|
1485 lp->sl_kidxs = NULL;
|
339
|
1486 vim_free(lp->sl_pidxs);
|
|
1487 lp->sl_pidxs = NULL;
|
323
|
1488
|
344
|
1489 gap = &lp->sl_rep;
|
|
1490 while (gap->ga_len > 0)
|
323
|
1491 {
|
344
|
1492 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
|
|
1493 vim_free(ftp->ft_from);
|
|
1494 vim_free(ftp->ft_to);
|
323
|
1495 }
|
344
|
1496 ga_clear(gap);
|
|
1497
|
|
1498 gap = &lp->sl_sal;
|
|
1499 while (gap->ga_len > 0)
|
|
1500 {
|
|
1501 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
|
|
1502 vim_free(smp->sm_lead);
|
|
1503 vim_free(smp->sm_to);
|
|
1504 }
|
|
1505 ga_clear(gap);
|
323
|
1506
|
339
|
1507 for (i = 0; i < lp->sl_prefixcnt; ++i)
|
|
1508 vim_free(lp->sl_prefprog[i]);
|
|
1509 vim_free(lp->sl_prefprog);
|
|
1510
|
330
|
1511 #ifdef FEAT_MBYTE
|
|
1512 {
|
|
1513 int todo = lp->sl_map_hash.ht_used;
|
|
1514 hashitem_T *hi;
|
|
1515
|
|
1516 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
|
|
1517 if (!HASHITEM_EMPTY(hi))
|
|
1518 {
|
|
1519 --todo;
|
|
1520 vim_free(hi->hi_key);
|
|
1521 }
|
|
1522 }
|
|
1523 hash_clear(&lp->sl_map_hash);
|
|
1524 #endif
|
236
|
1525 }
|
|
1526
|
|
1527 /*
|
307
|
1528 * Load one spell file and store the info into a slang_T.
|
236
|
1529 * Invoked through do_in_runtimepath().
|
|
1530 */
|
|
1531 static void
|
310
|
1532 spell_load_cb(fname, cookie)
|
236
|
1533 char_u *fname;
|
307
|
1534 void *cookie; /* points to the language name */
|
236
|
1535 {
|
323
|
1536 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
|
310
|
1537 }
|
|
1538
|
|
1539 /*
|
|
1540 * Load one spell file and store the info into a slang_T.
|
|
1541 *
|
|
1542 * This is invoked in two ways:
|
|
1543 * - From spell_load_cb() to load a spell file for the first time. "lang" is
|
|
1544 * the language name, "old_lp" is NULL. Will allocate an slang_T.
|
|
1545 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
|
|
1546 * points to the existing slang_T.
|
323
|
1547 * Returns the slang_T the spell file was loaded into. NULL for error.
|
310
|
1548 */
|
323
|
1549 static slang_T *
|
|
1550 spell_load_file(fname, lang, old_lp, silent)
|
310
|
1551 char_u *fname;
|
|
1552 char_u *lang;
|
|
1553 slang_T *old_lp;
|
323
|
1554 int silent; /* no error if file doesn't exist */
|
310
|
1555 {
|
236
|
1556 FILE *fd;
|
|
1557 char_u buf[MAXWLEN + 1];
|
|
1558 char_u *p;
|
339
|
1559 char_u *bp;
|
|
1560 idx_T *ip;
|
236
|
1561 int i;
|
339
|
1562 int n;
|
300
|
1563 int len;
|
236
|
1564 int round;
|
|
1565 char_u *save_sourcing_name = sourcing_name;
|
|
1566 linenr_T save_sourcing_lnum = sourcing_lnum;
|
255
|
1567 int cnt, ccnt;
|
|
1568 char_u *fol;
|
307
|
1569 slang_T *lp = NULL;
|
323
|
1570 garray_T *gap;
|
|
1571 fromto_T *ftp;
|
344
|
1572 salitem_T *smp;
|
323
|
1573 int rr;
|
|
1574 short *first;
|
324
|
1575 idx_T idx;
|
344
|
1576 int c = 0;
|
236
|
1577
|
310
|
1578 fd = mch_fopen((char *)fname, "r");
|
236
|
1579 if (fd == NULL)
|
|
1580 {
|
323
|
1581 if (!silent)
|
|
1582 EMSG2(_(e_notopen), fname);
|
|
1583 else if (p_verbose > 2)
|
|
1584 {
|
|
1585 verbose_enter();
|
|
1586 smsg((char_u *)e_notopen, fname);
|
|
1587 verbose_leave();
|
|
1588 }
|
255
|
1589 goto endFAIL;
|
236
|
1590 }
|
310
|
1591 if (p_verbose > 2)
|
|
1592 {
|
|
1593 verbose_enter();
|
|
1594 smsg((char_u *)_("Reading spell file \"%s\""), fname);
|
|
1595 verbose_leave();
|
|
1596 }
|
|
1597
|
|
1598 if (old_lp == NULL)
|
|
1599 {
|
|
1600 lp = slang_alloc(lang);
|
|
1601 if (lp == NULL)
|
|
1602 goto endFAIL;
|
|
1603
|
|
1604 /* Remember the file name, used to reload the file when it's updated. */
|
|
1605 lp->sl_fname = vim_strsave(fname);
|
|
1606 if (lp->sl_fname == NULL)
|
|
1607 goto endFAIL;
|
|
1608
|
|
1609 /* Check for .add.spl. */
|
|
1610 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
|
|
1611 }
|
|
1612 else
|
|
1613 lp = old_lp;
|
307
|
1614
|
236
|
1615 /* Set sourcing_name, so that error messages mention the file name. */
|
|
1616 sourcing_name = fname;
|
|
1617 sourcing_lnum = 0;
|
|
1618
|
339
|
1619 /* <HEADER>: <fileID>
|
|
1620 * <regioncnt> <regionname> ...
|
|
1621 * <charflagslen> <charflags>
|
|
1622 * <fcharslen> <fchars>
|
|
1623 * <prefcondcnt> <prefcond> ...
|
|
1624 */
|
236
|
1625 for (i = 0; i < VIMSPELLMAGICL; ++i)
|
|
1626 buf[i] = getc(fd); /* <fileID> */
|
|
1627 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
|
|
1628 {
|
|
1629 EMSG(_("E757: Wrong file ID in spell file"));
|
255
|
1630 goto endFAIL;
|
236
|
1631 }
|
|
1632
|
|
1633 cnt = getc(fd); /* <regioncnt> */
|
255
|
1634 if (cnt < 0)
|
236
|
1635 {
|
|
1636 truncerr:
|
|
1637 EMSG(_("E758: Truncated spell file"));
|
255
|
1638 goto endFAIL;
|
236
|
1639 }
|
|
1640 if (cnt > 8)
|
|
1641 {
|
|
1642 formerr:
|
307
|
1643 EMSG(_(e_format));
|
255
|
1644 goto endFAIL;
|
236
|
1645 }
|
|
1646 for (i = 0; i < cnt; ++i)
|
|
1647 {
|
|
1648 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
|
|
1649 lp->sl_regions[i * 2 + 1] = getc(fd);
|
|
1650 }
|
|
1651 lp->sl_regions[cnt * 2] = NUL;
|
|
1652
|
255
|
1653 cnt = getc(fd); /* <charflagslen> */
|
|
1654 if (cnt > 0)
|
|
1655 {
|
300
|
1656 p = alloc((unsigned)cnt);
|
255
|
1657 if (p == NULL)
|
|
1658 goto endFAIL;
|
|
1659 for (i = 0; i < cnt; ++i)
|
|
1660 p[i] = getc(fd); /* <charflags> */
|
|
1661
|
|
1662 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
|
|
1663 if (ccnt <= 0)
|
300
|
1664 {
|
|
1665 vim_free(p);
|
255
|
1666 goto formerr;
|
300
|
1667 }
|
|
1668 fol = alloc((unsigned)ccnt + 1);
|
255
|
1669 if (fol == NULL)
|
300
|
1670 {
|
|
1671 vim_free(p);
|
255
|
1672 goto endFAIL;
|
300
|
1673 }
|
255
|
1674 for (i = 0; i < ccnt; ++i)
|
|
1675 fol[i] = getc(fd); /* <fchars> */
|
|
1676 fol[i] = NUL;
|
|
1677
|
324
|
1678 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
|
300
|
1679 i = set_spell_charflags(p, cnt, fol);
|
|
1680 vim_free(p);
|
|
1681 vim_free(fol);
|
|
1682 if (i == FAIL)
|
255
|
1683 goto formerr;
|
|
1684 }
|
|
1685 else
|
|
1686 {
|
|
1687 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
|
|
1688 cnt = (getc(fd) << 8) + getc(fd);
|
|
1689 if (cnt != 0)
|
|
1690 goto formerr;
|
|
1691 }
|
|
1692
|
339
|
1693 /* <prefcondcnt> <prefcond> ... */
|
|
1694 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
|
|
1695 if (cnt > 0)
|
|
1696 {
|
|
1697 lp->sl_prefprog = (regprog_T **)alloc_clear(
|
|
1698 (unsigned)sizeof(regprog_T *) * cnt);
|
|
1699 if (lp->sl_prefprog == NULL)
|
|
1700 goto endFAIL;
|
|
1701 lp->sl_prefixcnt = cnt;
|
|
1702
|
|
1703 for (i = 0; i < cnt; ++i)
|
|
1704 {
|
|
1705 /* <prefcond> : <condlen> <condstr> */
|
|
1706 n = getc(fd); /* <condlen> */
|
|
1707 if (n < 0)
|
|
1708 goto formerr;
|
|
1709 /* When <condlen> is zero we have an empty condition. Otherwise
|
|
1710 * compile the regexp program used to check for the condition. */
|
|
1711 if (n > 0)
|
|
1712 {
|
344
|
1713 buf[0] = '^'; /* always match at one position only */
|
|
1714 p = buf + 1;
|
339
|
1715 while (n-- > 0)
|
|
1716 *p++ = getc(fd); /* <condstr> */
|
|
1717 *p = NUL;
|
|
1718 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
|
|
1719 }
|
|
1720 }
|
|
1721 }
|
|
1722
|
|
1723
|
323
|
1724 /* <SUGGEST> : <repcount> <rep> ...
|
|
1725 * <salflags> <salcount> <sal> ...
|
|
1726 * <maplen> <mapstr> */
|
339
|
1727
|
344
|
1728 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
|
|
1729 if (cnt < 0)
|
|
1730 goto formerr;
|
|
1731
|
|
1732 gap = &lp->sl_rep;
|
|
1733 if (ga_grow(gap, cnt) == FAIL)
|
|
1734 goto endFAIL;
|
|
1735
|
|
1736 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
|
|
1737 for (; gap->ga_len < cnt; ++gap->ga_len)
|
|
1738 {
|
|
1739 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
|
|
1740 for (rr = 1; rr <= 2; ++rr)
|
|
1741 {
|
|
1742 ccnt = getc(fd);
|
|
1743 if (ccnt < 0)
|
|
1744 {
|
|
1745 if (rr == 2)
|
|
1746 vim_free(ftp->ft_from);
|
|
1747 goto formerr;
|
|
1748 }
|
|
1749 if ((p = alloc(ccnt + 1)) == NULL)
|
|
1750 {
|
|
1751 if (rr == 2)
|
|
1752 vim_free(ftp->ft_from);
|
|
1753 goto endFAIL;
|
|
1754 }
|
|
1755 for (i = 0; i < ccnt; ++i)
|
|
1756 p[i] = getc(fd); /* <repfrom> or <repto> */
|
|
1757 p[i] = NUL;
|
|
1758 if (rr == 1)
|
|
1759 ftp->ft_from = p;
|
|
1760 else
|
|
1761 ftp->ft_to = p;
|
|
1762 }
|
|
1763 }
|
|
1764
|
|
1765 /* Fill the first-index table. */
|
|
1766 first = lp->sl_rep_first;
|
|
1767 for (i = 0; i < 256; ++i)
|
|
1768 first[i] = -1;
|
|
1769 for (i = 0; i < gap->ga_len; ++i)
|
323
|
1770 {
|
344
|
1771 ftp = &((fromto_T *)gap->ga_data)[i];
|
|
1772 if (first[*ftp->ft_from] == -1)
|
|
1773 first[*ftp->ft_from] = i;
|
|
1774 }
|
|
1775
|
|
1776 i = getc(fd); /* <salflags> */
|
|
1777 if (i & SAL_F0LLOWUP)
|
|
1778 lp->sl_followup = TRUE;
|
|
1779 if (i & SAL_COLLAPSE)
|
|
1780 lp->sl_collapse = TRUE;
|
|
1781 if (i & SAL_REM_ACCENTS)
|
|
1782 lp->sl_rem_accents = TRUE;
|
|
1783
|
|
1784 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
|
|
1785 if (cnt < 0)
|
|
1786 goto formerr;
|
|
1787
|
|
1788 gap = &lp->sl_sal;
|
|
1789 if (ga_grow(gap, cnt) == FAIL)
|
|
1790 goto endFAIL;
|
|
1791
|
|
1792 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
|
|
1793 for (; gap->ga_len < cnt; ++gap->ga_len)
|
|
1794 {
|
|
1795 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
|
|
1796 ccnt = getc(fd); /* <salfromlen> */
|
|
1797 if (ccnt < 0)
|
|
1798 goto formerr;
|
|
1799 if ((p = alloc(ccnt + 2)) == NULL)
|
|
1800 goto endFAIL;
|
|
1801 smp->sm_lead = p;
|
|
1802
|
|
1803 /* Read up to the first special char into sm_lead. */
|
|
1804 for (i = 0; i < ccnt; ++i)
|
323
|
1805 {
|
344
|
1806 c = getc(fd); /* <salfrom> */
|
|
1807 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
|
|
1808 break;
|
|
1809 *p++ = c;
|
|
1810 }
|
|
1811 smp->sm_leadlen = p - smp->sm_lead;
|
|
1812 *p++ = NUL;
|
|
1813
|
|
1814 /* Put optional chars in sm_oneoff, if any. */
|
|
1815 if (c == '(')
|
|
1816 {
|
|
1817 smp->sm_oneoff = p;
|
|
1818 for (++i; i < ccnt; ++i)
|
|
1819 {
|
|
1820 c = getc(fd); /* <salfrom> */
|
|
1821 if (c == ')')
|
|
1822 break;
|
|
1823 *p++ = c;
|
|
1824 }
|
|
1825 *p++ = NUL;
|
|
1826 if (++i < ccnt)
|
|
1827 c = getc(fd);
|
323
|
1828 }
|
|
1829 else
|
344
|
1830 smp->sm_oneoff = NULL;
|
|
1831
|
|
1832 /* Any following chars go in sm_rules. */
|
|
1833 smp->sm_rules = p;
|
|
1834 if (i < ccnt)
|
|
1835 *p++ = c;
|
|
1836 for (++i; i < ccnt; ++i)
|
|
1837 *p++ = getc(fd); /* <salfrom> */
|
|
1838 *p++ = NUL;
|
|
1839
|
|
1840 ccnt = getc(fd); /* <saltolen> */
|
|
1841 if (ccnt < 0)
|
323
|
1842 {
|
344
|
1843 vim_free(smp->sm_lead);
|
|
1844 goto formerr;
|
323
|
1845 }
|
344
|
1846 if ((p = alloc(ccnt + 1)) == NULL)
|
323
|
1847 {
|
344
|
1848 vim_free(smp->sm_lead);
|
|
1849 goto endFAIL;
|
323
|
1850 }
|
344
|
1851 smp->sm_to = p;
|
|
1852
|
|
1853 for (i = 0; i < ccnt; ++i)
|
|
1854 *p++ = getc(fd); /* <salto> */
|
|
1855 *p++ = NUL;
|
|
1856 }
|
|
1857
|
|
1858 /* Fill the first-index table. */
|
|
1859 first = lp->sl_sal_first;
|
|
1860 for (i = 0; i < 256; ++i)
|
|
1861 first[i] = -1;
|
|
1862 for (i = 0; i < gap->ga_len; ++i)
|
|
1863 {
|
|
1864 smp = &((salitem_T *)gap->ga_data)[i];
|
|
1865 if (first[*smp->sm_lead] == -1)
|
|
1866 first[*smp->sm_lead] = i;
|
323
|
1867 }
|
|
1868
|
|
1869 cnt = (getc(fd) << 8) + getc(fd); /* <maplen> */
|
|
1870 if (cnt < 0)
|
|
1871 goto formerr;
|
|
1872 p = alloc(cnt + 1);
|
|
1873 if (p == NULL)
|
|
1874 goto endFAIL;
|
|
1875 for (i = 0; i < cnt; ++i)
|
|
1876 p[i] = getc(fd); /* <mapstr> */
|
|
1877 p[i] = NUL;
|
330
|
1878 set_map_str(lp, p);
|
|
1879 vim_free(p);
|
323
|
1880
|
236
|
1881
|
300
|
1882 /* round 1: <LWORDTREE>
|
339
|
1883 * round 2: <KWORDTREE>
|
|
1884 * round 3: <PREFIXTREE> */
|
|
1885 for (round = 1; round <= 3; ++round)
|
236
|
1886 {
|
300
|
1887 /* The tree size was computed when writing the file, so that we can
|
|
1888 * allocate it as one long block. <nodecount> */
|
|
1889 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
1890 if (len < 0)
|
|
1891 goto truncerr;
|
|
1892 if (len > 0)
|
236
|
1893 {
|
300
|
1894 /* Allocate the byte array. */
|
339
|
1895 bp = lalloc((long_u)len, TRUE);
|
|
1896 if (bp == NULL)
|
300
|
1897 goto endFAIL;
|
|
1898 if (round == 1)
|
339
|
1899 lp->sl_fbyts = bp;
|
|
1900 else if (round == 2)
|
|
1901 lp->sl_kbyts = bp;
|
300
|
1902 else
|
339
|
1903 lp->sl_pbyts = bp;
|
236
|
1904
|
300
|
1905 /* Allocate the index array. */
|
339
|
1906 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
|
|
1907 if (ip == NULL)
|
300
|
1908 goto endFAIL;
|
|
1909 if (round == 1)
|
339
|
1910 lp->sl_fidxs = ip;
|
|
1911 else if (round == 2)
|
|
1912 lp->sl_kidxs = ip;
|
300
|
1913 else
|
339
|
1914 lp->sl_pidxs = ip;
|
300
|
1915
|
|
1916 /* Read the tree and store it in the array. */
|
339
|
1917 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
|
324
|
1918 if (idx == -1)
|
300
|
1919 goto truncerr;
|
324
|
1920 if (idx < 0)
|
236
|
1921 goto formerr;
|
|
1922 }
|
300
|
1923 }
|
243
|
1924
|
310
|
1925 /* For a new file link it in the list of spell files. */
|
|
1926 if (old_lp == NULL)
|
|
1927 {
|
|
1928 lp->sl_next = first_lang;
|
|
1929 first_lang = lp;
|
|
1930 }
|
307
|
1931
|
255
|
1932 goto endOK;
|
|
1933
|
|
1934 endFAIL:
|
310
|
1935 if (lang != NULL)
|
|
1936 /* truncating the name signals the error to spell_load_lang() */
|
|
1937 *lang = NUL;
|
|
1938 if (lp != NULL && old_lp == NULL)
|
323
|
1939 {
|
307
|
1940 slang_free(lp);
|
323
|
1941 lp = NULL;
|
|
1942 }
|
255
|
1943
|
|
1944 endOK:
|
236
|
1945 if (fd != NULL)
|
|
1946 fclose(fd);
|
|
1947 sourcing_name = save_sourcing_name;
|
|
1948 sourcing_lnum = save_sourcing_lnum;
|
323
|
1949
|
|
1950 return lp;
|
236
|
1951 }
|
|
1952
|
|
1953 /*
|
300
|
1954 * Read one row of siblings from the spell file and store it in the byte array
|
|
1955 * "byts" and index array "idxs". Recursively read the children.
|
|
1956 *
|
346
|
1957 * NOTE: The code here must match put_node().
|
300
|
1958 *
|
|
1959 * Returns the index follosing the siblings.
|
|
1960 * Returns -1 if the file is shorter than expected.
|
|
1961 * Returns -2 if there is a format error.
|
236
|
1962 */
|
324
|
1963 static idx_T
|
339
|
1964 read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
|
300
|
1965 FILE *fd;
|
|
1966 char_u *byts;
|
324
|
1967 idx_T *idxs;
|
300
|
1968 int maxidx; /* size of arrays */
|
324
|
1969 idx_T startidx; /* current index in "byts" and "idxs" */
|
339
|
1970 int prefixtree; /* TRUE for reading PREFIXTREE */
|
|
1971 int maxprefcondnr; /* maximum for <prefcondnr> */
|
236
|
1972 {
|
300
|
1973 int len;
|
|
1974 int i;
|
|
1975 int n;
|
324
|
1976 idx_T idx = startidx;
|
300
|
1977 int c;
|
|
1978 #define SHARED_MASK 0x8000000
|
236
|
1979
|
300
|
1980 len = getc(fd); /* <siblingcount> */
|
|
1981 if (len <= 0)
|
|
1982 return -1;
|
|
1983
|
|
1984 if (startidx + len >= maxidx)
|
|
1985 return -2;
|
|
1986 byts[idx++] = len;
|
|
1987
|
|
1988 /* Read the byte values, flag/region bytes and shared indexes. */
|
|
1989 for (i = 1; i <= len; ++i)
|
236
|
1990 {
|
300
|
1991 c = getc(fd); /* <byte> */
|
|
1992 if (c < 0)
|
|
1993 return -1;
|
|
1994 if (c <= BY_SPECIAL)
|
|
1995 {
|
|
1996 if (c == BY_NOFLAGS)
|
|
1997 {
|
|
1998 /* No flags, all regions. */
|
|
1999 idxs[idx] = 0;
|
|
2000 c = 0;
|
|
2001 }
|
|
2002 else if (c == BY_FLAGS)
|
|
2003 {
|
339
|
2004 if (prefixtree)
|
|
2005 {
|
|
2006 /* Read the prefix ID and the condition nr. In idxs[]
|
|
2007 * store the prefix ID in the low byte, the condition
|
|
2008 * index shifted up 8 bits. */
|
|
2009 c = getc(fd); /* <prefixID> */
|
|
2010 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
|
|
2011 if (n >= maxprefcondnr)
|
|
2012 return -2;
|
|
2013 c = (n << 8) + c;
|
|
2014 }
|
|
2015 else
|
|
2016 {
|
|
2017 /* Read flags and optional region and prefix ID. In
|
|
2018 * idxs[] the flags go in the low byte, region above that
|
|
2019 * and prefix ID above the region. */
|
|
2020 c = getc(fd); /* <flags> */
|
|
2021 if (c & WF_REGION)
|
|
2022 c = (getc(fd) << 8) + c; /* <region> */
|
|
2023 if (c & WF_PFX)
|
|
2024 c = (getc(fd) << 16) + c; /* <prefixID> */
|
|
2025 }
|
|
2026
|
300
|
2027 idxs[idx] = c;
|
|
2028 c = 0;
|
|
2029 }
|
|
2030 else /* c == BY_INDEX */
|
|
2031 {
|
|
2032 /* <nodeidx> */
|
|
2033 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
|
|
2034 if (n < 0 || n >= maxidx)
|
|
2035 return -2;
|
|
2036 idxs[idx] = n + SHARED_MASK;
|
|
2037 c = getc(fd); /* <xbyte> */
|
|
2038 }
|
|
2039 }
|
|
2040 byts[idx++] = c;
|
236
|
2041 }
|
|
2042
|
300
|
2043 /* Recursively read the children for non-shared siblings.
|
|
2044 * Skip the end-of-word ones (zero byte value) and the shared ones (and
|
|
2045 * remove SHARED_MASK) */
|
|
2046 for (i = 1; i <= len; ++i)
|
|
2047 if (byts[startidx + i] != 0)
|
|
2048 {
|
|
2049 if (idxs[startidx + i] & SHARED_MASK)
|
|
2050 idxs[startidx + i] &= ~SHARED_MASK;
|
|
2051 else
|
|
2052 {
|
|
2053 idxs[startidx + i] = idx;
|
339
|
2054 idx = read_tree(fd, byts, idxs, maxidx, idx,
|
|
2055 prefixtree, maxprefcondnr);
|
300
|
2056 if (idx < 0)
|
|
2057 break;
|
|
2058 }
|
|
2059 }
|
236
|
2060
|
300
|
2061 return idx;
|
236
|
2062 }
|
|
2063
|
|
2064 /*
|
|
2065 * Parse 'spelllang' and set buf->b_langp accordingly.
|
|
2066 * Returns an error message or NULL.
|
|
2067 */
|
|
2068 char_u *
|
|
2069 did_set_spelllang(buf)
|
|
2070 buf_T *buf;
|
|
2071 {
|
|
2072 garray_T ga;
|
|
2073 char_u *lang;
|
|
2074 char_u *e;
|
|
2075 char_u *region;
|
|
2076 int region_mask;
|
|
2077 slang_T *lp;
|
|
2078 int c;
|
|
2079 char_u lbuf[MAXWLEN + 1];
|
323
|
2080 char_u spf_name[MAXPATHL];
|
|
2081 int did_spf = FALSE;
|
236
|
2082
|
|
2083 ga_init2(&ga, sizeof(langp_T), 2);
|
|
2084
|
323
|
2085 /* Get the name of the .spl file associated with 'spellfile'. */
|
|
2086 if (*buf->b_p_spf == NUL)
|
|
2087 did_spf = TRUE;
|
|
2088 else
|
|
2089 vim_snprintf((char *)spf_name, sizeof(spf_name), "%s.spl",
|
|
2090 buf->b_p_spf);
|
|
2091
|
236
|
2092 /* loop over comma separated languages. */
|
|
2093 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
|
|
2094 {
|
|
2095 e = vim_strchr(lang, ',');
|
|
2096 if (e == NULL)
|
|
2097 e = lang + STRLEN(lang);
|
240
|
2098 region = NULL;
|
236
|
2099 if (e > lang + 2)
|
|
2100 {
|
|
2101 if (e - lang >= MAXWLEN)
|
|
2102 {
|
|
2103 ga_clear(&ga);
|
|
2104 return e_invarg;
|
|
2105 }
|
|
2106 if (lang[2] == '_')
|
|
2107 region = lang + 3;
|
|
2108 }
|
|
2109
|
307
|
2110 /* Check if we loaded this language before. */
|
236
|
2111 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
2112 if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
|
2113 break;
|
|
2114
|
|
2115 if (lp == NULL)
|
|
2116 {
|
|
2117 /* Not found, load the language. */
|
323
|
2118 vim_strncpy(lbuf, lang, e - lang);
|
236
|
2119 if (region != NULL)
|
|
2120 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
|
307
|
2121 spell_load_lang(lbuf);
|
236
|
2122 }
|
|
2123
|
307
|
2124 /*
|
|
2125 * Loop over the languages, there can be several files for each.
|
|
2126 */
|
|
2127 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
2128 if (STRNICMP(lp->sl_name, lang, 2) == 0)
|
236
|
2129 {
|
316
|
2130 region_mask = REGION_ALL;
|
|
2131 if (region != NULL)
|
236
|
2132 {
|
307
|
2133 /* find region in sl_regions */
|
|
2134 c = find_region(lp->sl_regions, region);
|
|
2135 if (c == REGION_ALL)
|
|
2136 {
|
316
|
2137 if (!lp->sl_add)
|
|
2138 {
|
|
2139 c = *e;
|
|
2140 *e = NUL;
|
|
2141 smsg((char_u *)_("Warning: region %s not supported"),
|
307
|
2142 lang);
|
316
|
2143 *e = c;
|
|
2144 }
|
307
|
2145 }
|
|
2146 else
|
|
2147 region_mask = 1 << c;
|
236
|
2148 }
|
307
|
2149
|
|
2150 if (ga_grow(&ga, 1) == FAIL)
|
|
2151 {
|
|
2152 ga_clear(&ga);
|
|
2153 return e_outofmem;
|
|
2154 }
|
|
2155 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
|
|
2156 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
|
|
2157 ++ga.ga_len;
|
323
|
2158
|
|
2159 /* Check if this is the 'spellfile' spell file. */
|
|
2160 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
|
|
2161 did_spf = TRUE;
|
236
|
2162 }
|
|
2163
|
|
2164 if (*e == ',')
|
|
2165 ++e;
|
|
2166 }
|
|
2167
|
323
|
2168 /*
|
|
2169 * Make sure the 'spellfile' file is loaded. It may be in 'runtimepath',
|
|
2170 * then it's probably loaded above already. Otherwise load it here.
|
|
2171 */
|
|
2172 if (!did_spf)
|
|
2173 {
|
|
2174 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
2175 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
|
|
2176 break;
|
|
2177 if (lp == NULL)
|
|
2178 {
|
|
2179 vim_strncpy(lbuf, gettail(spf_name), 2);
|
|
2180 lp = spell_load_file(spf_name, lbuf, NULL, TRUE);
|
|
2181 }
|
|
2182 if (lp != NULL && ga_grow(&ga, 1) == OK)
|
|
2183 {
|
|
2184 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
|
|
2185 LANGP_ENTRY(ga, ga.ga_len)->lp_region = REGION_ALL;
|
|
2186 ++ga.ga_len;
|
|
2187 }
|
|
2188 }
|
|
2189
|
236
|
2190 /* Add a NULL entry to mark the end of the list. */
|
|
2191 if (ga_grow(&ga, 1) == FAIL)
|
|
2192 {
|
|
2193 ga_clear(&ga);
|
|
2194 return e_outofmem;
|
|
2195 }
|
|
2196 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
|
|
2197 ++ga.ga_len;
|
|
2198
|
|
2199 /* Everything is fine, store the new b_langp value. */
|
|
2200 ga_clear(&buf->b_langp);
|
|
2201 buf->b_langp = ga;
|
|
2202
|
|
2203 return NULL;
|
|
2204 }
|
|
2205
|
|
2206 /*
|
|
2207 * Find the region "region[2]" in "rp" (points to "sl_regions").
|
|
2208 * Each region is simply stored as the two characters of it's name.
|
|
2209 * Returns the index if found, REGION_ALL if not found.
|
|
2210 */
|
|
2211 static int
|
|
2212 find_region(rp, region)
|
|
2213 char_u *rp;
|
|
2214 char_u *region;
|
|
2215 {
|
|
2216 int i;
|
|
2217
|
|
2218 for (i = 0; ; i += 2)
|
|
2219 {
|
|
2220 if (rp[i] == NUL)
|
|
2221 return REGION_ALL;
|
|
2222 if (rp[i] == region[0] && rp[i + 1] == region[1])
|
|
2223 break;
|
|
2224 }
|
|
2225 return i / 2;
|
|
2226 }
|
|
2227
|
|
2228 /*
|
323
|
2229 * Return case type of word:
|
236
|
2230 * w word 0
|
300
|
2231 * Word WF_ONECAP
|
|
2232 * W WORD WF_ALLCAP
|
|
2233 * WoRd wOrd WF_KEEPCAP
|
236
|
2234 */
|
|
2235 static int
|
|
2236 captype(word, end)
|
|
2237 char_u *word;
|
323
|
2238 char_u *end; /* When NULL use up to NUL byte. */
|
236
|
2239 {
|
|
2240 char_u *p;
|
|
2241 int c;
|
|
2242 int firstcap;
|
|
2243 int allcap;
|
|
2244 int past_second = FALSE; /* past second word char */
|
|
2245
|
|
2246 /* find first letter */
|
307
|
2247 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
|
323
|
2248 if (end == NULL ? *p == NUL : p >= end)
|
236
|
2249 return 0; /* only non-word characters, illegal word */
|
|
2250 #ifdef FEAT_MBYTE
|
310
|
2251 if (has_mbyte)
|
|
2252 c = mb_ptr2char_adv(&p);
|
|
2253 else
|
236
|
2254 #endif
|
310
|
2255 c = *p++;
|
324
|
2256 firstcap = allcap = SPELL_ISUPPER(c);
|
236
|
2257
|
|
2258 /*
|
|
2259 * Need to check all letters to find a word with mixed upper/lower.
|
|
2260 * But a word with an upper char only at start is a ONECAP.
|
|
2261 */
|
323
|
2262 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
|
307
|
2263 if (SPELL_ISWORDP(p))
|
236
|
2264 {
|
|
2265 #ifdef FEAT_MBYTE
|
|
2266 c = mb_ptr2char(p);
|
|
2267 #else
|
|
2268 c = *p;
|
|
2269 #endif
|
324
|
2270 if (!SPELL_ISUPPER(c))
|
236
|
2271 {
|
|
2272 /* UUl -> KEEPCAP */
|
|
2273 if (past_second && allcap)
|
300
|
2274 return WF_KEEPCAP;
|
236
|
2275 allcap = FALSE;
|
|
2276 }
|
|
2277 else if (!allcap)
|
|
2278 /* UlU -> KEEPCAP */
|
300
|
2279 return WF_KEEPCAP;
|
236
|
2280 past_second = TRUE;
|
|
2281 }
|
|
2282
|
|
2283 if (allcap)
|
300
|
2284 return WF_ALLCAP;
|
236
|
2285 if (firstcap)
|
300
|
2286 return WF_ONECAP;
|
236
|
2287 return 0;
|
|
2288 }
|
|
2289
|
|
2290 # if defined(FEAT_MBYTE) || defined(PROTO)
|
|
2291 /*
|
|
2292 * Clear all spelling tables and reload them.
|
307
|
2293 * Used after 'encoding' is set and when ":mkspell" was used.
|
236
|
2294 */
|
|
2295 void
|
|
2296 spell_reload()
|
|
2297 {
|
|
2298 buf_T *buf;
|
|
2299 slang_T *lp;
|
316
|
2300 win_T *wp;
|
236
|
2301
|
307
|
2302 /* Initialize the table for SPELL_ISWORDP(). */
|
236
|
2303 init_spell_chartab();
|
|
2304
|
|
2305 /* Unload all allocated memory. */
|
|
2306 while (first_lang != NULL)
|
|
2307 {
|
|
2308 lp = first_lang;
|
|
2309 first_lang = lp->sl_next;
|
|
2310 slang_free(lp);
|
|
2311 }
|
|
2312
|
|
2313 /* Go through all buffers and handle 'spelllang'. */
|
|
2314 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
|
|
2315 {
|
|
2316 ga_clear(&buf->b_langp);
|
316
|
2317
|
|
2318 /* Only load the wordlists when 'spelllang' is set and there is a
|
|
2319 * window for this buffer in which 'spell' is set. */
|
236
|
2320 if (*buf->b_p_spl != NUL)
|
316
|
2321 {
|
|
2322 FOR_ALL_WINDOWS(wp)
|
|
2323 if (wp->w_buffer == buf && wp->w_p_spell)
|
|
2324 {
|
|
2325 (void)did_set_spelllang(buf);
|
|
2326 # ifdef FEAT_WINDOWS
|
|
2327 break;
|
|
2328 # endif
|
|
2329 }
|
|
2330 }
|
236
|
2331 }
|
|
2332 }
|
|
2333 # endif
|
|
2334
|
310
|
2335 /*
|
|
2336 * Reload the spell file "fname" if it's loaded.
|
|
2337 */
|
|
2338 static void
|
323
|
2339 spell_reload_one(fname, added_word)
|
310
|
2340 char_u *fname;
|
323
|
2341 int added_word; /* invoked through "zg" */
|
310
|
2342 {
|
|
2343 slang_T *lp;
|
323
|
2344 int didit = FALSE;
|
310
|
2345
|
|
2346 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
|
|
2347 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
|
|
2348 {
|
|
2349 slang_clear(lp);
|
323
|
2350 (void)spell_load_file(fname, NULL, lp, FALSE);
|
310
|
2351 redraw_all_later(NOT_VALID);
|
323
|
2352 didit = TRUE;
|
310
|
2353 }
|
323
|
2354
|
|
2355 /* When "zg" was used and the file wasn't loaded yet, should redo
|
|
2356 * 'spelllang' to get it loaded. */
|
|
2357 if (added_word && !didit)
|
|
2358 did_set_spelllang(curbuf);
|
310
|
2359 }
|
|
2360
|
|
2361
|
236
|
2362 /*
|
|
2363 * Functions for ":mkspell".
|
|
2364 */
|
|
2365
|
300
|
2366 #define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
|
236
|
2367 and .dic file. */
|
|
2368 /*
|
|
2369 * Main structure to store the contents of a ".aff" file.
|
|
2370 */
|
|
2371 typedef struct afffile_S
|
|
2372 {
|
|
2373 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
|
310
|
2374 int af_rar; /* RAR ID for rare word */
|
|
2375 int af_kep; /* KEP ID for keep-case word */
|
346
|
2376 int af_bad; /* BAD ID for banned word */
|
339
|
2377 int af_pfxpostpone; /* postpone prefixes without chop string */
|
236
|
2378 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
|
|
2379 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
|
|
2380 } afffile_T;
|
|
2381
|
|
2382 typedef struct affentry_S affentry_T;
|
|
2383 /* Affix entry from ".aff" file. Used for prefixes and suffixes. */
|
|
2384 struct affentry_S
|
|
2385 {
|
|
2386 affentry_T *ae_next; /* next affix with same name/number */
|
|
2387 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
|
|
2388 char_u *ae_add; /* text to add to basic word (can be NULL) */
|
|
2389 char_u *ae_cond; /* condition (NULL for ".") */
|
|
2390 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
|
300
|
2391 };
|
|
2392
|
|
2393 /* Affix header from ".aff" file. Used for af_pref and af_suff. */
|
|
2394 typedef struct affheader_S
|
|
2395 {
|
|
2396 char_u ah_key[2]; /* key for hashtable == name of affix entry */
|
339
|
2397 int ah_newID; /* prefix ID after renumbering */
|
300
|
2398 int ah_combine; /* suffix may combine with prefix */
|
|
2399 affentry_T *ah_first; /* first affix entry */
|
|
2400 } affheader_T;
|
|
2401
|
|
2402 #define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
|
|
2403
|
|
2404 /*
|
|
2405 * Structure that is used to store the items in the word tree. This avoids
|
|
2406 * the need to keep track of each allocated thing, it's freed all at once
|
|
2407 * after ":mkspell" is done.
|
|
2408 */
|
|
2409 #define SBLOCKSIZE 16000 /* size of sb_data */
|
|
2410 typedef struct sblock_S sblock_T;
|
|
2411 struct sblock_S
|
|
2412 {
|
|
2413 sblock_T *sb_next; /* next block in list */
|
|
2414 int sb_used; /* nr of bytes already in use */
|
|
2415 char_u sb_data[1]; /* data, actually longer */
|
236
|
2416 };
|
|
2417
|
|
2418 /*
|
300
|
2419 * A node in the tree.
|
236
|
2420 */
|
300
|
2421 typedef struct wordnode_S wordnode_T;
|
|
2422 struct wordnode_S
|
236
|
2423 {
|
346
|
2424 union /* shared to save space */
|
|
2425 {
|
|
2426 char_u hashkey[6]; /* room for the hash key */
|
|
2427 int index; /* index in written nodes (valid after first
|
|
2428 round) */
|
|
2429 } wn_u1;
|
|
2430 union /* shared to save space */
|
|
2431 {
|
|
2432 wordnode_T *next; /* next node with same hash key */
|
|
2433 wordnode_T *wnode; /* parent node that will write this node */
|
|
2434 } wn_u2;
|
300
|
2435 wordnode_T *wn_child; /* child (next byte in word) */
|
|
2436 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
|
|
2437 always sorted) */
|
|
2438 char_u wn_byte; /* Byte for this node. NUL for word end */
|
|
2439 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
|
339
|
2440 short wn_region; /* when wn_byte is NUL: region mask; for
|
|
2441 PREFIXTREE it's the prefcondnr */
|
|
2442 char_u wn_prefixID; /* supported/required prefix ID or 0 */
|
236
|
2443 };
|
|
2444
|
300
|
2445 #define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
|
236
|
2446
|
300
|
2447 /*
|
|
2448 * Info used while reading the spell files.
|
|
2449 */
|
|
2450 typedef struct spellinfo_S
|
249
|
2451 {
|
300
|
2452 wordnode_T *si_foldroot; /* tree with case-folded words */
|
334
|
2453 long si_foldwcount; /* nr of words in si_foldroot */
|
300
|
2454 wordnode_T *si_keeproot; /* tree with keep-case words */
|
334
|
2455 long si_keepwcount; /* nr of words in si_keeproot */
|
339
|
2456 wordnode_T *si_prefroot; /* tree with postponed prefixes */
|
300
|
2457 sblock_T *si_blocks; /* memory blocks used */
|
|
2458 int si_ascii; /* handling only ASCII words */
|
310
|
2459 int si_add; /* addition file */
|
300
|
2460 int si_region; /* region mask */
|
|
2461 vimconv_T si_conv; /* for conversion to 'encoding' */
|
302
|
2462 int si_memtot; /* runtime memory used */
|
310
|
2463 int si_verbose; /* verbose messages */
|
316
|
2464 int si_region_count; /* number of regions supported (1 when there
|
|
2465 are no regions) */
|
|
2466 char_u si_region_name[16]; /* region names (if count > 1) */
|
323
|
2467
|
|
2468 garray_T si_rep; /* list of fromto_T entries from REP lines */
|
|
2469 garray_T si_sal; /* list of fromto_T entries from SAL lines */
|
|
2470 int si_followup; /* soundsalike: ? */
|
|
2471 int si_collapse; /* soundsalike: ? */
|
|
2472 int si_rem_accents; /* soundsalike: remove accents */
|
|
2473 garray_T si_map; /* MAP info concatenated */
|
339
|
2474 garray_T si_prefcond; /* table with conditions for postponed
|
|
2475 * prefixes, each stored as a string */
|
|
2476 int si_newID; /* current value for ah_newID */
|
300
|
2477 } spellinfo_T;
|
249
|
2478
|
300
|
2479 static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
|
339
|
2480 static int str_equal __ARGS((char_u *s1, char_u *s2));
|
323
|
2481 static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
|
|
2482 static int sal_to_bool __ARGS((char_u *s));
|
240
|
2483 static int has_non_ascii __ARGS((char_u *s));
|
300
|
2484 static void spell_free_aff __ARGS((afffile_T *aff));
|
|
2485 static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
|
339
|
2486 static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
|
|
2487 static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist));
|
300
|
2488 static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
|
|
2489 static void *getroom __ARGS((sblock_T **blp, size_t len));
|
|
2490 static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
|
|
2491 static void free_blocks __ARGS((sblock_T *bl));
|
|
2492 static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
|
339
|
2493 static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
|
|
2494 static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, int prefixID, sblock_T **blp));
|
310
|
2495 static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
|
300
|
2496 static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
|
|
2497 static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
|
316
|
2498 static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
|
346
|
2499 static void clear_node __ARGS((wordnode_T *node));
|
|
2500 static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
|
323
|
2501 static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
|
310
|
2502 static void init_spellfile __ARGS((void));
|
236
|
2503
|
|
2504 /*
|
323
|
2505 * Read the affix file "fname".
|
316
|
2506 * Returns an afffile_T, NULL for complete failure.
|
236
|
2507 */
|
|
2508 static afffile_T *
|
300
|
2509 spell_read_aff(fname, spin)
|
236
|
2510 char_u *fname;
|
300
|
2511 spellinfo_T *spin;
|
236
|
2512 {
|
|
2513 FILE *fd;
|
|
2514 afffile_T *aff;
|
|
2515 char_u rline[MAXLINELEN];
|
|
2516 char_u *line;
|
|
2517 char_u *pc = NULL;
|
334
|
2518 #define MAXITEMCNT 7
|
|
2519 char_u *(items[MAXITEMCNT]);
|
236
|
2520 int itemcnt;
|
|
2521 char_u *p;
|
|
2522 int lnum = 0;
|
|
2523 affheader_T *cur_aff = NULL;
|
|
2524 int aff_todo = 0;
|
|
2525 hashtab_T *tp;
|
255
|
2526 char_u *low = NULL;
|
|
2527 char_u *fol = NULL;
|
|
2528 char_u *upp = NULL;
|
307
|
2529 static char *e_affname = N_("Affix name too long in %s line %d: %s");
|
323
|
2530 int do_rep;
|
|
2531 int do_sal;
|
|
2532 int do_map;
|
|
2533 int found_map = FALSE;
|
324
|
2534 hashitem_T *hi;
|
236
|
2535
|
300
|
2536 /*
|
|
2537 * Open the file.
|
|
2538 */
|
310
|
2539 fd = mch_fopen((char *)fname, "r");
|
236
|
2540 if (fd == NULL)
|
|
2541 {
|
|
2542 EMSG2(_(e_notopen), fname);
|
|
2543 return NULL;
|
|
2544 }
|
|
2545
|
310
|
2546 if (spin->si_verbose || p_verbose > 2)
|
|
2547 {
|
|
2548 if (!spin->si_verbose)
|
|
2549 verbose_enter();
|
|
2550 smsg((char_u *)_("Reading affix file %s..."), fname);
|
|
2551 out_flush();
|
|
2552 if (!spin->si_verbose)
|
|
2553 verbose_leave();
|
|
2554 }
|
236
|
2555
|
323
|
2556 /* Only do REP lines when not done in another .aff file already. */
|
|
2557 do_rep = spin->si_rep.ga_len == 0;
|
|
2558
|
|
2559 /* Only do SAL lines when not done in another .aff file already. */
|
|
2560 do_sal = spin->si_sal.ga_len == 0;
|
|
2561
|
|
2562 /* Only do MAP lines when not done in another .aff file already. */
|
|
2563 do_map = spin->si_map.ga_len == 0;
|
|
2564
|
300
|
2565 /*
|
|
2566 * Allocate and init the afffile_T structure.
|
|
2567 */
|
|
2568 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
|
236
|
2569 if (aff == NULL)
|
|
2570 return NULL;
|
|
2571 hash_init(&aff->af_pref);
|
|
2572 hash_init(&aff->af_suff);
|
|
2573
|
|
2574 /*
|
|
2575 * Read all the lines in the file one by one.
|
|
2576 */
|
255
|
2577 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
236
|
2578 {
|
255
|
2579 line_breakcheck();
|
236
|
2580 ++lnum;
|
|
2581
|
|
2582 /* Skip comment lines. */
|
|
2583 if (*rline == '#')
|
|
2584 continue;
|
|
2585
|
|
2586 /* Convert from "SET" to 'encoding' when needed. */
|
|
2587 vim_free(pc);
|
310
|
2588 #ifdef FEAT_MBYTE
|
300
|
2589 if (spin->si_conv.vc_type != CONV_NONE)
|
236
|
2590 {
|
300
|
2591 pc = string_convert(&spin->si_conv, rline, NULL);
|
255
|
2592 if (pc == NULL)
|
|
2593 {
|
|
2594 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
2595 fname, lnum, rline);
|
|
2596 continue;
|
|
2597 }
|
236
|
2598 line = pc;
|
|
2599 }
|
|
2600 else
|
310
|
2601 #endif
|
236
|
2602 {
|
|
2603 pc = NULL;
|
|
2604 line = rline;
|
|
2605 }
|
|
2606
|
|
2607 /* Split the line up in white separated items. Put a NUL after each
|
|
2608 * item. */
|
|
2609 itemcnt = 0;
|
|
2610 for (p = line; ; )
|
|
2611 {
|
|
2612 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
|
|
2613 ++p;
|
|
2614 if (*p == NUL)
|
|
2615 break;
|
334
|
2616 if (itemcnt == MAXITEMCNT) /* too many items */
|
300
|
2617 break;
|
236
|
2618 items[itemcnt++] = p;
|
300
|
2619 while (*p > ' ') /* skip until white space or CR/NL */
|
236
|
2620 ++p;
|
|
2621 if (*p == NUL)
|
|
2622 break;
|
|
2623 *p++ = NUL;
|
|
2624 }
|
|
2625
|
|
2626 /* Handle non-empty lines. */
|
|
2627 if (itemcnt > 0)
|
|
2628 {
|
|
2629 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
|
|
2630 && aff->af_enc == NULL)
|
|
2631 {
|
310
|
2632 #ifdef FEAT_MBYTE
|
300
|
2633 /* Setup for conversion from "ENC" to 'encoding'. */
|
|
2634 aff->af_enc = enc_canonize(items[1]);
|
|
2635 if (aff->af_enc != NULL && !spin->si_ascii
|
|
2636 && convert_setup(&spin->si_conv, aff->af_enc,
|
|
2637 p_enc) == FAIL)
|
|
2638 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
|
2639 fname, aff->af_enc, p_enc);
|
310
|
2640 #else
|
|
2641 smsg((char_u *)_("Conversion in %s not supported"), fname);
|
|
2642 #endif
|
236
|
2643 }
|
302
|
2644 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
|
|
2645 {
|
323
|
2646 /* ignored, we always split */
|
302
|
2647 }
|
323
|
2648 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
|
300
|
2649 {
|
323
|
2650 /* ignored, we look in the tree for what chars may appear */
|
300
|
2651 }
|
307
|
2652 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
|
|
2653 && aff->af_rar == 0)
|
|
2654 {
|
|
2655 aff->af_rar = items[1][0];
|
|
2656 if (items[1][1] != NUL)
|
|
2657 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
2658 }
|
310
|
2659 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
|
|
2660 && aff->af_kep == 0)
|
307
|
2661 {
|
310
|
2662 aff->af_kep = items[1][0];
|
307
|
2663 if (items[1][1] != NUL)
|
|
2664 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
2665 }
|
346
|
2666 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
|
|
2667 && aff->af_bad == 0)
|
|
2668 {
|
|
2669 aff->af_bad = items[1][0];
|
|
2670 if (items[1][1] != NUL)
|
|
2671 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
2672 }
|
339
|
2673 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
|
|
2674 {
|
|
2675 aff->af_pfxpostpone = TRUE;
|
|
2676 }
|
236
|
2677 else if ((STRCMP(items[0], "PFX") == 0
|
|
2678 || STRCMP(items[0], "SFX") == 0)
|
|
2679 && aff_todo == 0
|
334
|
2680 && itemcnt >= 4)
|
236
|
2681 {
|
334
|
2682 /* Myspell allows extra text after the item, but that might
|
|
2683 * mean mistakes go unnoticed. Require a comment-starter. */
|
|
2684 if (itemcnt > 4 && *items[4] != '#')
|
|
2685 smsg((char_u *)_("Trailing text in %s line %d: %s"),
|
|
2686 fname, lnum, items[4]);
|
|
2687
|
236
|
2688 /* New affix letter. */
|
300
|
2689 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
|
|
2690 sizeof(affheader_T));
|
236
|
2691 if (cur_aff == NULL)
|
|
2692 break;
|
339
|
2693 cur_aff->ah_key[0] = *items[1]; /* TODO: multi-byte? */
|
236
|
2694 cur_aff->ah_key[1] = NUL;
|
|
2695 if (items[1][1] != NUL)
|
307
|
2696 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
236
|
2697 if (*items[2] == 'Y')
|
|
2698 cur_aff->ah_combine = TRUE;
|
300
|
2699 else if (*items[2] != 'N')
|
236
|
2700 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
|
|
2701 fname, lnum, items[2]);
|
339
|
2702
|
236
|
2703 if (*items[0] == 'P')
|
339
|
2704 {
|
236
|
2705 tp = &aff->af_pref;
|
339
|
2706 /* Use a new number in the .spl file later, to be able to
|
|
2707 * handle multiple .aff files. */
|
|
2708 if (aff->af_pfxpostpone)
|
344
|
2709 cur_aff->ah_newID = ++spin->si_newID;
|
339
|
2710 }
|
236
|
2711 else
|
|
2712 tp = &aff->af_suff;
|
300
|
2713 aff_todo = atoi((char *)items[3]);
|
324
|
2714 hi = hash_find(tp, cur_aff->ah_key);
|
|
2715 if (!HASHITEM_EMPTY(hi))
|
300
|
2716 {
|
236
|
2717 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
|
|
2718 fname, lnum, items[1]);
|
300
|
2719 aff_todo = 0;
|
|
2720 }
|
236
|
2721 else
|
|
2722 hash_add(tp, cur_aff->ah_key);
|
|
2723 }
|
|
2724 else if ((STRCMP(items[0], "PFX") == 0
|
|
2725 || STRCMP(items[0], "SFX") == 0)
|
|
2726 && aff_todo > 0
|
|
2727 && STRCMP(cur_aff->ah_key, items[1]) == 0
|
334
|
2728 && itemcnt >= 5)
|
236
|
2729 {
|
|
2730 affentry_T *aff_entry;
|
|
2731
|
334
|
2732 /* Myspell allows extra text after the item, but that might
|
|
2733 * mean mistakes go unnoticed. Require a comment-starter. */
|
|
2734 if (itemcnt > 5 && *items[5] != '#')
|
|
2735 smsg((char_u *)_("Trailing text in %s line %d: %s"),
|
|
2736 fname, lnum, items[5]);
|
|
2737
|
236
|
2738 /* New item for an affix letter. */
|
|
2739 --aff_todo;
|
300
|
2740 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
|
|
2741 sizeof(affentry_T));
|
236
|
2742 if (aff_entry == NULL)
|
|
2743 break;
|
240
|
2744
|
236
|
2745 if (STRCMP(items[2], "0") != 0)
|
300
|
2746 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
|
|
2747 items[2]);
|
236
|
2748 if (STRCMP(items[3], "0") != 0)
|
300
|
2749 aff_entry->ae_add = getroom_save(&spin->si_blocks,
|
|
2750 items[3]);
|
236
|
2751
|
300
|
2752 /* Don't use an affix entry with non-ASCII characters when
|
|
2753 * "spin->si_ascii" is TRUE. */
|
|
2754 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
|
240
|
2755 || has_non_ascii(aff_entry->ae_add)))
|
|
2756 {
|
|
2757 aff_entry->ae_next = cur_aff->ah_first;
|
|
2758 cur_aff->ah_first = aff_entry;
|
300
|
2759
|
|
2760 if (STRCMP(items[4], ".") != 0)
|
|
2761 {
|
|
2762 char_u buf[MAXLINELEN];
|
|
2763
|
|
2764 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
|
|
2765 items[4]);
|
|
2766 if (*items[0] == 'P')
|
|
2767 sprintf((char *)buf, "^%s", items[4]);
|
|
2768 else
|
|
2769 sprintf((char *)buf, "%s$", items[4]);
|
|
2770 aff_entry->ae_prog = vim_regcomp(buf,
|
|
2771 RE_MAGIC + RE_STRING);
|
|
2772 }
|
339
|
2773
|
|
2774 /* For postponed prefixes we need an entry in si_prefcond
|
|
2775 * for the condition. Use an existing one if possible. */
|
|
2776 if (*items[0] == 'P' && aff->af_pfxpostpone
|
|
2777 && aff_entry->ae_chop == NULL)
|
|
2778 {
|
|
2779 int idx;
|
|
2780 char_u **pp;
|
|
2781
|
|
2782 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
|
|
2783 --idx)
|
|
2784 {
|
|
2785 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
|
|
2786 if (str_equal(p, aff_entry->ae_cond))
|
|
2787 break;
|
|
2788 }
|
|
2789 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
|
|
2790 {
|
|
2791 /* Not found, add a new condition. */
|
|
2792 idx = spin->si_prefcond.ga_len++;
|
|
2793 pp = ((char_u **)spin->si_prefcond.ga_data) + idx;
|
|
2794 if (aff_entry->ae_cond == NULL)
|
|
2795 *pp = NULL;
|
|
2796 else
|
|
2797 *pp = getroom_save(&spin->si_blocks,
|
|
2798 aff_entry->ae_cond);
|
|
2799 }
|
|
2800
|
|
2801 /* Add the prefix to the prefix tree. */
|
|
2802 if (aff_entry->ae_add == NULL)
|
|
2803 p = (char_u *)"";
|
|
2804 else
|
|
2805 p = aff_entry->ae_add;
|
|
2806 tree_add_word(p, spin->si_prefroot, -1, idx,
|
|
2807 cur_aff->ah_newID, &spin->si_blocks);
|
|
2808 }
|
240
|
2809 }
|
236
|
2810 }
|
255
|
2811 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
|
|
2812 {
|
|
2813 if (fol != NULL)
|
|
2814 smsg((char_u *)_("Duplicate FOL in %s line %d"),
|
|
2815 fname, lnum);
|
|
2816 else
|
|
2817 fol = vim_strsave(items[1]);
|
|
2818 }
|
|
2819 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
|
|
2820 {
|
|
2821 if (low != NULL)
|
|
2822 smsg((char_u *)_("Duplicate LOW in %s line %d"),
|
|
2823 fname, lnum);
|
|
2824 else
|
|
2825 low = vim_strsave(items[1]);
|
|
2826 }
|
|
2827 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
|
|
2828 {
|
|
2829 if (upp != NULL)
|
|
2830 smsg((char_u *)_("Duplicate UPP in %s line %d"),
|
|
2831 fname, lnum);
|
|
2832 else
|
|
2833 upp = vim_strsave(items[1]);
|
|
2834 }
|
236
|
2835 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
|
323
|
2836 {
|
236
|
2837 /* Ignore REP count */;
|
323
|
2838 if (!isdigit(*items[1]))
|
|
2839 smsg((char_u *)_("Expected REP count in %s line %d"),
|
|
2840 fname, lnum);
|
|
2841 }
|
236
|
2842 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
|
|
2843 {
|
|
2844 /* REP item */
|
323
|
2845 if (do_rep)
|
|
2846 add_fromto(spin, &spin->si_rep, items[1], items[2]);
|
|
2847 }
|
|
2848 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
|
|
2849 {
|
|
2850 /* MAP item or count */
|
|
2851 if (!found_map)
|
|
2852 {
|
|
2853 /* First line contains the count. */
|
|
2854 found_map = TRUE;
|
|
2855 if (!isdigit(*items[1]))
|
|
2856 smsg((char_u *)_("Expected MAP count in %s line %d"),
|
|
2857 fname, lnum);
|
|
2858 }
|
|
2859 else if (do_map)
|
|
2860 {
|
346
|
2861 int c;
|
|
2862
|
|
2863 /* Check that every character appears only once. */
|
|
2864 for (p = items[1]; *p != NUL; )
|
|
2865 {
|
|
2866 #ifdef FEAT_MBYTE
|
|
2867 c = mb_ptr2char_adv(&p);
|
|
2868 #else
|
|
2869 c = *p++;
|
|
2870 #endif
|
|
2871 if ((spin->si_map.ga_len > 0
|
|
2872 && vim_strchr(spin->si_map.ga_data, c)
|
|
2873 != NULL)
|
|
2874 || vim_strchr(p, c) != NULL)
|
|
2875 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
|
|
2876 fname, lnum);
|
|
2877 }
|
|
2878
|
323
|
2879 /* We simply concatenate all the MAP strings, separated by
|
|
2880 * slashes. */
|
|
2881 ga_concat(&spin->si_map, items[1]);
|
|
2882 ga_append(&spin->si_map, '/');
|
|
2883 }
|
|
2884 }
|
|
2885 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
|
|
2886 {
|
|
2887 if (do_sal)
|
|
2888 {
|
|
2889 /* SAL item (sounds-a-like)
|
|
2890 * Either one of the known keys or a from-to pair. */
|
|
2891 if (STRCMP(items[1], "followup") == 0)
|
|
2892 spin->si_followup = sal_to_bool(items[2]);
|
|
2893 else if (STRCMP(items[1], "collapse_result") == 0)
|
|
2894 spin->si_collapse = sal_to_bool(items[2]);
|
|
2895 else if (STRCMP(items[1], "remove_accents") == 0)
|
|
2896 spin->si_rem_accents = sal_to_bool(items[2]);
|
|
2897 else
|
|
2898 /* when "to" is "_" it means empty */
|
|
2899 add_fromto(spin, &spin->si_sal, items[1],
|
|
2900 STRCMP(items[2], "_") == 0 ? (char_u *)""
|
|
2901 : items[2]);
|
|
2902 }
|
236
|
2903 }
|
300
|
2904 else
|
236
|
2905 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
|
|
2906 fname, lnum, items[0]);
|
|
2907 }
|
|
2908 }
|
|
2909
|
255
|
2910 if (fol != NULL || low != NULL || upp != NULL)
|
|
2911 {
|
316
|
2912 /*
|
|
2913 * Don't write a word table for an ASCII file, so that we don't check
|
|
2914 * for conflicts with a word table that matches 'encoding'.
|
324
|
2915 * Don't write one for utf-8 either, we use utf_*() and
|
316
|
2916 * mb_get_class(), the list of chars in the file will be incomplete.
|
|
2917 */
|
|
2918 if (!spin->si_ascii
|
|
2919 #ifdef FEAT_MBYTE
|
|
2920 && !enc_utf8
|
|
2921 #endif
|
|
2922 )
|
260
|
2923 {
|
|
2924 if (fol == NULL || low == NULL || upp == NULL)
|
|
2925 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
|
|
2926 else
|
316
|
2927 (void)set_spell_chartab(fol, low, upp);
|
260
|
2928 }
|
255
|
2929
|
|
2930 vim_free(fol);
|
|
2931 vim_free(low);
|
|
2932 vim_free(upp);
|
|
2933 }
|
|
2934
|
236
|
2935 vim_free(pc);
|
|
2936 fclose(fd);
|
|
2937 return aff;
|
|
2938 }
|
|
2939
|
|
2940 /*
|
339
|
2941 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
|
|
2942 * NULL as equal.
|
|
2943 */
|
|
2944 static int
|
|
2945 str_equal(s1, s2)
|
|
2946 char_u *s1;
|
|
2947 char_u *s2;
|
|
2948 {
|
|
2949 if (s1 == NULL || s2 == NULL)
|
|
2950 return s1 == s2;
|
|
2951 return STRCMP(s1, s2) == 0;
|
|
2952 }
|
|
2953
|
|
2954 /*
|
323
|
2955 * Add a from-to item to "gap". Used for REP and SAL items.
|
|
2956 * They are stored case-folded.
|
|
2957 */
|
|
2958 static void
|
|
2959 add_fromto(spin, gap, from, to)
|
|
2960 spellinfo_T *spin;
|
|
2961 garray_T *gap;
|
|
2962 char_u *from;
|
|
2963 char_u *to;
|
|
2964 {
|
|
2965 fromto_T *ftp;
|
|
2966 char_u word[MAXWLEN];
|
|
2967
|
|
2968 if (ga_grow(gap, 1) == OK)
|
|
2969 {
|
|
2970 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
|
|
2971 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
|
|
2972 ftp->ft_from = getroom_save(&spin->si_blocks, word);
|
|
2973 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
|
|
2974 ftp->ft_to = getroom_save(&spin->si_blocks, word);
|
|
2975 ++gap->ga_len;
|
|
2976 }
|
|
2977 }
|
|
2978
|
|
2979 /*
|
|
2980 * Convert a boolean argument in a SAL line to TRUE or FALSE;
|
|
2981 */
|
|
2982 static int
|
|
2983 sal_to_bool(s)
|
|
2984 char_u *s;
|
|
2985 {
|
|
2986 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
|
|
2987 }
|
|
2988
|
|
2989 /*
|
240
|
2990 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
|
|
2991 * When "s" is NULL FALSE is returned.
|
|
2992 */
|
|
2993 static int
|
|
2994 has_non_ascii(s)
|
|
2995 char_u *s;
|
|
2996 {
|
|
2997 char_u *p;
|
|
2998
|
|
2999 if (s != NULL)
|
|
3000 for (p = s; *p != NUL; ++p)
|
|
3001 if (*p >= 128)
|
|
3002 return TRUE;
|
|
3003 return FALSE;
|
|
3004 }
|
|
3005
|
|
3006 /*
|
236
|
3007 * Free the structure filled by spell_read_aff().
|
|
3008 */
|
|
3009 static void
|
|
3010 spell_free_aff(aff)
|
|
3011 afffile_T *aff;
|
|
3012 {
|
|
3013 hashtab_T *ht;
|
|
3014 hashitem_T *hi;
|
|
3015 int todo;
|
|
3016 affheader_T *ah;
|
300
|
3017 affentry_T *ae;
|
236
|
3018
|
|
3019 vim_free(aff->af_enc);
|
|
3020
|
339
|
3021 /* All this trouble to free the "ae_prog" items... */
|
236
|
3022 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
|
|
3023 {
|
|
3024 todo = ht->ht_used;
|
|
3025 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
3026 {
|
|
3027 if (!HASHITEM_EMPTY(hi))
|
|
3028 {
|
|
3029 --todo;
|
|
3030 ah = HI2AH(hi);
|
300
|
3031 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
3032 vim_free(ae->ae_prog);
|
236
|
3033 }
|
|
3034 }
|
|
3035 if (ht == &aff->af_suff)
|
|
3036 break;
|
|
3037 }
|
300
|
3038
|
236
|
3039 hash_clear(&aff->af_pref);
|
|
3040 hash_clear(&aff->af_suff);
|
|
3041 }
|
|
3042
|
|
3043 /*
|
300
|
3044 * Read dictionary file "fname".
|
236
|
3045 * Returns OK or FAIL;
|
|
3046 */
|
|
3047 static int
|
300
|
3048 spell_read_dic(fname, spin, affile)
|
236
|
3049 char_u *fname;
|
300
|
3050 spellinfo_T *spin;
|
|
3051 afffile_T *affile;
|
236
|
3052 {
|
300
|
3053 hashtab_T ht;
|
236
|
3054 char_u line[MAXLINELEN];
|
300
|
3055 char_u *afflist;
|
339
|
3056 char_u *pfxlist;
|
300
|
3057 char_u *dw;
|
236
|
3058 char_u *pc;
|
|
3059 char_u *w;
|
|
3060 int l;
|
|
3061 hash_T hash;
|
|
3062 hashitem_T *hi;
|
|
3063 FILE *fd;
|
|
3064 int lnum = 1;
|
300
|
3065 int non_ascii = 0;
|
|
3066 int retval = OK;
|
|
3067 char_u message[MAXLINELEN + MAXWLEN];
|
307
|
3068 int flags;
|
236
|
3069
|
300
|
3070 /*
|
|
3071 * Open the file.
|
|
3072 */
|
310
|
3073 fd = mch_fopen((char *)fname, "r");
|
236
|
3074 if (fd == NULL)
|
|
3075 {
|
|
3076 EMSG2(_(e_notopen), fname);
|
|
3077 return FAIL;
|
|
3078 }
|
|
3079
|
300
|
3080 /* The hashtable is only used to detect duplicated words. */
|
|
3081 hash_init(&ht);
|
|
3082
|
334
|
3083 spin->si_foldwcount = 0;
|
|
3084 spin->si_keepwcount = 0;
|
|
3085
|
310
|
3086 if (spin->si_verbose || p_verbose > 2)
|
|
3087 {
|
|
3088 if (!spin->si_verbose)
|
|
3089 verbose_enter();
|
|
3090 smsg((char_u *)_("Reading dictionary file %s..."), fname);
|
|
3091 out_flush();
|
|
3092 if (!spin->si_verbose)
|
|
3093 verbose_leave();
|
|
3094 }
|
236
|
3095
|
|
3096 /* Read and ignore the first line: word count. */
|
|
3097 (void)vim_fgets(line, MAXLINELEN, fd);
|
324
|
3098 if (!vim_isdigit(*skipwhite(line)))
|
236
|
3099 EMSG2(_("E760: No word count in %s"), fname);
|
|
3100
|
|
3101 /*
|
|
3102 * Read all the lines in the file one by one.
|
|
3103 * The words are converted to 'encoding' here, before being added to
|
|
3104 * the hashtable.
|
|
3105 */
|
255
|
3106 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
|
236
|
3107 {
|
255
|
3108 line_breakcheck();
|
236
|
3109 ++lnum;
|
|
3110
|
300
|
3111 /* Remove CR, LF and white space from the end. White space halfway
|
|
3112 * the word is kept to allow e.g., "et al.". */
|
236
|
3113 l = STRLEN(line);
|
|
3114 while (l > 0 && line[l - 1] <= ' ')
|
|
3115 --l;
|
|
3116 if (l == 0)
|
|
3117 continue; /* empty line */
|
|
3118 line[l] = NUL;
|
|
3119
|
|
3120 /* Find the optional affix names. */
|
300
|
3121 afflist = vim_strchr(line, '/');
|
|
3122 if (afflist != NULL)
|
|
3123 *afflist++ = NUL;
|
236
|
3124
|
300
|
3125 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
3126 if (spin->si_ascii && has_non_ascii(line))
|
|
3127 {
|
|
3128 ++non_ascii;
|
240
|
3129 continue;
|
300
|
3130 }
|
240
|
3131
|
310
|
3132 #ifdef FEAT_MBYTE
|
236
|
3133 /* Convert from "SET" to 'encoding' when needed. */
|
300
|
3134 if (spin->si_conv.vc_type != CONV_NONE)
|
236
|
3135 {
|
300
|
3136 pc = string_convert(&spin->si_conv, line, NULL);
|
255
|
3137 if (pc == NULL)
|
|
3138 {
|
|
3139 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
3140 fname, lnum, line);
|
|
3141 continue;
|
|
3142 }
|
236
|
3143 w = pc;
|
|
3144 }
|
|
3145 else
|
310
|
3146 #endif
|
236
|
3147 {
|
|
3148 pc = NULL;
|
|
3149 w = line;
|
|
3150 }
|
|
3151
|
339
|
3152 /* This takes time, print a message now and then. */
|
|
3153 if (spin->si_verbose && (lnum & 0x3ff) == 0)
|
|
3154 {
|
|
3155 vim_snprintf((char *)message, sizeof(message),
|
|
3156 _("line %6d, word %6d - %s"),
|
|
3157 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
|
|
3158 msg_start();
|
|
3159 msg_puts_long_attr(message, 0);
|
|
3160 msg_clr_eos();
|
|
3161 msg_didout = FALSE;
|
|
3162 msg_col = 0;
|
|
3163 out_flush();
|
|
3164 }
|
|
3165
|
300
|
3166 /* Store the word in the hashtable to be able to find duplicates. */
|
|
3167 dw = (char_u *)getroom_save(&spin->si_blocks, w);
|
236
|
3168 if (dw == NULL)
|
300
|
3169 retval = FAIL;
|
|
3170 vim_free(pc);
|
|
3171 if (retval == FAIL)
|
236
|
3172 break;
|
|
3173
|
300
|
3174 hash = hash_hash(dw);
|
|
3175 hi = hash_lookup(&ht, dw, hash);
|
236
|
3176 if (!HASHITEM_EMPTY(hi))
|
|
3177 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
|
339
|
3178 fname, lnum, w);
|
236
|
3179 else
|
300
|
3180 hash_add_item(&ht, hi, dw, hash);
|
|
3181
|
307
|
3182 flags = 0;
|
339
|
3183 pfxlist = NULL;
|
307
|
3184 if (afflist != NULL)
|
|
3185 {
|
|
3186 /* Check for affix name that stands for keep-case word and stands
|
|
3187 * for rare word (if defined). */
|
310
|
3188 if (affile->af_kep != NUL
|
|
3189 && vim_strchr(afflist, affile->af_kep) != NULL)
|
307
|
3190 flags |= WF_KEEPCAP;
|
|
3191 if (affile->af_rar != NUL
|
|
3192 && vim_strchr(afflist, affile->af_rar) != NULL)
|
|
3193 flags |= WF_RARE;
|
346
|
3194 if (affile->af_bad != NUL
|
|
3195 && vim_strchr(afflist, affile->af_bad) != NULL)
|
|
3196 flags |= WF_BANNED;
|
339
|
3197
|
|
3198 if (affile->af_pfxpostpone)
|
|
3199 /* Need to store the list of prefix IDs with the word. */
|
|
3200 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
|
307
|
3201 }
|
|
3202
|
300
|
3203 /* Add the word to the word tree(s). */
|
339
|
3204 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
|
300
|
3205 retval = FAIL;
|
236
|
3206
|
300
|
3207 if (afflist != NULL)
|
|
3208 {
|
|
3209 /* Find all matching suffixes and add the resulting words.
|
|
3210 * Additionally do matching prefixes that combine. */
|
339
|
3211 if (store_aff_word(dw, spin, afflist, affile,
|
307
|
3212 &affile->af_suff, &affile->af_pref,
|
339
|
3213 FALSE, flags, pfxlist) == FAIL)
|
300
|
3214 retval = FAIL;
|
|
3215
|
|
3216 /* Find all matching prefixes and add the resulting words. */
|
339
|
3217 if (store_aff_word(dw, spin, afflist, affile,
|
|
3218 &affile->af_pref, NULL,
|
|
3219 FALSE, flags, pfxlist) == FAIL)
|
300
|
3220 retval = FAIL;
|
|
3221 }
|
236
|
3222 }
|
|
3223
|
300
|
3224 if (spin->si_ascii && non_ascii > 0)
|
|
3225 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
3226 non_ascii);
|
|
3227 hash_clear(&ht);
|
|
3228
|
236
|
3229 fclose(fd);
|
300
|
3230 return retval;
|
236
|
3231 }
|
|
3232
|
|
3233 /*
|
339
|
3234 * Get the list of prefix IDs from the affix list "afflist".
|
|
3235 * Used for PFXPOSTPONE.
|
|
3236 * Returns a string allocated with getroom(). NULL when there are no prefixes
|
|
3237 * or when out of memory.
|
|
3238 */
|
|
3239 static char_u *
|
|
3240 get_pfxlist(affile, afflist, blp)
|
|
3241 afffile_T *affile;
|
|
3242 char_u *afflist;
|
|
3243 sblock_T **blp;
|
|
3244 {
|
|
3245 char_u *p;
|
|
3246 int cnt;
|
|
3247 int round;
|
|
3248 char_u *res = NULL;
|
|
3249 char_u key[2];
|
|
3250 hashitem_T *hi;
|
|
3251
|
|
3252 key[1] = NUL;
|
|
3253
|
|
3254 /* round 1: count the number of prefix IDs.
|
|
3255 * round 2: move prefix IDs to "res" */
|
|
3256 for (round = 1; round <= 2; ++round)
|
|
3257 {
|
|
3258 cnt = 0;
|
|
3259 for (p = afflist; *p != NUL; ++p)
|
|
3260 {
|
|
3261 key[0] = *p;
|
|
3262 hi = hash_find(&affile->af_pref, key);
|
|
3263 if (!HASHITEM_EMPTY(hi))
|
|
3264 {
|
|
3265 /* This is a prefix ID, use the new number. */
|
|
3266 if (round == 2)
|
|
3267 res[cnt] = HI2AH(hi)->ah_newID;
|
|
3268 ++cnt;
|
|
3269 }
|
|
3270 }
|
|
3271 if (round == 1 && cnt > 0)
|
|
3272 res = getroom(blp, cnt + 1);
|
|
3273 if (res == NULL)
|
|
3274 break;
|
|
3275 }
|
|
3276
|
|
3277 if (res != NULL)
|
|
3278 res[cnt] = NUL;
|
|
3279 return res;
|
|
3280 }
|
|
3281
|
|
3282 /*
|
300
|
3283 * Apply affixes to a word and store the resulting words.
|
|
3284 * "ht" is the hashtable with affentry_T that need to be applied, either
|
|
3285 * prefixes or suffixes.
|
|
3286 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
|
|
3287 * the resulting words for combining affixes.
|
|
3288 *
|
|
3289 * Returns FAIL when out of memory.
|
236
|
3290 */
|
300
|
3291 static int
|
339
|
3292 store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
|
300
|
3293 char_u *word; /* basic word start */
|
|
3294 spellinfo_T *spin; /* spell info */
|
|
3295 char_u *afflist; /* list of names of supported affixes */
|
339
|
3296 afffile_T *affile;
|
300
|
3297 hashtab_T *ht;
|
|
3298 hashtab_T *xht;
|
|
3299 int comb; /* only use affixes that combine */
|
307
|
3300 int flags; /* flags for the word */
|
339
|
3301 char_u *pfxlist; /* list of prefix IDs */
|
236
|
3302 {
|
|
3303 int todo;
|
|
3304 hashitem_T *hi;
|
300
|
3305 affheader_T *ah;
|
|
3306 affentry_T *ae;
|
|
3307 regmatch_T regmatch;
|
|
3308 char_u newword[MAXWLEN];
|
|
3309 int retval = OK;
|
|
3310 int i;
|
|
3311 char_u *p;
|
236
|
3312
|
300
|
3313 todo = ht->ht_used;
|
|
3314 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
|
236
|
3315 {
|
|
3316 if (!HASHITEM_EMPTY(hi))
|
|
3317 {
|
|
3318 --todo;
|
300
|
3319 ah = HI2AH(hi);
|
236
|
3320
|
300
|
3321 /* Check that the affix combines, if required, and that the word
|
|
3322 * supports this affix. */
|
|
3323 if ((!comb || ah->ah_combine)
|
|
3324 && vim_strchr(afflist, *ah->ah_key) != NULL)
|
236
|
3325 {
|
300
|
3326 /* Loop over all affix entries with this name. */
|
|
3327 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
236
|
3328 {
|
300
|
3329 /* Check the condition. It's not logical to match case
|
|
3330 * here, but it is required for compatibility with
|
339
|
3331 * Myspell.
|
|
3332 * For prefixes, when "PFXPOSTPONE" was used, only do
|
|
3333 * prefixes with a chop string. */
|
300
|
3334 regmatch.regprog = ae->ae_prog;
|
|
3335 regmatch.rm_ic = FALSE;
|
339
|
3336 if ((xht != NULL || !affile->af_pfxpostpone
|
|
3337 || ae->ae_chop != NULL)
|
|
3338 && (ae->ae_prog == NULL
|
|
3339 || vim_regexec(®match, word, (colnr_T)0)))
|
300
|
3340 {
|
|
3341 /* Match. Remove the chop and add the affix. */
|
|
3342 if (xht == NULL)
|
240
|
3343 {
|
300
|
3344 /* prefix: chop/add at the start of the word */
|
|
3345 if (ae->ae_add == NULL)
|
|
3346 *newword = NUL;
|
|
3347 else
|
|
3348 STRCPY(newword, ae->ae_add);
|
|
3349 p = word;
|
|
3350 if (ae->ae_chop != NULL)
|
310
|
3351 {
|
300
|
3352 /* Skip chop string. */
|
310
|
3353 #ifdef FEAT_MBYTE
|
|
3354 if (has_mbyte)
|
324
|
3355 {
|
310
|
3356 i = mb_charlen(ae->ae_chop);
|
324
|
3357 for ( ; i > 0; --i)
|
|
3358 mb_ptr_adv(p);
|
|
3359 }
|
310
|
3360 else
|
|
3361 #endif
|
324
|
3362 p += STRLEN(ae->ae_chop);
|
310
|
3363 }
|
300
|
3364 STRCAT(newword, p);
|
|
3365 }
|
|
3366 else
|
|
3367 {
|
|
3368 /* suffix: chop/add at the end of the word */
|
|
3369 STRCPY(newword, word);
|
|
3370 if (ae->ae_chop != NULL)
|
|
3371 {
|
|
3372 /* Remove chop string. */
|
|
3373 p = newword + STRLEN(newword);
|
310
|
3374 #ifdef FEAT_MBYTE
|
|
3375 if (has_mbyte)
|
|
3376 i = mb_charlen(ae->ae_chop);
|
|
3377 else
|
|
3378 #endif
|
|
3379 i = STRLEN(ae->ae_chop);
|
|
3380 for ( ; i > 0; --i)
|
300
|
3381 mb_ptr_back(newword, p);
|
|
3382 *p = NUL;
|
|
3383 }
|
|
3384 if (ae->ae_add != NULL)
|
|
3385 STRCAT(newword, ae->ae_add);
|
240
|
3386 }
|
|
3387
|
300
|
3388 /* Store the modified word. */
|
316
|
3389 if (store_word(newword, spin,
|
339
|
3390 flags, spin->si_region, pfxlist) == FAIL)
|
300
|
3391 retval = FAIL;
|
236
|
3392
|
300
|
3393 /* When added a suffix and combining is allowed also
|
|
3394 * try adding prefixes additionally. */
|
|
3395 if (xht != NULL && ah->ah_combine)
|
339
|
3396 if (store_aff_word(newword, spin, afflist, affile,
|
|
3397 xht, NULL, TRUE, flags, pfxlist) == FAIL)
|
300
|
3398 retval = FAIL;
|
236
|
3399 }
|
|
3400 }
|
|
3401 }
|
|
3402 }
|
|
3403 }
|
|
3404
|
|
3405 return retval;
|
|
3406 }
|
|
3407
|
|
3408 /*
|
300
|
3409 * Read a file with a list of words.
|
236
|
3410 */
|
|
3411 static int
|
300
|
3412 spell_read_wordfile(fname, spin)
|
|
3413 char_u *fname;
|
|
3414 spellinfo_T *spin;
|
236
|
3415 {
|
300
|
3416 FILE *fd;
|
|
3417 long lnum = 0;
|
|
3418 char_u rline[MAXLINELEN];
|
|
3419 char_u *line;
|
|
3420 char_u *pc = NULL;
|
|
3421 int l;
|
|
3422 int retval = OK;
|
|
3423 int did_word = FALSE;
|
|
3424 int non_ascii = 0;
|
307
|
3425 int flags;
|
316
|
3426 int regionmask;
|
236
|
3427
|
300
|
3428 /*
|
|
3429 * Open the file.
|
|
3430 */
|
310
|
3431 fd = mch_fopen((char *)fname, "r");
|
300
|
3432 if (fd == NULL)
|
236
|
3433 {
|
300
|
3434 EMSG2(_(e_notopen), fname);
|
|
3435 return FAIL;
|
236
|
3436 }
|
|
3437
|
310
|
3438 if (spin->si_verbose || p_verbose > 2)
|
|
3439 {
|
|
3440 if (!spin->si_verbose)
|
|
3441 verbose_enter();
|
|
3442 smsg((char_u *)_("Reading word file %s..."), fname);
|
|
3443 out_flush();
|
|
3444 if (!spin->si_verbose)
|
|
3445 verbose_leave();
|
|
3446 }
|
300
|
3447
|
|
3448 /*
|
|
3449 * Read all the lines in the file one by one.
|
|
3450 */
|
|
3451 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
|
3452 {
|
|
3453 line_breakcheck();
|
|
3454 ++lnum;
|
|
3455
|
|
3456 /* Skip comment lines. */
|
|
3457 if (*rline == '#')
|
|
3458 continue;
|
|
3459
|
|
3460 /* Remove CR, LF and white space from the end. */
|
|
3461 l = STRLEN(rline);
|
|
3462 while (l > 0 && rline[l - 1] <= ' ')
|
|
3463 --l;
|
|
3464 if (l == 0)
|
|
3465 continue; /* empty or blank line */
|
|
3466 rline[l] = NUL;
|
|
3467
|
|
3468 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
|
|
3469 vim_free(pc);
|
310
|
3470 #ifdef FEAT_MBYTE
|
300
|
3471 if (spin->si_conv.vc_type != CONV_NONE)
|
|
3472 {
|
|
3473 pc = string_convert(&spin->si_conv, rline, NULL);
|
|
3474 if (pc == NULL)
|
|
3475 {
|
|
3476 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
3477 fname, lnum, rline);
|
|
3478 continue;
|
|
3479 }
|
|
3480 line = pc;
|
|
3481 }
|
|
3482 else
|
310
|
3483 #endif
|
300
|
3484 {
|
|
3485 pc = NULL;
|
|
3486 line = rline;
|
|
3487 }
|
|
3488
|
307
|
3489 flags = 0;
|
316
|
3490 regionmask = spin->si_region;
|
307
|
3491
|
|
3492 if (*line == '/')
|
300
|
3493 {
|
307
|
3494 ++line;
|
316
|
3495
|
307
|
3496 if (STRNCMP(line, "encoding=", 9) == 0)
|
300
|
3497 {
|
|
3498 if (spin->si_conv.vc_type != CONV_NONE)
|
316
|
3499 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
|
|
3500 fname, lnum, line - 1);
|
300
|
3501 else if (did_word)
|
316
|
3502 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
|
|
3503 fname, lnum, line - 1);
|
300
|
3504 else
|
|
3505 {
|
310
|
3506 #ifdef FEAT_MBYTE
|
|
3507 char_u *enc;
|
|
3508
|
300
|
3509 /* Setup for conversion to 'encoding'. */
|
316
|
3510 line += 10;
|
|
3511 enc = enc_canonize(line);
|
300
|
3512 if (enc != NULL && !spin->si_ascii
|
|
3513 && convert_setup(&spin->si_conv, enc,
|
|
3514 p_enc) == FAIL)
|
|
3515 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
316
|
3516 fname, line, p_enc);
|
300
|
3517 vim_free(enc);
|
310
|
3518 #else
|
|
3519 smsg((char_u *)_("Conversion in %s not supported"), fname);
|
|
3520 #endif
|
300
|
3521 }
|
307
|
3522 continue;
|
300
|
3523 }
|
307
|
3524
|
316
|
3525 if (STRNCMP(line, "regions=", 8) == 0)
|
|
3526 {
|
|
3527 if (spin->si_region_count > 1)
|
|
3528 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
|
|
3529 fname, lnum, line);
|
|
3530 else
|
|
3531 {
|
|
3532 line += 8;
|
|
3533 if (STRLEN(line) > 16)
|
|
3534 smsg((char_u *)_("Too many regions in %s line %d: %s"),
|
|
3535 fname, lnum, line);
|
|
3536 else
|
|
3537 {
|
|
3538 spin->si_region_count = STRLEN(line) / 2;
|
|
3539 STRCPY(spin->si_region_name, line);
|
|
3540 }
|
|
3541 }
|
|
3542 continue;
|
|
3543 }
|
|
3544
|
307
|
3545 if (*line == '=')
|
|
3546 {
|
|
3547 /* keep-case word */
|
|
3548 flags |= WF_KEEPCAP;
|
|
3549 ++line;
|
|
3550 }
|
|
3551
|
|
3552 if (*line == '!')
|
|
3553 {
|
|
3554 /* Bad, bad, wicked word. */
|
|
3555 flags |= WF_BANNED;
|
|
3556 ++line;
|
|
3557 }
|
|
3558 else if (*line == '?')
|
|
3559 {
|
|
3560 /* Rare word. */
|
|
3561 flags |= WF_RARE;
|
|
3562 ++line;
|
|
3563 }
|
|
3564
|
316
|
3565 if (VIM_ISDIGIT(*line))
|
|
3566 {
|
|
3567 /* region number(s) */
|
|
3568 regionmask = 0;
|
|
3569 while (VIM_ISDIGIT(*line))
|
|
3570 {
|
|
3571 l = *line - '0';
|
|
3572 if (l > spin->si_region_count)
|
|
3573 {
|
|
3574 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
|
|
3575 fname, lnum, line);
|
|
3576 break;
|
|
3577 }
|
|
3578 regionmask |= 1 << (l - 1);
|
|
3579 ++line;
|
|
3580 }
|
|
3581 flags |= WF_REGION;
|
|
3582 }
|
|
3583
|
307
|
3584 if (flags == 0)
|
|
3585 {
|
|
3586 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
|
300
|
3587 fname, lnum, line);
|
307
|
3588 continue;
|
|
3589 }
|
300
|
3590 }
|
|
3591
|
|
3592 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
3593 if (spin->si_ascii && has_non_ascii(line))
|
|
3594 {
|
|
3595 ++non_ascii;
|
|
3596 continue;
|
|
3597 }
|
|
3598
|
|
3599 /* Normal word: store it. */
|
339
|
3600 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
|
300
|
3601 {
|
|
3602 retval = FAIL;
|
|
3603 break;
|
|
3604 }
|
|
3605 did_word = TRUE;
|
|
3606 }
|
|
3607
|
|
3608 vim_free(pc);
|
|
3609 fclose(fd);
|
|
3610
|
310
|
3611 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
|
|
3612 {
|
|
3613 if (p_verbose > 2)
|
|
3614 verbose_enter();
|
300
|
3615 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
3616 non_ascii);
|
310
|
3617 if (p_verbose > 2)
|
|
3618 verbose_leave();
|
|
3619 }
|
300
|
3620 return retval;
|
236
|
3621 }
|
|
3622
|
|
3623 /*
|
300
|
3624 * Get part of an sblock_T, "len" bytes long.
|
|
3625 * This avoids calling free() for every little struct we use.
|
|
3626 * The memory is cleared to all zeros.
|
|
3627 * Returns NULL when out of memory.
|
|
3628 */
|
|
3629 static void *
|
|
3630 getroom(blp, len)
|
|
3631 sblock_T **blp;
|
|
3632 size_t len; /* length needed */
|
|
3633 {
|
|
3634 char_u *p;
|
|
3635 sblock_T *bl = *blp;
|
|
3636
|
|
3637 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
|
|
3638 {
|
|
3639 /* Allocate a block of memory. This is not freed until much later. */
|
|
3640 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
|
|
3641 if (bl == NULL)
|
|
3642 return NULL;
|
|
3643 bl->sb_next = *blp;
|
|
3644 *blp = bl;
|
|
3645 bl->sb_used = 0;
|
|
3646 }
|
|
3647
|
|
3648 p = bl->sb_data + bl->sb_used;
|
|
3649 bl->sb_used += len;
|
|
3650
|
|
3651 return p;
|
|
3652 }
|
|
3653
|
|
3654 /*
|
|
3655 * Make a copy of a string into memory allocated with getroom().
|
|
3656 */
|
|
3657 static char_u *
|
|
3658 getroom_save(blp, s)
|
|
3659 sblock_T **blp;
|
|
3660 char_u *s;
|
|
3661 {
|
|
3662 char_u *sc;
|
|
3663
|
|
3664 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
|
|
3665 if (sc != NULL)
|
|
3666 STRCPY(sc, s);
|
|
3667 return sc;
|
|
3668 }
|
|
3669
|
|
3670
|
|
3671 /*
|
|
3672 * Free the list of allocated sblock_T.
|
236
|
3673 */
|
|
3674 static void
|
300
|
3675 free_blocks(bl)
|
|
3676 sblock_T *bl;
|
236
|
3677 {
|
300
|
3678 sblock_T *next;
|
236
|
3679
|
300
|
3680 while (bl != NULL)
|
236
|
3681 {
|
300
|
3682 next = bl->sb_next;
|
|
3683 vim_free(bl);
|
|
3684 bl = next;
|
236
|
3685 }
|
|
3686 }
|
|
3687
|
|
3688 /*
|
300
|
3689 * Allocate the root of a word tree.
|
236
|
3690 */
|
300
|
3691 static wordnode_T *
|
|
3692 wordtree_alloc(blp)
|
|
3693 sblock_T **blp;
|
236
|
3694 {
|
300
|
3695 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
236
|
3696 }
|
|
3697
|
|
3698 /*
|
300
|
3699 * Store a word in the tree(s).
|
307
|
3700 * Always store it in the case-folded tree. A keep-case word can also be used
|
|
3701 * with all caps.
|
300
|
3702 * For a keep-case word also store it in the keep-case tree.
|
339
|
3703 * When "pfxlist" is not NULL store the word for each prefix ID.
|
236
|
3704 */
|
|
3705 static int
|
339
|
3706 store_word(word, spin, flags, region, pfxlist)
|
300
|
3707 char_u *word;
|
|
3708 spellinfo_T *spin;
|
307
|
3709 int flags; /* extra flags, WF_BANNED */
|
316
|
3710 int region; /* supported region(s) */
|
339
|
3711 char_u *pfxlist; /* list of prefix IDs or NULL */
|
236
|
3712 {
|
300
|
3713 int len = STRLEN(word);
|
|
3714 int ct = captype(word, word + len);
|
|
3715 char_u foldword[MAXWLEN];
|
339
|
3716 int res = OK;
|
|
3717 char_u *p;
|
236
|
3718
|
323
|
3719 (void)spell_casefold(word, len, foldword, MAXWLEN);
|
339
|
3720 for (p = pfxlist; res == OK; ++p)
|
|
3721 {
|
|
3722 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
|
|
3723 region, p == NULL ? 0 : *p, &spin->si_blocks);
|
|
3724 if (p == NULL || *p == NUL)
|
|
3725 break;
|
|
3726 }
|
334
|
3727 ++spin->si_foldwcount;
|
307
|
3728
|
|
3729 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
|
334
|
3730 {
|
339
|
3731 for (p = pfxlist; res == OK; ++p)
|
|
3732 {
|
|
3733 res = tree_add_word(word, spin->si_keeproot, flags,
|
|
3734 region, p == NULL ? 0 : *p, &spin->si_blocks);
|
|
3735 if (p == NULL || *p == NUL)
|
|
3736 break;
|
|
3737 }
|
334
|
3738 ++spin->si_keepwcount;
|
|
3739 }
|
300
|
3740 return res;
|
236
|
3741 }
|
|
3742
|
|
3743 /*
|
300
|
3744 * Add word "word" to a word tree at "root".
|
339
|
3745 * When "flags" is -1 we are adding to the prefix tree where flags don't
|
|
3746 * matter and "region" is the condition nr.
|
255
|
3747 * Returns FAIL when out of memory.
|
236
|
3748 */
|
255
|
3749 static int
|
339
|
3750 tree_add_word(word, root, flags, region, prefixID, blp)
|
300
|
3751 char_u *word;
|
|
3752 wordnode_T *root;
|
|
3753 int flags;
|
|
3754 int region;
|
339
|
3755 int prefixID;
|
300
|
3756 sblock_T **blp;
|
236
|
3757 {
|
300
|
3758 wordnode_T *node = root;
|
|
3759 wordnode_T *np;
|
|
3760 wordnode_T **prev = NULL;
|
|
3761 int i;
|
255
|
3762
|
300
|
3763 /* Add each byte of the word to the tree, including the NUL at the end. */
|
|
3764 for (i = 0; ; ++i)
|
255
|
3765 {
|
300
|
3766 /* Look for the sibling that has the same character. They are sorted
|
|
3767 * on byte value, thus stop searching when a sibling is found with a
|
339
|
3768 * higher byte value. For zero bytes (end of word) the sorting is
|
|
3769 * done on flags and then on prefixID
|
300
|
3770 */
|
339
|
3771 while (node != NULL
|
|
3772 && (node->wn_byte < word[i]
|
|
3773 || (node->wn_byte == NUL
|
|
3774 && (flags < 0
|
|
3775 ? node->wn_prefixID < prefixID
|
|
3776 : node->wn_flags < (flags & 0xff)
|
|
3777 || (node->wn_flags == (flags & 0xff)
|
|
3778 && node->wn_prefixID < prefixID)))))
|
236
|
3779 {
|
300
|
3780 prev = &node->wn_sibling;
|
|
3781 node = *prev;
|
236
|
3782 }
|
339
|
3783 if (node == NULL
|
|
3784 || node->wn_byte != word[i]
|
|
3785 || (word[i] == NUL
|
|
3786 && (flags < 0
|
|
3787 || node->wn_flags != (flags & 0xff)
|
|
3788 || node->wn_prefixID != prefixID)))
|
255
|
3789 {
|
300
|
3790 /* Allocate a new node. */
|
|
3791 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
|
3792 if (np == NULL)
|
|
3793 return FAIL;
|
|
3794 np->wn_byte = word[i];
|
|
3795 *prev = np;
|
|
3796 np->wn_sibling = node;
|
|
3797 node = np;
|
255
|
3798 }
|
300
|
3799
|
|
3800 if (word[i] == NUL)
|
|
3801 {
|
|
3802 node->wn_flags = flags;
|
|
3803 node->wn_region |= region;
|
339
|
3804 node->wn_prefixID = prefixID;
|
300
|
3805 break;
|
|
3806 }
|
|
3807 prev = &node->wn_child;
|
|
3808 node = *prev;
|
255
|
3809 }
|
|
3810
|
|
3811 return OK;
|
236
|
3812 }
|
|
3813
|
|
3814 /*
|
300
|
3815 * Compress a tree: find tails that are identical and can be shared.
|
|
3816 */
|
|
3817 static void
|
310
|
3818 wordtree_compress(root, spin)
|
300
|
3819 wordnode_T *root;
|
310
|
3820 spellinfo_T *spin;
|
300
|
3821 {
|
|
3822 hashtab_T ht;
|
|
3823 int n;
|
|
3824 int tot = 0;
|
|
3825
|
|
3826 if (root != NULL)
|
|
3827 {
|
|
3828 hash_init(&ht);
|
|
3829 n = node_compress(root, &ht, &tot);
|
310
|
3830 if (spin->si_verbose || p_verbose > 2)
|
|
3831 {
|
|
3832 if (!spin->si_verbose)
|
|
3833 verbose_enter();
|
|
3834 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
|
300
|
3835 n, tot, (tot - n) * 100 / tot);
|
310
|
3836 if (p_verbose > 2)
|
|
3837 verbose_leave();
|
|
3838 }
|
300
|
3839 hash_clear(&ht);
|
|
3840 }
|
|
3841 }
|
|
3842
|
|
3843 /*
|
|
3844 * Compress a node, its siblings and its children, depth first.
|
|
3845 * Returns the number of compressed nodes.
|
236
|
3846 */
|
255
|
3847 static int
|
300
|
3848 node_compress(node, ht, tot)
|
|
3849 wordnode_T *node;
|
|
3850 hashtab_T *ht;
|
|
3851 int *tot; /* total count of nodes before compressing,
|
|
3852 incremented while going through the tree */
|
236
|
3853 {
|
300
|
3854 wordnode_T *np;
|
|
3855 wordnode_T *tp;
|
|
3856 wordnode_T *child;
|
|
3857 hash_T hash;
|
236
|
3858 hashitem_T *hi;
|
300
|
3859 int len = 0;
|
|
3860 unsigned nr, n;
|
|
3861 int compressed = 0;
|
236
|
3862
|
300
|
3863 /*
|
|
3864 * Go through the list of siblings. Compress each child and then try
|
|
3865 * finding an identical child to replace it.
|
|
3866 * Note that with "child" we mean not just the node that is pointed to,
|
|
3867 * but the whole list of siblings, of which the node is the first.
|
|
3868 */
|
|
3869 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
3870 {
|
300
|
3871 ++len;
|
|
3872 if ((child = np->wn_child) != NULL)
|
|
3873 {
|
346
|
3874 /* Compress the child. This fills hashkey. */
|
300
|
3875 compressed += node_compress(child, ht, tot);
|
|
3876
|
|
3877 /* Try to find an identical child. */
|
346
|
3878 hash = hash_hash(child->wn_u1.hashkey);
|
|
3879 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
|
300
|
3880 tp = NULL;
|
|
3881 if (!HASHITEM_EMPTY(hi))
|
|
3882 {
|
|
3883 /* There are children with an identical hash value. Now check
|
|
3884 * if there is one that is really identical. */
|
346
|
3885 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
|
300
|
3886 if (node_equal(child, tp))
|
|
3887 {
|
|
3888 /* Found one! Now use that child in place of the
|
|
3889 * current one. This means the current child is
|
|
3890 * dropped from the tree. */
|
|
3891 np->wn_child = tp;
|
|
3892 ++compressed;
|
|
3893 break;
|
|
3894 }
|
|
3895 if (tp == NULL)
|
|
3896 {
|
|
3897 /* No other child with this hash value equals the child of
|
|
3898 * the node, add it to the linked list after the first
|
|
3899 * item. */
|
|
3900 tp = HI2WN(hi);
|
346
|
3901 child->wn_u2.next = tp->wn_u2.next;
|
|
3902 tp->wn_u2.next = child;
|
300
|
3903 }
|
|
3904 }
|
|
3905 else
|
|
3906 /* No other child has this hash value, add it to the
|
|
3907 * hashtable. */
|
346
|
3908 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
|
300
|
3909 }
|
236
|
3910 }
|
300
|
3911 *tot += len;
|
|
3912
|
|
3913 /*
|
|
3914 * Make a hash key for the node and its siblings, so that we can quickly
|
|
3915 * find a lookalike node. This must be done after compressing the sibling
|
|
3916 * list, otherwise the hash key would become invalid by the compression.
|
|
3917 */
|
346
|
3918 node->wn_u1.hashkey[0] = len;
|
300
|
3919 nr = 0;
|
|
3920 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
3921 {
|
300
|
3922 if (np->wn_byte == NUL)
|
339
|
3923 /* end node: use wn_flags, wn_region and wn_prefixID */
|
|
3924 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
|
300
|
3925 else
|
|
3926 /* byte node: use the byte value and the child pointer */
|
|
3927 n = np->wn_byte + ((long_u)np->wn_child << 8);
|
|
3928 nr = nr * 101 + n;
|
236
|
3929 }
|
300
|
3930
|
|
3931 /* Avoid NUL bytes, it terminates the hash key. */
|
|
3932 n = nr & 0xff;
|
346
|
3933 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
|
300
|
3934 n = (nr >> 8) & 0xff;
|
346
|
3935 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
|
300
|
3936 n = (nr >> 16) & 0xff;
|
346
|
3937 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
|
300
|
3938 n = (nr >> 24) & 0xff;
|
346
|
3939 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
|
|
3940 node->wn_u1.hashkey[5] = NUL;
|
300
|
3941
|
|
3942 return compressed;
|
|
3943 }
|
|
3944
|
|
3945 /*
|
|
3946 * Return TRUE when two nodes have identical siblings and children.
|
|
3947 */
|
|
3948 static int
|
|
3949 node_equal(n1, n2)
|
|
3950 wordnode_T *n1;
|
|
3951 wordnode_T *n2;
|
|
3952 {
|
|
3953 wordnode_T *p1;
|
|
3954 wordnode_T *p2;
|
|
3955
|
|
3956 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
|
|
3957 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
|
|
3958 if (p1->wn_byte != p2->wn_byte
|
|
3959 || (p1->wn_byte == NUL
|
|
3960 ? (p1->wn_flags != p2->wn_flags
|
339
|
3961 || p1->wn_region != p2->wn_region
|
|
3962 || p1->wn_prefixID != p2->wn_prefixID)
|
300
|
3963 : (p1->wn_child != p2->wn_child)))
|
|
3964 break;
|
|
3965
|
|
3966 return p1 == NULL && p2 == NULL;
|
236
|
3967 }
|
|
3968
|
|
3969 /*
|
|
3970 * Write a number to file "fd", MSB first, in "len" bytes.
|
|
3971 */
|
255
|
3972 void
|
236
|
3973 put_bytes(fd, nr, len)
|
|
3974 FILE *fd;
|
|
3975 long_u nr;
|
|
3976 int len;
|
|
3977 {
|
|
3978 int i;
|
|
3979
|
|
3980 for (i = len - 1; i >= 0; --i)
|
|
3981 putc((int)(nr >> (i * 8)), fd);
|
|
3982 }
|
|
3983
|
323
|
3984 static int
|
|
3985 #ifdef __BORLANDC__
|
|
3986 _RTLENTRYF
|
|
3987 #endif
|
|
3988 rep_compare __ARGS((const void *s1, const void *s2));
|
|
3989
|
|
3990 /*
|
|
3991 * Function given to qsort() to sort the REP items on "from" string.
|
|
3992 */
|
|
3993 static int
|
|
3994 #ifdef __BORLANDC__
|
|
3995 _RTLENTRYF
|
|
3996 #endif
|
|
3997 rep_compare(s1, s2)
|
|
3998 const void *s1;
|
|
3999 const void *s2;
|
|
4000 {
|
|
4001 fromto_T *p1 = (fromto_T *)s1;
|
|
4002 fromto_T *p2 = (fromto_T *)s2;
|
|
4003
|
|
4004 return STRCMP(p1->ft_from, p2->ft_from);
|
|
4005 }
|
|
4006
|
236
|
4007 /*
|
|
4008 * Write the Vim spell file "fname".
|
|
4009 */
|
|
4010 static void
|
316
|
4011 write_vim_spell(fname, spin)
|
236
|
4012 char_u *fname;
|
300
|
4013 spellinfo_T *spin;
|
236
|
4014 {
|
300
|
4015 FILE *fd;
|
|
4016 int regionmask;
|
236
|
4017 int round;
|
300
|
4018 wordnode_T *tree;
|
|
4019 int nodecount;
|
323
|
4020 int i;
|
|
4021 int l;
|
|
4022 garray_T *gap;
|
|
4023 fromto_T *ftp;
|
|
4024 char_u *p;
|
|
4025 int rr;
|
236
|
4026
|
310
|
4027 fd = mch_fopen((char *)fname, "w");
|
300
|
4028 if (fd == NULL)
|
236
|
4029 {
|
|
4030 EMSG2(_(e_notopen), fname);
|
|
4031 return;
|
|
4032 }
|
|
4033
|
255
|
4034 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
339
|
4035 * <charflagslen> <charflags>
|
|
4036 * <fcharslen> <fchars>
|
|
4037 * <prefcondcnt> <prefcond> ... */
|
300
|
4038
|
|
4039 /* <fileID> */
|
|
4040 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
|
|
4041 EMSG(_(e_write));
|
236
|
4042
|
|
4043 /* write the region names if there is more than one */
|
316
|
4044 if (spin->si_region_count > 1)
|
236
|
4045 {
|
316
|
4046 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
|
|
4047 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
|
|
4048 (size_t)1, fd);
|
|
4049 regionmask = (1 << spin->si_region_count) - 1;
|
236
|
4050 }
|
|
4051 else
|
|
4052 {
|
300
|
4053 putc(0, fd);
|
|
4054 regionmask = 0;
|
236
|
4055 }
|
|
4056
|
323
|
4057 /*
|
|
4058 * Write the table with character flags and table for case folding.
|
260
|
4059 * <charflagslen> <charflags> <fcharlen> <fchars>
|
|
4060 * Skip this for ASCII, the table may conflict with the one used for
|
323
|
4061 * 'encoding'.
|
|
4062 * Also skip this for an .add.spl file, the main spell file must contain
|
|
4063 * the table (avoids that it conflicts). File is shorter too.
|
|
4064 */
|
|
4065 if (spin->si_ascii || spin->si_add)
|
260
|
4066 {
|
300
|
4067 putc(0, fd);
|
|
4068 putc(0, fd);
|
|
4069 putc(0, fd);
|
260
|
4070 }
|
|
4071 else
|
300
|
4072 write_spell_chartab(fd);
|
255
|
4073
|
339
|
4074 /* Write the prefix conditions. */
|
|
4075 write_spell_prefcond(fd, &spin->si_prefcond);
|
|
4076
|
323
|
4077 /* Sort the REP items. */
|
|
4078 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
|
|
4079 sizeof(fromto_T), rep_compare);
|
|
4080
|
|
4081 /* <SUGGEST> : <repcount> <rep> ...
|
|
4082 * <salflags> <salcount> <sal> ...
|
|
4083 * <maplen> <mapstr> */
|
|
4084 for (round = 1; round <= 2; ++round)
|
|
4085 {
|
|
4086 if (round == 1)
|
|
4087 gap = &spin->si_rep;
|
|
4088 else
|
|
4089 {
|
|
4090 gap = &spin->si_sal;
|
|
4091
|
|
4092 i = 0;
|
|
4093 if (spin->si_followup)
|
|
4094 i |= SAL_F0LLOWUP;
|
|
4095 if (spin->si_collapse)
|
|
4096 i |= SAL_COLLAPSE;
|
|
4097 if (spin->si_rem_accents)
|
|
4098 i |= SAL_REM_ACCENTS;
|
|
4099 putc(i, fd); /* <salflags> */
|
|
4100 }
|
|
4101
|
|
4102 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
|
|
4103 for (i = 0; i < gap->ga_len; ++i)
|
|
4104 {
|
|
4105 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
|
|
4106 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
|
|
4107 ftp = &((fromto_T *)gap->ga_data)[i];
|
|
4108 for (rr = 1; rr <= 2; ++rr)
|
|
4109 {
|
|
4110 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
|
|
4111 l = STRLEN(p);
|
|
4112 putc(l, fd);
|
|
4113 fwrite(p, l, (size_t)1, fd);
|
|
4114 }
|
|
4115 }
|
|
4116 }
|
|
4117
|
|
4118 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
|
|
4119 if (spin->si_map.ga_len > 0) /* <mapstr> */
|
|
4120 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
|
|
4121 (size_t)1, fd);
|
302
|
4122
|
236
|
4123 /*
|
339
|
4124 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
|
236
|
4125 */
|
323
|
4126 spin->si_memtot = 0;
|
339
|
4127 for (round = 1; round <= 3; ++round)
|
236
|
4128 {
|
339
|
4129 if (round == 1)
|
|
4130 tree = spin->si_foldroot;
|
|
4131 else if (round == 2)
|
|
4132 tree = spin->si_keeproot;
|
|
4133 else
|
|
4134 tree = spin->si_prefroot;
|
236
|
4135
|
346
|
4136 /* Clear the index and wnode fields in the tree. */
|
|
4137 clear_node(tree);
|
|
4138
|
300
|
4139 /* Count the number of nodes. Needed to be able to allocate the
|
346
|
4140 * memory when reading the nodes. Also fills in index for shared
|
300
|
4141 * nodes. */
|
346
|
4142 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
|
236
|
4143
|
300
|
4144 /* number of nodes in 4 bytes */
|
|
4145 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
|
302
|
4146 spin->si_memtot += nodecount + nodecount * sizeof(int);
|
236
|
4147
|
300
|
4148 /* Write the nodes. */
|
346
|
4149 (void)put_node(fd, tree, 0, regionmask, round == 3);
|
236
|
4150 }
|
|
4151
|
300
|
4152 fclose(fd);
|
236
|
4153 }
|
|
4154
|
|
4155 /*
|
346
|
4156 * Clear the index and wnode fields of "node", it siblings and its
|
|
4157 * children. This is needed because they are a union with other items to save
|
|
4158 * space.
|
|
4159 */
|
|
4160 static void
|
|
4161 clear_node(node)
|
|
4162 wordnode_T *node;
|
|
4163 {
|
|
4164 wordnode_T *np;
|
|
4165
|
|
4166 if (node != NULL)
|
|
4167 for (np = node; np != NULL; np = np->wn_sibling)
|
|
4168 {
|
|
4169 np->wn_u1.index = 0;
|
|
4170 np->wn_u2.wnode = NULL;
|
|
4171
|
|
4172 if (np->wn_byte != NUL)
|
|
4173 clear_node(np->wn_child);
|
|
4174 }
|
|
4175 }
|
|
4176
|
|
4177
|
|
4178 /*
|
300
|
4179 * Dump a word tree at node "node".
|
|
4180 *
|
|
4181 * This first writes the list of possible bytes (siblings). Then for each
|
|
4182 * byte recursively write the children.
|
|
4183 *
|
|
4184 * NOTE: The code here must match the code in read_tree(), since assumptions
|
|
4185 * are made about the indexes (so that we don't have to write them in the
|
|
4186 * file).
|
236
|
4187 *
|
300
|
4188 * Returns the number of nodes used.
|
236
|
4189 */
|
300
|
4190 static int
|
346
|
4191 put_node(fd, node, index, regionmask, prefixtree)
|
339
|
4192 FILE *fd; /* NULL when only counting */
|
300
|
4193 wordnode_T *node;
|
|
4194 int index;
|
|
4195 int regionmask;
|
339
|
4196 int prefixtree; /* TRUE for PREFIXTREE */
|
236
|
4197 {
|
300
|
4198 int newindex = index;
|
|
4199 int siblingcount = 0;
|
|
4200 wordnode_T *np;
|
236
|
4201 int flags;
|
300
|
4202
|
|
4203 /* If "node" is zero the tree is empty. */
|
|
4204 if (node == NULL)
|
|
4205 return 0;
|
|
4206
|
|
4207 /* Store the index where this node is written. */
|
346
|
4208 node->wn_u1.index = index;
|
236
|
4209
|
300
|
4210 /* Count the number of siblings. */
|
|
4211 for (np = node; np != NULL; np = np->wn_sibling)
|
|
4212 ++siblingcount;
|
236
|
4213
|
300
|
4214 /* Write the sibling count. */
|
|
4215 if (fd != NULL)
|
|
4216 putc(siblingcount, fd); /* <siblingcount> */
|
236
|
4217
|
300
|
4218 /* Write each sibling byte and optionally extra info. */
|
|
4219 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
4220 {
|
300
|
4221 if (np->wn_byte == 0)
|
|
4222 {
|
|
4223 if (fd != NULL)
|
|
4224 {
|
339
|
4225 /* For a NUL byte (end of word) write the flags etc. */
|
|
4226 if (prefixtree)
|
300
|
4227 {
|
339
|
4228 /* In PREFIXTREE write the required prefixID and the
|
|
4229 * associated condition nr (stored in wn_region). */
|
|
4230 putc(BY_FLAGS, fd); /* <byte> */
|
|
4231 putc(np->wn_prefixID, fd); /* <prefixID> */
|
|
4232 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
|
300
|
4233 }
|
|
4234 else
|
|
4235 {
|
339
|
4236 /* For word trees we write the flag/region items. */
|
|
4237 flags = np->wn_flags;
|
|
4238 if (regionmask != 0 && np->wn_region != regionmask)
|
|
4239 flags |= WF_REGION;
|
|
4240 if (np->wn_prefixID != 0)
|
|
4241 flags |= WF_PFX;
|
|
4242 if (flags == 0)
|
|
4243 {
|
|
4244 /* word without flags or region */
|
|
4245 putc(BY_NOFLAGS, fd); /* <byte> */
|
|
4246 }
|
|
4247 else
|
|
4248 {
|
|
4249 putc(BY_FLAGS, fd); /* <byte> */
|
|
4250 putc(flags, fd); /* <flags> */
|
|
4251 if (flags & WF_REGION)
|
|
4252 putc(np->wn_region, fd); /* <region> */
|
|
4253 if (flags & WF_PFX)
|
|
4254 putc(np->wn_prefixID, fd); /* <prefixID> */
|
|
4255 }
|
300
|
4256 }
|
|
4257 }
|
|
4258 }
|
|
4259 else
|
|
4260 {
|
346
|
4261 if (np->wn_child->wn_u1.index != 0
|
|
4262 && np->wn_child->wn_u2.wnode != node)
|
300
|
4263 {
|
|
4264 /* The child is written elsewhere, write the reference. */
|
|
4265 if (fd != NULL)
|
|
4266 {
|
|
4267 putc(BY_INDEX, fd); /* <byte> */
|
|
4268 /* <nodeidx> */
|
346
|
4269 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
|
300
|
4270 }
|
|
4271 }
|
346
|
4272 else if (np->wn_child->wn_u2.wnode == NULL)
|
300
|
4273 /* We will write the child below and give it an index. */
|
346
|
4274 np->wn_child->wn_u2.wnode = node;
|
236
|
4275
|
300
|
4276 if (fd != NULL)
|
|
4277 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
|
|
4278 {
|
|
4279 EMSG(_(e_write));
|
|
4280 return 0;
|
|
4281 }
|
|
4282 }
|
236
|
4283 }
|
|
4284
|
300
|
4285 /* Space used in the array when reading: one for each sibling and one for
|
|
4286 * the count. */
|
|
4287 newindex += siblingcount + 1;
|
249
|
4288
|
300
|
4289 /* Recursively dump the children of each sibling. */
|
|
4290 for (np = node; np != NULL; np = np->wn_sibling)
|
346
|
4291 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
|
|
4292 newindex = put_node(fd, np->wn_child, newindex, regionmask,
|
339
|
4293 prefixtree);
|
249
|
4294
|
300
|
4295 return newindex;
|
236
|
4296 }
|
|
4297
|
|
4298
|
|
4299 /*
|
310
|
4300 * ":mkspell [-ascii] outfile infile ..."
|
|
4301 * ":mkspell [-ascii] addfile"
|
236
|
4302 */
|
|
4303 void
|
|
4304 ex_mkspell(eap)
|
|
4305 exarg_T *eap;
|
|
4306 {
|
|
4307 int fcount;
|
|
4308 char_u **fnames;
|
310
|
4309 char_u *arg = eap->arg;
|
|
4310 int ascii = FALSE;
|
|
4311
|
|
4312 if (STRNCMP(arg, "-ascii", 6) == 0)
|
|
4313 {
|
|
4314 ascii = TRUE;
|
|
4315 arg = skipwhite(arg + 6);
|
|
4316 }
|
|
4317
|
|
4318 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
|
|
4319 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
|
|
4320 {
|
323
|
4321 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
|
310
|
4322 FreeWild(fcount, fnames);
|
|
4323 }
|
|
4324 }
|
|
4325
|
|
4326 /*
|
|
4327 * Create a Vim spell file from one or more word lists.
|
|
4328 * "fnames[0]" is the output file name.
|
|
4329 * "fnames[fcount - 1]" is the last input file name.
|
|
4330 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
|
|
4331 * and ".spl" is appended to make the output file name.
|
|
4332 */
|
|
4333 static void
|
323
|
4334 mkspell(fcount, fnames, ascii, overwrite, added_word)
|
310
|
4335 int fcount;
|
|
4336 char_u **fnames;
|
|
4337 int ascii; /* -ascii argument given */
|
|
4338 int overwrite; /* overwrite existing output file */
|
323
|
4339 int added_word; /* invoked through "zg" */
|
310
|
4340 {
|
236
|
4341 char_u fname[MAXPATHL];
|
|
4342 char_u wfname[MAXPATHL];
|
310
|
4343 char_u **innames;
|
|
4344 int incount;
|
236
|
4345 afffile_T *(afile[8]);
|
|
4346 int i;
|
|
4347 int len;
|
|
4348 struct stat st;
|
255
|
4349 int error = FALSE;
|
300
|
4350 spellinfo_T spin;
|
|
4351
|
|
4352 vim_memset(&spin, 0, sizeof(spin));
|
323
|
4353 spin.si_verbose = !added_word;
|
310
|
4354 spin.si_ascii = ascii;
|
323
|
4355 spin.si_followup = TRUE;
|
|
4356 spin.si_rem_accents = TRUE;
|
|
4357 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
|
|
4358 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
|
|
4359 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
|
339
|
4360 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
|
310
|
4361
|
|
4362 /* default: fnames[0] is output file, following are input files */
|
|
4363 innames = &fnames[1];
|
|
4364 incount = fcount - 1;
|
|
4365
|
|
4366 if (fcount >= 1)
|
240
|
4367 {
|
310
|
4368 len = STRLEN(fnames[0]);
|
|
4369 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
|
|
4370 {
|
|
4371 /* For ":mkspell path/en.latin1.add" output file is
|
|
4372 * "path/en.latin1.add.spl". */
|
|
4373 innames = &fnames[0];
|
|
4374 incount = 1;
|
|
4375 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
|
|
4376 }
|
|
4377 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
|
|
4378 {
|
|
4379 /* Name ends in ".spl", use as the file name. */
|
323
|
4380 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
|
310
|
4381 }
|
|
4382 else
|
|
4383 /* Name should be language, make the file name from it. */
|
|
4384 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
|
|
4385 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
|
|
4386
|
|
4387 /* Check for .ascii.spl. */
|
|
4388 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
|
|
4389 spin.si_ascii = TRUE;
|
|
4390
|
|
4391 /* Check for .add.spl. */
|
|
4392 if (strstr((char *)gettail(wfname), ".add.") != NULL)
|
|
4393 spin.si_add = TRUE;
|
240
|
4394 }
|
|
4395
|
310
|
4396 if (incount <= 0)
|
236
|
4397 EMSG(_(e_invarg)); /* need at least output and input names */
|
310
|
4398 else if (incount > 8)
|
236
|
4399 EMSG(_("E754: Only up to 8 regions supported"));
|
|
4400 else
|
|
4401 {
|
|
4402 /* Check for overwriting before doing things that may take a lot of
|
|
4403 * time. */
|
310
|
4404 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
|
236
|
4405 {
|
|
4406 EMSG(_(e_exists));
|
310
|
4407 return;
|
236
|
4408 }
|
310
|
4409 if (mch_isdir(wfname))
|
236
|
4410 {
|
310
|
4411 EMSG2(_(e_isadir2), wfname);
|
|
4412 return;
|
236
|
4413 }
|
|
4414
|
|
4415 /*
|
|
4416 * Init the aff and dic pointers.
|
|
4417 * Get the region names if there are more than 2 arguments.
|
|
4418 */
|
310
|
4419 for (i = 0; i < incount; ++i)
|
236
|
4420 {
|
310
|
4421 afile[i] = NULL;
|
300
|
4422
|
316
|
4423 if (incount > 1)
|
236
|
4424 {
|
310
|
4425 len = STRLEN(innames[i]);
|
|
4426 if (STRLEN(gettail(innames[i])) < 5
|
|
4427 || innames[i][len - 3] != '_')
|
236
|
4428 {
|
310
|
4429 EMSG2(_("E755: Invalid region in %s"), innames[i]);
|
|
4430 return;
|
236
|
4431 }
|
316
|
4432 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
|
|
4433 spin.si_region_name[i * 2 + 1] =
|
|
4434 TOLOWER_ASC(innames[i][len - 1]);
|
236
|
4435 }
|
|
4436 }
|
316
|
4437 spin.si_region_count = incount;
|
236
|
4438
|
310
|
4439 if (!spin.si_add)
|
|
4440 /* Clear the char type tables, don't want to use any of the
|
|
4441 * currently used spell properties. */
|
|
4442 init_spell_chartab();
|
255
|
4443
|
300
|
4444 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
|
|
4445 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
|
339
|
4446 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
|
|
4447 if (spin.si_foldroot == NULL
|
|
4448 || spin.si_keeproot == NULL
|
|
4449 || spin.si_prefroot == NULL)
|
300
|
4450 {
|
|
4451 error = TRUE;
|
310
|
4452 return;
|
300
|
4453 }
|
|
4454
|
236
|
4455 /*
|
|
4456 * Read all the .aff and .dic files.
|
|
4457 * Text is converted to 'encoding'.
|
300
|
4458 * Words are stored in the case-folded and keep-case trees.
|
236
|
4459 */
|
310
|
4460 for (i = 0; i < incount && !error; ++i)
|
236
|
4461 {
|
300
|
4462 spin.si_conv.vc_type = CONV_NONE;
|
310
|
4463 spin.si_region = 1 << i;
|
|
4464
|
|
4465 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
|
300
|
4466 if (mch_stat((char *)fname, &st) >= 0)
|
|
4467 {
|
|
4468 /* Read the .aff file. Will init "spin->si_conv" based on the
|
|
4469 * "SET" line. */
|
310
|
4470 afile[i] = spell_read_aff(fname, &spin);
|
|
4471 if (afile[i] == NULL)
|
300
|
4472 error = TRUE;
|
|
4473 else
|
|
4474 {
|
|
4475 /* Read the .dic file and store the words in the trees. */
|
|
4476 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
|
310
|
4477 innames[i]);
|
|
4478 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
|
300
|
4479 error = TRUE;
|
|
4480 }
|
|
4481 }
|
|
4482 else
|
|
4483 {
|
|
4484 /* No .aff file, try reading the file as a word list. Store
|
|
4485 * the words in the trees. */
|
310
|
4486 if (spell_read_wordfile(innames[i], &spin) == FAIL)
|
300
|
4487 error = TRUE;
|
|
4488 }
|
236
|
4489
|
310
|
4490 #ifdef FEAT_MBYTE
|
236
|
4491 /* Free any conversion stuff. */
|
300
|
4492 convert_setup(&spin.si_conv, NULL, NULL);
|
310
|
4493 #endif
|
236
|
4494 }
|
|
4495
|
300
|
4496 if (!error)
|
236
|
4497 {
|
|
4498 /*
|
300
|
4499 * Remove the dummy NUL from the start of the tree root.
|
236
|
4500 */
|
300
|
4501 spin.si_foldroot = spin.si_foldroot->wn_sibling;
|
|
4502 spin.si_keeproot = spin.si_keeproot->wn_sibling;
|
339
|
4503 spin.si_prefroot = spin.si_prefroot->wn_sibling;
|
236
|
4504
|
|
4505 /*
|
300
|
4506 * Combine tails in the tree.
|
236
|
4507 */
|
323
|
4508 if (!added_word || p_verbose > 2)
|
310
|
4509 {
|
323
|
4510 if (added_word)
|
310
|
4511 verbose_enter();
|
|
4512 MSG(_("Compressing word tree..."));
|
|
4513 out_flush();
|
323
|
4514 if (added_word)
|
310
|
4515 verbose_leave();
|
|
4516 }
|
|
4517 wordtree_compress(spin.si_foldroot, &spin);
|
|
4518 wordtree_compress(spin.si_keeproot, &spin);
|
339
|
4519 wordtree_compress(spin.si_prefroot, &spin);
|
236
|
4520 }
|
|
4521
|
300
|
4522 if (!error)
|
|
4523 {
|
|
4524 /*
|
|
4525 * Write the info in the spell file.
|
|
4526 */
|
323
|
4527 if (!added_word || p_verbose > 2)
|
310
|
4528 {
|
323
|
4529 if (added_word)
|
310
|
4530 verbose_enter();
|
|
4531 smsg((char_u *)_("Writing spell file %s..."), wfname);
|
|
4532 out_flush();
|
323
|
4533 if (added_word)
|
310
|
4534 verbose_leave();
|
|
4535 }
|
|
4536
|
316
|
4537 write_vim_spell(wfname, &spin);
|
310
|
4538
|
323
|
4539 if (!added_word || p_verbose > 2)
|
310
|
4540 {
|
323
|
4541 if (added_word)
|
310
|
4542 verbose_enter();
|
|
4543 MSG(_("Done!"));
|
|
4544 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
|
302
|
4545 spin.si_memtot);
|
310
|
4546 out_flush();
|
323
|
4547 if (added_word)
|
310
|
4548 verbose_leave();
|
|
4549 }
|
|
4550
|
|
4551 /* If the file is loaded need to reload it. */
|
323
|
4552 spell_reload_one(wfname, added_word);
|
300
|
4553 }
|
|
4554
|
|
4555 /* Free the allocated memory. */
|
323
|
4556 ga_clear(&spin.si_rep);
|
|
4557 ga_clear(&spin.si_sal);
|
|
4558 ga_clear(&spin.si_map);
|
339
|
4559 ga_clear(&spin.si_prefcond);
|
300
|
4560
|
|
4561 /* Free the .aff file structures. */
|
310
|
4562 for (i = 0; i < incount; ++i)
|
|
4563 if (afile[i] != NULL)
|
|
4564 spell_free_aff(afile[i]);
|
339
|
4565
|
|
4566 /* Free all the bits and pieces at once. */
|
|
4567 free_blocks(spin.si_blocks);
|
236
|
4568 }
|
310
|
4569 }
|
|
4570
|
|
4571
|
|
4572 /*
|
|
4573 * ":spellgood {word}"
|
|
4574 * ":spellwrong {word}"
|
|
4575 */
|
|
4576 void
|
|
4577 ex_spell(eap)
|
|
4578 exarg_T *eap;
|
|
4579 {
|
|
4580 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
|
236
|
4581 }
|
|
4582
|
310
|
4583 /*
|
|
4584 * Add "word[len]" to 'spellfile' as a good or bad word.
|
|
4585 */
|
|
4586 void
|
|
4587 spell_add_word(word, len, bad)
|
|
4588 char_u *word;
|
|
4589 int len;
|
|
4590 int bad;
|
|
4591 {
|
|
4592 FILE *fd;
|
|
4593 buf_T *buf;
|
|
4594
|
|
4595 if (*curbuf->b_p_spf == NUL)
|
|
4596 init_spellfile();
|
|
4597 if (*curbuf->b_p_spf == NUL)
|
323
|
4598 EMSG(_("E764: 'spellfile' is not set"));
|
310
|
4599 else
|
|
4600 {
|
|
4601 /* Check that the user isn't editing the .add file somewhere. */
|
|
4602 buf = buflist_findname_exp(curbuf->b_p_spf);
|
|
4603 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
|
|
4604 buf = NULL;
|
|
4605 if (buf != NULL && bufIsChanged(buf))
|
|
4606 EMSG(_(e_bufloaded));
|
|
4607 else
|
|
4608 {
|
|
4609 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
|
|
4610 if (fd == NULL)
|
|
4611 EMSG2(_(e_notopen), curbuf->b_p_spf);
|
|
4612 else
|
|
4613 {
|
|
4614 if (bad)
|
|
4615 fprintf(fd, "/!%.*s\n", len, word);
|
|
4616 else
|
|
4617 fprintf(fd, "%.*s\n", len, word);
|
|
4618 fclose(fd);
|
|
4619
|
|
4620 /* Update the .add.spl file. */
|
323
|
4621 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
|
310
|
4622
|
|
4623 /* If the .add file is edited somewhere, reload it. */
|
|
4624 if (buf != NULL)
|
|
4625 buf_reload(buf);
|
323
|
4626
|
|
4627 redraw_all_later(NOT_VALID);
|
310
|
4628 }
|
|
4629 }
|
|
4630 }
|
|
4631 }
|
|
4632
|
|
4633 /*
|
|
4634 * Initialize 'spellfile' for the current buffer.
|
|
4635 */
|
|
4636 static void
|
|
4637 init_spellfile()
|
|
4638 {
|
|
4639 char_u buf[MAXPATHL];
|
|
4640 int l;
|
|
4641 slang_T *sl;
|
|
4642 char_u *rtp;
|
|
4643
|
|
4644 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
|
|
4645 {
|
|
4646 /* Loop over all entries in 'runtimepath'. */
|
|
4647 rtp = p_rtp;
|
|
4648 while (*rtp != NUL)
|
|
4649 {
|
|
4650 /* Copy the path from 'runtimepath' to buf[]. */
|
|
4651 copy_option_part(&rtp, buf, MAXPATHL, ",");
|
|
4652 if (filewritable(buf) == 2)
|
|
4653 {
|
316
|
4654 /* Use the first language name from 'spelllang' and the
|
|
4655 * encoding used in the first loaded .spl file. */
|
310
|
4656 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
|
|
4657 l = STRLEN(buf);
|
|
4658 vim_snprintf((char *)buf + l, MAXPATHL - l,
|
316
|
4659 "/spell/%.*s.%s.add",
|
|
4660 2, curbuf->b_p_spl,
|
310
|
4661 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
|
|
4662 ? (char_u *)"ascii" : spell_enc());
|
|
4663 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
|
|
4664 break;
|
|
4665 }
|
|
4666 }
|
|
4667 }
|
|
4668 }
|
236
|
4669
|
300
|
4670
|
307
|
4671 /*
|
|
4672 * Init the chartab used for spelling for ASCII.
|
|
4673 * EBCDIC is not supported!
|
|
4674 */
|
|
4675 static void
|
|
4676 clear_spell_chartab(sp)
|
|
4677 spelltab_T *sp;
|
|
4678 {
|
324
|
4679 int i;
|
307
|
4680
|
|
4681 /* Init everything to FALSE. */
|
|
4682 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
|
|
4683 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
|
|
4684 for (i = 0; i < 256; ++i)
|
324
|
4685 {
|
307
|
4686 sp->st_fold[i] = i;
|
324
|
4687 sp->st_upper[i] = i;
|
|
4688 }
|
307
|
4689
|
|
4690 /* We include digits. A word shouldn't start with a digit, but handling
|
|
4691 * that is done separately. */
|
|
4692 for (i = '0'; i <= '9'; ++i)
|
|
4693 sp->st_isw[i] = TRUE;
|
|
4694 for (i = 'A'; i <= 'Z'; ++i)
|
|
4695 {
|
|
4696 sp->st_isw[i] = TRUE;
|
|
4697 sp->st_isu[i] = TRUE;
|
|
4698 sp->st_fold[i] = i + 0x20;
|
|
4699 }
|
|
4700 for (i = 'a'; i <= 'z'; ++i)
|
324
|
4701 {
|
307
|
4702 sp->st_isw[i] = TRUE;
|
324
|
4703 sp->st_upper[i] = i - 0x20;
|
|
4704 }
|
307
|
4705 }
|
|
4706
|
|
4707 /*
|
|
4708 * Init the chartab used for spelling. Only depends on 'encoding'.
|
|
4709 * Called once while starting up and when 'encoding' changes.
|
|
4710 * The default is to use isalpha(), but the spell file should define the word
|
|
4711 * characters to make it possible that 'encoding' differs from the current
|
|
4712 * locale.
|
|
4713 */
|
|
4714 void
|
|
4715 init_spell_chartab()
|
|
4716 {
|
|
4717 int i;
|
|
4718
|
|
4719 did_set_spelltab = FALSE;
|
|
4720 clear_spell_chartab(&spelltab);
|
|
4721
|
|
4722 #ifdef FEAT_MBYTE
|
|
4723 if (enc_dbcs)
|
|
4724 {
|
|
4725 /* DBCS: assume double-wide characters are word characters. */
|
|
4726 for (i = 128; i <= 255; ++i)
|
|
4727 if (MB_BYTE2LEN(i) == 2)
|
|
4728 spelltab.st_isw[i] = TRUE;
|
|
4729 }
|
324
|
4730 else if (enc_utf8)
|
|
4731 {
|
|
4732 for (i = 128; i < 256; ++i)
|
|
4733 {
|
|
4734 spelltab.st_isu[i] = utf_isupper(i);
|
|
4735 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
|
|
4736 spelltab.st_fold[i] = utf_fold(i);
|
|
4737 spelltab.st_upper[i] = utf_toupper(i);
|
|
4738 }
|
|
4739 }
|
307
|
4740 else
|
|
4741 #endif
|
|
4742 {
|
324
|
4743 /* Rough guess: use locale-dependent library functions. */
|
307
|
4744 for (i = 128; i < 256; ++i)
|
|
4745 {
|
|
4746 if (MB_ISUPPER(i))
|
|
4747 {
|
324
|
4748 spelltab.st_isw[i] = TRUE;
|
307
|
4749 spelltab.st_isu[i] = TRUE;
|
|
4750 spelltab.st_fold[i] = MB_TOLOWER(i);
|
|
4751 }
|
324
|
4752 else if (MB_ISLOWER(i))
|
|
4753 {
|
|
4754 spelltab.st_isw[i] = TRUE;
|
|
4755 spelltab.st_upper[i] = MB_TOUPPER(i);
|
|
4756 }
|
307
|
4757 }
|
|
4758 }
|
|
4759 }
|
|
4760
|
|
4761 static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
|
|
4762 static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
|
|
4763
|
|
4764 /*
|
|
4765 * Set the spell character tables from strings in the affix file.
|
|
4766 */
|
|
4767 static int
|
|
4768 set_spell_chartab(fol, low, upp)
|
|
4769 char_u *fol;
|
|
4770 char_u *low;
|
|
4771 char_u *upp;
|
|
4772 {
|
|
4773 /* We build the new tables here first, so that we can compare with the
|
|
4774 * previous one. */
|
|
4775 spelltab_T new_st;
|
|
4776 char_u *pf = fol, *pl = low, *pu = upp;
|
|
4777 int f, l, u;
|
|
4778
|
|
4779 clear_spell_chartab(&new_st);
|
|
4780
|
|
4781 while (*pf != NUL)
|
|
4782 {
|
|
4783 if (*pl == NUL || *pu == NUL)
|
|
4784 {
|
|
4785 EMSG(_(e_affform));
|
|
4786 return FAIL;
|
|
4787 }
|
|
4788 #ifdef FEAT_MBYTE
|
|
4789 f = mb_ptr2char_adv(&pf);
|
|
4790 l = mb_ptr2char_adv(&pl);
|
|
4791 u = mb_ptr2char_adv(&pu);
|
|
4792 #else
|
|
4793 f = *pf++;
|
|
4794 l = *pl++;
|
|
4795 u = *pu++;
|
|
4796 #endif
|
|
4797 /* Every character that appears is a word character. */
|
|
4798 if (f < 256)
|
|
4799 new_st.st_isw[f] = TRUE;
|
|
4800 if (l < 256)
|
|
4801 new_st.st_isw[l] = TRUE;
|
|
4802 if (u < 256)
|
|
4803 new_st.st_isw[u] = TRUE;
|
|
4804
|
|
4805 /* if "LOW" and "FOL" are not the same the "LOW" char needs
|
|
4806 * case-folding */
|
|
4807 if (l < 256 && l != f)
|
|
4808 {
|
|
4809 if (f >= 256)
|
|
4810 {
|
|
4811 EMSG(_(e_affrange));
|
|
4812 return FAIL;
|
|
4813 }
|
|
4814 new_st.st_fold[l] = f;
|
|
4815 }
|
|
4816
|
|
4817 /* if "UPP" and "FOL" are not the same the "UPP" char needs
|
324
|
4818 * case-folding, it's upper case and the "UPP" is the upper case of
|
|
4819 * "FOL" . */
|
307
|
4820 if (u < 256 && u != f)
|
|
4821 {
|
|
4822 if (f >= 256)
|
|
4823 {
|
|
4824 EMSG(_(e_affrange));
|
|
4825 return FAIL;
|
|
4826 }
|
|
4827 new_st.st_fold[u] = f;
|
|
4828 new_st.st_isu[u] = TRUE;
|
324
|
4829 new_st.st_upper[f] = u;
|
307
|
4830 }
|
|
4831 }
|
|
4832
|
|
4833 if (*pl != NUL || *pu != NUL)
|
|
4834 {
|
|
4835 EMSG(_(e_affform));
|
|
4836 return FAIL;
|
|
4837 }
|
|
4838
|
|
4839 return set_spell_finish(&new_st);
|
|
4840 }
|
|
4841
|
|
4842 /*
|
|
4843 * Set the spell character tables from strings in the .spl file.
|
|
4844 */
|
|
4845 static int
|
|
4846 set_spell_charflags(flags, cnt, upp)
|
|
4847 char_u *flags;
|
|
4848 int cnt;
|
|
4849 char_u *upp;
|
|
4850 {
|
|
4851 /* We build the new tables here first, so that we can compare with the
|
|
4852 * previous one. */
|
|
4853 spelltab_T new_st;
|
|
4854 int i;
|
|
4855 char_u *p = upp;
|
324
|
4856 int c;
|
307
|
4857
|
|
4858 clear_spell_chartab(&new_st);
|
|
4859
|
|
4860 for (i = 0; i < cnt; ++i)
|
|
4861 {
|
324
|
4862 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
|
|
4863 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
|
307
|
4864
|
|
4865 if (*p == NUL)
|
|
4866 return FAIL;
|
|
4867 #ifdef FEAT_MBYTE
|
324
|
4868 c = mb_ptr2char_adv(&p);
|
307
|
4869 #else
|
324
|
4870 c = *p++;
|
307
|
4871 #endif
|
324
|
4872 new_st.st_fold[i + 128] = c;
|
|
4873 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
|
|
4874 new_st.st_upper[c] = i + 128;
|
307
|
4875 }
|
|
4876
|
|
4877 return set_spell_finish(&new_st);
|
|
4878 }
|
|
4879
|
|
4880 static int
|
|
4881 set_spell_finish(new_st)
|
|
4882 spelltab_T *new_st;
|
|
4883 {
|
|
4884 int i;
|
|
4885
|
|
4886 if (did_set_spelltab)
|
|
4887 {
|
|
4888 /* check that it's the same table */
|
|
4889 for (i = 0; i < 256; ++i)
|
|
4890 {
|
|
4891 if (spelltab.st_isw[i] != new_st->st_isw[i]
|
|
4892 || spelltab.st_isu[i] != new_st->st_isu[i]
|
324
|
4893 || spelltab.st_fold[i] != new_st->st_fold[i]
|
|
4894 || spelltab.st_upper[i] != new_st->st_upper[i])
|
307
|
4895 {
|
|
4896 EMSG(_("E763: Word characters differ between spell files"));
|
|
4897 return FAIL;
|
|
4898 }
|
|
4899 }
|
|
4900 }
|
|
4901 else
|
|
4902 {
|
|
4903 /* copy the new spelltab into the one being used */
|
|
4904 spelltab = *new_st;
|
|
4905 did_set_spelltab = TRUE;
|
|
4906 }
|
|
4907
|
|
4908 return OK;
|
|
4909 }
|
|
4910
|
|
4911 /*
|
339
|
4912 * Write the table with prefix conditions to the .spl file.
|
|
4913 */
|
|
4914 static void
|
|
4915 write_spell_prefcond(fd, gap)
|
|
4916 FILE *fd;
|
|
4917 garray_T *gap;
|
|
4918 {
|
|
4919 int i;
|
|
4920 char_u *p;
|
|
4921 int len;
|
|
4922
|
|
4923 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
|
|
4924
|
|
4925 for (i = 0; i < gap->ga_len; ++i)
|
|
4926 {
|
|
4927 /* <prefcond> : <condlen> <condstr> */
|
|
4928 p = ((char_u **)gap->ga_data)[i];
|
|
4929 if (p == NULL)
|
|
4930 fputc(0, fd);
|
|
4931 else
|
|
4932 {
|
|
4933 len = STRLEN(p);
|
|
4934 fputc(len, fd);
|
|
4935 fwrite(p, (size_t)len, (size_t)1, fd);
|
|
4936 }
|
|
4937 }
|
|
4938 }
|
|
4939
|
|
4940 /*
|
307
|
4941 * Write the current tables into the .spl file.
|
|
4942 * This makes sure the same characters are recognized as word characters when
|
|
4943 * generating an when using a spell file.
|
|
4944 */
|
|
4945 static void
|
|
4946 write_spell_chartab(fd)
|
|
4947 FILE *fd;
|
|
4948 {
|
|
4949 char_u charbuf[256 * 4];
|
|
4950 int len = 0;
|
|
4951 int flags;
|
|
4952 int i;
|
|
4953
|
|
4954 fputc(128, fd); /* <charflagslen> */
|
|
4955 for (i = 128; i < 256; ++i)
|
|
4956 {
|
|
4957 flags = 0;
|
|
4958 if (spelltab.st_isw[i])
|
324
|
4959 flags |= CF_WORD;
|
307
|
4960 if (spelltab.st_isu[i])
|
324
|
4961 flags |= CF_UPPER;
|
307
|
4962 fputc(flags, fd); /* <charflags> */
|
|
4963
|
310
|
4964 #ifdef FEAT_MBYTE
|
|
4965 if (has_mbyte)
|
|
4966 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
|
|
4967 else
|
|
4968 #endif
|
|
4969 charbuf[len++] = spelltab.st_fold[i];
|
307
|
4970 }
|
|
4971
|
|
4972 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
|
|
4973 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
|
|
4974 }
|
|
4975
|
|
4976 /*
|
324
|
4977 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
|
|
4978 * Uses the character definitions from the .spl file.
|
307
|
4979 * When using a multi-byte 'encoding' the length may change!
|
|
4980 * Returns FAIL when something wrong.
|
|
4981 */
|
|
4982 static int
|
324
|
4983 spell_casefold(str, len, buf, buflen)
|
|
4984 char_u *str;
|
307
|
4985 int len;
|
|
4986 char_u *buf;
|
|
4987 int buflen;
|
|
4988 {
|
|
4989 int i;
|
|
4990
|
|
4991 if (len >= buflen)
|
|
4992 {
|
|
4993 buf[0] = NUL;
|
|
4994 return FAIL; /* result will not fit */
|
|
4995 }
|
|
4996
|
|
4997 #ifdef FEAT_MBYTE
|
|
4998 if (has_mbyte)
|
|
4999 {
|
324
|
5000 int outi = 0;
|
|
5001 char_u *p;
|
307
|
5002 int c;
|
|
5003
|
|
5004 /* Fold one character at a time. */
|
324
|
5005 for (p = str; p < str + len; )
|
307
|
5006 {
|
|
5007 if (outi + MB_MAXBYTES > buflen)
|
|
5008 {
|
|
5009 buf[outi] = NUL;
|
|
5010 return FAIL;
|
|
5011 }
|
324
|
5012 c = mb_ptr2char_adv(&p);
|
|
5013 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
|
307
|
5014 }
|
|
5015 buf[outi] = NUL;
|
|
5016 }
|
|
5017 else
|
|
5018 #endif
|
|
5019 {
|
|
5020 /* Be quick for non-multibyte encodings. */
|
|
5021 for (i = 0; i < len; ++i)
|
324
|
5022 buf[i] = spelltab.st_fold[str[i]];
|
307
|
5023 buf[i] = NUL;
|
|
5024 }
|
|
5025
|
|
5026 return OK;
|
|
5027 }
|
|
5028
|
323
|
5029 /*
|
|
5030 * "z?": Find badly spelled word under or after the cursor.
|
|
5031 * Give suggestions for the properly spelled word.
|
|
5032 */
|
|
5033 void
|
|
5034 spell_suggest()
|
|
5035 {
|
|
5036 char_u *line;
|
|
5037 pos_T prev_cursor = curwin->w_cursor;
|
|
5038 char_u wcopy[MAXWLEN + 2];
|
|
5039 char_u *p;
|
|
5040 int i;
|
|
5041 int c;
|
|
5042 suginfo_T sug;
|
|
5043 suggest_T *stp;
|
|
5044
|
344
|
5045 /* Find the start of the badly spelled word. */
|
346
|
5046 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
|
|
5047 || curwin->w_cursor.col > prev_cursor.col)
|
|
5048 {
|
|
5049 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
|
|
5050 return;
|
|
5051
|
|
5052 /* No bad word or it starts after the cursor: use the word under the
|
|
5053 * cursor. */
|
|
5054 curwin->w_cursor = prev_cursor;
|
|
5055 line = ml_get_curline();
|
|
5056 p = line + curwin->w_cursor.col;
|
|
5057 /* Backup to before start of word. */
|
|
5058 while (p > line && SPELL_ISWORDP(p))
|
|
5059 mb_ptr_back(line, p);
|
|
5060 /* Forward to start of word. */
|
|
5061 while (!SPELL_ISWORDP(p))
|
|
5062 mb_ptr_adv(p);
|
|
5063
|
|
5064 if (!SPELL_ISWORDP(p)) /* No word found. */
|
|
5065 {
|
|
5066 beep_flush();
|
|
5067 return;
|
|
5068 }
|
|
5069 curwin->w_cursor.col = p - line;
|
323
|
5070 }
|
|
5071
|
344
|
5072 /* Get the word and its length. */
|
323
|
5073 line = ml_get_curline();
|
344
|
5074
|
|
5075 /* Get the list of suggestions */
|
|
5076 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2);
|
323
|
5077
|
|
5078 if (sug.su_ga.ga_len == 0)
|
|
5079 MSG(_("Sorry, no suggestions"));
|
|
5080 else
|
|
5081 {
|
|
5082 /* List the suggestions. */
|
|
5083 msg_start();
|
|
5084 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
|
|
5085 sug.su_badlen, sug.su_badptr);
|
|
5086 msg_puts(IObuff);
|
|
5087 msg_clr_eos();
|
|
5088 msg_putchar('\n');
|
346
|
5089
|
323
|
5090 msg_scroll = TRUE;
|
|
5091 for (i = 0; i < sug.su_ga.ga_len; ++i)
|
|
5092 {
|
344
|
5093 stp = &SUG(sug.su_ga, i);
|
323
|
5094
|
|
5095 /* The suggested word may replace only part of the bad word, add
|
|
5096 * the not replaced part. */
|
|
5097 STRCPY(wcopy, stp->st_word);
|
|
5098 if (sug.su_badlen > stp->st_orglen)
|
|
5099 vim_strncpy(wcopy + STRLEN(wcopy),
|
|
5100 sug.su_badptr + stp->st_orglen,
|
|
5101 sug.su_badlen - stp->st_orglen);
|
346
|
5102 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
|
|
5103 msg_puts(IObuff);
|
|
5104
|
|
5105 /* The word may replace more than "su_badlen". */
|
|
5106 if (sug.su_badlen < stp->st_orglen)
|
|
5107 {
|
|
5108 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
|
|
5109 stp->st_orglen, sug.su_badptr);
|
|
5110 msg_puts(IObuff);
|
|
5111 }
|
|
5112
|
324
|
5113 if (p_verbose > 0)
|
344
|
5114 {
|
346
|
5115 /* Add the score. */
|
344
|
5116 if (sps_flags & SPS_DOUBLE)
|
346
|
5117 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
|
344
|
5118 stp->st_salscore ? "s " : "",
|
|
5119 stp->st_score, stp->st_altscore);
|
|
5120 else
|
346
|
5121 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
|
|
5122 stp->st_score);
|
|
5123 msg_advance(30);
|
|
5124 msg_puts(IObuff);
|
344
|
5125 }
|
323
|
5126 lines_left = 3; /* avoid more prompt */
|
|
5127 msg_putchar('\n');
|
|
5128 }
|
|
5129
|
|
5130 /* Ask for choice. */
|
|
5131 i = prompt_for_number();
|
344
|
5132 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
|
323
|
5133 {
|
|
5134 /* Replace the word. */
|
344
|
5135 stp = &SUG(sug.su_ga, i - 1);
|
323
|
5136 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
|
|
5137 if (p != NULL)
|
|
5138 {
|
|
5139 c = sug.su_badptr - line;
|
|
5140 mch_memmove(p, line, c);
|
|
5141 STRCPY(p + c, stp->st_word);
|
|
5142 STRCAT(p, sug.su_badptr + stp->st_orglen);
|
|
5143 ml_replace(curwin->w_cursor.lnum, p, FALSE);
|
|
5144 curwin->w_cursor.col = c;
|
|
5145 changed_bytes(curwin->w_cursor.lnum, c);
|
344
|
5146
|
|
5147 /* For redo we use a change-word command. */
|
|
5148 ResetRedobuff();
|
|
5149 AppendToRedobuff((char_u *)"ciw");
|
|
5150 AppendToRedobuff(stp->st_word);
|
|
5151 AppendCharToRedobuff(ESC);
|
323
|
5152 }
|
|
5153 }
|
|
5154 else
|
|
5155 curwin->w_cursor = prev_cursor;
|
|
5156 }
|
|
5157
|
344
|
5158 spell_find_cleanup(&sug);
|
|
5159 }
|
|
5160
|
|
5161 /*
|
|
5162 * Find spell suggestions for "word". Return them in the growarray "*gap" as
|
|
5163 * a list of allocated strings.
|
|
5164 */
|
|
5165 void
|
|
5166 spell_suggest_list(gap, word, maxcount)
|
|
5167 garray_T *gap;
|
|
5168 char_u *word;
|
|
5169 int maxcount; /* maximum nr of suggestions */
|
|
5170 {
|
|
5171 suginfo_T sug;
|
|
5172 int i;
|
|
5173 suggest_T *stp;
|
|
5174 char_u *wcopy;
|
|
5175
|
|
5176 spell_find_suggest(word, &sug, maxcount);
|
|
5177
|
|
5178 /* Make room in "gap". */
|
|
5179 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
|
|
5180 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
|
|
5181 return;
|
|
5182
|
323
|
5183 for (i = 0; i < sug.su_ga.ga_len; ++i)
|
344
|
5184 {
|
|
5185 stp = &SUG(sug.su_ga, i);
|
|
5186
|
|
5187 /* The suggested word may replace only part of "word", add the not
|
|
5188 * replaced part. */
|
|
5189 wcopy = alloc(STRLEN(stp->st_word)
|
|
5190 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
|
|
5191 if (wcopy == NULL)
|
|
5192 break;
|
|
5193 STRCPY(wcopy, stp->st_word);
|
|
5194 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
|
|
5195 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
|
|
5196 }
|
|
5197
|
|
5198 spell_find_cleanup(&sug);
|
|
5199 }
|
|
5200
|
|
5201 /*
|
|
5202 * Find spell suggestions for the word at the start of "badptr".
|
|
5203 * Return the suggestions in "su->su_ga".
|
|
5204 * The maximum number of suggestions is "maxcount".
|
|
5205 * Note: does use info for the current window.
|
|
5206 * This is based on the mechanisms of Aspell, but completely reimplemented.
|
|
5207 */
|
|
5208 static void
|
|
5209 spell_find_suggest(badptr, su, maxcount)
|
|
5210 char_u *badptr;
|
|
5211 suginfo_T *su;
|
|
5212 int maxcount;
|
|
5213 {
|
|
5214 int attr;
|
|
5215
|
|
5216 /*
|
|
5217 * Set the info in "*su".
|
|
5218 */
|
|
5219 vim_memset(su, 0, sizeof(suginfo_T));
|
|
5220 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
|
|
5221 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
|
|
5222 hash_init(&su->su_banned);
|
|
5223
|
|
5224 su->su_badptr = badptr;
|
|
5225 su->su_badlen = spell_check(curwin, su->su_badptr, &attr);
|
|
5226 su->su_maxcount = maxcount;
|
|
5227
|
|
5228 if (su->su_badlen >= MAXWLEN)
|
|
5229 su->su_badlen = MAXWLEN - 1; /* just in case */
|
|
5230 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
|
|
5231 (void)spell_casefold(su->su_badptr, su->su_badlen,
|
|
5232 su->su_fbadword, MAXWLEN);
|
346
|
5233 /* get caps flags for bad word */
|
|
5234 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
|
344
|
5235
|
|
5236 /* Ban the bad word itself. It may appear in another region. */
|
|
5237 add_banned(su, su->su_badword);
|
|
5238
|
|
5239 /*
|
346
|
5240 * 1. Try special cases, such as repeating a word: "the the" -> "the".
|
344
|
5241 *
|
|
5242 * Set a maximum score to limit the combination of operations that is
|
|
5243 * tried.
|
|
5244 */
|
|
5245 su->su_maxscore = SCORE_MAXINIT;
|
346
|
5246 suggest_try_special(su);
|
|
5247
|
|
5248 /*
|
|
5249 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
|
|
5250 * from the .aff file and inserting a space (split the word).
|
|
5251 */
|
|
5252 suggest_try_change(su);
|
344
|
5253
|
|
5254 /* For the resulting top-scorers compute the sound-a-like score. */
|
|
5255 if (sps_flags & SPS_DOUBLE)
|
|
5256 score_comp_sal(su);
|
|
5257
|
|
5258 /*
|
346
|
5259 * 3. Try finding sound-a-like words.
|
344
|
5260 *
|
|
5261 * Only do this when we don't have a lot of suggestions yet, because it's
|
|
5262 * very slow and often doesn't find new suggestions.
|
|
5263 */
|
|
5264 if ((sps_flags & SPS_DOUBLE)
|
|
5265 || (!(sps_flags & SPS_FAST)
|
|
5266 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
|
|
5267 {
|
|
5268 /* Allow a higher score now. */
|
|
5269 su->su_maxscore = SCORE_MAXMAX;
|
346
|
5270 suggest_try_soundalike(su);
|
344
|
5271 }
|
|
5272
|
|
5273 /* When CTRL-C was hit while searching do show the results. */
|
|
5274 ui_breakcheck();
|
|
5275 if (got_int)
|
|
5276 {
|
|
5277 (void)vgetc();
|
|
5278 got_int = FALSE;
|
|
5279 }
|
|
5280
|
|
5281 if (sps_flags & SPS_DOUBLE)
|
|
5282 {
|
|
5283 /* Combine the two list of suggestions. */
|
|
5284 score_combine(su);
|
|
5285 }
|
|
5286 else if (su->su_ga.ga_len != 0)
|
|
5287 {
|
|
5288 if (sps_flags & SPS_BEST)
|
|
5289 /* Adjust the word score for how it sounds like. */
|
|
5290 rescore_suggestions(su);
|
|
5291
|
|
5292 /* Sort the suggestions and truncate at "maxcount". */
|
|
5293 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, maxcount);
|
|
5294 }
|
|
5295 }
|
|
5296
|
|
5297 /*
|
|
5298 * Free the info put in "*su" by spell_find_suggest().
|
|
5299 */
|
|
5300 static void
|
|
5301 spell_find_cleanup(su)
|
|
5302 suginfo_T *su;
|
|
5303 {
|
|
5304 int i;
|
|
5305
|
|
5306 /* Free the suggestions. */
|
|
5307 for (i = 0; i < su->su_ga.ga_len; ++i)
|
|
5308 vim_free(SUG(su->su_ga, i).st_word);
|
|
5309 ga_clear(&su->su_ga);
|
|
5310 for (i = 0; i < su->su_sga.ga_len; ++i)
|
|
5311 vim_free(SUG(su->su_sga, i).st_word);
|
|
5312 ga_clear(&su->su_sga);
|
323
|
5313
|
|
5314 /* Free the banned words. */
|
344
|
5315 free_banned(su);
|
323
|
5316 }
|
|
5317
|
|
5318 /*
|
324
|
5319 * Make a copy of "word", with the first letter upper or lower cased, to
|
|
5320 * "wcopy[MAXWLEN]". "word" must not be empty.
|
|
5321 * The result is NUL terminated.
|
323
|
5322 */
|
|
5323 static void
|
324
|
5324 onecap_copy(word, wcopy, upper)
|
323
|
5325 char_u *word;
|
|
5326 char_u *wcopy;
|
|
5327 int upper; /* TRUE: first letter made upper case */
|
|
5328 {
|
|
5329 char_u *p;
|
|
5330 int c;
|
|
5331 int l;
|
|
5332
|
|
5333 p = word;
|
|
5334 #ifdef FEAT_MBYTE
|
|
5335 if (has_mbyte)
|
|
5336 c = mb_ptr2char_adv(&p);
|
|
5337 else
|
|
5338 #endif
|
|
5339 c = *p++;
|
|
5340 if (upper)
|
324
|
5341 c = SPELL_TOUPPER(c);
|
323
|
5342 else
|
324
|
5343 c = SPELL_TOFOLD(c);
|
323
|
5344 #ifdef FEAT_MBYTE
|
|
5345 if (has_mbyte)
|
|
5346 l = mb_char2bytes(c, wcopy);
|
|
5347 else
|
|
5348 #endif
|
|
5349 {
|
|
5350 l = 1;
|
|
5351 wcopy[0] = c;
|
|
5352 }
|
324
|
5353 vim_strncpy(wcopy + l, p, MAXWLEN - l);
|
323
|
5354 }
|
|
5355
|
|
5356 /*
|
324
|
5357 * Make a copy of "word" with all the letters upper cased into
|
|
5358 * "wcopy[MAXWLEN]". The result is NUL terminated.
|
323
|
5359 */
|
|
5360 static void
|
|
5361 allcap_copy(word, wcopy)
|
|
5362 char_u *word;
|
|
5363 char_u *wcopy;
|
|
5364 {
|
|
5365 char_u *s;
|
|
5366 char_u *d;
|
|
5367 int c;
|
|
5368
|
|
5369 d = wcopy;
|
|
5370 for (s = word; *s != NUL; )
|
|
5371 {
|
|
5372 #ifdef FEAT_MBYTE
|
|
5373 if (has_mbyte)
|
|
5374 c = mb_ptr2char_adv(&s);
|
|
5375 else
|
|
5376 #endif
|
|
5377 c = *s++;
|
324
|
5378 c = SPELL_TOUPPER(c);
|
323
|
5379
|
|
5380 #ifdef FEAT_MBYTE
|
|
5381 if (has_mbyte)
|
|
5382 {
|
|
5383 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
|
|
5384 break;
|
|
5385 d += mb_char2bytes(c, d);
|
|
5386 }
|
|
5387 else
|
|
5388 #endif
|
|
5389 {
|
|
5390 if (d - wcopy >= MAXWLEN - 1)
|
|
5391 break;
|
|
5392 *d++ = c;
|
|
5393 }
|
|
5394 }
|
|
5395 *d = NUL;
|
|
5396 }
|
|
5397
|
|
5398 /*
|
346
|
5399 * Try finding suggestions by recognizing specific situations.
|
|
5400 */
|
|
5401 static void
|
|
5402 suggest_try_special(su)
|
|
5403 suginfo_T *su;
|
|
5404 {
|
|
5405 char_u *p;
|
|
5406 int len;
|
|
5407 int c;
|
|
5408 char_u word[MAXWLEN];
|
|
5409
|
|
5410 /*
|
|
5411 * Recognize a word that is repeated: "the the".
|
|
5412 */
|
|
5413 p = skiptowhite(su->su_fbadword);
|
|
5414 len = p - su->su_fbadword;
|
|
5415 p = skipwhite(p);
|
|
5416 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
|
|
5417 {
|
|
5418 /* Include badflags: if the badword is onecap or allcap
|
|
5419 * use that for the goodword too: "The the" -> "The". */
|
|
5420 c = su->su_fbadword[len];
|
|
5421 su->su_fbadword[len] = NUL;
|
|
5422 make_case_word(su->su_fbadword, word, su->su_badflags);
|
|
5423 su->su_fbadword[len] = c;
|
|
5424 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, TRUE);
|
|
5425 }
|
|
5426 }
|
|
5427
|
|
5428 /*
|
323
|
5429 * Try finding suggestions by adding/removing/swapping letters.
|
330
|
5430 *
|
|
5431 * This uses a state machine. At each node in the tree we try various
|
|
5432 * operations. When trying if an operation work "depth" is increased and the
|
|
5433 * stack[] is used to store info. This allows combinations, thus insert one
|
|
5434 * character, replace one and delete another. The number of changes is
|
|
5435 * limited by su->su_maxscore, checked in try_deeper().
|
323
|
5436 */
|
|
5437 static void
|
346
|
5438 suggest_try_change(su)
|
323
|
5439 suginfo_T *su;
|
|
5440 {
|
|
5441 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
|
|
5442 char_u tword[MAXWLEN]; /* good word collected so far */
|
|
5443 trystate_T stack[MAXWLEN];
|
|
5444 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
|
|
5445 * to for word split) */
|
|
5446 char_u prewordlen = 0; /* length of word in "preword" */
|
|
5447 int splitoff = 0; /* index in tword after last split */
|
|
5448 trystate_T *sp;
|
|
5449 int newscore;
|
|
5450 langp_T *lp;
|
|
5451 char_u *byts;
|
324
|
5452 idx_T *idxs;
|
323
|
5453 int depth;
|
330
|
5454 int c, c2, c3;
|
|
5455 int n = 0;
|
323
|
5456 int flags;
|
|
5457 garray_T *gap;
|
324
|
5458 idx_T arridx;
|
323
|
5459 int len;
|
|
5460 char_u *p;
|
|
5461 fromto_T *ftp;
|
330
|
5462 int fl = 0, tl;
|
346
|
5463 int repextra = 0; /* extra bytes in fword[] from REP item */
|
323
|
5464
|
|
5465 /* We make a copy of the case-folded bad word, so that we can modify it
|
346
|
5466 * to find matches (esp. REP items). Append some more text, changing
|
|
5467 * chars after the bad word may help. */
|
323
|
5468 STRCPY(fword, su->su_fbadword);
|
346
|
5469 n = STRLEN(fword);
|
|
5470 p = su->su_badptr + su->su_badlen;
|
|
5471 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
|
323
|
5472
|
|
5473 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
5474 lp->lp_slang != NULL; ++lp)
|
|
5475 {
|
|
5476 /*
|
|
5477 * Go through the whole case-fold tree, try changes at each node.
|
|
5478 * "tword[]" contains the word collected from nodes in the tree.
|
|
5479 * "fword[]" the word we are trying to match with (initially the bad
|
|
5480 * word).
|
|
5481 */
|
|
5482 byts = lp->lp_slang->sl_fbyts;
|
|
5483 idxs = lp->lp_slang->sl_fidxs;
|
|
5484
|
|
5485 depth = 0;
|
|
5486 stack[0].ts_state = STATE_START;
|
|
5487 stack[0].ts_score = 0;
|
|
5488 stack[0].ts_curi = 1;
|
|
5489 stack[0].ts_fidx = 0;
|
|
5490 stack[0].ts_fidxtry = 0;
|
|
5491 stack[0].ts_twordlen = 0;
|
|
5492 stack[0].ts_arridx = 0;
|
330
|
5493 #ifdef FEAT_MBYTE
|
|
5494 stack[0].ts_tcharlen = 0;
|
|
5495 #endif
|
|
5496
|
|
5497 /*
|
|
5498 * Loop to find all suggestions. At each round we either:
|
|
5499 * - For the current state try one operation, advance "ts_curi",
|
|
5500 * increase "depth".
|
|
5501 * - When a state is done go to the next, set "ts_state".
|
|
5502 * - When all states are tried decrease "depth".
|
|
5503 */
|
323
|
5504 while (depth >= 0 && !got_int)
|
|
5505 {
|
|
5506 sp = &stack[depth];
|
|
5507 switch (sp->ts_state)
|
|
5508 {
|
|
5509 case STATE_START:
|
|
5510 /*
|
|
5511 * Start of node: Deal with NUL bytes, which means
|
|
5512 * tword[] may end here.
|
|
5513 */
|
|
5514 arridx = sp->ts_arridx; /* current node in the tree */
|
|
5515 len = byts[arridx]; /* bytes in this node */
|
|
5516 arridx += sp->ts_curi; /* index of current byte */
|
|
5517
|
346
|
5518 if (sp->ts_curi > len || byts[arridx] != 0)
|
323
|
5519 {
|
|
5520 /* Past bytes in node and/or past NUL bytes. */
|
|
5521 sp->ts_state = STATE_ENDNUL;
|
|
5522 break;
|
|
5523 }
|
|
5524
|
|
5525 /*
|
|
5526 * End of word in tree.
|
|
5527 */
|
|
5528 ++sp->ts_curi; /* eat one NUL byte */
|
|
5529
|
324
|
5530 flags = (int)idxs[arridx];
|
323
|
5531
|
|
5532 /*
|
|
5533 * Form the word with proper case in preword.
|
|
5534 * If there is a word from a previous split, append.
|
|
5535 */
|
|
5536 tword[sp->ts_twordlen] = NUL;
|
|
5537 if (flags & WF_KEEPCAP)
|
|
5538 /* Must find the word in the keep-case tree. */
|
|
5539 find_keepcap_word(lp->lp_slang, tword + splitoff,
|
|
5540 preword + prewordlen);
|
|
5541 else
|
346
|
5542 {
|
323
|
5543 /* Include badflags: if the badword is onecap or allcap
|
346
|
5544 * use that for the goodword too. But if the badword is
|
|
5545 * allcap and it's only one char long use onecap. */
|
|
5546 c = su->su_badflags;
|
|
5547 if ((c & WF_ALLCAP)
|
|
5548 #ifdef FEAT_MBYTE
|
|
5549 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
|
|
5550 #else
|
|
5551 && su->su_badlen == 1
|
|
5552 #endif
|
|
5553 )
|
|
5554 c = WF_ONECAP;
|
323
|
5555 make_case_word(tword + splitoff,
|
346
|
5556 preword + prewordlen, flags | c);
|
|
5557 }
|
323
|
5558
|
|
5559 /* Don't use a banned word. It may appear again as a good
|
|
5560 * word, thus remember it. */
|
|
5561 if (flags & WF_BANNED)
|
|
5562 {
|
|
5563 add_banned(su, preword + prewordlen);
|
|
5564 break;
|
|
5565 }
|
|
5566 if (was_banned(su, preword + prewordlen))
|
|
5567 break;
|
|
5568
|
|
5569 newscore = 0;
|
|
5570 if ((flags & WF_REGION)
|
|
5571 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
|
|
5572 newscore += SCORE_REGION;
|
|
5573 if (flags & WF_RARE)
|
|
5574 newscore += SCORE_RARE;
|
|
5575
|
346
|
5576 if (!spell_valid_case(su->su_badflags,
|
323
|
5577 captype(preword + prewordlen, NULL)))
|
|
5578 newscore += SCORE_ICASE;
|
|
5579
|
346
|
5580 if ((fword[sp->ts_fidx] == NUL
|
|
5581 || !SPELL_ISWORDP(fword + sp->ts_fidx))
|
|
5582 && sp->ts_fidx >= sp->ts_fidxtry)
|
323
|
5583 {
|
|
5584 /* The badword also ends: add suggestions, */
|
344
|
5585 add_suggestion(su, &su->su_ga, preword,
|
346
|
5586 sp->ts_fidx - repextra,
|
344
|
5587 sp->ts_score + newscore, FALSE);
|
323
|
5588 }
|
330
|
5589 else if (sp->ts_fidx >= sp->ts_fidxtry
|
|
5590 #ifdef FEAT_MBYTE
|
|
5591 /* Don't split halfway a character. */
|
|
5592 && (!has_mbyte || sp->ts_tcharlen == 0)
|
|
5593 #endif
|
|
5594 )
|
323
|
5595 {
|
|
5596 /* The word in the tree ends but the badword
|
|
5597 * continues: try inserting a space and check that a valid
|
|
5598 * words starts at fword[sp->ts_fidx]. */
|
|
5599 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
|
|
5600 {
|
|
5601 /* Save things to be restored at STATE_SPLITUNDO. */
|
|
5602 sp->ts_save_prewordlen = prewordlen;
|
346
|
5603 sp->ts_save_badflags = su->su_badflags;
|
323
|
5604 sp->ts_save_splitoff = splitoff;
|
|
5605
|
|
5606 /* Append a space to preword. */
|
|
5607 STRCAT(preword, " ");
|
|
5608 prewordlen = STRLEN(preword);
|
|
5609 splitoff = sp->ts_twordlen;
|
324
|
5610 #ifdef FEAT_MBYTE
|
|
5611 if (has_mbyte)
|
|
5612 {
|
|
5613 int i = 0;
|
|
5614
|
|
5615 /* Case-folding may change the number of bytes:
|
|
5616 * Count nr of chars in fword[sp->ts_fidx] and
|
|
5617 * advance that many chars in su->su_badptr. */
|
|
5618 for (p = fword; p < fword + sp->ts_fidx;
|
|
5619 mb_ptr_adv(p))
|
|
5620 ++i;
|
|
5621 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
|
|
5622 --i;
|
|
5623 }
|
|
5624 else
|
|
5625 #endif
|
|
5626 p = su->su_badptr + sp->ts_fidx;
|
346
|
5627 su->su_badflags = captype(p, su->su_badptr
|
|
5628 + su->su_badlen);
|
323
|
5629
|
|
5630 sp->ts_state = STATE_SPLITUNDO;
|
|
5631 ++depth;
|
|
5632 /* Restart at top of the tree. */
|
|
5633 stack[depth].ts_arridx = 0;
|
|
5634 }
|
|
5635 }
|
|
5636 break;
|
|
5637
|
|
5638 case STATE_SPLITUNDO:
|
346
|
5639 /* Undo the changes done for word split. */
|
|
5640 su->su_badflags = sp->ts_save_badflags;
|
323
|
5641 splitoff = sp->ts_save_splitoff;
|
|
5642 prewordlen = sp->ts_save_prewordlen;
|
|
5643
|
|
5644 /* Continue looking for NUL bytes. */
|
|
5645 sp->ts_state = STATE_START;
|
|
5646 break;
|
|
5647
|
|
5648 case STATE_ENDNUL:
|
|
5649 /* Past the NUL bytes in the node. */
|
346
|
5650 if (fword[sp->ts_fidx] == NUL)
|
323
|
5651 {
|
|
5652 /* The badword ends, can't use the bytes in this node. */
|
|
5653 sp->ts_state = STATE_DEL;
|
|
5654 break;
|
|
5655 }
|
|
5656 sp->ts_state = STATE_PLAIN;
|
|
5657 /*FALLTHROUGH*/
|
|
5658
|
|
5659 case STATE_PLAIN:
|
|
5660 /*
|
|
5661 * Go over all possible bytes at this node, add each to
|
|
5662 * tword[] and use child node. "ts_curi" is the index.
|
|
5663 */
|
|
5664 arridx = sp->ts_arridx;
|
|
5665 if (sp->ts_curi > byts[arridx])
|
|
5666 {
|
|
5667 /* Done all bytes at this node, do next state. When still
|
|
5668 * at already changed bytes skip the other tricks. */
|
|
5669 if (sp->ts_fidx >= sp->ts_fidxtry)
|
|
5670 sp->ts_state = STATE_DEL;
|
|
5671 else
|
|
5672 sp->ts_state = STATE_FINAL;
|
|
5673 }
|
|
5674 else
|
|
5675 {
|
|
5676 arridx += sp->ts_curi++;
|
|
5677 c = byts[arridx];
|
|
5678
|
|
5679 /* Normal byte, go one level deeper. If it's not equal to
|
|
5680 * the byte in the bad word adjust the score. But don't
|
|
5681 * even try when the byte was already changed. */
|
330
|
5682 if (c == fword[sp->ts_fidx]
|
|
5683 #ifdef FEAT_MBYTE
|
|
5684 || (sp->ts_tcharlen > 0
|
|
5685 && sp->ts_isdiff != DIFF_NONE)
|
|
5686 #endif
|
|
5687 )
|
323
|
5688 newscore = 0;
|
|
5689 else
|
|
5690 newscore = SCORE_SUBST;
|
|
5691 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
|
|
5692 && try_deeper(su, stack, depth, newscore))
|
|
5693 {
|
|
5694 ++depth;
|
330
|
5695 sp = &stack[depth];
|
|
5696 ++sp->ts_fidx;
|
|
5697 tword[sp->ts_twordlen++] = c;
|
|
5698 sp->ts_arridx = idxs[arridx];
|
|
5699 #ifdef FEAT_MBYTE
|
|
5700 if (newscore == SCORE_SUBST)
|
|
5701 sp->ts_isdiff = DIFF_YES;
|
|
5702 if (has_mbyte)
|
|
5703 {
|
|
5704 /* Multi-byte characters are a bit complicated to
|
|
5705 * handle: They differ when any of the bytes
|
|
5706 * differ and then their length may also differ. */
|
|
5707 if (sp->ts_tcharlen == 0)
|
|
5708 {
|
|
5709 /* First byte. */
|
|
5710 sp->ts_tcharidx = 0;
|
|
5711 sp->ts_tcharlen = MB_BYTE2LEN(c);
|
|
5712 sp->ts_fcharstart = sp->ts_fidx - 1;
|
|
5713 sp->ts_isdiff = (newscore != 0)
|
|
5714 ? DIFF_YES : DIFF_NONE;
|
|
5715 }
|
|
5716 else if (sp->ts_isdiff == DIFF_INSERT)
|
|
5717 /* When inserting trail bytes don't advance in
|
|
5718 * the bad word. */
|
|
5719 --sp->ts_fidx;
|
|
5720 if (++sp->ts_tcharidx == sp->ts_tcharlen)
|
|
5721 {
|
|
5722 /* Last byte of character. */
|
|
5723 if (sp->ts_isdiff == DIFF_YES)
|
|
5724 {
|
|
5725 /* Correct ts_fidx for the byte length of
|
|
5726 * the character (we didn't check that
|
|
5727 * before). */
|
|
5728 sp->ts_fidx = sp->ts_fcharstart
|
|
5729 + MB_BYTE2LEN(
|
|
5730 fword[sp->ts_fcharstart]);
|
|
5731
|
|
5732 /* For a similar character adjust score
|
|
5733 * from SCORE_SUBST to SCORE_SIMILAR. */
|
|
5734 if (lp->lp_slang->sl_has_map
|
|
5735 && similar_chars(lp->lp_slang,
|
|
5736 mb_ptr2char(tword
|
|
5737 + sp->ts_twordlen
|
|
5738 - sp->ts_tcharlen),
|
|
5739 mb_ptr2char(fword
|
|
5740 + sp->ts_fcharstart)))
|
|
5741 sp->ts_score -=
|
|
5742 SCORE_SUBST - SCORE_SIMILAR;
|
|
5743 }
|
|
5744
|
|
5745 /* Starting a new char, reset the length. */
|
|
5746 sp->ts_tcharlen = 0;
|
|
5747 }
|
|
5748 }
|
|
5749 else
|
|
5750 #endif
|
|
5751 {
|
|
5752 /* If we found a similar char adjust the score.
|
|
5753 * We do this after calling try_deeper() because
|
|
5754 * it's slow. */
|
|
5755 if (newscore != 0
|
|
5756 && lp->lp_slang->sl_has_map
|
|
5757 && similar_chars(lp->lp_slang,
|
|
5758 c, fword[sp->ts_fidx - 1]))
|
|
5759 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
|
|
5760 }
|
323
|
5761 }
|
|
5762 }
|
|
5763 break;
|
|
5764
|
|
5765 case STATE_DEL:
|
330
|
5766 #ifdef FEAT_MBYTE
|
|
5767 /* When past the first byte of a multi-byte char don't try
|
|
5768 * delete/insert/swap a character. */
|
|
5769 if (has_mbyte && sp->ts_tcharlen > 0)
|
|
5770 {
|
|
5771 sp->ts_state = STATE_FINAL;
|
|
5772 break;
|
|
5773 }
|
|
5774 #endif
|
|
5775 /*
|
|
5776 * Try skipping one character in the bad word (delete it).
|
|
5777 */
|
323
|
5778 sp->ts_state = STATE_INS;
|
|
5779 sp->ts_curi = 1;
|
|
5780 if (fword[sp->ts_fidx] != NUL
|
|
5781 && try_deeper(su, stack, depth, SCORE_DEL))
|
|
5782 {
|
|
5783 ++depth;
|
330
|
5784 #ifdef FEAT_MBYTE
|
|
5785 if (has_mbyte)
|
|
5786 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
|
|
5787 else
|
|
5788 #endif
|
|
5789 ++stack[depth].ts_fidx;
|
323
|
5790 break;
|
|
5791 }
|
|
5792 /*FALLTHROUGH*/
|
|
5793
|
|
5794 case STATE_INS:
|
330
|
5795 /* Insert one byte. Do this for each possible byte at this
|
323
|
5796 * node. */
|
|
5797 n = sp->ts_arridx;
|
|
5798 if (sp->ts_curi > byts[n])
|
|
5799 {
|
|
5800 /* Done all bytes at this node, do next state. */
|
|
5801 sp->ts_state = STATE_SWAP;
|
|
5802 }
|
|
5803 else
|
|
5804 {
|
330
|
5805 /* Do one more byte at this node. Skip NUL bytes. */
|
323
|
5806 n += sp->ts_curi++;
|
|
5807 c = byts[n];
|
|
5808 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
|
|
5809 {
|
|
5810 ++depth;
|
330
|
5811 sp = &stack[depth];
|
|
5812 tword[sp->ts_twordlen++] = c;
|
|
5813 sp->ts_arridx = idxs[n];
|
|
5814 #ifdef FEAT_MBYTE
|
|
5815 if (has_mbyte)
|
|
5816 {
|
|
5817 fl = MB_BYTE2LEN(c);
|
|
5818 if (fl > 1)
|
|
5819 {
|
|
5820 /* There are following bytes for the same
|
|
5821 * character. We must find all bytes before
|
|
5822 * trying delete/insert/swap/etc. */
|
|
5823 sp->ts_tcharlen = fl;
|
|
5824 sp->ts_tcharidx = 1;
|
|
5825 sp->ts_isdiff = DIFF_INSERT;
|
|
5826 }
|
|
5827 }
|
|
5828 #endif
|
323
|
5829 }
|
|
5830 }
|
|
5831 break;
|
|
5832
|
|
5833 case STATE_SWAP:
|
330
|
5834 /*
|
|
5835 * Swap two bytes in the bad word: "12" -> "21".
|
|
5836 * We change "fword" here, it's changed back afterwards.
|
|
5837 */
|
|
5838 p = fword + sp->ts_fidx;
|
|
5839 c = *p;
|
|
5840 if (c == NUL)
|
|
5841 {
|
|
5842 /* End of word, can't swap or replace. */
|
|
5843 sp->ts_state = STATE_FINAL;
|
|
5844 break;
|
|
5845 }
|
|
5846 #ifdef FEAT_MBYTE
|
|
5847 if (has_mbyte)
|
|
5848 {
|
|
5849 n = mb_ptr2len_check(p);
|
|
5850 c = mb_ptr2char(p);
|
|
5851 c2 = mb_ptr2char(p + n);
|
|
5852 }
|
|
5853 else
|
|
5854 #endif
|
|
5855 c2 = p[1];
|
|
5856 if (c == c2)
|
323
|
5857 {
|
330
|
5858 /* Characters are identical, swap won't do anything. */
|
|
5859 sp->ts_state = STATE_SWAP3;
|
|
5860 break;
|
|
5861 }
|
|
5862 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
|
|
5863 {
|
|
5864 sp->ts_state = STATE_UNSWAP;
|
323
|
5865 ++depth;
|
330
|
5866 #ifdef FEAT_MBYTE
|
|
5867 if (has_mbyte)
|
|
5868 {
|
|
5869 fl = mb_char2len(c2);
|
|
5870 mch_memmove(p, p + n, fl);
|
|
5871 mb_char2bytes(c, p + fl);
|
|
5872 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
|
|
5873 }
|
|
5874 else
|
|
5875 #endif
|
|
5876 {
|
|
5877 p[0] = c2;
|
|
5878 p[1] = c;
|
|
5879 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
|
|
5880 }
|
323
|
5881 }
|
|
5882 else
|
|
5883 /* If this swap doesn't work then SWAP3 won't either. */
|
|
5884 sp->ts_state = STATE_REP_INI;
|
|
5885 break;
|
|
5886
|
330
|
5887 case STATE_UNSWAP:
|
|
5888 /* Undo the STATE_SWAP swap: "21" -> "12". */
|
|
5889 p = fword + sp->ts_fidx;
|
|
5890 #ifdef FEAT_MBYTE
|
|
5891 if (has_mbyte)
|
|
5892 {
|
|
5893 n = MB_BYTE2LEN(*p);
|
|
5894 c = mb_ptr2char(p + n);
|
|
5895 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
|
|
5896 mb_char2bytes(c, p);
|
|
5897 }
|
|
5898 else
|
|
5899 #endif
|
|
5900 {
|
|
5901 c = *p;
|
|
5902 *p = p[1];
|
|
5903 p[1] = c;
|
|
5904 }
|
|
5905 /*FALLTHROUGH*/
|
|
5906
|
|
5907 case STATE_SWAP3:
|
323
|
5908 /* Swap two bytes, skipping one: "123" -> "321". We change
|
330
|
5909 * "fword" here, it's changed back afterwards. */
|
|
5910 p = fword + sp->ts_fidx;
|
|
5911 #ifdef FEAT_MBYTE
|
|
5912 if (has_mbyte)
|
|
5913 {
|
|
5914 n = mb_ptr2len_check(p);
|
|
5915 c = mb_ptr2char(p);
|
|
5916 fl = mb_ptr2len_check(p + n);
|
|
5917 c2 = mb_ptr2char(p + n);
|
|
5918 c3 = mb_ptr2char(p + n + fl);
|
|
5919 }
|
|
5920 else
|
|
5921 #endif
|
323
|
5922 {
|
330
|
5923 c = *p;
|
|
5924 c2 = p[1];
|
|
5925 c3 = p[2];
|
|
5926 }
|
|
5927
|
|
5928 /* When characters are identical: "121" then SWAP3 result is
|
|
5929 * identical, ROT3L result is same as SWAP: "211", ROT3L
|
|
5930 * result is same as SWAP on next char: "112". Thus skip all
|
|
5931 * swapping. Also skip when c3 is NUL. */
|
|
5932 if (c == c3 || c3 == NUL)
|
|
5933 {
|
|
5934 sp->ts_state = STATE_REP_INI;
|
|
5935 break;
|
|
5936 }
|
|
5937 if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
5938 {
|
|
5939 sp->ts_state = STATE_UNSWAP3;
|
323
|
5940 ++depth;
|
330
|
5941 #ifdef FEAT_MBYTE
|
|
5942 if (has_mbyte)
|
|
5943 {
|
|
5944 tl = mb_char2len(c3);
|
|
5945 mch_memmove(p, p + n + fl, tl);
|
|
5946 mb_char2bytes(c2, p + tl);
|
|
5947 mb_char2bytes(c, p + fl + tl);
|
|
5948 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
|
|
5949 }
|
|
5950 else
|
|
5951 #endif
|
|
5952 {
|
|
5953 p[0] = p[2];
|
|
5954 p[2] = c;
|
|
5955 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
5956 }
|
323
|
5957 }
|
|
5958 else
|
|
5959 sp->ts_state = STATE_REP_INI;
|
|
5960 break;
|
|
5961
|
330
|
5962 case STATE_UNSWAP3:
|
|
5963 /* Undo STATE_SWAP3: "321" -> "123" */
|
|
5964 p = fword + sp->ts_fidx;
|
|
5965 #ifdef FEAT_MBYTE
|
|
5966 if (has_mbyte)
|
|
5967 {
|
|
5968 n = MB_BYTE2LEN(*p);
|
|
5969 c2 = mb_ptr2char(p + n);
|
|
5970 fl = MB_BYTE2LEN(p[n]);
|
|
5971 c = mb_ptr2char(p + n + fl);
|
|
5972 tl = MB_BYTE2LEN(p[n + fl]);
|
|
5973 mch_memmove(p + fl + tl, p, n);
|
|
5974 mb_char2bytes(c, p);
|
|
5975 mb_char2bytes(c2, p + tl);
|
|
5976 }
|
|
5977 else
|
|
5978 #endif
|
|
5979 {
|
|
5980 c = *p;
|
|
5981 *p = p[2];
|
|
5982 p[2] = c;
|
|
5983 }
|
346
|
5984
|
330
|
5985 /* Rotate three characters left: "123" -> "231". We change
|
|
5986 * "fword" here, it's changed back afterwards. */
|
323
|
5987 if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
5988 {
|
330
|
5989 sp->ts_state = STATE_UNROT3L;
|
323
|
5990 ++depth;
|
330
|
5991 p = fword + sp->ts_fidx;
|
|
5992 #ifdef FEAT_MBYTE
|
|
5993 if (has_mbyte)
|
|
5994 {
|
|
5995 n = mb_ptr2len_check(p);
|
|
5996 c = mb_ptr2char(p);
|
|
5997 fl = mb_ptr2len_check(p + n);
|
|
5998 fl += mb_ptr2len_check(p + n + fl);
|
|
5999 mch_memmove(p, p + n, fl);
|
|
6000 mb_char2bytes(c, p + fl);
|
|
6001 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
|
|
6002 }
|
|
6003 else
|
|
6004 #endif
|
|
6005 {
|
|
6006 c = *p;
|
|
6007 *p = p[1];
|
|
6008 p[1] = p[2];
|
|
6009 p[2] = c;
|
|
6010 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
6011 }
|
323
|
6012 }
|
|
6013 else
|
|
6014 sp->ts_state = STATE_REP_INI;
|
|
6015 break;
|
|
6016
|
330
|
6017 case STATE_UNROT3L:
|
346
|
6018 /* Undo ROT3L: "231" -> "123" */
|
330
|
6019 p = fword + sp->ts_fidx;
|
|
6020 #ifdef FEAT_MBYTE
|
|
6021 if (has_mbyte)
|
|
6022 {
|
|
6023 n = MB_BYTE2LEN(*p);
|
|
6024 n += MB_BYTE2LEN(p[n]);
|
|
6025 c = mb_ptr2char(p + n);
|
|
6026 tl = MB_BYTE2LEN(p[n]);
|
|
6027 mch_memmove(p + tl, p, n);
|
|
6028 mb_char2bytes(c, p);
|
|
6029 }
|
|
6030 else
|
|
6031 #endif
|
|
6032 {
|
|
6033 c = p[2];
|
|
6034 p[2] = p[1];
|
|
6035 p[1] = *p;
|
|
6036 *p = c;
|
|
6037 }
|
346
|
6038
|
323
|
6039 /* Rotate three bytes right: "123" -> "312". We change
|
330
|
6040 * "fword" here, it's changed back afterwards. */
|
323
|
6041 if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
6042 {
|
330
|
6043 sp->ts_state = STATE_UNROT3R;
|
323
|
6044 ++depth;
|
330
|
6045 p = fword + sp->ts_fidx;
|
|
6046 #ifdef FEAT_MBYTE
|
|
6047 if (has_mbyte)
|
|
6048 {
|
|
6049 n = mb_ptr2len_check(p);
|
|
6050 n += mb_ptr2len_check(p + n);
|
|
6051 c = mb_ptr2char(p + n);
|
|
6052 tl = mb_ptr2len_check(p + n);
|
|
6053 mch_memmove(p + tl, p, n);
|
|
6054 mb_char2bytes(c, p);
|
|
6055 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
|
|
6056 }
|
|
6057 else
|
|
6058 #endif
|
|
6059 {
|
|
6060 c = p[2];
|
|
6061 p[2] = p[1];
|
|
6062 p[1] = *p;
|
|
6063 *p = c;
|
|
6064 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
6065 }
|
323
|
6066 }
|
|
6067 else
|
|
6068 sp->ts_state = STATE_REP_INI;
|
|
6069 break;
|
|
6070
|
330
|
6071 case STATE_UNROT3R:
|
346
|
6072 /* Undo ROT3R: "312" -> "123" */
|
330
|
6073 p = fword + sp->ts_fidx;
|
|
6074 #ifdef FEAT_MBYTE
|
|
6075 if (has_mbyte)
|
|
6076 {
|
|
6077 c = mb_ptr2char(p);
|
|
6078 tl = MB_BYTE2LEN(*p);
|
|
6079 n = MB_BYTE2LEN(p[tl]);
|
|
6080 n += MB_BYTE2LEN(p[tl + n]);
|
|
6081 mch_memmove(p, p + tl, n);
|
|
6082 mb_char2bytes(c, p + n);
|
|
6083 }
|
|
6084 else
|
|
6085 #endif
|
|
6086 {
|
|
6087 c = *p;
|
|
6088 *p = p[1];
|
|
6089 p[1] = p[2];
|
|
6090 p[2] = c;
|
|
6091 }
|
323
|
6092 /*FALLTHROUGH*/
|
|
6093
|
|
6094 case STATE_REP_INI:
|
|
6095 /* Check if matching with REP items from the .aff file would
|
|
6096 * work. Quickly skip if there are no REP items or the score
|
|
6097 * is going to be too high anyway. */
|
|
6098 gap = &lp->lp_slang->sl_rep;
|
|
6099 if (gap->ga_len == 0
|
|
6100 || sp->ts_score + SCORE_REP >= su->su_maxscore)
|
|
6101 {
|
|
6102 sp->ts_state = STATE_FINAL;
|
|
6103 break;
|
|
6104 }
|
|
6105
|
|
6106 /* Use the first byte to quickly find the first entry that
|
330
|
6107 * may match. If the index is -1 there is none. */
|
323
|
6108 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
|
|
6109 if (sp->ts_curi < 0)
|
|
6110 {
|
|
6111 sp->ts_state = STATE_FINAL;
|
|
6112 break;
|
|
6113 }
|
|
6114
|
|
6115 sp->ts_state = STATE_REP;
|
|
6116 /*FALLTHROUGH*/
|
|
6117
|
|
6118 case STATE_REP:
|
|
6119 /* Try matching with REP items from the .aff file. For each
|
330
|
6120 * match replace the characters and check if the resulting
|
|
6121 * word is valid. */
|
323
|
6122 p = fword + sp->ts_fidx;
|
|
6123
|
|
6124 gap = &lp->lp_slang->sl_rep;
|
|
6125 while (sp->ts_curi < gap->ga_len)
|
|
6126 {
|
|
6127 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
|
|
6128 if (*ftp->ft_from != *p)
|
|
6129 {
|
|
6130 /* past possible matching entries */
|
|
6131 sp->ts_curi = gap->ga_len;
|
|
6132 break;
|
|
6133 }
|
|
6134 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
|
|
6135 && try_deeper(su, stack, depth, SCORE_REP))
|
|
6136 {
|
|
6137 /* Need to undo this afterwards. */
|
|
6138 sp->ts_state = STATE_REP_UNDO;
|
|
6139
|
|
6140 /* Change the "from" to the "to" string. */
|
|
6141 ++depth;
|
|
6142 fl = STRLEN(ftp->ft_from);
|
|
6143 tl = STRLEN(ftp->ft_to);
|
|
6144 if (fl != tl)
|
346
|
6145 {
|
323
|
6146 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
|
346
|
6147 repextra += tl - fl;
|
|
6148 }
|
323
|
6149 mch_memmove(p, ftp->ft_to, tl);
|
|
6150 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
|
330
|
6151 #ifdef FEAT_MBYTE
|
|
6152 stack[depth].ts_tcharlen = 0;
|
|
6153 #endif
|
323
|
6154 break;
|
|
6155 }
|
|
6156 }
|
|
6157
|
|
6158 if (sp->ts_curi >= gap->ga_len)
|
|
6159 /* No (more) matches. */
|
|
6160 sp->ts_state = STATE_FINAL;
|
|
6161
|
|
6162 break;
|
|
6163
|
|
6164 case STATE_REP_UNDO:
|
|
6165 /* Undo a REP replacement and continue with the next one. */
|
|
6166 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
|
|
6167 + sp->ts_curi - 1;
|
|
6168 fl = STRLEN(ftp->ft_from);
|
|
6169 tl = STRLEN(ftp->ft_to);
|
|
6170 p = fword + sp->ts_fidx;
|
|
6171 if (fl != tl)
|
346
|
6172 {
|
323
|
6173 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
|
346
|
6174 repextra -= tl - fl;
|
|
6175 }
|
323
|
6176 mch_memmove(p, ftp->ft_from, fl);
|
|
6177 sp->ts_state = STATE_REP;
|
|
6178 break;
|
|
6179
|
|
6180 default:
|
|
6181 /* Did all possible states at this level, go up one level. */
|
|
6182 --depth;
|
344
|
6183
|
|
6184 /* Don't check for CTRL-C too often, it takes time. */
|
|
6185 line_breakcheck();
|
323
|
6186 }
|
|
6187 }
|
|
6188 }
|
|
6189 }
|
|
6190
|
|
6191 /*
|
|
6192 * Try going one level deeper in the tree.
|
|
6193 */
|
|
6194 static int
|
|
6195 try_deeper(su, stack, depth, score_add)
|
|
6196 suginfo_T *su;
|
|
6197 trystate_T *stack;
|
|
6198 int depth;
|
|
6199 int score_add;
|
|
6200 {
|
|
6201 int newscore;
|
|
6202
|
|
6203 /* Refuse to go deeper if the scrore is getting too big. */
|
|
6204 newscore = stack[depth].ts_score + score_add;
|
|
6205 if (newscore >= su->su_maxscore)
|
|
6206 return FALSE;
|
|
6207
|
330
|
6208 stack[depth + 1] = stack[depth];
|
323
|
6209 stack[depth + 1].ts_state = STATE_START;
|
|
6210 stack[depth + 1].ts_score = newscore;
|
|
6211 stack[depth + 1].ts_curi = 1; /* start just after length byte */
|
|
6212 return TRUE;
|
|
6213 }
|
|
6214
|
|
6215 /*
|
|
6216 * "fword" is a good word with case folded. Find the matching keep-case
|
|
6217 * words and put it in "kword".
|
|
6218 * Theoretically there could be several keep-case words that result in the
|
|
6219 * same case-folded word, but we only find one...
|
|
6220 */
|
|
6221 static void
|
|
6222 find_keepcap_word(slang, fword, kword)
|
|
6223 slang_T *slang;
|
|
6224 char_u *fword;
|
|
6225 char_u *kword;
|
|
6226 {
|
|
6227 char_u uword[MAXWLEN]; /* "fword" in upper-case */
|
|
6228 int depth;
|
324
|
6229 idx_T tryidx;
|
323
|
6230
|
|
6231 /* The following arrays are used at each depth in the tree. */
|
324
|
6232 idx_T arridx[MAXWLEN];
|
323
|
6233 int round[MAXWLEN];
|
|
6234 int fwordidx[MAXWLEN];
|
|
6235 int uwordidx[MAXWLEN];
|
|
6236 int kwordlen[MAXWLEN];
|
|
6237
|
|
6238 int flen, ulen;
|
|
6239 int l;
|
|
6240 int len;
|
|
6241 int c;
|
324
|
6242 idx_T lo, hi, m;
|
323
|
6243 char_u *p;
|
|
6244 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
|
324
|
6245 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
|
323
|
6246
|
|
6247 if (byts == NULL)
|
|
6248 {
|
|
6249 /* array is empty: "cannot happen" */
|
|
6250 *kword = NUL;
|
|
6251 return;
|
|
6252 }
|
|
6253
|
|
6254 /* Make an all-cap version of "fword". */
|
|
6255 allcap_copy(fword, uword);
|
|
6256
|
|
6257 /*
|
|
6258 * Each character needs to be tried both case-folded and upper-case.
|
|
6259 * All this gets very complicated if we keep in mind that changing case
|
|
6260 * may change the byte length of a multi-byte character...
|
|
6261 */
|
|
6262 depth = 0;
|
|
6263 arridx[0] = 0;
|
|
6264 round[0] = 0;
|
|
6265 fwordidx[0] = 0;
|
|
6266 uwordidx[0] = 0;
|
|
6267 kwordlen[0] = 0;
|
|
6268 while (depth >= 0)
|
|
6269 {
|
|
6270 if (fword[fwordidx[depth]] == NUL)
|
|
6271 {
|
|
6272 /* We are at the end of "fword". If the tree allows a word to end
|
|
6273 * here we have found a match. */
|
|
6274 if (byts[arridx[depth] + 1] == 0)
|
|
6275 {
|
|
6276 kword[kwordlen[depth]] = NUL;
|
|
6277 return;
|
|
6278 }
|
|
6279
|
|
6280 /* kword is getting too long, continue one level up */
|
|
6281 --depth;
|
|
6282 }
|
|
6283 else if (++round[depth] > 2)
|
|
6284 {
|
|
6285 /* tried both fold-case and upper-case character, continue one
|
|
6286 * level up */
|
|
6287 --depth;
|
|
6288 }
|
|
6289 else
|
|
6290 {
|
|
6291 /*
|
|
6292 * round[depth] == 1: Try using the folded-case character.
|
|
6293 * round[depth] == 2: Try using the upper-case character.
|
|
6294 */
|
|
6295 #ifdef FEAT_MBYTE
|
|
6296 if (has_mbyte)
|
|
6297 {
|
|
6298 flen = mb_ptr2len_check(fword + fwordidx[depth]);
|
|
6299 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
|
|
6300 }
|
|
6301 else
|
|
6302 #endif
|
|
6303 ulen = flen = 1;
|
|
6304 if (round[depth] == 1)
|
|
6305 {
|
|
6306 p = fword + fwordidx[depth];
|
|
6307 l = flen;
|
|
6308 }
|
|
6309 else
|
|
6310 {
|
|
6311 p = uword + uwordidx[depth];
|
|
6312 l = ulen;
|
|
6313 }
|
|
6314
|
|
6315 for (tryidx = arridx[depth]; l > 0; --l)
|
|
6316 {
|
|
6317 /* Perform a binary search in the list of accepted bytes. */
|
|
6318 len = byts[tryidx++];
|
|
6319 c = *p++;
|
|
6320 lo = tryidx;
|
|
6321 hi = tryidx + len - 1;
|
|
6322 while (lo < hi)
|
|
6323 {
|
|
6324 m = (lo + hi) / 2;
|
|
6325 if (byts[m] > c)
|
|
6326 hi = m - 1;
|
|
6327 else if (byts[m] < c)
|
|
6328 lo = m + 1;
|
|
6329 else
|
|
6330 {
|
|
6331 lo = hi = m;
|
|
6332 break;
|
|
6333 }
|
|
6334 }
|
|
6335
|
|
6336 /* Stop if there is no matching byte. */
|
|
6337 if (hi < lo || byts[lo] != c)
|
|
6338 break;
|
|
6339
|
|
6340 /* Continue at the child (if there is one). */
|
|
6341 tryidx = idxs[lo];
|
|
6342 }
|
|
6343
|
|
6344 if (l == 0)
|
|
6345 {
|
|
6346 /*
|
|
6347 * Found the matching char. Copy it to "kword" and go a
|
|
6348 * level deeper.
|
|
6349 */
|
|
6350 if (round[depth] == 1)
|
|
6351 {
|
|
6352 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
|
|
6353 flen);
|
|
6354 kwordlen[depth + 1] = kwordlen[depth] + flen;
|
|
6355 }
|
|
6356 else
|
|
6357 {
|
|
6358 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
|
|
6359 ulen);
|
|
6360 kwordlen[depth + 1] = kwordlen[depth] + ulen;
|
|
6361 }
|
|
6362 fwordidx[depth + 1] = fwordidx[depth] + flen;
|
|
6363 uwordidx[depth + 1] = uwordidx[depth] + ulen;
|
|
6364
|
|
6365 ++depth;
|
|
6366 arridx[depth] = tryidx;
|
|
6367 round[depth] = 0;
|
|
6368 }
|
|
6369 }
|
|
6370 }
|
|
6371
|
|
6372 /* Didn't find it: "cannot happen". */
|
|
6373 *kword = NUL;
|
|
6374 }
|
|
6375
|
|
6376 /*
|
344
|
6377 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
|
|
6378 * su->su_sga.
|
|
6379 */
|
|
6380 static void
|
|
6381 score_comp_sal(su)
|
|
6382 suginfo_T *su;
|
|
6383 {
|
|
6384 langp_T *lp;
|
|
6385 char_u badsound[MAXWLEN];
|
|
6386 int i;
|
|
6387 suggest_T *stp;
|
|
6388 suggest_T *sstp;
|
|
6389 char_u fword[MAXWLEN];
|
|
6390 char_u goodsound[MAXWLEN];
|
|
6391 int score;
|
|
6392
|
|
6393 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
|
|
6394 return;
|
|
6395
|
|
6396 /* Use the sound-folding of the first language that supports it. */
|
|
6397 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
6398 lp->lp_slang != NULL; ++lp)
|
|
6399 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
6400 {
|
|
6401 /* soundfold the bad word */
|
|
6402 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
|
|
6403
|
|
6404 for (i = 0; i < su->su_ga.ga_len; ++i)
|
|
6405 {
|
|
6406 stp = &SUG(su->su_ga, i);
|
|
6407
|
|
6408 /* Case-fold the suggested word and sound-fold it. */
|
|
6409 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word),
|
|
6410 fword, MAXWLEN);
|
|
6411 spell_soundfold(lp->lp_slang, fword, goodsound);
|
|
6412 score = soundalike_score(goodsound, badsound);
|
|
6413 if (score < SCORE_MAXMAX)
|
|
6414 {
|
|
6415 /* Add the suggestion. */
|
|
6416 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
|
|
6417 sstp->st_word = vim_strsave(stp->st_word);
|
|
6418 if (sstp->st_word != NULL)
|
|
6419 {
|
|
6420 sstp->st_score = score;
|
|
6421 sstp->st_altscore = 0;
|
|
6422 sstp->st_orglen = stp->st_orglen;
|
|
6423 ++su->su_sga.ga_len;
|
|
6424 }
|
|
6425 }
|
|
6426 }
|
|
6427 break;
|
|
6428 }
|
|
6429 }
|
|
6430
|
|
6431 /*
|
|
6432 * Combine the list of suggestions in su->su_ga and su->su_sga.
|
|
6433 * They are intwined.
|
|
6434 */
|
|
6435 static void
|
|
6436 score_combine(su)
|
|
6437 suginfo_T *su;
|
|
6438 {
|
|
6439 int i;
|
|
6440 int j;
|
|
6441 garray_T ga;
|
|
6442 garray_T *gap;
|
|
6443 langp_T *lp;
|
|
6444 suggest_T *stp;
|
|
6445 char_u *p;
|
|
6446 char_u badsound[MAXWLEN];
|
346
|
6447 char_u badsound2[MAXWLEN];
|
344
|
6448 char_u goodsound[MAXWLEN];
|
|
6449 char_u fword[MAXWLEN];
|
|
6450 int round;
|
|
6451
|
|
6452 /* Add the alternate score to su_ga. */
|
|
6453 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
6454 lp->lp_slang != NULL; ++lp)
|
|
6455 {
|
|
6456 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
6457 {
|
|
6458 /* soundfold the bad word */
|
|
6459 spell_soundfold(lp->lp_slang, su->su_fbadword, badsound);
|
|
6460
|
|
6461 for (i = 0; i < su->su_ga.ga_len; ++i)
|
|
6462 {
|
|
6463 stp = &SUG(su->su_ga, i);
|
|
6464
|
346
|
6465 if (stp->st_orglen <= su->su_badlen)
|
|
6466 p = badsound;
|
|
6467 else
|
|
6468 {
|
|
6469 /* soundfold the bad word with a different length */
|
|
6470 (void)spell_casefold(su->su_badptr, stp->st_orglen,
|
|
6471 fword, MAXWLEN);
|
|
6472 spell_soundfold(lp->lp_slang, fword, badsound2);
|
|
6473 p = badsound2;
|
|
6474 }
|
|
6475
|
344
|
6476 /* Case-fold the word, sound-fold the word and compute the
|
|
6477 * score for the difference. */
|
|
6478 (void)spell_casefold(stp->st_word, STRLEN(stp->st_word),
|
346
|
6479 fword, MAXWLEN);
|
344
|
6480 spell_soundfold(lp->lp_slang, fword, goodsound);
|
346
|
6481
|
|
6482 stp->st_altscore = soundalike_score(goodsound, p);
|
344
|
6483 if (stp->st_altscore == SCORE_MAXMAX)
|
|
6484 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
|
|
6485 else
|
|
6486 stp->st_score = (stp->st_score * 3
|
|
6487 + stp->st_altscore) / 4;
|
|
6488 stp->st_salscore = FALSE;
|
|
6489 }
|
|
6490 break;
|
|
6491 }
|
|
6492 }
|
|
6493
|
|
6494 /* Add the alternate score to su_sga. */
|
|
6495 for (i = 0; i < su->su_sga.ga_len; ++i)
|
|
6496 {
|
|
6497 stp = &SUG(su->su_sga, i);
|
|
6498 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
|
|
6499 if (stp->st_score == SCORE_MAXMAX)
|
|
6500 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
|
|
6501 else
|
|
6502 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
|
|
6503 stp->st_salscore = TRUE;
|
|
6504 }
|
|
6505
|
|
6506 /* Sort the suggestions and truncate at "maxcount" for both lists. */
|
|
6507 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
|
|
6508 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
|
|
6509
|
|
6510 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
|
|
6511 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
|
|
6512 return;
|
|
6513
|
|
6514 stp = &SUG(ga, 0);
|
|
6515 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
|
|
6516 {
|
|
6517 /* round 1: get a suggestion from su_ga
|
|
6518 * round 2: get a suggestion from su_sga */
|
|
6519 for (round = 1; round <= 2; ++round)
|
|
6520 {
|
|
6521 gap = round == 1 ? &su->su_ga : &su->su_sga;
|
|
6522 if (i < gap->ga_len)
|
|
6523 {
|
|
6524 /* Don't add a word if it's already there. */
|
|
6525 p = SUG(*gap, i).st_word;
|
|
6526 for (j = 0; j < ga.ga_len; ++j)
|
|
6527 if (STRCMP(stp[j].st_word, p) == 0)
|
|
6528 break;
|
|
6529 if (j == ga.ga_len)
|
|
6530 stp[ga.ga_len++] = SUG(*gap, i);
|
|
6531 else
|
|
6532 vim_free(p);
|
|
6533 }
|
|
6534 }
|
|
6535 }
|
|
6536
|
|
6537 ga_clear(&su->su_ga);
|
|
6538 ga_clear(&su->su_sga);
|
|
6539
|
|
6540 /* Truncate the list to the number of suggestions that will be displayed. */
|
|
6541 if (ga.ga_len > su->su_maxcount)
|
|
6542 {
|
|
6543 for (i = su->su_maxcount; i < ga.ga_len; ++i)
|
|
6544 vim_free(stp[i].st_word);
|
|
6545 ga.ga_len = su->su_maxcount;
|
|
6546 }
|
|
6547
|
|
6548 su->su_ga = ga;
|
|
6549 }
|
|
6550
|
|
6551 /*
|
323
|
6552 * Find suggestions by comparing the word in a sound-a-like form.
|
|
6553 */
|
|
6554 static void
|
346
|
6555 suggest_try_soundalike(su)
|
323
|
6556 suginfo_T *su;
|
|
6557 {
|
|
6558 char_u salword[MAXWLEN];
|
|
6559 char_u tword[MAXWLEN];
|
|
6560 char_u tfword[MAXWLEN];
|
|
6561 char_u tsalword[MAXWLEN];
|
324
|
6562 idx_T arridx[MAXWLEN];
|
323
|
6563 int curi[MAXWLEN];
|
|
6564 langp_T *lp;
|
|
6565 char_u *byts;
|
324
|
6566 idx_T *idxs;
|
323
|
6567 int depth;
|
|
6568 int c;
|
324
|
6569 idx_T n;
|
323
|
6570 int round;
|
|
6571 int flags;
|
344
|
6572 int sound_score;
|
|
6573
|
|
6574 /* Do this for all languages that support sound folding. */
|
323
|
6575 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
6576 lp->lp_slang != NULL; ++lp)
|
|
6577 {
|
|
6578 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
6579 {
|
|
6580 /* soundfold the bad word */
|
|
6581 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
|
|
6582
|
|
6583 /*
|
|
6584 * Go through the whole tree, soundfold each word and compare.
|
|
6585 * round 1: use the case-folded tree.
|
|
6586 * round 2: use the keep-case tree.
|
|
6587 */
|
|
6588 for (round = 1; round <= 2; ++round)
|
|
6589 {
|
|
6590 if (round == 1)
|
|
6591 {
|
|
6592 byts = lp->lp_slang->sl_fbyts;
|
|
6593 idxs = lp->lp_slang->sl_fidxs;
|
|
6594 }
|
|
6595 else
|
|
6596 {
|
|
6597 byts = lp->lp_slang->sl_kbyts;
|
|
6598 idxs = lp->lp_slang->sl_kidxs;
|
|
6599 }
|
|
6600
|
|
6601 depth = 0;
|
|
6602 arridx[0] = 0;
|
|
6603 curi[0] = 1;
|
|
6604 while (depth >= 0 && !got_int)
|
|
6605 {
|
|
6606 if (curi[depth] > byts[arridx[depth]])
|
|
6607 /* Done all bytes at this node, go up one level. */
|
|
6608 --depth;
|
|
6609 else
|
|
6610 {
|
|
6611 /* Do one more byte at this node. */
|
|
6612 n = arridx[depth] + curi[depth];
|
|
6613 ++curi[depth];
|
|
6614 c = byts[n];
|
|
6615 if (c == 0)
|
|
6616 {
|
|
6617 /* End of word, deal with the word. */
|
324
|
6618 flags = (int)idxs[n];
|
323
|
6619 if (round == 2 || (flags & WF_KEEPCAP) == 0)
|
|
6620 {
|
|
6621 tword[depth] = NUL;
|
|
6622 if (round == 1)
|
|
6623 spell_soundfold(lp->lp_slang,
|
|
6624 tword, tsalword);
|
|
6625 else
|
|
6626 {
|
|
6627 /* In keep-case tree need to case-fold the
|
|
6628 * word. */
|
|
6629 (void)spell_casefold(tword, depth,
|
|
6630 tfword, MAXWLEN);
|
|
6631 spell_soundfold(lp->lp_slang,
|
|
6632 tfword, tsalword);
|
|
6633 }
|
|
6634
|
344
|
6635 /* Compute the edit distance between the
|
|
6636 * sound-a-like words. */
|
|
6637 sound_score = soundalike_score(salword,
|
|
6638 tsalword);
|
324
|
6639 if (sound_score < SCORE_MAXMAX)
|
323
|
6640 {
|
324
|
6641 char_u cword[MAXWLEN];
|
|
6642 char_u *p;
|
344
|
6643 int score;
|
324
|
6644
|
323
|
6645 if (round == 1 && flags != 0)
|
|
6646 {
|
324
|
6647 /* Need to fix case according to
|
|
6648 * "flags". */
|
323
|
6649 make_case_word(tword, cword, flags);
|
324
|
6650 p = cword;
|
323
|
6651 }
|
|
6652 else
|
324
|
6653 p = tword;
|
|
6654
|
344
|
6655 if (sps_flags & SPS_DOUBLE)
|
|
6656 add_suggestion(su, &su->su_sga, p,
|
346
|
6657 su->su_badlen,
|
344
|
6658 sound_score, FALSE);
|
|
6659 else
|
|
6660 {
|
|
6661 /* Compute the score. */
|
|
6662 score = spell_edit_score(
|
|
6663 su->su_badword, p);
|
|
6664 if (sps_flags & SPS_BEST)
|
|
6665 /* give a bonus for the good word
|
|
6666 * sounding the same as the bad
|
|
6667 * word */
|
|
6668 add_suggestion(su, &su->su_ga, p,
|
346
|
6669 su->su_badlen,
|
344
|
6670 RESCORE(score, sound_score),
|
324
|
6671 TRUE);
|
344
|
6672 else
|
|
6673 add_suggestion(su, &su->su_ga, p,
|
346
|
6674 su->su_badlen,
|
344
|
6675 score + sound_score, FALSE);
|
|
6676 }
|
323
|
6677 }
|
|
6678 }
|
|
6679
|
|
6680 /* Skip over other NUL bytes. */
|
|
6681 while (byts[n + 1] == 0)
|
|
6682 {
|
|
6683 ++n;
|
|
6684 ++curi[depth];
|
|
6685 }
|
|
6686 }
|
|
6687 else
|
|
6688 {
|
|
6689 /* Normal char, go one level deeper. */
|
|
6690 tword[depth++] = c;
|
|
6691 arridx[depth] = idxs[n];
|
|
6692 curi[depth] = 1;
|
|
6693 }
|
|
6694 }
|
324
|
6695
|
|
6696 line_breakcheck();
|
323
|
6697 }
|
|
6698 }
|
|
6699 }
|
|
6700 }
|
|
6701 }
|
|
6702
|
|
6703 /*
|
324
|
6704 * Copy "fword" to "cword", fixing case according to "flags".
|
323
|
6705 */
|
|
6706 static void
|
|
6707 make_case_word(fword, cword, flags)
|
|
6708 char_u *fword;
|
|
6709 char_u *cword;
|
|
6710 int flags;
|
|
6711 {
|
|
6712 if (flags & WF_ALLCAP)
|
|
6713 /* Make it all upper-case */
|
|
6714 allcap_copy(fword, cword);
|
|
6715 else if (flags & WF_ONECAP)
|
|
6716 /* Make the first letter upper-case */
|
324
|
6717 onecap_copy(fword, cword, TRUE);
|
323
|
6718 else
|
|
6719 /* Use goodword as-is. */
|
|
6720 STRCPY(cword, fword);
|
|
6721 }
|
|
6722
|
330
|
6723 /*
|
|
6724 * Use map string "map" for languages "lp".
|
|
6725 */
|
|
6726 static void
|
|
6727 set_map_str(lp, map)
|
|
6728 slang_T *lp;
|
|
6729 char_u *map;
|
|
6730 {
|
|
6731 char_u *p;
|
|
6732 int headc = 0;
|
|
6733 int c;
|
|
6734 int i;
|
|
6735
|
|
6736 if (*map == NUL)
|
|
6737 {
|
|
6738 lp->sl_has_map = FALSE;
|
|
6739 return;
|
|
6740 }
|
|
6741 lp->sl_has_map = TRUE;
|
|
6742
|
|
6743 /* Init the array and hash table empty. */
|
|
6744 for (i = 0; i < 256; ++i)
|
|
6745 lp->sl_map_array[i] = 0;
|
|
6746 #ifdef FEAT_MBYTE
|
|
6747 hash_init(&lp->sl_map_hash);
|
|
6748 #endif
|
|
6749
|
|
6750 /*
|
|
6751 * The similar characters are stored separated with slashes:
|
|
6752 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
|
|
6753 * before the same slash. For characters above 255 sl_map_hash is used.
|
|
6754 */
|
|
6755 for (p = map; *p != NUL; )
|
|
6756 {
|
|
6757 #ifdef FEAT_MBYTE
|
|
6758 c = mb_ptr2char_adv(&p);
|
|
6759 #else
|
|
6760 c = *p++;
|
|
6761 #endif
|
|
6762 if (c == '/')
|
|
6763 headc = 0;
|
|
6764 else
|
|
6765 {
|
|
6766 if (headc == 0)
|
|
6767 headc = c;
|
|
6768
|
|
6769 #ifdef FEAT_MBYTE
|
|
6770 /* Characters above 255 don't fit in sl_map_array[], put them in
|
|
6771 * the hash table. Each entry is the char, a NUL the headchar and
|
|
6772 * a NUL. */
|
|
6773 if (c >= 256)
|
|
6774 {
|
|
6775 int cl = mb_char2len(c);
|
|
6776 int headcl = mb_char2len(headc);
|
|
6777 char_u *b;
|
|
6778 hash_T hash;
|
|
6779 hashitem_T *hi;
|
|
6780
|
|
6781 b = alloc((unsigned)(cl + headcl + 2));
|
|
6782 if (b == NULL)
|
|
6783 return;
|
|
6784 mb_char2bytes(c, b);
|
|
6785 b[cl] = NUL;
|
|
6786 mb_char2bytes(headc, b + cl + 1);
|
|
6787 b[cl + 1 + headcl] = NUL;
|
|
6788 hash = hash_hash(b);
|
|
6789 hi = hash_lookup(&lp->sl_map_hash, b, hash);
|
|
6790 if (HASHITEM_EMPTY(hi))
|
|
6791 hash_add_item(&lp->sl_map_hash, hi, b, hash);
|
|
6792 else
|
|
6793 {
|
|
6794 /* This should have been checked when generating the .spl
|
|
6795 * file. */
|
|
6796 EMSG(_("E999: duplicate char in MAP entry"));
|
|
6797 vim_free(b);
|
|
6798 }
|
|
6799 }
|
|
6800 else
|
|
6801 #endif
|
|
6802 lp->sl_map_array[c] = headc;
|
|
6803 }
|
|
6804 }
|
|
6805 }
|
|
6806
|
323
|
6807 /*
|
|
6808 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
|
|
6809 * lines in the .aff file.
|
|
6810 */
|
|
6811 static int
|
|
6812 similar_chars(slang, c1, c2)
|
|
6813 slang_T *slang;
|
|
6814 int c1;
|
|
6815 int c2;
|
|
6816 {
|
330
|
6817 int m1, m2;
|
|
6818 #ifdef FEAT_MBYTE
|
|
6819 char_u buf[MB_MAXBYTES];
|
|
6820 hashitem_T *hi;
|
|
6821
|
|
6822 if (c1 >= 256)
|
|
6823 {
|
|
6824 buf[mb_char2bytes(c1, buf)] = 0;
|
|
6825 hi = hash_find(&slang->sl_map_hash, buf);
|
|
6826 if (HASHITEM_EMPTY(hi))
|
|
6827 m1 = 0;
|
|
6828 else
|
|
6829 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
|
|
6830 }
|
|
6831 else
|
|
6832 #endif
|
|
6833 m1 = slang->sl_map_array[c1];
|
|
6834 if (m1 == 0)
|
323
|
6835 return FALSE;
|
330
|
6836
|
|
6837
|
|
6838 #ifdef FEAT_MBYTE
|
|
6839 if (c2 >= 256)
|
|
6840 {
|
|
6841 buf[mb_char2bytes(c2, buf)] = 0;
|
|
6842 hi = hash_find(&slang->sl_map_hash, buf);
|
|
6843 if (HASHITEM_EMPTY(hi))
|
|
6844 m2 = 0;
|
|
6845 else
|
|
6846 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
|
|
6847 }
|
|
6848 else
|
|
6849 #endif
|
|
6850 m2 = slang->sl_map_array[c2];
|
|
6851
|
|
6852 return m1 == m2;
|
323
|
6853 }
|
|
6854
|
|
6855 /*
|
|
6856 * Add a suggestion to the list of suggestions.
|
|
6857 * Do not add a duplicate suggestion or suggestions with a bad score.
|
|
6858 * When "use_score" is not zero it's used, otherwise the score is computed
|
|
6859 * with spell_edit_score().
|
|
6860 */
|
|
6861 static void
|
346
|
6862 add_suggestion(su, gap, goodword, badlen, score, had_bonus)
|
323
|
6863 suginfo_T *su;
|
344
|
6864 garray_T *gap;
|
323
|
6865 char_u *goodword;
|
346
|
6866 int badlen; /* length of bad word used */
|
324
|
6867 int score;
|
344
|
6868 int had_bonus; /* value for st_had_bonus */
|
323
|
6869 {
|
|
6870 suggest_T *stp;
|
|
6871 int i;
|
346
|
6872 char_u *p = NULL;
|
|
6873 int c = 0;
|
323
|
6874
|
|
6875 /* Check that the word wasn't banned. */
|
|
6876 if (was_banned(su, goodword))
|
|
6877 return;
|
|
6878
|
346
|
6879 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
|
|
6880 * Remove the common part from "goodword". */
|
|
6881 i = badlen - su->su_badlen;
|
|
6882 if (i > 0)
|
|
6883 {
|
|
6884 /* This assumes there was no case folding or it didn't change the
|
|
6885 * length... */
|
|
6886 p = goodword + STRLEN(goodword) - i;
|
|
6887 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
|
|
6888 {
|
|
6889 badlen = su->su_badlen;
|
|
6890 c = *p;
|
|
6891 *p = NUL;
|
|
6892 }
|
|
6893 else
|
|
6894 p = NULL;
|
|
6895 }
|
|
6896
|
323
|
6897 if (score <= su->su_maxscore)
|
|
6898 {
|
|
6899 /* Check if the word is already there. */
|
344
|
6900 stp = &SUG(*gap, 0);
|
|
6901 for (i = gap->ga_len - 1; i >= 0; --i)
|
323
|
6902 if (STRCMP(stp[i].st_word, goodword) == 0)
|
|
6903 {
|
|
6904 /* Found it. Remember the lowest score. */
|
|
6905 if (stp[i].st_score > score)
|
324
|
6906 {
|
323
|
6907 stp[i].st_score = score;
|
324
|
6908 stp[i].st_had_bonus = had_bonus;
|
|
6909 }
|
323
|
6910 break;
|
|
6911 }
|
|
6912
|
344
|
6913 if (i < 0 && ga_grow(gap, 1) == OK)
|
323
|
6914 {
|
|
6915 /* Add a suggestion. */
|
344
|
6916 stp = &SUG(*gap, gap->ga_len);
|
323
|
6917 stp->st_word = vim_strsave(goodword);
|
|
6918 if (stp->st_word != NULL)
|
|
6919 {
|
|
6920 stp->st_score = score;
|
344
|
6921 stp->st_altscore = 0;
|
324
|
6922 stp->st_had_bonus = had_bonus;
|
346
|
6923 stp->st_orglen = badlen;
|
344
|
6924 ++gap->ga_len;
|
323
|
6925
|
|
6926 /* If we have too many suggestions now, sort the list and keep
|
|
6927 * the best suggestions. */
|
344
|
6928 if (gap->ga_len > SUG_MAX_COUNT(su))
|
|
6929 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
|
|
6930 SUG_CLEAN_COUNT(su));
|
323
|
6931 }
|
|
6932 }
|
|
6933 }
|
346
|
6934
|
|
6935 if (p != NULL)
|
|
6936 *p = c; /* restore "goodword" */
|
323
|
6937 }
|
|
6938
|
|
6939 /*
|
|
6940 * Add a word to be banned.
|
|
6941 */
|
|
6942 static void
|
|
6943 add_banned(su, word)
|
|
6944 suginfo_T *su;
|
|
6945 char_u *word;
|
|
6946 {
|
|
6947 char_u *s = vim_strsave(word);
|
|
6948 hash_T hash;
|
|
6949 hashitem_T *hi;
|
|
6950
|
|
6951 if (s != NULL)
|
|
6952 {
|
|
6953 hash = hash_hash(s);
|
|
6954 hi = hash_lookup(&su->su_banned, s, hash);
|
|
6955 if (HASHITEM_EMPTY(hi))
|
|
6956 hash_add_item(&su->su_banned, hi, s, hash);
|
|
6957 }
|
|
6958 }
|
|
6959
|
|
6960 /*
|
|
6961 * Return TRUE if a word appears in the list of banned words.
|
|
6962 */
|
|
6963 static int
|
|
6964 was_banned(su, word)
|
|
6965 suginfo_T *su;
|
|
6966 char_u *word;
|
|
6967 {
|
324
|
6968 hashitem_T *hi = hash_find(&su->su_banned, word);
|
|
6969
|
|
6970 return !HASHITEM_EMPTY(hi);
|
323
|
6971 }
|
|
6972
|
|
6973 /*
|
|
6974 * Free the banned words in "su".
|
|
6975 */
|
|
6976 static void
|
|
6977 free_banned(su)
|
|
6978 suginfo_T *su;
|
|
6979 {
|
|
6980 int todo;
|
|
6981 hashitem_T *hi;
|
|
6982
|
|
6983 todo = su->su_banned.ht_used;
|
|
6984 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
|
|
6985 {
|
|
6986 if (!HASHITEM_EMPTY(hi))
|
|
6987 {
|
|
6988 vim_free(hi->hi_key);
|
|
6989 --todo;
|
|
6990 }
|
|
6991 }
|
|
6992 hash_clear(&su->su_banned);
|
|
6993 }
|
|
6994
|
324
|
6995 /*
|
|
6996 * Recompute the score if sound-folding is possible. This is slow,
|
|
6997 * thus only done for the final results.
|
|
6998 */
|
|
6999 static void
|
|
7000 rescore_suggestions(su)
|
|
7001 suginfo_T *su;
|
|
7002 {
|
|
7003 langp_T *lp;
|
|
7004 suggest_T *stp;
|
|
7005 char_u sal_badword[MAXWLEN];
|
346
|
7006 char_u tword[MAXWLEN];
|
|
7007 char_u salword[MAXWLEN];
|
|
7008 char_u *p;
|
324
|
7009 int score;
|
|
7010 int i;
|
|
7011
|
|
7012 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
7013 lp->lp_slang != NULL; ++lp)
|
|
7014 {
|
|
7015 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
7016 {
|
|
7017 /* soundfold the bad word */
|
|
7018 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
|
|
7019
|
|
7020 for (i = 0; i < su->su_ga.ga_len; ++i)
|
|
7021 {
|
344
|
7022 stp = &SUG(su->su_ga, i);
|
324
|
7023 if (!stp->st_had_bonus)
|
|
7024 {
|
346
|
7025 if (stp->st_orglen <= su->su_badlen)
|
|
7026 p = sal_badword;
|
|
7027 else
|
|
7028 {
|
|
7029 /* soundfold the bad word with a different length */
|
|
7030 (void)spell_casefold(su->su_badptr, stp->st_orglen,
|
|
7031 tword, MAXWLEN);
|
|
7032 spell_soundfold(lp->lp_slang, tword, salword);
|
|
7033 p = salword;
|
|
7034 }
|
|
7035 score = spell_sound_score(lp->lp_slang, stp->st_word, p);
|
324
|
7036 stp->st_score = RESCORE(stp->st_score, score);
|
|
7037 }
|
|
7038 }
|
|
7039 break;
|
|
7040 }
|
|
7041 }
|
|
7042 }
|
|
7043
|
323
|
7044 static int
|
|
7045 #ifdef __BORLANDC__
|
|
7046 _RTLENTRYF
|
|
7047 #endif
|
|
7048 sug_compare __ARGS((const void *s1, const void *s2));
|
|
7049
|
|
7050 /*
|
|
7051 * Function given to qsort() to sort the suggestions on st_score.
|
|
7052 */
|
|
7053 static int
|
|
7054 #ifdef __BORLANDC__
|
|
7055 _RTLENTRYF
|
|
7056 #endif
|
|
7057 sug_compare(s1, s2)
|
|
7058 const void *s1;
|
|
7059 const void *s2;
|
|
7060 {
|
|
7061 suggest_T *p1 = (suggest_T *)s1;
|
|
7062 suggest_T *p2 = (suggest_T *)s2;
|
344
|
7063 int n = p1->st_score - p2->st_score;
|
|
7064
|
|
7065 if (n == 0)
|
|
7066 return p1->st_altscore - p2->st_altscore;
|
|
7067 return n;
|
323
|
7068 }
|
|
7069
|
|
7070 /*
|
|
7071 * Cleanup the suggestions:
|
|
7072 * - Sort on score.
|
|
7073 * - Remove words that won't be displayed.
|
344
|
7074 * Returns the maximum score in the list or "maxscore" unmodified.
|
323
|
7075 */
|
344
|
7076 static int
|
|
7077 cleanup_suggestions(gap, maxscore, keep)
|
|
7078 garray_T *gap;
|
|
7079 int maxscore;
|
324
|
7080 int keep; /* nr of suggestions to keep */
|
323
|
7081 {
|
344
|
7082 suggest_T *stp = &SUG(*gap, 0);
|
323
|
7083 int i;
|
|
7084
|
|
7085 /* Sort the list. */
|
344
|
7086 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
|
323
|
7087
|
|
7088 /* Truncate the list to the number of suggestions that will be displayed. */
|
344
|
7089 if (gap->ga_len > keep)
|
323
|
7090 {
|
344
|
7091 for (i = keep; i < gap->ga_len; ++i)
|
323
|
7092 vim_free(stp[i].st_word);
|
344
|
7093 gap->ga_len = keep;
|
|
7094 return stp[keep - 1].st_score;
|
323
|
7095 }
|
344
|
7096 return maxscore;
|
323
|
7097 }
|
|
7098
|
|
7099 /*
|
|
7100 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
|
|
7101 */
|
|
7102 static void
|
|
7103 spell_soundfold(slang, inword, res)
|
|
7104 slang_T *slang;
|
|
7105 char_u *inword;
|
|
7106 char_u *res;
|
|
7107 {
|
344
|
7108 salitem_T *smp;
|
323
|
7109 char_u word[MAXWLEN];
|
|
7110 #ifdef FEAT_MBYTE
|
|
7111 int l;
|
324
|
7112 int found_mbyte = FALSE;
|
323
|
7113 #endif
|
|
7114 char_u *s;
|
|
7115 char_u *t;
|
344
|
7116 char_u *pf;
|
323
|
7117 int i, j, z;
|
344
|
7118 int reslen;
|
323
|
7119 int n, k = 0;
|
|
7120 int z0;
|
|
7121 int k0;
|
|
7122 int n0;
|
|
7123 int c;
|
|
7124 int pri;
|
|
7125 int p0 = -333;
|
|
7126 int c0;
|
|
7127
|
324
|
7128 /* Remove accents, if wanted. We actually remove all non-word characters.
|
|
7129 * But keep white space. */
|
323
|
7130 if (slang->sl_rem_accents)
|
|
7131 {
|
|
7132 t = word;
|
|
7133 for (s = inword; *s != NUL; )
|
|
7134 {
|
324
|
7135 if (vim_iswhite(*s))
|
344
|
7136 {
|
|
7137 *t++ = ' ';
|
|
7138 s = skipwhite(s);
|
|
7139 }
|
323
|
7140 #ifdef FEAT_MBYTE
|
324
|
7141 else if (has_mbyte)
|
323
|
7142 {
|
|
7143 l = mb_ptr2len_check(s);
|
|
7144 if (SPELL_ISWORDP(s))
|
|
7145 {
|
|
7146 mch_memmove(t, s, l);
|
|
7147 t += l;
|
324
|
7148 if (l > 1)
|
|
7149 found_mbyte = TRUE;
|
323
|
7150 }
|
|
7151 s += l;
|
|
7152 }
|
324
|
7153 #endif
|
323
|
7154 else
|
|
7155 {
|
|
7156 if (SPELL_ISWORDP(s))
|
|
7157 *t++ = *s;
|
|
7158 ++s;
|
|
7159 }
|
|
7160 }
|
|
7161 *t = NUL;
|
|
7162 }
|
|
7163 else
|
324
|
7164 {
|
|
7165 #ifdef FEAT_MBYTE
|
|
7166 if (has_mbyte)
|
|
7167 for (s = inword; *s != NUL; s += l)
|
|
7168 if ((l = mb_ptr2len_check(s)) > 1)
|
|
7169 {
|
|
7170 found_mbyte = TRUE;
|
|
7171 break;
|
|
7172 }
|
|
7173 #endif
|
323
|
7174 STRCPY(word, inword);
|
324
|
7175 }
|
|
7176
|
|
7177 #ifdef FEAT_MBYTE
|
|
7178 /* If there are multi-byte characters in the word return it as-is, because
|
|
7179 * the following won't work. */
|
|
7180 if (found_mbyte)
|
|
7181 {
|
|
7182 STRCPY(res, word);
|
|
7183 return;
|
|
7184 }
|
|
7185 #endif
|
323
|
7186
|
344
|
7187 smp = (salitem_T *)slang->sl_sal.ga_data;
|
323
|
7188
|
|
7189 /*
|
|
7190 * This comes from Aspell phonet.cpp. Converted from C++ to C.
|
324
|
7191 * Changed to keep spaces.
|
323
|
7192 * TODO: support for multi-byte chars.
|
|
7193 */
|
344
|
7194 i = reslen = z = 0;
|
323
|
7195 while ((c = word[i]) != NUL)
|
|
7196 {
|
344
|
7197 /* Start with the first rule that has the character in the word. */
|
323
|
7198 n = slang->sl_sal_first[c];
|
|
7199 z0 = 0;
|
|
7200
|
|
7201 if (n >= 0)
|
|
7202 {
|
|
7203 /* check all rules for the same letter */
|
344
|
7204 for (; (s = smp[n].sm_lead)[0] == c; ++n)
|
323
|
7205 {
|
344
|
7206 /* Quickly skip entries that don't match the word. Most
|
|
7207 * entries are less then three chars, optimize for that. */
|
|
7208 k = smp[n].sm_leadlen;
|
|
7209 if (k > 1)
|
323
|
7210 {
|
344
|
7211 if (word[i + 1] != s[1])
|
|
7212 continue;
|
|
7213 if (k > 2)
|
|
7214 {
|
|
7215 for (j = 2; j < k; ++j)
|
|
7216 if (word[i + j] != s[j])
|
|
7217 break;
|
|
7218 if (j < k)
|
|
7219 continue;
|
|
7220 }
|
323
|
7221 }
|
|
7222
|
344
|
7223 if ((pf = smp[n].sm_oneoff) != NULL)
|
323
|
7224 {
|
344
|
7225 /* Check for match with one of the chars in "sm_oneoff". */
|
|
7226 while (*pf != NUL && *pf != word[i + k])
|
|
7227 ++pf;
|
|
7228 if (*pf == NUL)
|
|
7229 continue;
|
|
7230 ++k;
|
323
|
7231 }
|
344
|
7232 s = smp[n].sm_rules;
|
|
7233 pri = 5; /* default priority */
|
323
|
7234
|
|
7235 p0 = *s;
|
|
7236 k0 = k;
|
|
7237 while (*s == '-' && k > 1)
|
|
7238 {
|
|
7239 k--;
|
|
7240 s++;
|
|
7241 }
|
|
7242 if (*s == '<')
|
|
7243 s++;
|
344
|
7244 if (VIM_ISDIGIT(*s))
|
323
|
7245 {
|
|
7246 /* determine priority */
|
|
7247 pri = *s - '0';
|
|
7248 s++;
|
|
7249 }
|
|
7250 if (*s == '^' && *(s + 1) == '^')
|
|
7251 s++;
|
|
7252
|
|
7253 if (*s == NUL
|
|
7254 || (*s == '^'
|
324
|
7255 && (i == 0 || !(word[i - 1] == ' '
|
|
7256 || SPELL_ISWORDP(word + i - 1)))
|
323
|
7257 && (*(s + 1) != '$'
|
|
7258 || (!SPELL_ISWORDP(word + i + k0))))
|
|
7259 || (*s == '$' && i > 0
|
|
7260 && SPELL_ISWORDP(word + i - 1)
|
|
7261 && (!SPELL_ISWORDP(word + i + k0))))
|
|
7262 {
|
|
7263 /* search for followup rules, if: */
|
|
7264 /* followup and k > 1 and NO '-' in searchstring */
|
|
7265 c0 = word[i + k - 1];
|
|
7266 n0 = slang->sl_sal_first[c0];
|
|
7267
|
|
7268 if (slang->sl_followup && k > 1 && n0 >= 0
|
344
|
7269 && p0 != '-' && word[i + k] != NUL)
|
323
|
7270 {
|
|
7271 /* test follow-up rule for "word[i + k]" */
|
344
|
7272 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
|
323
|
7273 {
|
344
|
7274 /* Quickly skip entries that don't match the word.
|
|
7275 * */
|
|
7276 k0 = smp[n0].sm_leadlen;
|
|
7277 if (k0 > 1)
|
323
|
7278 {
|
344
|
7279 if (word[i + k] != s[1])
|
|
7280 continue;
|
|
7281 if (k0 > 2)
|
|
7282 {
|
|
7283 pf = word + i + k + 1;
|
|
7284 for (j = 2; j < k0; ++j)
|
|
7285 if (*pf++ != s[j])
|
|
7286 break;
|
|
7287 if (j < k0)
|
|
7288 continue;
|
|
7289 }
|
323
|
7290 }
|
344
|
7291 k0 += k - 1;
|
|
7292
|
|
7293 if ((pf = smp[n0].sm_oneoff) != NULL)
|
323
|
7294 {
|
344
|
7295 /* Check for match with one of the chars in
|
|
7296 * "sm_oneoff". */
|
|
7297 while (*pf != NUL && *pf != word[i + k0])
|
|
7298 ++pf;
|
|
7299 if (*pf == NUL)
|
|
7300 continue;
|
|
7301 ++k0;
|
323
|
7302 }
|
344
|
7303
|
|
7304 p0 = 5;
|
|
7305 s = smp[n0].sm_rules;
|
323
|
7306 while (*s == '-')
|
|
7307 {
|
344
|
7308 /* "k0" gets NOT reduced because
|
|
7309 * "if (k0 == k)" */
|
323
|
7310 s++;
|
|
7311 }
|
|
7312 if (*s == '<')
|
|
7313 s++;
|
344
|
7314 if (VIM_ISDIGIT(*s))
|
323
|
7315 {
|
|
7316 p0 = *s - '0';
|
|
7317 s++;
|
|
7318 }
|
|
7319
|
|
7320 if (*s == NUL
|
|
7321 /* *s == '^' cuts */
|
|
7322 || (*s == '$'
|
|
7323 && !SPELL_ISWORDP(word + i + k0)))
|
|
7324 {
|
|
7325 if (k0 == k)
|
|
7326 /* this is just a piece of the string */
|
|
7327 continue;
|
|
7328
|
|
7329 if (p0 < pri)
|
|
7330 /* priority too low */
|
|
7331 continue;
|
|
7332 /* rule fits; stop search */
|
|
7333 break;
|
|
7334 }
|
|
7335 }
|
|
7336
|
344
|
7337 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
|
323
|
7338 continue;
|
|
7339 }
|
|
7340
|
|
7341 /* replace string */
|
344
|
7342 s = smp[n].sm_to;
|
|
7343 pf = smp[n].sm_rules;
|
|
7344 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
|
323
|
7345 if (p0 == 1 && z == 0)
|
|
7346 {
|
|
7347 /* rule with '<' is used */
|
344
|
7348 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
|
|
7349 || res[reslen - 1] == *s))
|
|
7350 reslen--;
|
323
|
7351 z0 = 1;
|
|
7352 z = 1;
|
|
7353 k0 = 0;
|
|
7354 while (*s != NUL && word[i+k0] != NUL)
|
|
7355 {
|
|
7356 word[i + k0] = *s;
|
|
7357 k0++;
|
|
7358 s++;
|
|
7359 }
|
|
7360 if (k > k0)
|
|
7361 mch_memmove(word + i + k0, word + i + k,
|
|
7362 STRLEN(word + i + k) + 1);
|
|
7363
|
|
7364 /* new "actual letter" */
|
|
7365 c = word[i];
|
|
7366 }
|
|
7367 else
|
|
7368 {
|
|
7369 /* no '<' rule used */
|
|
7370 i += k - 1;
|
|
7371 z = 0;
|
344
|
7372 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
|
323
|
7373 {
|
344
|
7374 if (reslen == 0 || res[reslen - 1] != *s)
|
323
|
7375 {
|
344
|
7376 res[reslen] = *s;
|
|
7377 reslen++;
|
323
|
7378 }
|
|
7379 s++;
|
|
7380 }
|
|
7381 /* new "actual letter" */
|
|
7382 c = *s;
|
344
|
7383 if (strstr((char *)pf, "^^") != NULL)
|
323
|
7384 {
|
|
7385 if (c != NUL)
|
|
7386 {
|
344
|
7387 res[reslen] = c;
|
|
7388 reslen++;
|
323
|
7389 }
|
|
7390 mch_memmove(word, word + i + 1,
|
|
7391 STRLEN(word + i + 1) + 1);
|
|
7392 i = 0;
|
|
7393 z0 = 1;
|
|
7394 }
|
|
7395 }
|
|
7396 break;
|
|
7397 }
|
|
7398 }
|
|
7399 }
|
324
|
7400 else if (vim_iswhite(c))
|
|
7401 {
|
|
7402 c = ' ';
|
|
7403 k = 1;
|
|
7404 }
|
323
|
7405
|
|
7406 if (z0 == 0)
|
|
7407 {
|
344
|
7408 if (k && !p0 && reslen < MAXWLEN && c != NUL
|
|
7409 && (!slang->sl_collapse || reslen == 0
|
|
7410 || res[reslen - 1] != c))
|
323
|
7411 {
|
|
7412 /* condense only double letters */
|
344
|
7413 res[reslen] = c;
|
|
7414 reslen++;
|
323
|
7415 }
|
|
7416
|
|
7417 i++;
|
|
7418 z = 0;
|
|
7419 k = 0;
|
|
7420 }
|
|
7421 }
|
|
7422
|
344
|
7423 res[reslen] = NUL;
|
323
|
7424 }
|
|
7425
|
324
|
7426 /*
|
|
7427 * Return the score for how much words sound different.
|
|
7428 */
|
|
7429 static int
|
|
7430 spell_sound_score(slang, goodword, badsound)
|
|
7431 slang_T *slang;
|
|
7432 char_u *goodword; /* good word */
|
|
7433 char_u *badsound; /* sound-folded bad word */
|
|
7434 {
|
|
7435 char_u fword[MAXWLEN];
|
|
7436 char_u goodsound[MAXWLEN];
|
|
7437 int score;
|
|
7438
|
344
|
7439 /* Case-fold the goodword, needed for sound folding. */
|
324
|
7440 (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
|
|
7441
|
344
|
7442 /* sound-fold the goodword */
|
324
|
7443 spell_soundfold(slang, fword, goodsound);
|
|
7444
|
344
|
7445 /* Compute the edit distance-score of the sounds. This is slow but we
|
|
7446 * only do it for a small number of words. */
|
324
|
7447 score = spell_edit_score(badsound, goodsound);
|
|
7448
|
|
7449 /* Correction: adding/inserting "*" at the start (word starts with vowel)
|
|
7450 * shouldn't be counted so much, vowels halfway the word aren't counted at
|
|
7451 * all. */
|
|
7452 if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
|
|
7453 score -= SCORE_DEL / 2;
|
|
7454
|
|
7455 return score;
|
|
7456 }
|
344
|
7457
|
|
7458 /*
|
|
7459 * Compute a score for two sound-a-like words.
|
|
7460 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
|
|
7461 * Instead of a generic loop we write out the code. That keeps it fast by
|
|
7462 * avoiding checks that will not be possible.
|
|
7463 */
|
|
7464 static int
|
|
7465 soundalike_score(goodsound, badsound)
|
|
7466 char_u *goodsound; /* sound-folded good word */
|
|
7467 char_u *badsound; /* sound-folded bad word */
|
|
7468 {
|
|
7469 int goodlen = STRLEN(goodsound);
|
|
7470 int badlen = STRLEN(badsound);
|
|
7471 int n;
|
|
7472 char_u *pl, *ps;
|
|
7473 char_u *pl2, *ps2;
|
|
7474
|
|
7475 /* Return quickly if the lenghts are too different to be fixed by two
|
|
7476 * changes. */
|
|
7477 n = goodlen - badlen;
|
|
7478 if (n < -2 || n > 2)
|
|
7479 return SCORE_MAXMAX;
|
|
7480
|
|
7481 if (n > 0)
|
|
7482 {
|
|
7483 pl = goodsound; /* longest */
|
|
7484 ps = badsound;
|
|
7485 }
|
|
7486 else
|
|
7487 {
|
|
7488 pl = badsound; /* longest */
|
|
7489 ps = goodsound;
|
|
7490 }
|
|
7491
|
|
7492 /* Skip over the identical part. */
|
|
7493 while (*pl == *ps && *pl != NUL)
|
|
7494 {
|
|
7495 ++pl;
|
|
7496 ++ps;
|
|
7497 }
|
|
7498
|
|
7499 switch (n)
|
|
7500 {
|
|
7501 case -2:
|
|
7502 case 2:
|
|
7503 /*
|
|
7504 * Must delete two characters from "pl".
|
|
7505 */
|
|
7506 ++pl; /* first delete */
|
|
7507 while (*pl == *ps)
|
|
7508 {
|
|
7509 ++pl;
|
|
7510 ++ps;
|
|
7511 }
|
|
7512 /* strings must be equal after second delete */
|
|
7513 if (STRCMP(pl + 1, ps) == 0)
|
|
7514 return SCORE_DEL * 2;
|
|
7515
|
|
7516 /* Failed to compare. */
|
|
7517 break;
|
|
7518
|
|
7519 case -1:
|
|
7520 case 1:
|
|
7521 /*
|
|
7522 * Minimal one delete from "pl" required.
|
|
7523 */
|
|
7524
|
|
7525 /* 1: delete */
|
|
7526 pl2 = pl + 1;
|
|
7527 ps2 = ps;
|
|
7528 while (*pl2 == *ps2)
|
|
7529 {
|
|
7530 if (*pl2 == NUL) /* reached the end */
|
|
7531 return SCORE_DEL;
|
|
7532 ++pl2;
|
|
7533 ++ps2;
|
|
7534 }
|
|
7535
|
|
7536 /* 2: delete then swap, then rest must be equal */
|
|
7537 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
|
|
7538 && STRCMP(pl2 + 2, ps2 + 2) == 0)
|
|
7539 return SCORE_DEL + SCORE_SWAP;
|
|
7540
|
|
7541 /* 3: delete then substitute, then the rest must be equal */
|
|
7542 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
|
|
7543 return SCORE_DEL + SCORE_SUBST;
|
|
7544
|
|
7545 /* 4: first swap then delete */
|
|
7546 if (pl[0] == ps[1] && pl[1] == ps[0])
|
|
7547 {
|
|
7548 pl2 = pl + 2; /* swap, skip two chars */
|
|
7549 ps2 = ps + 2;
|
|
7550 while (*pl2 == *ps2)
|
|
7551 {
|
|
7552 ++pl2;
|
|
7553 ++ps2;
|
|
7554 }
|
|
7555 /* delete a char and then strings must be equal */
|
|
7556 if (STRCMP(pl2 + 1, ps2) == 0)
|
|
7557 return SCORE_SWAP + SCORE_DEL;
|
|
7558 }
|
|
7559
|
|
7560 /* 5: first substitute then delete */
|
|
7561 pl2 = pl + 1; /* substitute, skip one char */
|
|
7562 ps2 = ps + 1;
|
|
7563 while (*pl2 == *ps2)
|
|
7564 {
|
|
7565 ++pl2;
|
|
7566 ++ps2;
|
|
7567 }
|
|
7568 /* delete a char and then strings must be equal */
|
|
7569 if (STRCMP(pl2 + 1, ps2) == 0)
|
|
7570 return SCORE_SUBST + SCORE_DEL;
|
|
7571
|
|
7572 /* Failed to compare. */
|
|
7573 break;
|
|
7574
|
|
7575 case 0:
|
|
7576 /*
|
|
7577 * Lenghts are equal, thus changes must result in same length: An
|
|
7578 * insert is only possible in combination with a delete.
|
|
7579 * 1: check if for identical strings
|
|
7580 */
|
|
7581 if (*pl == NUL)
|
|
7582 return 0;
|
|
7583
|
|
7584 /* 2: swap */
|
|
7585 if (pl[0] == ps[1] && pl[1] == ps[0])
|
|
7586 {
|
|
7587 pl2 = pl + 2; /* swap, skip two chars */
|
|
7588 ps2 = ps + 2;
|
|
7589 while (*pl2 == *ps2)
|
|
7590 {
|
|
7591 if (*pl2 == NUL) /* reached the end */
|
|
7592 return SCORE_SWAP;
|
|
7593 ++pl2;
|
|
7594 ++ps2;
|
|
7595 }
|
|
7596 /* 3: swap and swap again */
|
|
7597 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
|
|
7598 && STRCMP(pl2 + 2, ps2 + 2) == 0)
|
|
7599 return SCORE_SWAP + SCORE_SWAP;
|
|
7600
|
|
7601 /* 4: swap and substitute */
|
|
7602 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
|
|
7603 return SCORE_SWAP + SCORE_SUBST;
|
|
7604 }
|
|
7605
|
|
7606 /* 5: substitute */
|
|
7607 pl2 = pl + 1;
|
|
7608 ps2 = ps + 1;
|
|
7609 while (*pl2 == *ps2)
|
|
7610 {
|
|
7611 if (*pl2 == NUL) /* reached the end */
|
|
7612 return SCORE_SUBST;
|
|
7613 ++pl2;
|
|
7614 ++ps2;
|
|
7615 }
|
|
7616
|
|
7617 /* 6: substitute and swap */
|
|
7618 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
|
|
7619 && STRCMP(pl2 + 2, ps2 + 2) == 0)
|
|
7620 return SCORE_SUBST + SCORE_SWAP;
|
|
7621
|
|
7622 /* 7: substitute and substitute */
|
|
7623 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
|
|
7624 return SCORE_SUBST + SCORE_SUBST;
|
|
7625
|
|
7626 /* 8: insert then delete */
|
|
7627 pl2 = pl;
|
|
7628 ps2 = ps + 1;
|
|
7629 while (*pl2 == *ps2)
|
|
7630 {
|
|
7631 ++pl2;
|
|
7632 ++ps2;
|
|
7633 }
|
|
7634 if (STRCMP(pl2 + 1, ps2) == 0)
|
|
7635 return SCORE_INS + SCORE_DEL;
|
|
7636
|
|
7637 /* 9: delete then insert */
|
|
7638 pl2 = pl + 1;
|
|
7639 ps2 = ps;
|
|
7640 while (*pl2 == *ps2)
|
|
7641 {
|
|
7642 ++pl2;
|
|
7643 ++ps2;
|
|
7644 }
|
|
7645 if (STRCMP(pl2, ps2 + 1) == 0)
|
|
7646 return SCORE_INS + SCORE_DEL;
|
|
7647
|
|
7648 /* Failed to compare. */
|
|
7649 break;
|
|
7650 }
|
|
7651
|
|
7652 return SCORE_MAXMAX;
|
|
7653 }
|
324
|
7654
|
323
|
7655 /*
|
|
7656 * Compute the "edit distance" to turn "badword" into "goodword". The less
|
344
|
7657 * deletes/inserts/substitutes/swaps are required the lower the score.
|
324
|
7658 *
|
323
|
7659 * The algorithm comes from Aspell editdist.cpp, edit_distance().
|
324
|
7660 * It has been converted from C++ to C and modified to support multi-byte
|
|
7661 * characters.
|
323
|
7662 */
|
|
7663 static int
|
|
7664 spell_edit_score(badword, goodword)
|
|
7665 char_u *badword;
|
|
7666 char_u *goodword;
|
|
7667 {
|
|
7668 int *cnt;
|
|
7669 int badlen, goodlen;
|
|
7670 int j, i;
|
|
7671 int t;
|
|
7672 int bc, gc;
|
324
|
7673 int pbc, pgc;
|
|
7674 #ifdef FEAT_MBYTE
|
|
7675 char_u *p;
|
|
7676 int wbadword[MAXWLEN];
|
|
7677 int wgoodword[MAXWLEN];
|
|
7678
|
|
7679 if (has_mbyte)
|
|
7680 {
|
|
7681 /* Get the characters from the multi-byte strings and put them in an
|
|
7682 * int array for easy access. */
|
|
7683 for (p = badword, badlen = 0; *p != NUL; )
|
|
7684 wbadword[badlen++] = mb_ptr2char_adv(&p);
|
|
7685 ++badlen;
|
|
7686 for (p = goodword, goodlen = 0; *p != NUL; )
|
|
7687 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
|
|
7688 ++goodlen;
|
|
7689 }
|
|
7690 else
|
|
7691 #endif
|
|
7692 {
|
|
7693 badlen = STRLEN(badword) + 1;
|
|
7694 goodlen = STRLEN(goodword) + 1;
|
|
7695 }
|
323
|
7696
|
|
7697 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
|
|
7698 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
|
|
7699 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
|
|
7700 TRUE);
|
324
|
7701 if (cnt == NULL)
|
|
7702 return 0; /* out of memory */
|
323
|
7703
|
|
7704 CNT(0, 0) = 0;
|
|
7705 for (j = 1; j <= goodlen; ++j)
|
|
7706 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
|
|
7707
|
|
7708 for (i = 1; i <= badlen; ++i)
|
|
7709 {
|
|
7710 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
|
|
7711 for (j = 1; j <= goodlen; ++j)
|
|
7712 {
|
324
|
7713 #ifdef FEAT_MBYTE
|
|
7714 if (has_mbyte)
|
|
7715 {
|
|
7716 bc = wbadword[i - 1];
|
|
7717 gc = wgoodword[j - 1];
|
|
7718 }
|
|
7719 else
|
|
7720 #endif
|
|
7721 {
|
|
7722 bc = badword[i - 1];
|
|
7723 gc = goodword[j - 1];
|
|
7724 }
|
323
|
7725 if (bc == gc)
|
|
7726 CNT(i, j) = CNT(i - 1, j - 1);
|
|
7727 else
|
|
7728 {
|
|
7729 /* Use a better score when there is only a case difference. */
|
324
|
7730 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
|
323
|
7731 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
|
|
7732 else
|
|
7733 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
|
|
7734
|
324
|
7735 if (i > 1 && j > 1)
|
323
|
7736 {
|
324
|
7737 #ifdef FEAT_MBYTE
|
|
7738 if (has_mbyte)
|
|
7739 {
|
|
7740 pbc = wbadword[i - 2];
|
|
7741 pgc = wgoodword[j - 2];
|
|
7742 }
|
|
7743 else
|
|
7744 #endif
|
|
7745 {
|
|
7746 pbc = badword[i - 2];
|
|
7747 pgc = goodword[j - 2];
|
|
7748 }
|
|
7749 if (bc == pgc && pbc == gc)
|
|
7750 {
|
|
7751 t = SCORE_SWAP + CNT(i - 2, j - 2);
|
|
7752 if (t < CNT(i, j))
|
|
7753 CNT(i, j) = t;
|
|
7754 }
|
323
|
7755 }
|
|
7756 t = SCORE_DEL + CNT(i - 1, j);
|
|
7757 if (t < CNT(i, j))
|
|
7758 CNT(i, j) = t;
|
|
7759 t = SCORE_INS + CNT(i, j - 1);
|
|
7760 if (t < CNT(i, j))
|
|
7761 CNT(i, j) = t;
|
|
7762 }
|
|
7763 }
|
|
7764 }
|
344
|
7765
|
|
7766 i = CNT(badlen - 1, goodlen - 1);
|
|
7767 vim_free(cnt);
|
|
7768 return i;
|
323
|
7769 }
|
307
|
7770
|
236
|
7771 #endif /* FEAT_SYN_HL */
|