blob: 734c7768e1db8bd4fabdd438e9cd3fb960fa4b97 [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 Moolenaar51485f02005-06-04 21:55:20 +0000154 char_u *sl_fbyts; /* case-folded word bytes */
155 int *sl_fidxs; /* case-folded word indexes */
156 char_u *sl_kbyts; /* keep-case word bytes */
157 int *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000158 char_u *sl_try; /* "TRY" from .aff file TODO: not used */
159 garray_T sl_rep; /* list of repentry_T entries from REP lines
160 * TODO not used */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000161 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000162 int sl_error; /* error while loading */
163};
164
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000165/* First language that is loaded, start of the linked list of loaded
166 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167static slang_T *first_lang = NULL;
168
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169/*
170 * Structure used in "b_langp", filled from 'spelllang'.
171 */
172typedef struct langp_S
173{
174 slang_T *lp_slang; /* info for this language (NULL for last one) */
175 int lp_region; /* bitmask for region or REGION_ALL */
176} langp_T;
177
178#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
179
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000180#define REGION_ALL 0xff /* word valid in all regions */
181
182/* Result values. Lower number is accepted over higher one. */
183#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000184#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000185#define SP_RARE 1
186#define SP_LOCAL 2
187#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000188
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189#define VIMSPELLMAGIC "VIMspell05" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000190#define VIMSPELLMAGICL 10
191
192/*
193 * Structure to store info for word matching.
194 */
195typedef struct matchinf_S
196{
197 langp_T *mi_lp; /* info for language and region */
198 slang_T *mi_slang; /* info for the language */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000199
200 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000201 char_u *mi_word; /* start of word being checked */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000202 char_u *mi_end; /* end of matching word */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000203 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000204 char_u *mi_cend; /* char after what was used for
205 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000206
207 /* case-folded text */
208 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000210
211 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000212 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000213 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000214} matchinf_T;
215
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000216/*
217 * The tables used for recognizing word characters according to spelling.
218 * These are only used for the first 256 characters of 'encoding'.
219 */
220typedef struct spelltab_S
221{
222 char_u st_isw[256]; /* flags: is word char */
223 char_u st_isu[256]; /* flags: is uppercase char */
224 char_u st_fold[256]; /* chars: folded case */
225} spelltab_T;
226
227static spelltab_T spelltab;
228static int did_set_spelltab;
229
230#define SPELL_ISWORD 1
231#define SPELL_ISUPPER 2
232
233static void clear_spell_chartab __ARGS((spelltab_T *sp));
234static int set_spell_finish __ARGS((spelltab_T *new_st));
235
236/*
237 * Return TRUE if "p" points to a word character or "c" is a word character
238 * for spelling.
239 * Checking for a word character is done very often, avoid the function call
240 * overhead.
241 */
242#ifdef FEAT_MBYTE
243# define SPELL_ISWORDP(p) ((has_mbyte && MB_BYTE2LEN(*(p)) > 1) \
244 ? (mb_get_class(p) >= 2) : spelltab.st_isw[*(p)])
245#else
246# define SPELL_ISWORDP(p) (spelltab.st_isw[*(p)])
247#endif
248
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000249static slang_T *slang_alloc __ARGS((char_u *lang));
250static void slang_free __ARGS((slang_T *lp));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251static void find_word __ARGS((matchinf_T *mip, int keepcap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000252static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000253static void spell_load_file __ARGS((char_u *fname, void *cookie));
Bram Moolenaar51485f02005-06-04 21:55:20 +0000254static int read_tree __ARGS((FILE *fd, char_u *byts, int *idxs, int maxidx, int startidx));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000255static int find_region __ARGS((char_u *rp, char_u *region));
256static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000257static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
258#ifdef FEAT_MBYTE
259static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
260static void write_spell_chartab __ARGS((FILE *fd));
261#endif
262static int spell_isupper __ARGS((int c));
263static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
264
265static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000266
267/*
268 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000269 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000270 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
271 * or when it's OK it remains unchanged.
272 * This must only be called when 'spelllang' is not empty.
273 * Returns the length of the word in bytes, also when it's OK, so that the
274 * caller can skip over the word.
275 */
276 int
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277spell_check(wp, ptr, attrp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000278 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000279 char_u *ptr;
280 int *attrp;
281{
282 matchinf_T mi; /* Most things are put in "mi" so that it can
283 be passed to functions quickly. */
284
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000285 /* A word never starts at a space or a control character. Return quickly
286 * then, skipping over the character. */
287 if (*ptr <= ' ')
288 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000289
Bram Moolenaar51485f02005-06-04 21:55:20 +0000290 /* A word starting with a number is always OK. Also skip hexadecimal
291 * numbers 0xFF99 and 0X99FF. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000292 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000293 {
294 if (*ptr == '0' && (ptr[1] == 'x' || ptr[2] == 'X'))
295 mi.mi_end = skiphex(ptr);
296 else
297 mi.mi_end = skipdigits(ptr);
298 }
299 else
300 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000301 /* Find the end of the word. */
302 mi.mi_word = ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000303 mi.mi_fend = ptr;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000304 if (SPELL_ISWORDP(mi.mi_fend))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000305 {
306 /* Make case-folded copy of the characters until the next non-word
307 * character. */
308 do
309 {
310 mb_ptr_adv(mi.mi_fend);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000311 } while (*mi.mi_fend != NUL && SPELL_ISWORDP(mi.mi_fend));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000312
Bram Moolenaar51485f02005-06-04 21:55:20 +0000313 /* Check the caps type of the word. */
314 mi.mi_capflags = captype(ptr, mi.mi_fend);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000315 }
316 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000317 /* No word characters, caps type is always zero. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000318 mi.mi_capflags = 0;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000319
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000320 /* We always use the characters up to the next non-word character,
321 * also for bad words. */
322 mi.mi_end = mi.mi_fend;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000323 mi.mi_cend = mi.mi_fend;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000324
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000325 /* Include one non-word character so that we can check for the
326 * word end. */
327 if (*mi.mi_fend != NUL)
328 mb_ptr_adv(mi.mi_fend);
329
330 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
331 MAXWLEN + 1);
332 mi.mi_fwordlen = STRLEN(mi.mi_fword);
333
Bram Moolenaar51485f02005-06-04 21:55:20 +0000334 /* The word is bad unless we recognize it. */
335 mi.mi_result = SP_BAD;
336
337 /*
338 * Loop over the languages specified in 'spelllang'.
339 * We check them all, because a matching word may be longer than an
340 * already found matching word.
341 */
342 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000343 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000344 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000345 /* Check for a matching word in case-folded words. */
346 find_word(&mi, FALSE);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000347
Bram Moolenaar51485f02005-06-04 21:55:20 +0000348 /* Try keep-case words. */
349 find_word(&mi, TRUE);
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000350 }
351
Bram Moolenaar51485f02005-06-04 21:55:20 +0000352 if (mi.mi_result != SP_OK)
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000353 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000354 /* When we are at a non-word character there is no error, just
355 * skip over the character (try looking for a word after it). */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000356 if (!SPELL_ISWORDP(ptr))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357 {
358#ifdef FEAT_MBYTE
359 if (has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000360 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000361#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000362 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000363 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000364
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000365 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000366 *attrp = highlight_attr[HLF_SPB];
367 else if (mi.mi_result == SP_RARE)
368 *attrp = highlight_attr[HLF_SPR];
369 else
370 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000371 }
372 }
373
Bram Moolenaar51485f02005-06-04 21:55:20 +0000374 return (int)(mi.mi_end - ptr);
375}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000376
Bram Moolenaar51485f02005-06-04 21:55:20 +0000377/*
378 * Check if the word at "mip->mi_word" is in the tree.
379 * When "keepcap" is TRUE check in keep-case word tree.
380 *
381 * For a match mip->mi_result is updated.
382 */
383 static void
384find_word(mip, keepcap)
385 matchinf_T *mip;
386 int keepcap;
387{
388 int arridx = 0;
389 int endlen[MAXWLEN]; /* length at possible word endings */
390 int endidx[MAXWLEN]; /* possible word endings */
391 int endidxcnt = 0;
392 int len;
393 int wlen = 0;
394 int flen;
395 int c;
396 char_u *ptr;
397 unsigned lo, hi, m;
398#ifdef FEAT_MBYTE
399 char_u *s;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000400#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000401 char_u *p;
402 int res = SP_BAD;
403 int valid;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000404 slang_T *slang = mip->mi_lp->lp_slang;
405 unsigned flags;
406 char_u *byts;
407 int *idxs;
408
409 if (keepcap)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000411 /* Check for word with matching case in keep-case tree. */
412 ptr = mip->mi_word;
413 flen = 9999; /* no case folding, always enough bytes */
414 byts = slang->sl_kbyts;
415 idxs = slang->sl_kidxs;
416 }
417 else
418 {
419 /* Check for case-folded in case-folded tree. */
420 ptr = mip->mi_fword;
421 flen = mip->mi_fwordlen; /* available case-folded bytes */
422 byts = slang->sl_fbyts;
423 idxs = slang->sl_fidxs;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000424 }
425
Bram Moolenaar51485f02005-06-04 21:55:20 +0000426 if (byts == NULL)
427 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000428
Bram Moolenaar51485f02005-06-04 21:55:20 +0000429 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000430 * Repeat advancing in the tree until:
431 * - there is a byte that doesn't match,
432 * - we reach the end of the tree,
433 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000434 */
435 for (;;)
436 {
437 if (flen == 0 && *mip->mi_fend != NUL)
438 {
439 /* Need to fold at least one more character. Do until next
440 * non-word character for efficiency. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000441 p = mip->mi_fend;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000442 do
443 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000444 mb_ptr_adv(mip->mi_fend);
445 } while (*mip->mi_fend != NUL && SPELL_ISWORDP(mip->mi_fend));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000446
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000447 /* Include the non-word character so that we can check for the
448 * word end. */
449 if (*mip->mi_fend != NUL)
450 mb_ptr_adv(mip->mi_fend);
451
452 (void)spell_casefold(p, (int)(mip->mi_fend - p),
Bram Moolenaar51485f02005-06-04 21:55:20 +0000453 mip->mi_fword + mip->mi_fwordlen,
454 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000455 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
456 mip->mi_fwordlen += flen;
457 }
458
459 len = byts[arridx++];
460
461 /* If the first possible byte is a zero the word could end here.
462 * Remember this index, we first check for the longest word. */
463 if (byts[arridx] == 0)
464 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000465 if (endidxcnt == MAXWLEN)
466 {
467 /* Must be a corrupted spell file. */
468 EMSG(_(e_format));
469 return;
470 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000471 endlen[endidxcnt] = wlen;
472 endidx[endidxcnt++] = arridx++;
473 --len;
474
475 /* Skip over the zeros, there can be several flag/region
476 * combinations. */
477 while (len > 0 && byts[arridx] == 0)
478 {
479 ++arridx;
480 --len;
481 }
482 if (len == 0)
483 break; /* no children, word must end here */
484 }
485
486 /* Stop looking at end of the line. */
487 if (ptr[wlen] == NUL)
488 break;
489
490 /* Perform a binary search in the list of accepted bytes. */
491 c = ptr[wlen];
492 lo = arridx;
493 hi = arridx + len - 1;
494 while (lo < hi)
495 {
496 m = (lo + hi) / 2;
497 if (byts[m] > c)
498 hi = m - 1;
499 else if (byts[m] < c)
500 lo = m + 1;
501 else
502 {
503 lo = hi = m;
504 break;
505 }
506 }
507
508 /* Stop if there is no matching byte. */
509 if (hi < lo || byts[lo] != c)
510 break;
511
512 /* Continue at the child (if there is one). */
513 arridx = idxs[lo];
514 ++wlen;
515 --flen;
516 }
517
518 /*
519 * Verify that one of the possible endings is valid. Try the longest
520 * first.
521 */
522 while (endidxcnt > 0)
523 {
524 --endidxcnt;
525 arridx = endidx[endidxcnt];
526 wlen = endlen[endidxcnt];
527
528#ifdef FEAT_MBYTE
529 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
530 continue; /* not at first byte of character */
531#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000532 if (SPELL_ISWORDP(ptr + wlen))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000533 continue; /* next char is a word character */
534
535#ifdef FEAT_MBYTE
536 if (!keepcap && has_mbyte)
537 {
538 /* Compute byte length in original word, length may change
539 * when folding case. */
540 p = mip->mi_word;
541 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
542 mb_ptr_adv(p);
543 wlen = p - mip->mi_word;
544 }
545#endif
546
547 /* Check flags and region. Repeat this if there are more
548 * flags/region alternatives until there is a match. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000549 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0; --len)
550 {
551 flags = idxs[arridx];
552 if (keepcap)
553 {
554 /* For "keepcap" tree the case is always right. */
555 valid = TRUE;
556 }
557 else
558 {
559 /* Check that the word is in the required case. */
560 if (mip->mi_cend != mip->mi_word + wlen)
561 {
562 /* mi_capflags was set for a different word
563 * length, need to do it again. */
564 mip->mi_cend = mip->mi_word + wlen;
565 mip->mi_capflags = captype(mip->mi_word,
566 mip->mi_cend);
567 }
568
569 valid = (mip->mi_capflags == WF_ALLCAP
570 || ((flags & WF_ALLCAP) == 0
571 && ((flags & WF_ONECAP) == 0
572 || mip->mi_capflags == WF_ONECAP)));
573 }
574
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000575 if (valid)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000576 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000577 if (flags & WF_BANNED)
578 res = SP_BANNED;
579 else if (flags & WF_REGION)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000580 {
581 /* Check region. */
582 if ((mip->mi_lp->lp_region & (flags >> 8)) != 0)
583 res = SP_OK;
584 else
585 res = SP_LOCAL;
586 }
587 else if (flags & WF_RARE)
588 res = SP_RARE;
589 else
590 res = SP_OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000591
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000592 /* Always use the longest match and the best result. */
593 if (mip->mi_result > res)
594 {
595 mip->mi_result = res;
596 mip->mi_end = mip->mi_word + wlen;
597 }
598 else if (mip->mi_result == res
599 && mip->mi_end < mip->mi_word + wlen)
600 mip->mi_end = mip->mi_word + wlen;
601
602 if (res == SP_OK)
603 break;
604 }
605 else
606 res = SP_BAD;
607
Bram Moolenaar51485f02005-06-04 21:55:20 +0000608 ++arridx;
609 }
610
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000611 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000612 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000613 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000614}
615
Bram Moolenaar51485f02005-06-04 21:55:20 +0000616
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000617/*
618 * Move to next spell error.
619 * Return OK if found, FAIL otherwise.
620 */
621 int
622spell_move_to(dir, allwords)
623 int dir; /* FORWARD or BACKWARD */
624 int allwords; /* TRUE for "[s" and "]s" */
625{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000626 linenr_T lnum;
627 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000628 char_u *line;
629 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000630 int attr = 0;
631 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000632 int has_syntax = syntax_present(curbuf);
633 int col;
634 int can_spell;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000635
636 if (!curwin->w_p_spell || *curwin->w_buffer->b_p_spl == NUL)
637 {
638 EMSG(_("E756: Spell checking not enabled"));
639 return FAIL;
640 }
641
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000642 /*
643 * Start looking for bad word at the start of the line, because we can't
644 * start halfway a word, we don't know where it starts or ends.
645 *
646 * When searching backwards, we continue in the line to find the last
647 * bad word (in the cursor line: before the cursor).
648 */
649 lnum = curwin->w_cursor.lnum;
650 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000651
652 while (!got_int)
653 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000654 line = ml_get(lnum);
655 p = line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000656
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000657 while (*p != NUL)
658 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000659 /* When searching backward don't search after the cursor. */
660 if (dir == BACKWARD
661 && lnum == curwin->w_cursor.lnum
662 && (colnr_T)(p - line) >= curwin->w_cursor.col)
663 break;
664
665 /* start of word */
666 len = spell_check(curwin, p, &attr);
667
668 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000669 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000670 /* We found a bad word. Check the attribute. */
671 /* TODO: check for syntax @Spell cluster. */
672 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000673 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000674 /* When searching forward only accept a bad word after
675 * the cursor. */
676 if (dir == BACKWARD
677 || lnum > curwin->w_cursor.lnum
678 || (lnum == curwin->w_cursor.lnum
679 && (colnr_T)(p - line)
680 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000681 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000682 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000683 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000684 col = p - line;
685 (void)syn_get_id(lnum, (colnr_T)col,
686 FALSE, &can_spell);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000687
Bram Moolenaar51485f02005-06-04 21:55:20 +0000688 /* have to get the line again, a multi-line
689 * regexp may make it invalid */
690 line = ml_get(lnum);
691 p = line + col;
692 }
693 else
694 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000695
Bram Moolenaar51485f02005-06-04 21:55:20 +0000696 if (can_spell)
697 {
698 found_pos.lnum = lnum;
699 found_pos.col = p - line;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000700#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +0000701 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000702#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703 if (dir == FORWARD)
704 {
705 /* No need to search further. */
706 curwin->w_cursor = found_pos;
707 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000708 }
709 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000710 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000711 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000712 attr = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000713 }
714
Bram Moolenaar51485f02005-06-04 21:55:20 +0000715 /* advance to character after the word */
716 p += len;
717 if (*p == NUL)
718 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000719 }
720
721 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000722 if (dir == BACKWARD)
723 {
724 if (found_pos.lnum != 0)
725 {
726 /* Use the last match in the line. */
727 curwin->w_cursor = found_pos;
728 return OK;
729 }
730 if (lnum == 1)
731 return FAIL;
732 --lnum;
733 }
734 else
735 {
736 if (lnum == curbuf->b_ml.ml_line_count)
737 return FAIL;
738 ++lnum;
739 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000740
741 line_breakcheck();
742 }
743
744 return FAIL; /* interrupted */
745}
746
747/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000748 * Load word list(s) for "lang" from Vim spell file(s).
749 * "lang" must be the language without the region: "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000750 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000751 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000752spell_load_lang(lang)
753 char_u *lang;
754{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000755 char_u fname_enc[80];
756 char_u *p;
757 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000758 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000759
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000760 /* Copy the language name to pass it to spell_load_file() as a cookie.
761 * It's truncated when an error is detected. */
762 STRCPY(langcp, lang);
763
764 /* Find all spell files for "lang" in 'runtimepath' and load them.
765 * Use 'encoding', except that we use "latin1" for "latin9". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766#ifdef FEAT_MBYTE
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000767 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
768 p = p_enc;
769 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000770#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000771 p = (char_u *)"latin1";
772 vim_snprintf((char *)fname_enc, sizeof(fname_enc),
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000773 "spell/%s.%s.spl", lang, p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000774 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000775
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000776 if (r == FAIL && *langcp != NUL)
777 {
778 /* Try loading the ASCII version. */
779 vim_snprintf((char *)fname_enc, sizeof(fname_enc),
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000780 "spell/%s.ascii.spl", lang);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000781 r = do_in_runtimepath(fname_enc, TRUE, spell_load_file, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000782 }
783
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000784 if (r == FAIL)
785 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
786 fname_enc + 6);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000787}
788
789/*
790 * Allocate a new slang_T.
791 * Caller must fill "sl_next".
792 */
793 static slang_T *
794slang_alloc(lang)
795 char_u *lang;
796{
797 slang_T *lp;
798
Bram Moolenaar51485f02005-06-04 21:55:20 +0000799 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000800 if (lp != NULL)
801 {
802 lp->sl_name = vim_strsave(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000803 ga_init2(&lp->sl_rep, sizeof(repentry_T), 4);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000804 }
805 return lp;
806}
807
808/*
809 * Free the contents of an slang_T and the structure itself.
810 */
811 static void
812slang_free(lp)
813 slang_T *lp;
814{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000815 vim_free(lp->sl_name);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000816 vim_free(lp->sl_fbyts);
817 vim_free(lp->sl_kbyts);
818 vim_free(lp->sl_fidxs);
819 vim_free(lp->sl_kidxs);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000820 ga_clear(&lp->sl_rep);
821 vim_free(lp->sl_try);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000822 vim_free(lp);
823}
824
825/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000826 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000827 * Invoked through do_in_runtimepath().
828 */
829 static void
830spell_load_file(fname, cookie)
831 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000832 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000833{
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000834 char_u *lang = cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000835 FILE *fd;
836 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000837 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000838 int i;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000839 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840 int round;
841 char_u *save_sourcing_name = sourcing_name;
842 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000843 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000844 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000845 slang_T *lp = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000846
847 fd = fopen((char *)fname, "r");
848 if (fd == NULL)
849 {
850 EMSG2(_(e_notopen), fname);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000851 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000852 }
853
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000854 lp = slang_alloc(lang);
855 if (lp == NULL)
856 goto endFAIL;
857
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000858 /* Set sourcing_name, so that error messages mention the file name. */
859 sourcing_name = fname;
860 sourcing_lnum = 0;
861
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000862 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
863 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000864 for (i = 0; i < VIMSPELLMAGICL; ++i)
865 buf[i] = getc(fd); /* <fileID> */
866 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
867 {
868 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000869 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000870 }
871
872 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000873 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000874 {
875truncerr:
876 EMSG(_("E758: Truncated spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000877 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000878 }
879 if (cnt > 8)
880 {
881formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000882 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000883 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000884 }
885 for (i = 0; i < cnt; ++i)
886 {
887 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
888 lp->sl_regions[i * 2 + 1] = getc(fd);
889 }
890 lp->sl_regions[cnt * 2] = NUL;
891
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000892 cnt = getc(fd); /* <charflagslen> */
893 if (cnt > 0)
894 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000895 p = alloc((unsigned)cnt);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000896 if (p == NULL)
897 goto endFAIL;
898 for (i = 0; i < cnt; ++i)
899 p[i] = getc(fd); /* <charflags> */
900
901 ccnt = (getc(fd) << 8) + getc(fd); /* <fcharslen> */
902 if (ccnt <= 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000903 {
904 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000905 goto formerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000906 }
907 fol = alloc((unsigned)ccnt + 1);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000908 if (fol == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909 {
910 vim_free(p);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000911 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000912 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000913 for (i = 0; i < ccnt; ++i)
914 fol[i] = getc(fd); /* <fchars> */
915 fol[i] = NUL;
916
917 /* Set the word-char flags and fill spell_isupper() table. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000918 i = set_spell_charflags(p, cnt, fol);
919 vim_free(p);
920 vim_free(fol);
921 if (i == FAIL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000922 goto formerr;
923 }
924 else
925 {
926 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
927 cnt = (getc(fd) << 8) + getc(fd);
928 if (cnt != 0)
929 goto formerr;
930 }
931
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000932 /* <SUGGEST> : <suggestlen> <more> ... */
933 /* TODO, just skip this for now */
934 i = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
935 while (i-- > 0)
936 if (getc(fd) == EOF) /* <suggestlen> */
937 goto truncerr;
938
Bram Moolenaar51485f02005-06-04 21:55:20 +0000939 /* round 1: <LWORDTREE>
940 * round 2: <KWORDTREE> */
941 for (round = 1; round <= 2; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000942 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000943 /* The tree size was computed when writing the file, so that we can
944 * allocate it as one long block. <nodecount> */
945 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
946 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000947 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000948 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000949 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000950 /* Allocate the byte array. */
951 p = lalloc((long_u)len, TRUE);
952 if (p == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000953 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000954 if (round == 1)
955 lp->sl_fbyts = p;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +0000956 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000957 lp->sl_kbyts = p;
958
959 /* Allocate the index array. */
960 p = lalloc_clear((long_u)(len * sizeof(int)), TRUE);
961 if (p == NULL)
962 goto endFAIL;
963 if (round == 1)
964 lp->sl_fidxs = (int *)p;
965 else
966 lp->sl_kidxs = (int *)p;
967
968
969 /* Read the tree and store it in the array. */
970 i = read_tree(fd,
971 round == 1 ? lp->sl_fbyts : lp->sl_kbyts,
972 round == 1 ? lp->sl_fidxs : lp->sl_kidxs,
973 len, 0);
974 if (i == -1)
975 goto truncerr;
976 if (i < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000977 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000978 }
979 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000980
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000981 lp->sl_next = first_lang;
982 first_lang = lp;
983
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000984 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000985
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000986endFAIL:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000987 /* truncating the name signals the error to spell_load_lang() */
988 *lang = NUL;
989 if (lp != NULL)
990 slang_free(lp);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +0000991
992endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000993 if (fd != NULL)
994 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995 sourcing_name = save_sourcing_name;
996 sourcing_lnum = save_sourcing_lnum;
997}
998
999/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001000 * Read one row of siblings from the spell file and store it in the byte array
1001 * "byts" and index array "idxs". Recursively read the children.
1002 *
1003 * NOTE: The code here must match put_tree().
1004 *
1005 * Returns the index follosing the siblings.
1006 * Returns -1 if the file is shorter than expected.
1007 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001009 static int
1010read_tree(fd, byts, idxs, maxidx, startidx)
1011 FILE *fd;
1012 char_u *byts;
1013 int *idxs;
1014 int maxidx; /* size of arrays */
1015 int startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001016{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001017 int len;
1018 int i;
1019 int n;
1020 int idx = startidx;
1021 int c;
1022#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001023
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024 len = getc(fd); /* <siblingcount> */
1025 if (len <= 0)
1026 return -1;
1027
1028 if (startidx + len >= maxidx)
1029 return -2;
1030 byts[idx++] = len;
1031
1032 /* Read the byte values, flag/region bytes and shared indexes. */
1033 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001034 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001035 c = getc(fd); /* <byte> */
1036 if (c < 0)
1037 return -1;
1038 if (c <= BY_SPECIAL)
1039 {
1040 if (c == BY_NOFLAGS)
1041 {
1042 /* No flags, all regions. */
1043 idxs[idx] = 0;
1044 c = 0;
1045 }
1046 else if (c == BY_FLAGS)
1047 {
1048 /* Read flags and option region. */
1049 c = getc(fd); /* <flags> */
1050 if (c & WF_REGION)
1051 c = (getc(fd) << 8) + c; /* <region> */
1052 idxs[idx] = c;
1053 c = 0;
1054 }
1055 else /* c == BY_INDEX */
1056 {
1057 /* <nodeidx> */
1058 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
1059 if (n < 0 || n >= maxidx)
1060 return -2;
1061 idxs[idx] = n + SHARED_MASK;
1062 c = getc(fd); /* <xbyte> */
1063 }
1064 }
1065 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001066 }
1067
Bram Moolenaar51485f02005-06-04 21:55:20 +00001068 /* Recursively read the children for non-shared siblings.
1069 * Skip the end-of-word ones (zero byte value) and the shared ones (and
1070 * remove SHARED_MASK) */
1071 for (i = 1; i <= len; ++i)
1072 if (byts[startidx + i] != 0)
1073 {
1074 if (idxs[startidx + i] & SHARED_MASK)
1075 idxs[startidx + i] &= ~SHARED_MASK;
1076 else
1077 {
1078 idxs[startidx + i] = idx;
1079 idx = read_tree(fd, byts, idxs, maxidx, idx);
1080 if (idx < 0)
1081 break;
1082 }
1083 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001084
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001086}
1087
1088/*
1089 * Parse 'spelllang' and set buf->b_langp accordingly.
1090 * Returns an error message or NULL.
1091 */
1092 char_u *
1093did_set_spelllang(buf)
1094 buf_T *buf;
1095{
1096 garray_T ga;
1097 char_u *lang;
1098 char_u *e;
1099 char_u *region;
1100 int region_mask;
1101 slang_T *lp;
1102 int c;
1103 char_u lbuf[MAXWLEN + 1];
1104
1105 ga_init2(&ga, sizeof(langp_T), 2);
1106
1107 /* loop over comma separated languages. */
1108 for (lang = buf->b_p_spl; *lang != NUL; lang = e)
1109 {
1110 e = vim_strchr(lang, ',');
1111 if (e == NULL)
1112 e = lang + STRLEN(lang);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001113 region = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001114 if (e > lang + 2)
1115 {
1116 if (e - lang >= MAXWLEN)
1117 {
1118 ga_clear(&ga);
1119 return e_invarg;
1120 }
1121 if (lang[2] == '_')
1122 region = lang + 3;
1123 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001124
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001125 /* Check if we loaded this language before. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001126 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1127 if (STRNICMP(lp->sl_name, lang, 2) == 0)
1128 break;
1129
1130 if (lp == NULL)
1131 {
1132 /* Not found, load the language. */
1133 STRNCPY(lbuf, lang, e - lang);
1134 lbuf[e - lang] = NUL;
1135 if (region != NULL)
1136 mch_memmove(lbuf + 2, lbuf + 5, e - lang - 4);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001137 spell_load_lang(lbuf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001138 }
1139
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001140 /*
1141 * Loop over the languages, there can be several files for each.
1142 */
1143 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
1144 if (STRNICMP(lp->sl_name, lang, 2) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001145 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001146 if (region == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001147 region_mask = REGION_ALL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001148 else
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001149 {
1150 /* find region in sl_regions */
1151 c = find_region(lp->sl_regions, region);
1152 if (c == REGION_ALL)
1153 {
1154 c = *e;
1155 *e = NUL;
1156 smsg((char_u *)_("Warning: region %s not supported"),
1157 lang);
1158 *e = c;
1159 region_mask = REGION_ALL;
1160 }
1161 else
1162 region_mask = 1 << c;
1163 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001165 if (ga_grow(&ga, 1) == FAIL)
1166 {
1167 ga_clear(&ga);
1168 return e_outofmem;
1169 }
1170 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
1171 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
1172 ++ga.ga_len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001173 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001174
1175 if (*e == ',')
1176 ++e;
1177 }
1178
1179 /* Add a NULL entry to mark the end of the list. */
1180 if (ga_grow(&ga, 1) == FAIL)
1181 {
1182 ga_clear(&ga);
1183 return e_outofmem;
1184 }
1185 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
1186 ++ga.ga_len;
1187
1188 /* Everything is fine, store the new b_langp value. */
1189 ga_clear(&buf->b_langp);
1190 buf->b_langp = ga;
1191
1192 return NULL;
1193}
1194
1195/*
1196 * Find the region "region[2]" in "rp" (points to "sl_regions").
1197 * Each region is simply stored as the two characters of it's name.
1198 * Returns the index if found, REGION_ALL if not found.
1199 */
1200 static int
1201find_region(rp, region)
1202 char_u *rp;
1203 char_u *region;
1204{
1205 int i;
1206
1207 for (i = 0; ; i += 2)
1208 {
1209 if (rp[i] == NUL)
1210 return REGION_ALL;
1211 if (rp[i] == region[0] && rp[i + 1] == region[1])
1212 break;
1213 }
1214 return i / 2;
1215}
1216
1217/*
1218 * Return type of word:
1219 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220 * Word WF_ONECAP
1221 * W WORD WF_ALLCAP
1222 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001223 */
1224 static int
1225captype(word, end)
1226 char_u *word;
1227 char_u *end;
1228{
1229 char_u *p;
1230 int c;
1231 int firstcap;
1232 int allcap;
1233 int past_second = FALSE; /* past second word char */
1234
1235 /* find first letter */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001236 for (p = word; !SPELL_ISWORDP(p); mb_ptr_adv(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001237 if (p >= end)
1238 return 0; /* only non-word characters, illegal word */
1239#ifdef FEAT_MBYTE
1240 c = mb_ptr2char_adv(&p);
1241#else
1242 c = *p++;
1243#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001244 firstcap = allcap = spell_isupper(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001245
1246 /*
1247 * Need to check all letters to find a word with mixed upper/lower.
1248 * But a word with an upper char only at start is a ONECAP.
1249 */
1250 for ( ; p < end; mb_ptr_adv(p))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001251 if (SPELL_ISWORDP(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001252 {
1253#ifdef FEAT_MBYTE
1254 c = mb_ptr2char(p);
1255#else
1256 c = *p;
1257#endif
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001258 if (!spell_isupper(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001259 {
1260 /* UUl -> KEEPCAP */
1261 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001262 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001263 allcap = FALSE;
1264 }
1265 else if (!allcap)
1266 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001267 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001268 past_second = TRUE;
1269 }
1270
1271 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001272 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001273 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001274 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001275 return 0;
1276}
1277
1278# if defined(FEAT_MBYTE) || defined(PROTO)
1279/*
1280 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001281 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001282 */
1283 void
1284spell_reload()
1285{
1286 buf_T *buf;
1287 slang_T *lp;
1288
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001289 /* Initialize the table for SPELL_ISWORDP(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290 init_spell_chartab();
1291
1292 /* Unload all allocated memory. */
1293 while (first_lang != NULL)
1294 {
1295 lp = first_lang;
1296 first_lang = lp->sl_next;
1297 slang_free(lp);
1298 }
1299
1300 /* Go through all buffers and handle 'spelllang'. */
1301 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1302 {
1303 ga_clear(&buf->b_langp);
1304 if (*buf->b_p_spl != NUL)
1305 did_set_spelllang(buf);
1306 }
1307}
1308# endif
1309
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001310
1311#if defined(FEAT_MBYTE) || defined(PROTO)
1312/*
1313 * Functions for ":mkspell".
1314 * Only possible with the multi-byte feature.
1315 */
1316
Bram Moolenaar51485f02005-06-04 21:55:20 +00001317#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001318 and .dic file. */
1319/*
1320 * Main structure to store the contents of a ".aff" file.
1321 */
1322typedef struct afffile_S
1323{
1324 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
1325 char_u *af_try; /* "TRY" line in "af_enc" encoding */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001326 int af_rar; /* ID for rare word */
1327 int af_huh; /* ID for keep-case word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001328 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
1329 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
1330 garray_T af_rep; /* list of repentry_T entries from REP lines */
1331} afffile_T;
1332
1333typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001334/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
1335struct affentry_S
1336{
1337 affentry_T *ae_next; /* next affix with same name/number */
1338 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
1339 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001340 char_u *ae_cond; /* condition (NULL for ".") */
1341 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001342};
1343
1344/* Affix header from ".aff" file. Used for af_pref and af_suff. */
1345typedef struct affheader_S
1346{
1347 char_u ah_key[2]; /* key for hashtable == name of affix entry */
1348 int ah_combine; /* suffix may combine with prefix */
1349 affentry_T *ah_first; /* first affix entry */
1350} affheader_T;
1351
1352#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1353
1354/*
1355 * Structure that is used to store the items in the word tree. This avoids
1356 * the need to keep track of each allocated thing, it's freed all at once
1357 * after ":mkspell" is done.
1358 */
1359#define SBLOCKSIZE 16000 /* size of sb_data */
1360typedef struct sblock_S sblock_T;
1361struct sblock_S
1362{
1363 sblock_T *sb_next; /* next block in list */
1364 int sb_used; /* nr of bytes already in use */
1365 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001366};
1367
1368/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001369 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001370 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001371typedef struct wordnode_S wordnode_T;
1372struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001373{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001374 char_u wn_hashkey[6]; /* room for the hash key */
1375 wordnode_T *wn_next; /* next node with same hash key */
1376 wordnode_T *wn_child; /* child (next byte in word) */
1377 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
1378 always sorted) */
1379 wordnode_T *wn_wnode; /* parent node that will write this node */
1380 int wn_index; /* index in written nodes (valid after first
1381 round) */
1382 char_u wn_byte; /* Byte for this node. NUL for word end */
1383 char_u wn_flags; /* when wn_byte is NUL: WF_ flags */
1384 char_u wn_region; /* when wn_byte is NUL: region mask */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001385};
1386
Bram Moolenaar51485f02005-06-04 21:55:20 +00001387#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001388
Bram Moolenaar51485f02005-06-04 21:55:20 +00001389/*
1390 * Info used while reading the spell files.
1391 */
1392typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001393{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001394 wordnode_T *si_foldroot; /* tree with case-folded words */
1395 wordnode_T *si_keeproot; /* tree with keep-case words */
1396 sblock_T *si_blocks; /* memory blocks used */
1397 int si_ascii; /* handling only ASCII words */
1398 int si_region; /* region mask */
1399 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00001400 int si_memtot; /* runtime memory used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001401} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001402
Bram Moolenaar51485f02005-06-04 21:55:20 +00001403static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar5482f332005-04-17 20:18:43 +00001404static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001405static void spell_free_aff __ARGS((afffile_T *aff));
1406static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001407static 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 +00001408static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
1409static void *getroom __ARGS((sblock_T **blp, size_t len));
1410static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
1411static void free_blocks __ARGS((sblock_T *bl));
1412static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001413static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags));
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, sblock_T **blp));
1415static void wordtree_compress __ARGS((wordnode_T *root));
1416static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
1417static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
1418static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin, int regcount, char_u *regchars));
1419static int put_tree __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001420
1421/*
1422 * Read an affix ".aff" file.
1423 * Returns an afffile_T, NULL for failure.
1424 */
1425 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00001426spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001427 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001428 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001429{
1430 FILE *fd;
1431 afffile_T *aff;
1432 char_u rline[MAXLINELEN];
1433 char_u *line;
1434 char_u *pc = NULL;
1435 char_u *(items[6]);
1436 int itemcnt;
1437 char_u *p;
1438 int lnum = 0;
1439 affheader_T *cur_aff = NULL;
1440 int aff_todo = 0;
1441 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001442 char_u *low = NULL;
1443 char_u *fol = NULL;
1444 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001445 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001446
Bram Moolenaar51485f02005-06-04 21:55:20 +00001447 /*
1448 * Open the file.
1449 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001450 fd = fopen((char *)fname, "r");
1451 if (fd == NULL)
1452 {
1453 EMSG2(_(e_notopen), fname);
1454 return NULL;
1455 }
1456
1457 smsg((char_u *)_("Reading affix file %s..."), fname);
1458 out_flush();
1459
Bram Moolenaar51485f02005-06-04 21:55:20 +00001460 /*
1461 * Allocate and init the afffile_T structure.
1462 */
1463 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001464 if (aff == NULL)
1465 return NULL;
1466 hash_init(&aff->af_pref);
1467 hash_init(&aff->af_suff);
1468 ga_init2(&aff->af_rep, (int)sizeof(repentry_T), 20);
1469
1470 /*
1471 * Read all the lines in the file one by one.
1472 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001473 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001474 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001475 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 ++lnum;
1477
1478 /* Skip comment lines. */
1479 if (*rline == '#')
1480 continue;
1481
1482 /* Convert from "SET" to 'encoding' when needed. */
1483 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001484 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001485 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001486 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001487 if (pc == NULL)
1488 {
1489 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
1490 fname, lnum, rline);
1491 continue;
1492 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001493 line = pc;
1494 }
1495 else
1496 {
1497 pc = NULL;
1498 line = rline;
1499 }
1500
1501 /* Split the line up in white separated items. Put a NUL after each
1502 * item. */
1503 itemcnt = 0;
1504 for (p = line; ; )
1505 {
1506 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
1507 ++p;
1508 if (*p == NUL)
1509 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001510 if (itemcnt == 6) /* too many items */
1511 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001512 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001513 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001514 ++p;
1515 if (*p == NUL)
1516 break;
1517 *p++ = NUL;
1518 }
1519
1520 /* Handle non-empty lines. */
1521 if (itemcnt > 0)
1522 {
1523 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
1524 && aff->af_enc == NULL)
1525 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001526 /* Setup for conversion from "ENC" to 'encoding'. */
1527 aff->af_enc = enc_canonize(items[1]);
1528 if (aff->af_enc != NULL && !spin->si_ascii
1529 && convert_setup(&spin->si_conv, aff->af_enc,
1530 p_enc) == FAIL)
1531 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
1532 fname, aff->af_enc, p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001533 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00001534 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
1535 {
1536 /* ignored */
1537 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001538 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2
1539 && aff->af_try == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001540 {
1541 aff->af_try = getroom_save(&spin->si_blocks, items[1]);
1542 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001543 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
1544 && aff->af_rar == 0)
1545 {
1546 aff->af_rar = items[1][0];
1547 if (items[1][1] != NUL)
1548 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
1549 }
1550 else if (STRCMP(items[0], "HUH") == 0 && itemcnt == 2
1551 && aff->af_huh == 0)
1552 {
1553 aff->af_huh = items[1][0];
1554 if (items[1][1] != NUL)
1555 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
1556 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001557 else if ((STRCMP(items[0], "PFX") == 0
1558 || STRCMP(items[0], "SFX") == 0)
1559 && aff_todo == 0
1560 && itemcnt == 4)
1561 {
1562 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001563 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
1564 sizeof(affheader_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001565 if (cur_aff == NULL)
1566 break;
1567 cur_aff->ah_key[0] = *items[1];
1568 cur_aff->ah_key[1] = NUL;
1569 if (items[1][1] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001570 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001571 if (*items[2] == 'Y')
1572 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001573 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001574 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
1575 fname, lnum, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 if (*items[0] == 'P')
1577 tp = &aff->af_pref;
1578 else
1579 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001580 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001581 if (!HASHITEM_EMPTY(hash_find(tp, cur_aff->ah_key)))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001582 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001583 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
1584 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001585 aff_todo = 0;
1586 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001587 else
1588 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001589 }
1590 else if ((STRCMP(items[0], "PFX") == 0
1591 || STRCMP(items[0], "SFX") == 0)
1592 && aff_todo > 0
1593 && STRCMP(cur_aff->ah_key, items[1]) == 0
1594 && itemcnt == 5)
1595 {
1596 affentry_T *aff_entry;
1597
1598 /* New item for an affix letter. */
1599 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001600 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
1601 sizeof(affentry_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001602 if (aff_entry == NULL)
1603 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001604
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001605 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001606 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
1607 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001608 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001609 aff_entry->ae_add = getroom_save(&spin->si_blocks,
1610 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001611
Bram Moolenaar51485f02005-06-04 21:55:20 +00001612 /* Don't use an affix entry with non-ASCII characters when
1613 * "spin->si_ascii" is TRUE. */
1614 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00001615 || has_non_ascii(aff_entry->ae_add)))
1616 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00001617 aff_entry->ae_next = cur_aff->ah_first;
1618 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001619
1620 if (STRCMP(items[4], ".") != 0)
1621 {
1622 char_u buf[MAXLINELEN];
1623
1624 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
1625 items[4]);
1626 if (*items[0] == 'P')
1627 sprintf((char *)buf, "^%s", items[4]);
1628 else
1629 sprintf((char *)buf, "%s$", items[4]);
1630 aff_entry->ae_prog = vim_regcomp(buf,
1631 RE_MAGIC + RE_STRING);
1632 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00001633 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001634 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001635 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
1636 {
1637 if (fol != NULL)
1638 smsg((char_u *)_("Duplicate FOL in %s line %d"),
1639 fname, lnum);
1640 else
1641 fol = vim_strsave(items[1]);
1642 }
1643 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
1644 {
1645 if (low != NULL)
1646 smsg((char_u *)_("Duplicate LOW in %s line %d"),
1647 fname, lnum);
1648 else
1649 low = vim_strsave(items[1]);
1650 }
1651 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
1652 {
1653 if (upp != NULL)
1654 smsg((char_u *)_("Duplicate UPP in %s line %d"),
1655 fname, lnum);
1656 else
1657 upp = vim_strsave(items[1]);
1658 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001659 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
1660 /* Ignore REP count */;
1661 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
1662 {
1663 repentry_T *rp;
1664
1665 /* REP item */
1666 if (ga_grow(&aff->af_rep, 1) == FAIL)
1667 break;
1668 rp = ((repentry_T *)aff->af_rep.ga_data) + aff->af_rep.ga_len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001669 rp->re_from = getroom_save(&spin->si_blocks, items[1]);
1670 rp->re_to = getroom_save(&spin->si_blocks, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 ++aff->af_rep.ga_len;
1672 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001673 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001674 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
1675 fname, lnum, items[0]);
1676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001677 }
1678
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001679 if (fol != NULL || low != NULL || upp != NULL)
1680 {
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00001681 /* Don't write a word table for an ASCII file, so that we don't check
1682 * for conflicts with a word table that matches 'encoding'. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001683 if (!spin->si_ascii)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00001684 {
1685 if (fol == NULL || low == NULL || upp == NULL)
1686 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
1687 else
1688 set_spell_chartab(fol, low, upp);
1689 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001690
1691 vim_free(fol);
1692 vim_free(low);
1693 vim_free(upp);
1694 }
1695
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001696 vim_free(pc);
1697 fclose(fd);
1698 return aff;
1699}
1700
1701/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00001702 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
1703 * When "s" is NULL FALSE is returned.
1704 */
1705 static int
1706has_non_ascii(s)
1707 char_u *s;
1708{
1709 char_u *p;
1710
1711 if (s != NULL)
1712 for (p = s; *p != NUL; ++p)
1713 if (*p >= 128)
1714 return TRUE;
1715 return FALSE;
1716}
1717
1718/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001719 * Free the structure filled by spell_read_aff().
1720 */
1721 static void
1722spell_free_aff(aff)
1723 afffile_T *aff;
1724{
1725 hashtab_T *ht;
1726 hashitem_T *hi;
1727 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001728 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001729 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001730
1731 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001732
Bram Moolenaar51485f02005-06-04 21:55:20 +00001733 /* All this trouble to foree the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001734 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
1735 {
1736 todo = ht->ht_used;
1737 for (hi = ht->ht_array; todo > 0; ++hi)
1738 {
1739 if (!HASHITEM_EMPTY(hi))
1740 {
1741 --todo;
1742 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001743 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
1744 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001745 }
1746 }
1747 if (ht == &aff->af_suff)
1748 break;
1749 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001750
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001751 hash_clear(&aff->af_pref);
1752 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001753 ga_clear(&aff->af_rep);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001754}
1755
1756/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001757 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001758 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001759 */
1760 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00001761spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001762 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001763 spellinfo_T *spin;
1764 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001765{
Bram Moolenaar51485f02005-06-04 21:55:20 +00001766 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00001768 char_u *afflist;
1769 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770 char_u *pc;
1771 char_u *w;
1772 int l;
1773 hash_T hash;
1774 hashitem_T *hi;
1775 FILE *fd;
1776 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001777 int non_ascii = 0;
1778 int retval = OK;
1779 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001780 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781
Bram Moolenaar51485f02005-06-04 21:55:20 +00001782 /*
1783 * Open the file.
1784 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001785 fd = fopen((char *)fname, "r");
1786 if (fd == NULL)
1787 {
1788 EMSG2(_(e_notopen), fname);
1789 return FAIL;
1790 }
1791
Bram Moolenaar51485f02005-06-04 21:55:20 +00001792 /* The hashtable is only used to detect duplicated words. */
1793 hash_init(&ht);
1794
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001795 smsg((char_u *)_("Reading dictionary file %s..."), fname);
1796 out_flush();
1797
1798 /* Read and ignore the first line: word count. */
1799 (void)vim_fgets(line, MAXLINELEN, fd);
1800 if (!isdigit(*skipwhite(line)))
1801 EMSG2(_("E760: No word count in %s"), fname);
1802
1803 /*
1804 * Read all the lines in the file one by one.
1805 * The words are converted to 'encoding' here, before being added to
1806 * the hashtable.
1807 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001808 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001809 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001810 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811 ++lnum;
1812
Bram Moolenaar51485f02005-06-04 21:55:20 +00001813 /* Remove CR, LF and white space from the end. White space halfway
1814 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001815 l = STRLEN(line);
1816 while (l > 0 && line[l - 1] <= ' ')
1817 --l;
1818 if (l == 0)
1819 continue; /* empty line */
1820 line[l] = NUL;
1821
Bram Moolenaar51485f02005-06-04 21:55:20 +00001822 /* This takes time, print a message now and then. */
1823 if ((lnum & 0x3ff) == 0)
1824 {
1825 vim_snprintf((char *)message, sizeof(message),
1826 _("line %6d - %s"), lnum, line);
1827 msg_start();
1828 msg_outtrans_attr(message, 0);
1829 msg_clr_eos();
1830 msg_didout = FALSE;
1831 msg_col = 0;
1832 out_flush();
1833 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001834
Bram Moolenaar51485f02005-06-04 21:55:20 +00001835 /* Find the optional affix names. */
1836 afflist = vim_strchr(line, '/');
1837 if (afflist != NULL)
1838 *afflist++ = NUL;
1839
1840 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
1841 if (spin->si_ascii && has_non_ascii(line))
1842 {
1843 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00001844 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001845 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00001846
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001847 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001848 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001849 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001850 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001851 if (pc == NULL)
1852 {
1853 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
1854 fname, lnum, line);
1855 continue;
1856 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857 w = pc;
1858 }
1859 else
1860 {
1861 pc = NULL;
1862 w = line;
1863 }
1864
Bram Moolenaar51485f02005-06-04 21:55:20 +00001865 /* Store the word in the hashtable to be able to find duplicates. */
1866 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001867 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001868 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001869 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001870 if (retval == FAIL)
1871 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001872
Bram Moolenaar51485f02005-06-04 21:55:20 +00001873 hash = hash_hash(dw);
1874 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001875 if (!HASHITEM_EMPTY(hi))
1876 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00001877 fname, lnum, line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001878 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001879 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001880
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001881 flags = 0;
1882 if (afflist != NULL)
1883 {
1884 /* Check for affix name that stands for keep-case word and stands
1885 * for rare word (if defined). */
1886 if (affile->af_huh != NUL
1887 && vim_strchr(afflist, affile->af_huh) != NULL)
1888 flags |= WF_KEEPCAP;
1889 if (affile->af_rar != NUL
1890 && vim_strchr(afflist, affile->af_rar) != NULL)
1891 flags |= WF_RARE;
1892 }
1893
Bram Moolenaar51485f02005-06-04 21:55:20 +00001894 /* Add the word to the word tree(s). */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001895 if (store_word(dw, spin, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001896 retval = FAIL;
1897
1898 if (afflist != NULL)
1899 {
1900 /* Find all matching suffixes and add the resulting words.
1901 * Additionally do matching prefixes that combine. */
1902 if (store_aff_word(dw, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001903 &affile->af_suff, &affile->af_pref,
1904 FALSE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001905 retval = FAIL;
1906
1907 /* Find all matching prefixes and add the resulting words. */
1908 if (store_aff_word(dw, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001909 &affile->af_pref, NULL, FALSE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001910 retval = FAIL;
1911 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001912 }
1913
Bram Moolenaar51485f02005-06-04 21:55:20 +00001914 if (spin->si_ascii && non_ascii > 0)
1915 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
1916 non_ascii);
1917 hash_clear(&ht);
1918
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001919 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001920 return retval;
1921}
1922
1923/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00001924 * Apply affixes to a word and store the resulting words.
1925 * "ht" is the hashtable with affentry_T that need to be applied, either
1926 * prefixes or suffixes.
1927 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
1928 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001929 *
1930 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001931 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001932 static int
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001933store_aff_word(word, spin, afflist, ht, xht, comb, flags)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001934 char_u *word; /* basic word start */
1935 spellinfo_T *spin; /* spell info */
1936 char_u *afflist; /* list of names of supported affixes */
1937 hashtab_T *ht;
1938 hashtab_T *xht;
1939 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001940 int flags; /* flags for the word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001941{
1942 int todo;
1943 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001944 affheader_T *ah;
1945 affentry_T *ae;
1946 regmatch_T regmatch;
1947 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001948 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001949 int i;
1950 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951
Bram Moolenaar51485f02005-06-04 21:55:20 +00001952 todo = ht->ht_used;
1953 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001954 {
1955 if (!HASHITEM_EMPTY(hi))
1956 {
1957 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001958 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00001959
Bram Moolenaar51485f02005-06-04 21:55:20 +00001960 /* Check that the affix combines, if required, and that the word
1961 * supports this affix. */
1962 if ((!comb || ah->ah_combine)
1963 && vim_strchr(afflist, *ah->ah_key) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00001964 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001965 /* Loop over all affix entries with this name. */
1966 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001967 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001968 /* Check the condition. It's not logical to match case
1969 * here, but it is required for compatibility with
1970 * Myspell. */
1971 regmatch.regprog = ae->ae_prog;
1972 regmatch.rm_ic = FALSE;
1973 if (ae->ae_prog == NULL
1974 || vim_regexec(&regmatch, word, (colnr_T)0))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001975 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001976 /* Match. Remove the chop and add the affix. */
1977 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001978 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001979 /* prefix: chop/add at the start of the word */
1980 if (ae->ae_add == NULL)
1981 *newword = NUL;
1982 else
1983 STRCPY(newword, ae->ae_add);
1984 p = word;
1985 if (ae->ae_chop != NULL)
1986 /* Skip chop string. */
1987 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
1988 mb_ptr_adv(p);
1989 STRCAT(newword, p);
1990 }
1991 else
1992 {
1993 /* suffix: chop/add at the end of the word */
1994 STRCPY(newword, word);
1995 if (ae->ae_chop != NULL)
1996 {
1997 /* Remove chop string. */
1998 p = newword + STRLEN(newword);
1999 for (i = mb_charlen(ae->ae_chop); i > 0; --i)
2000 mb_ptr_back(newword, p);
2001 *p = NUL;
2002 }
2003 if (ae->ae_add != NULL)
2004 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002005 }
2006
Bram Moolenaar51485f02005-06-04 21:55:20 +00002007 /* Store the modified word. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002008 if (store_word(newword, spin, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002009 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002010
Bram Moolenaar51485f02005-06-04 21:55:20 +00002011 /* When added a suffix and combining is allowed also
2012 * try adding prefixes additionally. */
2013 if (xht != NULL && ah->ah_combine)
2014 if (store_aff_word(newword, spin, afflist,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002015 xht, NULL, TRUE, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002016 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002017 }
2018 }
2019 }
2020 }
2021 }
2022
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002023 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024}
2025
2026/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002027 * Read a file with a list of words.
2028 */
2029 static int
2030spell_read_wordfile(fname, spin)
2031 char_u *fname;
2032 spellinfo_T *spin;
2033{
2034 FILE *fd;
2035 long lnum = 0;
2036 char_u rline[MAXLINELEN];
2037 char_u *line;
2038 char_u *pc = NULL;
2039 int l;
2040 int retval = OK;
2041 int did_word = FALSE;
2042 int non_ascii = 0;
2043 char_u *enc;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002044 int flags;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002045
2046 /*
2047 * Open the file.
2048 */
2049 fd = fopen((char *)fname, "r");
2050 if (fd == NULL)
2051 {
2052 EMSG2(_(e_notopen), fname);
2053 return FAIL;
2054 }
2055
2056 smsg((char_u *)_("Reading word file %s..."), fname);
2057 out_flush();
2058
2059 /*
2060 * Read all the lines in the file one by one.
2061 */
2062 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2063 {
2064 line_breakcheck();
2065 ++lnum;
2066
2067 /* Skip comment lines. */
2068 if (*rline == '#')
2069 continue;
2070
2071 /* Remove CR, LF and white space from the end. */
2072 l = STRLEN(rline);
2073 while (l > 0 && rline[l - 1] <= ' ')
2074 --l;
2075 if (l == 0)
2076 continue; /* empty or blank line */
2077 rline[l] = NUL;
2078
2079 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
2080 vim_free(pc);
2081 if (spin->si_conv.vc_type != CONV_NONE)
2082 {
2083 pc = string_convert(&spin->si_conv, rline, NULL);
2084 if (pc == NULL)
2085 {
2086 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
2087 fname, lnum, rline);
2088 continue;
2089 }
2090 line = pc;
2091 }
2092 else
2093 {
2094 pc = NULL;
2095 line = rline;
2096 }
2097
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002098 flags = 0;
2099
2100 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00002101 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002102 ++line;
2103 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002104 {
2105 if (spin->si_conv.vc_type != CONV_NONE)
2106 smsg((char_u *)_("Duplicate =encoding= line ignored in %s line %d: %s"),
2107 fname, lnum, line);
2108 else if (did_word)
2109 smsg((char_u *)_("=encoding= line after word ignored in %s line %d: %s"),
2110 fname, lnum, line);
2111 else
2112 {
2113 /* Setup for conversion to 'encoding'. */
2114 enc = enc_canonize(line + 10);
2115 if (enc != NULL && !spin->si_ascii
2116 && convert_setup(&spin->si_conv, enc,
2117 p_enc) == FAIL)
2118 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
2119 fname, line + 10, p_enc);
2120 vim_free(enc);
2121 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002122 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002123 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002124
2125 if (*line == '=')
2126 {
2127 /* keep-case word */
2128 flags |= WF_KEEPCAP;
2129 ++line;
2130 }
2131
2132 if (*line == '!')
2133 {
2134 /* Bad, bad, wicked word. */
2135 flags |= WF_BANNED;
2136 ++line;
2137 }
2138 else if (*line == '?')
2139 {
2140 /* Rare word. */
2141 flags |= WF_RARE;
2142 ++line;
2143 }
2144
2145 if (flags == 0)
2146 {
2147 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 fname, lnum, line);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002149 continue;
2150 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002151 }
2152
2153 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
2154 if (spin->si_ascii && has_non_ascii(line))
2155 {
2156 ++non_ascii;
2157 continue;
2158 }
2159
2160 /* Normal word: store it. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002161 if (store_word(line, spin, flags) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002162 {
2163 retval = FAIL;
2164 break;
2165 }
2166 did_word = TRUE;
2167 }
2168
2169 vim_free(pc);
2170 fclose(fd);
2171
2172 if (spin->si_ascii && non_ascii > 0)
2173 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
2174 non_ascii);
2175 return retval;
2176}
2177
2178/*
2179 * Get part of an sblock_T, "len" bytes long.
2180 * This avoids calling free() for every little struct we use.
2181 * The memory is cleared to all zeros.
2182 * Returns NULL when out of memory.
2183 */
2184 static void *
2185getroom(blp, len)
2186 sblock_T **blp;
2187 size_t len; /* length needed */
2188{
2189 char_u *p;
2190 sblock_T *bl = *blp;
2191
2192 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
2193 {
2194 /* Allocate a block of memory. This is not freed until much later. */
2195 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
2196 if (bl == NULL)
2197 return NULL;
2198 bl->sb_next = *blp;
2199 *blp = bl;
2200 bl->sb_used = 0;
2201 }
2202
2203 p = bl->sb_data + bl->sb_used;
2204 bl->sb_used += len;
2205
2206 return p;
2207}
2208
2209/*
2210 * Make a copy of a string into memory allocated with getroom().
2211 */
2212 static char_u *
2213getroom_save(blp, s)
2214 sblock_T **blp;
2215 char_u *s;
2216{
2217 char_u *sc;
2218
2219 sc = (char_u *)getroom(blp, STRLEN(s) + 1);
2220 if (sc != NULL)
2221 STRCPY(sc, s);
2222 return sc;
2223}
2224
2225
2226/*
2227 * Free the list of allocated sblock_T.
2228 */
2229 static void
2230free_blocks(bl)
2231 sblock_T *bl;
2232{
2233 sblock_T *next;
2234
2235 while (bl != NULL)
2236 {
2237 next = bl->sb_next;
2238 vim_free(bl);
2239 bl = next;
2240 }
2241}
2242
2243/*
2244 * Allocate the root of a word tree.
2245 */
2246 static wordnode_T *
2247wordtree_alloc(blp)
2248 sblock_T **blp;
2249{
2250 return (wordnode_T *)getroom(blp, sizeof(wordnode_T));
2251}
2252
2253/*
2254 * Store a word in the tree(s).
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002255 * Always store it in the case-folded tree. A keep-case word can also be used
2256 * with all caps.
Bram Moolenaar51485f02005-06-04 21:55:20 +00002257 * For a keep-case word also store it in the keep-case tree.
2258 */
2259 static int
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002260store_word(word, spin, flags)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002261 char_u *word;
2262 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002263 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002264{
2265 int len = STRLEN(word);
2266 int ct = captype(word, word + len);
2267 char_u foldword[MAXWLEN];
2268 int res;
2269
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002270 if (flags & WF_KEEPCAP)
2271 res = OK; /* keep-case specified, don't add as fold-case */
2272 else
2273 {
2274 (void)spell_casefold(word, len, foldword, MAXWLEN);
2275 res = tree_add_word(foldword, spin->si_foldroot,
2276 (ct == WF_KEEPCAP ? WF_ALLCAP : ct) | flags,
2277 spin->si_region, &spin->si_blocks);
2278 }
2279
2280 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
2281 res = tree_add_word(word, spin->si_keeproot, flags,
2282 spin->si_region, &spin->si_blocks);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002283 return res;
2284}
2285
2286/*
2287 * Add word "word" to a word tree at "root".
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002288 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002290 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002291tree_add_word(word, root, flags, region, blp)
2292 char_u *word;
2293 wordnode_T *root;
2294 int flags;
2295 int region;
2296 sblock_T **blp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002297{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002298 wordnode_T *node = root;
2299 wordnode_T *np;
2300 wordnode_T **prev = NULL;
2301 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002302
Bram Moolenaar51485f02005-06-04 21:55:20 +00002303 /* Add each byte of the word to the tree, including the NUL at the end. */
2304 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002305 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002306 /* Look for the sibling that has the same character. They are sorted
2307 * on byte value, thus stop searching when a sibling is found with a
2308 * higher byte value. For zero bytes (end of word) check that the
2309 * flags are equal, there is a separate zero byte for each flag value.
2310 */
2311 while (node != NULL && (node->wn_byte < word[i]
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002312 || (node->wn_byte == 0 && node->wn_flags != (flags & 0xff))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002314 prev = &node->wn_sibling;
2315 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002317 if (node == NULL || node->wn_byte != word[i])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002318 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002319 /* Allocate a new node. */
2320 np = (wordnode_T *)getroom(blp, sizeof(wordnode_T));
2321 if (np == NULL)
2322 return FAIL;
2323 np->wn_byte = word[i];
2324 *prev = np;
2325 np->wn_sibling = node;
2326 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002327 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328
Bram Moolenaar51485f02005-06-04 21:55:20 +00002329 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002330 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002331 node->wn_flags = flags;
2332 node->wn_region |= region;
2333 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00002334 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002335 prev = &node->wn_child;
2336 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002337 }
2338
2339 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002340}
2341
2342/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002343 * Compress a tree: find tails that are identical and can be shared.
2344 */
2345 static void
2346wordtree_compress(root)
2347 wordnode_T *root;
2348{
2349 hashtab_T ht;
2350 int n;
2351 int tot = 0;
2352
2353 if (root != NULL)
2354 {
2355 hash_init(&ht);
2356 n = node_compress(root, &ht, &tot);
2357 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
2358 n, tot, (tot - n) * 100 / tot);
2359 hash_clear(&ht);
2360 }
2361}
2362
2363/*
2364 * Compress a node, its siblings and its children, depth first.
2365 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002367 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00002368node_compress(node, ht, tot)
2369 wordnode_T *node;
2370 hashtab_T *ht;
2371 int *tot; /* total count of nodes before compressing,
2372 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002373{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002374 wordnode_T *np;
2375 wordnode_T *tp;
2376 wordnode_T *child;
2377 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002379 int len = 0;
2380 unsigned nr, n;
2381 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002382
Bram Moolenaar51485f02005-06-04 21:55:20 +00002383 /*
2384 * Go through the list of siblings. Compress each child and then try
2385 * finding an identical child to replace it.
2386 * Note that with "child" we mean not just the node that is pointed to,
2387 * but the whole list of siblings, of which the node is the first.
2388 */
2389 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002390 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002391 ++len;
2392 if ((child = np->wn_child) != NULL)
2393 {
2394 /* Compress the child. This fills wn_hashkey. */
2395 compressed += node_compress(child, ht, tot);
2396
2397 /* Try to find an identical child. */
2398 hash = hash_hash(child->wn_hashkey);
2399 hi = hash_lookup(ht, child->wn_hashkey, hash);
2400 tp = NULL;
2401 if (!HASHITEM_EMPTY(hi))
2402 {
2403 /* There are children with an identical hash value. Now check
2404 * if there is one that is really identical. */
2405 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_next)
2406 if (node_equal(child, tp))
2407 {
2408 /* Found one! Now use that child in place of the
2409 * current one. This means the current child is
2410 * dropped from the tree. */
2411 np->wn_child = tp;
2412 ++compressed;
2413 break;
2414 }
2415 if (tp == NULL)
2416 {
2417 /* No other child with this hash value equals the child of
2418 * the node, add it to the linked list after the first
2419 * item. */
2420 tp = HI2WN(hi);
2421 child->wn_next = tp->wn_next;
2422 tp->wn_next = child;
2423 }
2424 }
2425 else
2426 /* No other child has this hash value, add it to the
2427 * hashtable. */
2428 hash_add_item(ht, hi, child->wn_hashkey, hash);
2429 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002430 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002431 *tot += len;
2432
2433 /*
2434 * Make a hash key for the node and its siblings, so that we can quickly
2435 * find a lookalike node. This must be done after compressing the sibling
2436 * list, otherwise the hash key would become invalid by the compression.
2437 */
2438 node->wn_hashkey[0] = len;
2439 nr = 0;
2440 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002441 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002442 if (np->wn_byte == NUL)
2443 /* end node: only use wn_flags and wn_region */
2444 n = np->wn_flags + (np->wn_region << 8);
2445 else
2446 /* byte node: use the byte value and the child pointer */
2447 n = np->wn_byte + ((long_u)np->wn_child << 8);
2448 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002449 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002450
2451 /* Avoid NUL bytes, it terminates the hash key. */
2452 n = nr & 0xff;
2453 node->wn_hashkey[1] = n == 0 ? 1 : n;
2454 n = (nr >> 8) & 0xff;
2455 node->wn_hashkey[2] = n == 0 ? 1 : n;
2456 n = (nr >> 16) & 0xff;
2457 node->wn_hashkey[3] = n == 0 ? 1 : n;
2458 n = (nr >> 24) & 0xff;
2459 node->wn_hashkey[4] = n == 0 ? 1 : n;
2460 node->wn_hashkey[5] = NUL;
2461
2462 return compressed;
2463}
2464
2465/*
2466 * Return TRUE when two nodes have identical siblings and children.
2467 */
2468 static int
2469node_equal(n1, n2)
2470 wordnode_T *n1;
2471 wordnode_T *n2;
2472{
2473 wordnode_T *p1;
2474 wordnode_T *p2;
2475
2476 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
2477 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
2478 if (p1->wn_byte != p2->wn_byte
2479 || (p1->wn_byte == NUL
2480 ? (p1->wn_flags != p2->wn_flags
2481 || p1->wn_region != p2->wn_region)
2482 : (p1->wn_child != p2->wn_child)))
2483 break;
2484
2485 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002486}
2487
2488/*
2489 * Write a number to file "fd", MSB first, in "len" bytes.
2490 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002491 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002492put_bytes(fd, nr, len)
2493 FILE *fd;
2494 long_u nr;
2495 int len;
2496{
2497 int i;
2498
2499 for (i = len - 1; i >= 0; --i)
2500 putc((int)(nr >> (i * 8)), fd);
2501}
2502
2503/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002504 * Write the Vim spell file "fname".
2505 */
2506 static void
Bram Moolenaar51485f02005-06-04 21:55:20 +00002507write_vim_spell(fname, spin, regcount, regchars)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002508 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002510 int regcount; /* number of regions */
2511 char_u *regchars; /* region names */
2512{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002513 FILE *fd;
2514 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 wordnode_T *tree;
2517 int nodecount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002518
Bram Moolenaar51485f02005-06-04 21:55:20 +00002519 fd = fopen((char *)fname, "w");
2520 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002521 {
2522 EMSG2(_(e_notopen), fname);
2523 return;
2524 }
2525
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002526 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
2527 * <charflagslen> <charflags> <fcharslen> <fchars> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002528
2529 /* <fileID> */
2530 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
2531 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002532
2533 /* write the region names if there is more than one */
2534 if (regcount > 1)
2535 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002536 putc(regcount, fd); /* <regioncnt> <regionname> ... */
2537 fwrite(regchars, (size_t)(regcount * 2), (size_t)1, fd);
2538 regionmask = (1 << regcount) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002539 }
2540 else
2541 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002542 putc(0, fd);
2543 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544 }
2545
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002546 /* Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002547 * <charflagslen> <charflags> <fcharlen> <fchars>
2548 * Skip this for ASCII, the table may conflict with the one used for
2549 * 'encoding'. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002550 if (spin->si_ascii)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002551 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002552 putc(0, fd);
2553 putc(0, fd);
2554 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00002555 }
2556 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00002557 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002558
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002560 /* <SUGGEST> : <suggestlen> <more> ...
2561 * TODO. Only write a zero length for now. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002562 put_bytes(fd, 0L, 4); /* <suggestlen> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563
Bram Moolenaar50cde822005-06-05 21:54:54 +00002564 spin->si_memtot = 0;
2565
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002566 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002567 * <LWORDTREE> <KWORDTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002568 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002569 for (round = 1; round <= 2; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002570 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002571 tree = (round == 1) ? spin->si_foldroot : spin->si_keeproot;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572
Bram Moolenaar51485f02005-06-04 21:55:20 +00002573 /* Count the number of nodes. Needed to be able to allocate the
2574 * memory when reading the nodes. Also fills in the index for shared
2575 * nodes. */
2576 nodecount = put_tree(NULL, tree, 0, regionmask);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002577
Bram Moolenaar51485f02005-06-04 21:55:20 +00002578 /* number of nodes in 4 bytes */
2579 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00002580 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002581
Bram Moolenaar51485f02005-06-04 21:55:20 +00002582 /* Write the nodes. */
2583 (void)put_tree(fd, tree, 0, regionmask);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002584 }
2585
Bram Moolenaar51485f02005-06-04 21:55:20 +00002586 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002587}
2588
2589/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002590 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002591 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00002592 * This first writes the list of possible bytes (siblings). Then for each
2593 * byte recursively write the children.
2594 *
2595 * NOTE: The code here must match the code in read_tree(), since assumptions
2596 * are made about the indexes (so that we don't have to write them in the
2597 * file).
2598 *
2599 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002601 static int
2602put_tree(fd, node, index, regionmask)
2603 FILE *fd; /* NULL when only counting */
2604 wordnode_T *node;
2605 int index;
2606 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002607{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002608 int newindex = index;
2609 int siblingcount = 0;
2610 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002612
Bram Moolenaar51485f02005-06-04 21:55:20 +00002613 /* If "node" is zero the tree is empty. */
2614 if (node == NULL)
2615 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616
Bram Moolenaar51485f02005-06-04 21:55:20 +00002617 /* Store the index where this node is written. */
2618 node->wn_index = index;
2619
2620 /* Count the number of siblings. */
2621 for (np = node; np != NULL; np = np->wn_sibling)
2622 ++siblingcount;
2623
2624 /* Write the sibling count. */
2625 if (fd != NULL)
2626 putc(siblingcount, fd); /* <siblingcount> */
2627
2628 /* Write each sibling byte and optionally extra info. */
2629 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002630 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002631 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002632 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002633 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002635 /* For a NUL byte (end of word) instead of the byte itself
2636 * we write the flag/region items. */
2637 flags = np->wn_flags;
2638 if (regionmask != 0 && np->wn_region != regionmask)
2639 flags |= WF_REGION;
2640 if (flags == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002641 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002642 /* word without flags or region */
2643 putc(BY_NOFLAGS, fd); /* <byte> */
2644 }
2645 else
2646 {
2647 putc(BY_FLAGS, fd); /* <byte> */
2648 putc(flags, fd); /* <flags> */
2649 if (flags & WF_REGION)
2650 putc(np->wn_region, fd); /* <regionmask> */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002651 }
2652 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002653 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002654 else
2655 {
2656 if (np->wn_child->wn_index != 0 && np->wn_child->wn_wnode != node)
2657 {
2658 /* The child is written elsewhere, write the reference. */
2659 if (fd != NULL)
2660 {
2661 putc(BY_INDEX, fd); /* <byte> */
2662 /* <nodeidx> */
2663 put_bytes(fd, (long_u)np->wn_child->wn_index, 3);
2664 }
2665 }
2666 else if (np->wn_child->wn_wnode == NULL)
2667 /* We will write the child below and give it an index. */
2668 np->wn_child->wn_wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002669
Bram Moolenaar51485f02005-06-04 21:55:20 +00002670 if (fd != NULL)
2671 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
2672 {
2673 EMSG(_(e_write));
2674 return 0;
2675 }
2676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002677 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002678
2679 /* Space used in the array when reading: one for each sibling and one for
2680 * the count. */
2681 newindex += siblingcount + 1;
2682
2683 /* Recursively dump the children of each sibling. */
2684 for (np = node; np != NULL; np = np->wn_sibling)
2685 if (np->wn_byte != 0 && np->wn_child->wn_wnode == node)
2686 newindex = put_tree(fd, np->wn_child, newindex, regionmask);
2687
2688 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002689}
2690
2691
2692/*
2693 * ":mkspell outfile infile ..."
2694 */
2695 void
2696ex_mkspell(eap)
2697 exarg_T *eap;
2698{
2699 int fcount;
2700 char_u **fnames;
2701 char_u fname[MAXPATHL];
2702 char_u wfname[MAXPATHL];
2703 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002704 int i;
2705 int len;
2706 char_u region_name[16];
2707 struct stat st;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002708 char_u *arg = eap->arg;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002709 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002710 spellinfo_T spin;
2711
2712 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002713
Bram Moolenaar5482f332005-04-17 20:18:43 +00002714 if (STRNCMP(arg, "-ascii", 6) == 0)
2715 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002716 spin.si_ascii = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00002717 arg = skipwhite(arg + 6);
2718 }
2719
2720 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
2721 if (get_arglist_exp(arg, &fcount, &fnames) == FAIL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002722 return;
2723 if (fcount < 2)
2724 EMSG(_(e_invarg)); /* need at least output and input names */
2725 else if (fcount > 9)
2726 EMSG(_("E754: Only up to 8 regions supported"));
2727 else
2728 {
2729 /* Check for overwriting before doing things that may take a lot of
2730 * time. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002731 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
Bram Moolenaar51485f02005-06-04 21:55:20 +00002732 spin.si_ascii ? (char_u *)"ascii" : p_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002733 if (!eap->forceit && mch_stat((char *)wfname, &st) >= 0)
2734 {
2735 EMSG(_(e_exists));
2736 goto theend;
2737 }
2738 if (mch_isdir(fnames[0]))
2739 {
2740 EMSG2(_(e_isadir2), fnames[0]);
2741 goto theend;
2742 }
2743
2744 /*
2745 * Init the aff and dic pointers.
2746 * Get the region names if there are more than 2 arguments.
2747 */
2748 for (i = 1; i < fcount; ++i)
2749 {
2750 afile[i - 1] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002751
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002752 if (fcount > 2)
2753 {
2754 len = STRLEN(fnames[i]);
2755 if (STRLEN(gettail(fnames[i])) < 5 || fnames[i][len - 3] != '_')
2756 {
2757 EMSG2(_("E755: Invalid region in %s"), fnames[i]);
2758 goto theend;
2759 }
2760 else
2761 {
2762 region_name[(i - 1) * 2] = TOLOWER_ASC(fnames[i][len - 2]);
2763 region_name[(i - 1) * 2 + 1] =
2764 TOLOWER_ASC(fnames[i][len - 1]);
2765 }
2766 }
2767 }
2768
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002769 /* Clear the char type tables, don't want to use any of the currently
2770 * used spell properties. */
2771 init_spell_chartab();
2772
Bram Moolenaar51485f02005-06-04 21:55:20 +00002773 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
2774 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
2775 if (spin.si_foldroot == NULL || spin.si_keeproot == NULL)
2776 {
2777 error = TRUE;
2778 goto theend;
2779 }
2780
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002781 /*
2782 * Read all the .aff and .dic files.
2783 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00002784 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002785 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002786 for (i = 1; i < fcount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002787 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002788 spin.si_conv.vc_type = CONV_NONE;
2789 spin.si_region = 1 << (i - 1);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002790
Bram Moolenaar51485f02005-06-04 21:55:20 +00002791 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", fnames[i]);
2792 if (mch_stat((char *)fname, &st) >= 0)
2793 {
2794 /* Read the .aff file. Will init "spin->si_conv" based on the
2795 * "SET" line. */
2796 afile[i - 1] = spell_read_aff(fname, &spin);
2797 if (afile[i - 1] == NULL)
2798 error = TRUE;
2799 else
2800 {
2801 /* Read the .dic file and store the words in the trees. */
2802 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
2803 fnames[i]);
2804 if (spell_read_dic(fname, &spin, afile[i - 1]) == FAIL)
2805 error = TRUE;
2806 }
2807 }
2808 else
2809 {
2810 /* No .aff file, try reading the file as a word list. Store
2811 * the words in the trees. */
2812 if (spell_read_wordfile(fnames[i], &spin) == FAIL)
2813 error = TRUE;
2814 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002815
2816 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002817 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 }
2819
Bram Moolenaar51485f02005-06-04 21:55:20 +00002820 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002821 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002822 /*
2823 * Remove the dummy NUL from the start of the tree root.
2824 */
2825 spin.si_foldroot = spin.si_foldroot->wn_sibling;
2826 spin.si_keeproot = spin.si_keeproot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002827
2828 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002829 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002830 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002831 MSG(_("Compressing word tree..."));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002832 out_flush();
Bram Moolenaar51485f02005-06-04 21:55:20 +00002833 wordtree_compress(spin.si_foldroot);
2834 wordtree_compress(spin.si_keeproot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002835 }
2836
Bram Moolenaar51485f02005-06-04 21:55:20 +00002837 if (!error)
2838 {
2839 /*
2840 * Write the info in the spell file.
2841 */
2842 smsg((char_u *)_("Writing spell file %s..."), wfname);
2843 out_flush();
2844 write_vim_spell(wfname, &spin, fcount - 1, region_name);
2845 MSG(_("Done!"));
Bram Moolenaar50cde822005-06-05 21:54:54 +00002846
2847 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
2848 spin.si_memtot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002849 out_flush();
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002850
2851 /* May need to reload spell dictionaries */
2852 spell_reload();
Bram Moolenaar51485f02005-06-04 21:55:20 +00002853 }
2854
2855 /* Free the allocated memory. */
2856 free_blocks(spin.si_blocks);
2857
2858 /* Free the .aff file structures. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002859 for (i = 1; i < fcount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002860 if (afile[i - 1] != NULL)
2861 spell_free_aff(afile[i - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002862 }
2863
2864theend:
2865 FreeWild(fcount, fnames);
2866}
2867
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002868#endif /* FEAT_MBYTE */
2869
Bram Moolenaar51485f02005-06-04 21:55:20 +00002870
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002871/*
2872 * Init the chartab used for spelling for ASCII.
2873 * EBCDIC is not supported!
2874 */
2875 static void
2876clear_spell_chartab(sp)
2877 spelltab_T *sp;
2878{
2879 int i;
2880
2881 /* Init everything to FALSE. */
2882 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
2883 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
2884 for (i = 0; i < 256; ++i)
2885 sp->st_fold[i] = i;
2886
2887 /* We include digits. A word shouldn't start with a digit, but handling
2888 * that is done separately. */
2889 for (i = '0'; i <= '9'; ++i)
2890 sp->st_isw[i] = TRUE;
2891 for (i = 'A'; i <= 'Z'; ++i)
2892 {
2893 sp->st_isw[i] = TRUE;
2894 sp->st_isu[i] = TRUE;
2895 sp->st_fold[i] = i + 0x20;
2896 }
2897 for (i = 'a'; i <= 'z'; ++i)
2898 sp->st_isw[i] = TRUE;
2899}
2900
2901/*
2902 * Init the chartab used for spelling. Only depends on 'encoding'.
2903 * Called once while starting up and when 'encoding' changes.
2904 * The default is to use isalpha(), but the spell file should define the word
2905 * characters to make it possible that 'encoding' differs from the current
2906 * locale.
2907 */
2908 void
2909init_spell_chartab()
2910{
2911 int i;
2912
2913 did_set_spelltab = FALSE;
2914 clear_spell_chartab(&spelltab);
2915
2916#ifdef FEAT_MBYTE
2917 if (enc_dbcs)
2918 {
2919 /* DBCS: assume double-wide characters are word characters. */
2920 for (i = 128; i <= 255; ++i)
2921 if (MB_BYTE2LEN(i) == 2)
2922 spelltab.st_isw[i] = TRUE;
2923 }
2924 else
2925#endif
2926 {
2927 /* Rough guess: use isalpha() and isupper() for characters above 128. */
2928 for (i = 128; i < 256; ++i)
2929 {
2930 spelltab.st_isw[i] = MB_ISUPPER(i) || MB_ISLOWER(i);
2931 if (MB_ISUPPER(i))
2932 {
2933 spelltab.st_isu[i] = TRUE;
2934 spelltab.st_fold[i] = MB_TOLOWER(i);
2935 }
2936 }
2937 }
2938}
2939
2940#if defined(FEAT_MBYTE) || defined(PROTO)
2941static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
2942static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
2943
2944/*
2945 * Set the spell character tables from strings in the affix file.
2946 */
2947 static int
2948set_spell_chartab(fol, low, upp)
2949 char_u *fol;
2950 char_u *low;
2951 char_u *upp;
2952{
2953 /* We build the new tables here first, so that we can compare with the
2954 * previous one. */
2955 spelltab_T new_st;
2956 char_u *pf = fol, *pl = low, *pu = upp;
2957 int f, l, u;
2958
2959 clear_spell_chartab(&new_st);
2960
2961 while (*pf != NUL)
2962 {
2963 if (*pl == NUL || *pu == NUL)
2964 {
2965 EMSG(_(e_affform));
2966 return FAIL;
2967 }
2968#ifdef FEAT_MBYTE
2969 f = mb_ptr2char_adv(&pf);
2970 l = mb_ptr2char_adv(&pl);
2971 u = mb_ptr2char_adv(&pu);
2972#else
2973 f = *pf++;
2974 l = *pl++;
2975 u = *pu++;
2976#endif
2977 /* Every character that appears is a word character. */
2978 if (f < 256)
2979 new_st.st_isw[f] = TRUE;
2980 if (l < 256)
2981 new_st.st_isw[l] = TRUE;
2982 if (u < 256)
2983 new_st.st_isw[u] = TRUE;
2984
2985 /* if "LOW" and "FOL" are not the same the "LOW" char needs
2986 * case-folding */
2987 if (l < 256 && l != f)
2988 {
2989 if (f >= 256)
2990 {
2991 EMSG(_(e_affrange));
2992 return FAIL;
2993 }
2994 new_st.st_fold[l] = f;
2995 }
2996
2997 /* if "UPP" and "FOL" are not the same the "UPP" char needs
2998 * case-folding and it's upper case. */
2999 if (u < 256 && u != f)
3000 {
3001 if (f >= 256)
3002 {
3003 EMSG(_(e_affrange));
3004 return FAIL;
3005 }
3006 new_st.st_fold[u] = f;
3007 new_st.st_isu[u] = TRUE;
3008 }
3009 }
3010
3011 if (*pl != NUL || *pu != NUL)
3012 {
3013 EMSG(_(e_affform));
3014 return FAIL;
3015 }
3016
3017 return set_spell_finish(&new_st);
3018}
3019#endif
3020
3021/*
3022 * Set the spell character tables from strings in the .spl file.
3023 */
3024 static int
3025set_spell_charflags(flags, cnt, upp)
3026 char_u *flags;
3027 int cnt;
3028 char_u *upp;
3029{
3030 /* We build the new tables here first, so that we can compare with the
3031 * previous one. */
3032 spelltab_T new_st;
3033 int i;
3034 char_u *p = upp;
3035
3036 clear_spell_chartab(&new_st);
3037
3038 for (i = 0; i < cnt; ++i)
3039 {
3040 new_st.st_isw[i + 128] = (flags[i] & SPELL_ISWORD) != 0;
3041 new_st.st_isu[i + 128] = (flags[i] & SPELL_ISUPPER) != 0;
3042
3043 if (*p == NUL)
3044 return FAIL;
3045#ifdef FEAT_MBYTE
3046 new_st.st_fold[i + 128] = mb_ptr2char_adv(&p);
3047#else
3048 new_st.st_fold[i + 128] = *p++;
3049#endif
3050 }
3051
3052 return set_spell_finish(&new_st);
3053}
3054
3055 static int
3056set_spell_finish(new_st)
3057 spelltab_T *new_st;
3058{
3059 int i;
3060
3061 if (did_set_spelltab)
3062 {
3063 /* check that it's the same table */
3064 for (i = 0; i < 256; ++i)
3065 {
3066 if (spelltab.st_isw[i] != new_st->st_isw[i]
3067 || spelltab.st_isu[i] != new_st->st_isu[i]
3068 || spelltab.st_fold[i] != new_st->st_fold[i])
3069 {
3070 EMSG(_("E763: Word characters differ between spell files"));
3071 return FAIL;
3072 }
3073 }
3074 }
3075 else
3076 {
3077 /* copy the new spelltab into the one being used */
3078 spelltab = *new_st;
3079 did_set_spelltab = TRUE;
3080 }
3081
3082 return OK;
3083}
3084
3085#if defined(FEAT_MBYTE) || defined(PROTO)
3086/*
3087 * Write the current tables into the .spl file.
3088 * This makes sure the same characters are recognized as word characters when
3089 * generating an when using a spell file.
3090 */
3091 static void
3092write_spell_chartab(fd)
3093 FILE *fd;
3094{
3095 char_u charbuf[256 * 4];
3096 int len = 0;
3097 int flags;
3098 int i;
3099
3100 fputc(128, fd); /* <charflagslen> */
3101 for (i = 128; i < 256; ++i)
3102 {
3103 flags = 0;
3104 if (spelltab.st_isw[i])
3105 flags |= SPELL_ISWORD;
3106 if (spelltab.st_isu[i])
3107 flags |= SPELL_ISUPPER;
3108 fputc(flags, fd); /* <charflags> */
3109
3110 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
3111 }
3112
3113 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
3114 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
3115}
3116#endif
3117
3118/*
3119 * Return TRUE if "c" is an upper-case character for spelling.
3120 */
3121 static int
3122spell_isupper(c)
3123 int c;
3124{
3125# ifdef FEAT_MBYTE
3126 if (enc_utf8)
3127 {
3128 /* For Unicode we can call utf_isupper(), but don't do that for ASCII,
3129 * because we don't want to use 'casemap' here. */
3130 if (c >= 128)
3131 return utf_isupper(c);
3132 }
3133 else if (has_mbyte && c > 256)
3134 {
3135 /* For characters above 255 we don't have something specfied.
3136 * Fall back to locale-dependent iswupper(). If not available
3137 * simply return FALSE. */
3138# ifdef HAVE_ISWUPPER
3139 return iswupper(c);
3140# else
3141 return FALSE;
3142# endif
3143 }
3144# endif
3145 return spelltab.st_isu[c];
3146}
3147
3148/*
3149 * Case-fold "p[len]" into "buf[buflen]". Used for spell checking.
3150 * When using a multi-byte 'encoding' the length may change!
3151 * Returns FAIL when something wrong.
3152 */
3153 static int
3154spell_casefold(p, len, buf, buflen)
3155 char_u *p;
3156 int len;
3157 char_u *buf;
3158 int buflen;
3159{
3160 int i;
3161
3162 if (len >= buflen)
3163 {
3164 buf[0] = NUL;
3165 return FAIL; /* result will not fit */
3166 }
3167
3168#ifdef FEAT_MBYTE
3169 if (has_mbyte)
3170 {
3171 int c;
3172 int outi = 0;
3173
3174 /* Fold one character at a time. */
3175 for (i = 0; i < len; i += mb_ptr2len_check(p + i))
3176 {
3177 c = mb_ptr2char(p + i);
3178 if (enc_utf8)
3179 /* For Unicode case folding is always the same, no need to use
3180 * the table from the spell file. */
3181 c = utf_fold(c);
3182 else if (c < 256)
3183 /* Use the table from the spell file. */
3184 c = spelltab.st_fold[c];
3185# ifdef HAVE_TOWLOWER
3186 else
3187 /* We don't know what to do, fall back to towlower(), it
3188 * depends on the current locale. */
3189 c = towlower(c);
3190# endif
3191 if (outi + MB_MAXBYTES > buflen)
3192 {
3193 buf[outi] = NUL;
3194 return FAIL;
3195 }
3196 outi += mb_char2bytes(c, buf + outi);
3197 }
3198 buf[outi] = NUL;
3199 }
3200 else
3201#endif
3202 {
3203 /* Be quick for non-multibyte encodings. */
3204 for (i = 0; i < len; ++i)
3205 buf[i] = spelltab.st_fold[p[i]];
3206 buf[i] = NUL;
3207 }
3208
3209 return OK;
3210}
3211
3212
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003213#endif /* FEAT_SYN_HL */