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 */
|
|
1959 wordnode_T *si_keeproot; /* tree with keep-case words */
|
|
1960 sblock_T *si_blocks; /* memory blocks used */
|
|
1961 int si_ascii; /* handling only ASCII words */
|
310
|
1962 int si_add; /* addition file */
|
300
|
1963 int si_region; /* region mask */
|
|
1964 vimconv_T si_conv; /* for conversion to 'encoding' */
|
302
|
1965 int si_memtot; /* runtime memory used */
|
310
|
1966 int si_verbose; /* verbose messages */
|
316
|
1967 int si_region_count; /* number of regions supported (1 when there
|
|
1968 are no regions) */
|
|
1969 char_u si_region_name[16]; /* region names (if count > 1) */
|
323
|
1970
|
|
1971 garray_T si_rep; /* list of fromto_T entries from REP lines */
|
|
1972 garray_T si_sal; /* list of fromto_T entries from SAL lines */
|
|
1973 int si_followup; /* soundsalike: ? */
|
|
1974 int si_collapse; /* soundsalike: ? */
|
|
1975 int si_rem_accents; /* soundsalike: remove accents */
|
|
1976 garray_T si_map; /* MAP info concatenated */
|
300
|
1977 } spellinfo_T;
|
249
|
1978
|
300
|
1979 static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
|
323
|
1980 static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
|
|
1981 static int sal_to_bool __ARGS((char_u *s));
|
240
|
1982 static int has_non_ascii __ARGS((char_u *s));
|
300
|
1983 static void spell_free_aff __ARGS((afffile_T *aff));
|
|
1984 static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
|
307
|
1985 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
|
1986 static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
|
|
1987 static void *getroom __ARGS((sblock_T **blp, size_t len));
|
|
1988 static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
|
|
1989 static void free_blocks __ARGS((sblock_T *bl));
|
|
1990 static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
|
316
|
1991 static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region));
|
300
|
1992 static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp));
|
310
|
1993 static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
|
300
|
1994 static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
|
|
1995 static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
|
316
|
1996 static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
|
300
|
1997 static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
|
323
|
1998 static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
|
310
|
1999 static void init_spellfile __ARGS((void));
|
236
|
2000
|
|
2001 /*
|
323
|
2002 * Read the affix file "fname".
|
316
|
2003 * Returns an afffile_T, NULL for complete failure.
|
236
|
2004 */
|
|
2005 static afffile_T *
|
300
|
2006 spell_read_aff(fname, spin)
|
236
|
2007 char_u *fname;
|
300
|
2008 spellinfo_T *spin;
|
236
|
2009 {
|
|
2010 FILE *fd;
|
|
2011 afffile_T *aff;
|
|
2012 char_u rline[MAXLINELEN];
|
|
2013 char_u *line;
|
|
2014 char_u *pc = NULL;
|
|
2015 char_u *(items[6]);
|
|
2016 int itemcnt;
|
|
2017 char_u *p;
|
|
2018 int lnum = 0;
|
|
2019 affheader_T *cur_aff = NULL;
|
|
2020 int aff_todo = 0;
|
|
2021 hashtab_T *tp;
|
255
|
2022 char_u *low = NULL;
|
|
2023 char_u *fol = NULL;
|
|
2024 char_u *upp = NULL;
|
307
|
2025 static char *e_affname = N_("Affix name too long in %s line %d: %s");
|
323
|
2026 int do_rep;
|
|
2027 int do_sal;
|
|
2028 int do_map;
|
|
2029 int found_map = FALSE;
|
324
|
2030 hashitem_T *hi;
|
236
|
2031
|
300
|
2032 /*
|
|
2033 * Open the file.
|
|
2034 */
|
310
|
2035 fd = mch_fopen((char *)fname, "r");
|
236
|
2036 if (fd == NULL)
|
|
2037 {
|
|
2038 EMSG2(_(e_notopen), fname);
|
|
2039 return NULL;
|
|
2040 }
|
|
2041
|
310
|
2042 if (spin->si_verbose || p_verbose > 2)
|
|
2043 {
|
|
2044 if (!spin->si_verbose)
|
|
2045 verbose_enter();
|
|
2046 smsg((char_u *)_("Reading affix file %s..."), fname);
|
|
2047 out_flush();
|
|
2048 if (!spin->si_verbose)
|
|
2049 verbose_leave();
|
|
2050 }
|
236
|
2051
|
323
|
2052 /* Only do REP lines when not done in another .aff file already. */
|
|
2053 do_rep = spin->si_rep.ga_len == 0;
|
|
2054
|
|
2055 /* Only do SAL lines when not done in another .aff file already. */
|
|
2056 do_sal = spin->si_sal.ga_len == 0;
|
|
2057
|
|
2058 /* Only do MAP lines when not done in another .aff file already. */
|
|
2059 do_map = spin->si_map.ga_len == 0;
|
|
2060
|
300
|
2061 /*
|
|
2062 * Allocate and init the afffile_T structure.
|
|
2063 */
|
|
2064 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
|
236
|
2065 if (aff == NULL)
|
|
2066 return NULL;
|
|
2067 hash_init(&aff->af_pref);
|
|
2068 hash_init(&aff->af_suff);
|
|
2069
|
|
2070 /*
|
|
2071 * Read all the lines in the file one by one.
|
|
2072 */
|
255
|
2073 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
236
|
2074 {
|
255
|
2075 line_breakcheck();
|
236
|
2076 ++lnum;
|
|
2077
|
|
2078 /* Skip comment lines. */
|
|
2079 if (*rline == '#')
|
|
2080 continue;
|
|
2081
|
|
2082 /* Convert from "SET" to 'encoding' when needed. */
|
|
2083 vim_free(pc);
|
310
|
2084 #ifdef FEAT_MBYTE
|
300
|
2085 if (spin->si_conv.vc_type != CONV_NONE)
|
236
|
2086 {
|
300
|
2087 pc = string_convert(&spin->si_conv, rline, NULL);
|
255
|
2088 if (pc == NULL)
|
|
2089 {
|
|
2090 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
2091 fname, lnum, rline);
|
|
2092 continue;
|
|
2093 }
|
236
|
2094 line = pc;
|
|
2095 }
|
|
2096 else
|
310
|
2097 #endif
|
236
|
2098 {
|
|
2099 pc = NULL;
|
|
2100 line = rline;
|
|
2101 }
|
|
2102
|
|
2103 /* Split the line up in white separated items. Put a NUL after each
|
|
2104 * item. */
|
|
2105 itemcnt = 0;
|
|
2106 for (p = line; ; )
|
|
2107 {
|
|
2108 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
|
|
2109 ++p;
|
|
2110 if (*p == NUL)
|
|
2111 break;
|
300
|
2112 if (itemcnt == 6) /* too many items */
|
|
2113 break;
|
236
|
2114 items[itemcnt++] = p;
|
300
|
2115 while (*p > ' ') /* skip until white space or CR/NL */
|
236
|
2116 ++p;
|
|
2117 if (*p == NUL)
|
|
2118 break;
|
|
2119 *p++ = NUL;
|
|
2120 }
|
|
2121
|
|
2122 /* Handle non-empty lines. */
|
|
2123 if (itemcnt > 0)
|
|
2124 {
|
|
2125 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
|
|
2126 && aff->af_enc == NULL)
|
|
2127 {
|
310
|
2128 #ifdef FEAT_MBYTE
|
300
|
2129 /* Setup for conversion from "ENC" to 'encoding'. */
|
|
2130 aff->af_enc = enc_canonize(items[1]);
|
|
2131 if (aff->af_enc != NULL && !spin->si_ascii
|
|
2132 && convert_setup(&spin->si_conv, aff->af_enc,
|
|
2133 p_enc) == FAIL)
|
|
2134 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
|
2135 fname, aff->af_enc, p_enc);
|
310
|
2136 #else
|
|
2137 smsg((char_u *)_("Conversion in %s not supported"), fname);
|
|
2138 #endif
|
236
|
2139 }
|
302
|
2140 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
|
|
2141 {
|
323
|
2142 /* ignored, we always split */
|
302
|
2143 }
|
323
|
2144 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
|
300
|
2145 {
|
323
|
2146 /* ignored, we look in the tree for what chars may appear */
|
300
|
2147 }
|
307
|
2148 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
|
|
2149 && aff->af_rar == 0)
|
|
2150 {
|
|
2151 aff->af_rar = items[1][0];
|
|
2152 if (items[1][1] != NUL)
|
|
2153 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
2154 }
|
310
|
2155 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
|
|
2156 && aff->af_kep == 0)
|
307
|
2157 {
|
310
|
2158 aff->af_kep = items[1][0];
|
307
|
2159 if (items[1][1] != NUL)
|
|
2160 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
|
2161 }
|
236
|
2162 else if ((STRCMP(items[0], "PFX") == 0
|
|
2163 || STRCMP(items[0], "SFX") == 0)
|
|
2164 && aff_todo == 0
|
|
2165 && itemcnt == 4)
|
|
2166 {
|
|
2167 /* New affix letter. */
|
300
|
2168 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
|
|
2169 sizeof(affheader_T));
|
236
|
2170 if (cur_aff == NULL)
|
|
2171 break;
|
|
2172 cur_aff->ah_key[0] = *items[1];
|
|
2173 cur_aff->ah_key[1] = NUL;
|
|
2174 if (items[1][1] != NUL)
|
307
|
2175 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
|
236
|
2176 if (*items[2] == 'Y')
|
|
2177 cur_aff->ah_combine = TRUE;
|
300
|
2178 else if (*items[2] != 'N')
|
236
|
2179 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
|
|
2180 fname, lnum, items[2]);
|
|
2181 if (*items[0] == 'P')
|
|
2182 tp = &aff->af_pref;
|
|
2183 else
|
|
2184 tp = &aff->af_suff;
|
300
|
2185 aff_todo = atoi((char *)items[3]);
|
324
|
2186 hi = hash_find(tp, cur_aff->ah_key);
|
|
2187 if (!HASHITEM_EMPTY(hi))
|
300
|
2188 {
|
236
|
2189 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
|
|
2190 fname, lnum, items[1]);
|
300
|
2191 aff_todo = 0;
|
|
2192 }
|
236
|
2193 else
|
|
2194 hash_add(tp, cur_aff->ah_key);
|
|
2195 }
|
|
2196 else if ((STRCMP(items[0], "PFX") == 0
|
|
2197 || STRCMP(items[0], "SFX") == 0)
|
|
2198 && aff_todo > 0
|
|
2199 && STRCMP(cur_aff->ah_key, items[1]) == 0
|
|
2200 && itemcnt == 5)
|
|
2201 {
|
|
2202 affentry_T *aff_entry;
|
|
2203
|
|
2204 /* New item for an affix letter. */
|
|
2205 --aff_todo;
|
300
|
2206 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
|
|
2207 sizeof(affentry_T));
|
236
|
2208 if (aff_entry == NULL)
|
|
2209 break;
|
240
|
2210
|
236
|
2211 if (STRCMP(items[2], "0") != 0)
|
300
|
2212 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
|
|
2213 items[2]);
|
236
|
2214 if (STRCMP(items[3], "0") != 0)
|
300
|
2215 aff_entry->ae_add = getroom_save(&spin->si_blocks,
|
|
2216 items[3]);
|
236
|
2217
|
300
|
2218 /* Don't use an affix entry with non-ASCII characters when
|
|
2219 * "spin->si_ascii" is TRUE. */
|
|
2220 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
|
240
|
2221 || has_non_ascii(aff_entry->ae_add)))
|
|
2222 {
|
|
2223 aff_entry->ae_next = cur_aff->ah_first;
|
|
2224 cur_aff->ah_first = aff_entry;
|
300
|
2225
|
|
2226 if (STRCMP(items[4], ".") != 0)
|
|
2227 {
|
|
2228 char_u buf[MAXLINELEN];
|
|
2229
|
|
2230 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
|
|
2231 items[4]);
|
|
2232 if (*items[0] == 'P')
|
|
2233 sprintf((char *)buf, "^%s", items[4]);
|
|
2234 else
|
|
2235 sprintf((char *)buf, "%s$", items[4]);
|
|
2236 aff_entry->ae_prog = vim_regcomp(buf,
|
|
2237 RE_MAGIC + RE_STRING);
|
|
2238 }
|
240
|
2239 }
|
236
|
2240 }
|
255
|
2241 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
|
|
2242 {
|
|
2243 if (fol != NULL)
|
|
2244 smsg((char_u *)_("Duplicate FOL in %s line %d"),
|
|
2245 fname, lnum);
|
|
2246 else
|
|
2247 fol = vim_strsave(items[1]);
|
|
2248 }
|
|
2249 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
|
|
2250 {
|
|
2251 if (low != NULL)
|
|
2252 smsg((char_u *)_("Duplicate LOW in %s line %d"),
|
|
2253 fname, lnum);
|
|
2254 else
|
|
2255 low = vim_strsave(items[1]);
|
|
2256 }
|
|
2257 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
|
|
2258 {
|
|
2259 if (upp != NULL)
|
|
2260 smsg((char_u *)_("Duplicate UPP in %s line %d"),
|
|
2261 fname, lnum);
|
|
2262 else
|
|
2263 upp = vim_strsave(items[1]);
|
|
2264 }
|
236
|
2265 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
|
323
|
2266 {
|
236
|
2267 /* Ignore REP count */;
|
323
|
2268 if (!isdigit(*items[1]))
|
|
2269 smsg((char_u *)_("Expected REP count in %s line %d"),
|
|
2270 fname, lnum);
|
|
2271 }
|
236
|
2272 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
|
|
2273 {
|
|
2274 /* REP item */
|
323
|
2275 if (do_rep)
|
|
2276 add_fromto(spin, &spin->si_rep, items[1], items[2]);
|
|
2277 }
|
|
2278 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
|
|
2279 {
|
|
2280 /* MAP item or count */
|
|
2281 if (!found_map)
|
|
2282 {
|
|
2283 /* First line contains the count. */
|
|
2284 found_map = TRUE;
|
|
2285 if (!isdigit(*items[1]))
|
|
2286 smsg((char_u *)_("Expected MAP count in %s line %d"),
|
|
2287 fname, lnum);
|
|
2288 }
|
|
2289 else if (do_map)
|
|
2290 {
|
|
2291 /* We simply concatenate all the MAP strings, separated by
|
|
2292 * slashes. */
|
|
2293 ga_concat(&spin->si_map, items[1]);
|
|
2294 ga_append(&spin->si_map, '/');
|
|
2295 }
|
|
2296 }
|
|
2297 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
|
|
2298 {
|
|
2299 if (do_sal)
|
|
2300 {
|
|
2301 /* SAL item (sounds-a-like)
|
|
2302 * Either one of the known keys or a from-to pair. */
|
|
2303 if (STRCMP(items[1], "followup") == 0)
|
|
2304 spin->si_followup = sal_to_bool(items[2]);
|
|
2305 else if (STRCMP(items[1], "collapse_result") == 0)
|
|
2306 spin->si_collapse = sal_to_bool(items[2]);
|
|
2307 else if (STRCMP(items[1], "remove_accents") == 0)
|
|
2308 spin->si_rem_accents = sal_to_bool(items[2]);
|
|
2309 else
|
|
2310 /* when "to" is "_" it means empty */
|
|
2311 add_fromto(spin, &spin->si_sal, items[1],
|
|
2312 STRCMP(items[2], "_") == 0 ? (char_u *)""
|
|
2313 : items[2]);
|
|
2314 }
|
236
|
2315 }
|
300
|
2316 else
|
236
|
2317 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
|
|
2318 fname, lnum, items[0]);
|
|
2319 }
|
|
2320 }
|
|
2321
|
255
|
2322 if (fol != NULL || low != NULL || upp != NULL)
|
|
2323 {
|
316
|
2324 /*
|
|
2325 * Don't write a word table for an ASCII file, so that we don't check
|
|
2326 * for conflicts with a word table that matches 'encoding'.
|
324
|
2327 * Don't write one for utf-8 either, we use utf_*() and
|
316
|
2328 * mb_get_class(), the list of chars in the file will be incomplete.
|
|
2329 */
|
|
2330 if (!spin->si_ascii
|
|
2331 #ifdef FEAT_MBYTE
|
|
2332 && !enc_utf8
|
|
2333 #endif
|
|
2334 )
|
260
|
2335 {
|
|
2336 if (fol == NULL || low == NULL || upp == NULL)
|
|
2337 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
|
|
2338 else
|
316
|
2339 (void)set_spell_chartab(fol, low, upp);
|
260
|
2340 }
|
255
|
2341
|
|
2342 vim_free(fol);
|
|
2343 vim_free(low);
|
|
2344 vim_free(upp);
|
|
2345 }
|
|
2346
|
236
|
2347 vim_free(pc);
|
|
2348 fclose(fd);
|
|
2349 return aff;
|
|
2350 }
|
|
2351
|
|
2352 /*
|
323
|
2353 * Add a from-to item to "gap". Used for REP and SAL items.
|
|
2354 * They are stored case-folded.
|
|
2355 */
|
|
2356 static void
|
|
2357 add_fromto(spin, gap, from, to)
|
|
2358 spellinfo_T *spin;
|
|
2359 garray_T *gap;
|
|
2360 char_u *from;
|
|
2361 char_u *to;
|
|
2362 {
|
|
2363 fromto_T *ftp;
|
|
2364 char_u word[MAXWLEN];
|
|
2365
|
|
2366 if (ga_grow(gap, 1) == OK)
|
|
2367 {
|
|
2368 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
|
|
2369 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
|
|
2370 ftp->ft_from = getroom_save(&spin->si_blocks, word);
|
|
2371 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
|
|
2372 ftp->ft_to = getroom_save(&spin->si_blocks, word);
|
|
2373 ++gap->ga_len;
|
|
2374 }
|
|
2375 }
|
|
2376
|
|
2377 /*
|
|
2378 * Convert a boolean argument in a SAL line to TRUE or FALSE;
|
|
2379 */
|
|
2380 static int
|
|
2381 sal_to_bool(s)
|
|
2382 char_u *s;
|
|
2383 {
|
|
2384 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
|
|
2385 }
|
|
2386
|
|
2387 /*
|
240
|
2388 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
|
|
2389 * When "s" is NULL FALSE is returned.
|
|
2390 */
|
|
2391 static int
|
|
2392 has_non_ascii(s)
|
|
2393 char_u *s;
|
|
2394 {
|
|
2395 char_u *p;
|
|
2396
|
|
2397 if (s != NULL)
|
|
2398 for (p = s; *p != NUL; ++p)
|
|
2399 if (*p >= 128)
|
|
2400 return TRUE;
|
|
2401 return FALSE;
|
|
2402 }
|
|
2403
|
|
2404 /*
|
236
|
2405 * Free the structure filled by spell_read_aff().
|
|
2406 */
|
|
2407 static void
|
|
2408 spell_free_aff(aff)
|
|
2409 afffile_T *aff;
|
|
2410 {
|
|
2411 hashtab_T *ht;
|
|
2412 hashitem_T *hi;
|
|
2413 int todo;
|
|
2414 affheader_T *ah;
|
300
|
2415 affentry_T *ae;
|
236
|
2416
|
|
2417 vim_free(aff->af_enc);
|
|
2418
|
300
|
2419 /* All this trouble to foree the "ae_prog" items... */
|
236
|
2420 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
|
|
2421 {
|
|
2422 todo = ht->ht_used;
|
|
2423 for (hi = ht->ht_array; todo > 0; ++hi)
|
|
2424 {
|
|
2425 if (!HASHITEM_EMPTY(hi))
|
|
2426 {
|
|
2427 --todo;
|
|
2428 ah = HI2AH(hi);
|
300
|
2429 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
|
2430 vim_free(ae->ae_prog);
|
236
|
2431 }
|
|
2432 }
|
|
2433 if (ht == &aff->af_suff)
|
|
2434 break;
|
|
2435 }
|
300
|
2436
|
236
|
2437 hash_clear(&aff->af_pref);
|
|
2438 hash_clear(&aff->af_suff);
|
|
2439 }
|
|
2440
|
|
2441 /*
|
300
|
2442 * Read dictionary file "fname".
|
236
|
2443 * Returns OK or FAIL;
|
|
2444 */
|
|
2445 static int
|
300
|
2446 spell_read_dic(fname, spin, affile)
|
236
|
2447 char_u *fname;
|
300
|
2448 spellinfo_T *spin;
|
|
2449 afffile_T *affile;
|
236
|
2450 {
|
300
|
2451 hashtab_T ht;
|
236
|
2452 char_u line[MAXLINELEN];
|
300
|
2453 char_u *afflist;
|
|
2454 char_u *dw;
|
236
|
2455 char_u *pc;
|
|
2456 char_u *w;
|
|
2457 int l;
|
|
2458 hash_T hash;
|
|
2459 hashitem_T *hi;
|
|
2460 FILE *fd;
|
|
2461 int lnum = 1;
|
300
|
2462 int non_ascii = 0;
|
|
2463 int retval = OK;
|
|
2464 char_u message[MAXLINELEN + MAXWLEN];
|
307
|
2465 int flags;
|
236
|
2466
|
300
|
2467 /*
|
|
2468 * Open the file.
|
|
2469 */
|
310
|
2470 fd = mch_fopen((char *)fname, "r");
|
236
|
2471 if (fd == NULL)
|
|
2472 {
|
|
2473 EMSG2(_(e_notopen), fname);
|
|
2474 return FAIL;
|
|
2475 }
|
|
2476
|
300
|
2477 /* The hashtable is only used to detect duplicated words. */
|
|
2478 hash_init(&ht);
|
|
2479
|
310
|
2480 if (spin->si_verbose || p_verbose > 2)
|
|
2481 {
|
|
2482 if (!spin->si_verbose)
|
|
2483 verbose_enter();
|
|
2484 smsg((char_u *)_("Reading dictionary file %s..."), fname);
|
|
2485 out_flush();
|
|
2486 if (!spin->si_verbose)
|
|
2487 verbose_leave();
|
|
2488 }
|
236
|
2489
|
|
2490 /* Read and ignore the first line: word count. */
|
|
2491 (void)vim_fgets(line, MAXLINELEN, fd);
|
324
|
2492 if (!vim_isdigit(*skipwhite(line)))
|
236
|
2493 EMSG2(_("E760: No word count in %s"), fname);
|
|
2494
|
|
2495 /*
|
|
2496 * Read all the lines in the file one by one.
|
|
2497 * The words are converted to 'encoding' here, before being added to
|
|
2498 * the hashtable.
|
|
2499 */
|
255
|
2500 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
|
236
|
2501 {
|
255
|
2502 line_breakcheck();
|
236
|
2503 ++lnum;
|
|
2504
|
300
|
2505 /* Remove CR, LF and white space from the end. White space halfway
|
|
2506 * the word is kept to allow e.g., "et al.". */
|
236
|
2507 l = STRLEN(line);
|
|
2508 while (l > 0 && line[l - 1] <= ' ')
|
|
2509 --l;
|
|
2510 if (l == 0)
|
|
2511 continue; /* empty line */
|
|
2512 line[l] = NUL;
|
|
2513
|
300
|
2514 /* This takes time, print a message now and then. */
|
310
|
2515 if (spin->si_verbose && (lnum & 0x3ff) == 0)
|
300
|
2516 {
|
|
2517 vim_snprintf((char *)message, sizeof(message),
|
|
2518 _("line %6d - %s"), lnum, line);
|
|
2519 msg_start();
|
|
2520 msg_outtrans_attr(message, 0);
|
|
2521 msg_clr_eos();
|
|
2522 msg_didout = FALSE;
|
|
2523 msg_col = 0;
|
|
2524 out_flush();
|
|
2525 }
|
|
2526
|
236
|
2527 /* Find the optional affix names. */
|
300
|
2528 afflist = vim_strchr(line, '/');
|
|
2529 if (afflist != NULL)
|
|
2530 *afflist++ = NUL;
|
236
|
2531
|
300
|
2532 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
2533 if (spin->si_ascii && has_non_ascii(line))
|
|
2534 {
|
|
2535 ++non_ascii;
|
240
|
2536 continue;
|
300
|
2537 }
|
240
|
2538
|
310
|
2539 #ifdef FEAT_MBYTE
|
236
|
2540 /* Convert from "SET" to 'encoding' when needed. */
|
300
|
2541 if (spin->si_conv.vc_type != CONV_NONE)
|
236
|
2542 {
|
300
|
2543 pc = string_convert(&spin->si_conv, line, NULL);
|
255
|
2544 if (pc == NULL)
|
|
2545 {
|
|
2546 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
2547 fname, lnum, line);
|
|
2548 continue;
|
|
2549 }
|
236
|
2550 w = pc;
|
|
2551 }
|
|
2552 else
|
310
|
2553 #endif
|
236
|
2554 {
|
|
2555 pc = NULL;
|
|
2556 w = line;
|
|
2557 }
|
|
2558
|
300
|
2559 /* Store the word in the hashtable to be able to find duplicates. */
|
|
2560 dw = (char_u *)getroom_save(&spin->si_blocks, w);
|
236
|
2561 if (dw == NULL)
|
300
|
2562 retval = FAIL;
|
|
2563 vim_free(pc);
|
|
2564 if (retval == FAIL)
|
236
|
2565 break;
|
|
2566
|
300
|
2567 hash = hash_hash(dw);
|
|
2568 hi = hash_lookup(&ht, dw, hash);
|
236
|
2569 if (!HASHITEM_EMPTY(hi))
|
|
2570 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
|
300
|
2571 fname, lnum, line);
|
236
|
2572 else
|
300
|
2573 hash_add_item(&ht, hi, dw, hash);
|
|
2574
|
307
|
2575 flags = 0;
|
|
2576 if (afflist != NULL)
|
|
2577 {
|
|
2578 /* Check for affix name that stands for keep-case word and stands
|
|
2579 * for rare word (if defined). */
|
310
|
2580 if (affile->af_kep != NUL
|
|
2581 && vim_strchr(afflist, affile->af_kep) != NULL)
|
307
|
2582 flags |= WF_KEEPCAP;
|
|
2583 if (affile->af_rar != NUL
|
|
2584 && vim_strchr(afflist, affile->af_rar) != NULL)
|
|
2585 flags |= WF_RARE;
|
|
2586 }
|
|
2587
|
300
|
2588 /* Add the word to the word tree(s). */
|
316
|
2589 if (store_word(dw, spin, flags, spin->si_region) == FAIL)
|
300
|
2590 retval = FAIL;
|
236
|
2591
|
300
|
2592 if (afflist != NULL)
|
|
2593 {
|
|
2594 /* Find all matching suffixes and add the resulting words.
|
|
2595 * Additionally do matching prefixes that combine. */
|
|
2596 if (store_aff_word(dw, spin, afflist,
|
307
|
2597 &affile->af_suff, &affile->af_pref,
|
|
2598 FALSE, flags) == FAIL)
|
300
|
2599 retval = FAIL;
|
|
2600
|
|
2601 /* Find all matching prefixes and add the resulting words. */
|
|
2602 if (store_aff_word(dw, spin, afflist,
|
307
|
2603 &affile->af_pref, NULL, FALSE, flags) == FAIL)
|
300
|
2604 retval = FAIL;
|
|
2605 }
|
236
|
2606 }
|
|
2607
|
300
|
2608 if (spin->si_ascii && non_ascii > 0)
|
|
2609 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
2610 non_ascii);
|
|
2611 hash_clear(&ht);
|
|
2612
|
236
|
2613 fclose(fd);
|
300
|
2614 return retval;
|
236
|
2615 }
|
|
2616
|
|
2617 /*
|
300
|
2618 * Apply affixes to a word and store the resulting words.
|
|
2619 * "ht" is the hashtable with affentry_T that need to be applied, either
|
|
2620 * prefixes or suffixes.
|
|
2621 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
|
|
2622 * the resulting words for combining affixes.
|
|
2623 *
|
|
2624 * Returns FAIL when out of memory.
|
236
|
2625 */
|
300
|
2626 static int
|
307
|
2627 store_aff_word(word, spin, afflist, ht, xht, comb, flags)
|
300
|
2628 char_u *word; /* basic word start */
|
|
2629 spellinfo_T *spin; /* spell info */
|
|
2630 char_u *afflist; /* list of names of supported affixes */
|
|
2631 hashtab_T *ht;
|
|
2632 hashtab_T *xht;
|
|
2633 int comb; /* only use affixes that combine */
|
307
|
2634 int flags; /* flags for the word */
|
236
|
2635 {
|
|
2636 int todo;
|
|
2637 hashitem_T *hi;
|
300
|
2638 affheader_T *ah;
|
|
2639 affentry_T *ae;
|
|
2640 regmatch_T regmatch;
|
|
2641 char_u newword[MAXWLEN];
|
|
2642 int retval = OK;
|
|
2643 int i;
|
|
2644 char_u *p;
|
236
|
2645
|
300
|
2646 todo = ht->ht_used;
|
|
2647 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
|
236
|
2648 {
|
|
2649 if (!HASHITEM_EMPTY(hi))
|
|
2650 {
|
|
2651 --todo;
|
300
|
2652 ah = HI2AH(hi);
|
236
|
2653
|
300
|
2654 /* Check that the affix combines, if required, and that the word
|
|
2655 * supports this affix. */
|
|
2656 if ((!comb || ah->ah_combine)
|
|
2657 && vim_strchr(afflist, *ah->ah_key) != NULL)
|
236
|
2658 {
|
300
|
2659 /* Loop over all affix entries with this name. */
|
|
2660 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
|
236
|
2661 {
|
300
|
2662 /* Check the condition. It's not logical to match case
|
|
2663 * here, but it is required for compatibility with
|
|
2664 * Myspell. */
|
|
2665 regmatch.regprog = ae->ae_prog;
|
|
2666 regmatch.rm_ic = FALSE;
|
|
2667 if (ae->ae_prog == NULL
|
|
2668 || vim_regexec(®match, word, (colnr_T)0))
|
|
2669 {
|
|
2670 /* Match. Remove the chop and add the affix. */
|
|
2671 if (xht == NULL)
|
240
|
2672 {
|
300
|
2673 /* prefix: chop/add at the start of the word */
|
|
2674 if (ae->ae_add == NULL)
|
|
2675 *newword = NUL;
|
|
2676 else
|
|
2677 STRCPY(newword, ae->ae_add);
|
|
2678 p = word;
|
|
2679 if (ae->ae_chop != NULL)
|
310
|
2680 {
|
300
|
2681 /* Skip chop string. */
|
310
|
2682 #ifdef FEAT_MBYTE
|
|
2683 if (has_mbyte)
|
324
|
2684 {
|
310
|
2685 i = mb_charlen(ae->ae_chop);
|
324
|
2686 for ( ; i > 0; --i)
|
|
2687 mb_ptr_adv(p);
|
|
2688 }
|
310
|
2689 else
|
|
2690 #endif
|
324
|
2691 p += STRLEN(ae->ae_chop);
|
310
|
2692 }
|
300
|
2693 STRCAT(newword, p);
|
|
2694 }
|
|
2695 else
|
|
2696 {
|
|
2697 /* suffix: chop/add at the end of the word */
|
|
2698 STRCPY(newword, word);
|
|
2699 if (ae->ae_chop != NULL)
|
|
2700 {
|
|
2701 /* Remove chop string. */
|
|
2702 p = newword + STRLEN(newword);
|
310
|
2703 #ifdef FEAT_MBYTE
|
|
2704 if (has_mbyte)
|
|
2705 i = mb_charlen(ae->ae_chop);
|
|
2706 else
|
|
2707 #endif
|
|
2708 i = STRLEN(ae->ae_chop);
|
|
2709 for ( ; i > 0; --i)
|
300
|
2710 mb_ptr_back(newword, p);
|
|
2711 *p = NUL;
|
|
2712 }
|
|
2713 if (ae->ae_add != NULL)
|
|
2714 STRCAT(newword, ae->ae_add);
|
240
|
2715 }
|
|
2716
|
300
|
2717 /* Store the modified word. */
|
316
|
2718 if (store_word(newword, spin,
|
|
2719 flags, spin->si_region) == FAIL)
|
300
|
2720 retval = FAIL;
|
236
|
2721
|
300
|
2722 /* When added a suffix and combining is allowed also
|
|
2723 * try adding prefixes additionally. */
|
|
2724 if (xht != NULL && ah->ah_combine)
|
|
2725 if (store_aff_word(newword, spin, afflist,
|
307
|
2726 xht, NULL, TRUE, flags) == FAIL)
|
300
|
2727 retval = FAIL;
|
236
|
2728 }
|
|
2729 }
|
|
2730 }
|
|
2731 }
|
|
2732 }
|
|
2733
|
|
2734 return retval;
|
|
2735 }
|
|
2736
|
|
2737 /*
|
300
|
2738 * Read a file with a list of words.
|
236
|
2739 */
|
|
2740 static int
|
300
|
2741 spell_read_wordfile(fname, spin)
|
|
2742 char_u *fname;
|
|
2743 spellinfo_T *spin;
|
236
|
2744 {
|
300
|
2745 FILE *fd;
|
|
2746 long lnum = 0;
|
|
2747 char_u rline[MAXLINELEN];
|
|
2748 char_u *line;
|
|
2749 char_u *pc = NULL;
|
|
2750 int l;
|
|
2751 int retval = OK;
|
|
2752 int did_word = FALSE;
|
|
2753 int non_ascii = 0;
|
307
|
2754 int flags;
|
316
|
2755 int regionmask;
|
236
|
2756
|
300
|
2757 /*
|
|
2758 * Open the file.
|
|
2759 */
|
310
|
2760 fd = mch_fopen((char *)fname, "r");
|
300
|
2761 if (fd == NULL)
|
236
|
2762 {
|
300
|
2763 EMSG2(_(e_notopen), fname);
|
|
2764 return FAIL;
|
236
|
2765 }
|
|
2766
|
310
|
2767 if (spin->si_verbose || p_verbose > 2)
|
|
2768 {
|
|
2769 if (!spin->si_verbose)
|
|
2770 verbose_enter();
|
|
2771 smsg((char_u *)_("Reading word file %s..."), fname);
|
|
2772 out_flush();
|
|
2773 if (!spin->si_verbose)
|
|
2774 verbose_leave();
|
|
2775 }
|
300
|
2776
|
|
2777 /*
|
|
2778 * Read all the lines in the file one by one.
|
|
2779 */
|
|
2780 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
|
|
2781 {
|
|
2782 line_breakcheck();
|
|
2783 ++lnum;
|
|
2784
|
|
2785 /* Skip comment lines. */
|
|
2786 if (*rline == '#')
|
|
2787 continue;
|
|
2788
|
|
2789 /* Remove CR, LF and white space from the end. */
|
|
2790 l = STRLEN(rline);
|
|
2791 while (l > 0 && rline[l - 1] <= ' ')
|
|
2792 --l;
|
|
2793 if (l == 0)
|
|
2794 continue; /* empty or blank line */
|
|
2795 rline[l] = NUL;
|
|
2796
|
|
2797 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
|
|
2798 vim_free(pc);
|
310
|
2799 #ifdef FEAT_MBYTE
|
300
|
2800 if (spin->si_conv.vc_type != CONV_NONE)
|
|
2801 {
|
|
2802 pc = string_convert(&spin->si_conv, rline, NULL);
|
|
2803 if (pc == NULL)
|
|
2804 {
|
|
2805 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
|
|
2806 fname, lnum, rline);
|
|
2807 continue;
|
|
2808 }
|
|
2809 line = pc;
|
|
2810 }
|
|
2811 else
|
310
|
2812 #endif
|
300
|
2813 {
|
|
2814 pc = NULL;
|
|
2815 line = rline;
|
|
2816 }
|
|
2817
|
307
|
2818 flags = 0;
|
316
|
2819 regionmask = spin->si_region;
|
307
|
2820
|
|
2821 if (*line == '/')
|
300
|
2822 {
|
307
|
2823 ++line;
|
316
|
2824
|
307
|
2825 if (STRNCMP(line, "encoding=", 9) == 0)
|
300
|
2826 {
|
|
2827 if (spin->si_conv.vc_type != CONV_NONE)
|
316
|
2828 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
|
|
2829 fname, lnum, line - 1);
|
300
|
2830 else if (did_word)
|
316
|
2831 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
|
|
2832 fname, lnum, line - 1);
|
300
|
2833 else
|
|
2834 {
|
310
|
2835 #ifdef FEAT_MBYTE
|
|
2836 char_u *enc;
|
|
2837
|
300
|
2838 /* Setup for conversion to 'encoding'. */
|
316
|
2839 line += 10;
|
|
2840 enc = enc_canonize(line);
|
300
|
2841 if (enc != NULL && !spin->si_ascii
|
|
2842 && convert_setup(&spin->si_conv, enc,
|
|
2843 p_enc) == FAIL)
|
|
2844 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
|
316
|
2845 fname, line, p_enc);
|
300
|
2846 vim_free(enc);
|
310
|
2847 #else
|
|
2848 smsg((char_u *)_("Conversion in %s not supported"), fname);
|
|
2849 #endif
|
300
|
2850 }
|
307
|
2851 continue;
|
300
|
2852 }
|
307
|
2853
|
316
|
2854 if (STRNCMP(line, "regions=", 8) == 0)
|
|
2855 {
|
|
2856 if (spin->si_region_count > 1)
|
|
2857 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
|
|
2858 fname, lnum, line);
|
|
2859 else
|
|
2860 {
|
|
2861 line += 8;
|
|
2862 if (STRLEN(line) > 16)
|
|
2863 smsg((char_u *)_("Too many regions in %s line %d: %s"),
|
|
2864 fname, lnum, line);
|
|
2865 else
|
|
2866 {
|
|
2867 spin->si_region_count = STRLEN(line) / 2;
|
|
2868 STRCPY(spin->si_region_name, line);
|
|
2869 }
|
|
2870 }
|
|
2871 continue;
|
|
2872 }
|
|
2873
|
307
|
2874 if (*line == '=')
|
|
2875 {
|
|
2876 /* keep-case word */
|
|
2877 flags |= WF_KEEPCAP;
|
|
2878 ++line;
|
|
2879 }
|
|
2880
|
|
2881 if (*line == '!')
|
|
2882 {
|
|
2883 /* Bad, bad, wicked word. */
|
|
2884 flags |= WF_BANNED;
|
|
2885 ++line;
|
|
2886 }
|
|
2887 else if (*line == '?')
|
|
2888 {
|
|
2889 /* Rare word. */
|
|
2890 flags |= WF_RARE;
|
|
2891 ++line;
|
|
2892 }
|
|
2893
|
316
|
2894 if (VIM_ISDIGIT(*line))
|
|
2895 {
|
|
2896 /* region number(s) */
|
|
2897 regionmask = 0;
|
|
2898 while (VIM_ISDIGIT(*line))
|
|
2899 {
|
|
2900 l = *line - '0';
|
|
2901 if (l > spin->si_region_count)
|
|
2902 {
|
|
2903 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
|
|
2904 fname, lnum, line);
|
|
2905 break;
|
|
2906 }
|
|
2907 regionmask |= 1 << (l - 1);
|
|
2908 ++line;
|
|
2909 }
|
|
2910 flags |= WF_REGION;
|
|
2911 }
|
|
2912
|
307
|
2913 if (flags == 0)
|
|
2914 {
|
|
2915 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
|
300
|
2916 fname, lnum, line);
|
307
|
2917 continue;
|
|
2918 }
|
300
|
2919 }
|
|
2920
|
|
2921 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
|
|
2922 if (spin->si_ascii && has_non_ascii(line))
|
|
2923 {
|
|
2924 ++non_ascii;
|
|
2925 continue;
|
|
2926 }
|
|
2927
|
|
2928 /* Normal word: store it. */
|
316
|
2929 if (store_word(line, spin, flags, regionmask) == FAIL)
|
300
|
2930 {
|
|
2931 retval = FAIL;
|
|
2932 break;
|
|
2933 }
|
|
2934 did_word = TRUE;
|
|
2935 }
|
|
2936
|
|
2937 vim_free(pc);
|
|
2938 fclose(fd);
|
|
2939
|
310
|
2940 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
|
|
2941 {
|
|
2942 if (p_verbose > 2)
|
|
2943 verbose_enter();
|
300
|
2944 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
|
|
2945 non_ascii);
|
310
|
2946 if (p_verbose > 2)
|
|
2947 verbose_leave();
|
|
2948 }
|
300
|
2949 return retval;
|
236
|
2950 }
|
|
2951
|
|
2952 /*
|
300
|
2953 * Get part of an sblock_T, "len" bytes long.
|
|
2954 * This avoids calling free() for every little struct we use.
|
|
2955 * The memory is cleared to all zeros.
|
|
2956 * Returns NULL when out of memory.
|
|
2957 */
|
|
2958 static void *
|
|
2959 getroom(blp, len)
|
|
2960 sblock_T **blp;
|
|
2961 size_t len; /* length needed */
|
|
2962 {
|
|
2963 char_u *p;
|
|
2964 sblock_T *bl = *blp;
|
|
2965
|
|
2966 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
|
|
2967 {
|
|
2968 /* Allocate a block of memory. This is not freed until much later. */
|
|
2969 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
|
|
2970 if (bl == NULL)
|
|
2971 return NULL;
|
|
2972 bl->sb_next = *blp;
|
|
2973 *blp = bl;
|
|
2974 bl->sb_used = 0;
|
|
2975 }
|
|
2976
|
|
2977 p = bl->sb_data + bl->sb_used;
|
|
2978 bl->sb_used += len;
|
|
2979
|
|
2980 return p;
|
|
2981 }
|
|
2982
|
|
2983 /*
|
|
2984 * Make a copy of a string into memory allocated with getroom().
|
|
2985 */
|
|
2986 static char_u *
|
|
2987 getroom_save(blp, s)
|
|
2988 sblock_T **blp;
|
|
2989 char_u *s;
|
|
2990 {
|
|
2991 char_u *sc;
|
|
2992
|
|
2993 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
|
|
2994 if (sc != NULL)
|
|
2995 STRCPY(sc, s);
|
|
2996 return sc;
|
|
2997 }
|
|
2998
|
|
2999
|
|
3000 /*
|
|
3001 * Free the list of allocated sblock_T.
|
236
|
3002 */
|
|
3003 static void
|
300
|
3004 free_blocks(bl)
|
|
3005 sblock_T *bl;
|
236
|
3006 {
|
300
|
3007 sblock_T *next;
|
236
|
3008
|
300
|
3009 while (bl != NULL)
|
236
|
3010 {
|
300
|
3011 next = bl->sb_next;
|
|
3012 vim_free(bl);
|
|
3013 bl = next;
|
236
|
3014 }
|
|
3015 }
|
|
3016
|
|
3017 /*
|
300
|
3018 * Allocate the root of a word tree.
|
236
|
3019 */
|
300
|
3020 static wordnode_T *
|
|
3021 wordtree_alloc(blp)
|
|
3022 sblock_T **blp;
|
236
|
3023 {
|
300
|
3024 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
236
|
3025 }
|
|
3026
|
|
3027 /*
|
300
|
3028 * Store a word in the tree(s).
|
307
|
3029 * Always store it in the case-folded tree. A keep-case word can also be used
|
|
3030 * with all caps.
|
300
|
3031 * For a keep-case word also store it in the keep-case tree.
|
236
|
3032 */
|
|
3033 static int
|
316
|
3034 store_word(word, spin, flags, region)
|
300
|
3035 char_u *word;
|
|
3036 spellinfo_T *spin;
|
307
|
3037 int flags; /* extra flags, WF_BANNED */
|
316
|
3038 int region; /* supported region(s) */
|
236
|
3039 {
|
300
|
3040 int len = STRLEN(word);
|
|
3041 int ct = captype(word, word + len);
|
|
3042 char_u foldword[MAXWLEN];
|
|
3043 int res;
|
236
|
3044
|
323
|
3045 (void)spell_casefold(word, len, foldword, MAXWLEN);
|
|
3046 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
|
|
3047 region, &spin->si_blocks);
|
307
|
3048
|
|
3049 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
|
|
3050 res = tree_add_word(word, spin->si_keeproot, flags,
|
316
|
3051 region, &spin->si_blocks);
|
300
|
3052 return res;
|
236
|
3053 }
|
|
3054
|
|
3055 /*
|
300
|
3056 * Add word "word" to a word tree at "root".
|
255
|
3057 * Returns FAIL when out of memory.
|
236
|
3058 */
|
255
|
3059 static int
|
300
|
3060 tree_add_word(word, root, flags, region, blp)
|
|
3061 char_u *word;
|
|
3062 wordnode_T *root;
|
|
3063 int flags;
|
|
3064 int region;
|
|
3065 sblock_T **blp;
|
236
|
3066 {
|
300
|
3067 wordnode_T *node = root;
|
|
3068 wordnode_T *np;
|
|
3069 wordnode_T **prev = NULL;
|
|
3070 int i;
|
255
|
3071
|
300
|
3072 /* Add each byte of the word to the tree, including the NUL at the end. */
|
|
3073 for (i = 0; ; ++i)
|
255
|
3074 {
|
300
|
3075 /* Look for the sibling that has the same character. They are sorted
|
|
3076 * on byte value, thus stop searching when a sibling is found with a
|
|
3077 * higher byte value. For zero bytes (end of word) check that the
|
|
3078 * flags are equal, there is a separate zero byte for each flag value.
|
|
3079 */
|
|
3080 while (node != NULL && (node->wn_byte < word[i]
|
307
|
3081 || (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
|
236
|
3082 {
|
300
|
3083 prev = &node->wn_sibling;
|
|
3084 node = *prev;
|
236
|
3085 }
|
300
|
3086 if (node == NULL || node->wn_byte != word[i])
|
255
|
3087 {
|
300
|
3088 /* Allocate a new node. */
|
|
3089 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
|
|
3090 if (np == NULL)
|
|
3091 return FAIL;
|
|
3092 np->wn_byte = word[i];
|
|
3093 *prev = np;
|
|
3094 np->wn_sibling = node;
|
|
3095 node = np;
|
255
|
3096 }
|
300
|
3097
|
|
3098 if (word[i] == NUL)
|
|
3099 {
|
|
3100 node->wn_flags = flags;
|
|
3101 node->wn_region |= region;
|
|
3102 break;
|
|
3103 }
|
|
3104 prev = &node->wn_child;
|
|
3105 node = *prev;
|
255
|
3106 }
|
|
3107
|
|
3108 return OK;
|
236
|
3109 }
|
|
3110
|
|
3111 /*
|
300
|
3112 * Compress a tree: find tails that are identical and can be shared.
|
|
3113 */
|
|
3114 static void
|
310
|
3115 wordtree_compress(root, spin)
|
300
|
3116 wordnode_T *root;
|
310
|
3117 spellinfo_T *spin;
|
300
|
3118 {
|
|
3119 hashtab_T ht;
|
|
3120 int n;
|
|
3121 int tot = 0;
|
|
3122
|
|
3123 if (root != NULL)
|
|
3124 {
|
|
3125 hash_init(&ht);
|
|
3126 n = node_compress(root, &ht, &tot);
|
310
|
3127 if (spin->si_verbose || p_verbose > 2)
|
|
3128 {
|
|
3129 if (!spin->si_verbose)
|
|
3130 verbose_enter();
|
|
3131 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
|
300
|
3132 n, tot, (tot - n) * 100 / tot);
|
310
|
3133 if (p_verbose > 2)
|
|
3134 verbose_leave();
|
|
3135 }
|
300
|
3136 hash_clear(&ht);
|
|
3137 }
|
|
3138 }
|
|
3139
|
|
3140 /*
|
|
3141 * Compress a node, its siblings and its children, depth first.
|
|
3142 * Returns the number of compressed nodes.
|
236
|
3143 */
|
255
|
3144 static int
|
300
|
3145 node_compress(node, ht, tot)
|
|
3146 wordnode_T *node;
|
|
3147 hashtab_T *ht;
|
|
3148 int *tot; /* total count of nodes before compressing,
|
|
3149 incremented while going through the tree */
|
236
|
3150 {
|
300
|
3151 wordnode_T *np;
|
|
3152 wordnode_T *tp;
|
|
3153 wordnode_T *child;
|
|
3154 hash_T hash;
|
236
|
3155 hashitem_T *hi;
|
300
|
3156 int len = 0;
|
|
3157 unsigned nr, n;
|
|
3158 int compressed = 0;
|
236
|
3159
|
300
|
3160 /*
|
|
3161 * Go through the list of siblings. Compress each child and then try
|
|
3162 * finding an identical child to replace it.
|
|
3163 * Note that with "child" we mean not just the node that is pointed to,
|
|
3164 * but the whole list of siblings, of which the node is the first.
|
|
3165 */
|
|
3166 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
3167 {
|
300
|
3168 ++len;
|
|
3169 if ((child = np->wn_child) != NULL)
|
|
3170 {
|
|
3171 /* Compress the child. This fills wn_hashkey. */
|
|
3172 compressed += node_compress(child, ht, tot);
|
|
3173
|
|
3174 /* Try to find an identical child. */
|
|
3175 hash = hash_hash(child->wn_hashkey);
|
|
3176 hi = hash_lookup(ht, child->wn_hashkey, hash);
|
|
3177 tp = NULL;
|
|
3178 if (!HASHITEM_EMPTY(hi))
|
|
3179 {
|
|
3180 /* There are children with an identical hash value. Now check
|
|
3181 * if there is one that is really identical. */
|
|
3182 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
|
|
3183 if (node_equal(child, tp))
|
|
3184 {
|
|
3185 /* Found one! Now use that child in place of the
|
|
3186 * current one. This means the current child is
|
|
3187 * dropped from the tree. */
|
|
3188 np->wn_child = tp;
|
|
3189 ++compressed;
|
|
3190 break;
|
|
3191 }
|
|
3192 if (tp == NULL)
|
|
3193 {
|
|
3194 /* No other child with this hash value equals the child of
|
|
3195 * the node, add it to the linked list after the first
|
|
3196 * item. */
|
|
3197 tp = HI2WN(hi);
|
|
3198 child->wn_next = tp->wn_next;
|
|
3199 tp->wn_next = child;
|
|
3200 }
|
|
3201 }
|
|
3202 else
|
|
3203 /* No other child has this hash value, add it to the
|
|
3204 * hashtable. */
|
|
3205 hash_add_item(ht, hi, child->wn_hashkey, hash);
|
|
3206 }
|
236
|
3207 }
|
300
|
3208 *tot += len;
|
|
3209
|
|
3210 /*
|
|
3211 * Make a hash key for the node and its siblings, so that we can quickly
|
|
3212 * find a lookalike node. This must be done after compressing the sibling
|
|
3213 * list, otherwise the hash key would become invalid by the compression.
|
|
3214 */
|
|
3215 node->wn_hashkey[0] = len;
|
|
3216 nr = 0;
|
|
3217 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
3218 {
|
300
|
3219 if (np->wn_byte == NUL)
|
|
3220 /* end node: only use wn_flags and wn_region */
|
|
3221 n = np->wn_flags + (np->wn_region << 8);
|
|
3222 else
|
|
3223 /* byte node: use the byte value and the child pointer */
|
|
3224 n = np->wn_byte + ((long_u)np->wn_child << 8);
|
|
3225 nr = nr * 101 + n;
|
236
|
3226 }
|
300
|
3227
|
|
3228 /* Avoid NUL bytes, it terminates the hash key. */
|
|
3229 n = nr & 0xff;
|
|
3230 node->wn_hashkey[1] = n == 0 ? 1 : n;
|
|
3231 n = (nr >> 8) & 0xff;
|
|
3232 node->wn_hashkey[2] = n == 0 ? 1 : n;
|
|
3233 n = (nr >> 16) & 0xff;
|
|
3234 node->wn_hashkey[3] = n == 0 ? 1 : n;
|
|
3235 n = (nr >> 24) & 0xff;
|
|
3236 node->wn_hashkey[4] = n == 0 ? 1 : n;
|
|
3237 node->wn_hashkey[5] = NUL;
|
|
3238
|
|
3239 return compressed;
|
|
3240 }
|
|
3241
|
|
3242 /*
|
|
3243 * Return TRUE when two nodes have identical siblings and children.
|
|
3244 */
|
|
3245 static int
|
|
3246 node_equal(n1, n2)
|
|
3247 wordnode_T *n1;
|
|
3248 wordnode_T *n2;
|
|
3249 {
|
|
3250 wordnode_T *p1;
|
|
3251 wordnode_T *p2;
|
|
3252
|
|
3253 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
|
|
3254 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
|
|
3255 if (p1->wn_byte != p2->wn_byte
|
|
3256 || (p1->wn_byte == NUL
|
|
3257 ? (p1->wn_flags != p2->wn_flags
|
|
3258 || p1->wn_region != p2->wn_region)
|
|
3259 : (p1->wn_child != p2->wn_child)))
|
|
3260 break;
|
|
3261
|
|
3262 return p1 == NULL && p2 == NULL;
|
236
|
3263 }
|
|
3264
|
|
3265 /*
|
|
3266 * Write a number to file "fd", MSB first, in "len" bytes.
|
|
3267 */
|
255
|
3268 void
|
236
|
3269 put_bytes(fd, nr, len)
|
|
3270 FILE *fd;
|
|
3271 long_u nr;
|
|
3272 int len;
|
|
3273 {
|
|
3274 int i;
|
|
3275
|
|
3276 for (i = len - 1; i >= 0; --i)
|
|
3277 putc((int)(nr >> (i * 8)), fd);
|
|
3278 }
|
|
3279
|
323
|
3280 static int
|
|
3281 #ifdef __BORLANDC__
|
|
3282 _RTLENTRYF
|
|
3283 #endif
|
|
3284 rep_compare __ARGS((const void *s1, const void *s2));
|
|
3285
|
|
3286 /*
|
|
3287 * Function given to qsort() to sort the REP items on "from" string.
|
|
3288 */
|
|
3289 static int
|
|
3290 #ifdef __BORLANDC__
|
|
3291 _RTLENTRYF
|
|
3292 #endif
|
|
3293 rep_compare(s1, s2)
|
|
3294 const void *s1;
|
|
3295 const void *s2;
|
|
3296 {
|
|
3297 fromto_T *p1 = (fromto_T *)s1;
|
|
3298 fromto_T *p2 = (fromto_T *)s2;
|
|
3299
|
|
3300 return STRCMP(p1->ft_from, p2->ft_from);
|
|
3301 }
|
|
3302
|
236
|
3303 /*
|
|
3304 * Write the Vim spell file "fname".
|
|
3305 */
|
|
3306 static void
|
316
|
3307 write_vim_spell(fname, spin)
|
236
|
3308 char_u *fname;
|
300
|
3309 spellinfo_T *spin;
|
236
|
3310 {
|
300
|
3311 FILE *fd;
|
|
3312 int regionmask;
|
236
|
3313 int round;
|
300
|
3314 wordnode_T *tree;
|
|
3315 int nodecount;
|
323
|
3316 int i;
|
|
3317 int l;
|
|
3318 garray_T *gap;
|
|
3319 fromto_T *ftp;
|
|
3320 char_u *p;
|
|
3321 int rr;
|
236
|
3322
|
310
|
3323 fd = mch_fopen((char *)fname, "w");
|
300
|
3324 if (fd == NULL)
|
236
|
3325 {
|
|
3326 EMSG2(_(e_notopen), fname);
|
|
3327 return;
|
|
3328 }
|
|
3329
|
255
|
3330 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
|
|
3331 * <charflagslen> <charflags> <fcharslen> <fchars> */
|
300
|
3332
|
|
3333 /* <fileID> */
|
|
3334 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
|
|
3335 EMSG(_(e_write));
|
236
|
3336
|
|
3337 /* write the region names if there is more than one */
|
316
|
3338 if (spin->si_region_count > 1)
|
236
|
3339 {
|
316
|
3340 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
|
|
3341 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
|
|
3342 (size_t)1, fd);
|
|
3343 regionmask = (1 << spin->si_region_count) - 1;
|
236
|
3344 }
|
|
3345 else
|
|
3346 {
|
300
|
3347 putc(0, fd);
|
|
3348 regionmask = 0;
|
236
|
3349 }
|
|
3350
|
323
|
3351 /*
|
|
3352 * Write the table with character flags and table for case folding.
|
260
|
3353 * <charflagslen> <charflags> <fcharlen> <fchars>
|
|
3354 * Skip this for ASCII, the table may conflict with the one used for
|
323
|
3355 * 'encoding'.
|
|
3356 * Also skip this for an .add.spl file, the main spell file must contain
|
|
3357 * the table (avoids that it conflicts). File is shorter too.
|
|
3358 */
|
|
3359 if (spin->si_ascii || spin->si_add)
|
260
|
3360 {
|
300
|
3361 putc(0, fd);
|
|
3362 putc(0, fd);
|
|
3363 putc(0, fd);
|
260
|
3364 }
|
|
3365 else
|
300
|
3366 write_spell_chartab(fd);
|
255
|
3367
|
323
|
3368 /* Sort the REP items. */
|
|
3369 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
|
|
3370 sizeof(fromto_T), rep_compare);
|
|
3371
|
|
3372 /* <SUGGEST> : <repcount> <rep> ...
|
|
3373 * <salflags> <salcount> <sal> ...
|
|
3374 * <maplen> <mapstr> */
|
|
3375 for (round = 1; round <= 2; ++round)
|
|
3376 {
|
|
3377 if (round == 1)
|
|
3378 gap = &spin->si_rep;
|
|
3379 else
|
|
3380 {
|
|
3381 gap = &spin->si_sal;
|
|
3382
|
|
3383 i = 0;
|
|
3384 if (spin->si_followup)
|
|
3385 i |= SAL_F0LLOWUP;
|
|
3386 if (spin->si_collapse)
|
|
3387 i |= SAL_COLLAPSE;
|
|
3388 if (spin->si_rem_accents)
|
|
3389 i |= SAL_REM_ACCENTS;
|
|
3390 putc(i, fd); /* <salflags> */
|
|
3391 }
|
|
3392
|
|
3393 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
|
|
3394 for (i = 0; i < gap->ga_len; ++i)
|
|
3395 {
|
|
3396 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
|
|
3397 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
|
|
3398 ftp = &((fromto_T *)gap->ga_data)[i];
|
|
3399 for (rr = 1; rr <= 2; ++rr)
|
|
3400 {
|
|
3401 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
|
|
3402 l = STRLEN(p);
|
|
3403 putc(l, fd);
|
|
3404 fwrite(p, l, (size_t)1, fd);
|
|
3405 }
|
|
3406 }
|
|
3407 }
|
|
3408
|
|
3409 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
|
|
3410 if (spin->si_map.ga_len > 0) /* <mapstr> */
|
|
3411 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
|
|
3412 (size_t)1, fd);
|
302
|
3413
|
236
|
3414 /*
|
300
|
3415 * <LWORDTREE> <KWORDTREE>
|
236
|
3416 */
|
323
|
3417 spin->si_memtot = 0;
|
300
|
3418 for (round = 1; round <= 2; ++round)
|
236
|
3419 {
|
300
|
3420 tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
|
236
|
3421
|
300
|
3422 /* Count the number of nodes. Needed to be able to allocate the
|
|
3423 * memory when reading the nodes. Also fills in the index for shared
|
|
3424 * nodes. */
|
|
3425 nodecount = put_tree(NULL, tree, 0, regionmask);
|
236
|
3426
|
300
|
3427 /* number of nodes in 4 bytes */
|
|
3428 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
|
302
|
3429 spin->si_memtot += nodecount + nodecount * sizeof(int);
|
236
|
3430
|
300
|
3431 /* Write the nodes. */
|
|
3432 (void)put_tree(fd, tree, 0, regionmask);
|
236
|
3433 }
|
|
3434
|
300
|
3435 fclose(fd);
|
236
|
3436 }
|
|
3437
|
|
3438 /*
|
300
|
3439 * Dump a word tree at node "node".
|
|
3440 *
|
|
3441 * This first writes the list of possible bytes (siblings). Then for each
|
|
3442 * byte recursively write the children.
|
|
3443 *
|
|
3444 * NOTE: The code here must match the code in read_tree(), since assumptions
|
|
3445 * are made about the indexes (so that we don't have to write them in the
|
|
3446 * file).
|
236
|
3447 *
|
300
|
3448 * Returns the number of nodes used.
|
236
|
3449 */
|
300
|
3450 static int
|
|
3451 put_tree(fd, node, index, regionmask)
|
|
3452 FILE *fd; /* NULL when only counting */
|
|
3453 wordnode_T *node;
|
|
3454 int index;
|
|
3455 int regionmask;
|
236
|
3456 {
|
300
|
3457 int newindex = index;
|
|
3458 int siblingcount = 0;
|
|
3459 wordnode_T *np;
|
236
|
3460 int flags;
|
300
|
3461
|
|
3462 /* If "node" is zero the tree is empty. */
|
|
3463 if (node == NULL)
|
|
3464 return 0;
|
|
3465
|
|
3466 /* Store the index where this node is written. */
|
|
3467 node->wn_index = index;
|
236
|
3468
|
300
|
3469 /* Count the number of siblings. */
|
|
3470 for (np = node; np != NULL; np = np->wn_sibling)
|
|
3471 ++siblingcount;
|
236
|
3472
|
300
|
3473 /* Write the sibling count. */
|
|
3474 if (fd != NULL)
|
|
3475 putc(siblingcount, fd); /* <siblingcount> */
|
236
|
3476
|
300
|
3477 /* Write each sibling byte and optionally extra info. */
|
|
3478 for (np = node; np != NULL; np = np->wn_sibling)
|
236
|
3479 {
|
300
|
3480 if (np->wn_byte == 0)
|
|
3481 {
|
|
3482 if (fd != NULL)
|
|
3483 {
|
|
3484 /* For a NUL byte (end of word) instead of the byte itself
|
|
3485 * we write the flag/region items. */
|
|
3486 flags = np->wn_flags;
|
|
3487 if (regionmask != 0 && np->wn_region != regionmask)
|
|
3488 flags |= WF_REGION;
|
|
3489 if (flags == 0)
|
|
3490 {
|
|
3491 /* word without flags or region */
|
|
3492 putc(BY_NOFLAGS, fd); /* <byte> */
|
|
3493 }
|
|
3494 else
|
|
3495 {
|
|
3496 putc(BY_FLAGS, fd); /* <byte> */
|
|
3497 putc(flags, fd); /* <flags> */
|
|
3498 if (flags & WF_REGION)
|
|
3499 putc(np->wn_region, fd); /* <regionmask> */
|
|
3500 }
|
|
3501 }
|
|
3502 }
|
|
3503 else
|
|
3504 {
|
|
3505 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
|
|
3506 {
|
|
3507 /* The child is written elsewhere, write the reference. */
|
|
3508 if (fd != NULL)
|
|
3509 {
|
|
3510 putc(BY_INDEX, fd); /* <byte> */
|
|
3511 /* <nodeidx> */
|
|
3512 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
|
|
3513 }
|
|
3514 }
|
|
3515 else if (np->wn_child->wn_wnode == NULL)
|
|
3516 /* We will write the child below and give it an index. */
|
|
3517 np->wn_child->wn_wnode = node;
|
236
|
3518
|
300
|
3519 if (fd != NULL)
|
|
3520 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
|
|
3521 {
|
|
3522 EMSG(_(e_write));
|
|
3523 return 0;
|
|
3524 }
|
|
3525 }
|
236
|
3526 }
|
|
3527
|
300
|
3528 /* Space used in the array when reading: one for each sibling and one for
|
|
3529 * the count. */
|
|
3530 newindex += siblingcount + 1;
|
249
|
3531
|
300
|
3532 /* Recursively dump the children of each sibling. */
|
|
3533 for (np = node; np != NULL; np = np->wn_sibling)
|
|
3534 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
|
|
3535 newindex = put_tree(fd, np->wn_child, newindex, regionmask);
|
249
|
3536
|
300
|
3537 return newindex;
|
236
|
3538 }
|
|
3539
|
|
3540
|
|
3541 /*
|
310
|
3542 * ":mkspell [-ascii] outfile infile ..."
|
|
3543 * ":mkspell [-ascii] addfile"
|
236
|
3544 */
|
|
3545 void
|
|
3546 ex_mkspell(eap)
|
|
3547 exarg_T *eap;
|
|
3548 {
|
|
3549 int fcount;
|
|
3550 char_u **fnames;
|
310
|
3551 char_u *arg = eap->arg;
|
|
3552 int ascii = FALSE;
|
|
3553
|
|
3554 if (STRNCMP(arg, "-ascii", 6) == 0)
|
|
3555 {
|
|
3556 ascii = TRUE;
|
|
3557 arg = skipwhite(arg + 6);
|
|
3558 }
|
|
3559
|
|
3560 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
|
|
3561 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
|
|
3562 {
|
323
|
3563 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
|
310
|
3564 FreeWild(fcount, fnames);
|
|
3565 }
|
|
3566 }
|
|
3567
|
|
3568 /*
|
|
3569 * Create a Vim spell file from one or more word lists.
|
|
3570 * "fnames[0]" is the output file name.
|
|
3571 * "fnames[fcount - 1]" is the last input file name.
|
|
3572 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
|
|
3573 * and ".spl" is appended to make the output file name.
|
|
3574 */
|
|
3575 static void
|
323
|
3576 mkspell(fcount, fnames, ascii, overwrite, added_word)
|
310
|
3577 int fcount;
|
|
3578 char_u **fnames;
|
|
3579 int ascii; /* -ascii argument given */
|
|
3580 int overwrite; /* overwrite existing output file */
|
323
|
3581 int added_word; /* invoked through "zg" */
|
310
|
3582 {
|
236
|
3583 char_u fname[MAXPATHL];
|
|
3584 char_u wfname[MAXPATHL];
|
310
|
3585 char_u **innames;
|
|
3586 int incount;
|
236
|
3587 afffile_T *(afile[8]);
|
|
3588 int i;
|
|
3589 int len;
|
|
3590 struct stat st;
|
255
|
3591 int error = FALSE;
|
300
|
3592 spellinfo_T spin;
|
|
3593
|
|
3594 vim_memset(&spin, 0, sizeof(spin));
|
323
|
3595 spin.si_verbose = !added_word;
|
310
|
3596 spin.si_ascii = ascii;
|
323
|
3597 spin.si_followup = TRUE;
|
|
3598 spin.si_rem_accents = TRUE;
|
|
3599 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
|
|
3600 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
|
|
3601 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
|
310
|
3602
|
|
3603 /* default: fnames[0] is output file, following are input files */
|
|
3604 innames = &fnames[1];
|
|
3605 incount = fcount - 1;
|
|
3606
|
|
3607 if (fcount >= 1)
|
240
|
3608 {
|
310
|
3609 len = STRLEN(fnames[0]);
|
|
3610 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
|
|
3611 {
|
|
3612 /* For ":mkspell path/en.latin1.add" output file is
|
|
3613 * "path/en.latin1.add.spl". */
|
|
3614 innames = &fnames[0];
|
|
3615 incount = 1;
|
|
3616 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
|
|
3617 }
|
|
3618 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
|
|
3619 {
|
|
3620 /* Name ends in ".spl", use as the file name. */
|
323
|
3621 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
|
310
|
3622 }
|
|
3623 else
|
|
3624 /* Name should be language, make the file name from it. */
|
|
3625 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
|
|
3626 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
|
|
3627
|
|
3628 /* Check for .ascii.spl. */
|
|
3629 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
|
|
3630 spin.si_ascii = TRUE;
|
|
3631
|
|
3632 /* Check for .add.spl. */
|
|
3633 if (strstr((char *)gettail(wfname), ".add.") != NULL)
|
|
3634 spin.si_add = TRUE;
|
240
|
3635 }
|
|
3636
|
310
|
3637 if (incount <= 0)
|
236
|
3638 EMSG(_(e_invarg)); /* need at least output and input names */
|
310
|
3639 else if (incount > 8)
|
236
|
3640 EMSG(_("E754: Only up to 8 regions supported"));
|
|
3641 else
|
|
3642 {
|
|
3643 /* Check for overwriting before doing things that may take a lot of
|
|
3644 * time. */
|
310
|
3645 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
|
236
|
3646 {
|
|
3647 EMSG(_(e_exists));
|
310
|
3648 return;
|
236
|
3649 }
|
310
|
3650 if (mch_isdir(wfname))
|
236
|
3651 {
|
310
|
3652 EMSG2(_(e_isadir2), wfname);
|
|
3653 return;
|
236
|
3654 }
|
|
3655
|
|
3656 /*
|
|
3657 * Init the aff and dic pointers.
|
|
3658 * Get the region names if there are more than 2 arguments.
|
|
3659 */
|
310
|
3660 for (i = 0; i < incount; ++i)
|
236
|
3661 {
|
310
|
3662 afile[i] = NULL;
|
300
|
3663
|
316
|
3664 if (incount > 1)
|
236
|
3665 {
|
310
|
3666 len = STRLEN(innames[i]);
|
|
3667 if (STRLEN(gettail(innames[i])) < 5
|
|
3668 || innames[i][len - 3] != '_')
|
236
|
3669 {
|
310
|
3670 EMSG2(_("E755: Invalid region in %s"), innames[i]);
|
|
3671 return;
|
236
|
3672 }
|
316
|
3673 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
|
|
3674 spin.si_region_name[i * 2 + 1] =
|
|
3675 TOLOWER_ASC(innames[i][len - 1]);
|
236
|
3676 }
|
|
3677 }
|
316
|
3678 spin.si_region_count = incount;
|
236
|
3679
|
310
|
3680 if (!spin.si_add)
|
|
3681 /* Clear the char type tables, don't want to use any of the
|
|
3682 * currently used spell properties. */
|
|
3683 init_spell_chartab();
|
255
|
3684
|
300
|
3685 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
|
|
3686 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
|
|
3687 if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
|
|
3688 {
|
|
3689 error = TRUE;
|
310
|
3690 return;
|
300
|
3691 }
|
|
3692
|
236
|
3693 /*
|
|
3694 * Read all the .aff and .dic files.
|
|
3695 * Text is converted to 'encoding'.
|
300
|
3696 * Words are stored in the case-folded and keep-case trees.
|
236
|
3697 */
|
310
|
3698 for (i = 0; i < incount && !error; ++i)
|
236
|
3699 {
|
300
|
3700 spin.si_conv.vc_type = CONV_NONE;
|
310
|
3701 spin.si_region = 1 << i;
|
|
3702
|
|
3703 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
|
300
|
3704 if (mch_stat((char *)fname, &st) >= 0)
|
|
3705 {
|
|
3706 /* Read the .aff file. Will init "spin->si_conv" based on the
|
|
3707 * "SET" line. */
|
310
|
3708 afile[i] = spell_read_aff(fname, &spin);
|
|
3709 if (afile[i] == NULL)
|
300
|
3710 error = TRUE;
|
|
3711 else
|
|
3712 {
|
|
3713 /* Read the .dic file and store the words in the trees. */
|
|
3714 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
|
310
|
3715 innames[i]);
|
|
3716 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
|
300
|
3717 error = TRUE;
|
|
3718 }
|
|
3719 }
|
|
3720 else
|
|
3721 {
|
|
3722 /* No .aff file, try reading the file as a word list. Store
|
|
3723 * the words in the trees. */
|
310
|
3724 if (spell_read_wordfile(innames[i], &spin) == FAIL)
|
300
|
3725 error = TRUE;
|
|
3726 }
|
236
|
3727
|
310
|
3728 #ifdef FEAT_MBYTE
|
236
|
3729 /* Free any conversion stuff. */
|
300
|
3730 convert_setup(&spin.si_conv, NULL, NULL);
|
310
|
3731 #endif
|
236
|
3732 }
|
|
3733
|
300
|
3734 if (!error)
|
236
|
3735 {
|
|
3736 /*
|
300
|
3737 * Remove the dummy NUL from the start of the tree root.
|
236
|
3738 */
|
300
|
3739 spin.si_foldroot = spin.si_foldroot->wn_sibling;
|
|
3740 spin.si_keeproot = spin.si_keeproot->wn_sibling;
|
236
|
3741
|
|
3742 /*
|
300
|
3743 * Combine tails in the tree.
|
236
|
3744 */
|
323
|
3745 if (!added_word || p_verbose > 2)
|
310
|
3746 {
|
323
|
3747 if (added_word)
|
310
|
3748 verbose_enter();
|
|
3749 MSG(_("Compressing word tree..."));
|
|
3750 out_flush();
|
323
|
3751 if (added_word)
|
310
|
3752 verbose_leave();
|
|
3753 }
|
|
3754 wordtree_compress(spin.si_foldroot, &spin);
|
|
3755 wordtree_compress(spin.si_keeproot, &spin);
|
236
|
3756 }
|
|
3757
|
300
|
3758 if (!error)
|
|
3759 {
|
|
3760 /*
|
|
3761 * Write the info in the spell file.
|
|
3762 */
|
323
|
3763 if (!added_word || p_verbose > 2)
|
310
|
3764 {
|
323
|
3765 if (added_word)
|
310
|
3766 verbose_enter();
|
|
3767 smsg((char_u *)_("Writing spell file %s..."), wfname);
|
|
3768 out_flush();
|
323
|
3769 if (added_word)
|
310
|
3770 verbose_leave();
|
|
3771 }
|
|
3772
|
316
|
3773 write_vim_spell(wfname, &spin);
|
310
|
3774
|
323
|
3775 if (!added_word || p_verbose > 2)
|
310
|
3776 {
|
323
|
3777 if (added_word)
|
310
|
3778 verbose_enter();
|
|
3779 MSG(_("Done!"));
|
|
3780 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
|
302
|
3781 spin.si_memtot);
|
310
|
3782 out_flush();
|
323
|
3783 if (added_word)
|
310
|
3784 verbose_leave();
|
|
3785 }
|
|
3786
|
|
3787 /* If the file is loaded need to reload it. */
|
323
|
3788 spell_reload_one(wfname, added_word);
|
300
|
3789 }
|
|
3790
|
|
3791 /* Free the allocated memory. */
|
|
3792 free_blocks(spin.si_blocks);
|
323
|
3793 ga_clear(&spin.si_rep);
|
|
3794 ga_clear(&spin.si_sal);
|
|
3795 ga_clear(&spin.si_map);
|
300
|
3796
|
|
3797 /* Free the .aff file structures. */
|
310
|
3798 for (i = 0; i < incount; ++i)
|
|
3799 if (afile[i] != NULL)
|
|
3800 spell_free_aff(afile[i]);
|
236
|
3801 }
|
310
|
3802 }
|
|
3803
|
|
3804
|
|
3805 /*
|
|
3806 * ":spellgood {word}"
|
|
3807 * ":spellwrong {word}"
|
|
3808 */
|
|
3809 void
|
|
3810 ex_spell(eap)
|
|
3811 exarg_T *eap;
|
|
3812 {
|
|
3813 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
|
236
|
3814 }
|
|
3815
|
310
|
3816 /*
|
|
3817 * Add "word[len]" to 'spellfile' as a good or bad word.
|
|
3818 */
|
|
3819 void
|
|
3820 spell_add_word(word, len, bad)
|
|
3821 char_u *word;
|
|
3822 int len;
|
|
3823 int bad;
|
|
3824 {
|
|
3825 FILE *fd;
|
|
3826 buf_T *buf;
|
|
3827
|
|
3828 if (*curbuf->b_p_spf == NUL)
|
|
3829 init_spellfile();
|
|
3830 if (*curbuf->b_p_spf == NUL)
|
323
|
3831 EMSG(_("E764: 'spellfile' is not set"));
|
310
|
3832 else
|
|
3833 {
|
|
3834 /* Check that the user isn't editing the .add file somewhere. */
|
|
3835 buf = buflist_findname_exp(curbuf->b_p_spf);
|
|
3836 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
|
|
3837 buf = NULL;
|
|
3838 if (buf != NULL && bufIsChanged(buf))
|
|
3839 EMSG(_(e_bufloaded));
|
|
3840 else
|
|
3841 {
|
|
3842 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
|
|
3843 if (fd == NULL)
|
|
3844 EMSG2(_(e_notopen), curbuf->b_p_spf);
|
|
3845 else
|
|
3846 {
|
|
3847 if (bad)
|
|
3848 fprintf(fd, "/!%.*s\n", len, word);
|
|
3849 else
|
|
3850 fprintf(fd, "%.*s\n", len, word);
|
|
3851 fclose(fd);
|
|
3852
|
|
3853 /* Update the .add.spl file. */
|
323
|
3854 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
|
310
|
3855
|
|
3856 /* If the .add file is edited somewhere, reload it. */
|
|
3857 if (buf != NULL)
|
|
3858 buf_reload(buf);
|
323
|
3859
|
|
3860 redraw_all_later(NOT_VALID);
|
310
|
3861 }
|
|
3862 }
|
|
3863 }
|
|
3864 }
|
|
3865
|
|
3866 /*
|
|
3867 * Initialize 'spellfile' for the current buffer.
|
|
3868 */
|
|
3869 static void
|
|
3870 init_spellfile()
|
|
3871 {
|
|
3872 char_u buf[MAXPATHL];
|
|
3873 int l;
|
|
3874 slang_T *sl;
|
|
3875 char_u *rtp;
|
|
3876
|
|
3877 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
|
|
3878 {
|
|
3879 /* Loop over all entries in 'runtimepath'. */
|
|
3880 rtp = p_rtp;
|
|
3881 while (*rtp != NUL)
|
|
3882 {
|
|
3883 /* Copy the path from 'runtimepath' to buf[]. */
|
|
3884 copy_option_part(&rtp, buf, MAXPATHL, ",");
|
|
3885 if (filewritable(buf) == 2)
|
|
3886 {
|
316
|
3887 /* Use the first language name from 'spelllang' and the
|
|
3888 * encoding used in the first loaded .spl file. */
|
310
|
3889 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
|
|
3890 l = STRLEN(buf);
|
|
3891 vim_snprintf((char *)buf + l, MAXPATHL - l,
|
316
|
3892 "/spell/%.*s.%s.add",
|
|
3893 2, curbuf->b_p_spl,
|
310
|
3894 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
|
|
3895 ? (char_u *)"ascii" : spell_enc());
|
|
3896 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
|
|
3897 break;
|
|
3898 }
|
|
3899 }
|
|
3900 }
|
|
3901 }
|
236
|
3902
|
300
|
3903
|
307
|
3904 /*
|
|
3905 * Init the chartab used for spelling for ASCII.
|
|
3906 * EBCDIC is not supported!
|
|
3907 */
|
|
3908 static void
|
|
3909 clear_spell_chartab(sp)
|
|
3910 spelltab_T *sp;
|
|
3911 {
|
324
|
3912 int i;
|
307
|
3913
|
|
3914 /* Init everything to FALSE. */
|
|
3915 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
|
|
3916 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
|
|
3917 for (i = 0; i < 256; ++i)
|
324
|
3918 {
|
307
|
3919 sp->st_fold[i] = i;
|
324
|
3920 sp->st_upper[i] = i;
|
|
3921 }
|
307
|
3922
|
|
3923 /* We include digits. A word shouldn't start with a digit, but handling
|
|
3924 * that is done separately. */
|
|
3925 for (i = '0'; i <= '9'; ++i)
|
|
3926 sp->st_isw[i] = TRUE;
|
|
3927 for (i = 'A'; i <= 'Z'; ++i)
|
|
3928 {
|
|
3929 sp->st_isw[i] = TRUE;
|
|
3930 sp->st_isu[i] = TRUE;
|
|
3931 sp->st_fold[i] = i + 0x20;
|
|
3932 }
|
|
3933 for (i = 'a'; i <= 'z'; ++i)
|
324
|
3934 {
|
307
|
3935 sp->st_isw[i] = TRUE;
|
324
|
3936 sp->st_upper[i] = i - 0x20;
|
|
3937 }
|
307
|
3938 }
|
|
3939
|
|
3940 /*
|
|
3941 * Init the chartab used for spelling. Only depends on 'encoding'.
|
|
3942 * Called once while starting up and when 'encoding' changes.
|
|
3943 * The default is to use isalpha(), but the spell file should define the word
|
|
3944 * characters to make it possible that 'encoding' differs from the current
|
|
3945 * locale.
|
|
3946 */
|
|
3947 void
|
|
3948 init_spell_chartab()
|
|
3949 {
|
|
3950 int i;
|
|
3951
|
|
3952 did_set_spelltab = FALSE;
|
|
3953 clear_spell_chartab(&spelltab);
|
|
3954
|
|
3955 #ifdef FEAT_MBYTE
|
|
3956 if (enc_dbcs)
|
|
3957 {
|
|
3958 /* DBCS: assume double-wide characters are word characters. */
|
|
3959 for (i = 128; i <= 255; ++i)
|
|
3960 if (MB_BYTE2LEN(i) == 2)
|
|
3961 spelltab.st_isw[i] = TRUE;
|
|
3962 }
|
324
|
3963 else if (enc_utf8)
|
|
3964 {
|
|
3965 for (i = 128; i < 256; ++i)
|
|
3966 {
|
|
3967 spelltab.st_isu[i] = utf_isupper(i);
|
|
3968 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
|
|
3969 spelltab.st_fold[i] = utf_fold(i);
|
|
3970 spelltab.st_upper[i] = utf_toupper(i);
|
|
3971 }
|
|
3972 }
|
307
|
3973 else
|
|
3974 #endif
|
|
3975 {
|
324
|
3976 /* Rough guess: use locale-dependent library functions. */
|
307
|
3977 for (i = 128; i < 256; ++i)
|
|
3978 {
|
|
3979 if (MB_ISUPPER(i))
|
|
3980 {
|
324
|
3981 spelltab.st_isw[i] = TRUE;
|
307
|
3982 spelltab.st_isu[i] = TRUE;
|
|
3983 spelltab.st_fold[i] = MB_TOLOWER(i);
|
|
3984 }
|
324
|
3985 else if (MB_ISLOWER(i))
|
|
3986 {
|
|
3987 spelltab.st_isw[i] = TRUE;
|
|
3988 spelltab.st_upper[i] = MB_TOUPPER(i);
|
|
3989 }
|
307
|
3990 }
|
|
3991 }
|
|
3992 }
|
|
3993
|
|
3994 static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
|
|
3995 static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
|
|
3996
|
|
3997 /*
|
|
3998 * Set the spell character tables from strings in the affix file.
|
|
3999 */
|
|
4000 static int
|
|
4001 set_spell_chartab(fol, low, upp)
|
|
4002 char_u *fol;
|
|
4003 char_u *low;
|
|
4004 char_u *upp;
|
|
4005 {
|
|
4006 /* We build the new tables here first, so that we can compare with the
|
|
4007 * previous one. */
|
|
4008 spelltab_T new_st;
|
|
4009 char_u *pf = fol, *pl = low, *pu = upp;
|
|
4010 int f, l, u;
|
|
4011
|
|
4012 clear_spell_chartab(&new_st);
|
|
4013
|
|
4014 while (*pf != NUL)
|
|
4015 {
|
|
4016 if (*pl == NUL || *pu == NUL)
|
|
4017 {
|
|
4018 EMSG(_(e_affform));
|
|
4019 return FAIL;
|
|
4020 }
|
|
4021 #ifdef FEAT_MBYTE
|
|
4022 f = mb_ptr2char_adv(&pf);
|
|
4023 l = mb_ptr2char_adv(&pl);
|
|
4024 u = mb_ptr2char_adv(&pu);
|
|
4025 #else
|
|
4026 f = *pf++;
|
|
4027 l = *pl++;
|
|
4028 u = *pu++;
|
|
4029 #endif
|
|
4030 /* Every character that appears is a word character. */
|
|
4031 if (f < 256)
|
|
4032 new_st.st_isw[f] = TRUE;
|
|
4033 if (l < 256)
|
|
4034 new_st.st_isw[l] = TRUE;
|
|
4035 if (u < 256)
|
|
4036 new_st.st_isw[u] = TRUE;
|
|
4037
|
|
4038 /* if "LOW" and "FOL" are not the same the "LOW" char needs
|
|
4039 * case-folding */
|
|
4040 if (l < 256 && l != f)
|
|
4041 {
|
|
4042 if (f >= 256)
|
|
4043 {
|
|
4044 EMSG(_(e_affrange));
|
|
4045 return FAIL;
|
|
4046 }
|
|
4047 new_st.st_fold[l] = f;
|
|
4048 }
|
|
4049
|
|
4050 /* if "UPP" and "FOL" are not the same the "UPP" char needs
|
324
|
4051 * case-folding, it's upper case and the "UPP" is the upper case of
|
|
4052 * "FOL" . */
|
307
|
4053 if (u < 256 && u != f)
|
|
4054 {
|
|
4055 if (f >= 256)
|
|
4056 {
|
|
4057 EMSG(_(e_affrange));
|
|
4058 return FAIL;
|
|
4059 }
|
|
4060 new_st.st_fold[u] = f;
|
|
4061 new_st.st_isu[u] = TRUE;
|
324
|
4062 new_st.st_upper[f] = u;
|
307
|
4063 }
|
|
4064 }
|
|
4065
|
|
4066 if (*pl != NUL || *pu != NUL)
|
|
4067 {
|
|
4068 EMSG(_(e_affform));
|
|
4069 return FAIL;
|
|
4070 }
|
|
4071
|
|
4072 return set_spell_finish(&new_st);
|
|
4073 }
|
|
4074
|
|
4075 /*
|
|
4076 * Set the spell character tables from strings in the .spl file.
|
|
4077 */
|
|
4078 static int
|
|
4079 set_spell_charflags(flags, cnt, upp)
|
|
4080 char_u *flags;
|
|
4081 int cnt;
|
|
4082 char_u *upp;
|
|
4083 {
|
|
4084 /* We build the new tables here first, so that we can compare with the
|
|
4085 * previous one. */
|
|
4086 spelltab_T new_st;
|
|
4087 int i;
|
|
4088 char_u *p = upp;
|
324
|
4089 int c;
|
307
|
4090
|
|
4091 clear_spell_chartab(&new_st);
|
|
4092
|
|
4093 for (i = 0; i < cnt; ++i)
|
|
4094 {
|
324
|
4095 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
|
|
4096 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
|
307
|
4097
|
|
4098 if (*p == NUL)
|
|
4099 return FAIL;
|
|
4100 #ifdef FEAT_MBYTE
|
324
|
4101 c = mb_ptr2char_adv(&p);
|
307
|
4102 #else
|
324
|
4103 c = *p++;
|
307
|
4104 #endif
|
324
|
4105 new_st.st_fold[i + 128] = c;
|
|
4106 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
|
|
4107 new_st.st_upper[c] = i + 128;
|
307
|
4108 }
|
|
4109
|
|
4110 return set_spell_finish(&new_st);
|
|
4111 }
|
|
4112
|
|
4113 static int
|
|
4114 set_spell_finish(new_st)
|
|
4115 spelltab_T *new_st;
|
|
4116 {
|
|
4117 int i;
|
|
4118
|
|
4119 if (did_set_spelltab)
|
|
4120 {
|
|
4121 /* check that it's the same table */
|
|
4122 for (i = 0; i < 256; ++i)
|
|
4123 {
|
|
4124 if (spelltab.st_isw[i] != new_st->st_isw[i]
|
|
4125 || spelltab.st_isu[i] != new_st->st_isu[i]
|
324
|
4126 || spelltab.st_fold[i] != new_st->st_fold[i]
|
|
4127 || spelltab.st_upper[i] != new_st->st_upper[i])
|
307
|
4128 {
|
|
4129 EMSG(_("E763: Word characters differ between spell files"));
|
|
4130 return FAIL;
|
|
4131 }
|
|
4132 }
|
|
4133 }
|
|
4134 else
|
|
4135 {
|
|
4136 /* copy the new spelltab into the one being used */
|
|
4137 spelltab = *new_st;
|
|
4138 did_set_spelltab = TRUE;
|
|
4139 }
|
|
4140
|
|
4141 return OK;
|
|
4142 }
|
|
4143
|
|
4144 /*
|
|
4145 * Write the current tables into the .spl file.
|
|
4146 * This makes sure the same characters are recognized as word characters when
|
|
4147 * generating an when using a spell file.
|
|
4148 */
|
|
4149 static void
|
|
4150 write_spell_chartab(fd)
|
|
4151 FILE *fd;
|
|
4152 {
|
|
4153 char_u charbuf[256 * 4];
|
|
4154 int len = 0;
|
|
4155 int flags;
|
|
4156 int i;
|
|
4157
|
|
4158 fputc(128, fd); /* <charflagslen> */
|
|
4159 for (i = 128; i < 256; ++i)
|
|
4160 {
|
|
4161 flags = 0;
|
|
4162 if (spelltab.st_isw[i])
|
324
|
4163 flags |= CF_WORD;
|
307
|
4164 if (spelltab.st_isu[i])
|
324
|
4165 flags |= CF_UPPER;
|
307
|
4166 fputc(flags, fd); /* <charflags> */
|
|
4167
|
310
|
4168 #ifdef FEAT_MBYTE
|
|
4169 if (has_mbyte)
|
|
4170 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
|
|
4171 else
|
|
4172 #endif
|
|
4173 charbuf[len++] = spelltab.st_fold[i];
|
307
|
4174 }
|
|
4175
|
|
4176 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
|
|
4177 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
|
|
4178 }
|
|
4179
|
|
4180 /*
|
324
|
4181 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
|
|
4182 * Uses the character definitions from the .spl file.
|
307
|
4183 * When using a multi-byte 'encoding' the length may change!
|
|
4184 * Returns FAIL when something wrong.
|
|
4185 */
|
|
4186 static int
|
324
|
4187 spell_casefold(str, len, buf, buflen)
|
|
4188 char_u *str;
|
307
|
4189 int len;
|
|
4190 char_u *buf;
|
|
4191 int buflen;
|
|
4192 {
|
|
4193 int i;
|
|
4194
|
|
4195 if (len >= buflen)
|
|
4196 {
|
|
4197 buf[0] = NUL;
|
|
4198 return FAIL; /* result will not fit */
|
|
4199 }
|
|
4200
|
|
4201 #ifdef FEAT_MBYTE
|
|
4202 if (has_mbyte)
|
|
4203 {
|
324
|
4204 int outi = 0;
|
|
4205 char_u *p;
|
307
|
4206 int c;
|
|
4207
|
|
4208 /* Fold one character at a time. */
|
324
|
4209 for (p = str; p < str + len; )
|
307
|
4210 {
|
|
4211 if (outi + MB_MAXBYTES > buflen)
|
|
4212 {
|
|
4213 buf[outi] = NUL;
|
|
4214 return FAIL;
|
|
4215 }
|
324
|
4216 c = mb_ptr2char_adv(&p);
|
|
4217 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
|
307
|
4218 }
|
|
4219 buf[outi] = NUL;
|
|
4220 }
|
|
4221 else
|
|
4222 #endif
|
|
4223 {
|
|
4224 /* Be quick for non-multibyte encodings. */
|
|
4225 for (i = 0; i < len; ++i)
|
324
|
4226 buf[i] = spelltab.st_fold[str[i]];
|
307
|
4227 buf[i] = NUL;
|
|
4228 }
|
|
4229
|
|
4230 return OK;
|
|
4231 }
|
|
4232
|
323
|
4233 /*
|
|
4234 * "z?": Find badly spelled word under or after the cursor.
|
|
4235 * Give suggestions for the properly spelled word.
|
|
4236 * This is based on the mechanisms of Aspell, but completely reimplemented.
|
|
4237 */
|
|
4238 void
|
|
4239 spell_suggest()
|
|
4240 {
|
|
4241 char_u *line;
|
|
4242 pos_T prev_cursor = curwin->w_cursor;
|
|
4243 int attr;
|
|
4244 char_u wcopy[MAXWLEN + 2];
|
|
4245 char_u *p;
|
|
4246 int i;
|
|
4247 int c;
|
|
4248 suginfo_T sug;
|
|
4249 suggest_T *stp;
|
|
4250
|
|
4251 /*
|
|
4252 * Find the start of the badly spelled word.
|
|
4253 */
|
|
4254 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
|
|
4255 {
|
|
4256 beep_flush();
|
|
4257 return;
|
|
4258 }
|
|
4259
|
|
4260 /*
|
|
4261 * Set the info in "sug".
|
|
4262 */
|
|
4263 vim_memset(&sug, 0, sizeof(sug));
|
|
4264 ga_init2(&sug.su_ga, (int)sizeof(suggest_T), 10);
|
|
4265 hash_init(&sug.su_banned);
|
|
4266 line = ml_get_curline();
|
|
4267 sug.su_badptr = line + curwin->w_cursor.col;
|
|
4268 sug.su_badlen = spell_check(curwin, sug.su_badptr, &attr);
|
|
4269 if (sug.su_badlen >= MAXWLEN)
|
|
4270 sug.su_badlen = MAXWLEN - 1; /* just in case */
|
|
4271 vim_strncpy(sug.su_badword, sug.su_badptr, sug.su_badlen);
|
|
4272 (void)spell_casefold(sug.su_badptr, sug.su_badlen,
|
|
4273 sug.su_fbadword, MAXWLEN);
|
|
4274
|
|
4275 /* Ban the bad word itself. It may appear in another region. */
|
|
4276 add_banned(&sug, sug.su_badword);
|
|
4277
|
|
4278 /*
|
|
4279 * 1. Try inserting/deleting/swapping/changing a letter, use REP entries
|
|
4280 * from the .aff file and inserting a space (split the word).
|
324
|
4281 *
|
|
4282 * Set a maximum score to limit the combination of operations that is
|
|
4283 * tried.
|
323
|
4284 */
|
|
4285 sug.su_maxscore = SCORE_MAXINIT;
|
|
4286 spell_try_change(&sug);
|
|
4287
|
|
4288 /*
|
|
4289 * 2. Try finding sound-a-like words.
|
324
|
4290 *
|
|
4291 * Only do this when we don't have a lot of suggestions yet, because it's
|
|
4292 * very slow and often doesn't find new suggestions.
|
323
|
4293 */
|
324
|
4294 if (sug.su_ga.ga_len < SUG_CLEAN_COUNT)
|
|
4295 {
|
|
4296 /* Allow a higher score now. */
|
323
|
4297 sug.su_maxscore = SCORE_MAXMAX;
|
324
|
4298 spell_try_soundalike(&sug);
|
|
4299 }
|
323
|
4300
|
|
4301 /* When CTRL-C was hit while searching do show the results. */
|
324
|
4302 ui_breakcheck();
|
323
|
4303 if (got_int)
|
|
4304 {
|
|
4305 (void)vgetc();
|
|
4306 got_int = FALSE;
|
|
4307 }
|
|
4308
|
|
4309 if (sug.su_ga.ga_len == 0)
|
|
4310 MSG(_("Sorry, no suggestions"));
|
|
4311 else
|
|
4312 {
|
324
|
4313 #ifdef RESCORE
|
|
4314 /* Do slow but more accurate computation of the word score. */
|
|
4315 rescore_suggestions(&sug);
|
|
4316 #endif
|
|
4317
|
|
4318 /* Sort the suggestions and truncate at SUG_PROMPT_COUNT. */
|
|
4319 cleanup_suggestions(&sug, SUG_PROMPT_COUNT);
|
323
|
4320
|
|
4321 /* List the suggestions. */
|
|
4322 msg_start();
|
|
4323 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
|
|
4324 sug.su_badlen, sug.su_badptr);
|
|
4325 msg_puts(IObuff);
|
|
4326 msg_clr_eos();
|
|
4327 msg_putchar('\n');
|
|
4328 msg_scroll = TRUE;
|
|
4329 for (i = 0; i < sug.su_ga.ga_len; ++i)
|
|
4330 {
|
|
4331 stp = &SUG(&sug, i);
|
|
4332
|
|
4333 /* The suggested word may replace only part of the bad word, add
|
|
4334 * the not replaced part. */
|
|
4335 STRCPY(wcopy, stp->st_word);
|
|
4336 if (sug.su_badlen > stp->st_orglen)
|
|
4337 vim_strncpy(wcopy + STRLEN(wcopy),
|
|
4338 sug.su_badptr + stp->st_orglen,
|
|
4339 sug.su_badlen - stp->st_orglen);
|
324
|
4340 if (p_verbose > 0)
|
|
4341 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\" (%d)"),
|
323
|
4342 i + 1, wcopy, stp->st_score);
|
324
|
4343 else
|
|
4344 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""),
|
|
4345 i + 1, wcopy);
|
323
|
4346 msg_puts(IObuff);
|
|
4347 lines_left = 3; /* avoid more prompt */
|
|
4348 msg_putchar('\n');
|
|
4349 }
|
|
4350
|
|
4351 /* Ask for choice. */
|
|
4352 i = prompt_for_number();
|
|
4353 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor())
|
|
4354 {
|
|
4355 /* Replace the word. */
|
|
4356 stp = &SUG(&sug, i - 1);
|
|
4357 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
|
|
4358 if (p != NULL)
|
|
4359 {
|
|
4360 c = sug.su_badptr - line;
|
|
4361 mch_memmove(p, line, c);
|
|
4362 STRCPY(p + c, stp->st_word);
|
|
4363 STRCAT(p, sug.su_badptr + stp->st_orglen);
|
|
4364 ml_replace(curwin->w_cursor.lnum, p, FALSE);
|
|
4365 curwin->w_cursor.col = c;
|
|
4366 changed_bytes(curwin->w_cursor.lnum, c);
|
|
4367 }
|
|
4368 }
|
|
4369 else
|
|
4370 curwin->w_cursor = prev_cursor;
|
|
4371 }
|
|
4372
|
|
4373 /* Free the suggestions. */
|
|
4374 for (i = 0; i < sug.su_ga.ga_len; ++i)
|
|
4375 vim_free(SUG(&sug, i).st_word);
|
|
4376 ga_clear(&sug.su_ga);
|
|
4377
|
|
4378 /* Free the banned words. */
|
|
4379 free_banned(&sug);
|
|
4380 }
|
|
4381
|
|
4382 /*
|
324
|
4383 * Make a copy of "word", with the first letter upper or lower cased, to
|
|
4384 * "wcopy[MAXWLEN]". "word" must not be empty.
|
|
4385 * The result is NUL terminated.
|
323
|
4386 */
|
|
4387 static void
|
324
|
4388 onecap_copy(word, wcopy, upper)
|
323
|
4389 char_u *word;
|
|
4390 char_u *wcopy;
|
|
4391 int upper; /* TRUE: first letter made upper case */
|
|
4392 {
|
|
4393 char_u *p;
|
|
4394 int c;
|
|
4395 int l;
|
|
4396
|
|
4397 p = word;
|
|
4398 #ifdef FEAT_MBYTE
|
|
4399 if (has_mbyte)
|
|
4400 c = mb_ptr2char_adv(&p);
|
|
4401 else
|
|
4402 #endif
|
|
4403 c = *p++;
|
|
4404 if (upper)
|
324
|
4405 c = SPELL_TOUPPER(c);
|
323
|
4406 else
|
324
|
4407 c = SPELL_TOFOLD(c);
|
323
|
4408 #ifdef FEAT_MBYTE
|
|
4409 if (has_mbyte)
|
|
4410 l = mb_char2bytes(c, wcopy);
|
|
4411 else
|
|
4412 #endif
|
|
4413 {
|
|
4414 l = 1;
|
|
4415 wcopy[0] = c;
|
|
4416 }
|
324
|
4417 vim_strncpy(wcopy + l, p, MAXWLEN - l);
|
323
|
4418 }
|
|
4419
|
|
4420 /*
|
324
|
4421 * Make a copy of "word" with all the letters upper cased into
|
|
4422 * "wcopy[MAXWLEN]". The result is NUL terminated.
|
323
|
4423 */
|
|
4424 static void
|
|
4425 allcap_copy(word, wcopy)
|
|
4426 char_u *word;
|
|
4427 char_u *wcopy;
|
|
4428 {
|
|
4429 char_u *s;
|
|
4430 char_u *d;
|
|
4431 int c;
|
|
4432
|
|
4433 d = wcopy;
|
|
4434 for (s = word; *s != NUL; )
|
|
4435 {
|
|
4436 #ifdef FEAT_MBYTE
|
|
4437 if (has_mbyte)
|
|
4438 c = mb_ptr2char_adv(&s);
|
|
4439 else
|
|
4440 #endif
|
|
4441 c = *s++;
|
324
|
4442 c = SPELL_TOUPPER(c);
|
323
|
4443
|
|
4444 #ifdef FEAT_MBYTE
|
|
4445 if (has_mbyte)
|
|
4446 {
|
|
4447 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
|
|
4448 break;
|
|
4449 d += mb_char2bytes(c, d);
|
|
4450 }
|
|
4451 else
|
|
4452 #endif
|
|
4453 {
|
|
4454 if (d - wcopy >= MAXWLEN - 1)
|
|
4455 break;
|
|
4456 *d++ = c;
|
|
4457 }
|
|
4458 }
|
|
4459 *d = NUL;
|
|
4460 }
|
|
4461
|
|
4462 /*
|
|
4463 * Try finding suggestions by adding/removing/swapping letters.
|
330
|
4464 *
|
|
4465 * This uses a state machine. At each node in the tree we try various
|
|
4466 * operations. When trying if an operation work "depth" is increased and the
|
|
4467 * stack[] is used to store info. This allows combinations, thus insert one
|
|
4468 * character, replace one and delete another. The number of changes is
|
|
4469 * limited by su->su_maxscore, checked in try_deeper().
|
323
|
4470 */
|
|
4471 static void
|
|
4472 spell_try_change(su)
|
|
4473 suginfo_T *su;
|
|
4474 {
|
|
4475 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
|
|
4476 char_u tword[MAXWLEN]; /* good word collected so far */
|
|
4477 trystate_T stack[MAXWLEN];
|
|
4478 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
|
|
4479 * to for word split) */
|
|
4480 char_u prewordlen = 0; /* length of word in "preword" */
|
|
4481 int splitoff = 0; /* index in tword after last split */
|
|
4482 trystate_T *sp;
|
|
4483 int newscore;
|
|
4484 langp_T *lp;
|
|
4485 char_u *byts;
|
324
|
4486 idx_T *idxs;
|
323
|
4487 int depth;
|
330
|
4488 int c, c2, c3;
|
|
4489 int n = 0;
|
323
|
4490 int flags;
|
|
4491 int badflags;
|
|
4492 garray_T *gap;
|
324
|
4493 idx_T arridx;
|
323
|
4494 int len;
|
|
4495 char_u *p;
|
|
4496 fromto_T *ftp;
|
330
|
4497 int fl = 0, tl;
|
323
|
4498
|
|
4499 /* get caps flags for bad word */
|
|
4500 badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
|
|
4501
|
|
4502 /* We make a copy of the case-folded bad word, so that we can modify it
|
|
4503 * to find matches (esp. REP items). */
|
|
4504 STRCPY(fword, su->su_fbadword);
|
|
4505
|
|
4506
|
|
4507 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
4508 lp->lp_slang != NULL; ++lp)
|
|
4509 {
|
|
4510 #ifdef SOUNDFOLD_SCORE
|
|
4511 su->su_slang = lp->lp_slang;
|
|
4512 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
4513 /* soundfold the bad word */
|
|
4514 spell_soundfold(lp->lp_slang, su->su_fbadword, su->su_salword);
|
|
4515 #endif
|
|
4516
|
|
4517 /*
|
|
4518 * Go through the whole case-fold tree, try changes at each node.
|
|
4519 * "tword[]" contains the word collected from nodes in the tree.
|
|
4520 * "fword[]" the word we are trying to match with (initially the bad
|
|
4521 * word).
|
|
4522 */
|
|
4523 byts = lp->lp_slang->sl_fbyts;
|
|
4524 idxs = lp->lp_slang->sl_fidxs;
|
|
4525
|
|
4526 depth = 0;
|
|
4527 stack[0].ts_state = STATE_START;
|
|
4528 stack[0].ts_score = 0;
|
|
4529 stack[0].ts_curi = 1;
|
|
4530 stack[0].ts_fidx = 0;
|
|
4531 stack[0].ts_fidxtry = 0;
|
|
4532 stack[0].ts_twordlen = 0;
|
|
4533 stack[0].ts_arridx = 0;
|
330
|
4534 #ifdef FEAT_MBYTE
|
|
4535 stack[0].ts_tcharlen = 0;
|
|
4536 #endif
|
|
4537
|
|
4538 /*
|
|
4539 * Loop to find all suggestions. At each round we either:
|
|
4540 * - For the current state try one operation, advance "ts_curi",
|
|
4541 * increase "depth".
|
|
4542 * - When a state is done go to the next, set "ts_state".
|
|
4543 * - When all states are tried decrease "depth".
|
|
4544 */
|
323
|
4545 while (depth >= 0 && !got_int)
|
|
4546 {
|
|
4547 sp = &stack[depth];
|
|
4548 switch (sp->ts_state)
|
|
4549 {
|
|
4550 case STATE_START:
|
|
4551 /*
|
|
4552 * Start of node: Deal with NUL bytes, which means
|
|
4553 * tword[] may end here.
|
|
4554 */
|
|
4555 arridx = sp->ts_arridx; /* current node in the tree */
|
|
4556 len = byts[arridx]; /* bytes in this node */
|
|
4557 arridx += sp->ts_curi; /* index of current byte */
|
|
4558
|
|
4559 if (sp->ts_curi > len || (c = byts[arridx]) != 0)
|
|
4560 {
|
|
4561 /* Past bytes in node and/or past NUL bytes. */
|
|
4562 sp->ts_state = STATE_ENDNUL;
|
|
4563 break;
|
|
4564 }
|
|
4565
|
|
4566 /*
|
|
4567 * End of word in tree.
|
|
4568 */
|
|
4569 ++sp->ts_curi; /* eat one NUL byte */
|
|
4570
|
324
|
4571 flags = (int)idxs[arridx];
|
323
|
4572
|
|
4573 /*
|
|
4574 * Form the word with proper case in preword.
|
|
4575 * If there is a word from a previous split, append.
|
|
4576 */
|
|
4577 tword[sp->ts_twordlen] = NUL;
|
|
4578 if (flags & WF_KEEPCAP)
|
|
4579 /* Must find the word in the keep-case tree. */
|
|
4580 find_keepcap_word(lp->lp_slang, tword + splitoff,
|
|
4581 preword + prewordlen);
|
|
4582 else
|
|
4583 /* Include badflags: if the badword is onecap or allcap
|
|
4584 * use that for the goodword too. */
|
|
4585 make_case_word(tword + splitoff,
|
|
4586 preword + prewordlen, flags | badflags);
|
|
4587
|
|
4588 /* Don't use a banned word. It may appear again as a good
|
|
4589 * word, thus remember it. */
|
|
4590 if (flags & WF_BANNED)
|
|
4591 {
|
|
4592 add_banned(su, preword + prewordlen);
|
|
4593 break;
|
|
4594 }
|
|
4595 if (was_banned(su, preword + prewordlen))
|
|
4596 break;
|
|
4597
|
|
4598 newscore = 0;
|
|
4599 if ((flags & WF_REGION)
|
|
4600 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
|
|
4601 newscore += SCORE_REGION;
|
|
4602 if (flags & WF_RARE)
|
|
4603 newscore += SCORE_RARE;
|
|
4604
|
|
4605 if (!spell_valid_case(badflags,
|
|
4606 captype(preword + prewordlen, NULL)))
|
|
4607 newscore += SCORE_ICASE;
|
|
4608
|
|
4609 if (fword[sp->ts_fidx] == 0)
|
|
4610 {
|
|
4611 /* The badword also ends: add suggestions, */
|
324
|
4612 add_suggestion(su, preword, sp->ts_score + newscore
|
|
4613 #ifdef RESCORE
|
|
4614 , FALSE
|
|
4615 #endif
|
|
4616 );
|
323
|
4617 }
|
330
|
4618 else if (sp->ts_fidx >= sp->ts_fidxtry
|
|
4619 #ifdef FEAT_MBYTE
|
|
4620 /* Don't split halfway a character. */
|
|
4621 && (!has_mbyte || sp->ts_tcharlen == 0)
|
|
4622 #endif
|
|
4623 )
|
323
|
4624 {
|
|
4625 /* The word in the tree ends but the badword
|
|
4626 * continues: try inserting a space and check that a valid
|
|
4627 * words starts at fword[sp->ts_fidx]. */
|
|
4628 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
|
|
4629 {
|
|
4630 /* Save things to be restored at STATE_SPLITUNDO. */
|
|
4631 sp->ts_save_prewordlen = prewordlen;
|
|
4632 sp->ts_save_badflags = badflags;
|
|
4633 sp->ts_save_splitoff = splitoff;
|
|
4634
|
|
4635 /* Append a space to preword. */
|
|
4636 STRCAT(preword, " ");
|
|
4637 prewordlen = STRLEN(preword);
|
|
4638 splitoff = sp->ts_twordlen;
|
324
|
4639 #ifdef FEAT_MBYTE
|
|
4640 if (has_mbyte)
|
|
4641 {
|
|
4642 int i = 0;
|
|
4643
|
|
4644 /* Case-folding may change the number of bytes:
|
|
4645 * Count nr of chars in fword[sp->ts_fidx] and
|
|
4646 * advance that many chars in su->su_badptr. */
|
|
4647 for (p = fword; p < fword + sp->ts_fidx;
|
|
4648 mb_ptr_adv(p))
|
|
4649 ++i;
|
|
4650 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
|
|
4651 --i;
|
|
4652 }
|
|
4653 else
|
|
4654 #endif
|
|
4655 p = su->su_badptr + sp->ts_fidx;
|
|
4656 badflags = captype(p, su->su_badptr + su->su_badlen);
|
323
|
4657
|
|
4658 sp->ts_state = STATE_SPLITUNDO;
|
|
4659 ++depth;
|
|
4660 /* Restart at top of the tree. */
|
|
4661 stack[depth].ts_arridx = 0;
|
|
4662 }
|
|
4663 }
|
|
4664 break;
|
|
4665
|
|
4666 case STATE_SPLITUNDO:
|
|
4667 /* Fixup the changes done for word split. */
|
|
4668 badflags = sp->ts_save_badflags;
|
|
4669 splitoff = sp->ts_save_splitoff;
|
|
4670 prewordlen = sp->ts_save_prewordlen;
|
|
4671
|
|
4672 /* Continue looking for NUL bytes. */
|
|
4673 sp->ts_state = STATE_START;
|
|
4674 break;
|
|
4675
|
|
4676 case STATE_ENDNUL:
|
|
4677 /* Past the NUL bytes in the node. */
|
|
4678 if (fword[sp->ts_fidx] == 0)
|
|
4679 {
|
|
4680 /* The badword ends, can't use the bytes in this node. */
|
|
4681 sp->ts_state = STATE_DEL;
|
|
4682 break;
|
|
4683 }
|
|
4684 sp->ts_state = STATE_PLAIN;
|
|
4685 /*FALLTHROUGH*/
|
|
4686
|
|
4687 case STATE_PLAIN:
|
|
4688 /*
|
|
4689 * Go over all possible bytes at this node, add each to
|
|
4690 * tword[] and use child node. "ts_curi" is the index.
|
|
4691 */
|
|
4692 arridx = sp->ts_arridx;
|
|
4693 if (sp->ts_curi > byts[arridx])
|
|
4694 {
|
|
4695 /* Done all bytes at this node, do next state. When still
|
|
4696 * at already changed bytes skip the other tricks. */
|
|
4697 if (sp->ts_fidx >= sp->ts_fidxtry)
|
|
4698 sp->ts_state = STATE_DEL;
|
|
4699 else
|
|
4700 sp->ts_state = STATE_FINAL;
|
|
4701 }
|
|
4702 else
|
|
4703 {
|
|
4704 arridx += sp->ts_curi++;
|
|
4705 c = byts[arridx];
|
|
4706
|
|
4707 /* Normal byte, go one level deeper. If it's not equal to
|
|
4708 * the byte in the bad word adjust the score. But don't
|
|
4709 * even try when the byte was already changed. */
|
330
|
4710 if (c == fword[sp->ts_fidx]
|
|
4711 #ifdef FEAT_MBYTE
|
|
4712 || (sp->ts_tcharlen > 0
|
|
4713 && sp->ts_isdiff != DIFF_NONE)
|
|
4714 #endif
|
|
4715 )
|
323
|
4716 newscore = 0;
|
|
4717 else
|
|
4718 newscore = SCORE_SUBST;
|
|
4719 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
|
|
4720 && try_deeper(su, stack, depth, newscore))
|
|
4721 {
|
|
4722 ++depth;
|
330
|
4723 sp = &stack[depth];
|
|
4724 ++sp->ts_fidx;
|
|
4725 tword[sp->ts_twordlen++] = c;
|
|
4726 sp->ts_arridx = idxs[arridx];
|
|
4727 #ifdef FEAT_MBYTE
|
|
4728 if (newscore == SCORE_SUBST)
|
|
4729 sp->ts_isdiff = DIFF_YES;
|
|
4730 if (has_mbyte)
|
|
4731 {
|
|
4732 /* Multi-byte characters are a bit complicated to
|
|
4733 * handle: They differ when any of the bytes
|
|
4734 * differ and then their length may also differ. */
|
|
4735 if (sp->ts_tcharlen == 0)
|
|
4736 {
|
|
4737 /* First byte. */
|
|
4738 sp->ts_tcharidx = 0;
|
|
4739 sp->ts_tcharlen = MB_BYTE2LEN(c);
|
|
4740 sp->ts_fcharstart = sp->ts_fidx - 1;
|
|
4741 sp->ts_isdiff = (newscore != 0)
|
|
4742 ? DIFF_YES : DIFF_NONE;
|
|
4743 }
|
|
4744 else if (sp->ts_isdiff == DIFF_INSERT)
|
|
4745 /* When inserting trail bytes don't advance in
|
|
4746 * the bad word. */
|
|
4747 --sp->ts_fidx;
|
|
4748 if (++sp->ts_tcharidx == sp->ts_tcharlen)
|
|
4749 {
|
|
4750 /* Last byte of character. */
|
|
4751 if (sp->ts_isdiff == DIFF_YES)
|
|
4752 {
|
|
4753 /* Correct ts_fidx for the byte length of
|
|
4754 * the character (we didn't check that
|
|
4755 * before). */
|
|
4756 sp->ts_fidx = sp->ts_fcharstart
|
|
4757 + MB_BYTE2LEN(
|
|
4758 fword[sp->ts_fcharstart]);
|
|
4759
|
|
4760 /* For a similar character adjust score
|
|
4761 * from SCORE_SUBST to SCORE_SIMILAR. */
|
|
4762 if (lp->lp_slang->sl_has_map
|
|
4763 && similar_chars(lp->lp_slang,
|
|
4764 mb_ptr2char(tword
|
|
4765 + sp->ts_twordlen
|
|
4766 - sp->ts_tcharlen),
|
|
4767 mb_ptr2char(fword
|
|
4768 + sp->ts_fcharstart)))
|
|
4769 sp->ts_score -=
|
|
4770 SCORE_SUBST - SCORE_SIMILAR;
|
|
4771 }
|
|
4772
|
|
4773 /* Starting a new char, reset the length. */
|
|
4774 sp->ts_tcharlen = 0;
|
|
4775 }
|
|
4776 }
|
|
4777 else
|
|
4778 #endif
|
|
4779 {
|
|
4780 /* If we found a similar char adjust the score.
|
|
4781 * We do this after calling try_deeper() because
|
|
4782 * it's slow. */
|
|
4783 if (newscore != 0
|
|
4784 && lp->lp_slang->sl_has_map
|
|
4785 && similar_chars(lp->lp_slang,
|
|
4786 c, fword[sp->ts_fidx - 1]))
|
|
4787 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
|
|
4788 }
|
323
|
4789 }
|
|
4790 }
|
|
4791 break;
|
|
4792
|
|
4793 case STATE_DEL:
|
330
|
4794 #ifdef FEAT_MBYTE
|
|
4795 /* When past the first byte of a multi-byte char don't try
|
|
4796 * delete/insert/swap a character. */
|
|
4797 if (has_mbyte && sp->ts_tcharlen > 0)
|
|
4798 {
|
|
4799 sp->ts_state = STATE_FINAL;
|
|
4800 break;
|
|
4801 }
|
|
4802 #endif
|
|
4803 /*
|
|
4804 * Try skipping one character in the bad word (delete it).
|
|
4805 */
|
323
|
4806 sp->ts_state = STATE_INS;
|
|
4807 sp->ts_curi = 1;
|
|
4808 if (fword[sp->ts_fidx] != NUL
|
|
4809 && try_deeper(su, stack, depth, SCORE_DEL))
|
|
4810 {
|
|
4811 ++depth;
|
330
|
4812 #ifdef FEAT_MBYTE
|
|
4813 if (has_mbyte)
|
|
4814 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
|
|
4815 else
|
|
4816 #endif
|
|
4817 ++stack[depth].ts_fidx;
|
323
|
4818 break;
|
|
4819 }
|
|
4820 /*FALLTHROUGH*/
|
|
4821
|
|
4822 case STATE_INS:
|
330
|
4823 /* Insert one byte. Do this for each possible byte at this
|
323
|
4824 * node. */
|
|
4825 n = sp->ts_arridx;
|
|
4826 if (sp->ts_curi > byts[n])
|
|
4827 {
|
|
4828 /* Done all bytes at this node, do next state. */
|
|
4829 sp->ts_state = STATE_SWAP;
|
|
4830 }
|
|
4831 else
|
|
4832 {
|
330
|
4833 /* Do one more byte at this node. Skip NUL bytes. */
|
323
|
4834 n += sp->ts_curi++;
|
|
4835 c = byts[n];
|
|
4836 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
|
|
4837 {
|
|
4838 ++depth;
|
330
|
4839 sp = &stack[depth];
|
|
4840 tword[sp->ts_twordlen++] = c;
|
|
4841 sp->ts_arridx = idxs[n];
|
|
4842 #ifdef FEAT_MBYTE
|
|
4843 if (has_mbyte)
|
|
4844 {
|
|
4845 fl = MB_BYTE2LEN(c);
|
|
4846 if (fl > 1)
|
|
4847 {
|
|
4848 /* There are following bytes for the same
|
|
4849 * character. We must find all bytes before
|
|
4850 * trying delete/insert/swap/etc. */
|
|
4851 sp->ts_tcharlen = fl;
|
|
4852 sp->ts_tcharidx = 1;
|
|
4853 sp->ts_isdiff = DIFF_INSERT;
|
|
4854 }
|
|
4855 }
|
|
4856 #endif
|
323
|
4857 }
|
|
4858 }
|
|
4859 break;
|
|
4860
|
|
4861 case STATE_SWAP:
|
330
|
4862 /*
|
|
4863 * Swap two bytes in the bad word: "12" -> "21".
|
|
4864 * We change "fword" here, it's changed back afterwards.
|
|
4865 */
|
|
4866 p = fword + sp->ts_fidx;
|
|
4867 c = *p;
|
|
4868 if (c == NUL)
|
|
4869 {
|
|
4870 /* End of word, can't swap or replace. */
|
|
4871 sp->ts_state = STATE_FINAL;
|
|
4872 break;
|
|
4873 }
|
|
4874 #ifdef FEAT_MBYTE
|
|
4875 if (has_mbyte)
|
|
4876 {
|
|
4877 n = mb_ptr2len_check(p);
|
|
4878 c = mb_ptr2char(p);
|
|
4879 c2 = mb_ptr2char(p + n);
|
|
4880 }
|
|
4881 else
|
|
4882 #endif
|
|
4883 c2 = p[1];
|
|
4884 if (c == c2)
|
323
|
4885 {
|
330
|
4886 /* Characters are identical, swap won't do anything. */
|
|
4887 sp->ts_state = STATE_SWAP3;
|
|
4888 break;
|
|
4889 }
|
|
4890 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
|
|
4891 {
|
|
4892 sp->ts_state = STATE_UNSWAP;
|
323
|
4893 ++depth;
|
330
|
4894 #ifdef FEAT_MBYTE
|
|
4895 if (has_mbyte)
|
|
4896 {
|
|
4897 fl = mb_char2len(c2);
|
|
4898 mch_memmove(p, p + n, fl);
|
|
4899 mb_char2bytes(c, p + fl);
|
|
4900 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
|
|
4901 }
|
|
4902 else
|
|
4903 #endif
|
|
4904 {
|
|
4905 p[0] = c2;
|
|
4906 p[1] = c;
|
|
4907 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
|
|
4908 }
|
323
|
4909 }
|
|
4910 else
|
|
4911 /* If this swap doesn't work then SWAP3 won't either. */
|
|
4912 sp->ts_state = STATE_REP_INI;
|
|
4913 break;
|
|
4914
|
330
|
4915 case STATE_UNSWAP:
|
|
4916 /* Undo the STATE_SWAP swap: "21" -> "12". */
|
|
4917 p = fword + sp->ts_fidx;
|
|
4918 #ifdef FEAT_MBYTE
|
|
4919 if (has_mbyte)
|
|
4920 {
|
|
4921 n = MB_BYTE2LEN(*p);
|
|
4922 c = mb_ptr2char(p + n);
|
|
4923 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
|
|
4924 mb_char2bytes(c, p);
|
|
4925 }
|
|
4926 else
|
|
4927 #endif
|
|
4928 {
|
|
4929 c = *p;
|
|
4930 *p = p[1];
|
|
4931 p[1] = c;
|
|
4932 }
|
|
4933 /*FALLTHROUGH*/
|
|
4934
|
|
4935 case STATE_SWAP3:
|
323
|
4936 /* Swap two bytes, skipping one: "123" -> "321". We change
|
330
|
4937 * "fword" here, it's changed back afterwards. */
|
|
4938 p = fword + sp->ts_fidx;
|
|
4939 #ifdef FEAT_MBYTE
|
|
4940 if (has_mbyte)
|
|
4941 {
|
|
4942 n = mb_ptr2len_check(p);
|
|
4943 c = mb_ptr2char(p);
|
|
4944 fl = mb_ptr2len_check(p + n);
|
|
4945 c2 = mb_ptr2char(p + n);
|
|
4946 c3 = mb_ptr2char(p + n + fl);
|
|
4947 }
|
|
4948 else
|
|
4949 #endif
|
323
|
4950 {
|
330
|
4951 c = *p;
|
|
4952 c2 = p[1];
|
|
4953 c3 = p[2];
|
|
4954 }
|
|
4955
|
|
4956 /* When characters are identical: "121" then SWAP3 result is
|
|
4957 * identical, ROT3L result is same as SWAP: "211", ROT3L
|
|
4958 * result is same as SWAP on next char: "112". Thus skip all
|
|
4959 * swapping. Also skip when c3 is NUL. */
|
|
4960 if (c == c3 || c3 == NUL)
|
|
4961 {
|
|
4962 sp->ts_state = STATE_REP_INI;
|
|
4963 break;
|
|
4964 }
|
|
4965 if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
4966 {
|
|
4967 sp->ts_state = STATE_UNSWAP3;
|
323
|
4968 ++depth;
|
330
|
4969 #ifdef FEAT_MBYTE
|
|
4970 if (has_mbyte)
|
|
4971 {
|
|
4972 tl = mb_char2len(c3);
|
|
4973 mch_memmove(p, p + n + fl, tl);
|
|
4974 mb_char2bytes(c2, p + tl);
|
|
4975 mb_char2bytes(c, p + fl + tl);
|
|
4976 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
|
|
4977 }
|
|
4978 else
|
|
4979 #endif
|
|
4980 {
|
|
4981 p[0] = p[2];
|
|
4982 p[2] = c;
|
|
4983 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
4984 }
|
323
|
4985 }
|
|
4986 else
|
|
4987 sp->ts_state = STATE_REP_INI;
|
|
4988 break;
|
|
4989
|
330
|
4990 case STATE_UNSWAP3:
|
|
4991 /* Undo STATE_SWAP3: "321" -> "123" */
|
|
4992 p = fword + sp->ts_fidx;
|
|
4993 #ifdef FEAT_MBYTE
|
|
4994 if (has_mbyte)
|
|
4995 {
|
|
4996 n = MB_BYTE2LEN(*p);
|
|
4997 c2 = mb_ptr2char(p + n);
|
|
4998 fl = MB_BYTE2LEN(p[n]);
|
|
4999 c = mb_ptr2char(p + n + fl);
|
|
5000 tl = MB_BYTE2LEN(p[n + fl]);
|
|
5001 mch_memmove(p + fl + tl, p, n);
|
|
5002 mb_char2bytes(c, p);
|
|
5003 mb_char2bytes(c2, p + tl);
|
|
5004 }
|
|
5005 else
|
|
5006 #endif
|
|
5007 {
|
|
5008 c = *p;
|
|
5009 *p = p[2];
|
|
5010 p[2] = c;
|
|
5011 }
|
|
5012 /*FALLTHROUGH*/
|
|
5013
|
323
|
5014 case STATE_ROT3L:
|
330
|
5015 /* Rotate three characters left: "123" -> "231". We change
|
|
5016 * "fword" here, it's changed back afterwards. */
|
323
|
5017 if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
5018 {
|
330
|
5019 sp->ts_state = STATE_UNROT3L;
|
323
|
5020 ++depth;
|
330
|
5021 p = fword + sp->ts_fidx;
|
|
5022 #ifdef FEAT_MBYTE
|
|
5023 if (has_mbyte)
|
|
5024 {
|
|
5025 n = mb_ptr2len_check(p);
|
|
5026 c = mb_ptr2char(p);
|
|
5027 fl = mb_ptr2len_check(p + n);
|
|
5028 fl += mb_ptr2len_check(p + n + fl);
|
|
5029 mch_memmove(p, p + n, fl);
|
|
5030 mb_char2bytes(c, p + fl);
|
|
5031 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
|
|
5032 }
|
|
5033 else
|
|
5034 #endif
|
|
5035 {
|
|
5036 c = *p;
|
|
5037 *p = p[1];
|
|
5038 p[1] = p[2];
|
|
5039 p[2] = c;
|
|
5040 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
5041 }
|
323
|
5042 }
|
|
5043 else
|
|
5044 sp->ts_state = STATE_REP_INI;
|
|
5045 break;
|
|
5046
|
330
|
5047 case STATE_UNROT3L:
|
|
5048 /* Undo STATE_ROT3L: "231" -> "123" */
|
|
5049 p = fword + sp->ts_fidx;
|
|
5050 #ifdef FEAT_MBYTE
|
|
5051 if (has_mbyte)
|
|
5052 {
|
|
5053 n = MB_BYTE2LEN(*p);
|
|
5054 n += MB_BYTE2LEN(p[n]);
|
|
5055 c = mb_ptr2char(p + n);
|
|
5056 tl = MB_BYTE2LEN(p[n]);
|
|
5057 mch_memmove(p + tl, p, n);
|
|
5058 mb_char2bytes(c, p);
|
|
5059 }
|
|
5060 else
|
|
5061 #endif
|
|
5062 {
|
|
5063 c = p[2];
|
|
5064 p[2] = p[1];
|
|
5065 p[1] = *p;
|
|
5066 *p = c;
|
|
5067 }
|
|
5068 /*FALLTHROUGH*/
|
|
5069
|
323
|
5070 case STATE_ROT3R:
|
|
5071 /* Rotate three bytes right: "123" -> "312". We change
|
330
|
5072 * "fword" here, it's changed back afterwards. */
|
323
|
5073 if (try_deeper(su, stack, depth, SCORE_SWAP3))
|
|
5074 {
|
330
|
5075 sp->ts_state = STATE_UNROT3R;
|
323
|
5076 ++depth;
|
330
|
5077 p = fword + sp->ts_fidx;
|
|
5078 #ifdef FEAT_MBYTE
|
|
5079 if (has_mbyte)
|
|
5080 {
|
|
5081 n = mb_ptr2len_check(p);
|
|
5082 n += mb_ptr2len_check(p + n);
|
|
5083 c = mb_ptr2char(p + n);
|
|
5084 tl = mb_ptr2len_check(p + n);
|
|
5085 mch_memmove(p + tl, p, n);
|
|
5086 mb_char2bytes(c, p);
|
|
5087 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
|
|
5088 }
|
|
5089 else
|
|
5090 #endif
|
|
5091 {
|
|
5092 c = p[2];
|
|
5093 p[2] = p[1];
|
|
5094 p[1] = *p;
|
|
5095 *p = c;
|
|
5096 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
|
|
5097 }
|
323
|
5098 }
|
|
5099 else
|
|
5100 sp->ts_state = STATE_REP_INI;
|
|
5101 break;
|
|
5102
|
330
|
5103 case STATE_UNROT3R:
|
323
|
5104 /* Undo STATE_ROT3R: "312" -> "123" */
|
330
|
5105 p = fword + sp->ts_fidx;
|
|
5106 #ifdef FEAT_MBYTE
|
|
5107 if (has_mbyte)
|
|
5108 {
|
|
5109 c = mb_ptr2char(p);
|
|
5110 tl = MB_BYTE2LEN(*p);
|
|
5111 n = MB_BYTE2LEN(p[tl]);
|
|
5112 n += MB_BYTE2LEN(p[tl + n]);
|
|
5113 mch_memmove(p, p + tl, n);
|
|
5114 mb_char2bytes(c, p + n);
|
|
5115 }
|
|
5116 else
|
|
5117 #endif
|
|
5118 {
|
|
5119 c = *p;
|
|
5120 *p = p[1];
|
|
5121 p[1] = p[2];
|
|
5122 p[2] = c;
|
|
5123 }
|
323
|
5124 /*FALLTHROUGH*/
|
|
5125
|
|
5126 case STATE_REP_INI:
|
|
5127 /* Check if matching with REP items from the .aff file would
|
|
5128 * work. Quickly skip if there are no REP items or the score
|
|
5129 * is going to be too high anyway. */
|
|
5130 gap = &lp->lp_slang->sl_rep;
|
|
5131 if (gap->ga_len == 0
|
|
5132 || sp->ts_score + SCORE_REP >= su->su_maxscore)
|
|
5133 {
|
|
5134 sp->ts_state = STATE_FINAL;
|
|
5135 break;
|
|
5136 }
|
|
5137
|
|
5138 /* Use the first byte to quickly find the first entry that
|
330
|
5139 * may match. If the index is -1 there is none. */
|
323
|
5140 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
|
|
5141 if (sp->ts_curi < 0)
|
|
5142 {
|
|
5143 sp->ts_state = STATE_FINAL;
|
|
5144 break;
|
|
5145 }
|
|
5146
|
|
5147 sp->ts_state = STATE_REP;
|
|
5148 /*FALLTHROUGH*/
|
|
5149
|
|
5150 case STATE_REP:
|
|
5151 /* Try matching with REP items from the .aff file. For each
|
330
|
5152 * match replace the characters and check if the resulting
|
|
5153 * word is valid. */
|
323
|
5154 p = fword + sp->ts_fidx;
|
|
5155
|
|
5156 gap = &lp->lp_slang->sl_rep;
|
|
5157 while (sp->ts_curi < gap->ga_len)
|
|
5158 {
|
|
5159 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
|
|
5160 if (*ftp->ft_from != *p)
|
|
5161 {
|
|
5162 /* past possible matching entries */
|
|
5163 sp->ts_curi = gap->ga_len;
|
|
5164 break;
|
|
5165 }
|
|
5166 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
|
|
5167 && try_deeper(su, stack, depth, SCORE_REP))
|
|
5168 {
|
|
5169 /* Need to undo this afterwards. */
|
|
5170 sp->ts_state = STATE_REP_UNDO;
|
|
5171
|
|
5172 /* Change the "from" to the "to" string. */
|
|
5173 ++depth;
|
|
5174 fl = STRLEN(ftp->ft_from);
|
|
5175 tl = STRLEN(ftp->ft_to);
|
|
5176 if (fl != tl)
|
|
5177 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
|
|
5178 mch_memmove(p, ftp->ft_to, tl);
|
|
5179 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
|
330
|
5180 #ifdef FEAT_MBYTE
|
|
5181 stack[depth].ts_tcharlen = 0;
|
|
5182 #endif
|
323
|
5183 break;
|
|
5184 }
|
|
5185 }
|
|
5186
|
|
5187 if (sp->ts_curi >= gap->ga_len)
|
|
5188 /* No (more) matches. */
|
|
5189 sp->ts_state = STATE_FINAL;
|
|
5190
|
|
5191 break;
|
|
5192
|
|
5193 case STATE_REP_UNDO:
|
|
5194 /* Undo a REP replacement and continue with the next one. */
|
|
5195 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
|
|
5196 + sp->ts_curi - 1;
|
|
5197 fl = STRLEN(ftp->ft_from);
|
|
5198 tl = STRLEN(ftp->ft_to);
|
|
5199 p = fword + sp->ts_fidx;
|
|
5200 if (fl != tl)
|
|
5201 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
|
|
5202 mch_memmove(p, ftp->ft_from, fl);
|
|
5203 sp->ts_state = STATE_REP;
|
|
5204 break;
|
|
5205
|
|
5206 default:
|
|
5207 /* Did all possible states at this level, go up one level. */
|
|
5208 --depth;
|
|
5209 }
|
|
5210
|
|
5211 line_breakcheck();
|
|
5212 }
|
|
5213 }
|
|
5214 }
|
|
5215
|
|
5216 /*
|
|
5217 * Try going one level deeper in the tree.
|
|
5218 */
|
|
5219 static int
|
|
5220 try_deeper(su, stack, depth, score_add)
|
|
5221 suginfo_T *su;
|
|
5222 trystate_T *stack;
|
|
5223 int depth;
|
|
5224 int score_add;
|
|
5225 {
|
|
5226 int newscore;
|
|
5227
|
|
5228 /* Refuse to go deeper if the scrore is getting too big. */
|
|
5229 newscore = stack[depth].ts_score + score_add;
|
|
5230 if (newscore >= su->su_maxscore)
|
|
5231 return FALSE;
|
|
5232
|
330
|
5233 stack[depth + 1] = stack[depth];
|
323
|
5234 stack[depth + 1].ts_state = STATE_START;
|
|
5235 stack[depth + 1].ts_score = newscore;
|
|
5236 stack[depth + 1].ts_curi = 1; /* start just after length byte */
|
|
5237 return TRUE;
|
|
5238 }
|
|
5239
|
|
5240 /*
|
|
5241 * "fword" is a good word with case folded. Find the matching keep-case
|
|
5242 * words and put it in "kword".
|
|
5243 * Theoretically there could be several keep-case words that result in the
|
|
5244 * same case-folded word, but we only find one...
|
|
5245 */
|
|
5246 static void
|
|
5247 find_keepcap_word(slang, fword, kword)
|
|
5248 slang_T *slang;
|
|
5249 char_u *fword;
|
|
5250 char_u *kword;
|
|
5251 {
|
|
5252 char_u uword[MAXWLEN]; /* "fword" in upper-case */
|
|
5253 int depth;
|
324
|
5254 idx_T tryidx;
|
323
|
5255
|
|
5256 /* The following arrays are used at each depth in the tree. */
|
324
|
5257 idx_T arridx[MAXWLEN];
|
323
|
5258 int round[MAXWLEN];
|
|
5259 int fwordidx[MAXWLEN];
|
|
5260 int uwordidx[MAXWLEN];
|
|
5261 int kwordlen[MAXWLEN];
|
|
5262
|
|
5263 int flen, ulen;
|
|
5264 int l;
|
|
5265 int len;
|
|
5266 int c;
|
324
|
5267 idx_T lo, hi, m;
|
323
|
5268 char_u *p;
|
|
5269 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
|
324
|
5270 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
|
323
|
5271
|
|
5272 if (byts == NULL)
|
|
5273 {
|
|
5274 /* array is empty: "cannot happen" */
|
|
5275 *kword = NUL;
|
|
5276 return;
|
|
5277 }
|
|
5278
|
|
5279 /* Make an all-cap version of "fword". */
|
|
5280 allcap_copy(fword, uword);
|
|
5281
|
|
5282 /*
|
|
5283 * Each character needs to be tried both case-folded and upper-case.
|
|
5284 * All this gets very complicated if we keep in mind that changing case
|
|
5285 * may change the byte length of a multi-byte character...
|
|
5286 */
|
|
5287 depth = 0;
|
|
5288 arridx[0] = 0;
|
|
5289 round[0] = 0;
|
|
5290 fwordidx[0] = 0;
|
|
5291 uwordidx[0] = 0;
|
|
5292 kwordlen[0] = 0;
|
|
5293 while (depth >= 0)
|
|
5294 {
|
|
5295 if (fword[fwordidx[depth]] == NUL)
|
|
5296 {
|
|
5297 /* We are at the end of "fword". If the tree allows a word to end
|
|
5298 * here we have found a match. */
|
|
5299 if (byts[arridx[depth] + 1] == 0)
|
|
5300 {
|
|
5301 kword[kwordlen[depth]] = NUL;
|
|
5302 return;
|
|
5303 }
|
|
5304
|
|
5305 /* kword is getting too long, continue one level up */
|
|
5306 --depth;
|
|
5307 }
|
|
5308 else if (++round[depth] > 2)
|
|
5309 {
|
|
5310 /* tried both fold-case and upper-case character, continue one
|
|
5311 * level up */
|
|
5312 --depth;
|
|
5313 }
|
|
5314 else
|
|
5315 {
|
|
5316 /*
|
|
5317 * round[depth] == 1: Try using the folded-case character.
|
|
5318 * round[depth] == 2: Try using the upper-case character.
|
|
5319 */
|
|
5320 #ifdef FEAT_MBYTE
|
|
5321 if (has_mbyte)
|
|
5322 {
|
|
5323 flen = mb_ptr2len_check(fword + fwordidx[depth]);
|
|
5324 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
|
|
5325 }
|
|
5326 else
|
|
5327 #endif
|
|
5328 ulen = flen = 1;
|
|
5329 if (round[depth] == 1)
|
|
5330 {
|
|
5331 p = fword + fwordidx[depth];
|
|
5332 l = flen;
|
|
5333 }
|
|
5334 else
|
|
5335 {
|
|
5336 p = uword + uwordidx[depth];
|
|
5337 l = ulen;
|
|
5338 }
|
|
5339
|
|
5340 for (tryidx = arridx[depth]; l > 0; --l)
|
|
5341 {
|
|
5342 /* Perform a binary search in the list of accepted bytes. */
|
|
5343 len = byts[tryidx++];
|
|
5344 c = *p++;
|
|
5345 lo = tryidx;
|
|
5346 hi = tryidx + len - 1;
|
|
5347 while (lo < hi)
|
|
5348 {
|
|
5349 m = (lo + hi) / 2;
|
|
5350 if (byts[m] > c)
|
|
5351 hi = m - 1;
|
|
5352 else if (byts[m] < c)
|
|
5353 lo = m + 1;
|
|
5354 else
|
|
5355 {
|
|
5356 lo = hi = m;
|
|
5357 break;
|
|
5358 }
|
|
5359 }
|
|
5360
|
|
5361 /* Stop if there is no matching byte. */
|
|
5362 if (hi < lo || byts[lo] != c)
|
|
5363 break;
|
|
5364
|
|
5365 /* Continue at the child (if there is one). */
|
|
5366 tryidx = idxs[lo];
|
|
5367 }
|
|
5368
|
|
5369 if (l == 0)
|
|
5370 {
|
|
5371 /*
|
|
5372 * Found the matching char. Copy it to "kword" and go a
|
|
5373 * level deeper.
|
|
5374 */
|
|
5375 if (round[depth] == 1)
|
|
5376 {
|
|
5377 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
|
|
5378 flen);
|
|
5379 kwordlen[depth + 1] = kwordlen[depth] + flen;
|
|
5380 }
|
|
5381 else
|
|
5382 {
|
|
5383 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
|
|
5384 ulen);
|
|
5385 kwordlen[depth + 1] = kwordlen[depth] + ulen;
|
|
5386 }
|
|
5387 fwordidx[depth + 1] = fwordidx[depth] + flen;
|
|
5388 uwordidx[depth + 1] = uwordidx[depth] + ulen;
|
|
5389
|
|
5390 ++depth;
|
|
5391 arridx[depth] = tryidx;
|
|
5392 round[depth] = 0;
|
|
5393 }
|
|
5394 }
|
|
5395 }
|
|
5396
|
|
5397 /* Didn't find it: "cannot happen". */
|
|
5398 *kword = NUL;
|
|
5399 }
|
|
5400
|
|
5401 /*
|
|
5402 * Find suggestions by comparing the word in a sound-a-like form.
|
|
5403 */
|
|
5404 static void
|
|
5405 spell_try_soundalike(su)
|
|
5406 suginfo_T *su;
|
|
5407 {
|
|
5408 char_u salword[MAXWLEN];
|
|
5409 char_u tword[MAXWLEN];
|
|
5410 char_u tfword[MAXWLEN];
|
|
5411 char_u tsalword[MAXWLEN];
|
324
|
5412 idx_T arridx[MAXWLEN];
|
323
|
5413 int curi[MAXWLEN];
|
|
5414 langp_T *lp;
|
|
5415 char_u *byts;
|
324
|
5416 idx_T *idxs;
|
323
|
5417 int depth;
|
|
5418 int c;
|
324
|
5419 idx_T n;
|
323
|
5420 int round;
|
|
5421 int flags;
|
324
|
5422 int score, sound_score;
|
|
5423 char_u *bp, *sp;
|
323
|
5424
|
|
5425 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
5426 lp->lp_slang != NULL; ++lp)
|
|
5427 {
|
|
5428 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
5429 {
|
|
5430 /* soundfold the bad word */
|
|
5431 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
|
|
5432
|
|
5433 /*
|
|
5434 * Go through the whole tree, soundfold each word and compare.
|
|
5435 * round 1: use the case-folded tree.
|
|
5436 * round 2: use the keep-case tree.
|
|
5437 */
|
|
5438 for (round = 1; round <= 2; ++round)
|
|
5439 {
|
|
5440 if (round == 1)
|
|
5441 {
|
|
5442 byts = lp->lp_slang->sl_fbyts;
|
|
5443 idxs = lp->lp_slang->sl_fidxs;
|
|
5444 }
|
|
5445 else
|
|
5446 {
|
|
5447 byts = lp->lp_slang->sl_kbyts;
|
|
5448 idxs = lp->lp_slang->sl_kidxs;
|
|
5449 }
|
|
5450
|
|
5451 depth = 0;
|
|
5452 arridx[0] = 0;
|
|
5453 curi[0] = 1;
|
|
5454 while (depth >= 0 && !got_int)
|
|
5455 {
|
|
5456 if (curi[depth] > byts[arridx[depth]])
|
|
5457 /* Done all bytes at this node, go up one level. */
|
|
5458 --depth;
|
|
5459 else
|
|
5460 {
|
|
5461 /* Do one more byte at this node. */
|
|
5462 n = arridx[depth] + curi[depth];
|
|
5463 ++curi[depth];
|
|
5464 c = byts[n];
|
|
5465 if (c == 0)
|
|
5466 {
|
|
5467 /* End of word, deal with the word. */
|
324
|
5468 flags = (int)idxs[n];
|
323
|
5469 if (round == 2 || (flags & WF_KEEPCAP) == 0)
|
|
5470 {
|
|
5471 tword[depth] = NUL;
|
|
5472 if (round == 1)
|
|
5473 spell_soundfold(lp->lp_slang,
|
|
5474 tword, tsalword);
|
|
5475 else
|
|
5476 {
|
|
5477 /* In keep-case tree need to case-fold the
|
|
5478 * word. */
|
|
5479 (void)spell_casefold(tword, depth,
|
|
5480 tfword, MAXWLEN);
|
|
5481 spell_soundfold(lp->lp_slang,
|
|
5482 tfword, tsalword);
|
|
5483 }
|
|
5484
|
324
|
5485 /*
|
|
5486 * Accept the word if the sound-folded words
|
|
5487 * are (almost) equal.
|
|
5488 */
|
|
5489 for (bp = salword, sp = tsalword; *bp == *sp;
|
|
5490 ++bp, ++sp)
|
|
5491 if (*bp == NUL)
|
|
5492 break;
|
|
5493
|
|
5494 if (*bp == *sp)
|
|
5495 /* equal */
|
|
5496 sound_score = 0;
|
|
5497 else if (*bp != NUL && bp[1] != NUL
|
|
5498 && *bp == sp[1] && bp[1] == *sp
|
|
5499 && STRCMP(bp + 2, sp + 2) == 0)
|
|
5500 /* swap two bytes */
|
|
5501 sound_score = SCORE_SWAP;
|
|
5502 else if (STRCMP(bp + 1, sp) == 0)
|
|
5503 /* delete byte */
|
|
5504 sound_score = SCORE_DEL;
|
|
5505 else if (STRCMP(bp, sp + 1) == 0)
|
|
5506 /* insert byte */
|
|
5507 sound_score = SCORE_INS;
|
|
5508 else if (STRCMP(bp + 1, sp + 1) == 0)
|
|
5509 /* skip one byte */
|
|
5510 sound_score = SCORE_SUBST;
|
|
5511 else
|
|
5512 /* not equal or similar */
|
|
5513 sound_score = SCORE_MAXMAX;
|
|
5514
|
|
5515 if (sound_score < SCORE_MAXMAX)
|
323
|
5516 {
|
324
|
5517 char_u cword[MAXWLEN];
|
|
5518 char_u *p;
|
|
5519
|
323
|
5520 if (round == 1 && flags != 0)
|
|
5521 {
|
324
|
5522 /* Need to fix case according to
|
|
5523 * "flags". */
|
323
|
5524 make_case_word(tword, cword, flags);
|
324
|
5525 p = cword;
|
323
|
5526 }
|
|
5527 else
|
324
|
5528 p = tword;
|
|
5529
|
|
5530 /* Compute the score. */
|
|
5531 score = spell_edit_score(su->su_badword, p);
|
|
5532 #ifdef RESCORE
|
|
5533 /* give a bonus for the good word sounding
|
|
5534 * the same as the bad word */
|
|
5535 add_suggestion(su, tword,
|
|
5536 RESCORE(score, sound_score),
|
|
5537 TRUE);
|
|
5538 #else
|
|
5539 add_suggestion(su, tword,
|
|
5540 score + sound_score);
|
|
5541 #endif
|
323
|
5542 }
|
|
5543 }
|
|
5544
|
|
5545 /* Skip over other NUL bytes. */
|
|
5546 while (byts[n + 1] == 0)
|
|
5547 {
|
|
5548 ++n;
|
|
5549 ++curi[depth];
|
|
5550 }
|
|
5551 }
|
|
5552 else
|
|
5553 {
|
|
5554 /* Normal char, go one level deeper. */
|
|
5555 tword[depth++] = c;
|
|
5556 arridx[depth] = idxs[n];
|
|
5557 curi[depth] = 1;
|
|
5558 }
|
|
5559 }
|
324
|
5560
|
|
5561 line_breakcheck();
|
323
|
5562 }
|
|
5563 }
|
|
5564 }
|
|
5565 }
|
|
5566 }
|
|
5567
|
|
5568 /*
|
324
|
5569 * Copy "fword" to "cword", fixing case according to "flags".
|
323
|
5570 */
|
|
5571 static void
|
|
5572 make_case_word(fword, cword, flags)
|
|
5573 char_u *fword;
|
|
5574 char_u *cword;
|
|
5575 int flags;
|
|
5576 {
|
|
5577 if (flags & WF_ALLCAP)
|
|
5578 /* Make it all upper-case */
|
|
5579 allcap_copy(fword, cword);
|
|
5580 else if (flags & WF_ONECAP)
|
|
5581 /* Make the first letter upper-case */
|
324
|
5582 onecap_copy(fword, cword, TRUE);
|
323
|
5583 else
|
|
5584 /* Use goodword as-is. */
|
|
5585 STRCPY(cword, fword);
|
|
5586 }
|
|
5587
|
330
|
5588 /*
|
|
5589 * Use map string "map" for languages "lp".
|
|
5590 */
|
|
5591 static void
|
|
5592 set_map_str(lp, map)
|
|
5593 slang_T *lp;
|
|
5594 char_u *map;
|
|
5595 {
|
|
5596 char_u *p;
|
|
5597 int headc = 0;
|
|
5598 int c;
|
|
5599 int i;
|
|
5600
|
|
5601 if (*map == NUL)
|
|
5602 {
|
|
5603 lp->sl_has_map = FALSE;
|
|
5604 return;
|
|
5605 }
|
|
5606 lp->sl_has_map = TRUE;
|
|
5607
|
|
5608 /* Init the array and hash table empty. */
|
|
5609 for (i = 0; i < 256; ++i)
|
|
5610 lp->sl_map_array[i] = 0;
|
|
5611 #ifdef FEAT_MBYTE
|
|
5612 hash_init(&lp->sl_map_hash);
|
|
5613 #endif
|
|
5614
|
|
5615 /*
|
|
5616 * The similar characters are stored separated with slashes:
|
|
5617 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
|
|
5618 * before the same slash. For characters above 255 sl_map_hash is used.
|
|
5619 */
|
|
5620 for (p = map; *p != NUL; )
|
|
5621 {
|
|
5622 #ifdef FEAT_MBYTE
|
|
5623 c = mb_ptr2char_adv(&p);
|
|
5624 #else
|
|
5625 c = *p++;
|
|
5626 #endif
|
|
5627 if (c == '/')
|
|
5628 headc = 0;
|
|
5629 else
|
|
5630 {
|
|
5631 if (headc == 0)
|
|
5632 headc = c;
|
|
5633
|
|
5634 #ifdef FEAT_MBYTE
|
|
5635 /* Characters above 255 don't fit in sl_map_array[], put them in
|
|
5636 * the hash table. Each entry is the char, a NUL the headchar and
|
|
5637 * a NUL. */
|
|
5638 if (c >= 256)
|
|
5639 {
|
|
5640 int cl = mb_char2len(c);
|
|
5641 int headcl = mb_char2len(headc);
|
|
5642 char_u *b;
|
|
5643 hash_T hash;
|
|
5644 hashitem_T *hi;
|
|
5645
|
|
5646 b = alloc((unsigned)(cl + headcl + 2));
|
|
5647 if (b == NULL)
|
|
5648 return;
|
|
5649 mb_char2bytes(c, b);
|
|
5650 b[cl] = NUL;
|
|
5651 mb_char2bytes(headc, b + cl + 1);
|
|
5652 b[cl + 1 + headcl] = NUL;
|
|
5653 hash = hash_hash(b);
|
|
5654 hi = hash_lookup(&lp->sl_map_hash, b, hash);
|
|
5655 if (HASHITEM_EMPTY(hi))
|
|
5656 hash_add_item(&lp->sl_map_hash, hi, b, hash);
|
|
5657 else
|
|
5658 {
|
|
5659 /* This should have been checked when generating the .spl
|
|
5660 * file. */
|
|
5661 EMSG(_("E999: duplicate char in MAP entry"));
|
|
5662 vim_free(b);
|
|
5663 }
|
|
5664 }
|
|
5665 else
|
|
5666 #endif
|
|
5667 lp->sl_map_array[c] = headc;
|
|
5668 }
|
|
5669 }
|
|
5670 }
|
|
5671
|
323
|
5672 /*
|
|
5673 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
|
|
5674 * lines in the .aff file.
|
|
5675 */
|
|
5676 static int
|
|
5677 similar_chars(slang, c1, c2)
|
|
5678 slang_T *slang;
|
|
5679 int c1;
|
|
5680 int c2;
|
|
5681 {
|
330
|
5682 int m1, m2;
|
|
5683 #ifdef FEAT_MBYTE
|
|
5684 char_u buf[MB_MAXBYTES];
|
|
5685 hashitem_T *hi;
|
|
5686
|
|
5687 if (c1 >= 256)
|
|
5688 {
|
|
5689 buf[mb_char2bytes(c1, buf)] = 0;
|
|
5690 hi = hash_find(&slang->sl_map_hash, buf);
|
|
5691 if (HASHITEM_EMPTY(hi))
|
|
5692 m1 = 0;
|
|
5693 else
|
|
5694 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
|
|
5695 }
|
|
5696 else
|
|
5697 #endif
|
|
5698 m1 = slang->sl_map_array[c1];
|
|
5699 if (m1 == 0)
|
323
|
5700 return FALSE;
|
330
|
5701
|
|
5702
|
|
5703 #ifdef FEAT_MBYTE
|
|
5704 if (c2 >= 256)
|
|
5705 {
|
|
5706 buf[mb_char2bytes(c2, buf)] = 0;
|
|
5707 hi = hash_find(&slang->sl_map_hash, buf);
|
|
5708 if (HASHITEM_EMPTY(hi))
|
|
5709 m2 = 0;
|
|
5710 else
|
|
5711 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
|
|
5712 }
|
|
5713 else
|
|
5714 #endif
|
|
5715 m2 = slang->sl_map_array[c2];
|
|
5716
|
|
5717 return m1 == m2;
|
323
|
5718 }
|
|
5719
|
|
5720 /*
|
|
5721 * Add a suggestion to the list of suggestions.
|
|
5722 * Do not add a duplicate suggestion or suggestions with a bad score.
|
|
5723 * When "use_score" is not zero it's used, otherwise the score is computed
|
|
5724 * with spell_edit_score().
|
|
5725 */
|
|
5726 static void
|
324
|
5727 add_suggestion(su, goodword, score
|
|
5728 #ifdef RESCORE
|
|
5729 , had_bonus
|
|
5730 #endif
|
|
5731 )
|
323
|
5732 suginfo_T *su;
|
|
5733 char_u *goodword;
|
324
|
5734 int score;
|
|
5735 #ifdef RESCORE
|
|
5736 int had_bonus; /* set st_had_bonus */
|
|
5737 #endif
|
323
|
5738 {
|
|
5739 suggest_T *stp;
|
|
5740 int i;
|
|
5741 #ifdef SOUNDFOLD_SCORE
|
|
5742 char_u fword[MAXWLEN];
|
|
5743 char_u salword[MAXWLEN];
|
|
5744 #endif
|
|
5745
|
|
5746 /* Check that the word wasn't banned. */
|
|
5747 if (was_banned(su, goodword))
|
|
5748 return;
|
|
5749
|
|
5750 if (score <= su->su_maxscore)
|
|
5751 {
|
|
5752 #ifdef SOUNDFOLD_SCORE
|
|
5753 /* Add to the score when the word sounds differently.
|
|
5754 * This is slow... */
|
|
5755 if (su->su_slang->sl_sal.ga_len > 0)
|
324
|
5756 score += spell_sound_score(su->su_slang, fword, su->su_salword);
|
323
|
5757 #endif
|
|
5758
|
|
5759 /* Check if the word is already there. */
|
|
5760 stp = &SUG(su, 0);
|
|
5761 for (i = su->su_ga.ga_len - 1; i >= 0; --i)
|
|
5762 if (STRCMP(stp[i].st_word, goodword) == 0)
|
|
5763 {
|
|
5764 /* Found it. Remember the lowest score. */
|
|
5765 if (stp[i].st_score > score)
|
324
|
5766 {
|
323
|
5767 stp[i].st_score = score;
|
324
|
5768 #ifdef RESCORE
|
|
5769 stp[i].st_had_bonus = had_bonus;
|
|
5770 #endif
|
|
5771 }
|
323
|
5772 break;
|
|
5773 }
|
|
5774
|
|
5775 if (i < 0 && ga_grow(&su->su_ga, 1) == OK)
|
|
5776 {
|
|
5777 /* Add a suggestion. */
|
|
5778 stp = &SUG(su, su->su_ga.ga_len);
|
|
5779 stp->st_word = vim_strsave(goodword);
|
|
5780 if (stp->st_word != NULL)
|
|
5781 {
|
|
5782 stp->st_score = score;
|
324
|
5783 #ifdef RESCORE
|
|
5784 stp->st_had_bonus = had_bonus;
|
|
5785 #endif
|
323
|
5786 stp->st_orglen = su->su_badlen;
|
|
5787 ++su->su_ga.ga_len;
|
|
5788
|
|
5789 /* If we have too many suggestions now, sort the list and keep
|
|
5790 * the best suggestions. */
|
324
|
5791 if (su->su_ga.ga_len > SUG_MAX_COUNT)
|
|
5792 cleanup_suggestions(su, SUG_CLEAN_COUNT);
|
323
|
5793 }
|
|
5794 }
|
|
5795 }
|
|
5796 }
|
|
5797
|
|
5798 /*
|
|
5799 * Add a word to be banned.
|
|
5800 */
|
|
5801 static void
|
|
5802 add_banned(su, word)
|
|
5803 suginfo_T *su;
|
|
5804 char_u *word;
|
|
5805 {
|
|
5806 char_u *s = vim_strsave(word);
|
|
5807 hash_T hash;
|
|
5808 hashitem_T *hi;
|
|
5809
|
|
5810 if (s != NULL)
|
|
5811 {
|
|
5812 hash = hash_hash(s);
|
|
5813 hi = hash_lookup(&su->su_banned, s, hash);
|
|
5814 if (HASHITEM_EMPTY(hi))
|
|
5815 hash_add_item(&su->su_banned, hi, s, hash);
|
|
5816 }
|
|
5817 }
|
|
5818
|
|
5819 /*
|
|
5820 * Return TRUE if a word appears in the list of banned words.
|
|
5821 */
|
|
5822 static int
|
|
5823 was_banned(su, word)
|
|
5824 suginfo_T *su;
|
|
5825 char_u *word;
|
|
5826 {
|
324
|
5827 hashitem_T *hi = hash_find(&su->su_banned, word);
|
|
5828
|
|
5829 return !HASHITEM_EMPTY(hi);
|
323
|
5830 }
|
|
5831
|
|
5832 /*
|
|
5833 * Free the banned words in "su".
|
|
5834 */
|
|
5835 static void
|
|
5836 free_banned(su)
|
|
5837 suginfo_T *su;
|
|
5838 {
|
|
5839 int todo;
|
|
5840 hashitem_T *hi;
|
|
5841
|
|
5842 todo = su->su_banned.ht_used;
|
|
5843 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
|
|
5844 {
|
|
5845 if (!HASHITEM_EMPTY(hi))
|
|
5846 {
|
|
5847 vim_free(hi->hi_key);
|
|
5848 --todo;
|
|
5849 }
|
|
5850 }
|
|
5851 hash_clear(&su->su_banned);
|
|
5852 }
|
|
5853
|
324
|
5854 #ifdef RESCORE
|
|
5855 /*
|
|
5856 * Recompute the score if sound-folding is possible. This is slow,
|
|
5857 * thus only done for the final results.
|
|
5858 */
|
|
5859 static void
|
|
5860 rescore_suggestions(su)
|
|
5861 suginfo_T *su;
|
|
5862 {
|
|
5863 langp_T *lp;
|
|
5864 suggest_T *stp;
|
|
5865 char_u sal_badword[MAXWLEN];
|
|
5866 int score;
|
|
5867 int i;
|
|
5868
|
|
5869 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
|
|
5870 lp->lp_slang != NULL; ++lp)
|
|
5871 {
|
|
5872 if (lp->lp_slang->sl_sal.ga_len > 0)
|
|
5873 {
|
|
5874 /* soundfold the bad word */
|
|
5875 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
|
|
5876
|
|
5877 for (i = 0; i < su->su_ga.ga_len; ++i)
|
|
5878 {
|
|
5879 stp = &SUG(su, i);
|
|
5880 if (!stp->st_had_bonus)
|
|
5881 {
|
|
5882 score = spell_sound_score(lp->lp_slang, stp->st_word,
|
|
5883 sal_badword);
|
|
5884 stp->st_score = RESCORE(stp->st_score, score);
|
|
5885 }
|
|
5886 }
|
|
5887 break;
|
|
5888 }
|
|
5889 }
|
|
5890 }
|
|
5891 #endif
|
|
5892
|
323
|
5893 static int
|
|
5894 #ifdef __BORLANDC__
|
|
5895 _RTLENTRYF
|
|
5896 #endif
|
|
5897 sug_compare __ARGS((const void *s1, const void *s2));
|
|
5898
|
|
5899 /*
|
|
5900 * Function given to qsort() to sort the suggestions on st_score.
|
|
5901 */
|
|
5902 static int
|
|
5903 #ifdef __BORLANDC__
|
|
5904 _RTLENTRYF
|
|
5905 #endif
|
|
5906 sug_compare(s1, s2)
|
|
5907 const void *s1;
|
|
5908 const void *s2;
|
|
5909 {
|
|
5910 suggest_T *p1 = (suggest_T *)s1;
|
|
5911 suggest_T *p2 = (suggest_T *)s2;
|
|
5912
|
|
5913 return p1->st_score - p2->st_score;
|
|
5914 }
|
|
5915
|
|
5916 /*
|
|
5917 * Cleanup the suggestions:
|
|
5918 * - Sort on score.
|
|
5919 * - Remove words that won't be displayed.
|
|
5920 */
|
|
5921 static void
|
324
|
5922 cleanup_suggestions(su, keep)
|
323
|
5923 suginfo_T *su;
|
324
|
5924 int keep; /* nr of suggestions to keep */
|
323
|
5925 {
|
|
5926 suggest_T *stp = &SUG(su, 0);
|
|
5927 int i;
|
|
5928
|
|
5929 /* Sort the list. */
|
|
5930 qsort(su->su_ga.ga_data, (size_t)su->su_ga.ga_len,
|
|
5931 sizeof(suggest_T), sug_compare);
|
|
5932
|
|
5933 /* Truncate the list to the number of suggestions that will be displayed. */
|
324
|
5934 if (su->su_ga.ga_len > keep)
|
323
|
5935 {
|
324
|
5936 for (i = keep; i < su->su_ga.ga_len; ++i)
|
323
|
5937 vim_free(stp[i].st_word);
|
324
|
5938 su->su_ga.ga_len = keep;
|
|
5939 su->su_maxscore = stp[keep - 1].st_score;
|
323
|
5940 }
|
|
5941 }
|
|
5942
|
|
5943 /*
|
|
5944 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
|
|
5945 */
|
|
5946 static void
|
|
5947 spell_soundfold(slang, inword, res)
|
|
5948 slang_T *slang;
|
|
5949 char_u *inword;
|
|
5950 char_u *res;
|
|
5951 {
|
|
5952 fromto_T *ftp;
|
|
5953 char_u word[MAXWLEN];
|
|
5954 #ifdef FEAT_MBYTE
|
|
5955 int l;
|
324
|
5956 int found_mbyte = FALSE;
|
323
|
5957 #endif
|
|
5958 char_u *s;
|
|
5959 char_u *t;
|
|
5960 int i, j, z;
|
|
5961 int n, k = 0;
|
|
5962 int z0;
|
|
5963 int k0;
|
|
5964 int n0;
|
|
5965 int c;
|
|
5966 int pri;
|
|
5967 int p0 = -333;
|
|
5968 int c0;
|
|
5969
|
324
|
5970 /* Remove accents, if wanted. We actually remove all non-word characters.
|
|
5971 * But keep white space. */
|
323
|
5972 if (slang->sl_rem_accents)
|
|
5973 {
|
|
5974 t = word;
|
|
5975 for (s = inword; *s != NUL; )
|
|
5976 {
|
324
|
5977 if (vim_iswhite(*s))
|
|
5978 *t++ = *s++;
|
323
|
5979 #ifdef FEAT_MBYTE
|
324
|
5980 else if (has_mbyte)
|
323
|
5981 {
|
|
5982 l = mb_ptr2len_check(s);
|
|
5983 if (SPELL_ISWORDP(s))
|
|
5984 {
|
|
5985 mch_memmove(t, s, l);
|
|
5986 t += l;
|
324
|
5987 if (l > 1)
|
|
5988 found_mbyte = TRUE;
|
323
|
5989 }
|
|
5990 s += l;
|
|
5991 }
|
324
|
5992 #endif
|
323
|
5993 else
|
|
5994 {
|
|
5995 if (SPELL_ISWORDP(s))
|
|
5996 *t++ = *s;
|
|
5997 ++s;
|
|
5998 }
|
|
5999 }
|
|
6000 *t = NUL;
|
|
6001 }
|
|
6002 else
|
324
|
6003 {
|
|
6004 #ifdef FEAT_MBYTE
|
|
6005 if (has_mbyte)
|
|
6006 for (s = inword; *s != NUL; s += l)
|
|
6007 if ((l = mb_ptr2len_check(s)) > 1)
|
|
6008 {
|
|
6009 found_mbyte = TRUE;
|
|
6010 break;
|
|
6011 }
|
|
6012 #endif
|
323
|
6013 STRCPY(word, inword);
|
324
|
6014 }
|
|
6015
|
|
6016 #ifdef FEAT_MBYTE
|
|
6017 /* If there are multi-byte characters in the word return it as-is, because
|
|
6018 * the following won't work. */
|
|
6019 if (found_mbyte)
|
|
6020 {
|
|
6021 STRCPY(res, word);
|
|
6022 return;
|
|
6023 }
|
|
6024 #endif
|
323
|
6025
|
|
6026 ftp = (fromto_T *)slang->sl_sal.ga_data;
|
|
6027
|
|
6028 /*
|
|
6029 * This comes from Aspell phonet.cpp. Converted from C++ to C.
|
324
|
6030 * Changed to keep spaces.
|
323
|
6031 * TODO: support for multi-byte chars.
|
|
6032 */
|
|
6033 i = j = z = 0;
|
|
6034 while ((c = word[i]) != NUL)
|
|
6035 {
|
|
6036 n = slang->sl_sal_first[c];
|
|
6037 z0 = 0;
|
|
6038
|
|
6039 if (n >= 0)
|
|
6040 {
|
|
6041 /* check all rules for the same letter */
|
|
6042 while (ftp[n].ft_from[0] == c)
|
|
6043 {
|
|
6044 /* check whole string */
|
|
6045 k = 1; /* number of found letters */
|
|
6046 pri = 5; /* default priority */
|
|
6047 s = ftp[n].ft_from;
|
|
6048 s++; /* important for (see below) "*(s-1)" */
|
|
6049
|
|
6050 /* Skip over normal letters that match with the word. */
|
|
6051 while (*s != NUL && word[i + k] == *s
|
|
6052 && !vim_isdigit(*s) && strchr("(-<^$", *s) == NULL)
|
|
6053 {
|
|
6054 k++;
|
|
6055 s++;
|
|
6056 }
|
|
6057
|
|
6058 if (*s == '(')
|
|
6059 {
|
|
6060 /* check alternate letters in "(..)" */
|
|
6061 for (t = s + 1; *t != ')' && *t != NUL; ++t)
|
|
6062 if (*t == word[i + k])
|
|
6063 {
|
|
6064 /* match */
|
|
6065 ++k;
|
|
6066 for (s = t + 1; *s != NUL; ++s)
|
|
6067 if (*s == ')')
|
|
6068 {
|
|
6069 ++s;
|
|
6070 break;
|
|
6071 }
|
|
6072 break;
|
|
6073 }
|
|
6074 }
|
|
6075
|
|
6076 p0 = *s;
|
|
6077 k0 = k;
|
|
6078 while (*s == '-' && k > 1)
|
|
6079 {
|
|
6080 k--;
|
|
6081 s++;
|
|
6082 }
|
|
6083 if (*s == '<')
|
|
6084 s++;
|
|
6085 if (vim_isdigit(*s))
|
|
6086 {
|
|
6087 /* determine priority */
|
|
6088 pri = *s - '0';
|
|
6089 s++;
|
|
6090 }
|
|
6091 if (*s == '^' && *(s + 1) == '^')
|
|
6092 s++;
|
|
6093
|
|
6094 if (*s == NUL
|
|
6095 || (*s == '^'
|
324
|
6096 && (i == 0 || !(word[i - 1] == ' '
|
|
6097 || SPELL_ISWORDP(word + i - 1)))
|
323
|
6098 && (*(s + 1) != '$'
|
|
6099 || (!SPELL_ISWORDP(word + i + k0))))
|
|
6100 || (*s == '$' && i > 0
|
|
6101 && SPELL_ISWORDP(word + i - 1)
|
|
6102 && (!SPELL_ISWORDP(word + i + k0))))
|
|
6103 {
|
|
6104 /* search for followup rules, if: */
|
|
6105 /* followup and k > 1 and NO '-' in searchstring */
|
|
6106 c0 = word[i + k - 1];
|
|
6107 n0 = slang->sl_sal_first[c0];
|
|
6108
|
|
6109 if (slang->sl_followup && k > 1 && n0 >= 0
|
|
6110 && p0 != '-' && word[i + k] != NUL)
|
|
6111 {
|
|
6112 /* test follow-up rule for "word[i + k]" */
|
|
6113 while (ftp[n0].ft_from[0] == c0)
|
|
6114 {
|
|
6115
|
|
6116 /* check whole string */
|
|
6117 k0 = k;
|
|
6118 p0 = 5;
|
|
6119 s = ftp[n0].ft_from;
|
|
6120 s++;
|
|
6121 while (*s != NUL && word[i+k0] == *s
|
|
6122 && !vim_isdigit(*s)
|
|
6123 && strchr("(-<^$",*s) == NULL)
|
|
6124 {
|
|
6125 k0++;
|
|
6126 s++;
|
|
6127 }
|
|
6128 if (*s == '(')
|
|
6129 {
|
|
6130 /* check alternate letters in "(..)" */
|
|
6131 for (t = s + 1; *t != ')' && *t != NUL; ++t)
|
|
6132 if (*t == word[i + k0])
|
|
6133 {
|
|
6134 /* match */
|
|
6135 ++k0;
|
|
6136 for (s = t + 1; *s != NUL; ++s)
|
|
6137 if (*s == ')')
|
|
6138 {
|
|
6139 ++s;
|
|
6140 break;
|
|
6141 }
|
|
6142 break;
|
|
6143 }
|
|
6144 }
|
|
6145 while (*s == '-')
|
|
6146 {
|
|
6147 /* "k0" gets NOT reduced */
|
|
6148 /* because "if (k0 == k)" */
|
|
6149 s++;
|
|
6150 }
|
|
6151 if (*s == '<')
|
|
6152 s++;
|
|
6153 if (vim_isdigit(*s))
|
|
6154 {
|
|
6155 p0 = *s - '0';
|
|
6156 s++;
|
|
6157 }
|
|
6158
|
|
6159 if (*s == NUL
|
|
6160 /* *s == '^' cuts */
|
|
6161 || (*s == '$'
|
|
6162 && !SPELL_ISWORDP(word + i + k0)))
|
|
6163 {
|
|
6164 if (k0 == k)
|
|
6165 {
|
|
6166 /* this is just a piece of the string */
|
|
6167 ++n0;
|
|
6168 continue;
|
|
6169 }
|
|
6170
|
|
6171 if (p0 < pri)
|
|
6172 {
|
|
6173 /* priority too low */
|
|
6174 ++n0;
|
|
6175 continue;
|
|
6176 }
|
|
6177 /* rule fits; stop search */
|
|
6178 break;
|
|
6179 }
|
|
6180 ++n0;
|
|
6181 }
|
|
6182
|
|
6183 if (p0 >= pri && ftp[n0].ft_from[0] == c0)
|
|
6184 {
|
|
6185 ++n;
|
|
6186 continue;
|
|
6187 }
|
|
6188 }
|
|
6189
|
|
6190 /* replace string */
|
|
6191 s = ftp[n].ft_to;
|
|
6192 p0 = (ftp[n].ft_from[0] != NUL
|
|
6193 && vim_strchr(ftp[n].ft_from + 1,
|
|
6194 '<') != NULL) ? 1 : 0;
|
|
6195 if (p0 == 1 && z == 0)
|
|
6196 {
|
|
6197 /* rule with '<' is used */
|
|
6198 if (j > 0 && *s != NUL
|
|
6199 && (res[j - 1] == c || res[j - 1] == *s))
|
|
6200 j--;
|
|
6201 z0 = 1;
|
|
6202 z = 1;
|
|
6203 k0 = 0;
|
|
6204 while (*s != NUL && word[i+k0] != NUL)
|
|
6205 {
|
|
6206 word[i + k0] = *s;
|
|
6207 k0++;
|
|
6208 s++;
|
|
6209 }
|
|
6210 if (k > k0)
|
|
6211 mch_memmove(word + i + k0, word + i + k,
|
|
6212 STRLEN(word + i + k) + 1);
|
|
6213
|
|
6214 /* new "actual letter" */
|
|
6215 c = word[i];
|
|
6216 }
|
|
6217 else
|
|
6218 {
|
|
6219 /* no '<' rule used */
|
|
6220 i += k - 1;
|
|
6221 z = 0;
|
|
6222 while (*s != NUL && s[1] != NUL && j < MAXWLEN)
|
|
6223 {
|
|
6224 if (j == 0 || res[j - 1] != *s)
|
|
6225 {
|
|
6226 res[j] = *s;
|
|
6227 j++;
|
|
6228 }
|
|
6229 s++;
|
|
6230 }
|
|
6231 /* new "actual letter" */
|
|
6232 c = *s;
|
|
6233 if (ftp[n].ft_from[0] != NUL
|
|
6234 && strstr((char *)ftp[n].ft_from + 1,
|
|
6235 "^^") != NULL)
|
|
6236 {
|
|
6237 if (c != NUL)
|
|
6238 {
|
|
6239 res[j] = c;
|
|
6240 j++;
|
|
6241 }
|
|
6242 mch_memmove(word, word + i + 1,
|
|
6243 STRLEN(word + i + 1) + 1);
|
|
6244 i = 0;
|
|
6245 z0 = 1;
|
|
6246 }
|
|
6247 }
|
|
6248 break;
|
|
6249 }
|
|
6250 ++n;
|
|
6251 }
|
|
6252 }
|
324
|
6253 else if (vim_iswhite(c))
|
|
6254 {
|
|
6255 c = ' ';
|
|
6256 k = 1;
|
|
6257 }
|
323
|
6258
|
|
6259 if (z0 == 0)
|
|
6260 {
|
|
6261 if (k && !p0 && j < MAXWLEN && c != NUL
|
|
6262 && (!slang->sl_collapse || j == 0 || res[j - 1] != c))
|
|
6263 {
|
|
6264 /* condense only double letters */
|
|
6265 res[j] = c;
|
|
6266 j++;
|
|
6267 }
|
|
6268
|
|
6269 i++;
|
|
6270 z = 0;
|
|
6271 k = 0;
|
|
6272 }
|
|
6273 }
|
|
6274
|
|
6275 res[j] = NUL;
|
|
6276 }
|
|
6277
|
324
|
6278 #if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
|
|
6279 /*
|
|
6280 * Return the score for how much words sound different.
|
|
6281 */
|
|
6282 static int
|
|
6283 spell_sound_score(slang, goodword, badsound)
|
|
6284 slang_T *slang;
|
|
6285 char_u *goodword; /* good word */
|
|
6286 char_u *badsound; /* sound-folded bad word */
|
|
6287 {
|
|
6288 char_u fword[MAXWLEN];
|
|
6289 char_u goodsound[MAXWLEN];
|
|
6290 int score;
|
|
6291
|
|
6292 /* Case-fold the word, needed for sound folding. */
|
|
6293 (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
|
|
6294
|
|
6295 /* sound-fold the good word */
|
|
6296 spell_soundfold(slang, fword, goodsound);
|
|
6297
|
|
6298 /* compute the edit distance-score of the sounds */
|
|
6299 score = spell_edit_score(badsound, goodsound);
|
|
6300
|
|
6301 /* Correction: adding/inserting "*" at the start (word starts with vowel)
|
|
6302 * shouldn't be counted so much, vowels halfway the word aren't counted at
|
|
6303 * all. */
|
|
6304 if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
|
|
6305 score -= SCORE_DEL / 2;
|
|
6306
|
|
6307 return score;
|
|
6308 }
|
|
6309 #endif
|
|
6310
|
323
|
6311 /*
|
|
6312 * Compute the "edit distance" to turn "badword" into "goodword". The less
|
|
6313 * deletes/inserts/swaps are required the lower the score.
|
324
|
6314 *
|
323
|
6315 * The algorithm comes from Aspell editdist.cpp, edit_distance().
|
324
|
6316 * It has been converted from C++ to C and modified to support multi-byte
|
|
6317 * characters.
|
323
|
6318 */
|
|
6319 static int
|
|
6320 spell_edit_score(badword, goodword)
|
|
6321 char_u *badword;
|
|
6322 char_u *goodword;
|
|
6323 {
|
|
6324 int *cnt;
|
|
6325 int badlen, goodlen;
|
|
6326 int j, i;
|
|
6327 int t;
|
|
6328 int bc, gc;
|
324
|
6329 int pbc, pgc;
|
|
6330 #ifdef FEAT_MBYTE
|
|
6331 char_u *p;
|
|
6332 int wbadword[MAXWLEN];
|
|
6333 int wgoodword[MAXWLEN];
|
|
6334
|
|
6335 if (has_mbyte)
|
|
6336 {
|
|
6337 /* Get the characters from the multi-byte strings and put them in an
|
|
6338 * int array for easy access. */
|
|
6339 for (p = badword, badlen = 0; *p != NUL; )
|
|
6340 wbadword[badlen++] = mb_ptr2char_adv(&p);
|
|
6341 ++badlen;
|
|
6342 for (p = goodword, goodlen = 0; *p != NUL; )
|
|
6343 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
|
|
6344 ++goodlen;
|
|
6345 }
|
|
6346 else
|
|
6347 #endif
|
|
6348 {
|
|
6349 badlen = STRLEN(badword) + 1;
|
|
6350 goodlen = STRLEN(goodword) + 1;
|
|
6351 }
|
323
|
6352
|
|
6353 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
|
|
6354 #define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
|
|
6355 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
|
|
6356 TRUE);
|
324
|
6357 if (cnt == NULL)
|
|
6358 return 0; /* out of memory */
|
323
|
6359
|
|
6360 CNT(0, 0) = 0;
|
|
6361 for (j = 1; j <= goodlen; ++j)
|
|
6362 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
|
|
6363
|
|
6364 for (i = 1; i <= badlen; ++i)
|
|
6365 {
|
|
6366 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
|
|
6367 for (j = 1; j <= goodlen; ++j)
|
|
6368 {
|
324
|
6369 #ifdef FEAT_MBYTE
|
|
6370 if (has_mbyte)
|
|
6371 {
|
|
6372 bc = wbadword[i - 1];
|
|
6373 gc = wgoodword[j - 1];
|
|
6374 }
|
|
6375 else
|
|
6376 #endif
|
|
6377 {
|
|
6378 bc = badword[i - 1];
|
|
6379 gc = goodword[j - 1];
|
|
6380 }
|
323
|
6381 if (bc == gc)
|
|
6382 CNT(i, j) = CNT(i - 1, j - 1);
|
|
6383 else
|
|
6384 {
|
|
6385 /* Use a better score when there is only a case difference. */
|
324
|
6386 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
|
323
|
6387 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
|
|
6388 else
|
|
6389 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
|
|
6390
|
324
|
6391 if (i > 1 && j > 1)
|
323
|
6392 {
|
324
|
6393 #ifdef FEAT_MBYTE
|
|
6394 if (has_mbyte)
|
|
6395 {
|
|
6396 pbc = wbadword[i - 2];
|
|
6397 pgc = wgoodword[j - 2];
|
|
6398 }
|
|
6399 else
|
|
6400 #endif
|
|
6401 {
|
|
6402 pbc = badword[i - 2];
|
|
6403 pgc = goodword[j - 2];
|
|
6404 }
|
|
6405 if (bc == pgc && pbc == gc)
|
|
6406 {
|
|
6407 t = SCORE_SWAP + CNT(i - 2, j - 2);
|
|
6408 if (t < CNT(i, j))
|
|
6409 CNT(i, j) = t;
|
|
6410 }
|
323
|
6411 }
|
|
6412 t = SCORE_DEL + CNT(i - 1, j);
|
|
6413 if (t < CNT(i, j))
|
|
6414 CNT(i, j) = t;
|
|
6415 t = SCORE_INS + CNT(i, j - 1);
|
|
6416 if (t < CNT(i, j))
|
|
6417 CNT(i, j) = t;
|
|
6418 }
|
|
6419 }
|
|
6420 }
|
|
6421 return CNT(badlen - 1, goodlen - 1);
|
|
6422 }
|
307
|
6423
|
236
|
6424 #endif /* FEAT_SYN_HL */
|