blob: 6aeac86b858477ffbadd414ea288bd09949be437 [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 Moolenaar07399e72020-08-24 20:05:50 +0200318static int spell_read_tree(FILE *fd, char_u **bytsp, long *bytsp_len, idx_T **idxsp, int prefixtree, int prefixcnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200319static 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;
Bram Moolenaarfc2a47f2020-08-20 15:41:55 +0200522 if (init_syl_tab(lp) != OK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200523 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 Moolenaar07399e72020-08-24 20:05:50 +0200556 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fbyts_len,
557 &lp->sl_fidxs, FALSE, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200558 if (res != 0)
559 goto someerror;
560
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100561 // <KWORDTREE>
Bram Moolenaar07399e72020-08-24 20:05:50 +0200562 res = spell_read_tree(fd, &lp->sl_kbyts, NULL, &lp->sl_kidxs, FALSE, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200563 if (res != 0)
564 goto someerror;
565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100566 // <PREFIXTREE>
Bram Moolenaar07399e72020-08-24 20:05:50 +0200567 res = spell_read_tree(fd, &lp->sl_pbyts, NULL, &lp->sl_pidxs, TRUE,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200568 lp->sl_prefixcnt);
569 if (res != 0)
570 goto someerror;
571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100572 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200573 if (old_lp == NULL && lang != NULL)
574 {
575 lp->sl_next = first_lang;
576 first_lang = lp;
577 }
578
579 goto endOK;
580
581endFAIL:
582 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100583 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200584 *lang = NUL;
585 if (lp != NULL && old_lp == NULL)
586 slang_free(lp);
587 lp = NULL;
588
589endOK:
590 if (fd != NULL)
591 fclose(fd);
Bram Moolenaarce6db022020-01-07 20:11:42 +0100592 if (did_estack_push)
Bram Moolenaare31ee862020-01-07 20:59:34 +0100593 {
594 ESTACK_CHECK_NOW
Bram Moolenaarce6db022020-01-07 20:11:42 +0100595 estack_pop();
Bram Moolenaare31ee862020-01-07 20:59:34 +0100596 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200597
598 return lp;
599}
600
601/*
602 * Fill in the wordcount fields for a trie.
603 * Returns the total number of words.
604 */
605 static void
606tree_count_words(char_u *byts, idx_T *idxs)
607{
608 int depth;
609 idx_T arridx[MAXWLEN];
610 int curi[MAXWLEN];
611 int c;
612 idx_T n;
613 int wordcount[MAXWLEN];
614
615 arridx[0] = 0;
616 curi[0] = 1;
617 wordcount[0] = 0;
618 depth = 0;
619 while (depth >= 0 && !got_int)
620 {
621 if (curi[depth] > byts[arridx[depth]])
622 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100623 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200624 idxs[arridx[depth]] = wordcount[depth];
625 if (depth > 0)
626 wordcount[depth - 1] += wordcount[depth];
627
628 --depth;
629 fast_breakcheck();
630 }
631 else
632 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100633 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200634 n = arridx[depth] + curi[depth];
635 ++curi[depth];
636
637 c = byts[n];
638 if (c == 0)
639 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100640 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200641 ++wordcount[depth];
642
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100643 // Skip over any other NUL bytes (same word with different
644 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200645 while (byts[n + 1] == 0)
646 {
647 ++n;
648 ++curi[depth];
649 }
650 }
651 else
652 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100653 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200654 ++depth;
655 arridx[depth] = idxs[n];
656 curi[depth] = 1;
657 wordcount[depth] = 0;
658 }
659 }
660 }
661}
662
663/*
664 * Load the .sug files for languages that have one and weren't loaded yet.
665 */
666 void
667suggest_load_files(void)
668{
669 langp_T *lp;
670 int lpi;
671 slang_T *slang;
672 char_u *dotp;
673 FILE *fd;
674 char_u buf[MAXWLEN];
675 int i;
676 time_t timestamp;
677 int wcount;
678 int wordnr;
679 garray_T ga;
680 int c;
681
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100682 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200683 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
684 {
685 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
686 slang = lp->lp_slang;
687 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 // Change ".spl" to ".sug" and open the file. When the file isn't
690 // found silently skip it. Do set "sl_sugloaded" so that we
691 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200692 slang->sl_sugloaded = TRUE;
693
694 dotp = vim_strrchr(slang->sl_fname, '.');
695 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
696 continue;
697 STRCPY(dotp, ".sug");
698 fd = mch_fopen((char *)slang->sl_fname, "r");
699 if (fd == NULL)
700 goto nextone;
701
702 /*
703 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
704 */
705 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200707 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100709 semsg(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200710 slang->sl_fname);
711 goto nextone;
712 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100713 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200714 if (c < VIMSUGVERSION)
715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100716 semsg(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200717 slang->sl_fname);
718 goto nextone;
719 }
720 else if (c > VIMSUGVERSION)
721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100722 semsg(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200723 slang->sl_fname);
724 goto nextone;
725 }
726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100727 // Check the timestamp, it must be exactly the same as the one in
728 // the .spl file. Otherwise the word numbers won't match.
729 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200730 if (timestamp != slang->sl_sugtime)
731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100732 semsg(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200733 slang->sl_fname);
734 goto nextone;
735 }
736
737 /*
738 * <SUGWORDTREE>: <wordtree>
739 * Read the trie with the soundfolded words.
740 */
Bram Moolenaar07399e72020-08-24 20:05:50 +0200741 if (spell_read_tree(fd, &slang->sl_sbyts, NULL, &slang->sl_sidxs,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200742 FALSE, 0) != 0)
743 {
744someerror:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100745 semsg(_("E782: error while reading .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200746 slang->sl_fname);
747 slang_clear_sug(slang);
748 goto nextone;
749 }
750
751 /*
752 * <SUGTABLE>: <sugwcount> <sugline> ...
753 *
754 * Read the table with word numbers. We use a file buffer for
755 * this, because it's so much like a file with lines. Makes it
756 * possible to swap the info and save on memory use.
757 */
758 slang->sl_sugbuf = open_spellbuf();
759 if (slang->sl_sugbuf == NULL)
760 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100761 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200762 wcount = get4c(fd);
763 if (wcount < 0)
764 goto someerror;
765
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100766 // Read all the wordnr lists into the buffer, one NUL terminated
767 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200768 ga_init2(&ga, 1, 100);
769 for (wordnr = 0; wordnr < wcount; ++wordnr)
770 {
771 ga.ga_len = 0;
772 for (;;)
773 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100774 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200775 if (c < 0 || ga_grow(&ga, 1) == FAIL)
776 goto someerror;
777 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
778 if (c == NUL)
779 break;
780 }
781 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
782 ga.ga_data, ga.ga_len, TRUE) == FAIL)
783 goto someerror;
784 }
785 ga_clear(&ga);
786
787 /*
788 * Need to put word counts in the word tries, so that we can find
789 * a word by its number.
790 */
791 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
792 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
793
794nextone:
795 if (fd != NULL)
796 fclose(fd);
797 STRCPY(dotp, ".spl");
798 }
799 }
800}
801
802
803/*
804 * Read a length field from "fd" in "cnt_bytes" bytes.
805 * Allocate memory, read the string into it and add a NUL at the end.
806 * Returns NULL when the count is zero.
807 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
808 * otherwise.
809 */
810 static char_u *
811read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
812{
813 int cnt = 0;
814 int i;
815 char_u *str;
816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100817 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200818 for (i = 0; i < cnt_bytes; ++i)
819 cnt = (cnt << 8) + getc(fd);
820 if (cnt < 0)
821 {
822 *cntp = SP_TRUNCERROR;
823 return NULL;
824 }
825 *cntp = cnt;
826 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100827 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200828
829 str = read_string(fd, cnt);
830 if (str == NULL)
831 *cntp = SP_OTHERERROR;
832 return str;
833}
834
835/*
836 * Read SN_REGION: <regionname> ...
837 * Return SP_*ERROR flags.
838 */
839 static int
840read_region_section(FILE *fd, slang_T *lp, int len)
841{
842 int i;
843
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100844 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200845 return SP_FORMERROR;
846 for (i = 0; i < len; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100847 lp->sl_regions[i] = getc(fd); // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200848 lp->sl_regions[len] = NUL;
849 return 0;
850}
851
852/*
853 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
854 * <folcharslen> <folchars>
855 * Return SP_*ERROR flags.
856 */
857 static int
858read_charflags_section(FILE *fd)
859{
860 char_u *flags;
861 char_u *fol;
862 int flagslen, follen;
863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100864 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200865 flags = read_cnt_string(fd, 1, &flagslen);
866 if (flagslen < 0)
867 return flagslen;
868
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100869 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200870 fol = read_cnt_string(fd, 2, &follen);
871 if (follen < 0)
872 {
873 vim_free(flags);
874 return follen;
875 }
876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100877 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200878 if (flags != NULL && fol != NULL)
879 set_spell_charflags(flags, flagslen, fol);
880
881 vim_free(flags);
882 vim_free(fol);
883
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100884 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200885 if ((flags == NULL) != (fol == NULL))
886 return SP_FORMERROR;
887 return 0;
888}
889
890/*
891 * Read SN_PREFCOND section.
892 * Return SP_*ERROR flags.
893 */
894 static int
895read_prefcond_section(FILE *fd, slang_T *lp)
896{
897 int cnt;
898 int i;
899 int n;
900 char_u *p;
901 char_u buf[MAXWLEN + 1];
902
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100903 // <prefcondcnt> <prefcond> ...
904 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200905 if (cnt <= 0)
906 return SP_FORMERROR;
907
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200908 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200909 if (lp->sl_prefprog == NULL)
910 return SP_OTHERERROR;
911 lp->sl_prefixcnt = cnt;
912
913 for (i = 0; i < cnt; ++i)
914 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100915 // <prefcond> : <condlen> <condstr>
916 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200917 if (n < 0 || n >= MAXWLEN)
918 return SP_FORMERROR;
919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100920 // When <condlen> is zero we have an empty condition. Otherwise
921 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200922 if (n > 0)
923 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100924 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200925 p = buf + 1;
926 while (n-- > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100927 *p++ = getc(fd); // <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200928 *p = NUL;
929 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
930 }
931 }
932 return 0;
933}
934
935/*
936 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
937 * Return SP_*ERROR flags.
938 */
939 static int
940read_rep_section(FILE *fd, garray_T *gap, short *first)
941{
942 int cnt;
943 fromto_T *ftp;
944 int i;
945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100946 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200947 if (cnt < 0)
948 return SP_TRUNCERROR;
949
950 if (ga_grow(gap, cnt) == FAIL)
951 return SP_OTHERERROR;
952
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100953 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200954 for (; gap->ga_len < cnt; ++gap->ga_len)
955 {
956 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
957 ftp->ft_from = read_cnt_string(fd, 1, &i);
958 if (i < 0)
959 return i;
960 if (i == 0)
961 return SP_FORMERROR;
962 ftp->ft_to = read_cnt_string(fd, 1, &i);
963 if (i <= 0)
964 {
965 vim_free(ftp->ft_from);
966 if (i < 0)
967 return i;
968 return SP_FORMERROR;
969 }
970 }
971
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100972 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200973 for (i = 0; i < 256; ++i)
974 first[i] = -1;
975 for (i = 0; i < gap->ga_len; ++i)
976 {
977 ftp = &((fromto_T *)gap->ga_data)[i];
978 if (first[*ftp->ft_from] == -1)
979 first[*ftp->ft_from] = i;
980 }
981 return 0;
982}
983
984/*
985 * Read SN_SAL section: <salflags> <salcount> <sal> ...
986 * Return SP_*ERROR flags.
987 */
988 static int
989read_sal_section(FILE *fd, slang_T *slang)
990{
991 int i;
992 int cnt;
993 garray_T *gap;
994 salitem_T *smp;
995 int ccnt;
996 char_u *p;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200997
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 {
Bram Moolenaar97d2f342020-07-10 20:03:03 +02001020 int c = NUL;
1021
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001022 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001023 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001024 if (ccnt < 0)
1025 return SP_TRUNCERROR;
1026 if ((p = alloc(ccnt + 2)) == NULL)
1027 return SP_OTHERERROR;
1028 smp->sm_lead = p;
1029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001030 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001031 for (i = 0; i < ccnt; ++i)
1032 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001033 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001034 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1035 break;
1036 *p++ = c;
1037 }
1038 smp->sm_leadlen = (int)(p - smp->sm_lead);
1039 *p++ = NUL;
1040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001041 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001042 if (c == '(')
1043 {
1044 smp->sm_oneof = p;
1045 for (++i; i < ccnt; ++i)
1046 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001047 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001048 if (c == ')')
1049 break;
1050 *p++ = c;
1051 }
1052 *p++ = NUL;
1053 if (++i < ccnt)
1054 c = getc(fd);
1055 }
1056 else
1057 smp->sm_oneof = NULL;
1058
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001059 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001060 smp->sm_rules = p;
1061 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001062 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001063 *p++ = c;
1064 for (++i; i < ccnt; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001065 *p++ = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001066 *p++ = NUL;
1067
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001068 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001069 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1070 if (ccnt < 0)
1071 {
1072 vim_free(smp->sm_lead);
1073 return ccnt;
1074 }
1075
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001076 if (has_mbyte)
1077 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001078 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001079 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1080 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1081 if (smp->sm_oneof == NULL)
1082 smp->sm_oneof_w = NULL;
1083 else
1084 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1085 if (smp->sm_to == NULL)
1086 smp->sm_to_w = NULL;
1087 else
1088 smp->sm_to_w = mb_str2wide(smp->sm_to);
1089 if (smp->sm_lead_w == NULL
1090 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1091 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1092 {
1093 vim_free(smp->sm_lead);
1094 vim_free(smp->sm_to);
1095 vim_free(smp->sm_lead_w);
1096 vim_free(smp->sm_oneof_w);
1097 vim_free(smp->sm_to_w);
1098 return SP_OTHERERROR;
1099 }
1100 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001101 }
1102
1103 if (gap->ga_len > 0)
1104 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001105 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1106 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001107 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1108 if ((p = alloc(1)) == NULL)
1109 return SP_OTHERERROR;
1110 p[0] = NUL;
1111 smp->sm_lead = p;
1112 smp->sm_leadlen = 0;
1113 smp->sm_oneof = NULL;
1114 smp->sm_rules = p;
1115 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001116 if (has_mbyte)
1117 {
1118 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1119 smp->sm_leadlen = 0;
1120 smp->sm_oneof_w = NULL;
1121 smp->sm_to_w = NULL;
1122 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001123 ++gap->ga_len;
1124 }
1125
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001126 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001127 set_sal_first(slang);
1128
1129 return 0;
1130}
1131
1132/*
1133 * Read SN_WORDS: <word> ...
1134 * Return SP_*ERROR flags.
1135 */
1136 static int
1137read_words_section(FILE *fd, slang_T *lp, int len)
1138{
1139 int done = 0;
1140 int i;
1141 int c;
1142 char_u word[MAXWLEN];
1143
1144 while (done < len)
1145 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001146 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001147 for (i = 0; ; ++i)
1148 {
1149 c = getc(fd);
1150 if (c == EOF)
1151 return SP_TRUNCERROR;
1152 word[i] = c;
1153 if (word[i] == NUL)
1154 break;
1155 if (i == MAXWLEN - 1)
1156 return SP_FORMERROR;
1157 }
1158
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001159 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001160 count_common_word(lp, word, -1, 10);
1161 done += i + 1;
1162 }
1163 return 0;
1164}
1165
1166/*
1167 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1168 * Return SP_*ERROR flags.
1169 */
1170 static int
1171read_sofo_section(FILE *fd, slang_T *slang)
1172{
1173 int cnt;
1174 char_u *from, *to;
1175 int res;
1176
1177 slang->sl_sofo = TRUE;
1178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001179 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001180 from = read_cnt_string(fd, 2, &cnt);
1181 if (cnt < 0)
1182 return cnt;
1183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001184 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001185 to = read_cnt_string(fd, 2, &cnt);
1186 if (cnt < 0)
1187 {
1188 vim_free(from);
1189 return cnt;
1190 }
1191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001192 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001193 if (from != NULL && to != NULL)
1194 res = set_sofo(slang, from, to);
1195 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001196 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001197 else
1198 res = 0;
1199
1200 vim_free(from);
1201 vim_free(to);
1202 return res;
1203}
1204
1205/*
1206 * Read the compound section from the .spl file:
1207 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1208 * Returns SP_*ERROR flags.
1209 */
1210 static int
1211read_compound(FILE *fd, slang_T *slang, int len)
1212{
1213 int todo = len;
1214 int c;
1215 int atstart;
1216 char_u *pat;
1217 char_u *pp;
1218 char_u *cp;
1219 char_u *ap;
1220 char_u *crp;
1221 int cnt;
1222 garray_T *gap;
1223
1224 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001225 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001226
1227 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001228 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001229 if (c < 2)
1230 c = MAXWLEN;
1231 slang->sl_compmax = c;
1232
1233 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001234 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001235 if (c < 1)
1236 c = 0;
1237 slang->sl_compminlen = c;
1238
1239 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001240 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001241 if (c < 1)
1242 c = MAXWLEN;
1243 slang->sl_compsylmax = c;
1244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001245 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001246 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001247 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001248 else
1249 {
1250 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001251 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001252 --todo;
1253 slang->sl_compoptions = c;
1254
1255 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001256 c = get2c(fd); // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001257 todo -= 2;
1258 ga_init2(gap, sizeof(char_u *), c);
1259 if (ga_grow(gap, c) == OK)
1260 while (--c >= 0)
1261 {
1262 ((char_u **)(gap->ga_data))[gap->ga_len++] =
1263 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001264 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001265 if (cnt < 0)
1266 return cnt;
1267 todo -= cnt + 1;
1268 }
1269 }
1270 if (todo < 0)
1271 return SP_FORMERROR;
1272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001273 // Turn the COMPOUNDRULE items into a regexp pattern:
1274 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1275 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1276 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001277 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001278 if (enc_utf8)
1279 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001280 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001281 if (pat == NULL)
1282 return SP_OTHERERROR;
1283
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001284 // We also need a list of all flags that can appear at the start and one
1285 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001286 cp = alloc(todo + 1);
1287 if (cp == NULL)
1288 {
1289 vim_free(pat);
1290 return SP_OTHERERROR;
1291 }
1292 slang->sl_compstartflags = cp;
1293 *cp = NUL;
1294
1295 ap = alloc(todo + 1);
1296 if (ap == NULL)
1297 {
1298 vim_free(pat);
1299 return SP_OTHERERROR;
1300 }
1301 slang->sl_compallflags = ap;
1302 *ap = NUL;
1303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001304 // And a list of all patterns in their original form, for checking whether
1305 // compounding may work in match_compoundrule(). This is freed when we
1306 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001307 crp = alloc(todo + 1);
1308 slang->sl_comprules = crp;
1309
1310 pp = pat;
1311 *pp++ = '^';
1312 *pp++ = '\\';
1313 *pp++ = '(';
1314
1315 atstart = 1;
1316 while (todo-- > 0)
1317 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001318 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001319 if (c == EOF)
1320 {
1321 vim_free(pat);
1322 return SP_TRUNCERROR;
1323 }
1324
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001325 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001326 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1327 && !byte_in_str(slang->sl_compallflags, c))
1328 {
1329 *ap++ = c;
1330 *ap = NUL;
1331 }
1332
1333 if (atstart != 0)
1334 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001335 // At start of item: copy flags to "sl_compstartflags". For a
1336 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001337 if (c == '[')
1338 atstart = 2;
1339 else if (c == ']')
1340 atstart = 0;
1341 else
1342 {
1343 if (!byte_in_str(slang->sl_compstartflags, c))
1344 {
1345 *cp++ = c;
1346 *cp = NUL;
1347 }
1348 if (atstart == 1)
1349 atstart = 0;
1350 }
1351 }
1352
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001353 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001354 if (crp != NULL)
1355 {
1356 if (c == '?' || c == '+' || c == '*')
1357 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001358 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001359 crp = NULL;
1360 }
1361 else
1362 *crp++ = c;
1363 }
1364
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001365 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001366 {
1367 *pp++ = '\\';
1368 *pp++ = '|';
1369 atstart = 1;
1370 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001371 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001372 {
1373 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001374 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001375 if (enc_utf8)
1376 pp += mb_char2bytes(c, pp);
1377 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001378 *pp++ = c;
1379 }
1380 }
1381
1382 *pp++ = '\\';
1383 *pp++ = ')';
1384 *pp++ = '$';
1385 *pp = NUL;
1386
1387 if (crp != NULL)
1388 *crp = NUL;
1389
1390 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1391 vim_free(pat);
1392 if (slang->sl_compprog == NULL)
1393 return SP_FORMERROR;
1394
1395 return 0;
1396}
1397
1398/*
1399 * Set the SOFOFROM and SOFOTO items in language "lp".
1400 * Returns SP_*ERROR flags when there is something wrong.
1401 */
1402 static int
1403set_sofo(slang_T *lp, char_u *from, char_u *to)
1404{
1405 int i;
1406
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001407 garray_T *gap;
1408 char_u *s;
1409 char_u *p;
1410 int c;
1411 int *inp;
1412
1413 if (has_mbyte)
1414 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001415 // Use "sl_sal" as an array with 256 pointers to a list of wide
1416 // characters. The index is the low byte of the character.
1417 // The list contains from-to pairs with a terminating NUL.
1418 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001419 gap = &lp->sl_sal;
1420 ga_init2(gap, sizeof(int *), 1);
1421 if (ga_grow(gap, 256) == FAIL)
1422 return SP_OTHERERROR;
1423 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1424 gap->ga_len = 256;
1425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426 // First count the number of items for each list. Temporarily use
1427 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001428 for (p = from, s = to; *p != NUL && *s != NUL; )
1429 {
1430 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001431 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001432 if (c >= 256)
1433 ++lp->sl_sal_first[c & 0xff];
1434 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001435 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001436 return SP_FORMERROR;
1437
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001438 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001439 for (i = 0; i < 256; ++i)
1440 if (lp->sl_sal_first[i] > 0)
1441 {
1442 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1443 if (p == NULL)
1444 return SP_OTHERERROR;
1445 ((int **)gap->ga_data)[i] = (int *)p;
1446 *(int *)p = 0;
1447 }
1448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001449 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1450 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001451 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1452 for (p = from, s = to; *p != NUL && *s != NUL; )
1453 {
1454 c = mb_cptr2char_adv(&p);
1455 i = mb_cptr2char_adv(&s);
1456 if (c >= 256)
1457 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001458 // Append the from-to chars at the end of the list with
1459 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001460 inp = ((int **)gap->ga_data)[c & 0xff];
1461 while (*inp != 0)
1462 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001463 *inp++ = c; // from char
1464 *inp++ = i; // to char
1465 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001466 }
1467 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001468 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001469 lp->sl_sal_first[c] = i;
1470 }
1471 }
1472 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001473 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001474 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001475 if (STRLEN(from) != STRLEN(to))
1476 return SP_FORMERROR;
1477
1478 for (i = 0; to[i] != NUL; ++i)
1479 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001480 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001481 }
1482
1483 return 0;
1484}
1485
1486/*
1487 * Fill the first-index table for "lp".
1488 */
1489 static void
1490set_sal_first(slang_T *lp)
1491{
1492 salfirst_T *sfirst;
1493 int i;
1494 salitem_T *smp;
1495 int c;
1496 garray_T *gap = &lp->sl_sal;
1497
1498 sfirst = lp->sl_sal_first;
1499 for (i = 0; i < 256; ++i)
1500 sfirst[i] = -1;
1501 smp = (salitem_T *)gap->ga_data;
1502 for (i = 0; i < gap->ga_len; ++i)
1503 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001504 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001505 // Use the lowest byte of the first character. For latin1 it's
1506 // the character, for other encodings it should differ for most
1507 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001508 c = *smp[i].sm_lead_w & 0xff;
1509 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001510 c = *smp[i].sm_lead;
1511 if (sfirst[c] == -1)
1512 {
1513 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001514 if (has_mbyte)
1515 {
1516 int n;
1517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001518 // Make sure all entries with this byte are following each
1519 // other. Move the ones that are in the wrong position. Do
1520 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001521 while (i + 1 < gap->ga_len
1522 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001523 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001524 ++i;
1525
1526 for (n = 1; i + n < gap->ga_len; ++n)
1527 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1528 {
1529 salitem_T tsal;
1530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001531 // Move entry with same index byte after the entries
1532 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001533 ++i;
1534 --n;
1535 tsal = smp[i + n];
1536 mch_memmove(smp + i + 1, smp + i,
1537 sizeof(salitem_T) * n);
1538 smp[i] = tsal;
1539 }
1540 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001541 }
1542 }
1543}
1544
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001545/*
1546 * Turn a multi-byte string into a wide character string.
1547 * Return it in allocated memory (NULL for out-of-memory)
1548 */
1549 static int *
1550mb_str2wide(char_u *s)
1551{
1552 int *res;
1553 char_u *p;
1554 int i = 0;
1555
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001556 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001557 if (res != NULL)
1558 {
1559 for (p = s; *p != NUL; )
1560 res[i++] = mb_ptr2char_adv(&p);
1561 res[i] = NUL;
1562 }
1563 return res;
1564}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001565
1566/*
1567 * Read a tree from the .spl or .sug file.
1568 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1569 * This is skipped when the tree has zero length.
1570 * Returns zero when OK, SP_ value for an error.
1571 */
1572 static int
1573spell_read_tree(
1574 FILE *fd,
1575 char_u **bytsp,
Bram Moolenaar07399e72020-08-24 20:05:50 +02001576 long *bytsp_len,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001577 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001578 int prefixtree, // TRUE for the prefix tree
1579 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001580{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001581 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001582 int idx;
1583 char_u *bp;
1584 idx_T *ip;
1585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001586 // The tree size was computed when writing the file, so that we can
1587 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001588 len = get4c(fd);
1589 if (len < 0)
1590 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001591 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001592 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001593 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001594 if (len > 0)
1595 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001596 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001597 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001598 if (bp == NULL)
1599 return SP_OTHERERROR;
1600 *bytsp = bp;
Bram Moolenaar07399e72020-08-24 20:05:50 +02001601 if (bytsp_len != NULL)
1602 *bytsp_len = len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001603
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001604 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001605 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001606 if (ip == NULL)
1607 return SP_OTHERERROR;
1608 *idxsp = ip;
1609
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001610 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001611 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1612 if (idx < 0)
1613 return idx;
1614 }
1615 return 0;
1616}
1617
1618/*
1619 * Read one row of siblings from the spell file and store it in the byte array
1620 * "byts" and index array "idxs". Recursively read the children.
1621 *
1622 * NOTE: The code here must match put_node()!
1623 *
1624 * Returns the index (>= 0) following the siblings.
1625 * Returns SP_TRUNCERROR if the file is shorter than expected.
1626 * Returns SP_FORMERROR if there is a format error.
1627 */
1628 static idx_T
1629read_tree_node(
1630 FILE *fd,
1631 char_u *byts,
1632 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001633 int maxidx, // size of arrays
1634 idx_T startidx, // current index in "byts" and "idxs"
1635 int prefixtree, // TRUE for reading PREFIXTREE
1636 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001637{
1638 int len;
1639 int i;
1640 int n;
1641 idx_T idx = startidx;
1642 int c;
1643 int c2;
1644#define SHARED_MASK 0x8000000
1645
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001646 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001647 if (len <= 0)
1648 return SP_TRUNCERROR;
1649
1650 if (startidx + len >= maxidx)
1651 return SP_FORMERROR;
1652 byts[idx++] = len;
1653
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001654 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001655 for (i = 1; i <= len; ++i)
1656 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001657 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001658 if (c < 0)
1659 return SP_TRUNCERROR;
1660 if (c <= BY_SPECIAL)
1661 {
1662 if (c == BY_NOFLAGS && !prefixtree)
1663 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001664 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001665 idxs[idx] = 0;
1666 c = 0;
1667 }
1668 else if (c != BY_INDEX)
1669 {
1670 if (prefixtree)
1671 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001672 // Read the optional pflags byte, the prefix ID and the
1673 // condition nr. In idxs[] store the prefix ID in the low
1674 // byte, the condition index shifted up 8 bits, the flags
1675 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001676 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001677 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001678 else
1679 c = 0;
1680
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001681 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001682
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001683 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001684 if (n >= maxprefcondnr)
1685 return SP_FORMERROR;
1686 c |= (n << 8);
1687 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001688 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001689 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 // Read flags and optional region and prefix ID. In
1691 // idxs[] the flags go in the low two bytes, region above
1692 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001693 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001694 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001695 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001696 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001697 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001698 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001699 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001700 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001701 }
1702
1703 idxs[idx] = c;
1704 c = 0;
1705 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001706 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001707 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001708 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001709 n = get3c(fd);
1710 if (n < 0 || n >= maxidx)
1711 return SP_FORMERROR;
1712 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001713 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001714 }
1715 }
1716 byts[idx++] = c;
1717 }
1718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001719 // Recursively read the children for non-shared siblings.
1720 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1721 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001722 for (i = 1; i <= len; ++i)
1723 if (byts[startidx + i] != 0)
1724 {
1725 if (idxs[startidx + i] & SHARED_MASK)
1726 idxs[startidx + i] &= ~SHARED_MASK;
1727 else
1728 {
1729 idxs[startidx + i] = idx;
1730 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1731 prefixtree, maxprefcondnr);
1732 if (idx < 0)
1733 break;
1734 }
1735 }
1736
1737 return idx;
1738}
1739
1740/*
1741 * Reload the spell file "fname" if it's loaded.
1742 */
1743 static void
1744spell_reload_one(
1745 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001746 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001747{
1748 slang_T *slang;
1749 int didit = FALSE;
1750
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001751 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001752 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001753 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001754 {
1755 slang_clear(slang);
1756 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001757 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001758 slang_clear(slang);
1759 redraw_all_later(SOME_VALID);
1760 didit = TRUE;
1761 }
1762 }
1763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 // When "zg" was used and the file wasn't loaded yet, should redo
1765 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001766 if (added_word && !didit)
1767 did_set_spelllang(curwin);
1768}
1769
1770
1771/*
1772 * Functions for ":mkspell".
1773 */
1774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001775#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1776 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001777/*
1778 * Main structure to store the contents of a ".aff" file.
1779 */
1780typedef struct afffile_S
1781{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001782 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1783 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1784 unsigned af_rare; // RARE ID for rare word
1785 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1786 unsigned af_bad; // BAD ID for banned word
1787 unsigned af_needaffix; // NEEDAFFIX ID
1788 unsigned af_circumfix; // CIRCUMFIX ID
1789 unsigned af_needcomp; // NEEDCOMPOUND ID
1790 unsigned af_comproot; // COMPOUNDROOT ID
1791 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1792 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1793 unsigned af_nosuggest; // NOSUGGEST ID
1794 int af_pfxpostpone; // postpone prefixes without chop string and
1795 // without flags
1796 int af_ignoreextra; // IGNOREEXTRA present
1797 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1798 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1799 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001800} afffile_T;
1801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001802#define AFT_CHAR 0 // flags are one character
1803#define AFT_LONG 1 // flags are two characters
1804#define AFT_CAPLONG 2 // flags are one or two characters
1805#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001806
1807typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001808// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001809struct affentry_S
1810{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001811 affentry_T *ae_next; // next affix with same name/number
1812 char_u *ae_chop; // text to chop off basic word (can be NULL)
1813 char_u *ae_add; // text to add to basic word (can be NULL)
1814 char_u *ae_flags; // flags on the affix (can be NULL)
1815 char_u *ae_cond; // condition (NULL for ".")
1816 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1817 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1818 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001819};
1820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001821#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001823// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001824typedef struct affheader_S
1825{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001826 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1827 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1828 int ah_newID; // prefix ID after renumbering; 0 if not used
1829 int ah_combine; // suffix may combine with prefix
1830 int ah_follows; // another affix block should be following
1831 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001832} affheader_T;
1833
1834#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001836// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001837typedef struct compitem_S
1838{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001839 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1840 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1841 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001842} compitem_T;
1843
1844#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1845
1846/*
1847 * Structure that is used to store the items in the word tree. This avoids
1848 * the need to keep track of each allocated thing, everything is freed all at
1849 * once after ":mkspell" is done.
1850 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1851 * "sb_data" is correct for systems where pointers must be aligned on
1852 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1853 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001854#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001855typedef struct sblock_S sblock_T;
1856struct sblock_S
1857{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001858 int sb_used; // nr of bytes already in use
1859 sblock_T *sb_next; // next block in list
1860 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001861};
1862
1863/*
1864 * A node in the tree.
1865 */
1866typedef struct wordnode_S wordnode_T;
1867struct wordnode_S
1868{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001869 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001870 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001871 char_u hashkey[6]; // the hash key, only used while compressing
1872 int index; // index in written nodes (valid after first
1873 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001874 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001875 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001876 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001877 wordnode_T *next; // next node with same hash key
1878 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001879 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001880 wordnode_T *wn_child; // child (next byte in word)
1881 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1882 // always sorted)
1883 int wn_refs; // Nr. of references to this node. Only
1884 // relevant for first node in a list of
1885 // siblings, in following siblings it is
1886 // always one.
1887 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001889 // Info for when "wn_byte" is NUL.
1890 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1891 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1892 // "wn_region" the LSW of the wordnr.
1893 char_u wn_affixID; // supported/required prefix ID or 0
1894 short_u wn_flags; // WF_ flags
1895 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001896
1897#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001898 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001899#endif
1900};
1901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001902#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001903
1904#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1905
1906/*
1907 * Info used while reading the spell files.
1908 */
1909typedef struct spellinfo_S
1910{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001911 wordnode_T *si_foldroot; // tree with case-folded words
1912 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001914 wordnode_T *si_keeproot; // tree with keep-case words
1915 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001917 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001921 sblock_T *si_blocks; // memory blocks used
1922 long si_blocks_cnt; // memory blocks allocated
1923 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001925 long si_compress_cnt; // words to add before lowering
1926 // compression limit
1927 wordnode_T *si_first_free; // List of nodes that have been freed during
1928 // compression, linked by "wn_child" field.
1929 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001930#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001932#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001933 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001935 int si_ascii; // handling only ASCII words
1936 int si_add; // addition file
1937 int si_clear_chartab; // when TRUE clear char tables
1938 int si_region; // region mask
1939 vimconv_T si_conv; // for conversion to 'encoding'
1940 int si_memtot; // runtime memory used
1941 int si_verbose; // verbose messages
1942 int si_msg_count; // number of words added since last message
1943 char_u *si_info; // info text chars or NULL
1944 int si_region_count; // number of regions supported (1 when there
1945 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001946 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001947 // region names; used only if
1948 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001950 garray_T si_rep; // list of fromto_T entries from REP lines
1951 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1952 garray_T si_sal; // list of fromto_T entries from SAL lines
1953 char_u *si_sofofr; // SOFOFROM text
1954 char_u *si_sofoto; // SOFOTO text
1955 int si_nosugfile; // NOSUGFILE item found
1956 int si_nosplitsugs; // NOSPLITSUGS item found
1957 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1958 int si_followup; // soundsalike: ?
1959 int si_collapse; // soundsalike: ?
1960 hashtab_T si_commonwords; // hashtable for common words
1961 time_t si_sugtime; // timestamp for .sug file
1962 int si_rem_accents; // soundsalike: remove accents
1963 garray_T si_map; // MAP info concatenated
1964 char_u *si_midword; // MIDWORD chars or NULL
1965 int si_compmax; // max nr of words for compounding
1966 int si_compminlen; // minimal length for compounding
1967 int si_compsylmax; // max nr of syllables for compounding
1968 int si_compoptions; // COMP_ flags
1969 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1970 // a string
1971 char_u *si_compflags; // flags used for compounding
1972 char_u si_nobreak; // NOBREAK
1973 char_u *si_syllable; // syllable string
1974 garray_T si_prefcond; // table with conditions for postponed
1975 // prefixes, each stored as a string
1976 int si_newprefID; // current value for ah_newID
1977 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001978} spellinfo_T;
1979
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001980static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1981static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1982static int spell_info_item(char_u *s);
1983static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1984static unsigned get_affitem(int flagtype, char_u **pp);
1985static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1986static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001987static void aff_check_number(int spinval, int affval, char *name);
1988static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1989static int str_equal(char_u *s1, char_u *s2);
1990static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1991static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001992static int get_affix_flags(afffile_T *affile, char_u *afflist);
1993static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1994static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1995static 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 +02001996static void *getroom(spellinfo_T *spin, size_t len, int align);
1997static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001998static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
1999static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
2000static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002001static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
Bram Moolenaar408c23b2020-06-03 22:15:45 +02002002static void wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name);
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02002003static long node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, long *tot);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002004static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002005static void clear_node(wordnode_T *node);
2006static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002007static int sug_filltree(spellinfo_T *spin, slang_T *slang);
2008static int sug_maketable(spellinfo_T *spin);
2009static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
2010static int offset2bytes(int nr, char_u *buf);
2011static void sug_write(spellinfo_T *spin, char_u *fname);
2012static void spell_message(spellinfo_T *spin, char_u *str);
2013static void init_spellfile(void);
2014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002015// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
2016// but it must be negative to indicate the prefix tree to tree_add_word().
2017// Use a negative number with the lower 8 bits zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002018#define PFX_FLAGS -256
2019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002020// flags for "condit" argument of store_aff_word()
2021#define CONDIT_COMB 1 // affix must combine
2022#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2023#define CONDIT_SUF 4 // add a suffix for matching flags
2024#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002025
2026/*
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02002027 * Tunable parameters for when the tree is compressed. Filled from the
2028 * 'mkspellmem' option.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002029 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002030static long compress_start = 30000; // memory / SBLOCKSIZE
2031static long compress_inc = 100; // memory / SBLOCKSIZE
2032static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002033
2034/*
2035 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2036 * Sets "sps_flags".
2037 */
2038 int
2039spell_check_msm(void)
2040{
2041 char_u *p = p_msm;
2042 long start = 0;
2043 long incr = 0;
2044 long added = 0;
2045
2046 if (!VIM_ISDIGIT(*p))
2047 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002048 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002049 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2050 if (*p != ',')
2051 return FAIL;
2052 ++p;
2053 if (!VIM_ISDIGIT(*p))
2054 return FAIL;
2055 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2056 if (*p != ',')
2057 return FAIL;
2058 ++p;
2059 if (!VIM_ISDIGIT(*p))
2060 return FAIL;
2061 added = getdigits(&p) * 1024;
2062 if (*p != NUL)
2063 return FAIL;
2064
2065 if (start == 0 || incr == 0 || added == 0 || incr > start)
2066 return FAIL;
2067
2068 compress_start = start;
2069 compress_inc = incr;
2070 compress_added = added;
2071 return OK;
2072}
2073
2074#ifdef SPELL_PRINTTREE
2075/*
2076 * For debugging the tree code: print the current tree in a (more or less)
2077 * readable format, so that we can see what happens when adding a word and/or
2078 * compressing the tree.
2079 * Based on code from Olaf Seibert.
2080 */
2081#define PRINTLINESIZE 1000
2082#define PRINTWIDTH 6
2083
2084#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2085 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2086
2087static char line1[PRINTLINESIZE];
2088static char line2[PRINTLINESIZE];
2089static char line3[PRINTLINESIZE];
2090
2091 static void
2092spell_clear_flags(wordnode_T *node)
2093{
2094 wordnode_T *np;
2095
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002096 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002097 {
2098 np->wn_u1.index = FALSE;
2099 spell_clear_flags(np->wn_child);
2100 }
2101}
2102
2103 static void
2104spell_print_node(wordnode_T *node, int depth)
2105{
2106 if (node->wn_u1.index)
2107 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002109 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2110 PRINTSOME(line2, depth, " ", 0, 0);
2111 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002112 msg(line1);
2113 msg(line2);
2114 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002115 }
2116 else
2117 {
2118 node->wn_u1.index = TRUE;
2119
2120 if (node->wn_byte != NUL)
2121 {
2122 if (node->wn_child != NULL)
2123 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2124 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002126 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2127 }
2128 else
2129 PRINTSOME(line1, depth, " $ ", 0, 0);
2130
2131 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2132
2133 if (node->wn_sibling != NULL)
2134 PRINTSOME(line3, depth, " | ", 0, 0);
2135 else
2136 PRINTSOME(line3, depth, " ", 0, 0);
2137
2138 if (node->wn_byte == NUL)
2139 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002140 msg(line1);
2141 msg(line2);
2142 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002143 }
2144
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002145 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002146 if (node->wn_byte != NUL && node->wn_child != NULL)
2147 spell_print_node(node->wn_child, depth + 1);
2148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002149 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002150 if (node->wn_sibling != NULL)
2151 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002153 STRCPY(line1, line3);
2154 STRCPY(line2, line3);
2155 spell_print_node(node->wn_sibling, depth);
2156 }
2157 }
2158}
2159
2160 static void
2161spell_print_tree(wordnode_T *root)
2162{
2163 if (root != NULL)
2164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002165 // Clear the "wn_u1.index" fields, used to remember what has been
2166 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002167 spell_clear_flags(root);
2168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002169 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002170 spell_print_node(root, 0);
2171 }
2172}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002173#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002174
2175/*
2176 * Read the affix file "fname".
2177 * Returns an afffile_T, NULL for complete failure.
2178 */
2179 static afffile_T *
2180spell_read_aff(spellinfo_T *spin, char_u *fname)
2181{
2182 FILE *fd;
2183 afffile_T *aff;
2184 char_u rline[MAXLINELEN];
2185 char_u *line;
2186 char_u *pc = NULL;
2187#define MAXITEMCNT 30
2188 char_u *(items[MAXITEMCNT]);
2189 int itemcnt;
2190 char_u *p;
2191 int lnum = 0;
2192 affheader_T *cur_aff = NULL;
2193 int did_postpone_prefix = FALSE;
2194 int aff_todo = 0;
2195 hashtab_T *tp;
2196 char_u *low = NULL;
2197 char_u *fol = NULL;
2198 char_u *upp = NULL;
2199 int do_rep;
2200 int do_repsal;
2201 int do_sal;
2202 int do_mapline;
2203 int found_map = FALSE;
2204 hashitem_T *hi;
2205 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002206 int compminlen = 0; // COMPOUNDMIN value
2207 int compsylmax = 0; // COMPOUNDSYLMAX value
2208 int compoptions = 0; // COMP_ flags
2209 int compmax = 0; // COMPOUNDWORDMAX value
2210 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2211 // concatenated
2212 char_u *midword = NULL; // MIDWORD value
2213 char_u *syllable = NULL; // SYLLABLE value
2214 char_u *sofofrom = NULL; // SOFOFROM value
2215 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002216
2217 /*
2218 * Open the file.
2219 */
2220 fd = mch_fopen((char *)fname, "r");
2221 if (fd == NULL)
2222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002223 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002224 return NULL;
2225 }
2226
Bram Moolenaarc1669272018-06-19 14:23:53 +02002227 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002228 spell_message(spin, IObuff);
2229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002230 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002231 do_rep = spin->si_rep.ga_len == 0;
2232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002233 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002234 do_repsal = spin->si_repsal.ga_len == 0;
2235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002236 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002237 do_sal = spin->si_sal.ga_len == 0;
2238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002239 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002240 do_mapline = spin->si_map.ga_len == 0;
2241
2242 /*
2243 * Allocate and init the afffile_T structure.
2244 */
2245 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2246 if (aff == NULL)
2247 {
2248 fclose(fd);
2249 return NULL;
2250 }
2251 hash_init(&aff->af_pref);
2252 hash_init(&aff->af_suff);
2253 hash_init(&aff->af_comp);
2254
2255 /*
2256 * Read all the lines in the file one by one.
2257 */
2258 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2259 {
2260 line_breakcheck();
2261 ++lnum;
2262
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002263 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002264 if (*rline == '#')
2265 continue;
2266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002267 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002268 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002269 if (spin->si_conv.vc_type != CONV_NONE)
2270 {
2271 pc = string_convert(&spin->si_conv, rline, NULL);
2272 if (pc == NULL)
2273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002275 fname, lnum, rline);
2276 continue;
2277 }
2278 line = pc;
2279 }
2280 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002281 {
2282 pc = NULL;
2283 line = rline;
2284 }
2285
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002286 // Split the line up in white separated items. Put a NUL after each
2287 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002288 itemcnt = 0;
2289 for (p = line; ; )
2290 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002291 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002292 ++p;
2293 if (*p == NUL)
2294 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002296 break;
2297 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002298 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002299 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002300 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002301 ++p;
2302 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002303 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002304 ++p;
2305 if (*p == NUL)
2306 break;
2307 *p++ = NUL;
2308 }
2309
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002310 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002311 if (itemcnt > 0)
2312 {
2313 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2314 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002315 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002316 aff->af_enc = enc_canonize(items[1]);
2317 if (aff->af_enc != NULL && !spin->si_ascii
2318 && convert_setup(&spin->si_conv, aff->af_enc,
2319 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002320 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002321 fname, aff->af_enc, p_enc);
2322 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002323 }
2324 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2325 && aff->af_flagtype == AFT_CHAR)
2326 {
2327 if (STRCMP(items[1], "long") == 0)
2328 aff->af_flagtype = AFT_LONG;
2329 else if (STRCMP(items[1], "num") == 0)
2330 aff->af_flagtype = AFT_NUM;
2331 else if (STRCMP(items[1], "caplong") == 0)
2332 aff->af_flagtype = AFT_CAPLONG;
2333 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002334 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002335 fname, lnum, items[1]);
2336 if (aff->af_rare != 0
2337 || aff->af_keepcase != 0
2338 || aff->af_bad != 0
2339 || aff->af_needaffix != 0
2340 || aff->af_circumfix != 0
2341 || aff->af_needcomp != 0
2342 || aff->af_comproot != 0
2343 || aff->af_nosuggest != 0
2344 || compflags != NULL
2345 || aff->af_suff.ht_used > 0
2346 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002348 fname, lnum, items[1]);
2349 }
2350 else if (spell_info_item(items[0]))
2351 {
2352 p = (char_u *)getroom(spin,
2353 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2354 + STRLEN(items[0])
2355 + STRLEN(items[1]) + 3, FALSE);
2356 if (p != NULL)
2357 {
2358 if (spin->si_info != NULL)
2359 {
2360 STRCPY(p, spin->si_info);
2361 STRCAT(p, "\n");
2362 }
2363 STRCAT(p, items[0]);
2364 STRCAT(p, " ");
2365 STRCAT(p, items[1]);
2366 spin->si_info = p;
2367 }
2368 }
2369 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2370 && midword == NULL)
2371 {
2372 midword = getroom_save(spin, items[1]);
2373 }
2374 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2375 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002376 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002377 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002379 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2380 || is_aff_rule(items, itemcnt, "RARE", 2))
2381 && aff->af_rare == 0)
2382 {
2383 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2384 fname, lnum);
2385 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002386 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002387 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2388 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2389 && aff->af_keepcase == 0)
2390 {
2391 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2392 fname, lnum);
2393 }
2394 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2395 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2396 && aff->af_bad == 0)
2397 {
2398 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2399 fname, lnum);
2400 }
2401 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2402 && aff->af_needaffix == 0)
2403 {
2404 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2405 fname, lnum);
2406 }
2407 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2408 && aff->af_circumfix == 0)
2409 {
2410 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2411 fname, lnum);
2412 }
2413 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2414 && aff->af_nosuggest == 0)
2415 {
2416 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2417 fname, lnum);
2418 }
2419 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2420 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2421 && aff->af_needcomp == 0)
2422 {
2423 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2424 fname, lnum);
2425 }
2426 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2427 && aff->af_comproot == 0)
2428 {
2429 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2430 fname, lnum);
2431 }
2432 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2433 && aff->af_compforbid == 0)
2434 {
2435 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2436 fname, lnum);
2437 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002439 fname, lnum);
2440 }
2441 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2442 && aff->af_comppermit == 0)
2443 {
2444 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2445 fname, lnum);
2446 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002448 fname, lnum);
2449 }
2450 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2451 && compflags == NULL)
2452 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002453 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2454 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002455 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2456 if (p != NULL)
2457 {
2458 STRCPY(p, items[1]);
2459 STRCAT(p, "+");
2460 compflags = p;
2461 }
2462 }
2463 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2464 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002465 // We don't use the count, but do check that it's a number and
2466 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002467 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002468 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002469 fname, lnum, items[1]);
2470 }
2471 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2472 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002473 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002474 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2475 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002476 // Concatenate this string to previously defined ones,
2477 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002478 l = (int)STRLEN(items[1]) + 1;
2479 if (compflags != NULL)
2480 l += (int)STRLEN(compflags) + 1;
2481 p = getroom(spin, l, FALSE);
2482 if (p != NULL)
2483 {
2484 if (compflags != NULL)
2485 {
2486 STRCPY(p, compflags);
2487 STRCAT(p, "/");
2488 }
2489 STRCAT(p, items[1]);
2490 compflags = p;
2491 }
2492 }
2493 }
2494 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2495 && compmax == 0)
2496 {
2497 compmax = atoi((char *)items[1]);
2498 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002499 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002500 fname, lnum, items[1]);
2501 }
2502 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2503 && compminlen == 0)
2504 {
2505 compminlen = atoi((char *)items[1]);
2506 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002507 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002508 fname, lnum, items[1]);
2509 }
2510 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2511 && compsylmax == 0)
2512 {
2513 compsylmax = atoi((char *)items[1]);
2514 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002515 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002516 fname, lnum, items[1]);
2517 }
2518 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2519 {
2520 compoptions |= COMP_CHECKDUP;
2521 }
2522 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2523 {
2524 compoptions |= COMP_CHECKREP;
2525 }
2526 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2527 {
2528 compoptions |= COMP_CHECKCASE;
2529 }
2530 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2531 {
2532 compoptions |= COMP_CHECKTRIPLE;
2533 }
2534 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2535 {
2536 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002538 fname, lnum, items[1]);
2539 }
2540 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2541 {
2542 garray_T *gap = &spin->si_comppat;
2543 int i;
2544
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002545 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002546 for (i = 0; i < gap->ga_len - 1; i += 2)
2547 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2548 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2549 items[2]) == 0)
2550 break;
2551 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2552 {
2553 ((char_u **)(gap->ga_data))[gap->ga_len++]
2554 = getroom_save(spin, items[1]);
2555 ((char_u **)(gap->ga_data))[gap->ga_len++]
2556 = getroom_save(spin, items[2]);
2557 }
2558 }
2559 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2560 && syllable == NULL)
2561 {
2562 syllable = getroom_save(spin, items[1]);
2563 }
2564 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2565 {
2566 spin->si_nobreak = TRUE;
2567 }
2568 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2569 {
2570 spin->si_nosplitsugs = TRUE;
2571 }
2572 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2573 {
2574 spin->si_nocompoundsugs = TRUE;
2575 }
2576 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2577 {
2578 spin->si_nosugfile = TRUE;
2579 }
2580 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2581 {
2582 aff->af_pfxpostpone = TRUE;
2583 }
2584 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2585 {
2586 aff->af_ignoreextra = TRUE;
2587 }
2588 else if ((STRCMP(items[0], "PFX") == 0
2589 || STRCMP(items[0], "SFX") == 0)
2590 && aff_todo == 0
2591 && itemcnt >= 4)
2592 {
2593 int lasti = 4;
2594 char_u key[AH_KEY_LEN];
2595
2596 if (*items[0] == 'P')
2597 tp = &aff->af_pref;
2598 else
2599 tp = &aff->af_suff;
2600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002601 // Myspell allows the same affix name to be used multiple
2602 // times. The affix files that do this have an undocumented
2603 // "S" flag on all but the last block, thus we check for that
2604 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002605 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2606 hi = hash_find(tp, key);
2607 if (!HASHITEM_EMPTY(hi))
2608 {
2609 cur_aff = HI2AH(hi);
2610 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002611 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002612 fname, lnum, items[1]);
2613 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002614 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002615 fname, lnum, items[1]);
2616 }
2617 else
2618 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002619 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002620 cur_aff = (affheader_T *)getroom(spin,
2621 sizeof(affheader_T), TRUE);
2622 if (cur_aff == NULL)
2623 break;
2624 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2625 fname, lnum);
2626 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2627 break;
2628 if (cur_aff->ah_flag == aff->af_bad
2629 || cur_aff->ah_flag == aff->af_rare
2630 || cur_aff->ah_flag == aff->af_keepcase
2631 || cur_aff->ah_flag == aff->af_needaffix
2632 || cur_aff->ah_flag == aff->af_circumfix
2633 || cur_aff->ah_flag == aff->af_nosuggest
2634 || cur_aff->ah_flag == aff->af_needcomp
2635 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002637 fname, lnum, items[1]);
2638 STRCPY(cur_aff->ah_key, items[1]);
2639 hash_add(tp, cur_aff->ah_key);
2640
2641 cur_aff->ah_combine = (*items[2] == 'Y');
2642 }
2643
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002644 // Check for the "S" flag, which apparently means that another
2645 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002646 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2647 {
2648 ++lasti;
2649 cur_aff->ah_follows = TRUE;
2650 }
2651 else
2652 cur_aff->ah_follows = FALSE;
2653
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002654 // Myspell allows extra text after the item, but that might
2655 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002656 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002657 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002658
2659 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002661 fname, lnum, items[2]);
2662
2663 if (*items[0] == 'P' && aff->af_pfxpostpone)
2664 {
2665 if (cur_aff->ah_newID == 0)
2666 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002667 // Use a new number in the .spl file later, to be able
2668 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002669 check_renumber(spin);
2670 cur_aff->ah_newID = ++spin->si_newprefID;
2671
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002672 // We only really use ah_newID if the prefix is
2673 // postponed. We know that only after handling all
2674 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002675 did_postpone_prefix = FALSE;
2676 }
2677 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002678 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002679 did_postpone_prefix = TRUE;
2680 }
2681
2682 aff_todo = atoi((char *)items[3]);
2683 }
2684 else if ((STRCMP(items[0], "PFX") == 0
2685 || STRCMP(items[0], "SFX") == 0)
2686 && aff_todo > 0
2687 && STRCMP(cur_aff->ah_key, items[1]) == 0
2688 && itemcnt >= 5)
2689 {
2690 affentry_T *aff_entry;
2691 int upper = FALSE;
2692 int lasti = 5;
2693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002694 // Myspell allows extra text after the item, but that might
2695 // mean mistakes go unnoticed. Require a comment-starter,
2696 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002697 if (itemcnt > lasti
2698 && !aff->af_ignoreextra
2699 && *items[lasti] != '#'
2700 && (STRCMP(items[lasti], "-") != 0
2701 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002704 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002705 --aff_todo;
2706 aff_entry = (affentry_T *)getroom(spin,
2707 sizeof(affentry_T), TRUE);
2708 if (aff_entry == NULL)
2709 break;
2710
2711 if (STRCMP(items[2], "0") != 0)
2712 aff_entry->ae_chop = getroom_save(spin, items[2]);
2713 if (STRCMP(items[3], "0") != 0)
2714 {
2715 aff_entry->ae_add = getroom_save(spin, items[3]);
2716
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002717 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002718 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2719 if (aff_entry->ae_flags != NULL)
2720 {
2721 *aff_entry->ae_flags++ = NUL;
2722 aff_process_flags(aff, aff_entry);
2723 }
2724 }
2725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002726 // Don't use an affix entry with non-ASCII characters when
2727 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002728 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2729 || has_non_ascii(aff_entry->ae_add)))
2730 {
2731 aff_entry->ae_next = cur_aff->ah_first;
2732 cur_aff->ah_first = aff_entry;
2733
2734 if (STRCMP(items[4], ".") != 0)
2735 {
2736 char_u buf[MAXLINELEN];
2737
2738 aff_entry->ae_cond = getroom_save(spin, items[4]);
2739 if (*items[0] == 'P')
2740 sprintf((char *)buf, "^%s", items[4]);
2741 else
2742 sprintf((char *)buf, "%s$", items[4]);
2743 aff_entry->ae_prog = vim_regcomp(buf,
2744 RE_MAGIC + RE_STRING + RE_STRICT);
2745 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002746 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002747 fname, lnum, items[4]);
2748 }
2749
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002750 // For postponed prefixes we need an entry in si_prefcond
2751 // for the condition. Use an existing one if possible.
2752 // Can't be done for an affix with flags, ignoring
2753 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002754 if (*items[0] == 'P' && aff->af_pfxpostpone
2755 && aff_entry->ae_flags == NULL)
2756 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002757 // When the chop string is one lower-case letter and
2758 // the add string ends in the upper-case letter we set
2759 // the "upper" flag, clear "ae_chop" and remove the
2760 // letters from "ae_add". The condition must either
2761 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002762 if (aff_entry->ae_chop != NULL
2763 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002764 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002765 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002766 {
2767 int c, c_up;
2768
2769 c = PTR2CHAR(aff_entry->ae_chop);
2770 c_up = SPELL_TOUPPER(c);
2771 if (c_up != c
2772 && (aff_entry->ae_cond == NULL
2773 || PTR2CHAR(aff_entry->ae_cond) == c))
2774 {
2775 p = aff_entry->ae_add
2776 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002777 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002778 if (PTR2CHAR(p) == c_up)
2779 {
2780 upper = TRUE;
2781 aff_entry->ae_chop = NULL;
2782 *p = NUL;
2783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 // The condition is matched with the
2785 // actual word, thus must check for the
2786 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002787 if (aff_entry->ae_cond != NULL)
2788 {
2789 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002790
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002791 if (has_mbyte)
2792 {
2793 onecap_copy(items[4], buf, TRUE);
2794 aff_entry->ae_cond = getroom_save(
2795 spin, buf);
2796 }
2797 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002798 *aff_entry->ae_cond = c_up;
2799 if (aff_entry->ae_cond != NULL)
2800 {
2801 sprintf((char *)buf, "^%s",
2802 aff_entry->ae_cond);
2803 vim_regfree(aff_entry->ae_prog);
2804 aff_entry->ae_prog = vim_regcomp(
2805 buf, RE_MAGIC + RE_STRING);
2806 }
2807 }
2808 }
2809 }
2810 }
2811
2812 if (aff_entry->ae_chop == NULL
2813 && aff_entry->ae_flags == NULL)
2814 {
2815 int idx;
2816 char_u **pp;
2817 int n;
2818
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002819 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002820 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2821 --idx)
2822 {
2823 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2824 if (str_equal(p, aff_entry->ae_cond))
2825 break;
2826 }
2827 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2828 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002829 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002830 idx = spin->si_prefcond.ga_len++;
2831 pp = ((char_u **)spin->si_prefcond.ga_data)
2832 + idx;
2833 if (aff_entry->ae_cond == NULL)
2834 *pp = NULL;
2835 else
2836 *pp = getroom_save(spin,
2837 aff_entry->ae_cond);
2838 }
2839
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002840 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002841 if (aff_entry->ae_add == NULL)
2842 p = (char_u *)"";
2843 else
2844 p = aff_entry->ae_add;
2845
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002846 // PFX_FLAGS is a negative number, so that
2847 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002848 n = PFX_FLAGS;
2849 if (!cur_aff->ah_combine)
2850 n |= WFP_NC;
2851 if (upper)
2852 n |= WFP_UP;
2853 if (aff_entry->ae_comppermit)
2854 n |= WFP_COMPPERMIT;
2855 if (aff_entry->ae_compforbid)
2856 n |= WFP_COMPFORBID;
2857 tree_add_word(spin, p, spin->si_prefroot, n,
2858 idx, cur_aff->ah_newID);
2859 did_postpone_prefix = TRUE;
2860 }
2861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002862 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002863 if (aff_todo == 0 && !did_postpone_prefix)
2864 {
2865 --spin->si_newprefID;
2866 cur_aff->ah_newID = 0;
2867 }
2868 }
2869 }
2870 }
2871 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2872 {
2873 fol = vim_strsave(items[1]);
2874 }
2875 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2876 {
2877 low = vim_strsave(items[1]);
2878 }
2879 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2880 {
2881 upp = vim_strsave(items[1]);
2882 }
2883 else if (is_aff_rule(items, itemcnt, "REP", 2)
2884 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002886 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002887 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002888 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002889 fname, lnum);
2890 }
2891 else if ((STRCMP(items[0], "REP") == 0
2892 || STRCMP(items[0], "REPSAL") == 0)
2893 && itemcnt >= 3)
2894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002895 // REP/REPSAL item
2896 // Myspell ignores extra arguments, we require it starts with
2897 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002898 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002899 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002900 if (items[0][3] == 'S' ? do_repsal : do_rep)
2901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002902 // Replace underscore with space (can't include a space
2903 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002904 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002905 if (*p == '_')
2906 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002907 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002908 if (*p == '_')
2909 *p = ' ';
2910 add_fromto(spin, items[0][3] == 'S'
2911 ? &spin->si_repsal
2912 : &spin->si_rep, items[1], items[2]);
2913 }
2914 }
2915 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2916 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002917 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002918 if (!found_map)
2919 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002920 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002921 found_map = TRUE;
2922 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002923 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002924 fname, lnum);
2925 }
2926 else if (do_mapline)
2927 {
2928 int c;
2929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002930 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002931 for (p = items[1]; *p != NUL; )
2932 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002933 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002934 if ((spin->si_map.ga_len > 0
2935 && vim_strchr(spin->si_map.ga_data, c)
2936 != NULL)
2937 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002938 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002939 fname, lnum);
2940 }
2941
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002942 // We simply concatenate all the MAP strings, separated by
2943 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002944 ga_concat(&spin->si_map, items[1]);
2945 ga_append(&spin->si_map, '/');
2946 }
2947 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002948 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002949 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2950 {
2951 if (do_sal)
2952 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002953 // SAL item (sounds-a-like)
2954 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002955 if (STRCMP(items[1], "followup") == 0)
2956 spin->si_followup = sal_to_bool(items[2]);
2957 else if (STRCMP(items[1], "collapse_result") == 0)
2958 spin->si_collapse = sal_to_bool(items[2]);
2959 else if (STRCMP(items[1], "remove_accents") == 0)
2960 spin->si_rem_accents = sal_to_bool(items[2]);
2961 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002962 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002963 add_fromto(spin, &spin->si_sal, items[1],
2964 STRCMP(items[2], "_") == 0 ? (char_u *)""
2965 : items[2]);
2966 }
2967 }
2968 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2969 && sofofrom == NULL)
2970 {
2971 sofofrom = getroom_save(spin, items[1]);
2972 }
2973 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2974 && sofoto == NULL)
2975 {
2976 sofoto = getroom_save(spin, items[1]);
2977 }
2978 else if (STRCMP(items[0], "COMMON") == 0)
2979 {
2980 int i;
2981
2982 for (i = 1; i < itemcnt; ++i)
2983 {
2984 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2985 items[i])))
2986 {
2987 p = vim_strsave(items[i]);
2988 if (p == NULL)
2989 break;
2990 hash_add(&spin->si_commonwords, p);
2991 }
2992 }
2993 }
2994 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002995 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002996 fname, lnum, items[0]);
2997 }
2998 }
2999
3000 if (fol != NULL || low != NULL || upp != NULL)
3001 {
3002 if (spin->si_clear_chartab)
3003 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003004 // Clear the char type tables, don't want to use any of the
3005 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003006 init_spell_chartab();
3007 spin->si_clear_chartab = FALSE;
3008 }
3009
3010 /*
3011 * Don't write a word table for an ASCII file, so that we don't check
3012 * for conflicts with a word table that matches 'encoding'.
3013 * Don't write one for utf-8 either, we use utf_*() and
3014 * mb_get_class(), the list of chars in the file will be incomplete.
3015 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003016 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003017 {
3018 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003020 else
3021 (void)set_spell_chartab(fol, low, upp);
3022 }
3023
3024 vim_free(fol);
3025 vim_free(low);
3026 vim_free(upp);
3027 }
3028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003029 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003030 if (compmax != 0)
3031 {
3032 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3033 spin->si_compmax = compmax;
3034 }
3035
3036 if (compminlen != 0)
3037 {
3038 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3039 spin->si_compminlen = compminlen;
3040 }
3041
3042 if (compsylmax != 0)
3043 {
3044 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003045 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003046 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3047 spin->si_compsylmax = compsylmax;
3048 }
3049
3050 if (compoptions != 0)
3051 {
3052 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3053 spin->si_compoptions |= compoptions;
3054 }
3055
3056 if (compflags != NULL)
3057 process_compflags(spin, aff, compflags);
3058
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003059 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003060 if (spin->si_newcompID < spin->si_newprefID)
3061 {
3062 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003063 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003064 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003065 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003066 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003067 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003068 }
3069
3070 if (syllable != NULL)
3071 {
3072 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3073 spin->si_syllable = syllable;
3074 }
3075
3076 if (sofofrom != NULL || sofoto != NULL)
3077 {
3078 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003079 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003080 sofofrom == NULL ? "FROM" : "TO", fname);
3081 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003082 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003083 else
3084 {
3085 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3086 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3087 spin->si_sofofr = sofofrom;
3088 spin->si_sofoto = sofoto;
3089 }
3090 }
3091
3092 if (midword != NULL)
3093 {
3094 aff_check_string(spin->si_midword, midword, "MIDWORD");
3095 spin->si_midword = midword;
3096 }
3097
3098 vim_free(pc);
3099 fclose(fd);
3100 return aff;
3101}
3102
3103/*
3104 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3105 * a comment is following after item "mincount".
3106 */
3107 static int
3108is_aff_rule(
3109 char_u **items,
3110 int itemcnt,
3111 char *rulename,
3112 int mincount)
3113{
3114 return (STRCMP(items[0], rulename) == 0
3115 && (itemcnt == mincount
3116 || (itemcnt > mincount && items[mincount][0] == '#')));
3117}
3118
3119/*
3120 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3121 * ae_flags to ae_comppermit and ae_compforbid.
3122 */
3123 static void
3124aff_process_flags(afffile_T *affile, affentry_T *entry)
3125{
3126 char_u *p;
3127 char_u *prevp;
3128 unsigned flag;
3129
3130 if (entry->ae_flags != NULL
3131 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3132 {
3133 for (p = entry->ae_flags; *p != NUL; )
3134 {
3135 prevp = p;
3136 flag = get_affitem(affile->af_flagtype, &p);
3137 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3138 {
3139 STRMOVE(prevp, p);
3140 p = prevp;
3141 if (flag == affile->af_comppermit)
3142 entry->ae_comppermit = TRUE;
3143 else
3144 entry->ae_compforbid = TRUE;
3145 }
3146 if (affile->af_flagtype == AFT_NUM && *p == ',')
3147 ++p;
3148 }
3149 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003150 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003151 }
3152}
3153
3154/*
3155 * Return TRUE if "s" is the name of an info item in the affix file.
3156 */
3157 static int
3158spell_info_item(char_u *s)
3159{
3160 return STRCMP(s, "NAME") == 0
3161 || STRCMP(s, "HOME") == 0
3162 || STRCMP(s, "VERSION") == 0
3163 || STRCMP(s, "AUTHOR") == 0
3164 || STRCMP(s, "EMAIL") == 0
3165 || STRCMP(s, "COPYRIGHT") == 0;
3166}
3167
3168/*
3169 * Turn an affix flag name into a number, according to the FLAG type.
3170 * returns zero for failure.
3171 */
3172 static unsigned
3173affitem2flag(
3174 int flagtype,
3175 char_u *item,
3176 char_u *fname,
3177 int lnum)
3178{
3179 unsigned res;
3180 char_u *p = item;
3181
3182 res = get_affitem(flagtype, &p);
3183 if (res == 0)
3184 {
3185 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003186 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003187 fname, lnum, item);
3188 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003189 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003190 fname, lnum, item);
3191 }
3192 if (*p != NUL)
3193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003194 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003195 return 0;
3196 }
3197
3198 return res;
3199}
3200
3201/*
3202 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003203 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003204 * Returns zero for an error, still advances the pointer then.
3205 */
3206 static unsigned
3207get_affitem(int flagtype, char_u **pp)
3208{
3209 int res;
3210
3211 if (flagtype == AFT_NUM)
3212 {
3213 if (!VIM_ISDIGIT(**pp))
3214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003215 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003216 return 0;
3217 }
3218 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003219 if (res == 0)
3220 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003221 }
3222 else
3223 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003224 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003225 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3226 && res >= 'A' && res <= 'Z'))
3227 {
3228 if (**pp == NUL)
3229 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003230 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003231 }
3232 }
3233 return res;
3234}
3235
3236/*
3237 * Process the "compflags" string used in an affix file and append it to
3238 * spin->si_compflags.
3239 * The processing involves changing the affix names to ID numbers, so that
3240 * they fit in one byte.
3241 */
3242 static void
3243process_compflags(
3244 spellinfo_T *spin,
3245 afffile_T *aff,
3246 char_u *compflags)
3247{
3248 char_u *p;
3249 char_u *prevp;
3250 unsigned flag;
3251 compitem_T *ci;
3252 int id;
3253 int len;
3254 char_u *tp;
3255 char_u key[AH_KEY_LEN];
3256 hashitem_T *hi;
3257
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003258 // Make room for the old and the new compflags, concatenated with a / in
3259 // between. Processing it makes it shorter, but we don't know by how
3260 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003261 len = (int)STRLEN(compflags) + 1;
3262 if (spin->si_compflags != NULL)
3263 len += (int)STRLEN(spin->si_compflags) + 1;
3264 p = getroom(spin, len, FALSE);
3265 if (p == NULL)
3266 return;
3267 if (spin->si_compflags != NULL)
3268 {
3269 STRCPY(p, spin->si_compflags);
3270 STRCAT(p, "/");
3271 }
3272 spin->si_compflags = p;
3273 tp = p + STRLEN(p);
3274
3275 for (p = compflags; *p != NUL; )
3276 {
3277 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003278 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003279 *tp++ = *p++;
3280 else
3281 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003282 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003283 prevp = p;
3284 flag = get_affitem(aff->af_flagtype, &p);
3285 if (flag != 0)
3286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003287 // Find the flag in the hashtable. If it was used before, use
3288 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003289 vim_strncpy(key, prevp, p - prevp);
3290 hi = hash_find(&aff->af_comp, key);
3291 if (!HASHITEM_EMPTY(hi))
3292 id = HI2CI(hi)->ci_newID;
3293 else
3294 {
3295 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3296 if (ci == NULL)
3297 break;
3298 STRCPY(ci->ci_key, key);
3299 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003300 // Avoid using a flag ID that has a special meaning in a
3301 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003302 do
3303 {
3304 check_renumber(spin);
3305 id = spin->si_newcompID--;
3306 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3307 ci->ci_newID = id;
3308 hash_add(&aff->af_comp, ci->ci_key);
3309 }
3310 *tp++ = id;
3311 }
3312 if (aff->af_flagtype == AFT_NUM && *p == ',')
3313 ++p;
3314 }
3315 }
3316
3317 *tp = NUL;
3318}
3319
3320/*
3321 * Check that the new IDs for postponed affixes and compounding don't overrun
3322 * each other. We have almost 255 available, but start at 0-127 to avoid
3323 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3324 * When that is used up an error message is given.
3325 */
3326 static void
3327check_renumber(spellinfo_T *spin)
3328{
3329 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3330 {
3331 spin->si_newprefID = 127;
3332 spin->si_newcompID = 255;
3333 }
3334}
3335
3336/*
3337 * Return TRUE if flag "flag" appears in affix list "afflist".
3338 */
3339 static int
3340flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3341{
3342 char_u *p;
3343 unsigned n;
3344
3345 switch (flagtype)
3346 {
3347 case AFT_CHAR:
3348 return vim_strchr(afflist, flag) != NULL;
3349
3350 case AFT_CAPLONG:
3351 case AFT_LONG:
3352 for (p = afflist; *p != NUL; )
3353 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003354 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003355 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3356 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003357 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003358 if (n == flag)
3359 return TRUE;
3360 }
3361 break;
3362
3363 case AFT_NUM:
3364 for (p = afflist; *p != NUL; )
3365 {
3366 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003367 if (n == 0)
3368 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003369 if (n == flag)
3370 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003371 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003372 ++p;
3373 }
3374 break;
3375 }
3376 return FALSE;
3377}
3378
3379/*
3380 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3381 */
3382 static void
3383aff_check_number(int spinval, int affval, char *name)
3384{
3385 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003386 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003387}
3388
3389/*
3390 * Give a warning when "spinval" and "affval" strings are set and not the same.
3391 */
3392 static void
3393aff_check_string(char_u *spinval, char_u *affval, char *name)
3394{
3395 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003396 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003397}
3398
3399/*
3400 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3401 * NULL as equal.
3402 */
3403 static int
3404str_equal(char_u *s1, char_u *s2)
3405{
3406 if (s1 == NULL || s2 == NULL)
3407 return s1 == s2;
3408 return STRCMP(s1, s2) == 0;
3409}
3410
3411/*
3412 * Add a from-to item to "gap". Used for REP and SAL items.
3413 * They are stored case-folded.
3414 */
3415 static void
3416add_fromto(
3417 spellinfo_T *spin,
3418 garray_T *gap,
3419 char_u *from,
3420 char_u *to)
3421{
3422 fromto_T *ftp;
3423 char_u word[MAXWLEN];
3424
3425 if (ga_grow(gap, 1) == OK)
3426 {
3427 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3428 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
3429 ftp->ft_from = getroom_save(spin, word);
3430 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
3431 ftp->ft_to = getroom_save(spin, word);
3432 ++gap->ga_len;
3433 }
3434}
3435
3436/*
3437 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3438 */
3439 static int
3440sal_to_bool(char_u *s)
3441{
3442 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3443}
3444
3445/*
3446 * Free the structure filled by spell_read_aff().
3447 */
3448 static void
3449spell_free_aff(afffile_T *aff)
3450{
3451 hashtab_T *ht;
3452 hashitem_T *hi;
3453 int todo;
3454 affheader_T *ah;
3455 affentry_T *ae;
3456
3457 vim_free(aff->af_enc);
3458
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003459 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003460 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3461 {
3462 todo = (int)ht->ht_used;
3463 for (hi = ht->ht_array; todo > 0; ++hi)
3464 {
3465 if (!HASHITEM_EMPTY(hi))
3466 {
3467 --todo;
3468 ah = HI2AH(hi);
3469 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3470 vim_regfree(ae->ae_prog);
3471 }
3472 }
3473 if (ht == &aff->af_suff)
3474 break;
3475 }
3476
3477 hash_clear(&aff->af_pref);
3478 hash_clear(&aff->af_suff);
3479 hash_clear(&aff->af_comp);
3480}
3481
3482/*
3483 * Read dictionary file "fname".
3484 * Returns OK or FAIL;
3485 */
3486 static int
3487spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3488{
3489 hashtab_T ht;
3490 char_u line[MAXLINELEN];
3491 char_u *p;
3492 char_u *afflist;
3493 char_u store_afflist[MAXWLEN];
3494 int pfxlen;
3495 int need_affix;
3496 char_u *dw;
3497 char_u *pc;
3498 char_u *w;
3499 int l;
3500 hash_T hash;
3501 hashitem_T *hi;
3502 FILE *fd;
3503 int lnum = 1;
3504 int non_ascii = 0;
3505 int retval = OK;
3506 char_u message[MAXLINELEN + MAXWLEN];
3507 int flags;
3508 int duplicate = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003509 time_T last_msg_time = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003510
3511 /*
3512 * Open the file.
3513 */
3514 fd = mch_fopen((char *)fname, "r");
3515 if (fd == NULL)
3516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003517 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003518 return FAIL;
3519 }
3520
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003521 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003522 hash_init(&ht);
3523
3524 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003525 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003526 spell_message(spin, IObuff);
3527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003528 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003529 spin->si_msg_count = 999999;
3530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003531 // Read and ignore the first line: word count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003532 (void)vim_fgets(line, MAXLINELEN, fd);
3533 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003534 semsg(_("E760: No word count in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003535
3536 /*
3537 * Read all the lines in the file one by one.
3538 * The words are converted to 'encoding' here, before being added to
3539 * the hashtable.
3540 */
3541 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3542 {
3543 line_breakcheck();
3544 ++lnum;
3545 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003546 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003548 // Remove CR, LF and white space from the end. White space halfway
3549 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003550 l = (int)STRLEN(line);
3551 while (l > 0 && line[l - 1] <= ' ')
3552 --l;
3553 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003554 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003555 line[l] = NUL;
3556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003557 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003558 if (spin->si_conv.vc_type != CONV_NONE)
3559 {
3560 pc = string_convert(&spin->si_conv, line, NULL);
3561 if (pc == NULL)
3562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003563 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003564 fname, lnum, line);
3565 continue;
3566 }
3567 w = pc;
3568 }
3569 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003570 {
3571 pc = NULL;
3572 w = line;
3573 }
3574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003575 // Truncate the word at the "/", set "afflist" to what follows.
3576 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003577 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003578 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003579 {
3580 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3581 STRMOVE(p, p + 1);
3582 else if (*p == '/')
3583 {
3584 *p = NUL;
3585 afflist = p + 1;
3586 break;
3587 }
3588 }
3589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003590 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003591 if (spin->si_ascii && has_non_ascii(w))
3592 {
3593 ++non_ascii;
3594 vim_free(pc);
3595 continue;
3596 }
3597
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003598 // This takes time, print a message every 10000 words, but not more
3599 // often than once per second.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003600 if (spin->si_verbose && spin->si_msg_count > 10000)
3601 {
3602 spin->si_msg_count = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003603 if (vim_time() > last_msg_time)
3604 {
3605 last_msg_time = vim_time();
3606 vim_snprintf((char *)message, sizeof(message),
3607 _("line %6d, word %6ld - %s"),
3608 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3609 msg_start();
3610 msg_outtrans_long_attr(message, 0);
3611 msg_clr_eos();
3612 msg_didout = FALSE;
3613 msg_col = 0;
3614 out_flush();
3615 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003616 }
3617
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003618 // Store the word in the hashtable to be able to find duplicates.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003619 dw = (char_u *)getroom_save(spin, w);
3620 if (dw == NULL)
3621 {
3622 retval = FAIL;
3623 vim_free(pc);
3624 break;
3625 }
3626
3627 hash = hash_hash(dw);
3628 hi = hash_lookup(&ht, dw, hash);
3629 if (!HASHITEM_EMPTY(hi))
3630 {
3631 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003632 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003633 fname, lnum, dw);
3634 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003635 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003636 fname, lnum, dw);
3637 ++duplicate;
3638 }
3639 else
3640 hash_add_item(&ht, hi, dw, hash);
3641
3642 flags = 0;
3643 store_afflist[0] = NUL;
3644 pfxlen = 0;
3645 need_affix = FALSE;
3646 if (afflist != NULL)
3647 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003648 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003649 flags |= get_affix_flags(affile, afflist);
3650
3651 if (affile->af_needaffix != 0 && flag_in_afflist(
3652 affile->af_flagtype, afflist, affile->af_needaffix))
3653 need_affix = TRUE;
3654
3655 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003656 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003657 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3658
3659 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 // Need to store the list of compound flags with the word.
3661 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003662 get_compflags(affile, afflist, store_afflist + pfxlen);
3663 }
3664
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003665 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003666 if (store_word(spin, dw, flags, spin->si_region,
3667 store_afflist, need_affix) == FAIL)
3668 retval = FAIL;
3669
3670 if (afflist != NULL)
3671 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003672 // Find all matching suffixes and add the resulting words.
3673 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003674 if (store_aff_word(spin, dw, afflist, affile,
3675 &affile->af_suff, &affile->af_pref,
3676 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3677 retval = FAIL;
3678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003679 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003680 if (store_aff_word(spin, dw, afflist, affile,
3681 &affile->af_pref, NULL,
3682 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3683 retval = FAIL;
3684 }
3685
3686 vim_free(pc);
3687 }
3688
3689 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003690 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003691 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003692 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003693 non_ascii, fname);
3694 hash_clear(&ht);
3695
3696 fclose(fd);
3697 return retval;
3698}
3699
3700/*
3701 * Check for affix flags in "afflist" that are turned into word flags.
3702 * Return WF_ flags.
3703 */
3704 static int
3705get_affix_flags(afffile_T *affile, char_u *afflist)
3706{
3707 int flags = 0;
3708
3709 if (affile->af_keepcase != 0 && flag_in_afflist(
3710 affile->af_flagtype, afflist, affile->af_keepcase))
3711 flags |= WF_KEEPCAP | WF_FIXCAP;
3712 if (affile->af_rare != 0 && flag_in_afflist(
3713 affile->af_flagtype, afflist, affile->af_rare))
3714 flags |= WF_RARE;
3715 if (affile->af_bad != 0 && flag_in_afflist(
3716 affile->af_flagtype, afflist, affile->af_bad))
3717 flags |= WF_BANNED;
3718 if (affile->af_needcomp != 0 && flag_in_afflist(
3719 affile->af_flagtype, afflist, affile->af_needcomp))
3720 flags |= WF_NEEDCOMP;
3721 if (affile->af_comproot != 0 && flag_in_afflist(
3722 affile->af_flagtype, afflist, affile->af_comproot))
3723 flags |= WF_COMPROOT;
3724 if (affile->af_nosuggest != 0 && flag_in_afflist(
3725 affile->af_flagtype, afflist, affile->af_nosuggest))
3726 flags |= WF_NOSUGGEST;
3727 return flags;
3728}
3729
3730/*
3731 * Get the list of prefix IDs from the affix list "afflist".
3732 * Used for PFXPOSTPONE.
3733 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3734 * and return the number of affixes.
3735 */
3736 static int
3737get_pfxlist(
3738 afffile_T *affile,
3739 char_u *afflist,
3740 char_u *store_afflist)
3741{
3742 char_u *p;
3743 char_u *prevp;
3744 int cnt = 0;
3745 int id;
3746 char_u key[AH_KEY_LEN];
3747 hashitem_T *hi;
3748
3749 for (p = afflist; *p != NUL; )
3750 {
3751 prevp = p;
3752 if (get_affitem(affile->af_flagtype, &p) != 0)
3753 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003754 // A flag is a postponed prefix flag if it appears in "af_pref"
3755 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003756 vim_strncpy(key, prevp, p - prevp);
3757 hi = hash_find(&affile->af_pref, key);
3758 if (!HASHITEM_EMPTY(hi))
3759 {
3760 id = HI2AH(hi)->ah_newID;
3761 if (id != 0)
3762 store_afflist[cnt++] = id;
3763 }
3764 }
3765 if (affile->af_flagtype == AFT_NUM && *p == ',')
3766 ++p;
3767 }
3768
3769 store_afflist[cnt] = NUL;
3770 return cnt;
3771}
3772
3773/*
3774 * Get the list of compound IDs from the affix list "afflist" that are used
3775 * for compound words.
3776 * Puts the flags in "store_afflist[]".
3777 */
3778 static void
3779get_compflags(
3780 afffile_T *affile,
3781 char_u *afflist,
3782 char_u *store_afflist)
3783{
3784 char_u *p;
3785 char_u *prevp;
3786 int cnt = 0;
3787 char_u key[AH_KEY_LEN];
3788 hashitem_T *hi;
3789
3790 for (p = afflist; *p != NUL; )
3791 {
3792 prevp = p;
3793 if (get_affitem(affile->af_flagtype, &p) != 0)
3794 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003795 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003796 vim_strncpy(key, prevp, p - prevp);
3797 hi = hash_find(&affile->af_comp, key);
3798 if (!HASHITEM_EMPTY(hi))
3799 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3800 }
3801 if (affile->af_flagtype == AFT_NUM && *p == ',')
3802 ++p;
3803 }
3804
3805 store_afflist[cnt] = NUL;
3806}
3807
3808/*
3809 * Apply affixes to a word and store the resulting words.
3810 * "ht" is the hashtable with affentry_T that need to be applied, either
3811 * prefixes or suffixes.
3812 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3813 * the resulting words for combining affixes.
3814 *
3815 * Returns FAIL when out of memory.
3816 */
3817 static int
3818store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003819 spellinfo_T *spin, // spell info
3820 char_u *word, // basic word start
3821 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003822 afffile_T *affile,
3823 hashtab_T *ht,
3824 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003825 int condit, // CONDIT_SUF et al.
3826 int flags, // flags for the word
3827 char_u *pfxlist, // list of prefix IDs
3828 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3829 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003830{
3831 int todo;
3832 hashitem_T *hi;
3833 affheader_T *ah;
3834 affentry_T *ae;
3835 char_u newword[MAXWLEN];
3836 int retval = OK;
3837 int i, j;
3838 char_u *p;
3839 int use_flags;
3840 char_u *use_pfxlist;
3841 int use_pfxlen;
3842 int need_affix;
3843 char_u store_afflist[MAXWLEN];
3844 char_u pfx_pfxlist[MAXWLEN];
3845 size_t wordlen = STRLEN(word);
3846 int use_condit;
3847
3848 todo = (int)ht->ht_used;
3849 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3850 {
3851 if (!HASHITEM_EMPTY(hi))
3852 {
3853 --todo;
3854 ah = HI2AH(hi);
3855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003856 // Check that the affix combines, if required, and that the word
3857 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003858 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3859 && flag_in_afflist(affile->af_flagtype, afflist,
3860 ah->ah_flag))
3861 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003862 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003863 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3864 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003865 // Check the condition. It's not logical to match case
3866 // here, but it is required for compatibility with
3867 // Myspell.
3868 // Another requirement from Myspell is that the chop
3869 // string is shorter than the word itself.
3870 // For prefixes, when "PFXPOSTPONE" was used, only do
3871 // prefixes with a chop string and/or flags.
3872 // When a previously added affix had CIRCUMFIX this one
3873 // must have it too, if it had not then this one must not
3874 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003875 if ((xht != NULL || !affile->af_pfxpostpone
3876 || ae->ae_chop != NULL
3877 || ae->ae_flags != NULL)
3878 && (ae->ae_chop == NULL
3879 || STRLEN(ae->ae_chop) < wordlen)
3880 && (ae->ae_prog == NULL
3881 || vim_regexec_prog(&ae->ae_prog, FALSE,
3882 word, (colnr_T)0))
3883 && (((condit & CONDIT_CFIX) == 0)
3884 == ((condit & CONDIT_AFF) == 0
3885 || ae->ae_flags == NULL
3886 || !flag_in_afflist(affile->af_flagtype,
3887 ae->ae_flags, affile->af_circumfix))))
3888 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003889 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003890 if (xht == NULL)
3891 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003892 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003893 if (ae->ae_add == NULL)
3894 *newword = NUL;
3895 else
3896 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3897 p = word;
3898 if (ae->ae_chop != NULL)
3899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003900 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003901 if (has_mbyte)
3902 {
3903 i = mb_charlen(ae->ae_chop);
3904 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003905 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003906 }
3907 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003908 p += STRLEN(ae->ae_chop);
3909 }
3910 STRCAT(newword, p);
3911 }
3912 else
3913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003914 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003915 vim_strncpy(newword, word, MAXWLEN - 1);
3916 if (ae->ae_chop != NULL)
3917 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003918 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003919 p = newword + STRLEN(newword);
3920 i = (int)MB_CHARLEN(ae->ae_chop);
3921 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003922 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003923 *p = NUL;
3924 }
3925 if (ae->ae_add != NULL)
3926 STRCAT(newword, ae->ae_add);
3927 }
3928
3929 use_flags = flags;
3930 use_pfxlist = pfxlist;
3931 use_pfxlen = pfxlen;
3932 need_affix = FALSE;
3933 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3934 if (ae->ae_flags != NULL)
3935 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003936 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003937 use_flags |= get_affix_flags(affile, ae->ae_flags);
3938
3939 if (affile->af_needaffix != 0 && flag_in_afflist(
3940 affile->af_flagtype, ae->ae_flags,
3941 affile->af_needaffix))
3942 need_affix = TRUE;
3943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003944 // When there is a CIRCUMFIX flag the other affix
3945 // must also have it and we don't add the word
3946 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003947 if (affile->af_circumfix != 0 && flag_in_afflist(
3948 affile->af_flagtype, ae->ae_flags,
3949 affile->af_circumfix))
3950 {
3951 use_condit |= CONDIT_CFIX;
3952 if ((condit & CONDIT_CFIX) == 0)
3953 need_affix = TRUE;
3954 }
3955
3956 if (affile->af_pfxpostpone
3957 || spin->si_compflags != NULL)
3958 {
3959 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003960 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003961 use_pfxlen = get_pfxlist(affile,
3962 ae->ae_flags, store_afflist);
3963 else
3964 use_pfxlen = 0;
3965 use_pfxlist = store_afflist;
3966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003967 // Combine the prefix IDs. Avoid adding the
3968 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003969 for (i = 0; i < pfxlen; ++i)
3970 {
3971 for (j = 0; j < use_pfxlen; ++j)
3972 if (pfxlist[i] == use_pfxlist[j])
3973 break;
3974 if (j == use_pfxlen)
3975 use_pfxlist[use_pfxlen++] = pfxlist[i];
3976 }
3977
3978 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003979 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003980 get_compflags(affile, ae->ae_flags,
3981 use_pfxlist + use_pfxlen);
3982
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003983 // Combine the list of compound flags.
3984 // Concatenate them to the prefix IDs list.
3985 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003986 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3987 {
3988 for (j = use_pfxlen;
3989 use_pfxlist[j] != NUL; ++j)
3990 if (pfxlist[i] == use_pfxlist[j])
3991 break;
3992 if (use_pfxlist[j] == NUL)
3993 {
3994 use_pfxlist[j++] = pfxlist[i];
3995 use_pfxlist[j] = NUL;
3996 }
3997 }
3998 }
3999 }
4000
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004001 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
4002 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004003 if (use_pfxlist != NULL && ae->ae_compforbid)
4004 {
4005 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
4006 use_pfxlist = pfx_pfxlist;
4007 }
4008
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004009 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004010 if (spin->si_prefroot != NULL
4011 && spin->si_prefroot->wn_sibling != NULL)
4012 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004013 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004014 use_flags |= WF_HAS_AFF;
4015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004016 // ... don't use a prefix list if combining
4017 // affixes is not allowed. But do use the
4018 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004019 if (!ah->ah_combine && use_pfxlist != NULL)
4020 use_pfxlist += use_pfxlen;
4021 }
4022
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004023 // When compounding is supported and there is no
4024 // "COMPOUNDPERMITFLAG" then forbid compounding on the
4025 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004026 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4027 {
4028 if (xht != NULL)
4029 use_flags |= WF_NOCOMPAFT;
4030 else
4031 use_flags |= WF_NOCOMPBEF;
4032 }
4033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004034 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004035 if (store_word(spin, newword, use_flags,
4036 spin->si_region, use_pfxlist,
4037 need_affix) == FAIL)
4038 retval = FAIL;
4039
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004040 // When added a prefix or a first suffix and the affix
4041 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004042 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4043 if (store_aff_word(spin, newword, ae->ae_flags,
4044 affile, &affile->af_suff, xht,
4045 use_condit & (xht == NULL
4046 ? ~0 : ~CONDIT_SUF),
4047 use_flags, use_pfxlist, pfxlen) == FAIL)
4048 retval = FAIL;
4049
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004050 // When added a suffix and combining is allowed also
4051 // try adding a prefix additionally. Both for the
4052 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004053 if (xht != NULL && ah->ah_combine)
4054 {
4055 if (store_aff_word(spin, newword,
4056 afflist, affile,
4057 xht, NULL, use_condit,
4058 use_flags, use_pfxlist,
4059 pfxlen) == FAIL
4060 || (ae->ae_flags != NULL
4061 && store_aff_word(spin, newword,
4062 ae->ae_flags, affile,
4063 xht, NULL, use_condit,
4064 use_flags, use_pfxlist,
4065 pfxlen) == FAIL))
4066 retval = FAIL;
4067 }
4068 }
4069 }
4070 }
4071 }
4072 }
4073
4074 return retval;
4075}
4076
4077/*
4078 * Read a file with a list of words.
4079 */
4080 static int
4081spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4082{
4083 FILE *fd;
4084 long lnum = 0;
4085 char_u rline[MAXLINELEN];
4086 char_u *line;
4087 char_u *pc = NULL;
4088 char_u *p;
4089 int l;
4090 int retval = OK;
4091 int did_word = FALSE;
4092 int non_ascii = 0;
4093 int flags;
4094 int regionmask;
4095
4096 /*
4097 * Open the file.
4098 */
4099 fd = mch_fopen((char *)fname, "r");
4100 if (fd == NULL)
4101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004102 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004103 return FAIL;
4104 }
4105
Bram Moolenaarc1669272018-06-19 14:23:53 +02004106 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004107 spell_message(spin, IObuff);
4108
4109 /*
4110 * Read all the lines in the file one by one.
4111 */
4112 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4113 {
4114 line_breakcheck();
4115 ++lnum;
4116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004117 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004118 if (*rline == '#')
4119 continue;
4120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004121 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004122 l = (int)STRLEN(rline);
4123 while (l > 0 && rline[l - 1] <= ' ')
4124 --l;
4125 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004126 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004127 rline[l] = NUL;
4128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004129 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004130 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004131 if (spin->si_conv.vc_type != CONV_NONE)
4132 {
4133 pc = string_convert(&spin->si_conv, rline, NULL);
4134 if (pc == NULL)
4135 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004136 smsg(_("Conversion failure for word in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004137 fname, lnum, rline);
4138 continue;
4139 }
4140 line = pc;
4141 }
4142 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004143 {
4144 pc = NULL;
4145 line = rline;
4146 }
4147
4148 if (*line == '/')
4149 {
4150 ++line;
4151 if (STRNCMP(line, "encoding=", 9) == 0)
4152 {
4153 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004154 smsg(_("Duplicate /encoding= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004155 fname, lnum, line - 1);
4156 else if (did_word)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004157 smsg(_("/encoding= line after word ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004158 fname, lnum, line - 1);
4159 else
4160 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004161 char_u *enc;
4162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004163 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004164 line += 9;
4165 enc = enc_canonize(line);
4166 if (enc != NULL && !spin->si_ascii
4167 && convert_setup(&spin->si_conv, enc,
4168 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004169 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004170 fname, line, p_enc);
4171 vim_free(enc);
4172 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004173 }
4174 continue;
4175 }
4176
4177 if (STRNCMP(line, "regions=", 8) == 0)
4178 {
4179 if (spin->si_region_count > 1)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004180 smsg(_("Duplicate /regions= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004181 fname, lnum, line);
4182 else
4183 {
4184 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004185 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004186 smsg(_("Too many regions in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004187 fname, lnum, line);
4188 else
4189 {
4190 spin->si_region_count = (int)STRLEN(line) / 2;
4191 STRCPY(spin->si_region_name, line);
4192
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004193 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004194 spin->si_region = (1 << spin->si_region_count) - 1;
4195 }
4196 }
4197 continue;
4198 }
4199
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004200 smsg(_("/ line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004201 fname, lnum, line - 1);
4202 continue;
4203 }
4204
4205 flags = 0;
4206 regionmask = spin->si_region;
4207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004208 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004209 p = vim_strchr(line, '/');
4210 if (p != NULL)
4211 {
4212 *p++ = NUL;
4213 while (*p != NUL)
4214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004215 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004216 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004217 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004218 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004219 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004220 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004221 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004224 regionmask = 0;
4225 flags |= WF_REGION;
4226
4227 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004228 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004229 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004230 smsg(_("Invalid region nr in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004231 fname, lnum, p);
4232 break;
4233 }
4234 regionmask |= 1 << (l - 1);
4235 }
4236 else
4237 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004238 smsg(_("Unrecognized flags in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004239 fname, lnum, p);
4240 break;
4241 }
4242 ++p;
4243 }
4244 }
4245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004246 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004247 if (spin->si_ascii && has_non_ascii(line))
4248 {
4249 ++non_ascii;
4250 continue;
4251 }
4252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004253 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004254 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4255 {
4256 retval = FAIL;
4257 break;
4258 }
4259 did_word = TRUE;
4260 }
4261
4262 vim_free(pc);
4263 fclose(fd);
4264
4265 if (spin->si_ascii && non_ascii > 0)
4266 {
4267 vim_snprintf((char *)IObuff, IOSIZE,
4268 _("Ignored %d words with non-ASCII characters"), non_ascii);
4269 spell_message(spin, IObuff);
4270 }
4271
4272 return retval;
4273}
4274
4275/*
4276 * Get part of an sblock_T, "len" bytes long.
4277 * This avoids calling free() for every little struct we use (and keeping
4278 * track of them).
4279 * The memory is cleared to all zeros.
4280 * Returns NULL when out of memory.
4281 */
4282 static void *
4283getroom(
4284 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004285 size_t len, // length needed
4286 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004287{
4288 char_u *p;
4289 sblock_T *bl = spin->si_blocks;
4290
4291 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004292 // Round size up for alignment. On some systems structures need to be
4293 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004294 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4295 & ~(sizeof(char *) - 1);
4296
4297 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4298 {
4299 if (len >= SBLOCKSIZE)
4300 bl = NULL;
4301 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004302 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004303 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004304 if (bl == NULL)
4305 {
4306 if (!spin->si_did_emsg)
4307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004308 emsg(_("E845: Insufficient memory, word list will be incomplete"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004309 spin->si_did_emsg = TRUE;
4310 }
4311 return NULL;
4312 }
4313 bl->sb_next = spin->si_blocks;
4314 spin->si_blocks = bl;
4315 bl->sb_used = 0;
4316 ++spin->si_blocks_cnt;
4317 }
4318
4319 p = bl->sb_data + bl->sb_used;
4320 bl->sb_used += (int)len;
4321
4322 return p;
4323}
4324
4325/*
4326 * Make a copy of a string into memory allocated with getroom().
4327 * Returns NULL when out of memory.
4328 */
4329 static char_u *
4330getroom_save(spellinfo_T *spin, char_u *s)
4331{
4332 char_u *sc;
4333
4334 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4335 if (sc != NULL)
4336 STRCPY(sc, s);
4337 return sc;
4338}
4339
4340
4341/*
4342 * Free the list of allocated sblock_T.
4343 */
4344 static void
4345free_blocks(sblock_T *bl)
4346{
4347 sblock_T *next;
4348
4349 while (bl != NULL)
4350 {
4351 next = bl->sb_next;
4352 vim_free(bl);
4353 bl = next;
4354 }
4355}
4356
4357/*
4358 * Allocate the root of a word tree.
4359 * Returns NULL when out of memory.
4360 */
4361 static wordnode_T *
4362wordtree_alloc(spellinfo_T *spin)
4363{
4364 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4365}
4366
4367/*
4368 * Store a word in the tree(s).
4369 * Always store it in the case-folded tree. For a keep-case word this is
4370 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4371 * used to find suggestions.
4372 * For a keep-case word also store it in the keep-case tree.
4373 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4374 * compound flag.
4375 */
4376 static int
4377store_word(
4378 spellinfo_T *spin,
4379 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004380 int flags, // extra flags, WF_BANNED
4381 int region, // supported region(s)
4382 char_u *pfxlist, // list of prefix IDs or NULL
4383 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004384{
4385 int len = (int)STRLEN(word);
4386 int ct = captype(word, word + len);
4387 char_u foldword[MAXWLEN];
4388 int res = OK;
4389 char_u *p;
4390
4391 (void)spell_casefold(word, len, foldword, MAXWLEN);
4392 for (p = pfxlist; res == OK; ++p)
4393 {
4394 if (!need_affix || (p != NULL && *p != NUL))
4395 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4396 region, p == NULL ? 0 : *p);
4397 if (p == NULL || *p == NUL)
4398 break;
4399 }
4400 ++spin->si_foldwcount;
4401
4402 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4403 {
4404 for (p = pfxlist; res == OK; ++p)
4405 {
4406 if (!need_affix || (p != NULL && *p != NUL))
4407 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4408 region, p == NULL ? 0 : *p);
4409 if (p == NULL || *p == NUL)
4410 break;
4411 }
4412 ++spin->si_keepwcount;
4413 }
4414 return res;
4415}
4416
4417/*
4418 * Add word "word" to a word tree at "root".
4419 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4420 * "rare" and "region" is the condition nr.
4421 * Returns FAIL when out of memory.
4422 */
4423 static int
4424tree_add_word(
4425 spellinfo_T *spin,
4426 char_u *word,
4427 wordnode_T *root,
4428 int flags,
4429 int region,
4430 int affixID)
4431{
4432 wordnode_T *node = root;
4433 wordnode_T *np;
4434 wordnode_T *copyp, **copyprev;
4435 wordnode_T **prev = NULL;
4436 int i;
4437
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004438 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004439 for (i = 0; ; ++i)
4440 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004441 // When there is more than one reference to this node we need to make
4442 // a copy, so that we can modify it. Copy the whole list of siblings
4443 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004444 if (node != NULL && node->wn_refs > 1)
4445 {
4446 --node->wn_refs;
4447 copyprev = prev;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004448 FOR_ALL_NODE_SIBLINGS(node, copyp)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004449 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004450 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004451 np = get_wordnode(spin);
4452 if (np == NULL)
4453 return FAIL;
4454 np->wn_child = copyp->wn_child;
4455 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004456 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004457 np->wn_byte = copyp->wn_byte;
4458 if (np->wn_byte == NUL)
4459 {
4460 np->wn_flags = copyp->wn_flags;
4461 np->wn_region = copyp->wn_region;
4462 np->wn_affixID = copyp->wn_affixID;
4463 }
4464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004465 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004466 np->wn_refs = 1;
4467 if (copyprev != NULL)
4468 *copyprev = np;
4469 copyprev = &np->wn_sibling;
4470
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004471 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004472 if (copyp == node)
4473 node = np;
4474 }
4475 }
4476
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004477 // Look for the sibling that has the same character. They are sorted
4478 // on byte value, thus stop searching when a sibling is found with a
4479 // higher byte value. For zero bytes (end of word) the sorting is
4480 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004481 while (node != NULL
4482 && (node->wn_byte < word[i]
4483 || (node->wn_byte == NUL
4484 && (flags < 0
4485 ? node->wn_affixID < (unsigned)affixID
4486 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4487 || (node->wn_flags == (flags & WN_MASK)
4488 && (spin->si_sugtree
4489 ? (node->wn_region & 0xffff) < region
4490 : node->wn_affixID
4491 < (unsigned)affixID)))))))
4492 {
4493 prev = &node->wn_sibling;
4494 node = *prev;
4495 }
4496 if (node == NULL
4497 || node->wn_byte != word[i]
4498 || (word[i] == NUL
4499 && (flags < 0
4500 || spin->si_sugtree
4501 || node->wn_flags != (flags & WN_MASK)
4502 || node->wn_affixID != affixID)))
4503 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004504 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004505 np = get_wordnode(spin);
4506 if (np == NULL)
4507 return FAIL;
4508 np->wn_byte = word[i];
4509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004510 // If "node" is NULL this is a new child or the end of the sibling
4511 // list: ref count is one. Otherwise use ref count of sibling and
4512 // make ref count of sibling one (matters when inserting in front
4513 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004514 if (node == NULL)
4515 np->wn_refs = 1;
4516 else
4517 {
4518 np->wn_refs = node->wn_refs;
4519 node->wn_refs = 1;
4520 }
4521 if (prev != NULL)
4522 *prev = np;
4523 np->wn_sibling = node;
4524 node = np;
4525 }
4526
4527 if (word[i] == NUL)
4528 {
4529 node->wn_flags = flags;
4530 node->wn_region |= region;
4531 node->wn_affixID = affixID;
4532 break;
4533 }
4534 prev = &node->wn_child;
4535 node = *prev;
4536 }
4537#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004538 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004539 spell_print_tree(root->wn_sibling);
4540#endif
4541
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004542 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004543 ++spin->si_msg_count;
4544
4545 if (spin->si_compress_cnt > 1)
4546 {
4547 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004548 // Did enough words to lower the block count limit.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004549 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004550 }
4551
4552 /*
4553 * When we have allocated lots of memory we need to compress the word tree
4554 * to free up some room. But compression is slow, and we might actually
4555 * need that room, thus only compress in the following situations:
4556 * 1. When not compressed before (si_compress_cnt == 0): when using
4557 * "compress_start" blocks.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004558 * 2. When compressed before and used "compress_inc" blocks before
4559 * adding "compress_added" words (si_compress_cnt > 1).
4560 * 3. When compressed before, added "compress_added" words
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004561 * (si_compress_cnt == 1) and the number of free nodes drops below the
4562 * maximum word length.
4563 */
4564#ifndef SPELL_COMPRESS_ALLWAYS
4565 if (spin->si_compress_cnt == 1
4566 ? spin->si_free_count < MAXWLEN
4567 : spin->si_blocks_cnt >= compress_start)
4568#endif
4569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004570 // Decrement the block counter. The effect is that we compress again
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004571 // when the freed up room has been used and another "compress_inc"
4572 // blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004573 // been added, then the limit is put back again.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004574 spin->si_blocks_cnt -= compress_inc;
4575 spin->si_compress_cnt = compress_added;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004576
4577 if (spin->si_verbose)
4578 {
4579 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004580 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004581 msg_clr_eos();
4582 msg_didout = FALSE;
4583 msg_col = 0;
4584 out_flush();
4585 }
4586
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004587 // Compress both trees. Either they both have many nodes, which makes
4588 // compression useful, or one of them is small, which means
4589 // compression goes fast. But when filling the soundfold word tree
4590 // there is no keep-case tree.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004591 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004592 if (affixID >= 0)
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004593 wordtree_compress(spin, spin->si_keeproot, "keep-case");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004594 }
4595
4596 return OK;
4597}
4598
4599/*
4600 * Get a wordnode_T, either from the list of previously freed nodes or
4601 * allocate a new one.
4602 * Returns NULL when out of memory.
4603 */
4604 static wordnode_T *
4605get_wordnode(spellinfo_T *spin)
4606{
4607 wordnode_T *n;
4608
4609 if (spin->si_first_free == NULL)
4610 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4611 else
4612 {
4613 n = spin->si_first_free;
4614 spin->si_first_free = n->wn_child;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004615 CLEAR_POINTER(n);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004616 --spin->si_free_count;
4617 }
4618#ifdef SPELL_PRINTTREE
4619 if (n != NULL)
4620 n->wn_nr = ++spin->si_wordnode_nr;
4621#endif
4622 return n;
4623}
4624
4625/*
4626 * Decrement the reference count on a node (which is the head of a list of
4627 * siblings). If the reference count becomes zero free the node and its
4628 * siblings.
4629 * Returns the number of nodes actually freed.
4630 */
4631 static int
4632deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4633{
4634 wordnode_T *np;
4635 int cnt = 0;
4636
4637 if (--node->wn_refs == 0)
4638 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004639 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004640 {
4641 if (np->wn_child != NULL)
4642 cnt += deref_wordnode(spin, np->wn_child);
4643 free_wordnode(spin, np);
4644 ++cnt;
4645 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004646 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004647 }
4648 return cnt;
4649}
4650
4651/*
4652 * Free a wordnode_T for re-use later.
4653 * Only the "wn_child" field becomes invalid.
4654 */
4655 static void
4656free_wordnode(spellinfo_T *spin, wordnode_T *n)
4657{
4658 n->wn_child = spin->si_first_free;
4659 spin->si_first_free = n;
4660 ++spin->si_free_count;
4661}
4662
4663/*
4664 * Compress a tree: find tails that are identical and can be shared.
4665 */
4666 static void
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004667wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004668{
4669 hashtab_T ht;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004670 long n;
4671 long tot = 0;
4672 long perc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004674 // Skip the root itself, it's not actually used. The first sibling is the
4675 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004676 if (root->wn_sibling != NULL)
4677 {
4678 hash_init(&ht);
4679 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4680
4681#ifndef SPELL_PRINTTREE
4682 if (spin->si_verbose || p_verbose > 2)
4683#endif
4684 {
4685 if (tot > 1000000)
4686 perc = (tot - n) / (tot / 100);
4687 else if (tot == 0)
4688 perc = 0;
4689 else
4690 perc = (tot - n) * 100 / tot;
4691 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004692 _("Compressed %s: %ld of %ld nodes; %ld (%ld%%) remaining"),
4693 name, n, tot, tot - n, perc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004694 spell_message(spin, IObuff);
4695 }
4696#ifdef SPELL_PRINTTREE
4697 spell_print_tree(root->wn_sibling);
4698#endif
4699 hash_clear(&ht);
4700 }
4701}
4702
4703/*
4704 * Compress a node, its siblings and its children, depth first.
4705 * Returns the number of compressed nodes.
4706 */
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004707 static long
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004708node_compress(
4709 spellinfo_T *spin,
4710 wordnode_T *node,
4711 hashtab_T *ht,
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004712 long *tot) // total count of nodes before compressing,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004713 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004714{
4715 wordnode_T *np;
4716 wordnode_T *tp;
4717 wordnode_T *child;
4718 hash_T hash;
4719 hashitem_T *hi;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004720 long len = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004721 unsigned nr, n;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004722 long compressed = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004723
4724 /*
4725 * Go through the list of siblings. Compress each child and then try
4726 * finding an identical child to replace it.
4727 * Note that with "child" we mean not just the node that is pointed to,
4728 * but the whole list of siblings of which the child node is the first.
4729 */
4730 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4731 {
4732 ++len;
4733 if ((child = np->wn_child) != NULL)
4734 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004735 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004736 compressed += node_compress(spin, child, ht, tot);
4737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004738 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004739 hash = hash_hash(child->wn_u1.hashkey);
4740 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4741 if (!HASHITEM_EMPTY(hi))
4742 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004743 // There are children we encountered before with a hash value
4744 // identical to the current child. Now check if there is one
4745 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004746 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4747 if (node_equal(child, tp))
4748 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004749 // Found one! Now use that child in place of the
4750 // current one. This means the current child and all
4751 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004752 ++tp->wn_refs;
4753 compressed += deref_wordnode(spin, child);
4754 np->wn_child = tp;
4755 break;
4756 }
4757 if (tp == NULL)
4758 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004759 // No other child with this hash value equals the child of
4760 // the node, add it to the linked list after the first
4761 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004762 tp = HI2WN(hi);
4763 child->wn_u2.next = tp->wn_u2.next;
4764 tp->wn_u2.next = child;
4765 }
4766 }
4767 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004768 // No other child has this hash value, add it to the
4769 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004770 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4771 }
4772 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004773 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004774
4775 /*
4776 * Make a hash key for the node and its siblings, so that we can quickly
4777 * find a lookalike node. This must be done after compressing the sibling
4778 * list, otherwise the hash key would become invalid by the compression.
4779 */
4780 node->wn_u1.hashkey[0] = len;
4781 nr = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004782 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004783 {
4784 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004785 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004786 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4787 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004788 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004789 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4790 nr = nr * 101 + n;
4791 }
4792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004793 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004794 n = nr & 0xff;
4795 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4796 n = (nr >> 8) & 0xff;
4797 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4798 n = (nr >> 16) & 0xff;
4799 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4800 n = (nr >> 24) & 0xff;
4801 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4802 node->wn_u1.hashkey[5] = NUL;
4803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004804 // Check for CTRL-C pressed now and then.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004805 veryfast_breakcheck();
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004806
4807 return compressed;
4808}
4809
4810/*
4811 * Return TRUE when two nodes have identical siblings and children.
4812 */
4813 static int
4814node_equal(wordnode_T *n1, wordnode_T *n2)
4815{
4816 wordnode_T *p1;
4817 wordnode_T *p2;
4818
4819 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4820 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4821 if (p1->wn_byte != p2->wn_byte
4822 || (p1->wn_byte == NUL
4823 ? (p1->wn_flags != p2->wn_flags
4824 || p1->wn_region != p2->wn_region
4825 || p1->wn_affixID != p2->wn_affixID)
4826 : (p1->wn_child != p2->wn_child)))
4827 break;
4828
4829 return p1 == NULL && p2 == NULL;
4830}
4831
Bram Moolenaareae1b912019-05-09 15:12:55 +02004832static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004833
4834/*
4835 * Function given to qsort() to sort the REP items on "from" string.
4836 */
4837 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004838rep_compare(const void *s1, const void *s2)
4839{
4840 fromto_T *p1 = (fromto_T *)s1;
4841 fromto_T *p2 = (fromto_T *)s2;
4842
4843 return STRCMP(p1->ft_from, p2->ft_from);
4844}
4845
4846/*
4847 * Write the Vim .spl file "fname".
4848 * Return FAIL or OK;
4849 */
4850 static int
4851write_vim_spell(spellinfo_T *spin, char_u *fname)
4852{
4853 FILE *fd;
4854 int regionmask;
4855 int round;
4856 wordnode_T *tree;
4857 int nodecount;
4858 int i;
4859 int l;
4860 garray_T *gap;
4861 fromto_T *ftp;
4862 char_u *p;
4863 int rr;
4864 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004865 size_t fwv = 1; // collect return value of fwrite() to avoid
4866 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004867
4868 fd = mch_fopen((char *)fname, "w");
4869 if (fd == NULL)
4870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004871 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004872 return FAIL;
4873 }
4874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004875 // <HEADER>: <fileID> <versionnr>
4876 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004877 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4878 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004879 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004880 goto theend;
4881
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004882 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004883
4884 /*
4885 * <SECTIONS>: <section> ... <sectionend>
4886 */
4887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004888 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004889 if (spin->si_info != NULL)
4890 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004891 putc(SN_INFO, fd); // <sectionID>
4892 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004893
4894 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004895 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4896 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004897 }
4898
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004899 // SN_REGION: <regionname> ...
4900 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004901 if (spin->si_region_count > 1)
4902 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004903 putc(SN_REGION, fd); // <sectionID>
4904 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004905 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004906 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004907 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004908 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004909 regionmask = (1 << spin->si_region_count) - 1;
4910 }
4911 else
4912 regionmask = 0;
4913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004914 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4915 //
4916 // The table with character flags and the table for case folding.
4917 // This makes sure the same characters are recognized as word characters
4918 // when generating an when using a spell file.
4919 // Skip this for ASCII, the table may conflict with the one used for
4920 // 'encoding'.
4921 // Also skip this for an .add.spl file, the main spell file must contain
4922 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004923 if (!spin->si_ascii && !spin->si_add)
4924 {
4925 char_u folchars[128 * 8];
4926 int flags;
4927
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004928 putc(SN_CHARFLAGS, fd); // <sectionID>
4929 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004931 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004932 l = 0;
4933 for (i = 128; i < 256; ++i)
4934 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004935 if (has_mbyte)
4936 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4937 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004938 folchars[l++] = spelltab.st_fold[i];
4939 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004940 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004941
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004942 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004943 for (i = 128; i < 256; ++i)
4944 {
4945 flags = 0;
4946 if (spelltab.st_isw[i])
4947 flags |= CF_WORD;
4948 if (spelltab.st_isu[i])
4949 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004950 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004951 }
4952
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004953 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4954 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004955 }
4956
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004957 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004958 if (spin->si_midword != NULL)
4959 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004960 putc(SN_MIDWORD, fd); // <sectionID>
4961 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004962
4963 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004964 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004965 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004966 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004967 }
4968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004969 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004970 if (spin->si_prefcond.ga_len > 0)
4971 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004972 putc(SN_PREFCOND, fd); // <sectionID>
4973 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004974
4975 l = write_spell_prefcond(NULL, &spin->si_prefcond);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004976 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004977
4978 write_spell_prefcond(fd, &spin->si_prefcond);
4979 }
4980
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004981 // SN_REP: <repcount> <rep> ...
4982 // SN_SAL: <salflags> <salcount> <sal> ...
4983 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004984
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004985 // round 1: SN_REP section
4986 // round 2: SN_SAL section (unless SN_SOFO is used)
4987 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004988 for (round = 1; round <= 3; ++round)
4989 {
4990 if (round == 1)
4991 gap = &spin->si_rep;
4992 else if (round == 2)
4993 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004994 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004995 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4996 continue;
4997 gap = &spin->si_sal;
4998 }
4999 else
5000 gap = &spin->si_repsal;
5001
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005002 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005003 if (gap->ga_len == 0)
5004 continue;
5005
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005006 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005007 if (round != 2)
5008 qsort(gap->ga_data, (size_t)gap->ga_len,
5009 sizeof(fromto_T), rep_compare);
5010
5011 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005014 // This is for making suggestions, section is not required.
5015 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005017 // Compute the length of what follows.
5018 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005019 for (i = 0; i < gap->ga_len; ++i)
5020 {
5021 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005022 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
5023 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005024 }
5025 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005026 ++l; // count <salflags>
5027 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005028
5029 if (round == 2)
5030 {
5031 i = 0;
5032 if (spin->si_followup)
5033 i |= SAL_F0LLOWUP;
5034 if (spin->si_collapse)
5035 i |= SAL_COLLAPSE;
5036 if (spin->si_rem_accents)
5037 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005038 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005039 }
5040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005041 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005042 for (i = 0; i < gap->ga_len; ++i)
5043 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005044 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5045 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005046 ftp = &((fromto_T *)gap->ga_data)[i];
5047 for (rr = 1; rr <= 2; ++rr)
5048 {
5049 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5050 l = (int)STRLEN(p);
5051 putc(l, fd);
5052 if (l > 0)
5053 fwv &= fwrite(p, l, (size_t)1, fd);
5054 }
5055 }
5056
5057 }
5058
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005059 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5060 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005061 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5062 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005063 putc(SN_SOFO, fd); // <sectionID>
5064 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005065
5066 l = (int)STRLEN(spin->si_sofofr);
5067 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005068 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005070 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5071 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005072
5073 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005074 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5075 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005076 }
5077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005078 // SN_WORDS: <word> ...
5079 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005080 if (spin->si_commonwords.ht_used > 0)
5081 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005082 putc(SN_WORDS, fd); // <sectionID>
5083 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005084
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005085 // round 1: count the bytes
5086 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005087 for (round = 1; round <= 2; ++round)
5088 {
5089 int todo;
5090 int len = 0;
5091 hashitem_T *hi;
5092
5093 todo = (int)spin->si_commonwords.ht_used;
5094 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5095 if (!HASHITEM_EMPTY(hi))
5096 {
5097 l = (int)STRLEN(hi->hi_key) + 1;
5098 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005099 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005100 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5101 --todo;
5102 }
5103 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005104 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005105 }
5106 }
5107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005108 // SN_MAP: <mapstr>
5109 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005110 if (spin->si_map.ga_len > 0)
5111 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 putc(SN_MAP, fd); // <sectionID>
5113 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005114 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005115 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005116 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005117 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005118 }
5119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005120 // SN_SUGFILE: <timestamp>
5121 // This is used to notify that a .sug file may be available and at the
5122 // same time allows for checking that a .sug file that is found matches
5123 // with this .spl file. That's because the word numbers must be exactly
5124 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005125 if (!spin->si_nosugfile
5126 && (spin->si_sal.ga_len > 0
5127 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5128 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005129 putc(SN_SUGFILE, fd); // <sectionID>
5130 putc(0, fd); // <sectionflags>
5131 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005133 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005134 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005135 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005136 }
5137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005138 // SN_NOSPLITSUGS: nothing
5139 // This is used to notify that no suggestions with word splits are to be
5140 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005141 if (spin->si_nosplitsugs)
5142 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005143 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5144 putc(0, fd); // <sectionflags>
5145 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005146 }
5147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005148 // SN_NOCOMPUNDSUGS: nothing
5149 // This is used to notify that no suggestions with compounds are to be
5150 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005151 if (spin->si_nocompoundsugs)
5152 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005153 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5154 putc(0, fd); // <sectionflags>
5155 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005156 }
5157
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005158 // SN_COMPOUND: compound info.
5159 // We don't mark it required, when not supported all compound words will
5160 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005161 if (spin->si_compflags != NULL)
5162 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005163 putc(SN_COMPOUND, fd); // <sectionID>
5164 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005165
5166 l = (int)STRLEN(spin->si_compflags);
5167 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5168 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005169 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005171 putc(spin->si_compmax, fd); // <compmax>
5172 putc(spin->si_compminlen, fd); // <compminlen>
5173 putc(spin->si_compsylmax, fd); // <compsylmax>
5174 putc(0, fd); // for Vim 7.0b compatibility
5175 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005176 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005177 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005178 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5179 {
5180 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005181 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005182 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005183 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005184 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005185 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005186 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5187 (size_t)1, fd);
5188 }
5189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005190 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005191 if (spin->si_nobreak)
5192 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005193 putc(SN_NOBREAK, fd); // <sectionID>
5194 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005195
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005196 // It's empty, the presence of the section flags the feature.
5197 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005198 }
5199
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005200 // SN_SYLLABLE: syllable info.
5201 // We don't mark it required, when not supported syllables will not be
5202 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005203 if (spin->si_syllable != NULL)
5204 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 putc(SN_SYLLABLE, fd); // <sectionID>
5206 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005207
5208 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005209 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005210 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005211 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005212 }
5213
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005214 // end of <SECTIONS>
5215 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005216
5217
5218 /*
5219 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5220 */
5221 spin->si_memtot = 0;
5222 for (round = 1; round <= 3; ++round)
5223 {
5224 if (round == 1)
5225 tree = spin->si_foldroot->wn_sibling;
5226 else if (round == 2)
5227 tree = spin->si_keeproot->wn_sibling;
5228 else
5229 tree = spin->si_prefroot->wn_sibling;
5230
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005231 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005232 clear_node(tree);
5233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005234 // Count the number of nodes. Needed to be able to allocate the
5235 // memory when reading the nodes. Also fills in index for shared
5236 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005237 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005239 // number of nodes in 4 bytes
5240 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005241 spin->si_memtot += nodecount + nodecount * sizeof(int);
5242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005243 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005244 (void)put_node(fd, tree, 0, regionmask, round == 3);
5245 }
5246
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005247 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005248 if (putc(0, fd) == EOF)
5249 retval = FAIL;
5250theend:
5251 if (fclose(fd) == EOF)
5252 retval = FAIL;
5253
5254 if (fwv != (size_t)1)
5255 retval = FAIL;
5256 if (retval == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005257 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005258
5259 return retval;
5260}
5261
5262/*
5263 * Clear the index and wnode fields of "node", it siblings and its
5264 * children. This is needed because they are a union with other items to save
5265 * space.
5266 */
5267 static void
5268clear_node(wordnode_T *node)
5269{
5270 wordnode_T *np;
5271
5272 if (node != NULL)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005273 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005274 {
5275 np->wn_u1.index = 0;
5276 np->wn_u2.wnode = NULL;
5277
5278 if (np->wn_byte != NUL)
5279 clear_node(np->wn_child);
5280 }
5281}
5282
5283
5284/*
5285 * Dump a word tree at node "node".
5286 *
5287 * This first writes the list of possible bytes (siblings). Then for each
5288 * byte recursively write the children.
5289 *
5290 * NOTE: The code here must match the code in read_tree_node(), since
5291 * assumptions are made about the indexes (so that we don't have to write them
5292 * in the file).
5293 *
5294 * Returns the number of nodes used.
5295 */
5296 static int
5297put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005299 wordnode_T *node,
5300 int idx,
5301 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005302 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005303{
5304 int newindex = idx;
5305 int siblingcount = 0;
5306 wordnode_T *np;
5307 int flags;
5308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005309 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005310 if (node == NULL)
5311 return 0;
5312
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005313 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005314 node->wn_u1.index = idx;
5315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005316 // Count the number of siblings.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005317 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005318 ++siblingcount;
5319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005320 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005321 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005322 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005324 // Write each sibling byte and optionally extra info.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005325 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005326 {
5327 if (np->wn_byte == 0)
5328 {
5329 if (fd != NULL)
5330 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005331 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005332 if (prefixtree)
5333 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005334 // In PREFIXTREE write the required affixID and the
5335 // associated condition nr (stored in wn_region). The
5336 // byte value is misused to store the "rare" and "not
5337 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005338 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005339 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005340 else
5341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005342 putc(BY_FLAGS, fd); // <byte>
5343 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005344 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005345 putc(np->wn_affixID, fd); // <affixID>
5346 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005347 }
5348 else
5349 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005350 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005351 flags = np->wn_flags;
5352 if (regionmask != 0 && np->wn_region != regionmask)
5353 flags |= WF_REGION;
5354 if (np->wn_affixID != 0)
5355 flags |= WF_AFX;
5356 if (flags == 0)
5357 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005358 // word without flags or region
5359 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005360 }
5361 else
5362 {
5363 if (np->wn_flags >= 0x100)
5364 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005365 putc(BY_FLAGS2, fd); // <byte>
5366 putc(flags, fd); // <flags>
5367 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005368 }
5369 else
5370 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005371 putc(BY_FLAGS, fd); // <byte>
5372 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005373 }
5374 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005375 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005376 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005377 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005378 }
5379 }
5380 }
5381 }
5382 else
5383 {
5384 if (np->wn_child->wn_u1.index != 0
5385 && np->wn_child->wn_u2.wnode != node)
5386 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005387 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005388 if (fd != NULL)
5389 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005390 putc(BY_INDEX, fd); // <byte>
5391 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005392 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5393 }
5394 }
5395 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005396 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005397 np->wn_child->wn_u2.wnode = node;
5398
5399 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005400 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005402 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005403 return 0;
5404 }
5405 }
5406 }
5407
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005408 // Space used in the array when reading: one for each sibling and one for
5409 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005410 newindex += siblingcount + 1;
5411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005412 // Recursively dump the children of each sibling.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005413 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005414 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5415 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5416 prefixtree);
5417
5418 return newindex;
5419}
5420
5421
5422/*
5423 * ":mkspell [-ascii] outfile infile ..."
5424 * ":mkspell [-ascii] addfile"
5425 */
5426 void
5427ex_mkspell(exarg_T *eap)
5428{
5429 int fcount;
5430 char_u **fnames;
5431 char_u *arg = eap->arg;
5432 int ascii = FALSE;
5433
5434 if (STRNCMP(arg, "-ascii", 6) == 0)
5435 {
5436 ascii = TRUE;
5437 arg = skipwhite(arg + 6);
5438 }
5439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005440 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005441 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5442 {
5443 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5444 FreeWild(fcount, fnames);
5445 }
5446}
5447
5448/*
5449 * Create the .sug file.
5450 * Uses the soundfold info in "spin".
5451 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5452 */
5453 static void
5454spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5455{
5456 char_u *fname = NULL;
5457 int len;
5458 slang_T *slang;
5459 int free_slang = FALSE;
5460
5461 /*
5462 * Read back the .spl file that was written. This fills the required
5463 * info for soundfolding. This also uses less memory than the
5464 * pointer-linked version of the trie. And it avoids having two versions
5465 * of the code for the soundfolding stuff.
5466 * It might have been done already by spell_reload_one().
5467 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005468 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005469 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005470 break;
5471 if (slang == NULL)
5472 {
5473 spell_message(spin, (char_u *)_("Reading back spell file..."));
5474 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5475 if (slang == NULL)
5476 return;
5477 free_slang = TRUE;
5478 }
5479
5480 /*
5481 * Clear the info in "spin" that is used.
5482 */
5483 spin->si_blocks = NULL;
5484 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005485 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005486 spin->si_free_count = 0;
5487 spin->si_first_free = NULL;
5488 spin->si_foldwcount = 0;
5489
5490 /*
5491 * Go through the trie of good words, soundfold each word and add it to
5492 * the soundfold trie.
5493 */
5494 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5495 if (sug_filltree(spin, slang) == FAIL)
5496 goto theend;
5497
5498 /*
5499 * Create the table which links each soundfold word with a list of the
5500 * good words it may come from. Creates buffer "spin->si_spellbuf".
5501 * This also removes the wordnr from the NUL byte entries to make
5502 * compression possible.
5503 */
5504 if (sug_maketable(spin) == FAIL)
5505 goto theend;
5506
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005507 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005508 (long)spin->si_spellbuf->b_ml.ml_line_count);
5509
5510 /*
5511 * Compress the soundfold trie.
5512 */
5513 spell_message(spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02005514 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005515
5516 /*
5517 * Write the .sug file.
5518 * Make the file name by changing ".spl" to ".sug".
5519 */
5520 fname = alloc(MAXPATHL);
5521 if (fname == NULL)
5522 goto theend;
5523 vim_strncpy(fname, wfname, MAXPATHL - 1);
5524 len = (int)STRLEN(fname);
5525 fname[len - 2] = 'u';
5526 fname[len - 1] = 'g';
5527 sug_write(spin, fname);
5528
5529theend:
5530 vim_free(fname);
5531 if (free_slang)
5532 slang_free(slang);
5533 free_blocks(spin->si_blocks);
5534 close_spellbuf(spin->si_spellbuf);
5535}
5536
5537/*
5538 * Build the soundfold trie for language "slang".
5539 */
5540 static int
5541sug_filltree(spellinfo_T *spin, slang_T *slang)
5542{
5543 char_u *byts;
5544 idx_T *idxs;
5545 int depth;
5546 idx_T arridx[MAXWLEN];
5547 int curi[MAXWLEN];
5548 char_u tword[MAXWLEN];
5549 char_u tsalword[MAXWLEN];
5550 int c;
5551 idx_T n;
5552 unsigned words_done = 0;
5553 int wordcount[MAXWLEN];
5554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005555 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005556 spin->si_foldroot = wordtree_alloc(spin);
5557 if (spin->si_foldroot == NULL)
5558 return FAIL;
5559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005560 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005561 spin->si_sugtree = TRUE;
5562
5563 /*
5564 * Go through the whole case-folded tree, soundfold each word and put it
5565 * in the trie.
5566 */
5567 byts = slang->sl_fbyts;
5568 idxs = slang->sl_fidxs;
5569
5570 arridx[0] = 0;
5571 curi[0] = 1;
5572 wordcount[0] = 0;
5573
5574 depth = 0;
5575 while (depth >= 0 && !got_int)
5576 {
5577 if (curi[depth] > byts[arridx[depth]])
5578 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005579 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005580 idxs[arridx[depth]] = wordcount[depth];
5581 if (depth > 0)
5582 wordcount[depth - 1] += wordcount[depth];
5583
5584 --depth;
5585 line_breakcheck();
5586 }
5587 else
5588 {
5589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005590 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005591 n = arridx[depth] + curi[depth];
5592 ++curi[depth];
5593
5594 c = byts[n];
5595 if (c == 0)
5596 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005597 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005598 tword[depth] = NUL;
5599 spell_soundfold(slang, tword, TRUE, tsalword);
5600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005601 // We use the "flags" field for the MSB of the wordnr,
5602 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005603 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5604 words_done >> 16, words_done & 0xffff,
5605 0) == FAIL)
5606 return FAIL;
5607
5608 ++words_done;
5609 ++wordcount[depth];
5610
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005611 // Reset the block count each time to avoid compression
5612 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005613 spin->si_blocks_cnt = 0;
5614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615 // Skip over any other NUL bytes (same word with different
Bram Moolenaar07399e72020-08-24 20:05:50 +02005616 // flags). But don't go over the end.
5617 while (n + 1 < slang->sl_fbyts_len && byts[n + 1] == 0)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005618 {
5619 ++n;
5620 ++curi[depth];
5621 }
5622 }
5623 else
5624 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005625 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005626 tword[depth++] = c;
5627 arridx[depth] = idxs[n];
5628 curi[depth] = 1;
5629 wordcount[depth] = 0;
5630 }
5631 }
5632 }
5633
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005634 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005635
5636 return OK;
5637}
5638
5639/*
5640 * Make the table that links each word in the soundfold trie to the words it
5641 * can be produced from.
5642 * This is not unlike lines in a file, thus use a memfile to be able to access
5643 * the table efficiently.
5644 * Returns FAIL when out of memory.
5645 */
5646 static int
5647sug_maketable(spellinfo_T *spin)
5648{
5649 garray_T ga;
5650 int res = OK;
5651
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005652 // Allocate a buffer, open a memline for it and create the swap file
5653 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005654 spin->si_spellbuf = open_spellbuf();
5655 if (spin->si_spellbuf == NULL)
5656 return FAIL;
5657
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005658 // Use a buffer to store the line info, avoids allocating many small
5659 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005660 ga_init2(&ga, 1, 100);
5661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005663 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5664 res = FAIL;
5665
5666 ga_clear(&ga);
5667 return res;
5668}
5669
5670/*
5671 * Fill the table for one node and its children.
5672 * Returns the wordnr at the start of the node.
5673 * Returns -1 when out of memory.
5674 */
5675 static int
5676sug_filltable(
5677 spellinfo_T *spin,
5678 wordnode_T *node,
5679 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005680 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005681{
5682 wordnode_T *p, *np;
5683 int wordnr = startwordnr;
5684 int nr;
5685 int prev_nr;
5686
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005687 FOR_ALL_NODE_SIBLINGS(node, p)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005688 {
5689 if (p->wn_byte == NUL)
5690 {
5691 gap->ga_len = 0;
5692 prev_nr = 0;
5693 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5694 {
5695 if (ga_grow(gap, 10) == FAIL)
5696 return -1;
5697
5698 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005699 // Compute the offset from the previous nr and store the
5700 // offset in a way that it takes a minimum number of bytes.
5701 // It's a bit like utf-8, but without the need to mark
5702 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005703 nr -= prev_nr;
5704 prev_nr += nr;
5705 gap->ga_len += offset2bytes(nr,
5706 (char_u *)gap->ga_data + gap->ga_len);
5707 }
5708
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005709 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005710 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5711
5712 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5713 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5714 return -1;
5715 ++wordnr;
5716
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005717 // Remove extra NUL entries, we no longer need them. We don't
5718 // bother freeing the nodes, the won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005719 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5720 p->wn_sibling = p->wn_sibling->wn_sibling;
5721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005722 // Clear the flags on the remaining NUL node, so that compression
5723 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005724 p->wn_flags = 0;
5725 p->wn_region = 0;
5726 }
5727 else
5728 {
5729 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5730 if (wordnr == -1)
5731 return -1;
5732 }
5733 }
5734 return wordnr;
5735}
5736
5737/*
5738 * Convert an offset into a minimal number of bytes.
5739 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5740 * bytes.
5741 */
5742 static int
5743offset2bytes(int nr, char_u *buf)
5744{
5745 int rem;
5746 int b1, b2, b3, b4;
5747
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005748 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005749 b1 = nr % 255 + 1;
5750 rem = nr / 255;
5751 b2 = rem % 255 + 1;
5752 rem = rem / 255;
5753 b3 = rem % 255 + 1;
5754 b4 = rem / 255 + 1;
5755
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005756 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005757 {
5758 buf[0] = 0xe0 + b4;
5759 buf[1] = b3;
5760 buf[2] = b2;
5761 buf[3] = b1;
5762 return 4;
5763 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005764 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005765 {
5766 buf[0] = 0xc0 + b3;
5767 buf[1] = b2;
5768 buf[2] = b1;
5769 return 3;
5770 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005771 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005772 {
5773 buf[0] = 0x80 + b2;
5774 buf[1] = b1;
5775 return 2;
5776 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005777 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005778 buf[0] = b1;
5779 return 1;
5780}
5781
5782/*
5783 * Write the .sug file in "fname".
5784 */
5785 static void
5786sug_write(spellinfo_T *spin, char_u *fname)
5787{
5788 FILE *fd;
5789 wordnode_T *tree;
5790 int nodecount;
5791 int wcount;
5792 char_u *line;
5793 linenr_T lnum;
5794 int len;
5795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005796 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005797 fd = mch_fopen((char *)fname, "w");
5798 if (fd == NULL)
5799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005800 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005801 return;
5802 }
5803
5804 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005805 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005806 spell_message(spin, IObuff);
5807
5808 /*
5809 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5810 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005811 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005813 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005814 goto theend;
5815 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005816 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005818 // Write si_sugtime to the file.
5819 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005820
5821 /*
5822 * <SUGWORDTREE>
5823 */
5824 spin->si_memtot = 0;
5825 tree = spin->si_foldroot->wn_sibling;
5826
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005827 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005828 clear_node(tree);
5829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005830 // Count the number of nodes. Needed to be able to allocate the
5831 // memory when reading the nodes. Also fills in index for shared
5832 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005833 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005835 // number of nodes in 4 bytes
5836 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005837 spin->si_memtot += nodecount + nodecount * sizeof(int);
5838
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005839 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005840 (void)put_node(fd, tree, 0, 0, FALSE);
5841
5842 /*
5843 * <SUGTABLE>: <sugwcount> <sugline> ...
5844 */
5845 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005846 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005847
5848 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5849 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005850 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005851 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5852 len = (int)STRLEN(line) + 1;
5853 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005855 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005856 goto theend;
5857 }
5858 spin->si_memtot += len;
5859 }
5860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005861 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005862 if (putc(0, fd) == EOF)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005863 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005864
5865 vim_snprintf((char *)IObuff, IOSIZE,
5866 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5867 spell_message(spin, IObuff);
5868
5869theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005870 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005871 fclose(fd);
5872}
5873
5874
5875/*
5876 * Create a Vim spell file from one or more word lists.
5877 * "fnames[0]" is the output file name.
5878 * "fnames[fcount - 1]" is the last input file name.
5879 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5880 * and ".spl" is appended to make the output file name.
5881 */
5882 void
5883mkspell(
5884 int fcount,
5885 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005886 int ascii, // -ascii argument given
5887 int over_write, // overwrite existing output file
5888 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005889{
5890 char_u *fname = NULL;
5891 char_u *wfname;
5892 char_u **innames;
5893 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005894 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005895 int i;
5896 int len;
5897 stat_T st;
5898 int error = FALSE;
5899 spellinfo_T spin;
5900
Bram Moolenaara80faa82020-04-12 19:37:17 +02005901 CLEAR_FIELD(spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005902 spin.si_verbose = !added_word;
5903 spin.si_ascii = ascii;
5904 spin.si_followup = TRUE;
5905 spin.si_rem_accents = TRUE;
5906 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5907 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
5908 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5909 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
5910 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
5911 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
5912 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005913 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005915 // default: fnames[0] is output file, following are input files
Bram Moolenaar927b7dd2020-06-29 22:24:56 +02005916 // When "fcount" is 1 there is only one file.
5917 innames = &fnames[fcount == 1 ? 0 : 1];
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005918 incount = fcount - 1;
5919
5920 wfname = alloc(MAXPATHL);
5921 if (wfname == NULL)
5922 return;
5923
5924 if (fcount >= 1)
5925 {
5926 len = (int)STRLEN(fnames[0]);
5927 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5928 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005929 // For ":mkspell path/en.latin1.add" output file is
5930 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005931 incount = 1;
5932 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5933 }
5934 else if (fcount == 1)
5935 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005936 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005937 incount = 1;
5938 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5939 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5940 }
5941 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5942 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005943 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005944 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5945 }
5946 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005947 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005948 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5949 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005951 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005952 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5953 spin.si_ascii = TRUE;
5954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005955 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005956 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5957 spin.si_add = TRUE;
5958 }
5959
5960 if (incount <= 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005961 emsg(_(e_invarg)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005962 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005963 emsg(_("E751: Output file name must not have region name"));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005964 else if (incount > MAXREGIONS)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005965 semsg(_("E754: Only up to %d regions supported"), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005966 else
5967 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005968 // Check for overwriting before doing things that may take a lot of
5969 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005970 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
5971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005972 emsg(_(e_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005973 goto theend;
5974 }
5975 if (mch_isdir(wfname))
5976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005977 semsg(_(e_isadir2), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005978 goto theend;
5979 }
5980
5981 fname = alloc(MAXPATHL);
5982 if (fname == NULL)
5983 goto theend;
5984
5985 /*
5986 * Init the aff and dic pointers.
5987 * Get the region names if there are more than 2 arguments.
5988 */
5989 for (i = 0; i < incount; ++i)
5990 {
5991 afile[i] = NULL;
5992
5993 if (incount > 1)
5994 {
5995 len = (int)STRLEN(innames[i]);
5996 if (STRLEN(gettail(innames[i])) < 5
5997 || innames[i][len - 3] != '_')
5998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005999 semsg(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006000 goto theend;
6001 }
6002 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6003 spin.si_region_name[i * 2 + 1] =
6004 TOLOWER_ASC(innames[i][len - 1]);
6005 }
6006 }
6007 spin.si_region_count = incount;
6008
6009 spin.si_foldroot = wordtree_alloc(&spin);
6010 spin.si_keeproot = wordtree_alloc(&spin);
6011 spin.si_prefroot = wordtree_alloc(&spin);
6012 if (spin.si_foldroot == NULL
6013 || spin.si_keeproot == NULL
6014 || spin.si_prefroot == NULL)
6015 {
6016 free_blocks(spin.si_blocks);
6017 goto theend;
6018 }
6019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006020 // When not producing a .add.spl file clear the character table when
6021 // we encounter one in the .aff file. This means we dump the current
6022 // one in the .spl file if the .aff file doesn't define one. That's
6023 // better than guessing the contents, the table will match a
6024 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006025 if (!spin.si_add)
6026 spin.si_clear_chartab = TRUE;
6027
6028 /*
6029 * Read all the .aff and .dic files.
6030 * Text is converted to 'encoding'.
6031 * Words are stored in the case-folded and keep-case trees.
6032 */
6033 for (i = 0; i < incount && !error; ++i)
6034 {
6035 spin.si_conv.vc_type = CONV_NONE;
6036 spin.si_region = 1 << i;
6037
6038 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6039 if (mch_stat((char *)fname, &st) >= 0)
6040 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006041 // Read the .aff file. Will init "spin->si_conv" based on the
6042 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006043 afile[i] = spell_read_aff(&spin, fname);
6044 if (afile[i] == NULL)
6045 error = TRUE;
6046 else
6047 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006048 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006049 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6050 innames[i]);
6051 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6052 error = TRUE;
6053 }
6054 }
6055 else
6056 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006057 // No .aff file, try reading the file as a word list. Store
6058 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006059 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6060 error = TRUE;
6061 }
6062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006063 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006064 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006065 }
6066
6067 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006068 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006069
6070 if (!error && !got_int)
6071 {
6072 /*
6073 * Combine tails in the tree.
6074 */
6075 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02006076 wordtree_compress(&spin, spin.si_foldroot, "case-folded");
6077 wordtree_compress(&spin, spin.si_keeproot, "keep-case");
6078 wordtree_compress(&spin, spin.si_prefroot, "prefixes");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006079 }
6080
6081 if (!error && !got_int)
6082 {
6083 /*
6084 * Write the info in the spell file.
6085 */
6086 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006087 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006088 spell_message(&spin, IObuff);
6089
6090 error = write_vim_spell(&spin, wfname) == FAIL;
6091
6092 spell_message(&spin, (char_u *)_("Done!"));
6093 vim_snprintf((char *)IObuff, IOSIZE,
6094 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6095 spell_message(&spin, IObuff);
6096
6097 /*
6098 * If the file is loaded need to reload it.
6099 */
6100 if (!error)
6101 spell_reload_one(wfname, added_word);
6102 }
6103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006104 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006105 ga_clear(&spin.si_rep);
6106 ga_clear(&spin.si_repsal);
6107 ga_clear(&spin.si_sal);
6108 ga_clear(&spin.si_map);
6109 ga_clear(&spin.si_comppat);
6110 ga_clear(&spin.si_prefcond);
6111 hash_clear_all(&spin.si_commonwords, 0);
6112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006113 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006114 for (i = 0; i < incount; ++i)
6115 if (afile[i] != NULL)
6116 spell_free_aff(afile[i]);
6117
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006118 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006119 free_blocks(spin.si_blocks);
6120
6121 /*
6122 * If there is soundfolding info and no NOSUGFILE item create the
6123 * .sug file with the soundfolded word trie.
6124 */
6125 if (spin.si_sugtime != 0 && !error && !got_int)
6126 spell_make_sugfile(&spin, wfname);
6127
6128 }
6129
6130theend:
6131 vim_free(fname);
6132 vim_free(wfname);
6133}
6134
6135/*
6136 * Display a message for spell file processing when 'verbose' is set or using
6137 * ":mkspell". "str" can be IObuff.
6138 */
6139 static void
6140spell_message(spellinfo_T *spin, char_u *str)
6141{
6142 if (spin->si_verbose || p_verbose > 2)
6143 {
6144 if (!spin->si_verbose)
6145 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006146 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006147 out_flush();
6148 if (!spin->si_verbose)
6149 verbose_leave();
6150 }
6151}
6152
6153/*
6154 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006155 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006156 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006157 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006158 */
6159 void
6160ex_spell(exarg_T *eap)
6161{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006162 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6163 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6164 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006165 eap->forceit ? 0 : (int)eap->line2,
6166 eap->cmdidx == CMD_spellundo);
6167}
6168
6169/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006170 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006171 */
6172 void
6173spell_add_word(
6174 char_u *word,
6175 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006176 int what, // SPELL_ADD_ values
6177 int idx, // "zG" and "zW": zero, otherwise index in
6178 // 'spellfile'
6179 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006180{
6181 FILE *fd = NULL;
6182 buf_T *buf = NULL;
6183 int new_spf = FALSE;
6184 char_u *fname;
6185 char_u *fnamebuf = NULL;
6186 char_u line[MAXWLEN * 2];
6187 long fpos, fpos_next = 0;
6188 int i;
6189 char_u *spf;
6190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006191 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006192 {
6193 if (int_wordlist == NULL)
6194 {
6195 int_wordlist = vim_tempname('s', FALSE);
6196 if (int_wordlist == NULL)
6197 return;
6198 }
6199 fname = int_wordlist;
6200 }
6201 else
6202 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006203 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006204 if (*curwin->w_s->b_p_spf == NUL)
6205 {
6206 init_spellfile();
6207 new_spf = TRUE;
6208 }
6209
6210 if (*curwin->w_s->b_p_spf == NUL)
6211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006212 semsg(_(e_notset), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006213 return;
6214 }
6215 fnamebuf = alloc(MAXPATHL);
6216 if (fnamebuf == NULL)
6217 return;
6218
6219 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6220 {
6221 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6222 if (i == idx)
6223 break;
6224 if (*spf == NUL)
6225 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006226 semsg(_("E765: 'spellfile' does not have %d entries"), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006227 vim_free(fnamebuf);
6228 return;
6229 }
6230 }
6231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006232 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006233 buf = buflist_findname_exp(fnamebuf);
6234 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6235 buf = NULL;
6236 if (buf != NULL && bufIsChanged(buf))
6237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006238 emsg(_(e_bufloaded));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006239 vim_free(fnamebuf);
6240 return;
6241 }
6242
6243 fname = fnamebuf;
6244 }
6245
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006246 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006247 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006248 // When the word appears as good word we need to remove that one,
6249 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006250 fd = mch_fopen((char *)fname, "r");
6251 if (fd != NULL)
6252 {
6253 while (!vim_fgets(line, MAXWLEN * 2, fd))
6254 {
6255 fpos = fpos_next;
6256 fpos_next = ftell(fd);
6257 if (STRNCMP(word, line, len) == 0
6258 && (line[len] == '/' || line[len] < ' '))
6259 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006260 // Found duplicate word. Remove it by writing a '#' at
6261 // the start of the line. Mixing reading and writing
6262 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006263 fclose(fd);
6264 fd = mch_fopen((char *)fname, "r+");
6265 if (fd == NULL)
6266 break;
6267 if (fseek(fd, fpos, SEEK_SET) == 0)
6268 {
6269 fputc('#', fd);
6270 if (undo)
6271 {
6272 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006273 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006274 len, word, NameBuff);
6275 }
6276 }
6277 fseek(fd, fpos_next, SEEK_SET);
6278 }
6279 }
6280 if (fd != NULL)
6281 fclose(fd);
6282 }
6283 }
6284
6285 if (!undo)
6286 {
6287 fd = mch_fopen((char *)fname, "a");
6288 if (fd == NULL && new_spf)
6289 {
6290 char_u *p;
6291
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006292 // We just initialized the 'spellfile' option and can't open the
6293 // file. We may need to create the "spell" directory first. We
6294 // already checked the runtime directory is writable in
6295 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006296 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6297 {
6298 int c = *p;
6299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006300 // The directory doesn't exist. Try creating it and opening
6301 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006302 *p = NUL;
6303 vim_mkdir(fname, 0755);
6304 *p = c;
6305 fd = mch_fopen((char *)fname, "a");
6306 }
6307 }
6308
6309 if (fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006310 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006311 else
6312 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006313 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006314 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006315 else if (what == SPELL_ADD_RARE)
6316 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006317 else
6318 fprintf(fd, "%.*s\n", len, word);
6319 fclose(fd);
6320
6321 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006322 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006323 }
6324 }
6325
6326 if (fd != NULL)
6327 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006328 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006329 mkspell(1, &fname, FALSE, TRUE, TRUE);
6330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006331 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006332 if (buf != NULL)
6333 buf_reload(buf, buf->b_orig_mode);
6334
6335 redraw_all_later(SOME_VALID);
6336 }
6337 vim_free(fnamebuf);
6338}
6339
6340/*
6341 * Initialize 'spellfile' for the current buffer.
6342 */
6343 static void
6344init_spellfile(void)
6345{
6346 char_u *buf;
6347 int l;
6348 char_u *fname;
6349 char_u *rtp;
6350 char_u *lend;
6351 int aspath = FALSE;
6352 char_u *lstart = curbuf->b_s.b_p_spl;
6353
6354 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6355 {
6356 buf = alloc(MAXPATHL);
6357 if (buf == NULL)
6358 return;
6359
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006360 // Find the end of the language name. Exclude the region. If there
6361 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006362 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6363 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6364 if (vim_ispathsep(*lend))
6365 {
6366 aspath = TRUE;
6367 lstart = lend + 1;
6368 }
6369
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006370 // Loop over all entries in 'runtimepath'. Use the first one where we
6371 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006372 rtp = p_rtp;
6373 while (*rtp != NUL)
6374 {
6375 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006376 // Use directory of an entry with path, e.g., for
6377 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006378 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6379 lstart - curbuf->b_s.b_p_spl - 1);
6380 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006381 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006382 copy_option_part(&rtp, buf, MAXPATHL, ",");
6383 if (filewritable(buf) == 2)
6384 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006385 // Use the first language name from 'spelllang' and the
6386 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006387 if (aspath)
6388 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6389 lend - curbuf->b_s.b_p_spl);
6390 else
6391 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006392 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006393 l = (int)STRLEN(buf);
6394 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6395 if (filewritable(buf) != 2)
6396 vim_mkdir(buf, 0755);
6397
6398 l = (int)STRLEN(buf);
6399 vim_snprintf((char *)buf + l, MAXPATHL - l,
6400 "/%.*s", (int)(lend - lstart), lstart);
6401 }
6402 l = (int)STRLEN(buf);
6403 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6404 ->lp_slang->sl_fname;
6405 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6406 fname != NULL
6407 && strstr((char *)gettail(fname), ".ascii.") != NULL
6408 ? (char_u *)"ascii" : spell_enc());
6409 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6410 break;
6411 }
6412 aspath = FALSE;
6413 }
6414
6415 vim_free(buf);
6416 }
6417}
6418
6419
6420
6421/*
6422 * Set the spell character tables from strings in the affix file.
6423 */
6424 static int
6425set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6426{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006427 // We build the new tables here first, so that we can compare with the
6428 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006429 spelltab_T new_st;
6430 char_u *pf = fol, *pl = low, *pu = upp;
6431 int f, l, u;
6432
6433 clear_spell_chartab(&new_st);
6434
6435 while (*pf != NUL)
6436 {
6437 if (*pl == NUL || *pu == NUL)
6438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006439 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006440 return FAIL;
6441 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006442 f = mb_ptr2char_adv(&pf);
6443 l = mb_ptr2char_adv(&pl);
6444 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006445
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006446 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006447 if (f < 256)
6448 new_st.st_isw[f] = TRUE;
6449 if (l < 256)
6450 new_st.st_isw[l] = TRUE;
6451 if (u < 256)
6452 new_st.st_isw[u] = TRUE;
6453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006454 // if "LOW" and "FOL" are not the same the "LOW" char needs
6455 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006456 if (l < 256 && l != f)
6457 {
6458 if (f >= 256)
6459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006460 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006461 return FAIL;
6462 }
6463 new_st.st_fold[l] = f;
6464 }
6465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006466 // if "UPP" and "FOL" are not the same the "UPP" char needs
6467 // case-folding, it's upper case and the "UPP" is the upper case of
6468 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006469 if (u < 256 && u != f)
6470 {
6471 if (f >= 256)
6472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006473 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006474 return FAIL;
6475 }
6476 new_st.st_fold[u] = f;
6477 new_st.st_isu[u] = TRUE;
6478 new_st.st_upper[f] = u;
6479 }
6480 }
6481
6482 if (*pl != NUL || *pu != NUL)
6483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006484 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006485 return FAIL;
6486 }
6487
6488 return set_spell_finish(&new_st);
6489}
6490
6491/*
6492 * Set the spell character tables from strings in the .spl file.
6493 */
6494 static void
6495set_spell_charflags(
6496 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006497 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006498 char_u *fol)
6499{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006500 // We build the new tables here first, so that we can compare with the
6501 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006502 spelltab_T new_st;
6503 int i;
6504 char_u *p = fol;
6505 int c;
6506
6507 clear_spell_chartab(&new_st);
6508
6509 for (i = 0; i < 128; ++i)
6510 {
6511 if (i < cnt)
6512 {
6513 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6514 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6515 }
6516
6517 if (*p != NUL)
6518 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006519 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006520 new_st.st_fold[i + 128] = c;
6521 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6522 new_st.st_upper[c] = i + 128;
6523 }
6524 }
6525
6526 (void)set_spell_finish(&new_st);
6527}
6528
6529 static int
6530set_spell_finish(spelltab_T *new_st)
6531{
6532 int i;
6533
6534 if (did_set_spelltab)
6535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006536 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006537 for (i = 0; i < 256; ++i)
6538 {
6539 if (spelltab.st_isw[i] != new_st->st_isw[i]
6540 || spelltab.st_isu[i] != new_st->st_isu[i]
6541 || spelltab.st_fold[i] != new_st->st_fold[i]
6542 || spelltab.st_upper[i] != new_st->st_upper[i])
6543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006544 emsg(_("E763: Word characters differ between spell files"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006545 return FAIL;
6546 }
6547 }
6548 }
6549 else
6550 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006551 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006552 spelltab = *new_st;
6553 did_set_spelltab = TRUE;
6554 }
6555
6556 return OK;
6557}
6558
6559/*
6560 * Write the table with prefix conditions to the .spl file.
6561 * When "fd" is NULL only count the length of what is written.
6562 */
6563 static int
6564write_spell_prefcond(FILE *fd, garray_T *gap)
6565{
6566 int i;
6567 char_u *p;
6568 int len;
6569 int totlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006570 size_t x = 1; // collect return value of fwrite()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006571
6572 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006573 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006575 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006576
6577 for (i = 0; i < gap->ga_len; ++i)
6578 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006579 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006580 p = ((char_u **)gap->ga_data)[i];
6581 if (p != NULL)
6582 {
6583 len = (int)STRLEN(p);
6584 if (fd != NULL)
6585 {
6586 fputc(len, fd);
6587 x &= fwrite(p, (size_t)len, (size_t)1, fd);
6588 }
6589 totlen += len;
6590 }
6591 else if (fd != NULL)
6592 fputc(0, fd);
6593 }
6594
6595 return totlen;
6596}
6597
6598
6599/*
6600 * Use map string "map" for languages "lp".
6601 */
6602 static void
6603set_map_str(slang_T *lp, char_u *map)
6604{
6605 char_u *p;
6606 int headc = 0;
6607 int c;
6608 int i;
6609
6610 if (*map == NUL)
6611 {
6612 lp->sl_has_map = FALSE;
6613 return;
6614 }
6615 lp->sl_has_map = TRUE;
6616
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006617 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006618 for (i = 0; i < 256; ++i)
6619 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006620 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006621
6622 /*
6623 * The similar characters are stored separated with slashes:
6624 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6625 * before the same slash. For characters above 255 sl_map_hash is used.
6626 */
6627 for (p = map; *p != NUL; )
6628 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006629 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006630 if (c == '/')
6631 headc = 0;
6632 else
6633 {
6634 if (headc == 0)
6635 headc = c;
6636
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006637 // Characters above 255 don't fit in sl_map_array[], put them in
6638 // the hash table. Each entry is the char, a NUL the headchar and
6639 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006640 if (c >= 256)
6641 {
6642 int cl = mb_char2len(c);
6643 int headcl = mb_char2len(headc);
6644 char_u *b;
6645 hash_T hash;
6646 hashitem_T *hi;
6647
Bram Moolenaar964b3742019-05-24 18:54:09 +02006648 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006649 if (b == NULL)
6650 return;
6651 mb_char2bytes(c, b);
6652 b[cl] = NUL;
6653 mb_char2bytes(headc, b + cl + 1);
6654 b[cl + 1 + headcl] = NUL;
6655 hash = hash_hash(b);
6656 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6657 if (HASHITEM_EMPTY(hi))
6658 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6659 else
6660 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006661 // This should have been checked when generating the .spl
6662 // file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006663 emsg(_("E783: duplicate char in MAP entry"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006664 vim_free(b);
6665 }
6666 }
6667 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006668 lp->sl_map_array[c] = headc;
6669 }
6670 }
6671}
6672
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006673#endif // FEAT_SPELL