blob: 6cad202fd3a6ac5f0c3b5b035d68c9263cdc5120 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002 *
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 * spellfile.c: code for reading and writing spell files.
12 *
13 * See spell.c for information about spell checking.
14 */
15
16/*
17 * Vim spell file format: <HEADER>
18 * <SECTIONS>
19 * <LWORDTREE>
20 * <KWORDTREE>
21 * <PREFIXTREE>
22 *
23 * <HEADER>: <fileID> <versionnr>
24 *
25 * <fileID> 8 bytes "VIMspell"
26 * <versionnr> 1 byte VIMSPELLVERSION
27 *
28 *
29 * Sections make it possible to add information to the .spl file without
30 * making it incompatible with previous versions. There are two kinds of
31 * sections:
32 * 1. Not essential for correct spell checking. E.g. for making suggestions.
33 * These are skipped when not supported.
34 * 2. Optional information, but essential for spell checking when present.
35 * E.g. conditions for affixes. When this section is present but not
36 * supported an error message is given.
37 *
38 * <SECTIONS>: <section> ... <sectionend>
39 *
40 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
41 *
42 * <sectionID> 1 byte number from 0 to 254 identifying the section
43 *
44 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
45 * spell checking
46 *
47 * <sectionlen> 4 bytes length of section contents, MSB first
48 *
49 * <sectionend> 1 byte SN_END
50 *
51 *
52 * sectionID == SN_INFO: <infotext>
53 * <infotext> N bytes free format text with spell file info (version,
54 * website, etc)
55 *
56 * sectionID == SN_REGION: <regionname> ...
Bram Moolenaar2993ac52018-02-10 14:12:43 +010057 * <regionname> 2 bytes Up to MAXREGIONS region names: ca, au, etc. Lower
58 * case. First <regionname> is region 1.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020059 *
60 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
61 * <folcharslen> <folchars>
62 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
63 * <charflags> N bytes List of flags (first one is for character 128):
64 * 0x01 word character CF_WORD
65 * 0x02 upper-case character CF_UPPER
66 * <folcharslen> 2 bytes Number of bytes in <folchars>.
67 * <folchars> N bytes Folded characters, first one is for character 128.
68 *
69 * sectionID == SN_MIDWORD: <midword>
70 * <midword> N bytes Characters that are word characters only when used
71 * in the middle of a word.
72 *
73 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
74 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
75 * <prefcond> : <condlen> <condstr>
76 * <condlen> 1 byte Length of <condstr>.
77 * <condstr> N bytes Condition for the prefix.
78 *
79 * sectionID == SN_REP: <repcount> <rep> ...
80 * <repcount> 2 bytes number of <rep> items, MSB first.
81 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
82 * <repfromlen> 1 byte length of <repfrom>
83 * <repfrom> N bytes "from" part of replacement
84 * <reptolen> 1 byte length of <repto>
85 * <repto> N bytes "to" part of replacement
86 *
87 * sectionID == SN_REPSAL: <repcount> <rep> ...
88 * just like SN_REP but for soundfolded words
89 *
90 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
91 * <salflags> 1 byte flags for soundsalike conversion:
92 * SAL_F0LLOWUP
93 * SAL_COLLAPSE
94 * SAL_REM_ACCENTS
95 * <salcount> 2 bytes number of <sal> items following
96 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
97 * <salfromlen> 1 byte length of <salfrom>
98 * <salfrom> N bytes "from" part of soundsalike
99 * <saltolen> 1 byte length of <salto>
100 * <salto> N bytes "to" part of soundsalike
101 *
102 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
103 * <sofofromlen> 2 bytes length of <sofofrom>
104 * <sofofrom> N bytes "from" part of soundfold
105 * <sofotolen> 2 bytes length of <sofoto>
106 * <sofoto> N bytes "to" part of soundfold
107 *
108 * sectionID == SN_SUGFILE: <timestamp>
109 * <timestamp> 8 bytes time in seconds that must match with .sug file
110 *
111 * sectionID == SN_NOSPLITSUGS: nothing
112 *
113 * sectionID == SN_NOCOMPOUNDSUGS: nothing
114 *
115 * sectionID == SN_WORDS: <word> ...
116 * <word> N bytes NUL terminated common word
117 *
118 * sectionID == SN_MAP: <mapstr>
119 * <mapstr> N bytes String with sequences of similar characters,
120 * separated by slashes.
121 *
122 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
123 * <comppatcount> <comppattern> ... <compflags>
124 * <compmax> 1 byte Maximum nr of words in compound word.
125 * <compminlen> 1 byte Minimal word length for compounding.
126 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
127 * <compoptions> 2 bytes COMP_ flags.
128 * <comppatcount> 2 bytes number of <comppattern> following
129 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
130 * slashes.
131 *
132 * <comppattern>: <comppatlen> <comppattext>
133 * <comppatlen> 1 byte length of <comppattext>
134 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
135 *
136 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
137 *
138 * sectionID == SN_SYLLABLE: <syllable>
139 * <syllable> N bytes String from SYLLABLE item.
140 *
141 * <LWORDTREE>: <wordtree>
142 *
143 * <KWORDTREE>: <wordtree>
144 *
145 * <PREFIXTREE>: <wordtree>
146 *
147 *
148 * <wordtree>: <nodecount> <nodedata> ...
149 *
150 * <nodecount> 4 bytes Number of nodes following. MSB first.
151 *
152 * <nodedata>: <siblingcount> <sibling> ...
153 *
154 * <siblingcount> 1 byte Number of siblings in this node. The siblings
155 * follow in sorted order.
156 *
157 * <sibling>: <byte> [ <nodeidx> <xbyte>
158 * | <flags> [<flags2>] [<region>] [<affixID>]
159 * | [<pflags>] <affixID> <prefcondnr> ]
160 *
161 * <byte> 1 byte Byte value of the sibling. Special cases:
162 * BY_NOFLAGS: End of word without flags and for all
163 * regions.
164 * For PREFIXTREE <affixID> and
165 * <prefcondnr> follow.
166 * BY_FLAGS: End of word, <flags> follow.
167 * For PREFIXTREE <pflags>, <affixID>
168 * and <prefcondnr> follow.
169 * BY_FLAGS2: End of word, <flags> and <flags2>
170 * follow. Not used in PREFIXTREE.
171 * BY_INDEX: Child of sibling is shared, <nodeidx>
172 * and <xbyte> follow.
173 *
174 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
175 *
176 * <xbyte> 1 byte byte value of the sibling.
177 *
178 * <flags> 1 byte bitmask of:
179 * WF_ALLCAP word must have only capitals
180 * WF_ONECAP first char of word must be capital
181 * WF_KEEPCAP keep-case word
182 * WF_FIXCAP keep-case word, all caps not allowed
183 * WF_RARE rare word
184 * WF_BANNED bad word
185 * WF_REGION <region> follows
186 * WF_AFX <affixID> follows
187 *
188 * <flags2> 1 byte Bitmask of:
189 * WF_HAS_AFF >> 8 word includes affix
190 * WF_NEEDCOMP >> 8 word only valid in compound
191 * WF_NOSUGGEST >> 8 word not used for suggestions
192 * WF_COMPROOT >> 8 word already a compound
193 * WF_NOCOMPBEF >> 8 no compounding before this word
194 * WF_NOCOMPAFT >> 8 no compounding after this word
195 *
196 * <pflags> 1 byte bitmask of:
197 * WFP_RARE rare prefix
198 * WFP_NC non-combining prefix
199 * WFP_UP letter after prefix made upper case
200 *
201 * <region> 1 byte Bitmask for regions in which word is valid. When
202 * omitted it's valid in all regions.
203 * Lowest bit is for region 1.
204 *
205 * <affixID> 1 byte ID of affix that can be used with this word. In
206 * PREFIXTREE used for the required prefix ID.
207 *
208 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
209 * from HEADER.
210 *
211 * All text characters are in 'encoding', but stored as single bytes.
212 */
213
214/*
215 * Vim .sug file format: <SUGHEADER>
216 * <SUGWORDTREE>
217 * <SUGTABLE>
218 *
219 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
220 *
221 * <fileID> 6 bytes "VIMsug"
222 * <versionnr> 1 byte VIMSUGVERSION
223 * <timestamp> 8 bytes timestamp that must match with .spl file
224 *
225 *
226 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
227 *
228 *
229 * <SUGTABLE>: <sugwcount> <sugline> ...
230 *
231 * <sugwcount> 4 bytes number of <sugline> following
232 *
233 * <sugline>: <sugnr> ... NUL
234 *
235 * <sugnr>: X bytes word number that results in this soundfolded word,
236 * stored as an offset to the previous number in as
237 * few bytes as possible, see offset2bytes())
238 */
239
240#include "vim.h"
241
242#if defined(FEAT_SPELL) || defined(PROTO)
243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100244#ifndef UNIX // it's in os_unix.h for Unix
245# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200246#endif
247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100248#ifndef UNIX // it's in os_unix.h for Unix
249# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200250#endif
251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100252// Special byte values for <byte>. Some are only used in the tree for
253// postponed prefixes, some only in the other trees. This is a bit messy...
254#define BY_NOFLAGS 0 // end of word without flags or region; for
255 // postponed prefix: no <pflags>
256#define BY_INDEX 1 // child is shared, index follows
257#define BY_FLAGS 2 // end of word, <flags> byte follows; for
258 // postponed prefix: <pflags> follows
259#define BY_FLAGS2 3 // end of word, <flags> and <flags2> bytes
260 // follow; never used in prefix tree
261#define BY_SPECIAL BY_FLAGS2 // highest special byte value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200262
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +0100263#define ZERO_FLAG 65009 // used when flag is zero: "0"
264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100265// Flags used in .spl file for soundsalike flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200266#define SAL_F0LLOWUP 1
267#define SAL_COLLAPSE 2
268#define SAL_REM_ACCENTS 4
269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100270#define VIMSPELLMAGIC "VIMspell" // string at start of Vim spell file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200271#define VIMSPELLMAGICL 8
272#define VIMSPELLVERSION 50
273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274// Section IDs. Only renumber them when VIMSPELLVERSION changes!
275#define SN_REGION 0 // <regionname> section
276#define SN_CHARFLAGS 1 // charflags section
277#define SN_MIDWORD 2 // <midword> section
278#define SN_PREFCOND 3 // <prefcond> section
279#define SN_REP 4 // REP items section
280#define SN_SAL 5 // SAL items section
281#define SN_SOFO 6 // soundfolding section
282#define SN_MAP 7 // MAP items section
283#define SN_COMPOUND 8 // compound words section
284#define SN_SYLLABLE 9 // syllable section
285#define SN_NOBREAK 10 // NOBREAK section
286#define SN_SUGFILE 11 // timestamp for .sug file
287#define SN_REPSAL 12 // REPSAL items section
288#define SN_WORDS 13 // common words
289#define SN_NOSPLITSUGS 14 // don't split word for suggestions
290#define SN_INFO 15 // info section
291#define SN_NOCOMPOUNDSUGS 16 // don't compound for suggestions
292#define SN_END 255 // end of sections
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100294#define SNF_REQUIRED 1 // <sectionflags>: required section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200295
296#define CF_WORD 0x01
297#define CF_UPPER 0x02
298
299static int set_spell_finish(spelltab_T *new_st);
300static int write_spell_prefcond(FILE *fd, garray_T *gap);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200301static int read_region_section(FILE *fd, slang_T *slang, int len);
302static int read_charflags_section(FILE *fd);
303static int read_prefcond_section(FILE *fd, slang_T *lp);
304static int read_rep_section(FILE *fd, garray_T *gap, short *first);
305static int read_sal_section(FILE *fd, slang_T *slang);
306static int read_words_section(FILE *fd, slang_T *lp, int len);
307static int read_sofo_section(FILE *fd, slang_T *slang);
308static int read_compound(FILE *fd, slang_T *slang, int len);
309static int set_sofo(slang_T *lp, char_u *from, char_u *to);
310static void set_sal_first(slang_T *lp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200311static int *mb_str2wide(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200312static int spell_read_tree(FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt);
313static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200314static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
315static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
316static void set_map_str(slang_T *lp, char_u *map);
317
318
319static char *e_spell_trunc = N_("E758: Truncated spell file");
320static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
321static char *e_affname = N_("Affix name too long in %s line %d: %s");
322static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
323static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
324static char *msg_compressing = N_("Compressing word tree...");
325
326/*
327 * Load one spell file and store the info into a slang_T.
328 *
329 * This is invoked in three ways:
330 * - From spell_load_cb() to load a spell file for the first time. "lang" is
331 * the language name, "old_lp" is NULL. Will allocate an slang_T.
332 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
333 * points to the existing slang_T.
334 * - Just after writing a .spl file; it's read back to produce the .sug file.
335 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
336 *
337 * Returns the slang_T the spell file was loaded into. NULL for error.
338 */
339 slang_T *
340spell_load_file(
341 char_u *fname,
342 char_u *lang,
343 slang_T *old_lp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100344 int silent) // no error if file doesn't exist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200345{
346 FILE *fd;
347 char_u buf[VIMSPELLMAGICL];
348 char_u *p;
349 int i;
350 int n;
351 int len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200352 slang_T *lp = NULL;
353 int c = 0;
354 int res;
355
356 fd = mch_fopen((char *)fname, "r");
357 if (fd == NULL)
358 {
359 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100360 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200361 else if (p_verbose > 2)
362 {
363 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100364 smsg((const char *)e_notopen, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200365 verbose_leave();
366 }
367 goto endFAIL;
368 }
369 if (p_verbose > 2)
370 {
371 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100372 smsg(_("Reading spell file \"%s\""), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200373 verbose_leave();
374 }
375
376 if (old_lp == NULL)
377 {
378 lp = slang_alloc(lang);
379 if (lp == NULL)
380 goto endFAIL;
381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100382 // Remember the file name, used to reload the file when it's updated.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200383 lp->sl_fname = vim_strsave(fname);
384 if (lp->sl_fname == NULL)
385 goto endFAIL;
386
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100387 // Check for .add.spl (_add.spl for VMS).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200388 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
389 }
390 else
391 lp = old_lp;
392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100393 // Set sourcing_name, so that error messages mention the file name.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100394 estack_push(ETYPE_SPELL, fname, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200395
396 /*
397 * <HEADER>: <fileID>
398 */
399 for (i = 0; i < VIMSPELLMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100400 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200401 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100403 emsg(_("E757: This does not look like a spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200404 goto endFAIL;
405 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100406 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200407 if (c < VIMSPELLVERSION)
408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100409 emsg(_("E771: Old spell file, needs to be updated"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200410 goto endFAIL;
411 }
412 else if (c > VIMSPELLVERSION)
413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100414 emsg(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200415 goto endFAIL;
416 }
417
418
419 /*
420 * <SECTIONS>: <section> ... <sectionend>
421 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
422 */
423 for (;;)
424 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100425 n = getc(fd); // <sectionID> or <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200426 if (n == SN_END)
427 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100428 c = getc(fd); // <sectionflags>
429 len = get4c(fd); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200430 if (len < 0)
431 goto truncerr;
432
433 res = 0;
434 switch (n)
435 {
436 case SN_INFO:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100437 lp->sl_info = read_string(fd, len); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200438 if (lp->sl_info == NULL)
439 goto endFAIL;
440 break;
441
442 case SN_REGION:
443 res = read_region_section(fd, lp, len);
444 break;
445
446 case SN_CHARFLAGS:
447 res = read_charflags_section(fd);
448 break;
449
450 case SN_MIDWORD:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100451 lp->sl_midword = read_string(fd, len); // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200452 if (lp->sl_midword == NULL)
453 goto endFAIL;
454 break;
455
456 case SN_PREFCOND:
457 res = read_prefcond_section(fd, lp);
458 break;
459
460 case SN_REP:
461 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
462 break;
463
464 case SN_REPSAL:
465 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
466 break;
467
468 case SN_SAL:
469 res = read_sal_section(fd, lp);
470 break;
471
472 case SN_SOFO:
473 res = read_sofo_section(fd, lp);
474 break;
475
476 case SN_MAP:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100477 p = read_string(fd, len); // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200478 if (p == NULL)
479 goto endFAIL;
480 set_map_str(lp, p);
481 vim_free(p);
482 break;
483
484 case SN_WORDS:
485 res = read_words_section(fd, lp, len);
486 break;
487
488 case SN_SUGFILE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100489 lp->sl_sugtime = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200490 break;
491
492 case SN_NOSPLITSUGS:
493 lp->sl_nosplitsugs = TRUE;
494 break;
495
496 case SN_NOCOMPOUNDSUGS:
497 lp->sl_nocompoundsugs = TRUE;
498 break;
499
500 case SN_COMPOUND:
501 res = read_compound(fd, lp, len);
502 break;
503
504 case SN_NOBREAK:
505 lp->sl_nobreak = TRUE;
506 break;
507
508 case SN_SYLLABLE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100509 lp->sl_syllable = read_string(fd, len); // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200510 if (lp->sl_syllable == NULL)
511 goto endFAIL;
512 if (init_syl_tab(lp) == FAIL)
513 goto endFAIL;
514 break;
515
516 default:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100517 // Unsupported section. When it's required give an error
518 // message. When it's not required skip the contents.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200519 if (c & SNF_REQUIRED)
520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100521 emsg(_("E770: Unsupported section in spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200522 goto endFAIL;
523 }
524 while (--len >= 0)
525 if (getc(fd) < 0)
526 goto truncerr;
527 break;
528 }
529someerror:
530 if (res == SP_FORMERROR)
531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100532 emsg(_(e_format));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200533 goto endFAIL;
534 }
535 if (res == SP_TRUNCERROR)
536 {
537truncerr:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100538 emsg(_(e_spell_trunc));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200539 goto endFAIL;
540 }
541 if (res == SP_OTHERERROR)
542 goto endFAIL;
543 }
544
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100545 // <LWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200546 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
547 if (res != 0)
548 goto someerror;
549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 // <KWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200551 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
552 if (res != 0)
553 goto someerror;
554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100555 // <PREFIXTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200556 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
557 lp->sl_prefixcnt);
558 if (res != 0)
559 goto someerror;
560
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100561 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200562 if (old_lp == NULL && lang != NULL)
563 {
564 lp->sl_next = first_lang;
565 first_lang = lp;
566 }
567
568 goto endOK;
569
570endFAIL:
571 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100572 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200573 *lang = NUL;
574 if (lp != NULL && old_lp == NULL)
575 slang_free(lp);
576 lp = NULL;
577
578endOK:
579 if (fd != NULL)
580 fclose(fd);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100581 estack_pop();
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200582
583 return lp;
584}
585
586/*
587 * Fill in the wordcount fields for a trie.
588 * Returns the total number of words.
589 */
590 static void
591tree_count_words(char_u *byts, idx_T *idxs)
592{
593 int depth;
594 idx_T arridx[MAXWLEN];
595 int curi[MAXWLEN];
596 int c;
597 idx_T n;
598 int wordcount[MAXWLEN];
599
600 arridx[0] = 0;
601 curi[0] = 1;
602 wordcount[0] = 0;
603 depth = 0;
604 while (depth >= 0 && !got_int)
605 {
606 if (curi[depth] > byts[arridx[depth]])
607 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100608 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200609 idxs[arridx[depth]] = wordcount[depth];
610 if (depth > 0)
611 wordcount[depth - 1] += wordcount[depth];
612
613 --depth;
614 fast_breakcheck();
615 }
616 else
617 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100618 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200619 n = arridx[depth] + curi[depth];
620 ++curi[depth];
621
622 c = byts[n];
623 if (c == 0)
624 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100625 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200626 ++wordcount[depth];
627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100628 // Skip over any other NUL bytes (same word with different
629 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200630 while (byts[n + 1] == 0)
631 {
632 ++n;
633 ++curi[depth];
634 }
635 }
636 else
637 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100638 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200639 ++depth;
640 arridx[depth] = idxs[n];
641 curi[depth] = 1;
642 wordcount[depth] = 0;
643 }
644 }
645 }
646}
647
648/*
649 * Load the .sug files for languages that have one and weren't loaded yet.
650 */
651 void
652suggest_load_files(void)
653{
654 langp_T *lp;
655 int lpi;
656 slang_T *slang;
657 char_u *dotp;
658 FILE *fd;
659 char_u buf[MAXWLEN];
660 int i;
661 time_t timestamp;
662 int wcount;
663 int wordnr;
664 garray_T ga;
665 int c;
666
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100667 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200668 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
669 {
670 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
671 slang = lp->lp_slang;
672 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
673 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100674 // Change ".spl" to ".sug" and open the file. When the file isn't
675 // found silently skip it. Do set "sl_sugloaded" so that we
676 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200677 slang->sl_sugloaded = TRUE;
678
679 dotp = vim_strrchr(slang->sl_fname, '.');
680 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
681 continue;
682 STRCPY(dotp, ".sug");
683 fd = mch_fopen((char *)slang->sl_fname, "r");
684 if (fd == NULL)
685 goto nextone;
686
687 /*
688 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
689 */
690 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100691 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200692 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100694 semsg(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200695 slang->sl_fname);
696 goto nextone;
697 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100698 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200699 if (c < VIMSUGVERSION)
700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100701 semsg(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200702 slang->sl_fname);
703 goto nextone;
704 }
705 else if (c > VIMSUGVERSION)
706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100707 semsg(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200708 slang->sl_fname);
709 goto nextone;
710 }
711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100712 // Check the timestamp, it must be exactly the same as the one in
713 // the .spl file. Otherwise the word numbers won't match.
714 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200715 if (timestamp != slang->sl_sugtime)
716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100717 semsg(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200718 slang->sl_fname);
719 goto nextone;
720 }
721
722 /*
723 * <SUGWORDTREE>: <wordtree>
724 * Read the trie with the soundfolded words.
725 */
726 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
727 FALSE, 0) != 0)
728 {
729someerror:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100730 semsg(_("E782: error while reading .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200731 slang->sl_fname);
732 slang_clear_sug(slang);
733 goto nextone;
734 }
735
736 /*
737 * <SUGTABLE>: <sugwcount> <sugline> ...
738 *
739 * Read the table with word numbers. We use a file buffer for
740 * this, because it's so much like a file with lines. Makes it
741 * possible to swap the info and save on memory use.
742 */
743 slang->sl_sugbuf = open_spellbuf();
744 if (slang->sl_sugbuf == NULL)
745 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100746 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200747 wcount = get4c(fd);
748 if (wcount < 0)
749 goto someerror;
750
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100751 // Read all the wordnr lists into the buffer, one NUL terminated
752 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200753 ga_init2(&ga, 1, 100);
754 for (wordnr = 0; wordnr < wcount; ++wordnr)
755 {
756 ga.ga_len = 0;
757 for (;;)
758 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100759 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200760 if (c < 0 || ga_grow(&ga, 1) == FAIL)
761 goto someerror;
762 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
763 if (c == NUL)
764 break;
765 }
766 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
767 ga.ga_data, ga.ga_len, TRUE) == FAIL)
768 goto someerror;
769 }
770 ga_clear(&ga);
771
772 /*
773 * Need to put word counts in the word tries, so that we can find
774 * a word by its number.
775 */
776 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
777 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
778
779nextone:
780 if (fd != NULL)
781 fclose(fd);
782 STRCPY(dotp, ".spl");
783 }
784 }
785}
786
787
788/*
789 * Read a length field from "fd" in "cnt_bytes" bytes.
790 * Allocate memory, read the string into it and add a NUL at the end.
791 * Returns NULL when the count is zero.
792 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
793 * otherwise.
794 */
795 static char_u *
796read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
797{
798 int cnt = 0;
799 int i;
800 char_u *str;
801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100802 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200803 for (i = 0; i < cnt_bytes; ++i)
804 cnt = (cnt << 8) + getc(fd);
805 if (cnt < 0)
806 {
807 *cntp = SP_TRUNCERROR;
808 return NULL;
809 }
810 *cntp = cnt;
811 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100812 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200813
814 str = read_string(fd, cnt);
815 if (str == NULL)
816 *cntp = SP_OTHERERROR;
817 return str;
818}
819
820/*
821 * Read SN_REGION: <regionname> ...
822 * Return SP_*ERROR flags.
823 */
824 static int
825read_region_section(FILE *fd, slang_T *lp, int len)
826{
827 int i;
828
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100829 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200830 return SP_FORMERROR;
831 for (i = 0; i < len; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100832 lp->sl_regions[i] = getc(fd); // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200833 lp->sl_regions[len] = NUL;
834 return 0;
835}
836
837/*
838 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
839 * <folcharslen> <folchars>
840 * Return SP_*ERROR flags.
841 */
842 static int
843read_charflags_section(FILE *fd)
844{
845 char_u *flags;
846 char_u *fol;
847 int flagslen, follen;
848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100849 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200850 flags = read_cnt_string(fd, 1, &flagslen);
851 if (flagslen < 0)
852 return flagslen;
853
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100854 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200855 fol = read_cnt_string(fd, 2, &follen);
856 if (follen < 0)
857 {
858 vim_free(flags);
859 return follen;
860 }
861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100862 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200863 if (flags != NULL && fol != NULL)
864 set_spell_charflags(flags, flagslen, fol);
865
866 vim_free(flags);
867 vim_free(fol);
868
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100869 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200870 if ((flags == NULL) != (fol == NULL))
871 return SP_FORMERROR;
872 return 0;
873}
874
875/*
876 * Read SN_PREFCOND section.
877 * Return SP_*ERROR flags.
878 */
879 static int
880read_prefcond_section(FILE *fd, slang_T *lp)
881{
882 int cnt;
883 int i;
884 int n;
885 char_u *p;
886 char_u buf[MAXWLEN + 1];
887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100888 // <prefcondcnt> <prefcond> ...
889 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200890 if (cnt <= 0)
891 return SP_FORMERROR;
892
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200893 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200894 if (lp->sl_prefprog == NULL)
895 return SP_OTHERERROR;
896 lp->sl_prefixcnt = cnt;
897
898 for (i = 0; i < cnt; ++i)
899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100900 // <prefcond> : <condlen> <condstr>
901 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200902 if (n < 0 || n >= MAXWLEN)
903 return SP_FORMERROR;
904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100905 // When <condlen> is zero we have an empty condition. Otherwise
906 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200907 if (n > 0)
908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100909 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200910 p = buf + 1;
911 while (n-- > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100912 *p++ = getc(fd); // <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200913 *p = NUL;
914 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
915 }
916 }
917 return 0;
918}
919
920/*
921 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
922 * Return SP_*ERROR flags.
923 */
924 static int
925read_rep_section(FILE *fd, garray_T *gap, short *first)
926{
927 int cnt;
928 fromto_T *ftp;
929 int i;
930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100931 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200932 if (cnt < 0)
933 return SP_TRUNCERROR;
934
935 if (ga_grow(gap, cnt) == FAIL)
936 return SP_OTHERERROR;
937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100938 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200939 for (; gap->ga_len < cnt; ++gap->ga_len)
940 {
941 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
942 ftp->ft_from = read_cnt_string(fd, 1, &i);
943 if (i < 0)
944 return i;
945 if (i == 0)
946 return SP_FORMERROR;
947 ftp->ft_to = read_cnt_string(fd, 1, &i);
948 if (i <= 0)
949 {
950 vim_free(ftp->ft_from);
951 if (i < 0)
952 return i;
953 return SP_FORMERROR;
954 }
955 }
956
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100957 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200958 for (i = 0; i < 256; ++i)
959 first[i] = -1;
960 for (i = 0; i < gap->ga_len; ++i)
961 {
962 ftp = &((fromto_T *)gap->ga_data)[i];
963 if (first[*ftp->ft_from] == -1)
964 first[*ftp->ft_from] = i;
965 }
966 return 0;
967}
968
969/*
970 * Read SN_SAL section: <salflags> <salcount> <sal> ...
971 * Return SP_*ERROR flags.
972 */
973 static int
974read_sal_section(FILE *fd, slang_T *slang)
975{
976 int i;
977 int cnt;
978 garray_T *gap;
979 salitem_T *smp;
980 int ccnt;
981 char_u *p;
982 int c = NUL;
983
984 slang->sl_sofo = FALSE;
985
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100986 i = getc(fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200987 if (i & SAL_F0LLOWUP)
988 slang->sl_followup = TRUE;
989 if (i & SAL_COLLAPSE)
990 slang->sl_collapse = TRUE;
991 if (i & SAL_REM_ACCENTS)
992 slang->sl_rem_accents = TRUE;
993
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100994 cnt = get2c(fd); // <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200995 if (cnt < 0)
996 return SP_TRUNCERROR;
997
998 gap = &slang->sl_sal;
999 ga_init2(gap, sizeof(salitem_T), 10);
1000 if (ga_grow(gap, cnt + 1) == FAIL)
1001 return SP_OTHERERROR;
1002
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001003 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001004 for (; gap->ga_len < cnt; ++gap->ga_len)
1005 {
1006 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001007 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001008 if (ccnt < 0)
1009 return SP_TRUNCERROR;
1010 if ((p = alloc(ccnt + 2)) == NULL)
1011 return SP_OTHERERROR;
1012 smp->sm_lead = p;
1013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001014 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001015 for (i = 0; i < ccnt; ++i)
1016 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001017 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001018 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1019 break;
1020 *p++ = c;
1021 }
1022 smp->sm_leadlen = (int)(p - smp->sm_lead);
1023 *p++ = NUL;
1024
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001025 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001026 if (c == '(')
1027 {
1028 smp->sm_oneof = p;
1029 for (++i; i < ccnt; ++i)
1030 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001031 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001032 if (c == ')')
1033 break;
1034 *p++ = c;
1035 }
1036 *p++ = NUL;
1037 if (++i < ccnt)
1038 c = getc(fd);
1039 }
1040 else
1041 smp->sm_oneof = NULL;
1042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001043 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001044 smp->sm_rules = p;
1045 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001046 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001047 *p++ = c;
1048 for (++i; i < ccnt; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001049 *p++ = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001050 *p++ = NUL;
1051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001052 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001053 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1054 if (ccnt < 0)
1055 {
1056 vim_free(smp->sm_lead);
1057 return ccnt;
1058 }
1059
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001060 if (has_mbyte)
1061 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001062 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001063 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1064 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1065 if (smp->sm_oneof == NULL)
1066 smp->sm_oneof_w = NULL;
1067 else
1068 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1069 if (smp->sm_to == NULL)
1070 smp->sm_to_w = NULL;
1071 else
1072 smp->sm_to_w = mb_str2wide(smp->sm_to);
1073 if (smp->sm_lead_w == NULL
1074 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1075 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1076 {
1077 vim_free(smp->sm_lead);
1078 vim_free(smp->sm_to);
1079 vim_free(smp->sm_lead_w);
1080 vim_free(smp->sm_oneof_w);
1081 vim_free(smp->sm_to_w);
1082 return SP_OTHERERROR;
1083 }
1084 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001085 }
1086
1087 if (gap->ga_len > 0)
1088 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001089 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1090 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001091 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1092 if ((p = alloc(1)) == NULL)
1093 return SP_OTHERERROR;
1094 p[0] = NUL;
1095 smp->sm_lead = p;
1096 smp->sm_leadlen = 0;
1097 smp->sm_oneof = NULL;
1098 smp->sm_rules = p;
1099 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001100 if (has_mbyte)
1101 {
1102 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1103 smp->sm_leadlen = 0;
1104 smp->sm_oneof_w = NULL;
1105 smp->sm_to_w = NULL;
1106 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001107 ++gap->ga_len;
1108 }
1109
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001110 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001111 set_sal_first(slang);
1112
1113 return 0;
1114}
1115
1116/*
1117 * Read SN_WORDS: <word> ...
1118 * Return SP_*ERROR flags.
1119 */
1120 static int
1121read_words_section(FILE *fd, slang_T *lp, int len)
1122{
1123 int done = 0;
1124 int i;
1125 int c;
1126 char_u word[MAXWLEN];
1127
1128 while (done < len)
1129 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001130 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001131 for (i = 0; ; ++i)
1132 {
1133 c = getc(fd);
1134 if (c == EOF)
1135 return SP_TRUNCERROR;
1136 word[i] = c;
1137 if (word[i] == NUL)
1138 break;
1139 if (i == MAXWLEN - 1)
1140 return SP_FORMERROR;
1141 }
1142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001143 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001144 count_common_word(lp, word, -1, 10);
1145 done += i + 1;
1146 }
1147 return 0;
1148}
1149
1150/*
1151 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1152 * Return SP_*ERROR flags.
1153 */
1154 static int
1155read_sofo_section(FILE *fd, slang_T *slang)
1156{
1157 int cnt;
1158 char_u *from, *to;
1159 int res;
1160
1161 slang->sl_sofo = TRUE;
1162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001163 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001164 from = read_cnt_string(fd, 2, &cnt);
1165 if (cnt < 0)
1166 return cnt;
1167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001168 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001169 to = read_cnt_string(fd, 2, &cnt);
1170 if (cnt < 0)
1171 {
1172 vim_free(from);
1173 return cnt;
1174 }
1175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001176 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001177 if (from != NULL && to != NULL)
1178 res = set_sofo(slang, from, to);
1179 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001180 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001181 else
1182 res = 0;
1183
1184 vim_free(from);
1185 vim_free(to);
1186 return res;
1187}
1188
1189/*
1190 * Read the compound section from the .spl file:
1191 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1192 * Returns SP_*ERROR flags.
1193 */
1194 static int
1195read_compound(FILE *fd, slang_T *slang, int len)
1196{
1197 int todo = len;
1198 int c;
1199 int atstart;
1200 char_u *pat;
1201 char_u *pp;
1202 char_u *cp;
1203 char_u *ap;
1204 char_u *crp;
1205 int cnt;
1206 garray_T *gap;
1207
1208 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001209 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001210
1211 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001212 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001213 if (c < 2)
1214 c = MAXWLEN;
1215 slang->sl_compmax = c;
1216
1217 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001218 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001219 if (c < 1)
1220 c = 0;
1221 slang->sl_compminlen = c;
1222
1223 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001224 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001225 if (c < 1)
1226 c = MAXWLEN;
1227 slang->sl_compsylmax = c;
1228
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001229 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001230 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001231 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001232 else
1233 {
1234 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001235 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001236 --todo;
1237 slang->sl_compoptions = c;
1238
1239 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001240 c = get2c(fd); // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001241 todo -= 2;
1242 ga_init2(gap, sizeof(char_u *), c);
1243 if (ga_grow(gap, c) == OK)
1244 while (--c >= 0)
1245 {
1246 ((char_u **)(gap->ga_data))[gap->ga_len++] =
1247 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001248 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001249 if (cnt < 0)
1250 return cnt;
1251 todo -= cnt + 1;
1252 }
1253 }
1254 if (todo < 0)
1255 return SP_FORMERROR;
1256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001257 // Turn the COMPOUNDRULE items into a regexp pattern:
1258 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1259 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1260 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001261 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001262 if (enc_utf8)
1263 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001264 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001265 if (pat == NULL)
1266 return SP_OTHERERROR;
1267
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001268 // We also need a list of all flags that can appear at the start and one
1269 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001270 cp = alloc(todo + 1);
1271 if (cp == NULL)
1272 {
1273 vim_free(pat);
1274 return SP_OTHERERROR;
1275 }
1276 slang->sl_compstartflags = cp;
1277 *cp = NUL;
1278
1279 ap = alloc(todo + 1);
1280 if (ap == NULL)
1281 {
1282 vim_free(pat);
1283 return SP_OTHERERROR;
1284 }
1285 slang->sl_compallflags = ap;
1286 *ap = NUL;
1287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001288 // And a list of all patterns in their original form, for checking whether
1289 // compounding may work in match_compoundrule(). This is freed when we
1290 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001291 crp = alloc(todo + 1);
1292 slang->sl_comprules = crp;
1293
1294 pp = pat;
1295 *pp++ = '^';
1296 *pp++ = '\\';
1297 *pp++ = '(';
1298
1299 atstart = 1;
1300 while (todo-- > 0)
1301 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001302 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001303 if (c == EOF)
1304 {
1305 vim_free(pat);
1306 return SP_TRUNCERROR;
1307 }
1308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001309 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001310 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1311 && !byte_in_str(slang->sl_compallflags, c))
1312 {
1313 *ap++ = c;
1314 *ap = NUL;
1315 }
1316
1317 if (atstart != 0)
1318 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001319 // At start of item: copy flags to "sl_compstartflags". For a
1320 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001321 if (c == '[')
1322 atstart = 2;
1323 else if (c == ']')
1324 atstart = 0;
1325 else
1326 {
1327 if (!byte_in_str(slang->sl_compstartflags, c))
1328 {
1329 *cp++ = c;
1330 *cp = NUL;
1331 }
1332 if (atstart == 1)
1333 atstart = 0;
1334 }
1335 }
1336
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001337 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001338 if (crp != NULL)
1339 {
1340 if (c == '?' || c == '+' || c == '*')
1341 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001342 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001343 crp = NULL;
1344 }
1345 else
1346 *crp++ = c;
1347 }
1348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001349 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001350 {
1351 *pp++ = '\\';
1352 *pp++ = '|';
1353 atstart = 1;
1354 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001355 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001356 {
1357 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001358 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001359 if (enc_utf8)
1360 pp += mb_char2bytes(c, pp);
1361 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001362 *pp++ = c;
1363 }
1364 }
1365
1366 *pp++ = '\\';
1367 *pp++ = ')';
1368 *pp++ = '$';
1369 *pp = NUL;
1370
1371 if (crp != NULL)
1372 *crp = NUL;
1373
1374 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1375 vim_free(pat);
1376 if (slang->sl_compprog == NULL)
1377 return SP_FORMERROR;
1378
1379 return 0;
1380}
1381
1382/*
1383 * Set the SOFOFROM and SOFOTO items in language "lp".
1384 * Returns SP_*ERROR flags when there is something wrong.
1385 */
1386 static int
1387set_sofo(slang_T *lp, char_u *from, char_u *to)
1388{
1389 int i;
1390
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001391 garray_T *gap;
1392 char_u *s;
1393 char_u *p;
1394 int c;
1395 int *inp;
1396
1397 if (has_mbyte)
1398 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001399 // Use "sl_sal" as an array with 256 pointers to a list of wide
1400 // characters. The index is the low byte of the character.
1401 // The list contains from-to pairs with a terminating NUL.
1402 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001403 gap = &lp->sl_sal;
1404 ga_init2(gap, sizeof(int *), 1);
1405 if (ga_grow(gap, 256) == FAIL)
1406 return SP_OTHERERROR;
1407 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1408 gap->ga_len = 256;
1409
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001410 // First count the number of items for each list. Temporarily use
1411 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001412 for (p = from, s = to; *p != NUL && *s != NUL; )
1413 {
1414 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001415 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001416 if (c >= 256)
1417 ++lp->sl_sal_first[c & 0xff];
1418 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001419 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001420 return SP_FORMERROR;
1421
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001423 for (i = 0; i < 256; ++i)
1424 if (lp->sl_sal_first[i] > 0)
1425 {
1426 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1427 if (p == NULL)
1428 return SP_OTHERERROR;
1429 ((int **)gap->ga_data)[i] = (int *)p;
1430 *(int *)p = 0;
1431 }
1432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1434 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001435 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1436 for (p = from, s = to; *p != NUL && *s != NUL; )
1437 {
1438 c = mb_cptr2char_adv(&p);
1439 i = mb_cptr2char_adv(&s);
1440 if (c >= 256)
1441 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001442 // Append the from-to chars at the end of the list with
1443 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001444 inp = ((int **)gap->ga_data)[c & 0xff];
1445 while (*inp != 0)
1446 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001447 *inp++ = c; // from char
1448 *inp++ = i; // to char
1449 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001450 }
1451 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001452 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001453 lp->sl_sal_first[c] = i;
1454 }
1455 }
1456 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001457 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001458 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001459 if (STRLEN(from) != STRLEN(to))
1460 return SP_FORMERROR;
1461
1462 for (i = 0; to[i] != NUL; ++i)
1463 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001464 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001465 }
1466
1467 return 0;
1468}
1469
1470/*
1471 * Fill the first-index table for "lp".
1472 */
1473 static void
1474set_sal_first(slang_T *lp)
1475{
1476 salfirst_T *sfirst;
1477 int i;
1478 salitem_T *smp;
1479 int c;
1480 garray_T *gap = &lp->sl_sal;
1481
1482 sfirst = lp->sl_sal_first;
1483 for (i = 0; i < 256; ++i)
1484 sfirst[i] = -1;
1485 smp = (salitem_T *)gap->ga_data;
1486 for (i = 0; i < gap->ga_len; ++i)
1487 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001488 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001489 // Use the lowest byte of the first character. For latin1 it's
1490 // the character, for other encodings it should differ for most
1491 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001492 c = *smp[i].sm_lead_w & 0xff;
1493 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001494 c = *smp[i].sm_lead;
1495 if (sfirst[c] == -1)
1496 {
1497 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001498 if (has_mbyte)
1499 {
1500 int n;
1501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001502 // Make sure all entries with this byte are following each
1503 // other. Move the ones that are in the wrong position. Do
1504 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001505 while (i + 1 < gap->ga_len
1506 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001507 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001508 ++i;
1509
1510 for (n = 1; i + n < gap->ga_len; ++n)
1511 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1512 {
1513 salitem_T tsal;
1514
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001515 // Move entry with same index byte after the entries
1516 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001517 ++i;
1518 --n;
1519 tsal = smp[i + n];
1520 mch_memmove(smp + i + 1, smp + i,
1521 sizeof(salitem_T) * n);
1522 smp[i] = tsal;
1523 }
1524 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001525 }
1526 }
1527}
1528
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001529/*
1530 * Turn a multi-byte string into a wide character string.
1531 * Return it in allocated memory (NULL for out-of-memory)
1532 */
1533 static int *
1534mb_str2wide(char_u *s)
1535{
1536 int *res;
1537 char_u *p;
1538 int i = 0;
1539
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001540 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001541 if (res != NULL)
1542 {
1543 for (p = s; *p != NUL; )
1544 res[i++] = mb_ptr2char_adv(&p);
1545 res[i] = NUL;
1546 }
1547 return res;
1548}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001549
1550/*
1551 * Read a tree from the .spl or .sug file.
1552 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1553 * This is skipped when the tree has zero length.
1554 * Returns zero when OK, SP_ value for an error.
1555 */
1556 static int
1557spell_read_tree(
1558 FILE *fd,
1559 char_u **bytsp,
1560 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001561 int prefixtree, // TRUE for the prefix tree
1562 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001563{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001564 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001565 int idx;
1566 char_u *bp;
1567 idx_T *ip;
1568
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001569 // The tree size was computed when writing the file, so that we can
1570 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001571 len = get4c(fd);
1572 if (len < 0)
1573 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001574 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001575 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001576 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001577 if (len > 0)
1578 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001579 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001580 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001581 if (bp == NULL)
1582 return SP_OTHERERROR;
1583 *bytsp = bp;
1584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001585 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001586 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001587 if (ip == NULL)
1588 return SP_OTHERERROR;
1589 *idxsp = ip;
1590
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001591 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001592 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1593 if (idx < 0)
1594 return idx;
1595 }
1596 return 0;
1597}
1598
1599/*
1600 * Read one row of siblings from the spell file and store it in the byte array
1601 * "byts" and index array "idxs". Recursively read the children.
1602 *
1603 * NOTE: The code here must match put_node()!
1604 *
1605 * Returns the index (>= 0) following the siblings.
1606 * Returns SP_TRUNCERROR if the file is shorter than expected.
1607 * Returns SP_FORMERROR if there is a format error.
1608 */
1609 static idx_T
1610read_tree_node(
1611 FILE *fd,
1612 char_u *byts,
1613 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001614 int maxidx, // size of arrays
1615 idx_T startidx, // current index in "byts" and "idxs"
1616 int prefixtree, // TRUE for reading PREFIXTREE
1617 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001618{
1619 int len;
1620 int i;
1621 int n;
1622 idx_T idx = startidx;
1623 int c;
1624 int c2;
1625#define SHARED_MASK 0x8000000
1626
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001627 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001628 if (len <= 0)
1629 return SP_TRUNCERROR;
1630
1631 if (startidx + len >= maxidx)
1632 return SP_FORMERROR;
1633 byts[idx++] = len;
1634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001635 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001636 for (i = 1; i <= len; ++i)
1637 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001638 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001639 if (c < 0)
1640 return SP_TRUNCERROR;
1641 if (c <= BY_SPECIAL)
1642 {
1643 if (c == BY_NOFLAGS && !prefixtree)
1644 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001645 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001646 idxs[idx] = 0;
1647 c = 0;
1648 }
1649 else if (c != BY_INDEX)
1650 {
1651 if (prefixtree)
1652 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001653 // Read the optional pflags byte, the prefix ID and the
1654 // condition nr. In idxs[] store the prefix ID in the low
1655 // byte, the condition index shifted up 8 bits, the flags
1656 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001657 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001658 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001659 else
1660 c = 0;
1661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001662 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001664 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001665 if (n >= maxprefcondnr)
1666 return SP_FORMERROR;
1667 c |= (n << 8);
1668 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001669 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001671 // Read flags and optional region and prefix ID. In
1672 // idxs[] the flags go in the low two bytes, region above
1673 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001674 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001675 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001676 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001677 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001678 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001679 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001680 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001681 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001682 }
1683
1684 idxs[idx] = c;
1685 c = 0;
1686 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001687 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001690 n = get3c(fd);
1691 if (n < 0 || n >= maxidx)
1692 return SP_FORMERROR;
1693 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001694 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001695 }
1696 }
1697 byts[idx++] = c;
1698 }
1699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001700 // Recursively read the children for non-shared siblings.
1701 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1702 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001703 for (i = 1; i <= len; ++i)
1704 if (byts[startidx + i] != 0)
1705 {
1706 if (idxs[startidx + i] & SHARED_MASK)
1707 idxs[startidx + i] &= ~SHARED_MASK;
1708 else
1709 {
1710 idxs[startidx + i] = idx;
1711 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1712 prefixtree, maxprefcondnr);
1713 if (idx < 0)
1714 break;
1715 }
1716 }
1717
1718 return idx;
1719}
1720
1721/*
1722 * Reload the spell file "fname" if it's loaded.
1723 */
1724 static void
1725spell_reload_one(
1726 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001727 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001728{
1729 slang_T *slang;
1730 int didit = FALSE;
1731
1732 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
1733 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001734 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001735 {
1736 slang_clear(slang);
1737 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001738 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001739 slang_clear(slang);
1740 redraw_all_later(SOME_VALID);
1741 didit = TRUE;
1742 }
1743 }
1744
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001745 // When "zg" was used and the file wasn't loaded yet, should redo
1746 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001747 if (added_word && !didit)
1748 did_set_spelllang(curwin);
1749}
1750
1751
1752/*
1753 * Functions for ":mkspell".
1754 */
1755
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001756#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1757 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001758/*
1759 * Main structure to store the contents of a ".aff" file.
1760 */
1761typedef struct afffile_S
1762{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001763 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1764 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1765 unsigned af_rare; // RARE ID for rare word
1766 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1767 unsigned af_bad; // BAD ID for banned word
1768 unsigned af_needaffix; // NEEDAFFIX ID
1769 unsigned af_circumfix; // CIRCUMFIX ID
1770 unsigned af_needcomp; // NEEDCOMPOUND ID
1771 unsigned af_comproot; // COMPOUNDROOT ID
1772 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1773 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1774 unsigned af_nosuggest; // NOSUGGEST ID
1775 int af_pfxpostpone; // postpone prefixes without chop string and
1776 // without flags
1777 int af_ignoreextra; // IGNOREEXTRA present
1778 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1779 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1780 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001781} afffile_T;
1782
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001783#define AFT_CHAR 0 // flags are one character
1784#define AFT_LONG 1 // flags are two characters
1785#define AFT_CAPLONG 2 // flags are one or two characters
1786#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001787
1788typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001789// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001790struct affentry_S
1791{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792 affentry_T *ae_next; // next affix with same name/number
1793 char_u *ae_chop; // text to chop off basic word (can be NULL)
1794 char_u *ae_add; // text to add to basic word (can be NULL)
1795 char_u *ae_flags; // flags on the affix (can be NULL)
1796 char_u *ae_cond; // condition (NULL for ".")
1797 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1798 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1799 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001800};
1801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001802#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001804// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001805typedef struct affheader_S
1806{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001807 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1808 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1809 int ah_newID; // prefix ID after renumbering; 0 if not used
1810 int ah_combine; // suffix may combine with prefix
1811 int ah_follows; // another affix block should be following
1812 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001813} affheader_T;
1814
1815#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001817// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001818typedef struct compitem_S
1819{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001820 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1821 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1822 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001823} compitem_T;
1824
1825#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1826
1827/*
1828 * Structure that is used to store the items in the word tree. This avoids
1829 * the need to keep track of each allocated thing, everything is freed all at
1830 * once after ":mkspell" is done.
1831 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1832 * "sb_data" is correct for systems where pointers must be aligned on
1833 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1834 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001835#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001836typedef struct sblock_S sblock_T;
1837struct sblock_S
1838{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001839 int sb_used; // nr of bytes already in use
1840 sblock_T *sb_next; // next block in list
1841 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001842};
1843
1844/*
1845 * A node in the tree.
1846 */
1847typedef struct wordnode_S wordnode_T;
1848struct wordnode_S
1849{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001850 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001851 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001852 char_u hashkey[6]; // the hash key, only used while compressing
1853 int index; // index in written nodes (valid after first
1854 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001855 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001856 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001857 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001858 wordnode_T *next; // next node with same hash key
1859 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001860 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001861 wordnode_T *wn_child; // child (next byte in word)
1862 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1863 // always sorted)
1864 int wn_refs; // Nr. of references to this node. Only
1865 // relevant for first node in a list of
1866 // siblings, in following siblings it is
1867 // always one.
1868 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001869
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001870 // Info for when "wn_byte" is NUL.
1871 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1872 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1873 // "wn_region" the LSW of the wordnr.
1874 char_u wn_affixID; // supported/required prefix ID or 0
1875 short_u wn_flags; // WF_ flags
1876 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001877
1878#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001879 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001880#endif
1881};
1882
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001883#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001884
1885#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1886
1887/*
1888 * Info used while reading the spell files.
1889 */
1890typedef struct spellinfo_S
1891{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001892 wordnode_T *si_foldroot; // tree with case-folded words
1893 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001894
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001895 wordnode_T *si_keeproot; // tree with keep-case words
1896 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001897
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001898 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001899
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001900 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001902 sblock_T *si_blocks; // memory blocks used
1903 long si_blocks_cnt; // memory blocks allocated
1904 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001905
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001906 long si_compress_cnt; // words to add before lowering
1907 // compression limit
1908 wordnode_T *si_first_free; // List of nodes that have been freed during
1909 // compression, linked by "wn_child" field.
1910 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001911#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001912 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001913#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001914 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001916 int si_ascii; // handling only ASCII words
1917 int si_add; // addition file
1918 int si_clear_chartab; // when TRUE clear char tables
1919 int si_region; // region mask
1920 vimconv_T si_conv; // for conversion to 'encoding'
1921 int si_memtot; // runtime memory used
1922 int si_verbose; // verbose messages
1923 int si_msg_count; // number of words added since last message
1924 char_u *si_info; // info text chars or NULL
1925 int si_region_count; // number of regions supported (1 when there
1926 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001927 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 // region names; used only if
1929 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 garray_T si_rep; // list of fromto_T entries from REP lines
1932 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1933 garray_T si_sal; // list of fromto_T entries from SAL lines
1934 char_u *si_sofofr; // SOFOFROM text
1935 char_u *si_sofoto; // SOFOTO text
1936 int si_nosugfile; // NOSUGFILE item found
1937 int si_nosplitsugs; // NOSPLITSUGS item found
1938 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1939 int si_followup; // soundsalike: ?
1940 int si_collapse; // soundsalike: ?
1941 hashtab_T si_commonwords; // hashtable for common words
1942 time_t si_sugtime; // timestamp for .sug file
1943 int si_rem_accents; // soundsalike: remove accents
1944 garray_T si_map; // MAP info concatenated
1945 char_u *si_midword; // MIDWORD chars or NULL
1946 int si_compmax; // max nr of words for compounding
1947 int si_compminlen; // minimal length for compounding
1948 int si_compsylmax; // max nr of syllables for compounding
1949 int si_compoptions; // COMP_ flags
1950 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1951 // a string
1952 char_u *si_compflags; // flags used for compounding
1953 char_u si_nobreak; // NOBREAK
1954 char_u *si_syllable; // syllable string
1955 garray_T si_prefcond; // table with conditions for postponed
1956 // prefixes, each stored as a string
1957 int si_newprefID; // current value for ah_newID
1958 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001959} spellinfo_T;
1960
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001961static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1962static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1963static int spell_info_item(char_u *s);
1964static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1965static unsigned get_affitem(int flagtype, char_u **pp);
1966static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1967static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001968static void aff_check_number(int spinval, int affval, char *name);
1969static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1970static int str_equal(char_u *s1, char_u *s2);
1971static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1972static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001973static int get_affix_flags(afffile_T *affile, char_u *afflist);
1974static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1975static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1976static int store_aff_word(spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001977static void *getroom(spellinfo_T *spin, size_t len, int align);
1978static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001979static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
1980static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
1981static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001982static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
1983static void wordtree_compress(spellinfo_T *spin, wordnode_T *root);
1984static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot);
1985static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001986static void clear_node(wordnode_T *node);
1987static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001988static int sug_filltree(spellinfo_T *spin, slang_T *slang);
1989static int sug_maketable(spellinfo_T *spin);
1990static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
1991static int offset2bytes(int nr, char_u *buf);
1992static void sug_write(spellinfo_T *spin, char_u *fname);
1993static void spell_message(spellinfo_T *spin, char_u *str);
1994static void init_spellfile(void);
1995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001996// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
1997// but it must be negative to indicate the prefix tree to tree_add_word().
1998// Use a negative number with the lower 8 bits zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001999#define PFX_FLAGS -256
2000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002001// flags for "condit" argument of store_aff_word()
2002#define CONDIT_COMB 1 // affix must combine
2003#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2004#define CONDIT_SUF 4 // add a suffix for matching flags
2005#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002006
2007/*
2008 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
2009 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002010static long compress_start = 30000; // memory / SBLOCKSIZE
2011static long compress_inc = 100; // memory / SBLOCKSIZE
2012static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002013
2014/*
2015 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2016 * Sets "sps_flags".
2017 */
2018 int
2019spell_check_msm(void)
2020{
2021 char_u *p = p_msm;
2022 long start = 0;
2023 long incr = 0;
2024 long added = 0;
2025
2026 if (!VIM_ISDIGIT(*p))
2027 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002029 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2030 if (*p != ',')
2031 return FAIL;
2032 ++p;
2033 if (!VIM_ISDIGIT(*p))
2034 return FAIL;
2035 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2036 if (*p != ',')
2037 return FAIL;
2038 ++p;
2039 if (!VIM_ISDIGIT(*p))
2040 return FAIL;
2041 added = getdigits(&p) * 1024;
2042 if (*p != NUL)
2043 return FAIL;
2044
2045 if (start == 0 || incr == 0 || added == 0 || incr > start)
2046 return FAIL;
2047
2048 compress_start = start;
2049 compress_inc = incr;
2050 compress_added = added;
2051 return OK;
2052}
2053
2054#ifdef SPELL_PRINTTREE
2055/*
2056 * For debugging the tree code: print the current tree in a (more or less)
2057 * readable format, so that we can see what happens when adding a word and/or
2058 * compressing the tree.
2059 * Based on code from Olaf Seibert.
2060 */
2061#define PRINTLINESIZE 1000
2062#define PRINTWIDTH 6
2063
2064#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2065 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2066
2067static char line1[PRINTLINESIZE];
2068static char line2[PRINTLINESIZE];
2069static char line3[PRINTLINESIZE];
2070
2071 static void
2072spell_clear_flags(wordnode_T *node)
2073{
2074 wordnode_T *np;
2075
2076 for (np = node; np != NULL; np = np->wn_sibling)
2077 {
2078 np->wn_u1.index = FALSE;
2079 spell_clear_flags(np->wn_child);
2080 }
2081}
2082
2083 static void
2084spell_print_node(wordnode_T *node, int depth)
2085{
2086 if (node->wn_u1.index)
2087 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002088 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002089 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2090 PRINTSOME(line2, depth, " ", 0, 0);
2091 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002092 msg(line1);
2093 msg(line2);
2094 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002095 }
2096 else
2097 {
2098 node->wn_u1.index = TRUE;
2099
2100 if (node->wn_byte != NUL)
2101 {
2102 if (node->wn_child != NULL)
2103 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2104 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002105 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002106 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2107 }
2108 else
2109 PRINTSOME(line1, depth, " $ ", 0, 0);
2110
2111 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2112
2113 if (node->wn_sibling != NULL)
2114 PRINTSOME(line3, depth, " | ", 0, 0);
2115 else
2116 PRINTSOME(line3, depth, " ", 0, 0);
2117
2118 if (node->wn_byte == NUL)
2119 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002120 msg(line1);
2121 msg(line2);
2122 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002123 }
2124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002126 if (node->wn_byte != NUL && node->wn_child != NULL)
2127 spell_print_node(node->wn_child, depth + 1);
2128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002129 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002130 if (node->wn_sibling != NULL)
2131 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002133 STRCPY(line1, line3);
2134 STRCPY(line2, line3);
2135 spell_print_node(node->wn_sibling, depth);
2136 }
2137 }
2138}
2139
2140 static void
2141spell_print_tree(wordnode_T *root)
2142{
2143 if (root != NULL)
2144 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002145 // Clear the "wn_u1.index" fields, used to remember what has been
2146 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002147 spell_clear_flags(root);
2148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002149 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002150 spell_print_node(root, 0);
2151 }
2152}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002153#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002154
2155/*
2156 * Read the affix file "fname".
2157 * Returns an afffile_T, NULL for complete failure.
2158 */
2159 static afffile_T *
2160spell_read_aff(spellinfo_T *spin, char_u *fname)
2161{
2162 FILE *fd;
2163 afffile_T *aff;
2164 char_u rline[MAXLINELEN];
2165 char_u *line;
2166 char_u *pc = NULL;
2167#define MAXITEMCNT 30
2168 char_u *(items[MAXITEMCNT]);
2169 int itemcnt;
2170 char_u *p;
2171 int lnum = 0;
2172 affheader_T *cur_aff = NULL;
2173 int did_postpone_prefix = FALSE;
2174 int aff_todo = 0;
2175 hashtab_T *tp;
2176 char_u *low = NULL;
2177 char_u *fol = NULL;
2178 char_u *upp = NULL;
2179 int do_rep;
2180 int do_repsal;
2181 int do_sal;
2182 int do_mapline;
2183 int found_map = FALSE;
2184 hashitem_T *hi;
2185 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002186 int compminlen = 0; // COMPOUNDMIN value
2187 int compsylmax = 0; // COMPOUNDSYLMAX value
2188 int compoptions = 0; // COMP_ flags
2189 int compmax = 0; // COMPOUNDWORDMAX value
2190 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2191 // concatenated
2192 char_u *midword = NULL; // MIDWORD value
2193 char_u *syllable = NULL; // SYLLABLE value
2194 char_u *sofofrom = NULL; // SOFOFROM value
2195 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002196
2197 /*
2198 * Open the file.
2199 */
2200 fd = mch_fopen((char *)fname, "r");
2201 if (fd == NULL)
2202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002203 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002204 return NULL;
2205 }
2206
Bram Moolenaarc1669272018-06-19 14:23:53 +02002207 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002208 spell_message(spin, IObuff);
2209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002210 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002211 do_rep = spin->si_rep.ga_len == 0;
2212
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002214 do_repsal = spin->si_repsal.ga_len == 0;
2215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002217 do_sal = spin->si_sal.ga_len == 0;
2218
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002219 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002220 do_mapline = spin->si_map.ga_len == 0;
2221
2222 /*
2223 * Allocate and init the afffile_T structure.
2224 */
2225 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2226 if (aff == NULL)
2227 {
2228 fclose(fd);
2229 return NULL;
2230 }
2231 hash_init(&aff->af_pref);
2232 hash_init(&aff->af_suff);
2233 hash_init(&aff->af_comp);
2234
2235 /*
2236 * Read all the lines in the file one by one.
2237 */
2238 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2239 {
2240 line_breakcheck();
2241 ++lnum;
2242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002243 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002244 if (*rline == '#')
2245 continue;
2246
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002248 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002249 if (spin->si_conv.vc_type != CONV_NONE)
2250 {
2251 pc = string_convert(&spin->si_conv, rline, NULL);
2252 if (pc == NULL)
2253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002254 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002255 fname, lnum, rline);
2256 continue;
2257 }
2258 line = pc;
2259 }
2260 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002261 {
2262 pc = NULL;
2263 line = rline;
2264 }
2265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002266 // Split the line up in white separated items. Put a NUL after each
2267 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002268 itemcnt = 0;
2269 for (p = line; ; )
2270 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002271 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002272 ++p;
2273 if (*p == NUL)
2274 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002275 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002276 break;
2277 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002278 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002279 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002280 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002281 ++p;
2282 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002283 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002284 ++p;
2285 if (*p == NUL)
2286 break;
2287 *p++ = NUL;
2288 }
2289
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002290 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002291 if (itemcnt > 0)
2292 {
2293 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2294 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002296 aff->af_enc = enc_canonize(items[1]);
2297 if (aff->af_enc != NULL && !spin->si_ascii
2298 && convert_setup(&spin->si_conv, aff->af_enc,
2299 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002300 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002301 fname, aff->af_enc, p_enc);
2302 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002303 }
2304 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2305 && aff->af_flagtype == AFT_CHAR)
2306 {
2307 if (STRCMP(items[1], "long") == 0)
2308 aff->af_flagtype = AFT_LONG;
2309 else if (STRCMP(items[1], "num") == 0)
2310 aff->af_flagtype = AFT_NUM;
2311 else if (STRCMP(items[1], "caplong") == 0)
2312 aff->af_flagtype = AFT_CAPLONG;
2313 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002314 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002315 fname, lnum, items[1]);
2316 if (aff->af_rare != 0
2317 || aff->af_keepcase != 0
2318 || aff->af_bad != 0
2319 || aff->af_needaffix != 0
2320 || aff->af_circumfix != 0
2321 || aff->af_needcomp != 0
2322 || aff->af_comproot != 0
2323 || aff->af_nosuggest != 0
2324 || compflags != NULL
2325 || aff->af_suff.ht_used > 0
2326 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002327 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002328 fname, lnum, items[1]);
2329 }
2330 else if (spell_info_item(items[0]))
2331 {
2332 p = (char_u *)getroom(spin,
2333 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2334 + STRLEN(items[0])
2335 + STRLEN(items[1]) + 3, FALSE);
2336 if (p != NULL)
2337 {
2338 if (spin->si_info != NULL)
2339 {
2340 STRCPY(p, spin->si_info);
2341 STRCAT(p, "\n");
2342 }
2343 STRCAT(p, items[0]);
2344 STRCAT(p, " ");
2345 STRCAT(p, items[1]);
2346 spin->si_info = p;
2347 }
2348 }
2349 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2350 && midword == NULL)
2351 {
2352 midword = getroom_save(spin, items[1]);
2353 }
2354 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2355 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002356 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002357 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002358 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002359 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2360 || is_aff_rule(items, itemcnt, "RARE", 2))
2361 && aff->af_rare == 0)
2362 {
2363 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2364 fname, lnum);
2365 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002366 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002367 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2368 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2369 && aff->af_keepcase == 0)
2370 {
2371 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2372 fname, lnum);
2373 }
2374 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2375 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2376 && aff->af_bad == 0)
2377 {
2378 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2379 fname, lnum);
2380 }
2381 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2382 && aff->af_needaffix == 0)
2383 {
2384 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2385 fname, lnum);
2386 }
2387 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2388 && aff->af_circumfix == 0)
2389 {
2390 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2391 fname, lnum);
2392 }
2393 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2394 && aff->af_nosuggest == 0)
2395 {
2396 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2397 fname, lnum);
2398 }
2399 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2400 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2401 && aff->af_needcomp == 0)
2402 {
2403 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2404 fname, lnum);
2405 }
2406 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2407 && aff->af_comproot == 0)
2408 {
2409 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2410 fname, lnum);
2411 }
2412 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2413 && aff->af_compforbid == 0)
2414 {
2415 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2416 fname, lnum);
2417 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002418 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002419 fname, lnum);
2420 }
2421 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2422 && aff->af_comppermit == 0)
2423 {
2424 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2425 fname, lnum);
2426 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002427 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002428 fname, lnum);
2429 }
2430 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2431 && compflags == NULL)
2432 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002433 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2434 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002435 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2436 if (p != NULL)
2437 {
2438 STRCPY(p, items[1]);
2439 STRCAT(p, "+");
2440 compflags = p;
2441 }
2442 }
2443 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2444 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002445 // We don't use the count, but do check that it's a number and
2446 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002447 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002448 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002449 fname, lnum, items[1]);
2450 }
2451 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2452 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002453 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002454 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2455 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002456 // Concatenate this string to previously defined ones,
2457 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002458 l = (int)STRLEN(items[1]) + 1;
2459 if (compflags != NULL)
2460 l += (int)STRLEN(compflags) + 1;
2461 p = getroom(spin, l, FALSE);
2462 if (p != NULL)
2463 {
2464 if (compflags != NULL)
2465 {
2466 STRCPY(p, compflags);
2467 STRCAT(p, "/");
2468 }
2469 STRCAT(p, items[1]);
2470 compflags = p;
2471 }
2472 }
2473 }
2474 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2475 && compmax == 0)
2476 {
2477 compmax = atoi((char *)items[1]);
2478 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002480 fname, lnum, items[1]);
2481 }
2482 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2483 && compminlen == 0)
2484 {
2485 compminlen = atoi((char *)items[1]);
2486 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002487 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002488 fname, lnum, items[1]);
2489 }
2490 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2491 && compsylmax == 0)
2492 {
2493 compsylmax = atoi((char *)items[1]);
2494 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002495 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002496 fname, lnum, items[1]);
2497 }
2498 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2499 {
2500 compoptions |= COMP_CHECKDUP;
2501 }
2502 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2503 {
2504 compoptions |= COMP_CHECKREP;
2505 }
2506 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2507 {
2508 compoptions |= COMP_CHECKCASE;
2509 }
2510 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2511 {
2512 compoptions |= COMP_CHECKTRIPLE;
2513 }
2514 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2515 {
2516 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002518 fname, lnum, items[1]);
2519 }
2520 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2521 {
2522 garray_T *gap = &spin->si_comppat;
2523 int i;
2524
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002525 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002526 for (i = 0; i < gap->ga_len - 1; i += 2)
2527 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2528 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2529 items[2]) == 0)
2530 break;
2531 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2532 {
2533 ((char_u **)(gap->ga_data))[gap->ga_len++]
2534 = getroom_save(spin, items[1]);
2535 ((char_u **)(gap->ga_data))[gap->ga_len++]
2536 = getroom_save(spin, items[2]);
2537 }
2538 }
2539 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2540 && syllable == NULL)
2541 {
2542 syllable = getroom_save(spin, items[1]);
2543 }
2544 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2545 {
2546 spin->si_nobreak = TRUE;
2547 }
2548 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2549 {
2550 spin->si_nosplitsugs = TRUE;
2551 }
2552 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2553 {
2554 spin->si_nocompoundsugs = TRUE;
2555 }
2556 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2557 {
2558 spin->si_nosugfile = TRUE;
2559 }
2560 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2561 {
2562 aff->af_pfxpostpone = TRUE;
2563 }
2564 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2565 {
2566 aff->af_ignoreextra = TRUE;
2567 }
2568 else if ((STRCMP(items[0], "PFX") == 0
2569 || STRCMP(items[0], "SFX") == 0)
2570 && aff_todo == 0
2571 && itemcnt >= 4)
2572 {
2573 int lasti = 4;
2574 char_u key[AH_KEY_LEN];
2575
2576 if (*items[0] == 'P')
2577 tp = &aff->af_pref;
2578 else
2579 tp = &aff->af_suff;
2580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002581 // Myspell allows the same affix name to be used multiple
2582 // times. The affix files that do this have an undocumented
2583 // "S" flag on all but the last block, thus we check for that
2584 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002585 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2586 hi = hash_find(tp, key);
2587 if (!HASHITEM_EMPTY(hi))
2588 {
2589 cur_aff = HI2AH(hi);
2590 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002591 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002592 fname, lnum, items[1]);
2593 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002594 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002595 fname, lnum, items[1]);
2596 }
2597 else
2598 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002599 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002600 cur_aff = (affheader_T *)getroom(spin,
2601 sizeof(affheader_T), TRUE);
2602 if (cur_aff == NULL)
2603 break;
2604 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2605 fname, lnum);
2606 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2607 break;
2608 if (cur_aff->ah_flag == aff->af_bad
2609 || cur_aff->ah_flag == aff->af_rare
2610 || cur_aff->ah_flag == aff->af_keepcase
2611 || cur_aff->ah_flag == aff->af_needaffix
2612 || cur_aff->ah_flag == aff->af_circumfix
2613 || cur_aff->ah_flag == aff->af_nosuggest
2614 || cur_aff->ah_flag == aff->af_needcomp
2615 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002617 fname, lnum, items[1]);
2618 STRCPY(cur_aff->ah_key, items[1]);
2619 hash_add(tp, cur_aff->ah_key);
2620
2621 cur_aff->ah_combine = (*items[2] == 'Y');
2622 }
2623
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002624 // Check for the "S" flag, which apparently means that another
2625 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002626 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2627 {
2628 ++lasti;
2629 cur_aff->ah_follows = TRUE;
2630 }
2631 else
2632 cur_aff->ah_follows = FALSE;
2633
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002634 // Myspell allows extra text after the item, but that might
2635 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002636 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002637 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002638
2639 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002640 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002641 fname, lnum, items[2]);
2642
2643 if (*items[0] == 'P' && aff->af_pfxpostpone)
2644 {
2645 if (cur_aff->ah_newID == 0)
2646 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002647 // Use a new number in the .spl file later, to be able
2648 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002649 check_renumber(spin);
2650 cur_aff->ah_newID = ++spin->si_newprefID;
2651
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002652 // We only really use ah_newID if the prefix is
2653 // postponed. We know that only after handling all
2654 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002655 did_postpone_prefix = FALSE;
2656 }
2657 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002658 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002659 did_postpone_prefix = TRUE;
2660 }
2661
2662 aff_todo = atoi((char *)items[3]);
2663 }
2664 else if ((STRCMP(items[0], "PFX") == 0
2665 || STRCMP(items[0], "SFX") == 0)
2666 && aff_todo > 0
2667 && STRCMP(cur_aff->ah_key, items[1]) == 0
2668 && itemcnt >= 5)
2669 {
2670 affentry_T *aff_entry;
2671 int upper = FALSE;
2672 int lasti = 5;
2673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002674 // Myspell allows extra text after the item, but that might
2675 // mean mistakes go unnoticed. Require a comment-starter,
2676 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002677 if (itemcnt > lasti
2678 && !aff->af_ignoreextra
2679 && *items[lasti] != '#'
2680 && (STRCMP(items[lasti], "-") != 0
2681 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002682 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002683
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002684 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002685 --aff_todo;
2686 aff_entry = (affentry_T *)getroom(spin,
2687 sizeof(affentry_T), TRUE);
2688 if (aff_entry == NULL)
2689 break;
2690
2691 if (STRCMP(items[2], "0") != 0)
2692 aff_entry->ae_chop = getroom_save(spin, items[2]);
2693 if (STRCMP(items[3], "0") != 0)
2694 {
2695 aff_entry->ae_add = getroom_save(spin, items[3]);
2696
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002698 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2699 if (aff_entry->ae_flags != NULL)
2700 {
2701 *aff_entry->ae_flags++ = NUL;
2702 aff_process_flags(aff, aff_entry);
2703 }
2704 }
2705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002706 // Don't use an affix entry with non-ASCII characters when
2707 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002708 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2709 || has_non_ascii(aff_entry->ae_add)))
2710 {
2711 aff_entry->ae_next = cur_aff->ah_first;
2712 cur_aff->ah_first = aff_entry;
2713
2714 if (STRCMP(items[4], ".") != 0)
2715 {
2716 char_u buf[MAXLINELEN];
2717
2718 aff_entry->ae_cond = getroom_save(spin, items[4]);
2719 if (*items[0] == 'P')
2720 sprintf((char *)buf, "^%s", items[4]);
2721 else
2722 sprintf((char *)buf, "%s$", items[4]);
2723 aff_entry->ae_prog = vim_regcomp(buf,
2724 RE_MAGIC + RE_STRING + RE_STRICT);
2725 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002726 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002727 fname, lnum, items[4]);
2728 }
2729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002730 // For postponed prefixes we need an entry in si_prefcond
2731 // for the condition. Use an existing one if possible.
2732 // Can't be done for an affix with flags, ignoring
2733 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002734 if (*items[0] == 'P' && aff->af_pfxpostpone
2735 && aff_entry->ae_flags == NULL)
2736 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002737 // When the chop string is one lower-case letter and
2738 // the add string ends in the upper-case letter we set
2739 // the "upper" flag, clear "ae_chop" and remove the
2740 // letters from "ae_add". The condition must either
2741 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002742 if (aff_entry->ae_chop != NULL
2743 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002744 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002745 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002746 {
2747 int c, c_up;
2748
2749 c = PTR2CHAR(aff_entry->ae_chop);
2750 c_up = SPELL_TOUPPER(c);
2751 if (c_up != c
2752 && (aff_entry->ae_cond == NULL
2753 || PTR2CHAR(aff_entry->ae_cond) == c))
2754 {
2755 p = aff_entry->ae_add
2756 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002757 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002758 if (PTR2CHAR(p) == c_up)
2759 {
2760 upper = TRUE;
2761 aff_entry->ae_chop = NULL;
2762 *p = NUL;
2763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002764 // The condition is matched with the
2765 // actual word, thus must check for the
2766 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002767 if (aff_entry->ae_cond != NULL)
2768 {
2769 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002770
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002771 if (has_mbyte)
2772 {
2773 onecap_copy(items[4], buf, TRUE);
2774 aff_entry->ae_cond = getroom_save(
2775 spin, buf);
2776 }
2777 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002778 *aff_entry->ae_cond = c_up;
2779 if (aff_entry->ae_cond != NULL)
2780 {
2781 sprintf((char *)buf, "^%s",
2782 aff_entry->ae_cond);
2783 vim_regfree(aff_entry->ae_prog);
2784 aff_entry->ae_prog = vim_regcomp(
2785 buf, RE_MAGIC + RE_STRING);
2786 }
2787 }
2788 }
2789 }
2790 }
2791
2792 if (aff_entry->ae_chop == NULL
2793 && aff_entry->ae_flags == NULL)
2794 {
2795 int idx;
2796 char_u **pp;
2797 int n;
2798
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002799 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002800 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2801 --idx)
2802 {
2803 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2804 if (str_equal(p, aff_entry->ae_cond))
2805 break;
2806 }
2807 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2808 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002810 idx = spin->si_prefcond.ga_len++;
2811 pp = ((char_u **)spin->si_prefcond.ga_data)
2812 + idx;
2813 if (aff_entry->ae_cond == NULL)
2814 *pp = NULL;
2815 else
2816 *pp = getroom_save(spin,
2817 aff_entry->ae_cond);
2818 }
2819
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002820 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002821 if (aff_entry->ae_add == NULL)
2822 p = (char_u *)"";
2823 else
2824 p = aff_entry->ae_add;
2825
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002826 // PFX_FLAGS is a negative number, so that
2827 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002828 n = PFX_FLAGS;
2829 if (!cur_aff->ah_combine)
2830 n |= WFP_NC;
2831 if (upper)
2832 n |= WFP_UP;
2833 if (aff_entry->ae_comppermit)
2834 n |= WFP_COMPPERMIT;
2835 if (aff_entry->ae_compforbid)
2836 n |= WFP_COMPFORBID;
2837 tree_add_word(spin, p, spin->si_prefroot, n,
2838 idx, cur_aff->ah_newID);
2839 did_postpone_prefix = TRUE;
2840 }
2841
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002842 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002843 if (aff_todo == 0 && !did_postpone_prefix)
2844 {
2845 --spin->si_newprefID;
2846 cur_aff->ah_newID = 0;
2847 }
2848 }
2849 }
2850 }
2851 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2852 {
2853 fol = vim_strsave(items[1]);
2854 }
2855 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2856 {
2857 low = vim_strsave(items[1]);
2858 }
2859 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2860 {
2861 upp = vim_strsave(items[1]);
2862 }
2863 else if (is_aff_rule(items, itemcnt, "REP", 2)
2864 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2865 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002866 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002867 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002868 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002869 fname, lnum);
2870 }
2871 else if ((STRCMP(items[0], "REP") == 0
2872 || STRCMP(items[0], "REPSAL") == 0)
2873 && itemcnt >= 3)
2874 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002875 // REP/REPSAL item
2876 // Myspell ignores extra arguments, we require it starts with
2877 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002878 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002879 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002880 if (items[0][3] == 'S' ? do_repsal : do_rep)
2881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002882 // Replace underscore with space (can't include a space
2883 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002884 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002885 if (*p == '_')
2886 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002887 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002888 if (*p == '_')
2889 *p = ' ';
2890 add_fromto(spin, items[0][3] == 'S'
2891 ? &spin->si_repsal
2892 : &spin->si_rep, items[1], items[2]);
2893 }
2894 }
2895 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2896 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002897 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002898 if (!found_map)
2899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002901 found_map = TRUE;
2902 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002903 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002904 fname, lnum);
2905 }
2906 else if (do_mapline)
2907 {
2908 int c;
2909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002910 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002911 for (p = items[1]; *p != NUL; )
2912 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002913 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002914 if ((spin->si_map.ga_len > 0
2915 && vim_strchr(spin->si_map.ga_data, c)
2916 != NULL)
2917 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002918 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002919 fname, lnum);
2920 }
2921
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002922 // We simply concatenate all the MAP strings, separated by
2923 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002924 ga_concat(&spin->si_map, items[1]);
2925 ga_append(&spin->si_map, '/');
2926 }
2927 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002928 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002929 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2930 {
2931 if (do_sal)
2932 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002933 // SAL item (sounds-a-like)
2934 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002935 if (STRCMP(items[1], "followup") == 0)
2936 spin->si_followup = sal_to_bool(items[2]);
2937 else if (STRCMP(items[1], "collapse_result") == 0)
2938 spin->si_collapse = sal_to_bool(items[2]);
2939 else if (STRCMP(items[1], "remove_accents") == 0)
2940 spin->si_rem_accents = sal_to_bool(items[2]);
2941 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002942 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002943 add_fromto(spin, &spin->si_sal, items[1],
2944 STRCMP(items[2], "_") == 0 ? (char_u *)""
2945 : items[2]);
2946 }
2947 }
2948 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2949 && sofofrom == NULL)
2950 {
2951 sofofrom = getroom_save(spin, items[1]);
2952 }
2953 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2954 && sofoto == NULL)
2955 {
2956 sofoto = getroom_save(spin, items[1]);
2957 }
2958 else if (STRCMP(items[0], "COMMON") == 0)
2959 {
2960 int i;
2961
2962 for (i = 1; i < itemcnt; ++i)
2963 {
2964 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2965 items[i])))
2966 {
2967 p = vim_strsave(items[i]);
2968 if (p == NULL)
2969 break;
2970 hash_add(&spin->si_commonwords, p);
2971 }
2972 }
2973 }
2974 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002975 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002976 fname, lnum, items[0]);
2977 }
2978 }
2979
2980 if (fol != NULL || low != NULL || upp != NULL)
2981 {
2982 if (spin->si_clear_chartab)
2983 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002984 // Clear the char type tables, don't want to use any of the
2985 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002986 init_spell_chartab();
2987 spin->si_clear_chartab = FALSE;
2988 }
2989
2990 /*
2991 * Don't write a word table for an ASCII file, so that we don't check
2992 * for conflicts with a word table that matches 'encoding'.
2993 * Don't write one for utf-8 either, we use utf_*() and
2994 * mb_get_class(), the list of chars in the file will be incomplete.
2995 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002996 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002997 {
2998 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002999 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003000 else
3001 (void)set_spell_chartab(fol, low, upp);
3002 }
3003
3004 vim_free(fol);
3005 vim_free(low);
3006 vim_free(upp);
3007 }
3008
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003009 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003010 if (compmax != 0)
3011 {
3012 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3013 spin->si_compmax = compmax;
3014 }
3015
3016 if (compminlen != 0)
3017 {
3018 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3019 spin->si_compminlen = compminlen;
3020 }
3021
3022 if (compsylmax != 0)
3023 {
3024 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003026 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3027 spin->si_compsylmax = compsylmax;
3028 }
3029
3030 if (compoptions != 0)
3031 {
3032 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3033 spin->si_compoptions |= compoptions;
3034 }
3035
3036 if (compflags != NULL)
3037 process_compflags(spin, aff, compflags);
3038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003039 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003040 if (spin->si_newcompID < spin->si_newprefID)
3041 {
3042 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003043 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003044 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003045 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003046 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003047 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003048 }
3049
3050 if (syllable != NULL)
3051 {
3052 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3053 spin->si_syllable = syllable;
3054 }
3055
3056 if (sofofrom != NULL || sofoto != NULL)
3057 {
3058 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003059 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003060 sofofrom == NULL ? "FROM" : "TO", fname);
3061 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003063 else
3064 {
3065 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3066 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3067 spin->si_sofofr = sofofrom;
3068 spin->si_sofoto = sofoto;
3069 }
3070 }
3071
3072 if (midword != NULL)
3073 {
3074 aff_check_string(spin->si_midword, midword, "MIDWORD");
3075 spin->si_midword = midword;
3076 }
3077
3078 vim_free(pc);
3079 fclose(fd);
3080 return aff;
3081}
3082
3083/*
3084 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3085 * a comment is following after item "mincount".
3086 */
3087 static int
3088is_aff_rule(
3089 char_u **items,
3090 int itemcnt,
3091 char *rulename,
3092 int mincount)
3093{
3094 return (STRCMP(items[0], rulename) == 0
3095 && (itemcnt == mincount
3096 || (itemcnt > mincount && items[mincount][0] == '#')));
3097}
3098
3099/*
3100 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3101 * ae_flags to ae_comppermit and ae_compforbid.
3102 */
3103 static void
3104aff_process_flags(afffile_T *affile, affentry_T *entry)
3105{
3106 char_u *p;
3107 char_u *prevp;
3108 unsigned flag;
3109
3110 if (entry->ae_flags != NULL
3111 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3112 {
3113 for (p = entry->ae_flags; *p != NUL; )
3114 {
3115 prevp = p;
3116 flag = get_affitem(affile->af_flagtype, &p);
3117 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3118 {
3119 STRMOVE(prevp, p);
3120 p = prevp;
3121 if (flag == affile->af_comppermit)
3122 entry->ae_comppermit = TRUE;
3123 else
3124 entry->ae_compforbid = TRUE;
3125 }
3126 if (affile->af_flagtype == AFT_NUM && *p == ',')
3127 ++p;
3128 }
3129 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003130 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003131 }
3132}
3133
3134/*
3135 * Return TRUE if "s" is the name of an info item in the affix file.
3136 */
3137 static int
3138spell_info_item(char_u *s)
3139{
3140 return STRCMP(s, "NAME") == 0
3141 || STRCMP(s, "HOME") == 0
3142 || STRCMP(s, "VERSION") == 0
3143 || STRCMP(s, "AUTHOR") == 0
3144 || STRCMP(s, "EMAIL") == 0
3145 || STRCMP(s, "COPYRIGHT") == 0;
3146}
3147
3148/*
3149 * Turn an affix flag name into a number, according to the FLAG type.
3150 * returns zero for failure.
3151 */
3152 static unsigned
3153affitem2flag(
3154 int flagtype,
3155 char_u *item,
3156 char_u *fname,
3157 int lnum)
3158{
3159 unsigned res;
3160 char_u *p = item;
3161
3162 res = get_affitem(flagtype, &p);
3163 if (res == 0)
3164 {
3165 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003166 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003167 fname, lnum, item);
3168 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003169 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003170 fname, lnum, item);
3171 }
3172 if (*p != NUL)
3173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003174 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003175 return 0;
3176 }
3177
3178 return res;
3179}
3180
3181/*
3182 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003183 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003184 * Returns zero for an error, still advances the pointer then.
3185 */
3186 static unsigned
3187get_affitem(int flagtype, char_u **pp)
3188{
3189 int res;
3190
3191 if (flagtype == AFT_NUM)
3192 {
3193 if (!VIM_ISDIGIT(**pp))
3194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003195 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003196 return 0;
3197 }
3198 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003199 if (res == 0)
3200 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003201 }
3202 else
3203 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003204 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003205 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3206 && res >= 'A' && res <= 'Z'))
3207 {
3208 if (**pp == NUL)
3209 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003210 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003211 }
3212 }
3213 return res;
3214}
3215
3216/*
3217 * Process the "compflags" string used in an affix file and append it to
3218 * spin->si_compflags.
3219 * The processing involves changing the affix names to ID numbers, so that
3220 * they fit in one byte.
3221 */
3222 static void
3223process_compflags(
3224 spellinfo_T *spin,
3225 afffile_T *aff,
3226 char_u *compflags)
3227{
3228 char_u *p;
3229 char_u *prevp;
3230 unsigned flag;
3231 compitem_T *ci;
3232 int id;
3233 int len;
3234 char_u *tp;
3235 char_u key[AH_KEY_LEN];
3236 hashitem_T *hi;
3237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003238 // Make room for the old and the new compflags, concatenated with a / in
3239 // between. Processing it makes it shorter, but we don't know by how
3240 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003241 len = (int)STRLEN(compflags) + 1;
3242 if (spin->si_compflags != NULL)
3243 len += (int)STRLEN(spin->si_compflags) + 1;
3244 p = getroom(spin, len, FALSE);
3245 if (p == NULL)
3246 return;
3247 if (spin->si_compflags != NULL)
3248 {
3249 STRCPY(p, spin->si_compflags);
3250 STRCAT(p, "/");
3251 }
3252 spin->si_compflags = p;
3253 tp = p + STRLEN(p);
3254
3255 for (p = compflags; *p != NUL; )
3256 {
3257 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003258 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003259 *tp++ = *p++;
3260 else
3261 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003262 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003263 prevp = p;
3264 flag = get_affitem(aff->af_flagtype, &p);
3265 if (flag != 0)
3266 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003267 // Find the flag in the hashtable. If it was used before, use
3268 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003269 vim_strncpy(key, prevp, p - prevp);
3270 hi = hash_find(&aff->af_comp, key);
3271 if (!HASHITEM_EMPTY(hi))
3272 id = HI2CI(hi)->ci_newID;
3273 else
3274 {
3275 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3276 if (ci == NULL)
3277 break;
3278 STRCPY(ci->ci_key, key);
3279 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003280 // Avoid using a flag ID that has a special meaning in a
3281 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003282 do
3283 {
3284 check_renumber(spin);
3285 id = spin->si_newcompID--;
3286 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3287 ci->ci_newID = id;
3288 hash_add(&aff->af_comp, ci->ci_key);
3289 }
3290 *tp++ = id;
3291 }
3292 if (aff->af_flagtype == AFT_NUM && *p == ',')
3293 ++p;
3294 }
3295 }
3296
3297 *tp = NUL;
3298}
3299
3300/*
3301 * Check that the new IDs for postponed affixes and compounding don't overrun
3302 * each other. We have almost 255 available, but start at 0-127 to avoid
3303 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3304 * When that is used up an error message is given.
3305 */
3306 static void
3307check_renumber(spellinfo_T *spin)
3308{
3309 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3310 {
3311 spin->si_newprefID = 127;
3312 spin->si_newcompID = 255;
3313 }
3314}
3315
3316/*
3317 * Return TRUE if flag "flag" appears in affix list "afflist".
3318 */
3319 static int
3320flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3321{
3322 char_u *p;
3323 unsigned n;
3324
3325 switch (flagtype)
3326 {
3327 case AFT_CHAR:
3328 return vim_strchr(afflist, flag) != NULL;
3329
3330 case AFT_CAPLONG:
3331 case AFT_LONG:
3332 for (p = afflist; *p != NUL; )
3333 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003334 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003335 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3336 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003337 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003338 if (n == flag)
3339 return TRUE;
3340 }
3341 break;
3342
3343 case AFT_NUM:
3344 for (p = afflist; *p != NUL; )
3345 {
3346 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003347 if (n == 0)
3348 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003349 if (n == flag)
3350 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003351 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003352 ++p;
3353 }
3354 break;
3355 }
3356 return FALSE;
3357}
3358
3359/*
3360 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3361 */
3362 static void
3363aff_check_number(int spinval, int affval, char *name)
3364{
3365 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003366 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003367}
3368
3369/*
3370 * Give a warning when "spinval" and "affval" strings are set and not the same.
3371 */
3372 static void
3373aff_check_string(char_u *spinval, char_u *affval, char *name)
3374{
3375 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003376 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003377}
3378
3379/*
3380 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3381 * NULL as equal.
3382 */
3383 static int
3384str_equal(char_u *s1, char_u *s2)
3385{
3386 if (s1 == NULL || s2 == NULL)
3387 return s1 == s2;
3388 return STRCMP(s1, s2) == 0;
3389}
3390
3391/*
3392 * Add a from-to item to "gap". Used for REP and SAL items.
3393 * They are stored case-folded.
3394 */
3395 static void
3396add_fromto(
3397 spellinfo_T *spin,
3398 garray_T *gap,
3399 char_u *from,
3400 char_u *to)
3401{
3402 fromto_T *ftp;
3403 char_u word[MAXWLEN];
3404
3405 if (ga_grow(gap, 1) == OK)
3406 {
3407 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3408 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
3409 ftp->ft_from = getroom_save(spin, word);
3410 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
3411 ftp->ft_to = getroom_save(spin, word);
3412 ++gap->ga_len;
3413 }
3414}
3415
3416/*
3417 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3418 */
3419 static int
3420sal_to_bool(char_u *s)
3421{
3422 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3423}
3424
3425/*
3426 * Free the structure filled by spell_read_aff().
3427 */
3428 static void
3429spell_free_aff(afffile_T *aff)
3430{
3431 hashtab_T *ht;
3432 hashitem_T *hi;
3433 int todo;
3434 affheader_T *ah;
3435 affentry_T *ae;
3436
3437 vim_free(aff->af_enc);
3438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003439 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003440 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3441 {
3442 todo = (int)ht->ht_used;
3443 for (hi = ht->ht_array; todo > 0; ++hi)
3444 {
3445 if (!HASHITEM_EMPTY(hi))
3446 {
3447 --todo;
3448 ah = HI2AH(hi);
3449 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3450 vim_regfree(ae->ae_prog);
3451 }
3452 }
3453 if (ht == &aff->af_suff)
3454 break;
3455 }
3456
3457 hash_clear(&aff->af_pref);
3458 hash_clear(&aff->af_suff);
3459 hash_clear(&aff->af_comp);
3460}
3461
3462/*
3463 * Read dictionary file "fname".
3464 * Returns OK or FAIL;
3465 */
3466 static int
3467spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3468{
3469 hashtab_T ht;
3470 char_u line[MAXLINELEN];
3471 char_u *p;
3472 char_u *afflist;
3473 char_u store_afflist[MAXWLEN];
3474 int pfxlen;
3475 int need_affix;
3476 char_u *dw;
3477 char_u *pc;
3478 char_u *w;
3479 int l;
3480 hash_T hash;
3481 hashitem_T *hi;
3482 FILE *fd;
3483 int lnum = 1;
3484 int non_ascii = 0;
3485 int retval = OK;
3486 char_u message[MAXLINELEN + MAXWLEN];
3487 int flags;
3488 int duplicate = 0;
3489
3490 /*
3491 * Open the file.
3492 */
3493 fd = mch_fopen((char *)fname, "r");
3494 if (fd == NULL)
3495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003496 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003497 return FAIL;
3498 }
3499
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003500 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003501 hash_init(&ht);
3502
3503 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003504 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003505 spell_message(spin, IObuff);
3506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003507 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003508 spin->si_msg_count = 999999;
3509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003510 // Read and ignore the first line: word count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003511 (void)vim_fgets(line, MAXLINELEN, fd);
3512 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003513 semsg(_("E760: No word count in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003514
3515 /*
3516 * Read all the lines in the file one by one.
3517 * The words are converted to 'encoding' here, before being added to
3518 * the hashtable.
3519 */
3520 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3521 {
3522 line_breakcheck();
3523 ++lnum;
3524 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003525 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003527 // Remove CR, LF and white space from the end. White space halfway
3528 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003529 l = (int)STRLEN(line);
3530 while (l > 0 && line[l - 1] <= ' ')
3531 --l;
3532 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003533 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003534 line[l] = NUL;
3535
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003536 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003537 if (spin->si_conv.vc_type != CONV_NONE)
3538 {
3539 pc = string_convert(&spin->si_conv, line, NULL);
3540 if (pc == NULL)
3541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003542 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003543 fname, lnum, line);
3544 continue;
3545 }
3546 w = pc;
3547 }
3548 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003549 {
3550 pc = NULL;
3551 w = line;
3552 }
3553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003554 // Truncate the word at the "/", set "afflist" to what follows.
3555 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003556 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003557 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003558 {
3559 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3560 STRMOVE(p, p + 1);
3561 else if (*p == '/')
3562 {
3563 *p = NUL;
3564 afflist = p + 1;
3565 break;
3566 }
3567 }
3568
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003569 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003570 if (spin->si_ascii && has_non_ascii(w))
3571 {
3572 ++non_ascii;
3573 vim_free(pc);
3574 continue;
3575 }
3576
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003577 // This takes time, print a message every 10000 words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003578 if (spin->si_verbose && spin->si_msg_count > 10000)
3579 {
3580 spin->si_msg_count = 0;
3581 vim_snprintf((char *)message, sizeof(message),
Bram Moolenaarea391762018-04-08 13:07:22 +02003582 _("line %6d, word %6ld - %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003583 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3584 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003585 msg_outtrans_long_attr(message, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003586 msg_clr_eos();
3587 msg_didout = FALSE;
3588 msg_col = 0;
3589 out_flush();
3590 }
3591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003592 // Store the word in the hashtable to be able to find duplicates.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003593 dw = (char_u *)getroom_save(spin, w);
3594 if (dw == NULL)
3595 {
3596 retval = FAIL;
3597 vim_free(pc);
3598 break;
3599 }
3600
3601 hash = hash_hash(dw);
3602 hi = hash_lookup(&ht, dw, hash);
3603 if (!HASHITEM_EMPTY(hi))
3604 {
3605 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003606 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003607 fname, lnum, dw);
3608 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003609 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003610 fname, lnum, dw);
3611 ++duplicate;
3612 }
3613 else
3614 hash_add_item(&ht, hi, dw, hash);
3615
3616 flags = 0;
3617 store_afflist[0] = NUL;
3618 pfxlen = 0;
3619 need_affix = FALSE;
3620 if (afflist != NULL)
3621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003622 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003623 flags |= get_affix_flags(affile, afflist);
3624
3625 if (affile->af_needaffix != 0 && flag_in_afflist(
3626 affile->af_flagtype, afflist, affile->af_needaffix))
3627 need_affix = TRUE;
3628
3629 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003630 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003631 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3632
3633 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003634 // Need to store the list of compound flags with the word.
3635 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003636 get_compflags(affile, afflist, store_afflist + pfxlen);
3637 }
3638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003639 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003640 if (store_word(spin, dw, flags, spin->si_region,
3641 store_afflist, need_affix) == FAIL)
3642 retval = FAIL;
3643
3644 if (afflist != NULL)
3645 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003646 // Find all matching suffixes and add the resulting words.
3647 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003648 if (store_aff_word(spin, dw, afflist, affile,
3649 &affile->af_suff, &affile->af_pref,
3650 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3651 retval = FAIL;
3652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003653 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003654 if (store_aff_word(spin, dw, afflist, affile,
3655 &affile->af_pref, NULL,
3656 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3657 retval = FAIL;
3658 }
3659
3660 vim_free(pc);
3661 }
3662
3663 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003664 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003665 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003666 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003667 non_ascii, fname);
3668 hash_clear(&ht);
3669
3670 fclose(fd);
3671 return retval;
3672}
3673
3674/*
3675 * Check for affix flags in "afflist" that are turned into word flags.
3676 * Return WF_ flags.
3677 */
3678 static int
3679get_affix_flags(afffile_T *affile, char_u *afflist)
3680{
3681 int flags = 0;
3682
3683 if (affile->af_keepcase != 0 && flag_in_afflist(
3684 affile->af_flagtype, afflist, affile->af_keepcase))
3685 flags |= WF_KEEPCAP | WF_FIXCAP;
3686 if (affile->af_rare != 0 && flag_in_afflist(
3687 affile->af_flagtype, afflist, affile->af_rare))
3688 flags |= WF_RARE;
3689 if (affile->af_bad != 0 && flag_in_afflist(
3690 affile->af_flagtype, afflist, affile->af_bad))
3691 flags |= WF_BANNED;
3692 if (affile->af_needcomp != 0 && flag_in_afflist(
3693 affile->af_flagtype, afflist, affile->af_needcomp))
3694 flags |= WF_NEEDCOMP;
3695 if (affile->af_comproot != 0 && flag_in_afflist(
3696 affile->af_flagtype, afflist, affile->af_comproot))
3697 flags |= WF_COMPROOT;
3698 if (affile->af_nosuggest != 0 && flag_in_afflist(
3699 affile->af_flagtype, afflist, affile->af_nosuggest))
3700 flags |= WF_NOSUGGEST;
3701 return flags;
3702}
3703
3704/*
3705 * Get the list of prefix IDs from the affix list "afflist".
3706 * Used for PFXPOSTPONE.
3707 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3708 * and return the number of affixes.
3709 */
3710 static int
3711get_pfxlist(
3712 afffile_T *affile,
3713 char_u *afflist,
3714 char_u *store_afflist)
3715{
3716 char_u *p;
3717 char_u *prevp;
3718 int cnt = 0;
3719 int id;
3720 char_u key[AH_KEY_LEN];
3721 hashitem_T *hi;
3722
3723 for (p = afflist; *p != NUL; )
3724 {
3725 prevp = p;
3726 if (get_affitem(affile->af_flagtype, &p) != 0)
3727 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003728 // A flag is a postponed prefix flag if it appears in "af_pref"
3729 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003730 vim_strncpy(key, prevp, p - prevp);
3731 hi = hash_find(&affile->af_pref, key);
3732 if (!HASHITEM_EMPTY(hi))
3733 {
3734 id = HI2AH(hi)->ah_newID;
3735 if (id != 0)
3736 store_afflist[cnt++] = id;
3737 }
3738 }
3739 if (affile->af_flagtype == AFT_NUM && *p == ',')
3740 ++p;
3741 }
3742
3743 store_afflist[cnt] = NUL;
3744 return cnt;
3745}
3746
3747/*
3748 * Get the list of compound IDs from the affix list "afflist" that are used
3749 * for compound words.
3750 * Puts the flags in "store_afflist[]".
3751 */
3752 static void
3753get_compflags(
3754 afffile_T *affile,
3755 char_u *afflist,
3756 char_u *store_afflist)
3757{
3758 char_u *p;
3759 char_u *prevp;
3760 int cnt = 0;
3761 char_u key[AH_KEY_LEN];
3762 hashitem_T *hi;
3763
3764 for (p = afflist; *p != NUL; )
3765 {
3766 prevp = p;
3767 if (get_affitem(affile->af_flagtype, &p) != 0)
3768 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003769 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003770 vim_strncpy(key, prevp, p - prevp);
3771 hi = hash_find(&affile->af_comp, key);
3772 if (!HASHITEM_EMPTY(hi))
3773 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3774 }
3775 if (affile->af_flagtype == AFT_NUM && *p == ',')
3776 ++p;
3777 }
3778
3779 store_afflist[cnt] = NUL;
3780}
3781
3782/*
3783 * Apply affixes to a word and store the resulting words.
3784 * "ht" is the hashtable with affentry_T that need to be applied, either
3785 * prefixes or suffixes.
3786 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3787 * the resulting words for combining affixes.
3788 *
3789 * Returns FAIL when out of memory.
3790 */
3791 static int
3792store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003793 spellinfo_T *spin, // spell info
3794 char_u *word, // basic word start
3795 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003796 afffile_T *affile,
3797 hashtab_T *ht,
3798 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003799 int condit, // CONDIT_SUF et al.
3800 int flags, // flags for the word
3801 char_u *pfxlist, // list of prefix IDs
3802 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3803 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003804{
3805 int todo;
3806 hashitem_T *hi;
3807 affheader_T *ah;
3808 affentry_T *ae;
3809 char_u newword[MAXWLEN];
3810 int retval = OK;
3811 int i, j;
3812 char_u *p;
3813 int use_flags;
3814 char_u *use_pfxlist;
3815 int use_pfxlen;
3816 int need_affix;
3817 char_u store_afflist[MAXWLEN];
3818 char_u pfx_pfxlist[MAXWLEN];
3819 size_t wordlen = STRLEN(word);
3820 int use_condit;
3821
3822 todo = (int)ht->ht_used;
3823 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3824 {
3825 if (!HASHITEM_EMPTY(hi))
3826 {
3827 --todo;
3828 ah = HI2AH(hi);
3829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003830 // Check that the affix combines, if required, and that the word
3831 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003832 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3833 && flag_in_afflist(affile->af_flagtype, afflist,
3834 ah->ah_flag))
3835 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003836 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003837 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3838 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003839 // Check the condition. It's not logical to match case
3840 // here, but it is required for compatibility with
3841 // Myspell.
3842 // Another requirement from Myspell is that the chop
3843 // string is shorter than the word itself.
3844 // For prefixes, when "PFXPOSTPONE" was used, only do
3845 // prefixes with a chop string and/or flags.
3846 // When a previously added affix had CIRCUMFIX this one
3847 // must have it too, if it had not then this one must not
3848 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003849 if ((xht != NULL || !affile->af_pfxpostpone
3850 || ae->ae_chop != NULL
3851 || ae->ae_flags != NULL)
3852 && (ae->ae_chop == NULL
3853 || STRLEN(ae->ae_chop) < wordlen)
3854 && (ae->ae_prog == NULL
3855 || vim_regexec_prog(&ae->ae_prog, FALSE,
3856 word, (colnr_T)0))
3857 && (((condit & CONDIT_CFIX) == 0)
3858 == ((condit & CONDIT_AFF) == 0
3859 || ae->ae_flags == NULL
3860 || !flag_in_afflist(affile->af_flagtype,
3861 ae->ae_flags, affile->af_circumfix))))
3862 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003863 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003864 if (xht == NULL)
3865 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003866 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003867 if (ae->ae_add == NULL)
3868 *newword = NUL;
3869 else
3870 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3871 p = word;
3872 if (ae->ae_chop != NULL)
3873 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003874 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003875 if (has_mbyte)
3876 {
3877 i = mb_charlen(ae->ae_chop);
3878 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003879 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003880 }
3881 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003882 p += STRLEN(ae->ae_chop);
3883 }
3884 STRCAT(newword, p);
3885 }
3886 else
3887 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003888 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003889 vim_strncpy(newword, word, MAXWLEN - 1);
3890 if (ae->ae_chop != NULL)
3891 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003892 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003893 p = newword + STRLEN(newword);
3894 i = (int)MB_CHARLEN(ae->ae_chop);
3895 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003896 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003897 *p = NUL;
3898 }
3899 if (ae->ae_add != NULL)
3900 STRCAT(newword, ae->ae_add);
3901 }
3902
3903 use_flags = flags;
3904 use_pfxlist = pfxlist;
3905 use_pfxlen = pfxlen;
3906 need_affix = FALSE;
3907 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3908 if (ae->ae_flags != NULL)
3909 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003910 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003911 use_flags |= get_affix_flags(affile, ae->ae_flags);
3912
3913 if (affile->af_needaffix != 0 && flag_in_afflist(
3914 affile->af_flagtype, ae->ae_flags,
3915 affile->af_needaffix))
3916 need_affix = TRUE;
3917
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003918 // When there is a CIRCUMFIX flag the other affix
3919 // must also have it and we don't add the word
3920 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003921 if (affile->af_circumfix != 0 && flag_in_afflist(
3922 affile->af_flagtype, ae->ae_flags,
3923 affile->af_circumfix))
3924 {
3925 use_condit |= CONDIT_CFIX;
3926 if ((condit & CONDIT_CFIX) == 0)
3927 need_affix = TRUE;
3928 }
3929
3930 if (affile->af_pfxpostpone
3931 || spin->si_compflags != NULL)
3932 {
3933 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003934 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003935 use_pfxlen = get_pfxlist(affile,
3936 ae->ae_flags, store_afflist);
3937 else
3938 use_pfxlen = 0;
3939 use_pfxlist = store_afflist;
3940
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003941 // Combine the prefix IDs. Avoid adding the
3942 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003943 for (i = 0; i < pfxlen; ++i)
3944 {
3945 for (j = 0; j < use_pfxlen; ++j)
3946 if (pfxlist[i] == use_pfxlist[j])
3947 break;
3948 if (j == use_pfxlen)
3949 use_pfxlist[use_pfxlen++] = pfxlist[i];
3950 }
3951
3952 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003953 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003954 get_compflags(affile, ae->ae_flags,
3955 use_pfxlist + use_pfxlen);
3956
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003957 // Combine the list of compound flags.
3958 // Concatenate them to the prefix IDs list.
3959 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003960 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3961 {
3962 for (j = use_pfxlen;
3963 use_pfxlist[j] != NUL; ++j)
3964 if (pfxlist[i] == use_pfxlist[j])
3965 break;
3966 if (use_pfxlist[j] == NUL)
3967 {
3968 use_pfxlist[j++] = pfxlist[i];
3969 use_pfxlist[j] = NUL;
3970 }
3971 }
3972 }
3973 }
3974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
3976 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003977 if (use_pfxlist != NULL && ae->ae_compforbid)
3978 {
3979 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
3980 use_pfxlist = pfx_pfxlist;
3981 }
3982
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003983 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003984 if (spin->si_prefroot != NULL
3985 && spin->si_prefroot->wn_sibling != NULL)
3986 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003987 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003988 use_flags |= WF_HAS_AFF;
3989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003990 // ... don't use a prefix list if combining
3991 // affixes is not allowed. But do use the
3992 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003993 if (!ah->ah_combine && use_pfxlist != NULL)
3994 use_pfxlist += use_pfxlen;
3995 }
3996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003997 // When compounding is supported and there is no
3998 // "COMPOUNDPERMITFLAG" then forbid compounding on the
3999 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004000 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4001 {
4002 if (xht != NULL)
4003 use_flags |= WF_NOCOMPAFT;
4004 else
4005 use_flags |= WF_NOCOMPBEF;
4006 }
4007
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004008 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004009 if (store_word(spin, newword, use_flags,
4010 spin->si_region, use_pfxlist,
4011 need_affix) == FAIL)
4012 retval = FAIL;
4013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004014 // When added a prefix or a first suffix and the affix
4015 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004016 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4017 if (store_aff_word(spin, newword, ae->ae_flags,
4018 affile, &affile->af_suff, xht,
4019 use_condit & (xht == NULL
4020 ? ~0 : ~CONDIT_SUF),
4021 use_flags, use_pfxlist, pfxlen) == FAIL)
4022 retval = FAIL;
4023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004024 // When added a suffix and combining is allowed also
4025 // try adding a prefix additionally. Both for the
4026 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004027 if (xht != NULL && ah->ah_combine)
4028 {
4029 if (store_aff_word(spin, newword,
4030 afflist, affile,
4031 xht, NULL, use_condit,
4032 use_flags, use_pfxlist,
4033 pfxlen) == FAIL
4034 || (ae->ae_flags != NULL
4035 && store_aff_word(spin, newword,
4036 ae->ae_flags, affile,
4037 xht, NULL, use_condit,
4038 use_flags, use_pfxlist,
4039 pfxlen) == FAIL))
4040 retval = FAIL;
4041 }
4042 }
4043 }
4044 }
4045 }
4046 }
4047
4048 return retval;
4049}
4050
4051/*
4052 * Read a file with a list of words.
4053 */
4054 static int
4055spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4056{
4057 FILE *fd;
4058 long lnum = 0;
4059 char_u rline[MAXLINELEN];
4060 char_u *line;
4061 char_u *pc = NULL;
4062 char_u *p;
4063 int l;
4064 int retval = OK;
4065 int did_word = FALSE;
4066 int non_ascii = 0;
4067 int flags;
4068 int regionmask;
4069
4070 /*
4071 * Open the file.
4072 */
4073 fd = mch_fopen((char *)fname, "r");
4074 if (fd == NULL)
4075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004076 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004077 return FAIL;
4078 }
4079
Bram Moolenaarc1669272018-06-19 14:23:53 +02004080 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004081 spell_message(spin, IObuff);
4082
4083 /*
4084 * Read all the lines in the file one by one.
4085 */
4086 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4087 {
4088 line_breakcheck();
4089 ++lnum;
4090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004091 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004092 if (*rline == '#')
4093 continue;
4094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004095 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004096 l = (int)STRLEN(rline);
4097 while (l > 0 && rline[l - 1] <= ' ')
4098 --l;
4099 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004100 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004101 rline[l] = NUL;
4102
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004103 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004104 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004105 if (spin->si_conv.vc_type != CONV_NONE)
4106 {
4107 pc = string_convert(&spin->si_conv, rline, NULL);
4108 if (pc == NULL)
4109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004110 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004111 fname, lnum, rline);
4112 continue;
4113 }
4114 line = pc;
4115 }
4116 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004117 {
4118 pc = NULL;
4119 line = rline;
4120 }
4121
4122 if (*line == '/')
4123 {
4124 ++line;
4125 if (STRNCMP(line, "encoding=", 9) == 0)
4126 {
4127 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004128 smsg(_("Duplicate /encoding= line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004129 fname, lnum, line - 1);
4130 else if (did_word)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004131 smsg(_("/encoding= line after word ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004132 fname, lnum, line - 1);
4133 else
4134 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004135 char_u *enc;
4136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004137 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004138 line += 9;
4139 enc = enc_canonize(line);
4140 if (enc != NULL && !spin->si_ascii
4141 && convert_setup(&spin->si_conv, enc,
4142 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004143 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004144 fname, line, p_enc);
4145 vim_free(enc);
4146 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004147 }
4148 continue;
4149 }
4150
4151 if (STRNCMP(line, "regions=", 8) == 0)
4152 {
4153 if (spin->si_region_count > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004154 smsg(_("Duplicate /regions= line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004155 fname, lnum, line);
4156 else
4157 {
4158 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004159 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 smsg(_("Too many regions in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004161 fname, lnum, line);
4162 else
4163 {
4164 spin->si_region_count = (int)STRLEN(line) / 2;
4165 STRCPY(spin->si_region_name, line);
4166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004167 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004168 spin->si_region = (1 << spin->si_region_count) - 1;
4169 }
4170 }
4171 continue;
4172 }
4173
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004174 smsg(_("/ line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004175 fname, lnum, line - 1);
4176 continue;
4177 }
4178
4179 flags = 0;
4180 regionmask = spin->si_region;
4181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004182 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004183 p = vim_strchr(line, '/');
4184 if (p != NULL)
4185 {
4186 *p++ = NUL;
4187 while (*p != NUL)
4188 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004189 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004190 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004192 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004193 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004194 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004195 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004196 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004197 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004198 regionmask = 0;
4199 flags |= WF_REGION;
4200
4201 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004202 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004204 smsg(_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004205 fname, lnum, p);
4206 break;
4207 }
4208 regionmask |= 1 << (l - 1);
4209 }
4210 else
4211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004212 smsg(_("Unrecognized flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004213 fname, lnum, p);
4214 break;
4215 }
4216 ++p;
4217 }
4218 }
4219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004220 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004221 if (spin->si_ascii && has_non_ascii(line))
4222 {
4223 ++non_ascii;
4224 continue;
4225 }
4226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004227 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004228 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4229 {
4230 retval = FAIL;
4231 break;
4232 }
4233 did_word = TRUE;
4234 }
4235
4236 vim_free(pc);
4237 fclose(fd);
4238
4239 if (spin->si_ascii && non_ascii > 0)
4240 {
4241 vim_snprintf((char *)IObuff, IOSIZE,
4242 _("Ignored %d words with non-ASCII characters"), non_ascii);
4243 spell_message(spin, IObuff);
4244 }
4245
4246 return retval;
4247}
4248
4249/*
4250 * Get part of an sblock_T, "len" bytes long.
4251 * This avoids calling free() for every little struct we use (and keeping
4252 * track of them).
4253 * The memory is cleared to all zeros.
4254 * Returns NULL when out of memory.
4255 */
4256 static void *
4257getroom(
4258 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004259 size_t len, // length needed
4260 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004261{
4262 char_u *p;
4263 sblock_T *bl = spin->si_blocks;
4264
4265 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004266 // Round size up for alignment. On some systems structures need to be
4267 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004268 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4269 & ~(sizeof(char *) - 1);
4270
4271 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4272 {
4273 if (len >= SBLOCKSIZE)
4274 bl = NULL;
4275 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004276 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004277 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004278 if (bl == NULL)
4279 {
4280 if (!spin->si_did_emsg)
4281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004282 emsg(_("E845: Insufficient memory, word list will be incomplete"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004283 spin->si_did_emsg = TRUE;
4284 }
4285 return NULL;
4286 }
4287 bl->sb_next = spin->si_blocks;
4288 spin->si_blocks = bl;
4289 bl->sb_used = 0;
4290 ++spin->si_blocks_cnt;
4291 }
4292
4293 p = bl->sb_data + bl->sb_used;
4294 bl->sb_used += (int)len;
4295
4296 return p;
4297}
4298
4299/*
4300 * Make a copy of a string into memory allocated with getroom().
4301 * Returns NULL when out of memory.
4302 */
4303 static char_u *
4304getroom_save(spellinfo_T *spin, char_u *s)
4305{
4306 char_u *sc;
4307
4308 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4309 if (sc != NULL)
4310 STRCPY(sc, s);
4311 return sc;
4312}
4313
4314
4315/*
4316 * Free the list of allocated sblock_T.
4317 */
4318 static void
4319free_blocks(sblock_T *bl)
4320{
4321 sblock_T *next;
4322
4323 while (bl != NULL)
4324 {
4325 next = bl->sb_next;
4326 vim_free(bl);
4327 bl = next;
4328 }
4329}
4330
4331/*
4332 * Allocate the root of a word tree.
4333 * Returns NULL when out of memory.
4334 */
4335 static wordnode_T *
4336wordtree_alloc(spellinfo_T *spin)
4337{
4338 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4339}
4340
4341/*
4342 * Store a word in the tree(s).
4343 * Always store it in the case-folded tree. For a keep-case word this is
4344 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4345 * used to find suggestions.
4346 * For a keep-case word also store it in the keep-case tree.
4347 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4348 * compound flag.
4349 */
4350 static int
4351store_word(
4352 spellinfo_T *spin,
4353 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004354 int flags, // extra flags, WF_BANNED
4355 int region, // supported region(s)
4356 char_u *pfxlist, // list of prefix IDs or NULL
4357 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004358{
4359 int len = (int)STRLEN(word);
4360 int ct = captype(word, word + len);
4361 char_u foldword[MAXWLEN];
4362 int res = OK;
4363 char_u *p;
4364
4365 (void)spell_casefold(word, len, foldword, MAXWLEN);
4366 for (p = pfxlist; res == OK; ++p)
4367 {
4368 if (!need_affix || (p != NULL && *p != NUL))
4369 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4370 region, p == NULL ? 0 : *p);
4371 if (p == NULL || *p == NUL)
4372 break;
4373 }
4374 ++spin->si_foldwcount;
4375
4376 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4377 {
4378 for (p = pfxlist; res == OK; ++p)
4379 {
4380 if (!need_affix || (p != NULL && *p != NUL))
4381 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4382 region, p == NULL ? 0 : *p);
4383 if (p == NULL || *p == NUL)
4384 break;
4385 }
4386 ++spin->si_keepwcount;
4387 }
4388 return res;
4389}
4390
4391/*
4392 * Add word "word" to a word tree at "root".
4393 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4394 * "rare" and "region" is the condition nr.
4395 * Returns FAIL when out of memory.
4396 */
4397 static int
4398tree_add_word(
4399 spellinfo_T *spin,
4400 char_u *word,
4401 wordnode_T *root,
4402 int flags,
4403 int region,
4404 int affixID)
4405{
4406 wordnode_T *node = root;
4407 wordnode_T *np;
4408 wordnode_T *copyp, **copyprev;
4409 wordnode_T **prev = NULL;
4410 int i;
4411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004412 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004413 for (i = 0; ; ++i)
4414 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004415 // When there is more than one reference to this node we need to make
4416 // a copy, so that we can modify it. Copy the whole list of siblings
4417 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004418 if (node != NULL && node->wn_refs > 1)
4419 {
4420 --node->wn_refs;
4421 copyprev = prev;
4422 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4423 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004424 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004425 np = get_wordnode(spin);
4426 if (np == NULL)
4427 return FAIL;
4428 np->wn_child = copyp->wn_child;
4429 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004430 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004431 np->wn_byte = copyp->wn_byte;
4432 if (np->wn_byte == NUL)
4433 {
4434 np->wn_flags = copyp->wn_flags;
4435 np->wn_region = copyp->wn_region;
4436 np->wn_affixID = copyp->wn_affixID;
4437 }
4438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004439 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004440 np->wn_refs = 1;
4441 if (copyprev != NULL)
4442 *copyprev = np;
4443 copyprev = &np->wn_sibling;
4444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004445 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004446 if (copyp == node)
4447 node = np;
4448 }
4449 }
4450
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004451 // Look for the sibling that has the same character. They are sorted
4452 // on byte value, thus stop searching when a sibling is found with a
4453 // higher byte value. For zero bytes (end of word) the sorting is
4454 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004455 while (node != NULL
4456 && (node->wn_byte < word[i]
4457 || (node->wn_byte == NUL
4458 && (flags < 0
4459 ? node->wn_affixID < (unsigned)affixID
4460 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4461 || (node->wn_flags == (flags & WN_MASK)
4462 && (spin->si_sugtree
4463 ? (node->wn_region & 0xffff) < region
4464 : node->wn_affixID
4465 < (unsigned)affixID)))))))
4466 {
4467 prev = &node->wn_sibling;
4468 node = *prev;
4469 }
4470 if (node == NULL
4471 || node->wn_byte != word[i]
4472 || (word[i] == NUL
4473 && (flags < 0
4474 || spin->si_sugtree
4475 || node->wn_flags != (flags & WN_MASK)
4476 || node->wn_affixID != affixID)))
4477 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004478 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004479 np = get_wordnode(spin);
4480 if (np == NULL)
4481 return FAIL;
4482 np->wn_byte = word[i];
4483
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004484 // If "node" is NULL this is a new child or the end of the sibling
4485 // list: ref count is one. Otherwise use ref count of sibling and
4486 // make ref count of sibling one (matters when inserting in front
4487 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004488 if (node == NULL)
4489 np->wn_refs = 1;
4490 else
4491 {
4492 np->wn_refs = node->wn_refs;
4493 node->wn_refs = 1;
4494 }
4495 if (prev != NULL)
4496 *prev = np;
4497 np->wn_sibling = node;
4498 node = np;
4499 }
4500
4501 if (word[i] == NUL)
4502 {
4503 node->wn_flags = flags;
4504 node->wn_region |= region;
4505 node->wn_affixID = affixID;
4506 break;
4507 }
4508 prev = &node->wn_child;
4509 node = *prev;
4510 }
4511#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004512 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004513 spell_print_tree(root->wn_sibling);
4514#endif
4515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004516 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004517 ++spin->si_msg_count;
4518
4519 if (spin->si_compress_cnt > 1)
4520 {
4521 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004522 // Did enough words to lower the block count limit.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004523 spin->si_blocks_cnt += compress_inc;
4524 }
4525
4526 /*
4527 * When we have allocated lots of memory we need to compress the word tree
4528 * to free up some room. But compression is slow, and we might actually
4529 * need that room, thus only compress in the following situations:
4530 * 1. When not compressed before (si_compress_cnt == 0): when using
4531 * "compress_start" blocks.
4532 * 2. When compressed before and used "compress_inc" blocks before
4533 * adding "compress_added" words (si_compress_cnt > 1).
4534 * 3. When compressed before, added "compress_added" words
4535 * (si_compress_cnt == 1) and the number of free nodes drops below the
4536 * maximum word length.
4537 */
4538#ifndef SPELL_COMPRESS_ALLWAYS
4539 if (spin->si_compress_cnt == 1
4540 ? spin->si_free_count < MAXWLEN
4541 : spin->si_blocks_cnt >= compress_start)
4542#endif
4543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004544 // Decrement the block counter. The effect is that we compress again
4545 // when the freed up room has been used and another "compress_inc"
4546 // blocks have been allocated. Unless "compress_added" words have
4547 // been added, then the limit is put back again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004548 spin->si_blocks_cnt -= compress_inc;
4549 spin->si_compress_cnt = compress_added;
4550
4551 if (spin->si_verbose)
4552 {
4553 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004555 msg_clr_eos();
4556 msg_didout = FALSE;
4557 msg_col = 0;
4558 out_flush();
4559 }
4560
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004561 // Compress both trees. Either they both have many nodes, which makes
4562 // compression useful, or one of them is small, which means
4563 // compression goes fast. But when filling the soundfold word tree
4564 // there is no keep-case tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004565 wordtree_compress(spin, spin->si_foldroot);
4566 if (affixID >= 0)
4567 wordtree_compress(spin, spin->si_keeproot);
4568 }
4569
4570 return OK;
4571}
4572
4573/*
4574 * Get a wordnode_T, either from the list of previously freed nodes or
4575 * allocate a new one.
4576 * Returns NULL when out of memory.
4577 */
4578 static wordnode_T *
4579get_wordnode(spellinfo_T *spin)
4580{
4581 wordnode_T *n;
4582
4583 if (spin->si_first_free == NULL)
4584 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4585 else
4586 {
4587 n = spin->si_first_free;
4588 spin->si_first_free = n->wn_child;
4589 vim_memset(n, 0, sizeof(wordnode_T));
4590 --spin->si_free_count;
4591 }
4592#ifdef SPELL_PRINTTREE
4593 if (n != NULL)
4594 n->wn_nr = ++spin->si_wordnode_nr;
4595#endif
4596 return n;
4597}
4598
4599/*
4600 * Decrement the reference count on a node (which is the head of a list of
4601 * siblings). If the reference count becomes zero free the node and its
4602 * siblings.
4603 * Returns the number of nodes actually freed.
4604 */
4605 static int
4606deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4607{
4608 wordnode_T *np;
4609 int cnt = 0;
4610
4611 if (--node->wn_refs == 0)
4612 {
4613 for (np = node; np != NULL; np = np->wn_sibling)
4614 {
4615 if (np->wn_child != NULL)
4616 cnt += deref_wordnode(spin, np->wn_child);
4617 free_wordnode(spin, np);
4618 ++cnt;
4619 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004620 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004621 }
4622 return cnt;
4623}
4624
4625/*
4626 * Free a wordnode_T for re-use later.
4627 * Only the "wn_child" field becomes invalid.
4628 */
4629 static void
4630free_wordnode(spellinfo_T *spin, wordnode_T *n)
4631{
4632 n->wn_child = spin->si_first_free;
4633 spin->si_first_free = n;
4634 ++spin->si_free_count;
4635}
4636
4637/*
4638 * Compress a tree: find tails that are identical and can be shared.
4639 */
4640 static void
4641wordtree_compress(spellinfo_T *spin, wordnode_T *root)
4642{
4643 hashtab_T ht;
4644 int n;
4645 int tot = 0;
4646 int perc;
4647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004648 // Skip the root itself, it's not actually used. The first sibling is the
4649 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004650 if (root->wn_sibling != NULL)
4651 {
4652 hash_init(&ht);
4653 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4654
4655#ifndef SPELL_PRINTTREE
4656 if (spin->si_verbose || p_verbose > 2)
4657#endif
4658 {
4659 if (tot > 1000000)
4660 perc = (tot - n) / (tot / 100);
4661 else if (tot == 0)
4662 perc = 0;
4663 else
4664 perc = (tot - n) * 100 / tot;
4665 vim_snprintf((char *)IObuff, IOSIZE,
4666 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
4667 n, tot, tot - n, perc);
4668 spell_message(spin, IObuff);
4669 }
4670#ifdef SPELL_PRINTTREE
4671 spell_print_tree(root->wn_sibling);
4672#endif
4673 hash_clear(&ht);
4674 }
4675}
4676
4677/*
4678 * Compress a node, its siblings and its children, depth first.
4679 * Returns the number of compressed nodes.
4680 */
4681 static int
4682node_compress(
4683 spellinfo_T *spin,
4684 wordnode_T *node,
4685 hashtab_T *ht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004686 int *tot) // total count of nodes before compressing,
4687 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004688{
4689 wordnode_T *np;
4690 wordnode_T *tp;
4691 wordnode_T *child;
4692 hash_T hash;
4693 hashitem_T *hi;
4694 int len = 0;
4695 unsigned nr, n;
4696 int compressed = 0;
4697
4698 /*
4699 * Go through the list of siblings. Compress each child and then try
4700 * finding an identical child to replace it.
4701 * Note that with "child" we mean not just the node that is pointed to,
4702 * but the whole list of siblings of which the child node is the first.
4703 */
4704 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4705 {
4706 ++len;
4707 if ((child = np->wn_child) != NULL)
4708 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004709 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004710 compressed += node_compress(spin, child, ht, tot);
4711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004712 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004713 hash = hash_hash(child->wn_u1.hashkey);
4714 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4715 if (!HASHITEM_EMPTY(hi))
4716 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004717 // There are children we encountered before with a hash value
4718 // identical to the current child. Now check if there is one
4719 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004720 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4721 if (node_equal(child, tp))
4722 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004723 // Found one! Now use that child in place of the
4724 // current one. This means the current child and all
4725 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004726 ++tp->wn_refs;
4727 compressed += deref_wordnode(spin, child);
4728 np->wn_child = tp;
4729 break;
4730 }
4731 if (tp == NULL)
4732 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004733 // No other child with this hash value equals the child of
4734 // the node, add it to the linked list after the first
4735 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004736 tp = HI2WN(hi);
4737 child->wn_u2.next = tp->wn_u2.next;
4738 tp->wn_u2.next = child;
4739 }
4740 }
4741 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004742 // No other child has this hash value, add it to the
4743 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004744 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4745 }
4746 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004747 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004748
4749 /*
4750 * Make a hash key for the node and its siblings, so that we can quickly
4751 * find a lookalike node. This must be done after compressing the sibling
4752 * list, otherwise the hash key would become invalid by the compression.
4753 */
4754 node->wn_u1.hashkey[0] = len;
4755 nr = 0;
4756 for (np = node; np != NULL; np = np->wn_sibling)
4757 {
4758 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004759 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004760 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4761 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004762 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004763 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4764 nr = nr * 101 + n;
4765 }
4766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004767 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004768 n = nr & 0xff;
4769 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4770 n = (nr >> 8) & 0xff;
4771 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4772 n = (nr >> 16) & 0xff;
4773 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4774 n = (nr >> 24) & 0xff;
4775 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4776 node->wn_u1.hashkey[5] = NUL;
4777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004778 // Check for CTRL-C pressed now and then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004779 fast_breakcheck();
4780
4781 return compressed;
4782}
4783
4784/*
4785 * Return TRUE when two nodes have identical siblings and children.
4786 */
4787 static int
4788node_equal(wordnode_T *n1, wordnode_T *n2)
4789{
4790 wordnode_T *p1;
4791 wordnode_T *p2;
4792
4793 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4794 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4795 if (p1->wn_byte != p2->wn_byte
4796 || (p1->wn_byte == NUL
4797 ? (p1->wn_flags != p2->wn_flags
4798 || p1->wn_region != p2->wn_region
4799 || p1->wn_affixID != p2->wn_affixID)
4800 : (p1->wn_child != p2->wn_child)))
4801 break;
4802
4803 return p1 == NULL && p2 == NULL;
4804}
4805
Bram Moolenaareae1b912019-05-09 15:12:55 +02004806static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004807
4808/*
4809 * Function given to qsort() to sort the REP items on "from" string.
4810 */
4811 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004812rep_compare(const void *s1, const void *s2)
4813{
4814 fromto_T *p1 = (fromto_T *)s1;
4815 fromto_T *p2 = (fromto_T *)s2;
4816
4817 return STRCMP(p1->ft_from, p2->ft_from);
4818}
4819
4820/*
4821 * Write the Vim .spl file "fname".
4822 * Return FAIL or OK;
4823 */
4824 static int
4825write_vim_spell(spellinfo_T *spin, char_u *fname)
4826{
4827 FILE *fd;
4828 int regionmask;
4829 int round;
4830 wordnode_T *tree;
4831 int nodecount;
4832 int i;
4833 int l;
4834 garray_T *gap;
4835 fromto_T *ftp;
4836 char_u *p;
4837 int rr;
4838 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004839 size_t fwv = 1; // collect return value of fwrite() to avoid
4840 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004841
4842 fd = mch_fopen((char *)fname, "w");
4843 if (fd == NULL)
4844 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004845 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004846 return FAIL;
4847 }
4848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004849 // <HEADER>: <fileID> <versionnr>
4850 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004851 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4852 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004853 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004854 goto theend;
4855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004856 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004857
4858 /*
4859 * <SECTIONS>: <section> ... <sectionend>
4860 */
4861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004862 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004863 if (spin->si_info != NULL)
4864 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004865 putc(SN_INFO, fd); // <sectionID>
4866 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004867
4868 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004869 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4870 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004871 }
4872
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004873 // SN_REGION: <regionname> ...
4874 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004875 if (spin->si_region_count > 1)
4876 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004877 putc(SN_REGION, fd); // <sectionID>
4878 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004879 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004880 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004881 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004882 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004883 regionmask = (1 << spin->si_region_count) - 1;
4884 }
4885 else
4886 regionmask = 0;
4887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004888 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4889 //
4890 // The table with character flags and the table for case folding.
4891 // This makes sure the same characters are recognized as word characters
4892 // when generating an when using a spell file.
4893 // Skip this for ASCII, the table may conflict with the one used for
4894 // 'encoding'.
4895 // Also skip this for an .add.spl file, the main spell file must contain
4896 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004897 if (!spin->si_ascii && !spin->si_add)
4898 {
4899 char_u folchars[128 * 8];
4900 int flags;
4901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004902 putc(SN_CHARFLAGS, fd); // <sectionID>
4903 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004905 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004906 l = 0;
4907 for (i = 128; i < 256; ++i)
4908 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004909 if (has_mbyte)
4910 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4911 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004912 folchars[l++] = spelltab.st_fold[i];
4913 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004914 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004916 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004917 for (i = 128; i < 256; ++i)
4918 {
4919 flags = 0;
4920 if (spelltab.st_isw[i])
4921 flags |= CF_WORD;
4922 if (spelltab.st_isu[i])
4923 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004924 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004925 }
4926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004927 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4928 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004929 }
4930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004931 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004932 if (spin->si_midword != NULL)
4933 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004934 putc(SN_MIDWORD, fd); // <sectionID>
4935 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004936
4937 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004938 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004939 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004940 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004941 }
4942
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004943 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004944 if (spin->si_prefcond.ga_len > 0)
4945 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004946 putc(SN_PREFCOND, fd); // <sectionID>
4947 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004948
4949 l = write_spell_prefcond(NULL, &spin->si_prefcond);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004950 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004951
4952 write_spell_prefcond(fd, &spin->si_prefcond);
4953 }
4954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004955 // SN_REP: <repcount> <rep> ...
4956 // SN_SAL: <salflags> <salcount> <sal> ...
4957 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004959 // round 1: SN_REP section
4960 // round 2: SN_SAL section (unless SN_SOFO is used)
4961 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004962 for (round = 1; round <= 3; ++round)
4963 {
4964 if (round == 1)
4965 gap = &spin->si_rep;
4966 else if (round == 2)
4967 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004968 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004969 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4970 continue;
4971 gap = &spin->si_sal;
4972 }
4973 else
4974 gap = &spin->si_repsal;
4975
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004976 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004977 if (gap->ga_len == 0)
4978 continue;
4979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004980 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004981 if (round != 2)
4982 qsort(gap->ga_data, (size_t)gap->ga_len,
4983 sizeof(fromto_T), rep_compare);
4984
4985 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004986 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004987
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004988 // This is for making suggestions, section is not required.
4989 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004991 // Compute the length of what follows.
4992 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004993 for (i = 0; i < gap->ga_len; ++i)
4994 {
4995 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004996 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
4997 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004998 }
4999 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005000 ++l; // count <salflags>
5001 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005002
5003 if (round == 2)
5004 {
5005 i = 0;
5006 if (spin->si_followup)
5007 i |= SAL_F0LLOWUP;
5008 if (spin->si_collapse)
5009 i |= SAL_COLLAPSE;
5010 if (spin->si_rem_accents)
5011 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005013 }
5014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005015 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005016 for (i = 0; i < gap->ga_len; ++i)
5017 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005018 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5019 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005020 ftp = &((fromto_T *)gap->ga_data)[i];
5021 for (rr = 1; rr <= 2; ++rr)
5022 {
5023 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5024 l = (int)STRLEN(p);
5025 putc(l, fd);
5026 if (l > 0)
5027 fwv &= fwrite(p, l, (size_t)1, fd);
5028 }
5029 }
5030
5031 }
5032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005033 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5034 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005035 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5036 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005037 putc(SN_SOFO, fd); // <sectionID>
5038 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005039
5040 l = (int)STRLEN(spin->si_sofofr);
5041 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005042 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005044 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5045 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005046
5047 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005048 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5049 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005050 }
5051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005052 // SN_WORDS: <word> ...
5053 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005054 if (spin->si_commonwords.ht_used > 0)
5055 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005056 putc(SN_WORDS, fd); // <sectionID>
5057 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005058
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005059 // round 1: count the bytes
5060 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005061 for (round = 1; round <= 2; ++round)
5062 {
5063 int todo;
5064 int len = 0;
5065 hashitem_T *hi;
5066
5067 todo = (int)spin->si_commonwords.ht_used;
5068 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5069 if (!HASHITEM_EMPTY(hi))
5070 {
5071 l = (int)STRLEN(hi->hi_key) + 1;
5072 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005073 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005074 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5075 --todo;
5076 }
5077 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005078 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005079 }
5080 }
5081
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005082 // SN_MAP: <mapstr>
5083 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005084 if (spin->si_map.ga_len > 0)
5085 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005086 putc(SN_MAP, fd); // <sectionID>
5087 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005088 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005089 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005090 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005091 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005092 }
5093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005094 // SN_SUGFILE: <timestamp>
5095 // This is used to notify that a .sug file may be available and at the
5096 // same time allows for checking that a .sug file that is found matches
5097 // with this .spl file. That's because the word numbers must be exactly
5098 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005099 if (!spin->si_nosugfile
5100 && (spin->si_sal.ga_len > 0
5101 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5102 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005103 putc(SN_SUGFILE, fd); // <sectionID>
5104 putc(0, fd); // <sectionflags>
5105 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005107 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005108 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005109 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005110 }
5111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 // SN_NOSPLITSUGS: nothing
5113 // This is used to notify that no suggestions with word splits are to be
5114 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005115 if (spin->si_nosplitsugs)
5116 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005117 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5118 putc(0, fd); // <sectionflags>
5119 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005120 }
5121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005122 // SN_NOCOMPUNDSUGS: nothing
5123 // This is used to notify that no suggestions with compounds are to be
5124 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005125 if (spin->si_nocompoundsugs)
5126 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005127 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5128 putc(0, fd); // <sectionflags>
5129 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005130 }
5131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005132 // SN_COMPOUND: compound info.
5133 // We don't mark it required, when not supported all compound words will
5134 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005135 if (spin->si_compflags != NULL)
5136 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005137 putc(SN_COMPOUND, fd); // <sectionID>
5138 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005139
5140 l = (int)STRLEN(spin->si_compflags);
5141 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5142 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005143 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005144
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005145 putc(spin->si_compmax, fd); // <compmax>
5146 putc(spin->si_compminlen, fd); // <compminlen>
5147 putc(spin->si_compsylmax, fd); // <compsylmax>
5148 putc(0, fd); // for Vim 7.0b compatibility
5149 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005150 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005151 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005152 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5153 {
5154 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005155 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005156 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005157 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005158 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005159 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005160 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5161 (size_t)1, fd);
5162 }
5163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005164 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005165 if (spin->si_nobreak)
5166 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005167 putc(SN_NOBREAK, fd); // <sectionID>
5168 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005169
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005170 // It's empty, the presence of the section flags the feature.
5171 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005172 }
5173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005174 // SN_SYLLABLE: syllable info.
5175 // We don't mark it required, when not supported syllables will not be
5176 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005177 if (spin->si_syllable != NULL)
5178 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005179 putc(SN_SYLLABLE, fd); // <sectionID>
5180 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005181
5182 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005183 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005184 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005185 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005186 }
5187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005188 // end of <SECTIONS>
5189 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005190
5191
5192 /*
5193 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5194 */
5195 spin->si_memtot = 0;
5196 for (round = 1; round <= 3; ++round)
5197 {
5198 if (round == 1)
5199 tree = spin->si_foldroot->wn_sibling;
5200 else if (round == 2)
5201 tree = spin->si_keeproot->wn_sibling;
5202 else
5203 tree = spin->si_prefroot->wn_sibling;
5204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005206 clear_node(tree);
5207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005208 // Count the number of nodes. Needed to be able to allocate the
5209 // memory when reading the nodes. Also fills in index for shared
5210 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005211 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5212
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005213 // number of nodes in 4 bytes
5214 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005215 spin->si_memtot += nodecount + nodecount * sizeof(int);
5216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005217 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005218 (void)put_node(fd, tree, 0, regionmask, round == 3);
5219 }
5220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005221 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005222 if (putc(0, fd) == EOF)
5223 retval = FAIL;
5224theend:
5225 if (fclose(fd) == EOF)
5226 retval = FAIL;
5227
5228 if (fwv != (size_t)1)
5229 retval = FAIL;
5230 if (retval == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005231 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005232
5233 return retval;
5234}
5235
5236/*
5237 * Clear the index and wnode fields of "node", it siblings and its
5238 * children. This is needed because they are a union with other items to save
5239 * space.
5240 */
5241 static void
5242clear_node(wordnode_T *node)
5243{
5244 wordnode_T *np;
5245
5246 if (node != NULL)
5247 for (np = node; np != NULL; np = np->wn_sibling)
5248 {
5249 np->wn_u1.index = 0;
5250 np->wn_u2.wnode = NULL;
5251
5252 if (np->wn_byte != NUL)
5253 clear_node(np->wn_child);
5254 }
5255}
5256
5257
5258/*
5259 * Dump a word tree at node "node".
5260 *
5261 * This first writes the list of possible bytes (siblings). Then for each
5262 * byte recursively write the children.
5263 *
5264 * NOTE: The code here must match the code in read_tree_node(), since
5265 * assumptions are made about the indexes (so that we don't have to write them
5266 * in the file).
5267 *
5268 * Returns the number of nodes used.
5269 */
5270 static int
5271put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005272 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005273 wordnode_T *node,
5274 int idx,
5275 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005276 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005277{
5278 int newindex = idx;
5279 int siblingcount = 0;
5280 wordnode_T *np;
5281 int flags;
5282
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005283 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005284 if (node == NULL)
5285 return 0;
5286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005287 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005288 node->wn_u1.index = idx;
5289
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005290 // Count the number of siblings.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005291 for (np = node; np != NULL; np = np->wn_sibling)
5292 ++siblingcount;
5293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005294 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005295 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005296 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005297
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 // Write each sibling byte and optionally extra info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005299 for (np = node; np != NULL; np = np->wn_sibling)
5300 {
5301 if (np->wn_byte == 0)
5302 {
5303 if (fd != NULL)
5304 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005305 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005306 if (prefixtree)
5307 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005308 // In PREFIXTREE write the required affixID and the
5309 // associated condition nr (stored in wn_region). The
5310 // byte value is misused to store the "rare" and "not
5311 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005312 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005313 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005314 else
5315 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005316 putc(BY_FLAGS, fd); // <byte>
5317 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005318 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005319 putc(np->wn_affixID, fd); // <affixID>
5320 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005321 }
5322 else
5323 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005324 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005325 flags = np->wn_flags;
5326 if (regionmask != 0 && np->wn_region != regionmask)
5327 flags |= WF_REGION;
5328 if (np->wn_affixID != 0)
5329 flags |= WF_AFX;
5330 if (flags == 0)
5331 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005332 // word without flags or region
5333 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005334 }
5335 else
5336 {
5337 if (np->wn_flags >= 0x100)
5338 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005339 putc(BY_FLAGS2, fd); // <byte>
5340 putc(flags, fd); // <flags>
5341 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005342 }
5343 else
5344 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005345 putc(BY_FLAGS, fd); // <byte>
5346 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005347 }
5348 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005349 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005350 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005351 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005352 }
5353 }
5354 }
5355 }
5356 else
5357 {
5358 if (np->wn_child->wn_u1.index != 0
5359 && np->wn_child->wn_u2.wnode != node)
5360 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005361 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005362 if (fd != NULL)
5363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005364 putc(BY_INDEX, fd); // <byte>
5365 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005366 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5367 }
5368 }
5369 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005370 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005371 np->wn_child->wn_u2.wnode = node;
5372
5373 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005374 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005376 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005377 return 0;
5378 }
5379 }
5380 }
5381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005382 // Space used in the array when reading: one for each sibling and one for
5383 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005384 newindex += siblingcount + 1;
5385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005386 // Recursively dump the children of each sibling.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005387 for (np = node; np != NULL; np = np->wn_sibling)
5388 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5389 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5390 prefixtree);
5391
5392 return newindex;
5393}
5394
5395
5396/*
5397 * ":mkspell [-ascii] outfile infile ..."
5398 * ":mkspell [-ascii] addfile"
5399 */
5400 void
5401ex_mkspell(exarg_T *eap)
5402{
5403 int fcount;
5404 char_u **fnames;
5405 char_u *arg = eap->arg;
5406 int ascii = FALSE;
5407
5408 if (STRNCMP(arg, "-ascii", 6) == 0)
5409 {
5410 ascii = TRUE;
5411 arg = skipwhite(arg + 6);
5412 }
5413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005414 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005415 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5416 {
5417 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5418 FreeWild(fcount, fnames);
5419 }
5420}
5421
5422/*
5423 * Create the .sug file.
5424 * Uses the soundfold info in "spin".
5425 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5426 */
5427 static void
5428spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5429{
5430 char_u *fname = NULL;
5431 int len;
5432 slang_T *slang;
5433 int free_slang = FALSE;
5434
5435 /*
5436 * Read back the .spl file that was written. This fills the required
5437 * info for soundfolding. This also uses less memory than the
5438 * pointer-linked version of the trie. And it avoids having two versions
5439 * of the code for the soundfolding stuff.
5440 * It might have been done already by spell_reload_one().
5441 */
5442 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005443 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005444 break;
5445 if (slang == NULL)
5446 {
5447 spell_message(spin, (char_u *)_("Reading back spell file..."));
5448 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5449 if (slang == NULL)
5450 return;
5451 free_slang = TRUE;
5452 }
5453
5454 /*
5455 * Clear the info in "spin" that is used.
5456 */
5457 spin->si_blocks = NULL;
5458 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005459 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005460 spin->si_free_count = 0;
5461 spin->si_first_free = NULL;
5462 spin->si_foldwcount = 0;
5463
5464 /*
5465 * Go through the trie of good words, soundfold each word and add it to
5466 * the soundfold trie.
5467 */
5468 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5469 if (sug_filltree(spin, slang) == FAIL)
5470 goto theend;
5471
5472 /*
5473 * Create the table which links each soundfold word with a list of the
5474 * good words it may come from. Creates buffer "spin->si_spellbuf".
5475 * This also removes the wordnr from the NUL byte entries to make
5476 * compression possible.
5477 */
5478 if (sug_maketable(spin) == FAIL)
5479 goto theend;
5480
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005481 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005482 (long)spin->si_spellbuf->b_ml.ml_line_count);
5483
5484 /*
5485 * Compress the soundfold trie.
5486 */
5487 spell_message(spin, (char_u *)_(msg_compressing));
5488 wordtree_compress(spin, spin->si_foldroot);
5489
5490 /*
5491 * Write the .sug file.
5492 * Make the file name by changing ".spl" to ".sug".
5493 */
5494 fname = alloc(MAXPATHL);
5495 if (fname == NULL)
5496 goto theend;
5497 vim_strncpy(fname, wfname, MAXPATHL - 1);
5498 len = (int)STRLEN(fname);
5499 fname[len - 2] = 'u';
5500 fname[len - 1] = 'g';
5501 sug_write(spin, fname);
5502
5503theend:
5504 vim_free(fname);
5505 if (free_slang)
5506 slang_free(slang);
5507 free_blocks(spin->si_blocks);
5508 close_spellbuf(spin->si_spellbuf);
5509}
5510
5511/*
5512 * Build the soundfold trie for language "slang".
5513 */
5514 static int
5515sug_filltree(spellinfo_T *spin, slang_T *slang)
5516{
5517 char_u *byts;
5518 idx_T *idxs;
5519 int depth;
5520 idx_T arridx[MAXWLEN];
5521 int curi[MAXWLEN];
5522 char_u tword[MAXWLEN];
5523 char_u tsalword[MAXWLEN];
5524 int c;
5525 idx_T n;
5526 unsigned words_done = 0;
5527 int wordcount[MAXWLEN];
5528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005529 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005530 spin->si_foldroot = wordtree_alloc(spin);
5531 if (spin->si_foldroot == NULL)
5532 return FAIL;
5533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005534 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005535 spin->si_sugtree = TRUE;
5536
5537 /*
5538 * Go through the whole case-folded tree, soundfold each word and put it
5539 * in the trie.
5540 */
5541 byts = slang->sl_fbyts;
5542 idxs = slang->sl_fidxs;
5543
5544 arridx[0] = 0;
5545 curi[0] = 1;
5546 wordcount[0] = 0;
5547
5548 depth = 0;
5549 while (depth >= 0 && !got_int)
5550 {
5551 if (curi[depth] > byts[arridx[depth]])
5552 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005553 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005554 idxs[arridx[depth]] = wordcount[depth];
5555 if (depth > 0)
5556 wordcount[depth - 1] += wordcount[depth];
5557
5558 --depth;
5559 line_breakcheck();
5560 }
5561 else
5562 {
5563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005564 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005565 n = arridx[depth] + curi[depth];
5566 ++curi[depth];
5567
5568 c = byts[n];
5569 if (c == 0)
5570 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005571 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005572 tword[depth] = NUL;
5573 spell_soundfold(slang, tword, TRUE, tsalword);
5574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005575 // We use the "flags" field for the MSB of the wordnr,
5576 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005577 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5578 words_done >> 16, words_done & 0xffff,
5579 0) == FAIL)
5580 return FAIL;
5581
5582 ++words_done;
5583 ++wordcount[depth];
5584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005585 // Reset the block count each time to avoid compression
5586 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005587 spin->si_blocks_cnt = 0;
5588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005589 // Skip over any other NUL bytes (same word with different
5590 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005591 while (byts[n + 1] == 0)
5592 {
5593 ++n;
5594 ++curi[depth];
5595 }
5596 }
5597 else
5598 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005599 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005600 tword[depth++] = c;
5601 arridx[depth] = idxs[n];
5602 curi[depth] = 1;
5603 wordcount[depth] = 0;
5604 }
5605 }
5606 }
5607
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005608 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005609
5610 return OK;
5611}
5612
5613/*
5614 * Make the table that links each word in the soundfold trie to the words it
5615 * can be produced from.
5616 * This is not unlike lines in a file, thus use a memfile to be able to access
5617 * the table efficiently.
5618 * Returns FAIL when out of memory.
5619 */
5620 static int
5621sug_maketable(spellinfo_T *spin)
5622{
5623 garray_T ga;
5624 int res = OK;
5625
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005626 // Allocate a buffer, open a memline for it and create the swap file
5627 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005628 spin->si_spellbuf = open_spellbuf();
5629 if (spin->si_spellbuf == NULL)
5630 return FAIL;
5631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005632 // Use a buffer to store the line info, avoids allocating many small
5633 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005634 ga_init2(&ga, 1, 100);
5635
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005636 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005637 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5638 res = FAIL;
5639
5640 ga_clear(&ga);
5641 return res;
5642}
5643
5644/*
5645 * Fill the table for one node and its children.
5646 * Returns the wordnr at the start of the node.
5647 * Returns -1 when out of memory.
5648 */
5649 static int
5650sug_filltable(
5651 spellinfo_T *spin,
5652 wordnode_T *node,
5653 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005654 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005655{
5656 wordnode_T *p, *np;
5657 int wordnr = startwordnr;
5658 int nr;
5659 int prev_nr;
5660
5661 for (p = node; p != NULL; p = p->wn_sibling)
5662 {
5663 if (p->wn_byte == NUL)
5664 {
5665 gap->ga_len = 0;
5666 prev_nr = 0;
5667 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5668 {
5669 if (ga_grow(gap, 10) == FAIL)
5670 return -1;
5671
5672 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005673 // Compute the offset from the previous nr and store the
5674 // offset in a way that it takes a minimum number of bytes.
5675 // It's a bit like utf-8, but without the need to mark
5676 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005677 nr -= prev_nr;
5678 prev_nr += nr;
5679 gap->ga_len += offset2bytes(nr,
5680 (char_u *)gap->ga_data + gap->ga_len);
5681 }
5682
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005683 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005684 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5685
5686 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5687 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5688 return -1;
5689 ++wordnr;
5690
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005691 // Remove extra NUL entries, we no longer need them. We don't
5692 // bother freeing the nodes, the won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005693 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5694 p->wn_sibling = p->wn_sibling->wn_sibling;
5695
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005696 // Clear the flags on the remaining NUL node, so that compression
5697 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005698 p->wn_flags = 0;
5699 p->wn_region = 0;
5700 }
5701 else
5702 {
5703 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5704 if (wordnr == -1)
5705 return -1;
5706 }
5707 }
5708 return wordnr;
5709}
5710
5711/*
5712 * Convert an offset into a minimal number of bytes.
5713 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5714 * bytes.
5715 */
5716 static int
5717offset2bytes(int nr, char_u *buf)
5718{
5719 int rem;
5720 int b1, b2, b3, b4;
5721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005722 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005723 b1 = nr % 255 + 1;
5724 rem = nr / 255;
5725 b2 = rem % 255 + 1;
5726 rem = rem / 255;
5727 b3 = rem % 255 + 1;
5728 b4 = rem / 255 + 1;
5729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005730 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005731 {
5732 buf[0] = 0xe0 + b4;
5733 buf[1] = b3;
5734 buf[2] = b2;
5735 buf[3] = b1;
5736 return 4;
5737 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005738 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005739 {
5740 buf[0] = 0xc0 + b3;
5741 buf[1] = b2;
5742 buf[2] = b1;
5743 return 3;
5744 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005745 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005746 {
5747 buf[0] = 0x80 + b2;
5748 buf[1] = b1;
5749 return 2;
5750 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005751 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005752 buf[0] = b1;
5753 return 1;
5754}
5755
5756/*
5757 * Write the .sug file in "fname".
5758 */
5759 static void
5760sug_write(spellinfo_T *spin, char_u *fname)
5761{
5762 FILE *fd;
5763 wordnode_T *tree;
5764 int nodecount;
5765 int wcount;
5766 char_u *line;
5767 linenr_T lnum;
5768 int len;
5769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005770 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005771 fd = mch_fopen((char *)fname, "w");
5772 if (fd == NULL)
5773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005774 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005775 return;
5776 }
5777
5778 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005779 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005780 spell_message(spin, IObuff);
5781
5782 /*
5783 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5784 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005785 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005787 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005788 goto theend;
5789 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005790 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005791
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005792 // Write si_sugtime to the file.
5793 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005794
5795 /*
5796 * <SUGWORDTREE>
5797 */
5798 spin->si_memtot = 0;
5799 tree = spin->si_foldroot->wn_sibling;
5800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005801 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005802 clear_node(tree);
5803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005804 // Count the number of nodes. Needed to be able to allocate the
5805 // memory when reading the nodes. Also fills in index for shared
5806 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005807 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005809 // number of nodes in 4 bytes
5810 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005811 spin->si_memtot += nodecount + nodecount * sizeof(int);
5812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005813 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005814 (void)put_node(fd, tree, 0, 0, FALSE);
5815
5816 /*
5817 * <SUGTABLE>: <sugwcount> <sugline> ...
5818 */
5819 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005820 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005821
5822 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5823 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005824 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005825 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5826 len = (int)STRLEN(line) + 1;
5827 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005829 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005830 goto theend;
5831 }
5832 spin->si_memtot += len;
5833 }
5834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005835 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005836 if (putc(0, fd) == EOF)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005837 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005838
5839 vim_snprintf((char *)IObuff, IOSIZE,
5840 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5841 spell_message(spin, IObuff);
5842
5843theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005844 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005845 fclose(fd);
5846}
5847
5848
5849/*
5850 * Create a Vim spell file from one or more word lists.
5851 * "fnames[0]" is the output file name.
5852 * "fnames[fcount - 1]" is the last input file name.
5853 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5854 * and ".spl" is appended to make the output file name.
5855 */
5856 void
5857mkspell(
5858 int fcount,
5859 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005860 int ascii, // -ascii argument given
5861 int over_write, // overwrite existing output file
5862 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005863{
5864 char_u *fname = NULL;
5865 char_u *wfname;
5866 char_u **innames;
5867 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005868 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005869 int i;
5870 int len;
5871 stat_T st;
5872 int error = FALSE;
5873 spellinfo_T spin;
5874
5875 vim_memset(&spin, 0, sizeof(spin));
5876 spin.si_verbose = !added_word;
5877 spin.si_ascii = ascii;
5878 spin.si_followup = TRUE;
5879 spin.si_rem_accents = TRUE;
5880 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5881 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
5882 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5883 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
5884 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
5885 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
5886 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005887 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005889 // default: fnames[0] is output file, following are input files
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005890 innames = &fnames[1];
5891 incount = fcount - 1;
5892
5893 wfname = alloc(MAXPATHL);
5894 if (wfname == NULL)
5895 return;
5896
5897 if (fcount >= 1)
5898 {
5899 len = (int)STRLEN(fnames[0]);
5900 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005902 // For ":mkspell path/en.latin1.add" output file is
5903 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005904 innames = &fnames[0];
5905 incount = 1;
5906 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5907 }
5908 else if (fcount == 1)
5909 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005910 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005911 innames = &fnames[0];
5912 incount = 1;
5913 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5914 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5915 }
5916 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5917 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005918 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005919 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5920 }
5921 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005922 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005923 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5924 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005926 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005927 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5928 spin.si_ascii = TRUE;
5929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005930 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005931 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5932 spin.si_add = TRUE;
5933 }
5934
5935 if (incount <= 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005936 emsg(_(e_invarg)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005937 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005938 emsg(_("E751: Output file name must not have region name"));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005939 else if (incount > MAXREGIONS)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005940 semsg(_("E754: Only up to %d regions supported"), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005941 else
5942 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005943 // Check for overwriting before doing things that may take a lot of
5944 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005945 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
5946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005947 emsg(_(e_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005948 goto theend;
5949 }
5950 if (mch_isdir(wfname))
5951 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005952 semsg(_(e_isadir2), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005953 goto theend;
5954 }
5955
5956 fname = alloc(MAXPATHL);
5957 if (fname == NULL)
5958 goto theend;
5959
5960 /*
5961 * Init the aff and dic pointers.
5962 * Get the region names if there are more than 2 arguments.
5963 */
5964 for (i = 0; i < incount; ++i)
5965 {
5966 afile[i] = NULL;
5967
5968 if (incount > 1)
5969 {
5970 len = (int)STRLEN(innames[i]);
5971 if (STRLEN(gettail(innames[i])) < 5
5972 || innames[i][len - 3] != '_')
5973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005974 semsg(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005975 goto theend;
5976 }
5977 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5978 spin.si_region_name[i * 2 + 1] =
5979 TOLOWER_ASC(innames[i][len - 1]);
5980 }
5981 }
5982 spin.si_region_count = incount;
5983
5984 spin.si_foldroot = wordtree_alloc(&spin);
5985 spin.si_keeproot = wordtree_alloc(&spin);
5986 spin.si_prefroot = wordtree_alloc(&spin);
5987 if (spin.si_foldroot == NULL
5988 || spin.si_keeproot == NULL
5989 || spin.si_prefroot == NULL)
5990 {
5991 free_blocks(spin.si_blocks);
5992 goto theend;
5993 }
5994
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005995 // When not producing a .add.spl file clear the character table when
5996 // we encounter one in the .aff file. This means we dump the current
5997 // one in the .spl file if the .aff file doesn't define one. That's
5998 // better than guessing the contents, the table will match a
5999 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006000 if (!spin.si_add)
6001 spin.si_clear_chartab = TRUE;
6002
6003 /*
6004 * Read all the .aff and .dic files.
6005 * Text is converted to 'encoding'.
6006 * Words are stored in the case-folded and keep-case trees.
6007 */
6008 for (i = 0; i < incount && !error; ++i)
6009 {
6010 spin.si_conv.vc_type = CONV_NONE;
6011 spin.si_region = 1 << i;
6012
6013 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6014 if (mch_stat((char *)fname, &st) >= 0)
6015 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006016 // Read the .aff file. Will init "spin->si_conv" based on the
6017 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006018 afile[i] = spell_read_aff(&spin, fname);
6019 if (afile[i] == NULL)
6020 error = TRUE;
6021 else
6022 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006023 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006024 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6025 innames[i]);
6026 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6027 error = TRUE;
6028 }
6029 }
6030 else
6031 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006032 // No .aff file, try reading the file as a word list. Store
6033 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006034 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6035 error = TRUE;
6036 }
6037
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006038 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006039 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006040 }
6041
6042 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006043 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006044
6045 if (!error && !got_int)
6046 {
6047 /*
6048 * Combine tails in the tree.
6049 */
6050 spell_message(&spin, (char_u *)_(msg_compressing));
6051 wordtree_compress(&spin, spin.si_foldroot);
6052 wordtree_compress(&spin, spin.si_keeproot);
6053 wordtree_compress(&spin, spin.si_prefroot);
6054 }
6055
6056 if (!error && !got_int)
6057 {
6058 /*
6059 * Write the info in the spell file.
6060 */
6061 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006062 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006063 spell_message(&spin, IObuff);
6064
6065 error = write_vim_spell(&spin, wfname) == FAIL;
6066
6067 spell_message(&spin, (char_u *)_("Done!"));
6068 vim_snprintf((char *)IObuff, IOSIZE,
6069 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6070 spell_message(&spin, IObuff);
6071
6072 /*
6073 * If the file is loaded need to reload it.
6074 */
6075 if (!error)
6076 spell_reload_one(wfname, added_word);
6077 }
6078
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006079 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006080 ga_clear(&spin.si_rep);
6081 ga_clear(&spin.si_repsal);
6082 ga_clear(&spin.si_sal);
6083 ga_clear(&spin.si_map);
6084 ga_clear(&spin.si_comppat);
6085 ga_clear(&spin.si_prefcond);
6086 hash_clear_all(&spin.si_commonwords, 0);
6087
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006088 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006089 for (i = 0; i < incount; ++i)
6090 if (afile[i] != NULL)
6091 spell_free_aff(afile[i]);
6092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006093 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006094 free_blocks(spin.si_blocks);
6095
6096 /*
6097 * If there is soundfolding info and no NOSUGFILE item create the
6098 * .sug file with the soundfolded word trie.
6099 */
6100 if (spin.si_sugtime != 0 && !error && !got_int)
6101 spell_make_sugfile(&spin, wfname);
6102
6103 }
6104
6105theend:
6106 vim_free(fname);
6107 vim_free(wfname);
6108}
6109
6110/*
6111 * Display a message for spell file processing when 'verbose' is set or using
6112 * ":mkspell". "str" can be IObuff.
6113 */
6114 static void
6115spell_message(spellinfo_T *spin, char_u *str)
6116{
6117 if (spin->si_verbose || p_verbose > 2)
6118 {
6119 if (!spin->si_verbose)
6120 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006121 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006122 out_flush();
6123 if (!spin->si_verbose)
6124 verbose_leave();
6125 }
6126}
6127
6128/*
6129 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006130 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006131 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006132 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006133 */
6134 void
6135ex_spell(exarg_T *eap)
6136{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006137 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6138 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6139 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006140 eap->forceit ? 0 : (int)eap->line2,
6141 eap->cmdidx == CMD_spellundo);
6142}
6143
6144/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006145 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006146 */
6147 void
6148spell_add_word(
6149 char_u *word,
6150 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006151 int what, // SPELL_ADD_ values
6152 int idx, // "zG" and "zW": zero, otherwise index in
6153 // 'spellfile'
6154 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006155{
6156 FILE *fd = NULL;
6157 buf_T *buf = NULL;
6158 int new_spf = FALSE;
6159 char_u *fname;
6160 char_u *fnamebuf = NULL;
6161 char_u line[MAXWLEN * 2];
6162 long fpos, fpos_next = 0;
6163 int i;
6164 char_u *spf;
6165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006166 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006167 {
6168 if (int_wordlist == NULL)
6169 {
6170 int_wordlist = vim_tempname('s', FALSE);
6171 if (int_wordlist == NULL)
6172 return;
6173 }
6174 fname = int_wordlist;
6175 }
6176 else
6177 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006178 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006179 if (*curwin->w_s->b_p_spf == NUL)
6180 {
6181 init_spellfile();
6182 new_spf = TRUE;
6183 }
6184
6185 if (*curwin->w_s->b_p_spf == NUL)
6186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006187 semsg(_(e_notset), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006188 return;
6189 }
6190 fnamebuf = alloc(MAXPATHL);
6191 if (fnamebuf == NULL)
6192 return;
6193
6194 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6195 {
6196 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6197 if (i == idx)
6198 break;
6199 if (*spf == NUL)
6200 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006201 semsg(_("E765: 'spellfile' does not have %d entries"), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006202 vim_free(fnamebuf);
6203 return;
6204 }
6205 }
6206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006207 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006208 buf = buflist_findname_exp(fnamebuf);
6209 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6210 buf = NULL;
6211 if (buf != NULL && bufIsChanged(buf))
6212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006213 emsg(_(e_bufloaded));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006214 vim_free(fnamebuf);
6215 return;
6216 }
6217
6218 fname = fnamebuf;
6219 }
6220
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006221 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006223 // When the word appears as good word we need to remove that one,
6224 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006225 fd = mch_fopen((char *)fname, "r");
6226 if (fd != NULL)
6227 {
6228 while (!vim_fgets(line, MAXWLEN * 2, fd))
6229 {
6230 fpos = fpos_next;
6231 fpos_next = ftell(fd);
6232 if (STRNCMP(word, line, len) == 0
6233 && (line[len] == '/' || line[len] < ' '))
6234 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006235 // Found duplicate word. Remove it by writing a '#' at
6236 // the start of the line. Mixing reading and writing
6237 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006238 fclose(fd);
6239 fd = mch_fopen((char *)fname, "r+");
6240 if (fd == NULL)
6241 break;
6242 if (fseek(fd, fpos, SEEK_SET) == 0)
6243 {
6244 fputc('#', fd);
6245 if (undo)
6246 {
6247 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006248 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006249 len, word, NameBuff);
6250 }
6251 }
6252 fseek(fd, fpos_next, SEEK_SET);
6253 }
6254 }
6255 if (fd != NULL)
6256 fclose(fd);
6257 }
6258 }
6259
6260 if (!undo)
6261 {
6262 fd = mch_fopen((char *)fname, "a");
6263 if (fd == NULL && new_spf)
6264 {
6265 char_u *p;
6266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267 // We just initialized the 'spellfile' option and can't open the
6268 // file. We may need to create the "spell" directory first. We
6269 // already checked the runtime directory is writable in
6270 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006271 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6272 {
6273 int c = *p;
6274
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006275 // The directory doesn't exist. Try creating it and opening
6276 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006277 *p = NUL;
6278 vim_mkdir(fname, 0755);
6279 *p = c;
6280 fd = mch_fopen((char *)fname, "a");
6281 }
6282 }
6283
6284 if (fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006285 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006286 else
6287 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006288 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006289 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006290 else if (what == SPELL_ADD_RARE)
6291 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006292 else
6293 fprintf(fd, "%.*s\n", len, word);
6294 fclose(fd);
6295
6296 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006297 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006298 }
6299 }
6300
6301 if (fd != NULL)
6302 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006303 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006304 mkspell(1, &fname, FALSE, TRUE, TRUE);
6305
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006306 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006307 if (buf != NULL)
6308 buf_reload(buf, buf->b_orig_mode);
6309
6310 redraw_all_later(SOME_VALID);
6311 }
6312 vim_free(fnamebuf);
6313}
6314
6315/*
6316 * Initialize 'spellfile' for the current buffer.
6317 */
6318 static void
6319init_spellfile(void)
6320{
6321 char_u *buf;
6322 int l;
6323 char_u *fname;
6324 char_u *rtp;
6325 char_u *lend;
6326 int aspath = FALSE;
6327 char_u *lstart = curbuf->b_s.b_p_spl;
6328
6329 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6330 {
6331 buf = alloc(MAXPATHL);
6332 if (buf == NULL)
6333 return;
6334
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006335 // Find the end of the language name. Exclude the region. If there
6336 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006337 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6338 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6339 if (vim_ispathsep(*lend))
6340 {
6341 aspath = TRUE;
6342 lstart = lend + 1;
6343 }
6344
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006345 // Loop over all entries in 'runtimepath'. Use the first one where we
6346 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006347 rtp = p_rtp;
6348 while (*rtp != NUL)
6349 {
6350 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006351 // Use directory of an entry with path, e.g., for
6352 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006353 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6354 lstart - curbuf->b_s.b_p_spl - 1);
6355 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006356 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006357 copy_option_part(&rtp, buf, MAXPATHL, ",");
6358 if (filewritable(buf) == 2)
6359 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006360 // Use the first language name from 'spelllang' and the
6361 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006362 if (aspath)
6363 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6364 lend - curbuf->b_s.b_p_spl);
6365 else
6366 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006367 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006368 l = (int)STRLEN(buf);
6369 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6370 if (filewritable(buf) != 2)
6371 vim_mkdir(buf, 0755);
6372
6373 l = (int)STRLEN(buf);
6374 vim_snprintf((char *)buf + l, MAXPATHL - l,
6375 "/%.*s", (int)(lend - lstart), lstart);
6376 }
6377 l = (int)STRLEN(buf);
6378 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6379 ->lp_slang->sl_fname;
6380 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6381 fname != NULL
6382 && strstr((char *)gettail(fname), ".ascii.") != NULL
6383 ? (char_u *)"ascii" : spell_enc());
6384 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6385 break;
6386 }
6387 aspath = FALSE;
6388 }
6389
6390 vim_free(buf);
6391 }
6392}
6393
6394
6395
6396/*
6397 * Set the spell character tables from strings in the affix file.
6398 */
6399 static int
6400set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6401{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006402 // We build the new tables here first, so that we can compare with the
6403 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006404 spelltab_T new_st;
6405 char_u *pf = fol, *pl = low, *pu = upp;
6406 int f, l, u;
6407
6408 clear_spell_chartab(&new_st);
6409
6410 while (*pf != NUL)
6411 {
6412 if (*pl == NUL || *pu == NUL)
6413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006414 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006415 return FAIL;
6416 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006417 f = mb_ptr2char_adv(&pf);
6418 l = mb_ptr2char_adv(&pl);
6419 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006421 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006422 if (f < 256)
6423 new_st.st_isw[f] = TRUE;
6424 if (l < 256)
6425 new_st.st_isw[l] = TRUE;
6426 if (u < 256)
6427 new_st.st_isw[u] = TRUE;
6428
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006429 // if "LOW" and "FOL" are not the same the "LOW" char needs
6430 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006431 if (l < 256 && l != f)
6432 {
6433 if (f >= 256)
6434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006435 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006436 return FAIL;
6437 }
6438 new_st.st_fold[l] = f;
6439 }
6440
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006441 // if "UPP" and "FOL" are not the same the "UPP" char needs
6442 // case-folding, it's upper case and the "UPP" is the upper case of
6443 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006444 if (u < 256 && u != f)
6445 {
6446 if (f >= 256)
6447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006448 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006449 return FAIL;
6450 }
6451 new_st.st_fold[u] = f;
6452 new_st.st_isu[u] = TRUE;
6453 new_st.st_upper[f] = u;
6454 }
6455 }
6456
6457 if (*pl != NUL || *pu != NUL)
6458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006459 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006460 return FAIL;
6461 }
6462
6463 return set_spell_finish(&new_st);
6464}
6465
6466/*
6467 * Set the spell character tables from strings in the .spl file.
6468 */
6469 static void
6470set_spell_charflags(
6471 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006472 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006473 char_u *fol)
6474{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006475 // We build the new tables here first, so that we can compare with the
6476 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006477 spelltab_T new_st;
6478 int i;
6479 char_u *p = fol;
6480 int c;
6481
6482 clear_spell_chartab(&new_st);
6483
6484 for (i = 0; i < 128; ++i)
6485 {
6486 if (i < cnt)
6487 {
6488 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6489 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6490 }
6491
6492 if (*p != NUL)
6493 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006494 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006495 new_st.st_fold[i + 128] = c;
6496 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6497 new_st.st_upper[c] = i + 128;
6498 }
6499 }
6500
6501 (void)set_spell_finish(&new_st);
6502}
6503
6504 static int
6505set_spell_finish(spelltab_T *new_st)
6506{
6507 int i;
6508
6509 if (did_set_spelltab)
6510 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006511 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006512 for (i = 0; i < 256; ++i)
6513 {
6514 if (spelltab.st_isw[i] != new_st->st_isw[i]
6515 || spelltab.st_isu[i] != new_st->st_isu[i]
6516 || spelltab.st_fold[i] != new_st->st_fold[i]
6517 || spelltab.st_upper[i] != new_st->st_upper[i])
6518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006519 emsg(_("E763: Word characters differ between spell files"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006520 return FAIL;
6521 }
6522 }
6523 }
6524 else
6525 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006526 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006527 spelltab = *new_st;
6528 did_set_spelltab = TRUE;
6529 }
6530
6531 return OK;
6532}
6533
6534/*
6535 * Write the table with prefix conditions to the .spl file.
6536 * When "fd" is NULL only count the length of what is written.
6537 */
6538 static int
6539write_spell_prefcond(FILE *fd, garray_T *gap)
6540{
6541 int i;
6542 char_u *p;
6543 int len;
6544 int totlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006545 size_t x = 1; // collect return value of fwrite()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006546
6547 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006548 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006550 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006551
6552 for (i = 0; i < gap->ga_len; ++i)
6553 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006554 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006555 p = ((char_u **)gap->ga_data)[i];
6556 if (p != NULL)
6557 {
6558 len = (int)STRLEN(p);
6559 if (fd != NULL)
6560 {
6561 fputc(len, fd);
6562 x &= fwrite(p, (size_t)len, (size_t)1, fd);
6563 }
6564 totlen += len;
6565 }
6566 else if (fd != NULL)
6567 fputc(0, fd);
6568 }
6569
6570 return totlen;
6571}
6572
6573
6574/*
6575 * Use map string "map" for languages "lp".
6576 */
6577 static void
6578set_map_str(slang_T *lp, char_u *map)
6579{
6580 char_u *p;
6581 int headc = 0;
6582 int c;
6583 int i;
6584
6585 if (*map == NUL)
6586 {
6587 lp->sl_has_map = FALSE;
6588 return;
6589 }
6590 lp->sl_has_map = TRUE;
6591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006592 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006593 for (i = 0; i < 256; ++i)
6594 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006595 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006596
6597 /*
6598 * The similar characters are stored separated with slashes:
6599 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6600 * before the same slash. For characters above 255 sl_map_hash is used.
6601 */
6602 for (p = map; *p != NUL; )
6603 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006604 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006605 if (c == '/')
6606 headc = 0;
6607 else
6608 {
6609 if (headc == 0)
6610 headc = c;
6611
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006612 // Characters above 255 don't fit in sl_map_array[], put them in
6613 // the hash table. Each entry is the char, a NUL the headchar and
6614 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006615 if (c >= 256)
6616 {
6617 int cl = mb_char2len(c);
6618 int headcl = mb_char2len(headc);
6619 char_u *b;
6620 hash_T hash;
6621 hashitem_T *hi;
6622
Bram Moolenaar964b3742019-05-24 18:54:09 +02006623 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006624 if (b == NULL)
6625 return;
6626 mb_char2bytes(c, b);
6627 b[cl] = NUL;
6628 mb_char2bytes(headc, b + cl + 1);
6629 b[cl + 1 + headcl] = NUL;
6630 hash = hash_hash(b);
6631 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6632 if (HASHITEM_EMPTY(hi))
6633 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6634 else
6635 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006636 // This should have been checked when generating the .spl
6637 // file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006638 emsg(_("E783: duplicate char in MAP entry"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006639 vim_free(b);
6640 }
6641 }
6642 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006643 lp->sl_map_array[c] = headc;
6644 }
6645 }
6646}
6647
6648
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006649#endif // FEAT_SPELL