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