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