blob: fc365e2a03a78d5043f6a9f7fec6efde1bcffad3 [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
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200299/*
300 * Loop through all the siblings of a node (including the node)
301 */
302#define FOR_ALL_NODE_SIBLINGS(node, np) \
303 for ((np) = (node); (np) != NULL; (np) = (np)->wn_sibling)
304
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200305static int set_spell_finish(spelltab_T *new_st);
306static int write_spell_prefcond(FILE *fd, garray_T *gap);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200307static int read_region_section(FILE *fd, slang_T *slang, int len);
308static int read_charflags_section(FILE *fd);
309static int read_prefcond_section(FILE *fd, slang_T *lp);
310static int read_rep_section(FILE *fd, garray_T *gap, short *first);
311static int read_sal_section(FILE *fd, slang_T *slang);
312static int read_words_section(FILE *fd, slang_T *lp, int len);
313static int read_sofo_section(FILE *fd, slang_T *slang);
314static int read_compound(FILE *fd, slang_T *slang, int len);
315static int set_sofo(slang_T *lp, char_u *from, char_u *to);
316static void set_sal_first(slang_T *lp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200317static int *mb_str2wide(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200318static int spell_read_tree(FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt);
319static 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 +0200320static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
321static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
322static void set_map_str(slang_T *lp, char_u *map);
323
324
325static char *e_spell_trunc = N_("E758: Truncated spell file");
326static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
327static char *e_affname = N_("Affix name too long in %s line %d: %s");
328static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
329static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
330static char *msg_compressing = N_("Compressing word tree...");
331
332/*
333 * Load one spell file and store the info into a slang_T.
334 *
335 * This is invoked in three ways:
336 * - From spell_load_cb() to load a spell file for the first time. "lang" is
337 * the language name, "old_lp" is NULL. Will allocate an slang_T.
338 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
339 * points to the existing slang_T.
340 * - Just after writing a .spl file; it's read back to produce the .sug file.
341 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
342 *
343 * Returns the slang_T the spell file was loaded into. NULL for error.
344 */
345 slang_T *
346spell_load_file(
347 char_u *fname,
348 char_u *lang,
349 slang_T *old_lp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100350 int silent) // no error if file doesn't exist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200351{
352 FILE *fd;
353 char_u buf[VIMSPELLMAGICL];
354 char_u *p;
355 int i;
356 int n;
357 int len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200358 slang_T *lp = NULL;
359 int c = 0;
360 int res;
Bram Moolenaarce6db022020-01-07 20:11:42 +0100361 int did_estack_push = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +0100362 ESTACK_CHECK_DECLARATION
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200363
364 fd = mch_fopen((char *)fname, "r");
365 if (fd == NULL)
366 {
367 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100368 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200369 else if (p_verbose > 2)
370 {
371 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100372 smsg((const char *)e_notopen, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200373 verbose_leave();
374 }
375 goto endFAIL;
376 }
377 if (p_verbose > 2)
378 {
379 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380 smsg(_("Reading spell file \"%s\""), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200381 verbose_leave();
382 }
383
384 if (old_lp == NULL)
385 {
386 lp = slang_alloc(lang);
387 if (lp == NULL)
388 goto endFAIL;
389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100390 // Remember the file name, used to reload the file when it's updated.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200391 lp->sl_fname = vim_strsave(fname);
392 if (lp->sl_fname == NULL)
393 goto endFAIL;
394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100395 // Check for .add.spl (_add.spl for VMS).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200396 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
397 }
398 else
399 lp = old_lp;
400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100401 // Set sourcing_name, so that error messages mention the file name.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100402 estack_push(ETYPE_SPELL, fname, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +0100403 ESTACK_CHECK_SETUP
Bram Moolenaarce6db022020-01-07 20:11:42 +0100404 did_estack_push = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200405
406 /*
407 * <HEADER>: <fileID>
408 */
409 for (i = 0; i < VIMSPELLMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100410 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200411 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100413 emsg(_("E757: This does not look like a spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200414 goto endFAIL;
415 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100416 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200417 if (c < VIMSPELLVERSION)
418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100419 emsg(_("E771: Old spell file, needs to be updated"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200420 goto endFAIL;
421 }
422 else if (c > VIMSPELLVERSION)
423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424 emsg(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200425 goto endFAIL;
426 }
427
428
429 /*
430 * <SECTIONS>: <section> ... <sectionend>
431 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
432 */
433 for (;;)
434 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100435 n = getc(fd); // <sectionID> or <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200436 if (n == SN_END)
437 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100438 c = getc(fd); // <sectionflags>
439 len = get4c(fd); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200440 if (len < 0)
441 goto truncerr;
442
443 res = 0;
444 switch (n)
445 {
446 case SN_INFO:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100447 lp->sl_info = read_string(fd, len); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200448 if (lp->sl_info == NULL)
449 goto endFAIL;
450 break;
451
452 case SN_REGION:
453 res = read_region_section(fd, lp, len);
454 break;
455
456 case SN_CHARFLAGS:
457 res = read_charflags_section(fd);
458 break;
459
460 case SN_MIDWORD:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100461 lp->sl_midword = read_string(fd, len); // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200462 if (lp->sl_midword == NULL)
463 goto endFAIL;
464 break;
465
466 case SN_PREFCOND:
467 res = read_prefcond_section(fd, lp);
468 break;
469
470 case SN_REP:
471 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
472 break;
473
474 case SN_REPSAL:
475 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
476 break;
477
478 case SN_SAL:
479 res = read_sal_section(fd, lp);
480 break;
481
482 case SN_SOFO:
483 res = read_sofo_section(fd, lp);
484 break;
485
486 case SN_MAP:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100487 p = read_string(fd, len); // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200488 if (p == NULL)
489 goto endFAIL;
490 set_map_str(lp, p);
491 vim_free(p);
492 break;
493
494 case SN_WORDS:
495 res = read_words_section(fd, lp, len);
496 break;
497
498 case SN_SUGFILE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100499 lp->sl_sugtime = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200500 break;
501
502 case SN_NOSPLITSUGS:
503 lp->sl_nosplitsugs = TRUE;
504 break;
505
506 case SN_NOCOMPOUNDSUGS:
507 lp->sl_nocompoundsugs = TRUE;
508 break;
509
510 case SN_COMPOUND:
511 res = read_compound(fd, lp, len);
512 break;
513
514 case SN_NOBREAK:
515 lp->sl_nobreak = TRUE;
516 break;
517
518 case SN_SYLLABLE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100519 lp->sl_syllable = read_string(fd, len); // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200520 if (lp->sl_syllable == NULL)
521 goto endFAIL;
522 if (init_syl_tab(lp) == FAIL)
523 goto endFAIL;
524 break;
525
526 default:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100527 // Unsupported section. When it's required give an error
528 // message. When it's not required skip the contents.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200529 if (c & SNF_REQUIRED)
530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_("E770: Unsupported section in spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200532 goto endFAIL;
533 }
534 while (--len >= 0)
535 if (getc(fd) < 0)
536 goto truncerr;
537 break;
538 }
539someerror:
540 if (res == SP_FORMERROR)
541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100542 emsg(_(e_format));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200543 goto endFAIL;
544 }
545 if (res == SP_TRUNCERROR)
546 {
547truncerr:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_(e_spell_trunc));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200549 goto endFAIL;
550 }
551 if (res == SP_OTHERERROR)
552 goto endFAIL;
553 }
554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100555 // <LWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200556 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
557 if (res != 0)
558 goto someerror;
559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // <KWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200561 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
562 if (res != 0)
563 goto someerror;
564
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100565 // <PREFIXTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200566 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
567 lp->sl_prefixcnt);
568 if (res != 0)
569 goto someerror;
570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100571 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200572 if (old_lp == NULL && lang != NULL)
573 {
574 lp->sl_next = first_lang;
575 first_lang = lp;
576 }
577
578 goto endOK;
579
580endFAIL:
581 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100582 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200583 *lang = NUL;
584 if (lp != NULL && old_lp == NULL)
585 slang_free(lp);
586 lp = NULL;
587
588endOK:
589 if (fd != NULL)
590 fclose(fd);
Bram Moolenaarce6db022020-01-07 20:11:42 +0100591 if (did_estack_push)
Bram Moolenaare31ee862020-01-07 20:59:34 +0100592 {
593 ESTACK_CHECK_NOW
Bram Moolenaarce6db022020-01-07 20:11:42 +0100594 estack_pop();
Bram Moolenaare31ee862020-01-07 20:59:34 +0100595 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200596
597 return lp;
598}
599
600/*
601 * Fill in the wordcount fields for a trie.
602 * Returns the total number of words.
603 */
604 static void
605tree_count_words(char_u *byts, idx_T *idxs)
606{
607 int depth;
608 idx_T arridx[MAXWLEN];
609 int curi[MAXWLEN];
610 int c;
611 idx_T n;
612 int wordcount[MAXWLEN];
613
614 arridx[0] = 0;
615 curi[0] = 1;
616 wordcount[0] = 0;
617 depth = 0;
618 while (depth >= 0 && !got_int)
619 {
620 if (curi[depth] > byts[arridx[depth]])
621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100622 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200623 idxs[arridx[depth]] = wordcount[depth];
624 if (depth > 0)
625 wordcount[depth - 1] += wordcount[depth];
626
627 --depth;
628 fast_breakcheck();
629 }
630 else
631 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100632 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200633 n = arridx[depth] + curi[depth];
634 ++curi[depth];
635
636 c = byts[n];
637 if (c == 0)
638 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100639 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200640 ++wordcount[depth];
641
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100642 // Skip over any other NUL bytes (same word with different
643 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200644 while (byts[n + 1] == 0)
645 {
646 ++n;
647 ++curi[depth];
648 }
649 }
650 else
651 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100652 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200653 ++depth;
654 arridx[depth] = idxs[n];
655 curi[depth] = 1;
656 wordcount[depth] = 0;
657 }
658 }
659 }
660}
661
662/*
663 * Load the .sug files for languages that have one and weren't loaded yet.
664 */
665 void
666suggest_load_files(void)
667{
668 langp_T *lp;
669 int lpi;
670 slang_T *slang;
671 char_u *dotp;
672 FILE *fd;
673 char_u buf[MAXWLEN];
674 int i;
675 time_t timestamp;
676 int wcount;
677 int wordnr;
678 garray_T ga;
679 int c;
680
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200682 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
683 {
684 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
685 slang = lp->lp_slang;
686 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100688 // Change ".spl" to ".sug" and open the file. When the file isn't
689 // found silently skip it. Do set "sl_sugloaded" so that we
690 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200691 slang->sl_sugloaded = TRUE;
692
693 dotp = vim_strrchr(slang->sl_fname, '.');
694 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
695 continue;
696 STRCPY(dotp, ".sug");
697 fd = mch_fopen((char *)slang->sl_fname, "r");
698 if (fd == NULL)
699 goto nextone;
700
701 /*
702 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
703 */
704 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100705 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200706 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100708 semsg(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200709 slang->sl_fname);
710 goto nextone;
711 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100712 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200713 if (c < VIMSUGVERSION)
714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 semsg(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200716 slang->sl_fname);
717 goto nextone;
718 }
719 else if (c > VIMSUGVERSION)
720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 semsg(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200722 slang->sl_fname);
723 goto nextone;
724 }
725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100726 // Check the timestamp, it must be exactly the same as the one in
727 // the .spl file. Otherwise the word numbers won't match.
728 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200729 if (timestamp != slang->sl_sugtime)
730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 semsg(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200732 slang->sl_fname);
733 goto nextone;
734 }
735
736 /*
737 * <SUGWORDTREE>: <wordtree>
738 * Read the trie with the soundfolded words.
739 */
740 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
741 FALSE, 0) != 0)
742 {
743someerror:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744 semsg(_("E782: error while reading .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200745 slang->sl_fname);
746 slang_clear_sug(slang);
747 goto nextone;
748 }
749
750 /*
751 * <SUGTABLE>: <sugwcount> <sugline> ...
752 *
753 * Read the table with word numbers. We use a file buffer for
754 * this, because it's so much like a file with lines. Makes it
755 * possible to swap the info and save on memory use.
756 */
757 slang->sl_sugbuf = open_spellbuf();
758 if (slang->sl_sugbuf == NULL)
759 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100760 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200761 wcount = get4c(fd);
762 if (wcount < 0)
763 goto someerror;
764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100765 // Read all the wordnr lists into the buffer, one NUL terminated
766 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200767 ga_init2(&ga, 1, 100);
768 for (wordnr = 0; wordnr < wcount; ++wordnr)
769 {
770 ga.ga_len = 0;
771 for (;;)
772 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100773 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200774 if (c < 0 || ga_grow(&ga, 1) == FAIL)
775 goto someerror;
776 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
777 if (c == NUL)
778 break;
779 }
780 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
781 ga.ga_data, ga.ga_len, TRUE) == FAIL)
782 goto someerror;
783 }
784 ga_clear(&ga);
785
786 /*
787 * Need to put word counts in the word tries, so that we can find
788 * a word by its number.
789 */
790 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
791 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
792
793nextone:
794 if (fd != NULL)
795 fclose(fd);
796 STRCPY(dotp, ".spl");
797 }
798 }
799}
800
801
802/*
803 * Read a length field from "fd" in "cnt_bytes" bytes.
804 * Allocate memory, read the string into it and add a NUL at the end.
805 * Returns NULL when the count is zero.
806 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
807 * otherwise.
808 */
809 static char_u *
810read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
811{
812 int cnt = 0;
813 int i;
814 char_u *str;
815
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100816 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200817 for (i = 0; i < cnt_bytes; ++i)
818 cnt = (cnt << 8) + getc(fd);
819 if (cnt < 0)
820 {
821 *cntp = SP_TRUNCERROR;
822 return NULL;
823 }
824 *cntp = cnt;
825 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100826 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200827
828 str = read_string(fd, cnt);
829 if (str == NULL)
830 *cntp = SP_OTHERERROR;
831 return str;
832}
833
834/*
835 * Read SN_REGION: <regionname> ...
836 * Return SP_*ERROR flags.
837 */
838 static int
839read_region_section(FILE *fd, slang_T *lp, int len)
840{
841 int i;
842
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100843 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200844 return SP_FORMERROR;
845 for (i = 0; i < len; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100846 lp->sl_regions[i] = getc(fd); // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200847 lp->sl_regions[len] = NUL;
848 return 0;
849}
850
851/*
852 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
853 * <folcharslen> <folchars>
854 * Return SP_*ERROR flags.
855 */
856 static int
857read_charflags_section(FILE *fd)
858{
859 char_u *flags;
860 char_u *fol;
861 int flagslen, follen;
862
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100863 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200864 flags = read_cnt_string(fd, 1, &flagslen);
865 if (flagslen < 0)
866 return flagslen;
867
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100868 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200869 fol = read_cnt_string(fd, 2, &follen);
870 if (follen < 0)
871 {
872 vim_free(flags);
873 return follen;
874 }
875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100876 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200877 if (flags != NULL && fol != NULL)
878 set_spell_charflags(flags, flagslen, fol);
879
880 vim_free(flags);
881 vim_free(fol);
882
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100883 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200884 if ((flags == NULL) != (fol == NULL))
885 return SP_FORMERROR;
886 return 0;
887}
888
889/*
890 * Read SN_PREFCOND section.
891 * Return SP_*ERROR flags.
892 */
893 static int
894read_prefcond_section(FILE *fd, slang_T *lp)
895{
896 int cnt;
897 int i;
898 int n;
899 char_u *p;
900 char_u buf[MAXWLEN + 1];
901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100902 // <prefcondcnt> <prefcond> ...
903 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200904 if (cnt <= 0)
905 return SP_FORMERROR;
906
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200907 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200908 if (lp->sl_prefprog == NULL)
909 return SP_OTHERERROR;
910 lp->sl_prefixcnt = cnt;
911
912 for (i = 0; i < cnt; ++i)
913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100914 // <prefcond> : <condlen> <condstr>
915 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200916 if (n < 0 || n >= MAXWLEN)
917 return SP_FORMERROR;
918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100919 // When <condlen> is zero we have an empty condition. Otherwise
920 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200921 if (n > 0)
922 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100923 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200924 p = buf + 1;
925 while (n-- > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100926 *p++ = getc(fd); // <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200927 *p = NUL;
928 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
929 }
930 }
931 return 0;
932}
933
934/*
935 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
936 * Return SP_*ERROR flags.
937 */
938 static int
939read_rep_section(FILE *fd, garray_T *gap, short *first)
940{
941 int cnt;
942 fromto_T *ftp;
943 int i;
944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100945 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200946 if (cnt < 0)
947 return SP_TRUNCERROR;
948
949 if (ga_grow(gap, cnt) == FAIL)
950 return SP_OTHERERROR;
951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100952 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200953 for (; gap->ga_len < cnt; ++gap->ga_len)
954 {
955 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
956 ftp->ft_from = read_cnt_string(fd, 1, &i);
957 if (i < 0)
958 return i;
959 if (i == 0)
960 return SP_FORMERROR;
961 ftp->ft_to = read_cnt_string(fd, 1, &i);
962 if (i <= 0)
963 {
964 vim_free(ftp->ft_from);
965 if (i < 0)
966 return i;
967 return SP_FORMERROR;
968 }
969 }
970
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100971 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200972 for (i = 0; i < 256; ++i)
973 first[i] = -1;
974 for (i = 0; i < gap->ga_len; ++i)
975 {
976 ftp = &((fromto_T *)gap->ga_data)[i];
977 if (first[*ftp->ft_from] == -1)
978 first[*ftp->ft_from] = i;
979 }
980 return 0;
981}
982
983/*
984 * Read SN_SAL section: <salflags> <salcount> <sal> ...
985 * Return SP_*ERROR flags.
986 */
987 static int
988read_sal_section(FILE *fd, slang_T *slang)
989{
990 int i;
991 int cnt;
992 garray_T *gap;
993 salitem_T *smp;
994 int ccnt;
995 char_u *p;
996 int c = NUL;
997
998 slang->sl_sofo = FALSE;
999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001000 i = getc(fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001001 if (i & SAL_F0LLOWUP)
1002 slang->sl_followup = TRUE;
1003 if (i & SAL_COLLAPSE)
1004 slang->sl_collapse = TRUE;
1005 if (i & SAL_REM_ACCENTS)
1006 slang->sl_rem_accents = TRUE;
1007
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001008 cnt = get2c(fd); // <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001009 if (cnt < 0)
1010 return SP_TRUNCERROR;
1011
1012 gap = &slang->sl_sal;
1013 ga_init2(gap, sizeof(salitem_T), 10);
1014 if (ga_grow(gap, cnt + 1) == FAIL)
1015 return SP_OTHERERROR;
1016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001017 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001018 for (; gap->ga_len < cnt; ++gap->ga_len)
1019 {
1020 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001021 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001022 if (ccnt < 0)
1023 return SP_TRUNCERROR;
1024 if ((p = alloc(ccnt + 2)) == NULL)
1025 return SP_OTHERERROR;
1026 smp->sm_lead = p;
1027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001028 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001029 for (i = 0; i < ccnt; ++i)
1030 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001031 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001032 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1033 break;
1034 *p++ = c;
1035 }
1036 smp->sm_leadlen = (int)(p - smp->sm_lead);
1037 *p++ = NUL;
1038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001039 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001040 if (c == '(')
1041 {
1042 smp->sm_oneof = p;
1043 for (++i; i < ccnt; ++i)
1044 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001045 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001046 if (c == ')')
1047 break;
1048 *p++ = c;
1049 }
1050 *p++ = NUL;
1051 if (++i < ccnt)
1052 c = getc(fd);
1053 }
1054 else
1055 smp->sm_oneof = NULL;
1056
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001057 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001058 smp->sm_rules = p;
1059 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001060 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001061 *p++ = c;
1062 for (++i; i < ccnt; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001063 *p++ = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001064 *p++ = NUL;
1065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001066 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001067 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1068 if (ccnt < 0)
1069 {
1070 vim_free(smp->sm_lead);
1071 return ccnt;
1072 }
1073
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001074 if (has_mbyte)
1075 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001076 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001077 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1078 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1079 if (smp->sm_oneof == NULL)
1080 smp->sm_oneof_w = NULL;
1081 else
1082 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1083 if (smp->sm_to == NULL)
1084 smp->sm_to_w = NULL;
1085 else
1086 smp->sm_to_w = mb_str2wide(smp->sm_to);
1087 if (smp->sm_lead_w == NULL
1088 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1089 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1090 {
1091 vim_free(smp->sm_lead);
1092 vim_free(smp->sm_to);
1093 vim_free(smp->sm_lead_w);
1094 vim_free(smp->sm_oneof_w);
1095 vim_free(smp->sm_to_w);
1096 return SP_OTHERERROR;
1097 }
1098 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001099 }
1100
1101 if (gap->ga_len > 0)
1102 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001103 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1104 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001105 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1106 if ((p = alloc(1)) == NULL)
1107 return SP_OTHERERROR;
1108 p[0] = NUL;
1109 smp->sm_lead = p;
1110 smp->sm_leadlen = 0;
1111 smp->sm_oneof = NULL;
1112 smp->sm_rules = p;
1113 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001114 if (has_mbyte)
1115 {
1116 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1117 smp->sm_leadlen = 0;
1118 smp->sm_oneof_w = NULL;
1119 smp->sm_to_w = NULL;
1120 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001121 ++gap->ga_len;
1122 }
1123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001124 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001125 set_sal_first(slang);
1126
1127 return 0;
1128}
1129
1130/*
1131 * Read SN_WORDS: <word> ...
1132 * Return SP_*ERROR flags.
1133 */
1134 static int
1135read_words_section(FILE *fd, slang_T *lp, int len)
1136{
1137 int done = 0;
1138 int i;
1139 int c;
1140 char_u word[MAXWLEN];
1141
1142 while (done < len)
1143 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001144 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001145 for (i = 0; ; ++i)
1146 {
1147 c = getc(fd);
1148 if (c == EOF)
1149 return SP_TRUNCERROR;
1150 word[i] = c;
1151 if (word[i] == NUL)
1152 break;
1153 if (i == MAXWLEN - 1)
1154 return SP_FORMERROR;
1155 }
1156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001157 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001158 count_common_word(lp, word, -1, 10);
1159 done += i + 1;
1160 }
1161 return 0;
1162}
1163
1164/*
1165 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1166 * Return SP_*ERROR flags.
1167 */
1168 static int
1169read_sofo_section(FILE *fd, slang_T *slang)
1170{
1171 int cnt;
1172 char_u *from, *to;
1173 int res;
1174
1175 slang->sl_sofo = TRUE;
1176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001177 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001178 from = read_cnt_string(fd, 2, &cnt);
1179 if (cnt < 0)
1180 return cnt;
1181
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001182 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001183 to = read_cnt_string(fd, 2, &cnt);
1184 if (cnt < 0)
1185 {
1186 vim_free(from);
1187 return cnt;
1188 }
1189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001190 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001191 if (from != NULL && to != NULL)
1192 res = set_sofo(slang, from, to);
1193 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001194 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001195 else
1196 res = 0;
1197
1198 vim_free(from);
1199 vim_free(to);
1200 return res;
1201}
1202
1203/*
1204 * Read the compound section from the .spl file:
1205 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1206 * Returns SP_*ERROR flags.
1207 */
1208 static int
1209read_compound(FILE *fd, slang_T *slang, int len)
1210{
1211 int todo = len;
1212 int c;
1213 int atstart;
1214 char_u *pat;
1215 char_u *pp;
1216 char_u *cp;
1217 char_u *ap;
1218 char_u *crp;
1219 int cnt;
1220 garray_T *gap;
1221
1222 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001223 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001224
1225 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001226 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001227 if (c < 2)
1228 c = MAXWLEN;
1229 slang->sl_compmax = c;
1230
1231 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001232 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001233 if (c < 1)
1234 c = 0;
1235 slang->sl_compminlen = c;
1236
1237 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001238 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001239 if (c < 1)
1240 c = MAXWLEN;
1241 slang->sl_compsylmax = c;
1242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001243 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001244 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001245 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001246 else
1247 {
1248 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001249 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001250 --todo;
1251 slang->sl_compoptions = c;
1252
1253 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001254 c = get2c(fd); // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001255 todo -= 2;
1256 ga_init2(gap, sizeof(char_u *), c);
1257 if (ga_grow(gap, c) == OK)
1258 while (--c >= 0)
1259 {
1260 ((char_u **)(gap->ga_data))[gap->ga_len++] =
1261 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001262 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001263 if (cnt < 0)
1264 return cnt;
1265 todo -= cnt + 1;
1266 }
1267 }
1268 if (todo < 0)
1269 return SP_FORMERROR;
1270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001271 // Turn the COMPOUNDRULE items into a regexp pattern:
1272 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1273 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1274 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001275 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001276 if (enc_utf8)
1277 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001278 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001279 if (pat == NULL)
1280 return SP_OTHERERROR;
1281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001282 // We also need a list of all flags that can appear at the start and one
1283 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001284 cp = alloc(todo + 1);
1285 if (cp == NULL)
1286 {
1287 vim_free(pat);
1288 return SP_OTHERERROR;
1289 }
1290 slang->sl_compstartflags = cp;
1291 *cp = NUL;
1292
1293 ap = alloc(todo + 1);
1294 if (ap == NULL)
1295 {
1296 vim_free(pat);
1297 return SP_OTHERERROR;
1298 }
1299 slang->sl_compallflags = ap;
1300 *ap = NUL;
1301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001302 // And a list of all patterns in their original form, for checking whether
1303 // compounding may work in match_compoundrule(). This is freed when we
1304 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001305 crp = alloc(todo + 1);
1306 slang->sl_comprules = crp;
1307
1308 pp = pat;
1309 *pp++ = '^';
1310 *pp++ = '\\';
1311 *pp++ = '(';
1312
1313 atstart = 1;
1314 while (todo-- > 0)
1315 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001316 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001317 if (c == EOF)
1318 {
1319 vim_free(pat);
1320 return SP_TRUNCERROR;
1321 }
1322
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001323 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001324 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1325 && !byte_in_str(slang->sl_compallflags, c))
1326 {
1327 *ap++ = c;
1328 *ap = NUL;
1329 }
1330
1331 if (atstart != 0)
1332 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001333 // At start of item: copy flags to "sl_compstartflags". For a
1334 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001335 if (c == '[')
1336 atstart = 2;
1337 else if (c == ']')
1338 atstart = 0;
1339 else
1340 {
1341 if (!byte_in_str(slang->sl_compstartflags, c))
1342 {
1343 *cp++ = c;
1344 *cp = NUL;
1345 }
1346 if (atstart == 1)
1347 atstart = 0;
1348 }
1349 }
1350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001351 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001352 if (crp != NULL)
1353 {
1354 if (c == '?' || c == '+' || c == '*')
1355 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001356 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001357 crp = NULL;
1358 }
1359 else
1360 *crp++ = c;
1361 }
1362
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001363 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001364 {
1365 *pp++ = '\\';
1366 *pp++ = '|';
1367 atstart = 1;
1368 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001369 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001370 {
1371 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001372 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001373 if (enc_utf8)
1374 pp += mb_char2bytes(c, pp);
1375 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001376 *pp++ = c;
1377 }
1378 }
1379
1380 *pp++ = '\\';
1381 *pp++ = ')';
1382 *pp++ = '$';
1383 *pp = NUL;
1384
1385 if (crp != NULL)
1386 *crp = NUL;
1387
1388 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1389 vim_free(pat);
1390 if (slang->sl_compprog == NULL)
1391 return SP_FORMERROR;
1392
1393 return 0;
1394}
1395
1396/*
1397 * Set the SOFOFROM and SOFOTO items in language "lp".
1398 * Returns SP_*ERROR flags when there is something wrong.
1399 */
1400 static int
1401set_sofo(slang_T *lp, char_u *from, char_u *to)
1402{
1403 int i;
1404
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001405 garray_T *gap;
1406 char_u *s;
1407 char_u *p;
1408 int c;
1409 int *inp;
1410
1411 if (has_mbyte)
1412 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001413 // Use "sl_sal" as an array with 256 pointers to a list of wide
1414 // characters. The index is the low byte of the character.
1415 // The list contains from-to pairs with a terminating NUL.
1416 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001417 gap = &lp->sl_sal;
1418 ga_init2(gap, sizeof(int *), 1);
1419 if (ga_grow(gap, 256) == FAIL)
1420 return SP_OTHERERROR;
1421 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1422 gap->ga_len = 256;
1423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001424 // First count the number of items for each list. Temporarily use
1425 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001426 for (p = from, s = to; *p != NUL && *s != NUL; )
1427 {
1428 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001429 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001430 if (c >= 256)
1431 ++lp->sl_sal_first[c & 0xff];
1432 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001434 return SP_FORMERROR;
1435
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001436 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001437 for (i = 0; i < 256; ++i)
1438 if (lp->sl_sal_first[i] > 0)
1439 {
1440 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1441 if (p == NULL)
1442 return SP_OTHERERROR;
1443 ((int **)gap->ga_data)[i] = (int *)p;
1444 *(int *)p = 0;
1445 }
1446
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001447 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1448 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001449 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1450 for (p = from, s = to; *p != NUL && *s != NUL; )
1451 {
1452 c = mb_cptr2char_adv(&p);
1453 i = mb_cptr2char_adv(&s);
1454 if (c >= 256)
1455 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001456 // Append the from-to chars at the end of the list with
1457 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001458 inp = ((int **)gap->ga_data)[c & 0xff];
1459 while (*inp != 0)
1460 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001461 *inp++ = c; // from char
1462 *inp++ = i; // to char
1463 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001464 }
1465 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001466 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001467 lp->sl_sal_first[c] = i;
1468 }
1469 }
1470 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001471 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001472 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001473 if (STRLEN(from) != STRLEN(to))
1474 return SP_FORMERROR;
1475
1476 for (i = 0; to[i] != NUL; ++i)
1477 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001478 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001479 }
1480
1481 return 0;
1482}
1483
1484/*
1485 * Fill the first-index table for "lp".
1486 */
1487 static void
1488set_sal_first(slang_T *lp)
1489{
1490 salfirst_T *sfirst;
1491 int i;
1492 salitem_T *smp;
1493 int c;
1494 garray_T *gap = &lp->sl_sal;
1495
1496 sfirst = lp->sl_sal_first;
1497 for (i = 0; i < 256; ++i)
1498 sfirst[i] = -1;
1499 smp = (salitem_T *)gap->ga_data;
1500 for (i = 0; i < gap->ga_len; ++i)
1501 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001502 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001503 // Use the lowest byte of the first character. For latin1 it's
1504 // the character, for other encodings it should differ for most
1505 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001506 c = *smp[i].sm_lead_w & 0xff;
1507 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001508 c = *smp[i].sm_lead;
1509 if (sfirst[c] == -1)
1510 {
1511 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001512 if (has_mbyte)
1513 {
1514 int n;
1515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001516 // Make sure all entries with this byte are following each
1517 // other. Move the ones that are in the wrong position. Do
1518 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001519 while (i + 1 < gap->ga_len
1520 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001521 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001522 ++i;
1523
1524 for (n = 1; i + n < gap->ga_len; ++n)
1525 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1526 {
1527 salitem_T tsal;
1528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001529 // Move entry with same index byte after the entries
1530 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001531 ++i;
1532 --n;
1533 tsal = smp[i + n];
1534 mch_memmove(smp + i + 1, smp + i,
1535 sizeof(salitem_T) * n);
1536 smp[i] = tsal;
1537 }
1538 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001539 }
1540 }
1541}
1542
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001543/*
1544 * Turn a multi-byte string into a wide character string.
1545 * Return it in allocated memory (NULL for out-of-memory)
1546 */
1547 static int *
1548mb_str2wide(char_u *s)
1549{
1550 int *res;
1551 char_u *p;
1552 int i = 0;
1553
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001554 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001555 if (res != NULL)
1556 {
1557 for (p = s; *p != NUL; )
1558 res[i++] = mb_ptr2char_adv(&p);
1559 res[i] = NUL;
1560 }
1561 return res;
1562}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001563
1564/*
1565 * Read a tree from the .spl or .sug file.
1566 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1567 * This is skipped when the tree has zero length.
1568 * Returns zero when OK, SP_ value for an error.
1569 */
1570 static int
1571spell_read_tree(
1572 FILE *fd,
1573 char_u **bytsp,
1574 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001575 int prefixtree, // TRUE for the prefix tree
1576 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001577{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001578 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001579 int idx;
1580 char_u *bp;
1581 idx_T *ip;
1582
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001583 // The tree size was computed when writing the file, so that we can
1584 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001585 len = get4c(fd);
1586 if (len < 0)
1587 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001588 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001589 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001590 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001591 if (len > 0)
1592 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001593 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001594 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001595 if (bp == NULL)
1596 return SP_OTHERERROR;
1597 *bytsp = bp;
1598
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001599 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001600 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001601 if (ip == NULL)
1602 return SP_OTHERERROR;
1603 *idxsp = ip;
1604
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001605 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001606 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1607 if (idx < 0)
1608 return idx;
1609 }
1610 return 0;
1611}
1612
1613/*
1614 * Read one row of siblings from the spell file and store it in the byte array
1615 * "byts" and index array "idxs". Recursively read the children.
1616 *
1617 * NOTE: The code here must match put_node()!
1618 *
1619 * Returns the index (>= 0) following the siblings.
1620 * Returns SP_TRUNCERROR if the file is shorter than expected.
1621 * Returns SP_FORMERROR if there is a format error.
1622 */
1623 static idx_T
1624read_tree_node(
1625 FILE *fd,
1626 char_u *byts,
1627 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001628 int maxidx, // size of arrays
1629 idx_T startidx, // current index in "byts" and "idxs"
1630 int prefixtree, // TRUE for reading PREFIXTREE
1631 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001632{
1633 int len;
1634 int i;
1635 int n;
1636 idx_T idx = startidx;
1637 int c;
1638 int c2;
1639#define SHARED_MASK 0x8000000
1640
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001641 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001642 if (len <= 0)
1643 return SP_TRUNCERROR;
1644
1645 if (startidx + len >= maxidx)
1646 return SP_FORMERROR;
1647 byts[idx++] = len;
1648
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001649 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001650 for (i = 1; i <= len; ++i)
1651 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001652 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001653 if (c < 0)
1654 return SP_TRUNCERROR;
1655 if (c <= BY_SPECIAL)
1656 {
1657 if (c == BY_NOFLAGS && !prefixtree)
1658 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001659 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001660 idxs[idx] = 0;
1661 c = 0;
1662 }
1663 else if (c != BY_INDEX)
1664 {
1665 if (prefixtree)
1666 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001667 // Read the optional pflags byte, the prefix ID and the
1668 // condition nr. In idxs[] store the prefix ID in the low
1669 // byte, the condition index shifted up 8 bits, the flags
1670 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001671 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001672 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001673 else
1674 c = 0;
1675
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001676 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001678 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001679 if (n >= maxprefcondnr)
1680 return SP_FORMERROR;
1681 c |= (n << 8);
1682 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001683 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001684 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001685 // Read flags and optional region and prefix ID. In
1686 // idxs[] the flags go in the low two bytes, region above
1687 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001688 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001690 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001691 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001692 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001693 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001694 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001695 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001696 }
1697
1698 idxs[idx] = c;
1699 c = 0;
1700 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001701 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001702 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001703 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001704 n = get3c(fd);
1705 if (n < 0 || n >= maxidx)
1706 return SP_FORMERROR;
1707 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001708 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001709 }
1710 }
1711 byts[idx++] = c;
1712 }
1713
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001714 // Recursively read the children for non-shared siblings.
1715 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1716 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001717 for (i = 1; i <= len; ++i)
1718 if (byts[startidx + i] != 0)
1719 {
1720 if (idxs[startidx + i] & SHARED_MASK)
1721 idxs[startidx + i] &= ~SHARED_MASK;
1722 else
1723 {
1724 idxs[startidx + i] = idx;
1725 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1726 prefixtree, maxprefcondnr);
1727 if (idx < 0)
1728 break;
1729 }
1730 }
1731
1732 return idx;
1733}
1734
1735/*
1736 * Reload the spell file "fname" if it's loaded.
1737 */
1738 static void
1739spell_reload_one(
1740 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001741 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001742{
1743 slang_T *slang;
1744 int didit = FALSE;
1745
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001746 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001747 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001748 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001749 {
1750 slang_clear(slang);
1751 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001752 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001753 slang_clear(slang);
1754 redraw_all_later(SOME_VALID);
1755 didit = TRUE;
1756 }
1757 }
1758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001759 // When "zg" was used and the file wasn't loaded yet, should redo
1760 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001761 if (added_word && !didit)
1762 did_set_spelllang(curwin);
1763}
1764
1765
1766/*
1767 * Functions for ":mkspell".
1768 */
1769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001770#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1771 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001772/*
1773 * Main structure to store the contents of a ".aff" file.
1774 */
1775typedef struct afffile_S
1776{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001777 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1778 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1779 unsigned af_rare; // RARE ID for rare word
1780 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1781 unsigned af_bad; // BAD ID for banned word
1782 unsigned af_needaffix; // NEEDAFFIX ID
1783 unsigned af_circumfix; // CIRCUMFIX ID
1784 unsigned af_needcomp; // NEEDCOMPOUND ID
1785 unsigned af_comproot; // COMPOUNDROOT ID
1786 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1787 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1788 unsigned af_nosuggest; // NOSUGGEST ID
1789 int af_pfxpostpone; // postpone prefixes without chop string and
1790 // without flags
1791 int af_ignoreextra; // IGNOREEXTRA present
1792 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1793 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1794 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001795} afffile_T;
1796
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001797#define AFT_CHAR 0 // flags are one character
1798#define AFT_LONG 1 // flags are two characters
1799#define AFT_CAPLONG 2 // flags are one or two characters
1800#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001801
1802typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001803// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001804struct affentry_S
1805{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001806 affentry_T *ae_next; // next affix with same name/number
1807 char_u *ae_chop; // text to chop off basic word (can be NULL)
1808 char_u *ae_add; // text to add to basic word (can be NULL)
1809 char_u *ae_flags; // flags on the affix (can be NULL)
1810 char_u *ae_cond; // condition (NULL for ".")
1811 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1812 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1813 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001814};
1815
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001816#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001819typedef struct affheader_S
1820{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001821 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1822 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1823 int ah_newID; // prefix ID after renumbering; 0 if not used
1824 int ah_combine; // suffix may combine with prefix
1825 int ah_follows; // another affix block should be following
1826 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001827} affheader_T;
1828
1829#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1830
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001831// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001832typedef struct compitem_S
1833{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001834 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1835 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1836 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001837} compitem_T;
1838
1839#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1840
1841/*
1842 * Structure that is used to store the items in the word tree. This avoids
1843 * the need to keep track of each allocated thing, everything is freed all at
1844 * once after ":mkspell" is done.
1845 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1846 * "sb_data" is correct for systems where pointers must be aligned on
1847 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1848 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001849#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001850typedef struct sblock_S sblock_T;
1851struct sblock_S
1852{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001853 int sb_used; // nr of bytes already in use
1854 sblock_T *sb_next; // next block in list
1855 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001856};
1857
1858/*
1859 * A node in the tree.
1860 */
1861typedef struct wordnode_S wordnode_T;
1862struct wordnode_S
1863{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001864 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001865 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001866 char_u hashkey[6]; // the hash key, only used while compressing
1867 int index; // index in written nodes (valid after first
1868 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001869 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001870 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001871 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001872 wordnode_T *next; // next node with same hash key
1873 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001874 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001875 wordnode_T *wn_child; // child (next byte in word)
1876 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1877 // always sorted)
1878 int wn_refs; // Nr. of references to this node. Only
1879 // relevant for first node in a list of
1880 // siblings, in following siblings it is
1881 // always one.
1882 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001883
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001884 // Info for when "wn_byte" is NUL.
1885 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1886 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1887 // "wn_region" the LSW of the wordnr.
1888 char_u wn_affixID; // supported/required prefix ID or 0
1889 short_u wn_flags; // WF_ flags
1890 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001891
1892#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001893 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001894#endif
1895};
1896
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001897#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001898
1899#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1900
1901/*
1902 * Info used while reading the spell files.
1903 */
1904typedef struct spellinfo_S
1905{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001906 wordnode_T *si_foldroot; // tree with case-folded words
1907 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001909 wordnode_T *si_keeproot; // tree with keep-case words
1910 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001911
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001912 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001914 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001916 sblock_T *si_blocks; // memory blocks used
1917 long si_blocks_cnt; // memory blocks allocated
1918 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001920 long si_compress_cnt; // words to add before lowering
1921 // compression limit
1922 wordnode_T *si_first_free; // List of nodes that have been freed during
1923 // compression, linked by "wn_child" field.
1924 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001925#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001926 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001927#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001930 int si_ascii; // handling only ASCII words
1931 int si_add; // addition file
1932 int si_clear_chartab; // when TRUE clear char tables
1933 int si_region; // region mask
1934 vimconv_T si_conv; // for conversion to 'encoding'
1935 int si_memtot; // runtime memory used
1936 int si_verbose; // verbose messages
1937 int si_msg_count; // number of words added since last message
1938 char_u *si_info; // info text chars or NULL
1939 int si_region_count; // number of regions supported (1 when there
1940 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001941 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001942 // region names; used only if
1943 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001945 garray_T si_rep; // list of fromto_T entries from REP lines
1946 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1947 garray_T si_sal; // list of fromto_T entries from SAL lines
1948 char_u *si_sofofr; // SOFOFROM text
1949 char_u *si_sofoto; // SOFOTO text
1950 int si_nosugfile; // NOSUGFILE item found
1951 int si_nosplitsugs; // NOSPLITSUGS item found
1952 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1953 int si_followup; // soundsalike: ?
1954 int si_collapse; // soundsalike: ?
1955 hashtab_T si_commonwords; // hashtable for common words
1956 time_t si_sugtime; // timestamp for .sug file
1957 int si_rem_accents; // soundsalike: remove accents
1958 garray_T si_map; // MAP info concatenated
1959 char_u *si_midword; // MIDWORD chars or NULL
1960 int si_compmax; // max nr of words for compounding
1961 int si_compminlen; // minimal length for compounding
1962 int si_compsylmax; // max nr of syllables for compounding
1963 int si_compoptions; // COMP_ flags
1964 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1965 // a string
1966 char_u *si_compflags; // flags used for compounding
1967 char_u si_nobreak; // NOBREAK
1968 char_u *si_syllable; // syllable string
1969 garray_T si_prefcond; // table with conditions for postponed
1970 // prefixes, each stored as a string
1971 int si_newprefID; // current value for ah_newID
1972 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001973} spellinfo_T;
1974
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001975static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1976static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1977static int spell_info_item(char_u *s);
1978static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1979static unsigned get_affitem(int flagtype, char_u **pp);
1980static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1981static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001982static void aff_check_number(int spinval, int affval, char *name);
1983static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1984static int str_equal(char_u *s1, char_u *s2);
1985static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1986static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001987static int get_affix_flags(afffile_T *affile, char_u *afflist);
1988static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1989static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1990static 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 +02001991static void *getroom(spellinfo_T *spin, size_t len, int align);
1992static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001993static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
1994static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
1995static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001996static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
Bram Moolenaar408c23b2020-06-03 22:15:45 +02001997static void wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name);
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02001998static long node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, long *tot);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001999static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002000static void clear_node(wordnode_T *node);
2001static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002002static int sug_filltree(spellinfo_T *spin, slang_T *slang);
2003static int sug_maketable(spellinfo_T *spin);
2004static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
2005static int offset2bytes(int nr, char_u *buf);
2006static void sug_write(spellinfo_T *spin, char_u *fname);
2007static void spell_message(spellinfo_T *spin, char_u *str);
2008static void init_spellfile(void);
2009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002010// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
2011// but it must be negative to indicate the prefix tree to tree_add_word().
2012// Use a negative number with the lower 8 bits zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002013#define PFX_FLAGS -256
2014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002015// flags for "condit" argument of store_aff_word()
2016#define CONDIT_COMB 1 // affix must combine
2017#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2018#define CONDIT_SUF 4 // add a suffix for matching flags
2019#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002020
2021/*
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02002022 * Tunable parameters for when the tree is compressed. Filled from the
2023 * 'mkspellmem' option.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002024 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002025static long compress_start = 30000; // memory / SBLOCKSIZE
2026static long compress_inc = 100; // memory / SBLOCKSIZE
2027static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002028
2029/*
2030 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2031 * Sets "sps_flags".
2032 */
2033 int
2034spell_check_msm(void)
2035{
2036 char_u *p = p_msm;
2037 long start = 0;
2038 long incr = 0;
2039 long added = 0;
2040
2041 if (!VIM_ISDIGIT(*p))
2042 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002043 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002044 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2045 if (*p != ',')
2046 return FAIL;
2047 ++p;
2048 if (!VIM_ISDIGIT(*p))
2049 return FAIL;
2050 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2051 if (*p != ',')
2052 return FAIL;
2053 ++p;
2054 if (!VIM_ISDIGIT(*p))
2055 return FAIL;
2056 added = getdigits(&p) * 1024;
2057 if (*p != NUL)
2058 return FAIL;
2059
2060 if (start == 0 || incr == 0 || added == 0 || incr > start)
2061 return FAIL;
2062
2063 compress_start = start;
2064 compress_inc = incr;
2065 compress_added = added;
2066 return OK;
2067}
2068
2069#ifdef SPELL_PRINTTREE
2070/*
2071 * For debugging the tree code: print the current tree in a (more or less)
2072 * readable format, so that we can see what happens when adding a word and/or
2073 * compressing the tree.
2074 * Based on code from Olaf Seibert.
2075 */
2076#define PRINTLINESIZE 1000
2077#define PRINTWIDTH 6
2078
2079#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2080 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2081
2082static char line1[PRINTLINESIZE];
2083static char line2[PRINTLINESIZE];
2084static char line3[PRINTLINESIZE];
2085
2086 static void
2087spell_clear_flags(wordnode_T *node)
2088{
2089 wordnode_T *np;
2090
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002091 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002092 {
2093 np->wn_u1.index = FALSE;
2094 spell_clear_flags(np->wn_child);
2095 }
2096}
2097
2098 static void
2099spell_print_node(wordnode_T *node, int depth)
2100{
2101 if (node->wn_u1.index)
2102 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002103 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002104 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2105 PRINTSOME(line2, depth, " ", 0, 0);
2106 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002107 msg(line1);
2108 msg(line2);
2109 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002110 }
2111 else
2112 {
2113 node->wn_u1.index = TRUE;
2114
2115 if (node->wn_byte != NUL)
2116 {
2117 if (node->wn_child != NULL)
2118 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2119 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002120 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002121 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2122 }
2123 else
2124 PRINTSOME(line1, depth, " $ ", 0, 0);
2125
2126 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2127
2128 if (node->wn_sibling != NULL)
2129 PRINTSOME(line3, depth, " | ", 0, 0);
2130 else
2131 PRINTSOME(line3, depth, " ", 0, 0);
2132
2133 if (node->wn_byte == NUL)
2134 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002135 msg(line1);
2136 msg(line2);
2137 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002138 }
2139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002140 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002141 if (node->wn_byte != NUL && node->wn_child != NULL)
2142 spell_print_node(node->wn_child, depth + 1);
2143
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002144 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002145 if (node->wn_sibling != NULL)
2146 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002147 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002148 STRCPY(line1, line3);
2149 STRCPY(line2, line3);
2150 spell_print_node(node->wn_sibling, depth);
2151 }
2152 }
2153}
2154
2155 static void
2156spell_print_tree(wordnode_T *root)
2157{
2158 if (root != NULL)
2159 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002160 // Clear the "wn_u1.index" fields, used to remember what has been
2161 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002162 spell_clear_flags(root);
2163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002164 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002165 spell_print_node(root, 0);
2166 }
2167}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002168#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002169
2170/*
2171 * Read the affix file "fname".
2172 * Returns an afffile_T, NULL for complete failure.
2173 */
2174 static afffile_T *
2175spell_read_aff(spellinfo_T *spin, char_u *fname)
2176{
2177 FILE *fd;
2178 afffile_T *aff;
2179 char_u rline[MAXLINELEN];
2180 char_u *line;
2181 char_u *pc = NULL;
2182#define MAXITEMCNT 30
2183 char_u *(items[MAXITEMCNT]);
2184 int itemcnt;
2185 char_u *p;
2186 int lnum = 0;
2187 affheader_T *cur_aff = NULL;
2188 int did_postpone_prefix = FALSE;
2189 int aff_todo = 0;
2190 hashtab_T *tp;
2191 char_u *low = NULL;
2192 char_u *fol = NULL;
2193 char_u *upp = NULL;
2194 int do_rep;
2195 int do_repsal;
2196 int do_sal;
2197 int do_mapline;
2198 int found_map = FALSE;
2199 hashitem_T *hi;
2200 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002201 int compminlen = 0; // COMPOUNDMIN value
2202 int compsylmax = 0; // COMPOUNDSYLMAX value
2203 int compoptions = 0; // COMP_ flags
2204 int compmax = 0; // COMPOUNDWORDMAX value
2205 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2206 // concatenated
2207 char_u *midword = NULL; // MIDWORD value
2208 char_u *syllable = NULL; // SYLLABLE value
2209 char_u *sofofrom = NULL; // SOFOFROM value
2210 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002211
2212 /*
2213 * Open the file.
2214 */
2215 fd = mch_fopen((char *)fname, "r");
2216 if (fd == NULL)
2217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002219 return NULL;
2220 }
2221
Bram Moolenaarc1669272018-06-19 14:23:53 +02002222 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002223 spell_message(spin, IObuff);
2224
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002225 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002226 do_rep = spin->si_rep.ga_len == 0;
2227
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002228 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002229 do_repsal = spin->si_repsal.ga_len == 0;
2230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002231 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002232 do_sal = spin->si_sal.ga_len == 0;
2233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002234 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002235 do_mapline = spin->si_map.ga_len == 0;
2236
2237 /*
2238 * Allocate and init the afffile_T structure.
2239 */
2240 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2241 if (aff == NULL)
2242 {
2243 fclose(fd);
2244 return NULL;
2245 }
2246 hash_init(&aff->af_pref);
2247 hash_init(&aff->af_suff);
2248 hash_init(&aff->af_comp);
2249
2250 /*
2251 * Read all the lines in the file one by one.
2252 */
2253 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2254 {
2255 line_breakcheck();
2256 ++lnum;
2257
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002258 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002259 if (*rline == '#')
2260 continue;
2261
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002262 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002263 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002264 if (spin->si_conv.vc_type != CONV_NONE)
2265 {
2266 pc = string_convert(&spin->si_conv, rline, NULL);
2267 if (pc == NULL)
2268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002269 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002270 fname, lnum, rline);
2271 continue;
2272 }
2273 line = pc;
2274 }
2275 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002276 {
2277 pc = NULL;
2278 line = rline;
2279 }
2280
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002281 // Split the line up in white separated items. Put a NUL after each
2282 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002283 itemcnt = 0;
2284 for (p = line; ; )
2285 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002286 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002287 ++p;
2288 if (*p == NUL)
2289 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002290 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002291 break;
2292 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002294 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002296 ++p;
2297 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002298 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002299 ++p;
2300 if (*p == NUL)
2301 break;
2302 *p++ = NUL;
2303 }
2304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002305 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002306 if (itemcnt > 0)
2307 {
2308 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2309 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002310 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002311 aff->af_enc = enc_canonize(items[1]);
2312 if (aff->af_enc != NULL && !spin->si_ascii
2313 && convert_setup(&spin->si_conv, aff->af_enc,
2314 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002315 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002316 fname, aff->af_enc, p_enc);
2317 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002318 }
2319 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2320 && aff->af_flagtype == AFT_CHAR)
2321 {
2322 if (STRCMP(items[1], "long") == 0)
2323 aff->af_flagtype = AFT_LONG;
2324 else if (STRCMP(items[1], "num") == 0)
2325 aff->af_flagtype = AFT_NUM;
2326 else if (STRCMP(items[1], "caplong") == 0)
2327 aff->af_flagtype = AFT_CAPLONG;
2328 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002330 fname, lnum, items[1]);
2331 if (aff->af_rare != 0
2332 || aff->af_keepcase != 0
2333 || aff->af_bad != 0
2334 || aff->af_needaffix != 0
2335 || aff->af_circumfix != 0
2336 || aff->af_needcomp != 0
2337 || aff->af_comproot != 0
2338 || aff->af_nosuggest != 0
2339 || compflags != NULL
2340 || aff->af_suff.ht_used > 0
2341 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002342 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002343 fname, lnum, items[1]);
2344 }
2345 else if (spell_info_item(items[0]))
2346 {
2347 p = (char_u *)getroom(spin,
2348 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2349 + STRLEN(items[0])
2350 + STRLEN(items[1]) + 3, FALSE);
2351 if (p != NULL)
2352 {
2353 if (spin->si_info != NULL)
2354 {
2355 STRCPY(p, spin->si_info);
2356 STRCAT(p, "\n");
2357 }
2358 STRCAT(p, items[0]);
2359 STRCAT(p, " ");
2360 STRCAT(p, items[1]);
2361 spin->si_info = p;
2362 }
2363 }
2364 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2365 && midword == NULL)
2366 {
2367 midword = getroom_save(spin, items[1]);
2368 }
2369 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2370 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002371 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002372 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002373 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002374 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2375 || is_aff_rule(items, itemcnt, "RARE", 2))
2376 && aff->af_rare == 0)
2377 {
2378 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2379 fname, lnum);
2380 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002381 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002382 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2383 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2384 && aff->af_keepcase == 0)
2385 {
2386 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2387 fname, lnum);
2388 }
2389 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2390 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2391 && aff->af_bad == 0)
2392 {
2393 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2394 fname, lnum);
2395 }
2396 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2397 && aff->af_needaffix == 0)
2398 {
2399 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2400 fname, lnum);
2401 }
2402 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2403 && aff->af_circumfix == 0)
2404 {
2405 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2406 fname, lnum);
2407 }
2408 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2409 && aff->af_nosuggest == 0)
2410 {
2411 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2412 fname, lnum);
2413 }
2414 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2415 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2416 && aff->af_needcomp == 0)
2417 {
2418 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2419 fname, lnum);
2420 }
2421 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2422 && aff->af_comproot == 0)
2423 {
2424 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2425 fname, lnum);
2426 }
2427 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2428 && aff->af_compforbid == 0)
2429 {
2430 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2431 fname, lnum);
2432 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002433 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002434 fname, lnum);
2435 }
2436 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2437 && aff->af_comppermit == 0)
2438 {
2439 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2440 fname, lnum);
2441 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002442 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002443 fname, lnum);
2444 }
2445 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2446 && compflags == NULL)
2447 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002448 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2449 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002450 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2451 if (p != NULL)
2452 {
2453 STRCPY(p, items[1]);
2454 STRCAT(p, "+");
2455 compflags = p;
2456 }
2457 }
2458 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2459 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002460 // We don't use the count, but do check that it's a number and
2461 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002462 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002463 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002464 fname, lnum, items[1]);
2465 }
2466 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2467 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002468 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002469 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2470 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002471 // Concatenate this string to previously defined ones,
2472 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002473 l = (int)STRLEN(items[1]) + 1;
2474 if (compflags != NULL)
2475 l += (int)STRLEN(compflags) + 1;
2476 p = getroom(spin, l, FALSE);
2477 if (p != NULL)
2478 {
2479 if (compflags != NULL)
2480 {
2481 STRCPY(p, compflags);
2482 STRCAT(p, "/");
2483 }
2484 STRCAT(p, items[1]);
2485 compflags = p;
2486 }
2487 }
2488 }
2489 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2490 && compmax == 0)
2491 {
2492 compmax = atoi((char *)items[1]);
2493 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002494 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002495 fname, lnum, items[1]);
2496 }
2497 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2498 && compminlen == 0)
2499 {
2500 compminlen = atoi((char *)items[1]);
2501 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002502 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002503 fname, lnum, items[1]);
2504 }
2505 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2506 && compsylmax == 0)
2507 {
2508 compsylmax = atoi((char *)items[1]);
2509 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002510 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002511 fname, lnum, items[1]);
2512 }
2513 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2514 {
2515 compoptions |= COMP_CHECKDUP;
2516 }
2517 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2518 {
2519 compoptions |= COMP_CHECKREP;
2520 }
2521 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2522 {
2523 compoptions |= COMP_CHECKCASE;
2524 }
2525 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2526 {
2527 compoptions |= COMP_CHECKTRIPLE;
2528 }
2529 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2530 {
2531 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002532 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002533 fname, lnum, items[1]);
2534 }
2535 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2536 {
2537 garray_T *gap = &spin->si_comppat;
2538 int i;
2539
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002540 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002541 for (i = 0; i < gap->ga_len - 1; i += 2)
2542 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2543 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2544 items[2]) == 0)
2545 break;
2546 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2547 {
2548 ((char_u **)(gap->ga_data))[gap->ga_len++]
2549 = getroom_save(spin, items[1]);
2550 ((char_u **)(gap->ga_data))[gap->ga_len++]
2551 = getroom_save(spin, items[2]);
2552 }
2553 }
2554 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2555 && syllable == NULL)
2556 {
2557 syllable = getroom_save(spin, items[1]);
2558 }
2559 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2560 {
2561 spin->si_nobreak = TRUE;
2562 }
2563 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2564 {
2565 spin->si_nosplitsugs = TRUE;
2566 }
2567 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2568 {
2569 spin->si_nocompoundsugs = TRUE;
2570 }
2571 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2572 {
2573 spin->si_nosugfile = TRUE;
2574 }
2575 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2576 {
2577 aff->af_pfxpostpone = TRUE;
2578 }
2579 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2580 {
2581 aff->af_ignoreextra = TRUE;
2582 }
2583 else if ((STRCMP(items[0], "PFX") == 0
2584 || STRCMP(items[0], "SFX") == 0)
2585 && aff_todo == 0
2586 && itemcnt >= 4)
2587 {
2588 int lasti = 4;
2589 char_u key[AH_KEY_LEN];
2590
2591 if (*items[0] == 'P')
2592 tp = &aff->af_pref;
2593 else
2594 tp = &aff->af_suff;
2595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002596 // Myspell allows the same affix name to be used multiple
2597 // times. The affix files that do this have an undocumented
2598 // "S" flag on all but the last block, thus we check for that
2599 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002600 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2601 hi = hash_find(tp, key);
2602 if (!HASHITEM_EMPTY(hi))
2603 {
2604 cur_aff = HI2AH(hi);
2605 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002606 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002607 fname, lnum, items[1]);
2608 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002609 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002610 fname, lnum, items[1]);
2611 }
2612 else
2613 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002614 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002615 cur_aff = (affheader_T *)getroom(spin,
2616 sizeof(affheader_T), TRUE);
2617 if (cur_aff == NULL)
2618 break;
2619 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2620 fname, lnum);
2621 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2622 break;
2623 if (cur_aff->ah_flag == aff->af_bad
2624 || cur_aff->ah_flag == aff->af_rare
2625 || cur_aff->ah_flag == aff->af_keepcase
2626 || cur_aff->ah_flag == aff->af_needaffix
2627 || cur_aff->ah_flag == aff->af_circumfix
2628 || cur_aff->ah_flag == aff->af_nosuggest
2629 || cur_aff->ah_flag == aff->af_needcomp
2630 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002631 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002632 fname, lnum, items[1]);
2633 STRCPY(cur_aff->ah_key, items[1]);
2634 hash_add(tp, cur_aff->ah_key);
2635
2636 cur_aff->ah_combine = (*items[2] == 'Y');
2637 }
2638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002639 // Check for the "S" flag, which apparently means that another
2640 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002641 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2642 {
2643 ++lasti;
2644 cur_aff->ah_follows = TRUE;
2645 }
2646 else
2647 cur_aff->ah_follows = FALSE;
2648
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002649 // Myspell allows extra text after the item, but that might
2650 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002651 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002652 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002653
2654 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002655 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002656 fname, lnum, items[2]);
2657
2658 if (*items[0] == 'P' && aff->af_pfxpostpone)
2659 {
2660 if (cur_aff->ah_newID == 0)
2661 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002662 // Use a new number in the .spl file later, to be able
2663 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002664 check_renumber(spin);
2665 cur_aff->ah_newID = ++spin->si_newprefID;
2666
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002667 // We only really use ah_newID if the prefix is
2668 // postponed. We know that only after handling all
2669 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002670 did_postpone_prefix = FALSE;
2671 }
2672 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002673 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002674 did_postpone_prefix = TRUE;
2675 }
2676
2677 aff_todo = atoi((char *)items[3]);
2678 }
2679 else if ((STRCMP(items[0], "PFX") == 0
2680 || STRCMP(items[0], "SFX") == 0)
2681 && aff_todo > 0
2682 && STRCMP(cur_aff->ah_key, items[1]) == 0
2683 && itemcnt >= 5)
2684 {
2685 affentry_T *aff_entry;
2686 int upper = FALSE;
2687 int lasti = 5;
2688
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002689 // Myspell allows extra text after the item, but that might
2690 // mean mistakes go unnoticed. Require a comment-starter,
2691 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002692 if (itemcnt > lasti
2693 && !aff->af_ignoreextra
2694 && *items[lasti] != '#'
2695 && (STRCMP(items[lasti], "-") != 0
2696 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002697 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002699 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002700 --aff_todo;
2701 aff_entry = (affentry_T *)getroom(spin,
2702 sizeof(affentry_T), TRUE);
2703 if (aff_entry == NULL)
2704 break;
2705
2706 if (STRCMP(items[2], "0") != 0)
2707 aff_entry->ae_chop = getroom_save(spin, items[2]);
2708 if (STRCMP(items[3], "0") != 0)
2709 {
2710 aff_entry->ae_add = getroom_save(spin, items[3]);
2711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002712 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002713 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2714 if (aff_entry->ae_flags != NULL)
2715 {
2716 *aff_entry->ae_flags++ = NUL;
2717 aff_process_flags(aff, aff_entry);
2718 }
2719 }
2720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002721 // Don't use an affix entry with non-ASCII characters when
2722 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002723 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2724 || has_non_ascii(aff_entry->ae_add)))
2725 {
2726 aff_entry->ae_next = cur_aff->ah_first;
2727 cur_aff->ah_first = aff_entry;
2728
2729 if (STRCMP(items[4], ".") != 0)
2730 {
2731 char_u buf[MAXLINELEN];
2732
2733 aff_entry->ae_cond = getroom_save(spin, items[4]);
2734 if (*items[0] == 'P')
2735 sprintf((char *)buf, "^%s", items[4]);
2736 else
2737 sprintf((char *)buf, "%s$", items[4]);
2738 aff_entry->ae_prog = vim_regcomp(buf,
2739 RE_MAGIC + RE_STRING + RE_STRICT);
2740 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002741 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002742 fname, lnum, items[4]);
2743 }
2744
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002745 // For postponed prefixes we need an entry in si_prefcond
2746 // for the condition. Use an existing one if possible.
2747 // Can't be done for an affix with flags, ignoring
2748 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002749 if (*items[0] == 'P' && aff->af_pfxpostpone
2750 && aff_entry->ae_flags == NULL)
2751 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002752 // When the chop string is one lower-case letter and
2753 // the add string ends in the upper-case letter we set
2754 // the "upper" flag, clear "ae_chop" and remove the
2755 // letters from "ae_add". The condition must either
2756 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002757 if (aff_entry->ae_chop != NULL
2758 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002759 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002760 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002761 {
2762 int c, c_up;
2763
2764 c = PTR2CHAR(aff_entry->ae_chop);
2765 c_up = SPELL_TOUPPER(c);
2766 if (c_up != c
2767 && (aff_entry->ae_cond == NULL
2768 || PTR2CHAR(aff_entry->ae_cond) == c))
2769 {
2770 p = aff_entry->ae_add
2771 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002772 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002773 if (PTR2CHAR(p) == c_up)
2774 {
2775 upper = TRUE;
2776 aff_entry->ae_chop = NULL;
2777 *p = NUL;
2778
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002779 // The condition is matched with the
2780 // actual word, thus must check for the
2781 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002782 if (aff_entry->ae_cond != NULL)
2783 {
2784 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002785
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002786 if (has_mbyte)
2787 {
2788 onecap_copy(items[4], buf, TRUE);
2789 aff_entry->ae_cond = getroom_save(
2790 spin, buf);
2791 }
2792 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002793 *aff_entry->ae_cond = c_up;
2794 if (aff_entry->ae_cond != NULL)
2795 {
2796 sprintf((char *)buf, "^%s",
2797 aff_entry->ae_cond);
2798 vim_regfree(aff_entry->ae_prog);
2799 aff_entry->ae_prog = vim_regcomp(
2800 buf, RE_MAGIC + RE_STRING);
2801 }
2802 }
2803 }
2804 }
2805 }
2806
2807 if (aff_entry->ae_chop == NULL
2808 && aff_entry->ae_flags == NULL)
2809 {
2810 int idx;
2811 char_u **pp;
2812 int n;
2813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002814 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002815 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2816 --idx)
2817 {
2818 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2819 if (str_equal(p, aff_entry->ae_cond))
2820 break;
2821 }
2822 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2823 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002824 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002825 idx = spin->si_prefcond.ga_len++;
2826 pp = ((char_u **)spin->si_prefcond.ga_data)
2827 + idx;
2828 if (aff_entry->ae_cond == NULL)
2829 *pp = NULL;
2830 else
2831 *pp = getroom_save(spin,
2832 aff_entry->ae_cond);
2833 }
2834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002835 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002836 if (aff_entry->ae_add == NULL)
2837 p = (char_u *)"";
2838 else
2839 p = aff_entry->ae_add;
2840
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002841 // PFX_FLAGS is a negative number, so that
2842 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002843 n = PFX_FLAGS;
2844 if (!cur_aff->ah_combine)
2845 n |= WFP_NC;
2846 if (upper)
2847 n |= WFP_UP;
2848 if (aff_entry->ae_comppermit)
2849 n |= WFP_COMPPERMIT;
2850 if (aff_entry->ae_compforbid)
2851 n |= WFP_COMPFORBID;
2852 tree_add_word(spin, p, spin->si_prefroot, n,
2853 idx, cur_aff->ah_newID);
2854 did_postpone_prefix = TRUE;
2855 }
2856
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002857 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002858 if (aff_todo == 0 && !did_postpone_prefix)
2859 {
2860 --spin->si_newprefID;
2861 cur_aff->ah_newID = 0;
2862 }
2863 }
2864 }
2865 }
2866 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2867 {
2868 fol = vim_strsave(items[1]);
2869 }
2870 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2871 {
2872 low = vim_strsave(items[1]);
2873 }
2874 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2875 {
2876 upp = vim_strsave(items[1]);
2877 }
2878 else if (is_aff_rule(items, itemcnt, "REP", 2)
2879 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2880 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002881 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002882 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002883 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002884 fname, lnum);
2885 }
2886 else if ((STRCMP(items[0], "REP") == 0
2887 || STRCMP(items[0], "REPSAL") == 0)
2888 && itemcnt >= 3)
2889 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002890 // REP/REPSAL item
2891 // Myspell ignores extra arguments, we require it starts with
2892 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002893 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002894 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002895 if (items[0][3] == 'S' ? do_repsal : do_rep)
2896 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002897 // Replace underscore with space (can't include a space
2898 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002899 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002900 if (*p == '_')
2901 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002902 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002903 if (*p == '_')
2904 *p = ' ';
2905 add_fromto(spin, items[0][3] == 'S'
2906 ? &spin->si_repsal
2907 : &spin->si_rep, items[1], items[2]);
2908 }
2909 }
2910 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2911 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002912 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002913 if (!found_map)
2914 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002915 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002916 found_map = TRUE;
2917 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002918 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002919 fname, lnum);
2920 }
2921 else if (do_mapline)
2922 {
2923 int c;
2924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002926 for (p = items[1]; *p != NUL; )
2927 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002928 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002929 if ((spin->si_map.ga_len > 0
2930 && vim_strchr(spin->si_map.ga_data, c)
2931 != NULL)
2932 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002933 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002934 fname, lnum);
2935 }
2936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002937 // We simply concatenate all the MAP strings, separated by
2938 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002939 ga_concat(&spin->si_map, items[1]);
2940 ga_append(&spin->si_map, '/');
2941 }
2942 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002943 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002944 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2945 {
2946 if (do_sal)
2947 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002948 // SAL item (sounds-a-like)
2949 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002950 if (STRCMP(items[1], "followup") == 0)
2951 spin->si_followup = sal_to_bool(items[2]);
2952 else if (STRCMP(items[1], "collapse_result") == 0)
2953 spin->si_collapse = sal_to_bool(items[2]);
2954 else if (STRCMP(items[1], "remove_accents") == 0)
2955 spin->si_rem_accents = sal_to_bool(items[2]);
2956 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002957 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002958 add_fromto(spin, &spin->si_sal, items[1],
2959 STRCMP(items[2], "_") == 0 ? (char_u *)""
2960 : items[2]);
2961 }
2962 }
2963 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2964 && sofofrom == NULL)
2965 {
2966 sofofrom = getroom_save(spin, items[1]);
2967 }
2968 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2969 && sofoto == NULL)
2970 {
2971 sofoto = getroom_save(spin, items[1]);
2972 }
2973 else if (STRCMP(items[0], "COMMON") == 0)
2974 {
2975 int i;
2976
2977 for (i = 1; i < itemcnt; ++i)
2978 {
2979 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2980 items[i])))
2981 {
2982 p = vim_strsave(items[i]);
2983 if (p == NULL)
2984 break;
2985 hash_add(&spin->si_commonwords, p);
2986 }
2987 }
2988 }
2989 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002991 fname, lnum, items[0]);
2992 }
2993 }
2994
2995 if (fol != NULL || low != NULL || upp != NULL)
2996 {
2997 if (spin->si_clear_chartab)
2998 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002999 // Clear the char type tables, don't want to use any of the
3000 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003001 init_spell_chartab();
3002 spin->si_clear_chartab = FALSE;
3003 }
3004
3005 /*
3006 * Don't write a word table for an ASCII file, so that we don't check
3007 * for conflicts with a word table that matches 'encoding'.
3008 * Don't write one for utf-8 either, we use utf_*() and
3009 * mb_get_class(), the list of chars in the file will be incomplete.
3010 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003011 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003012 {
3013 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003015 else
3016 (void)set_spell_chartab(fol, low, upp);
3017 }
3018
3019 vim_free(fol);
3020 vim_free(low);
3021 vim_free(upp);
3022 }
3023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003024 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003025 if (compmax != 0)
3026 {
3027 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3028 spin->si_compmax = compmax;
3029 }
3030
3031 if (compminlen != 0)
3032 {
3033 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3034 spin->si_compminlen = compminlen;
3035 }
3036
3037 if (compsylmax != 0)
3038 {
3039 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003040 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003041 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3042 spin->si_compsylmax = compsylmax;
3043 }
3044
3045 if (compoptions != 0)
3046 {
3047 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3048 spin->si_compoptions |= compoptions;
3049 }
3050
3051 if (compflags != NULL)
3052 process_compflags(spin, aff, compflags);
3053
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003054 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003055 if (spin->si_newcompID < spin->si_newprefID)
3056 {
3057 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003058 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003059 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003060 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003061 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003062 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003063 }
3064
3065 if (syllable != NULL)
3066 {
3067 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3068 spin->si_syllable = syllable;
3069 }
3070
3071 if (sofofrom != NULL || sofoto != NULL)
3072 {
3073 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003074 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003075 sofofrom == NULL ? "FROM" : "TO", fname);
3076 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003077 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003078 else
3079 {
3080 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3081 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3082 spin->si_sofofr = sofofrom;
3083 spin->si_sofoto = sofoto;
3084 }
3085 }
3086
3087 if (midword != NULL)
3088 {
3089 aff_check_string(spin->si_midword, midword, "MIDWORD");
3090 spin->si_midword = midword;
3091 }
3092
3093 vim_free(pc);
3094 fclose(fd);
3095 return aff;
3096}
3097
3098/*
3099 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3100 * a comment is following after item "mincount".
3101 */
3102 static int
3103is_aff_rule(
3104 char_u **items,
3105 int itemcnt,
3106 char *rulename,
3107 int mincount)
3108{
3109 return (STRCMP(items[0], rulename) == 0
3110 && (itemcnt == mincount
3111 || (itemcnt > mincount && items[mincount][0] == '#')));
3112}
3113
3114/*
3115 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3116 * ae_flags to ae_comppermit and ae_compforbid.
3117 */
3118 static void
3119aff_process_flags(afffile_T *affile, affentry_T *entry)
3120{
3121 char_u *p;
3122 char_u *prevp;
3123 unsigned flag;
3124
3125 if (entry->ae_flags != NULL
3126 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3127 {
3128 for (p = entry->ae_flags; *p != NUL; )
3129 {
3130 prevp = p;
3131 flag = get_affitem(affile->af_flagtype, &p);
3132 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3133 {
3134 STRMOVE(prevp, p);
3135 p = prevp;
3136 if (flag == affile->af_comppermit)
3137 entry->ae_comppermit = TRUE;
3138 else
3139 entry->ae_compforbid = TRUE;
3140 }
3141 if (affile->af_flagtype == AFT_NUM && *p == ',')
3142 ++p;
3143 }
3144 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003145 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003146 }
3147}
3148
3149/*
3150 * Return TRUE if "s" is the name of an info item in the affix file.
3151 */
3152 static int
3153spell_info_item(char_u *s)
3154{
3155 return STRCMP(s, "NAME") == 0
3156 || STRCMP(s, "HOME") == 0
3157 || STRCMP(s, "VERSION") == 0
3158 || STRCMP(s, "AUTHOR") == 0
3159 || STRCMP(s, "EMAIL") == 0
3160 || STRCMP(s, "COPYRIGHT") == 0;
3161}
3162
3163/*
3164 * Turn an affix flag name into a number, according to the FLAG type.
3165 * returns zero for failure.
3166 */
3167 static unsigned
3168affitem2flag(
3169 int flagtype,
3170 char_u *item,
3171 char_u *fname,
3172 int lnum)
3173{
3174 unsigned res;
3175 char_u *p = item;
3176
3177 res = get_affitem(flagtype, &p);
3178 if (res == 0)
3179 {
3180 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003181 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003182 fname, lnum, item);
3183 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003184 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003185 fname, lnum, item);
3186 }
3187 if (*p != NUL)
3188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003189 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003190 return 0;
3191 }
3192
3193 return res;
3194}
3195
3196/*
3197 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003198 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003199 * Returns zero for an error, still advances the pointer then.
3200 */
3201 static unsigned
3202get_affitem(int flagtype, char_u **pp)
3203{
3204 int res;
3205
3206 if (flagtype == AFT_NUM)
3207 {
3208 if (!VIM_ISDIGIT(**pp))
3209 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003210 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003211 return 0;
3212 }
3213 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003214 if (res == 0)
3215 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003216 }
3217 else
3218 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003219 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003220 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3221 && res >= 'A' && res <= 'Z'))
3222 {
3223 if (**pp == NUL)
3224 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003225 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003226 }
3227 }
3228 return res;
3229}
3230
3231/*
3232 * Process the "compflags" string used in an affix file and append it to
3233 * spin->si_compflags.
3234 * The processing involves changing the affix names to ID numbers, so that
3235 * they fit in one byte.
3236 */
3237 static void
3238process_compflags(
3239 spellinfo_T *spin,
3240 afffile_T *aff,
3241 char_u *compflags)
3242{
3243 char_u *p;
3244 char_u *prevp;
3245 unsigned flag;
3246 compitem_T *ci;
3247 int id;
3248 int len;
3249 char_u *tp;
3250 char_u key[AH_KEY_LEN];
3251 hashitem_T *hi;
3252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003253 // Make room for the old and the new compflags, concatenated with a / in
3254 // between. Processing it makes it shorter, but we don't know by how
3255 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003256 len = (int)STRLEN(compflags) + 1;
3257 if (spin->si_compflags != NULL)
3258 len += (int)STRLEN(spin->si_compflags) + 1;
3259 p = getroom(spin, len, FALSE);
3260 if (p == NULL)
3261 return;
3262 if (spin->si_compflags != NULL)
3263 {
3264 STRCPY(p, spin->si_compflags);
3265 STRCAT(p, "/");
3266 }
3267 spin->si_compflags = p;
3268 tp = p + STRLEN(p);
3269
3270 for (p = compflags; *p != NUL; )
3271 {
3272 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003273 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003274 *tp++ = *p++;
3275 else
3276 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003277 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003278 prevp = p;
3279 flag = get_affitem(aff->af_flagtype, &p);
3280 if (flag != 0)
3281 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003282 // Find the flag in the hashtable. If it was used before, use
3283 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003284 vim_strncpy(key, prevp, p - prevp);
3285 hi = hash_find(&aff->af_comp, key);
3286 if (!HASHITEM_EMPTY(hi))
3287 id = HI2CI(hi)->ci_newID;
3288 else
3289 {
3290 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3291 if (ci == NULL)
3292 break;
3293 STRCPY(ci->ci_key, key);
3294 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003295 // Avoid using a flag ID that has a special meaning in a
3296 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003297 do
3298 {
3299 check_renumber(spin);
3300 id = spin->si_newcompID--;
3301 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3302 ci->ci_newID = id;
3303 hash_add(&aff->af_comp, ci->ci_key);
3304 }
3305 *tp++ = id;
3306 }
3307 if (aff->af_flagtype == AFT_NUM && *p == ',')
3308 ++p;
3309 }
3310 }
3311
3312 *tp = NUL;
3313}
3314
3315/*
3316 * Check that the new IDs for postponed affixes and compounding don't overrun
3317 * each other. We have almost 255 available, but start at 0-127 to avoid
3318 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3319 * When that is used up an error message is given.
3320 */
3321 static void
3322check_renumber(spellinfo_T *spin)
3323{
3324 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3325 {
3326 spin->si_newprefID = 127;
3327 spin->si_newcompID = 255;
3328 }
3329}
3330
3331/*
3332 * Return TRUE if flag "flag" appears in affix list "afflist".
3333 */
3334 static int
3335flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3336{
3337 char_u *p;
3338 unsigned n;
3339
3340 switch (flagtype)
3341 {
3342 case AFT_CHAR:
3343 return vim_strchr(afflist, flag) != NULL;
3344
3345 case AFT_CAPLONG:
3346 case AFT_LONG:
3347 for (p = afflist; *p != NUL; )
3348 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003349 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003350 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3351 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003352 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003353 if (n == flag)
3354 return TRUE;
3355 }
3356 break;
3357
3358 case AFT_NUM:
3359 for (p = afflist; *p != NUL; )
3360 {
3361 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003362 if (n == 0)
3363 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003364 if (n == flag)
3365 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003366 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003367 ++p;
3368 }
3369 break;
3370 }
3371 return FALSE;
3372}
3373
3374/*
3375 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3376 */
3377 static void
3378aff_check_number(int spinval, int affval, char *name)
3379{
3380 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003381 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003382}
3383
3384/*
3385 * Give a warning when "spinval" and "affval" strings are set and not the same.
3386 */
3387 static void
3388aff_check_string(char_u *spinval, char_u *affval, char *name)
3389{
3390 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003391 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003392}
3393
3394/*
3395 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3396 * NULL as equal.
3397 */
3398 static int
3399str_equal(char_u *s1, char_u *s2)
3400{
3401 if (s1 == NULL || s2 == NULL)
3402 return s1 == s2;
3403 return STRCMP(s1, s2) == 0;
3404}
3405
3406/*
3407 * Add a from-to item to "gap". Used for REP and SAL items.
3408 * They are stored case-folded.
3409 */
3410 static void
3411add_fromto(
3412 spellinfo_T *spin,
3413 garray_T *gap,
3414 char_u *from,
3415 char_u *to)
3416{
3417 fromto_T *ftp;
3418 char_u word[MAXWLEN];
3419
3420 if (ga_grow(gap, 1) == OK)
3421 {
3422 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3423 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
3424 ftp->ft_from = getroom_save(spin, word);
3425 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
3426 ftp->ft_to = getroom_save(spin, word);
3427 ++gap->ga_len;
3428 }
3429}
3430
3431/*
3432 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3433 */
3434 static int
3435sal_to_bool(char_u *s)
3436{
3437 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3438}
3439
3440/*
3441 * Free the structure filled by spell_read_aff().
3442 */
3443 static void
3444spell_free_aff(afffile_T *aff)
3445{
3446 hashtab_T *ht;
3447 hashitem_T *hi;
3448 int todo;
3449 affheader_T *ah;
3450 affentry_T *ae;
3451
3452 vim_free(aff->af_enc);
3453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003454 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003455 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3456 {
3457 todo = (int)ht->ht_used;
3458 for (hi = ht->ht_array; todo > 0; ++hi)
3459 {
3460 if (!HASHITEM_EMPTY(hi))
3461 {
3462 --todo;
3463 ah = HI2AH(hi);
3464 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3465 vim_regfree(ae->ae_prog);
3466 }
3467 }
3468 if (ht == &aff->af_suff)
3469 break;
3470 }
3471
3472 hash_clear(&aff->af_pref);
3473 hash_clear(&aff->af_suff);
3474 hash_clear(&aff->af_comp);
3475}
3476
3477/*
3478 * Read dictionary file "fname".
3479 * Returns OK or FAIL;
3480 */
3481 static int
3482spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3483{
3484 hashtab_T ht;
3485 char_u line[MAXLINELEN];
3486 char_u *p;
3487 char_u *afflist;
3488 char_u store_afflist[MAXWLEN];
3489 int pfxlen;
3490 int need_affix;
3491 char_u *dw;
3492 char_u *pc;
3493 char_u *w;
3494 int l;
3495 hash_T hash;
3496 hashitem_T *hi;
3497 FILE *fd;
3498 int lnum = 1;
3499 int non_ascii = 0;
3500 int retval = OK;
3501 char_u message[MAXLINELEN + MAXWLEN];
3502 int flags;
3503 int duplicate = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003504 time_T last_msg_time = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003505
3506 /*
3507 * Open the file.
3508 */
3509 fd = mch_fopen((char *)fname, "r");
3510 if (fd == NULL)
3511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003512 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003513 return FAIL;
3514 }
3515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003516 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003517 hash_init(&ht);
3518
3519 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003520 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003521 spell_message(spin, IObuff);
3522
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003523 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003524 spin->si_msg_count = 999999;
3525
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003526 // Read and ignore the first line: word count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003527 (void)vim_fgets(line, MAXLINELEN, fd);
3528 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003529 semsg(_("E760: No word count in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003530
3531 /*
3532 * Read all the lines in the file one by one.
3533 * The words are converted to 'encoding' here, before being added to
3534 * the hashtable.
3535 */
3536 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3537 {
3538 line_breakcheck();
3539 ++lnum;
3540 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003541 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003542
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003543 // Remove CR, LF and white space from the end. White space halfway
3544 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003545 l = (int)STRLEN(line);
3546 while (l > 0 && line[l - 1] <= ' ')
3547 --l;
3548 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003549 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003550 line[l] = NUL;
3551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003552 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003553 if (spin->si_conv.vc_type != CONV_NONE)
3554 {
3555 pc = string_convert(&spin->si_conv, line, NULL);
3556 if (pc == NULL)
3557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003558 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003559 fname, lnum, line);
3560 continue;
3561 }
3562 w = pc;
3563 }
3564 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003565 {
3566 pc = NULL;
3567 w = line;
3568 }
3569
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003570 // Truncate the word at the "/", set "afflist" to what follows.
3571 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003572 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003573 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003574 {
3575 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3576 STRMOVE(p, p + 1);
3577 else if (*p == '/')
3578 {
3579 *p = NUL;
3580 afflist = p + 1;
3581 break;
3582 }
3583 }
3584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003585 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003586 if (spin->si_ascii && has_non_ascii(w))
3587 {
3588 ++non_ascii;
3589 vim_free(pc);
3590 continue;
3591 }
3592
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003593 // This takes time, print a message every 10000 words, but not more
3594 // often than once per second.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003595 if (spin->si_verbose && spin->si_msg_count > 10000)
3596 {
3597 spin->si_msg_count = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003598 if (vim_time() > last_msg_time)
3599 {
3600 last_msg_time = vim_time();
3601 vim_snprintf((char *)message, sizeof(message),
3602 _("line %6d, word %6ld - %s"),
3603 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3604 msg_start();
3605 msg_outtrans_long_attr(message, 0);
3606 msg_clr_eos();
3607 msg_didout = FALSE;
3608 msg_col = 0;
3609 out_flush();
3610 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003611 }
3612
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003613 // Store the word in the hashtable to be able to find duplicates.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003614 dw = (char_u *)getroom_save(spin, w);
3615 if (dw == NULL)
3616 {
3617 retval = FAIL;
3618 vim_free(pc);
3619 break;
3620 }
3621
3622 hash = hash_hash(dw);
3623 hi = hash_lookup(&ht, dw, hash);
3624 if (!HASHITEM_EMPTY(hi))
3625 {
3626 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003627 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003628 fname, lnum, dw);
3629 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003630 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003631 fname, lnum, dw);
3632 ++duplicate;
3633 }
3634 else
3635 hash_add_item(&ht, hi, dw, hash);
3636
3637 flags = 0;
3638 store_afflist[0] = NUL;
3639 pfxlen = 0;
3640 need_affix = FALSE;
3641 if (afflist != NULL)
3642 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003643 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003644 flags |= get_affix_flags(affile, afflist);
3645
3646 if (affile->af_needaffix != 0 && flag_in_afflist(
3647 affile->af_flagtype, afflist, affile->af_needaffix))
3648 need_affix = TRUE;
3649
3650 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003651 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003652 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3653
3654 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003655 // Need to store the list of compound flags with the word.
3656 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003657 get_compflags(affile, afflist, store_afflist + pfxlen);
3658 }
3659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003661 if (store_word(spin, dw, flags, spin->si_region,
3662 store_afflist, need_affix) == FAIL)
3663 retval = FAIL;
3664
3665 if (afflist != NULL)
3666 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003667 // Find all matching suffixes and add the resulting words.
3668 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003669 if (store_aff_word(spin, dw, afflist, affile,
3670 &affile->af_suff, &affile->af_pref,
3671 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3672 retval = FAIL;
3673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003674 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003675 if (store_aff_word(spin, dw, afflist, affile,
3676 &affile->af_pref, NULL,
3677 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3678 retval = FAIL;
3679 }
3680
3681 vim_free(pc);
3682 }
3683
3684 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003685 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003686 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003687 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003688 non_ascii, fname);
3689 hash_clear(&ht);
3690
3691 fclose(fd);
3692 return retval;
3693}
3694
3695/*
3696 * Check for affix flags in "afflist" that are turned into word flags.
3697 * Return WF_ flags.
3698 */
3699 static int
3700get_affix_flags(afffile_T *affile, char_u *afflist)
3701{
3702 int flags = 0;
3703
3704 if (affile->af_keepcase != 0 && flag_in_afflist(
3705 affile->af_flagtype, afflist, affile->af_keepcase))
3706 flags |= WF_KEEPCAP | WF_FIXCAP;
3707 if (affile->af_rare != 0 && flag_in_afflist(
3708 affile->af_flagtype, afflist, affile->af_rare))
3709 flags |= WF_RARE;
3710 if (affile->af_bad != 0 && flag_in_afflist(
3711 affile->af_flagtype, afflist, affile->af_bad))
3712 flags |= WF_BANNED;
3713 if (affile->af_needcomp != 0 && flag_in_afflist(
3714 affile->af_flagtype, afflist, affile->af_needcomp))
3715 flags |= WF_NEEDCOMP;
3716 if (affile->af_comproot != 0 && flag_in_afflist(
3717 affile->af_flagtype, afflist, affile->af_comproot))
3718 flags |= WF_COMPROOT;
3719 if (affile->af_nosuggest != 0 && flag_in_afflist(
3720 affile->af_flagtype, afflist, affile->af_nosuggest))
3721 flags |= WF_NOSUGGEST;
3722 return flags;
3723}
3724
3725/*
3726 * Get the list of prefix IDs from the affix list "afflist".
3727 * Used for PFXPOSTPONE.
3728 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3729 * and return the number of affixes.
3730 */
3731 static int
3732get_pfxlist(
3733 afffile_T *affile,
3734 char_u *afflist,
3735 char_u *store_afflist)
3736{
3737 char_u *p;
3738 char_u *prevp;
3739 int cnt = 0;
3740 int id;
3741 char_u key[AH_KEY_LEN];
3742 hashitem_T *hi;
3743
3744 for (p = afflist; *p != NUL; )
3745 {
3746 prevp = p;
3747 if (get_affitem(affile->af_flagtype, &p) != 0)
3748 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003749 // A flag is a postponed prefix flag if it appears in "af_pref"
3750 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003751 vim_strncpy(key, prevp, p - prevp);
3752 hi = hash_find(&affile->af_pref, key);
3753 if (!HASHITEM_EMPTY(hi))
3754 {
3755 id = HI2AH(hi)->ah_newID;
3756 if (id != 0)
3757 store_afflist[cnt++] = id;
3758 }
3759 }
3760 if (affile->af_flagtype == AFT_NUM && *p == ',')
3761 ++p;
3762 }
3763
3764 store_afflist[cnt] = NUL;
3765 return cnt;
3766}
3767
3768/*
3769 * Get the list of compound IDs from the affix list "afflist" that are used
3770 * for compound words.
3771 * Puts the flags in "store_afflist[]".
3772 */
3773 static void
3774get_compflags(
3775 afffile_T *affile,
3776 char_u *afflist,
3777 char_u *store_afflist)
3778{
3779 char_u *p;
3780 char_u *prevp;
3781 int cnt = 0;
3782 char_u key[AH_KEY_LEN];
3783 hashitem_T *hi;
3784
3785 for (p = afflist; *p != NUL; )
3786 {
3787 prevp = p;
3788 if (get_affitem(affile->af_flagtype, &p) != 0)
3789 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003790 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003791 vim_strncpy(key, prevp, p - prevp);
3792 hi = hash_find(&affile->af_comp, key);
3793 if (!HASHITEM_EMPTY(hi))
3794 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3795 }
3796 if (affile->af_flagtype == AFT_NUM && *p == ',')
3797 ++p;
3798 }
3799
3800 store_afflist[cnt] = NUL;
3801}
3802
3803/*
3804 * Apply affixes to a word and store the resulting words.
3805 * "ht" is the hashtable with affentry_T that need to be applied, either
3806 * prefixes or suffixes.
3807 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3808 * the resulting words for combining affixes.
3809 *
3810 * Returns FAIL when out of memory.
3811 */
3812 static int
3813store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003814 spellinfo_T *spin, // spell info
3815 char_u *word, // basic word start
3816 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003817 afffile_T *affile,
3818 hashtab_T *ht,
3819 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003820 int condit, // CONDIT_SUF et al.
3821 int flags, // flags for the word
3822 char_u *pfxlist, // list of prefix IDs
3823 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3824 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003825{
3826 int todo;
3827 hashitem_T *hi;
3828 affheader_T *ah;
3829 affentry_T *ae;
3830 char_u newword[MAXWLEN];
3831 int retval = OK;
3832 int i, j;
3833 char_u *p;
3834 int use_flags;
3835 char_u *use_pfxlist;
3836 int use_pfxlen;
3837 int need_affix;
3838 char_u store_afflist[MAXWLEN];
3839 char_u pfx_pfxlist[MAXWLEN];
3840 size_t wordlen = STRLEN(word);
3841 int use_condit;
3842
3843 todo = (int)ht->ht_used;
3844 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3845 {
3846 if (!HASHITEM_EMPTY(hi))
3847 {
3848 --todo;
3849 ah = HI2AH(hi);
3850
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003851 // Check that the affix combines, if required, and that the word
3852 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003853 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3854 && flag_in_afflist(affile->af_flagtype, afflist,
3855 ah->ah_flag))
3856 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003857 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003858 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3859 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003860 // Check the condition. It's not logical to match case
3861 // here, but it is required for compatibility with
3862 // Myspell.
3863 // Another requirement from Myspell is that the chop
3864 // string is shorter than the word itself.
3865 // For prefixes, when "PFXPOSTPONE" was used, only do
3866 // prefixes with a chop string and/or flags.
3867 // When a previously added affix had CIRCUMFIX this one
3868 // must have it too, if it had not then this one must not
3869 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003870 if ((xht != NULL || !affile->af_pfxpostpone
3871 || ae->ae_chop != NULL
3872 || ae->ae_flags != NULL)
3873 && (ae->ae_chop == NULL
3874 || STRLEN(ae->ae_chop) < wordlen)
3875 && (ae->ae_prog == NULL
3876 || vim_regexec_prog(&ae->ae_prog, FALSE,
3877 word, (colnr_T)0))
3878 && (((condit & CONDIT_CFIX) == 0)
3879 == ((condit & CONDIT_AFF) == 0
3880 || ae->ae_flags == NULL
3881 || !flag_in_afflist(affile->af_flagtype,
3882 ae->ae_flags, affile->af_circumfix))))
3883 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003884 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003885 if (xht == NULL)
3886 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003888 if (ae->ae_add == NULL)
3889 *newword = NUL;
3890 else
3891 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3892 p = word;
3893 if (ae->ae_chop != NULL)
3894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003895 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003896 if (has_mbyte)
3897 {
3898 i = mb_charlen(ae->ae_chop);
3899 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003900 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003901 }
3902 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003903 p += STRLEN(ae->ae_chop);
3904 }
3905 STRCAT(newword, p);
3906 }
3907 else
3908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003909 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003910 vim_strncpy(newword, word, MAXWLEN - 1);
3911 if (ae->ae_chop != NULL)
3912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003913 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003914 p = newword + STRLEN(newword);
3915 i = (int)MB_CHARLEN(ae->ae_chop);
3916 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003917 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003918 *p = NUL;
3919 }
3920 if (ae->ae_add != NULL)
3921 STRCAT(newword, ae->ae_add);
3922 }
3923
3924 use_flags = flags;
3925 use_pfxlist = pfxlist;
3926 use_pfxlen = pfxlen;
3927 need_affix = FALSE;
3928 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3929 if (ae->ae_flags != NULL)
3930 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003931 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003932 use_flags |= get_affix_flags(affile, ae->ae_flags);
3933
3934 if (affile->af_needaffix != 0 && flag_in_afflist(
3935 affile->af_flagtype, ae->ae_flags,
3936 affile->af_needaffix))
3937 need_affix = TRUE;
3938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003939 // When there is a CIRCUMFIX flag the other affix
3940 // must also have it and we don't add the word
3941 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003942 if (affile->af_circumfix != 0 && flag_in_afflist(
3943 affile->af_flagtype, ae->ae_flags,
3944 affile->af_circumfix))
3945 {
3946 use_condit |= CONDIT_CFIX;
3947 if ((condit & CONDIT_CFIX) == 0)
3948 need_affix = TRUE;
3949 }
3950
3951 if (affile->af_pfxpostpone
3952 || spin->si_compflags != NULL)
3953 {
3954 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003955 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003956 use_pfxlen = get_pfxlist(affile,
3957 ae->ae_flags, store_afflist);
3958 else
3959 use_pfxlen = 0;
3960 use_pfxlist = store_afflist;
3961
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003962 // Combine the prefix IDs. Avoid adding the
3963 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003964 for (i = 0; i < pfxlen; ++i)
3965 {
3966 for (j = 0; j < use_pfxlen; ++j)
3967 if (pfxlist[i] == use_pfxlist[j])
3968 break;
3969 if (j == use_pfxlen)
3970 use_pfxlist[use_pfxlen++] = pfxlist[i];
3971 }
3972
3973 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003974 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003975 get_compflags(affile, ae->ae_flags,
3976 use_pfxlist + use_pfxlen);
3977
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003978 // Combine the list of compound flags.
3979 // Concatenate them to the prefix IDs list.
3980 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003981 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3982 {
3983 for (j = use_pfxlen;
3984 use_pfxlist[j] != NUL; ++j)
3985 if (pfxlist[i] == use_pfxlist[j])
3986 break;
3987 if (use_pfxlist[j] == NUL)
3988 {
3989 use_pfxlist[j++] = pfxlist[i];
3990 use_pfxlist[j] = NUL;
3991 }
3992 }
3993 }
3994 }
3995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003996 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
3997 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003998 if (use_pfxlist != NULL && ae->ae_compforbid)
3999 {
4000 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
4001 use_pfxlist = pfx_pfxlist;
4002 }
4003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004004 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004005 if (spin->si_prefroot != NULL
4006 && spin->si_prefroot->wn_sibling != NULL)
4007 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004008 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004009 use_flags |= WF_HAS_AFF;
4010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004011 // ... don't use a prefix list if combining
4012 // affixes is not allowed. But do use the
4013 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004014 if (!ah->ah_combine && use_pfxlist != NULL)
4015 use_pfxlist += use_pfxlen;
4016 }
4017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004018 // When compounding is supported and there is no
4019 // "COMPOUNDPERMITFLAG" then forbid compounding on the
4020 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004021 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4022 {
4023 if (xht != NULL)
4024 use_flags |= WF_NOCOMPAFT;
4025 else
4026 use_flags |= WF_NOCOMPBEF;
4027 }
4028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004029 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004030 if (store_word(spin, newword, use_flags,
4031 spin->si_region, use_pfxlist,
4032 need_affix) == FAIL)
4033 retval = FAIL;
4034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004035 // When added a prefix or a first suffix and the affix
4036 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004037 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4038 if (store_aff_word(spin, newword, ae->ae_flags,
4039 affile, &affile->af_suff, xht,
4040 use_condit & (xht == NULL
4041 ? ~0 : ~CONDIT_SUF),
4042 use_flags, use_pfxlist, pfxlen) == FAIL)
4043 retval = FAIL;
4044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004045 // When added a suffix and combining is allowed also
4046 // try adding a prefix additionally. Both for the
4047 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004048 if (xht != NULL && ah->ah_combine)
4049 {
4050 if (store_aff_word(spin, newword,
4051 afflist, affile,
4052 xht, NULL, use_condit,
4053 use_flags, use_pfxlist,
4054 pfxlen) == FAIL
4055 || (ae->ae_flags != NULL
4056 && store_aff_word(spin, newword,
4057 ae->ae_flags, affile,
4058 xht, NULL, use_condit,
4059 use_flags, use_pfxlist,
4060 pfxlen) == FAIL))
4061 retval = FAIL;
4062 }
4063 }
4064 }
4065 }
4066 }
4067 }
4068
4069 return retval;
4070}
4071
4072/*
4073 * Read a file with a list of words.
4074 */
4075 static int
4076spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4077{
4078 FILE *fd;
4079 long lnum = 0;
4080 char_u rline[MAXLINELEN];
4081 char_u *line;
4082 char_u *pc = NULL;
4083 char_u *p;
4084 int l;
4085 int retval = OK;
4086 int did_word = FALSE;
4087 int non_ascii = 0;
4088 int flags;
4089 int regionmask;
4090
4091 /*
4092 * Open the file.
4093 */
4094 fd = mch_fopen((char *)fname, "r");
4095 if (fd == NULL)
4096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004097 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004098 return FAIL;
4099 }
4100
Bram Moolenaarc1669272018-06-19 14:23:53 +02004101 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004102 spell_message(spin, IObuff);
4103
4104 /*
4105 * Read all the lines in the file one by one.
4106 */
4107 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4108 {
4109 line_breakcheck();
4110 ++lnum;
4111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004112 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004113 if (*rline == '#')
4114 continue;
4115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004116 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004117 l = (int)STRLEN(rline);
4118 while (l > 0 && rline[l - 1] <= ' ')
4119 --l;
4120 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004121 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004122 rline[l] = NUL;
4123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004124 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004125 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004126 if (spin->si_conv.vc_type != CONV_NONE)
4127 {
4128 pc = string_convert(&spin->si_conv, rline, NULL);
4129 if (pc == NULL)
4130 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004131 smsg(_("Conversion failure for word in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004132 fname, lnum, rline);
4133 continue;
4134 }
4135 line = pc;
4136 }
4137 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004138 {
4139 pc = NULL;
4140 line = rline;
4141 }
4142
4143 if (*line == '/')
4144 {
4145 ++line;
4146 if (STRNCMP(line, "encoding=", 9) == 0)
4147 {
4148 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004149 smsg(_("Duplicate /encoding= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004150 fname, lnum, line - 1);
4151 else if (did_word)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004152 smsg(_("/encoding= line after word ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004153 fname, lnum, line - 1);
4154 else
4155 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004156 char_u *enc;
4157
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004158 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004159 line += 9;
4160 enc = enc_canonize(line);
4161 if (enc != NULL && !spin->si_ascii
4162 && convert_setup(&spin->si_conv, enc,
4163 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004165 fname, line, p_enc);
4166 vim_free(enc);
4167 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004168 }
4169 continue;
4170 }
4171
4172 if (STRNCMP(line, "regions=", 8) == 0)
4173 {
4174 if (spin->si_region_count > 1)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004175 smsg(_("Duplicate /regions= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004176 fname, lnum, line);
4177 else
4178 {
4179 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004180 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004181 smsg(_("Too many regions in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004182 fname, lnum, line);
4183 else
4184 {
4185 spin->si_region_count = (int)STRLEN(line) / 2;
4186 STRCPY(spin->si_region_name, line);
4187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004188 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004189 spin->si_region = (1 << spin->si_region_count) - 1;
4190 }
4191 }
4192 continue;
4193 }
4194
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004195 smsg(_("/ line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004196 fname, lnum, line - 1);
4197 continue;
4198 }
4199
4200 flags = 0;
4201 regionmask = spin->si_region;
4202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004203 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004204 p = vim_strchr(line, '/');
4205 if (p != NULL)
4206 {
4207 *p++ = NUL;
4208 while (*p != NUL)
4209 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004210 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004211 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004213 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004214 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004215 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004216 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004217 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004218 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004219 regionmask = 0;
4220 flags |= WF_REGION;
4221
4222 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004223 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004224 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004225 smsg(_("Invalid region nr in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004226 fname, lnum, p);
4227 break;
4228 }
4229 regionmask |= 1 << (l - 1);
4230 }
4231 else
4232 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004233 smsg(_("Unrecognized flags in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004234 fname, lnum, p);
4235 break;
4236 }
4237 ++p;
4238 }
4239 }
4240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004241 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004242 if (spin->si_ascii && has_non_ascii(line))
4243 {
4244 ++non_ascii;
4245 continue;
4246 }
4247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004248 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004249 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4250 {
4251 retval = FAIL;
4252 break;
4253 }
4254 did_word = TRUE;
4255 }
4256
4257 vim_free(pc);
4258 fclose(fd);
4259
4260 if (spin->si_ascii && non_ascii > 0)
4261 {
4262 vim_snprintf((char *)IObuff, IOSIZE,
4263 _("Ignored %d words with non-ASCII characters"), non_ascii);
4264 spell_message(spin, IObuff);
4265 }
4266
4267 return retval;
4268}
4269
4270/*
4271 * Get part of an sblock_T, "len" bytes long.
4272 * This avoids calling free() for every little struct we use (and keeping
4273 * track of them).
4274 * The memory is cleared to all zeros.
4275 * Returns NULL when out of memory.
4276 */
4277 static void *
4278getroom(
4279 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004280 size_t len, // length needed
4281 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004282{
4283 char_u *p;
4284 sblock_T *bl = spin->si_blocks;
4285
4286 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004287 // Round size up for alignment. On some systems structures need to be
4288 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004289 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4290 & ~(sizeof(char *) - 1);
4291
4292 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4293 {
4294 if (len >= SBLOCKSIZE)
4295 bl = NULL;
4296 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004297 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004298 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004299 if (bl == NULL)
4300 {
4301 if (!spin->si_did_emsg)
4302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004303 emsg(_("E845: Insufficient memory, word list will be incomplete"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004304 spin->si_did_emsg = TRUE;
4305 }
4306 return NULL;
4307 }
4308 bl->sb_next = spin->si_blocks;
4309 spin->si_blocks = bl;
4310 bl->sb_used = 0;
4311 ++spin->si_blocks_cnt;
4312 }
4313
4314 p = bl->sb_data + bl->sb_used;
4315 bl->sb_used += (int)len;
4316
4317 return p;
4318}
4319
4320/*
4321 * Make a copy of a string into memory allocated with getroom().
4322 * Returns NULL when out of memory.
4323 */
4324 static char_u *
4325getroom_save(spellinfo_T *spin, char_u *s)
4326{
4327 char_u *sc;
4328
4329 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4330 if (sc != NULL)
4331 STRCPY(sc, s);
4332 return sc;
4333}
4334
4335
4336/*
4337 * Free the list of allocated sblock_T.
4338 */
4339 static void
4340free_blocks(sblock_T *bl)
4341{
4342 sblock_T *next;
4343
4344 while (bl != NULL)
4345 {
4346 next = bl->sb_next;
4347 vim_free(bl);
4348 bl = next;
4349 }
4350}
4351
4352/*
4353 * Allocate the root of a word tree.
4354 * Returns NULL when out of memory.
4355 */
4356 static wordnode_T *
4357wordtree_alloc(spellinfo_T *spin)
4358{
4359 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4360}
4361
4362/*
4363 * Store a word in the tree(s).
4364 * Always store it in the case-folded tree. For a keep-case word this is
4365 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4366 * used to find suggestions.
4367 * For a keep-case word also store it in the keep-case tree.
4368 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4369 * compound flag.
4370 */
4371 static int
4372store_word(
4373 spellinfo_T *spin,
4374 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004375 int flags, // extra flags, WF_BANNED
4376 int region, // supported region(s)
4377 char_u *pfxlist, // list of prefix IDs or NULL
4378 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004379{
4380 int len = (int)STRLEN(word);
4381 int ct = captype(word, word + len);
4382 char_u foldword[MAXWLEN];
4383 int res = OK;
4384 char_u *p;
4385
4386 (void)spell_casefold(word, len, foldword, MAXWLEN);
4387 for (p = pfxlist; res == OK; ++p)
4388 {
4389 if (!need_affix || (p != NULL && *p != NUL))
4390 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4391 region, p == NULL ? 0 : *p);
4392 if (p == NULL || *p == NUL)
4393 break;
4394 }
4395 ++spin->si_foldwcount;
4396
4397 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4398 {
4399 for (p = pfxlist; res == OK; ++p)
4400 {
4401 if (!need_affix || (p != NULL && *p != NUL))
4402 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4403 region, p == NULL ? 0 : *p);
4404 if (p == NULL || *p == NUL)
4405 break;
4406 }
4407 ++spin->si_keepwcount;
4408 }
4409 return res;
4410}
4411
4412/*
4413 * Add word "word" to a word tree at "root".
4414 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4415 * "rare" and "region" is the condition nr.
4416 * Returns FAIL when out of memory.
4417 */
4418 static int
4419tree_add_word(
4420 spellinfo_T *spin,
4421 char_u *word,
4422 wordnode_T *root,
4423 int flags,
4424 int region,
4425 int affixID)
4426{
4427 wordnode_T *node = root;
4428 wordnode_T *np;
4429 wordnode_T *copyp, **copyprev;
4430 wordnode_T **prev = NULL;
4431 int i;
4432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004433 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004434 for (i = 0; ; ++i)
4435 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004436 // When there is more than one reference to this node we need to make
4437 // a copy, so that we can modify it. Copy the whole list of siblings
4438 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004439 if (node != NULL && node->wn_refs > 1)
4440 {
4441 --node->wn_refs;
4442 copyprev = prev;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004443 FOR_ALL_NODE_SIBLINGS(node, copyp)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004444 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004445 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004446 np = get_wordnode(spin);
4447 if (np == NULL)
4448 return FAIL;
4449 np->wn_child = copyp->wn_child;
4450 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004451 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004452 np->wn_byte = copyp->wn_byte;
4453 if (np->wn_byte == NUL)
4454 {
4455 np->wn_flags = copyp->wn_flags;
4456 np->wn_region = copyp->wn_region;
4457 np->wn_affixID = copyp->wn_affixID;
4458 }
4459
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004460 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004461 np->wn_refs = 1;
4462 if (copyprev != NULL)
4463 *copyprev = np;
4464 copyprev = &np->wn_sibling;
4465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004466 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004467 if (copyp == node)
4468 node = np;
4469 }
4470 }
4471
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004472 // Look for the sibling that has the same character. They are sorted
4473 // on byte value, thus stop searching when a sibling is found with a
4474 // higher byte value. For zero bytes (end of word) the sorting is
4475 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004476 while (node != NULL
4477 && (node->wn_byte < word[i]
4478 || (node->wn_byte == NUL
4479 && (flags < 0
4480 ? node->wn_affixID < (unsigned)affixID
4481 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4482 || (node->wn_flags == (flags & WN_MASK)
4483 && (spin->si_sugtree
4484 ? (node->wn_region & 0xffff) < region
4485 : node->wn_affixID
4486 < (unsigned)affixID)))))))
4487 {
4488 prev = &node->wn_sibling;
4489 node = *prev;
4490 }
4491 if (node == NULL
4492 || node->wn_byte != word[i]
4493 || (word[i] == NUL
4494 && (flags < 0
4495 || spin->si_sugtree
4496 || node->wn_flags != (flags & WN_MASK)
4497 || node->wn_affixID != affixID)))
4498 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004499 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004500 np = get_wordnode(spin);
4501 if (np == NULL)
4502 return FAIL;
4503 np->wn_byte = word[i];
4504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004505 // If "node" is NULL this is a new child or the end of the sibling
4506 // list: ref count is one. Otherwise use ref count of sibling and
4507 // make ref count of sibling one (matters when inserting in front
4508 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004509 if (node == NULL)
4510 np->wn_refs = 1;
4511 else
4512 {
4513 np->wn_refs = node->wn_refs;
4514 node->wn_refs = 1;
4515 }
4516 if (prev != NULL)
4517 *prev = np;
4518 np->wn_sibling = node;
4519 node = np;
4520 }
4521
4522 if (word[i] == NUL)
4523 {
4524 node->wn_flags = flags;
4525 node->wn_region |= region;
4526 node->wn_affixID = affixID;
4527 break;
4528 }
4529 prev = &node->wn_child;
4530 node = *prev;
4531 }
4532#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004533 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004534 spell_print_tree(root->wn_sibling);
4535#endif
4536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004537 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004538 ++spin->si_msg_count;
4539
4540 if (spin->si_compress_cnt > 1)
4541 {
4542 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004543 // Did enough words to lower the block count limit.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004544 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004545 }
4546
4547 /*
4548 * When we have allocated lots of memory we need to compress the word tree
4549 * to free up some room. But compression is slow, and we might actually
4550 * need that room, thus only compress in the following situations:
4551 * 1. When not compressed before (si_compress_cnt == 0): when using
4552 * "compress_start" blocks.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004553 * 2. When compressed before and used "compress_inc" blocks before
4554 * adding "compress_added" words (si_compress_cnt > 1).
4555 * 3. When compressed before, added "compress_added" words
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004556 * (si_compress_cnt == 1) and the number of free nodes drops below the
4557 * maximum word length.
4558 */
4559#ifndef SPELL_COMPRESS_ALLWAYS
4560 if (spin->si_compress_cnt == 1
4561 ? spin->si_free_count < MAXWLEN
4562 : spin->si_blocks_cnt >= compress_start)
4563#endif
4564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004565 // Decrement the block counter. The effect is that we compress again
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004566 // when the freed up room has been used and another "compress_inc"
4567 // blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004568 // been added, then the limit is put back again.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004569 spin->si_blocks_cnt -= compress_inc;
4570 spin->si_compress_cnt = compress_added;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004571
4572 if (spin->si_verbose)
4573 {
4574 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004575 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004576 msg_clr_eos();
4577 msg_didout = FALSE;
4578 msg_col = 0;
4579 out_flush();
4580 }
4581
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004582 // Compress both trees. Either they both have many nodes, which makes
4583 // compression useful, or one of them is small, which means
4584 // compression goes fast. But when filling the soundfold word tree
4585 // there is no keep-case tree.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004586 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004587 if (affixID >= 0)
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004588 wordtree_compress(spin, spin->si_keeproot, "keep-case");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004589 }
4590
4591 return OK;
4592}
4593
4594/*
4595 * Get a wordnode_T, either from the list of previously freed nodes or
4596 * allocate a new one.
4597 * Returns NULL when out of memory.
4598 */
4599 static wordnode_T *
4600get_wordnode(spellinfo_T *spin)
4601{
4602 wordnode_T *n;
4603
4604 if (spin->si_first_free == NULL)
4605 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4606 else
4607 {
4608 n = spin->si_first_free;
4609 spin->si_first_free = n->wn_child;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004610 CLEAR_POINTER(n);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004611 --spin->si_free_count;
4612 }
4613#ifdef SPELL_PRINTTREE
4614 if (n != NULL)
4615 n->wn_nr = ++spin->si_wordnode_nr;
4616#endif
4617 return n;
4618}
4619
4620/*
4621 * Decrement the reference count on a node (which is the head of a list of
4622 * siblings). If the reference count becomes zero free the node and its
4623 * siblings.
4624 * Returns the number of nodes actually freed.
4625 */
4626 static int
4627deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4628{
4629 wordnode_T *np;
4630 int cnt = 0;
4631
4632 if (--node->wn_refs == 0)
4633 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004634 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004635 {
4636 if (np->wn_child != NULL)
4637 cnt += deref_wordnode(spin, np->wn_child);
4638 free_wordnode(spin, np);
4639 ++cnt;
4640 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004641 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004642 }
4643 return cnt;
4644}
4645
4646/*
4647 * Free a wordnode_T for re-use later.
4648 * Only the "wn_child" field becomes invalid.
4649 */
4650 static void
4651free_wordnode(spellinfo_T *spin, wordnode_T *n)
4652{
4653 n->wn_child = spin->si_first_free;
4654 spin->si_first_free = n;
4655 ++spin->si_free_count;
4656}
4657
4658/*
4659 * Compress a tree: find tails that are identical and can be shared.
4660 */
4661 static void
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004662wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004663{
4664 hashtab_T ht;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004665 long n;
4666 long tot = 0;
4667 long perc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004668
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004669 // Skip the root itself, it's not actually used. The first sibling is the
4670 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004671 if (root->wn_sibling != NULL)
4672 {
4673 hash_init(&ht);
4674 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4675
4676#ifndef SPELL_PRINTTREE
4677 if (spin->si_verbose || p_verbose > 2)
4678#endif
4679 {
4680 if (tot > 1000000)
4681 perc = (tot - n) / (tot / 100);
4682 else if (tot == 0)
4683 perc = 0;
4684 else
4685 perc = (tot - n) * 100 / tot;
4686 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004687 _("Compressed %s: %ld of %ld nodes; %ld (%ld%%) remaining"),
4688 name, n, tot, tot - n, perc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004689 spell_message(spin, IObuff);
4690 }
4691#ifdef SPELL_PRINTTREE
4692 spell_print_tree(root->wn_sibling);
4693#endif
4694 hash_clear(&ht);
4695 }
4696}
4697
4698/*
4699 * Compress a node, its siblings and its children, depth first.
4700 * Returns the number of compressed nodes.
4701 */
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004702 static long
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004703node_compress(
4704 spellinfo_T *spin,
4705 wordnode_T *node,
4706 hashtab_T *ht,
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004707 long *tot) // total count of nodes before compressing,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004708 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004709{
4710 wordnode_T *np;
4711 wordnode_T *tp;
4712 wordnode_T *child;
4713 hash_T hash;
4714 hashitem_T *hi;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004715 long len = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004716 unsigned nr, n;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004717 long compressed = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004718
4719 /*
4720 * Go through the list of siblings. Compress each child and then try
4721 * finding an identical child to replace it.
4722 * Note that with "child" we mean not just the node that is pointed to,
4723 * but the whole list of siblings of which the child node is the first.
4724 */
4725 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4726 {
4727 ++len;
4728 if ((child = np->wn_child) != NULL)
4729 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004730 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004731 compressed += node_compress(spin, child, ht, tot);
4732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004733 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004734 hash = hash_hash(child->wn_u1.hashkey);
4735 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4736 if (!HASHITEM_EMPTY(hi))
4737 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004738 // There are children we encountered before with a hash value
4739 // identical to the current child. Now check if there is one
4740 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004741 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4742 if (node_equal(child, tp))
4743 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004744 // Found one! Now use that child in place of the
4745 // current one. This means the current child and all
4746 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004747 ++tp->wn_refs;
4748 compressed += deref_wordnode(spin, child);
4749 np->wn_child = tp;
4750 break;
4751 }
4752 if (tp == NULL)
4753 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004754 // No other child with this hash value equals the child of
4755 // the node, add it to the linked list after the first
4756 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004757 tp = HI2WN(hi);
4758 child->wn_u2.next = tp->wn_u2.next;
4759 tp->wn_u2.next = child;
4760 }
4761 }
4762 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004763 // No other child has this hash value, add it to the
4764 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004765 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4766 }
4767 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004768 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004769
4770 /*
4771 * Make a hash key for the node and its siblings, so that we can quickly
4772 * find a lookalike node. This must be done after compressing the sibling
4773 * list, otherwise the hash key would become invalid by the compression.
4774 */
4775 node->wn_u1.hashkey[0] = len;
4776 nr = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004777 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004778 {
4779 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004780 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004781 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4782 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004783 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004784 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4785 nr = nr * 101 + n;
4786 }
4787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004788 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004789 n = nr & 0xff;
4790 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4791 n = (nr >> 8) & 0xff;
4792 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4793 n = (nr >> 16) & 0xff;
4794 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4795 n = (nr >> 24) & 0xff;
4796 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4797 node->wn_u1.hashkey[5] = NUL;
4798
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004799 // Check for CTRL-C pressed now and then.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004800 veryfast_breakcheck();
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004801
4802 return compressed;
4803}
4804
4805/*
4806 * Return TRUE when two nodes have identical siblings and children.
4807 */
4808 static int
4809node_equal(wordnode_T *n1, wordnode_T *n2)
4810{
4811 wordnode_T *p1;
4812 wordnode_T *p2;
4813
4814 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4815 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4816 if (p1->wn_byte != p2->wn_byte
4817 || (p1->wn_byte == NUL
4818 ? (p1->wn_flags != p2->wn_flags
4819 || p1->wn_region != p2->wn_region
4820 || p1->wn_affixID != p2->wn_affixID)
4821 : (p1->wn_child != p2->wn_child)))
4822 break;
4823
4824 return p1 == NULL && p2 == NULL;
4825}
4826
Bram Moolenaareae1b912019-05-09 15:12:55 +02004827static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004828
4829/*
4830 * Function given to qsort() to sort the REP items on "from" string.
4831 */
4832 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004833rep_compare(const void *s1, const void *s2)
4834{
4835 fromto_T *p1 = (fromto_T *)s1;
4836 fromto_T *p2 = (fromto_T *)s2;
4837
4838 return STRCMP(p1->ft_from, p2->ft_from);
4839}
4840
4841/*
4842 * Write the Vim .spl file "fname".
4843 * Return FAIL or OK;
4844 */
4845 static int
4846write_vim_spell(spellinfo_T *spin, char_u *fname)
4847{
4848 FILE *fd;
4849 int regionmask;
4850 int round;
4851 wordnode_T *tree;
4852 int nodecount;
4853 int i;
4854 int l;
4855 garray_T *gap;
4856 fromto_T *ftp;
4857 char_u *p;
4858 int rr;
4859 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004860 size_t fwv = 1; // collect return value of fwrite() to avoid
4861 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004862
4863 fd = mch_fopen((char *)fname, "w");
4864 if (fd == NULL)
4865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004867 return FAIL;
4868 }
4869
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004870 // <HEADER>: <fileID> <versionnr>
4871 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004872 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4873 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004874 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004875 goto theend;
4876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004877 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004878
4879 /*
4880 * <SECTIONS>: <section> ... <sectionend>
4881 */
4882
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004883 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004884 if (spin->si_info != NULL)
4885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004886 putc(SN_INFO, fd); // <sectionID>
4887 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004888
4889 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004890 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4891 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004892 }
4893
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004894 // SN_REGION: <regionname> ...
4895 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004896 if (spin->si_region_count > 1)
4897 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004898 putc(SN_REGION, fd); // <sectionID>
4899 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004900 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004901 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004902 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004903 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004904 regionmask = (1 << spin->si_region_count) - 1;
4905 }
4906 else
4907 regionmask = 0;
4908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004909 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4910 //
4911 // The table with character flags and the table for case folding.
4912 // This makes sure the same characters are recognized as word characters
4913 // when generating an when using a spell file.
4914 // Skip this for ASCII, the table may conflict with the one used for
4915 // 'encoding'.
4916 // Also skip this for an .add.spl file, the main spell file must contain
4917 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004918 if (!spin->si_ascii && !spin->si_add)
4919 {
4920 char_u folchars[128 * 8];
4921 int flags;
4922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004923 putc(SN_CHARFLAGS, fd); // <sectionID>
4924 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004926 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004927 l = 0;
4928 for (i = 128; i < 256; ++i)
4929 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004930 if (has_mbyte)
4931 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4932 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004933 folchars[l++] = spelltab.st_fold[i];
4934 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004935 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004937 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004938 for (i = 128; i < 256; ++i)
4939 {
4940 flags = 0;
4941 if (spelltab.st_isw[i])
4942 flags |= CF_WORD;
4943 if (spelltab.st_isu[i])
4944 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004945 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004946 }
4947
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004948 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4949 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004950 }
4951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004952 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004953 if (spin->si_midword != NULL)
4954 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004955 putc(SN_MIDWORD, fd); // <sectionID>
4956 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004957
4958 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004959 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004960 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004961 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004962 }
4963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004964 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004965 if (spin->si_prefcond.ga_len > 0)
4966 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004967 putc(SN_PREFCOND, fd); // <sectionID>
4968 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004969
4970 l = write_spell_prefcond(NULL, &spin->si_prefcond);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004971 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004972
4973 write_spell_prefcond(fd, &spin->si_prefcond);
4974 }
4975
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004976 // SN_REP: <repcount> <rep> ...
4977 // SN_SAL: <salflags> <salcount> <sal> ...
4978 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004980 // round 1: SN_REP section
4981 // round 2: SN_SAL section (unless SN_SOFO is used)
4982 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004983 for (round = 1; round <= 3; ++round)
4984 {
4985 if (round == 1)
4986 gap = &spin->si_rep;
4987 else if (round == 2)
4988 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004989 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004990 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4991 continue;
4992 gap = &spin->si_sal;
4993 }
4994 else
4995 gap = &spin->si_repsal;
4996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004997 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004998 if (gap->ga_len == 0)
4999 continue;
5000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005001 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005002 if (round != 2)
5003 qsort(gap->ga_data, (size_t)gap->ga_len,
5004 sizeof(fromto_T), rep_compare);
5005
5006 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005007 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005008
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005009 // This is for making suggestions, section is not required.
5010 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 // Compute the length of what follows.
5013 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005014 for (i = 0; i < gap->ga_len; ++i)
5015 {
5016 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005017 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
5018 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005019 }
5020 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005021 ++l; // count <salflags>
5022 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005023
5024 if (round == 2)
5025 {
5026 i = 0;
5027 if (spin->si_followup)
5028 i |= SAL_F0LLOWUP;
5029 if (spin->si_collapse)
5030 i |= SAL_COLLAPSE;
5031 if (spin->si_rem_accents)
5032 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005033 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005034 }
5035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005036 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005037 for (i = 0; i < gap->ga_len; ++i)
5038 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005039 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5040 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005041 ftp = &((fromto_T *)gap->ga_data)[i];
5042 for (rr = 1; rr <= 2; ++rr)
5043 {
5044 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5045 l = (int)STRLEN(p);
5046 putc(l, fd);
5047 if (l > 0)
5048 fwv &= fwrite(p, l, (size_t)1, fd);
5049 }
5050 }
5051
5052 }
5053
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005054 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5055 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005056 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5057 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005058 putc(SN_SOFO, fd); // <sectionID>
5059 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005060
5061 l = (int)STRLEN(spin->si_sofofr);
5062 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005063 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005064
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005065 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5066 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005067
5068 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005069 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5070 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005071 }
5072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005073 // SN_WORDS: <word> ...
5074 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005075 if (spin->si_commonwords.ht_used > 0)
5076 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005077 putc(SN_WORDS, fd); // <sectionID>
5078 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005079
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005080 // round 1: count the bytes
5081 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005082 for (round = 1; round <= 2; ++round)
5083 {
5084 int todo;
5085 int len = 0;
5086 hashitem_T *hi;
5087
5088 todo = (int)spin->si_commonwords.ht_used;
5089 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5090 if (!HASHITEM_EMPTY(hi))
5091 {
5092 l = (int)STRLEN(hi->hi_key) + 1;
5093 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005094 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005095 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5096 --todo;
5097 }
5098 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005099 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005100 }
5101 }
5102
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005103 // SN_MAP: <mapstr>
5104 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005105 if (spin->si_map.ga_len > 0)
5106 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005107 putc(SN_MAP, fd); // <sectionID>
5108 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005109 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005110 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005111 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005113 }
5114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005115 // SN_SUGFILE: <timestamp>
5116 // This is used to notify that a .sug file may be available and at the
5117 // same time allows for checking that a .sug file that is found matches
5118 // with this .spl file. That's because the word numbers must be exactly
5119 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005120 if (!spin->si_nosugfile
5121 && (spin->si_sal.ga_len > 0
5122 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5123 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005124 putc(SN_SUGFILE, fd); // <sectionID>
5125 putc(0, fd); // <sectionflags>
5126 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005128 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005129 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005130 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005131 }
5132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005133 // SN_NOSPLITSUGS: nothing
5134 // This is used to notify that no suggestions with word splits are to be
5135 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005136 if (spin->si_nosplitsugs)
5137 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005138 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5139 putc(0, fd); // <sectionflags>
5140 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005141 }
5142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005143 // SN_NOCOMPUNDSUGS: nothing
5144 // This is used to notify that no suggestions with compounds are to be
5145 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005146 if (spin->si_nocompoundsugs)
5147 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005148 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5149 putc(0, fd); // <sectionflags>
5150 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005151 }
5152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005153 // SN_COMPOUND: compound info.
5154 // We don't mark it required, when not supported all compound words will
5155 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005156 if (spin->si_compflags != NULL)
5157 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005158 putc(SN_COMPOUND, fd); // <sectionID>
5159 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005160
5161 l = (int)STRLEN(spin->si_compflags);
5162 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5163 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005164 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005166 putc(spin->si_compmax, fd); // <compmax>
5167 putc(spin->si_compminlen, fd); // <compminlen>
5168 putc(spin->si_compsylmax, fd); // <compsylmax>
5169 putc(0, fd); // for Vim 7.0b compatibility
5170 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005171 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005172 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005173 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5174 {
5175 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005176 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005177 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005178 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005179 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005180 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005181 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5182 (size_t)1, fd);
5183 }
5184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005185 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005186 if (spin->si_nobreak)
5187 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005188 putc(SN_NOBREAK, fd); // <sectionID>
5189 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005191 // It's empty, the presence of the section flags the feature.
5192 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005193 }
5194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005195 // SN_SYLLABLE: syllable info.
5196 // We don't mark it required, when not supported syllables will not be
5197 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005198 if (spin->si_syllable != NULL)
5199 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005200 putc(SN_SYLLABLE, fd); // <sectionID>
5201 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005202
5203 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005204 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005205 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005206 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005207 }
5208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005209 // end of <SECTIONS>
5210 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005211
5212
5213 /*
5214 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5215 */
5216 spin->si_memtot = 0;
5217 for (round = 1; round <= 3; ++round)
5218 {
5219 if (round == 1)
5220 tree = spin->si_foldroot->wn_sibling;
5221 else if (round == 2)
5222 tree = spin->si_keeproot->wn_sibling;
5223 else
5224 tree = spin->si_prefroot->wn_sibling;
5225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005226 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005227 clear_node(tree);
5228
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005229 // Count the number of nodes. Needed to be able to allocate the
5230 // memory when reading the nodes. Also fills in index for shared
5231 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005232 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005234 // number of nodes in 4 bytes
5235 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005236 spin->si_memtot += nodecount + nodecount * sizeof(int);
5237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005238 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005239 (void)put_node(fd, tree, 0, regionmask, round == 3);
5240 }
5241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005242 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005243 if (putc(0, fd) == EOF)
5244 retval = FAIL;
5245theend:
5246 if (fclose(fd) == EOF)
5247 retval = FAIL;
5248
5249 if (fwv != (size_t)1)
5250 retval = FAIL;
5251 if (retval == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005252 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005253
5254 return retval;
5255}
5256
5257/*
5258 * Clear the index and wnode fields of "node", it siblings and its
5259 * children. This is needed because they are a union with other items to save
5260 * space.
5261 */
5262 static void
5263clear_node(wordnode_T *node)
5264{
5265 wordnode_T *np;
5266
5267 if (node != NULL)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005268 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005269 {
5270 np->wn_u1.index = 0;
5271 np->wn_u2.wnode = NULL;
5272
5273 if (np->wn_byte != NUL)
5274 clear_node(np->wn_child);
5275 }
5276}
5277
5278
5279/*
5280 * Dump a word tree at node "node".
5281 *
5282 * This first writes the list of possible bytes (siblings). Then for each
5283 * byte recursively write the children.
5284 *
5285 * NOTE: The code here must match the code in read_tree_node(), since
5286 * assumptions are made about the indexes (so that we don't have to write them
5287 * in the file).
5288 *
5289 * Returns the number of nodes used.
5290 */
5291 static int
5292put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005293 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005294 wordnode_T *node,
5295 int idx,
5296 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005297 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005298{
5299 int newindex = idx;
5300 int siblingcount = 0;
5301 wordnode_T *np;
5302 int flags;
5303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005304 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005305 if (node == NULL)
5306 return 0;
5307
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005308 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005309 node->wn_u1.index = idx;
5310
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005311 // Count the number of siblings.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005312 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005313 ++siblingcount;
5314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005315 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005316 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005317 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005319 // Write each sibling byte and optionally extra info.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005320 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005321 {
5322 if (np->wn_byte == 0)
5323 {
5324 if (fd != NULL)
5325 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005327 if (prefixtree)
5328 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 // In PREFIXTREE write the required affixID and the
5330 // associated condition nr (stored in wn_region). The
5331 // byte value is misused to store the "rare" and "not
5332 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005333 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005334 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005335 else
5336 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005337 putc(BY_FLAGS, fd); // <byte>
5338 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005339 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005340 putc(np->wn_affixID, fd); // <affixID>
5341 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005342 }
5343 else
5344 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005345 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005346 flags = np->wn_flags;
5347 if (regionmask != 0 && np->wn_region != regionmask)
5348 flags |= WF_REGION;
5349 if (np->wn_affixID != 0)
5350 flags |= WF_AFX;
5351 if (flags == 0)
5352 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005353 // word without flags or region
5354 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005355 }
5356 else
5357 {
5358 if (np->wn_flags >= 0x100)
5359 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005360 putc(BY_FLAGS2, fd); // <byte>
5361 putc(flags, fd); // <flags>
5362 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005363 }
5364 else
5365 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005366 putc(BY_FLAGS, fd); // <byte>
5367 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005368 }
5369 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005370 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005371 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005372 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005373 }
5374 }
5375 }
5376 }
5377 else
5378 {
5379 if (np->wn_child->wn_u1.index != 0
5380 && np->wn_child->wn_u2.wnode != node)
5381 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005382 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005383 if (fd != NULL)
5384 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005385 putc(BY_INDEX, fd); // <byte>
5386 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005387 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5388 }
5389 }
5390 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005391 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005392 np->wn_child->wn_u2.wnode = node;
5393
5394 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005395 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005397 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005398 return 0;
5399 }
5400 }
5401 }
5402
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005403 // Space used in the array when reading: one for each sibling and one for
5404 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005405 newindex += siblingcount + 1;
5406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005407 // Recursively dump the children of each sibling.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005408 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005409 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5410 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5411 prefixtree);
5412
5413 return newindex;
5414}
5415
5416
5417/*
5418 * ":mkspell [-ascii] outfile infile ..."
5419 * ":mkspell [-ascii] addfile"
5420 */
5421 void
5422ex_mkspell(exarg_T *eap)
5423{
5424 int fcount;
5425 char_u **fnames;
5426 char_u *arg = eap->arg;
5427 int ascii = FALSE;
5428
5429 if (STRNCMP(arg, "-ascii", 6) == 0)
5430 {
5431 ascii = TRUE;
5432 arg = skipwhite(arg + 6);
5433 }
5434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005435 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005436 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5437 {
5438 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5439 FreeWild(fcount, fnames);
5440 }
5441}
5442
5443/*
5444 * Create the .sug file.
5445 * Uses the soundfold info in "spin".
5446 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5447 */
5448 static void
5449spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5450{
5451 char_u *fname = NULL;
5452 int len;
5453 slang_T *slang;
5454 int free_slang = FALSE;
5455
5456 /*
5457 * Read back the .spl file that was written. This fills the required
5458 * info for soundfolding. This also uses less memory than the
5459 * pointer-linked version of the trie. And it avoids having two versions
5460 * of the code for the soundfolding stuff.
5461 * It might have been done already by spell_reload_one().
5462 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005463 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005464 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005465 break;
5466 if (slang == NULL)
5467 {
5468 spell_message(spin, (char_u *)_("Reading back spell file..."));
5469 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5470 if (slang == NULL)
5471 return;
5472 free_slang = TRUE;
5473 }
5474
5475 /*
5476 * Clear the info in "spin" that is used.
5477 */
5478 spin->si_blocks = NULL;
5479 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005480 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005481 spin->si_free_count = 0;
5482 spin->si_first_free = NULL;
5483 spin->si_foldwcount = 0;
5484
5485 /*
5486 * Go through the trie of good words, soundfold each word and add it to
5487 * the soundfold trie.
5488 */
5489 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5490 if (sug_filltree(spin, slang) == FAIL)
5491 goto theend;
5492
5493 /*
5494 * Create the table which links each soundfold word with a list of the
5495 * good words it may come from. Creates buffer "spin->si_spellbuf".
5496 * This also removes the wordnr from the NUL byte entries to make
5497 * compression possible.
5498 */
5499 if (sug_maketable(spin) == FAIL)
5500 goto theend;
5501
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005502 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005503 (long)spin->si_spellbuf->b_ml.ml_line_count);
5504
5505 /*
5506 * Compress the soundfold trie.
5507 */
5508 spell_message(spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02005509 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005510
5511 /*
5512 * Write the .sug file.
5513 * Make the file name by changing ".spl" to ".sug".
5514 */
5515 fname = alloc(MAXPATHL);
5516 if (fname == NULL)
5517 goto theend;
5518 vim_strncpy(fname, wfname, MAXPATHL - 1);
5519 len = (int)STRLEN(fname);
5520 fname[len - 2] = 'u';
5521 fname[len - 1] = 'g';
5522 sug_write(spin, fname);
5523
5524theend:
5525 vim_free(fname);
5526 if (free_slang)
5527 slang_free(slang);
5528 free_blocks(spin->si_blocks);
5529 close_spellbuf(spin->si_spellbuf);
5530}
5531
5532/*
5533 * Build the soundfold trie for language "slang".
5534 */
5535 static int
5536sug_filltree(spellinfo_T *spin, slang_T *slang)
5537{
5538 char_u *byts;
5539 idx_T *idxs;
5540 int depth;
5541 idx_T arridx[MAXWLEN];
5542 int curi[MAXWLEN];
5543 char_u tword[MAXWLEN];
5544 char_u tsalword[MAXWLEN];
5545 int c;
5546 idx_T n;
5547 unsigned words_done = 0;
5548 int wordcount[MAXWLEN];
5549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005550 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005551 spin->si_foldroot = wordtree_alloc(spin);
5552 if (spin->si_foldroot == NULL)
5553 return FAIL;
5554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005555 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005556 spin->si_sugtree = TRUE;
5557
5558 /*
5559 * Go through the whole case-folded tree, soundfold each word and put it
5560 * in the trie.
5561 */
5562 byts = slang->sl_fbyts;
5563 idxs = slang->sl_fidxs;
5564
5565 arridx[0] = 0;
5566 curi[0] = 1;
5567 wordcount[0] = 0;
5568
5569 depth = 0;
5570 while (depth >= 0 && !got_int)
5571 {
5572 if (curi[depth] > byts[arridx[depth]])
5573 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005574 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005575 idxs[arridx[depth]] = wordcount[depth];
5576 if (depth > 0)
5577 wordcount[depth - 1] += wordcount[depth];
5578
5579 --depth;
5580 line_breakcheck();
5581 }
5582 else
5583 {
5584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005585 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005586 n = arridx[depth] + curi[depth];
5587 ++curi[depth];
5588
5589 c = byts[n];
5590 if (c == 0)
5591 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005593 tword[depth] = NUL;
5594 spell_soundfold(slang, tword, TRUE, tsalword);
5595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005596 // We use the "flags" field for the MSB of the wordnr,
5597 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005598 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5599 words_done >> 16, words_done & 0xffff,
5600 0) == FAIL)
5601 return FAIL;
5602
5603 ++words_done;
5604 ++wordcount[depth];
5605
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005606 // Reset the block count each time to avoid compression
5607 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005608 spin->si_blocks_cnt = 0;
5609
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005610 // Skip over any other NUL bytes (same word with different
5611 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005612 while (byts[n + 1] == 0)
5613 {
5614 ++n;
5615 ++curi[depth];
5616 }
5617 }
5618 else
5619 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005620 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005621 tword[depth++] = c;
5622 arridx[depth] = idxs[n];
5623 curi[depth] = 1;
5624 wordcount[depth] = 0;
5625 }
5626 }
5627 }
5628
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005629 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005630
5631 return OK;
5632}
5633
5634/*
5635 * Make the table that links each word in the soundfold trie to the words it
5636 * can be produced from.
5637 * This is not unlike lines in a file, thus use a memfile to be able to access
5638 * the table efficiently.
5639 * Returns FAIL when out of memory.
5640 */
5641 static int
5642sug_maketable(spellinfo_T *spin)
5643{
5644 garray_T ga;
5645 int res = OK;
5646
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005647 // Allocate a buffer, open a memline for it and create the swap file
5648 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005649 spin->si_spellbuf = open_spellbuf();
5650 if (spin->si_spellbuf == NULL)
5651 return FAIL;
5652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005653 // Use a buffer to store the line info, avoids allocating many small
5654 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005655 ga_init2(&ga, 1, 100);
5656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005657 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005658 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5659 res = FAIL;
5660
5661 ga_clear(&ga);
5662 return res;
5663}
5664
5665/*
5666 * Fill the table for one node and its children.
5667 * Returns the wordnr at the start of the node.
5668 * Returns -1 when out of memory.
5669 */
5670 static int
5671sug_filltable(
5672 spellinfo_T *spin,
5673 wordnode_T *node,
5674 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005675 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005676{
5677 wordnode_T *p, *np;
5678 int wordnr = startwordnr;
5679 int nr;
5680 int prev_nr;
5681
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005682 FOR_ALL_NODE_SIBLINGS(node, p)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005683 {
5684 if (p->wn_byte == NUL)
5685 {
5686 gap->ga_len = 0;
5687 prev_nr = 0;
5688 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5689 {
5690 if (ga_grow(gap, 10) == FAIL)
5691 return -1;
5692
5693 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005694 // Compute the offset from the previous nr and store the
5695 // offset in a way that it takes a minimum number of bytes.
5696 // It's a bit like utf-8, but without the need to mark
5697 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005698 nr -= prev_nr;
5699 prev_nr += nr;
5700 gap->ga_len += offset2bytes(nr,
5701 (char_u *)gap->ga_data + gap->ga_len);
5702 }
5703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005704 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005705 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5706
5707 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5708 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5709 return -1;
5710 ++wordnr;
5711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005712 // Remove extra NUL entries, we no longer need them. We don't
5713 // bother freeing the nodes, the won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005714 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5715 p->wn_sibling = p->wn_sibling->wn_sibling;
5716
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005717 // Clear the flags on the remaining NUL node, so that compression
5718 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005719 p->wn_flags = 0;
5720 p->wn_region = 0;
5721 }
5722 else
5723 {
5724 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5725 if (wordnr == -1)
5726 return -1;
5727 }
5728 }
5729 return wordnr;
5730}
5731
5732/*
5733 * Convert an offset into a minimal number of bytes.
5734 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5735 * bytes.
5736 */
5737 static int
5738offset2bytes(int nr, char_u *buf)
5739{
5740 int rem;
5741 int b1, b2, b3, b4;
5742
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005743 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005744 b1 = nr % 255 + 1;
5745 rem = nr / 255;
5746 b2 = rem % 255 + 1;
5747 rem = rem / 255;
5748 b3 = rem % 255 + 1;
5749 b4 = rem / 255 + 1;
5750
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005751 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005752 {
5753 buf[0] = 0xe0 + b4;
5754 buf[1] = b3;
5755 buf[2] = b2;
5756 buf[3] = b1;
5757 return 4;
5758 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005759 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005760 {
5761 buf[0] = 0xc0 + b3;
5762 buf[1] = b2;
5763 buf[2] = b1;
5764 return 3;
5765 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005766 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005767 {
5768 buf[0] = 0x80 + b2;
5769 buf[1] = b1;
5770 return 2;
5771 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005772 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005773 buf[0] = b1;
5774 return 1;
5775}
5776
5777/*
5778 * Write the .sug file in "fname".
5779 */
5780 static void
5781sug_write(spellinfo_T *spin, char_u *fname)
5782{
5783 FILE *fd;
5784 wordnode_T *tree;
5785 int nodecount;
5786 int wcount;
5787 char_u *line;
5788 linenr_T lnum;
5789 int len;
5790
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005791 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005792 fd = mch_fopen((char *)fname, "w");
5793 if (fd == NULL)
5794 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005795 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005796 return;
5797 }
5798
5799 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005800 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005801 spell_message(spin, IObuff);
5802
5803 /*
5804 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5805 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005806 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005808 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005809 goto theend;
5810 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005811 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005813 // Write si_sugtime to the file.
5814 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005815
5816 /*
5817 * <SUGWORDTREE>
5818 */
5819 spin->si_memtot = 0;
5820 tree = spin->si_foldroot->wn_sibling;
5821
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005822 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005823 clear_node(tree);
5824
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005825 // Count the number of nodes. Needed to be able to allocate the
5826 // memory when reading the nodes. Also fills in index for shared
5827 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005828 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005830 // number of nodes in 4 bytes
5831 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005832 spin->si_memtot += nodecount + nodecount * sizeof(int);
5833
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005834 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005835 (void)put_node(fd, tree, 0, 0, FALSE);
5836
5837 /*
5838 * <SUGTABLE>: <sugwcount> <sugline> ...
5839 */
5840 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005841 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005842
5843 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5844 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005845 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005846 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5847 len = (int)STRLEN(line) + 1;
5848 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005850 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005851 goto theend;
5852 }
5853 spin->si_memtot += len;
5854 }
5855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005856 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005857 if (putc(0, fd) == EOF)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005858 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005859
5860 vim_snprintf((char *)IObuff, IOSIZE,
5861 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5862 spell_message(spin, IObuff);
5863
5864theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005865 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005866 fclose(fd);
5867}
5868
5869
5870/*
5871 * Create a Vim spell file from one or more word lists.
5872 * "fnames[0]" is the output file name.
5873 * "fnames[fcount - 1]" is the last input file name.
5874 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5875 * and ".spl" is appended to make the output file name.
5876 */
5877 void
5878mkspell(
5879 int fcount,
5880 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005881 int ascii, // -ascii argument given
5882 int over_write, // overwrite existing output file
5883 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005884{
5885 char_u *fname = NULL;
5886 char_u *wfname;
5887 char_u **innames;
5888 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005889 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005890 int i;
5891 int len;
5892 stat_T st;
5893 int error = FALSE;
5894 spellinfo_T spin;
5895
Bram Moolenaara80faa82020-04-12 19:37:17 +02005896 CLEAR_FIELD(spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005897 spin.si_verbose = !added_word;
5898 spin.si_ascii = ascii;
5899 spin.si_followup = TRUE;
5900 spin.si_rem_accents = TRUE;
5901 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5902 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
5903 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5904 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
5905 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
5906 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
5907 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005908 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005910 // default: fnames[0] is output file, following are input files
Bram Moolenaar927b7dd2020-06-29 22:24:56 +02005911 // When "fcount" is 1 there is only one file.
5912 innames = &fnames[fcount == 1 ? 0 : 1];
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005913 incount = fcount - 1;
5914
5915 wfname = alloc(MAXPATHL);
5916 if (wfname == NULL)
5917 return;
5918
5919 if (fcount >= 1)
5920 {
5921 len = (int)STRLEN(fnames[0]);
5922 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5923 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005924 // For ":mkspell path/en.latin1.add" output file is
5925 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005926 incount = 1;
5927 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5928 }
5929 else if (fcount == 1)
5930 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005931 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005932 incount = 1;
5933 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5934 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5935 }
5936 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5937 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005938 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005939 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5940 }
5941 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005942 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005943 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5944 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005946 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005947 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5948 spin.si_ascii = TRUE;
5949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005950 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005951 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5952 spin.si_add = TRUE;
5953 }
5954
5955 if (incount <= 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005956 emsg(_(e_invarg)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005957 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005958 emsg(_("E751: Output file name must not have region name"));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005959 else if (incount > MAXREGIONS)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005960 semsg(_("E754: Only up to %d regions supported"), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005961 else
5962 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005963 // Check for overwriting before doing things that may take a lot of
5964 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005965 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
5966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005967 emsg(_(e_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005968 goto theend;
5969 }
5970 if (mch_isdir(wfname))
5971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005972 semsg(_(e_isadir2), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005973 goto theend;
5974 }
5975
5976 fname = alloc(MAXPATHL);
5977 if (fname == NULL)
5978 goto theend;
5979
5980 /*
5981 * Init the aff and dic pointers.
5982 * Get the region names if there are more than 2 arguments.
5983 */
5984 for (i = 0; i < incount; ++i)
5985 {
5986 afile[i] = NULL;
5987
5988 if (incount > 1)
5989 {
5990 len = (int)STRLEN(innames[i]);
5991 if (STRLEN(gettail(innames[i])) < 5
5992 || innames[i][len - 3] != '_')
5993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005994 semsg(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005995 goto theend;
5996 }
5997 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5998 spin.si_region_name[i * 2 + 1] =
5999 TOLOWER_ASC(innames[i][len - 1]);
6000 }
6001 }
6002 spin.si_region_count = incount;
6003
6004 spin.si_foldroot = wordtree_alloc(&spin);
6005 spin.si_keeproot = wordtree_alloc(&spin);
6006 spin.si_prefroot = wordtree_alloc(&spin);
6007 if (spin.si_foldroot == NULL
6008 || spin.si_keeproot == NULL
6009 || spin.si_prefroot == NULL)
6010 {
6011 free_blocks(spin.si_blocks);
6012 goto theend;
6013 }
6014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006015 // When not producing a .add.spl file clear the character table when
6016 // we encounter one in the .aff file. This means we dump the current
6017 // one in the .spl file if the .aff file doesn't define one. That's
6018 // better than guessing the contents, the table will match a
6019 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006020 if (!spin.si_add)
6021 spin.si_clear_chartab = TRUE;
6022
6023 /*
6024 * Read all the .aff and .dic files.
6025 * Text is converted to 'encoding'.
6026 * Words are stored in the case-folded and keep-case trees.
6027 */
6028 for (i = 0; i < incount && !error; ++i)
6029 {
6030 spin.si_conv.vc_type = CONV_NONE;
6031 spin.si_region = 1 << i;
6032
6033 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6034 if (mch_stat((char *)fname, &st) >= 0)
6035 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006036 // Read the .aff file. Will init "spin->si_conv" based on the
6037 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006038 afile[i] = spell_read_aff(&spin, fname);
6039 if (afile[i] == NULL)
6040 error = TRUE;
6041 else
6042 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006043 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006044 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6045 innames[i]);
6046 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6047 error = TRUE;
6048 }
6049 }
6050 else
6051 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006052 // No .aff file, try reading the file as a word list. Store
6053 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006054 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6055 error = TRUE;
6056 }
6057
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006058 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006059 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006060 }
6061
6062 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006063 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006064
6065 if (!error && !got_int)
6066 {
6067 /*
6068 * Combine tails in the tree.
6069 */
6070 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02006071 wordtree_compress(&spin, spin.si_foldroot, "case-folded");
6072 wordtree_compress(&spin, spin.si_keeproot, "keep-case");
6073 wordtree_compress(&spin, spin.si_prefroot, "prefixes");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006074 }
6075
6076 if (!error && !got_int)
6077 {
6078 /*
6079 * Write the info in the spell file.
6080 */
6081 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006082 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006083 spell_message(&spin, IObuff);
6084
6085 error = write_vim_spell(&spin, wfname) == FAIL;
6086
6087 spell_message(&spin, (char_u *)_("Done!"));
6088 vim_snprintf((char *)IObuff, IOSIZE,
6089 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6090 spell_message(&spin, IObuff);
6091
6092 /*
6093 * If the file is loaded need to reload it.
6094 */
6095 if (!error)
6096 spell_reload_one(wfname, added_word);
6097 }
6098
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006099 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006100 ga_clear(&spin.si_rep);
6101 ga_clear(&spin.si_repsal);
6102 ga_clear(&spin.si_sal);
6103 ga_clear(&spin.si_map);
6104 ga_clear(&spin.si_comppat);
6105 ga_clear(&spin.si_prefcond);
6106 hash_clear_all(&spin.si_commonwords, 0);
6107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006108 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006109 for (i = 0; i < incount; ++i)
6110 if (afile[i] != NULL)
6111 spell_free_aff(afile[i]);
6112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006113 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006114 free_blocks(spin.si_blocks);
6115
6116 /*
6117 * If there is soundfolding info and no NOSUGFILE item create the
6118 * .sug file with the soundfolded word trie.
6119 */
6120 if (spin.si_sugtime != 0 && !error && !got_int)
6121 spell_make_sugfile(&spin, wfname);
6122
6123 }
6124
6125theend:
6126 vim_free(fname);
6127 vim_free(wfname);
6128}
6129
6130/*
6131 * Display a message for spell file processing when 'verbose' is set or using
6132 * ":mkspell". "str" can be IObuff.
6133 */
6134 static void
6135spell_message(spellinfo_T *spin, char_u *str)
6136{
6137 if (spin->si_verbose || p_verbose > 2)
6138 {
6139 if (!spin->si_verbose)
6140 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006141 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006142 out_flush();
6143 if (!spin->si_verbose)
6144 verbose_leave();
6145 }
6146}
6147
6148/*
6149 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006150 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006151 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006152 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006153 */
6154 void
6155ex_spell(exarg_T *eap)
6156{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006157 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6158 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6159 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006160 eap->forceit ? 0 : (int)eap->line2,
6161 eap->cmdidx == CMD_spellundo);
6162}
6163
6164/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006165 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006166 */
6167 void
6168spell_add_word(
6169 char_u *word,
6170 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006171 int what, // SPELL_ADD_ values
6172 int idx, // "zG" and "zW": zero, otherwise index in
6173 // 'spellfile'
6174 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006175{
6176 FILE *fd = NULL;
6177 buf_T *buf = NULL;
6178 int new_spf = FALSE;
6179 char_u *fname;
6180 char_u *fnamebuf = NULL;
6181 char_u line[MAXWLEN * 2];
6182 long fpos, fpos_next = 0;
6183 int i;
6184 char_u *spf;
6185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006186 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006187 {
6188 if (int_wordlist == NULL)
6189 {
6190 int_wordlist = vim_tempname('s', FALSE);
6191 if (int_wordlist == NULL)
6192 return;
6193 }
6194 fname = int_wordlist;
6195 }
6196 else
6197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006198 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006199 if (*curwin->w_s->b_p_spf == NUL)
6200 {
6201 init_spellfile();
6202 new_spf = TRUE;
6203 }
6204
6205 if (*curwin->w_s->b_p_spf == NUL)
6206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006207 semsg(_(e_notset), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006208 return;
6209 }
6210 fnamebuf = alloc(MAXPATHL);
6211 if (fnamebuf == NULL)
6212 return;
6213
6214 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6215 {
6216 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6217 if (i == idx)
6218 break;
6219 if (*spf == NUL)
6220 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006221 semsg(_("E765: 'spellfile' does not have %d entries"), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006222 vim_free(fnamebuf);
6223 return;
6224 }
6225 }
6226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006227 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006228 buf = buflist_findname_exp(fnamebuf);
6229 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6230 buf = NULL;
6231 if (buf != NULL && bufIsChanged(buf))
6232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006233 emsg(_(e_bufloaded));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006234 vim_free(fnamebuf);
6235 return;
6236 }
6237
6238 fname = fnamebuf;
6239 }
6240
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006241 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006242 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006243 // When the word appears as good word we need to remove that one,
6244 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006245 fd = mch_fopen((char *)fname, "r");
6246 if (fd != NULL)
6247 {
6248 while (!vim_fgets(line, MAXWLEN * 2, fd))
6249 {
6250 fpos = fpos_next;
6251 fpos_next = ftell(fd);
6252 if (STRNCMP(word, line, len) == 0
6253 && (line[len] == '/' || line[len] < ' '))
6254 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006255 // Found duplicate word. Remove it by writing a '#' at
6256 // the start of the line. Mixing reading and writing
6257 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006258 fclose(fd);
6259 fd = mch_fopen((char *)fname, "r+");
6260 if (fd == NULL)
6261 break;
6262 if (fseek(fd, fpos, SEEK_SET) == 0)
6263 {
6264 fputc('#', fd);
6265 if (undo)
6266 {
6267 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006268 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006269 len, word, NameBuff);
6270 }
6271 }
6272 fseek(fd, fpos_next, SEEK_SET);
6273 }
6274 }
6275 if (fd != NULL)
6276 fclose(fd);
6277 }
6278 }
6279
6280 if (!undo)
6281 {
6282 fd = mch_fopen((char *)fname, "a");
6283 if (fd == NULL && new_spf)
6284 {
6285 char_u *p;
6286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006287 // We just initialized the 'spellfile' option and can't open the
6288 // file. We may need to create the "spell" directory first. We
6289 // already checked the runtime directory is writable in
6290 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006291 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6292 {
6293 int c = *p;
6294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006295 // The directory doesn't exist. Try creating it and opening
6296 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006297 *p = NUL;
6298 vim_mkdir(fname, 0755);
6299 *p = c;
6300 fd = mch_fopen((char *)fname, "a");
6301 }
6302 }
6303
6304 if (fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006305 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006306 else
6307 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006308 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006309 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006310 else if (what == SPELL_ADD_RARE)
6311 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006312 else
6313 fprintf(fd, "%.*s\n", len, word);
6314 fclose(fd);
6315
6316 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006317 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006318 }
6319 }
6320
6321 if (fd != NULL)
6322 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006323 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006324 mkspell(1, &fname, FALSE, TRUE, TRUE);
6325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006326 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006327 if (buf != NULL)
6328 buf_reload(buf, buf->b_orig_mode);
6329
6330 redraw_all_later(SOME_VALID);
6331 }
6332 vim_free(fnamebuf);
6333}
6334
6335/*
6336 * Initialize 'spellfile' for the current buffer.
6337 */
6338 static void
6339init_spellfile(void)
6340{
6341 char_u *buf;
6342 int l;
6343 char_u *fname;
6344 char_u *rtp;
6345 char_u *lend;
6346 int aspath = FALSE;
6347 char_u *lstart = curbuf->b_s.b_p_spl;
6348
6349 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6350 {
6351 buf = alloc(MAXPATHL);
6352 if (buf == NULL)
6353 return;
6354
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006355 // Find the end of the language name. Exclude the region. If there
6356 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006357 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6358 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6359 if (vim_ispathsep(*lend))
6360 {
6361 aspath = TRUE;
6362 lstart = lend + 1;
6363 }
6364
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006365 // Loop over all entries in 'runtimepath'. Use the first one where we
6366 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006367 rtp = p_rtp;
6368 while (*rtp != NUL)
6369 {
6370 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006371 // Use directory of an entry with path, e.g., for
6372 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006373 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6374 lstart - curbuf->b_s.b_p_spl - 1);
6375 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006376 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006377 copy_option_part(&rtp, buf, MAXPATHL, ",");
6378 if (filewritable(buf) == 2)
6379 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006380 // Use the first language name from 'spelllang' and the
6381 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006382 if (aspath)
6383 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6384 lend - curbuf->b_s.b_p_spl);
6385 else
6386 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006387 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006388 l = (int)STRLEN(buf);
6389 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6390 if (filewritable(buf) != 2)
6391 vim_mkdir(buf, 0755);
6392
6393 l = (int)STRLEN(buf);
6394 vim_snprintf((char *)buf + l, MAXPATHL - l,
6395 "/%.*s", (int)(lend - lstart), lstart);
6396 }
6397 l = (int)STRLEN(buf);
6398 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6399 ->lp_slang->sl_fname;
6400 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6401 fname != NULL
6402 && strstr((char *)gettail(fname), ".ascii.") != NULL
6403 ? (char_u *)"ascii" : spell_enc());
6404 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6405 break;
6406 }
6407 aspath = FALSE;
6408 }
6409
6410 vim_free(buf);
6411 }
6412}
6413
6414
6415
6416/*
6417 * Set the spell character tables from strings in the affix file.
6418 */
6419 static int
6420set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6421{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006422 // We build the new tables here first, so that we can compare with the
6423 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006424 spelltab_T new_st;
6425 char_u *pf = fol, *pl = low, *pu = upp;
6426 int f, l, u;
6427
6428 clear_spell_chartab(&new_st);
6429
6430 while (*pf != NUL)
6431 {
6432 if (*pl == NUL || *pu == NUL)
6433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006434 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006435 return FAIL;
6436 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006437 f = mb_ptr2char_adv(&pf);
6438 l = mb_ptr2char_adv(&pl);
6439 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006440
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006441 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006442 if (f < 256)
6443 new_st.st_isw[f] = TRUE;
6444 if (l < 256)
6445 new_st.st_isw[l] = TRUE;
6446 if (u < 256)
6447 new_st.st_isw[u] = TRUE;
6448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006449 // if "LOW" and "FOL" are not the same the "LOW" char needs
6450 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006451 if (l < 256 && l != f)
6452 {
6453 if (f >= 256)
6454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006455 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006456 return FAIL;
6457 }
6458 new_st.st_fold[l] = f;
6459 }
6460
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 // if "UPP" and "FOL" are not the same the "UPP" char needs
6462 // case-folding, it's upper case and the "UPP" is the upper case of
6463 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006464 if (u < 256 && u != f)
6465 {
6466 if (f >= 256)
6467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006468 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006469 return FAIL;
6470 }
6471 new_st.st_fold[u] = f;
6472 new_st.st_isu[u] = TRUE;
6473 new_st.st_upper[f] = u;
6474 }
6475 }
6476
6477 if (*pl != NUL || *pu != NUL)
6478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006479 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006480 return FAIL;
6481 }
6482
6483 return set_spell_finish(&new_st);
6484}
6485
6486/*
6487 * Set the spell character tables from strings in the .spl file.
6488 */
6489 static void
6490set_spell_charflags(
6491 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006492 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006493 char_u *fol)
6494{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006495 // We build the new tables here first, so that we can compare with the
6496 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006497 spelltab_T new_st;
6498 int i;
6499 char_u *p = fol;
6500 int c;
6501
6502 clear_spell_chartab(&new_st);
6503
6504 for (i = 0; i < 128; ++i)
6505 {
6506 if (i < cnt)
6507 {
6508 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6509 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6510 }
6511
6512 if (*p != NUL)
6513 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006514 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006515 new_st.st_fold[i + 128] = c;
6516 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6517 new_st.st_upper[c] = i + 128;
6518 }
6519 }
6520
6521 (void)set_spell_finish(&new_st);
6522}
6523
6524 static int
6525set_spell_finish(spelltab_T *new_st)
6526{
6527 int i;
6528
6529 if (did_set_spelltab)
6530 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006531 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006532 for (i = 0; i < 256; ++i)
6533 {
6534 if (spelltab.st_isw[i] != new_st->st_isw[i]
6535 || spelltab.st_isu[i] != new_st->st_isu[i]
6536 || spelltab.st_fold[i] != new_st->st_fold[i]
6537 || spelltab.st_upper[i] != new_st->st_upper[i])
6538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006539 emsg(_("E763: Word characters differ between spell files"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006540 return FAIL;
6541 }
6542 }
6543 }
6544 else
6545 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006546 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006547 spelltab = *new_st;
6548 did_set_spelltab = TRUE;
6549 }
6550
6551 return OK;
6552}
6553
6554/*
6555 * Write the table with prefix conditions to the .spl file.
6556 * When "fd" is NULL only count the length of what is written.
6557 */
6558 static int
6559write_spell_prefcond(FILE *fd, garray_T *gap)
6560{
6561 int i;
6562 char_u *p;
6563 int len;
6564 int totlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006565 size_t x = 1; // collect return value of fwrite()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006566
6567 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006568 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006569
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006570 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006571
6572 for (i = 0; i < gap->ga_len; ++i)
6573 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006574 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006575 p = ((char_u **)gap->ga_data)[i];
6576 if (p != NULL)
6577 {
6578 len = (int)STRLEN(p);
6579 if (fd != NULL)
6580 {
6581 fputc(len, fd);
6582 x &= fwrite(p, (size_t)len, (size_t)1, fd);
6583 }
6584 totlen += len;
6585 }
6586 else if (fd != NULL)
6587 fputc(0, fd);
6588 }
6589
6590 return totlen;
6591}
6592
6593
6594/*
6595 * Use map string "map" for languages "lp".
6596 */
6597 static void
6598set_map_str(slang_T *lp, char_u *map)
6599{
6600 char_u *p;
6601 int headc = 0;
6602 int c;
6603 int i;
6604
6605 if (*map == NUL)
6606 {
6607 lp->sl_has_map = FALSE;
6608 return;
6609 }
6610 lp->sl_has_map = TRUE;
6611
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006612 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006613 for (i = 0; i < 256; ++i)
6614 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006615 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006616
6617 /*
6618 * The similar characters are stored separated with slashes:
6619 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6620 * before the same slash. For characters above 255 sl_map_hash is used.
6621 */
6622 for (p = map; *p != NUL; )
6623 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006624 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006625 if (c == '/')
6626 headc = 0;
6627 else
6628 {
6629 if (headc == 0)
6630 headc = c;
6631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006632 // Characters above 255 don't fit in sl_map_array[], put them in
6633 // the hash table. Each entry is the char, a NUL the headchar and
6634 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006635 if (c >= 256)
6636 {
6637 int cl = mb_char2len(c);
6638 int headcl = mb_char2len(headc);
6639 char_u *b;
6640 hash_T hash;
6641 hashitem_T *hi;
6642
Bram Moolenaar964b3742019-05-24 18:54:09 +02006643 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006644 if (b == NULL)
6645 return;
6646 mb_char2bytes(c, b);
6647 b[cl] = NUL;
6648 mb_char2bytes(headc, b + cl + 1);
6649 b[cl + 1 + headcl] = NUL;
6650 hash = hash_hash(b);
6651 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6652 if (HASHITEM_EMPTY(hi))
6653 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6654 else
6655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006656 // This should have been checked when generating the .spl
6657 // file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006658 emsg(_("E783: duplicate char in MAP entry"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006659 vim_free(b);
6660 }
6661 }
6662 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006663 lp->sl_map_array[c] = headc;
6664 }
6665 }
6666}
6667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006668#endif // FEAT_SPELL