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