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