blob: cbe2f9fc6da5483d7aa33815a49cec3440b7d562 [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).
16 * A NUL byte is used where the word may end.
17 *
18 * There are two trees: one with case-folded words and one with words in
19 * original case. The second one is only used for keep-case words and is
20 * usually small.
21 *
22 * Thanks to Olaf Seibert for providing an example implementation of this tree
23 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000024 *
25 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000026 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000027 * Why doesn't Vim use aspell/ispell/myspell/etc.?
28 * See ":help develop-spell".
29 */
30
Bram Moolenaar51485f02005-06-04 21:55:20 +000031/*
32 * Vim spell file format: <HEADER> <SUGGEST> <LWORDTREE> <KWORDTREE>
33 *
34 * <HEADER>: <fileID> <regioncnt> <regionname> ...
35 * <charflagslen> <charflags> <fcharslen> <fchars>
36 *
37 * <fileID> 10 bytes "VIMspell05"
38 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000039 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +000040 * First <regionname> is region 1.
41 *
42 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
43 * <charflags> N bytes List of flags (first one is for character 128):
44 * 0x01 word character
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000045 * 0x02 upper-case character
Bram Moolenaar51485f02005-06-04 21:55:20 +000046 * <fcharslen> 2 bytes Number of bytes in <fchars>.
47 * <fchars> N bytes Folded characters, first one is for character 128.
48 *
49 *
50 * <SUGGEST> : <suggestlen> <more> ...
51 *
52 * <suggestlen> 4 bytes Length of <SUGGEST> in bytes, excluding
53 * <suggestlen>. MSB first.
54 * <more> To be defined.
55 *
56 *
57 * <LWORDTREE>: <wordtree>
58 *
59 * <wordtree>: <nodecount> <nodedata> ...
60 *
61 * <nodecount> 4 bytes Number of nodes following. MSB first.
62 *
63 * <nodedata>: <siblingcount> <sibling> ...
64 *
65 * <siblingcount> 1 byte Number of siblings in this node. The siblings
66 * follow in sorted order.
67 *
68 * <sibling>: <byte> [<nodeidx> <xbyte> | <flags> [<region>]]
69 *
70 * <byte> 1 byte Byte value of the sibling. Special cases:
71 * BY_NOFLAGS: End of word without flags and for all
72 * regions.
73 * BY_FLAGS: End of word, <flags> follow.
74 * BY_INDEX: Child of sibling is shared, <nodeidx>
75 * and <xbyte> follow.
76 *
77 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
78 *
79 * <xbyte> 1 byte byte value of the sibling.
80 *
81 * <flags> 1 byte bitmask of:
82 * WF_ALLCAP word must have only capitals
83 * WF_ONECAP first char of word must be capital
84 * WF_RARE rare word
85 * WF_REGION <region> follows
86 *
87 * <region> 1 byte Bitmask for regions in which word is valid. When
88 * omitted it's valid in all regions.
89 * Lowest bit is for region 1.
90 *
91 * <KWORDTREE>: <wordtree>
92 *
93 *
94 * All text characters are in 'encoding', but stored as single bytes.
95 * The region name is ASCII.
96 */
97
Bram Moolenaare19defe2005-03-21 08:23:33 +000098#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
99# include <io.h> /* for lseek(), must be before vim.h */
100#endif
101
102#include "vim.h"
103
104#if defined(FEAT_SYN_HL) || defined(PROTO)
105
106#ifdef HAVE_FCNTL_H
107# include <fcntl.h>
108#endif
109
Bram Moolenaar51485f02005-06-04 21:55:20 +0000110#define MAXWLEN 250 /* assume max. word len is this many bytes */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000111
Bram Moolenaar51485f02005-06-04 21:55:20 +0000112/* Flags used for a word. */
113#define WF_REGION 0x01 /* region byte follows */
114#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
115#define WF_ALLCAP 0x04 /* word must be all capitals */
116#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000117#define WF_BANNED 0x10 /* bad word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000118
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000119#define WF_KEEPCAP 0x100 /* keep-case word (not stored in file) */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000120
121#define BY_NOFLAGS 0 /* end of word without flags or region */
122#define BY_FLAGS 1 /* end of word, flag byte follows */
123#define BY_INDEX 2 /* child is shared, index follows */
124#define BY_SPECIAL BY_INDEX /* hightest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000125
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000126/* Info from "REP" entries in ".aff" file used in af_rep.
127 * TODO: This is not used yet. Either use it or remove it. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000128typedef struct repentry_S
129{
130 char_u *re_from;
131 char_u *re_to;
132} repentry_T;
133
134/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000135 * Structure used to store words and other info for one language, loaded from
136 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000137 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
138 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
139 *
140 * The "byts" array stores the possible bytes in each tree node, preceded by
141 * the number of possible bytes, sorted on byte value:
142 * <len> <byte1> <byte2> ...
143 * The "idxs" array stores the index of the child node corresponding to the
144 * byte in "byts".
145 * Exception: when the byte is zero, the word may end here and "idxs" holds
146 * the flags and region for the word. There may be several zeros in sequence
147 * for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000148 */
149typedef struct slang_S slang_T;
150struct slang_S
151{
152 slang_T *sl_next; /* next language */
153 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000154 char_u *sl_fname; /* name of .spl file */
155 int sl_add; /* TRUE if it's an addition. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000156 char_u *sl_fbyts; /* case-folded word bytes */
157 int *sl_fidxs; /* case-folded word indexes */
158 char_u *sl_kbyts; /* keep-case word bytes */
159 int *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000160 char_u *sl_try; /* "TRY" from .aff file TODO: not used */
161 garray_T sl_rep; /* list of repentry_T entries from REP lines
162 * TODO not used */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000163 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000164 int sl_error; /* error while loading */
165};
166
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000167/* First language that is loaded, start of the linked list of loaded
168 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169static slang_T *first_lang = NULL;
170
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171/*
172 * Structure used in "b_langp", filled from 'spelllang'.
173 */
174typedef struct langp_S
175{
176 slang_T *lp_slang; /* info for this language (NULL for last one) */
177 int lp_region; /* bitmask for region or REGION_ALL */
178} langp_T;
179
180#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
181
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000182#define REGION_ALL 0xff /* word valid in all regions */
183
184/* Result values. Lower number is accepted over higher one. */
185#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000186#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000187#define SP_RARE 1
188#define SP_LOCAL 2
189#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000190
Bram Moolenaar51485f02005-06-04 21:55:20 +0000191#define VIMSPELLMAGIC "VIMspell05" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000192#define VIMSPELLMAGICL 10
193
194/*
195 * Structure to store info for word matching.
196 */
197typedef struct matchinf_S
198{
199 langp_T *mi_lp; /* info for language and region */
200 slang_T *mi_slang; /* info for the language */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000201
202 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000203 char_u *mi_word; /* start of word being checked */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000204 char_u *mi_end; /* end of matching word */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000205 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 char_u *mi_cend; /* char after what was used for
207 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000208
209 /* case-folded text */
210 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000211 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000212
213 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000214 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000215 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000216} matchinf_T;
217
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000218/*
219 * The tables used for recognizing word characters according to spelling.
220 * These are only used for the first 256 characters of 'encoding'.
221 */
222typedef struct spelltab_S
223{
224 char_u st_isw[256]; /* flags: is word char */
225 char_u st_isu[256]; /* flags: is uppercase char */
226 char_u st_fold[256]; /* chars: folded case */
227} spelltab_T;
228
229static spelltab_T spelltab;
230static int did_set_spelltab;
231
232#define SPELL_ISWORD 1
233#define SPELL_ISUPPER 2
234
235static void clear_spell_chartab __ARGS((spelltab_T *sp));
236static int set_spell_finish __ARGS((spelltab_T *new_st));
237
238/*
239 * Return TRUE if "p" points to a word character or "c" is a word character
240 * for spelling.
241 * Checking for a word character is done very often, avoid the function call
242 * overhead.
243 */
244#ifdef FEAT_MBYTE
245# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
246 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
247#else
248# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
249#endif
250
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000251static slang_T *slang_alloc __ARGS((char_u *lang));
252static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000253static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000254static void find_word __ARGS((matchinf_T *mip, int keepcap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000255static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000256static char_u *spell_enc __ARGS((void));
257static void spell_load_cb __ARGS((char_u *fname, void *cookie));
258static void spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000259static int read_tree __ARGS((FILE *fd, char_u *byts, int *idxs, int maxidx, int startidx));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000260static int find_region __ARGS((char_u *rp, char_u *region));
261static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000262static void spell_reload_one __ARGS((char_u *fname));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000263static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000264static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
265static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000266static int spell_isupper __ARGS((int c));
267static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
268
269static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000270
271/*
272 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000273 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000274 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
275 * or when it's OK it remains unchanged.
276 * This must only be called when 'spelllang' is not empty.
277 * Returns the length of the word in bytes, also when it's OK, so that the
278 * caller can skip over the word.
279 */
280 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000281spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000282 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000283 char_u *ptr;
284 int *attrp;
285{
286 matchinf_T mi; /* Most things are put in "mi" so that it can
287 be passed to functions quickly. */
288
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000289 /* A word never starts at a space or a control character. Return quickly
290 * then, skipping over the character. */
291 if (*ptr <= ' ')
292 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000293
Bram Moolenaar51485f02005-06-04 21:55:20 +0000294 /* A word starting with a number is always OK. Also skip hexadecimal
295 * numbers 0xFF99 and 0X99FF. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000296 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000297 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000298 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
299 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000300 else
301 mi.mi_end = skipdigits(ptr);
302 }
303 else
304 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000305 /* Find the end of the word. */
306 mi.mi_word = ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000307 mi.mi_fend = ptr;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000308 if (SPELL_ISWORDP(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000309 {
310 /* Make case-folded copy of the characters until the next non-word
311 * character. */
312 do
313 {
314 mb_ptr_adv(mi.mi_fend);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000315 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000316
Bram Moolenaar51485f02005-06-04 21:55:20 +0000317 /* Check the caps type of the word. */
318 mi.mi_capflags = captype(ptr, mi.mi_fend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000319 }
320 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000321 /* No word characters, caps type is always zero. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000322 mi.mi_capflags = 0;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000323
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000324 /* We always use the characters up to the next non-word character,
325 * also for bad words. */
326 mi.mi_end = mi.mi_fend;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000327 mi.mi_cend = mi.mi_fend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000328
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000329 /* Include one non-word character so that we can check for the
330 * word end. */
331 if (*mi.mi_fend != NUL)
332 mb_ptr_adv(mi.mi_fend);
333
334 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
335 MAXWLEN + 1);
336 mi.mi_fwordlen = STRLEN(mi.mi_fword);
337
Bram Moolenaar51485f02005-06-04 21:55:20 +0000338 /* The word is bad unless we recognize it. */
339 mi.mi_result = SP_BAD;
340
341 /*
342 * Loop over the languages specified in 'spelllang'.
343 * We check them all, because a matching word may be longer than an
344 * already found matching word.
345 */
346 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000347 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000348 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000349 /* Check for a matching word in case-folded words. */
350 find_word(&mi, FALSE);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000351
Bram Moolenaar51485f02005-06-04 21:55:20 +0000352 /* Try keep-case words. */
353 find_word(&mi, TRUE);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000354 }
355
Bram Moolenaar51485f02005-06-04 21:55:20 +0000356 if (mi.mi_result != SP_OK)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000357 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000358 /* When we are at a non-word character there is no error, just
359 * skip over the character (try looking for a word after it). */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000360 if (!SPELL_ISWORDP(ptr))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361 {
362#ifdef FEAT_MBYTE
363 if (has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000364 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000365#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000366 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000367 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000368
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000369 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000370 *attrp = highlight_attr[HLF_SPB];
371 else if (mi.mi_result == SP_RARE)
372 *attrp = highlight_attr[HLF_SPR];
373 else
374 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000375 }
376 }
377
Bram Moolenaar51485f02005-06-04 21:55:20 +0000378 return (int)(mi.mi_end - ptr);
379}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000380
Bram Moolenaar51485f02005-06-04 21:55:20 +0000381/*
382 * Check if the word at "mip->mi_word" is in the tree.
383 * When "keepcap" is TRUE check in keep-case word tree.
384 *
385 * For a match mip->mi_result is updated.
386 */
387 static void
388find_word(mip, keepcap)
389 matchinf_T *mip;
390 int keepcap;
391{
392 int arridx = 0;
393 int endlen[MAXWLEN]; /* length at possible word endings */
394 int endidx[MAXWLEN]; /* possible word endings */
395 int endidxcnt = 0;
396 int len;
397 int wlen = 0;
398 int flen;
399 int c;
400 char_u *ptr;
401 unsigned lo, hi, m;
402#ifdef FEAT_MBYTE
403 char_u *s;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000404#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000405 char_u *p;
406 int res = SP_BAD;
407 int valid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000408 slang_T *slang = mip->mi_lp->lp_slang;
409 unsigned flags;
410 char_u *byts;
411 int *idxs;
412
413 if (keepcap)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000415 /* Check for word with matching case in keep-case tree. */
416 ptr = mip->mi_word;
417 flen = 9999; /* no case folding, always enough bytes */
418 byts = slang->sl_kbyts;
419 idxs = slang->sl_kidxs;
420 }
421 else
422 {
423 /* Check for case-folded in case-folded tree. */
424 ptr = mip->mi_fword;
425 flen = mip->mi_fwordlen; /* available case-folded bytes */
426 byts = slang->sl_fbyts;
427 idxs = slang->sl_fidxs;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428 }
429
Bram Moolenaar51485f02005-06-04 21:55:20 +0000430 if (byts == NULL)
431 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000432
Bram Moolenaar51485f02005-06-04 21:55:20 +0000433 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000434 * Repeat advancing in the tree until:
435 * - there is a byte that doesn't match,
436 * - we reach the end of the tree,
437 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000438 */
439 for (;;)
440 {
441 if (flen == 0 && *mip->mi_fend != NUL)
442 {
443 /* Need to fold at least one more character. Do until next
444 * non-word character for efficiency. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000445 p = mip->mi_fend;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000446 do
447 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000448 mb_ptr_adv(mip->mi_fend);
449 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000450
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000451 /* Include the non-word character so that we can check for the
452 * word end. */
453 if (*mip->mi_fend != NUL)
454 mb_ptr_adv(mip->mi_fend);
455
456 (void)spell_casefold(p, (int)(mip->mi_fend - p),
Bram Moolenaar51485f02005-06-04 21:55:20 +0000457 mip->mi_fword + mip->mi_fwordlen,
458 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000459 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
460 mip->mi_fwordlen += flen;
461 }
462
463 len = byts[arridx++];
464
465 /* If the first possible byte is a zero the word could end here.
466 * Remember this index, we first check for the longest word. */
467 if (byts[arridx] == 0)
468 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000469 if (endidxcnt == MAXWLEN)
470 {
471 /* Must be a corrupted spell file. */
472 EMSG(_(e_format));
473 return;
474 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000475 endlen[endidxcnt] = wlen;
476 endidx[endidxcnt++] = arridx++;
477 --len;
478
479 /* Skip over the zeros, there can be several flag/region
480 * combinations. */
481 while (len > 0 && byts[arridx] == 0)
482 {
483 ++arridx;
484 --len;
485 }
486 if (len == 0)
487 break; /* no children, word must end here */
488 }
489
490 /* Stop looking at end of the line. */
491 if (ptr[wlen] == NUL)
492 break;
493
494 /* Perform a binary search in the list of accepted bytes. */
495 c = ptr[wlen];
496 lo = arridx;
497 hi = arridx + len - 1;
498 while (lo < hi)
499 {
500 m = (lo + hi) / 2;
501 if (byts[m] > c)
502 hi = m - 1;
503 else if (byts[m] < c)
504 lo = m + 1;
505 else
506 {
507 lo = hi = m;
508 break;
509 }
510 }
511
512 /* Stop if there is no matching byte. */
513 if (hi < lo || byts[lo] != c)
514 break;
515
516 /* Continue at the child (if there is one). */
517 arridx = idxs[lo];
518 ++wlen;
519 --flen;
520 }
521
522 /*
523 * Verify that one of the possible endings is valid. Try the longest
524 * first.
525 */
526 while (endidxcnt > 0)
527 {
528 --endidxcnt;
529 arridx = endidx[endidxcnt];
530 wlen = endlen[endidxcnt];
531
532#ifdef FEAT_MBYTE
533 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
534 continue; /* not at first byte of character */
535#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000536 if (SPELL_ISWORDP(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000537 continue; /* next char is a word character */
538
539#ifdef FEAT_MBYTE
540 if (!keepcap && has_mbyte)
541 {
542 /* Compute byte length in original word, length may change
543 * when folding case. */
544 p = mip->mi_word;
545 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
546 mb_ptr_adv(p);
547 wlen = p - mip->mi_word;
548 }
549#endif
550
551 /* Check flags and region. Repeat this if there are more
552 * flags/region alternatives until there is a match. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000553 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len)
554 {
555 flags = idxs[arridx];
556 if (keepcap)
557 {
558 /* For "keepcap" tree the case is always right. */
559 valid = TRUE;
560 }
561 else
562 {
563 /* Check that the word is in the required case. */
564 if (mip->mi_cend != mip->mi_word + wlen)
565 {
566 /* mi_capflags was set for a different word
567 * length, need to do it again. */
568 mip->mi_cend = mip->mi_word + wlen;
569 mip->mi_capflags = captype(mip->mi_word,
570 mip->mi_cend);
571 }
572
573 valid = (mip->mi_capflags == WF_ALLCAP
574 || ((flags & WF_ALLCAP) == 0
575 && ((flags & WF_ONECAP) == 0
576 || mip->mi_capflags == WF_ONECAP)));
577 }
578
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000579 if (valid)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000580 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000581 if (flags & WF_BANNED)
582 res = SP_BANNED;
583 else if (flags & WF_REGION)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000584 {
585 /* Check region. */
586 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
587 res = SP_OK;
588 else
589 res = SP_LOCAL;
590 }
591 else if (flags & WF_RARE)
592 res = SP_RARE;
593 else
594 res = SP_OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000595
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000596 /* Always use the longest match and the best result. */
597 if (mip->mi_result > res)
598 {
599 mip->mi_result = res;
600 mip->mi_end = mip->mi_word + wlen;
601 }
602 else if (mip->mi_result == res
603 && mip->mi_end < mip->mi_word + wlen)
604 mip->mi_end = mip->mi_word + wlen;
605
606 if (res == SP_OK)
607 break;
608 }
609 else
610 res = SP_BAD;
611
Bram Moolenaar51485f02005-06-04 21:55:20 +0000612 ++arridx;
613 }
614
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000615 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000616 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000617 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000618}
619
Bram Moolenaar51485f02005-06-04 21:55:20 +0000620
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000621/*
622 * Move to next spell error.
623 * Return OK if found, FAIL otherwise.
624 */
625 int
626spell_move_to(dir, allwords)
627 int dir; /* FORWARD or BACKWARD */
628 int allwords; /* TRUE for "[s" and "]s" */
629{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000630 linenr_T lnum;
631 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000632 char_u *line;
633 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000634 int attr = 0;
635 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000636 int has_syntax = syntax_present(curbuf);
637 int col;
638 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000639
Bram Moolenaarb765d632005-06-07 21:00:02 +0000640 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000641 {
642 EMSG(_("E756: Spell checking not enabled"));
643 return FAIL;
644 }
645
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000646 /*
647 * Start looking for bad word at the start of the line, because we can't
648 * start halfway a word, we don't know where it starts or ends.
649 *
650 * When searching backwards, we continue in the line to find the last
651 * bad word (in the cursor line: before the cursor).
652 */
653 lnum = curwin->w_cursor.lnum;
654 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000655
656 while (!got_int)
657 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000658 line = ml_get(lnum);
659 p = line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000660
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000661 while (*p != NUL)
662 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000663 /* When searching backward don't search after the cursor. */
664 if (dir == BACKWARD
665 && lnum == curwin->w_cursor.lnum
666 && (colnr_T)(p - line) >= curwin->w_cursor.col)
667 break;
668
669 /* start of word */
670 len = spell_check(curwin, p, &attr);
671
672 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000673 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000674 /* We found a bad word. Check the attribute. */
675 /* TODO: check for syntax @Spell cluster. */
676 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000678 /* When searching forward only accept a bad word after
679 * the cursor. */
680 if (dir == BACKWARD
681 || lnum > curwin->w_cursor.lnum
682 || (lnum == curwin->w_cursor.lnum
683 && (colnr_T)(p - line)
684 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000685 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000686 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000687 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000688 col = p - line;
689 (void)syn_get_id(lnum, (colnr_T)col,
690 FALSE, &can_spell);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000691
Bram Moolenaar51485f02005-06-04 21:55:20 +0000692 /* have to get the line again, a multi-line
693 * regexp may make it invalid */
694 line = ml_get(lnum);
695 p = line + col;
696 }
697 else
698 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000699
Bram Moolenaar51485f02005-06-04 21:55:20 +0000700 if (can_spell)
701 {
702 found_pos.lnum = lnum;
703 found_pos.col = p - line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000704#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000706#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000707 if (dir == FORWARD)
708 {
709 /* No need to search further. */
710 curwin->w_cursor = found_pos;
711 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000712 }
713 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000714 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000715 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000716 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000717 }
718
Bram Moolenaar51485f02005-06-04 21:55:20 +0000719 /* advance to character after the word */
720 p += len;
721 if (*p == NUL)
722 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000723 }
724
725 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000726 if (dir == BACKWARD)
727 {
728 if (found_pos.lnum != 0)
729 {
730 /* Use the last match in the line. */
731 curwin->w_cursor = found_pos;
732 return OK;
733 }
734 if (lnum == 1)
735 return FAIL;
736 --lnum;
737 }
738 else
739 {
740 if (lnum == curbuf->b_ml.ml_line_count)
741 return FAIL;
742 ++lnum;
743 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000744
745 line_breakcheck();
746 }
747
748 return FAIL; /* interrupted */
749}
750
751/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000752 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +0000753 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000754 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000755 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000756spell_load_lang(lang)
757 char_u *lang;
758{
Bram Moolenaarb765d632005-06-07 21:00:02 +0000759 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000760 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000761 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000762
Bram Moolenaarb765d632005-06-07 21:00:02 +0000763 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000764 * It's truncated when an error is detected. */
765 STRCPY(langcp, lang);
766
Bram Moolenaarb765d632005-06-07 21:00:02 +0000767 /*
768 * Find the first spell file for "lang" in 'runtimepath' and load it.
769 */
770 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
771 "spell/%s.%s.spl", lang, spell_enc());
772 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000773
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000774 if (r == FAIL && *langcp != NUL)
775 {
776 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000777 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000778 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000779 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000780 }
781
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000782 if (r == FAIL)
783 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
784 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000785 else if (*langcp != NUL)
786 {
787 /* Load all the additions. */
788 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
789 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
790 }
791}
792
793/*
794 * Return the encoding used for spell checking: Use 'encoding', except that we
795 * use "latin1" for "latin9". And limit to 60 characters (just in case).
796 */
797 static char_u *
798spell_enc()
799{
800
801#ifdef FEAT_MBYTE
802 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
803 return p_enc;
804#endif
805 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000806}
807
808/*
809 * Allocate a new slang_T.
810 * Caller must fill "sl_next".
811 */
812 static slang_T *
813slang_alloc(lang)
814 char_u *lang;
815{
816 slang_T *lp;
817
Bram Moolenaar51485f02005-06-04 21:55:20 +0000818 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000819 if (lp != NULL)
820 {
821 lp->sl_name = vim_strsave(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000822 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000823 }
824 return lp;
825}
826
827/*
828 * Free the contents of an slang_T and the structure itself.
829 */
830 static void
831slang_free(lp)
832 slang_T *lp;
833{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000834 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000835 vim_free(lp->sl_fname);
836 slang_clear(lp);
837 vim_free(lp);
838}
839
840/*
841 * Clear an slang_T so that the file can be reloaded.
842 */
843 static void
844slang_clear(lp)
845 slang_T *lp;
846{
Bram Moolenaar51485f02005-06-04 21:55:20 +0000847 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000848 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000849 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000850 lp->sl_kbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000852 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000854 lp->sl_kidxs = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000855 ga_clear(&lp->sl_rep);
856 vim_free(lp->sl_try);
Bram Moolenaarb765d632005-06-07 21:00:02 +0000857 lp->sl_try = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000858}
859
860/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000861 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000862 * Invoked through do_in_runtimepath().
863 */
864 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +0000865spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000866 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000867 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000868{
Bram Moolenaarb765d632005-06-07 21:00:02 +0000869 spell_load_file(fname, (char_u *)cookie, NULL);
870}
871
872/*
873 * Load one spell file and store the info into a slang_T.
874 *
875 * This is invoked in two ways:
876 * - From spell_load_cb() to load a spell file for the first time. "lang" is
877 * the language name, "old_lp" is NULL. Will allocate an slang_T.
878 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
879 * points to the existing slang_T.
880 */
881 static void
882spell_load_file(fname, lang, old_lp)
883 char_u *fname;
884 char_u *lang;
885 slang_T *old_lp;
886{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000887 FILE *fd;
888 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000889 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000890 int i;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000891 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000892 int round;
893 char_u *save_sourcing_name = sourcing_name;
894 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000895 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000896 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000897 slang_T *lp = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000898
Bram Moolenaarb765d632005-06-07 21:00:02 +0000899 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000900 if (fd == NULL)
901 {
902 EMSG2(_(e_notopen), fname);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000903 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000904 }
Bram Moolenaarb765d632005-06-07 21:00:02 +0000905 if (p_verbose > 2)
906 {
907 verbose_enter();
908 smsg((char_u *)_("Reading spell file \"%s\""), fname);
909 verbose_leave();
910 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000911
Bram Moolenaarb765d632005-06-07 21:00:02 +0000912 if (old_lp == NULL)
913 {
914 lp = slang_alloc(lang);
915 if (lp == NULL)
916 goto endFAIL;
917
918 /* Remember the file name, used to reload the file when it's updated. */
919 lp->sl_fname = vim_strsave(fname);
920 if (lp->sl_fname == NULL)
921 goto endFAIL;
922
923 /* Check for .add.spl. */
924 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
925 }
926 else
927 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000928
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000929 /* Set sourcing_name, so that error messages mention the file name. */
930 sourcing_name = fname;
931 sourcing_lnum = 0;
932
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000933 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
934 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000935 for (i = 0; i < VIMSPELLMAGICL; ++i)
936 buf[i] = getc(fd); /* <fileID> */
937 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
938 {
939 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000940 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000941 }
942
943 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000944 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000945 {
946truncerr:
947 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000948 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000949 }
950 if (cnt > 8)
951 {
952formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000953 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000954 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000955 }
956 for (i = 0; i < cnt; ++i)
957 {
958 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
959 lp->sl_regions[i * 2 + 1] = getc(fd);
960 }
961 lp->sl_regions[cnt * 2] = NUL;
962
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000963 cnt = getc(fd); /* <charflagslen> */
964 if (cnt > 0)
965 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000966 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000967 if (p == NULL)
968 goto endFAIL;
969 for (i = 0; i < cnt; ++i)
970 p[i] = getc(fd); /* <charflags> */
971
972 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
973 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000974 {
975 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000976 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000977 }
978 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000979 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000980 {
981 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000982 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000983 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000984 for (i = 0; i < ccnt; ++i)
985 fol[i] = getc(fd); /* <fchars> */
986 fol[i] = NUL;
987
988 /* Set the word-char flags and fill spell_isupper() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000989 i = set_spell_charflags(p, cnt, fol);
990 vim_free(p);
991 vim_free(fol);
992 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000993 goto formerr;
994 }
995 else
996 {
997 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
998 cnt = (getc(fd) << 8) + getc(fd);
999 if (cnt != 0)
1000 goto formerr;
1001 }
1002
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003 /* <SUGGEST> : <suggestlen> <more> ... */
1004 /* TODO, just skip this for now */
1005 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1006 while (i-- > 0)
1007 if (getc(fd) == EOF) /* <suggestlen> */
1008 goto truncerr;
1009
Bram Moolenaar51485f02005-06-04 21:55:20 +00001010 /* round 1: <LWORDTREE>
1011 * round 2: <KWORDTREE> */
1012 for (round = 1; round <= 2; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001014 /* The tree size was computed when writing the file, so that we can
1015 * allocate it as one long block. <nodecount> */
1016 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1017 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001019 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001020 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001021 /* Allocate the byte array. */
1022 p = lalloc((long_u)len, TRUE);
1023 if (p == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001024 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001025 if (round == 1)
1026 lp->sl_fbyts = p;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001027 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028 lp->sl_kbyts = p;
1029
1030 /* Allocate the index array. */
1031 p = lalloc_clear((long_u)(len * sizeof(int)), TRUE);
1032 if (p == NULL)
1033 goto endFAIL;
1034 if (round == 1)
1035 lp->sl_fidxs = (int *)p;
1036 else
1037 lp->sl_kidxs = (int *)p;
1038
1039
1040 /* Read the tree and store it in the array. */
1041 i = read_tree(fd,
1042 round == 1 ? lp->sl_fbyts : lp->sl_kbyts,
1043 round == 1 ? lp->sl_fidxs : lp->sl_kidxs,
1044 len, 0);
1045 if (i == -1)
1046 goto truncerr;
1047 if (i < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001048 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001049 }
1050 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001051
Bram Moolenaarb765d632005-06-07 21:00:02 +00001052 /* For a new file link it in the list of spell files. */
1053 if (old_lp == NULL)
1054 {
1055 lp->sl_next = first_lang;
1056 first_lang = lp;
1057 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001058
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001059 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001060
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001061endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00001062 if (lang != NULL)
1063 /* truncating the name signals the error to spell_load_lang() */
1064 *lang = NUL;
1065 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001066 slang_free(lp);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001067
1068endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001069 if (fd != NULL)
1070 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001071 sourcing_name = save_sourcing_name;
1072 sourcing_lnum = save_sourcing_lnum;
1073}
1074
1075/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001076 * Read one row of siblings from the spell file and store it in the byte array
1077 * "byts" and index array "idxs". Recursively read the children.
1078 *
1079 * NOTE: The code here must match put_tree().
1080 *
1081 * Returns the index follosing the siblings.
1082 * Returns -1 if the file is shorter than expected.
1083 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001084 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085 static int
1086read_tree(fd, byts, idxs, maxidx, startidx)
1087 FILE *fd;
1088 char_u *byts;
1089 int *idxs;
1090 int maxidx; /* size of arrays */
1091 int startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001092{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001093 int len;
1094 int i;
1095 int n;
1096 int idx = startidx;
1097 int c;
1098#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001099
Bram Moolenaar51485f02005-06-04 21:55:20 +00001100 len = getc(fd); /* <siblingcount> */
1101 if (len <= 0)
1102 return -1;
1103
1104 if (startidx + len >= maxidx)
1105 return -2;
1106 byts[idx++] = len;
1107
1108 /* Read the byte values, flag/region bytes and shared indexes. */
1109 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001111 c = getc(fd); /* <byte> */
1112 if (c < 0)
1113 return -1;
1114 if (c <= BY_SPECIAL)
1115 {
1116 if (c == BY_NOFLAGS)
1117 {
1118 /* No flags, all regions. */
1119 idxs[idx] = 0;
1120 c = 0;
1121 }
1122 else if (c == BY_FLAGS)
1123 {
1124 /* Read flags and option region. */
1125 c = getc(fd); /* <flags> */
1126 if (c & WF_REGION)
1127 c = (getc(fd) << 8) + c; /* <region> */
1128 idxs[idx] = c;
1129 c = 0;
1130 }
1131 else /* c == BY_INDEX */
1132 {
1133 /* <nodeidx> */
1134 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1135 if (n < 0 || n >= maxidx)
1136 return -2;
1137 idxs[idx] = n + SHARED_MASK;
1138 c = getc(fd); /* <xbyte> */
1139 }
1140 }
1141 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001142 }
1143
Bram Moolenaar51485f02005-06-04 21:55:20 +00001144 /* Recursively read the children for non-shared siblings.
1145 * Skip the end-of-word ones (zero byte value) and the shared ones (and
1146 * remove SHARED_MASK) */
1147 for (i = 1; i <= len; ++i)
1148 if (byts[startidx + i] != 0)
1149 {
1150 if (idxs[startidx + i] & SHARED_MASK)
1151 idxs[startidx + i] &= ~SHARED_MASK;
1152 else
1153 {
1154 idxs[startidx + i] = idx;
1155 idx = read_tree(fd, byts, idxs, maxidx, idx);
1156 if (idx < 0)
1157 break;
1158 }
1159 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001160
Bram Moolenaar51485f02005-06-04 21:55:20 +00001161 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001162}
1163
1164/*
1165 * Parse 'spelllang' and set buf->b_langp accordingly.
1166 * Returns an error message or NULL.
1167 */
1168 char_u *
1169did_set_spelllang(buf)
1170 buf_T *buf;
1171{
1172 garray_T ga;
1173 char_u *lang;
1174 char_u *e;
1175 char_u *region;
1176 int region_mask;
1177 slang_T *lp;
1178 int c;
1179 char_u lbuf[MAXWLEN + 1];
1180
1181 ga_init2(&ga, sizeof(langp_T), 2);
1182
1183 /* loop over comma separated languages. */
1184 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
1185 {
1186 e = vim_strchr(lang, ',');
1187 if (e == NULL)
1188 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001189 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001190 if (e > lang + 2)
1191 {
1192 if (e - lang >= MAXWLEN)
1193 {
1194 ga_clear(&ga);
1195 return e_invarg;
1196 }
1197 if (lang[2] == '_')
1198 region = lang + 3;
1199 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001200
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001201 /* Check if we loaded this language before. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001202 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1203 if (STRNICMP(lp->sl_name, lang, 2) == 0)
1204 break;
1205
1206 if (lp == NULL)
1207 {
1208 /* Not found, load the language. */
1209 STRNCPY(lbuf, lang, e - lang);
1210 lbuf[e - lang] = NUL;
1211 if (region != NULL)
1212 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001213 spell_load_lang(lbuf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001214 }
1215
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001216 /*
1217 * Loop over the languages, there can be several files for each.
1218 */
1219 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1220 if (STRNICMP(lp->sl_name, lang, 2) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001221 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001222 region_mask = REGION_ALL;
1223 if (region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001224 {
1225 /* find region in sl_regions */
1226 c = find_region(lp->sl_regions, region);
1227 if (c == REGION_ALL)
1228 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001229 if (!lp->sl_add)
1230 {
1231 c = *e;
1232 *e = NUL;
1233 smsg((char_u *)_("Warning: region %s not supported"),
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001234 lang);
Bram Moolenaar3982c542005-06-08 21:56:31 +00001235 *e = c;
1236 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001237 }
1238 else
1239 region_mask = 1 << c;
1240 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001241
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001242 if (ga_grow(&ga, 1) == FAIL)
1243 {
1244 ga_clear(&ga);
1245 return e_outofmem;
1246 }
1247 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
1248 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
1249 ++ga.ga_len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001250 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001251
1252 if (*e == ',')
1253 ++e;
1254 }
1255
1256 /* Add a NULL entry to mark the end of the list. */
1257 if (ga_grow(&ga, 1) == FAIL)
1258 {
1259 ga_clear(&ga);
1260 return e_outofmem;
1261 }
1262 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
1263 ++ga.ga_len;
1264
1265 /* Everything is fine, store the new b_langp value. */
1266 ga_clear(&buf->b_langp);
1267 buf->b_langp = ga;
1268
1269 return NULL;
1270}
1271
1272/*
1273 * Find the region "region[2]" in "rp" (points to "sl_regions").
1274 * Each region is simply stored as the two characters of it's name.
1275 * Returns the index if found, REGION_ALL if not found.
1276 */
1277 static int
1278find_region(rp, region)
1279 char_u *rp;
1280 char_u *region;
1281{
1282 int i;
1283
1284 for (i = 0; ; i += 2)
1285 {
1286 if (rp[i] == NUL)
1287 return REGION_ALL;
1288 if (rp[i] == region[0] && rp[i + 1] == region[1])
1289 break;
1290 }
1291 return i / 2;
1292}
1293
1294/*
1295 * Return type of word:
1296 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00001297 * Word WF_ONECAP
1298 * W WORD WF_ALLCAP
1299 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001300 */
1301 static int
1302captype(word, end)
1303 char_u *word;
1304 char_u *end;
1305{
1306 char_u *p;
1307 int c;
1308 int firstcap;
1309 int allcap;
1310 int past_second = FALSE; /* past second word char */
1311
1312 /* find first letter */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001313 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001314 if (p >= end)
1315 return 0; /* only non-word characters, illegal word */
1316#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00001317 if (has_mbyte)
1318 c = mb_ptr2char_adv(&p);
1319 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001320#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00001321 c = *p++;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001322 firstcap = allcap = spell_isupper(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001323
1324 /*
1325 * Need to check all letters to find a word with mixed upper/lower.
1326 * But a word with an upper char only at start is a ONECAP.
1327 */
1328 for ( ; p < end; mb_ptr_adv(p))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001329 if (SPELL_ISWORDP(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001330 {
1331#ifdef FEAT_MBYTE
1332 c = mb_ptr2char(p);
1333#else
1334 c = *p;
1335#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001336 if (!spell_isupper(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001337 {
1338 /* UUl -> KEEPCAP */
1339 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001340 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001341 allcap = FALSE;
1342 }
1343 else if (!allcap)
1344 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001345 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001346 past_second = TRUE;
1347 }
1348
1349 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001350 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001351 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001352 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001353 return 0;
1354}
1355
1356# if defined(FEAT_MBYTE) || defined(PROTO)
1357/*
1358 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001359 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001360 */
1361 void
1362spell_reload()
1363{
1364 buf_T *buf;
1365 slang_T *lp;
Bram Moolenaar3982c542005-06-08 21:56:31 +00001366 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001367
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001368 /* Initialize the table for SPELL_ISWORDP(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001369 init_spell_chartab();
1370
1371 /* Unload all allocated memory. */
1372 while (first_lang != NULL)
1373 {
1374 lp = first_lang;
1375 first_lang = lp->sl_next;
1376 slang_free(lp);
1377 }
1378
1379 /* Go through all buffers and handle 'spelllang'. */
1380 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1381 {
1382 ga_clear(&buf->b_langp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00001383
1384 /* Only load the wordlists when 'spelllang' is set and there is a
1385 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001386 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00001387 {
1388 FOR_ALL_WINDOWS(wp)
1389 if (wp->w_buffer == buf && wp->w_p_spell)
1390 {
1391 (void)did_set_spelllang(buf);
1392# ifdef FEAT_WINDOWS
1393 break;
1394# endif
1395 }
1396 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001397 }
1398}
1399# endif
1400
Bram Moolenaarb765d632005-06-07 21:00:02 +00001401/*
1402 * Reload the spell file "fname" if it's loaded.
1403 */
1404 static void
1405spell_reload_one(fname)
1406 char_u *fname;
1407{
1408 slang_T *lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001409
Bram Moolenaarb765d632005-06-07 21:00:02 +00001410 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1411 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
1412 {
1413 slang_clear(lp);
1414 spell_load_file(fname, NULL, lp);
1415 redraw_all_later(NOT_VALID);
1416 }
1417}
1418
1419
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001420/*
1421 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001422 */
1423
Bram Moolenaar51485f02005-06-04 21:55:20 +00001424#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001425 and .dic file. */
1426/*
1427 * Main structure to store the contents of a ".aff" file.
1428 */
1429typedef struct afffile_S
1430{
1431 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
1432 char_u *af_try; /* "TRY" line in "af_enc" encoding */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001433 int af_rar; /* RAR ID for rare word */
1434 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001435 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
1436 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
1437 garray_T af_rep; /* list of repentry_T entries from REP lines */
1438} afffile_T;
1439
1440typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001441/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
1442struct affentry_S
1443{
1444 affentry_T *ae_next; /* next affix with same name/number */
1445 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
1446 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001447 char_u *ae_cond; /* condition (NULL for ".") */
1448 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449};
1450
1451/* Affix header from ".aff" file. Used for af_pref and af_suff. */
1452typedef struct affheader_S
1453{
1454 char_u ah_key[2]; /* key for hashtable == name of affix entry */
1455 int ah_combine; /* suffix may combine with prefix */
1456 affentry_T *ah_first; /* first affix entry */
1457} affheader_T;
1458
1459#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1460
1461/*
1462 * Structure that is used to store the items in the word tree. This avoids
1463 * the need to keep track of each allocated thing, it's freed all at once
1464 * after ":mkspell" is done.
1465 */
1466#define SBLOCKSIZE 16000 /* size of sb_data */
1467typedef struct sblock_S sblock_T;
1468struct sblock_S
1469{
1470 sblock_T *sb_next; /* next block in list */
1471 int sb_used; /* nr of bytes already in use */
1472 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001473};
1474
1475/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001476 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001477 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001478typedef struct wordnode_S wordnode_T;
1479struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001480{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001481 char_u wn_hashkey[6]; /* room for the hash key */
1482 wordnode_T *wn_next; /* next node with same hash key */
1483 wordnode_T *wn_child; /* child (next byte in word) */
1484 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
1485 always sorted) */
1486 wordnode_T *wn_wnode; /* parent node that will write this node */
1487 int wn_index; /* index in written nodes (valid after first
1488 round) */
1489 char_u wn_byte; /* Byte for this node. NUL for word end */
1490 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
1491 char_u wn_region; /* when wn_byte is NUL: region mask */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001492};
1493
Bram Moolenaar51485f02005-06-04 21:55:20 +00001494#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001495
Bram Moolenaar51485f02005-06-04 21:55:20 +00001496/*
1497 * Info used while reading the spell files.
1498 */
1499typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001500{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001501 wordnode_T *si_foldroot; /* tree with case-folded words */
1502 wordnode_T *si_keeproot; /* tree with keep-case words */
1503 sblock_T *si_blocks; /* memory blocks used */
1504 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001505 int si_add; /* addition file */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001506 int si_region; /* region mask */
1507 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00001508 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001509 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00001510 int si_region_count; /* number of regions supported (1 when there
1511 are no regions) */
1512 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001513} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001514
Bram Moolenaar51485f02005-06-04 21:55:20 +00001515static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar5482f332005-04-17 20:18:43 +00001516static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001517static void spell_free_aff __ARGS((afffile_T *aff));
1518static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001519static 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 +00001520static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
1521static void *getroom __ARGS((sblock_T **blp, size_t len));
1522static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
1523static void free_blocks __ARGS((sblock_T *bl));
1524static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar3982c542005-06-08 21:56:31 +00001525static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001526static 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 +00001527static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001528static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
1529static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00001530static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001531static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
Bram Moolenaarb765d632005-06-07 21:00:02 +00001532static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int verbose));
1533static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001534
1535/*
1536 * Read an affix ".aff" file.
Bram Moolenaar3982c542005-06-08 21:56:31 +00001537 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001538 */
1539 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00001540spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001541 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001542 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001543{
1544 FILE *fd;
1545 afffile_T *aff;
1546 char_u rline[MAXLINELEN];
1547 char_u *line;
1548 char_u *pc = NULL;
1549 char_u *(items[6]);
1550 int itemcnt;
1551 char_u *p;
1552 int lnum = 0;
1553 affheader_T *cur_aff = NULL;
1554 int aff_todo = 0;
1555 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001556 char_u *low = NULL;
1557 char_u *fol = NULL;
1558 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001559 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001560
Bram Moolenaar51485f02005-06-04 21:55:20 +00001561 /*
1562 * Open the file.
1563 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001564 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001565 if (fd == NULL)
1566 {
1567 EMSG2(_(e_notopen), fname);
1568 return NULL;
1569 }
1570
Bram Moolenaarb765d632005-06-07 21:00:02 +00001571 if (spin->si_verbose || p_verbose > 2)
1572 {
1573 if (!spin->si_verbose)
1574 verbose_enter();
1575 smsg((char_u *)_("Reading affix file %s..."), fname);
1576 out_flush();
1577 if (!spin->si_verbose)
1578 verbose_leave();
1579 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001580
Bram Moolenaar51485f02005-06-04 21:55:20 +00001581 /*
1582 * Allocate and init the afffile_T structure.
1583 */
1584 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001585 if (aff == NULL)
1586 return NULL;
1587 hash_init(&aff->af_pref);
1588 hash_init(&aff->af_suff);
1589 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
1590
1591 /*
1592 * Read all the lines in the file one by one.
1593 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001594 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001595 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001596 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001597 ++lnum;
1598
1599 /* Skip comment lines. */
1600 if (*rline == '#')
1601 continue;
1602
1603 /* Convert from "SET" to 'encoding' when needed. */
1604 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001605#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00001606 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001607 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001608 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001609 if (pc == NULL)
1610 {
1611 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
1612 fname, lnum, rline);
1613 continue;
1614 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001615 line = pc;
1616 }
1617 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00001618#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001619 {
1620 pc = NULL;
1621 line = rline;
1622 }
1623
1624 /* Split the line up in white separated items. Put a NUL after each
1625 * item. */
1626 itemcnt = 0;
1627 for (p = line; ; )
1628 {
1629 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
1630 ++p;
1631 if (*p == NUL)
1632 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001633 if (itemcnt == 6) /* too many items */
1634 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001635 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001636 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001637 ++p;
1638 if (*p == NUL)
1639 break;
1640 *p++ = NUL;
1641 }
1642
1643 /* Handle non-empty lines. */
1644 if (itemcnt > 0)
1645 {
1646 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
1647 && aff->af_enc == NULL)
1648 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00001649#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00001650 /* Setup for conversion from "ENC" to 'encoding'. */
1651 aff->af_enc = enc_canonize(items[1]);
1652 if (aff->af_enc != NULL && !spin->si_ascii
1653 && convert_setup(&spin->si_conv, aff->af_enc,
1654 p_enc) == FAIL)
1655 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
1656 fname, aff->af_enc, p_enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001657#else
1658 smsg((char_u *)_("Conversion in %s not supported"), fname);
1659#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001660 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00001661 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
1662 {
1663 /* ignored */
1664 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001665 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
1666 && aff->af_try == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001667 {
1668 aff->af_try = getroom_save(&spin->si_blocks, items[1]);
1669 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001670 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
1671 && aff->af_rar == 0)
1672 {
1673 aff->af_rar = items[1][0];
1674 if (items[1][1] != NUL)
1675 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
1676 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001677 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
1678 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001679 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00001680 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001681 if (items[1][1] != NUL)
1682 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
1683 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001684 else if ((STRCMP(items[0], "PFX") == 0
1685 || STRCMP(items[0], "SFX") == 0)
1686 && aff_todo == 0
1687 && itemcnt == 4)
1688 {
1689 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001690 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
1691 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001692 if (cur_aff == NULL)
1693 break;
1694 cur_aff->ah_key[0] = *items[1];
1695 cur_aff->ah_key[1] = NUL;
1696 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001697 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001698 if (*items[2] == 'Y')
1699 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001700 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001701 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
1702 fname, lnum, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001703 if (*items[0] == 'P')
1704 tp = &aff->af_pref;
1705 else
1706 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001707 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001708 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001709 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001710 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
1711 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001712 aff_todo = 0;
1713 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001714 else
1715 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001716 }
1717 else if ((STRCMP(items[0], "PFX") == 0
1718 || STRCMP(items[0], "SFX") == 0)
1719 && aff_todo > 0
1720 && STRCMP(cur_aff->ah_key, items[1]) == 0
1721 && itemcnt == 5)
1722 {
1723 affentry_T *aff_entry;
1724
1725 /* New item for an affix letter. */
1726 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001727 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
1728 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001729 if (aff_entry == NULL)
1730 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001731
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001732 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001733 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
1734 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001735 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001736 aff_entry->ae_add = getroom_save(&spin->si_blocks,
1737 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001738
Bram Moolenaar51485f02005-06-04 21:55:20 +00001739 /* Don't use an affix entry with non-ASCII characters when
1740 * "spin->si_ascii" is TRUE. */
1741 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00001742 || has_non_ascii(aff_entry->ae_add)))
1743 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00001744 aff_entry->ae_next = cur_aff->ah_first;
1745 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001746
1747 if (STRCMP(items[4], ".") != 0)
1748 {
1749 char_u buf[MAXLINELEN];
1750
1751 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
1752 items[4]);
1753 if (*items[0] == 'P')
1754 sprintf((char *)buf, "^%s", items[4]);
1755 else
1756 sprintf((char *)buf, "%s$", items[4]);
1757 aff_entry->ae_prog = vim_regcomp(buf,
1758 RE_MAGIC + RE_STRING);
1759 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00001760 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001761 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001762 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
1763 {
1764 if (fol != NULL)
1765 smsg((char_u *)_("Duplicate FOL in %s line %d"),
1766 fname, lnum);
1767 else
1768 fol = vim_strsave(items[1]);
1769 }
1770 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
1771 {
1772 if (low != NULL)
1773 smsg((char_u *)_("Duplicate LOW in %s line %d"),
1774 fname, lnum);
1775 else
1776 low = vim_strsave(items[1]);
1777 }
1778 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
1779 {
1780 if (upp != NULL)
1781 smsg((char_u *)_("Duplicate UPP in %s line %d"),
1782 fname, lnum);
1783 else
1784 upp = vim_strsave(items[1]);
1785 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001786 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
1787 /* Ignore REP count */;
1788 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
1789 {
1790 repentry_T *rp;
1791
1792 /* REP item */
1793 if (ga_grow(&aff->af_rep, 1) == FAIL)
1794 break;
1795 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001796 rp->re_from = getroom_save(&spin->si_blocks, items[1]);
1797 rp->re_to = getroom_save(&spin->si_blocks, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001798 ++aff->af_rep.ga_len;
1799 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001800 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001801 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
1802 fname, lnum, items[0]);
1803 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804 }
1805
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001806 if (fol != NULL || low != NULL || upp != NULL)
1807 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001808 /*
1809 * Don't write a word table for an ASCII file, so that we don't check
1810 * for conflicts with a word table that matches 'encoding'.
1811 * Don't write one for utf-8 either, we use utf_isupper() and
1812 * mb_get_class(), the list of chars in the file will be incomplete.
1813 */
1814 if (!spin->si_ascii
1815#ifdef FEAT_MBYTE
1816 && !enc_utf8
1817#endif
1818 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00001819 {
1820 if (fol == NULL || low == NULL || upp == NULL)
1821 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
1822 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00001823 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00001824 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001825
1826 vim_free(fol);
1827 vim_free(low);
1828 vim_free(upp);
1829 }
1830
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001831 vim_free(pc);
1832 fclose(fd);
1833 return aff;
1834}
1835
1836/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00001837 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
1838 * When "s" is NULL FALSE is returned.
1839 */
1840 static int
1841has_non_ascii(s)
1842 char_u *s;
1843{
1844 char_u *p;
1845
1846 if (s != NULL)
1847 for (p = s; *p != NUL; ++p)
1848 if (*p >= 128)
1849 return TRUE;
1850 return FALSE;
1851}
1852
1853/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001854 * Free the structure filled by spell_read_aff().
1855 */
1856 static void
1857spell_free_aff(aff)
1858 afffile_T *aff;
1859{
1860 hashtab_T *ht;
1861 hashitem_T *hi;
1862 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001863 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001864 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001865
1866 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001867
Bram Moolenaar51485f02005-06-04 21:55:20 +00001868 /* All this trouble to foree the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001869 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
1870 {
1871 todo = ht->ht_used;
1872 for (hi = ht->ht_array; todo > 0; ++hi)
1873 {
1874 if (!HASHITEM_EMPTY(hi))
1875 {
1876 --todo;
1877 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001878 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
1879 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001880 }
1881 }
1882 if (ht == &aff->af_suff)
1883 break;
1884 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001885
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001886 hash_clear(&aff->af_pref);
1887 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001888 ga_clear(&aff->af_rep);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889}
1890
1891/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001892 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001893 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001894 */
1895 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00001896spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001897 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001898 spellinfo_T *spin;
1899 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001900{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001901 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001902 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00001903 char_u *afflist;
1904 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001905 char_u *pc;
1906 char_u *w;
1907 int l;
1908 hash_T hash;
1909 hashitem_T *hi;
1910 FILE *fd;
1911 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001912 int non_ascii = 0;
1913 int retval = OK;
1914 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001915 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001916
Bram Moolenaar51485f02005-06-04 21:55:20 +00001917 /*
1918 * Open the file.
1919 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001920 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001921 if (fd == NULL)
1922 {
1923 EMSG2(_(e_notopen), fname);
1924 return FAIL;
1925 }
1926
Bram Moolenaar51485f02005-06-04 21:55:20 +00001927 /* The hashtable is only used to detect duplicated words. */
1928 hash_init(&ht);
1929
Bram Moolenaarb765d632005-06-07 21:00:02 +00001930 if (spin->si_verbose || p_verbose > 2)
1931 {
1932 if (!spin->si_verbose)
1933 verbose_enter();
1934 smsg((char_u *)_("Reading dictionary file %s..."), fname);
1935 out_flush();
1936 if (!spin->si_verbose)
1937 verbose_leave();
1938 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001939
1940 /* Read and ignore the first line: word count. */
1941 (void)vim_fgets(line, MAXLINELEN, fd);
1942 if (!isdigit(*skipwhite(line)))
1943 EMSG2(_("E760: No word count in %s"), fname);
1944
1945 /*
1946 * Read all the lines in the file one by one.
1947 * The words are converted to 'encoding' here, before being added to
1948 * the hashtable.
1949 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001950 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001952 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001953 ++lnum;
1954
Bram Moolenaar51485f02005-06-04 21:55:20 +00001955 /* Remove CR, LF and white space from the end. White space halfway
1956 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001957 l = STRLEN(line);
1958 while (l > 0 && line[l - 1] <= ' ')
1959 --l;
1960 if (l == 0)
1961 continue; /* empty line */
1962 line[l] = NUL;
1963
Bram Moolenaar51485f02005-06-04 21:55:20 +00001964 /* This takes time, print a message now and then. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001965 if (spin->si_verbose && (lnum & 0x3ff) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001966 {
1967 vim_snprintf((char *)message, sizeof(message),
1968 _("line %6d - %s"), lnum, line);
1969 msg_start();
1970 msg_outtrans_attr(message, 0);
1971 msg_clr_eos();
1972 msg_didout = FALSE;
1973 msg_col = 0;
1974 out_flush();
1975 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001976
Bram Moolenaar51485f02005-06-04 21:55:20 +00001977 /* Find the optional affix names. */
1978 afflist = vim_strchr(line, '/');
1979 if (afflist != NULL)
1980 *afflist++ = NUL;
1981
1982 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
1983 if (spin->si_ascii && has_non_ascii(line))
1984 {
1985 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001986 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001987 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00001988
Bram Moolenaarb765d632005-06-07 21:00:02 +00001989#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001990 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001991 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001992 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001993 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001994 if (pc == NULL)
1995 {
1996 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
1997 fname, lnum, line);
1998 continue;
1999 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002000 w = pc;
2001 }
2002 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002003#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002004 {
2005 pc = NULL;
2006 w = line;
2007 }
2008
Bram Moolenaar51485f02005-06-04 21:55:20 +00002009 /* Store the word in the hashtable to be able to find duplicates. */
2010 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002011 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002012 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002013 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002014 if (retval == FAIL)
2015 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002016
Bram Moolenaar51485f02005-06-04 21:55:20 +00002017 hash = hash_hash(dw);
2018 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019 if (!HASHITEM_EMPTY(hi))
2020 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00002021 fname, lnum, line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002022 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00002023 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002025 flags = 0;
2026 if (afflist != NULL)
2027 {
2028 /* Check for affix name that stands for keep-case word and stands
2029 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002030 if (affile->af_kep != NUL
2031 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002032 flags |= WF_KEEPCAP;
2033 if (affile->af_rar != NUL
2034 && vim_strchr(afflist, affile->af_rar) != NULL)
2035 flags |= WF_RARE;
2036 }
2037
Bram Moolenaar51485f02005-06-04 21:55:20 +00002038 /* Add the word to the word tree(s). */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002039 if (store_word(dw, spin, flags, spin->si_region) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002040 retval = FAIL;
2041
2042 if (afflist != NULL)
2043 {
2044 /* Find all matching suffixes and add the resulting words.
2045 * Additionally do matching prefixes that combine. */
2046 if (store_aff_word(dw, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002047 &affile->af_suff, &affile->af_pref,
2048 FALSE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002049 retval = FAIL;
2050
2051 /* Find all matching prefixes and add the resulting words. */
2052 if (store_aff_word(dw, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002053 &affile->af_pref, NULL, FALSE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002054 retval = FAIL;
2055 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002056 }
2057
Bram Moolenaar51485f02005-06-04 21:55:20 +00002058 if (spin->si_ascii && non_ascii > 0)
2059 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
2060 non_ascii);
2061 hash_clear(&ht);
2062
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002063 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002064 return retval;
2065}
2066
2067/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002068 * Apply affixes to a word and store the resulting words.
2069 * "ht" is the hashtable with affentry_T that need to be applied, either
2070 * prefixes or suffixes.
2071 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
2072 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002073 *
2074 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002076 static int
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002077store_aff_word(word, spin, afflist, ht, xht, comb, flags)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002078 char_u *word; /* basic word start */
2079 spellinfo_T *spin; /* spell info */
2080 char_u *afflist; /* list of names of supported affixes */
2081 hashtab_T *ht;
2082 hashtab_T *xht;
2083 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002084 int flags; /* flags for the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002085{
2086 int todo;
2087 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002088 affheader_T *ah;
2089 affentry_T *ae;
2090 regmatch_T regmatch;
2091 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002092 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002093 int i;
2094 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002095
Bram Moolenaar51485f02005-06-04 21:55:20 +00002096 todo = ht->ht_used;
2097 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098 {
2099 if (!HASHITEM_EMPTY(hi))
2100 {
2101 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002102 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00002103
Bram Moolenaar51485f02005-06-04 21:55:20 +00002104 /* Check that the affix combines, if required, and that the word
2105 * supports this affix. */
2106 if ((!comb || ah->ah_combine)
2107 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002108 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002109 /* Loop over all affix entries with this name. */
2110 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002111 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002112 /* Check the condition. It's not logical to match case
2113 * here, but it is required for compatibility with
2114 * Myspell. */
2115 regmatch.regprog = ae->ae_prog;
2116 regmatch.rm_ic = FALSE;
2117 if (ae->ae_prog == NULL
2118 || vim_regexec(&regmatch, word, (colnr_T)0))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002119 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 /* Match. Remove the chop and add the affix. */
2121 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002122 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002123 /* prefix: chop/add at the start of the word */
2124 if (ae->ae_add == NULL)
2125 *newword = NUL;
2126 else
2127 STRCPY(newword, ae->ae_add);
2128 p = word;
2129 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002132#ifdef FEAT_MBYTE
2133 if (has_mbyte)
2134 i = mb_charlen(ae->ae_chop);
2135 else
2136#endif
2137 i = STRLEN(ae->ae_chop);
2138 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002139 mb_ptr_adv(p);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002140 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002141 STRCAT(newword, p);
2142 }
2143 else
2144 {
2145 /* suffix: chop/add at the end of the word */
2146 STRCPY(newword, word);
2147 if (ae->ae_chop != NULL)
2148 {
2149 /* Remove chop string. */
2150 p = newword + STRLEN(newword);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002151#ifdef FEAT_MBYTE
2152 if (has_mbyte)
2153 i = mb_charlen(ae->ae_chop);
2154 else
2155#endif
2156 i = STRLEN(ae->ae_chop);
2157 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002158 mb_ptr_back(newword, p);
2159 *p = NUL;
2160 }
2161 if (ae->ae_add != NULL)
2162 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 }
2164
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165 /* Store the modified word. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002166 if (store_word(newword, spin,
2167 flags, spin->si_region) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002168 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169
Bram Moolenaar51485f02005-06-04 21:55:20 +00002170 /* When added a suffix and combining is allowed also
2171 * try adding prefixes additionally. */
2172 if (xht != NULL && ah->ah_combine)
2173 if (store_aff_word(newword, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002174 xht, NULL, TRUE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002175 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002176 }
2177 }
2178 }
2179 }
2180 }
2181
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002182 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183}
2184
2185/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002186 * Read a file with a list of words.
2187 */
2188 static int
2189spell_read_wordfile(fname, spin)
2190 char_u *fname;
2191 spellinfo_T *spin;
2192{
2193 FILE *fd;
2194 long lnum = 0;
2195 char_u rline[MAXLINELEN];
2196 char_u *line;
2197 char_u *pc = NULL;
2198 int l;
2199 int retval = OK;
2200 int did_word = FALSE;
2201 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002202 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002203 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002204
2205 /*
2206 * Open the file.
2207 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002208 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00002209 if (fd == NULL)
2210 {
2211 EMSG2(_(e_notopen), fname);
2212 return FAIL;
2213 }
2214
Bram Moolenaarb765d632005-06-07 21:00:02 +00002215 if (spin->si_verbose || p_verbose > 2)
2216 {
2217 if (!spin->si_verbose)
2218 verbose_enter();
2219 smsg((char_u *)_("Reading word file %s..."), fname);
2220 out_flush();
2221 if (!spin->si_verbose)
2222 verbose_leave();
2223 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002224
2225 /*
2226 * Read all the lines in the file one by one.
2227 */
2228 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2229 {
2230 line_breakcheck();
2231 ++lnum;
2232
2233 /* Skip comment lines. */
2234 if (*rline == '#')
2235 continue;
2236
2237 /* Remove CR, LF and white space from the end. */
2238 l = STRLEN(rline);
2239 while (l > 0 && rline[l - 1] <= ' ')
2240 --l;
2241 if (l == 0)
2242 continue; /* empty or blank line */
2243 rline[l] = NUL;
2244
2245 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
2246 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002247#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00002248 if (spin->si_conv.vc_type != CONV_NONE)
2249 {
2250 pc = string_convert(&spin->si_conv, rline, NULL);
2251 if (pc == NULL)
2252 {
2253 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2254 fname, lnum, rline);
2255 continue;
2256 }
2257 line = pc;
2258 }
2259 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00002260#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002261 {
2262 pc = NULL;
2263 line = rline;
2264 }
2265
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002266 flags = 0;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002267 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002268
2269 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00002270 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002271 ++line;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002272
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002273 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002274 {
2275 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002276 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
2277 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002278 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00002279 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
2280 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002281 else
2282 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002283#ifdef FEAT_MBYTE
2284 char_u *enc;
2285
Bram Moolenaar51485f02005-06-04 21:55:20 +00002286 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002287 line += 10;
2288 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002289 if (enc != NULL && !spin->si_ascii
2290 && convert_setup(&spin->si_conv, enc,
2291 p_enc) == FAIL)
2292 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00002293 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002294 vim_free(enc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002295#else
2296 smsg((char_u *)_("Conversion in %s not supported"), fname);
2297#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002298 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002299 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002300 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002301
Bram Moolenaar3982c542005-06-08 21:56:31 +00002302 if (STRNCMP(line, "regions=", 8) == 0)
2303 {
2304 if (spin->si_region_count > 1)
2305 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
2306 fname, lnum, line);
2307 else
2308 {
2309 line += 8;
2310 if (STRLEN(line) > 16)
2311 smsg((char_u *)_("Too many regions in %s line %d: %s"),
2312 fname, lnum, line);
2313 else
2314 {
2315 spin->si_region_count = STRLEN(line) / 2;
2316 STRCPY(spin->si_region_name, line);
2317 }
2318 }
2319 continue;
2320 }
2321
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002322 if (*line == '=')
2323 {
2324 /* keep-case word */
2325 flags |= WF_KEEPCAP;
2326 ++line;
2327 }
2328
2329 if (*line == '!')
2330 {
2331 /* Bad, bad, wicked word. */
2332 flags |= WF_BANNED;
2333 ++line;
2334 }
2335 else if (*line == '?')
2336 {
2337 /* Rare word. */
2338 flags |= WF_RARE;
2339 ++line;
2340 }
2341
Bram Moolenaar3982c542005-06-08 21:56:31 +00002342 if (VIM_ISDIGIT(*line))
2343 {
2344 /* region number(s) */
2345 regionmask = 0;
2346 while (VIM_ISDIGIT(*line))
2347 {
2348 l = *line - '0';
2349 if (l > spin->si_region_count)
2350 {
2351 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
2352 fname, lnum, line);
2353 break;
2354 }
2355 regionmask |= 1 << (l - 1);
2356 ++line;
2357 }
2358 flags |= WF_REGION;
2359 }
2360
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002361 if (flags == 0)
2362 {
2363 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00002364 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002365 continue;
2366 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002367 }
2368
2369 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
2370 if (spin->si_ascii && has_non_ascii(line))
2371 {
2372 ++non_ascii;
2373 continue;
2374 }
2375
2376 /* Normal word: store it. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002377 if (store_word(line, spin, flags, regionmask) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002378 {
2379 retval = FAIL;
2380 break;
2381 }
2382 did_word = TRUE;
2383 }
2384
2385 vim_free(pc);
2386 fclose(fd);
2387
Bram Moolenaarb765d632005-06-07 21:00:02 +00002388 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
2389 {
2390 if (p_verbose > 2)
2391 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00002392 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
2393 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002394 if (p_verbose > 2)
2395 verbose_leave();
2396 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002397 return retval;
2398}
2399
2400/*
2401 * Get part of an sblock_T, "len" bytes long.
2402 * This avoids calling free() for every little struct we use.
2403 * The memory is cleared to all zeros.
2404 * Returns NULL when out of memory.
2405 */
2406 static void *
2407getroom(blp, len)
2408 sblock_T **blp;
2409 size_t len; /* length needed */
2410{
2411 char_u *p;
2412 sblock_T *bl = *blp;
2413
2414 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
2415 {
2416 /* Allocate a block of memory. This is not freed until much later. */
2417 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
2418 if (bl == NULL)
2419 return NULL;
2420 bl->sb_next = *blp;
2421 *blp = bl;
2422 bl->sb_used = 0;
2423 }
2424
2425 p = bl->sb_data + bl->sb_used;
2426 bl->sb_used += len;
2427
2428 return p;
2429}
2430
2431/*
2432 * Make a copy of a string into memory allocated with getroom().
2433 */
2434 static char_u *
2435getroom_save(blp, s)
2436 sblock_T **blp;
2437 char_u *s;
2438{
2439 char_u *sc;
2440
2441 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
2442 if (sc != NULL)
2443 STRCPY(sc, s);
2444 return sc;
2445}
2446
2447
2448/*
2449 * Free the list of allocated sblock_T.
2450 */
2451 static void
2452free_blocks(bl)
2453 sblock_T *bl;
2454{
2455 sblock_T *next;
2456
2457 while (bl != NULL)
2458 {
2459 next = bl->sb_next;
2460 vim_free(bl);
2461 bl = next;
2462 }
2463}
2464
2465/*
2466 * Allocate the root of a word tree.
2467 */
2468 static wordnode_T *
2469wordtree_alloc(blp)
2470 sblock_T **blp;
2471{
2472 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
2473}
2474
2475/*
2476 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002477 * Always store it in the case-folded tree. A keep-case word can also be used
2478 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00002479 * For a keep-case word also store it in the keep-case tree.
2480 */
2481 static int
Bram Moolenaar3982c542005-06-08 21:56:31 +00002482store_word(word, spin, flags, region)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002483 char_u *word;
2484 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002485 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002486 int region; /* supported region(s) */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002487{
2488 int len = STRLEN(word);
2489 int ct = captype(word, word + len);
2490 char_u foldword[MAXWLEN];
2491 int res;
2492
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002493 if (flags & WF_KEEPCAP)
2494 res = OK; /* keep-case specified, don't add as fold-case */
2495 else
2496 {
2497 (void)spell_casefold(word, len, foldword, MAXWLEN);
2498 res = tree_add_word(foldword, spin->si_foldroot,
2499 (ct == WF_KEEPCAP ? WF_ALLCAP : ct) | flags,
Bram Moolenaar3982c542005-06-08 21:56:31 +00002500 region, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002501 }
2502
2503 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
2504 res = tree_add_word(word, spin->si_keeproot, flags,
Bram Moolenaar3982c542005-06-08 21:56:31 +00002505 region, &spin->si_blocks);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002506 return res;
2507}
2508
2509/*
2510 * Add word "word" to a word tree at "root".
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002511 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002512 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002513 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002514tree_add_word(word, root, flags, region, blp)
2515 char_u *word;
2516 wordnode_T *root;
2517 int flags;
2518 int region;
2519 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002520{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002521 wordnode_T *node = root;
2522 wordnode_T *np;
2523 wordnode_T **prev = NULL;
2524 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002525
Bram Moolenaar51485f02005-06-04 21:55:20 +00002526 /* Add each byte of the word to the tree, including the NUL at the end. */
2527 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002528 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002529 /* Look for the sibling that has the same character. They are sorted
2530 * on byte value, thus stop searching when a sibling is found with a
2531 * higher byte value. For zero bytes (end of word) check that the
2532 * flags are equal, there is a separate zero byte for each flag value.
2533 */
2534 while (node != NULL && (node->wn_byte < word[i]
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002535 || (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002536 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002537 prev = &node->wn_sibling;
2538 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002539 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002540 if (node == NULL || node->wn_byte != word[i])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002541 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002542 /* Allocate a new node. */
2543 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
2544 if (np == NULL)
2545 return FAIL;
2546 np->wn_byte = word[i];
2547 *prev = np;
2548 np->wn_sibling = node;
2549 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002551
Bram Moolenaar51485f02005-06-04 21:55:20 +00002552 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002553 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002554 node->wn_flags = flags;
2555 node->wn_region |= region;
2556 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002557 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002558 prev = &node->wn_child;
2559 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002560 }
2561
2562 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563}
2564
2565/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002566 * Compress a tree: find tails that are identical and can be shared.
2567 */
2568 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002569wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002570 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002571 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002572{
2573 hashtab_T ht;
2574 int n;
2575 int tot = 0;
2576
2577 if (root != NULL)
2578 {
2579 hash_init(&ht);
2580 n = node_compress(root, &ht, &tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002581 if (spin->si_verbose || p_verbose > 2)
2582 {
2583 if (!spin->si_verbose)
2584 verbose_enter();
2585 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00002586 n, tot, (tot - n) * 100 / tot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002587 if (p_verbose > 2)
2588 verbose_leave();
2589 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002590 hash_clear(&ht);
2591 }
2592}
2593
2594/*
2595 * Compress a node, its siblings and its children, depth first.
2596 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002597 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002598 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002599node_compress(node, ht, tot)
2600 wordnode_T *node;
2601 hashtab_T *ht;
2602 int *tot; /* total count of nodes before compressing,
2603 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002604{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002605 wordnode_T *np;
2606 wordnode_T *tp;
2607 wordnode_T *child;
2608 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002610 int len = 0;
2611 unsigned nr, n;
2612 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002613
Bram Moolenaar51485f02005-06-04 21:55:20 +00002614 /*
2615 * Go through the list of siblings. Compress each child and then try
2616 * finding an identical child to replace it.
2617 * Note that with "child" we mean not just the node that is pointed to,
2618 * but the whole list of siblings, of which the node is the first.
2619 */
2620 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002621 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002622 ++len;
2623 if ((child = np->wn_child) != NULL)
2624 {
2625 /* Compress the child. This fills wn_hashkey. */
2626 compressed += node_compress(child, ht, tot);
2627
2628 /* Try to find an identical child. */
2629 hash = hash_hash(child->wn_hashkey);
2630 hi = hash_lookup(ht, child->wn_hashkey, hash);
2631 tp = NULL;
2632 if (!HASHITEM_EMPTY(hi))
2633 {
2634 /* There are children with an identical hash value. Now check
2635 * if there is one that is really identical. */
2636 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
2637 if (node_equal(child, tp))
2638 {
2639 /* Found one! Now use that child in place of the
2640 * current one. This means the current child is
2641 * dropped from the tree. */
2642 np->wn_child = tp;
2643 ++compressed;
2644 break;
2645 }
2646 if (tp == NULL)
2647 {
2648 /* No other child with this hash value equals the child of
2649 * the node, add it to the linked list after the first
2650 * item. */
2651 tp = HI2WN(hi);
2652 child->wn_next = tp->wn_next;
2653 tp->wn_next = child;
2654 }
2655 }
2656 else
2657 /* No other child has this hash value, add it to the
2658 * hashtable. */
2659 hash_add_item(ht, hi, child->wn_hashkey, hash);
2660 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002661 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002662 *tot += len;
2663
2664 /*
2665 * Make a hash key for the node and its siblings, so that we can quickly
2666 * find a lookalike node. This must be done after compressing the sibling
2667 * list, otherwise the hash key would become invalid by the compression.
2668 */
2669 node->wn_hashkey[0] = len;
2670 nr = 0;
2671 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002672 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002673 if (np->wn_byte == NUL)
2674 /* end node: only use wn_flags and wn_region */
2675 n = np->wn_flags + (np->wn_region << 8);
2676 else
2677 /* byte node: use the byte value and the child pointer */
2678 n = np->wn_byte + ((long_u)np->wn_child << 8);
2679 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002681
2682 /* Avoid NUL bytes, it terminates the hash key. */
2683 n = nr & 0xff;
2684 node->wn_hashkey[1] = n == 0 ? 1 : n;
2685 n = (nr >> 8) & 0xff;
2686 node->wn_hashkey[2] = n == 0 ? 1 : n;
2687 n = (nr >> 16) & 0xff;
2688 node->wn_hashkey[3] = n == 0 ? 1 : n;
2689 n = (nr >> 24) & 0xff;
2690 node->wn_hashkey[4] = n == 0 ? 1 : n;
2691 node->wn_hashkey[5] = NUL;
2692
2693 return compressed;
2694}
2695
2696/*
2697 * Return TRUE when two nodes have identical siblings and children.
2698 */
2699 static int
2700node_equal(n1, n2)
2701 wordnode_T *n1;
2702 wordnode_T *n2;
2703{
2704 wordnode_T *p1;
2705 wordnode_T *p2;
2706
2707 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
2708 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
2709 if (p1->wn_byte != p2->wn_byte
2710 || (p1->wn_byte == NUL
2711 ? (p1->wn_flags != p2->wn_flags
2712 || p1->wn_region != p2->wn_region)
2713 : (p1->wn_child != p2->wn_child)))
2714 break;
2715
2716 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002717}
2718
2719/*
2720 * Write a number to file "fd", MSB first, in "len" bytes.
2721 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002722 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002723put_bytes(fd, nr, len)
2724 FILE *fd;
2725 long_u nr;
2726 int len;
2727{
2728 int i;
2729
2730 for (i = len - 1; i >= 0; --i)
2731 putc((int)(nr >> (i * 8)), fd);
2732}
2733
2734/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002735 * Write the Vim spell file "fname".
2736 */
2737 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00002738write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002739 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002740 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002741{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002742 FILE *fd;
2743 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002744 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002745 wordnode_T *tree;
2746 int nodecount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747
Bram Moolenaarb765d632005-06-07 21:00:02 +00002748 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00002749 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002750 {
2751 EMSG2(_(e_notopen), fname);
2752 return;
2753 }
2754
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002755 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
2756 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002757
2758 /* <fileID> */
2759 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
2760 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002761
2762 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00002763 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002764 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002765 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
2766 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
2767 (size_t)1, fd);
2768 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 }
2770 else
2771 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002772 putc(0, fd);
2773 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002774 }
2775
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002776 /* Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002777 * <charflagslen> <charflags> <fcharlen> <fchars>
2778 * Skip this for ASCII, the table may conflict with the one used for
2779 * 'encoding'. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002780 if (spin->si_ascii)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002781 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002782 putc(0, fd);
2783 putc(0, fd);
2784 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002785 }
2786 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00002787 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002788
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002789
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002790 /* <SUGGEST> : <suggestlen> <more> ...
2791 * TODO. Only write a zero length for now. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002792 put_bytes(fd, 0L, 4); /* <suggestlen> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002793
Bram Moolenaar50cde822005-06-05 21:54:54 +00002794 spin->si_memtot = 0;
2795
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002796 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002797 * <LWORDTREE> <KWORDTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002798 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002799 for (round = 1; round <= 2; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002800 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002801 tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002802
Bram Moolenaar51485f02005-06-04 21:55:20 +00002803 /* Count the number of nodes. Needed to be able to allocate the
2804 * memory when reading the nodes. Also fills in the index for shared
2805 * nodes. */
2806 nodecount = put_tree(NULL, tree, 0, regionmask);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807
Bram Moolenaar51485f02005-06-04 21:55:20 +00002808 /* number of nodes in 4 bytes */
2809 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002810 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002811
Bram Moolenaar51485f02005-06-04 21:55:20 +00002812 /* Write the nodes. */
2813 (void)put_tree(fd, tree, 0, regionmask);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002814 }
2815
Bram Moolenaar51485f02005-06-04 21:55:20 +00002816 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002817}
2818
2819/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002820 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002821 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002822 * This first writes the list of possible bytes (siblings). Then for each
2823 * byte recursively write the children.
2824 *
2825 * NOTE: The code here must match the code in read_tree(), since assumptions
2826 * are made about the indexes (so that we don't have to write them in the
2827 * file).
2828 *
2829 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002830 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002831 static int
2832put_tree(fd, node, index, regionmask)
2833 FILE *fd; /* NULL when only counting */
2834 wordnode_T *node;
2835 int index;
2836 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002837{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002838 int newindex = index;
2839 int siblingcount = 0;
2840 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002841 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002842
Bram Moolenaar51485f02005-06-04 21:55:20 +00002843 /* If "node" is zero the tree is empty. */
2844 if (node == NULL)
2845 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002846
Bram Moolenaar51485f02005-06-04 21:55:20 +00002847 /* Store the index where this node is written. */
2848 node->wn_index = index;
2849
2850 /* Count the number of siblings. */
2851 for (np = node; np != NULL; np = np->wn_sibling)
2852 ++siblingcount;
2853
2854 /* Write the sibling count. */
2855 if (fd != NULL)
2856 putc(siblingcount, fd); /* <siblingcount> */
2857
2858 /* Write each sibling byte and optionally extra info. */
2859 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002860 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002861 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002862 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002863 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002864 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002865 /* For a NUL byte (end of word) instead of the byte itself
2866 * we write the flag/region items. */
2867 flags = np->wn_flags;
2868 if (regionmask != 0 && np->wn_region != regionmask)
2869 flags |= WF_REGION;
2870 if (flags == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002871 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002872 /* word without flags or region */
2873 putc(BY_NOFLAGS, fd); /* <byte> */
2874 }
2875 else
2876 {
2877 putc(BY_FLAGS, fd); /* <byte> */
2878 putc(flags, fd); /* <flags> */
2879 if (flags & WF_REGION)
2880 putc(np->wn_region, fd); /* <regionmask> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002881 }
2882 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002883 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002884 else
2885 {
2886 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
2887 {
2888 /* The child is written elsewhere, write the reference. */
2889 if (fd != NULL)
2890 {
2891 putc(BY_INDEX, fd); /* <byte> */
2892 /* <nodeidx> */
2893 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
2894 }
2895 }
2896 else if (np->wn_child->wn_wnode == NULL)
2897 /* We will write the child below and give it an index. */
2898 np->wn_child->wn_wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002899
Bram Moolenaar51485f02005-06-04 21:55:20 +00002900 if (fd != NULL)
2901 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
2902 {
2903 EMSG(_(e_write));
2904 return 0;
2905 }
2906 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002907 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002908
2909 /* Space used in the array when reading: one for each sibling and one for
2910 * the count. */
2911 newindex += siblingcount + 1;
2912
2913 /* Recursively dump the children of each sibling. */
2914 for (np = node; np != NULL; np = np->wn_sibling)
2915 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
2916 newindex = put_tree(fd, np->wn_child, newindex, regionmask);
2917
2918 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002919}
2920
2921
2922/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00002923 * ":mkspell [-ascii] outfile infile ..."
2924 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002925 */
2926 void
2927ex_mkspell(eap)
2928 exarg_T *eap;
2929{
2930 int fcount;
2931 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002932 char_u *arg = eap->arg;
2933 int ascii = FALSE;
2934
2935 if (STRNCMP(arg, "-ascii", 6) == 0)
2936 {
2937 ascii = TRUE;
2938 arg = skipwhite(arg + 6);
2939 }
2940
2941 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
2942 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
2943 {
2944 mkspell(fcount, fnames, ascii, eap->forceit, TRUE);
2945 FreeWild(fcount, fnames);
2946 }
2947}
2948
2949/*
2950 * Create a Vim spell file from one or more word lists.
2951 * "fnames[0]" is the output file name.
2952 * "fnames[fcount - 1]" is the last input file name.
2953 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
2954 * and ".spl" is appended to make the output file name.
2955 */
2956 static void
2957mkspell(fcount, fnames, ascii, overwrite, verbose)
2958 int fcount;
2959 char_u **fnames;
2960 int ascii; /* -ascii argument given */
2961 int overwrite; /* overwrite existing output file */
2962 int verbose; /* give progress messages */
2963{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002964 char_u fname[MAXPATHL];
2965 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00002966 char_u **innames;
2967 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002968 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002969 int i;
2970 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002971 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002972 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002973 spellinfo_T spin;
2974
2975 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaarb765d632005-06-07 21:00:02 +00002976 spin.si_verbose = verbose;
2977 spin.si_ascii = ascii;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002978
Bram Moolenaarb765d632005-06-07 21:00:02 +00002979 /* default: fnames[0] is output file, following are input files */
2980 innames = &fnames[1];
2981 incount = fcount - 1;
2982
2983 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00002984 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00002985 len = STRLEN(fnames[0]);
2986 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
2987 {
2988 /* For ":mkspell path/en.latin1.add" output file is
2989 * "path/en.latin1.add.spl". */
2990 innames = &fnames[0];
2991 incount = 1;
2992 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
2993 }
2994 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
2995 {
2996 /* Name ends in ".spl", use as the file name. */
2997 STRNCPY(wfname, fnames[0], sizeof(wfname));
2998 wfname[sizeof(wfname) - 1] = NUL;
2999 }
3000 else
3001 /* Name should be language, make the file name from it. */
3002 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
3003 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
3004
3005 /* Check for .ascii.spl. */
3006 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
3007 spin.si_ascii = TRUE;
3008
3009 /* Check for .add.spl. */
3010 if (strstr((char *)gettail(wfname), ".add.") != NULL)
3011 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003012 }
3013
Bram Moolenaarb765d632005-06-07 21:00:02 +00003014 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003015 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003016 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003017 EMSG(_("E754: Only up to 8 regions supported"));
3018 else
3019 {
3020 /* Check for overwriting before doing things that may take a lot of
3021 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003022 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003023 {
3024 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003025 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003026 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003027 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003028 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003029 EMSG2(_(e_isadir2), wfname);
3030 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003031 }
3032
3033 /*
3034 * Init the aff and dic pointers.
3035 * Get the region names if there are more than 2 arguments.
3036 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003037 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003038 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003039 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003040
Bram Moolenaar3982c542005-06-08 21:56:31 +00003041 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003042 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003043 len = STRLEN(innames[i]);
3044 if (STRLEN(gettail(innames[i])) < 5
3045 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003046 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003047 EMSG2(_("E755: Invalid region in %s"), innames[i]);
3048 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003049 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003050 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
3051 spin.si_region_name[i * 2 + 1] =
3052 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003053 }
3054 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003055 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003056
Bram Moolenaarb765d632005-06-07 21:00:02 +00003057 if (!spin.si_add)
3058 /* Clear the char type tables, don't want to use any of the
3059 * currently used spell properties. */
3060 init_spell_chartab();
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003061
Bram Moolenaar51485f02005-06-04 21:55:20 +00003062 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
3063 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
3064 if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
3065 {
3066 error = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003067 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003068 }
3069
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003070 /*
3071 * Read all the .aff and .dic files.
3072 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003073 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003074 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003075 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003076 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003077 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003078 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003079
Bram Moolenaarb765d632005-06-07 21:00:02 +00003080 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003081 if (mch_stat((char *)fname, &st) >= 0)
3082 {
3083 /* Read the .aff file. Will init "spin->si_conv" based on the
3084 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003085 afile[i] = spell_read_aff(fname, &spin);
3086 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003087 error = TRUE;
3088 else
3089 {
3090 /* Read the .dic file and store the words in the trees. */
3091 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00003092 innames[i]);
3093 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003094 error = TRUE;
3095 }
3096 }
3097 else
3098 {
3099 /* No .aff file, try reading the file as a word list. Store
3100 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003101 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003102 error = TRUE;
3103 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003104
Bram Moolenaarb765d632005-06-07 21:00:02 +00003105#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003107 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003108#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003109 }
3110
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003112 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003113 /*
3114 * Remove the dummy NUL from the start of the tree root.
3115 */
3116 spin.si_foldroot = spin.si_foldroot->wn_sibling;
3117 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003118
3119 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003120 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003121 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003122 if (verbose || p_verbose > 2)
3123 {
3124 if (!verbose)
3125 verbose_enter();
3126 MSG(_("Compressing word tree..."));
3127 out_flush();
3128 if (!verbose)
3129 verbose_leave();
3130 }
3131 wordtree_compress(spin.si_foldroot, &spin);
3132 wordtree_compress(spin.si_keeproot, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003133 }
3134
Bram Moolenaar51485f02005-06-04 21:55:20 +00003135 if (!error)
3136 {
3137 /*
3138 * Write the info in the spell file.
3139 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003140 if (verbose || p_verbose > 2)
3141 {
3142 if (!verbose)
3143 verbose_enter();
3144 smsg((char_u *)_("Writing spell file %s..."), wfname);
3145 out_flush();
3146 if (!verbose)
3147 verbose_leave();
3148 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003149
Bram Moolenaar3982c542005-06-08 21:56:31 +00003150 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003151
3152 if (verbose || p_verbose > 2)
3153 {
3154 if (!verbose)
3155 verbose_enter();
3156 MSG(_("Done!"));
3157 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00003158 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003159 out_flush();
3160 if (!verbose)
3161 verbose_leave();
3162 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003163
Bram Moolenaarb765d632005-06-07 21:00:02 +00003164 /* If the file is loaded need to reload it. */
3165 spell_reload_one(wfname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003166 }
3167
3168 /* Free the allocated memory. */
3169 free_blocks(spin.si_blocks);
3170
3171 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003172 for (i = 0; i < incount; ++i)
3173 if (afile[i] != NULL)
3174 spell_free_aff(afile[i]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003175 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003176}
3177
Bram Moolenaarb765d632005-06-07 21:00:02 +00003178
3179/*
3180 * ":spellgood {word}"
3181 * ":spellwrong {word}"
3182 */
3183 void
3184ex_spell(eap)
3185 exarg_T *eap;
3186{
3187 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong);
3188}
3189
3190/*
3191 * Add "word[len]" to 'spellfile' as a good or bad word.
3192 */
3193 void
3194spell_add_word(word, len, bad)
3195 char_u *word;
3196 int len;
3197 int bad;
3198{
3199 FILE *fd;
3200 buf_T *buf;
3201
3202 if (*curbuf->b_p_spf == NUL)
3203 init_spellfile();
3204 if (*curbuf->b_p_spf == NUL)
3205 EMSG(_("E999: 'spellfile' is not set"));
3206 else
3207 {
3208 /* Check that the user isn't editing the .add file somewhere. */
3209 buf = buflist_findname_exp(curbuf->b_p_spf);
3210 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
3211 buf = NULL;
3212 if (buf != NULL && bufIsChanged(buf))
3213 EMSG(_(e_bufloaded));
3214 else
3215 {
3216 fd = mch_fopen((char *)curbuf->b_p_spf, "a");
3217 if (fd == NULL)
3218 EMSG2(_(e_notopen), curbuf->b_p_spf);
3219 else
3220 {
3221 if (bad)
3222 fprintf(fd, "/!%.*s\n", len, word);
3223 else
3224 fprintf(fd, "%.*s\n", len, word);
3225 fclose(fd);
3226
3227 /* Update the .add.spl file. */
3228 mkspell(1, &curbuf->b_p_spf, FALSE, TRUE, FALSE);
3229
3230 /* If the .add file is edited somewhere, reload it. */
3231 if (buf != NULL)
3232 buf_reload(buf);
3233 }
3234 }
3235 }
3236}
3237
3238/*
3239 * Initialize 'spellfile' for the current buffer.
3240 */
3241 static void
3242init_spellfile()
3243{
3244 char_u buf[MAXPATHL];
3245 int l;
3246 slang_T *sl;
3247 char_u *rtp;
3248
3249 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
3250 {
3251 /* Loop over all entries in 'runtimepath'. */
3252 rtp = p_rtp;
3253 while (*rtp != NUL)
3254 {
3255 /* Copy the path from 'runtimepath' to buf[]. */
3256 copy_option_part(&rtp, buf, MAXPATHL, ",");
3257 if (filewritable(buf) == 2)
3258 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003259 /* Use the first language name from 'spelllang' and the
3260 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003261 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
3262 l = STRLEN(buf);
3263 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00003264 "/spell/%.*s.%s.add",
3265 2, curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00003266 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
3267 ? (char_u *)"ascii" : spell_enc());
3268 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
3269 break;
3270 }
3271 }
3272 }
3273}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003274
Bram Moolenaar51485f02005-06-04 21:55:20 +00003275
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003276/*
3277 * Init the chartab used for spelling for ASCII.
3278 * EBCDIC is not supported!
3279 */
3280 static void
3281clear_spell_chartab(sp)
3282 spelltab_T *sp;
3283{
3284 int i;
3285
3286 /* Init everything to FALSE. */
3287 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
3288 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
3289 for (i = 0; i < 256; ++i)
3290 sp->st_fold[i] = i;
3291
3292 /* We include digits. A word shouldn't start with a digit, but handling
3293 * that is done separately. */
3294 for (i = '0'; i <= '9'; ++i)
3295 sp->st_isw[i] = TRUE;
3296 for (i = 'A'; i <= 'Z'; ++i)
3297 {
3298 sp->st_isw[i] = TRUE;
3299 sp->st_isu[i] = TRUE;
3300 sp->st_fold[i] = i + 0x20;
3301 }
3302 for (i = 'a'; i <= 'z'; ++i)
3303 sp->st_isw[i] = TRUE;
3304}
3305
3306/*
3307 * Init the chartab used for spelling. Only depends on 'encoding'.
3308 * Called once while starting up and when 'encoding' changes.
3309 * The default is to use isalpha(), but the spell file should define the word
3310 * characters to make it possible that 'encoding' differs from the current
3311 * locale.
3312 */
3313 void
3314init_spell_chartab()
3315{
3316 int i;
3317
3318 did_set_spelltab = FALSE;
3319 clear_spell_chartab(&spelltab);
3320
3321#ifdef FEAT_MBYTE
3322 if (enc_dbcs)
3323 {
3324 /* DBCS: assume double-wide characters are word characters. */
3325 for (i = 128; i <= 255; ++i)
3326 if (MB_BYTE2LEN(i) == 2)
3327 spelltab.st_isw[i] = TRUE;
3328 }
3329 else
3330#endif
3331 {
3332 /* Rough guess: use isalpha() and isupper() for characters above 128. */
3333 for (i = 128; i < 256; ++i)
3334 {
3335 spelltab.st_isw[i] = MB_ISUPPER(i) || MB_ISLOWER(i);
3336 if (MB_ISUPPER(i))
3337 {
3338 spelltab.st_isu[i] = TRUE;
3339 spelltab.st_fold[i] = MB_TOLOWER(i);
3340 }
3341 }
3342 }
3343}
3344
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003345static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
3346static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
3347
3348/*
3349 * Set the spell character tables from strings in the affix file.
3350 */
3351 static int
3352set_spell_chartab(fol, low, upp)
3353 char_u *fol;
3354 char_u *low;
3355 char_u *upp;
3356{
3357 /* We build the new tables here first, so that we can compare with the
3358 * previous one. */
3359 spelltab_T new_st;
3360 char_u *pf = fol, *pl = low, *pu = upp;
3361 int f, l, u;
3362
3363 clear_spell_chartab(&new_st);
3364
3365 while (*pf != NUL)
3366 {
3367 if (*pl == NUL || *pu == NUL)
3368 {
3369 EMSG(_(e_affform));
3370 return FAIL;
3371 }
3372#ifdef FEAT_MBYTE
3373 f = mb_ptr2char_adv(&pf);
3374 l = mb_ptr2char_adv(&pl);
3375 u = mb_ptr2char_adv(&pu);
3376#else
3377 f = *pf++;
3378 l = *pl++;
3379 u = *pu++;
3380#endif
3381 /* Every character that appears is a word character. */
3382 if (f < 256)
3383 new_st.st_isw[f] = TRUE;
3384 if (l < 256)
3385 new_st.st_isw[l] = TRUE;
3386 if (u < 256)
3387 new_st.st_isw[u] = TRUE;
3388
3389 /* if "LOW" and "FOL" are not the same the "LOW" char needs
3390 * case-folding */
3391 if (l < 256 && l != f)
3392 {
3393 if (f >= 256)
3394 {
3395 EMSG(_(e_affrange));
3396 return FAIL;
3397 }
3398 new_st.st_fold[l] = f;
3399 }
3400
3401 /* if "UPP" and "FOL" are not the same the "UPP" char needs
3402 * case-folding and it's upper case. */
3403 if (u < 256 && u != f)
3404 {
3405 if (f >= 256)
3406 {
3407 EMSG(_(e_affrange));
3408 return FAIL;
3409 }
3410 new_st.st_fold[u] = f;
3411 new_st.st_isu[u] = TRUE;
3412 }
3413 }
3414
3415 if (*pl != NUL || *pu != NUL)
3416 {
3417 EMSG(_(e_affform));
3418 return FAIL;
3419 }
3420
3421 return set_spell_finish(&new_st);
3422}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003423
3424/*
3425 * Set the spell character tables from strings in the .spl file.
3426 */
3427 static int
3428set_spell_charflags(flags, cnt, upp)
3429 char_u *flags;
3430 int cnt;
3431 char_u *upp;
3432{
3433 /* We build the new tables here first, so that we can compare with the
3434 * previous one. */
3435 spelltab_T new_st;
3436 int i;
3437 char_u *p = upp;
3438
3439 clear_spell_chartab(&new_st);
3440
3441 for (i = 0; i < cnt; ++i)
3442 {
3443 new_st.st_isw[i + 128] = (flags[i] & SPELL_ISWORD) != 0;
3444 new_st.st_isu[i + 128] = (flags[i] & SPELL_ISUPPER) != 0;
3445
3446 if (*p == NUL)
3447 return FAIL;
3448#ifdef FEAT_MBYTE
3449 new_st.st_fold[i + 128] = mb_ptr2char_adv(&p);
3450#else
3451 new_st.st_fold[i + 128] = *p++;
3452#endif
3453 }
3454
3455 return set_spell_finish(&new_st);
3456}
3457
3458 static int
3459set_spell_finish(new_st)
3460 spelltab_T *new_st;
3461{
3462 int i;
3463
3464 if (did_set_spelltab)
3465 {
3466 /* check that it's the same table */
3467 for (i = 0; i < 256; ++i)
3468 {
3469 if (spelltab.st_isw[i] != new_st->st_isw[i]
3470 || spelltab.st_isu[i] != new_st->st_isu[i]
3471 || spelltab.st_fold[i] != new_st->st_fold[i])
3472 {
3473 EMSG(_("E763: Word characters differ between spell files"));
3474 return FAIL;
3475 }
3476 }
3477 }
3478 else
3479 {
3480 /* copy the new spelltab into the one being used */
3481 spelltab = *new_st;
3482 did_set_spelltab = TRUE;
3483 }
3484
3485 return OK;
3486}
3487
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003488/*
3489 * Write the current tables into the .spl file.
3490 * This makes sure the same characters are recognized as word characters when
3491 * generating an when using a spell file.
3492 */
3493 static void
3494write_spell_chartab(fd)
3495 FILE *fd;
3496{
3497 char_u charbuf[256 * 4];
3498 int len = 0;
3499 int flags;
3500 int i;
3501
3502 fputc(128, fd); /* <charflagslen> */
3503 for (i = 128; i < 256; ++i)
3504 {
3505 flags = 0;
3506 if (spelltab.st_isw[i])
3507 flags |= SPELL_ISWORD;
3508 if (spelltab.st_isu[i])
3509 flags |= SPELL_ISUPPER;
3510 fputc(flags, fd); /* <charflags> */
3511
Bram Moolenaarb765d632005-06-07 21:00:02 +00003512#ifdef FEAT_MBYTE
3513 if (has_mbyte)
3514 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
3515 else
3516#endif
3517 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003518 }
3519
3520 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
3521 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
3522}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003523
3524/*
3525 * Return TRUE if "c" is an upper-case character for spelling.
3526 */
3527 static int
3528spell_isupper(c)
3529 int c;
3530{
3531# ifdef FEAT_MBYTE
3532 if (enc_utf8)
3533 {
3534 /* For Unicode we can call utf_isupper(), but don't do that for ASCII,
3535 * because we don't want to use 'casemap' here. */
3536 if (c >= 128)
3537 return utf_isupper(c);
3538 }
3539 else if (has_mbyte && c > 256)
3540 {
3541 /* For characters above 255 we don't have something specfied.
3542 * Fall back to locale-dependent iswupper(). If not available
3543 * simply return FALSE. */
3544# ifdef HAVE_ISWUPPER
3545 return iswupper(c);
3546# else
3547 return FALSE;
3548# endif
3549 }
3550# endif
3551 return spelltab.st_isu[c];
3552}
3553
3554/*
3555 * Case-fold "p[len]" into "buf[buflen]". Used for spell checking.
3556 * When using a multi-byte 'encoding' the length may change!
3557 * Returns FAIL when something wrong.
3558 */
3559 static int
3560spell_casefold(p, len, buf, buflen)
3561 char_u *p;
3562 int len;
3563 char_u *buf;
3564 int buflen;
3565{
3566 int i;
3567
3568 if (len >= buflen)
3569 {
3570 buf[0] = NUL;
3571 return FAIL; /* result will not fit */
3572 }
3573
3574#ifdef FEAT_MBYTE
3575 if (has_mbyte)
3576 {
3577 int c;
3578 int outi = 0;
3579
3580 /* Fold one character at a time. */
3581 for (i = 0; i < len; i += mb_ptr2len_check(p + i))
3582 {
3583 c = mb_ptr2char(p + i);
3584 if (enc_utf8)
3585 /* For Unicode case folding is always the same, no need to use
3586 * the table from the spell file. */
3587 c = utf_fold(c);
3588 else if (c < 256)
3589 /* Use the table from the spell file. */
3590 c = spelltab.st_fold[c];
3591# ifdef HAVE_TOWLOWER
3592 else
3593 /* We don't know what to do, fall back to towlower(), it
3594 * depends on the current locale. */
3595 c = towlower(c);
3596# endif
3597 if (outi + MB_MAXBYTES > buflen)
3598 {
3599 buf[outi] = NUL;
3600 return FAIL;
3601 }
3602 outi += mb_char2bytes(c, buf + outi);
3603 }
3604 buf[outi] = NUL;
3605 }
3606 else
3607#endif
3608 {
3609 /* Be quick for non-multibyte encodings. */
3610 for (i = 0; i < len; ++i)
3611 buf[i] = spelltab.st_fold[p[i]];
3612 buf[i] = NUL;
3613 }
3614
3615 return OK;
3616}
3617
3618
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003619#endif /* FEAT_SYN_HL */