blob: 957be097a643a4e1bf5cc3d6f376968c7f1502b6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spellfile.c: code for reading and writing spell files.
12 *
13 * See spell.c for information about spell checking.
14 */
15
16/*
17 * Vim spell file format: <HEADER>
18 * <SECTIONS>
19 * <LWORDTREE>
20 * <KWORDTREE>
21 * <PREFIXTREE>
22 *
23 * <HEADER>: <fileID> <versionnr>
24 *
25 * <fileID> 8 bytes "VIMspell"
26 * <versionnr> 1 byte VIMSPELLVERSION
27 *
28 *
29 * Sections make it possible to add information to the .spl file without
30 * making it incompatible with previous versions. There are two kinds of
31 * sections:
32 * 1. Not essential for correct spell checking. E.g. for making suggestions.
33 * These are skipped when not supported.
34 * 2. Optional information, but essential for spell checking when present.
35 * E.g. conditions for affixes. When this section is present but not
36 * supported an error message is given.
37 *
38 * <SECTIONS>: <section> ... <sectionend>
39 *
40 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
41 *
42 * <sectionID> 1 byte number from 0 to 254 identifying the section
43 *
44 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
45 * spell checking
46 *
47 * <sectionlen> 4 bytes length of section contents, MSB first
48 *
49 * <sectionend> 1 byte SN_END
50 *
51 *
52 * sectionID == SN_INFO: <infotext>
53 * <infotext> N bytes free format text with spell file info (version,
54 * website, etc)
55 *
56 * sectionID == SN_REGION: <regionname> ...
Bram Moolenaar2993ac52018-02-10 14:12:43 +010057 * <regionname> 2 bytes Up to MAXREGIONS region names: ca, au, etc. Lower
58 * case. First <regionname> is region 1.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020059 *
60 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
61 * <folcharslen> <folchars>
62 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
63 * <charflags> N bytes List of flags (first one is for character 128):
64 * 0x01 word character CF_WORD
65 * 0x02 upper-case character CF_UPPER
66 * <folcharslen> 2 bytes Number of bytes in <folchars>.
67 * <folchars> N bytes Folded characters, first one is for character 128.
68 *
69 * sectionID == SN_MIDWORD: <midword>
70 * <midword> N bytes Characters that are word characters only when used
71 * in the middle of a word.
72 *
73 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
74 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
75 * <prefcond> : <condlen> <condstr>
76 * <condlen> 1 byte Length of <condstr>.
77 * <condstr> N bytes Condition for the prefix.
78 *
79 * sectionID == SN_REP: <repcount> <rep> ...
80 * <repcount> 2 bytes number of <rep> items, MSB first.
81 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
82 * <repfromlen> 1 byte length of <repfrom>
83 * <repfrom> N bytes "from" part of replacement
84 * <reptolen> 1 byte length of <repto>
85 * <repto> N bytes "to" part of replacement
86 *
87 * sectionID == SN_REPSAL: <repcount> <rep> ...
88 * just like SN_REP but for soundfolded words
89 *
90 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
91 * <salflags> 1 byte flags for soundsalike conversion:
92 * SAL_F0LLOWUP
93 * SAL_COLLAPSE
94 * SAL_REM_ACCENTS
95 * <salcount> 2 bytes number of <sal> items following
96 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
97 * <salfromlen> 1 byte length of <salfrom>
98 * <salfrom> N bytes "from" part of soundsalike
99 * <saltolen> 1 byte length of <salto>
100 * <salto> N bytes "to" part of soundsalike
101 *
102 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
103 * <sofofromlen> 2 bytes length of <sofofrom>
104 * <sofofrom> N bytes "from" part of soundfold
105 * <sofotolen> 2 bytes length of <sofoto>
106 * <sofoto> N bytes "to" part of soundfold
107 *
108 * sectionID == SN_SUGFILE: <timestamp>
109 * <timestamp> 8 bytes time in seconds that must match with .sug file
110 *
111 * sectionID == SN_NOSPLITSUGS: nothing
112 *
113 * sectionID == SN_NOCOMPOUNDSUGS: nothing
114 *
115 * sectionID == SN_WORDS: <word> ...
116 * <word> N bytes NUL terminated common word
117 *
118 * sectionID == SN_MAP: <mapstr>
119 * <mapstr> N bytes String with sequences of similar characters,
120 * separated by slashes.
121 *
122 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
123 * <comppatcount> <comppattern> ... <compflags>
124 * <compmax> 1 byte Maximum nr of words in compound word.
125 * <compminlen> 1 byte Minimal word length for compounding.
126 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
127 * <compoptions> 2 bytes COMP_ flags.
128 * <comppatcount> 2 bytes number of <comppattern> following
129 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
130 * slashes.
131 *
132 * <comppattern>: <comppatlen> <comppattext>
133 * <comppatlen> 1 byte length of <comppattext>
134 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
135 *
136 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
137 *
138 * sectionID == SN_SYLLABLE: <syllable>
139 * <syllable> N bytes String from SYLLABLE item.
140 *
141 * <LWORDTREE>: <wordtree>
142 *
143 * <KWORDTREE>: <wordtree>
144 *
145 * <PREFIXTREE>: <wordtree>
146 *
147 *
148 * <wordtree>: <nodecount> <nodedata> ...
149 *
150 * <nodecount> 4 bytes Number of nodes following. MSB first.
151 *
152 * <nodedata>: <siblingcount> <sibling> ...
153 *
154 * <siblingcount> 1 byte Number of siblings in this node. The siblings
155 * follow in sorted order.
156 *
157 * <sibling>: <byte> [ <nodeidx> <xbyte>
158 * | <flags> [<flags2>] [<region>] [<affixID>]
159 * | [<pflags>] <affixID> <prefcondnr> ]
160 *
161 * <byte> 1 byte Byte value of the sibling. Special cases:
162 * BY_NOFLAGS: End of word without flags and for all
163 * regions.
164 * For PREFIXTREE <affixID> and
165 * <prefcondnr> follow.
166 * BY_FLAGS: End of word, <flags> follow.
167 * For PREFIXTREE <pflags>, <affixID>
168 * and <prefcondnr> follow.
169 * BY_FLAGS2: End of word, <flags> and <flags2>
170 * follow. Not used in PREFIXTREE.
171 * BY_INDEX: Child of sibling is shared, <nodeidx>
172 * and <xbyte> follow.
173 *
174 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
175 *
176 * <xbyte> 1 byte byte value of the sibling.
177 *
178 * <flags> 1 byte bitmask of:
179 * WF_ALLCAP word must have only capitals
180 * WF_ONECAP first char of word must be capital
181 * WF_KEEPCAP keep-case word
182 * WF_FIXCAP keep-case word, all caps not allowed
183 * WF_RARE rare word
184 * WF_BANNED bad word
185 * WF_REGION <region> follows
186 * WF_AFX <affixID> follows
187 *
188 * <flags2> 1 byte Bitmask of:
189 * WF_HAS_AFF >> 8 word includes affix
190 * WF_NEEDCOMP >> 8 word only valid in compound
191 * WF_NOSUGGEST >> 8 word not used for suggestions
192 * WF_COMPROOT >> 8 word already a compound
193 * WF_NOCOMPBEF >> 8 no compounding before this word
194 * WF_NOCOMPAFT >> 8 no compounding after this word
195 *
196 * <pflags> 1 byte bitmask of:
197 * WFP_RARE rare prefix
198 * WFP_NC non-combining prefix
199 * WFP_UP letter after prefix made upper case
200 *
201 * <region> 1 byte Bitmask for regions in which word is valid. When
202 * omitted it's valid in all regions.
203 * Lowest bit is for region 1.
204 *
205 * <affixID> 1 byte ID of affix that can be used with this word. In
206 * PREFIXTREE used for the required prefix ID.
207 *
208 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
209 * from HEADER.
210 *
211 * All text characters are in 'encoding', but stored as single bytes.
212 */
213
214/*
215 * Vim .sug file format: <SUGHEADER>
216 * <SUGWORDTREE>
217 * <SUGTABLE>
218 *
219 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
220 *
221 * <fileID> 6 bytes "VIMsug"
222 * <versionnr> 1 byte VIMSUGVERSION
223 * <timestamp> 8 bytes timestamp that must match with .spl file
224 *
225 *
226 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
227 *
228 *
229 * <SUGTABLE>: <sugwcount> <sugline> ...
230 *
231 * <sugwcount> 4 bytes number of <sugline> following
232 *
233 * <sugline>: <sugnr> ... NUL
234 *
235 * <sugnr>: X bytes word number that results in this soundfolded word,
236 * stored as an offset to the previous number in as
237 * few bytes as possible, see offset2bytes())
238 */
239
240#include "vim.h"
241
242#if defined(FEAT_SPELL) || defined(PROTO)
243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100244#ifndef UNIX // it's in os_unix.h for Unix
245# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200246#endif
247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100248#ifndef UNIX // it's in os_unix.h for Unix
249# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200250#endif
251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100252// Special byte values for <byte>. Some are only used in the tree for
253// postponed prefixes, some only in the other trees. This is a bit messy...
254#define BY_NOFLAGS 0 // end of word without flags or region; for
255 // postponed prefix: no <pflags>
256#define BY_INDEX 1 // child is shared, index follows
257#define BY_FLAGS 2 // end of word, <flags> byte follows; for
258 // postponed prefix: <pflags> follows
259#define BY_FLAGS2 3 // end of word, <flags> and <flags2> bytes
260 // follow; never used in prefix tree
261#define BY_SPECIAL BY_FLAGS2 // highest special byte value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200262
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +0100263#define ZERO_FLAG 65009 // used when flag is zero: "0"
264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100265// Flags used in .spl file for soundsalike flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200266#define SAL_F0LLOWUP 1
267#define SAL_COLLAPSE 2
268#define SAL_REM_ACCENTS 4
269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100270#define VIMSPELLMAGIC "VIMspell" // string at start of Vim spell file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200271#define VIMSPELLMAGICL 8
272#define VIMSPELLVERSION 50
273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274// Section IDs. Only renumber them when VIMSPELLVERSION changes!
275#define SN_REGION 0 // <regionname> section
276#define SN_CHARFLAGS 1 // charflags section
277#define SN_MIDWORD 2 // <midword> section
278#define SN_PREFCOND 3 // <prefcond> section
279#define SN_REP 4 // REP items section
280#define SN_SAL 5 // SAL items section
281#define SN_SOFO 6 // soundfolding section
282#define SN_MAP 7 // MAP items section
283#define SN_COMPOUND 8 // compound words section
284#define SN_SYLLABLE 9 // syllable section
285#define SN_NOBREAK 10 // NOBREAK section
286#define SN_SUGFILE 11 // timestamp for .sug file
287#define SN_REPSAL 12 // REPSAL items section
288#define SN_WORDS 13 // common words
289#define SN_NOSPLITSUGS 14 // don't split word for suggestions
290#define SN_INFO 15 // info section
291#define SN_NOCOMPOUNDSUGS 16 // don't compound for suggestions
292#define SN_END 255 // end of sections
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100294#define SNF_REQUIRED 1 // <sectionflags>: required section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200295
296#define CF_WORD 0x01
297#define CF_UPPER 0x02
298
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200299/*
300 * Loop through all the siblings of a node (including the node)
301 */
302#define FOR_ALL_NODE_SIBLINGS(node, np) \
303 for ((np) = (node); (np) != NULL; (np) = (np)->wn_sibling)
304
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200305static int set_spell_finish(spelltab_T *new_st);
306static int write_spell_prefcond(FILE *fd, garray_T *gap);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200307static int read_region_section(FILE *fd, slang_T *slang, int len);
308static int read_charflags_section(FILE *fd);
309static int read_prefcond_section(FILE *fd, slang_T *lp);
310static int read_rep_section(FILE *fd, garray_T *gap, short *first);
311static int read_sal_section(FILE *fd, slang_T *slang);
312static int read_words_section(FILE *fd, slang_T *lp, int len);
313static int read_sofo_section(FILE *fd, slang_T *slang);
314static int read_compound(FILE *fd, slang_T *slang, int len);
315static int set_sofo(slang_T *lp, char_u *from, char_u *to);
316static void set_sal_first(slang_T *lp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200317static int *mb_str2wide(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200318static int spell_read_tree(FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt);
319static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200320static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
321static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
322static void set_map_str(slang_T *lp, char_u *map);
323
324
325static char *e_spell_trunc = N_("E758: Truncated spell file");
326static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
327static char *e_affname = N_("Affix name too long in %s line %d: %s");
328static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
329static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
330static char *msg_compressing = N_("Compressing word tree...");
331
332/*
333 * Load one spell file and store the info into a slang_T.
334 *
335 * This is invoked in three ways:
336 * - From spell_load_cb() to load a spell file for the first time. "lang" is
337 * the language name, "old_lp" is NULL. Will allocate an slang_T.
338 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
339 * points to the existing slang_T.
340 * - Just after writing a .spl file; it's read back to produce the .sug file.
341 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
342 *
343 * Returns the slang_T the spell file was loaded into. NULL for error.
344 */
345 slang_T *
346spell_load_file(
347 char_u *fname,
348 char_u *lang,
349 slang_T *old_lp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100350 int silent) // no error if file doesn't exist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200351{
352 FILE *fd;
353 char_u buf[VIMSPELLMAGICL];
354 char_u *p;
355 int i;
356 int n;
357 int len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200358 slang_T *lp = NULL;
359 int c = 0;
360 int res;
Bram Moolenaarce6db022020-01-07 20:11:42 +0100361 int did_estack_push = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +0100362 ESTACK_CHECK_DECLARATION
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200363
364 fd = mch_fopen((char *)fname, "r");
365 if (fd == NULL)
366 {
367 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100368 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200369 else if (p_verbose > 2)
370 {
371 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100372 smsg((const char *)e_notopen, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200373 verbose_leave();
374 }
375 goto endFAIL;
376 }
377 if (p_verbose > 2)
378 {
379 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380 smsg(_("Reading spell file \"%s\""), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200381 verbose_leave();
382 }
383
384 if (old_lp == NULL)
385 {
386 lp = slang_alloc(lang);
387 if (lp == NULL)
388 goto endFAIL;
389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100390 // Remember the file name, used to reload the file when it's updated.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200391 lp->sl_fname = vim_strsave(fname);
392 if (lp->sl_fname == NULL)
393 goto endFAIL;
394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100395 // Check for .add.spl (_add.spl for VMS).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200396 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
397 }
398 else
399 lp = old_lp;
400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100401 // Set sourcing_name, so that error messages mention the file name.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100402 estack_push(ETYPE_SPELL, fname, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +0100403 ESTACK_CHECK_SETUP
Bram Moolenaarce6db022020-01-07 20:11:42 +0100404 did_estack_push = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200405
406 /*
407 * <HEADER>: <fileID>
408 */
409 for (i = 0; i < VIMSPELLMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100410 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200411 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100413 emsg(_("E757: This does not look like a spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200414 goto endFAIL;
415 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100416 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200417 if (c < VIMSPELLVERSION)
418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100419 emsg(_("E771: Old spell file, needs to be updated"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200420 goto endFAIL;
421 }
422 else if (c > VIMSPELLVERSION)
423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424 emsg(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200425 goto endFAIL;
426 }
427
428
429 /*
430 * <SECTIONS>: <section> ... <sectionend>
431 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
432 */
433 for (;;)
434 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100435 n = getc(fd); // <sectionID> or <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200436 if (n == SN_END)
437 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100438 c = getc(fd); // <sectionflags>
439 len = get4c(fd); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200440 if (len < 0)
441 goto truncerr;
442
443 res = 0;
444 switch (n)
445 {
446 case SN_INFO:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100447 lp->sl_info = read_string(fd, len); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200448 if (lp->sl_info == NULL)
449 goto endFAIL;
450 break;
451
452 case SN_REGION:
453 res = read_region_section(fd, lp, len);
454 break;
455
456 case SN_CHARFLAGS:
457 res = read_charflags_section(fd);
458 break;
459
460 case SN_MIDWORD:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100461 lp->sl_midword = read_string(fd, len); // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200462 if (lp->sl_midword == NULL)
463 goto endFAIL;
464 break;
465
466 case SN_PREFCOND:
467 res = read_prefcond_section(fd, lp);
468 break;
469
470 case SN_REP:
471 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
472 break;
473
474 case SN_REPSAL:
475 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
476 break;
477
478 case SN_SAL:
479 res = read_sal_section(fd, lp);
480 break;
481
482 case SN_SOFO:
483 res = read_sofo_section(fd, lp);
484 break;
485
486 case SN_MAP:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100487 p = read_string(fd, len); // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200488 if (p == NULL)
489 goto endFAIL;
490 set_map_str(lp, p);
491 vim_free(p);
492 break;
493
494 case SN_WORDS:
495 res = read_words_section(fd, lp, len);
496 break;
497
498 case SN_SUGFILE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100499 lp->sl_sugtime = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200500 break;
501
502 case SN_NOSPLITSUGS:
503 lp->sl_nosplitsugs = TRUE;
504 break;
505
506 case SN_NOCOMPOUNDSUGS:
507 lp->sl_nocompoundsugs = TRUE;
508 break;
509
510 case SN_COMPOUND:
511 res = read_compound(fd, lp, len);
512 break;
513
514 case SN_NOBREAK:
515 lp->sl_nobreak = TRUE;
516 break;
517
518 case SN_SYLLABLE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100519 lp->sl_syllable = read_string(fd, len); // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200520 if (lp->sl_syllable == NULL)
521 goto endFAIL;
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 Moolenaar9ccfebd2016-07-19 16:39:08 +0200556 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
557 if (res != 0)
558 goto someerror;
559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // <KWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200561 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
562 if (res != 0)
563 goto someerror;
564
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100565 // <PREFIXTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200566 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
567 lp->sl_prefixcnt);
568 if (res != 0)
569 goto someerror;
570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100571 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200572 if (old_lp == NULL && lang != NULL)
573 {
574 lp->sl_next = first_lang;
575 first_lang = lp;
576 }
577
578 goto endOK;
579
580endFAIL:
581 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100582 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200583 *lang = NUL;
584 if (lp != NULL && old_lp == NULL)
585 slang_free(lp);
586 lp = NULL;
587
588endOK:
589 if (fd != NULL)
590 fclose(fd);
Bram Moolenaarce6db022020-01-07 20:11:42 +0100591 if (did_estack_push)
Bram Moolenaare31ee862020-01-07 20:59:34 +0100592 {
593 ESTACK_CHECK_NOW
Bram Moolenaarce6db022020-01-07 20:11:42 +0100594 estack_pop();
Bram Moolenaare31ee862020-01-07 20:59:34 +0100595 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200596
597 return lp;
598}
599
600/*
601 * Fill in the wordcount fields for a trie.
602 * Returns the total number of words.
603 */
604 static void
605tree_count_words(char_u *byts, idx_T *idxs)
606{
607 int depth;
608 idx_T arridx[MAXWLEN];
609 int curi[MAXWLEN];
610 int c;
611 idx_T n;
612 int wordcount[MAXWLEN];
613
614 arridx[0] = 0;
615 curi[0] = 1;
616 wordcount[0] = 0;
617 depth = 0;
618 while (depth >= 0 && !got_int)
619 {
620 if (curi[depth] > byts[arridx[depth]])
621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100622 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200623 idxs[arridx[depth]] = wordcount[depth];
624 if (depth > 0)
625 wordcount[depth - 1] += wordcount[depth];
626
627 --depth;
628 fast_breakcheck();
629 }
630 else
631 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100632 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200633 n = arridx[depth] + curi[depth];
634 ++curi[depth];
635
636 c = byts[n];
637 if (c == 0)
638 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100639 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200640 ++wordcount[depth];
641
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100642 // Skip over any other NUL bytes (same word with different
643 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200644 while (byts[n + 1] == 0)
645 {
646 ++n;
647 ++curi[depth];
648 }
649 }
650 else
651 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100652 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200653 ++depth;
654 arridx[depth] = idxs[n];
655 curi[depth] = 1;
656 wordcount[depth] = 0;
657 }
658 }
659 }
660}
661
662/*
663 * Load the .sug files for languages that have one and weren't loaded yet.
664 */
665 void
666suggest_load_files(void)
667{
668 langp_T *lp;
669 int lpi;
670 slang_T *slang;
671 char_u *dotp;
672 FILE *fd;
673 char_u buf[MAXWLEN];
674 int i;
675 time_t timestamp;
676 int wcount;
677 int wordnr;
678 garray_T ga;
679 int c;
680
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100681 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200682 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
683 {
684 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
685 slang = lp->lp_slang;
686 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100688 // Change ".spl" to ".sug" and open the file. When the file isn't
689 // found silently skip it. Do set "sl_sugloaded" so that we
690 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200691 slang->sl_sugloaded = TRUE;
692
693 dotp = vim_strrchr(slang->sl_fname, '.');
694 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
695 continue;
696 STRCPY(dotp, ".sug");
697 fd = mch_fopen((char *)slang->sl_fname, "r");
698 if (fd == NULL)
699 goto nextone;
700
701 /*
702 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
703 */
704 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100705 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200706 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100708 semsg(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200709 slang->sl_fname);
710 goto nextone;
711 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100712 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200713 if (c < VIMSUGVERSION)
714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 semsg(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200716 slang->sl_fname);
717 goto nextone;
718 }
719 else if (c > VIMSUGVERSION)
720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 semsg(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200722 slang->sl_fname);
723 goto nextone;
724 }
725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100726 // Check the timestamp, it must be exactly the same as the one in
727 // the .spl file. Otherwise the word numbers won't match.
728 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200729 if (timestamp != slang->sl_sugtime)
730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 semsg(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200732 slang->sl_fname);
733 goto nextone;
734 }
735
736 /*
737 * <SUGWORDTREE>: <wordtree>
738 * Read the trie with the soundfolded words.
739 */
740 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
741 FALSE, 0) != 0)
742 {
743someerror:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744 semsg(_("E782: error while reading .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200745 slang->sl_fname);
746 slang_clear_sug(slang);
747 goto nextone;
748 }
749
750 /*
751 * <SUGTABLE>: <sugwcount> <sugline> ...
752 *
753 * Read the table with word numbers. We use a file buffer for
754 * this, because it's so much like a file with lines. Makes it
755 * possible to swap the info and save on memory use.
756 */
757 slang->sl_sugbuf = open_spellbuf();
758 if (slang->sl_sugbuf == NULL)
759 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100760 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200761 wcount = get4c(fd);
762 if (wcount < 0)
763 goto someerror;
764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100765 // Read all the wordnr lists into the buffer, one NUL terminated
766 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200767 ga_init2(&ga, 1, 100);
768 for (wordnr = 0; wordnr < wcount; ++wordnr)
769 {
770 ga.ga_len = 0;
771 for (;;)
772 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100773 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200774 if (c < 0 || ga_grow(&ga, 1) == FAIL)
775 goto someerror;
776 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
777 if (c == NUL)
778 break;
779 }
780 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
781 ga.ga_data, ga.ga_len, TRUE) == FAIL)
782 goto someerror;
783 }
784 ga_clear(&ga);
785
786 /*
787 * Need to put word counts in the word tries, so that we can find
788 * a word by its number.
789 */
790 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
791 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
792
793nextone:
794 if (fd != NULL)
795 fclose(fd);
796 STRCPY(dotp, ".spl");
797 }
798 }
799}
800
801
802/*
803 * Read a length field from "fd" in "cnt_bytes" bytes.
804 * Allocate memory, read the string into it and add a NUL at the end.
805 * Returns NULL when the count is zero.
806 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
807 * otherwise.
808 */
809 static char_u *
810read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
811{
812 int cnt = 0;
813 int i;
814 char_u *str;
815
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100816 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200817 for (i = 0; i < cnt_bytes; ++i)
818 cnt = (cnt << 8) + getc(fd);
819 if (cnt < 0)
820 {
821 *cntp = SP_TRUNCERROR;
822 return NULL;
823 }
824 *cntp = cnt;
825 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100826 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200827
828 str = read_string(fd, cnt);
829 if (str == NULL)
830 *cntp = SP_OTHERERROR;
831 return str;
832}
833
834/*
835 * Read SN_REGION: <regionname> ...
836 * Return SP_*ERROR flags.
837 */
838 static int
839read_region_section(FILE *fd, slang_T *lp, int len)
840{
841 int i;
842
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100843 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200844 return SP_FORMERROR;
845 for (i = 0; i < len; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100846 lp->sl_regions[i] = getc(fd); // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200847 lp->sl_regions[len] = NUL;
848 return 0;
849}
850
851/*
852 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
853 * <folcharslen> <folchars>
854 * Return SP_*ERROR flags.
855 */
856 static int
857read_charflags_section(FILE *fd)
858{
859 char_u *flags;
860 char_u *fol;
861 int flagslen, follen;
862
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100863 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200864 flags = read_cnt_string(fd, 1, &flagslen);
865 if (flagslen < 0)
866 return flagslen;
867
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100868 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200869 fol = read_cnt_string(fd, 2, &follen);
870 if (follen < 0)
871 {
872 vim_free(flags);
873 return follen;
874 }
875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100876 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200877 if (flags != NULL && fol != NULL)
878 set_spell_charflags(flags, flagslen, fol);
879
880 vim_free(flags);
881 vim_free(fol);
882
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100883 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200884 if ((flags == NULL) != (fol == NULL))
885 return SP_FORMERROR;
886 return 0;
887}
888
889/*
890 * Read SN_PREFCOND section.
891 * Return SP_*ERROR flags.
892 */
893 static int
894read_prefcond_section(FILE *fd, slang_T *lp)
895{
896 int cnt;
897 int i;
898 int n;
899 char_u *p;
900 char_u buf[MAXWLEN + 1];
901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100902 // <prefcondcnt> <prefcond> ...
903 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200904 if (cnt <= 0)
905 return SP_FORMERROR;
906
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200907 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200908 if (lp->sl_prefprog == NULL)
909 return SP_OTHERERROR;
910 lp->sl_prefixcnt = cnt;
911
912 for (i = 0; i < cnt; ++i)
913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100914 // <prefcond> : <condlen> <condstr>
915 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200916 if (n < 0 || n >= MAXWLEN)
917 return SP_FORMERROR;
918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100919 // When <condlen> is zero we have an empty condition. Otherwise
920 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200921 if (n > 0)
922 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100923 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200924 p = buf + 1;
925 while (n-- > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100926 *p++ = getc(fd); // <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200927 *p = NUL;
928 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
929 }
930 }
931 return 0;
932}
933
934/*
935 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
936 * Return SP_*ERROR flags.
937 */
938 static int
939read_rep_section(FILE *fd, garray_T *gap, short *first)
940{
941 int cnt;
942 fromto_T *ftp;
943 int i;
944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100945 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200946 if (cnt < 0)
947 return SP_TRUNCERROR;
948
949 if (ga_grow(gap, cnt) == FAIL)
950 return SP_OTHERERROR;
951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100952 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200953 for (; gap->ga_len < cnt; ++gap->ga_len)
954 {
955 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
956 ftp->ft_from = read_cnt_string(fd, 1, &i);
957 if (i < 0)
958 return i;
959 if (i == 0)
960 return SP_FORMERROR;
961 ftp->ft_to = read_cnt_string(fd, 1, &i);
962 if (i <= 0)
963 {
964 vim_free(ftp->ft_from);
965 if (i < 0)
966 return i;
967 return SP_FORMERROR;
968 }
969 }
970
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100971 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200972 for (i = 0; i < 256; ++i)
973 first[i] = -1;
974 for (i = 0; i < gap->ga_len; ++i)
975 {
976 ftp = &((fromto_T *)gap->ga_data)[i];
977 if (first[*ftp->ft_from] == -1)
978 first[*ftp->ft_from] = i;
979 }
980 return 0;
981}
982
983/*
984 * Read SN_SAL section: <salflags> <salcount> <sal> ...
985 * Return SP_*ERROR flags.
986 */
987 static int
988read_sal_section(FILE *fd, slang_T *slang)
989{
990 int i;
991 int cnt;
992 garray_T *gap;
993 salitem_T *smp;
994 int ccnt;
995 char_u *p;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200996
997 slang->sl_sofo = FALSE;
998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100999 i = getc(fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001000 if (i & SAL_F0LLOWUP)
1001 slang->sl_followup = TRUE;
1002 if (i & SAL_COLLAPSE)
1003 slang->sl_collapse = TRUE;
1004 if (i & SAL_REM_ACCENTS)
1005 slang->sl_rem_accents = TRUE;
1006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001007 cnt = get2c(fd); // <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001008 if (cnt < 0)
1009 return SP_TRUNCERROR;
1010
1011 gap = &slang->sl_sal;
1012 ga_init2(gap, sizeof(salitem_T), 10);
1013 if (ga_grow(gap, cnt + 1) == FAIL)
1014 return SP_OTHERERROR;
1015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001016 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001017 for (; gap->ga_len < cnt; ++gap->ga_len)
1018 {
Bram Moolenaar97d2f342020-07-10 20:03:03 +02001019 int c = NUL;
1020
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001021 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001022 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001023 if (ccnt < 0)
1024 return SP_TRUNCERROR;
1025 if ((p = alloc(ccnt + 2)) == NULL)
1026 return SP_OTHERERROR;
1027 smp->sm_lead = p;
1028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001029 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001030 for (i = 0; i < ccnt; ++i)
1031 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001032 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001033 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1034 break;
1035 *p++ = c;
1036 }
1037 smp->sm_leadlen = (int)(p - smp->sm_lead);
1038 *p++ = NUL;
1039
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001040 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001041 if (c == '(')
1042 {
1043 smp->sm_oneof = p;
1044 for (++i; i < ccnt; ++i)
1045 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001046 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001047 if (c == ')')
1048 break;
1049 *p++ = c;
1050 }
1051 *p++ = NUL;
1052 if (++i < ccnt)
1053 c = getc(fd);
1054 }
1055 else
1056 smp->sm_oneof = NULL;
1057
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001058 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001059 smp->sm_rules = p;
1060 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001061 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001062 *p++ = c;
1063 for (++i; i < ccnt; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001064 *p++ = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001065 *p++ = NUL;
1066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001067 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001068 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1069 if (ccnt < 0)
1070 {
1071 vim_free(smp->sm_lead);
1072 return ccnt;
1073 }
1074
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001075 if (has_mbyte)
1076 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001077 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001078 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1079 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1080 if (smp->sm_oneof == NULL)
1081 smp->sm_oneof_w = NULL;
1082 else
1083 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1084 if (smp->sm_to == NULL)
1085 smp->sm_to_w = NULL;
1086 else
1087 smp->sm_to_w = mb_str2wide(smp->sm_to);
1088 if (smp->sm_lead_w == NULL
1089 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1090 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1091 {
1092 vim_free(smp->sm_lead);
1093 vim_free(smp->sm_to);
1094 vim_free(smp->sm_lead_w);
1095 vim_free(smp->sm_oneof_w);
1096 vim_free(smp->sm_to_w);
1097 return SP_OTHERERROR;
1098 }
1099 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001100 }
1101
1102 if (gap->ga_len > 0)
1103 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001104 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1105 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001106 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1107 if ((p = alloc(1)) == NULL)
1108 return SP_OTHERERROR;
1109 p[0] = NUL;
1110 smp->sm_lead = p;
1111 smp->sm_leadlen = 0;
1112 smp->sm_oneof = NULL;
1113 smp->sm_rules = p;
1114 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001115 if (has_mbyte)
1116 {
1117 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1118 smp->sm_leadlen = 0;
1119 smp->sm_oneof_w = NULL;
1120 smp->sm_to_w = NULL;
1121 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001122 ++gap->ga_len;
1123 }
1124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001125 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001126 set_sal_first(slang);
1127
1128 return 0;
1129}
1130
1131/*
1132 * Read SN_WORDS: <word> ...
1133 * Return SP_*ERROR flags.
1134 */
1135 static int
1136read_words_section(FILE *fd, slang_T *lp, int len)
1137{
1138 int done = 0;
1139 int i;
1140 int c;
1141 char_u word[MAXWLEN];
1142
1143 while (done < len)
1144 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001145 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001146 for (i = 0; ; ++i)
1147 {
1148 c = getc(fd);
1149 if (c == EOF)
1150 return SP_TRUNCERROR;
1151 word[i] = c;
1152 if (word[i] == NUL)
1153 break;
1154 if (i == MAXWLEN - 1)
1155 return SP_FORMERROR;
1156 }
1157
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001158 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001159 count_common_word(lp, word, -1, 10);
1160 done += i + 1;
1161 }
1162 return 0;
1163}
1164
1165/*
1166 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1167 * Return SP_*ERROR flags.
1168 */
1169 static int
1170read_sofo_section(FILE *fd, slang_T *slang)
1171{
1172 int cnt;
1173 char_u *from, *to;
1174 int res;
1175
1176 slang->sl_sofo = TRUE;
1177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001178 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001179 from = read_cnt_string(fd, 2, &cnt);
1180 if (cnt < 0)
1181 return cnt;
1182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001183 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001184 to = read_cnt_string(fd, 2, &cnt);
1185 if (cnt < 0)
1186 {
1187 vim_free(from);
1188 return cnt;
1189 }
1190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001191 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001192 if (from != NULL && to != NULL)
1193 res = set_sofo(slang, from, to);
1194 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001195 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001196 else
1197 res = 0;
1198
1199 vim_free(from);
1200 vim_free(to);
1201 return res;
1202}
1203
1204/*
1205 * Read the compound section from the .spl file:
1206 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1207 * Returns SP_*ERROR flags.
1208 */
1209 static int
1210read_compound(FILE *fd, slang_T *slang, int len)
1211{
1212 int todo = len;
1213 int c;
1214 int atstart;
1215 char_u *pat;
1216 char_u *pp;
1217 char_u *cp;
1218 char_u *ap;
1219 char_u *crp;
1220 int cnt;
1221 garray_T *gap;
1222
1223 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001224 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001225
1226 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001227 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001228 if (c < 2)
1229 c = MAXWLEN;
1230 slang->sl_compmax = c;
1231
1232 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001233 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001234 if (c < 1)
1235 c = 0;
1236 slang->sl_compminlen = c;
1237
1238 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001239 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001240 if (c < 1)
1241 c = MAXWLEN;
1242 slang->sl_compsylmax = c;
1243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001244 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001245 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001246 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001247 else
1248 {
1249 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001250 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001251 --todo;
1252 slang->sl_compoptions = c;
1253
1254 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001255 c = get2c(fd); // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001256 todo -= 2;
1257 ga_init2(gap, sizeof(char_u *), c);
1258 if (ga_grow(gap, c) == OK)
1259 while (--c >= 0)
1260 {
1261 ((char_u **)(gap->ga_data))[gap->ga_len++] =
1262 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001263 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001264 if (cnt < 0)
1265 return cnt;
1266 todo -= cnt + 1;
1267 }
1268 }
1269 if (todo < 0)
1270 return SP_FORMERROR;
1271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001272 // Turn the COMPOUNDRULE items into a regexp pattern:
1273 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1274 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1275 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001276 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001277 if (enc_utf8)
1278 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001279 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001280 if (pat == NULL)
1281 return SP_OTHERERROR;
1282
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001283 // We also need a list of all flags that can appear at the start and one
1284 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001285 cp = alloc(todo + 1);
1286 if (cp == NULL)
1287 {
1288 vim_free(pat);
1289 return SP_OTHERERROR;
1290 }
1291 slang->sl_compstartflags = cp;
1292 *cp = NUL;
1293
1294 ap = alloc(todo + 1);
1295 if (ap == NULL)
1296 {
1297 vim_free(pat);
1298 return SP_OTHERERROR;
1299 }
1300 slang->sl_compallflags = ap;
1301 *ap = NUL;
1302
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001303 // And a list of all patterns in their original form, for checking whether
1304 // compounding may work in match_compoundrule(). This is freed when we
1305 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001306 crp = alloc(todo + 1);
1307 slang->sl_comprules = crp;
1308
1309 pp = pat;
1310 *pp++ = '^';
1311 *pp++ = '\\';
1312 *pp++ = '(';
1313
1314 atstart = 1;
1315 while (todo-- > 0)
1316 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001318 if (c == EOF)
1319 {
1320 vim_free(pat);
1321 return SP_TRUNCERROR;
1322 }
1323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001324 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001325 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1326 && !byte_in_str(slang->sl_compallflags, c))
1327 {
1328 *ap++ = c;
1329 *ap = NUL;
1330 }
1331
1332 if (atstart != 0)
1333 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001334 // At start of item: copy flags to "sl_compstartflags". For a
1335 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001336 if (c == '[')
1337 atstart = 2;
1338 else if (c == ']')
1339 atstart = 0;
1340 else
1341 {
1342 if (!byte_in_str(slang->sl_compstartflags, c))
1343 {
1344 *cp++ = c;
1345 *cp = NUL;
1346 }
1347 if (atstart == 1)
1348 atstart = 0;
1349 }
1350 }
1351
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001352 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001353 if (crp != NULL)
1354 {
1355 if (c == '?' || c == '+' || c == '*')
1356 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001357 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001358 crp = NULL;
1359 }
1360 else
1361 *crp++ = c;
1362 }
1363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001364 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001365 {
1366 *pp++ = '\\';
1367 *pp++ = '|';
1368 atstart = 1;
1369 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001370 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001371 {
1372 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001373 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001374 if (enc_utf8)
1375 pp += mb_char2bytes(c, pp);
1376 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001377 *pp++ = c;
1378 }
1379 }
1380
1381 *pp++ = '\\';
1382 *pp++ = ')';
1383 *pp++ = '$';
1384 *pp = NUL;
1385
1386 if (crp != NULL)
1387 *crp = NUL;
1388
1389 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1390 vim_free(pat);
1391 if (slang->sl_compprog == NULL)
1392 return SP_FORMERROR;
1393
1394 return 0;
1395}
1396
1397/*
1398 * Set the SOFOFROM and SOFOTO items in language "lp".
1399 * Returns SP_*ERROR flags when there is something wrong.
1400 */
1401 static int
1402set_sofo(slang_T *lp, char_u *from, char_u *to)
1403{
1404 int i;
1405
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001406 garray_T *gap;
1407 char_u *s;
1408 char_u *p;
1409 int c;
1410 int *inp;
1411
1412 if (has_mbyte)
1413 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001414 // Use "sl_sal" as an array with 256 pointers to a list of wide
1415 // characters. The index is the low byte of the character.
1416 // The list contains from-to pairs with a terminating NUL.
1417 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001418 gap = &lp->sl_sal;
1419 ga_init2(gap, sizeof(int *), 1);
1420 if (ga_grow(gap, 256) == FAIL)
1421 return SP_OTHERERROR;
1422 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1423 gap->ga_len = 256;
1424
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001425 // First count the number of items for each list. Temporarily use
1426 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001427 for (p = from, s = to; *p != NUL && *s != NUL; )
1428 {
1429 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001430 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001431 if (c >= 256)
1432 ++lp->sl_sal_first[c & 0xff];
1433 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001434 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001435 return SP_FORMERROR;
1436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001438 for (i = 0; i < 256; ++i)
1439 if (lp->sl_sal_first[i] > 0)
1440 {
1441 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1442 if (p == NULL)
1443 return SP_OTHERERROR;
1444 ((int **)gap->ga_data)[i] = (int *)p;
1445 *(int *)p = 0;
1446 }
1447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001448 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1449 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001450 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1451 for (p = from, s = to; *p != NUL && *s != NUL; )
1452 {
1453 c = mb_cptr2char_adv(&p);
1454 i = mb_cptr2char_adv(&s);
1455 if (c >= 256)
1456 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001457 // Append the from-to chars at the end of the list with
1458 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001459 inp = ((int **)gap->ga_data)[c & 0xff];
1460 while (*inp != 0)
1461 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001462 *inp++ = c; // from char
1463 *inp++ = i; // to char
1464 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001465 }
1466 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001467 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001468 lp->sl_sal_first[c] = i;
1469 }
1470 }
1471 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001472 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001473 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001474 if (STRLEN(from) != STRLEN(to))
1475 return SP_FORMERROR;
1476
1477 for (i = 0; to[i] != NUL; ++i)
1478 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001479 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001480 }
1481
1482 return 0;
1483}
1484
1485/*
1486 * Fill the first-index table for "lp".
1487 */
1488 static void
1489set_sal_first(slang_T *lp)
1490{
1491 salfirst_T *sfirst;
1492 int i;
1493 salitem_T *smp;
1494 int c;
1495 garray_T *gap = &lp->sl_sal;
1496
1497 sfirst = lp->sl_sal_first;
1498 for (i = 0; i < 256; ++i)
1499 sfirst[i] = -1;
1500 smp = (salitem_T *)gap->ga_data;
1501 for (i = 0; i < gap->ga_len; ++i)
1502 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001503 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001504 // Use the lowest byte of the first character. For latin1 it's
1505 // the character, for other encodings it should differ for most
1506 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001507 c = *smp[i].sm_lead_w & 0xff;
1508 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001509 c = *smp[i].sm_lead;
1510 if (sfirst[c] == -1)
1511 {
1512 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001513 if (has_mbyte)
1514 {
1515 int n;
1516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001517 // Make sure all entries with this byte are following each
1518 // other. Move the ones that are in the wrong position. Do
1519 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001520 while (i + 1 < gap->ga_len
1521 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001522 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001523 ++i;
1524
1525 for (n = 1; i + n < gap->ga_len; ++n)
1526 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1527 {
1528 salitem_T tsal;
1529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001530 // Move entry with same index byte after the entries
1531 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001532 ++i;
1533 --n;
1534 tsal = smp[i + n];
1535 mch_memmove(smp + i + 1, smp + i,
1536 sizeof(salitem_T) * n);
1537 smp[i] = tsal;
1538 }
1539 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001540 }
1541 }
1542}
1543
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001544/*
1545 * Turn a multi-byte string into a wide character string.
1546 * Return it in allocated memory (NULL for out-of-memory)
1547 */
1548 static int *
1549mb_str2wide(char_u *s)
1550{
1551 int *res;
1552 char_u *p;
1553 int i = 0;
1554
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001555 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001556 if (res != NULL)
1557 {
1558 for (p = s; *p != NUL; )
1559 res[i++] = mb_ptr2char_adv(&p);
1560 res[i] = NUL;
1561 }
1562 return res;
1563}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001564
1565/*
1566 * Read a tree from the .spl or .sug file.
1567 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1568 * This is skipped when the tree has zero length.
1569 * Returns zero when OK, SP_ value for an error.
1570 */
1571 static int
1572spell_read_tree(
1573 FILE *fd,
1574 char_u **bytsp,
1575 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001576 int prefixtree, // TRUE for the prefix tree
1577 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001578{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001579 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001580 int idx;
1581 char_u *bp;
1582 idx_T *ip;
1583
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001584 // The tree size was computed when writing the file, so that we can
1585 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001586 len = get4c(fd);
1587 if (len < 0)
1588 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001589 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001590 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001591 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001592 if (len > 0)
1593 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001594 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001595 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001596 if (bp == NULL)
1597 return SP_OTHERERROR;
1598 *bytsp = bp;
1599
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001600 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001601 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001602 if (ip == NULL)
1603 return SP_OTHERERROR;
1604 *idxsp = ip;
1605
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001606 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001607 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1608 if (idx < 0)
1609 return idx;
1610 }
1611 return 0;
1612}
1613
1614/*
1615 * Read one row of siblings from the spell file and store it in the byte array
1616 * "byts" and index array "idxs". Recursively read the children.
1617 *
1618 * NOTE: The code here must match put_node()!
1619 *
1620 * Returns the index (>= 0) following the siblings.
1621 * Returns SP_TRUNCERROR if the file is shorter than expected.
1622 * Returns SP_FORMERROR if there is a format error.
1623 */
1624 static idx_T
1625read_tree_node(
1626 FILE *fd,
1627 char_u *byts,
1628 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001629 int maxidx, // size of arrays
1630 idx_T startidx, // current index in "byts" and "idxs"
1631 int prefixtree, // TRUE for reading PREFIXTREE
1632 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001633{
1634 int len;
1635 int i;
1636 int n;
1637 idx_T idx = startidx;
1638 int c;
1639 int c2;
1640#define SHARED_MASK 0x8000000
1641
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001642 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001643 if (len <= 0)
1644 return SP_TRUNCERROR;
1645
1646 if (startidx + len >= maxidx)
1647 return SP_FORMERROR;
1648 byts[idx++] = len;
1649
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001650 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001651 for (i = 1; i <= len; ++i)
1652 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001653 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001654 if (c < 0)
1655 return SP_TRUNCERROR;
1656 if (c <= BY_SPECIAL)
1657 {
1658 if (c == BY_NOFLAGS && !prefixtree)
1659 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001660 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001661 idxs[idx] = 0;
1662 c = 0;
1663 }
1664 else if (c != BY_INDEX)
1665 {
1666 if (prefixtree)
1667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001668 // Read the optional pflags byte, the prefix ID and the
1669 // condition nr. In idxs[] store the prefix ID in the low
1670 // byte, the condition index shifted up 8 bits, the flags
1671 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001672 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001673 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001674 else
1675 c = 0;
1676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001677 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001679 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001680 if (n >= maxprefcondnr)
1681 return SP_FORMERROR;
1682 c |= (n << 8);
1683 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001684 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001685 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001686 // Read flags and optional region and prefix ID. In
1687 // idxs[] the flags go in the low two bytes, region above
1688 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001689 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001691 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001692 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001693 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001694 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001695 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001696 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001697 }
1698
1699 idxs[idx] = c;
1700 c = 0;
1701 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001702 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001703 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001704 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001705 n = get3c(fd);
1706 if (n < 0 || n >= maxidx)
1707 return SP_FORMERROR;
1708 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001709 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001710 }
1711 }
1712 byts[idx++] = c;
1713 }
1714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // Recursively read the children for non-shared siblings.
1716 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1717 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001718 for (i = 1; i <= len; ++i)
1719 if (byts[startidx + i] != 0)
1720 {
1721 if (idxs[startidx + i] & SHARED_MASK)
1722 idxs[startidx + i] &= ~SHARED_MASK;
1723 else
1724 {
1725 idxs[startidx + i] = idx;
1726 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1727 prefixtree, maxprefcondnr);
1728 if (idx < 0)
1729 break;
1730 }
1731 }
1732
1733 return idx;
1734}
1735
1736/*
1737 * Reload the spell file "fname" if it's loaded.
1738 */
1739 static void
1740spell_reload_one(
1741 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001742 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001743{
1744 slang_T *slang;
1745 int didit = FALSE;
1746
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001747 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001748 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001749 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001750 {
1751 slang_clear(slang);
1752 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001753 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001754 slang_clear(slang);
1755 redraw_all_later(SOME_VALID);
1756 didit = TRUE;
1757 }
1758 }
1759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001760 // When "zg" was used and the file wasn't loaded yet, should redo
1761 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001762 if (added_word && !didit)
1763 did_set_spelllang(curwin);
1764}
1765
1766
1767/*
1768 * Functions for ":mkspell".
1769 */
1770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001771#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1772 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001773/*
1774 * Main structure to store the contents of a ".aff" file.
1775 */
1776typedef struct afffile_S
1777{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001778 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1779 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1780 unsigned af_rare; // RARE ID for rare word
1781 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1782 unsigned af_bad; // BAD ID for banned word
1783 unsigned af_needaffix; // NEEDAFFIX ID
1784 unsigned af_circumfix; // CIRCUMFIX ID
1785 unsigned af_needcomp; // NEEDCOMPOUND ID
1786 unsigned af_comproot; // COMPOUNDROOT ID
1787 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1788 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1789 unsigned af_nosuggest; // NOSUGGEST ID
1790 int af_pfxpostpone; // postpone prefixes without chop string and
1791 // without flags
1792 int af_ignoreextra; // IGNOREEXTRA present
1793 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1794 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1795 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001796} afffile_T;
1797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001798#define AFT_CHAR 0 // flags are one character
1799#define AFT_LONG 1 // flags are two characters
1800#define AFT_CAPLONG 2 // flags are one or two characters
1801#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001802
1803typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001804// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001805struct affentry_S
1806{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001807 affentry_T *ae_next; // next affix with same name/number
1808 char_u *ae_chop; // text to chop off basic word (can be NULL)
1809 char_u *ae_add; // text to add to basic word (can be NULL)
1810 char_u *ae_flags; // flags on the affix (can be NULL)
1811 char_u *ae_cond; // condition (NULL for ".")
1812 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1813 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1814 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001815};
1816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001817#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001818
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001819// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001820typedef struct affheader_S
1821{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001822 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1823 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1824 int ah_newID; // prefix ID after renumbering; 0 if not used
1825 int ah_combine; // suffix may combine with prefix
1826 int ah_follows; // another affix block should be following
1827 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001828} affheader_T;
1829
1830#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1831
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001832// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001833typedef struct compitem_S
1834{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001835 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1836 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1837 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001838} compitem_T;
1839
1840#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1841
1842/*
1843 * Structure that is used to store the items in the word tree. This avoids
1844 * the need to keep track of each allocated thing, everything is freed all at
1845 * once after ":mkspell" is done.
1846 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1847 * "sb_data" is correct for systems where pointers must be aligned on
1848 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1849 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001850#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001851typedef struct sblock_S sblock_T;
1852struct sblock_S
1853{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001854 int sb_used; // nr of bytes already in use
1855 sblock_T *sb_next; // next block in list
1856 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001857};
1858
1859/*
1860 * A node in the tree.
1861 */
1862typedef struct wordnode_S wordnode_T;
1863struct wordnode_S
1864{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001865 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001866 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001867 char_u hashkey[6]; // the hash key, only used while compressing
1868 int index; // index in written nodes (valid after first
1869 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001870 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001871 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001872 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001873 wordnode_T *next; // next node with same hash key
1874 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001875 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001876 wordnode_T *wn_child; // child (next byte in word)
1877 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1878 // always sorted)
1879 int wn_refs; // Nr. of references to this node. Only
1880 // relevant for first node in a list of
1881 // siblings, in following siblings it is
1882 // always one.
1883 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001884
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001885 // Info for when "wn_byte" is NUL.
1886 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1887 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1888 // "wn_region" the LSW of the wordnr.
1889 char_u wn_affixID; // supported/required prefix ID or 0
1890 short_u wn_flags; // WF_ flags
1891 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001892
1893#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001894 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001895#endif
1896};
1897
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001898#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001899
1900#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1901
1902/*
1903 * Info used while reading the spell files.
1904 */
1905typedef struct spellinfo_S
1906{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001907 wordnode_T *si_foldroot; // tree with case-folded words
1908 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001910 wordnode_T *si_keeproot; // tree with keep-case words
1911 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001913 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001915 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001917 sblock_T *si_blocks; // memory blocks used
1918 long si_blocks_cnt; // memory blocks allocated
1919 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001921 long si_compress_cnt; // words to add before lowering
1922 // compression limit
1923 wordnode_T *si_first_free; // List of nodes that have been freed during
1924 // compression, linked by "wn_child" field.
1925 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001926#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001927 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001928#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001929 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 int si_ascii; // handling only ASCII words
1932 int si_add; // addition file
1933 int si_clear_chartab; // when TRUE clear char tables
1934 int si_region; // region mask
1935 vimconv_T si_conv; // for conversion to 'encoding'
1936 int si_memtot; // runtime memory used
1937 int si_verbose; // verbose messages
1938 int si_msg_count; // number of words added since last message
1939 char_u *si_info; // info text chars or NULL
1940 int si_region_count; // number of regions supported (1 when there
1941 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001942 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001943 // region names; used only if
1944 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001946 garray_T si_rep; // list of fromto_T entries from REP lines
1947 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1948 garray_T si_sal; // list of fromto_T entries from SAL lines
1949 char_u *si_sofofr; // SOFOFROM text
1950 char_u *si_sofoto; // SOFOTO text
1951 int si_nosugfile; // NOSUGFILE item found
1952 int si_nosplitsugs; // NOSPLITSUGS item found
1953 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1954 int si_followup; // soundsalike: ?
1955 int si_collapse; // soundsalike: ?
1956 hashtab_T si_commonwords; // hashtable for common words
1957 time_t si_sugtime; // timestamp for .sug file
1958 int si_rem_accents; // soundsalike: remove accents
1959 garray_T si_map; // MAP info concatenated
1960 char_u *si_midword; // MIDWORD chars or NULL
1961 int si_compmax; // max nr of words for compounding
1962 int si_compminlen; // minimal length for compounding
1963 int si_compsylmax; // max nr of syllables for compounding
1964 int si_compoptions; // COMP_ flags
1965 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1966 // a string
1967 char_u *si_compflags; // flags used for compounding
1968 char_u si_nobreak; // NOBREAK
1969 char_u *si_syllable; // syllable string
1970 garray_T si_prefcond; // table with conditions for postponed
1971 // prefixes, each stored as a string
1972 int si_newprefID; // current value for ah_newID
1973 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001974} spellinfo_T;
1975
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001976static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1977static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1978static int spell_info_item(char_u *s);
1979static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1980static unsigned get_affitem(int flagtype, char_u **pp);
1981static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1982static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001983static void aff_check_number(int spinval, int affval, char *name);
1984static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1985static int str_equal(char_u *s1, char_u *s2);
1986static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1987static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001988static int get_affix_flags(afffile_T *affile, char_u *afflist);
1989static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1990static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1991static 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 +02001992static void *getroom(spellinfo_T *spin, size_t len, int align);
1993static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001994static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
1995static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
1996static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001997static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
Bram Moolenaar408c23b2020-06-03 22:15:45 +02001998static void wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name);
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02001999static long node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, long *tot);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002000static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002001static void clear_node(wordnode_T *node);
2002static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002003static int sug_filltree(spellinfo_T *spin, slang_T *slang);
2004static int sug_maketable(spellinfo_T *spin);
2005static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
2006static int offset2bytes(int nr, char_u *buf);
2007static void sug_write(spellinfo_T *spin, char_u *fname);
2008static void spell_message(spellinfo_T *spin, char_u *str);
2009static void init_spellfile(void);
2010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002011// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
2012// but it must be negative to indicate the prefix tree to tree_add_word().
2013// Use a negative number with the lower 8 bits zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002014#define PFX_FLAGS -256
2015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002016// flags for "condit" argument of store_aff_word()
2017#define CONDIT_COMB 1 // affix must combine
2018#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2019#define CONDIT_SUF 4 // add a suffix for matching flags
2020#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002021
2022/*
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02002023 * Tunable parameters for when the tree is compressed. Filled from the
2024 * 'mkspellmem' option.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002025 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002026static long compress_start = 30000; // memory / SBLOCKSIZE
2027static long compress_inc = 100; // memory / SBLOCKSIZE
2028static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002029
2030/*
2031 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2032 * Sets "sps_flags".
2033 */
2034 int
2035spell_check_msm(void)
2036{
2037 char_u *p = p_msm;
2038 long start = 0;
2039 long incr = 0;
2040 long added = 0;
2041
2042 if (!VIM_ISDIGIT(*p))
2043 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002044 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002045 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2046 if (*p != ',')
2047 return FAIL;
2048 ++p;
2049 if (!VIM_ISDIGIT(*p))
2050 return FAIL;
2051 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2052 if (*p != ',')
2053 return FAIL;
2054 ++p;
2055 if (!VIM_ISDIGIT(*p))
2056 return FAIL;
2057 added = getdigits(&p) * 1024;
2058 if (*p != NUL)
2059 return FAIL;
2060
2061 if (start == 0 || incr == 0 || added == 0 || incr > start)
2062 return FAIL;
2063
2064 compress_start = start;
2065 compress_inc = incr;
2066 compress_added = added;
2067 return OK;
2068}
2069
2070#ifdef SPELL_PRINTTREE
2071/*
2072 * For debugging the tree code: print the current tree in a (more or less)
2073 * readable format, so that we can see what happens when adding a word and/or
2074 * compressing the tree.
2075 * Based on code from Olaf Seibert.
2076 */
2077#define PRINTLINESIZE 1000
2078#define PRINTWIDTH 6
2079
2080#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2081 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2082
2083static char line1[PRINTLINESIZE];
2084static char line2[PRINTLINESIZE];
2085static char line3[PRINTLINESIZE];
2086
2087 static void
2088spell_clear_flags(wordnode_T *node)
2089{
2090 wordnode_T *np;
2091
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002092 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002093 {
2094 np->wn_u1.index = FALSE;
2095 spell_clear_flags(np->wn_child);
2096 }
2097}
2098
2099 static void
2100spell_print_node(wordnode_T *node, int depth)
2101{
2102 if (node->wn_u1.index)
2103 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002104 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002105 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2106 PRINTSOME(line2, depth, " ", 0, 0);
2107 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002108 msg(line1);
2109 msg(line2);
2110 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002111 }
2112 else
2113 {
2114 node->wn_u1.index = TRUE;
2115
2116 if (node->wn_byte != NUL)
2117 {
2118 if (node->wn_child != NULL)
2119 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2120 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002121 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002122 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2123 }
2124 else
2125 PRINTSOME(line1, depth, " $ ", 0, 0);
2126
2127 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2128
2129 if (node->wn_sibling != NULL)
2130 PRINTSOME(line3, depth, " | ", 0, 0);
2131 else
2132 PRINTSOME(line3, depth, " ", 0, 0);
2133
2134 if (node->wn_byte == NUL)
2135 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002136 msg(line1);
2137 msg(line2);
2138 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002139 }
2140
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002141 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002142 if (node->wn_byte != NUL && node->wn_child != NULL)
2143 spell_print_node(node->wn_child, depth + 1);
2144
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002145 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002146 if (node->wn_sibling != NULL)
2147 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002148 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002149 STRCPY(line1, line3);
2150 STRCPY(line2, line3);
2151 spell_print_node(node->wn_sibling, depth);
2152 }
2153 }
2154}
2155
2156 static void
2157spell_print_tree(wordnode_T *root)
2158{
2159 if (root != NULL)
2160 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002161 // Clear the "wn_u1.index" fields, used to remember what has been
2162 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002163 spell_clear_flags(root);
2164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002165 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002166 spell_print_node(root, 0);
2167 }
2168}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002169#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002170
2171/*
2172 * Read the affix file "fname".
2173 * Returns an afffile_T, NULL for complete failure.
2174 */
2175 static afffile_T *
2176spell_read_aff(spellinfo_T *spin, char_u *fname)
2177{
2178 FILE *fd;
2179 afffile_T *aff;
2180 char_u rline[MAXLINELEN];
2181 char_u *line;
2182 char_u *pc = NULL;
2183#define MAXITEMCNT 30
2184 char_u *(items[MAXITEMCNT]);
2185 int itemcnt;
2186 char_u *p;
2187 int lnum = 0;
2188 affheader_T *cur_aff = NULL;
2189 int did_postpone_prefix = FALSE;
2190 int aff_todo = 0;
2191 hashtab_T *tp;
2192 char_u *low = NULL;
2193 char_u *fol = NULL;
2194 char_u *upp = NULL;
2195 int do_rep;
2196 int do_repsal;
2197 int do_sal;
2198 int do_mapline;
2199 int found_map = FALSE;
2200 hashitem_T *hi;
2201 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002202 int compminlen = 0; // COMPOUNDMIN value
2203 int compsylmax = 0; // COMPOUNDSYLMAX value
2204 int compoptions = 0; // COMP_ flags
2205 int compmax = 0; // COMPOUNDWORDMAX value
2206 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2207 // concatenated
2208 char_u *midword = NULL; // MIDWORD value
2209 char_u *syllable = NULL; // SYLLABLE value
2210 char_u *sofofrom = NULL; // SOFOFROM value
2211 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002212
2213 /*
2214 * Open the file.
2215 */
2216 fd = mch_fopen((char *)fname, "r");
2217 if (fd == NULL)
2218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002219 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002220 return NULL;
2221 }
2222
Bram Moolenaarc1669272018-06-19 14:23:53 +02002223 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002224 spell_message(spin, IObuff);
2225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002226 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002227 do_rep = spin->si_rep.ga_len == 0;
2228
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002229 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002230 do_repsal = spin->si_repsal.ga_len == 0;
2231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002232 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002233 do_sal = spin->si_sal.ga_len == 0;
2234
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002235 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002236 do_mapline = spin->si_map.ga_len == 0;
2237
2238 /*
2239 * Allocate and init the afffile_T structure.
2240 */
2241 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2242 if (aff == NULL)
2243 {
2244 fclose(fd);
2245 return NULL;
2246 }
2247 hash_init(&aff->af_pref);
2248 hash_init(&aff->af_suff);
2249 hash_init(&aff->af_comp);
2250
2251 /*
2252 * Read all the lines in the file one by one.
2253 */
2254 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2255 {
2256 line_breakcheck();
2257 ++lnum;
2258
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002259 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002260 if (*rline == '#')
2261 continue;
2262
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002263 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002264 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002265 if (spin->si_conv.vc_type != CONV_NONE)
2266 {
2267 pc = string_convert(&spin->si_conv, rline, NULL);
2268 if (pc == NULL)
2269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002270 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002271 fname, lnum, rline);
2272 continue;
2273 }
2274 line = pc;
2275 }
2276 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002277 {
2278 pc = NULL;
2279 line = rline;
2280 }
2281
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002282 // Split the line up in white separated items. Put a NUL after each
2283 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002284 itemcnt = 0;
2285 for (p = line; ; )
2286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002287 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002288 ++p;
2289 if (*p == NUL)
2290 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002291 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002292 break;
2293 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002294 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002295 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002296 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002297 ++p;
2298 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002299 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002300 ++p;
2301 if (*p == NUL)
2302 break;
2303 *p++ = NUL;
2304 }
2305
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002306 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002307 if (itemcnt > 0)
2308 {
2309 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2310 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002311 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002312 aff->af_enc = enc_canonize(items[1]);
2313 if (aff->af_enc != NULL && !spin->si_ascii
2314 && convert_setup(&spin->si_conv, aff->af_enc,
2315 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002317 fname, aff->af_enc, p_enc);
2318 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002319 }
2320 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2321 && aff->af_flagtype == AFT_CHAR)
2322 {
2323 if (STRCMP(items[1], "long") == 0)
2324 aff->af_flagtype = AFT_LONG;
2325 else if (STRCMP(items[1], "num") == 0)
2326 aff->af_flagtype = AFT_NUM;
2327 else if (STRCMP(items[1], "caplong") == 0)
2328 aff->af_flagtype = AFT_CAPLONG;
2329 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002331 fname, lnum, items[1]);
2332 if (aff->af_rare != 0
2333 || aff->af_keepcase != 0
2334 || aff->af_bad != 0
2335 || aff->af_needaffix != 0
2336 || aff->af_circumfix != 0
2337 || aff->af_needcomp != 0
2338 || aff->af_comproot != 0
2339 || aff->af_nosuggest != 0
2340 || compflags != NULL
2341 || aff->af_suff.ht_used > 0
2342 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002343 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002344 fname, lnum, items[1]);
2345 }
2346 else if (spell_info_item(items[0]))
2347 {
2348 p = (char_u *)getroom(spin,
2349 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2350 + STRLEN(items[0])
2351 + STRLEN(items[1]) + 3, FALSE);
2352 if (p != NULL)
2353 {
2354 if (spin->si_info != NULL)
2355 {
2356 STRCPY(p, spin->si_info);
2357 STRCAT(p, "\n");
2358 }
2359 STRCAT(p, items[0]);
2360 STRCAT(p, " ");
2361 STRCAT(p, items[1]);
2362 spin->si_info = p;
2363 }
2364 }
2365 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2366 && midword == NULL)
2367 {
2368 midword = getroom_save(spin, items[1]);
2369 }
2370 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2371 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002372 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002373 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002374 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002375 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2376 || is_aff_rule(items, itemcnt, "RARE", 2))
2377 && aff->af_rare == 0)
2378 {
2379 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2380 fname, lnum);
2381 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002382 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002383 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2384 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2385 && aff->af_keepcase == 0)
2386 {
2387 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2388 fname, lnum);
2389 }
2390 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2391 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2392 && aff->af_bad == 0)
2393 {
2394 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2395 fname, lnum);
2396 }
2397 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2398 && aff->af_needaffix == 0)
2399 {
2400 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2401 fname, lnum);
2402 }
2403 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2404 && aff->af_circumfix == 0)
2405 {
2406 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2407 fname, lnum);
2408 }
2409 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2410 && aff->af_nosuggest == 0)
2411 {
2412 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2413 fname, lnum);
2414 }
2415 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2416 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2417 && aff->af_needcomp == 0)
2418 {
2419 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2420 fname, lnum);
2421 }
2422 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2423 && aff->af_comproot == 0)
2424 {
2425 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2426 fname, lnum);
2427 }
2428 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2429 && aff->af_compforbid == 0)
2430 {
2431 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2432 fname, lnum);
2433 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002434 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002435 fname, lnum);
2436 }
2437 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2438 && aff->af_comppermit == 0)
2439 {
2440 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2441 fname, lnum);
2442 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002443 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002444 fname, lnum);
2445 }
2446 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2447 && compflags == NULL)
2448 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002449 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2450 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002451 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2452 if (p != NULL)
2453 {
2454 STRCPY(p, items[1]);
2455 STRCAT(p, "+");
2456 compflags = p;
2457 }
2458 }
2459 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002461 // We don't use the count, but do check that it's a number and
2462 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002463 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002464 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002465 fname, lnum, items[1]);
2466 }
2467 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2468 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002469 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002470 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2471 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002472 // Concatenate this string to previously defined ones,
2473 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002474 l = (int)STRLEN(items[1]) + 1;
2475 if (compflags != NULL)
2476 l += (int)STRLEN(compflags) + 1;
2477 p = getroom(spin, l, FALSE);
2478 if (p != NULL)
2479 {
2480 if (compflags != NULL)
2481 {
2482 STRCPY(p, compflags);
2483 STRCAT(p, "/");
2484 }
2485 STRCAT(p, items[1]);
2486 compflags = p;
2487 }
2488 }
2489 }
2490 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2491 && compmax == 0)
2492 {
2493 compmax = atoi((char *)items[1]);
2494 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002495 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002496 fname, lnum, items[1]);
2497 }
2498 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2499 && compminlen == 0)
2500 {
2501 compminlen = atoi((char *)items[1]);
2502 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002503 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002504 fname, lnum, items[1]);
2505 }
2506 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2507 && compsylmax == 0)
2508 {
2509 compsylmax = atoi((char *)items[1]);
2510 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002511 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002512 fname, lnum, items[1]);
2513 }
2514 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2515 {
2516 compoptions |= COMP_CHECKDUP;
2517 }
2518 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2519 {
2520 compoptions |= COMP_CHECKREP;
2521 }
2522 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2523 {
2524 compoptions |= COMP_CHECKCASE;
2525 }
2526 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2527 {
2528 compoptions |= COMP_CHECKTRIPLE;
2529 }
2530 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2531 {
2532 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002534 fname, lnum, items[1]);
2535 }
2536 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2537 {
2538 garray_T *gap = &spin->si_comppat;
2539 int i;
2540
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002541 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002542 for (i = 0; i < gap->ga_len - 1; i += 2)
2543 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2544 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2545 items[2]) == 0)
2546 break;
2547 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2548 {
2549 ((char_u **)(gap->ga_data))[gap->ga_len++]
2550 = getroom_save(spin, items[1]);
2551 ((char_u **)(gap->ga_data))[gap->ga_len++]
2552 = getroom_save(spin, items[2]);
2553 }
2554 }
2555 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2556 && syllable == NULL)
2557 {
2558 syllable = getroom_save(spin, items[1]);
2559 }
2560 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2561 {
2562 spin->si_nobreak = TRUE;
2563 }
2564 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2565 {
2566 spin->si_nosplitsugs = TRUE;
2567 }
2568 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2569 {
2570 spin->si_nocompoundsugs = TRUE;
2571 }
2572 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2573 {
2574 spin->si_nosugfile = TRUE;
2575 }
2576 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2577 {
2578 aff->af_pfxpostpone = TRUE;
2579 }
2580 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2581 {
2582 aff->af_ignoreextra = TRUE;
2583 }
2584 else if ((STRCMP(items[0], "PFX") == 0
2585 || STRCMP(items[0], "SFX") == 0)
2586 && aff_todo == 0
2587 && itemcnt >= 4)
2588 {
2589 int lasti = 4;
2590 char_u key[AH_KEY_LEN];
2591
2592 if (*items[0] == 'P')
2593 tp = &aff->af_pref;
2594 else
2595 tp = &aff->af_suff;
2596
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002597 // Myspell allows the same affix name to be used multiple
2598 // times. The affix files that do this have an undocumented
2599 // "S" flag on all but the last block, thus we check for that
2600 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002601 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2602 hi = hash_find(tp, key);
2603 if (!HASHITEM_EMPTY(hi))
2604 {
2605 cur_aff = HI2AH(hi);
2606 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002608 fname, lnum, items[1]);
2609 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002610 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002611 fname, lnum, items[1]);
2612 }
2613 else
2614 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002615 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002616 cur_aff = (affheader_T *)getroom(spin,
2617 sizeof(affheader_T), TRUE);
2618 if (cur_aff == NULL)
2619 break;
2620 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2621 fname, lnum);
2622 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2623 break;
2624 if (cur_aff->ah_flag == aff->af_bad
2625 || cur_aff->ah_flag == aff->af_rare
2626 || cur_aff->ah_flag == aff->af_keepcase
2627 || cur_aff->ah_flag == aff->af_needaffix
2628 || cur_aff->ah_flag == aff->af_circumfix
2629 || cur_aff->ah_flag == aff->af_nosuggest
2630 || cur_aff->ah_flag == aff->af_needcomp
2631 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002632 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002633 fname, lnum, items[1]);
2634 STRCPY(cur_aff->ah_key, items[1]);
2635 hash_add(tp, cur_aff->ah_key);
2636
2637 cur_aff->ah_combine = (*items[2] == 'Y');
2638 }
2639
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002640 // Check for the "S" flag, which apparently means that another
2641 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002642 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2643 {
2644 ++lasti;
2645 cur_aff->ah_follows = TRUE;
2646 }
2647 else
2648 cur_aff->ah_follows = FALSE;
2649
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 // Myspell allows extra text after the item, but that might
2651 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002652 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002653 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002654
2655 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002656 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002657 fname, lnum, items[2]);
2658
2659 if (*items[0] == 'P' && aff->af_pfxpostpone)
2660 {
2661 if (cur_aff->ah_newID == 0)
2662 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002663 // Use a new number in the .spl file later, to be able
2664 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002665 check_renumber(spin);
2666 cur_aff->ah_newID = ++spin->si_newprefID;
2667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002668 // We only really use ah_newID if the prefix is
2669 // postponed. We know that only after handling all
2670 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002671 did_postpone_prefix = FALSE;
2672 }
2673 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002674 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002675 did_postpone_prefix = TRUE;
2676 }
2677
2678 aff_todo = atoi((char *)items[3]);
2679 }
2680 else if ((STRCMP(items[0], "PFX") == 0
2681 || STRCMP(items[0], "SFX") == 0)
2682 && aff_todo > 0
2683 && STRCMP(cur_aff->ah_key, items[1]) == 0
2684 && itemcnt >= 5)
2685 {
2686 affentry_T *aff_entry;
2687 int upper = FALSE;
2688 int lasti = 5;
2689
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002690 // Myspell allows extra text after the item, but that might
2691 // mean mistakes go unnoticed. Require a comment-starter,
2692 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002693 if (itemcnt > lasti
2694 && !aff->af_ignoreextra
2695 && *items[lasti] != '#'
2696 && (STRCMP(items[lasti], "-") != 0
2697 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002698 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002701 --aff_todo;
2702 aff_entry = (affentry_T *)getroom(spin,
2703 sizeof(affentry_T), TRUE);
2704 if (aff_entry == NULL)
2705 break;
2706
2707 if (STRCMP(items[2], "0") != 0)
2708 aff_entry->ae_chop = getroom_save(spin, items[2]);
2709 if (STRCMP(items[3], "0") != 0)
2710 {
2711 aff_entry->ae_add = getroom_save(spin, items[3]);
2712
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002713 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002714 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2715 if (aff_entry->ae_flags != NULL)
2716 {
2717 *aff_entry->ae_flags++ = NUL;
2718 aff_process_flags(aff, aff_entry);
2719 }
2720 }
2721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002722 // Don't use an affix entry with non-ASCII characters when
2723 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002724 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2725 || has_non_ascii(aff_entry->ae_add)))
2726 {
2727 aff_entry->ae_next = cur_aff->ah_first;
2728 cur_aff->ah_first = aff_entry;
2729
2730 if (STRCMP(items[4], ".") != 0)
2731 {
2732 char_u buf[MAXLINELEN];
2733
2734 aff_entry->ae_cond = getroom_save(spin, items[4]);
2735 if (*items[0] == 'P')
2736 sprintf((char *)buf, "^%s", items[4]);
2737 else
2738 sprintf((char *)buf, "%s$", items[4]);
2739 aff_entry->ae_prog = vim_regcomp(buf,
2740 RE_MAGIC + RE_STRING + RE_STRICT);
2741 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002742 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002743 fname, lnum, items[4]);
2744 }
2745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002746 // For postponed prefixes we need an entry in si_prefcond
2747 // for the condition. Use an existing one if possible.
2748 // Can't be done for an affix with flags, ignoring
2749 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002750 if (*items[0] == 'P' && aff->af_pfxpostpone
2751 && aff_entry->ae_flags == NULL)
2752 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002753 // When the chop string is one lower-case letter and
2754 // the add string ends in the upper-case letter we set
2755 // the "upper" flag, clear "ae_chop" and remove the
2756 // letters from "ae_add". The condition must either
2757 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002758 if (aff_entry->ae_chop != NULL
2759 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002760 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002761 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002762 {
2763 int c, c_up;
2764
2765 c = PTR2CHAR(aff_entry->ae_chop);
2766 c_up = SPELL_TOUPPER(c);
2767 if (c_up != c
2768 && (aff_entry->ae_cond == NULL
2769 || PTR2CHAR(aff_entry->ae_cond) == c))
2770 {
2771 p = aff_entry->ae_add
2772 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002773 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002774 if (PTR2CHAR(p) == c_up)
2775 {
2776 upper = TRUE;
2777 aff_entry->ae_chop = NULL;
2778 *p = NUL;
2779
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002780 // The condition is matched with the
2781 // actual word, thus must check for the
2782 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002783 if (aff_entry->ae_cond != NULL)
2784 {
2785 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002786
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002787 if (has_mbyte)
2788 {
2789 onecap_copy(items[4], buf, TRUE);
2790 aff_entry->ae_cond = getroom_save(
2791 spin, buf);
2792 }
2793 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002794 *aff_entry->ae_cond = c_up;
2795 if (aff_entry->ae_cond != NULL)
2796 {
2797 sprintf((char *)buf, "^%s",
2798 aff_entry->ae_cond);
2799 vim_regfree(aff_entry->ae_prog);
2800 aff_entry->ae_prog = vim_regcomp(
2801 buf, RE_MAGIC + RE_STRING);
2802 }
2803 }
2804 }
2805 }
2806 }
2807
2808 if (aff_entry->ae_chop == NULL
2809 && aff_entry->ae_flags == NULL)
2810 {
2811 int idx;
2812 char_u **pp;
2813 int n;
2814
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002815 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002816 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2817 --idx)
2818 {
2819 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2820 if (str_equal(p, aff_entry->ae_cond))
2821 break;
2822 }
2823 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2824 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002825 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002826 idx = spin->si_prefcond.ga_len++;
2827 pp = ((char_u **)spin->si_prefcond.ga_data)
2828 + idx;
2829 if (aff_entry->ae_cond == NULL)
2830 *pp = NULL;
2831 else
2832 *pp = getroom_save(spin,
2833 aff_entry->ae_cond);
2834 }
2835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002836 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002837 if (aff_entry->ae_add == NULL)
2838 p = (char_u *)"";
2839 else
2840 p = aff_entry->ae_add;
2841
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002842 // PFX_FLAGS is a negative number, so that
2843 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002844 n = PFX_FLAGS;
2845 if (!cur_aff->ah_combine)
2846 n |= WFP_NC;
2847 if (upper)
2848 n |= WFP_UP;
2849 if (aff_entry->ae_comppermit)
2850 n |= WFP_COMPPERMIT;
2851 if (aff_entry->ae_compforbid)
2852 n |= WFP_COMPFORBID;
2853 tree_add_word(spin, p, spin->si_prefroot, n,
2854 idx, cur_aff->ah_newID);
2855 did_postpone_prefix = TRUE;
2856 }
2857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002858 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002859 if (aff_todo == 0 && !did_postpone_prefix)
2860 {
2861 --spin->si_newprefID;
2862 cur_aff->ah_newID = 0;
2863 }
2864 }
2865 }
2866 }
2867 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2868 {
2869 fol = vim_strsave(items[1]);
2870 }
2871 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2872 {
2873 low = vim_strsave(items[1]);
2874 }
2875 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2876 {
2877 upp = vim_strsave(items[1]);
2878 }
2879 else if (is_aff_rule(items, itemcnt, "REP", 2)
2880 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002882 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002883 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002884 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002885 fname, lnum);
2886 }
2887 else if ((STRCMP(items[0], "REP") == 0
2888 || STRCMP(items[0], "REPSAL") == 0)
2889 && itemcnt >= 3)
2890 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002891 // REP/REPSAL item
2892 // Myspell ignores extra arguments, we require it starts with
2893 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002894 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002895 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002896 if (items[0][3] == 'S' ? do_repsal : do_rep)
2897 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002898 // Replace underscore with space (can't include a space
2899 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002900 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002901 if (*p == '_')
2902 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002903 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002904 if (*p == '_')
2905 *p = ' ';
2906 add_fromto(spin, items[0][3] == 'S'
2907 ? &spin->si_repsal
2908 : &spin->si_rep, items[1], items[2]);
2909 }
2910 }
2911 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002913 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002914 if (!found_map)
2915 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002916 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002917 found_map = TRUE;
2918 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002919 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002920 fname, lnum);
2921 }
2922 else if (do_mapline)
2923 {
2924 int c;
2925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002926 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002927 for (p = items[1]; *p != NUL; )
2928 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002929 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002930 if ((spin->si_map.ga_len > 0
2931 && vim_strchr(spin->si_map.ga_data, c)
2932 != NULL)
2933 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002934 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002935 fname, lnum);
2936 }
2937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002938 // We simply concatenate all the MAP strings, separated by
2939 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002940 ga_concat(&spin->si_map, items[1]);
2941 ga_append(&spin->si_map, '/');
2942 }
2943 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002944 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002945 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2946 {
2947 if (do_sal)
2948 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002949 // SAL item (sounds-a-like)
2950 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002951 if (STRCMP(items[1], "followup") == 0)
2952 spin->si_followup = sal_to_bool(items[2]);
2953 else if (STRCMP(items[1], "collapse_result") == 0)
2954 spin->si_collapse = sal_to_bool(items[2]);
2955 else if (STRCMP(items[1], "remove_accents") == 0)
2956 spin->si_rem_accents = sal_to_bool(items[2]);
2957 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002958 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002959 add_fromto(spin, &spin->si_sal, items[1],
2960 STRCMP(items[2], "_") == 0 ? (char_u *)""
2961 : items[2]);
2962 }
2963 }
2964 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2965 && sofofrom == NULL)
2966 {
2967 sofofrom = getroom_save(spin, items[1]);
2968 }
2969 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2970 && sofoto == NULL)
2971 {
2972 sofoto = getroom_save(spin, items[1]);
2973 }
2974 else if (STRCMP(items[0], "COMMON") == 0)
2975 {
2976 int i;
2977
2978 for (i = 1; i < itemcnt; ++i)
2979 {
2980 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2981 items[i])))
2982 {
2983 p = vim_strsave(items[i]);
2984 if (p == NULL)
2985 break;
2986 hash_add(&spin->si_commonwords, p);
2987 }
2988 }
2989 }
2990 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002991 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002992 fname, lnum, items[0]);
2993 }
2994 }
2995
2996 if (fol != NULL || low != NULL || upp != NULL)
2997 {
2998 if (spin->si_clear_chartab)
2999 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003000 // Clear the char type tables, don't want to use any of the
3001 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003002 init_spell_chartab();
3003 spin->si_clear_chartab = FALSE;
3004 }
3005
3006 /*
3007 * Don't write a word table for an ASCII file, so that we don't check
3008 * for conflicts with a word table that matches 'encoding'.
3009 * Don't write one for utf-8 either, we use utf_*() and
3010 * mb_get_class(), the list of chars in the file will be incomplete.
3011 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003012 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003013 {
3014 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003016 else
3017 (void)set_spell_chartab(fol, low, upp);
3018 }
3019
3020 vim_free(fol);
3021 vim_free(low);
3022 vim_free(upp);
3023 }
3024
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003025 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003026 if (compmax != 0)
3027 {
3028 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3029 spin->si_compmax = compmax;
3030 }
3031
3032 if (compminlen != 0)
3033 {
3034 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3035 spin->si_compminlen = compminlen;
3036 }
3037
3038 if (compsylmax != 0)
3039 {
3040 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003041 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003042 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3043 spin->si_compsylmax = compsylmax;
3044 }
3045
3046 if (compoptions != 0)
3047 {
3048 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3049 spin->si_compoptions |= compoptions;
3050 }
3051
3052 if (compflags != NULL)
3053 process_compflags(spin, aff, compflags);
3054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003055 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003056 if (spin->si_newcompID < spin->si_newprefID)
3057 {
3058 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003059 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003060 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003061 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003062 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003063 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003064 }
3065
3066 if (syllable != NULL)
3067 {
3068 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3069 spin->si_syllable = syllable;
3070 }
3071
3072 if (sofofrom != NULL || sofoto != NULL)
3073 {
3074 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003075 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003076 sofofrom == NULL ? "FROM" : "TO", fname);
3077 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003078 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003079 else
3080 {
3081 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3082 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3083 spin->si_sofofr = sofofrom;
3084 spin->si_sofoto = sofoto;
3085 }
3086 }
3087
3088 if (midword != NULL)
3089 {
3090 aff_check_string(spin->si_midword, midword, "MIDWORD");
3091 spin->si_midword = midword;
3092 }
3093
3094 vim_free(pc);
3095 fclose(fd);
3096 return aff;
3097}
3098
3099/*
3100 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3101 * a comment is following after item "mincount".
3102 */
3103 static int
3104is_aff_rule(
3105 char_u **items,
3106 int itemcnt,
3107 char *rulename,
3108 int mincount)
3109{
3110 return (STRCMP(items[0], rulename) == 0
3111 && (itemcnt == mincount
3112 || (itemcnt > mincount && items[mincount][0] == '#')));
3113}
3114
3115/*
3116 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3117 * ae_flags to ae_comppermit and ae_compforbid.
3118 */
3119 static void
3120aff_process_flags(afffile_T *affile, affentry_T *entry)
3121{
3122 char_u *p;
3123 char_u *prevp;
3124 unsigned flag;
3125
3126 if (entry->ae_flags != NULL
3127 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3128 {
3129 for (p = entry->ae_flags; *p != NUL; )
3130 {
3131 prevp = p;
3132 flag = get_affitem(affile->af_flagtype, &p);
3133 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3134 {
3135 STRMOVE(prevp, p);
3136 p = prevp;
3137 if (flag == affile->af_comppermit)
3138 entry->ae_comppermit = TRUE;
3139 else
3140 entry->ae_compforbid = TRUE;
3141 }
3142 if (affile->af_flagtype == AFT_NUM && *p == ',')
3143 ++p;
3144 }
3145 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003146 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003147 }
3148}
3149
3150/*
3151 * Return TRUE if "s" is the name of an info item in the affix file.
3152 */
3153 static int
3154spell_info_item(char_u *s)
3155{
3156 return STRCMP(s, "NAME") == 0
3157 || STRCMP(s, "HOME") == 0
3158 || STRCMP(s, "VERSION") == 0
3159 || STRCMP(s, "AUTHOR") == 0
3160 || STRCMP(s, "EMAIL") == 0
3161 || STRCMP(s, "COPYRIGHT") == 0;
3162}
3163
3164/*
3165 * Turn an affix flag name into a number, according to the FLAG type.
3166 * returns zero for failure.
3167 */
3168 static unsigned
3169affitem2flag(
3170 int flagtype,
3171 char_u *item,
3172 char_u *fname,
3173 int lnum)
3174{
3175 unsigned res;
3176 char_u *p = item;
3177
3178 res = get_affitem(flagtype, &p);
3179 if (res == 0)
3180 {
3181 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003182 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003183 fname, lnum, item);
3184 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003185 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003186 fname, lnum, item);
3187 }
3188 if (*p != NUL)
3189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003190 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003191 return 0;
3192 }
3193
3194 return res;
3195}
3196
3197/*
3198 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003199 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003200 * Returns zero for an error, still advances the pointer then.
3201 */
3202 static unsigned
3203get_affitem(int flagtype, char_u **pp)
3204{
3205 int res;
3206
3207 if (flagtype == AFT_NUM)
3208 {
3209 if (!VIM_ISDIGIT(**pp))
3210 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003211 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003212 return 0;
3213 }
3214 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003215 if (res == 0)
3216 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003217 }
3218 else
3219 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003220 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003221 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3222 && res >= 'A' && res <= 'Z'))
3223 {
3224 if (**pp == NUL)
3225 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003226 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003227 }
3228 }
3229 return res;
3230}
3231
3232/*
3233 * Process the "compflags" string used in an affix file and append it to
3234 * spin->si_compflags.
3235 * The processing involves changing the affix names to ID numbers, so that
3236 * they fit in one byte.
3237 */
3238 static void
3239process_compflags(
3240 spellinfo_T *spin,
3241 afffile_T *aff,
3242 char_u *compflags)
3243{
3244 char_u *p;
3245 char_u *prevp;
3246 unsigned flag;
3247 compitem_T *ci;
3248 int id;
3249 int len;
3250 char_u *tp;
3251 char_u key[AH_KEY_LEN];
3252 hashitem_T *hi;
3253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003254 // Make room for the old and the new compflags, concatenated with a / in
3255 // between. Processing it makes it shorter, but we don't know by how
3256 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003257 len = (int)STRLEN(compflags) + 1;
3258 if (spin->si_compflags != NULL)
3259 len += (int)STRLEN(spin->si_compflags) + 1;
3260 p = getroom(spin, len, FALSE);
3261 if (p == NULL)
3262 return;
3263 if (spin->si_compflags != NULL)
3264 {
3265 STRCPY(p, spin->si_compflags);
3266 STRCAT(p, "/");
3267 }
3268 spin->si_compflags = p;
3269 tp = p + STRLEN(p);
3270
3271 for (p = compflags; *p != NUL; )
3272 {
3273 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003274 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003275 *tp++ = *p++;
3276 else
3277 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003278 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003279 prevp = p;
3280 flag = get_affitem(aff->af_flagtype, &p);
3281 if (flag != 0)
3282 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003283 // Find the flag in the hashtable. If it was used before, use
3284 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003285 vim_strncpy(key, prevp, p - prevp);
3286 hi = hash_find(&aff->af_comp, key);
3287 if (!HASHITEM_EMPTY(hi))
3288 id = HI2CI(hi)->ci_newID;
3289 else
3290 {
3291 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3292 if (ci == NULL)
3293 break;
3294 STRCPY(ci->ci_key, key);
3295 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003296 // Avoid using a flag ID that has a special meaning in a
3297 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003298 do
3299 {
3300 check_renumber(spin);
3301 id = spin->si_newcompID--;
3302 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3303 ci->ci_newID = id;
3304 hash_add(&aff->af_comp, ci->ci_key);
3305 }
3306 *tp++ = id;
3307 }
3308 if (aff->af_flagtype == AFT_NUM && *p == ',')
3309 ++p;
3310 }
3311 }
3312
3313 *tp = NUL;
3314}
3315
3316/*
3317 * Check that the new IDs for postponed affixes and compounding don't overrun
3318 * each other. We have almost 255 available, but start at 0-127 to avoid
3319 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3320 * When that is used up an error message is given.
3321 */
3322 static void
3323check_renumber(spellinfo_T *spin)
3324{
3325 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3326 {
3327 spin->si_newprefID = 127;
3328 spin->si_newcompID = 255;
3329 }
3330}
3331
3332/*
3333 * Return TRUE if flag "flag" appears in affix list "afflist".
3334 */
3335 static int
3336flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3337{
3338 char_u *p;
3339 unsigned n;
3340
3341 switch (flagtype)
3342 {
3343 case AFT_CHAR:
3344 return vim_strchr(afflist, flag) != NULL;
3345
3346 case AFT_CAPLONG:
3347 case AFT_LONG:
3348 for (p = afflist; *p != NUL; )
3349 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003350 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003351 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3352 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003353 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003354 if (n == flag)
3355 return TRUE;
3356 }
3357 break;
3358
3359 case AFT_NUM:
3360 for (p = afflist; *p != NUL; )
3361 {
3362 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003363 if (n == 0)
3364 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003365 if (n == flag)
3366 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003367 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003368 ++p;
3369 }
3370 break;
3371 }
3372 return FALSE;
3373}
3374
3375/*
3376 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3377 */
3378 static void
3379aff_check_number(int spinval, int affval, char *name)
3380{
3381 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003382 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003383}
3384
3385/*
3386 * Give a warning when "spinval" and "affval" strings are set and not the same.
3387 */
3388 static void
3389aff_check_string(char_u *spinval, char_u *affval, char *name)
3390{
3391 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003392 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003393}
3394
3395/*
3396 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3397 * NULL as equal.
3398 */
3399 static int
3400str_equal(char_u *s1, char_u *s2)
3401{
3402 if (s1 == NULL || s2 == NULL)
3403 return s1 == s2;
3404 return STRCMP(s1, s2) == 0;
3405}
3406
3407/*
3408 * Add a from-to item to "gap". Used for REP and SAL items.
3409 * They are stored case-folded.
3410 */
3411 static void
3412add_fromto(
3413 spellinfo_T *spin,
3414 garray_T *gap,
3415 char_u *from,
3416 char_u *to)
3417{
3418 fromto_T *ftp;
3419 char_u word[MAXWLEN];
3420
3421 if (ga_grow(gap, 1) == OK)
3422 {
3423 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3424 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
3425 ftp->ft_from = getroom_save(spin, word);
3426 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
3427 ftp->ft_to = getroom_save(spin, word);
3428 ++gap->ga_len;
3429 }
3430}
3431
3432/*
3433 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3434 */
3435 static int
3436sal_to_bool(char_u *s)
3437{
3438 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3439}
3440
3441/*
3442 * Free the structure filled by spell_read_aff().
3443 */
3444 static void
3445spell_free_aff(afffile_T *aff)
3446{
3447 hashtab_T *ht;
3448 hashitem_T *hi;
3449 int todo;
3450 affheader_T *ah;
3451 affentry_T *ae;
3452
3453 vim_free(aff->af_enc);
3454
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003455 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003456 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3457 {
3458 todo = (int)ht->ht_used;
3459 for (hi = ht->ht_array; todo > 0; ++hi)
3460 {
3461 if (!HASHITEM_EMPTY(hi))
3462 {
3463 --todo;
3464 ah = HI2AH(hi);
3465 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3466 vim_regfree(ae->ae_prog);
3467 }
3468 }
3469 if (ht == &aff->af_suff)
3470 break;
3471 }
3472
3473 hash_clear(&aff->af_pref);
3474 hash_clear(&aff->af_suff);
3475 hash_clear(&aff->af_comp);
3476}
3477
3478/*
3479 * Read dictionary file "fname".
3480 * Returns OK or FAIL;
3481 */
3482 static int
3483spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3484{
3485 hashtab_T ht;
3486 char_u line[MAXLINELEN];
3487 char_u *p;
3488 char_u *afflist;
3489 char_u store_afflist[MAXWLEN];
3490 int pfxlen;
3491 int need_affix;
3492 char_u *dw;
3493 char_u *pc;
3494 char_u *w;
3495 int l;
3496 hash_T hash;
3497 hashitem_T *hi;
3498 FILE *fd;
3499 int lnum = 1;
3500 int non_ascii = 0;
3501 int retval = OK;
3502 char_u message[MAXLINELEN + MAXWLEN];
3503 int flags;
3504 int duplicate = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003505 time_T last_msg_time = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003506
3507 /*
3508 * Open the file.
3509 */
3510 fd = mch_fopen((char *)fname, "r");
3511 if (fd == NULL)
3512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003513 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003514 return FAIL;
3515 }
3516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003517 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003518 hash_init(&ht);
3519
3520 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003521 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003522 spell_message(spin, IObuff);
3523
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003524 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003525 spin->si_msg_count = 999999;
3526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003527 // Read and ignore the first line: word count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003528 (void)vim_fgets(line, MAXLINELEN, fd);
3529 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003530 semsg(_("E760: No word count in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003531
3532 /*
3533 * Read all the lines in the file one by one.
3534 * The words are converted to 'encoding' here, before being added to
3535 * the hashtable.
3536 */
3537 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3538 {
3539 line_breakcheck();
3540 ++lnum;
3541 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003542 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003544 // Remove CR, LF and white space from the end. White space halfway
3545 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003546 l = (int)STRLEN(line);
3547 while (l > 0 && line[l - 1] <= ' ')
3548 --l;
3549 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003550 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003551 line[l] = NUL;
3552
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003553 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003554 if (spin->si_conv.vc_type != CONV_NONE)
3555 {
3556 pc = string_convert(&spin->si_conv, line, NULL);
3557 if (pc == NULL)
3558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003559 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003560 fname, lnum, line);
3561 continue;
3562 }
3563 w = pc;
3564 }
3565 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003566 {
3567 pc = NULL;
3568 w = line;
3569 }
3570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003571 // Truncate the word at the "/", set "afflist" to what follows.
3572 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003573 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003574 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003575 {
3576 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3577 STRMOVE(p, p + 1);
3578 else if (*p == '/')
3579 {
3580 *p = NUL;
3581 afflist = p + 1;
3582 break;
3583 }
3584 }
3585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003586 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003587 if (spin->si_ascii && has_non_ascii(w))
3588 {
3589 ++non_ascii;
3590 vim_free(pc);
3591 continue;
3592 }
3593
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003594 // This takes time, print a message every 10000 words, but not more
3595 // often than once per second.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003596 if (spin->si_verbose && spin->si_msg_count > 10000)
3597 {
3598 spin->si_msg_count = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003599 if (vim_time() > last_msg_time)
3600 {
3601 last_msg_time = vim_time();
3602 vim_snprintf((char *)message, sizeof(message),
3603 _("line %6d, word %6ld - %s"),
3604 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3605 msg_start();
3606 msg_outtrans_long_attr(message, 0);
3607 msg_clr_eos();
3608 msg_didout = FALSE;
3609 msg_col = 0;
3610 out_flush();
3611 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003612 }
3613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003614 // Store the word in the hashtable to be able to find duplicates.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003615 dw = (char_u *)getroom_save(spin, w);
3616 if (dw == NULL)
3617 {
3618 retval = FAIL;
3619 vim_free(pc);
3620 break;
3621 }
3622
3623 hash = hash_hash(dw);
3624 hi = hash_lookup(&ht, dw, hash);
3625 if (!HASHITEM_EMPTY(hi))
3626 {
3627 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003628 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003629 fname, lnum, dw);
3630 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003631 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003632 fname, lnum, dw);
3633 ++duplicate;
3634 }
3635 else
3636 hash_add_item(&ht, hi, dw, hash);
3637
3638 flags = 0;
3639 store_afflist[0] = NUL;
3640 pfxlen = 0;
3641 need_affix = FALSE;
3642 if (afflist != NULL)
3643 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003644 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003645 flags |= get_affix_flags(affile, afflist);
3646
3647 if (affile->af_needaffix != 0 && flag_in_afflist(
3648 affile->af_flagtype, afflist, affile->af_needaffix))
3649 need_affix = TRUE;
3650
3651 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003652 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003653 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3654
3655 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003656 // Need to store the list of compound flags with the word.
3657 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003658 get_compflags(affile, afflist, store_afflist + pfxlen);
3659 }
3660
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003661 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003662 if (store_word(spin, dw, flags, spin->si_region,
3663 store_afflist, need_affix) == FAIL)
3664 retval = FAIL;
3665
3666 if (afflist != NULL)
3667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003668 // Find all matching suffixes and add the resulting words.
3669 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003670 if (store_aff_word(spin, dw, afflist, affile,
3671 &affile->af_suff, &affile->af_pref,
3672 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3673 retval = FAIL;
3674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003675 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003676 if (store_aff_word(spin, dw, afflist, affile,
3677 &affile->af_pref, NULL,
3678 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3679 retval = FAIL;
3680 }
3681
3682 vim_free(pc);
3683 }
3684
3685 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003686 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003687 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003688 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003689 non_ascii, fname);
3690 hash_clear(&ht);
3691
3692 fclose(fd);
3693 return retval;
3694}
3695
3696/*
3697 * Check for affix flags in "afflist" that are turned into word flags.
3698 * Return WF_ flags.
3699 */
3700 static int
3701get_affix_flags(afffile_T *affile, char_u *afflist)
3702{
3703 int flags = 0;
3704
3705 if (affile->af_keepcase != 0 && flag_in_afflist(
3706 affile->af_flagtype, afflist, affile->af_keepcase))
3707 flags |= WF_KEEPCAP | WF_FIXCAP;
3708 if (affile->af_rare != 0 && flag_in_afflist(
3709 affile->af_flagtype, afflist, affile->af_rare))
3710 flags |= WF_RARE;
3711 if (affile->af_bad != 0 && flag_in_afflist(
3712 affile->af_flagtype, afflist, affile->af_bad))
3713 flags |= WF_BANNED;
3714 if (affile->af_needcomp != 0 && flag_in_afflist(
3715 affile->af_flagtype, afflist, affile->af_needcomp))
3716 flags |= WF_NEEDCOMP;
3717 if (affile->af_comproot != 0 && flag_in_afflist(
3718 affile->af_flagtype, afflist, affile->af_comproot))
3719 flags |= WF_COMPROOT;
3720 if (affile->af_nosuggest != 0 && flag_in_afflist(
3721 affile->af_flagtype, afflist, affile->af_nosuggest))
3722 flags |= WF_NOSUGGEST;
3723 return flags;
3724}
3725
3726/*
3727 * Get the list of prefix IDs from the affix list "afflist".
3728 * Used for PFXPOSTPONE.
3729 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3730 * and return the number of affixes.
3731 */
3732 static int
3733get_pfxlist(
3734 afffile_T *affile,
3735 char_u *afflist,
3736 char_u *store_afflist)
3737{
3738 char_u *p;
3739 char_u *prevp;
3740 int cnt = 0;
3741 int id;
3742 char_u key[AH_KEY_LEN];
3743 hashitem_T *hi;
3744
3745 for (p = afflist; *p != NUL; )
3746 {
3747 prevp = p;
3748 if (get_affitem(affile->af_flagtype, &p) != 0)
3749 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003750 // A flag is a postponed prefix flag if it appears in "af_pref"
3751 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003752 vim_strncpy(key, prevp, p - prevp);
3753 hi = hash_find(&affile->af_pref, key);
3754 if (!HASHITEM_EMPTY(hi))
3755 {
3756 id = HI2AH(hi)->ah_newID;
3757 if (id != 0)
3758 store_afflist[cnt++] = id;
3759 }
3760 }
3761 if (affile->af_flagtype == AFT_NUM && *p == ',')
3762 ++p;
3763 }
3764
3765 store_afflist[cnt] = NUL;
3766 return cnt;
3767}
3768
3769/*
3770 * Get the list of compound IDs from the affix list "afflist" that are used
3771 * for compound words.
3772 * Puts the flags in "store_afflist[]".
3773 */
3774 static void
3775get_compflags(
3776 afffile_T *affile,
3777 char_u *afflist,
3778 char_u *store_afflist)
3779{
3780 char_u *p;
3781 char_u *prevp;
3782 int cnt = 0;
3783 char_u key[AH_KEY_LEN];
3784 hashitem_T *hi;
3785
3786 for (p = afflist; *p != NUL; )
3787 {
3788 prevp = p;
3789 if (get_affitem(affile->af_flagtype, &p) != 0)
3790 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003791 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003792 vim_strncpy(key, prevp, p - prevp);
3793 hi = hash_find(&affile->af_comp, key);
3794 if (!HASHITEM_EMPTY(hi))
3795 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3796 }
3797 if (affile->af_flagtype == AFT_NUM && *p == ',')
3798 ++p;
3799 }
3800
3801 store_afflist[cnt] = NUL;
3802}
3803
3804/*
3805 * Apply affixes to a word and store the resulting words.
3806 * "ht" is the hashtable with affentry_T that need to be applied, either
3807 * prefixes or suffixes.
3808 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3809 * the resulting words for combining affixes.
3810 *
3811 * Returns FAIL when out of memory.
3812 */
3813 static int
3814store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003815 spellinfo_T *spin, // spell info
3816 char_u *word, // basic word start
3817 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003818 afffile_T *affile,
3819 hashtab_T *ht,
3820 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003821 int condit, // CONDIT_SUF et al.
3822 int flags, // flags for the word
3823 char_u *pfxlist, // list of prefix IDs
3824 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3825 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003826{
3827 int todo;
3828 hashitem_T *hi;
3829 affheader_T *ah;
3830 affentry_T *ae;
3831 char_u newword[MAXWLEN];
3832 int retval = OK;
3833 int i, j;
3834 char_u *p;
3835 int use_flags;
3836 char_u *use_pfxlist;
3837 int use_pfxlen;
3838 int need_affix;
3839 char_u store_afflist[MAXWLEN];
3840 char_u pfx_pfxlist[MAXWLEN];
3841 size_t wordlen = STRLEN(word);
3842 int use_condit;
3843
3844 todo = (int)ht->ht_used;
3845 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3846 {
3847 if (!HASHITEM_EMPTY(hi))
3848 {
3849 --todo;
3850 ah = HI2AH(hi);
3851
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // Check that the affix combines, if required, and that the word
3853 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003854 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3855 && flag_in_afflist(affile->af_flagtype, afflist,
3856 ah->ah_flag))
3857 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003858 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003859 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3860 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003861 // Check the condition. It's not logical to match case
3862 // here, but it is required for compatibility with
3863 // Myspell.
3864 // Another requirement from Myspell is that the chop
3865 // string is shorter than the word itself.
3866 // For prefixes, when "PFXPOSTPONE" was used, only do
3867 // prefixes with a chop string and/or flags.
3868 // When a previously added affix had CIRCUMFIX this one
3869 // must have it too, if it had not then this one must not
3870 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003871 if ((xht != NULL || !affile->af_pfxpostpone
3872 || ae->ae_chop != NULL
3873 || ae->ae_flags != NULL)
3874 && (ae->ae_chop == NULL
3875 || STRLEN(ae->ae_chop) < wordlen)
3876 && (ae->ae_prog == NULL
3877 || vim_regexec_prog(&ae->ae_prog, FALSE,
3878 word, (colnr_T)0))
3879 && (((condit & CONDIT_CFIX) == 0)
3880 == ((condit & CONDIT_AFF) == 0
3881 || ae->ae_flags == NULL
3882 || !flag_in_afflist(affile->af_flagtype,
3883 ae->ae_flags, affile->af_circumfix))))
3884 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003885 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003886 if (xht == NULL)
3887 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003888 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003889 if (ae->ae_add == NULL)
3890 *newword = NUL;
3891 else
3892 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3893 p = word;
3894 if (ae->ae_chop != NULL)
3895 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003896 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003897 if (has_mbyte)
3898 {
3899 i = mb_charlen(ae->ae_chop);
3900 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003901 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003902 }
3903 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003904 p += STRLEN(ae->ae_chop);
3905 }
3906 STRCAT(newword, p);
3907 }
3908 else
3909 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003910 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003911 vim_strncpy(newword, word, MAXWLEN - 1);
3912 if (ae->ae_chop != NULL)
3913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003914 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003915 p = newword + STRLEN(newword);
3916 i = (int)MB_CHARLEN(ae->ae_chop);
3917 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003918 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003919 *p = NUL;
3920 }
3921 if (ae->ae_add != NULL)
3922 STRCAT(newword, ae->ae_add);
3923 }
3924
3925 use_flags = flags;
3926 use_pfxlist = pfxlist;
3927 use_pfxlen = pfxlen;
3928 need_affix = FALSE;
3929 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3930 if (ae->ae_flags != NULL)
3931 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003932 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003933 use_flags |= get_affix_flags(affile, ae->ae_flags);
3934
3935 if (affile->af_needaffix != 0 && flag_in_afflist(
3936 affile->af_flagtype, ae->ae_flags,
3937 affile->af_needaffix))
3938 need_affix = TRUE;
3939
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003940 // When there is a CIRCUMFIX flag the other affix
3941 // must also have it and we don't add the word
3942 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003943 if (affile->af_circumfix != 0 && flag_in_afflist(
3944 affile->af_flagtype, ae->ae_flags,
3945 affile->af_circumfix))
3946 {
3947 use_condit |= CONDIT_CFIX;
3948 if ((condit & CONDIT_CFIX) == 0)
3949 need_affix = TRUE;
3950 }
3951
3952 if (affile->af_pfxpostpone
3953 || spin->si_compflags != NULL)
3954 {
3955 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003956 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003957 use_pfxlen = get_pfxlist(affile,
3958 ae->ae_flags, store_afflist);
3959 else
3960 use_pfxlen = 0;
3961 use_pfxlist = store_afflist;
3962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003963 // Combine the prefix IDs. Avoid adding the
3964 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003965 for (i = 0; i < pfxlen; ++i)
3966 {
3967 for (j = 0; j < use_pfxlen; ++j)
3968 if (pfxlist[i] == use_pfxlist[j])
3969 break;
3970 if (j == use_pfxlen)
3971 use_pfxlist[use_pfxlen++] = pfxlist[i];
3972 }
3973
3974 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003976 get_compflags(affile, ae->ae_flags,
3977 use_pfxlist + use_pfxlen);
3978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003979 // Combine the list of compound flags.
3980 // Concatenate them to the prefix IDs list.
3981 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003982 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3983 {
3984 for (j = use_pfxlen;
3985 use_pfxlist[j] != NUL; ++j)
3986 if (pfxlist[i] == use_pfxlist[j])
3987 break;
3988 if (use_pfxlist[j] == NUL)
3989 {
3990 use_pfxlist[j++] = pfxlist[i];
3991 use_pfxlist[j] = NUL;
3992 }
3993 }
3994 }
3995 }
3996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003997 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
3998 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003999 if (use_pfxlist != NULL && ae->ae_compforbid)
4000 {
4001 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
4002 use_pfxlist = pfx_pfxlist;
4003 }
4004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004005 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004006 if (spin->si_prefroot != NULL
4007 && spin->si_prefroot->wn_sibling != NULL)
4008 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004009 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004010 use_flags |= WF_HAS_AFF;
4011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004012 // ... don't use a prefix list if combining
4013 // affixes is not allowed. But do use the
4014 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004015 if (!ah->ah_combine && use_pfxlist != NULL)
4016 use_pfxlist += use_pfxlen;
4017 }
4018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004019 // When compounding is supported and there is no
4020 // "COMPOUNDPERMITFLAG" then forbid compounding on the
4021 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004022 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4023 {
4024 if (xht != NULL)
4025 use_flags |= WF_NOCOMPAFT;
4026 else
4027 use_flags |= WF_NOCOMPBEF;
4028 }
4029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004030 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004031 if (store_word(spin, newword, use_flags,
4032 spin->si_region, use_pfxlist,
4033 need_affix) == FAIL)
4034 retval = FAIL;
4035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004036 // When added a prefix or a first suffix and the affix
4037 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004038 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4039 if (store_aff_word(spin, newword, ae->ae_flags,
4040 affile, &affile->af_suff, xht,
4041 use_condit & (xht == NULL
4042 ? ~0 : ~CONDIT_SUF),
4043 use_flags, use_pfxlist, pfxlen) == FAIL)
4044 retval = FAIL;
4045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004046 // When added a suffix and combining is allowed also
4047 // try adding a prefix additionally. Both for the
4048 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004049 if (xht != NULL && ah->ah_combine)
4050 {
4051 if (store_aff_word(spin, newword,
4052 afflist, affile,
4053 xht, NULL, use_condit,
4054 use_flags, use_pfxlist,
4055 pfxlen) == FAIL
4056 || (ae->ae_flags != NULL
4057 && store_aff_word(spin, newword,
4058 ae->ae_flags, affile,
4059 xht, NULL, use_condit,
4060 use_flags, use_pfxlist,
4061 pfxlen) == FAIL))
4062 retval = FAIL;
4063 }
4064 }
4065 }
4066 }
4067 }
4068 }
4069
4070 return retval;
4071}
4072
4073/*
4074 * Read a file with a list of words.
4075 */
4076 static int
4077spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4078{
4079 FILE *fd;
4080 long lnum = 0;
4081 char_u rline[MAXLINELEN];
4082 char_u *line;
4083 char_u *pc = NULL;
4084 char_u *p;
4085 int l;
4086 int retval = OK;
4087 int did_word = FALSE;
4088 int non_ascii = 0;
4089 int flags;
4090 int regionmask;
4091
4092 /*
4093 * Open the file.
4094 */
4095 fd = mch_fopen((char *)fname, "r");
4096 if (fd == NULL)
4097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004098 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004099 return FAIL;
4100 }
4101
Bram Moolenaarc1669272018-06-19 14:23:53 +02004102 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004103 spell_message(spin, IObuff);
4104
4105 /*
4106 * Read all the lines in the file one by one.
4107 */
4108 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4109 {
4110 line_breakcheck();
4111 ++lnum;
4112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004113 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004114 if (*rline == '#')
4115 continue;
4116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004117 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004118 l = (int)STRLEN(rline);
4119 while (l > 0 && rline[l - 1] <= ' ')
4120 --l;
4121 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004122 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004123 rline[l] = NUL;
4124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004125 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004126 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004127 if (spin->si_conv.vc_type != CONV_NONE)
4128 {
4129 pc = string_convert(&spin->si_conv, rline, NULL);
4130 if (pc == NULL)
4131 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004132 smsg(_("Conversion failure for word in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004133 fname, lnum, rline);
4134 continue;
4135 }
4136 line = pc;
4137 }
4138 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004139 {
4140 pc = NULL;
4141 line = rline;
4142 }
4143
4144 if (*line == '/')
4145 {
4146 ++line;
4147 if (STRNCMP(line, "encoding=", 9) == 0)
4148 {
4149 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004150 smsg(_("Duplicate /encoding= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004151 fname, lnum, line - 1);
4152 else if (did_word)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004153 smsg(_("/encoding= line after word ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004154 fname, lnum, line - 1);
4155 else
4156 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004157 char_u *enc;
4158
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004159 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004160 line += 9;
4161 enc = enc_canonize(line);
4162 if (enc != NULL && !spin->si_ascii
4163 && convert_setup(&spin->si_conv, enc,
4164 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004165 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004166 fname, line, p_enc);
4167 vim_free(enc);
4168 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004169 }
4170 continue;
4171 }
4172
4173 if (STRNCMP(line, "regions=", 8) == 0)
4174 {
4175 if (spin->si_region_count > 1)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004176 smsg(_("Duplicate /regions= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004177 fname, lnum, line);
4178 else
4179 {
4180 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004181 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004182 smsg(_("Too many regions in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004183 fname, lnum, line);
4184 else
4185 {
4186 spin->si_region_count = (int)STRLEN(line) / 2;
4187 STRCPY(spin->si_region_name, line);
4188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004189 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004190 spin->si_region = (1 << spin->si_region_count) - 1;
4191 }
4192 }
4193 continue;
4194 }
4195
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004196 smsg(_("/ line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004197 fname, lnum, line - 1);
4198 continue;
4199 }
4200
4201 flags = 0;
4202 regionmask = spin->si_region;
4203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004204 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004205 p = vim_strchr(line, '/');
4206 if (p != NULL)
4207 {
4208 *p++ = NUL;
4209 while (*p != NUL)
4210 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004211 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004212 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004213 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004214 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004215 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004216 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004217 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004218 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004219 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004220 regionmask = 0;
4221 flags |= WF_REGION;
4222
4223 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004224 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004225 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004226 smsg(_("Invalid region nr in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004227 fname, lnum, p);
4228 break;
4229 }
4230 regionmask |= 1 << (l - 1);
4231 }
4232 else
4233 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004234 smsg(_("Unrecognized flags in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004235 fname, lnum, p);
4236 break;
4237 }
4238 ++p;
4239 }
4240 }
4241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004242 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004243 if (spin->si_ascii && has_non_ascii(line))
4244 {
4245 ++non_ascii;
4246 continue;
4247 }
4248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004249 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004250 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4251 {
4252 retval = FAIL;
4253 break;
4254 }
4255 did_word = TRUE;
4256 }
4257
4258 vim_free(pc);
4259 fclose(fd);
4260
4261 if (spin->si_ascii && non_ascii > 0)
4262 {
4263 vim_snprintf((char *)IObuff, IOSIZE,
4264 _("Ignored %d words with non-ASCII characters"), non_ascii);
4265 spell_message(spin, IObuff);
4266 }
4267
4268 return retval;
4269}
4270
4271/*
4272 * Get part of an sblock_T, "len" bytes long.
4273 * This avoids calling free() for every little struct we use (and keeping
4274 * track of them).
4275 * The memory is cleared to all zeros.
4276 * Returns NULL when out of memory.
4277 */
4278 static void *
4279getroom(
4280 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004281 size_t len, // length needed
4282 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004283{
4284 char_u *p;
4285 sblock_T *bl = spin->si_blocks;
4286
4287 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004288 // Round size up for alignment. On some systems structures need to be
4289 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004290 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4291 & ~(sizeof(char *) - 1);
4292
4293 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4294 {
4295 if (len >= SBLOCKSIZE)
4296 bl = NULL;
4297 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004298 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004299 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004300 if (bl == NULL)
4301 {
4302 if (!spin->si_did_emsg)
4303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004304 emsg(_("E845: Insufficient memory, word list will be incomplete"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004305 spin->si_did_emsg = TRUE;
4306 }
4307 return NULL;
4308 }
4309 bl->sb_next = spin->si_blocks;
4310 spin->si_blocks = bl;
4311 bl->sb_used = 0;
4312 ++spin->si_blocks_cnt;
4313 }
4314
4315 p = bl->sb_data + bl->sb_used;
4316 bl->sb_used += (int)len;
4317
4318 return p;
4319}
4320
4321/*
4322 * Make a copy of a string into memory allocated with getroom().
4323 * Returns NULL when out of memory.
4324 */
4325 static char_u *
4326getroom_save(spellinfo_T *spin, char_u *s)
4327{
4328 char_u *sc;
4329
4330 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4331 if (sc != NULL)
4332 STRCPY(sc, s);
4333 return sc;
4334}
4335
4336
4337/*
4338 * Free the list of allocated sblock_T.
4339 */
4340 static void
4341free_blocks(sblock_T *bl)
4342{
4343 sblock_T *next;
4344
4345 while (bl != NULL)
4346 {
4347 next = bl->sb_next;
4348 vim_free(bl);
4349 bl = next;
4350 }
4351}
4352
4353/*
4354 * Allocate the root of a word tree.
4355 * Returns NULL when out of memory.
4356 */
4357 static wordnode_T *
4358wordtree_alloc(spellinfo_T *spin)
4359{
4360 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4361}
4362
4363/*
4364 * Store a word in the tree(s).
4365 * Always store it in the case-folded tree. For a keep-case word this is
4366 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4367 * used to find suggestions.
4368 * For a keep-case word also store it in the keep-case tree.
4369 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4370 * compound flag.
4371 */
4372 static int
4373store_word(
4374 spellinfo_T *spin,
4375 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004376 int flags, // extra flags, WF_BANNED
4377 int region, // supported region(s)
4378 char_u *pfxlist, // list of prefix IDs or NULL
4379 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004380{
4381 int len = (int)STRLEN(word);
4382 int ct = captype(word, word + len);
4383 char_u foldword[MAXWLEN];
4384 int res = OK;
4385 char_u *p;
4386
4387 (void)spell_casefold(word, len, foldword, MAXWLEN);
4388 for (p = pfxlist; res == OK; ++p)
4389 {
4390 if (!need_affix || (p != NULL && *p != NUL))
4391 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4392 region, p == NULL ? 0 : *p);
4393 if (p == NULL || *p == NUL)
4394 break;
4395 }
4396 ++spin->si_foldwcount;
4397
4398 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4399 {
4400 for (p = pfxlist; res == OK; ++p)
4401 {
4402 if (!need_affix || (p != NULL && *p != NUL))
4403 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4404 region, p == NULL ? 0 : *p);
4405 if (p == NULL || *p == NUL)
4406 break;
4407 }
4408 ++spin->si_keepwcount;
4409 }
4410 return res;
4411}
4412
4413/*
4414 * Add word "word" to a word tree at "root".
4415 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4416 * "rare" and "region" is the condition nr.
4417 * Returns FAIL when out of memory.
4418 */
4419 static int
4420tree_add_word(
4421 spellinfo_T *spin,
4422 char_u *word,
4423 wordnode_T *root,
4424 int flags,
4425 int region,
4426 int affixID)
4427{
4428 wordnode_T *node = root;
4429 wordnode_T *np;
4430 wordnode_T *copyp, **copyprev;
4431 wordnode_T **prev = NULL;
4432 int i;
4433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004434 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004435 for (i = 0; ; ++i)
4436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004437 // When there is more than one reference to this node we need to make
4438 // a copy, so that we can modify it. Copy the whole list of siblings
4439 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004440 if (node != NULL && node->wn_refs > 1)
4441 {
4442 --node->wn_refs;
4443 copyprev = prev;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004444 FOR_ALL_NODE_SIBLINGS(node, copyp)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004445 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004446 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004447 np = get_wordnode(spin);
4448 if (np == NULL)
4449 return FAIL;
4450 np->wn_child = copyp->wn_child;
4451 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004452 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004453 np->wn_byte = copyp->wn_byte;
4454 if (np->wn_byte == NUL)
4455 {
4456 np->wn_flags = copyp->wn_flags;
4457 np->wn_region = copyp->wn_region;
4458 np->wn_affixID = copyp->wn_affixID;
4459 }
4460
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004461 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004462 np->wn_refs = 1;
4463 if (copyprev != NULL)
4464 *copyprev = np;
4465 copyprev = &np->wn_sibling;
4466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004467 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004468 if (copyp == node)
4469 node = np;
4470 }
4471 }
4472
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004473 // Look for the sibling that has the same character. They are sorted
4474 // on byte value, thus stop searching when a sibling is found with a
4475 // higher byte value. For zero bytes (end of word) the sorting is
4476 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004477 while (node != NULL
4478 && (node->wn_byte < word[i]
4479 || (node->wn_byte == NUL
4480 && (flags < 0
4481 ? node->wn_affixID < (unsigned)affixID
4482 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4483 || (node->wn_flags == (flags & WN_MASK)
4484 && (spin->si_sugtree
4485 ? (node->wn_region & 0xffff) < region
4486 : node->wn_affixID
4487 < (unsigned)affixID)))))))
4488 {
4489 prev = &node->wn_sibling;
4490 node = *prev;
4491 }
4492 if (node == NULL
4493 || node->wn_byte != word[i]
4494 || (word[i] == NUL
4495 && (flags < 0
4496 || spin->si_sugtree
4497 || node->wn_flags != (flags & WN_MASK)
4498 || node->wn_affixID != affixID)))
4499 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004500 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004501 np = get_wordnode(spin);
4502 if (np == NULL)
4503 return FAIL;
4504 np->wn_byte = word[i];
4505
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004506 // If "node" is NULL this is a new child or the end of the sibling
4507 // list: ref count is one. Otherwise use ref count of sibling and
4508 // make ref count of sibling one (matters when inserting in front
4509 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004510 if (node == NULL)
4511 np->wn_refs = 1;
4512 else
4513 {
4514 np->wn_refs = node->wn_refs;
4515 node->wn_refs = 1;
4516 }
4517 if (prev != NULL)
4518 *prev = np;
4519 np->wn_sibling = node;
4520 node = np;
4521 }
4522
4523 if (word[i] == NUL)
4524 {
4525 node->wn_flags = flags;
4526 node->wn_region |= region;
4527 node->wn_affixID = affixID;
4528 break;
4529 }
4530 prev = &node->wn_child;
4531 node = *prev;
4532 }
4533#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004534 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004535 spell_print_tree(root->wn_sibling);
4536#endif
4537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004538 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004539 ++spin->si_msg_count;
4540
4541 if (spin->si_compress_cnt > 1)
4542 {
4543 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004544 // Did enough words to lower the block count limit.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004545 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004546 }
4547
4548 /*
4549 * When we have allocated lots of memory we need to compress the word tree
4550 * to free up some room. But compression is slow, and we might actually
4551 * need that room, thus only compress in the following situations:
4552 * 1. When not compressed before (si_compress_cnt == 0): when using
4553 * "compress_start" blocks.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004554 * 2. When compressed before and used "compress_inc" blocks before
4555 * adding "compress_added" words (si_compress_cnt > 1).
4556 * 3. When compressed before, added "compress_added" words
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004557 * (si_compress_cnt == 1) and the number of free nodes drops below the
4558 * maximum word length.
4559 */
4560#ifndef SPELL_COMPRESS_ALLWAYS
4561 if (spin->si_compress_cnt == 1
4562 ? spin->si_free_count < MAXWLEN
4563 : spin->si_blocks_cnt >= compress_start)
4564#endif
4565 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004566 // Decrement the block counter. The effect is that we compress again
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004567 // when the freed up room has been used and another "compress_inc"
4568 // blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004569 // been added, then the limit is put back again.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004570 spin->si_blocks_cnt -= compress_inc;
4571 spin->si_compress_cnt = compress_added;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004572
4573 if (spin->si_verbose)
4574 {
4575 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004576 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004577 msg_clr_eos();
4578 msg_didout = FALSE;
4579 msg_col = 0;
4580 out_flush();
4581 }
4582
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004583 // Compress both trees. Either they both have many nodes, which makes
4584 // compression useful, or one of them is small, which means
4585 // compression goes fast. But when filling the soundfold word tree
4586 // there is no keep-case tree.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004587 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004588 if (affixID >= 0)
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004589 wordtree_compress(spin, spin->si_keeproot, "keep-case");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004590 }
4591
4592 return OK;
4593}
4594
4595/*
4596 * Get a wordnode_T, either from the list of previously freed nodes or
4597 * allocate a new one.
4598 * Returns NULL when out of memory.
4599 */
4600 static wordnode_T *
4601get_wordnode(spellinfo_T *spin)
4602{
4603 wordnode_T *n;
4604
4605 if (spin->si_first_free == NULL)
4606 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4607 else
4608 {
4609 n = spin->si_first_free;
4610 spin->si_first_free = n->wn_child;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004611 CLEAR_POINTER(n);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004612 --spin->si_free_count;
4613 }
4614#ifdef SPELL_PRINTTREE
4615 if (n != NULL)
4616 n->wn_nr = ++spin->si_wordnode_nr;
4617#endif
4618 return n;
4619}
4620
4621/*
4622 * Decrement the reference count on a node (which is the head of a list of
4623 * siblings). If the reference count becomes zero free the node and its
4624 * siblings.
4625 * Returns the number of nodes actually freed.
4626 */
4627 static int
4628deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4629{
4630 wordnode_T *np;
4631 int cnt = 0;
4632
4633 if (--node->wn_refs == 0)
4634 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004635 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004636 {
4637 if (np->wn_child != NULL)
4638 cnt += deref_wordnode(spin, np->wn_child);
4639 free_wordnode(spin, np);
4640 ++cnt;
4641 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004642 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004643 }
4644 return cnt;
4645}
4646
4647/*
4648 * Free a wordnode_T for re-use later.
4649 * Only the "wn_child" field becomes invalid.
4650 */
4651 static void
4652free_wordnode(spellinfo_T *spin, wordnode_T *n)
4653{
4654 n->wn_child = spin->si_first_free;
4655 spin->si_first_free = n;
4656 ++spin->si_free_count;
4657}
4658
4659/*
4660 * Compress a tree: find tails that are identical and can be shared.
4661 */
4662 static void
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004663wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004664{
4665 hashtab_T ht;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004666 long n;
4667 long tot = 0;
4668 long perc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004669
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004670 // Skip the root itself, it's not actually used. The first sibling is the
4671 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004672 if (root->wn_sibling != NULL)
4673 {
4674 hash_init(&ht);
4675 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4676
4677#ifndef SPELL_PRINTTREE
4678 if (spin->si_verbose || p_verbose > 2)
4679#endif
4680 {
4681 if (tot > 1000000)
4682 perc = (tot - n) / (tot / 100);
4683 else if (tot == 0)
4684 perc = 0;
4685 else
4686 perc = (tot - n) * 100 / tot;
4687 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004688 _("Compressed %s: %ld of %ld nodes; %ld (%ld%%) remaining"),
4689 name, n, tot, tot - n, perc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004690 spell_message(spin, IObuff);
4691 }
4692#ifdef SPELL_PRINTTREE
4693 spell_print_tree(root->wn_sibling);
4694#endif
4695 hash_clear(&ht);
4696 }
4697}
4698
4699/*
4700 * Compress a node, its siblings and its children, depth first.
4701 * Returns the number of compressed nodes.
4702 */
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004703 static long
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004704node_compress(
4705 spellinfo_T *spin,
4706 wordnode_T *node,
4707 hashtab_T *ht,
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004708 long *tot) // total count of nodes before compressing,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004709 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004710{
4711 wordnode_T *np;
4712 wordnode_T *tp;
4713 wordnode_T *child;
4714 hash_T hash;
4715 hashitem_T *hi;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004716 long len = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004717 unsigned nr, n;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004718 long compressed = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004719
4720 /*
4721 * Go through the list of siblings. Compress each child and then try
4722 * finding an identical child to replace it.
4723 * Note that with "child" we mean not just the node that is pointed to,
4724 * but the whole list of siblings of which the child node is the first.
4725 */
4726 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4727 {
4728 ++len;
4729 if ((child = np->wn_child) != NULL)
4730 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004731 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004732 compressed += node_compress(spin, child, ht, tot);
4733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004734 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004735 hash = hash_hash(child->wn_u1.hashkey);
4736 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4737 if (!HASHITEM_EMPTY(hi))
4738 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004739 // There are children we encountered before with a hash value
4740 // identical to the current child. Now check if there is one
4741 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004742 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4743 if (node_equal(child, tp))
4744 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004745 // Found one! Now use that child in place of the
4746 // current one. This means the current child and all
4747 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004748 ++tp->wn_refs;
4749 compressed += deref_wordnode(spin, child);
4750 np->wn_child = tp;
4751 break;
4752 }
4753 if (tp == NULL)
4754 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004755 // No other child with this hash value equals the child of
4756 // the node, add it to the linked list after the first
4757 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004758 tp = HI2WN(hi);
4759 child->wn_u2.next = tp->wn_u2.next;
4760 tp->wn_u2.next = child;
4761 }
4762 }
4763 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004764 // No other child has this hash value, add it to the
4765 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004766 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4767 }
4768 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004769 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004770
4771 /*
4772 * Make a hash key for the node and its siblings, so that we can quickly
4773 * find a lookalike node. This must be done after compressing the sibling
4774 * list, otherwise the hash key would become invalid by the compression.
4775 */
4776 node->wn_u1.hashkey[0] = len;
4777 nr = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004778 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004779 {
4780 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004781 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004782 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4783 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004784 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004785 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4786 nr = nr * 101 + n;
4787 }
4788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004789 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004790 n = nr & 0xff;
4791 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4792 n = (nr >> 8) & 0xff;
4793 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4794 n = (nr >> 16) & 0xff;
4795 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4796 n = (nr >> 24) & 0xff;
4797 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4798 node->wn_u1.hashkey[5] = NUL;
4799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004800 // Check for CTRL-C pressed now and then.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004801 veryfast_breakcheck();
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004802
4803 return compressed;
4804}
4805
4806/*
4807 * Return TRUE when two nodes have identical siblings and children.
4808 */
4809 static int
4810node_equal(wordnode_T *n1, wordnode_T *n2)
4811{
4812 wordnode_T *p1;
4813 wordnode_T *p2;
4814
4815 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4816 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4817 if (p1->wn_byte != p2->wn_byte
4818 || (p1->wn_byte == NUL
4819 ? (p1->wn_flags != p2->wn_flags
4820 || p1->wn_region != p2->wn_region
4821 || p1->wn_affixID != p2->wn_affixID)
4822 : (p1->wn_child != p2->wn_child)))
4823 break;
4824
4825 return p1 == NULL && p2 == NULL;
4826}
4827
Bram Moolenaareae1b912019-05-09 15:12:55 +02004828static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004829
4830/*
4831 * Function given to qsort() to sort the REP items on "from" string.
4832 */
4833 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004834rep_compare(const void *s1, const void *s2)
4835{
4836 fromto_T *p1 = (fromto_T *)s1;
4837 fromto_T *p2 = (fromto_T *)s2;
4838
4839 return STRCMP(p1->ft_from, p2->ft_from);
4840}
4841
4842/*
4843 * Write the Vim .spl file "fname".
4844 * Return FAIL or OK;
4845 */
4846 static int
4847write_vim_spell(spellinfo_T *spin, char_u *fname)
4848{
4849 FILE *fd;
4850 int regionmask;
4851 int round;
4852 wordnode_T *tree;
4853 int nodecount;
4854 int i;
4855 int l;
4856 garray_T *gap;
4857 fromto_T *ftp;
4858 char_u *p;
4859 int rr;
4860 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004861 size_t fwv = 1; // collect return value of fwrite() to avoid
4862 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004863
4864 fd = mch_fopen((char *)fname, "w");
4865 if (fd == NULL)
4866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004868 return FAIL;
4869 }
4870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004871 // <HEADER>: <fileID> <versionnr>
4872 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004873 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4874 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004875 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004876 goto theend;
4877
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004878 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004879
4880 /*
4881 * <SECTIONS>: <section> ... <sectionend>
4882 */
4883
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004884 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004885 if (spin->si_info != NULL)
4886 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004887 putc(SN_INFO, fd); // <sectionID>
4888 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004889
4890 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004891 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4892 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004893 }
4894
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004895 // SN_REGION: <regionname> ...
4896 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004897 if (spin->si_region_count > 1)
4898 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004899 putc(SN_REGION, fd); // <sectionID>
4900 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004901 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004902 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004903 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004904 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004905 regionmask = (1 << spin->si_region_count) - 1;
4906 }
4907 else
4908 regionmask = 0;
4909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004910 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4911 //
4912 // The table with character flags and the table for case folding.
4913 // This makes sure the same characters are recognized as word characters
4914 // when generating an when using a spell file.
4915 // Skip this for ASCII, the table may conflict with the one used for
4916 // 'encoding'.
4917 // Also skip this for an .add.spl file, the main spell file must contain
4918 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004919 if (!spin->si_ascii && !spin->si_add)
4920 {
4921 char_u folchars[128 * 8];
4922 int flags;
4923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004924 putc(SN_CHARFLAGS, fd); // <sectionID>
4925 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004927 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004928 l = 0;
4929 for (i = 128; i < 256; ++i)
4930 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004931 if (has_mbyte)
4932 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4933 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004934 folchars[l++] = spelltab.st_fold[i];
4935 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004936 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004938 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004939 for (i = 128; i < 256; ++i)
4940 {
4941 flags = 0;
4942 if (spelltab.st_isw[i])
4943 flags |= CF_WORD;
4944 if (spelltab.st_isu[i])
4945 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004946 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004947 }
4948
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004949 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4950 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004951 }
4952
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004953 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004954 if (spin->si_midword != NULL)
4955 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004956 putc(SN_MIDWORD, fd); // <sectionID>
4957 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004958
4959 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004960 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004961 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004962 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004963 }
4964
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004965 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004966 if (spin->si_prefcond.ga_len > 0)
4967 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004968 putc(SN_PREFCOND, fd); // <sectionID>
4969 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004970
4971 l = write_spell_prefcond(NULL, &spin->si_prefcond);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004972 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004973
4974 write_spell_prefcond(fd, &spin->si_prefcond);
4975 }
4976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004977 // SN_REP: <repcount> <rep> ...
4978 // SN_SAL: <salflags> <salcount> <sal> ...
4979 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004980
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004981 // round 1: SN_REP section
4982 // round 2: SN_SAL section (unless SN_SOFO is used)
4983 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004984 for (round = 1; round <= 3; ++round)
4985 {
4986 if (round == 1)
4987 gap = &spin->si_rep;
4988 else if (round == 2)
4989 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004990 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004991 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4992 continue;
4993 gap = &spin->si_sal;
4994 }
4995 else
4996 gap = &spin->si_repsal;
4997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004998 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004999 if (gap->ga_len == 0)
5000 continue;
5001
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005002 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005003 if (round != 2)
5004 qsort(gap->ga_data, (size_t)gap->ga_len,
5005 sizeof(fromto_T), rep_compare);
5006
5007 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005008 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005010 // This is for making suggestions, section is not required.
5011 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005013 // Compute the length of what follows.
5014 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005015 for (i = 0; i < gap->ga_len; ++i)
5016 {
5017 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005018 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
5019 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005020 }
5021 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005022 ++l; // count <salflags>
5023 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005024
5025 if (round == 2)
5026 {
5027 i = 0;
5028 if (spin->si_followup)
5029 i |= SAL_F0LLOWUP;
5030 if (spin->si_collapse)
5031 i |= SAL_COLLAPSE;
5032 if (spin->si_rem_accents)
5033 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005034 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005035 }
5036
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005037 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005038 for (i = 0; i < gap->ga_len; ++i)
5039 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005040 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5041 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005042 ftp = &((fromto_T *)gap->ga_data)[i];
5043 for (rr = 1; rr <= 2; ++rr)
5044 {
5045 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5046 l = (int)STRLEN(p);
5047 putc(l, fd);
5048 if (l > 0)
5049 fwv &= fwrite(p, l, (size_t)1, fd);
5050 }
5051 }
5052
5053 }
5054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005055 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5056 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005057 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5058 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005059 putc(SN_SOFO, fd); // <sectionID>
5060 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005061
5062 l = (int)STRLEN(spin->si_sofofr);
5063 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005064 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005066 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5067 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005068
5069 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005070 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5071 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005072 }
5073
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005074 // SN_WORDS: <word> ...
5075 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005076 if (spin->si_commonwords.ht_used > 0)
5077 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005078 putc(SN_WORDS, fd); // <sectionID>
5079 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005081 // round 1: count the bytes
5082 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005083 for (round = 1; round <= 2; ++round)
5084 {
5085 int todo;
5086 int len = 0;
5087 hashitem_T *hi;
5088
5089 todo = (int)spin->si_commonwords.ht_used;
5090 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5091 if (!HASHITEM_EMPTY(hi))
5092 {
5093 l = (int)STRLEN(hi->hi_key) + 1;
5094 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005095 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005096 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5097 --todo;
5098 }
5099 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005100 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005101 }
5102 }
5103
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005104 // SN_MAP: <mapstr>
5105 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005106 if (spin->si_map.ga_len > 0)
5107 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005108 putc(SN_MAP, fd); // <sectionID>
5109 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005110 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005111 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005112 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005113 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005114 }
5115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005116 // SN_SUGFILE: <timestamp>
5117 // This is used to notify that a .sug file may be available and at the
5118 // same time allows for checking that a .sug file that is found matches
5119 // with this .spl file. That's because the word numbers must be exactly
5120 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005121 if (!spin->si_nosugfile
5122 && (spin->si_sal.ga_len > 0
5123 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5124 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005125 putc(SN_SUGFILE, fd); // <sectionID>
5126 putc(0, fd); // <sectionflags>
5127 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005129 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005130 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005131 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005132 }
5133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005134 // SN_NOSPLITSUGS: nothing
5135 // This is used to notify that no suggestions with word splits are to be
5136 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005137 if (spin->si_nosplitsugs)
5138 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005139 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5140 putc(0, fd); // <sectionflags>
5141 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005142 }
5143
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005144 // SN_NOCOMPUNDSUGS: nothing
5145 // This is used to notify that no suggestions with compounds are to be
5146 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005147 if (spin->si_nocompoundsugs)
5148 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005149 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5150 putc(0, fd); // <sectionflags>
5151 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005152 }
5153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005154 // SN_COMPOUND: compound info.
5155 // We don't mark it required, when not supported all compound words will
5156 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005157 if (spin->si_compflags != NULL)
5158 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005159 putc(SN_COMPOUND, fd); // <sectionID>
5160 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005161
5162 l = (int)STRLEN(spin->si_compflags);
5163 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5164 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005165 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005167 putc(spin->si_compmax, fd); // <compmax>
5168 putc(spin->si_compminlen, fd); // <compminlen>
5169 putc(spin->si_compsylmax, fd); // <compsylmax>
5170 putc(0, fd); // for Vim 7.0b compatibility
5171 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005172 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005173 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005174 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5175 {
5176 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005177 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005178 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005179 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005180 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005181 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005182 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5183 (size_t)1, fd);
5184 }
5185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005186 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005187 if (spin->si_nobreak)
5188 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005189 putc(SN_NOBREAK, fd); // <sectionID>
5190 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005192 // It's empty, the presence of the section flags the feature.
5193 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005194 }
5195
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005196 // SN_SYLLABLE: syllable info.
5197 // We don't mark it required, when not supported syllables will not be
5198 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005199 if (spin->si_syllable != NULL)
5200 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005201 putc(SN_SYLLABLE, fd); // <sectionID>
5202 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005203
5204 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005206 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005207 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005208 }
5209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005210 // end of <SECTIONS>
5211 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005212
5213
5214 /*
5215 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5216 */
5217 spin->si_memtot = 0;
5218 for (round = 1; round <= 3; ++round)
5219 {
5220 if (round == 1)
5221 tree = spin->si_foldroot->wn_sibling;
5222 else if (round == 2)
5223 tree = spin->si_keeproot->wn_sibling;
5224 else
5225 tree = spin->si_prefroot->wn_sibling;
5226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005227 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005228 clear_node(tree);
5229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005230 // Count the number of nodes. Needed to be able to allocate the
5231 // memory when reading the nodes. Also fills in index for shared
5232 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005233 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5234
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005235 // number of nodes in 4 bytes
5236 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005237 spin->si_memtot += nodecount + nodecount * sizeof(int);
5238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005239 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005240 (void)put_node(fd, tree, 0, regionmask, round == 3);
5241 }
5242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005243 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005244 if (putc(0, fd) == EOF)
5245 retval = FAIL;
5246theend:
5247 if (fclose(fd) == EOF)
5248 retval = FAIL;
5249
5250 if (fwv != (size_t)1)
5251 retval = FAIL;
5252 if (retval == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005253 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005254
5255 return retval;
5256}
5257
5258/*
5259 * Clear the index and wnode fields of "node", it siblings and its
5260 * children. This is needed because they are a union with other items to save
5261 * space.
5262 */
5263 static void
5264clear_node(wordnode_T *node)
5265{
5266 wordnode_T *np;
5267
5268 if (node != NULL)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005269 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005270 {
5271 np->wn_u1.index = 0;
5272 np->wn_u2.wnode = NULL;
5273
5274 if (np->wn_byte != NUL)
5275 clear_node(np->wn_child);
5276 }
5277}
5278
5279
5280/*
5281 * Dump a word tree at node "node".
5282 *
5283 * This first writes the list of possible bytes (siblings). Then for each
5284 * byte recursively write the children.
5285 *
5286 * NOTE: The code here must match the code in read_tree_node(), since
5287 * assumptions are made about the indexes (so that we don't have to write them
5288 * in the file).
5289 *
5290 * Returns the number of nodes used.
5291 */
5292 static int
5293put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005294 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005295 wordnode_T *node,
5296 int idx,
5297 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005299{
5300 int newindex = idx;
5301 int siblingcount = 0;
5302 wordnode_T *np;
5303 int flags;
5304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005305 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005306 if (node == NULL)
5307 return 0;
5308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005309 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005310 node->wn_u1.index = idx;
5311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005312 // Count the number of siblings.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005313 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005314 ++siblingcount;
5315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005316 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005317 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005318 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005319
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005320 // Write each sibling byte and optionally extra info.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005321 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005322 {
5323 if (np->wn_byte == 0)
5324 {
5325 if (fd != NULL)
5326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005327 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005328 if (prefixtree)
5329 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005330 // In PREFIXTREE write the required affixID and the
5331 // associated condition nr (stored in wn_region). The
5332 // byte value is misused to store the "rare" and "not
5333 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005334 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005335 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005336 else
5337 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005338 putc(BY_FLAGS, fd); // <byte>
5339 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005340 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005341 putc(np->wn_affixID, fd); // <affixID>
5342 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005343 }
5344 else
5345 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005346 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005347 flags = np->wn_flags;
5348 if (regionmask != 0 && np->wn_region != regionmask)
5349 flags |= WF_REGION;
5350 if (np->wn_affixID != 0)
5351 flags |= WF_AFX;
5352 if (flags == 0)
5353 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005354 // word without flags or region
5355 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005356 }
5357 else
5358 {
5359 if (np->wn_flags >= 0x100)
5360 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005361 putc(BY_FLAGS2, fd); // <byte>
5362 putc(flags, fd); // <flags>
5363 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005364 }
5365 else
5366 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005367 putc(BY_FLAGS, fd); // <byte>
5368 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005369 }
5370 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005371 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005372 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005373 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005374 }
5375 }
5376 }
5377 }
5378 else
5379 {
5380 if (np->wn_child->wn_u1.index != 0
5381 && np->wn_child->wn_u2.wnode != node)
5382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005383 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005384 if (fd != NULL)
5385 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005386 putc(BY_INDEX, fd); // <byte>
5387 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005388 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5389 }
5390 }
5391 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005392 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005393 np->wn_child->wn_u2.wnode = node;
5394
5395 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005396 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005398 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005399 return 0;
5400 }
5401 }
5402 }
5403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005404 // Space used in the array when reading: one for each sibling and one for
5405 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005406 newindex += siblingcount + 1;
5407
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005408 // Recursively dump the children of each sibling.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005409 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005410 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5411 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5412 prefixtree);
5413
5414 return newindex;
5415}
5416
5417
5418/*
5419 * ":mkspell [-ascii] outfile infile ..."
5420 * ":mkspell [-ascii] addfile"
5421 */
5422 void
5423ex_mkspell(exarg_T *eap)
5424{
5425 int fcount;
5426 char_u **fnames;
5427 char_u *arg = eap->arg;
5428 int ascii = FALSE;
5429
5430 if (STRNCMP(arg, "-ascii", 6) == 0)
5431 {
5432 ascii = TRUE;
5433 arg = skipwhite(arg + 6);
5434 }
5435
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005436 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005437 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5438 {
5439 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5440 FreeWild(fcount, fnames);
5441 }
5442}
5443
5444/*
5445 * Create the .sug file.
5446 * Uses the soundfold info in "spin".
5447 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5448 */
5449 static void
5450spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5451{
5452 char_u *fname = NULL;
5453 int len;
5454 slang_T *slang;
5455 int free_slang = FALSE;
5456
5457 /*
5458 * Read back the .spl file that was written. This fills the required
5459 * info for soundfolding. This also uses less memory than the
5460 * pointer-linked version of the trie. And it avoids having two versions
5461 * of the code for the soundfolding stuff.
5462 * It might have been done already by spell_reload_one().
5463 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005464 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005465 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005466 break;
5467 if (slang == NULL)
5468 {
5469 spell_message(spin, (char_u *)_("Reading back spell file..."));
5470 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5471 if (slang == NULL)
5472 return;
5473 free_slang = TRUE;
5474 }
5475
5476 /*
5477 * Clear the info in "spin" that is used.
5478 */
5479 spin->si_blocks = NULL;
5480 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005481 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005482 spin->si_free_count = 0;
5483 spin->si_first_free = NULL;
5484 spin->si_foldwcount = 0;
5485
5486 /*
5487 * Go through the trie of good words, soundfold each word and add it to
5488 * the soundfold trie.
5489 */
5490 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5491 if (sug_filltree(spin, slang) == FAIL)
5492 goto theend;
5493
5494 /*
5495 * Create the table which links each soundfold word with a list of the
5496 * good words it may come from. Creates buffer "spin->si_spellbuf".
5497 * This also removes the wordnr from the NUL byte entries to make
5498 * compression possible.
5499 */
5500 if (sug_maketable(spin) == FAIL)
5501 goto theend;
5502
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005503 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005504 (long)spin->si_spellbuf->b_ml.ml_line_count);
5505
5506 /*
5507 * Compress the soundfold trie.
5508 */
5509 spell_message(spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02005510 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005511
5512 /*
5513 * Write the .sug file.
5514 * Make the file name by changing ".spl" to ".sug".
5515 */
5516 fname = alloc(MAXPATHL);
5517 if (fname == NULL)
5518 goto theend;
5519 vim_strncpy(fname, wfname, MAXPATHL - 1);
5520 len = (int)STRLEN(fname);
5521 fname[len - 2] = 'u';
5522 fname[len - 1] = 'g';
5523 sug_write(spin, fname);
5524
5525theend:
5526 vim_free(fname);
5527 if (free_slang)
5528 slang_free(slang);
5529 free_blocks(spin->si_blocks);
5530 close_spellbuf(spin->si_spellbuf);
5531}
5532
5533/*
5534 * Build the soundfold trie for language "slang".
5535 */
5536 static int
5537sug_filltree(spellinfo_T *spin, slang_T *slang)
5538{
5539 char_u *byts;
5540 idx_T *idxs;
5541 int depth;
5542 idx_T arridx[MAXWLEN];
5543 int curi[MAXWLEN];
5544 char_u tword[MAXWLEN];
5545 char_u tsalword[MAXWLEN];
5546 int c;
5547 idx_T n;
5548 unsigned words_done = 0;
5549 int wordcount[MAXWLEN];
5550
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005551 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005552 spin->si_foldroot = wordtree_alloc(spin);
5553 if (spin->si_foldroot == NULL)
5554 return FAIL;
5555
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005556 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005557 spin->si_sugtree = TRUE;
5558
5559 /*
5560 * Go through the whole case-folded tree, soundfold each word and put it
5561 * in the trie.
5562 */
5563 byts = slang->sl_fbyts;
5564 idxs = slang->sl_fidxs;
5565
5566 arridx[0] = 0;
5567 curi[0] = 1;
5568 wordcount[0] = 0;
5569
5570 depth = 0;
5571 while (depth >= 0 && !got_int)
5572 {
5573 if (curi[depth] > byts[arridx[depth]])
5574 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005575 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005576 idxs[arridx[depth]] = wordcount[depth];
5577 if (depth > 0)
5578 wordcount[depth - 1] += wordcount[depth];
5579
5580 --depth;
5581 line_breakcheck();
5582 }
5583 else
5584 {
5585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005586 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005587 n = arridx[depth] + curi[depth];
5588 ++curi[depth];
5589
5590 c = byts[n];
5591 if (c == 0)
5592 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005593 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005594 tword[depth] = NUL;
5595 spell_soundfold(slang, tword, TRUE, tsalword);
5596
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005597 // We use the "flags" field for the MSB of the wordnr,
5598 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005599 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5600 words_done >> 16, words_done & 0xffff,
5601 0) == FAIL)
5602 return FAIL;
5603
5604 ++words_done;
5605 ++wordcount[depth];
5606
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005607 // Reset the block count each time to avoid compression
5608 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005609 spin->si_blocks_cnt = 0;
5610
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005611 // Skip over any other NUL bytes (same word with different
5612 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005613 while (byts[n + 1] == 0)
5614 {
5615 ++n;
5616 ++curi[depth];
5617 }
5618 }
5619 else
5620 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005621 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005622 tword[depth++] = c;
5623 arridx[depth] = idxs[n];
5624 curi[depth] = 1;
5625 wordcount[depth] = 0;
5626 }
5627 }
5628 }
5629
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005630 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005631
5632 return OK;
5633}
5634
5635/*
5636 * Make the table that links each word in the soundfold trie to the words it
5637 * can be produced from.
5638 * This is not unlike lines in a file, thus use a memfile to be able to access
5639 * the table efficiently.
5640 * Returns FAIL when out of memory.
5641 */
5642 static int
5643sug_maketable(spellinfo_T *spin)
5644{
5645 garray_T ga;
5646 int res = OK;
5647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005648 // Allocate a buffer, open a memline for it and create the swap file
5649 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005650 spin->si_spellbuf = open_spellbuf();
5651 if (spin->si_spellbuf == NULL)
5652 return FAIL;
5653
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005654 // Use a buffer to store the line info, avoids allocating many small
5655 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005656 ga_init2(&ga, 1, 100);
5657
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005658 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005659 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5660 res = FAIL;
5661
5662 ga_clear(&ga);
5663 return res;
5664}
5665
5666/*
5667 * Fill the table for one node and its children.
5668 * Returns the wordnr at the start of the node.
5669 * Returns -1 when out of memory.
5670 */
5671 static int
5672sug_filltable(
5673 spellinfo_T *spin,
5674 wordnode_T *node,
5675 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005676 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005677{
5678 wordnode_T *p, *np;
5679 int wordnr = startwordnr;
5680 int nr;
5681 int prev_nr;
5682
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005683 FOR_ALL_NODE_SIBLINGS(node, p)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005684 {
5685 if (p->wn_byte == NUL)
5686 {
5687 gap->ga_len = 0;
5688 prev_nr = 0;
5689 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5690 {
5691 if (ga_grow(gap, 10) == FAIL)
5692 return -1;
5693
5694 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005695 // Compute the offset from the previous nr and store the
5696 // offset in a way that it takes a minimum number of bytes.
5697 // It's a bit like utf-8, but without the need to mark
5698 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005699 nr -= prev_nr;
5700 prev_nr += nr;
5701 gap->ga_len += offset2bytes(nr,
5702 (char_u *)gap->ga_data + gap->ga_len);
5703 }
5704
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005705 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005706 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5707
5708 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5709 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5710 return -1;
5711 ++wordnr;
5712
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005713 // Remove extra NUL entries, we no longer need them. We don't
5714 // bother freeing the nodes, the won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005715 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5716 p->wn_sibling = p->wn_sibling->wn_sibling;
5717
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005718 // Clear the flags on the remaining NUL node, so that compression
5719 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005720 p->wn_flags = 0;
5721 p->wn_region = 0;
5722 }
5723 else
5724 {
5725 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5726 if (wordnr == -1)
5727 return -1;
5728 }
5729 }
5730 return wordnr;
5731}
5732
5733/*
5734 * Convert an offset into a minimal number of bytes.
5735 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5736 * bytes.
5737 */
5738 static int
5739offset2bytes(int nr, char_u *buf)
5740{
5741 int rem;
5742 int b1, b2, b3, b4;
5743
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005744 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005745 b1 = nr % 255 + 1;
5746 rem = nr / 255;
5747 b2 = rem % 255 + 1;
5748 rem = rem / 255;
5749 b3 = rem % 255 + 1;
5750 b4 = rem / 255 + 1;
5751
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005752 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005753 {
5754 buf[0] = 0xe0 + b4;
5755 buf[1] = b3;
5756 buf[2] = b2;
5757 buf[3] = b1;
5758 return 4;
5759 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005760 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005761 {
5762 buf[0] = 0xc0 + b3;
5763 buf[1] = b2;
5764 buf[2] = b1;
5765 return 3;
5766 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005767 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005768 {
5769 buf[0] = 0x80 + b2;
5770 buf[1] = b1;
5771 return 2;
5772 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005773 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005774 buf[0] = b1;
5775 return 1;
5776}
5777
5778/*
5779 * Write the .sug file in "fname".
5780 */
5781 static void
5782sug_write(spellinfo_T *spin, char_u *fname)
5783{
5784 FILE *fd;
5785 wordnode_T *tree;
5786 int nodecount;
5787 int wcount;
5788 char_u *line;
5789 linenr_T lnum;
5790 int len;
5791
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005792 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005793 fd = mch_fopen((char *)fname, "w");
5794 if (fd == NULL)
5795 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005796 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005797 return;
5798 }
5799
5800 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005801 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005802 spell_message(spin, IObuff);
5803
5804 /*
5805 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5806 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005807 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005809 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005810 goto theend;
5811 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005812 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005814 // Write si_sugtime to the file.
5815 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005816
5817 /*
5818 * <SUGWORDTREE>
5819 */
5820 spin->si_memtot = 0;
5821 tree = spin->si_foldroot->wn_sibling;
5822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005823 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005824 clear_node(tree);
5825
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005826 // Count the number of nodes. Needed to be able to allocate the
5827 // memory when reading the nodes. Also fills in index for shared
5828 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005829 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5830
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005831 // number of nodes in 4 bytes
5832 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005833 spin->si_memtot += nodecount + nodecount * sizeof(int);
5834
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005835 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005836 (void)put_node(fd, tree, 0, 0, FALSE);
5837
5838 /*
5839 * <SUGTABLE>: <sugwcount> <sugline> ...
5840 */
5841 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005842 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005843
5844 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5845 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005846 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005847 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5848 len = (int)STRLEN(line) + 1;
5849 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005851 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005852 goto theend;
5853 }
5854 spin->si_memtot += len;
5855 }
5856
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005857 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005858 if (putc(0, fd) == EOF)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005859 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005860
5861 vim_snprintf((char *)IObuff, IOSIZE,
5862 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5863 spell_message(spin, IObuff);
5864
5865theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005866 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005867 fclose(fd);
5868}
5869
5870
5871/*
5872 * Create a Vim spell file from one or more word lists.
5873 * "fnames[0]" is the output file name.
5874 * "fnames[fcount - 1]" is the last input file name.
5875 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5876 * and ".spl" is appended to make the output file name.
5877 */
5878 void
5879mkspell(
5880 int fcount,
5881 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005882 int ascii, // -ascii argument given
5883 int over_write, // overwrite existing output file
5884 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005885{
5886 char_u *fname = NULL;
5887 char_u *wfname;
5888 char_u **innames;
5889 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005890 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005891 int i;
5892 int len;
5893 stat_T st;
5894 int error = FALSE;
5895 spellinfo_T spin;
5896
Bram Moolenaara80faa82020-04-12 19:37:17 +02005897 CLEAR_FIELD(spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005898 spin.si_verbose = !added_word;
5899 spin.si_ascii = ascii;
5900 spin.si_followup = TRUE;
5901 spin.si_rem_accents = TRUE;
5902 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5903 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
5904 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5905 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
5906 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
5907 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
5908 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005909 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005910
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005911 // default: fnames[0] is output file, following are input files
Bram Moolenaar927b7dd2020-06-29 22:24:56 +02005912 // When "fcount" is 1 there is only one file.
5913 innames = &fnames[fcount == 1 ? 0 : 1];
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005914 incount = fcount - 1;
5915
5916 wfname = alloc(MAXPATHL);
5917 if (wfname == NULL)
5918 return;
5919
5920 if (fcount >= 1)
5921 {
5922 len = (int)STRLEN(fnames[0]);
5923 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5924 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005925 // For ":mkspell path/en.latin1.add" output file is
5926 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005927 incount = 1;
5928 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5929 }
5930 else if (fcount == 1)
5931 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005932 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005933 incount = 1;
5934 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5935 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5936 }
5937 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5938 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005939 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005940 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5941 }
5942 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005943 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005944 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5945 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005947 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005948 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5949 spin.si_ascii = TRUE;
5950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005951 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005952 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5953 spin.si_add = TRUE;
5954 }
5955
5956 if (incount <= 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005957 emsg(_(e_invarg)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005958 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005959 emsg(_("E751: Output file name must not have region name"));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005960 else if (incount > MAXREGIONS)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005961 semsg(_("E754: Only up to %d regions supported"), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005962 else
5963 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005964 // Check for overwriting before doing things that may take a lot of
5965 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005966 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
5967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005968 emsg(_(e_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005969 goto theend;
5970 }
5971 if (mch_isdir(wfname))
5972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005973 semsg(_(e_isadir2), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005974 goto theend;
5975 }
5976
5977 fname = alloc(MAXPATHL);
5978 if (fname == NULL)
5979 goto theend;
5980
5981 /*
5982 * Init the aff and dic pointers.
5983 * Get the region names if there are more than 2 arguments.
5984 */
5985 for (i = 0; i < incount; ++i)
5986 {
5987 afile[i] = NULL;
5988
5989 if (incount > 1)
5990 {
5991 len = (int)STRLEN(innames[i]);
5992 if (STRLEN(gettail(innames[i])) < 5
5993 || innames[i][len - 3] != '_')
5994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005995 semsg(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005996 goto theend;
5997 }
5998 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5999 spin.si_region_name[i * 2 + 1] =
6000 TOLOWER_ASC(innames[i][len - 1]);
6001 }
6002 }
6003 spin.si_region_count = incount;
6004
6005 spin.si_foldroot = wordtree_alloc(&spin);
6006 spin.si_keeproot = wordtree_alloc(&spin);
6007 spin.si_prefroot = wordtree_alloc(&spin);
6008 if (spin.si_foldroot == NULL
6009 || spin.si_keeproot == NULL
6010 || spin.si_prefroot == NULL)
6011 {
6012 free_blocks(spin.si_blocks);
6013 goto theend;
6014 }
6015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006016 // When not producing a .add.spl file clear the character table when
6017 // we encounter one in the .aff file. This means we dump the current
6018 // one in the .spl file if the .aff file doesn't define one. That's
6019 // better than guessing the contents, the table will match a
6020 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006021 if (!spin.si_add)
6022 spin.si_clear_chartab = TRUE;
6023
6024 /*
6025 * Read all the .aff and .dic files.
6026 * Text is converted to 'encoding'.
6027 * Words are stored in the case-folded and keep-case trees.
6028 */
6029 for (i = 0; i < incount && !error; ++i)
6030 {
6031 spin.si_conv.vc_type = CONV_NONE;
6032 spin.si_region = 1 << i;
6033
6034 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6035 if (mch_stat((char *)fname, &st) >= 0)
6036 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006037 // Read the .aff file. Will init "spin->si_conv" based on the
6038 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006039 afile[i] = spell_read_aff(&spin, fname);
6040 if (afile[i] == NULL)
6041 error = TRUE;
6042 else
6043 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006044 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006045 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6046 innames[i]);
6047 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6048 error = TRUE;
6049 }
6050 }
6051 else
6052 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006053 // No .aff file, try reading the file as a word list. Store
6054 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006055 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6056 error = TRUE;
6057 }
6058
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006059 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006060 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006061 }
6062
6063 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006064 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006065
6066 if (!error && !got_int)
6067 {
6068 /*
6069 * Combine tails in the tree.
6070 */
6071 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02006072 wordtree_compress(&spin, spin.si_foldroot, "case-folded");
6073 wordtree_compress(&spin, spin.si_keeproot, "keep-case");
6074 wordtree_compress(&spin, spin.si_prefroot, "prefixes");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006075 }
6076
6077 if (!error && !got_int)
6078 {
6079 /*
6080 * Write the info in the spell file.
6081 */
6082 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006083 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006084 spell_message(&spin, IObuff);
6085
6086 error = write_vim_spell(&spin, wfname) == FAIL;
6087
6088 spell_message(&spin, (char_u *)_("Done!"));
6089 vim_snprintf((char *)IObuff, IOSIZE,
6090 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6091 spell_message(&spin, IObuff);
6092
6093 /*
6094 * If the file is loaded need to reload it.
6095 */
6096 if (!error)
6097 spell_reload_one(wfname, added_word);
6098 }
6099
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006100 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006101 ga_clear(&spin.si_rep);
6102 ga_clear(&spin.si_repsal);
6103 ga_clear(&spin.si_sal);
6104 ga_clear(&spin.si_map);
6105 ga_clear(&spin.si_comppat);
6106 ga_clear(&spin.si_prefcond);
6107 hash_clear_all(&spin.si_commonwords, 0);
6108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006109 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006110 for (i = 0; i < incount; ++i)
6111 if (afile[i] != NULL)
6112 spell_free_aff(afile[i]);
6113
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006114 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006115 free_blocks(spin.si_blocks);
6116
6117 /*
6118 * If there is soundfolding info and no NOSUGFILE item create the
6119 * .sug file with the soundfolded word trie.
6120 */
6121 if (spin.si_sugtime != 0 && !error && !got_int)
6122 spell_make_sugfile(&spin, wfname);
6123
6124 }
6125
6126theend:
6127 vim_free(fname);
6128 vim_free(wfname);
6129}
6130
6131/*
6132 * Display a message for spell file processing when 'verbose' is set or using
6133 * ":mkspell". "str" can be IObuff.
6134 */
6135 static void
6136spell_message(spellinfo_T *spin, char_u *str)
6137{
6138 if (spin->si_verbose || p_verbose > 2)
6139 {
6140 if (!spin->si_verbose)
6141 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006142 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006143 out_flush();
6144 if (!spin->si_verbose)
6145 verbose_leave();
6146 }
6147}
6148
6149/*
6150 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006151 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006152 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006153 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006154 */
6155 void
6156ex_spell(exarg_T *eap)
6157{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006158 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6159 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6160 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006161 eap->forceit ? 0 : (int)eap->line2,
6162 eap->cmdidx == CMD_spellundo);
6163}
6164
6165/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006166 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006167 */
6168 void
6169spell_add_word(
6170 char_u *word,
6171 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006172 int what, // SPELL_ADD_ values
6173 int idx, // "zG" and "zW": zero, otherwise index in
6174 // 'spellfile'
6175 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006176{
6177 FILE *fd = NULL;
6178 buf_T *buf = NULL;
6179 int new_spf = FALSE;
6180 char_u *fname;
6181 char_u *fnamebuf = NULL;
6182 char_u line[MAXWLEN * 2];
6183 long fpos, fpos_next = 0;
6184 int i;
6185 char_u *spf;
6186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006187 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006188 {
6189 if (int_wordlist == NULL)
6190 {
6191 int_wordlist = vim_tempname('s', FALSE);
6192 if (int_wordlist == NULL)
6193 return;
6194 }
6195 fname = int_wordlist;
6196 }
6197 else
6198 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006199 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006200 if (*curwin->w_s->b_p_spf == NUL)
6201 {
6202 init_spellfile();
6203 new_spf = TRUE;
6204 }
6205
6206 if (*curwin->w_s->b_p_spf == NUL)
6207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006208 semsg(_(e_notset), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006209 return;
6210 }
6211 fnamebuf = alloc(MAXPATHL);
6212 if (fnamebuf == NULL)
6213 return;
6214
6215 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6216 {
6217 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6218 if (i == idx)
6219 break;
6220 if (*spf == NUL)
6221 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006222 semsg(_("E765: 'spellfile' does not have %d entries"), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006223 vim_free(fnamebuf);
6224 return;
6225 }
6226 }
6227
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006228 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006229 buf = buflist_findname_exp(fnamebuf);
6230 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6231 buf = NULL;
6232 if (buf != NULL && bufIsChanged(buf))
6233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006234 emsg(_(e_bufloaded));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006235 vim_free(fnamebuf);
6236 return;
6237 }
6238
6239 fname = fnamebuf;
6240 }
6241
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006242 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006243 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006244 // When the word appears as good word we need to remove that one,
6245 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006246 fd = mch_fopen((char *)fname, "r");
6247 if (fd != NULL)
6248 {
6249 while (!vim_fgets(line, MAXWLEN * 2, fd))
6250 {
6251 fpos = fpos_next;
6252 fpos_next = ftell(fd);
6253 if (STRNCMP(word, line, len) == 0
6254 && (line[len] == '/' || line[len] < ' '))
6255 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006256 // Found duplicate word. Remove it by writing a '#' at
6257 // the start of the line. Mixing reading and writing
6258 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006259 fclose(fd);
6260 fd = mch_fopen((char *)fname, "r+");
6261 if (fd == NULL)
6262 break;
6263 if (fseek(fd, fpos, SEEK_SET) == 0)
6264 {
6265 fputc('#', fd);
6266 if (undo)
6267 {
6268 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006269 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006270 len, word, NameBuff);
6271 }
6272 }
6273 fseek(fd, fpos_next, SEEK_SET);
6274 }
6275 }
6276 if (fd != NULL)
6277 fclose(fd);
6278 }
6279 }
6280
6281 if (!undo)
6282 {
6283 fd = mch_fopen((char *)fname, "a");
6284 if (fd == NULL && new_spf)
6285 {
6286 char_u *p;
6287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006288 // We just initialized the 'spellfile' option and can't open the
6289 // file. We may need to create the "spell" directory first. We
6290 // already checked the runtime directory is writable in
6291 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006292 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6293 {
6294 int c = *p;
6295
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006296 // The directory doesn't exist. Try creating it and opening
6297 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006298 *p = NUL;
6299 vim_mkdir(fname, 0755);
6300 *p = c;
6301 fd = mch_fopen((char *)fname, "a");
6302 }
6303 }
6304
6305 if (fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006306 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006307 else
6308 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006309 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006310 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006311 else if (what == SPELL_ADD_RARE)
6312 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006313 else
6314 fprintf(fd, "%.*s\n", len, word);
6315 fclose(fd);
6316
6317 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006318 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006319 }
6320 }
6321
6322 if (fd != NULL)
6323 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006324 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006325 mkspell(1, &fname, FALSE, TRUE, TRUE);
6326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006327 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006328 if (buf != NULL)
6329 buf_reload(buf, buf->b_orig_mode);
6330
6331 redraw_all_later(SOME_VALID);
6332 }
6333 vim_free(fnamebuf);
6334}
6335
6336/*
6337 * Initialize 'spellfile' for the current buffer.
6338 */
6339 static void
6340init_spellfile(void)
6341{
6342 char_u *buf;
6343 int l;
6344 char_u *fname;
6345 char_u *rtp;
6346 char_u *lend;
6347 int aspath = FALSE;
6348 char_u *lstart = curbuf->b_s.b_p_spl;
6349
6350 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6351 {
6352 buf = alloc(MAXPATHL);
6353 if (buf == NULL)
6354 return;
6355
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006356 // Find the end of the language name. Exclude the region. If there
6357 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006358 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6359 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6360 if (vim_ispathsep(*lend))
6361 {
6362 aspath = TRUE;
6363 lstart = lend + 1;
6364 }
6365
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006366 // Loop over all entries in 'runtimepath'. Use the first one where we
6367 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006368 rtp = p_rtp;
6369 while (*rtp != NUL)
6370 {
6371 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006372 // Use directory of an entry with path, e.g., for
6373 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006374 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6375 lstart - curbuf->b_s.b_p_spl - 1);
6376 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006377 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006378 copy_option_part(&rtp, buf, MAXPATHL, ",");
6379 if (filewritable(buf) == 2)
6380 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006381 // Use the first language name from 'spelllang' and the
6382 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006383 if (aspath)
6384 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6385 lend - curbuf->b_s.b_p_spl);
6386 else
6387 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006388 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006389 l = (int)STRLEN(buf);
6390 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6391 if (filewritable(buf) != 2)
6392 vim_mkdir(buf, 0755);
6393
6394 l = (int)STRLEN(buf);
6395 vim_snprintf((char *)buf + l, MAXPATHL - l,
6396 "/%.*s", (int)(lend - lstart), lstart);
6397 }
6398 l = (int)STRLEN(buf);
6399 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6400 ->lp_slang->sl_fname;
6401 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6402 fname != NULL
6403 && strstr((char *)gettail(fname), ".ascii.") != NULL
6404 ? (char_u *)"ascii" : spell_enc());
6405 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6406 break;
6407 }
6408 aspath = FALSE;
6409 }
6410
6411 vim_free(buf);
6412 }
6413}
6414
6415
6416
6417/*
6418 * Set the spell character tables from strings in the affix file.
6419 */
6420 static int
6421set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6422{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006423 // We build the new tables here first, so that we can compare with the
6424 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006425 spelltab_T new_st;
6426 char_u *pf = fol, *pl = low, *pu = upp;
6427 int f, l, u;
6428
6429 clear_spell_chartab(&new_st);
6430
6431 while (*pf != NUL)
6432 {
6433 if (*pl == NUL || *pu == NUL)
6434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006435 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006436 return FAIL;
6437 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006438 f = mb_ptr2char_adv(&pf);
6439 l = mb_ptr2char_adv(&pl);
6440 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006442 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006443 if (f < 256)
6444 new_st.st_isw[f] = TRUE;
6445 if (l < 256)
6446 new_st.st_isw[l] = TRUE;
6447 if (u < 256)
6448 new_st.st_isw[u] = TRUE;
6449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006450 // if "LOW" and "FOL" are not the same the "LOW" char needs
6451 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006452 if (l < 256 && l != f)
6453 {
6454 if (f >= 256)
6455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006456 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006457 return FAIL;
6458 }
6459 new_st.st_fold[l] = f;
6460 }
6461
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006462 // if "UPP" and "FOL" are not the same the "UPP" char needs
6463 // case-folding, it's upper case and the "UPP" is the upper case of
6464 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006465 if (u < 256 && u != f)
6466 {
6467 if (f >= 256)
6468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006469 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006470 return FAIL;
6471 }
6472 new_st.st_fold[u] = f;
6473 new_st.st_isu[u] = TRUE;
6474 new_st.st_upper[f] = u;
6475 }
6476 }
6477
6478 if (*pl != NUL || *pu != NUL)
6479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006480 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006481 return FAIL;
6482 }
6483
6484 return set_spell_finish(&new_st);
6485}
6486
6487/*
6488 * Set the spell character tables from strings in the .spl file.
6489 */
6490 static void
6491set_spell_charflags(
6492 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006493 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006494 char_u *fol)
6495{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006496 // We build the new tables here first, so that we can compare with the
6497 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006498 spelltab_T new_st;
6499 int i;
6500 char_u *p = fol;
6501 int c;
6502
6503 clear_spell_chartab(&new_st);
6504
6505 for (i = 0; i < 128; ++i)
6506 {
6507 if (i < cnt)
6508 {
6509 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6510 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6511 }
6512
6513 if (*p != NUL)
6514 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006515 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006516 new_st.st_fold[i + 128] = c;
6517 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6518 new_st.st_upper[c] = i + 128;
6519 }
6520 }
6521
6522 (void)set_spell_finish(&new_st);
6523}
6524
6525 static int
6526set_spell_finish(spelltab_T *new_st)
6527{
6528 int i;
6529
6530 if (did_set_spelltab)
6531 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006532 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006533 for (i = 0; i < 256; ++i)
6534 {
6535 if (spelltab.st_isw[i] != new_st->st_isw[i]
6536 || spelltab.st_isu[i] != new_st->st_isu[i]
6537 || spelltab.st_fold[i] != new_st->st_fold[i]
6538 || spelltab.st_upper[i] != new_st->st_upper[i])
6539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006540 emsg(_("E763: Word characters differ between spell files"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006541 return FAIL;
6542 }
6543 }
6544 }
6545 else
6546 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006547 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006548 spelltab = *new_st;
6549 did_set_spelltab = TRUE;
6550 }
6551
6552 return OK;
6553}
6554
6555/*
6556 * Write the table with prefix conditions to the .spl file.
6557 * When "fd" is NULL only count the length of what is written.
6558 */
6559 static int
6560write_spell_prefcond(FILE *fd, garray_T *gap)
6561{
6562 int i;
6563 char_u *p;
6564 int len;
6565 int totlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006566 size_t x = 1; // collect return value of fwrite()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006567
6568 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006569 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006571 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006572
6573 for (i = 0; i < gap->ga_len; ++i)
6574 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006575 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006576 p = ((char_u **)gap->ga_data)[i];
6577 if (p != NULL)
6578 {
6579 len = (int)STRLEN(p);
6580 if (fd != NULL)
6581 {
6582 fputc(len, fd);
6583 x &= fwrite(p, (size_t)len, (size_t)1, fd);
6584 }
6585 totlen += len;
6586 }
6587 else if (fd != NULL)
6588 fputc(0, fd);
6589 }
6590
6591 return totlen;
6592}
6593
6594
6595/*
6596 * Use map string "map" for languages "lp".
6597 */
6598 static void
6599set_map_str(slang_T *lp, char_u *map)
6600{
6601 char_u *p;
6602 int headc = 0;
6603 int c;
6604 int i;
6605
6606 if (*map == NUL)
6607 {
6608 lp->sl_has_map = FALSE;
6609 return;
6610 }
6611 lp->sl_has_map = TRUE;
6612
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006613 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006614 for (i = 0; i < 256; ++i)
6615 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006616 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006617
6618 /*
6619 * The similar characters are stored separated with slashes:
6620 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6621 * before the same slash. For characters above 255 sl_map_hash is used.
6622 */
6623 for (p = map; *p != NUL; )
6624 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006625 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006626 if (c == '/')
6627 headc = 0;
6628 else
6629 {
6630 if (headc == 0)
6631 headc = c;
6632
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006633 // Characters above 255 don't fit in sl_map_array[], put them in
6634 // the hash table. Each entry is the char, a NUL the headchar and
6635 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006636 if (c >= 256)
6637 {
6638 int cl = mb_char2len(c);
6639 int headcl = mb_char2len(headc);
6640 char_u *b;
6641 hash_T hash;
6642 hashitem_T *hi;
6643
Bram Moolenaar964b3742019-05-24 18:54:09 +02006644 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006645 if (b == NULL)
6646 return;
6647 mb_char2bytes(c, b);
6648 b[cl] = NUL;
6649 mb_char2bytes(headc, b + cl + 1);
6650 b[cl + 1 + headcl] = NUL;
6651 hash = hash_hash(b);
6652 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6653 if (HASHITEM_EMPTY(hi))
6654 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6655 else
6656 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006657 // This should have been checked when generating the .spl
6658 // file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006659 emsg(_("E783: duplicate char in MAP entry"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006660 vim_free(b);
6661 }
6662 }
6663 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006664 lp->sl_map_array[c] = headc;
6665 }
6666 }
6667}
6668
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006669#endif // FEAT_SPELL