blob: b1ebfdacc5efbacda51415cbee2d998bdf302676 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* 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
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * 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).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
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".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
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.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000040 *
41 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000042 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000043 * Why doesn't Vim use aspell/ispell/myspell/etc.?
44 * See ":help develop-spell".
45 */
46
Bram Moolenaar51485f02005-06-04 21:55:20 +000047/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000048 * Use this to let the score depend in how much a suggestion sounds like the
Bram Moolenaar9f30f502005-06-14 22:01:04 +000049 * 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)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000060 */
61
62/*
Bram Moolenaar51485f02005-06-04 21:55:20 +000063 * Vim spell file format: <HEADER> <SUGGEST> <LWORDTREE> <KWORDTREE>
64 *
65 * <HEADER>: <fileID> <regioncnt> <regionname> ...
66 * <charflagslen> <charflags> <fcharslen> <fchars>
67 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000068 * <fileID> 10 bytes "VIMspell06"
Bram Moolenaar51485f02005-06-04 21:55:20 +000069 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000070 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000071 * 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):
Bram Moolenaar9f30f502005-06-14 22:01:04 +000075 * 0x01 word character CF_WORD
76 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +000077 * <fcharslen> 2 bytes Number of bytes in <fchars>.
78 * <fchars> N bytes Folded characters, first one is for character 128.
79 *
80 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000081 * <SUGGEST> : <repcount> <rep> ...
82 * <salflags> <salcount> <sal> ...
83 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000084 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000085 * <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
96 *
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.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000116 *
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 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000154 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000155 */
156
Bram Moolenaare19defe2005-03-21 08:23:33 +0000157#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
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169#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. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000172
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000173/* 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
176typedef int idx_T;
177#else
178typedef 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. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000183#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 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000187#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000188#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000190#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000191
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 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000197/* 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". */
200typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000201{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000202 char_u *ft_from;
203 char_u *ft_to;
204} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000205
206/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000207 * Structure used to store words and other info for one language, loaded from
208 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 * 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.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000220 */
221typedef struct slang_S slang_T;
222struct slang_S
223{
224 slang_T *sl_next; /* next language */
225 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000226 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000227 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000228 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000229 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000230 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000231 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000232 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000233
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 */
Bram Moolenaarea424162005-06-16 21:51:00 +0000243 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250};
251
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000252/* First language that is loaded, start of the linked list of loaded
253 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000254static slang_T *first_lang = NULL;
255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000256/* 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000261/*
262 * Structure used in "b_langp", filled from 'spelllang'.
263 */
264typedef 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
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000272#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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000276#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000277#define SP_RARE 1
278#define SP_LOCAL 2
279#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000280
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000281#define VIMSPELLMAGIC "VIMspell06" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000282#define VIMSPELLMAGICL 10
283
284/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000285 * Information used when looking for suggestions.
286 */
287typedef 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". */
305typedef 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 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000310#ifdef RESCORE
311 int st_had_bonus; /* bonus already included in score */
312#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000313} 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
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000320/* 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)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000328
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 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000342#define SCORE_DEL 94 /* delete a character */
343#define SCORE_INS 96 /* insert a character */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000344
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/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000350 * Structure to store info for word matching.
351 */
352typedef struct matchinf_S
353{
354 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000355
356 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357 char_u *mi_word; /* start of word being checked */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000358 char_u *mi_end; /* end of matching word */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000359 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000360 char_u *mi_cend; /* char after what was used for
361 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000362
363 /* case-folded text */
364 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000365 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000366
367 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000368 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000369 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000370} matchinf_T;
371
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000372/*
373 * The tables used for recognizing word characters according to spelling.
374 * These are only used for the first 256 characters of 'encoding'.
375 */
376typedef 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 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000381 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000382} spelltab_T;
383
384static spelltab_T spelltab;
385static int did_set_spelltab;
386
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000387#define CF_WORD 0x01
388#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000389
390static void clear_spell_chartab __ARGS((spelltab_T *sp));
391static 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
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000406/*
Bram Moolenaarea424162005-06-16 21:51:00 +0000407 * For finding suggestion: At each node in the tree these states are tried:
408 */
409typedef 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/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000433 * Struct to keep the state at each level in spell_try_change().
434 */
435typedef struct trystate_S
436{
Bram Moolenaarea424162005-06-16 21:51:00 +0000437 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000438 int ts_score; /* score */
Bram Moolenaarea424162005-06-16 21:51:00 +0000439 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
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000449 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000450 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000451 char_u ts_save_splitoff; /* su_splitoff saved here */
452 char_u ts_save_badflags; /* badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000453} trystate_T;
454
Bram Moolenaarea424162005-06-16 21:51:00 +0000455/* 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000460static slang_T *slang_alloc __ARGS((char_u *lang));
461static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000462static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000463static void find_word __ARGS((matchinf_T *mip, int keepcap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464static int spell_valid_case __ARGS((int origflags, int treeflags));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000465static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000466static char_u *spell_enc __ARGS((void));
467static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000468static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000469static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000470static int find_region __ARGS((char_u *rp, char_u *region));
471static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000472static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000473static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000474static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
475static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000476static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000477static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000478static void spell_try_change __ARGS((suginfo_T *su));
479static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
480static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
481static void spell_try_soundalike __ARGS((suginfo_T *su));
482static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000483static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000484static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000485#ifdef RESCORE
486static void add_suggestion __ARGS((suginfo_T *su, char_u *goodword, int use_score, int had_bonus));
487#else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488static void add_suggestion __ARGS((suginfo_T *su, char_u *goodword, int use_score));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000489#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490static void add_banned __ARGS((suginfo_T *su, char_u *word));
491static int was_banned __ARGS((suginfo_T *su, char_u *word));
492static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000493#ifdef RESCORE
494static void rescore_suggestions __ARGS((suginfo_T *su));
495#endif
496static void cleanup_suggestions __ARGS((suginfo_T *su, int keep));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000498#if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
499static int spell_sound_score __ARGS((slang_T *slang, char_u *goodword, char_u *badsound));
500#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000501static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
502
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000503/*
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
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542
543static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000544
545/*
546 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000547 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000548 * "*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.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000551 *
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 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000555 * 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
Bram Moolenaar51485f02005-06-04 21:55:20 +0000559spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000560 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000561 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
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000567 /* 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;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571
Bram Moolenaar51485f02005-06-04 21:55:20 +0000572 /* A word starting with a number is always OK. Also skip hexadecimal
573 * numbers 0xFF99 and 0X99FF. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000574 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000575 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000576 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
577 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000578 else
579 mi.mi_end = skipdigits(ptr);
580 }
581 else
582 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000583 /* Find the end of the word. */
584 mi.mi_word = ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000585 mi.mi_fend = ptr;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000586
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000587 if (SPELL_ISWORDP(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000588 {
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);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000594 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000595 }
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000596
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000597 /* We always use the characters up to the next non-word character,
598 * also for bad words. */
599 mi.mi_end = mi.mi_fend;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000600
601 /* Check caps type later. */
602 mi.mi_capflags = 0;
603 mi.mi_cend = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000604
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000605 /* 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
Bram Moolenaar51485f02005-06-04 21:55:20 +0000614 /* The word is bad unless we recognize it. */
615 mi.mi_result = SP_BAD;
616
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);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000623 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000624 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000625 /* Check for a matching word in case-folded words. */
626 find_word(&mi, FALSE);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000627
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000628 /* Check for a matching word in keep-case words. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000629 find_word(&mi, TRUE);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000630 }
631
Bram Moolenaar51485f02005-06-04 21:55:20 +0000632 if (mi.mi_result != SP_OK)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000633 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000634 /* 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). */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000636 if (!SPELL_ISWORDP(ptr))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000637 {
638#ifdef FEAT_MBYTE
639 if (has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000640 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000641#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000642 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000643 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000644
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000645 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000646 *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];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000651 }
652 }
653
Bram Moolenaar51485f02005-06-04 21:55:20 +0000654 return (int)(mi.mi_end - ptr);
655}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000656
Bram Moolenaar51485f02005-06-04 21:55:20 +0000657/*
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.
662 */
663 static void
664find_word(mip, keepcap)
665 matchinf_T *mip;
666 int keepcap;
667{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000668 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000669 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000670 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000671 int endidxcnt = 0;
672 int len;
673 int wlen = 0;
674 int flen;
675 int c;
676 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000677 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000678#ifdef FEAT_MBYTE
679 char_u *s;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000680#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000681 char_u *p;
682 int res = SP_BAD;
683 int valid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000684 slang_T *slang = mip->mi_lp->lp_slang;
685 unsigned flags;
686 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000687 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000688
689 if (keepcap)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000690 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000691 /* 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;
696 }
697 else
698 {
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;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000704 }
705
Bram Moolenaar51485f02005-06-04 21:55:20 +0000706 if (byts == NULL)
707 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000708
Bram Moolenaar51485f02005-06-04 21:55:20 +0000709 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000710 * 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.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000714 */
715 for (;;)
716 {
717 if (flen == 0 && *mip->mi_fend != NUL)
718 {
719 /* Need to fold at least one more character. Do until next
720 * non-word character for efficiency. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000721 p = mip->mi_fend;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000722 do
723 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000724 mb_ptr_adv(mip->mi_fend);
725 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000726
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000727 /* 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),
Bram Moolenaar51485f02005-06-04 21:55:20 +0000733 mip->mi_fword + mip->mi_fwordlen,
734 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000735 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 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000745 if (endidxcnt == MAXWLEN)
746 {
747 /* Must be a corrupted spell file. */
748 EMSG(_(e_format));
749 return;
750 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000751 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;
785 }
786 }
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;
796 }
797
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];
807
808#ifdef FEAT_MBYTE
809 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
810 continue; /* not at first byte of character */
811#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000812 if (SPELL_ISWORDP(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000813 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
826
827 /* Check flags and region. Repeat this if there are more
828 * flags/region alternatives until there is a match. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000829 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len)
830 {
831 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000832
Bram Moolenaar51485f02005-06-04 21:55:20 +0000833 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 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000843 /* mi_capflags was set for a different word length, need
844 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000845 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000846 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000847 }
848
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000849 valid = spell_valid_case(mip->mi_capflags, flags);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000850 }
851
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000852 if (valid)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000854 if (flags & WF_BANNED)
855 res = SP_BANNED;
856 else if (flags & WF_REGION)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 {
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;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000868
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000869 /* 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;
881 }
882 else
883 res = SP_BAD;
884
Bram Moolenaar51485f02005-06-04 21:55:20 +0000885 ++arridx;
886 }
887
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000888 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000889 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000890 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000891}
892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000893/*
894 * Check case flags for a word. Return TRUE if the word has the requested
895 * case.
896 */
897 static int
898spell_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
Bram Moolenaar51485f02005-06-04 21:55:20 +0000907
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000908/*
909 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000911 * Return OK if found, FAIL otherwise.
912 */
913 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000914spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000915 int dir; /* FORWARD or BACKWARD */
916 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000918{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000919 linenr_T lnum;
920 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000921 char_u *line;
922 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000923 int attr = 0;
924 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000925 int has_syntax = syntax_present(curbuf);
926 int col;
927 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000928
Bram Moolenaarb765d632005-06-07 21:00:02 +0000929 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000930 {
931 EMSG(_("E756: Spell checking not enabled"));
932 return FAIL;
933 }
934
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000935 /*
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;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000944
945 while (!got_int)
946 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000947 line = ml_get(lnum);
948 p = line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000949
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000950 while (*p != NUL)
951 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000952 /* 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;
957
958 /* start of word */
959 len = spell_check(curwin, p, &attr);
960
961 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000963 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000964 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000965 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000966 /* 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
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000971 && (colnr_T)(curline ? p - line + len
972 : p - line)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000973 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000974 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000975 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000976 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000977 col = p - line;
978 (void)syn_get_id(lnum, (colnr_T)col,
979 FALSE, &can_spell);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000980
Bram Moolenaar51485f02005-06-04 21:55:20 +0000981 /* 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;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000988
Bram Moolenaar51485f02005-06-04 21:55:20 +0000989 if (can_spell)
990 {
991 found_pos.lnum = lnum;
992 found_pos.col = p - line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000993#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +0000994 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000995#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000996 if (dir == FORWARD)
997 {
998 /* No need to search further. */
999 curwin->w_cursor = found_pos;
1000 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001001 }
1002 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001004 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001005 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001006 }
1007
Bram Moolenaar51485f02005-06-04 21:55:20 +00001008 /* advance to character after the word */
1009 p += len;
1010 if (*p == NUL)
1011 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 }
1013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001014 if (curline)
1015 return FAIL; /* only check cursor line */
1016
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001018 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 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001036
1037 line_breakcheck();
1038 }
1039
1040 return FAIL; /* interrupted */
1041}
1042
1043/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001044 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001045 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001046 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001047 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001048spell_load_lang(lang)
1049 char_u *lang;
1050{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001051 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001052 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001053 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001054
Bram Moolenaarb765d632005-06-07 21:00:02 +00001055 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001056 * It's truncated when an error is detected. */
1057 STRCPY(langcp, lang);
1058
Bram Moolenaarb765d632005-06-07 21:00:02 +00001059 /*
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);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001065
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001066 if (r == FAIL && *langcp != NUL)
1067 {
1068 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001069 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001070 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001071 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001072 }
1073
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074 if (r == FAIL)
1075 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1076 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001077 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 *
1090spell_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";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001098}
1099
1100/*
1101 * Allocate a new slang_T.
1102 * Caller must fill "sl_next".
1103 */
1104 static slang_T *
1105slang_alloc(lang)
1106 char_u *lang;
1107{
1108 slang_T *lp;
1109
Bram Moolenaar51485f02005-06-04 21:55:20 +00001110 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001111 if (lp != NULL)
1112 {
1113 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001114 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
1115 ga_init2(&lp->sl_sal, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001116 }
1117 return lp;
1118}
1119
1120/*
1121 * Free the contents of an slang_T and the structure itself.
1122 */
1123 static void
1124slang_free(lp)
1125 slang_T *lp;
1126{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001127 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001128 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
1137slang_clear(lp)
1138 slang_T *lp;
1139{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001140 garray_T *gap;
1141 fromto_T *ftp;
1142 int round;
1143
Bram Moolenaar51485f02005-06-04 21:55:20 +00001144 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001145 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001146 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001147 lp->sl_kbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001148 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001149 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001150 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001151 lp->sl_kidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001152
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
Bram Moolenaarea424162005-06-16 21:51:00 +00001165#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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001179}
1180
1181/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001182 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001183 * Invoked through do_in_runtimepath().
1184 */
1185 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001186spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001187 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001188 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001189{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001190 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001191}
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.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001201 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001202 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001203 static slang_T *
1204spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001205 char_u *fname;
1206 char_u *lang;
1207 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001208 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001209{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001210 FILE *fd;
1211 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001212 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001213 int i;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215 int round;
1216 char_u *save_sourcing_name = sourcing_name;
1217 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001218 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001219 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001220 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001221 garray_T *gap;
1222 fromto_T *ftp;
1223 int rr;
1224 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001225 idx_T idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001226
Bram Moolenaarb765d632005-06-07 21:00:02 +00001227 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001228 if (fd == NULL)
1229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001230 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 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001238 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001239 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001240 if (p_verbose > 2)
1241 {
1242 verbose_enter();
1243 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1244 verbose_leave();
1245 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001246
Bram Moolenaarb765d632005-06-07 21:00:02 +00001247 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;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001264 /* Set sourcing_name, so that error messages mention the file name. */
1265 sourcing_name = fname;
1266 sourcing_lnum = 0;
1267
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001268 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
1269 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001270 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"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001275 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001276 }
1277
1278 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001279 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001280 {
1281truncerr:
1282 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001283 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001284 }
1285 if (cnt > 8)
1286 {
1287formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001288 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001289 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290 }
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
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001298 cnt = getc(fd); /* <charflagslen> */
1299 if (cnt > 0)
1300 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001301 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001302 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)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001309 {
1310 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001311 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001312 }
1313 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001314 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001315 {
1316 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001317 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001319 for (i = 0; i < ccnt; ++i)
1320 fol[i] = getc(fd); /* <fchars> */
1321 fol[i] = NUL;
1322
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001323 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001324 i = set_spell_charflags(p, cnt, fol);
1325 vim_free(p);
1326 vim_free(fol);
1327 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001328 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
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001338 /* <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;
Bram Moolenaarea424162005-06-16 21:51:00 +00001418 set_map_str(lp, p);
1419 vim_free(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001420
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001421
Bram Moolenaar51485f02005-06-04 21:55:20 +00001422 /* round 1: <LWORDTREE>
1423 * round 2: <KWORDTREE> */
1424 for (round = 1; round <= 2; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001425 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001426 /* 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)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001430 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001432 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001433 /* Allocate the byte array. */
1434 p = lalloc((long_u)len, TRUE);
1435 if (p == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001436 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001437 if (round == 1)
1438 lp->sl_fbyts = p;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001439 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 lp->sl_kbyts = p;
1441
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)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001447 lp->sl_fidxs = (idx_T *)p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001449 lp->sl_kidxs = (idx_T *)p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001450
1451
1452 /* Read the tree and store it in the array. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001453 idx = read_tree(fd,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 round == 1 ? lp->sl_fbyts : lp->sl_kbyts,
1455 round == 1 ? lp->sl_fidxs : lp->sl_kidxs,
1456 len, 0);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001457 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001459 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001460 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001461 }
1462 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001463
Bram Moolenaarb765d632005-06-07 21:00:02 +00001464 /* 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 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001470
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001471 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001472
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001473endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00001474 if (lang != NULL)
1475 /* truncating the name signals the error to spell_load_lang() */
1476 *lang = NUL;
1477 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001478 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001479 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001480 lp = NULL;
1481 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001482
1483endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001484 if (fd != NULL)
1485 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001486 sourcing_name = save_sourcing_name;
1487 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001488
1489 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001490}
1491
1492/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001493 * 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.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001501 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001502 static idx_T
Bram Moolenaar51485f02005-06-04 21:55:20 +00001503read_tree(fd, byts, idxs, maxidx, startidx)
1504 FILE *fd;
1505 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001506 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001507 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001508 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001509{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001510 int len;
1511 int i;
1512 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001513 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001514 int c;
1515#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001516
Bram Moolenaar51485f02005-06-04 21:55:20 +00001517 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)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001527 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001528 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;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001559 }
1560
Bram Moolenaar51485f02005-06-04 21:55:20 +00001561 /* 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 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001577
Bram Moolenaar51485f02005-06-04 21:55:20 +00001578 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001579}
1580
1581/*
1582 * Parse 'spelllang' and set buf->b_langp accordingly.
1583 * Returns an error message or NULL.
1584 */
1585 char_u *
1586did_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];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001597 char_u spf_name[MAXPATHL];
1598 int did_spf = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001599
1600 ga_init2(&ga, sizeof(langp_T), 2);
1601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001602 /* 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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001609 /* 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);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001615 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001616 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 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001626
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001627 /* Check if we loaded this language before. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001628 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. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001635 vim_strncpy(lbuf, lang, e - lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001636 if (region != NULL)
1637 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001638 spell_load_lang(lbuf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001639 }
1640
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001641 /*
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)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001646 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001647 region_mask = REGION_ALL;
1648 if (region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001649 {
1650 /* find region in sl_regions */
1651 c = find_region(lp->sl_regions, region);
1652 if (c == REGION_ALL)
1653 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001654 if (!lp->sl_add)
1655 {
1656 c = *e;
1657 *e = NUL;
1658 smsg((char_u *)_("Warning: region %s not supported"),
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001659 lang);
Bram Moolenaar3982c542005-06-08 21:56:31 +00001660 *e = c;
1661 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001662 }
1663 else
1664 region_mask = 1 << c;
1665 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001666
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001667 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;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001675
1676 /* Check if this is the 'spellfile' spell file. */
1677 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
1678 did_spf = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001679 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001680
1681 if (*e == ',')
1682 ++e;
1683 }
1684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001685 /*
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
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001707 /* 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
1729find_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/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001747 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00001748 * Word WF_ONECAP
1749 * W WORD WF_ALLCAP
1750 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001751 */
1752 static int
1753captype(word, end)
1754 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001755 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001756{
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 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001764 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001765 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001766 return 0; /* only non-word characters, illegal word */
1767#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00001768 if (has_mbyte)
1769 c = mb_ptr2char_adv(&p);
1770 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001771#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00001772 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001773 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001774
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 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001780 if (SPELL_ISWORDP(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781 {
1782#ifdef FEAT_MBYTE
1783 c = mb_ptr2char(p);
1784#else
1785 c = *p;
1786#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001787 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001788 {
1789 /* UUl -> KEEPCAP */
1790 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001791 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001792 allcap = FALSE;
1793 }
1794 else if (!allcap)
1795 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001796 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001797 past_second = TRUE;
1798 }
1799
1800 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001801 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001803 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804 return 0;
1805}
1806
1807# if defined(FEAT_MBYTE) || defined(PROTO)
1808/*
1809 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001810 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811 */
1812 void
1813spell_reload()
1814{
1815 buf_T *buf;
1816 slang_T *lp;
Bram Moolenaar3982c542005-06-08 21:56:31 +00001817 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001818
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001819 /* Initialize the table for SPELL_ISWORDP(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820 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);
Bram Moolenaar3982c542005-06-08 21:56:31 +00001834
1835 /* Only load the wordlists when 'spelllang' is set and there is a
1836 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001837 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00001838 {
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 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001848 }
1849}
1850# endif
1851
Bram Moolenaarb765d632005-06-07 21:00:02 +00001852/*
1853 * Reload the spell file "fname" if it's loaded.
1854 */
1855 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001856spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001857 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001858 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001859{
1860 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001861 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001862
Bram Moolenaarb765d632005-06-07 21:00:02 +00001863 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);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001867 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001868 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001869 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00001870 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001871
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);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001876}
1877
1878
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001879/*
1880 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881 */
1882
Bram Moolenaar51485f02005-06-04 21:55:20 +00001883#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001884 and .dic file. */
1885/*
1886 * Main structure to store the contents of a ".aff" file.
1887 */
1888typedef struct afffile_S
1889{
1890 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001891 int af_rar; /* RAR ID for rare word */
1892 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001893 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
1894 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001895} afffile_T;
1896
1897typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001898/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
1899struct 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) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001904 char_u *ae_cond; /* condition (NULL for ".") */
1905 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001906};
1907
1908/* Affix header from ".aff" file. Used for af_pref and af_suff. */
1909typedef 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 */
1924typedef struct sblock_S sblock_T;
1925struct 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 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001930};
1931
1932/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001933 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001934 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001935typedef struct wordnode_S wordnode_T;
1936struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001937{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001938 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 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001949};
1950
Bram Moolenaar51485f02005-06-04 21:55:20 +00001951#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952
Bram Moolenaar51485f02005-06-04 21:55:20 +00001953/*
1954 * Info used while reading the spell files.
1955 */
1956typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001957{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001958 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00001959 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001960 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00001961 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001962 sblock_T *si_blocks; /* memory blocks used */
1963 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001964 int si_add; /* addition file */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001965 int si_region; /* region mask */
1966 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00001967 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001968 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00001969 int si_region_count; /* number of regions supported (1 when there
1970 are no regions) */
1971 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001972
1973 garray_T si_rep; /* list of fromto_T entries from REP lines */
1974 garray_T si_sal; /* list of fromto_T entries from SAL lines */
1975 int si_followup; /* soundsalike: ? */
1976 int si_collapse; /* soundsalike: ? */
1977 int si_rem_accents; /* soundsalike: remove accents */
1978 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001979} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980
Bram Moolenaar51485f02005-06-04 21:55:20 +00001981static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001982static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
1983static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00001984static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001985static void spell_free_aff __ARGS((afffile_T *aff));
1986static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001987static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, hashtab_T *ht, hashtab_T *xht, int comb, int flags));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001988static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
1989static void *getroom __ARGS((sblock_T **blp, size_t len));
1990static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
1991static void free_blocks __ARGS((sblock_T *bl));
1992static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar3982c542005-06-08 21:56:31 +00001993static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001994static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp));
Bram Moolenaarb765d632005-06-07 21:00:02 +00001995static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001996static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
1997static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00001998static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001999static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002000static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002001static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002002
2003/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002004 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00002005 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002006 */
2007 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002008spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002009 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002010 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002011{
2012 FILE *fd;
2013 afffile_T *aff;
2014 char_u rline[MAXLINELEN];
2015 char_u *line;
2016 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002017#define MAXITEMCNT 7
2018 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019 int itemcnt;
2020 char_u *p;
2021 int lnum = 0;
2022 affheader_T *cur_aff = NULL;
2023 int aff_todo = 0;
2024 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002025 char_u *low = NULL;
2026 char_u *fol = NULL;
2027 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002028 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002029 int do_rep;
2030 int do_sal;
2031 int do_map;
2032 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002033 hashitem_T *hi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002034
Bram Moolenaar51485f02005-06-04 21:55:20 +00002035 /*
2036 * Open the file.
2037 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002038 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 if (fd == NULL)
2040 {
2041 EMSG2(_(e_notopen), fname);
2042 return NULL;
2043 }
2044
Bram Moolenaarb765d632005-06-07 21:00:02 +00002045 if (spin->si_verbose || p_verbose > 2)
2046 {
2047 if (!spin->si_verbose)
2048 verbose_enter();
2049 smsg((char_u *)_("Reading affix file %s..."), fname);
2050 out_flush();
2051 if (!spin->si_verbose)
2052 verbose_leave();
2053 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002055 /* Only do REP lines when not done in another .aff file already. */
2056 do_rep = spin->si_rep.ga_len == 0;
2057
2058 /* Only do SAL lines when not done in another .aff file already. */
2059 do_sal = spin->si_sal.ga_len == 0;
2060
2061 /* Only do MAP lines when not done in another .aff file already. */
2062 do_map = spin->si_map.ga_len == 0;
2063
Bram Moolenaar51485f02005-06-04 21:55:20 +00002064 /*
2065 * Allocate and init the afffile_T structure.
2066 */
2067 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002068 if (aff == NULL)
2069 return NULL;
2070 hash_init(&aff->af_pref);
2071 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072
2073 /*
2074 * Read all the lines in the file one by one.
2075 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002076 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002077 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002078 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002079 ++lnum;
2080
2081 /* Skip comment lines. */
2082 if (*rline == '#')
2083 continue;
2084
2085 /* Convert from "SET" to 'encoding' when needed. */
2086 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002087#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002088 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002089 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002090 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002091 if (pc == NULL)
2092 {
2093 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2094 fname, lnum, rline);
2095 continue;
2096 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002097 line = pc;
2098 }
2099 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002100#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002101 {
2102 pc = NULL;
2103 line = rline;
2104 }
2105
2106 /* Split the line up in white separated items. Put a NUL after each
2107 * item. */
2108 itemcnt = 0;
2109 for (p = line; ; )
2110 {
2111 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
2112 ++p;
2113 if (*p == NUL)
2114 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00002115 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002116 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002117 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002118 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002119 ++p;
2120 if (*p == NUL)
2121 break;
2122 *p++ = NUL;
2123 }
2124
2125 /* Handle non-empty lines. */
2126 if (itemcnt > 0)
2127 {
2128 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
2129 && aff->af_enc == NULL)
2130 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002131#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002132 /* Setup for conversion from "ENC" to 'encoding'. */
2133 aff->af_enc = enc_canonize(items[1]);
2134 if (aff->af_enc != NULL && !spin->si_ascii
2135 && convert_setup(&spin->si_conv, aff->af_enc,
2136 p_enc) == FAIL)
2137 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2138 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002139#else
2140 smsg((char_u *)_("Conversion in %s not supported"), fname);
2141#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002142 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00002143 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
2144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002145 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002146 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002149 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002150 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002151 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
2152 && aff->af_rar == 0)
2153 {
2154 aff->af_rar = items[1][0];
2155 if (items[1][1] != NUL)
2156 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2157 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002158 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
2159 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002160 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002161 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002162 if (items[1][1] != NUL)
2163 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
2164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 else if ((STRCMP(items[0], "PFX") == 0
2166 || STRCMP(items[0], "SFX") == 0)
2167 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002168 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00002170 /* Myspell allows extra text after the item, but that might
2171 * mean mistakes go unnoticed. Require a comment-starter. */
2172 if (itemcnt > 4 && *items[4] != '#')
2173 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2174 fname, lnum, items[4]);
2175
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002176 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002177 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
2178 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002179 if (cur_aff == NULL)
2180 break;
2181 cur_aff->ah_key[0] = *items[1];
2182 cur_aff->ah_key[1] = NUL;
2183 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002184 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185 if (*items[2] == 'Y')
2186 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002187 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002188 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
2189 fname, lnum, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002190 if (*items[0] == 'P')
2191 tp = &aff->af_pref;
2192 else
2193 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002194 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002195 hi = hash_find(tp, cur_aff->ah_key);
2196 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00002197 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002198 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
2199 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002200 aff_todo = 0;
2201 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002202 else
2203 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002204 }
2205 else if ((STRCMP(items[0], "PFX") == 0
2206 || STRCMP(items[0], "SFX") == 0)
2207 && aff_todo > 0
2208 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00002209 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002210 {
2211 affentry_T *aff_entry;
2212
Bram Moolenaar8db73182005-06-17 21:51:16 +00002213 /* Myspell allows extra text after the item, but that might
2214 * mean mistakes go unnoticed. Require a comment-starter. */
2215 if (itemcnt > 5 && *items[5] != '#')
2216 smsg((char_u *)_("Trailing text in %s line %d: %s"),
2217 fname, lnum, items[5]);
2218
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002219 /* New item for an affix letter. */
2220 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002221 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
2222 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002223 if (aff_entry == NULL)
2224 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002225
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002226 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002227 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
2228 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002229 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002230 aff_entry->ae_add = getroom_save(&spin->si_blocks,
2231 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002232
Bram Moolenaar51485f02005-06-04 21:55:20 +00002233 /* Don't use an affix entry with non-ASCII characters when
2234 * "spin->si_ascii" is TRUE. */
2235 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002236 || has_non_ascii(aff_entry->ae_add)))
2237 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00002238 aff_entry->ae_next = cur_aff->ah_first;
2239 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002240
2241 if (STRCMP(items[4], ".") != 0)
2242 {
2243 char_u buf[MAXLINELEN];
2244
2245 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
2246 items[4]);
2247 if (*items[0] == 'P')
2248 sprintf((char *)buf, "^%s", items[4]);
2249 else
2250 sprintf((char *)buf, "%s$", items[4]);
2251 aff_entry->ae_prog = vim_regcomp(buf,
2252 RE_MAGIC + RE_STRING);
2253 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002254 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002255 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002256 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
2257 {
2258 if (fol != NULL)
2259 smsg((char_u *)_("Duplicate FOL in %s line %d"),
2260 fname, lnum);
2261 else
2262 fol = vim_strsave(items[1]);
2263 }
2264 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
2265 {
2266 if (low != NULL)
2267 smsg((char_u *)_("Duplicate LOW in %s line %d"),
2268 fname, lnum);
2269 else
2270 low = vim_strsave(items[1]);
2271 }
2272 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
2273 {
2274 if (upp != NULL)
2275 smsg((char_u *)_("Duplicate UPP in %s line %d"),
2276 fname, lnum);
2277 else
2278 upp = vim_strsave(items[1]);
2279 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002280 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002281 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002283 if (!isdigit(*items[1]))
2284 smsg((char_u *)_("Expected REP count in %s line %d"),
2285 fname, lnum);
2286 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
2288 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002290 if (do_rep)
2291 add_fromto(spin, &spin->si_rep, items[1], items[2]);
2292 }
2293 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
2294 {
2295 /* MAP item or count */
2296 if (!found_map)
2297 {
2298 /* First line contains the count. */
2299 found_map = TRUE;
2300 if (!isdigit(*items[1]))
2301 smsg((char_u *)_("Expected MAP count in %s line %d"),
2302 fname, lnum);
2303 }
2304 else if (do_map)
2305 {
2306 /* We simply concatenate all the MAP strings, separated by
2307 * slashes. */
2308 ga_concat(&spin->si_map, items[1]);
2309 ga_append(&spin->si_map, '/');
2310 }
2311 }
2312 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
2313 {
2314 if (do_sal)
2315 {
2316 /* SAL item (sounds-a-like)
2317 * Either one of the known keys or a from-to pair. */
2318 if (STRCMP(items[1], "followup") == 0)
2319 spin->si_followup = sal_to_bool(items[2]);
2320 else if (STRCMP(items[1], "collapse_result") == 0)
2321 spin->si_collapse = sal_to_bool(items[2]);
2322 else if (STRCMP(items[1], "remove_accents") == 0)
2323 spin->si_rem_accents = sal_to_bool(items[2]);
2324 else
2325 /* when "to" is "_" it means empty */
2326 add_fromto(spin, &spin->si_sal, items[1],
2327 STRCMP(items[2], "_") == 0 ? (char_u *)""
2328 : items[2]);
2329 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002330 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002331 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002332 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
2333 fname, lnum, items[0]);
2334 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335 }
2336
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002337 if (fol != NULL || low != NULL || upp != NULL)
2338 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002339 /*
2340 * Don't write a word table for an ASCII file, so that we don't check
2341 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002342 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00002343 * mb_get_class(), the list of chars in the file will be incomplete.
2344 */
2345 if (!spin->si_ascii
2346#ifdef FEAT_MBYTE
2347 && !enc_utf8
2348#endif
2349 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002350 {
2351 if (fol == NULL || low == NULL || upp == NULL)
2352 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
2353 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00002354 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002355 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002356
2357 vim_free(fol);
2358 vim_free(low);
2359 vim_free(upp);
2360 }
2361
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002362 vim_free(pc);
2363 fclose(fd);
2364 return aff;
2365}
2366
2367/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 * Add a from-to item to "gap". Used for REP and SAL items.
2369 * They are stored case-folded.
2370 */
2371 static void
2372add_fromto(spin, gap, from, to)
2373 spellinfo_T *spin;
2374 garray_T *gap;
2375 char_u *from;
2376 char_u *to;
2377{
2378 fromto_T *ftp;
2379 char_u word[MAXWLEN];
2380
2381 if (ga_grow(gap, 1) == OK)
2382 {
2383 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
2384 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
2385 ftp->ft_from = getroom_save(&spin->si_blocks, word);
2386 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
2387 ftp->ft_to = getroom_save(&spin->si_blocks, word);
2388 ++gap->ga_len;
2389 }
2390}
2391
2392/*
2393 * Convert a boolean argument in a SAL line to TRUE or FALSE;
2394 */
2395 static int
2396sal_to_bool(s)
2397 char_u *s;
2398{
2399 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
2400}
2401
2402/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00002403 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
2404 * When "s" is NULL FALSE is returned.
2405 */
2406 static int
2407has_non_ascii(s)
2408 char_u *s;
2409{
2410 char_u *p;
2411
2412 if (s != NULL)
2413 for (p = s; *p != NUL; ++p)
2414 if (*p >= 128)
2415 return TRUE;
2416 return FALSE;
2417}
2418
2419/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002420 * Free the structure filled by spell_read_aff().
2421 */
2422 static void
2423spell_free_aff(aff)
2424 afffile_T *aff;
2425{
2426 hashtab_T *ht;
2427 hashitem_T *hi;
2428 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002429 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002430 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002431
2432 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002433
Bram Moolenaar51485f02005-06-04 21:55:20 +00002434 /* All this trouble to foree the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002435 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
2436 {
2437 todo = ht->ht_used;
2438 for (hi = ht->ht_array; todo > 0; ++hi)
2439 {
2440 if (!HASHITEM_EMPTY(hi))
2441 {
2442 --todo;
2443 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002444 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
2445 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446 }
2447 }
2448 if (ht == &aff->af_suff)
2449 break;
2450 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002451
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 hash_clear(&aff->af_pref);
2453 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454}
2455
2456/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002457 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002459 */
2460 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002461spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002462 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002463 spellinfo_T *spin;
2464 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002466 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00002468 char_u *afflist;
2469 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002470 char_u *pc;
2471 char_u *w;
2472 int l;
2473 hash_T hash;
2474 hashitem_T *hi;
2475 FILE *fd;
2476 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002477 int non_ascii = 0;
2478 int retval = OK;
2479 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002480 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002481
Bram Moolenaar51485f02005-06-04 21:55:20 +00002482 /*
2483 * Open the file.
2484 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002485 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002486 if (fd == NULL)
2487 {
2488 EMSG2(_(e_notopen), fname);
2489 return FAIL;
2490 }
2491
Bram Moolenaar51485f02005-06-04 21:55:20 +00002492 /* The hashtable is only used to detect duplicated words. */
2493 hash_init(&ht);
2494
Bram Moolenaar8db73182005-06-17 21:51:16 +00002495 spin->si_foldwcount = 0;
2496 spin->si_keepwcount = 0;
2497
Bram Moolenaarb765d632005-06-07 21:00:02 +00002498 if (spin->si_verbose || p_verbose > 2)
2499 {
2500 if (!spin->si_verbose)
2501 verbose_enter();
2502 smsg((char_u *)_("Reading dictionary file %s..."), fname);
2503 out_flush();
2504 if (!spin->si_verbose)
2505 verbose_leave();
2506 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002507
2508 /* Read and ignore the first line: word count. */
2509 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002510 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 EMSG2(_("E760: No word count in %s"), fname);
2512
2513 /*
2514 * Read all the lines in the file one by one.
2515 * The words are converted to 'encoding' here, before being added to
2516 * the hashtable.
2517 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002518 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002519 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002520 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002521 ++lnum;
2522
Bram Moolenaar51485f02005-06-04 21:55:20 +00002523 /* Remove CR, LF and white space from the end. White space halfway
2524 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002525 l = STRLEN(line);
2526 while (l > 0 && line[l - 1] <= ' ')
2527 --l;
2528 if (l == 0)
2529 continue; /* empty line */
2530 line[l] = NUL;
2531
Bram Moolenaar51485f02005-06-04 21:55:20 +00002532 /* This takes time, print a message now and then. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002533 if (spin->si_verbose && (lnum & 0x3ff) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002534 {
2535 vim_snprintf((char *)message, sizeof(message),
Bram Moolenaar8db73182005-06-17 21:51:16 +00002536 _("line %6d, word %6d - %s"),
2537 lnum, spin->si_foldwcount + spin->si_keepwcount, line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002538 msg_start();
2539 msg_outtrans_attr(message, 0);
2540 msg_clr_eos();
2541 msg_didout = FALSE;
2542 msg_col = 0;
2543 out_flush();
2544 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002545
Bram Moolenaar51485f02005-06-04 21:55:20 +00002546 /* Find the optional affix names. */
2547 afflist = vim_strchr(line, '/');
2548 if (afflist != NULL)
2549 *afflist++ = NUL;
2550
2551 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
2552 if (spin->si_ascii && has_non_ascii(line))
2553 {
2554 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002555 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002556 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00002557
Bram Moolenaarb765d632005-06-07 21:00:02 +00002558#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002560 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002562 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002563 if (pc == NULL)
2564 {
2565 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2566 fname, lnum, line);
2567 continue;
2568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 w = pc;
2570 }
2571 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002572#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573 {
2574 pc = NULL;
2575 w = line;
2576 }
2577
Bram Moolenaar51485f02005-06-04 21:55:20 +00002578 /* Store the word in the hashtable to be able to find duplicates. */
2579 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002580 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002581 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002582 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002583 if (retval == FAIL)
2584 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002585
Bram Moolenaar51485f02005-06-04 21:55:20 +00002586 hash = hash_hash(dw);
2587 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002588 if (!HASHITEM_EMPTY(hi))
2589 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00002590 fname, lnum, line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002591 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00002592 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002593
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002594 flags = 0;
2595 if (afflist != NULL)
2596 {
2597 /* Check for affix name that stands for keep-case word and stands
2598 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002599 if (affile->af_kep != NUL
2600 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002601 flags |= WF_KEEPCAP;
2602 if (affile->af_rar != NUL
2603 && vim_strchr(afflist, affile->af_rar) != NULL)
2604 flags |= WF_RARE;
2605 }
2606
Bram Moolenaar51485f02005-06-04 21:55:20 +00002607 /* Add the word to the word tree(s). */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002608 if (store_word(dw, spin, flags, spin->si_region) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002609 retval = FAIL;
2610
2611 if (afflist != NULL)
2612 {
2613 /* Find all matching suffixes and add the resulting words.
2614 * Additionally do matching prefixes that combine. */
2615 if (store_aff_word(dw, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002616 &affile->af_suff, &affile->af_pref,
2617 FALSE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002618 retval = FAIL;
2619
2620 /* Find all matching prefixes and add the resulting words. */
2621 if (store_aff_word(dw, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002622 &affile->af_pref, NULL, FALSE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002623 retval = FAIL;
2624 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002625 }
2626
Bram Moolenaar51485f02005-06-04 21:55:20 +00002627 if (spin->si_ascii && non_ascii > 0)
2628 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
2629 non_ascii);
2630 hash_clear(&ht);
2631
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002632 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002633 return retval;
2634}
2635
2636/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002637 * Apply affixes to a word and store the resulting words.
2638 * "ht" is the hashtable with affentry_T that need to be applied, either
2639 * prefixes or suffixes.
2640 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
2641 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002642 *
2643 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002644 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002645 static int
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002646store_aff_word(word, spin, afflist, ht, xht, comb, flags)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002647 char_u *word; /* basic word start */
2648 spellinfo_T *spin; /* spell info */
2649 char_u *afflist; /* list of names of supported affixes */
2650 hashtab_T *ht;
2651 hashtab_T *xht;
2652 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002653 int flags; /* flags for the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002654{
2655 int todo;
2656 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002657 affheader_T *ah;
2658 affentry_T *ae;
2659 regmatch_T regmatch;
2660 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002661 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002662 int i;
2663 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002664
Bram Moolenaar51485f02005-06-04 21:55:20 +00002665 todo = ht->ht_used;
2666 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002667 {
2668 if (!HASHITEM_EMPTY(hi))
2669 {
2670 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002671 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002672
Bram Moolenaar51485f02005-06-04 21:55:20 +00002673 /* Check that the affix combines, if required, and that the word
2674 * supports this affix. */
2675 if ((!comb || ah->ah_combine)
2676 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002677 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002678 /* Loop over all affix entries with this name. */
2679 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002681 /* Check the condition. It's not logical to match case
2682 * here, but it is required for compatibility with
2683 * Myspell. */
2684 regmatch.regprog = ae->ae_prog;
2685 regmatch.rm_ic = FALSE;
2686 if (ae->ae_prog == NULL
2687 || vim_regexec(&regmatch, word, (colnr_T)0))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002688 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002689 /* Match. Remove the chop and add the affix. */
2690 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002692 /* prefix: chop/add at the start of the word */
2693 if (ae->ae_add == NULL)
2694 *newword = NUL;
2695 else
2696 STRCPY(newword, ae->ae_add);
2697 p = word;
2698 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002699 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002700 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002701#ifdef FEAT_MBYTE
2702 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002703 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002704 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002705 for ( ; i > 0; --i)
2706 mb_ptr_adv(p);
2707 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002708 else
2709#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002710 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002711 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002712 STRCAT(newword, p);
2713 }
2714 else
2715 {
2716 /* suffix: chop/add at the end of the word */
2717 STRCPY(newword, word);
2718 if (ae->ae_chop != NULL)
2719 {
2720 /* Remove chop string. */
2721 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002722#ifdef FEAT_MBYTE
2723 if (has_mbyte)
2724 i = mb_charlen(ae->ae_chop);
2725 else
2726#endif
2727 i = STRLEN(ae->ae_chop);
2728 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002729 mb_ptr_back(newword, p);
2730 *p = NUL;
2731 }
2732 if (ae->ae_add != NULL)
2733 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002734 }
2735
Bram Moolenaar51485f02005-06-04 21:55:20 +00002736 /* Store the modified word. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002737 if (store_word(newword, spin,
2738 flags, spin->si_region) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002739 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002740
Bram Moolenaar51485f02005-06-04 21:55:20 +00002741 /* When added a suffix and combining is allowed also
2742 * try adding prefixes additionally. */
2743 if (xht != NULL && ah->ah_combine)
2744 if (store_aff_word(newword, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002745 xht, NULL, TRUE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002746 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747 }
2748 }
2749 }
2750 }
2751 }
2752
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002753 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002754}
2755
2756/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002757 * Read a file with a list of words.
2758 */
2759 static int
2760spell_read_wordfile(fname, spin)
2761 char_u *fname;
2762 spellinfo_T *spin;
2763{
2764 FILE *fd;
2765 long lnum = 0;
2766 char_u rline[MAXLINELEN];
2767 char_u *line;
2768 char_u *pc = NULL;
2769 int l;
2770 int retval = OK;
2771 int did_word = FALSE;
2772 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002773 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002774 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002775
2776 /*
2777 * Open the file.
2778 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002779 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00002780 if (fd == NULL)
2781 {
2782 EMSG2(_(e_notopen), fname);
2783 return FAIL;
2784 }
2785
Bram Moolenaarb765d632005-06-07 21:00:02 +00002786 if (spin->si_verbose || p_verbose > 2)
2787 {
2788 if (!spin->si_verbose)
2789 verbose_enter();
2790 smsg((char_u *)_("Reading word file %s..."), fname);
2791 out_flush();
2792 if (!spin->si_verbose)
2793 verbose_leave();
2794 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002795
2796 /*
2797 * Read all the lines in the file one by one.
2798 */
2799 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2800 {
2801 line_breakcheck();
2802 ++lnum;
2803
2804 /* Skip comment lines. */
2805 if (*rline == '#')
2806 continue;
2807
2808 /* Remove CR, LF and white space from the end. */
2809 l = STRLEN(rline);
2810 while (l > 0 && rline[l - 1] <= ' ')
2811 --l;
2812 if (l == 0)
2813 continue; /* empty or blank line */
2814 rline[l] = NUL;
2815
2816 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
2817 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002818#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002819 if (spin->si_conv.vc_type != CONV_NONE)
2820 {
2821 pc = string_convert(&spin->si_conv, rline, NULL);
2822 if (pc == NULL)
2823 {
2824 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2825 fname, lnum, rline);
2826 continue;
2827 }
2828 line = pc;
2829 }
2830 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002831#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002832 {
2833 pc = NULL;
2834 line = rline;
2835 }
2836
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002837 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002838 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002839
2840 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00002841 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002842 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002843
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002844 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002845 {
2846 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002847 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
2848 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002849 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002850 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
2851 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002852 else
2853 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002854#ifdef FEAT_MBYTE
2855 char_u *enc;
2856
Bram Moolenaar51485f02005-06-04 21:55:20 +00002857 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002858 line += 10;
2859 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002860 if (enc != NULL && !spin->si_ascii
2861 && convert_setup(&spin->si_conv, enc,
2862 p_enc) == FAIL)
2863 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00002864 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002865 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002866#else
2867 smsg((char_u *)_("Conversion in %s not supported"), fname);
2868#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002869 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002870 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002871 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002872
Bram Moolenaar3982c542005-06-08 21:56:31 +00002873 if (STRNCMP(line, "regions=", 8) == 0)
2874 {
2875 if (spin->si_region_count > 1)
2876 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
2877 fname, lnum, line);
2878 else
2879 {
2880 line += 8;
2881 if (STRLEN(line) > 16)
2882 smsg((char_u *)_("Too many regions in %s line %d: %s"),
2883 fname, lnum, line);
2884 else
2885 {
2886 spin->si_region_count = STRLEN(line) / 2;
2887 STRCPY(spin->si_region_name, line);
2888 }
2889 }
2890 continue;
2891 }
2892
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002893 if (*line == '=')
2894 {
2895 /* keep-case word */
2896 flags |= WF_KEEPCAP;
2897 ++line;
2898 }
2899
2900 if (*line == '!')
2901 {
2902 /* Bad, bad, wicked word. */
2903 flags |= WF_BANNED;
2904 ++line;
2905 }
2906 else if (*line == '?')
2907 {
2908 /* Rare word. */
2909 flags |= WF_RARE;
2910 ++line;
2911 }
2912
Bram Moolenaar3982c542005-06-08 21:56:31 +00002913 if (VIM_ISDIGIT(*line))
2914 {
2915 /* region number(s) */
2916 regionmask = 0;
2917 while (VIM_ISDIGIT(*line))
2918 {
2919 l = *line - '0';
2920 if (l > spin->si_region_count)
2921 {
2922 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
2923 fname, lnum, line);
2924 break;
2925 }
2926 regionmask |= 1 << (l - 1);
2927 ++line;
2928 }
2929 flags |= WF_REGION;
2930 }
2931
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002932 if (flags == 0)
2933 {
2934 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00002935 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002936 continue;
2937 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002938 }
2939
2940 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
2941 if (spin->si_ascii && has_non_ascii(line))
2942 {
2943 ++non_ascii;
2944 continue;
2945 }
2946
2947 /* Normal word: store it. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002948 if (store_word(line, spin, flags, regionmask) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002949 {
2950 retval = FAIL;
2951 break;
2952 }
2953 did_word = TRUE;
2954 }
2955
2956 vim_free(pc);
2957 fclose(fd);
2958
Bram Moolenaarb765d632005-06-07 21:00:02 +00002959 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
2960 {
2961 if (p_verbose > 2)
2962 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00002963 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
2964 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002965 if (p_verbose > 2)
2966 verbose_leave();
2967 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002968 return retval;
2969}
2970
2971/*
2972 * Get part of an sblock_T, "len" bytes long.
2973 * This avoids calling free() for every little struct we use.
2974 * The memory is cleared to all zeros.
2975 * Returns NULL when out of memory.
2976 */
2977 static void *
2978getroom(blp, len)
2979 sblock_T **blp;
2980 size_t len; /* length needed */
2981{
2982 char_u *p;
2983 sblock_T *bl = *blp;
2984
2985 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
2986 {
2987 /* Allocate a block of memory. This is not freed until much later. */
2988 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
2989 if (bl == NULL)
2990 return NULL;
2991 bl->sb_next = *blp;
2992 *blp = bl;
2993 bl->sb_used = 0;
2994 }
2995
2996 p = bl->sb_data + bl->sb_used;
2997 bl->sb_used += len;
2998
2999 return p;
3000}
3001
3002/*
3003 * Make a copy of a string into memory allocated with getroom().
3004 */
3005 static char_u *
3006getroom_save(blp, s)
3007 sblock_T **blp;
3008 char_u *s;
3009{
3010 char_u *sc;
3011
3012 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
3013 if (sc != NULL)
3014 STRCPY(sc, s);
3015 return sc;
3016}
3017
3018
3019/*
3020 * Free the list of allocated sblock_T.
3021 */
3022 static void
3023free_blocks(bl)
3024 sblock_T *bl;
3025{
3026 sblock_T *next;
3027
3028 while (bl != NULL)
3029 {
3030 next = bl->sb_next;
3031 vim_free(bl);
3032 bl = next;
3033 }
3034}
3035
3036/*
3037 * Allocate the root of a word tree.
3038 */
3039 static wordnode_T *
3040wordtree_alloc(blp)
3041 sblock_T **blp;
3042{
3043 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3044}
3045
3046/*
3047 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003048 * Always store it in the case-folded tree. A keep-case word can also be used
3049 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003050 * For a keep-case word also store it in the keep-case tree.
3051 */
3052 static int
Bram Moolenaar3982c542005-06-08 21:56:31 +00003053store_word(word, spin, flags, region)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003054 char_u *word;
3055 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003056 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003057 int region; /* supported region(s) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003058{
3059 int len = STRLEN(word);
3060 int ct = captype(word, word + len);
3061 char_u foldword[MAXWLEN];
3062 int res;
3063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003064 (void)spell_casefold(word, len, foldword, MAXWLEN);
3065 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
3066 region, &spin->si_blocks);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003067 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003068
3069 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00003070 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003071 res = tree_add_word(word, spin->si_keeproot, flags,
Bram Moolenaar3982c542005-06-08 21:56:31 +00003072 region, &spin->si_blocks);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003073 ++spin->si_keepwcount;
3074 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003075 return res;
3076}
3077
3078/*
3079 * Add word "word" to a word tree at "root".
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003080 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003081 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003082 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003083tree_add_word(word, root, flags, region, blp)
3084 char_u *word;
3085 wordnode_T *root;
3086 int flags;
3087 int region;
3088 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003089{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003090 wordnode_T *node = root;
3091 wordnode_T *np;
3092 wordnode_T **prev = NULL;
3093 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003094
Bram Moolenaar51485f02005-06-04 21:55:20 +00003095 /* Add each byte of the word to the tree, including the NUL at the end. */
3096 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003097 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003098 /* Look for the sibling that has the same character. They are sorted
3099 * on byte value, thus stop searching when a sibling is found with a
3100 * higher byte value. For zero bytes (end of word) check that the
3101 * flags are equal, there is a separate zero byte for each flag value.
3102 */
3103 while (node != NULL && (node->wn_byte < word[i]
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003104 || (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003105 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003106 prev = &node->wn_sibling;
3107 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003108 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003109 if (node == NULL || node->wn_byte != word[i])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111 /* Allocate a new node. */
3112 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
3113 if (np == NULL)
3114 return FAIL;
3115 np->wn_byte = word[i];
3116 *prev = np;
3117 np->wn_sibling = node;
3118 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003119 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003120
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003122 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003123 node->wn_flags = flags;
3124 node->wn_region |= region;
3125 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00003126 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003127 prev = &node->wn_child;
3128 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003129 }
3130
3131 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003132}
3133
3134/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003135 * Compress a tree: find tails that are identical and can be shared.
3136 */
3137 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00003138wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003139 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003140 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003141{
3142 hashtab_T ht;
3143 int n;
3144 int tot = 0;
3145
3146 if (root != NULL)
3147 {
3148 hash_init(&ht);
3149 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003150 if (spin->si_verbose || p_verbose > 2)
3151 {
3152 if (!spin->si_verbose)
3153 verbose_enter();
3154 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00003155 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003156 if (p_verbose > 2)
3157 verbose_leave();
3158 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003159 hash_clear(&ht);
3160 }
3161}
3162
3163/*
3164 * Compress a node, its siblings and its children, depth first.
3165 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003166 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003167 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00003168node_compress(node, ht, tot)
3169 wordnode_T *node;
3170 hashtab_T *ht;
3171 int *tot; /* total count of nodes before compressing,
3172 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003173{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003174 wordnode_T *np;
3175 wordnode_T *tp;
3176 wordnode_T *child;
3177 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003178 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003179 int len = 0;
3180 unsigned nr, n;
3181 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003182
Bram Moolenaar51485f02005-06-04 21:55:20 +00003183 /*
3184 * Go through the list of siblings. Compress each child and then try
3185 * finding an identical child to replace it.
3186 * Note that with "child" we mean not just the node that is pointed to,
3187 * but the whole list of siblings, of which the node is the first.
3188 */
3189 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003190 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003191 ++len;
3192 if ((child = np->wn_child) != NULL)
3193 {
3194 /* Compress the child. This fills wn_hashkey. */
3195 compressed += node_compress(child, ht, tot);
3196
3197 /* Try to find an identical child. */
3198 hash = hash_hash(child->wn_hashkey);
3199 hi = hash_lookup(ht, child->wn_hashkey, hash);
3200 tp = NULL;
3201 if (!HASHITEM_EMPTY(hi))
3202 {
3203 /* There are children with an identical hash value. Now check
3204 * if there is one that is really identical. */
3205 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
3206 if (node_equal(child, tp))
3207 {
3208 /* Found one! Now use that child in place of the
3209 * current one. This means the current child is
3210 * dropped from the tree. */
3211 np->wn_child = tp;
3212 ++compressed;
3213 break;
3214 }
3215 if (tp == NULL)
3216 {
3217 /* No other child with this hash value equals the child of
3218 * the node, add it to the linked list after the first
3219 * item. */
3220 tp = HI2WN(hi);
3221 child->wn_next = tp->wn_next;
3222 tp->wn_next = child;
3223 }
3224 }
3225 else
3226 /* No other child has this hash value, add it to the
3227 * hashtable. */
3228 hash_add_item(ht, hi, child->wn_hashkey, hash);
3229 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003230 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003231 *tot += len;
3232
3233 /*
3234 * Make a hash key for the node and its siblings, so that we can quickly
3235 * find a lookalike node. This must be done after compressing the sibling
3236 * list, otherwise the hash key would become invalid by the compression.
3237 */
3238 node->wn_hashkey[0] = len;
3239 nr = 0;
3240 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003241 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003242 if (np->wn_byte == NUL)
3243 /* end node: only use wn_flags and wn_region */
3244 n = np->wn_flags + (np->wn_region << 8);
3245 else
3246 /* byte node: use the byte value and the child pointer */
3247 n = np->wn_byte + ((long_u)np->wn_child << 8);
3248 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003249 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003250
3251 /* Avoid NUL bytes, it terminates the hash key. */
3252 n = nr & 0xff;
3253 node->wn_hashkey[1] = n == 0 ? 1 : n;
3254 n = (nr >> 8) & 0xff;
3255 node->wn_hashkey[2] = n == 0 ? 1 : n;
3256 n = (nr >> 16) & 0xff;
3257 node->wn_hashkey[3] = n == 0 ? 1 : n;
3258 n = (nr >> 24) & 0xff;
3259 node->wn_hashkey[4] = n == 0 ? 1 : n;
3260 node->wn_hashkey[5] = NUL;
3261
3262 return compressed;
3263}
3264
3265/*
3266 * Return TRUE when two nodes have identical siblings and children.
3267 */
3268 static int
3269node_equal(n1, n2)
3270 wordnode_T *n1;
3271 wordnode_T *n2;
3272{
3273 wordnode_T *p1;
3274 wordnode_T *p2;
3275
3276 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
3277 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
3278 if (p1->wn_byte != p2->wn_byte
3279 || (p1->wn_byte == NUL
3280 ? (p1->wn_flags != p2->wn_flags
3281 || p1->wn_region != p2->wn_region)
3282 : (p1->wn_child != p2->wn_child)))
3283 break;
3284
3285 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003286}
3287
3288/*
3289 * Write a number to file "fd", MSB first, in "len" bytes.
3290 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003291 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003292put_bytes(fd, nr, len)
3293 FILE *fd;
3294 long_u nr;
3295 int len;
3296{
3297 int i;
3298
3299 for (i = len - 1; i >= 0; --i)
3300 putc((int)(nr >> (i * 8)), fd);
3301}
3302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003303static int
3304#ifdef __BORLANDC__
3305_RTLENTRYF
3306#endif
3307rep_compare __ARGS((const void *s1, const void *s2));
3308
3309/*
3310 * Function given to qsort() to sort the REP items on "from" string.
3311 */
3312 static int
3313#ifdef __BORLANDC__
3314_RTLENTRYF
3315#endif
3316rep_compare(s1, s2)
3317 const void *s1;
3318 const void *s2;
3319{
3320 fromto_T *p1 = (fromto_T *)s1;
3321 fromto_T *p2 = (fromto_T *)s2;
3322
3323 return STRCMP(p1->ft_from, p2->ft_from);
3324}
3325
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003326/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003327 * Write the Vim spell file "fname".
3328 */
3329 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00003330write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003331 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003332 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003333{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003334 FILE *fd;
3335 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003336 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003337 wordnode_T *tree;
3338 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003339 int i;
3340 int l;
3341 garray_T *gap;
3342 fromto_T *ftp;
3343 char_u *p;
3344 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003345
Bram Moolenaarb765d632005-06-07 21:00:02 +00003346 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00003347 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003348 {
3349 EMSG2(_(e_notopen), fname);
3350 return;
3351 }
3352
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003353 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
3354 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003355
3356 /* <fileID> */
3357 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
3358 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003359
3360 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003361 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003362 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003363 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
3364 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
3365 (size_t)1, fd);
3366 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003367 }
3368 else
3369 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003370 putc(0, fd);
3371 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003372 }
3373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003374 /*
3375 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003376 * <charflagslen> <charflags> <fcharlen> <fchars>
3377 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 * 'encoding'.
3379 * Also skip this for an .add.spl file, the main spell file must contain
3380 * the table (avoids that it conflicts). File is shorter too.
3381 */
3382 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003383 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003384 putc(0, fd);
3385 putc(0, fd);
3386 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003387 }
3388 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00003389 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003391 /* Sort the REP items. */
3392 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
3393 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003395 /* <SUGGEST> : <repcount> <rep> ...
3396 * <salflags> <salcount> <sal> ...
3397 * <maplen> <mapstr> */
3398 for (round = 1; round <= 2; ++round)
3399 {
3400 if (round == 1)
3401 gap = &spin->si_rep;
3402 else
3403 {
3404 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003406 i = 0;
3407 if (spin->si_followup)
3408 i |= SAL_F0LLOWUP;
3409 if (spin->si_collapse)
3410 i |= SAL_COLLAPSE;
3411 if (spin->si_rem_accents)
3412 i |= SAL_REM_ACCENTS;
3413 putc(i, fd); /* <salflags> */
3414 }
3415
3416 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
3417 for (i = 0; i < gap->ga_len; ++i)
3418 {
3419 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3420 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3421 ftp = &((fromto_T *)gap->ga_data)[i];
3422 for (rr = 1; rr <= 2; ++rr)
3423 {
3424 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
3425 l = STRLEN(p);
3426 putc(l, fd);
3427 fwrite(p, l, (size_t)1, fd);
3428 }
3429 }
3430 }
3431
3432 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
3433 if (spin->si_map.ga_len > 0) /* <mapstr> */
3434 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
3435 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00003436
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003437 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003438 * <LWORDTREE> <KWORDTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003439 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003440 spin->si_memtot = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003441 for (round = 1; round <= 2; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003443 tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003444
Bram Moolenaar51485f02005-06-04 21:55:20 +00003445 /* Count the number of nodes. Needed to be able to allocate the
3446 * memory when reading the nodes. Also fills in the index for shared
3447 * nodes. */
3448 nodecount = put_tree(NULL, tree, 0, regionmask);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003449
Bram Moolenaar51485f02005-06-04 21:55:20 +00003450 /* number of nodes in 4 bytes */
3451 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003452 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003453
Bram Moolenaar51485f02005-06-04 21:55:20 +00003454 /* Write the nodes. */
3455 (void)put_tree(fd, tree, 0, regionmask);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003456 }
3457
Bram Moolenaar51485f02005-06-04 21:55:20 +00003458 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003459}
3460
3461/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003462 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003463 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003464 * This first writes the list of possible bytes (siblings). Then for each
3465 * byte recursively write the children.
3466 *
3467 * NOTE: The code here must match the code in read_tree(), since assumptions
3468 * are made about the indexes (so that we don't have to write them in the
3469 * file).
3470 *
3471 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003472 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003473 static int
3474put_tree(fd, node, index, regionmask)
3475 FILE *fd; /* NULL when only counting */
3476 wordnode_T *node;
3477 int index;
3478 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003479{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003480 int newindex = index;
3481 int siblingcount = 0;
3482 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003483 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003484
Bram Moolenaar51485f02005-06-04 21:55:20 +00003485 /* If "node" is zero the tree is empty. */
3486 if (node == NULL)
3487 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003488
Bram Moolenaar51485f02005-06-04 21:55:20 +00003489 /* Store the index where this node is written. */
3490 node->wn_index = index;
3491
3492 /* Count the number of siblings. */
3493 for (np = node; np != NULL; np = np->wn_sibling)
3494 ++siblingcount;
3495
3496 /* Write the sibling count. */
3497 if (fd != NULL)
3498 putc(siblingcount, fd); /* <siblingcount> */
3499
3500 /* Write each sibling byte and optionally extra info. */
3501 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003502 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003503 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003504 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003505 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003506 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003507 /* For a NUL byte (end of word) instead of the byte itself
3508 * we write the flag/region items. */
3509 flags = np->wn_flags;
3510 if (regionmask != 0 && np->wn_region != regionmask)
3511 flags |= WF_REGION;
3512 if (flags == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003513 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003514 /* word without flags or region */
3515 putc(BY_NOFLAGS, fd); /* <byte> */
3516 }
3517 else
3518 {
3519 putc(BY_FLAGS, fd); /* <byte> */
3520 putc(flags, fd); /* <flags> */
3521 if (flags & WF_REGION)
3522 putc(np->wn_region, fd); /* <regionmask> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003523 }
3524 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00003525 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003526 else
3527 {
3528 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
3529 {
3530 /* The child is written elsewhere, write the reference. */
3531 if (fd != NULL)
3532 {
3533 putc(BY_INDEX, fd); /* <byte> */
3534 /* <nodeidx> */
3535 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
3536 }
3537 }
3538 else if (np->wn_child->wn_wnode == NULL)
3539 /* We will write the child below and give it an index. */
3540 np->wn_child->wn_wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003541
Bram Moolenaar51485f02005-06-04 21:55:20 +00003542 if (fd != NULL)
3543 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
3544 {
3545 EMSG(_(e_write));
3546 return 0;
3547 }
3548 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003549 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003550
3551 /* Space used in the array when reading: one for each sibling and one for
3552 * the count. */
3553 newindex += siblingcount + 1;
3554
3555 /* Recursively dump the children of each sibling. */
3556 for (np = node; np != NULL; np = np->wn_sibling)
3557 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
3558 newindex = put_tree(fd, np->wn_child, newindex, regionmask);
3559
3560 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561}
3562
3563
3564/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00003565 * ":mkspell [-ascii] outfile infile ..."
3566 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003567 */
3568 void
3569ex_mkspell(eap)
3570 exarg_T *eap;
3571{
3572 int fcount;
3573 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003574 char_u *arg = eap->arg;
3575 int ascii = FALSE;
3576
3577 if (STRNCMP(arg, "-ascii", 6) == 0)
3578 {
3579 ascii = TRUE;
3580 arg = skipwhite(arg + 6);
3581 }
3582
3583 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
3584 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
3585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003587 FreeWild(fcount, fnames);
3588 }
3589}
3590
3591/*
3592 * Create a Vim spell file from one or more word lists.
3593 * "fnames[0]" is the output file name.
3594 * "fnames[fcount - 1]" is the last input file name.
3595 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
3596 * and ".spl" is appended to make the output file name.
3597 */
3598 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003600 int fcount;
3601 char_u **fnames;
3602 int ascii; /* -ascii argument given */
3603 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003605{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003606 char_u fname[MAXPATHL];
3607 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00003608 char_u **innames;
3609 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003610 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003611 int i;
3612 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003613 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003614 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003615 spellinfo_T spin;
3616
3617 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003618 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003619 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003620 spin.si_followup = TRUE;
3621 spin.si_rem_accents = TRUE;
3622 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
3623 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
3624 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003625
Bram Moolenaarb765d632005-06-07 21:00:02 +00003626 /* default: fnames[0] is output file, following are input files */
3627 innames = &fnames[1];
3628 incount = fcount - 1;
3629
3630 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003631 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003632 len = STRLEN(fnames[0]);
3633 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
3634 {
3635 /* For ":mkspell path/en.latin1.add" output file is
3636 * "path/en.latin1.add.spl". */
3637 innames = &fnames[0];
3638 incount = 1;
3639 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
3640 }
3641 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
3642 {
3643 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003644 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003645 }
3646 else
3647 /* Name should be language, make the file name from it. */
3648 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
3649 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
3650
3651 /* Check for .ascii.spl. */
3652 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
3653 spin.si_ascii = TRUE;
3654
3655 /* Check for .add.spl. */
3656 if (strstr((char *)gettail(wfname), ".add.") != NULL)
3657 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003658 }
3659
Bram Moolenaarb765d632005-06-07 21:00:02 +00003660 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003661 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003662 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003663 EMSG(_("E754: Only up to 8 regions supported"));
3664 else
3665 {
3666 /* Check for overwriting before doing things that may take a lot of
3667 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003668 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003669 {
3670 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003671 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003672 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003673 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003674 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003675 EMSG2(_(e_isadir2), wfname);
3676 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003677 }
3678
3679 /*
3680 * Init the aff and dic pointers.
3681 * Get the region names if there are more than 2 arguments.
3682 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003683 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003684 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003685 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003686
Bram Moolenaar3982c542005-06-08 21:56:31 +00003687 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003688 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003689 len = STRLEN(innames[i]);
3690 if (STRLEN(gettail(innames[i])) < 5
3691 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003692 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003693 EMSG2(_("E755: Invalid region in %s"), innames[i]);
3694 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003695 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003696 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
3697 spin.si_region_name[i * 2 + 1] =
3698 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003699 }
3700 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003701 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003702
Bram Moolenaarb765d632005-06-07 21:00:02 +00003703 if (!spin.si_add)
3704 /* Clear the char type tables, don't want to use any of the
3705 * currently used spell properties. */
3706 init_spell_chartab();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003707
Bram Moolenaar51485f02005-06-04 21:55:20 +00003708 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
3709 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
3710 if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
3711 {
3712 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003713 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003714 }
3715
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003716 /*
3717 * Read all the .aff and .dic files.
3718 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003719 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003720 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003721 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003722 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003723 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003724 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725
Bram Moolenaarb765d632005-06-07 21:00:02 +00003726 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003727 if (mch_stat((char *)fname, &st) >= 0)
3728 {
3729 /* Read the .aff file. Will init "spin->si_conv" based on the
3730 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003731 afile[i] = spell_read_aff(fname, &spin);
3732 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003733 error = TRUE;
3734 else
3735 {
3736 /* Read the .dic file and store the words in the trees. */
3737 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00003738 innames[i]);
3739 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003740 error = TRUE;
3741 }
3742 }
3743 else
3744 {
3745 /* No .aff file, try reading the file as a word list. Store
3746 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003747 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003748 error = TRUE;
3749 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003750
Bram Moolenaarb765d632005-06-07 21:00:02 +00003751#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003752 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003753 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003754#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003755 }
3756
Bram Moolenaar51485f02005-06-04 21:55:20 +00003757 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003758 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003759 /*
3760 * Remove the dummy NUL from the start of the tree root.
3761 */
3762 spin.si_foldroot = spin.si_foldroot->wn_sibling;
3763 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003764
3765 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003766 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003767 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003768 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003771 verbose_enter();
3772 MSG(_("Compressing word tree..."));
3773 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003775 verbose_leave();
3776 }
3777 wordtree_compress(spin.si_foldroot, &spin);
3778 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003779 }
3780
Bram Moolenaar51485f02005-06-04 21:55:20 +00003781 if (!error)
3782 {
3783 /*
3784 * Write the info in the spell file.
3785 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003789 verbose_enter();
3790 smsg((char_u *)_("Writing spell file %s..."), wfname);
3791 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003793 verbose_leave();
3794 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003795
Bram Moolenaar3982c542005-06-08 21:56:31 +00003796 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003801 verbose_enter();
3802 MSG(_("Done!"));
3803 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00003804 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003805 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003806 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003807 verbose_leave();
3808 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003809
Bram Moolenaarb765d632005-06-07 21:00:02 +00003810 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003811 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003812 }
3813
3814 /* Free the allocated memory. */
3815 free_blocks(spin.si_blocks);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 ga_clear(&spin.si_rep);
3817 ga_clear(&spin.si_sal);
3818 ga_clear(&spin.si_map);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003819
3820 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003821 for (i = 0; i < incount; ++i)
3822 if (afile[i] != NULL)
3823 spell_free_aff(afile[i]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003824 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003825}
3826
Bram Moolenaarb765d632005-06-07 21:00:02 +00003827
3828/*
3829 * ":spellgood {word}"
3830 * ":spellwrong {word}"
3831 */
3832 void
3833ex_spell(eap)
3834 exarg_T *eap;
3835{
3836 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
3837}
3838
3839/*
3840 * Add "word[len]" to 'spellfile' as a good or bad word.
3841 */
3842 void
3843spell_add_word(word, len, bad)
3844 char_u *word;
3845 int len;
3846 int bad;
3847{
3848 FILE *fd;
3849 buf_T *buf;
3850
3851 if (*curbuf->b_p_spf == NUL)
3852 init_spellfile();
3853 if (*curbuf->b_p_spf == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 EMSG(_("E764: 'spellfile' is not set"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003855 else
3856 {
3857 /* Check that the user isn't editing the .add file somewhere. */
3858 buf = buflist_findname_exp(curbuf->b_p_spf);
3859 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
3860 buf = NULL;
3861 if (buf != NULL && bufIsChanged(buf))
3862 EMSG(_(e_bufloaded));
3863 else
3864 {
3865 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
3866 if (fd == NULL)
3867 EMSG2(_(e_notopen), curbuf->b_p_spf);
3868 else
3869 {
3870 if (bad)
3871 fprintf(fd, "/!%.*s\n", len, word);
3872 else
3873 fprintf(fd, "%.*s\n", len, word);
3874 fclose(fd);
3875
3876 /* Update the .add.spl file. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003877 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, TRUE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003878
3879 /* If the .add file is edited somewhere, reload it. */
3880 if (buf != NULL)
3881 buf_reload(buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003882
3883 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003884 }
3885 }
3886 }
3887}
3888
3889/*
3890 * Initialize 'spellfile' for the current buffer.
3891 */
3892 static void
3893init_spellfile()
3894{
3895 char_u buf[MAXPATHL];
3896 int l;
3897 slang_T *sl;
3898 char_u *rtp;
3899
3900 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
3901 {
3902 /* Loop over all entries in 'runtimepath'. */
3903 rtp = p_rtp;
3904 while (*rtp != NUL)
3905 {
3906 /* Copy the path from 'runtimepath' to buf[]. */
3907 copy_option_part(&rtp, buf, MAXPATHL, ",");
3908 if (filewritable(buf) == 2)
3909 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003910 /* Use the first language name from 'spelllang' and the
3911 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003912 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
3913 l = STRLEN(buf);
3914 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00003915 "/spell/%.*s.%s.add",
3916 2, curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00003917 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
3918 ? (char_u *)"ascii" : spell_enc());
3919 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
3920 break;
3921 }
3922 }
3923 }
3924}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003925
Bram Moolenaar51485f02005-06-04 21:55:20 +00003926
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003927/*
3928 * Init the chartab used for spelling for ASCII.
3929 * EBCDIC is not supported!
3930 */
3931 static void
3932clear_spell_chartab(sp)
3933 spelltab_T *sp;
3934{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003935 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003936
3937 /* Init everything to FALSE. */
3938 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
3939 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
3940 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003941 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003942 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003943 sp->st_upper[i] = i;
3944 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003945
3946 /* We include digits. A word shouldn't start with a digit, but handling
3947 * that is done separately. */
3948 for (i = '0'; i <= '9'; ++i)
3949 sp->st_isw[i] = TRUE;
3950 for (i = 'A'; i <= 'Z'; ++i)
3951 {
3952 sp->st_isw[i] = TRUE;
3953 sp->st_isu[i] = TRUE;
3954 sp->st_fold[i] = i + 0x20;
3955 }
3956 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003957 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003958 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003959 sp->st_upper[i] = i - 0x20;
3960 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003961}
3962
3963/*
3964 * Init the chartab used for spelling. Only depends on 'encoding'.
3965 * Called once while starting up and when 'encoding' changes.
3966 * The default is to use isalpha(), but the spell file should define the word
3967 * characters to make it possible that 'encoding' differs from the current
3968 * locale.
3969 */
3970 void
3971init_spell_chartab()
3972{
3973 int i;
3974
3975 did_set_spelltab = FALSE;
3976 clear_spell_chartab(&spelltab);
3977
3978#ifdef FEAT_MBYTE
3979 if (enc_dbcs)
3980 {
3981 /* DBCS: assume double-wide characters are word characters. */
3982 for (i = 128; i <= 255; ++i)
3983 if (MB_BYTE2LEN(i) == 2)
3984 spelltab.st_isw[i] = TRUE;
3985 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003986 else if (enc_utf8)
3987 {
3988 for (i = 128; i < 256; ++i)
3989 {
3990 spelltab.st_isu[i] = utf_isupper(i);
3991 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
3992 spelltab.st_fold[i] = utf_fold(i);
3993 spelltab.st_upper[i] = utf_toupper(i);
3994 }
3995 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003996 else
3997#endif
3998 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003999 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004000 for (i = 128; i < 256; ++i)
4001 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004002 if (MB_ISUPPER(i))
4003 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004004 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004005 spelltab.st_isu[i] = TRUE;
4006 spelltab.st_fold[i] = MB_TOLOWER(i);
4007 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004008 else if (MB_ISLOWER(i))
4009 {
4010 spelltab.st_isw[i] = TRUE;
4011 spelltab.st_upper[i] = MB_TOUPPER(i);
4012 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004013 }
4014 }
4015}
4016
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004017static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
4018static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
4019
4020/*
4021 * Set the spell character tables from strings in the affix file.
4022 */
4023 static int
4024set_spell_chartab(fol, low, upp)
4025 char_u *fol;
4026 char_u *low;
4027 char_u *upp;
4028{
4029 /* We build the new tables here first, so that we can compare with the
4030 * previous one. */
4031 spelltab_T new_st;
4032 char_u *pf = fol, *pl = low, *pu = upp;
4033 int f, l, u;
4034
4035 clear_spell_chartab(&new_st);
4036
4037 while (*pf != NUL)
4038 {
4039 if (*pl == NUL || *pu == NUL)
4040 {
4041 EMSG(_(e_affform));
4042 return FAIL;
4043 }
4044#ifdef FEAT_MBYTE
4045 f = mb_ptr2char_adv(&pf);
4046 l = mb_ptr2char_adv(&pl);
4047 u = mb_ptr2char_adv(&pu);
4048#else
4049 f = *pf++;
4050 l = *pl++;
4051 u = *pu++;
4052#endif
4053 /* Every character that appears is a word character. */
4054 if (f < 256)
4055 new_st.st_isw[f] = TRUE;
4056 if (l < 256)
4057 new_st.st_isw[l] = TRUE;
4058 if (u < 256)
4059 new_st.st_isw[u] = TRUE;
4060
4061 /* if "LOW" and "FOL" are not the same the "LOW" char needs
4062 * case-folding */
4063 if (l < 256 && l != f)
4064 {
4065 if (f >= 256)
4066 {
4067 EMSG(_(e_affrange));
4068 return FAIL;
4069 }
4070 new_st.st_fold[l] = f;
4071 }
4072
4073 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004074 * case-folding, it's upper case and the "UPP" is the upper case of
4075 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004076 if (u < 256 && u != f)
4077 {
4078 if (f >= 256)
4079 {
4080 EMSG(_(e_affrange));
4081 return FAIL;
4082 }
4083 new_st.st_fold[u] = f;
4084 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004085 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004086 }
4087 }
4088
4089 if (*pl != NUL || *pu != NUL)
4090 {
4091 EMSG(_(e_affform));
4092 return FAIL;
4093 }
4094
4095 return set_spell_finish(&new_st);
4096}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004097
4098/*
4099 * Set the spell character tables from strings in the .spl file.
4100 */
4101 static int
4102set_spell_charflags(flags, cnt, upp)
4103 char_u *flags;
4104 int cnt;
4105 char_u *upp;
4106{
4107 /* We build the new tables here first, so that we can compare with the
4108 * previous one. */
4109 spelltab_T new_st;
4110 int i;
4111 char_u *p = upp;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004112 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004113
4114 clear_spell_chartab(&new_st);
4115
4116 for (i = 0; i < cnt; ++i)
4117 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004118 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
4119 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004120
4121 if (*p == NUL)
4122 return FAIL;
4123#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004124 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004125#else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004126 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004127#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004128 new_st.st_fold[i + 128] = c;
4129 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
4130 new_st.st_upper[c] = i + 128;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004131 }
4132
4133 return set_spell_finish(&new_st);
4134}
4135
4136 static int
4137set_spell_finish(new_st)
4138 spelltab_T *new_st;
4139{
4140 int i;
4141
4142 if (did_set_spelltab)
4143 {
4144 /* check that it's the same table */
4145 for (i = 0; i < 256; ++i)
4146 {
4147 if (spelltab.st_isw[i] != new_st->st_isw[i]
4148 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004149 || spelltab.st_fold[i] != new_st->st_fold[i]
4150 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004151 {
4152 EMSG(_("E763: Word characters differ between spell files"));
4153 return FAIL;
4154 }
4155 }
4156 }
4157 else
4158 {
4159 /* copy the new spelltab into the one being used */
4160 spelltab = *new_st;
4161 did_set_spelltab = TRUE;
4162 }
4163
4164 return OK;
4165}
4166
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004167/*
4168 * Write the current tables into the .spl file.
4169 * This makes sure the same characters are recognized as word characters when
4170 * generating an when using a spell file.
4171 */
4172 static void
4173write_spell_chartab(fd)
4174 FILE *fd;
4175{
4176 char_u charbuf[256 * 4];
4177 int len = 0;
4178 int flags;
4179 int i;
4180
4181 fputc(128, fd); /* <charflagslen> */
4182 for (i = 128; i < 256; ++i)
4183 {
4184 flags = 0;
4185 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004186 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004187 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004188 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004189 fputc(flags, fd); /* <charflags> */
4190
Bram Moolenaarb765d632005-06-07 21:00:02 +00004191#ifdef FEAT_MBYTE
4192 if (has_mbyte)
4193 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
4194 else
4195#endif
4196 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004197 }
4198
4199 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
4200 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
4201}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004202
4203/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004204 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
4205 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004206 * When using a multi-byte 'encoding' the length may change!
4207 * Returns FAIL when something wrong.
4208 */
4209 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004210spell_casefold(str, len, buf, buflen)
4211 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004212 int len;
4213 char_u *buf;
4214 int buflen;
4215{
4216 int i;
4217
4218 if (len >= buflen)
4219 {
4220 buf[0] = NUL;
4221 return FAIL; /* result will not fit */
4222 }
4223
4224#ifdef FEAT_MBYTE
4225 if (has_mbyte)
4226 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004227 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004228 char_u *p;
4229 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004230
4231 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004232 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004233 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004234 if (outi + MB_MAXBYTES > buflen)
4235 {
4236 buf[outi] = NUL;
4237 return FAIL;
4238 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004239 c = mb_ptr2char_adv(&p);
4240 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004241 }
4242 buf[outi] = NUL;
4243 }
4244 else
4245#endif
4246 {
4247 /* Be quick for non-multibyte encodings. */
4248 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004249 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004250 buf[i] = NUL;
4251 }
4252
4253 return OK;
4254}
4255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256/*
4257 * "z?": Find badly spelled word under or after the cursor.
4258 * Give suggestions for the properly spelled word.
4259 * This is based on the mechanisms of Aspell, but completely reimplemented.
4260 */
4261 void
4262spell_suggest()
4263{
4264 char_u *line;
4265 pos_T prev_cursor = curwin->w_cursor;
4266 int attr;
4267 char_u wcopy[MAXWLEN + 2];
4268 char_u *p;
4269 int i;
4270 int c;
4271 suginfo_T sug;
4272 suggest_T *stp;
4273
4274 /*
4275 * Find the start of the badly spelled word.
4276 */
4277 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
4278 {
4279 beep_flush();
4280 return;
4281 }
4282
4283 /*
4284 * Set the info in "sug".
4285 */
4286 vim_memset(&sug, 0, sizeof(sug));
4287 ga_init2(&sug.su_ga, (int)sizeof(suggest_T), 10);
4288 hash_init(&sug.su_banned);
4289 line = ml_get_curline();
4290 sug.su_badptr = line + curwin->w_cursor.col;
4291 sug.su_badlen = spell_check(curwin, sug.su_badptr, &attr);
4292 if (sug.su_badlen >= MAXWLEN)
4293 sug.su_badlen = MAXWLEN - 1; /* just in case */
4294 vim_strncpy(sug.su_badword, sug.su_badptr, sug.su_badlen);
4295 (void)spell_casefold(sug.su_badptr, sug.su_badlen,
4296 sug.su_fbadword, MAXWLEN);
4297
4298 /* Ban the bad word itself. It may appear in another region. */
4299 add_banned(&sug, sug.su_badword);
4300
4301 /*
4302 * 1. Try inserting/deleting/swapping/changing a letter, use REP entries
4303 * from the .aff file and inserting a space (split the word).
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004304 *
4305 * Set a maximum score to limit the combination of operations that is
4306 * tried.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004307 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 sug.su_maxscore = SCORE_MAXINIT;
4309 spell_try_change(&sug);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310
4311 /*
4312 * 2. Try finding sound-a-like words.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004313 *
4314 * Only do this when we don't have a lot of suggestions yet, because it's
4315 * very slow and often doesn't find new suggestions.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004316 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004317 if (sug.su_ga.ga_len < SUG_CLEAN_COUNT)
4318 {
4319 /* Allow a higher score now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 sug.su_maxscore = SCORE_MAXMAX;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004321 spell_try_soundalike(&sug);
4322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323
4324 /* When CTRL-C was hit while searching do show the results. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004325 ui_breakcheck();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (got_int)
4327 {
4328 (void)vgetc();
4329 got_int = FALSE;
4330 }
4331
4332 if (sug.su_ga.ga_len == 0)
4333 MSG(_("Sorry, no suggestions"));
4334 else
4335 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004336#ifdef RESCORE
4337 /* Do slow but more accurate computation of the word score. */
4338 rescore_suggestions(&sug);
4339#endif
4340
4341 /* Sort the suggestions and truncate at SUG_PROMPT_COUNT. */
4342 cleanup_suggestions(&sug, SUG_PROMPT_COUNT);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343
4344 /* List the suggestions. */
4345 msg_start();
4346 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
4347 sug.su_badlen, sug.su_badptr);
4348 msg_puts(IObuff);
4349 msg_clr_eos();
4350 msg_putchar('\n');
4351 msg_scroll = TRUE;
4352 for (i = 0; i < sug.su_ga.ga_len; ++i)
4353 {
4354 stp = &SUG(&sug, i);
4355
4356 /* The suggested word may replace only part of the bad word, add
4357 * the not replaced part. */
4358 STRCPY(wcopy, stp->st_word);
4359 if (sug.su_badlen > stp->st_orglen)
4360 vim_strncpy(wcopy + STRLEN(wcopy),
4361 sug.su_badptr + stp->st_orglen,
4362 sug.su_badlen - stp->st_orglen);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004363 if (p_verbose > 0)
4364 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\" (%d)"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 i + 1, wcopy, stp->st_score);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004366 else
4367 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""),
4368 i + 1, wcopy);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004369 msg_puts(IObuff);
4370 lines_left = 3; /* avoid more prompt */
4371 msg_putchar('\n');
4372 }
4373
4374 /* Ask for choice. */
4375 i = prompt_for_number();
4376 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor())
4377 {
4378 /* Replace the word. */
4379 stp = &SUG(&sug, i - 1);
4380 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
4381 if (p != NULL)
4382 {
4383 c = sug.su_badptr - line;
4384 mch_memmove(p, line, c);
4385 STRCPY(p + c, stp->st_word);
4386 STRCAT(p, sug.su_badptr + stp->st_orglen);
4387 ml_replace(curwin->w_cursor.lnum, p, FALSE);
4388 curwin->w_cursor.col = c;
4389 changed_bytes(curwin->w_cursor.lnum, c);
4390 }
4391 }
4392 else
4393 curwin->w_cursor = prev_cursor;
4394 }
4395
4396 /* Free the suggestions. */
4397 for (i = 0; i < sug.su_ga.ga_len; ++i)
4398 vim_free(SUG(&sug, i).st_word);
4399 ga_clear(&sug.su_ga);
4400
4401 /* Free the banned words. */
4402 free_banned(&sug);
4403}
4404
4405/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004406 * Make a copy of "word", with the first letter upper or lower cased, to
4407 * "wcopy[MAXWLEN]". "word" must not be empty.
4408 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 */
4410 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004411onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 char_u *wcopy;
4414 int upper; /* TRUE: first letter made upper case */
4415{
4416 char_u *p;
4417 int c;
4418 int l;
4419
4420 p = word;
4421#ifdef FEAT_MBYTE
4422 if (has_mbyte)
4423 c = mb_ptr2char_adv(&p);
4424 else
4425#endif
4426 c = *p++;
4427 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004428 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004430 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004431#ifdef FEAT_MBYTE
4432 if (has_mbyte)
4433 l = mb_char2bytes(c, wcopy);
4434 else
4435#endif
4436 {
4437 l = 1;
4438 wcopy[0] = c;
4439 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004440 vim_strncpy(wcopy + l, p, MAXWLEN - l);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441}
4442
4443/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004444 * Make a copy of "word" with all the letters upper cased into
4445 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004446 */
4447 static void
4448allcap_copy(word, wcopy)
4449 char_u *word;
4450 char_u *wcopy;
4451{
4452 char_u *s;
4453 char_u *d;
4454 int c;
4455
4456 d = wcopy;
4457 for (s = word; *s != NUL; )
4458 {
4459#ifdef FEAT_MBYTE
4460 if (has_mbyte)
4461 c = mb_ptr2char_adv(&s);
4462 else
4463#endif
4464 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004465 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004466
4467#ifdef FEAT_MBYTE
4468 if (has_mbyte)
4469 {
4470 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
4471 break;
4472 d += mb_char2bytes(c, d);
4473 }
4474 else
4475#endif
4476 {
4477 if (d - wcopy >= MAXWLEN - 1)
4478 break;
4479 *d++ = c;
4480 }
4481 }
4482 *d = NUL;
4483}
4484
4485/*
4486 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00004487 *
4488 * This uses a state machine. At each node in the tree we try various
4489 * operations. When trying if an operation work "depth" is increased and the
4490 * stack[] is used to store info. This allows combinations, thus insert one
4491 * character, replace one and delete another. The number of changes is
4492 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004493 */
4494 static void
4495spell_try_change(su)
4496 suginfo_T *su;
4497{
4498 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
4499 char_u tword[MAXWLEN]; /* good word collected so far */
4500 trystate_T stack[MAXWLEN];
4501 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
4502 * to for word split) */
4503 char_u prewordlen = 0; /* length of word in "preword" */
4504 int splitoff = 0; /* index in tword after last split */
4505 trystate_T *sp;
4506 int newscore;
4507 langp_T *lp;
4508 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004509 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004510 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00004511 int c, c2, c3;
4512 int n = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004513 int flags;
4514 int badflags;
4515 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004516 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 int len;
4518 char_u *p;
4519 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00004520 int fl = 0, tl;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004521
4522 /* get caps flags for bad word */
4523 badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
4524
4525 /* We make a copy of the case-folded bad word, so that we can modify it
4526 * to find matches (esp. REP items). */
4527 STRCPY(fword, su->su_fbadword);
4528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004529
4530 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
4531 lp->lp_slang != NULL; ++lp)
4532 {
4533#ifdef SOUNDFOLD_SCORE
4534 su->su_slang = lp->lp_slang;
4535 if (lp->lp_slang->sl_sal.ga_len > 0)
4536 /* soundfold the bad word */
4537 spell_soundfold(lp->lp_slang, su->su_fbadword, su->su_salword);
4538#endif
4539
4540 /*
4541 * Go through the whole case-fold tree, try changes at each node.
4542 * "tword[]" contains the word collected from nodes in the tree.
4543 * "fword[]" the word we are trying to match with (initially the bad
4544 * word).
4545 */
4546 byts = lp->lp_slang->sl_fbyts;
4547 idxs = lp->lp_slang->sl_fidxs;
4548
4549 depth = 0;
4550 stack[0].ts_state = STATE_START;
4551 stack[0].ts_score = 0;
4552 stack[0].ts_curi = 1;
4553 stack[0].ts_fidx = 0;
4554 stack[0].ts_fidxtry = 0;
4555 stack[0].ts_twordlen = 0;
4556 stack[0].ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00004557#ifdef FEAT_MBYTE
4558 stack[0].ts_tcharlen = 0;
4559#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004560
Bram Moolenaarea424162005-06-16 21:51:00 +00004561 /*
4562 * Loop to find all suggestions. At each round we either:
4563 * - For the current state try one operation, advance "ts_curi",
4564 * increase "depth".
4565 * - When a state is done go to the next, set "ts_state".
4566 * - When all states are tried decrease "depth".
4567 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004568 while (depth >= 0 && !got_int)
4569 {
4570 sp = &stack[depth];
4571 switch (sp->ts_state)
4572 {
4573 case STATE_START:
4574 /*
4575 * Start of node: Deal with NUL bytes, which means
4576 * tword[] may end here.
4577 */
4578 arridx = sp->ts_arridx; /* current node in the tree */
4579 len = byts[arridx]; /* bytes in this node */
4580 arridx += sp->ts_curi; /* index of current byte */
4581
4582 if (sp->ts_curi > len || (c = byts[arridx]) != 0)
4583 {
4584 /* Past bytes in node and/or past NUL bytes. */
4585 sp->ts_state = STATE_ENDNUL;
4586 break;
4587 }
4588
4589 /*
4590 * End of word in tree.
4591 */
4592 ++sp->ts_curi; /* eat one NUL byte */
4593
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004594 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595
4596 /*
4597 * Form the word with proper case in preword.
4598 * If there is a word from a previous split, append.
4599 */
4600 tword[sp->ts_twordlen] = NUL;
4601 if (flags & WF_KEEPCAP)
4602 /* Must find the word in the keep-case tree. */
4603 find_keepcap_word(lp->lp_slang, tword + splitoff,
4604 preword + prewordlen);
4605 else
4606 /* Include badflags: if the badword is onecap or allcap
4607 * use that for the goodword too. */
4608 make_case_word(tword + splitoff,
4609 preword + prewordlen, flags | badflags);
4610
4611 /* Don't use a banned word. It may appear again as a good
4612 * word, thus remember it. */
4613 if (flags & WF_BANNED)
4614 {
4615 add_banned(su, preword + prewordlen);
4616 break;
4617 }
4618 if (was_banned(su, preword + prewordlen))
4619 break;
4620
4621 newscore = 0;
4622 if ((flags & WF_REGION)
4623 && (((unsigned)flags >> 8) & lp->lp_region) == 0)
4624 newscore += SCORE_REGION;
4625 if (flags & WF_RARE)
4626 newscore += SCORE_RARE;
4627
4628 if (!spell_valid_case(badflags,
4629 captype(preword + prewordlen, NULL)))
4630 newscore += SCORE_ICASE;
4631
4632 if (fword[sp->ts_fidx] == 0)
4633 {
4634 /* The badword also ends: add suggestions, */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004635 add_suggestion(su, preword, sp->ts_score + newscore
4636#ifdef RESCORE
4637 , FALSE
4638#endif
4639 );
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004640 }
Bram Moolenaarea424162005-06-16 21:51:00 +00004641 else if (sp->ts_fidx >= sp->ts_fidxtry
4642#ifdef FEAT_MBYTE
4643 /* Don't split halfway a character. */
4644 && (!has_mbyte || sp->ts_tcharlen == 0)
4645#endif
4646 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 {
4648 /* The word in the tree ends but the badword
4649 * continues: try inserting a space and check that a valid
4650 * words starts at fword[sp->ts_fidx]. */
4651 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
4652 {
4653 /* Save things to be restored at STATE_SPLITUNDO. */
4654 sp->ts_save_prewordlen = prewordlen;
4655 sp->ts_save_badflags = badflags;
4656 sp->ts_save_splitoff = splitoff;
4657
4658 /* Append a space to preword. */
4659 STRCAT(preword, " ");
4660 prewordlen = STRLEN(preword);
4661 splitoff = sp->ts_twordlen;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004662#ifdef FEAT_MBYTE
4663 if (has_mbyte)
4664 {
4665 int i = 0;
4666
4667 /* Case-folding may change the number of bytes:
4668 * Count nr of chars in fword[sp->ts_fidx] and
4669 * advance that many chars in su->su_badptr. */
4670 for (p = fword; p < fword + sp->ts_fidx;
4671 mb_ptr_adv(p))
4672 ++i;
4673 for (p = su->su_badptr; i > 0; mb_ptr_adv(p))
4674 --i;
4675 }
4676 else
4677#endif
4678 p = su->su_badptr + sp->ts_fidx;
4679 badflags = captype(p, su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004680
4681 sp->ts_state = STATE_SPLITUNDO;
4682 ++depth;
4683 /* Restart at top of the tree. */
4684 stack[depth].ts_arridx = 0;
4685 }
4686 }
4687 break;
4688
4689 case STATE_SPLITUNDO:
4690 /* Fixup the changes done for word split. */
4691 badflags = sp->ts_save_badflags;
4692 splitoff = sp->ts_save_splitoff;
4693 prewordlen = sp->ts_save_prewordlen;
4694
4695 /* Continue looking for NUL bytes. */
4696 sp->ts_state = STATE_START;
4697 break;
4698
4699 case STATE_ENDNUL:
4700 /* Past the NUL bytes in the node. */
4701 if (fword[sp->ts_fidx] == 0)
4702 {
4703 /* The badword ends, can't use the bytes in this node. */
4704 sp->ts_state = STATE_DEL;
4705 break;
4706 }
4707 sp->ts_state = STATE_PLAIN;
4708 /*FALLTHROUGH*/
4709
4710 case STATE_PLAIN:
4711 /*
4712 * Go over all possible bytes at this node, add each to
4713 * tword[] and use child node. "ts_curi" is the index.
4714 */
4715 arridx = sp->ts_arridx;
4716 if (sp->ts_curi > byts[arridx])
4717 {
4718 /* Done all bytes at this node, do next state. When still
4719 * at already changed bytes skip the other tricks. */
4720 if (sp->ts_fidx >= sp->ts_fidxtry)
4721 sp->ts_state = STATE_DEL;
4722 else
4723 sp->ts_state = STATE_FINAL;
4724 }
4725 else
4726 {
4727 arridx += sp->ts_curi++;
4728 c = byts[arridx];
4729
4730 /* Normal byte, go one level deeper. If it's not equal to
4731 * the byte in the bad word adjust the score. But don't
4732 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00004733 if (c == fword[sp->ts_fidx]
4734#ifdef FEAT_MBYTE
4735 || (sp->ts_tcharlen > 0
4736 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004737#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00004738 )
4739 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004740 else
4741 newscore = SCORE_SUBST;
4742 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
4743 && try_deeper(su, stack, depth, newscore))
4744 {
4745 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00004746 sp = &stack[depth];
4747 ++sp->ts_fidx;
4748 tword[sp->ts_twordlen++] = c;
4749 sp->ts_arridx = idxs[arridx];
4750#ifdef FEAT_MBYTE
4751 if (newscore == SCORE_SUBST)
4752 sp->ts_isdiff = DIFF_YES;
4753 if (has_mbyte)
4754 {
4755 /* Multi-byte characters are a bit complicated to
4756 * handle: They differ when any of the bytes
4757 * differ and then their length may also differ. */
4758 if (sp->ts_tcharlen == 0)
4759 {
4760 /* First byte. */
4761 sp->ts_tcharidx = 0;
4762 sp->ts_tcharlen = MB_BYTE2LEN(c);
4763 sp->ts_fcharstart = sp->ts_fidx - 1;
4764 sp->ts_isdiff = (newscore != 0)
4765 ? DIFF_YES : DIFF_NONE;
4766 }
4767 else if (sp->ts_isdiff == DIFF_INSERT)
4768 /* When inserting trail bytes don't advance in
4769 * the bad word. */
4770 --sp->ts_fidx;
4771 if (++sp->ts_tcharidx == sp->ts_tcharlen)
4772 {
4773 /* Last byte of character. */
4774 if (sp->ts_isdiff == DIFF_YES)
4775 {
4776 /* Correct ts_fidx for the byte length of
4777 * the character (we didn't check that
4778 * before). */
4779 sp->ts_fidx = sp->ts_fcharstart
4780 + MB_BYTE2LEN(
4781 fword[sp->ts_fcharstart]);
4782
4783 /* For a similar character adjust score
4784 * from SCORE_SUBST to SCORE_SIMILAR. */
4785 if (lp->lp_slang->sl_has_map
4786 && similar_chars(lp->lp_slang,
4787 mb_ptr2char(tword
4788 + sp->ts_twordlen
4789 - sp->ts_tcharlen),
4790 mb_ptr2char(fword
4791 + sp->ts_fcharstart)))
4792 sp->ts_score -=
4793 SCORE_SUBST - SCORE_SIMILAR;
4794 }
4795
4796 /* Starting a new char, reset the length. */
4797 sp->ts_tcharlen = 0;
4798 }
4799 }
4800 else
4801#endif
4802 {
4803 /* If we found a similar char adjust the score.
4804 * We do this after calling try_deeper() because
4805 * it's slow. */
4806 if (newscore != 0
4807 && lp->lp_slang->sl_has_map
4808 && similar_chars(lp->lp_slang,
4809 c, fword[sp->ts_fidx - 1]))
4810 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
4811 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 }
4813 }
4814 break;
4815
4816 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00004817#ifdef FEAT_MBYTE
4818 /* When past the first byte of a multi-byte char don't try
4819 * delete/insert/swap a character. */
4820 if (has_mbyte && sp->ts_tcharlen > 0)
4821 {
4822 sp->ts_state = STATE_FINAL;
4823 break;
4824 }
4825#endif
4826 /*
4827 * Try skipping one character in the bad word (delete it).
4828 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 sp->ts_state = STATE_INS;
4830 sp->ts_curi = 1;
4831 if (fword[sp->ts_fidx] != NUL
4832 && try_deeper(su, stack, depth, SCORE_DEL))
4833 {
4834 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00004835#ifdef FEAT_MBYTE
4836 if (has_mbyte)
4837 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
4838 else
4839#endif
4840 ++stack[depth].ts_fidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 break;
4842 }
4843 /*FALLTHROUGH*/
4844
4845 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00004846 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004847 * node. */
4848 n = sp->ts_arridx;
4849 if (sp->ts_curi > byts[n])
4850 {
4851 /* Done all bytes at this node, do next state. */
4852 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004853 }
4854 else
4855 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004856 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857 n += sp->ts_curi++;
4858 c = byts[n];
4859 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
4860 {
4861 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00004862 sp = &stack[depth];
4863 tword[sp->ts_twordlen++] = c;
4864 sp->ts_arridx = idxs[n];
4865#ifdef FEAT_MBYTE
4866 if (has_mbyte)
4867 {
4868 fl = MB_BYTE2LEN(c);
4869 if (fl > 1)
4870 {
4871 /* There are following bytes for the same
4872 * character. We must find all bytes before
4873 * trying delete/insert/swap/etc. */
4874 sp->ts_tcharlen = fl;
4875 sp->ts_tcharidx = 1;
4876 sp->ts_isdiff = DIFF_INSERT;
4877 }
4878 }
4879#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 }
4881 }
4882 break;
4883
4884 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00004885 /*
4886 * Swap two bytes in the bad word: "12" -> "21".
4887 * We change "fword" here, it's changed back afterwards.
4888 */
4889 p = fword + sp->ts_fidx;
4890 c = *p;
4891 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004892 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004893 /* End of word, can't swap or replace. */
4894 sp->ts_state = STATE_FINAL;
4895 break;
4896 }
4897#ifdef FEAT_MBYTE
4898 if (has_mbyte)
4899 {
4900 n = mb_ptr2len_check(p);
4901 c = mb_ptr2char(p);
4902 c2 = mb_ptr2char(p + n);
4903 }
4904 else
4905#endif
4906 c2 = p[1];
4907 if (c == c2)
4908 {
4909 /* Characters are identical, swap won't do anything. */
4910 sp->ts_state = STATE_SWAP3;
4911 break;
4912 }
4913 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
4914 {
4915 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00004917#ifdef FEAT_MBYTE
4918 if (has_mbyte)
4919 {
4920 fl = mb_char2len(c2);
4921 mch_memmove(p, p + n, fl);
4922 mb_char2bytes(c, p + fl);
4923 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
4924 }
4925 else
4926#endif
4927 {
4928 p[0] = c2;
4929 p[1] = c;
4930 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
4931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004932 }
4933 else
4934 /* If this swap doesn't work then SWAP3 won't either. */
4935 sp->ts_state = STATE_REP_INI;
4936 break;
4937
Bram Moolenaarea424162005-06-16 21:51:00 +00004938 case STATE_UNSWAP:
4939 /* Undo the STATE_SWAP swap: "21" -> "12". */
4940 p = fword + sp->ts_fidx;
4941#ifdef FEAT_MBYTE
4942 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004943 {
Bram Moolenaarea424162005-06-16 21:51:00 +00004944 n = MB_BYTE2LEN(*p);
4945 c = mb_ptr2char(p + n);
4946 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
4947 mb_char2bytes(c, p);
4948 }
4949 else
4950#endif
4951 {
4952 c = *p;
4953 *p = p[1];
4954 p[1] = c;
4955 }
4956 /*FALLTHROUGH*/
4957
4958 case STATE_SWAP3:
4959 /* Swap two bytes, skipping one: "123" -> "321". We change
4960 * "fword" here, it's changed back afterwards. */
4961 p = fword + sp->ts_fidx;
4962#ifdef FEAT_MBYTE
4963 if (has_mbyte)
4964 {
4965 n = mb_ptr2len_check(p);
4966 c = mb_ptr2char(p);
4967 fl = mb_ptr2len_check(p + n);
4968 c2 = mb_ptr2char(p + n);
4969 c3 = mb_ptr2char(p + n + fl);
4970 }
4971 else
4972#endif
4973 {
4974 c = *p;
4975 c2 = p[1];
4976 c3 = p[2];
4977 }
4978
4979 /* When characters are identical: "121" then SWAP3 result is
4980 * identical, ROT3L result is same as SWAP: "211", ROT3L
4981 * result is same as SWAP on next char: "112". Thus skip all
4982 * swapping. Also skip when c3 is NUL. */
4983 if (c == c3 || c3 == NUL)
4984 {
4985 sp->ts_state = STATE_REP_INI;
4986 break;
4987 }
4988 if (try_deeper(su, stack, depth, SCORE_SWAP3))
4989 {
4990 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004991 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00004992#ifdef FEAT_MBYTE
4993 if (has_mbyte)
4994 {
4995 tl = mb_char2len(c3);
4996 mch_memmove(p, p + n + fl, tl);
4997 mb_char2bytes(c2, p + tl);
4998 mb_char2bytes(c, p + fl + tl);
4999 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
5000 }
5001 else
5002#endif
5003 {
5004 p[0] = p[2];
5005 p[2] = c;
5006 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005008 }
5009 else
5010 sp->ts_state = STATE_REP_INI;
5011 break;
5012
Bram Moolenaarea424162005-06-16 21:51:00 +00005013 case STATE_UNSWAP3:
5014 /* Undo STATE_SWAP3: "321" -> "123" */
5015 p = fword + sp->ts_fidx;
5016#ifdef FEAT_MBYTE
5017 if (has_mbyte)
5018 {
5019 n = MB_BYTE2LEN(*p);
5020 c2 = mb_ptr2char(p + n);
5021 fl = MB_BYTE2LEN(p[n]);
5022 c = mb_ptr2char(p + n + fl);
5023 tl = MB_BYTE2LEN(p[n + fl]);
5024 mch_memmove(p + fl + tl, p, n);
5025 mb_char2bytes(c, p);
5026 mb_char2bytes(c2, p + tl);
5027 }
5028 else
5029#endif
5030 {
5031 c = *p;
5032 *p = p[2];
5033 p[2] = c;
5034 }
5035 /*FALLTHROUGH*/
5036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005037 case STATE_ROT3L:
Bram Moolenaarea424162005-06-16 21:51:00 +00005038 /* Rotate three characters left: "123" -> "231". We change
5039 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005040 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5041 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005042 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005043 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005044 p = fword + sp->ts_fidx;
5045#ifdef FEAT_MBYTE
5046 if (has_mbyte)
5047 {
5048 n = mb_ptr2len_check(p);
5049 c = mb_ptr2char(p);
5050 fl = mb_ptr2len_check(p + n);
5051 fl += mb_ptr2len_check(p + n + fl);
5052 mch_memmove(p, p + n, fl);
5053 mb_char2bytes(c, p + fl);
5054 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
5055 }
5056 else
5057#endif
5058 {
5059 c = *p;
5060 *p = p[1];
5061 p[1] = p[2];
5062 p[2] = c;
5063 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005065 }
5066 else
5067 sp->ts_state = STATE_REP_INI;
5068 break;
5069
Bram Moolenaarea424162005-06-16 21:51:00 +00005070 case STATE_UNROT3L:
5071 /* Undo STATE_ROT3L: "231" -> "123" */
5072 p = fword + sp->ts_fidx;
5073#ifdef FEAT_MBYTE
5074 if (has_mbyte)
5075 {
5076 n = MB_BYTE2LEN(*p);
5077 n += MB_BYTE2LEN(p[n]);
5078 c = mb_ptr2char(p + n);
5079 tl = MB_BYTE2LEN(p[n]);
5080 mch_memmove(p + tl, p, n);
5081 mb_char2bytes(c, p);
5082 }
5083 else
5084#endif
5085 {
5086 c = p[2];
5087 p[2] = p[1];
5088 p[1] = *p;
5089 *p = c;
5090 }
5091 /*FALLTHROUGH*/
5092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005093 case STATE_ROT3R:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005094 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00005095 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005096 if (try_deeper(su, stack, depth, SCORE_SWAP3))
5097 {
Bram Moolenaarea424162005-06-16 21:51:00 +00005098 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005099 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00005100 p = fword + sp->ts_fidx;
5101#ifdef FEAT_MBYTE
5102 if (has_mbyte)
5103 {
5104 n = mb_ptr2len_check(p);
5105 n += mb_ptr2len_check(p + n);
5106 c = mb_ptr2char(p + n);
5107 tl = mb_ptr2len_check(p + n);
5108 mch_memmove(p + tl, p, n);
5109 mb_char2bytes(c, p);
5110 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
5111 }
5112 else
5113#endif
5114 {
5115 c = p[2];
5116 p[2] = p[1];
5117 p[1] = *p;
5118 *p = c;
5119 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
5120 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005121 }
5122 else
5123 sp->ts_state = STATE_REP_INI;
5124 break;
5125
Bram Moolenaarea424162005-06-16 21:51:00 +00005126 case STATE_UNROT3R:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 /* Undo STATE_ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00005128 p = fword + sp->ts_fidx;
5129#ifdef FEAT_MBYTE
5130 if (has_mbyte)
5131 {
5132 c = mb_ptr2char(p);
5133 tl = MB_BYTE2LEN(*p);
5134 n = MB_BYTE2LEN(p[tl]);
5135 n += MB_BYTE2LEN(p[tl + n]);
5136 mch_memmove(p, p + tl, n);
5137 mb_char2bytes(c, p + n);
5138 }
5139 else
5140#endif
5141 {
5142 c = *p;
5143 *p = p[1];
5144 p[1] = p[2];
5145 p[2] = c;
5146 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005147 /*FALLTHROUGH*/
5148
5149 case STATE_REP_INI:
5150 /* Check if matching with REP items from the .aff file would
5151 * work. Quickly skip if there are no REP items or the score
5152 * is going to be too high anyway. */
5153 gap = &lp->lp_slang->sl_rep;
5154 if (gap->ga_len == 0
5155 || sp->ts_score + SCORE_REP >= su->su_maxscore)
5156 {
5157 sp->ts_state = STATE_FINAL;
5158 break;
5159 }
5160
5161 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00005162 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005163 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
5164 if (sp->ts_curi < 0)
5165 {
5166 sp->ts_state = STATE_FINAL;
5167 break;
5168 }
5169
5170 sp->ts_state = STATE_REP;
5171 /*FALLTHROUGH*/
5172
5173 case STATE_REP:
5174 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00005175 * match replace the characters and check if the resulting
5176 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005177 p = fword + sp->ts_fidx;
5178
5179 gap = &lp->lp_slang->sl_rep;
5180 while (sp->ts_curi < gap->ga_len)
5181 {
5182 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
5183 if (*ftp->ft_from != *p)
5184 {
5185 /* past possible matching entries */
5186 sp->ts_curi = gap->ga_len;
5187 break;
5188 }
5189 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
5190 && try_deeper(su, stack, depth, SCORE_REP))
5191 {
5192 /* Need to undo this afterwards. */
5193 sp->ts_state = STATE_REP_UNDO;
5194
5195 /* Change the "from" to the "to" string. */
5196 ++depth;
5197 fl = STRLEN(ftp->ft_from);
5198 tl = STRLEN(ftp->ft_to);
5199 if (fl != tl)
5200 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
5201 mch_memmove(p, ftp->ft_to, tl);
5202 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00005203#ifdef FEAT_MBYTE
5204 stack[depth].ts_tcharlen = 0;
5205#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 break;
5207 }
5208 }
5209
5210 if (sp->ts_curi >= gap->ga_len)
5211 /* No (more) matches. */
5212 sp->ts_state = STATE_FINAL;
5213
5214 break;
5215
5216 case STATE_REP_UNDO:
5217 /* Undo a REP replacement and continue with the next one. */
5218 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
5219 + sp->ts_curi - 1;
5220 fl = STRLEN(ftp->ft_from);
5221 tl = STRLEN(ftp->ft_to);
5222 p = fword + sp->ts_fidx;
5223 if (fl != tl)
5224 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
5225 mch_memmove(p, ftp->ft_from, fl);
5226 sp->ts_state = STATE_REP;
5227 break;
5228
5229 default:
5230 /* Did all possible states at this level, go up one level. */
5231 --depth;
5232 }
5233
5234 line_breakcheck();
5235 }
5236 }
5237}
5238
5239/*
5240 * Try going one level deeper in the tree.
5241 */
5242 static int
5243try_deeper(su, stack, depth, score_add)
5244 suginfo_T *su;
5245 trystate_T *stack;
5246 int depth;
5247 int score_add;
5248{
5249 int newscore;
5250
5251 /* Refuse to go deeper if the scrore is getting too big. */
5252 newscore = stack[depth].ts_score + score_add;
5253 if (newscore >= su->su_maxscore)
5254 return FALSE;
5255
Bram Moolenaarea424162005-06-16 21:51:00 +00005256 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 stack[depth + 1].ts_state = STATE_START;
5258 stack[depth + 1].ts_score = newscore;
5259 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 return TRUE;
5261}
5262
5263/*
5264 * "fword" is a good word with case folded. Find the matching keep-case
5265 * words and put it in "kword".
5266 * Theoretically there could be several keep-case words that result in the
5267 * same case-folded word, but we only find one...
5268 */
5269 static void
5270find_keepcap_word(slang, fword, kword)
5271 slang_T *slang;
5272 char_u *fword;
5273 char_u *kword;
5274{
5275 char_u uword[MAXWLEN]; /* "fword" in upper-case */
5276 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005277 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278
5279 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005280 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281 int round[MAXWLEN];
5282 int fwordidx[MAXWLEN];
5283 int uwordidx[MAXWLEN];
5284 int kwordlen[MAXWLEN];
5285
5286 int flen, ulen;
5287 int l;
5288 int len;
5289 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005290 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 char_u *p;
5292 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005293 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294
5295 if (byts == NULL)
5296 {
5297 /* array is empty: "cannot happen" */
5298 *kword = NUL;
5299 return;
5300 }
5301
5302 /* Make an all-cap version of "fword". */
5303 allcap_copy(fword, uword);
5304
5305 /*
5306 * Each character needs to be tried both case-folded and upper-case.
5307 * All this gets very complicated if we keep in mind that changing case
5308 * may change the byte length of a multi-byte character...
5309 */
5310 depth = 0;
5311 arridx[0] = 0;
5312 round[0] = 0;
5313 fwordidx[0] = 0;
5314 uwordidx[0] = 0;
5315 kwordlen[0] = 0;
5316 while (depth >= 0)
5317 {
5318 if (fword[fwordidx[depth]] == NUL)
5319 {
5320 /* We are at the end of "fword". If the tree allows a word to end
5321 * here we have found a match. */
5322 if (byts[arridx[depth] + 1] == 0)
5323 {
5324 kword[kwordlen[depth]] = NUL;
5325 return;
5326 }
5327
5328 /* kword is getting too long, continue one level up */
5329 --depth;
5330 }
5331 else if (++round[depth] > 2)
5332 {
5333 /* tried both fold-case and upper-case character, continue one
5334 * level up */
5335 --depth;
5336 }
5337 else
5338 {
5339 /*
5340 * round[depth] == 1: Try using the folded-case character.
5341 * round[depth] == 2: Try using the upper-case character.
5342 */
5343#ifdef FEAT_MBYTE
5344 if (has_mbyte)
5345 {
5346 flen = mb_ptr2len_check(fword + fwordidx[depth]);
5347 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
5348 }
5349 else
5350#endif
5351 ulen = flen = 1;
5352 if (round[depth] == 1)
5353 {
5354 p = fword + fwordidx[depth];
5355 l = flen;
5356 }
5357 else
5358 {
5359 p = uword + uwordidx[depth];
5360 l = ulen;
5361 }
5362
5363 for (tryidx = arridx[depth]; l > 0; --l)
5364 {
5365 /* Perform a binary search in the list of accepted bytes. */
5366 len = byts[tryidx++];
5367 c = *p++;
5368 lo = tryidx;
5369 hi = tryidx + len - 1;
5370 while (lo < hi)
5371 {
5372 m = (lo + hi) / 2;
5373 if (byts[m] > c)
5374 hi = m - 1;
5375 else if (byts[m] < c)
5376 lo = m + 1;
5377 else
5378 {
5379 lo = hi = m;
5380 break;
5381 }
5382 }
5383
5384 /* Stop if there is no matching byte. */
5385 if (hi < lo || byts[lo] != c)
5386 break;
5387
5388 /* Continue at the child (if there is one). */
5389 tryidx = idxs[lo];
5390 }
5391
5392 if (l == 0)
5393 {
5394 /*
5395 * Found the matching char. Copy it to "kword" and go a
5396 * level deeper.
5397 */
5398 if (round[depth] == 1)
5399 {
5400 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
5401 flen);
5402 kwordlen[depth + 1] = kwordlen[depth] + flen;
5403 }
5404 else
5405 {
5406 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
5407 ulen);
5408 kwordlen[depth + 1] = kwordlen[depth] + ulen;
5409 }
5410 fwordidx[depth + 1] = fwordidx[depth] + flen;
5411 uwordidx[depth + 1] = uwordidx[depth] + ulen;
5412
5413 ++depth;
5414 arridx[depth] = tryidx;
5415 round[depth] = 0;
5416 }
5417 }
5418 }
5419
5420 /* Didn't find it: "cannot happen". */
5421 *kword = NUL;
5422}
5423
5424/*
5425 * Find suggestions by comparing the word in a sound-a-like form.
5426 */
5427 static void
5428spell_try_soundalike(su)
5429 suginfo_T *su;
5430{
5431 char_u salword[MAXWLEN];
5432 char_u tword[MAXWLEN];
5433 char_u tfword[MAXWLEN];
5434 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005435 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005436 int curi[MAXWLEN];
5437 langp_T *lp;
5438 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005439 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005440 int depth;
5441 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005442 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005443 int round;
5444 int flags;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005445 int score, sound_score;
5446 char_u *bp, *sp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005447
5448 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5449 lp->lp_slang != NULL; ++lp)
5450 {
5451 if (lp->lp_slang->sl_sal.ga_len > 0)
5452 {
5453 /* soundfold the bad word */
5454 spell_soundfold(lp->lp_slang, su->su_fbadword, salword);
5455
5456 /*
5457 * Go through the whole tree, soundfold each word and compare.
5458 * round 1: use the case-folded tree.
5459 * round 2: use the keep-case tree.
5460 */
5461 for (round = 1; round <= 2; ++round)
5462 {
5463 if (round == 1)
5464 {
5465 byts = lp->lp_slang->sl_fbyts;
5466 idxs = lp->lp_slang->sl_fidxs;
5467 }
5468 else
5469 {
5470 byts = lp->lp_slang->sl_kbyts;
5471 idxs = lp->lp_slang->sl_kidxs;
5472 }
5473
5474 depth = 0;
5475 arridx[0] = 0;
5476 curi[0] = 1;
5477 while (depth >= 0 && !got_int)
5478 {
5479 if (curi[depth] > byts[arridx[depth]])
5480 /* Done all bytes at this node, go up one level. */
5481 --depth;
5482 else
5483 {
5484 /* Do one more byte at this node. */
5485 n = arridx[depth] + curi[depth];
5486 ++curi[depth];
5487 c = byts[n];
5488 if (c == 0)
5489 {
5490 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005491 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005492 if (round == 2 || (flags & WF_KEEPCAP) == 0)
5493 {
5494 tword[depth] = NUL;
5495 if (round == 1)
5496 spell_soundfold(lp->lp_slang,
5497 tword, tsalword);
5498 else
5499 {
5500 /* In keep-case tree need to case-fold the
5501 * word. */
5502 (void)spell_casefold(tword, depth,
5503 tfword, MAXWLEN);
5504 spell_soundfold(lp->lp_slang,
5505 tfword, tsalword);
5506 }
5507
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005508 /*
5509 * Accept the word if the sound-folded words
5510 * are (almost) equal.
5511 */
5512 for (bp = salword, sp = tsalword; *bp == *sp;
5513 ++bp, ++sp)
5514 if (*bp == NUL)
5515 break;
5516
5517 if (*bp == *sp)
5518 /* equal */
5519 sound_score = 0;
5520 else if (*bp != NUL && bp[1] != NUL
5521 && *bp == sp[1] && bp[1] == *sp
5522 && STRCMP(bp + 2, sp + 2) == 0)
5523 /* swap two bytes */
5524 sound_score = SCORE_SWAP;
5525 else if (STRCMP(bp + 1, sp) == 0)
5526 /* delete byte */
5527 sound_score = SCORE_DEL;
5528 else if (STRCMP(bp, sp + 1) == 0)
5529 /* insert byte */
5530 sound_score = SCORE_INS;
5531 else if (STRCMP(bp + 1, sp + 1) == 0)
5532 /* skip one byte */
5533 sound_score = SCORE_SUBST;
5534 else
5535 /* not equal or similar */
5536 sound_score = SCORE_MAXMAX;
5537
5538 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005539 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005540 char_u cword[MAXWLEN];
5541 char_u *p;
5542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005543 if (round == 1 && flags != 0)
5544 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005545 /* Need to fix case according to
5546 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005548 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005549 }
5550 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005551 p = tword;
5552
5553 /* Compute the score. */
5554 score = spell_edit_score(su->su_badword, p);
5555#ifdef RESCORE
5556 /* give a bonus for the good word sounding
5557 * the same as the bad word */
5558 add_suggestion(su, tword,
5559 RESCORE(score, sound_score),
5560 TRUE);
5561#else
5562 add_suggestion(su, tword,
5563 score + sound_score);
5564#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005565 }
5566 }
5567
5568 /* Skip over other NUL bytes. */
5569 while (byts[n + 1] == 0)
5570 {
5571 ++n;
5572 ++curi[depth];
5573 }
5574 }
5575 else
5576 {
5577 /* Normal char, go one level deeper. */
5578 tword[depth++] = c;
5579 arridx[depth] = idxs[n];
5580 curi[depth] = 1;
5581 }
5582 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005583
5584 line_breakcheck();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005585 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005586 }
5587 }
5588 }
5589}
5590
5591/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005592 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005593 */
5594 static void
5595make_case_word(fword, cword, flags)
5596 char_u *fword;
5597 char_u *cword;
5598 int flags;
5599{
5600 if (flags & WF_ALLCAP)
5601 /* Make it all upper-case */
5602 allcap_copy(fword, cword);
5603 else if (flags & WF_ONECAP)
5604 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005605 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005606 else
5607 /* Use goodword as-is. */
5608 STRCPY(cword, fword);
5609}
5610
Bram Moolenaarea424162005-06-16 21:51:00 +00005611/*
5612 * Use map string "map" for languages "lp".
5613 */
5614 static void
5615set_map_str(lp, map)
5616 slang_T *lp;
5617 char_u *map;
5618{
5619 char_u *p;
5620 int headc = 0;
5621 int c;
5622 int i;
5623
5624 if (*map == NUL)
5625 {
5626 lp->sl_has_map = FALSE;
5627 return;
5628 }
5629 lp->sl_has_map = TRUE;
5630
5631 /* Init the array and hash table empty. */
5632 for (i = 0; i < 256; ++i)
5633 lp->sl_map_array[i] = 0;
5634#ifdef FEAT_MBYTE
5635 hash_init(&lp->sl_map_hash);
5636#endif
5637
5638 /*
5639 * The similar characters are stored separated with slashes:
5640 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
5641 * before the same slash. For characters above 255 sl_map_hash is used.
5642 */
5643 for (p = map; *p != NUL; )
5644 {
5645#ifdef FEAT_MBYTE
5646 c = mb_ptr2char_adv(&p);
5647#else
5648 c = *p++;
5649#endif
5650 if (c == '/')
5651 headc = 0;
5652 else
5653 {
5654 if (headc == 0)
5655 headc = c;
5656
5657#ifdef FEAT_MBYTE
5658 /* Characters above 255 don't fit in sl_map_array[], put them in
5659 * the hash table. Each entry is the char, a NUL the headchar and
5660 * a NUL. */
5661 if (c >= 256)
5662 {
5663 int cl = mb_char2len(c);
5664 int headcl = mb_char2len(headc);
5665 char_u *b;
5666 hash_T hash;
5667 hashitem_T *hi;
5668
5669 b = alloc((unsigned)(cl + headcl + 2));
5670 if (b == NULL)
5671 return;
5672 mb_char2bytes(c, b);
5673 b[cl] = NUL;
5674 mb_char2bytes(headc, b + cl + 1);
5675 b[cl + 1 + headcl] = NUL;
5676 hash = hash_hash(b);
5677 hi = hash_lookup(&lp->sl_map_hash, b, hash);
5678 if (HASHITEM_EMPTY(hi))
5679 hash_add_item(&lp->sl_map_hash, hi, b, hash);
5680 else
5681 {
5682 /* This should have been checked when generating the .spl
5683 * file. */
5684 EMSG(_("E999: duplicate char in MAP entry"));
5685 vim_free(b);
5686 }
5687 }
5688 else
5689#endif
5690 lp->sl_map_array[c] = headc;
5691 }
5692 }
5693}
5694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005695/*
5696 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
5697 * lines in the .aff file.
5698 */
5699 static int
5700similar_chars(slang, c1, c2)
5701 slang_T *slang;
5702 int c1;
5703 int c2;
5704{
Bram Moolenaarea424162005-06-16 21:51:00 +00005705 int m1, m2;
5706#ifdef FEAT_MBYTE
5707 char_u buf[MB_MAXBYTES];
5708 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005709
Bram Moolenaarea424162005-06-16 21:51:00 +00005710 if (c1 >= 256)
5711 {
5712 buf[mb_char2bytes(c1, buf)] = 0;
5713 hi = hash_find(&slang->sl_map_hash, buf);
5714 if (HASHITEM_EMPTY(hi))
5715 m1 = 0;
5716 else
5717 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
5718 }
5719 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005720#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00005721 m1 = slang->sl_map_array[c1];
5722 if (m1 == 0)
5723 return FALSE;
5724
5725
5726#ifdef FEAT_MBYTE
5727 if (c2 >= 256)
5728 {
5729 buf[mb_char2bytes(c2, buf)] = 0;
5730 hi = hash_find(&slang->sl_map_hash, buf);
5731 if (HASHITEM_EMPTY(hi))
5732 m2 = 0;
5733 else
5734 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
5735 }
5736 else
5737#endif
5738 m2 = slang->sl_map_array[c2];
5739
5740 return m1 == m2;
5741}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005742
5743/*
5744 * Add a suggestion to the list of suggestions.
5745 * Do not add a duplicate suggestion or suggestions with a bad score.
5746 * When "use_score" is not zero it's used, otherwise the score is computed
5747 * with spell_edit_score().
5748 */
5749 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005750add_suggestion(su, goodword, score
5751#ifdef RESCORE
5752 , had_bonus
5753#endif
5754 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005755 suginfo_T *su;
5756 char_u *goodword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005757 int score;
5758#ifdef RESCORE
5759 int had_bonus; /* set st_had_bonus */
5760#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005761{
5762 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005763 int i;
5764#ifdef SOUNDFOLD_SCORE
5765 char_u fword[MAXWLEN];
5766 char_u salword[MAXWLEN];
5767#endif
5768
5769 /* Check that the word wasn't banned. */
5770 if (was_banned(su, goodword))
5771 return;
5772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005773 if (score <= su->su_maxscore)
5774 {
5775#ifdef SOUNDFOLD_SCORE
5776 /* Add to the score when the word sounds differently.
5777 * This is slow... */
5778 if (su->su_slang->sl_sal.ga_len > 0)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005779 score += spell_sound_score(su->su_slang, fword, su->su_salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005780#endif
5781
5782 /* Check if the word is already there. */
5783 stp = &SUG(su, 0);
5784 for (i = su->su_ga.ga_len - 1; i >= 0; --i)
5785 if (STRCMP(stp[i].st_word, goodword) == 0)
5786 {
5787 /* Found it. Remember the lowest score. */
5788 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005790 stp[i].st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005791#ifdef RESCORE
5792 stp[i].st_had_bonus = had_bonus;
5793#endif
5794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005795 break;
5796 }
5797
5798 if (i < 0 && ga_grow(&su->su_ga, 1) == OK)
5799 {
5800 /* Add a suggestion. */
5801 stp = &SUG(su, su->su_ga.ga_len);
5802 stp->st_word = vim_strsave(goodword);
5803 if (stp->st_word != NULL)
5804 {
5805 stp->st_score = score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005806#ifdef RESCORE
5807 stp->st_had_bonus = had_bonus;
5808#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005809 stp->st_orglen = su->su_badlen;
5810 ++su->su_ga.ga_len;
5811
5812 /* If we have too many suggestions now, sort the list and keep
5813 * the best suggestions. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005814 if (su->su_ga.ga_len > SUG_MAX_COUNT)
5815 cleanup_suggestions(su, SUG_CLEAN_COUNT);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005816 }
5817 }
5818 }
5819}
5820
5821/*
5822 * Add a word to be banned.
5823 */
5824 static void
5825add_banned(su, word)
5826 suginfo_T *su;
5827 char_u *word;
5828{
5829 char_u *s = vim_strsave(word);
5830 hash_T hash;
5831 hashitem_T *hi;
5832
5833 if (s != NULL)
5834 {
5835 hash = hash_hash(s);
5836 hi = hash_lookup(&su->su_banned, s, hash);
5837 if (HASHITEM_EMPTY(hi))
5838 hash_add_item(&su->su_banned, hi, s, hash);
5839 }
5840}
5841
5842/*
5843 * Return TRUE if a word appears in the list of banned words.
5844 */
5845 static int
5846was_banned(su, word)
5847 suginfo_T *su;
5848 char_u *word;
5849{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005850 hashitem_T *hi = hash_find(&su->su_banned, word);
5851
5852 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005853}
5854
5855/*
5856 * Free the banned words in "su".
5857 */
5858 static void
5859free_banned(su)
5860 suginfo_T *su;
5861{
5862 int todo;
5863 hashitem_T *hi;
5864
5865 todo = su->su_banned.ht_used;
5866 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
5867 {
5868 if (!HASHITEM_EMPTY(hi))
5869 {
5870 vim_free(hi->hi_key);
5871 --todo;
5872 }
5873 }
5874 hash_clear(&su->su_banned);
5875}
5876
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005877#ifdef RESCORE
5878/*
5879 * Recompute the score if sound-folding is possible. This is slow,
5880 * thus only done for the final results.
5881 */
5882 static void
5883rescore_suggestions(su)
5884 suginfo_T *su;
5885{
5886 langp_T *lp;
5887 suggest_T *stp;
5888 char_u sal_badword[MAXWLEN];
5889 int score;
5890 int i;
5891
5892 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
5893 lp->lp_slang != NULL; ++lp)
5894 {
5895 if (lp->lp_slang->sl_sal.ga_len > 0)
5896 {
5897 /* soundfold the bad word */
5898 spell_soundfold(lp->lp_slang, su->su_fbadword, sal_badword);
5899
5900 for (i = 0; i < su->su_ga.ga_len; ++i)
5901 {
5902 stp = &SUG(su, i);
5903 if (!stp->st_had_bonus)
5904 {
5905 score = spell_sound_score(lp->lp_slang, stp->st_word,
5906 sal_badword);
5907 stp->st_score = RESCORE(stp->st_score, score);
5908 }
5909 }
5910 break;
5911 }
5912 }
5913}
5914#endif
5915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005916static int
5917#ifdef __BORLANDC__
5918_RTLENTRYF
5919#endif
5920sug_compare __ARGS((const void *s1, const void *s2));
5921
5922/*
5923 * Function given to qsort() to sort the suggestions on st_score.
5924 */
5925 static int
5926#ifdef __BORLANDC__
5927_RTLENTRYF
5928#endif
5929sug_compare(s1, s2)
5930 const void *s1;
5931 const void *s2;
5932{
5933 suggest_T *p1 = (suggest_T *)s1;
5934 suggest_T *p2 = (suggest_T *)s2;
5935
5936 return p1->st_score - p2->st_score;
5937}
5938
5939/*
5940 * Cleanup the suggestions:
5941 * - Sort on score.
5942 * - Remove words that won't be displayed.
5943 */
5944 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005945cleanup_suggestions(su, keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005946 suginfo_T *su;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005947 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005948{
5949 suggest_T *stp = &SUG(su, 0);
5950 int i;
5951
5952 /* Sort the list. */
5953 qsort(su->su_ga.ga_data, (size_t)su->su_ga.ga_len,
5954 sizeof(suggest_T), sug_compare);
5955
5956 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005957 if (su->su_ga.ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005958 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005959 for (i = keep; i < su->su_ga.ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005960 vim_free(stp[i].st_word);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005961 su->su_ga.ga_len = keep;
5962 su->su_maxscore = stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005963 }
5964}
5965
5966/*
5967 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
5968 */
5969 static void
5970spell_soundfold(slang, inword, res)
5971 slang_T *slang;
5972 char_u *inword;
5973 char_u *res;
5974{
5975 fromto_T *ftp;
5976 char_u word[MAXWLEN];
5977#ifdef FEAT_MBYTE
5978 int l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005979 int found_mbyte = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005980#endif
5981 char_u *s;
5982 char_u *t;
5983 int i, j, z;
5984 int n, k = 0;
5985 int z0;
5986 int k0;
5987 int n0;
5988 int c;
5989 int pri;
5990 int p0 = -333;
5991 int c0;
5992
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005993 /* Remove accents, if wanted. We actually remove all non-word characters.
5994 * But keep white space. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005995 if (slang->sl_rem_accents)
5996 {
5997 t = word;
5998 for (s = inword; *s != NUL; )
5999 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006000 if (vim_iswhite(*s))
6001 *t++ = *s++;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006002#ifdef FEAT_MBYTE
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006003 else if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006004 {
6005 l = mb_ptr2len_check(s);
6006 if (SPELL_ISWORDP(s))
6007 {
6008 mch_memmove(t, s, l);
6009 t += l;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006010 if (l > 1)
6011 found_mbyte = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006012 }
6013 s += l;
6014 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006015#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006016 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006017 {
6018 if (SPELL_ISWORDP(s))
6019 *t++ = *s;
6020 ++s;
6021 }
6022 }
6023 *t = NUL;
6024 }
6025 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006026 {
6027#ifdef FEAT_MBYTE
6028 if (has_mbyte)
6029 for (s = inword; *s != NUL; s += l)
6030 if ((l = mb_ptr2len_check(s)) > 1)
6031 {
6032 found_mbyte = TRUE;
6033 break;
6034 }
6035#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006036 STRCPY(word, inword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006037 }
6038
6039#ifdef FEAT_MBYTE
6040 /* If there are multi-byte characters in the word return it as-is, because
6041 * the following won't work. */
6042 if (found_mbyte)
6043 {
6044 STRCPY(res, word);
6045 return;
6046 }
6047#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006048
6049 ftp = (fromto_T *)slang->sl_sal.ga_data;
6050
6051 /*
6052 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006053 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006054 * TODO: support for multi-byte chars.
6055 */
6056 i = j = z = 0;
6057 while ((c = word[i]) != NUL)
6058 {
6059 n = slang->sl_sal_first[c];
6060 z0 = 0;
6061
6062 if (n >= 0)
6063 {
6064 /* check all rules for the same letter */
6065 while (ftp[n].ft_from[0] == c)
6066 {
6067 /* check whole string */
6068 k = 1; /* number of found letters */
6069 pri = 5; /* default priority */
6070 s = ftp[n].ft_from;
6071 s++; /* important for (see below) "*(s-1)" */
6072
6073 /* Skip over normal letters that match with the word. */
6074 while (*s != NUL && word[i + k] == *s
6075 && !vim_isdigit(*s) && strchr("(-<^$", *s) == NULL)
6076 {
6077 k++;
6078 s++;
6079 }
6080
6081 if (*s == '(')
6082 {
6083 /* check alternate letters in "(..)" */
6084 for (t = s + 1; *t != ')' && *t != NUL; ++t)
6085 if (*t == word[i + k])
6086 {
6087 /* match */
6088 ++k;
6089 for (s = t + 1; *s != NUL; ++s)
6090 if (*s == ')')
6091 {
6092 ++s;
6093 break;
6094 }
6095 break;
6096 }
6097 }
6098
6099 p0 = *s;
6100 k0 = k;
6101 while (*s == '-' && k > 1)
6102 {
6103 k--;
6104 s++;
6105 }
6106 if (*s == '<')
6107 s++;
6108 if (vim_isdigit(*s))
6109 {
6110 /* determine priority */
6111 pri = *s - '0';
6112 s++;
6113 }
6114 if (*s == '^' && *(s + 1) == '^')
6115 s++;
6116
6117 if (*s == NUL
6118 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006119 && (i == 0 || !(word[i - 1] == ' '
6120 || SPELL_ISWORDP(word + i - 1)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006121 && (*(s + 1) != '$'
6122 || (!SPELL_ISWORDP(word + i + k0))))
6123 || (*s == '$' && i > 0
6124 && SPELL_ISWORDP(word + i - 1)
6125 && (!SPELL_ISWORDP(word + i + k0))))
6126 {
6127 /* search for followup rules, if: */
6128 /* followup and k > 1 and NO '-' in searchstring */
6129 c0 = word[i + k - 1];
6130 n0 = slang->sl_sal_first[c0];
6131
6132 if (slang->sl_followup && k > 1 && n0 >= 0
6133 && p0 != '-' && word[i + k] != NUL)
6134 {
6135 /* test follow-up rule for "word[i + k]" */
6136 while (ftp[n0].ft_from[0] == c0)
6137 {
6138
6139 /* check whole string */
6140 k0 = k;
6141 p0 = 5;
6142 s = ftp[n0].ft_from;
6143 s++;
6144 while (*s != NUL && word[i+k0] == *s
6145 && !vim_isdigit(*s)
6146 && strchr("(-<^$",*s) == NULL)
6147 {
6148 k0++;
6149 s++;
6150 }
6151 if (*s == '(')
6152 {
6153 /* check alternate letters in "(..)" */
6154 for (t = s + 1; *t != ')' && *t != NUL; ++t)
6155 if (*t == word[i + k0])
6156 {
6157 /* match */
6158 ++k0;
6159 for (s = t + 1; *s != NUL; ++s)
6160 if (*s == ')')
6161 {
6162 ++s;
6163 break;
6164 }
6165 break;
6166 }
6167 }
6168 while (*s == '-')
6169 {
6170 /* "k0" gets NOT reduced */
6171 /* because "if (k0 == k)" */
6172 s++;
6173 }
6174 if (*s == '<')
6175 s++;
6176 if (vim_isdigit(*s))
6177 {
6178 p0 = *s - '0';
6179 s++;
6180 }
6181
6182 if (*s == NUL
6183 /* *s == '^' cuts */
6184 || (*s == '$'
6185 && !SPELL_ISWORDP(word + i + k0)))
6186 {
6187 if (k0 == k)
6188 {
6189 /* this is just a piece of the string */
6190 ++n0;
6191 continue;
6192 }
6193
6194 if (p0 < pri)
6195 {
6196 /* priority too low */
6197 ++n0;
6198 continue;
6199 }
6200 /* rule fits; stop search */
6201 break;
6202 }
6203 ++n0;
6204 }
6205
6206 if (p0 >= pri && ftp[n0].ft_from[0] == c0)
6207 {
6208 ++n;
6209 continue;
6210 }
6211 }
6212
6213 /* replace string */
6214 s = ftp[n].ft_to;
6215 p0 = (ftp[n].ft_from[0] != NUL
6216 && vim_strchr(ftp[n].ft_from + 1,
6217 '<') != NULL) ? 1 : 0;
6218 if (p0 == 1 && z == 0)
6219 {
6220 /* rule with '<' is used */
6221 if (j > 0 && *s != NUL
6222 && (res[j - 1] == c || res[j - 1] == *s))
6223 j--;
6224 z0 = 1;
6225 z = 1;
6226 k0 = 0;
6227 while (*s != NUL && word[i+k0] != NUL)
6228 {
6229 word[i + k0] = *s;
6230 k0++;
6231 s++;
6232 }
6233 if (k > k0)
6234 mch_memmove(word + i + k0, word + i + k,
6235 STRLEN(word + i + k) + 1);
6236
6237 /* new "actual letter" */
6238 c = word[i];
6239 }
6240 else
6241 {
6242 /* no '<' rule used */
6243 i += k - 1;
6244 z = 0;
6245 while (*s != NUL && s[1] != NUL && j < MAXWLEN)
6246 {
6247 if (j == 0 || res[j - 1] != *s)
6248 {
6249 res[j] = *s;
6250 j++;
6251 }
6252 s++;
6253 }
6254 /* new "actual letter" */
6255 c = *s;
6256 if (ftp[n].ft_from[0] != NUL
6257 && strstr((char *)ftp[n].ft_from + 1,
6258 "^^") != NULL)
6259 {
6260 if (c != NUL)
6261 {
6262 res[j] = c;
6263 j++;
6264 }
6265 mch_memmove(word, word + i + 1,
6266 STRLEN(word + i + 1) + 1);
6267 i = 0;
6268 z0 = 1;
6269 }
6270 }
6271 break;
6272 }
6273 ++n;
6274 }
6275 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006276 else if (vim_iswhite(c))
6277 {
6278 c = ' ';
6279 k = 1;
6280 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006281
6282 if (z0 == 0)
6283 {
6284 if (k && !p0 && j < MAXWLEN && c != NUL
6285 && (!slang->sl_collapse || j == 0 || res[j - 1] != c))
6286 {
6287 /* condense only double letters */
6288 res[j] = c;
6289 j++;
6290 }
6291
6292 i++;
6293 z = 0;
6294 k = 0;
6295 }
6296 }
6297
6298 res[j] = NUL;
6299}
6300
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006301#if defined(RESCORE) || defined(SOUNDFOLD_SCORE)
6302/*
6303 * Return the score for how much words sound different.
6304 */
6305 static int
6306spell_sound_score(slang, goodword, badsound)
6307 slang_T *slang;
6308 char_u *goodword; /* good word */
6309 char_u *badsound; /* sound-folded bad word */
6310{
6311 char_u fword[MAXWLEN];
6312 char_u goodsound[MAXWLEN];
6313 int score;
6314
6315 /* Case-fold the word, needed for sound folding. */
6316 (void)spell_casefold(goodword, STRLEN(goodword), fword, MAXWLEN);
6317
6318 /* sound-fold the good word */
6319 spell_soundfold(slang, fword, goodsound);
6320
6321 /* compute the edit distance-score of the sounds */
6322 score = spell_edit_score(badsound, goodsound);
6323
6324 /* Correction: adding/inserting "*" at the start (word starts with vowel)
6325 * shouldn't be counted so much, vowels halfway the word aren't counted at
6326 * all. */
6327 if (*badsound != *goodsound && (*badsound == '*' || *goodsound == '*'))
6328 score -= SCORE_DEL / 2;
6329
6330 return score;
6331}
6332#endif
6333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006334/*
6335 * Compute the "edit distance" to turn "badword" into "goodword". The less
6336 * deletes/inserts/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006337 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006338 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006339 * It has been converted from C++ to C and modified to support multi-byte
6340 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006341 */
6342 static int
6343spell_edit_score(badword, goodword)
6344 char_u *badword;
6345 char_u *goodword;
6346{
6347 int *cnt;
6348 int badlen, goodlen;
6349 int j, i;
6350 int t;
6351 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006352 int pbc, pgc;
6353#ifdef FEAT_MBYTE
6354 char_u *p;
6355 int wbadword[MAXWLEN];
6356 int wgoodword[MAXWLEN];
6357
6358 if (has_mbyte)
6359 {
6360 /* Get the characters from the multi-byte strings and put them in an
6361 * int array for easy access. */
6362 for (p = badword, badlen = 0; *p != NUL; )
6363 wbadword[badlen++] = mb_ptr2char_adv(&p);
6364 ++badlen;
6365 for (p = goodword, goodlen = 0; *p != NUL; )
6366 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
6367 ++goodlen;
6368 }
6369 else
6370#endif
6371 {
6372 badlen = STRLEN(badword) + 1;
6373 goodlen = STRLEN(goodword) + 1;
6374 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006375
6376 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
6377#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006378 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
6379 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006380 if (cnt == NULL)
6381 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006382
6383 CNT(0, 0) = 0;
6384 for (j = 1; j <= goodlen; ++j)
6385 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
6386
6387 for (i = 1; i <= badlen; ++i)
6388 {
6389 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
6390 for (j = 1; j <= goodlen; ++j)
6391 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006392#ifdef FEAT_MBYTE
6393 if (has_mbyte)
6394 {
6395 bc = wbadword[i - 1];
6396 gc = wgoodword[j - 1];
6397 }
6398 else
6399#endif
6400 {
6401 bc = badword[i - 1];
6402 gc = goodword[j - 1];
6403 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006404 if (bc == gc)
6405 CNT(i, j) = CNT(i - 1, j - 1);
6406 else
6407 {
6408 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006409 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006410 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
6411 else
6412 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
6413
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006414 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006415 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006416#ifdef FEAT_MBYTE
6417 if (has_mbyte)
6418 {
6419 pbc = wbadword[i - 2];
6420 pgc = wgoodword[j - 2];
6421 }
6422 else
6423#endif
6424 {
6425 pbc = badword[i - 2];
6426 pgc = goodword[j - 2];
6427 }
6428 if (bc == pgc && pbc == gc)
6429 {
6430 t = SCORE_SWAP + CNT(i - 2, j - 2);
6431 if (t < CNT(i, j))
6432 CNT(i, j) = t;
6433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006434 }
6435 t = SCORE_DEL + CNT(i - 1, j);
6436 if (t < CNT(i, j))
6437 CNT(i, j) = t;
6438 t = SCORE_INS + CNT(i, j - 1);
6439 if (t < CNT(i, j))
6440 CNT(i, j) = t;
6441 }
6442 }
6443 }
6444 return CNT(badlen - 1, goodlen - 1);
6445}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006446
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006447#endif /* FEAT_SYN_HL */