blob: b9451ecd6d28751fd722bb079d451c7fa9b43fb4 [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;
352 char_u *save_sourcing_name = sourcing_name;
353 linenr_T save_sourcing_lnum = sourcing_lnum;
354 slang_T *lp = NULL;
355 int c = 0;
356 int res;
357
358 fd = mch_fopen((char *)fname, "r");
359 if (fd == NULL)
360 {
361 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100362 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200363 else if (p_verbose > 2)
364 {
365 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100366 smsg((const char *)e_notopen, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200367 verbose_leave();
368 }
369 goto endFAIL;
370 }
371 if (p_verbose > 2)
372 {
373 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100374 smsg(_("Reading spell file \"%s\""), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200375 verbose_leave();
376 }
377
378 if (old_lp == NULL)
379 {
380 lp = slang_alloc(lang);
381 if (lp == NULL)
382 goto endFAIL;
383
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100384 // Remember the file name, used to reload the file when it's updated.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200385 lp->sl_fname = vim_strsave(fname);
386 if (lp->sl_fname == NULL)
387 goto endFAIL;
388
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100389 // Check for .add.spl (_add.spl for VMS).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200390 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
391 }
392 else
393 lp = old_lp;
394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100395 // Set sourcing_name, so that error messages mention the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200396 sourcing_name = fname;
397 sourcing_lnum = 0;
398
399 /*
400 * <HEADER>: <fileID>
401 */
402 for (i = 0; i < VIMSPELLMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100403 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200404 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100406 emsg(_("E757: This does not look like a spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200407 goto endFAIL;
408 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100409 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200410 if (c < VIMSPELLVERSION)
411 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100412 emsg(_("E771: Old spell file, needs to be updated"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200413 goto endFAIL;
414 }
415 else if (c > VIMSPELLVERSION)
416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100417 emsg(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200418 goto endFAIL;
419 }
420
421
422 /*
423 * <SECTIONS>: <section> ... <sectionend>
424 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
425 */
426 for (;;)
427 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100428 n = getc(fd); // <sectionID> or <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200429 if (n == SN_END)
430 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100431 c = getc(fd); // <sectionflags>
432 len = get4c(fd); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200433 if (len < 0)
434 goto truncerr;
435
436 res = 0;
437 switch (n)
438 {
439 case SN_INFO:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100440 lp->sl_info = read_string(fd, len); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200441 if (lp->sl_info == NULL)
442 goto endFAIL;
443 break;
444
445 case SN_REGION:
446 res = read_region_section(fd, lp, len);
447 break;
448
449 case SN_CHARFLAGS:
450 res = read_charflags_section(fd);
451 break;
452
453 case SN_MIDWORD:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100454 lp->sl_midword = read_string(fd, len); // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200455 if (lp->sl_midword == NULL)
456 goto endFAIL;
457 break;
458
459 case SN_PREFCOND:
460 res = read_prefcond_section(fd, lp);
461 break;
462
463 case SN_REP:
464 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
465 break;
466
467 case SN_REPSAL:
468 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
469 break;
470
471 case SN_SAL:
472 res = read_sal_section(fd, lp);
473 break;
474
475 case SN_SOFO:
476 res = read_sofo_section(fd, lp);
477 break;
478
479 case SN_MAP:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 p = read_string(fd, len); // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200481 if (p == NULL)
482 goto endFAIL;
483 set_map_str(lp, p);
484 vim_free(p);
485 break;
486
487 case SN_WORDS:
488 res = read_words_section(fd, lp, len);
489 break;
490
491 case SN_SUGFILE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100492 lp->sl_sugtime = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200493 break;
494
495 case SN_NOSPLITSUGS:
496 lp->sl_nosplitsugs = TRUE;
497 break;
498
499 case SN_NOCOMPOUNDSUGS:
500 lp->sl_nocompoundsugs = TRUE;
501 break;
502
503 case SN_COMPOUND:
504 res = read_compound(fd, lp, len);
505 break;
506
507 case SN_NOBREAK:
508 lp->sl_nobreak = TRUE;
509 break;
510
511 case SN_SYLLABLE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100512 lp->sl_syllable = read_string(fd, len); // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200513 if (lp->sl_syllable == NULL)
514 goto endFAIL;
515 if (init_syl_tab(lp) == FAIL)
516 goto endFAIL;
517 break;
518
519 default:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100520 // Unsupported section. When it's required give an error
521 // message. When it's not required skip the contents.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200522 if (c & SNF_REQUIRED)
523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100524 emsg(_("E770: Unsupported section in spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200525 goto endFAIL;
526 }
527 while (--len >= 0)
528 if (getc(fd) < 0)
529 goto truncerr;
530 break;
531 }
532someerror:
533 if (res == SP_FORMERROR)
534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100535 emsg(_(e_format));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200536 goto endFAIL;
537 }
538 if (res == SP_TRUNCERROR)
539 {
540truncerr:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100541 emsg(_(e_spell_trunc));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200542 goto endFAIL;
543 }
544 if (res == SP_OTHERERROR)
545 goto endFAIL;
546 }
547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100548 // <LWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200549 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
550 if (res != 0)
551 goto someerror;
552
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100553 // <KWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200554 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
555 if (res != 0)
556 goto someerror;
557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100558 // <PREFIXTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200559 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
560 lp->sl_prefixcnt);
561 if (res != 0)
562 goto someerror;
563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100564 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200565 if (old_lp == NULL && lang != NULL)
566 {
567 lp->sl_next = first_lang;
568 first_lang = lp;
569 }
570
571 goto endOK;
572
573endFAIL:
574 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100575 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200576 *lang = NUL;
577 if (lp != NULL && old_lp == NULL)
578 slang_free(lp);
579 lp = NULL;
580
581endOK:
582 if (fd != NULL)
583 fclose(fd);
584 sourcing_name = save_sourcing_name;
585 sourcing_lnum = save_sourcing_lnum;
586
587 return lp;
588}
589
590/*
591 * Fill in the wordcount fields for a trie.
592 * Returns the total number of words.
593 */
594 static void
595tree_count_words(char_u *byts, idx_T *idxs)
596{
597 int depth;
598 idx_T arridx[MAXWLEN];
599 int curi[MAXWLEN];
600 int c;
601 idx_T n;
602 int wordcount[MAXWLEN];
603
604 arridx[0] = 0;
605 curi[0] = 1;
606 wordcount[0] = 0;
607 depth = 0;
608 while (depth >= 0 && !got_int)
609 {
610 if (curi[depth] > byts[arridx[depth]])
611 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100612 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200613 idxs[arridx[depth]] = wordcount[depth];
614 if (depth > 0)
615 wordcount[depth - 1] += wordcount[depth];
616
617 --depth;
618 fast_breakcheck();
619 }
620 else
621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100622 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200623 n = arridx[depth] + curi[depth];
624 ++curi[depth];
625
626 c = byts[n];
627 if (c == 0)
628 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100629 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200630 ++wordcount[depth];
631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100632 // Skip over any other NUL bytes (same word with different
633 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200634 while (byts[n + 1] == 0)
635 {
636 ++n;
637 ++curi[depth];
638 }
639 }
640 else
641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100642 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200643 ++depth;
644 arridx[depth] = idxs[n];
645 curi[depth] = 1;
646 wordcount[depth] = 0;
647 }
648 }
649 }
650}
651
652/*
653 * Load the .sug files for languages that have one and weren't loaded yet.
654 */
655 void
656suggest_load_files(void)
657{
658 langp_T *lp;
659 int lpi;
660 slang_T *slang;
661 char_u *dotp;
662 FILE *fd;
663 char_u buf[MAXWLEN];
664 int i;
665 time_t timestamp;
666 int wcount;
667 int wordnr;
668 garray_T ga;
669 int c;
670
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100671 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200672 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
673 {
674 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
675 slang = lp->lp_slang;
676 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
677 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100678 // Change ".spl" to ".sug" and open the file. When the file isn't
679 // found silently skip it. Do set "sl_sugloaded" so that we
680 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200681 slang->sl_sugloaded = TRUE;
682
683 dotp = vim_strrchr(slang->sl_fname, '.');
684 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
685 continue;
686 STRCPY(dotp, ".sug");
687 fd = mch_fopen((char *)slang->sl_fname, "r");
688 if (fd == NULL)
689 goto nextone;
690
691 /*
692 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
693 */
694 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100695 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200696 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100698 semsg(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200699 slang->sl_fname);
700 goto nextone;
701 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100702 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200703 if (c < VIMSUGVERSION)
704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100705 semsg(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200706 slang->sl_fname);
707 goto nextone;
708 }
709 else if (c > VIMSUGVERSION)
710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100711 semsg(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200712 slang->sl_fname);
713 goto nextone;
714 }
715
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100716 // Check the timestamp, it must be exactly the same as the one in
717 // the .spl file. Otherwise the word numbers won't match.
718 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200719 if (timestamp != slang->sl_sugtime)
720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 semsg(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200722 slang->sl_fname);
723 goto nextone;
724 }
725
726 /*
727 * <SUGWORDTREE>: <wordtree>
728 * Read the trie with the soundfolded words.
729 */
730 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
731 FALSE, 0) != 0)
732 {
733someerror:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 semsg(_("E782: error while reading .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200735 slang->sl_fname);
736 slang_clear_sug(slang);
737 goto nextone;
738 }
739
740 /*
741 * <SUGTABLE>: <sugwcount> <sugline> ...
742 *
743 * Read the table with word numbers. We use a file buffer for
744 * this, because it's so much like a file with lines. Makes it
745 * possible to swap the info and save on memory use.
746 */
747 slang->sl_sugbuf = open_spellbuf();
748 if (slang->sl_sugbuf == NULL)
749 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100750 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200751 wcount = get4c(fd);
752 if (wcount < 0)
753 goto someerror;
754
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100755 // Read all the wordnr lists into the buffer, one NUL terminated
756 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200757 ga_init2(&ga, 1, 100);
758 for (wordnr = 0; wordnr < wcount; ++wordnr)
759 {
760 ga.ga_len = 0;
761 for (;;)
762 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100763 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200764 if (c < 0 || ga_grow(&ga, 1) == FAIL)
765 goto someerror;
766 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
767 if (c == NUL)
768 break;
769 }
770 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
771 ga.ga_data, ga.ga_len, TRUE) == FAIL)
772 goto someerror;
773 }
774 ga_clear(&ga);
775
776 /*
777 * Need to put word counts in the word tries, so that we can find
778 * a word by its number.
779 */
780 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
781 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
782
783nextone:
784 if (fd != NULL)
785 fclose(fd);
786 STRCPY(dotp, ".spl");
787 }
788 }
789}
790
791
792/*
793 * Read a length field from "fd" in "cnt_bytes" bytes.
794 * Allocate memory, read the string into it and add a NUL at the end.
795 * Returns NULL when the count is zero.
796 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
797 * otherwise.
798 */
799 static char_u *
800read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
801{
802 int cnt = 0;
803 int i;
804 char_u *str;
805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100806 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200807 for (i = 0; i < cnt_bytes; ++i)
808 cnt = (cnt << 8) + getc(fd);
809 if (cnt < 0)
810 {
811 *cntp = SP_TRUNCERROR;
812 return NULL;
813 }
814 *cntp = cnt;
815 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100816 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200817
818 str = read_string(fd, cnt);
819 if (str == NULL)
820 *cntp = SP_OTHERERROR;
821 return str;
822}
823
824/*
825 * Read SN_REGION: <regionname> ...
826 * Return SP_*ERROR flags.
827 */
828 static int
829read_region_section(FILE *fd, slang_T *lp, int len)
830{
831 int i;
832
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100833 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200834 return SP_FORMERROR;
835 for (i = 0; i < len; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100836 lp->sl_regions[i] = getc(fd); // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200837 lp->sl_regions[len] = NUL;
838 return 0;
839}
840
841/*
842 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
843 * <folcharslen> <folchars>
844 * Return SP_*ERROR flags.
845 */
846 static int
847read_charflags_section(FILE *fd)
848{
849 char_u *flags;
850 char_u *fol;
851 int flagslen, follen;
852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100853 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200854 flags = read_cnt_string(fd, 1, &flagslen);
855 if (flagslen < 0)
856 return flagslen;
857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100858 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200859 fol = read_cnt_string(fd, 2, &follen);
860 if (follen < 0)
861 {
862 vim_free(flags);
863 return follen;
864 }
865
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100866 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200867 if (flags != NULL && fol != NULL)
868 set_spell_charflags(flags, flagslen, fol);
869
870 vim_free(flags);
871 vim_free(fol);
872
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100873 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200874 if ((flags == NULL) != (fol == NULL))
875 return SP_FORMERROR;
876 return 0;
877}
878
879/*
880 * Read SN_PREFCOND section.
881 * Return SP_*ERROR flags.
882 */
883 static int
884read_prefcond_section(FILE *fd, slang_T *lp)
885{
886 int cnt;
887 int i;
888 int n;
889 char_u *p;
890 char_u buf[MAXWLEN + 1];
891
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100892 // <prefcondcnt> <prefcond> ...
893 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200894 if (cnt <= 0)
895 return SP_FORMERROR;
896
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200897 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200898 if (lp->sl_prefprog == NULL)
899 return SP_OTHERERROR;
900 lp->sl_prefixcnt = cnt;
901
902 for (i = 0; i < cnt; ++i)
903 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100904 // <prefcond> : <condlen> <condstr>
905 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200906 if (n < 0 || n >= MAXWLEN)
907 return SP_FORMERROR;
908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100909 // When <condlen> is zero we have an empty condition. Otherwise
910 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200911 if (n > 0)
912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100913 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200914 p = buf + 1;
915 while (n-- > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100916 *p++ = getc(fd); // <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200917 *p = NUL;
918 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
919 }
920 }
921 return 0;
922}
923
924/*
925 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
926 * Return SP_*ERROR flags.
927 */
928 static int
929read_rep_section(FILE *fd, garray_T *gap, short *first)
930{
931 int cnt;
932 fromto_T *ftp;
933 int i;
934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100935 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200936 if (cnt < 0)
937 return SP_TRUNCERROR;
938
939 if (ga_grow(gap, cnt) == FAIL)
940 return SP_OTHERERROR;
941
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100942 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200943 for (; gap->ga_len < cnt; ++gap->ga_len)
944 {
945 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
946 ftp->ft_from = read_cnt_string(fd, 1, &i);
947 if (i < 0)
948 return i;
949 if (i == 0)
950 return SP_FORMERROR;
951 ftp->ft_to = read_cnt_string(fd, 1, &i);
952 if (i <= 0)
953 {
954 vim_free(ftp->ft_from);
955 if (i < 0)
956 return i;
957 return SP_FORMERROR;
958 }
959 }
960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100961 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200962 for (i = 0; i < 256; ++i)
963 first[i] = -1;
964 for (i = 0; i < gap->ga_len; ++i)
965 {
966 ftp = &((fromto_T *)gap->ga_data)[i];
967 if (first[*ftp->ft_from] == -1)
968 first[*ftp->ft_from] = i;
969 }
970 return 0;
971}
972
973/*
974 * Read SN_SAL section: <salflags> <salcount> <sal> ...
975 * Return SP_*ERROR flags.
976 */
977 static int
978read_sal_section(FILE *fd, slang_T *slang)
979{
980 int i;
981 int cnt;
982 garray_T *gap;
983 salitem_T *smp;
984 int ccnt;
985 char_u *p;
986 int c = NUL;
987
988 slang->sl_sofo = FALSE;
989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100990 i = getc(fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200991 if (i & SAL_F0LLOWUP)
992 slang->sl_followup = TRUE;
993 if (i & SAL_COLLAPSE)
994 slang->sl_collapse = TRUE;
995 if (i & SAL_REM_ACCENTS)
996 slang->sl_rem_accents = TRUE;
997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100998 cnt = get2c(fd); // <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200999 if (cnt < 0)
1000 return SP_TRUNCERROR;
1001
1002 gap = &slang->sl_sal;
1003 ga_init2(gap, sizeof(salitem_T), 10);
1004 if (ga_grow(gap, cnt + 1) == FAIL)
1005 return SP_OTHERERROR;
1006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001007 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001008 for (; gap->ga_len < cnt; ++gap->ga_len)
1009 {
1010 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001011 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001012 if (ccnt < 0)
1013 return SP_TRUNCERROR;
1014 if ((p = alloc(ccnt + 2)) == NULL)
1015 return SP_OTHERERROR;
1016 smp->sm_lead = p;
1017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001018 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001019 for (i = 0; i < ccnt; ++i)
1020 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001021 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001022 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1023 break;
1024 *p++ = c;
1025 }
1026 smp->sm_leadlen = (int)(p - smp->sm_lead);
1027 *p++ = NUL;
1028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001029 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001030 if (c == '(')
1031 {
1032 smp->sm_oneof = p;
1033 for (++i; i < ccnt; ++i)
1034 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001035 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001036 if (c == ')')
1037 break;
1038 *p++ = c;
1039 }
1040 *p++ = NUL;
1041 if (++i < ccnt)
1042 c = getc(fd);
1043 }
1044 else
1045 smp->sm_oneof = NULL;
1046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001047 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001048 smp->sm_rules = p;
1049 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001050 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001051 *p++ = c;
1052 for (++i; i < ccnt; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001053 *p++ = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001054 *p++ = NUL;
1055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001056 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001057 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1058 if (ccnt < 0)
1059 {
1060 vim_free(smp->sm_lead);
1061 return ccnt;
1062 }
1063
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001064 if (has_mbyte)
1065 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001066 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001067 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1068 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1069 if (smp->sm_oneof == NULL)
1070 smp->sm_oneof_w = NULL;
1071 else
1072 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1073 if (smp->sm_to == NULL)
1074 smp->sm_to_w = NULL;
1075 else
1076 smp->sm_to_w = mb_str2wide(smp->sm_to);
1077 if (smp->sm_lead_w == NULL
1078 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1079 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1080 {
1081 vim_free(smp->sm_lead);
1082 vim_free(smp->sm_to);
1083 vim_free(smp->sm_lead_w);
1084 vim_free(smp->sm_oneof_w);
1085 vim_free(smp->sm_to_w);
1086 return SP_OTHERERROR;
1087 }
1088 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001089 }
1090
1091 if (gap->ga_len > 0)
1092 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001093 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1094 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001095 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1096 if ((p = alloc(1)) == NULL)
1097 return SP_OTHERERROR;
1098 p[0] = NUL;
1099 smp->sm_lead = p;
1100 smp->sm_leadlen = 0;
1101 smp->sm_oneof = NULL;
1102 smp->sm_rules = p;
1103 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001104 if (has_mbyte)
1105 {
1106 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1107 smp->sm_leadlen = 0;
1108 smp->sm_oneof_w = NULL;
1109 smp->sm_to_w = NULL;
1110 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001111 ++gap->ga_len;
1112 }
1113
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001114 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001115 set_sal_first(slang);
1116
1117 return 0;
1118}
1119
1120/*
1121 * Read SN_WORDS: <word> ...
1122 * Return SP_*ERROR flags.
1123 */
1124 static int
1125read_words_section(FILE *fd, slang_T *lp, int len)
1126{
1127 int done = 0;
1128 int i;
1129 int c;
1130 char_u word[MAXWLEN];
1131
1132 while (done < len)
1133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001134 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001135 for (i = 0; ; ++i)
1136 {
1137 c = getc(fd);
1138 if (c == EOF)
1139 return SP_TRUNCERROR;
1140 word[i] = c;
1141 if (word[i] == NUL)
1142 break;
1143 if (i == MAXWLEN - 1)
1144 return SP_FORMERROR;
1145 }
1146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001147 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001148 count_common_word(lp, word, -1, 10);
1149 done += i + 1;
1150 }
1151 return 0;
1152}
1153
1154/*
1155 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1156 * Return SP_*ERROR flags.
1157 */
1158 static int
1159read_sofo_section(FILE *fd, slang_T *slang)
1160{
1161 int cnt;
1162 char_u *from, *to;
1163 int res;
1164
1165 slang->sl_sofo = TRUE;
1166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001167 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001168 from = read_cnt_string(fd, 2, &cnt);
1169 if (cnt < 0)
1170 return cnt;
1171
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001172 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001173 to = read_cnt_string(fd, 2, &cnt);
1174 if (cnt < 0)
1175 {
1176 vim_free(from);
1177 return cnt;
1178 }
1179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001180 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001181 if (from != NULL && to != NULL)
1182 res = set_sofo(slang, from, to);
1183 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001184 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001185 else
1186 res = 0;
1187
1188 vim_free(from);
1189 vim_free(to);
1190 return res;
1191}
1192
1193/*
1194 * Read the compound section from the .spl file:
1195 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1196 * Returns SP_*ERROR flags.
1197 */
1198 static int
1199read_compound(FILE *fd, slang_T *slang, int len)
1200{
1201 int todo = len;
1202 int c;
1203 int atstart;
1204 char_u *pat;
1205 char_u *pp;
1206 char_u *cp;
1207 char_u *ap;
1208 char_u *crp;
1209 int cnt;
1210 garray_T *gap;
1211
1212 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001213 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001214
1215 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001216 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001217 if (c < 2)
1218 c = MAXWLEN;
1219 slang->sl_compmax = c;
1220
1221 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001222 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001223 if (c < 1)
1224 c = 0;
1225 slang->sl_compminlen = c;
1226
1227 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001228 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001229 if (c < 1)
1230 c = MAXWLEN;
1231 slang->sl_compsylmax = c;
1232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001233 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001234 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001235 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001236 else
1237 {
1238 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001239 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001240 --todo;
1241 slang->sl_compoptions = c;
1242
1243 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001244 c = get2c(fd); // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001245 todo -= 2;
1246 ga_init2(gap, sizeof(char_u *), c);
1247 if (ga_grow(gap, c) == OK)
1248 while (--c >= 0)
1249 {
1250 ((char_u **)(gap->ga_data))[gap->ga_len++] =
1251 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001252 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001253 if (cnt < 0)
1254 return cnt;
1255 todo -= cnt + 1;
1256 }
1257 }
1258 if (todo < 0)
1259 return SP_FORMERROR;
1260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001261 // Turn the COMPOUNDRULE items into a regexp pattern:
1262 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1263 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1264 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001265 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001266 if (enc_utf8)
1267 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001268 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001269 if (pat == NULL)
1270 return SP_OTHERERROR;
1271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001272 // We also need a list of all flags that can appear at the start and one
1273 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001274 cp = alloc(todo + 1);
1275 if (cp == NULL)
1276 {
1277 vim_free(pat);
1278 return SP_OTHERERROR;
1279 }
1280 slang->sl_compstartflags = cp;
1281 *cp = NUL;
1282
1283 ap = alloc(todo + 1);
1284 if (ap == NULL)
1285 {
1286 vim_free(pat);
1287 return SP_OTHERERROR;
1288 }
1289 slang->sl_compallflags = ap;
1290 *ap = NUL;
1291
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001292 // And a list of all patterns in their original form, for checking whether
1293 // compounding may work in match_compoundrule(). This is freed when we
1294 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001295 crp = alloc(todo + 1);
1296 slang->sl_comprules = crp;
1297
1298 pp = pat;
1299 *pp++ = '^';
1300 *pp++ = '\\';
1301 *pp++ = '(';
1302
1303 atstart = 1;
1304 while (todo-- > 0)
1305 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001306 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001307 if (c == EOF)
1308 {
1309 vim_free(pat);
1310 return SP_TRUNCERROR;
1311 }
1312
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001313 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001314 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1315 && !byte_in_str(slang->sl_compallflags, c))
1316 {
1317 *ap++ = c;
1318 *ap = NUL;
1319 }
1320
1321 if (atstart != 0)
1322 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001323 // At start of item: copy flags to "sl_compstartflags". For a
1324 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001325 if (c == '[')
1326 atstart = 2;
1327 else if (c == ']')
1328 atstart = 0;
1329 else
1330 {
1331 if (!byte_in_str(slang->sl_compstartflags, c))
1332 {
1333 *cp++ = c;
1334 *cp = NUL;
1335 }
1336 if (atstart == 1)
1337 atstart = 0;
1338 }
1339 }
1340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001342 if (crp != NULL)
1343 {
1344 if (c == '?' || c == '+' || c == '*')
1345 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001346 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001347 crp = NULL;
1348 }
1349 else
1350 *crp++ = c;
1351 }
1352
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001353 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001354 {
1355 *pp++ = '\\';
1356 *pp++ = '|';
1357 atstart = 1;
1358 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001359 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001360 {
1361 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001362 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001363 if (enc_utf8)
1364 pp += mb_char2bytes(c, pp);
1365 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001366 *pp++ = c;
1367 }
1368 }
1369
1370 *pp++ = '\\';
1371 *pp++ = ')';
1372 *pp++ = '$';
1373 *pp = NUL;
1374
1375 if (crp != NULL)
1376 *crp = NUL;
1377
1378 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1379 vim_free(pat);
1380 if (slang->sl_compprog == NULL)
1381 return SP_FORMERROR;
1382
1383 return 0;
1384}
1385
1386/*
1387 * Set the SOFOFROM and SOFOTO items in language "lp".
1388 * Returns SP_*ERROR flags when there is something wrong.
1389 */
1390 static int
1391set_sofo(slang_T *lp, char_u *from, char_u *to)
1392{
1393 int i;
1394
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001395 garray_T *gap;
1396 char_u *s;
1397 char_u *p;
1398 int c;
1399 int *inp;
1400
1401 if (has_mbyte)
1402 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001403 // Use "sl_sal" as an array with 256 pointers to a list of wide
1404 // characters. The index is the low byte of the character.
1405 // The list contains from-to pairs with a terminating NUL.
1406 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001407 gap = &lp->sl_sal;
1408 ga_init2(gap, sizeof(int *), 1);
1409 if (ga_grow(gap, 256) == FAIL)
1410 return SP_OTHERERROR;
1411 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1412 gap->ga_len = 256;
1413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001414 // First count the number of items for each list. Temporarily use
1415 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001416 for (p = from, s = to; *p != NUL && *s != NUL; )
1417 {
1418 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001419 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001420 if (c >= 256)
1421 ++lp->sl_sal_first[c & 0xff];
1422 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001424 return SP_FORMERROR;
1425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001427 for (i = 0; i < 256; ++i)
1428 if (lp->sl_sal_first[i] > 0)
1429 {
1430 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1431 if (p == NULL)
1432 return SP_OTHERERROR;
1433 ((int **)gap->ga_data)[i] = (int *)p;
1434 *(int *)p = 0;
1435 }
1436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1438 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001439 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1440 for (p = from, s = to; *p != NUL && *s != NUL; )
1441 {
1442 c = mb_cptr2char_adv(&p);
1443 i = mb_cptr2char_adv(&s);
1444 if (c >= 256)
1445 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 // Append the from-to chars at the end of the list with
1447 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001448 inp = ((int **)gap->ga_data)[c & 0xff];
1449 while (*inp != 0)
1450 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001451 *inp++ = c; // from char
1452 *inp++ = i; // to char
1453 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001454 }
1455 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001456 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001457 lp->sl_sal_first[c] = i;
1458 }
1459 }
1460 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001461 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001462 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001463 if (STRLEN(from) != STRLEN(to))
1464 return SP_FORMERROR;
1465
1466 for (i = 0; to[i] != NUL; ++i)
1467 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001468 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001469 }
1470
1471 return 0;
1472}
1473
1474/*
1475 * Fill the first-index table for "lp".
1476 */
1477 static void
1478set_sal_first(slang_T *lp)
1479{
1480 salfirst_T *sfirst;
1481 int i;
1482 salitem_T *smp;
1483 int c;
1484 garray_T *gap = &lp->sl_sal;
1485
1486 sfirst = lp->sl_sal_first;
1487 for (i = 0; i < 256; ++i)
1488 sfirst[i] = -1;
1489 smp = (salitem_T *)gap->ga_data;
1490 for (i = 0; i < gap->ga_len; ++i)
1491 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001492 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001493 // Use the lowest byte of the first character. For latin1 it's
1494 // the character, for other encodings it should differ for most
1495 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001496 c = *smp[i].sm_lead_w & 0xff;
1497 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001498 c = *smp[i].sm_lead;
1499 if (sfirst[c] == -1)
1500 {
1501 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001502 if (has_mbyte)
1503 {
1504 int n;
1505
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001506 // Make sure all entries with this byte are following each
1507 // other. Move the ones that are in the wrong position. Do
1508 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001509 while (i + 1 < gap->ga_len
1510 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001511 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001512 ++i;
1513
1514 for (n = 1; i + n < gap->ga_len; ++n)
1515 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1516 {
1517 salitem_T tsal;
1518
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001519 // Move entry with same index byte after the entries
1520 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001521 ++i;
1522 --n;
1523 tsal = smp[i + n];
1524 mch_memmove(smp + i + 1, smp + i,
1525 sizeof(salitem_T) * n);
1526 smp[i] = tsal;
1527 }
1528 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001529 }
1530 }
1531}
1532
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001533/*
1534 * Turn a multi-byte string into a wide character string.
1535 * Return it in allocated memory (NULL for out-of-memory)
1536 */
1537 static int *
1538mb_str2wide(char_u *s)
1539{
1540 int *res;
1541 char_u *p;
1542 int i = 0;
1543
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001544 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001545 if (res != NULL)
1546 {
1547 for (p = s; *p != NUL; )
1548 res[i++] = mb_ptr2char_adv(&p);
1549 res[i] = NUL;
1550 }
1551 return res;
1552}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001553
1554/*
1555 * Read a tree from the .spl or .sug file.
1556 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1557 * This is skipped when the tree has zero length.
1558 * Returns zero when OK, SP_ value for an error.
1559 */
1560 static int
1561spell_read_tree(
1562 FILE *fd,
1563 char_u **bytsp,
1564 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001565 int prefixtree, // TRUE for the prefix tree
1566 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001567{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001568 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001569 int idx;
1570 char_u *bp;
1571 idx_T *ip;
1572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001573 // The tree size was computed when writing the file, so that we can
1574 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001575 len = get4c(fd);
1576 if (len < 0)
1577 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001578 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001579 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001580 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001581 if (len > 0)
1582 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001583 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001584 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001585 if (bp == NULL)
1586 return SP_OTHERERROR;
1587 *bytsp = bp;
1588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001589 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001590 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001591 if (ip == NULL)
1592 return SP_OTHERERROR;
1593 *idxsp = ip;
1594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001595 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001596 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1597 if (idx < 0)
1598 return idx;
1599 }
1600 return 0;
1601}
1602
1603/*
1604 * Read one row of siblings from the spell file and store it in the byte array
1605 * "byts" and index array "idxs". Recursively read the children.
1606 *
1607 * NOTE: The code here must match put_node()!
1608 *
1609 * Returns the index (>= 0) following the siblings.
1610 * Returns SP_TRUNCERROR if the file is shorter than expected.
1611 * Returns SP_FORMERROR if there is a format error.
1612 */
1613 static idx_T
1614read_tree_node(
1615 FILE *fd,
1616 char_u *byts,
1617 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001618 int maxidx, // size of arrays
1619 idx_T startidx, // current index in "byts" and "idxs"
1620 int prefixtree, // TRUE for reading PREFIXTREE
1621 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001622{
1623 int len;
1624 int i;
1625 int n;
1626 idx_T idx = startidx;
1627 int c;
1628 int c2;
1629#define SHARED_MASK 0x8000000
1630
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001631 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001632 if (len <= 0)
1633 return SP_TRUNCERROR;
1634
1635 if (startidx + len >= maxidx)
1636 return SP_FORMERROR;
1637 byts[idx++] = len;
1638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001639 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001640 for (i = 1; i <= len; ++i)
1641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001642 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001643 if (c < 0)
1644 return SP_TRUNCERROR;
1645 if (c <= BY_SPECIAL)
1646 {
1647 if (c == BY_NOFLAGS && !prefixtree)
1648 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001649 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001650 idxs[idx] = 0;
1651 c = 0;
1652 }
1653 else if (c != BY_INDEX)
1654 {
1655 if (prefixtree)
1656 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001657 // Read the optional pflags byte, the prefix ID and the
1658 // condition nr. In idxs[] store the prefix ID in the low
1659 // byte, the condition index shifted up 8 bits, the flags
1660 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001661 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001662 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001663 else
1664 c = 0;
1665
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001666 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001668 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001669 if (n >= maxprefcondnr)
1670 return SP_FORMERROR;
1671 c |= (n << 8);
1672 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001673 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001674 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001675 // Read flags and optional region and prefix ID. In
1676 // idxs[] the flags go in the low two bytes, region above
1677 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001678 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001679 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001680 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001681 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001682 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001683 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001684 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001685 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001686 }
1687
1688 idxs[idx] = c;
1689 c = 0;
1690 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001691 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001692 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001693 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001694 n = get3c(fd);
1695 if (n < 0 || n >= maxidx)
1696 return SP_FORMERROR;
1697 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001698 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001699 }
1700 }
1701 byts[idx++] = c;
1702 }
1703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001704 // Recursively read the children for non-shared siblings.
1705 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1706 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001707 for (i = 1; i <= len; ++i)
1708 if (byts[startidx + i] != 0)
1709 {
1710 if (idxs[startidx + i] & SHARED_MASK)
1711 idxs[startidx + i] &= ~SHARED_MASK;
1712 else
1713 {
1714 idxs[startidx + i] = idx;
1715 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1716 prefixtree, maxprefcondnr);
1717 if (idx < 0)
1718 break;
1719 }
1720 }
1721
1722 return idx;
1723}
1724
1725/*
1726 * Reload the spell file "fname" if it's loaded.
1727 */
1728 static void
1729spell_reload_one(
1730 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001731 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001732{
1733 slang_T *slang;
1734 int didit = FALSE;
1735
1736 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
1737 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001738 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001739 {
1740 slang_clear(slang);
1741 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001742 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001743 slang_clear(slang);
1744 redraw_all_later(SOME_VALID);
1745 didit = TRUE;
1746 }
1747 }
1748
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001749 // When "zg" was used and the file wasn't loaded yet, should redo
1750 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001751 if (added_word && !didit)
1752 did_set_spelllang(curwin);
1753}
1754
1755
1756/*
1757 * Functions for ":mkspell".
1758 */
1759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001760#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1761 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001762/*
1763 * Main structure to store the contents of a ".aff" file.
1764 */
1765typedef struct afffile_S
1766{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001767 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1768 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1769 unsigned af_rare; // RARE ID for rare word
1770 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1771 unsigned af_bad; // BAD ID for banned word
1772 unsigned af_needaffix; // NEEDAFFIX ID
1773 unsigned af_circumfix; // CIRCUMFIX ID
1774 unsigned af_needcomp; // NEEDCOMPOUND ID
1775 unsigned af_comproot; // COMPOUNDROOT ID
1776 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1777 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1778 unsigned af_nosuggest; // NOSUGGEST ID
1779 int af_pfxpostpone; // postpone prefixes without chop string and
1780 // without flags
1781 int af_ignoreextra; // IGNOREEXTRA present
1782 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1783 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1784 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001785} afffile_T;
1786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001787#define AFT_CHAR 0 // flags are one character
1788#define AFT_LONG 1 // flags are two characters
1789#define AFT_CAPLONG 2 // flags are one or two characters
1790#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001791
1792typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001793// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001794struct affentry_S
1795{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001796 affentry_T *ae_next; // next affix with same name/number
1797 char_u *ae_chop; // text to chop off basic word (can be NULL)
1798 char_u *ae_add; // text to add to basic word (can be NULL)
1799 char_u *ae_flags; // flags on the affix (can be NULL)
1800 char_u *ae_cond; // condition (NULL for ".")
1801 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1802 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1803 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001804};
1805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001806#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001807
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001808// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001809typedef struct affheader_S
1810{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001811 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1812 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1813 int ah_newID; // prefix ID after renumbering; 0 if not used
1814 int ah_combine; // suffix may combine with prefix
1815 int ah_follows; // another affix block should be following
1816 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001817} affheader_T;
1818
1819#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001821// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001822typedef struct compitem_S
1823{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001824 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1825 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1826 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001827} compitem_T;
1828
1829#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1830
1831/*
1832 * Structure that is used to store the items in the word tree. This avoids
1833 * the need to keep track of each allocated thing, everything is freed all at
1834 * once after ":mkspell" is done.
1835 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1836 * "sb_data" is correct for systems where pointers must be aligned on
1837 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1838 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001839#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001840typedef struct sblock_S sblock_T;
1841struct sblock_S
1842{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001843 int sb_used; // nr of bytes already in use
1844 sblock_T *sb_next; // next block in list
1845 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001846};
1847
1848/*
1849 * A node in the tree.
1850 */
1851typedef struct wordnode_S wordnode_T;
1852struct wordnode_S
1853{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001854 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001855 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001856 char_u hashkey[6]; // the hash key, only used while compressing
1857 int index; // index in written nodes (valid after first
1858 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001859 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001860 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001861 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001862 wordnode_T *next; // next node with same hash key
1863 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001864 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001865 wordnode_T *wn_child; // child (next byte in word)
1866 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1867 // always sorted)
1868 int wn_refs; // Nr. of references to this node. Only
1869 // relevant for first node in a list of
1870 // siblings, in following siblings it is
1871 // always one.
1872 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001873
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001874 // Info for when "wn_byte" is NUL.
1875 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1876 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1877 // "wn_region" the LSW of the wordnr.
1878 char_u wn_affixID; // supported/required prefix ID or 0
1879 short_u wn_flags; // WF_ flags
1880 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001881
1882#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001883 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001884#endif
1885};
1886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001887#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001888
1889#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1890
1891/*
1892 * Info used while reading the spell files.
1893 */
1894typedef struct spellinfo_S
1895{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001896 wordnode_T *si_foldroot; // tree with case-folded words
1897 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001898
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001899 wordnode_T *si_keeproot; // tree with keep-case words
1900 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001902 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001903
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001904 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001905
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001906 sblock_T *si_blocks; // memory blocks used
1907 long si_blocks_cnt; // memory blocks allocated
1908 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001910 long si_compress_cnt; // words to add before lowering
1911 // compression limit
1912 wordnode_T *si_first_free; // List of nodes that have been freed during
1913 // compression, linked by "wn_child" field.
1914 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001915#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001916 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001917#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001918 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001920 int si_ascii; // handling only ASCII words
1921 int si_add; // addition file
1922 int si_clear_chartab; // when TRUE clear char tables
1923 int si_region; // region mask
1924 vimconv_T si_conv; // for conversion to 'encoding'
1925 int si_memtot; // runtime memory used
1926 int si_verbose; // verbose messages
1927 int si_msg_count; // number of words added since last message
1928 char_u *si_info; // info text chars or NULL
1929 int si_region_count; // number of regions supported (1 when there
1930 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001931 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001932 // region names; used only if
1933 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001935 garray_T si_rep; // list of fromto_T entries from REP lines
1936 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1937 garray_T si_sal; // list of fromto_T entries from SAL lines
1938 char_u *si_sofofr; // SOFOFROM text
1939 char_u *si_sofoto; // SOFOTO text
1940 int si_nosugfile; // NOSUGFILE item found
1941 int si_nosplitsugs; // NOSPLITSUGS item found
1942 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1943 int si_followup; // soundsalike: ?
1944 int si_collapse; // soundsalike: ?
1945 hashtab_T si_commonwords; // hashtable for common words
1946 time_t si_sugtime; // timestamp for .sug file
1947 int si_rem_accents; // soundsalike: remove accents
1948 garray_T si_map; // MAP info concatenated
1949 char_u *si_midword; // MIDWORD chars or NULL
1950 int si_compmax; // max nr of words for compounding
1951 int si_compminlen; // minimal length for compounding
1952 int si_compsylmax; // max nr of syllables for compounding
1953 int si_compoptions; // COMP_ flags
1954 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1955 // a string
1956 char_u *si_compflags; // flags used for compounding
1957 char_u si_nobreak; // NOBREAK
1958 char_u *si_syllable; // syllable string
1959 garray_T si_prefcond; // table with conditions for postponed
1960 // prefixes, each stored as a string
1961 int si_newprefID; // current value for ah_newID
1962 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001963} spellinfo_T;
1964
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001965static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1966static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1967static int spell_info_item(char_u *s);
1968static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1969static unsigned get_affitem(int flagtype, char_u **pp);
1970static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1971static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001972static void aff_check_number(int spinval, int affval, char *name);
1973static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1974static int str_equal(char_u *s1, char_u *s2);
1975static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1976static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001977static int get_affix_flags(afffile_T *affile, char_u *afflist);
1978static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1979static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1980static 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 +02001981static void *getroom(spellinfo_T *spin, size_t len, int align);
1982static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001983static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
1984static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
1985static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001986static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
1987static void wordtree_compress(spellinfo_T *spin, wordnode_T *root);
1988static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot);
1989static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001990static void clear_node(wordnode_T *node);
1991static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001992static int sug_filltree(spellinfo_T *spin, slang_T *slang);
1993static int sug_maketable(spellinfo_T *spin);
1994static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
1995static int offset2bytes(int nr, char_u *buf);
1996static void sug_write(spellinfo_T *spin, char_u *fname);
1997static void spell_message(spellinfo_T *spin, char_u *str);
1998static void init_spellfile(void);
1999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002000// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
2001// but it must be negative to indicate the prefix tree to tree_add_word().
2002// Use a negative number with the lower 8 bits zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002003#define PFX_FLAGS -256
2004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002005// flags for "condit" argument of store_aff_word()
2006#define CONDIT_COMB 1 // affix must combine
2007#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2008#define CONDIT_SUF 4 // add a suffix for matching flags
2009#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002010
2011/*
2012 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
2013 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002014static long compress_start = 30000; // memory / SBLOCKSIZE
2015static long compress_inc = 100; // memory / SBLOCKSIZE
2016static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002017
2018/*
2019 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2020 * Sets "sps_flags".
2021 */
2022 int
2023spell_check_msm(void)
2024{
2025 char_u *p = p_msm;
2026 long start = 0;
2027 long incr = 0;
2028 long added = 0;
2029
2030 if (!VIM_ISDIGIT(*p))
2031 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002032 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002033 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2034 if (*p != ',')
2035 return FAIL;
2036 ++p;
2037 if (!VIM_ISDIGIT(*p))
2038 return FAIL;
2039 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2040 if (*p != ',')
2041 return FAIL;
2042 ++p;
2043 if (!VIM_ISDIGIT(*p))
2044 return FAIL;
2045 added = getdigits(&p) * 1024;
2046 if (*p != NUL)
2047 return FAIL;
2048
2049 if (start == 0 || incr == 0 || added == 0 || incr > start)
2050 return FAIL;
2051
2052 compress_start = start;
2053 compress_inc = incr;
2054 compress_added = added;
2055 return OK;
2056}
2057
2058#ifdef SPELL_PRINTTREE
2059/*
2060 * For debugging the tree code: print the current tree in a (more or less)
2061 * readable format, so that we can see what happens when adding a word and/or
2062 * compressing the tree.
2063 * Based on code from Olaf Seibert.
2064 */
2065#define PRINTLINESIZE 1000
2066#define PRINTWIDTH 6
2067
2068#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2069 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2070
2071static char line1[PRINTLINESIZE];
2072static char line2[PRINTLINESIZE];
2073static char line3[PRINTLINESIZE];
2074
2075 static void
2076spell_clear_flags(wordnode_T *node)
2077{
2078 wordnode_T *np;
2079
2080 for (np = node; np != NULL; np = np->wn_sibling)
2081 {
2082 np->wn_u1.index = FALSE;
2083 spell_clear_flags(np->wn_child);
2084 }
2085}
2086
2087 static void
2088spell_print_node(wordnode_T *node, int depth)
2089{
2090 if (node->wn_u1.index)
2091 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002092 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002093 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2094 PRINTSOME(line2, depth, " ", 0, 0);
2095 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002096 msg(line1);
2097 msg(line2);
2098 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002099 }
2100 else
2101 {
2102 node->wn_u1.index = TRUE;
2103
2104 if (node->wn_byte != NUL)
2105 {
2106 if (node->wn_child != NULL)
2107 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2108 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002109 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002110 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2111 }
2112 else
2113 PRINTSOME(line1, depth, " $ ", 0, 0);
2114
2115 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2116
2117 if (node->wn_sibling != NULL)
2118 PRINTSOME(line3, depth, " | ", 0, 0);
2119 else
2120 PRINTSOME(line3, depth, " ", 0, 0);
2121
2122 if (node->wn_byte == NUL)
2123 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002124 msg(line1);
2125 msg(line2);
2126 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002127 }
2128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002129 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002130 if (node->wn_byte != NUL && node->wn_child != NULL)
2131 spell_print_node(node->wn_child, depth + 1);
2132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002133 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002134 if (node->wn_sibling != NULL)
2135 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002136 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002137 STRCPY(line1, line3);
2138 STRCPY(line2, line3);
2139 spell_print_node(node->wn_sibling, depth);
2140 }
2141 }
2142}
2143
2144 static void
2145spell_print_tree(wordnode_T *root)
2146{
2147 if (root != NULL)
2148 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002149 // Clear the "wn_u1.index" fields, used to remember what has been
2150 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002151 spell_clear_flags(root);
2152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002153 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002154 spell_print_node(root, 0);
2155 }
2156}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002157#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002158
2159/*
2160 * Read the affix file "fname".
2161 * Returns an afffile_T, NULL for complete failure.
2162 */
2163 static afffile_T *
2164spell_read_aff(spellinfo_T *spin, char_u *fname)
2165{
2166 FILE *fd;
2167 afffile_T *aff;
2168 char_u rline[MAXLINELEN];
2169 char_u *line;
2170 char_u *pc = NULL;
2171#define MAXITEMCNT 30
2172 char_u *(items[MAXITEMCNT]);
2173 int itemcnt;
2174 char_u *p;
2175 int lnum = 0;
2176 affheader_T *cur_aff = NULL;
2177 int did_postpone_prefix = FALSE;
2178 int aff_todo = 0;
2179 hashtab_T *tp;
2180 char_u *low = NULL;
2181 char_u *fol = NULL;
2182 char_u *upp = NULL;
2183 int do_rep;
2184 int do_repsal;
2185 int do_sal;
2186 int do_mapline;
2187 int found_map = FALSE;
2188 hashitem_T *hi;
2189 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002190 int compminlen = 0; // COMPOUNDMIN value
2191 int compsylmax = 0; // COMPOUNDSYLMAX value
2192 int compoptions = 0; // COMP_ flags
2193 int compmax = 0; // COMPOUNDWORDMAX value
2194 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2195 // concatenated
2196 char_u *midword = NULL; // MIDWORD value
2197 char_u *syllable = NULL; // SYLLABLE value
2198 char_u *sofofrom = NULL; // SOFOFROM value
2199 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002200
2201 /*
2202 * Open the file.
2203 */
2204 fd = mch_fopen((char *)fname, "r");
2205 if (fd == NULL)
2206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002207 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002208 return NULL;
2209 }
2210
Bram Moolenaarc1669272018-06-19 14:23:53 +02002211 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002212 spell_message(spin, IObuff);
2213
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002214 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002215 do_rep = spin->si_rep.ga_len == 0;
2216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002217 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002218 do_repsal = spin->si_repsal.ga_len == 0;
2219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002220 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002221 do_sal = spin->si_sal.ga_len == 0;
2222
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002223 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002224 do_mapline = spin->si_map.ga_len == 0;
2225
2226 /*
2227 * Allocate and init the afffile_T structure.
2228 */
2229 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2230 if (aff == NULL)
2231 {
2232 fclose(fd);
2233 return NULL;
2234 }
2235 hash_init(&aff->af_pref);
2236 hash_init(&aff->af_suff);
2237 hash_init(&aff->af_comp);
2238
2239 /*
2240 * Read all the lines in the file one by one.
2241 */
2242 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2243 {
2244 line_breakcheck();
2245 ++lnum;
2246
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002248 if (*rline == '#')
2249 continue;
2250
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002251 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002252 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002253 if (spin->si_conv.vc_type != CONV_NONE)
2254 {
2255 pc = string_convert(&spin->si_conv, rline, NULL);
2256 if (pc == NULL)
2257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002258 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002259 fname, lnum, rline);
2260 continue;
2261 }
2262 line = pc;
2263 }
2264 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002265 {
2266 pc = NULL;
2267 line = rline;
2268 }
2269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 // Split the line up in white separated items. Put a NUL after each
2271 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002272 itemcnt = 0;
2273 for (p = line; ; )
2274 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002275 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002276 ++p;
2277 if (*p == NUL)
2278 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002279 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002280 break;
2281 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002282 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002283 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002284 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002285 ++p;
2286 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002287 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002288 ++p;
2289 if (*p == NUL)
2290 break;
2291 *p++ = NUL;
2292 }
2293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002294 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002295 if (itemcnt > 0)
2296 {
2297 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2298 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002299 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002300 aff->af_enc = enc_canonize(items[1]);
2301 if (aff->af_enc != NULL && !spin->si_ascii
2302 && convert_setup(&spin->si_conv, aff->af_enc,
2303 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002304 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002305 fname, aff->af_enc, p_enc);
2306 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002307 }
2308 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2309 && aff->af_flagtype == AFT_CHAR)
2310 {
2311 if (STRCMP(items[1], "long") == 0)
2312 aff->af_flagtype = AFT_LONG;
2313 else if (STRCMP(items[1], "num") == 0)
2314 aff->af_flagtype = AFT_NUM;
2315 else if (STRCMP(items[1], "caplong") == 0)
2316 aff->af_flagtype = AFT_CAPLONG;
2317 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002318 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002319 fname, lnum, items[1]);
2320 if (aff->af_rare != 0
2321 || aff->af_keepcase != 0
2322 || aff->af_bad != 0
2323 || aff->af_needaffix != 0
2324 || aff->af_circumfix != 0
2325 || aff->af_needcomp != 0
2326 || aff->af_comproot != 0
2327 || aff->af_nosuggest != 0
2328 || compflags != NULL
2329 || aff->af_suff.ht_used > 0
2330 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002332 fname, lnum, items[1]);
2333 }
2334 else if (spell_info_item(items[0]))
2335 {
2336 p = (char_u *)getroom(spin,
2337 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2338 + STRLEN(items[0])
2339 + STRLEN(items[1]) + 3, FALSE);
2340 if (p != NULL)
2341 {
2342 if (spin->si_info != NULL)
2343 {
2344 STRCPY(p, spin->si_info);
2345 STRCAT(p, "\n");
2346 }
2347 STRCAT(p, items[0]);
2348 STRCAT(p, " ");
2349 STRCAT(p, items[1]);
2350 spin->si_info = p;
2351 }
2352 }
2353 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2354 && midword == NULL)
2355 {
2356 midword = getroom_save(spin, items[1]);
2357 }
2358 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2359 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002360 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002361 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002362 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002363 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2364 || is_aff_rule(items, itemcnt, "RARE", 2))
2365 && aff->af_rare == 0)
2366 {
2367 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2368 fname, lnum);
2369 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002370 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002371 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2372 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2373 && aff->af_keepcase == 0)
2374 {
2375 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2376 fname, lnum);
2377 }
2378 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2379 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2380 && aff->af_bad == 0)
2381 {
2382 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2383 fname, lnum);
2384 }
2385 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2386 && aff->af_needaffix == 0)
2387 {
2388 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2389 fname, lnum);
2390 }
2391 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2392 && aff->af_circumfix == 0)
2393 {
2394 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2395 fname, lnum);
2396 }
2397 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2398 && aff->af_nosuggest == 0)
2399 {
2400 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2401 fname, lnum);
2402 }
2403 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2404 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2405 && aff->af_needcomp == 0)
2406 {
2407 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2408 fname, lnum);
2409 }
2410 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2411 && aff->af_comproot == 0)
2412 {
2413 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2414 fname, lnum);
2415 }
2416 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2417 && aff->af_compforbid == 0)
2418 {
2419 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2420 fname, lnum);
2421 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002422 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002423 fname, lnum);
2424 }
2425 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2426 && aff->af_comppermit == 0)
2427 {
2428 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2429 fname, lnum);
2430 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002431 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002432 fname, lnum);
2433 }
2434 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2435 && compflags == NULL)
2436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002437 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2438 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002439 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2440 if (p != NULL)
2441 {
2442 STRCPY(p, items[1]);
2443 STRCAT(p, "+");
2444 compflags = p;
2445 }
2446 }
2447 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2448 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002449 // We don't use the count, but do check that it's a number and
2450 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002451 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002452 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002453 fname, lnum, items[1]);
2454 }
2455 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2456 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002457 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002458 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2459 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002460 // Concatenate this string to previously defined ones,
2461 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002462 l = (int)STRLEN(items[1]) + 1;
2463 if (compflags != NULL)
2464 l += (int)STRLEN(compflags) + 1;
2465 p = getroom(spin, l, FALSE);
2466 if (p != NULL)
2467 {
2468 if (compflags != NULL)
2469 {
2470 STRCPY(p, compflags);
2471 STRCAT(p, "/");
2472 }
2473 STRCAT(p, items[1]);
2474 compflags = p;
2475 }
2476 }
2477 }
2478 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2479 && compmax == 0)
2480 {
2481 compmax = atoi((char *)items[1]);
2482 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002483 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002484 fname, lnum, items[1]);
2485 }
2486 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2487 && compminlen == 0)
2488 {
2489 compminlen = atoi((char *)items[1]);
2490 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002491 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002492 fname, lnum, items[1]);
2493 }
2494 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2495 && compsylmax == 0)
2496 {
2497 compsylmax = atoi((char *)items[1]);
2498 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002499 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002500 fname, lnum, items[1]);
2501 }
2502 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2503 {
2504 compoptions |= COMP_CHECKDUP;
2505 }
2506 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2507 {
2508 compoptions |= COMP_CHECKREP;
2509 }
2510 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2511 {
2512 compoptions |= COMP_CHECKCASE;
2513 }
2514 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2515 {
2516 compoptions |= COMP_CHECKTRIPLE;
2517 }
2518 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2519 {
2520 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002521 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002522 fname, lnum, items[1]);
2523 }
2524 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2525 {
2526 garray_T *gap = &spin->si_comppat;
2527 int i;
2528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002529 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002530 for (i = 0; i < gap->ga_len - 1; i += 2)
2531 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2532 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2533 items[2]) == 0)
2534 break;
2535 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2536 {
2537 ((char_u **)(gap->ga_data))[gap->ga_len++]
2538 = getroom_save(spin, items[1]);
2539 ((char_u **)(gap->ga_data))[gap->ga_len++]
2540 = getroom_save(spin, items[2]);
2541 }
2542 }
2543 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2544 && syllable == NULL)
2545 {
2546 syllable = getroom_save(spin, items[1]);
2547 }
2548 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2549 {
2550 spin->si_nobreak = TRUE;
2551 }
2552 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2553 {
2554 spin->si_nosplitsugs = TRUE;
2555 }
2556 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2557 {
2558 spin->si_nocompoundsugs = TRUE;
2559 }
2560 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2561 {
2562 spin->si_nosugfile = TRUE;
2563 }
2564 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2565 {
2566 aff->af_pfxpostpone = TRUE;
2567 }
2568 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2569 {
2570 aff->af_ignoreextra = TRUE;
2571 }
2572 else if ((STRCMP(items[0], "PFX") == 0
2573 || STRCMP(items[0], "SFX") == 0)
2574 && aff_todo == 0
2575 && itemcnt >= 4)
2576 {
2577 int lasti = 4;
2578 char_u key[AH_KEY_LEN];
2579
2580 if (*items[0] == 'P')
2581 tp = &aff->af_pref;
2582 else
2583 tp = &aff->af_suff;
2584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002585 // Myspell allows the same affix name to be used multiple
2586 // times. The affix files that do this have an undocumented
2587 // "S" flag on all but the last block, thus we check for that
2588 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002589 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2590 hi = hash_find(tp, key);
2591 if (!HASHITEM_EMPTY(hi))
2592 {
2593 cur_aff = HI2AH(hi);
2594 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002595 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002596 fname, lnum, items[1]);
2597 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002598 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002599 fname, lnum, items[1]);
2600 }
2601 else
2602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002604 cur_aff = (affheader_T *)getroom(spin,
2605 sizeof(affheader_T), TRUE);
2606 if (cur_aff == NULL)
2607 break;
2608 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2609 fname, lnum);
2610 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2611 break;
2612 if (cur_aff->ah_flag == aff->af_bad
2613 || cur_aff->ah_flag == aff->af_rare
2614 || cur_aff->ah_flag == aff->af_keepcase
2615 || cur_aff->ah_flag == aff->af_needaffix
2616 || cur_aff->ah_flag == aff->af_circumfix
2617 || cur_aff->ah_flag == aff->af_nosuggest
2618 || cur_aff->ah_flag == aff->af_needcomp
2619 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002620 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002621 fname, lnum, items[1]);
2622 STRCPY(cur_aff->ah_key, items[1]);
2623 hash_add(tp, cur_aff->ah_key);
2624
2625 cur_aff->ah_combine = (*items[2] == 'Y');
2626 }
2627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002628 // Check for the "S" flag, which apparently means that another
2629 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002630 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2631 {
2632 ++lasti;
2633 cur_aff->ah_follows = TRUE;
2634 }
2635 else
2636 cur_aff->ah_follows = FALSE;
2637
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002638 // Myspell allows extra text after the item, but that might
2639 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002640 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002641 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002642
2643 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002644 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002645 fname, lnum, items[2]);
2646
2647 if (*items[0] == 'P' && aff->af_pfxpostpone)
2648 {
2649 if (cur_aff->ah_newID == 0)
2650 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002651 // Use a new number in the .spl file later, to be able
2652 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002653 check_renumber(spin);
2654 cur_aff->ah_newID = ++spin->si_newprefID;
2655
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002656 // We only really use ah_newID if the prefix is
2657 // postponed. We know that only after handling all
2658 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002659 did_postpone_prefix = FALSE;
2660 }
2661 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002662 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002663 did_postpone_prefix = TRUE;
2664 }
2665
2666 aff_todo = atoi((char *)items[3]);
2667 }
2668 else if ((STRCMP(items[0], "PFX") == 0
2669 || STRCMP(items[0], "SFX") == 0)
2670 && aff_todo > 0
2671 && STRCMP(cur_aff->ah_key, items[1]) == 0
2672 && itemcnt >= 5)
2673 {
2674 affentry_T *aff_entry;
2675 int upper = FALSE;
2676 int lasti = 5;
2677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002678 // Myspell allows extra text after the item, but that might
2679 // mean mistakes go unnoticed. Require a comment-starter,
2680 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002681 if (itemcnt > lasti
2682 && !aff->af_ignoreextra
2683 && *items[lasti] != '#'
2684 && (STRCMP(items[lasti], "-") != 0
2685 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002686 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002687
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002688 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002689 --aff_todo;
2690 aff_entry = (affentry_T *)getroom(spin,
2691 sizeof(affentry_T), TRUE);
2692 if (aff_entry == NULL)
2693 break;
2694
2695 if (STRCMP(items[2], "0") != 0)
2696 aff_entry->ae_chop = getroom_save(spin, items[2]);
2697 if (STRCMP(items[3], "0") != 0)
2698 {
2699 aff_entry->ae_add = getroom_save(spin, items[3]);
2700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002701 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002702 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2703 if (aff_entry->ae_flags != NULL)
2704 {
2705 *aff_entry->ae_flags++ = NUL;
2706 aff_process_flags(aff, aff_entry);
2707 }
2708 }
2709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002710 // Don't use an affix entry with non-ASCII characters when
2711 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002712 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2713 || has_non_ascii(aff_entry->ae_add)))
2714 {
2715 aff_entry->ae_next = cur_aff->ah_first;
2716 cur_aff->ah_first = aff_entry;
2717
2718 if (STRCMP(items[4], ".") != 0)
2719 {
2720 char_u buf[MAXLINELEN];
2721
2722 aff_entry->ae_cond = getroom_save(spin, items[4]);
2723 if (*items[0] == 'P')
2724 sprintf((char *)buf, "^%s", items[4]);
2725 else
2726 sprintf((char *)buf, "%s$", items[4]);
2727 aff_entry->ae_prog = vim_regcomp(buf,
2728 RE_MAGIC + RE_STRING + RE_STRICT);
2729 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002730 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002731 fname, lnum, items[4]);
2732 }
2733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002734 // For postponed prefixes we need an entry in si_prefcond
2735 // for the condition. Use an existing one if possible.
2736 // Can't be done for an affix with flags, ignoring
2737 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002738 if (*items[0] == 'P' && aff->af_pfxpostpone
2739 && aff_entry->ae_flags == NULL)
2740 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002741 // When the chop string is one lower-case letter and
2742 // the add string ends in the upper-case letter we set
2743 // the "upper" flag, clear "ae_chop" and remove the
2744 // letters from "ae_add". The condition must either
2745 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002746 if (aff_entry->ae_chop != NULL
2747 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002748 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002749 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002750 {
2751 int c, c_up;
2752
2753 c = PTR2CHAR(aff_entry->ae_chop);
2754 c_up = SPELL_TOUPPER(c);
2755 if (c_up != c
2756 && (aff_entry->ae_cond == NULL
2757 || PTR2CHAR(aff_entry->ae_cond) == c))
2758 {
2759 p = aff_entry->ae_add
2760 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002761 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002762 if (PTR2CHAR(p) == c_up)
2763 {
2764 upper = TRUE;
2765 aff_entry->ae_chop = NULL;
2766 *p = NUL;
2767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002768 // The condition is matched with the
2769 // actual word, thus must check for the
2770 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002771 if (aff_entry->ae_cond != NULL)
2772 {
2773 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002774
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002775 if (has_mbyte)
2776 {
2777 onecap_copy(items[4], buf, TRUE);
2778 aff_entry->ae_cond = getroom_save(
2779 spin, buf);
2780 }
2781 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002782 *aff_entry->ae_cond = c_up;
2783 if (aff_entry->ae_cond != NULL)
2784 {
2785 sprintf((char *)buf, "^%s",
2786 aff_entry->ae_cond);
2787 vim_regfree(aff_entry->ae_prog);
2788 aff_entry->ae_prog = vim_regcomp(
2789 buf, RE_MAGIC + RE_STRING);
2790 }
2791 }
2792 }
2793 }
2794 }
2795
2796 if (aff_entry->ae_chop == NULL
2797 && aff_entry->ae_flags == NULL)
2798 {
2799 int idx;
2800 char_u **pp;
2801 int n;
2802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002803 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002804 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2805 --idx)
2806 {
2807 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2808 if (str_equal(p, aff_entry->ae_cond))
2809 break;
2810 }
2811 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2812 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002813 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002814 idx = spin->si_prefcond.ga_len++;
2815 pp = ((char_u **)spin->si_prefcond.ga_data)
2816 + idx;
2817 if (aff_entry->ae_cond == NULL)
2818 *pp = NULL;
2819 else
2820 *pp = getroom_save(spin,
2821 aff_entry->ae_cond);
2822 }
2823
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002824 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002825 if (aff_entry->ae_add == NULL)
2826 p = (char_u *)"";
2827 else
2828 p = aff_entry->ae_add;
2829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002830 // PFX_FLAGS is a negative number, so that
2831 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002832 n = PFX_FLAGS;
2833 if (!cur_aff->ah_combine)
2834 n |= WFP_NC;
2835 if (upper)
2836 n |= WFP_UP;
2837 if (aff_entry->ae_comppermit)
2838 n |= WFP_COMPPERMIT;
2839 if (aff_entry->ae_compforbid)
2840 n |= WFP_COMPFORBID;
2841 tree_add_word(spin, p, spin->si_prefroot, n,
2842 idx, cur_aff->ah_newID);
2843 did_postpone_prefix = TRUE;
2844 }
2845
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002846 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002847 if (aff_todo == 0 && !did_postpone_prefix)
2848 {
2849 --spin->si_newprefID;
2850 cur_aff->ah_newID = 0;
2851 }
2852 }
2853 }
2854 }
2855 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2856 {
2857 fol = vim_strsave(items[1]);
2858 }
2859 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2860 {
2861 low = vim_strsave(items[1]);
2862 }
2863 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2864 {
2865 upp = vim_strsave(items[1]);
2866 }
2867 else if (is_aff_rule(items, itemcnt, "REP", 2)
2868 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2869 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002870 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002871 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002872 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002873 fname, lnum);
2874 }
2875 else if ((STRCMP(items[0], "REP") == 0
2876 || STRCMP(items[0], "REPSAL") == 0)
2877 && itemcnt >= 3)
2878 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002879 // REP/REPSAL item
2880 // Myspell ignores extra arguments, we require it starts with
2881 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002882 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002883 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002884 if (items[0][3] == 'S' ? do_repsal : do_rep)
2885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002886 // Replace underscore with space (can't include a space
2887 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002888 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002889 if (*p == '_')
2890 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002891 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002892 if (*p == '_')
2893 *p = ' ';
2894 add_fromto(spin, items[0][3] == 'S'
2895 ? &spin->si_repsal
2896 : &spin->si_rep, items[1], items[2]);
2897 }
2898 }
2899 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2900 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002901 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002902 if (!found_map)
2903 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002904 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002905 found_map = TRUE;
2906 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002907 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002908 fname, lnum);
2909 }
2910 else if (do_mapline)
2911 {
2912 int c;
2913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002914 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002915 for (p = items[1]; *p != NUL; )
2916 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002917 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002918 if ((spin->si_map.ga_len > 0
2919 && vim_strchr(spin->si_map.ga_data, c)
2920 != NULL)
2921 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002922 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002923 fname, lnum);
2924 }
2925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002926 // We simply concatenate all the MAP strings, separated by
2927 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002928 ga_concat(&spin->si_map, items[1]);
2929 ga_append(&spin->si_map, '/');
2930 }
2931 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002932 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002933 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2934 {
2935 if (do_sal)
2936 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002937 // SAL item (sounds-a-like)
2938 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002939 if (STRCMP(items[1], "followup") == 0)
2940 spin->si_followup = sal_to_bool(items[2]);
2941 else if (STRCMP(items[1], "collapse_result") == 0)
2942 spin->si_collapse = sal_to_bool(items[2]);
2943 else if (STRCMP(items[1], "remove_accents") == 0)
2944 spin->si_rem_accents = sal_to_bool(items[2]);
2945 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002946 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002947 add_fromto(spin, &spin->si_sal, items[1],
2948 STRCMP(items[2], "_") == 0 ? (char_u *)""
2949 : items[2]);
2950 }
2951 }
2952 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2953 && sofofrom == NULL)
2954 {
2955 sofofrom = getroom_save(spin, items[1]);
2956 }
2957 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2958 && sofoto == NULL)
2959 {
2960 sofoto = getroom_save(spin, items[1]);
2961 }
2962 else if (STRCMP(items[0], "COMMON") == 0)
2963 {
2964 int i;
2965
2966 for (i = 1; i < itemcnt; ++i)
2967 {
2968 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2969 items[i])))
2970 {
2971 p = vim_strsave(items[i]);
2972 if (p == NULL)
2973 break;
2974 hash_add(&spin->si_commonwords, p);
2975 }
2976 }
2977 }
2978 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002979 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002980 fname, lnum, items[0]);
2981 }
2982 }
2983
2984 if (fol != NULL || low != NULL || upp != NULL)
2985 {
2986 if (spin->si_clear_chartab)
2987 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002988 // Clear the char type tables, don't want to use any of the
2989 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002990 init_spell_chartab();
2991 spin->si_clear_chartab = FALSE;
2992 }
2993
2994 /*
2995 * Don't write a word table for an ASCII file, so that we don't check
2996 * for conflicts with a word table that matches 'encoding'.
2997 * Don't write one for utf-8 either, we use utf_*() and
2998 * mb_get_class(), the list of chars in the file will be incomplete.
2999 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003000 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003001 {
3002 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003003 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003004 else
3005 (void)set_spell_chartab(fol, low, upp);
3006 }
3007
3008 vim_free(fol);
3009 vim_free(low);
3010 vim_free(upp);
3011 }
3012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003013 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003014 if (compmax != 0)
3015 {
3016 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3017 spin->si_compmax = compmax;
3018 }
3019
3020 if (compminlen != 0)
3021 {
3022 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3023 spin->si_compminlen = compminlen;
3024 }
3025
3026 if (compsylmax != 0)
3027 {
3028 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003029 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003030 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3031 spin->si_compsylmax = compsylmax;
3032 }
3033
3034 if (compoptions != 0)
3035 {
3036 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3037 spin->si_compoptions |= compoptions;
3038 }
3039
3040 if (compflags != NULL)
3041 process_compflags(spin, aff, compflags);
3042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003043 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003044 if (spin->si_newcompID < spin->si_newprefID)
3045 {
3046 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003047 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003048 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003049 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003050 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003051 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003052 }
3053
3054 if (syllable != NULL)
3055 {
3056 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3057 spin->si_syllable = syllable;
3058 }
3059
3060 if (sofofrom != NULL || sofoto != NULL)
3061 {
3062 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003063 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003064 sofofrom == NULL ? "FROM" : "TO", fname);
3065 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003066 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003067 else
3068 {
3069 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3070 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3071 spin->si_sofofr = sofofrom;
3072 spin->si_sofoto = sofoto;
3073 }
3074 }
3075
3076 if (midword != NULL)
3077 {
3078 aff_check_string(spin->si_midword, midword, "MIDWORD");
3079 spin->si_midword = midword;
3080 }
3081
3082 vim_free(pc);
3083 fclose(fd);
3084 return aff;
3085}
3086
3087/*
3088 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3089 * a comment is following after item "mincount".
3090 */
3091 static int
3092is_aff_rule(
3093 char_u **items,
3094 int itemcnt,
3095 char *rulename,
3096 int mincount)
3097{
3098 return (STRCMP(items[0], rulename) == 0
3099 && (itemcnt == mincount
3100 || (itemcnt > mincount && items[mincount][0] == '#')));
3101}
3102
3103/*
3104 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3105 * ae_flags to ae_comppermit and ae_compforbid.
3106 */
3107 static void
3108aff_process_flags(afffile_T *affile, affentry_T *entry)
3109{
3110 char_u *p;
3111 char_u *prevp;
3112 unsigned flag;
3113
3114 if (entry->ae_flags != NULL
3115 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3116 {
3117 for (p = entry->ae_flags; *p != NUL; )
3118 {
3119 prevp = p;
3120 flag = get_affitem(affile->af_flagtype, &p);
3121 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3122 {
3123 STRMOVE(prevp, p);
3124 p = prevp;
3125 if (flag == affile->af_comppermit)
3126 entry->ae_comppermit = TRUE;
3127 else
3128 entry->ae_compforbid = TRUE;
3129 }
3130 if (affile->af_flagtype == AFT_NUM && *p == ',')
3131 ++p;
3132 }
3133 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003134 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003135 }
3136}
3137
3138/*
3139 * Return TRUE if "s" is the name of an info item in the affix file.
3140 */
3141 static int
3142spell_info_item(char_u *s)
3143{
3144 return STRCMP(s, "NAME") == 0
3145 || STRCMP(s, "HOME") == 0
3146 || STRCMP(s, "VERSION") == 0
3147 || STRCMP(s, "AUTHOR") == 0
3148 || STRCMP(s, "EMAIL") == 0
3149 || STRCMP(s, "COPYRIGHT") == 0;
3150}
3151
3152/*
3153 * Turn an affix flag name into a number, according to the FLAG type.
3154 * returns zero for failure.
3155 */
3156 static unsigned
3157affitem2flag(
3158 int flagtype,
3159 char_u *item,
3160 char_u *fname,
3161 int lnum)
3162{
3163 unsigned res;
3164 char_u *p = item;
3165
3166 res = get_affitem(flagtype, &p);
3167 if (res == 0)
3168 {
3169 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003170 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003171 fname, lnum, item);
3172 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003173 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003174 fname, lnum, item);
3175 }
3176 if (*p != NUL)
3177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003178 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003179 return 0;
3180 }
3181
3182 return res;
3183}
3184
3185/*
3186 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003187 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003188 * Returns zero for an error, still advances the pointer then.
3189 */
3190 static unsigned
3191get_affitem(int flagtype, char_u **pp)
3192{
3193 int res;
3194
3195 if (flagtype == AFT_NUM)
3196 {
3197 if (!VIM_ISDIGIT(**pp))
3198 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003199 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003200 return 0;
3201 }
3202 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003203 if (res == 0)
3204 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003205 }
3206 else
3207 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003208 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003209 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3210 && res >= 'A' && res <= 'Z'))
3211 {
3212 if (**pp == NUL)
3213 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003214 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003215 }
3216 }
3217 return res;
3218}
3219
3220/*
3221 * Process the "compflags" string used in an affix file and append it to
3222 * spin->si_compflags.
3223 * The processing involves changing the affix names to ID numbers, so that
3224 * they fit in one byte.
3225 */
3226 static void
3227process_compflags(
3228 spellinfo_T *spin,
3229 afffile_T *aff,
3230 char_u *compflags)
3231{
3232 char_u *p;
3233 char_u *prevp;
3234 unsigned flag;
3235 compitem_T *ci;
3236 int id;
3237 int len;
3238 char_u *tp;
3239 char_u key[AH_KEY_LEN];
3240 hashitem_T *hi;
3241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003242 // Make room for the old and the new compflags, concatenated with a / in
3243 // between. Processing it makes it shorter, but we don't know by how
3244 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003245 len = (int)STRLEN(compflags) + 1;
3246 if (spin->si_compflags != NULL)
3247 len += (int)STRLEN(spin->si_compflags) + 1;
3248 p = getroom(spin, len, FALSE);
3249 if (p == NULL)
3250 return;
3251 if (spin->si_compflags != NULL)
3252 {
3253 STRCPY(p, spin->si_compflags);
3254 STRCAT(p, "/");
3255 }
3256 spin->si_compflags = p;
3257 tp = p + STRLEN(p);
3258
3259 for (p = compflags; *p != NUL; )
3260 {
3261 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003262 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003263 *tp++ = *p++;
3264 else
3265 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003266 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003267 prevp = p;
3268 flag = get_affitem(aff->af_flagtype, &p);
3269 if (flag != 0)
3270 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003271 // Find the flag in the hashtable. If it was used before, use
3272 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003273 vim_strncpy(key, prevp, p - prevp);
3274 hi = hash_find(&aff->af_comp, key);
3275 if (!HASHITEM_EMPTY(hi))
3276 id = HI2CI(hi)->ci_newID;
3277 else
3278 {
3279 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3280 if (ci == NULL)
3281 break;
3282 STRCPY(ci->ci_key, key);
3283 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003284 // Avoid using a flag ID that has a special meaning in a
3285 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003286 do
3287 {
3288 check_renumber(spin);
3289 id = spin->si_newcompID--;
3290 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3291 ci->ci_newID = id;
3292 hash_add(&aff->af_comp, ci->ci_key);
3293 }
3294 *tp++ = id;
3295 }
3296 if (aff->af_flagtype == AFT_NUM && *p == ',')
3297 ++p;
3298 }
3299 }
3300
3301 *tp = NUL;
3302}
3303
3304/*
3305 * Check that the new IDs for postponed affixes and compounding don't overrun
3306 * each other. We have almost 255 available, but start at 0-127 to avoid
3307 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3308 * When that is used up an error message is given.
3309 */
3310 static void
3311check_renumber(spellinfo_T *spin)
3312{
3313 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3314 {
3315 spin->si_newprefID = 127;
3316 spin->si_newcompID = 255;
3317 }
3318}
3319
3320/*
3321 * Return TRUE if flag "flag" appears in affix list "afflist".
3322 */
3323 static int
3324flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3325{
3326 char_u *p;
3327 unsigned n;
3328
3329 switch (flagtype)
3330 {
3331 case AFT_CHAR:
3332 return vim_strchr(afflist, flag) != NULL;
3333
3334 case AFT_CAPLONG:
3335 case AFT_LONG:
3336 for (p = afflist; *p != NUL; )
3337 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003338 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003339 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3340 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003341 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003342 if (n == flag)
3343 return TRUE;
3344 }
3345 break;
3346
3347 case AFT_NUM:
3348 for (p = afflist; *p != NUL; )
3349 {
3350 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003351 if (n == 0)
3352 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003353 if (n == flag)
3354 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003355 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003356 ++p;
3357 }
3358 break;
3359 }
3360 return FALSE;
3361}
3362
3363/*
3364 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3365 */
3366 static void
3367aff_check_number(int spinval, int affval, char *name)
3368{
3369 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003370 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003371}
3372
3373/*
3374 * Give a warning when "spinval" and "affval" strings are set and not the same.
3375 */
3376 static void
3377aff_check_string(char_u *spinval, char_u *affval, char *name)
3378{
3379 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003380 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003381}
3382
3383/*
3384 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3385 * NULL as equal.
3386 */
3387 static int
3388str_equal(char_u *s1, char_u *s2)
3389{
3390 if (s1 == NULL || s2 == NULL)
3391 return s1 == s2;
3392 return STRCMP(s1, s2) == 0;
3393}
3394
3395/*
3396 * Add a from-to item to "gap". Used for REP and SAL items.
3397 * They are stored case-folded.
3398 */
3399 static void
3400add_fromto(
3401 spellinfo_T *spin,
3402 garray_T *gap,
3403 char_u *from,
3404 char_u *to)
3405{
3406 fromto_T *ftp;
3407 char_u word[MAXWLEN];
3408
3409 if (ga_grow(gap, 1) == OK)
3410 {
3411 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3412 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
3413 ftp->ft_from = getroom_save(spin, word);
3414 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
3415 ftp->ft_to = getroom_save(spin, word);
3416 ++gap->ga_len;
3417 }
3418}
3419
3420/*
3421 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3422 */
3423 static int
3424sal_to_bool(char_u *s)
3425{
3426 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3427}
3428
3429/*
3430 * Free the structure filled by spell_read_aff().
3431 */
3432 static void
3433spell_free_aff(afffile_T *aff)
3434{
3435 hashtab_T *ht;
3436 hashitem_T *hi;
3437 int todo;
3438 affheader_T *ah;
3439 affentry_T *ae;
3440
3441 vim_free(aff->af_enc);
3442
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003443 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003444 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3445 {
3446 todo = (int)ht->ht_used;
3447 for (hi = ht->ht_array; todo > 0; ++hi)
3448 {
3449 if (!HASHITEM_EMPTY(hi))
3450 {
3451 --todo;
3452 ah = HI2AH(hi);
3453 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3454 vim_regfree(ae->ae_prog);
3455 }
3456 }
3457 if (ht == &aff->af_suff)
3458 break;
3459 }
3460
3461 hash_clear(&aff->af_pref);
3462 hash_clear(&aff->af_suff);
3463 hash_clear(&aff->af_comp);
3464}
3465
3466/*
3467 * Read dictionary file "fname".
3468 * Returns OK or FAIL;
3469 */
3470 static int
3471spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3472{
3473 hashtab_T ht;
3474 char_u line[MAXLINELEN];
3475 char_u *p;
3476 char_u *afflist;
3477 char_u store_afflist[MAXWLEN];
3478 int pfxlen;
3479 int need_affix;
3480 char_u *dw;
3481 char_u *pc;
3482 char_u *w;
3483 int l;
3484 hash_T hash;
3485 hashitem_T *hi;
3486 FILE *fd;
3487 int lnum = 1;
3488 int non_ascii = 0;
3489 int retval = OK;
3490 char_u message[MAXLINELEN + MAXWLEN];
3491 int flags;
3492 int duplicate = 0;
3493
3494 /*
3495 * Open the file.
3496 */
3497 fd = mch_fopen((char *)fname, "r");
3498 if (fd == NULL)
3499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003500 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003501 return FAIL;
3502 }
3503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003504 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003505 hash_init(&ht);
3506
3507 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003508 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003509 spell_message(spin, IObuff);
3510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003511 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003512 spin->si_msg_count = 999999;
3513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003514 // Read and ignore the first line: word count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003515 (void)vim_fgets(line, MAXLINELEN, fd);
3516 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003517 semsg(_("E760: No word count in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003518
3519 /*
3520 * Read all the lines in the file one by one.
3521 * The words are converted to 'encoding' here, before being added to
3522 * the hashtable.
3523 */
3524 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3525 {
3526 line_breakcheck();
3527 ++lnum;
3528 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003529 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003531 // Remove CR, LF and white space from the end. White space halfway
3532 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003533 l = (int)STRLEN(line);
3534 while (l > 0 && line[l - 1] <= ' ')
3535 --l;
3536 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003537 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003538 line[l] = NUL;
3539
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003540 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003541 if (spin->si_conv.vc_type != CONV_NONE)
3542 {
3543 pc = string_convert(&spin->si_conv, line, NULL);
3544 if (pc == NULL)
3545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003546 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003547 fname, lnum, line);
3548 continue;
3549 }
3550 w = pc;
3551 }
3552 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003553 {
3554 pc = NULL;
3555 w = line;
3556 }
3557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003558 // Truncate the word at the "/", set "afflist" to what follows.
3559 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003560 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003561 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003562 {
3563 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3564 STRMOVE(p, p + 1);
3565 else if (*p == '/')
3566 {
3567 *p = NUL;
3568 afflist = p + 1;
3569 break;
3570 }
3571 }
3572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003573 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003574 if (spin->si_ascii && has_non_ascii(w))
3575 {
3576 ++non_ascii;
3577 vim_free(pc);
3578 continue;
3579 }
3580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003581 // This takes time, print a message every 10000 words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003582 if (spin->si_verbose && spin->si_msg_count > 10000)
3583 {
3584 spin->si_msg_count = 0;
3585 vim_snprintf((char *)message, sizeof(message),
Bram Moolenaarea391762018-04-08 13:07:22 +02003586 _("line %6d, word %6ld - %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003587 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3588 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003589 msg_outtrans_long_attr(message, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003590 msg_clr_eos();
3591 msg_didout = FALSE;
3592 msg_col = 0;
3593 out_flush();
3594 }
3595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003596 // Store the word in the hashtable to be able to find duplicates.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003597 dw = (char_u *)getroom_save(spin, w);
3598 if (dw == NULL)
3599 {
3600 retval = FAIL;
3601 vim_free(pc);
3602 break;
3603 }
3604
3605 hash = hash_hash(dw);
3606 hi = hash_lookup(&ht, dw, hash);
3607 if (!HASHITEM_EMPTY(hi))
3608 {
3609 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003610 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003611 fname, lnum, dw);
3612 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003613 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003614 fname, lnum, dw);
3615 ++duplicate;
3616 }
3617 else
3618 hash_add_item(&ht, hi, dw, hash);
3619
3620 flags = 0;
3621 store_afflist[0] = NUL;
3622 pfxlen = 0;
3623 need_affix = FALSE;
3624 if (afflist != NULL)
3625 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003626 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003627 flags |= get_affix_flags(affile, afflist);
3628
3629 if (affile->af_needaffix != 0 && flag_in_afflist(
3630 affile->af_flagtype, afflist, affile->af_needaffix))
3631 need_affix = TRUE;
3632
3633 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003634 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003635 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3636
3637 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003638 // Need to store the list of compound flags with the word.
3639 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003640 get_compflags(affile, afflist, store_afflist + pfxlen);
3641 }
3642
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003643 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003644 if (store_word(spin, dw, flags, spin->si_region,
3645 store_afflist, need_affix) == FAIL)
3646 retval = FAIL;
3647
3648 if (afflist != NULL)
3649 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003650 // Find all matching suffixes and add the resulting words.
3651 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003652 if (store_aff_word(spin, dw, afflist, affile,
3653 &affile->af_suff, &affile->af_pref,
3654 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3655 retval = FAIL;
3656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003657 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003658 if (store_aff_word(spin, dw, afflist, affile,
3659 &affile->af_pref, NULL,
3660 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3661 retval = FAIL;
3662 }
3663
3664 vim_free(pc);
3665 }
3666
3667 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003668 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003669 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003670 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003671 non_ascii, fname);
3672 hash_clear(&ht);
3673
3674 fclose(fd);
3675 return retval;
3676}
3677
3678/*
3679 * Check for affix flags in "afflist" that are turned into word flags.
3680 * Return WF_ flags.
3681 */
3682 static int
3683get_affix_flags(afffile_T *affile, char_u *afflist)
3684{
3685 int flags = 0;
3686
3687 if (affile->af_keepcase != 0 && flag_in_afflist(
3688 affile->af_flagtype, afflist, affile->af_keepcase))
3689 flags |= WF_KEEPCAP | WF_FIXCAP;
3690 if (affile->af_rare != 0 && flag_in_afflist(
3691 affile->af_flagtype, afflist, affile->af_rare))
3692 flags |= WF_RARE;
3693 if (affile->af_bad != 0 && flag_in_afflist(
3694 affile->af_flagtype, afflist, affile->af_bad))
3695 flags |= WF_BANNED;
3696 if (affile->af_needcomp != 0 && flag_in_afflist(
3697 affile->af_flagtype, afflist, affile->af_needcomp))
3698 flags |= WF_NEEDCOMP;
3699 if (affile->af_comproot != 0 && flag_in_afflist(
3700 affile->af_flagtype, afflist, affile->af_comproot))
3701 flags |= WF_COMPROOT;
3702 if (affile->af_nosuggest != 0 && flag_in_afflist(
3703 affile->af_flagtype, afflist, affile->af_nosuggest))
3704 flags |= WF_NOSUGGEST;
3705 return flags;
3706}
3707
3708/*
3709 * Get the list of prefix IDs from the affix list "afflist".
3710 * Used for PFXPOSTPONE.
3711 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3712 * and return the number of affixes.
3713 */
3714 static int
3715get_pfxlist(
3716 afffile_T *affile,
3717 char_u *afflist,
3718 char_u *store_afflist)
3719{
3720 char_u *p;
3721 char_u *prevp;
3722 int cnt = 0;
3723 int id;
3724 char_u key[AH_KEY_LEN];
3725 hashitem_T *hi;
3726
3727 for (p = afflist; *p != NUL; )
3728 {
3729 prevp = p;
3730 if (get_affitem(affile->af_flagtype, &p) != 0)
3731 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003732 // A flag is a postponed prefix flag if it appears in "af_pref"
3733 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003734 vim_strncpy(key, prevp, p - prevp);
3735 hi = hash_find(&affile->af_pref, key);
3736 if (!HASHITEM_EMPTY(hi))
3737 {
3738 id = HI2AH(hi)->ah_newID;
3739 if (id != 0)
3740 store_afflist[cnt++] = id;
3741 }
3742 }
3743 if (affile->af_flagtype == AFT_NUM && *p == ',')
3744 ++p;
3745 }
3746
3747 store_afflist[cnt] = NUL;
3748 return cnt;
3749}
3750
3751/*
3752 * Get the list of compound IDs from the affix list "afflist" that are used
3753 * for compound words.
3754 * Puts the flags in "store_afflist[]".
3755 */
3756 static void
3757get_compflags(
3758 afffile_T *affile,
3759 char_u *afflist,
3760 char_u *store_afflist)
3761{
3762 char_u *p;
3763 char_u *prevp;
3764 int cnt = 0;
3765 char_u key[AH_KEY_LEN];
3766 hashitem_T *hi;
3767
3768 for (p = afflist; *p != NUL; )
3769 {
3770 prevp = p;
3771 if (get_affitem(affile->af_flagtype, &p) != 0)
3772 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003773 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003774 vim_strncpy(key, prevp, p - prevp);
3775 hi = hash_find(&affile->af_comp, key);
3776 if (!HASHITEM_EMPTY(hi))
3777 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3778 }
3779 if (affile->af_flagtype == AFT_NUM && *p == ',')
3780 ++p;
3781 }
3782
3783 store_afflist[cnt] = NUL;
3784}
3785
3786/*
3787 * Apply affixes to a word and store the resulting words.
3788 * "ht" is the hashtable with affentry_T that need to be applied, either
3789 * prefixes or suffixes.
3790 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3791 * the resulting words for combining affixes.
3792 *
3793 * Returns FAIL when out of memory.
3794 */
3795 static int
3796store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003797 spellinfo_T *spin, // spell info
3798 char_u *word, // basic word start
3799 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003800 afffile_T *affile,
3801 hashtab_T *ht,
3802 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003803 int condit, // CONDIT_SUF et al.
3804 int flags, // flags for the word
3805 char_u *pfxlist, // list of prefix IDs
3806 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3807 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003808{
3809 int todo;
3810 hashitem_T *hi;
3811 affheader_T *ah;
3812 affentry_T *ae;
3813 char_u newword[MAXWLEN];
3814 int retval = OK;
3815 int i, j;
3816 char_u *p;
3817 int use_flags;
3818 char_u *use_pfxlist;
3819 int use_pfxlen;
3820 int need_affix;
3821 char_u store_afflist[MAXWLEN];
3822 char_u pfx_pfxlist[MAXWLEN];
3823 size_t wordlen = STRLEN(word);
3824 int use_condit;
3825
3826 todo = (int)ht->ht_used;
3827 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3828 {
3829 if (!HASHITEM_EMPTY(hi))
3830 {
3831 --todo;
3832 ah = HI2AH(hi);
3833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003834 // Check that the affix combines, if required, and that the word
3835 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003836 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3837 && flag_in_afflist(affile->af_flagtype, afflist,
3838 ah->ah_flag))
3839 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003840 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003841 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3842 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003843 // Check the condition. It's not logical to match case
3844 // here, but it is required for compatibility with
3845 // Myspell.
3846 // Another requirement from Myspell is that the chop
3847 // string is shorter than the word itself.
3848 // For prefixes, when "PFXPOSTPONE" was used, only do
3849 // prefixes with a chop string and/or flags.
3850 // When a previously added affix had CIRCUMFIX this one
3851 // must have it too, if it had not then this one must not
3852 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003853 if ((xht != NULL || !affile->af_pfxpostpone
3854 || ae->ae_chop != NULL
3855 || ae->ae_flags != NULL)
3856 && (ae->ae_chop == NULL
3857 || STRLEN(ae->ae_chop) < wordlen)
3858 && (ae->ae_prog == NULL
3859 || vim_regexec_prog(&ae->ae_prog, FALSE,
3860 word, (colnr_T)0))
3861 && (((condit & CONDIT_CFIX) == 0)
3862 == ((condit & CONDIT_AFF) == 0
3863 || ae->ae_flags == NULL
3864 || !flag_in_afflist(affile->af_flagtype,
3865 ae->ae_flags, affile->af_circumfix))))
3866 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003867 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003868 if (xht == NULL)
3869 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003870 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003871 if (ae->ae_add == NULL)
3872 *newword = NUL;
3873 else
3874 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3875 p = word;
3876 if (ae->ae_chop != NULL)
3877 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003878 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003879 if (has_mbyte)
3880 {
3881 i = mb_charlen(ae->ae_chop);
3882 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003883 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003884 }
3885 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003886 p += STRLEN(ae->ae_chop);
3887 }
3888 STRCAT(newword, p);
3889 }
3890 else
3891 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003892 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003893 vim_strncpy(newword, word, MAXWLEN - 1);
3894 if (ae->ae_chop != NULL)
3895 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003896 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003897 p = newword + STRLEN(newword);
3898 i = (int)MB_CHARLEN(ae->ae_chop);
3899 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003900 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003901 *p = NUL;
3902 }
3903 if (ae->ae_add != NULL)
3904 STRCAT(newword, ae->ae_add);
3905 }
3906
3907 use_flags = flags;
3908 use_pfxlist = pfxlist;
3909 use_pfxlen = pfxlen;
3910 need_affix = FALSE;
3911 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3912 if (ae->ae_flags != NULL)
3913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003914 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003915 use_flags |= get_affix_flags(affile, ae->ae_flags);
3916
3917 if (affile->af_needaffix != 0 && flag_in_afflist(
3918 affile->af_flagtype, ae->ae_flags,
3919 affile->af_needaffix))
3920 need_affix = TRUE;
3921
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003922 // When there is a CIRCUMFIX flag the other affix
3923 // must also have it and we don't add the word
3924 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003925 if (affile->af_circumfix != 0 && flag_in_afflist(
3926 affile->af_flagtype, ae->ae_flags,
3927 affile->af_circumfix))
3928 {
3929 use_condit |= CONDIT_CFIX;
3930 if ((condit & CONDIT_CFIX) == 0)
3931 need_affix = TRUE;
3932 }
3933
3934 if (affile->af_pfxpostpone
3935 || spin->si_compflags != NULL)
3936 {
3937 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003938 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003939 use_pfxlen = get_pfxlist(affile,
3940 ae->ae_flags, store_afflist);
3941 else
3942 use_pfxlen = 0;
3943 use_pfxlist = store_afflist;
3944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003945 // Combine the prefix IDs. Avoid adding the
3946 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003947 for (i = 0; i < pfxlen; ++i)
3948 {
3949 for (j = 0; j < use_pfxlen; ++j)
3950 if (pfxlist[i] == use_pfxlist[j])
3951 break;
3952 if (j == use_pfxlen)
3953 use_pfxlist[use_pfxlen++] = pfxlist[i];
3954 }
3955
3956 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003957 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003958 get_compflags(affile, ae->ae_flags,
3959 use_pfxlist + use_pfxlen);
3960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003961 // Combine the list of compound flags.
3962 // Concatenate them to the prefix IDs list.
3963 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003964 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3965 {
3966 for (j = use_pfxlen;
3967 use_pfxlist[j] != NUL; ++j)
3968 if (pfxlist[i] == use_pfxlist[j])
3969 break;
3970 if (use_pfxlist[j] == NUL)
3971 {
3972 use_pfxlist[j++] = pfxlist[i];
3973 use_pfxlist[j] = NUL;
3974 }
3975 }
3976 }
3977 }
3978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003979 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
3980 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003981 if (use_pfxlist != NULL && ae->ae_compforbid)
3982 {
3983 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
3984 use_pfxlist = pfx_pfxlist;
3985 }
3986
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003987 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003988 if (spin->si_prefroot != NULL
3989 && spin->si_prefroot->wn_sibling != NULL)
3990 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003991 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003992 use_flags |= WF_HAS_AFF;
3993
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003994 // ... don't use a prefix list if combining
3995 // affixes is not allowed. But do use the
3996 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003997 if (!ah->ah_combine && use_pfxlist != NULL)
3998 use_pfxlist += use_pfxlen;
3999 }
4000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 // When compounding is supported and there is no
4002 // "COMPOUNDPERMITFLAG" then forbid compounding on the
4003 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004004 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4005 {
4006 if (xht != NULL)
4007 use_flags |= WF_NOCOMPAFT;
4008 else
4009 use_flags |= WF_NOCOMPBEF;
4010 }
4011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004012 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004013 if (store_word(spin, newword, use_flags,
4014 spin->si_region, use_pfxlist,
4015 need_affix) == FAIL)
4016 retval = FAIL;
4017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 // When added a prefix or a first suffix and the affix
4019 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004020 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4021 if (store_aff_word(spin, newword, ae->ae_flags,
4022 affile, &affile->af_suff, xht,
4023 use_condit & (xht == NULL
4024 ? ~0 : ~CONDIT_SUF),
4025 use_flags, use_pfxlist, pfxlen) == FAIL)
4026 retval = FAIL;
4027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004028 // When added a suffix and combining is allowed also
4029 // try adding a prefix additionally. Both for the
4030 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004031 if (xht != NULL && ah->ah_combine)
4032 {
4033 if (store_aff_word(spin, newword,
4034 afflist, affile,
4035 xht, NULL, use_condit,
4036 use_flags, use_pfxlist,
4037 pfxlen) == FAIL
4038 || (ae->ae_flags != NULL
4039 && store_aff_word(spin, newword,
4040 ae->ae_flags, affile,
4041 xht, NULL, use_condit,
4042 use_flags, use_pfxlist,
4043 pfxlen) == FAIL))
4044 retval = FAIL;
4045 }
4046 }
4047 }
4048 }
4049 }
4050 }
4051
4052 return retval;
4053}
4054
4055/*
4056 * Read a file with a list of words.
4057 */
4058 static int
4059spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4060{
4061 FILE *fd;
4062 long lnum = 0;
4063 char_u rline[MAXLINELEN];
4064 char_u *line;
4065 char_u *pc = NULL;
4066 char_u *p;
4067 int l;
4068 int retval = OK;
4069 int did_word = FALSE;
4070 int non_ascii = 0;
4071 int flags;
4072 int regionmask;
4073
4074 /*
4075 * Open the file.
4076 */
4077 fd = mch_fopen((char *)fname, "r");
4078 if (fd == NULL)
4079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004080 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004081 return FAIL;
4082 }
4083
Bram Moolenaarc1669272018-06-19 14:23:53 +02004084 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004085 spell_message(spin, IObuff);
4086
4087 /*
4088 * Read all the lines in the file one by one.
4089 */
4090 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4091 {
4092 line_breakcheck();
4093 ++lnum;
4094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004095 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004096 if (*rline == '#')
4097 continue;
4098
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004099 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004100 l = (int)STRLEN(rline);
4101 while (l > 0 && rline[l - 1] <= ' ')
4102 --l;
4103 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004104 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004105 rline[l] = NUL;
4106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004107 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004108 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004109 if (spin->si_conv.vc_type != CONV_NONE)
4110 {
4111 pc = string_convert(&spin->si_conv, rline, NULL);
4112 if (pc == NULL)
4113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004114 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004115 fname, lnum, rline);
4116 continue;
4117 }
4118 line = pc;
4119 }
4120 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004121 {
4122 pc = NULL;
4123 line = rline;
4124 }
4125
4126 if (*line == '/')
4127 {
4128 ++line;
4129 if (STRNCMP(line, "encoding=", 9) == 0)
4130 {
4131 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004132 smsg(_("Duplicate /encoding= line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004133 fname, lnum, line - 1);
4134 else if (did_word)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004135 smsg(_("/encoding= line after word ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004136 fname, lnum, line - 1);
4137 else
4138 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004139 char_u *enc;
4140
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004141 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004142 line += 9;
4143 enc = enc_canonize(line);
4144 if (enc != NULL && !spin->si_ascii
4145 && convert_setup(&spin->si_conv, enc,
4146 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004147 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004148 fname, line, p_enc);
4149 vim_free(enc);
4150 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004151 }
4152 continue;
4153 }
4154
4155 if (STRNCMP(line, "regions=", 8) == 0)
4156 {
4157 if (spin->si_region_count > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004158 smsg(_("Duplicate /regions= line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004159 fname, lnum, line);
4160 else
4161 {
4162 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004163 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 smsg(_("Too many regions in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004165 fname, lnum, line);
4166 else
4167 {
4168 spin->si_region_count = (int)STRLEN(line) / 2;
4169 STRCPY(spin->si_region_name, line);
4170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004171 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004172 spin->si_region = (1 << spin->si_region_count) - 1;
4173 }
4174 }
4175 continue;
4176 }
4177
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004178 smsg(_("/ line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004179 fname, lnum, line - 1);
4180 continue;
4181 }
4182
4183 flags = 0;
4184 regionmask = spin->si_region;
4185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004186 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004187 p = vim_strchr(line, '/');
4188 if (p != NULL)
4189 {
4190 *p++ = NUL;
4191 while (*p != NUL)
4192 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004193 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004194 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004195 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004196 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004197 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004198 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004199 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004200 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004201 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004202 regionmask = 0;
4203 flags |= WF_REGION;
4204
4205 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004206 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004208 smsg(_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004209 fname, lnum, p);
4210 break;
4211 }
4212 regionmask |= 1 << (l - 1);
4213 }
4214 else
4215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004216 smsg(_("Unrecognized flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004217 fname, lnum, p);
4218 break;
4219 }
4220 ++p;
4221 }
4222 }
4223
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004224 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004225 if (spin->si_ascii && has_non_ascii(line))
4226 {
4227 ++non_ascii;
4228 continue;
4229 }
4230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004231 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004232 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4233 {
4234 retval = FAIL;
4235 break;
4236 }
4237 did_word = TRUE;
4238 }
4239
4240 vim_free(pc);
4241 fclose(fd);
4242
4243 if (spin->si_ascii && non_ascii > 0)
4244 {
4245 vim_snprintf((char *)IObuff, IOSIZE,
4246 _("Ignored %d words with non-ASCII characters"), non_ascii);
4247 spell_message(spin, IObuff);
4248 }
4249
4250 return retval;
4251}
4252
4253/*
4254 * Get part of an sblock_T, "len" bytes long.
4255 * This avoids calling free() for every little struct we use (and keeping
4256 * track of them).
4257 * The memory is cleared to all zeros.
4258 * Returns NULL when out of memory.
4259 */
4260 static void *
4261getroom(
4262 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004263 size_t len, // length needed
4264 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004265{
4266 char_u *p;
4267 sblock_T *bl = spin->si_blocks;
4268
4269 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004270 // Round size up for alignment. On some systems structures need to be
4271 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004272 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4273 & ~(sizeof(char *) - 1);
4274
4275 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4276 {
4277 if (len >= SBLOCKSIZE)
4278 bl = NULL;
4279 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004280 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004281 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004282 if (bl == NULL)
4283 {
4284 if (!spin->si_did_emsg)
4285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 emsg(_("E845: Insufficient memory, word list will be incomplete"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004287 spin->si_did_emsg = TRUE;
4288 }
4289 return NULL;
4290 }
4291 bl->sb_next = spin->si_blocks;
4292 spin->si_blocks = bl;
4293 bl->sb_used = 0;
4294 ++spin->si_blocks_cnt;
4295 }
4296
4297 p = bl->sb_data + bl->sb_used;
4298 bl->sb_used += (int)len;
4299
4300 return p;
4301}
4302
4303/*
4304 * Make a copy of a string into memory allocated with getroom().
4305 * Returns NULL when out of memory.
4306 */
4307 static char_u *
4308getroom_save(spellinfo_T *spin, char_u *s)
4309{
4310 char_u *sc;
4311
4312 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4313 if (sc != NULL)
4314 STRCPY(sc, s);
4315 return sc;
4316}
4317
4318
4319/*
4320 * Free the list of allocated sblock_T.
4321 */
4322 static void
4323free_blocks(sblock_T *bl)
4324{
4325 sblock_T *next;
4326
4327 while (bl != NULL)
4328 {
4329 next = bl->sb_next;
4330 vim_free(bl);
4331 bl = next;
4332 }
4333}
4334
4335/*
4336 * Allocate the root of a word tree.
4337 * Returns NULL when out of memory.
4338 */
4339 static wordnode_T *
4340wordtree_alloc(spellinfo_T *spin)
4341{
4342 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4343}
4344
4345/*
4346 * Store a word in the tree(s).
4347 * Always store it in the case-folded tree. For a keep-case word this is
4348 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4349 * used to find suggestions.
4350 * For a keep-case word also store it in the keep-case tree.
4351 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4352 * compound flag.
4353 */
4354 static int
4355store_word(
4356 spellinfo_T *spin,
4357 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004358 int flags, // extra flags, WF_BANNED
4359 int region, // supported region(s)
4360 char_u *pfxlist, // list of prefix IDs or NULL
4361 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004362{
4363 int len = (int)STRLEN(word);
4364 int ct = captype(word, word + len);
4365 char_u foldword[MAXWLEN];
4366 int res = OK;
4367 char_u *p;
4368
4369 (void)spell_casefold(word, len, foldword, MAXWLEN);
4370 for (p = pfxlist; res == OK; ++p)
4371 {
4372 if (!need_affix || (p != NULL && *p != NUL))
4373 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4374 region, p == NULL ? 0 : *p);
4375 if (p == NULL || *p == NUL)
4376 break;
4377 }
4378 ++spin->si_foldwcount;
4379
4380 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4381 {
4382 for (p = pfxlist; res == OK; ++p)
4383 {
4384 if (!need_affix || (p != NULL && *p != NUL))
4385 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4386 region, p == NULL ? 0 : *p);
4387 if (p == NULL || *p == NUL)
4388 break;
4389 }
4390 ++spin->si_keepwcount;
4391 }
4392 return res;
4393}
4394
4395/*
4396 * Add word "word" to a word tree at "root".
4397 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4398 * "rare" and "region" is the condition nr.
4399 * Returns FAIL when out of memory.
4400 */
4401 static int
4402tree_add_word(
4403 spellinfo_T *spin,
4404 char_u *word,
4405 wordnode_T *root,
4406 int flags,
4407 int region,
4408 int affixID)
4409{
4410 wordnode_T *node = root;
4411 wordnode_T *np;
4412 wordnode_T *copyp, **copyprev;
4413 wordnode_T **prev = NULL;
4414 int i;
4415
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004416 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004417 for (i = 0; ; ++i)
4418 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004419 // When there is more than one reference to this node we need to make
4420 // a copy, so that we can modify it. Copy the whole list of siblings
4421 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004422 if (node != NULL && node->wn_refs > 1)
4423 {
4424 --node->wn_refs;
4425 copyprev = prev;
4426 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4427 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004428 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004429 np = get_wordnode(spin);
4430 if (np == NULL)
4431 return FAIL;
4432 np->wn_child = copyp->wn_child;
4433 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004434 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004435 np->wn_byte = copyp->wn_byte;
4436 if (np->wn_byte == NUL)
4437 {
4438 np->wn_flags = copyp->wn_flags;
4439 np->wn_region = copyp->wn_region;
4440 np->wn_affixID = copyp->wn_affixID;
4441 }
4442
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004443 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004444 np->wn_refs = 1;
4445 if (copyprev != NULL)
4446 *copyprev = np;
4447 copyprev = &np->wn_sibling;
4448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004449 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004450 if (copyp == node)
4451 node = np;
4452 }
4453 }
4454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004455 // Look for the sibling that has the same character. They are sorted
4456 // on byte value, thus stop searching when a sibling is found with a
4457 // higher byte value. For zero bytes (end of word) the sorting is
4458 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004459 while (node != NULL
4460 && (node->wn_byte < word[i]
4461 || (node->wn_byte == NUL
4462 && (flags < 0
4463 ? node->wn_affixID < (unsigned)affixID
4464 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4465 || (node->wn_flags == (flags & WN_MASK)
4466 && (spin->si_sugtree
4467 ? (node->wn_region & 0xffff) < region
4468 : node->wn_affixID
4469 < (unsigned)affixID)))))))
4470 {
4471 prev = &node->wn_sibling;
4472 node = *prev;
4473 }
4474 if (node == NULL
4475 || node->wn_byte != word[i]
4476 || (word[i] == NUL
4477 && (flags < 0
4478 || spin->si_sugtree
4479 || node->wn_flags != (flags & WN_MASK)
4480 || node->wn_affixID != affixID)))
4481 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004482 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004483 np = get_wordnode(spin);
4484 if (np == NULL)
4485 return FAIL;
4486 np->wn_byte = word[i];
4487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004488 // If "node" is NULL this is a new child or the end of the sibling
4489 // list: ref count is one. Otherwise use ref count of sibling and
4490 // make ref count of sibling one (matters when inserting in front
4491 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004492 if (node == NULL)
4493 np->wn_refs = 1;
4494 else
4495 {
4496 np->wn_refs = node->wn_refs;
4497 node->wn_refs = 1;
4498 }
4499 if (prev != NULL)
4500 *prev = np;
4501 np->wn_sibling = node;
4502 node = np;
4503 }
4504
4505 if (word[i] == NUL)
4506 {
4507 node->wn_flags = flags;
4508 node->wn_region |= region;
4509 node->wn_affixID = affixID;
4510 break;
4511 }
4512 prev = &node->wn_child;
4513 node = *prev;
4514 }
4515#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004516 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004517 spell_print_tree(root->wn_sibling);
4518#endif
4519
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004520 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004521 ++spin->si_msg_count;
4522
4523 if (spin->si_compress_cnt > 1)
4524 {
4525 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004526 // Did enough words to lower the block count limit.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004527 spin->si_blocks_cnt += compress_inc;
4528 }
4529
4530 /*
4531 * When we have allocated lots of memory we need to compress the word tree
4532 * to free up some room. But compression is slow, and we might actually
4533 * need that room, thus only compress in the following situations:
4534 * 1. When not compressed before (si_compress_cnt == 0): when using
4535 * "compress_start" blocks.
4536 * 2. When compressed before and used "compress_inc" blocks before
4537 * adding "compress_added" words (si_compress_cnt > 1).
4538 * 3. When compressed before, added "compress_added" words
4539 * (si_compress_cnt == 1) and the number of free nodes drops below the
4540 * maximum word length.
4541 */
4542#ifndef SPELL_COMPRESS_ALLWAYS
4543 if (spin->si_compress_cnt == 1
4544 ? spin->si_free_count < MAXWLEN
4545 : spin->si_blocks_cnt >= compress_start)
4546#endif
4547 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004548 // Decrement the block counter. The effect is that we compress again
4549 // when the freed up room has been used and another "compress_inc"
4550 // blocks have been allocated. Unless "compress_added" words have
4551 // been added, then the limit is put back again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004552 spin->si_blocks_cnt -= compress_inc;
4553 spin->si_compress_cnt = compress_added;
4554
4555 if (spin->si_verbose)
4556 {
4557 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004558 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004559 msg_clr_eos();
4560 msg_didout = FALSE;
4561 msg_col = 0;
4562 out_flush();
4563 }
4564
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004565 // Compress both trees. Either they both have many nodes, which makes
4566 // compression useful, or one of them is small, which means
4567 // compression goes fast. But when filling the soundfold word tree
4568 // there is no keep-case tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004569 wordtree_compress(spin, spin->si_foldroot);
4570 if (affixID >= 0)
4571 wordtree_compress(spin, spin->si_keeproot);
4572 }
4573
4574 return OK;
4575}
4576
4577/*
4578 * Get a wordnode_T, either from the list of previously freed nodes or
4579 * allocate a new one.
4580 * Returns NULL when out of memory.
4581 */
4582 static wordnode_T *
4583get_wordnode(spellinfo_T *spin)
4584{
4585 wordnode_T *n;
4586
4587 if (spin->si_first_free == NULL)
4588 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4589 else
4590 {
4591 n = spin->si_first_free;
4592 spin->si_first_free = n->wn_child;
4593 vim_memset(n, 0, sizeof(wordnode_T));
4594 --spin->si_free_count;
4595 }
4596#ifdef SPELL_PRINTTREE
4597 if (n != NULL)
4598 n->wn_nr = ++spin->si_wordnode_nr;
4599#endif
4600 return n;
4601}
4602
4603/*
4604 * Decrement the reference count on a node (which is the head of a list of
4605 * siblings). If the reference count becomes zero free the node and its
4606 * siblings.
4607 * Returns the number of nodes actually freed.
4608 */
4609 static int
4610deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4611{
4612 wordnode_T *np;
4613 int cnt = 0;
4614
4615 if (--node->wn_refs == 0)
4616 {
4617 for (np = node; np != NULL; np = np->wn_sibling)
4618 {
4619 if (np->wn_child != NULL)
4620 cnt += deref_wordnode(spin, np->wn_child);
4621 free_wordnode(spin, np);
4622 ++cnt;
4623 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004624 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004625 }
4626 return cnt;
4627}
4628
4629/*
4630 * Free a wordnode_T for re-use later.
4631 * Only the "wn_child" field becomes invalid.
4632 */
4633 static void
4634free_wordnode(spellinfo_T *spin, wordnode_T *n)
4635{
4636 n->wn_child = spin->si_first_free;
4637 spin->si_first_free = n;
4638 ++spin->si_free_count;
4639}
4640
4641/*
4642 * Compress a tree: find tails that are identical and can be shared.
4643 */
4644 static void
4645wordtree_compress(spellinfo_T *spin, wordnode_T *root)
4646{
4647 hashtab_T ht;
4648 int n;
4649 int tot = 0;
4650 int perc;
4651
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004652 // Skip the root itself, it's not actually used. The first sibling is the
4653 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004654 if (root->wn_sibling != NULL)
4655 {
4656 hash_init(&ht);
4657 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4658
4659#ifndef SPELL_PRINTTREE
4660 if (spin->si_verbose || p_verbose > 2)
4661#endif
4662 {
4663 if (tot > 1000000)
4664 perc = (tot - n) / (tot / 100);
4665 else if (tot == 0)
4666 perc = 0;
4667 else
4668 perc = (tot - n) * 100 / tot;
4669 vim_snprintf((char *)IObuff, IOSIZE,
4670 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
4671 n, tot, tot - n, perc);
4672 spell_message(spin, IObuff);
4673 }
4674#ifdef SPELL_PRINTTREE
4675 spell_print_tree(root->wn_sibling);
4676#endif
4677 hash_clear(&ht);
4678 }
4679}
4680
4681/*
4682 * Compress a node, its siblings and its children, depth first.
4683 * Returns the number of compressed nodes.
4684 */
4685 static int
4686node_compress(
4687 spellinfo_T *spin,
4688 wordnode_T *node,
4689 hashtab_T *ht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004690 int *tot) // total count of nodes before compressing,
4691 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004692{
4693 wordnode_T *np;
4694 wordnode_T *tp;
4695 wordnode_T *child;
4696 hash_T hash;
4697 hashitem_T *hi;
4698 int len = 0;
4699 unsigned nr, n;
4700 int compressed = 0;
4701
4702 /*
4703 * Go through the list of siblings. Compress each child and then try
4704 * finding an identical child to replace it.
4705 * Note that with "child" we mean not just the node that is pointed to,
4706 * but the whole list of siblings of which the child node is the first.
4707 */
4708 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4709 {
4710 ++len;
4711 if ((child = np->wn_child) != NULL)
4712 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004713 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004714 compressed += node_compress(spin, child, ht, tot);
4715
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004716 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004717 hash = hash_hash(child->wn_u1.hashkey);
4718 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4719 if (!HASHITEM_EMPTY(hi))
4720 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004721 // There are children we encountered before with a hash value
4722 // identical to the current child. Now check if there is one
4723 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004724 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4725 if (node_equal(child, tp))
4726 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004727 // Found one! Now use that child in place of the
4728 // current one. This means the current child and all
4729 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004730 ++tp->wn_refs;
4731 compressed += deref_wordnode(spin, child);
4732 np->wn_child = tp;
4733 break;
4734 }
4735 if (tp == NULL)
4736 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004737 // No other child with this hash value equals the child of
4738 // the node, add it to the linked list after the first
4739 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004740 tp = HI2WN(hi);
4741 child->wn_u2.next = tp->wn_u2.next;
4742 tp->wn_u2.next = child;
4743 }
4744 }
4745 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004746 // No other child has this hash value, add it to the
4747 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004748 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4749 }
4750 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004751 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004752
4753 /*
4754 * Make a hash key for the node and its siblings, so that we can quickly
4755 * find a lookalike node. This must be done after compressing the sibling
4756 * list, otherwise the hash key would become invalid by the compression.
4757 */
4758 node->wn_u1.hashkey[0] = len;
4759 nr = 0;
4760 for (np = node; np != NULL; np = np->wn_sibling)
4761 {
4762 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004763 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004764 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4765 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004766 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004767 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4768 nr = nr * 101 + n;
4769 }
4770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004771 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004772 n = nr & 0xff;
4773 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4774 n = (nr >> 8) & 0xff;
4775 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4776 n = (nr >> 16) & 0xff;
4777 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4778 n = (nr >> 24) & 0xff;
4779 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4780 node->wn_u1.hashkey[5] = NUL;
4781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004782 // Check for CTRL-C pressed now and then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004783 fast_breakcheck();
4784
4785 return compressed;
4786}
4787
4788/*
4789 * Return TRUE when two nodes have identical siblings and children.
4790 */
4791 static int
4792node_equal(wordnode_T *n1, wordnode_T *n2)
4793{
4794 wordnode_T *p1;
4795 wordnode_T *p2;
4796
4797 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4798 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4799 if (p1->wn_byte != p2->wn_byte
4800 || (p1->wn_byte == NUL
4801 ? (p1->wn_flags != p2->wn_flags
4802 || p1->wn_region != p2->wn_region
4803 || p1->wn_affixID != p2->wn_affixID)
4804 : (p1->wn_child != p2->wn_child)))
4805 break;
4806
4807 return p1 == NULL && p2 == NULL;
4808}
4809
Bram Moolenaareae1b912019-05-09 15:12:55 +02004810static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004811
4812/*
4813 * Function given to qsort() to sort the REP items on "from" string.
4814 */
4815 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004816rep_compare(const void *s1, const void *s2)
4817{
4818 fromto_T *p1 = (fromto_T *)s1;
4819 fromto_T *p2 = (fromto_T *)s2;
4820
4821 return STRCMP(p1->ft_from, p2->ft_from);
4822}
4823
4824/*
4825 * Write the Vim .spl file "fname".
4826 * Return FAIL or OK;
4827 */
4828 static int
4829write_vim_spell(spellinfo_T *spin, char_u *fname)
4830{
4831 FILE *fd;
4832 int regionmask;
4833 int round;
4834 wordnode_T *tree;
4835 int nodecount;
4836 int i;
4837 int l;
4838 garray_T *gap;
4839 fromto_T *ftp;
4840 char_u *p;
4841 int rr;
4842 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004843 size_t fwv = 1; // collect return value of fwrite() to avoid
4844 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004845
4846 fd = mch_fopen((char *)fname, "w");
4847 if (fd == NULL)
4848 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004849 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004850 return FAIL;
4851 }
4852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004853 // <HEADER>: <fileID> <versionnr>
4854 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004855 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4856 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004857 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004858 goto theend;
4859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004860 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004861
4862 /*
4863 * <SECTIONS>: <section> ... <sectionend>
4864 */
4865
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004866 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004867 if (spin->si_info != NULL)
4868 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004869 putc(SN_INFO, fd); // <sectionID>
4870 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004871
4872 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004873 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4874 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004875 }
4876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004877 // SN_REGION: <regionname> ...
4878 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004879 if (spin->si_region_count > 1)
4880 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004881 putc(SN_REGION, fd); // <sectionID>
4882 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004883 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004884 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004885 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004886 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004887 regionmask = (1 << spin->si_region_count) - 1;
4888 }
4889 else
4890 regionmask = 0;
4891
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004892 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4893 //
4894 // The table with character flags and the table for case folding.
4895 // This makes sure the same characters are recognized as word characters
4896 // when generating an when using a spell file.
4897 // Skip this for ASCII, the table may conflict with the one used for
4898 // 'encoding'.
4899 // Also skip this for an .add.spl file, the main spell file must contain
4900 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004901 if (!spin->si_ascii && !spin->si_add)
4902 {
4903 char_u folchars[128 * 8];
4904 int flags;
4905
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004906 putc(SN_CHARFLAGS, fd); // <sectionID>
4907 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004909 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004910 l = 0;
4911 for (i = 128; i < 256; ++i)
4912 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004913 if (has_mbyte)
4914 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4915 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004916 folchars[l++] = spelltab.st_fold[i];
4917 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004918 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004920 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004921 for (i = 128; i < 256; ++i)
4922 {
4923 flags = 0;
4924 if (spelltab.st_isw[i])
4925 flags |= CF_WORD;
4926 if (spelltab.st_isu[i])
4927 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004928 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004929 }
4930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004931 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4932 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004933 }
4934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004935 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004936 if (spin->si_midword != NULL)
4937 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004938 putc(SN_MIDWORD, fd); // <sectionID>
4939 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004940
4941 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004942 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004943 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004944 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004945 }
4946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004947 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004948 if (spin->si_prefcond.ga_len > 0)
4949 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004950 putc(SN_PREFCOND, fd); // <sectionID>
4951 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004952
4953 l = write_spell_prefcond(NULL, &spin->si_prefcond);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004954 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004955
4956 write_spell_prefcond(fd, &spin->si_prefcond);
4957 }
4958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004959 // SN_REP: <repcount> <rep> ...
4960 // SN_SAL: <salflags> <salcount> <sal> ...
4961 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004963 // round 1: SN_REP section
4964 // round 2: SN_SAL section (unless SN_SOFO is used)
4965 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004966 for (round = 1; round <= 3; ++round)
4967 {
4968 if (round == 1)
4969 gap = &spin->si_rep;
4970 else if (round == 2)
4971 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004972 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004973 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4974 continue;
4975 gap = &spin->si_sal;
4976 }
4977 else
4978 gap = &spin->si_repsal;
4979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004980 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004981 if (gap->ga_len == 0)
4982 continue;
4983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004984 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004985 if (round != 2)
4986 qsort(gap->ga_data, (size_t)gap->ga_len,
4987 sizeof(fromto_T), rep_compare);
4988
4989 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004990 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004991
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004992 // This is for making suggestions, section is not required.
4993 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004994
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004995 // Compute the length of what follows.
4996 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004997 for (i = 0; i < gap->ga_len; ++i)
4998 {
4999 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005000 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
5001 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005002 }
5003 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005004 ++l; // count <salflags>
5005 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005006
5007 if (round == 2)
5008 {
5009 i = 0;
5010 if (spin->si_followup)
5011 i |= SAL_F0LLOWUP;
5012 if (spin->si_collapse)
5013 i |= SAL_COLLAPSE;
5014 if (spin->si_rem_accents)
5015 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005016 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005017 }
5018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005019 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005020 for (i = 0; i < gap->ga_len; ++i)
5021 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005022 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5023 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005024 ftp = &((fromto_T *)gap->ga_data)[i];
5025 for (rr = 1; rr <= 2; ++rr)
5026 {
5027 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5028 l = (int)STRLEN(p);
5029 putc(l, fd);
5030 if (l > 0)
5031 fwv &= fwrite(p, l, (size_t)1, fd);
5032 }
5033 }
5034
5035 }
5036
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005037 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5038 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005039 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5040 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005041 putc(SN_SOFO, fd); // <sectionID>
5042 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005043
5044 l = (int)STRLEN(spin->si_sofofr);
5045 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005046 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005048 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5049 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005050
5051 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005052 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5053 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005054 }
5055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005056 // SN_WORDS: <word> ...
5057 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005058 if (spin->si_commonwords.ht_used > 0)
5059 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005060 putc(SN_WORDS, fd); // <sectionID>
5061 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005063 // round 1: count the bytes
5064 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005065 for (round = 1; round <= 2; ++round)
5066 {
5067 int todo;
5068 int len = 0;
5069 hashitem_T *hi;
5070
5071 todo = (int)spin->si_commonwords.ht_used;
5072 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5073 if (!HASHITEM_EMPTY(hi))
5074 {
5075 l = (int)STRLEN(hi->hi_key) + 1;
5076 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005077 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005078 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5079 --todo;
5080 }
5081 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005082 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005083 }
5084 }
5085
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005086 // SN_MAP: <mapstr>
5087 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005088 if (spin->si_map.ga_len > 0)
5089 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005090 putc(SN_MAP, fd); // <sectionID>
5091 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005092 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005093 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005094 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005095 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005096 }
5097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005098 // SN_SUGFILE: <timestamp>
5099 // This is used to notify that a .sug file may be available and at the
5100 // same time allows for checking that a .sug file that is found matches
5101 // with this .spl file. That's because the word numbers must be exactly
5102 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005103 if (!spin->si_nosugfile
5104 && (spin->si_sal.ga_len > 0
5105 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5106 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005107 putc(SN_SUGFILE, fd); // <sectionID>
5108 putc(0, fd); // <sectionflags>
5109 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005111 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005112 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005113 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005114 }
5115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005116 // SN_NOSPLITSUGS: nothing
5117 // This is used to notify that no suggestions with word splits are to be
5118 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005119 if (spin->si_nosplitsugs)
5120 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005121 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5122 putc(0, fd); // <sectionflags>
5123 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005124 }
5125
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005126 // SN_NOCOMPUNDSUGS: nothing
5127 // This is used to notify that no suggestions with compounds are to be
5128 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005129 if (spin->si_nocompoundsugs)
5130 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005131 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5132 putc(0, fd); // <sectionflags>
5133 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005134 }
5135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005136 // SN_COMPOUND: compound info.
5137 // We don't mark it required, when not supported all compound words will
5138 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005139 if (spin->si_compflags != NULL)
5140 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005141 putc(SN_COMPOUND, fd); // <sectionID>
5142 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005143
5144 l = (int)STRLEN(spin->si_compflags);
5145 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5146 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005147 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005149 putc(spin->si_compmax, fd); // <compmax>
5150 putc(spin->si_compminlen, fd); // <compminlen>
5151 putc(spin->si_compsylmax, fd); // <compsylmax>
5152 putc(0, fd); // for Vim 7.0b compatibility
5153 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005154 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005155 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005156 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5157 {
5158 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005159 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005160 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005161 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005162 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005163 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005164 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5165 (size_t)1, fd);
5166 }
5167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005168 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005169 if (spin->si_nobreak)
5170 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005171 putc(SN_NOBREAK, fd); // <sectionID>
5172 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005174 // It's empty, the presence of the section flags the feature.
5175 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005176 }
5177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005178 // SN_SYLLABLE: syllable info.
5179 // We don't mark it required, when not supported syllables will not be
5180 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005181 if (spin->si_syllable != NULL)
5182 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005183 putc(SN_SYLLABLE, fd); // <sectionID>
5184 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005185
5186 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005187 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005188 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005189 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005190 }
5191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005192 // end of <SECTIONS>
5193 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005194
5195
5196 /*
5197 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5198 */
5199 spin->si_memtot = 0;
5200 for (round = 1; round <= 3; ++round)
5201 {
5202 if (round == 1)
5203 tree = spin->si_foldroot->wn_sibling;
5204 else if (round == 2)
5205 tree = spin->si_keeproot->wn_sibling;
5206 else
5207 tree = spin->si_prefroot->wn_sibling;
5208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005209 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005210 clear_node(tree);
5211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005212 // Count the number of nodes. Needed to be able to allocate the
5213 // memory when reading the nodes. Also fills in index for shared
5214 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005215 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005217 // number of nodes in 4 bytes
5218 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005219 spin->si_memtot += nodecount + nodecount * sizeof(int);
5220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005221 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005222 (void)put_node(fd, tree, 0, regionmask, round == 3);
5223 }
5224
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005225 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005226 if (putc(0, fd) == EOF)
5227 retval = FAIL;
5228theend:
5229 if (fclose(fd) == EOF)
5230 retval = FAIL;
5231
5232 if (fwv != (size_t)1)
5233 retval = FAIL;
5234 if (retval == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005235 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005236
5237 return retval;
5238}
5239
5240/*
5241 * Clear the index and wnode fields of "node", it siblings and its
5242 * children. This is needed because they are a union with other items to save
5243 * space.
5244 */
5245 static void
5246clear_node(wordnode_T *node)
5247{
5248 wordnode_T *np;
5249
5250 if (node != NULL)
5251 for (np = node; np != NULL; np = np->wn_sibling)
5252 {
5253 np->wn_u1.index = 0;
5254 np->wn_u2.wnode = NULL;
5255
5256 if (np->wn_byte != NUL)
5257 clear_node(np->wn_child);
5258 }
5259}
5260
5261
5262/*
5263 * Dump a word tree at node "node".
5264 *
5265 * This first writes the list of possible bytes (siblings). Then for each
5266 * byte recursively write the children.
5267 *
5268 * NOTE: The code here must match the code in read_tree_node(), since
5269 * assumptions are made about the indexes (so that we don't have to write them
5270 * in the file).
5271 *
5272 * Returns the number of nodes used.
5273 */
5274 static int
5275put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005276 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005277 wordnode_T *node,
5278 int idx,
5279 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005280 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005281{
5282 int newindex = idx;
5283 int siblingcount = 0;
5284 wordnode_T *np;
5285 int flags;
5286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005287 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005288 if (node == NULL)
5289 return 0;
5290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005291 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005292 node->wn_u1.index = idx;
5293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005294 // Count the number of siblings.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005295 for (np = node; np != NULL; np = np->wn_sibling)
5296 ++siblingcount;
5297
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005299 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005300 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005302 // Write each sibling byte and optionally extra info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005303 for (np = node; np != NULL; np = np->wn_sibling)
5304 {
5305 if (np->wn_byte == 0)
5306 {
5307 if (fd != NULL)
5308 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005309 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005310 if (prefixtree)
5311 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005312 // In PREFIXTREE write the required affixID and the
5313 // associated condition nr (stored in wn_region). The
5314 // byte value is misused to store the "rare" and "not
5315 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005316 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005317 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005318 else
5319 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005320 putc(BY_FLAGS, fd); // <byte>
5321 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005322 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005323 putc(np->wn_affixID, fd); // <affixID>
5324 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005325 }
5326 else
5327 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005328 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005329 flags = np->wn_flags;
5330 if (regionmask != 0 && np->wn_region != regionmask)
5331 flags |= WF_REGION;
5332 if (np->wn_affixID != 0)
5333 flags |= WF_AFX;
5334 if (flags == 0)
5335 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005336 // word without flags or region
5337 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005338 }
5339 else
5340 {
5341 if (np->wn_flags >= 0x100)
5342 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005343 putc(BY_FLAGS2, fd); // <byte>
5344 putc(flags, fd); // <flags>
5345 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005346 }
5347 else
5348 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005349 putc(BY_FLAGS, fd); // <byte>
5350 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005351 }
5352 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005353 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005354 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005355 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005356 }
5357 }
5358 }
5359 }
5360 else
5361 {
5362 if (np->wn_child->wn_u1.index != 0
5363 && np->wn_child->wn_u2.wnode != node)
5364 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005365 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005366 if (fd != NULL)
5367 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 putc(BY_INDEX, fd); // <byte>
5369 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005370 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5371 }
5372 }
5373 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005374 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005375 np->wn_child->wn_u2.wnode = node;
5376
5377 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005378 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005380 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005381 return 0;
5382 }
5383 }
5384 }
5385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005386 // Space used in the array when reading: one for each sibling and one for
5387 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005388 newindex += siblingcount + 1;
5389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005390 // Recursively dump the children of each sibling.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005391 for (np = node; np != NULL; np = np->wn_sibling)
5392 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5393 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5394 prefixtree);
5395
5396 return newindex;
5397}
5398
5399
5400/*
5401 * ":mkspell [-ascii] outfile infile ..."
5402 * ":mkspell [-ascii] addfile"
5403 */
5404 void
5405ex_mkspell(exarg_T *eap)
5406{
5407 int fcount;
5408 char_u **fnames;
5409 char_u *arg = eap->arg;
5410 int ascii = FALSE;
5411
5412 if (STRNCMP(arg, "-ascii", 6) == 0)
5413 {
5414 ascii = TRUE;
5415 arg = skipwhite(arg + 6);
5416 }
5417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005418 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005419 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5420 {
5421 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5422 FreeWild(fcount, fnames);
5423 }
5424}
5425
5426/*
5427 * Create the .sug file.
5428 * Uses the soundfold info in "spin".
5429 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5430 */
5431 static void
5432spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5433{
5434 char_u *fname = NULL;
5435 int len;
5436 slang_T *slang;
5437 int free_slang = FALSE;
5438
5439 /*
5440 * Read back the .spl file that was written. This fills the required
5441 * info for soundfolding. This also uses less memory than the
5442 * pointer-linked version of the trie. And it avoids having two versions
5443 * of the code for the soundfolding stuff.
5444 * It might have been done already by spell_reload_one().
5445 */
5446 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005447 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005448 break;
5449 if (slang == NULL)
5450 {
5451 spell_message(spin, (char_u *)_("Reading back spell file..."));
5452 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5453 if (slang == NULL)
5454 return;
5455 free_slang = TRUE;
5456 }
5457
5458 /*
5459 * Clear the info in "spin" that is used.
5460 */
5461 spin->si_blocks = NULL;
5462 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005463 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005464 spin->si_free_count = 0;
5465 spin->si_first_free = NULL;
5466 spin->si_foldwcount = 0;
5467
5468 /*
5469 * Go through the trie of good words, soundfold each word and add it to
5470 * the soundfold trie.
5471 */
5472 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5473 if (sug_filltree(spin, slang) == FAIL)
5474 goto theend;
5475
5476 /*
5477 * Create the table which links each soundfold word with a list of the
5478 * good words it may come from. Creates buffer "spin->si_spellbuf".
5479 * This also removes the wordnr from the NUL byte entries to make
5480 * compression possible.
5481 */
5482 if (sug_maketable(spin) == FAIL)
5483 goto theend;
5484
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005485 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005486 (long)spin->si_spellbuf->b_ml.ml_line_count);
5487
5488 /*
5489 * Compress the soundfold trie.
5490 */
5491 spell_message(spin, (char_u *)_(msg_compressing));
5492 wordtree_compress(spin, spin->si_foldroot);
5493
5494 /*
5495 * Write the .sug file.
5496 * Make the file name by changing ".spl" to ".sug".
5497 */
5498 fname = alloc(MAXPATHL);
5499 if (fname == NULL)
5500 goto theend;
5501 vim_strncpy(fname, wfname, MAXPATHL - 1);
5502 len = (int)STRLEN(fname);
5503 fname[len - 2] = 'u';
5504 fname[len - 1] = 'g';
5505 sug_write(spin, fname);
5506
5507theend:
5508 vim_free(fname);
5509 if (free_slang)
5510 slang_free(slang);
5511 free_blocks(spin->si_blocks);
5512 close_spellbuf(spin->si_spellbuf);
5513}
5514
5515/*
5516 * Build the soundfold trie for language "slang".
5517 */
5518 static int
5519sug_filltree(spellinfo_T *spin, slang_T *slang)
5520{
5521 char_u *byts;
5522 idx_T *idxs;
5523 int depth;
5524 idx_T arridx[MAXWLEN];
5525 int curi[MAXWLEN];
5526 char_u tword[MAXWLEN];
5527 char_u tsalword[MAXWLEN];
5528 int c;
5529 idx_T n;
5530 unsigned words_done = 0;
5531 int wordcount[MAXWLEN];
5532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005533 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005534 spin->si_foldroot = wordtree_alloc(spin);
5535 if (spin->si_foldroot == NULL)
5536 return FAIL;
5537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005538 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005539 spin->si_sugtree = TRUE;
5540
5541 /*
5542 * Go through the whole case-folded tree, soundfold each word and put it
5543 * in the trie.
5544 */
5545 byts = slang->sl_fbyts;
5546 idxs = slang->sl_fidxs;
5547
5548 arridx[0] = 0;
5549 curi[0] = 1;
5550 wordcount[0] = 0;
5551
5552 depth = 0;
5553 while (depth >= 0 && !got_int)
5554 {
5555 if (curi[depth] > byts[arridx[depth]])
5556 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005557 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005558 idxs[arridx[depth]] = wordcount[depth];
5559 if (depth > 0)
5560 wordcount[depth - 1] += wordcount[depth];
5561
5562 --depth;
5563 line_breakcheck();
5564 }
5565 else
5566 {
5567
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005568 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005569 n = arridx[depth] + curi[depth];
5570 ++curi[depth];
5571
5572 c = byts[n];
5573 if (c == 0)
5574 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005575 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005576 tword[depth] = NUL;
5577 spell_soundfold(slang, tword, TRUE, tsalword);
5578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005579 // We use the "flags" field for the MSB of the wordnr,
5580 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005581 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5582 words_done >> 16, words_done & 0xffff,
5583 0) == FAIL)
5584 return FAIL;
5585
5586 ++words_done;
5587 ++wordcount[depth];
5588
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005589 // Reset the block count each time to avoid compression
5590 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005591 spin->si_blocks_cnt = 0;
5592
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005593 // Skip over any other NUL bytes (same word with different
5594 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005595 while (byts[n + 1] == 0)
5596 {
5597 ++n;
5598 ++curi[depth];
5599 }
5600 }
5601 else
5602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005603 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005604 tword[depth++] = c;
5605 arridx[depth] = idxs[n];
5606 curi[depth] = 1;
5607 wordcount[depth] = 0;
5608 }
5609 }
5610 }
5611
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005612 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005613
5614 return OK;
5615}
5616
5617/*
5618 * Make the table that links each word in the soundfold trie to the words it
5619 * can be produced from.
5620 * This is not unlike lines in a file, thus use a memfile to be able to access
5621 * the table efficiently.
5622 * Returns FAIL when out of memory.
5623 */
5624 static int
5625sug_maketable(spellinfo_T *spin)
5626{
5627 garray_T ga;
5628 int res = OK;
5629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005630 // Allocate a buffer, open a memline for it and create the swap file
5631 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005632 spin->si_spellbuf = open_spellbuf();
5633 if (spin->si_spellbuf == NULL)
5634 return FAIL;
5635
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005636 // Use a buffer to store the line info, avoids allocating many small
5637 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005638 ga_init2(&ga, 1, 100);
5639
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005640 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005641 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5642 res = FAIL;
5643
5644 ga_clear(&ga);
5645 return res;
5646}
5647
5648/*
5649 * Fill the table for one node and its children.
5650 * Returns the wordnr at the start of the node.
5651 * Returns -1 when out of memory.
5652 */
5653 static int
5654sug_filltable(
5655 spellinfo_T *spin,
5656 wordnode_T *node,
5657 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005658 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005659{
5660 wordnode_T *p, *np;
5661 int wordnr = startwordnr;
5662 int nr;
5663 int prev_nr;
5664
5665 for (p = node; p != NULL; p = p->wn_sibling)
5666 {
5667 if (p->wn_byte == NUL)
5668 {
5669 gap->ga_len = 0;
5670 prev_nr = 0;
5671 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5672 {
5673 if (ga_grow(gap, 10) == FAIL)
5674 return -1;
5675
5676 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005677 // Compute the offset from the previous nr and store the
5678 // offset in a way that it takes a minimum number of bytes.
5679 // It's a bit like utf-8, but without the need to mark
5680 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005681 nr -= prev_nr;
5682 prev_nr += nr;
5683 gap->ga_len += offset2bytes(nr,
5684 (char_u *)gap->ga_data + gap->ga_len);
5685 }
5686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005687 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005688 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5689
5690 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5691 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5692 return -1;
5693 ++wordnr;
5694
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005695 // Remove extra NUL entries, we no longer need them. We don't
5696 // bother freeing the nodes, the won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005697 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5698 p->wn_sibling = p->wn_sibling->wn_sibling;
5699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005700 // Clear the flags on the remaining NUL node, so that compression
5701 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005702 p->wn_flags = 0;
5703 p->wn_region = 0;
5704 }
5705 else
5706 {
5707 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5708 if (wordnr == -1)
5709 return -1;
5710 }
5711 }
5712 return wordnr;
5713}
5714
5715/*
5716 * Convert an offset into a minimal number of bytes.
5717 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5718 * bytes.
5719 */
5720 static int
5721offset2bytes(int nr, char_u *buf)
5722{
5723 int rem;
5724 int b1, b2, b3, b4;
5725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005726 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005727 b1 = nr % 255 + 1;
5728 rem = nr / 255;
5729 b2 = rem % 255 + 1;
5730 rem = rem / 255;
5731 b3 = rem % 255 + 1;
5732 b4 = rem / 255 + 1;
5733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005734 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005735 {
5736 buf[0] = 0xe0 + b4;
5737 buf[1] = b3;
5738 buf[2] = b2;
5739 buf[3] = b1;
5740 return 4;
5741 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005742 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005743 {
5744 buf[0] = 0xc0 + b3;
5745 buf[1] = b2;
5746 buf[2] = b1;
5747 return 3;
5748 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005749 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005750 {
5751 buf[0] = 0x80 + b2;
5752 buf[1] = b1;
5753 return 2;
5754 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005755 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005756 buf[0] = b1;
5757 return 1;
5758}
5759
5760/*
5761 * Write the .sug file in "fname".
5762 */
5763 static void
5764sug_write(spellinfo_T *spin, char_u *fname)
5765{
5766 FILE *fd;
5767 wordnode_T *tree;
5768 int nodecount;
5769 int wcount;
5770 char_u *line;
5771 linenr_T lnum;
5772 int len;
5773
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005774 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005775 fd = mch_fopen((char *)fname, "w");
5776 if (fd == NULL)
5777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005778 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005779 return;
5780 }
5781
5782 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005783 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005784 spell_message(spin, IObuff);
5785
5786 /*
5787 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5788 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005789 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005791 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005792 goto theend;
5793 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005794 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005796 // Write si_sugtime to the file.
5797 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005798
5799 /*
5800 * <SUGWORDTREE>
5801 */
5802 spin->si_memtot = 0;
5803 tree = spin->si_foldroot->wn_sibling;
5804
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005805 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005806 clear_node(tree);
5807
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005808 // Count the number of nodes. Needed to be able to allocate the
5809 // memory when reading the nodes. Also fills in index for shared
5810 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005811 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005813 // number of nodes in 4 bytes
5814 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005815 spin->si_memtot += nodecount + nodecount * sizeof(int);
5816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005817 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005818 (void)put_node(fd, tree, 0, 0, FALSE);
5819
5820 /*
5821 * <SUGTABLE>: <sugwcount> <sugline> ...
5822 */
5823 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005824 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005825
5826 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5827 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005828 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005829 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5830 len = (int)STRLEN(line) + 1;
5831 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005833 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005834 goto theend;
5835 }
5836 spin->si_memtot += len;
5837 }
5838
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005839 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005840 if (putc(0, fd) == EOF)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005841 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005842
5843 vim_snprintf((char *)IObuff, IOSIZE,
5844 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5845 spell_message(spin, IObuff);
5846
5847theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005848 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005849 fclose(fd);
5850}
5851
5852
5853/*
5854 * Create a Vim spell file from one or more word lists.
5855 * "fnames[0]" is the output file name.
5856 * "fnames[fcount - 1]" is the last input file name.
5857 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5858 * and ".spl" is appended to make the output file name.
5859 */
5860 void
5861mkspell(
5862 int fcount,
5863 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005864 int ascii, // -ascii argument given
5865 int over_write, // overwrite existing output file
5866 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005867{
5868 char_u *fname = NULL;
5869 char_u *wfname;
5870 char_u **innames;
5871 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005872 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005873 int i;
5874 int len;
5875 stat_T st;
5876 int error = FALSE;
5877 spellinfo_T spin;
5878
5879 vim_memset(&spin, 0, sizeof(spin));
5880 spin.si_verbose = !added_word;
5881 spin.si_ascii = ascii;
5882 spin.si_followup = TRUE;
5883 spin.si_rem_accents = TRUE;
5884 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5885 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
5886 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5887 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
5888 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
5889 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
5890 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005891 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005892
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005893 // default: fnames[0] is output file, following are input files
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005894 innames = &fnames[1];
5895 incount = fcount - 1;
5896
5897 wfname = alloc(MAXPATHL);
5898 if (wfname == NULL)
5899 return;
5900
5901 if (fcount >= 1)
5902 {
5903 len = (int)STRLEN(fnames[0]);
5904 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5905 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005906 // For ":mkspell path/en.latin1.add" output file is
5907 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005908 innames = &fnames[0];
5909 incount = 1;
5910 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5911 }
5912 else if (fcount == 1)
5913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005914 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005915 innames = &fnames[0];
5916 incount = 1;
5917 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5918 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5919 }
5920 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5921 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005922 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005923 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5924 }
5925 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005926 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005927 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5928 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005930 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005931 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5932 spin.si_ascii = TRUE;
5933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005934 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005935 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5936 spin.si_add = TRUE;
5937 }
5938
5939 if (incount <= 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005940 emsg(_(e_invarg)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005941 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005942 emsg(_("E751: Output file name must not have region name"));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005943 else if (incount > MAXREGIONS)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005944 semsg(_("E754: Only up to %d regions supported"), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005945 else
5946 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005947 // Check for overwriting before doing things that may take a lot of
5948 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005949 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
5950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005951 emsg(_(e_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005952 goto theend;
5953 }
5954 if (mch_isdir(wfname))
5955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005956 semsg(_(e_isadir2), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005957 goto theend;
5958 }
5959
5960 fname = alloc(MAXPATHL);
5961 if (fname == NULL)
5962 goto theend;
5963
5964 /*
5965 * Init the aff and dic pointers.
5966 * Get the region names if there are more than 2 arguments.
5967 */
5968 for (i = 0; i < incount; ++i)
5969 {
5970 afile[i] = NULL;
5971
5972 if (incount > 1)
5973 {
5974 len = (int)STRLEN(innames[i]);
5975 if (STRLEN(gettail(innames[i])) < 5
5976 || innames[i][len - 3] != '_')
5977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005978 semsg(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005979 goto theend;
5980 }
5981 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5982 spin.si_region_name[i * 2 + 1] =
5983 TOLOWER_ASC(innames[i][len - 1]);
5984 }
5985 }
5986 spin.si_region_count = incount;
5987
5988 spin.si_foldroot = wordtree_alloc(&spin);
5989 spin.si_keeproot = wordtree_alloc(&spin);
5990 spin.si_prefroot = wordtree_alloc(&spin);
5991 if (spin.si_foldroot == NULL
5992 || spin.si_keeproot == NULL
5993 || spin.si_prefroot == NULL)
5994 {
5995 free_blocks(spin.si_blocks);
5996 goto theend;
5997 }
5998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005999 // When not producing a .add.spl file clear the character table when
6000 // we encounter one in the .aff file. This means we dump the current
6001 // one in the .spl file if the .aff file doesn't define one. That's
6002 // better than guessing the contents, the table will match a
6003 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006004 if (!spin.si_add)
6005 spin.si_clear_chartab = TRUE;
6006
6007 /*
6008 * Read all the .aff and .dic files.
6009 * Text is converted to 'encoding'.
6010 * Words are stored in the case-folded and keep-case trees.
6011 */
6012 for (i = 0; i < incount && !error; ++i)
6013 {
6014 spin.si_conv.vc_type = CONV_NONE;
6015 spin.si_region = 1 << i;
6016
6017 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6018 if (mch_stat((char *)fname, &st) >= 0)
6019 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006020 // Read the .aff file. Will init "spin->si_conv" based on the
6021 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006022 afile[i] = spell_read_aff(&spin, fname);
6023 if (afile[i] == NULL)
6024 error = TRUE;
6025 else
6026 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006027 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006028 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6029 innames[i]);
6030 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6031 error = TRUE;
6032 }
6033 }
6034 else
6035 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006036 // No .aff file, try reading the file as a word list. Store
6037 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006038 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6039 error = TRUE;
6040 }
6041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006042 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006043 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006044 }
6045
6046 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006047 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006048
6049 if (!error && !got_int)
6050 {
6051 /*
6052 * Combine tails in the tree.
6053 */
6054 spell_message(&spin, (char_u *)_(msg_compressing));
6055 wordtree_compress(&spin, spin.si_foldroot);
6056 wordtree_compress(&spin, spin.si_keeproot);
6057 wordtree_compress(&spin, spin.si_prefroot);
6058 }
6059
6060 if (!error && !got_int)
6061 {
6062 /*
6063 * Write the info in the spell file.
6064 */
6065 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006066 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006067 spell_message(&spin, IObuff);
6068
6069 error = write_vim_spell(&spin, wfname) == FAIL;
6070
6071 spell_message(&spin, (char_u *)_("Done!"));
6072 vim_snprintf((char *)IObuff, IOSIZE,
6073 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6074 spell_message(&spin, IObuff);
6075
6076 /*
6077 * If the file is loaded need to reload it.
6078 */
6079 if (!error)
6080 spell_reload_one(wfname, added_word);
6081 }
6082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006083 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006084 ga_clear(&spin.si_rep);
6085 ga_clear(&spin.si_repsal);
6086 ga_clear(&spin.si_sal);
6087 ga_clear(&spin.si_map);
6088 ga_clear(&spin.si_comppat);
6089 ga_clear(&spin.si_prefcond);
6090 hash_clear_all(&spin.si_commonwords, 0);
6091
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006092 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006093 for (i = 0; i < incount; ++i)
6094 if (afile[i] != NULL)
6095 spell_free_aff(afile[i]);
6096
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006097 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006098 free_blocks(spin.si_blocks);
6099
6100 /*
6101 * If there is soundfolding info and no NOSUGFILE item create the
6102 * .sug file with the soundfolded word trie.
6103 */
6104 if (spin.si_sugtime != 0 && !error && !got_int)
6105 spell_make_sugfile(&spin, wfname);
6106
6107 }
6108
6109theend:
6110 vim_free(fname);
6111 vim_free(wfname);
6112}
6113
6114/*
6115 * Display a message for spell file processing when 'verbose' is set or using
6116 * ":mkspell". "str" can be IObuff.
6117 */
6118 static void
6119spell_message(spellinfo_T *spin, char_u *str)
6120{
6121 if (spin->si_verbose || p_verbose > 2)
6122 {
6123 if (!spin->si_verbose)
6124 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006125 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006126 out_flush();
6127 if (!spin->si_verbose)
6128 verbose_leave();
6129 }
6130}
6131
6132/*
6133 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006134 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006135 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006136 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006137 */
6138 void
6139ex_spell(exarg_T *eap)
6140{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006141 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6142 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6143 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006144 eap->forceit ? 0 : (int)eap->line2,
6145 eap->cmdidx == CMD_spellundo);
6146}
6147
6148/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006149 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006150 */
6151 void
6152spell_add_word(
6153 char_u *word,
6154 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006155 int what, // SPELL_ADD_ values
6156 int idx, // "zG" and "zW": zero, otherwise index in
6157 // 'spellfile'
6158 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006159{
6160 FILE *fd = NULL;
6161 buf_T *buf = NULL;
6162 int new_spf = FALSE;
6163 char_u *fname;
6164 char_u *fnamebuf = NULL;
6165 char_u line[MAXWLEN * 2];
6166 long fpos, fpos_next = 0;
6167 int i;
6168 char_u *spf;
6169
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006170 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006171 {
6172 if (int_wordlist == NULL)
6173 {
6174 int_wordlist = vim_tempname('s', FALSE);
6175 if (int_wordlist == NULL)
6176 return;
6177 }
6178 fname = int_wordlist;
6179 }
6180 else
6181 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006182 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006183 if (*curwin->w_s->b_p_spf == NUL)
6184 {
6185 init_spellfile();
6186 new_spf = TRUE;
6187 }
6188
6189 if (*curwin->w_s->b_p_spf == NUL)
6190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006191 semsg(_(e_notset), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006192 return;
6193 }
6194 fnamebuf = alloc(MAXPATHL);
6195 if (fnamebuf == NULL)
6196 return;
6197
6198 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6199 {
6200 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6201 if (i == idx)
6202 break;
6203 if (*spf == NUL)
6204 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006205 semsg(_("E765: 'spellfile' does not have %d entries"), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006206 vim_free(fnamebuf);
6207 return;
6208 }
6209 }
6210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006211 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006212 buf = buflist_findname_exp(fnamebuf);
6213 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6214 buf = NULL;
6215 if (buf != NULL && bufIsChanged(buf))
6216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006217 emsg(_(e_bufloaded));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006218 vim_free(fnamebuf);
6219 return;
6220 }
6221
6222 fname = fnamebuf;
6223 }
6224
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006225 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006226 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006227 // When the word appears as good word we need to remove that one,
6228 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006229 fd = mch_fopen((char *)fname, "r");
6230 if (fd != NULL)
6231 {
6232 while (!vim_fgets(line, MAXWLEN * 2, fd))
6233 {
6234 fpos = fpos_next;
6235 fpos_next = ftell(fd);
6236 if (STRNCMP(word, line, len) == 0
6237 && (line[len] == '/' || line[len] < ' '))
6238 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006239 // Found duplicate word. Remove it by writing a '#' at
6240 // the start of the line. Mixing reading and writing
6241 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006242 fclose(fd);
6243 fd = mch_fopen((char *)fname, "r+");
6244 if (fd == NULL)
6245 break;
6246 if (fseek(fd, fpos, SEEK_SET) == 0)
6247 {
6248 fputc('#', fd);
6249 if (undo)
6250 {
6251 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006252 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006253 len, word, NameBuff);
6254 }
6255 }
6256 fseek(fd, fpos_next, SEEK_SET);
6257 }
6258 }
6259 if (fd != NULL)
6260 fclose(fd);
6261 }
6262 }
6263
6264 if (!undo)
6265 {
6266 fd = mch_fopen((char *)fname, "a");
6267 if (fd == NULL && new_spf)
6268 {
6269 char_u *p;
6270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006271 // We just initialized the 'spellfile' option and can't open the
6272 // file. We may need to create the "spell" directory first. We
6273 // already checked the runtime directory is writable in
6274 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006275 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6276 {
6277 int c = *p;
6278
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006279 // The directory doesn't exist. Try creating it and opening
6280 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006281 *p = NUL;
6282 vim_mkdir(fname, 0755);
6283 *p = c;
6284 fd = mch_fopen((char *)fname, "a");
6285 }
6286 }
6287
6288 if (fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006289 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006290 else
6291 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006292 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006293 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006294 else if (what == SPELL_ADD_RARE)
6295 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006296 else
6297 fprintf(fd, "%.*s\n", len, word);
6298 fclose(fd);
6299
6300 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006301 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006302 }
6303 }
6304
6305 if (fd != NULL)
6306 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006307 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006308 mkspell(1, &fname, FALSE, TRUE, TRUE);
6309
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006310 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006311 if (buf != NULL)
6312 buf_reload(buf, buf->b_orig_mode);
6313
6314 redraw_all_later(SOME_VALID);
6315 }
6316 vim_free(fnamebuf);
6317}
6318
6319/*
6320 * Initialize 'spellfile' for the current buffer.
6321 */
6322 static void
6323init_spellfile(void)
6324{
6325 char_u *buf;
6326 int l;
6327 char_u *fname;
6328 char_u *rtp;
6329 char_u *lend;
6330 int aspath = FALSE;
6331 char_u *lstart = curbuf->b_s.b_p_spl;
6332
6333 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6334 {
6335 buf = alloc(MAXPATHL);
6336 if (buf == NULL)
6337 return;
6338
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006339 // Find the end of the language name. Exclude the region. If there
6340 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006341 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6342 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6343 if (vim_ispathsep(*lend))
6344 {
6345 aspath = TRUE;
6346 lstart = lend + 1;
6347 }
6348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006349 // Loop over all entries in 'runtimepath'. Use the first one where we
6350 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006351 rtp = p_rtp;
6352 while (*rtp != NUL)
6353 {
6354 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006355 // Use directory of an entry with path, e.g., for
6356 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006357 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6358 lstart - curbuf->b_s.b_p_spl - 1);
6359 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006360 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006361 copy_option_part(&rtp, buf, MAXPATHL, ",");
6362 if (filewritable(buf) == 2)
6363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006364 // Use the first language name from 'spelllang' and the
6365 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006366 if (aspath)
6367 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6368 lend - curbuf->b_s.b_p_spl);
6369 else
6370 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006371 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006372 l = (int)STRLEN(buf);
6373 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6374 if (filewritable(buf) != 2)
6375 vim_mkdir(buf, 0755);
6376
6377 l = (int)STRLEN(buf);
6378 vim_snprintf((char *)buf + l, MAXPATHL - l,
6379 "/%.*s", (int)(lend - lstart), lstart);
6380 }
6381 l = (int)STRLEN(buf);
6382 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6383 ->lp_slang->sl_fname;
6384 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6385 fname != NULL
6386 && strstr((char *)gettail(fname), ".ascii.") != NULL
6387 ? (char_u *)"ascii" : spell_enc());
6388 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6389 break;
6390 }
6391 aspath = FALSE;
6392 }
6393
6394 vim_free(buf);
6395 }
6396}
6397
6398
6399
6400/*
6401 * Set the spell character tables from strings in the affix file.
6402 */
6403 static int
6404set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6405{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006406 // We build the new tables here first, so that we can compare with the
6407 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006408 spelltab_T new_st;
6409 char_u *pf = fol, *pl = low, *pu = upp;
6410 int f, l, u;
6411
6412 clear_spell_chartab(&new_st);
6413
6414 while (*pf != NUL)
6415 {
6416 if (*pl == NUL || *pu == NUL)
6417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006418 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006419 return FAIL;
6420 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006421 f = mb_ptr2char_adv(&pf);
6422 l = mb_ptr2char_adv(&pl);
6423 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006424
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006425 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006426 if (f < 256)
6427 new_st.st_isw[f] = TRUE;
6428 if (l < 256)
6429 new_st.st_isw[l] = TRUE;
6430 if (u < 256)
6431 new_st.st_isw[u] = TRUE;
6432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006433 // if "LOW" and "FOL" are not the same the "LOW" char needs
6434 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006435 if (l < 256 && l != f)
6436 {
6437 if (f >= 256)
6438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006439 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006440 return FAIL;
6441 }
6442 new_st.st_fold[l] = f;
6443 }
6444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006445 // if "UPP" and "FOL" are not the same the "UPP" char needs
6446 // case-folding, it's upper case and the "UPP" is the upper case of
6447 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006448 if (u < 256 && u != f)
6449 {
6450 if (f >= 256)
6451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006452 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006453 return FAIL;
6454 }
6455 new_st.st_fold[u] = f;
6456 new_st.st_isu[u] = TRUE;
6457 new_st.st_upper[f] = u;
6458 }
6459 }
6460
6461 if (*pl != NUL || *pu != NUL)
6462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006463 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006464 return FAIL;
6465 }
6466
6467 return set_spell_finish(&new_st);
6468}
6469
6470/*
6471 * Set the spell character tables from strings in the .spl file.
6472 */
6473 static void
6474set_spell_charflags(
6475 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006476 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006477 char_u *fol)
6478{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006479 // We build the new tables here first, so that we can compare with the
6480 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006481 spelltab_T new_st;
6482 int i;
6483 char_u *p = fol;
6484 int c;
6485
6486 clear_spell_chartab(&new_st);
6487
6488 for (i = 0; i < 128; ++i)
6489 {
6490 if (i < cnt)
6491 {
6492 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6493 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6494 }
6495
6496 if (*p != NUL)
6497 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006498 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006499 new_st.st_fold[i + 128] = c;
6500 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6501 new_st.st_upper[c] = i + 128;
6502 }
6503 }
6504
6505 (void)set_spell_finish(&new_st);
6506}
6507
6508 static int
6509set_spell_finish(spelltab_T *new_st)
6510{
6511 int i;
6512
6513 if (did_set_spelltab)
6514 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006515 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006516 for (i = 0; i < 256; ++i)
6517 {
6518 if (spelltab.st_isw[i] != new_st->st_isw[i]
6519 || spelltab.st_isu[i] != new_st->st_isu[i]
6520 || spelltab.st_fold[i] != new_st->st_fold[i]
6521 || spelltab.st_upper[i] != new_st->st_upper[i])
6522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006523 emsg(_("E763: Word characters differ between spell files"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006524 return FAIL;
6525 }
6526 }
6527 }
6528 else
6529 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006530 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006531 spelltab = *new_st;
6532 did_set_spelltab = TRUE;
6533 }
6534
6535 return OK;
6536}
6537
6538/*
6539 * Write the table with prefix conditions to the .spl file.
6540 * When "fd" is NULL only count the length of what is written.
6541 */
6542 static int
6543write_spell_prefcond(FILE *fd, garray_T *gap)
6544{
6545 int i;
6546 char_u *p;
6547 int len;
6548 int totlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006549 size_t x = 1; // collect return value of fwrite()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006550
6551 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006552 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006554 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006555
6556 for (i = 0; i < gap->ga_len; ++i)
6557 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006558 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006559 p = ((char_u **)gap->ga_data)[i];
6560 if (p != NULL)
6561 {
6562 len = (int)STRLEN(p);
6563 if (fd != NULL)
6564 {
6565 fputc(len, fd);
6566 x &= fwrite(p, (size_t)len, (size_t)1, fd);
6567 }
6568 totlen += len;
6569 }
6570 else if (fd != NULL)
6571 fputc(0, fd);
6572 }
6573
6574 return totlen;
6575}
6576
6577
6578/*
6579 * Use map string "map" for languages "lp".
6580 */
6581 static void
6582set_map_str(slang_T *lp, char_u *map)
6583{
6584 char_u *p;
6585 int headc = 0;
6586 int c;
6587 int i;
6588
6589 if (*map == NUL)
6590 {
6591 lp->sl_has_map = FALSE;
6592 return;
6593 }
6594 lp->sl_has_map = TRUE;
6595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006596 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006597 for (i = 0; i < 256; ++i)
6598 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006599 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006600
6601 /*
6602 * The similar characters are stored separated with slashes:
6603 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6604 * before the same slash. For characters above 255 sl_map_hash is used.
6605 */
6606 for (p = map; *p != NUL; )
6607 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006608 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006609 if (c == '/')
6610 headc = 0;
6611 else
6612 {
6613 if (headc == 0)
6614 headc = c;
6615
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006616 // Characters above 255 don't fit in sl_map_array[], put them in
6617 // the hash table. Each entry is the char, a NUL the headchar and
6618 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006619 if (c >= 256)
6620 {
6621 int cl = mb_char2len(c);
6622 int headcl = mb_char2len(headc);
6623 char_u *b;
6624 hash_T hash;
6625 hashitem_T *hi;
6626
Bram Moolenaar964b3742019-05-24 18:54:09 +02006627 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006628 if (b == NULL)
6629 return;
6630 mb_char2bytes(c, b);
6631 b[cl] = NUL;
6632 mb_char2bytes(headc, b + cl + 1);
6633 b[cl + 1 + headcl] = NUL;
6634 hash = hash_hash(b);
6635 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6636 if (HASHITEM_EMPTY(hi))
6637 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6638 else
6639 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006640 // This should have been checked when generating the .spl
6641 // file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006642 emsg(_("E783: duplicate char in MAP entry"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006643 vim_free(b);
6644 }
6645 }
6646 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006647 lp->sl_map_array[c] = headc;
6648 }
6649 }
6650}
6651
6652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006653#endif // FEAT_SPELL