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