blob: fa450f33ddeb14a07ad05942d39c5f0a1b6c1848 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spellfile.c: code for reading and writing spell files.
12 *
13 * See spell.c for information about spell checking.
14 */
15
16/*
17 * Vim spell file format: <HEADER>
18 * <SECTIONS>
19 * <LWORDTREE>
20 * <KWORDTREE>
21 * <PREFIXTREE>
22 *
23 * <HEADER>: <fileID> <versionnr>
24 *
25 * <fileID> 8 bytes "VIMspell"
26 * <versionnr> 1 byte VIMSPELLVERSION
27 *
28 *
29 * Sections make it possible to add information to the .spl file without
30 * making it incompatible with previous versions. There are two kinds of
31 * sections:
32 * 1. Not essential for correct spell checking. E.g. for making suggestions.
33 * These are skipped when not supported.
34 * 2. Optional information, but essential for spell checking when present.
35 * E.g. conditions for affixes. When this section is present but not
36 * supported an error message is given.
37 *
38 * <SECTIONS>: <section> ... <sectionend>
39 *
40 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
41 *
42 * <sectionID> 1 byte number from 0 to 254 identifying the section
43 *
44 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
45 * spell checking
46 *
47 * <sectionlen> 4 bytes length of section contents, MSB first
48 *
49 * <sectionend> 1 byte SN_END
50 *
51 *
52 * sectionID == SN_INFO: <infotext>
53 * <infotext> N bytes free format text with spell file info (version,
54 * website, etc)
55 *
56 * sectionID == SN_REGION: <regionname> ...
Bram Moolenaar2993ac52018-02-10 14:12:43 +010057 * <regionname> 2 bytes Up to MAXREGIONS region names: ca, au, etc. Lower
58 * case. First <regionname> is region 1.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020059 *
60 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
61 * <folcharslen> <folchars>
62 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
63 * <charflags> N bytes List of flags (first one is for character 128):
64 * 0x01 word character CF_WORD
65 * 0x02 upper-case character CF_UPPER
66 * <folcharslen> 2 bytes Number of bytes in <folchars>.
67 * <folchars> N bytes Folded characters, first one is for character 128.
68 *
69 * sectionID == SN_MIDWORD: <midword>
70 * <midword> N bytes Characters that are word characters only when used
71 * in the middle of a word.
72 *
73 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
74 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
75 * <prefcond> : <condlen> <condstr>
76 * <condlen> 1 byte Length of <condstr>.
77 * <condstr> N bytes Condition for the prefix.
78 *
79 * sectionID == SN_REP: <repcount> <rep> ...
80 * <repcount> 2 bytes number of <rep> items, MSB first.
81 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
82 * <repfromlen> 1 byte length of <repfrom>
83 * <repfrom> N bytes "from" part of replacement
84 * <reptolen> 1 byte length of <repto>
85 * <repto> N bytes "to" part of replacement
86 *
87 * sectionID == SN_REPSAL: <repcount> <rep> ...
88 * just like SN_REP but for soundfolded words
89 *
90 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
91 * <salflags> 1 byte flags for soundsalike conversion:
92 * SAL_F0LLOWUP
93 * SAL_COLLAPSE
94 * SAL_REM_ACCENTS
95 * <salcount> 2 bytes number of <sal> items following
96 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
97 * <salfromlen> 1 byte length of <salfrom>
98 * <salfrom> N bytes "from" part of soundsalike
99 * <saltolen> 1 byte length of <salto>
100 * <salto> N bytes "to" part of soundsalike
101 *
102 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
103 * <sofofromlen> 2 bytes length of <sofofrom>
104 * <sofofrom> N bytes "from" part of soundfold
105 * <sofotolen> 2 bytes length of <sofoto>
106 * <sofoto> N bytes "to" part of soundfold
107 *
108 * sectionID == SN_SUGFILE: <timestamp>
109 * <timestamp> 8 bytes time in seconds that must match with .sug file
110 *
111 * sectionID == SN_NOSPLITSUGS: nothing
112 *
113 * sectionID == SN_NOCOMPOUNDSUGS: nothing
114 *
115 * sectionID == SN_WORDS: <word> ...
116 * <word> N bytes NUL terminated common word
117 *
118 * sectionID == SN_MAP: <mapstr>
119 * <mapstr> N bytes String with sequences of similar characters,
120 * separated by slashes.
121 *
122 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
123 * <comppatcount> <comppattern> ... <compflags>
124 * <compmax> 1 byte Maximum nr of words in compound word.
125 * <compminlen> 1 byte Minimal word length for compounding.
126 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
127 * <compoptions> 2 bytes COMP_ flags.
128 * <comppatcount> 2 bytes number of <comppattern> following
129 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
130 * slashes.
131 *
132 * <comppattern>: <comppatlen> <comppattext>
133 * <comppatlen> 1 byte length of <comppattext>
134 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
135 *
136 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
137 *
138 * sectionID == SN_SYLLABLE: <syllable>
139 * <syllable> N bytes String from SYLLABLE item.
140 *
141 * <LWORDTREE>: <wordtree>
142 *
143 * <KWORDTREE>: <wordtree>
144 *
145 * <PREFIXTREE>: <wordtree>
146 *
147 *
148 * <wordtree>: <nodecount> <nodedata> ...
149 *
150 * <nodecount> 4 bytes Number of nodes following. MSB first.
151 *
152 * <nodedata>: <siblingcount> <sibling> ...
153 *
154 * <siblingcount> 1 byte Number of siblings in this node. The siblings
155 * follow in sorted order.
156 *
157 * <sibling>: <byte> [ <nodeidx> <xbyte>
158 * | <flags> [<flags2>] [<region>] [<affixID>]
159 * | [<pflags>] <affixID> <prefcondnr> ]
160 *
161 * <byte> 1 byte Byte value of the sibling. Special cases:
162 * BY_NOFLAGS: End of word without flags and for all
163 * regions.
164 * For PREFIXTREE <affixID> and
165 * <prefcondnr> follow.
166 * BY_FLAGS: End of word, <flags> follow.
167 * For PREFIXTREE <pflags>, <affixID>
168 * and <prefcondnr> follow.
169 * BY_FLAGS2: End of word, <flags> and <flags2>
170 * follow. Not used in PREFIXTREE.
171 * BY_INDEX: Child of sibling is shared, <nodeidx>
172 * and <xbyte> follow.
173 *
174 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
175 *
176 * <xbyte> 1 byte byte value of the sibling.
177 *
178 * <flags> 1 byte bitmask of:
179 * WF_ALLCAP word must have only capitals
180 * WF_ONECAP first char of word must be capital
181 * WF_KEEPCAP keep-case word
182 * WF_FIXCAP keep-case word, all caps not allowed
183 * WF_RARE rare word
184 * WF_BANNED bad word
185 * WF_REGION <region> follows
186 * WF_AFX <affixID> follows
187 *
188 * <flags2> 1 byte Bitmask of:
189 * WF_HAS_AFF >> 8 word includes affix
190 * WF_NEEDCOMP >> 8 word only valid in compound
191 * WF_NOSUGGEST >> 8 word not used for suggestions
192 * WF_COMPROOT >> 8 word already a compound
193 * WF_NOCOMPBEF >> 8 no compounding before this word
194 * WF_NOCOMPAFT >> 8 no compounding after this word
195 *
196 * <pflags> 1 byte bitmask of:
197 * WFP_RARE rare prefix
198 * WFP_NC non-combining prefix
199 * WFP_UP letter after prefix made upper case
200 *
201 * <region> 1 byte Bitmask for regions in which word is valid. When
202 * omitted it's valid in all regions.
203 * Lowest bit is for region 1.
204 *
205 * <affixID> 1 byte ID of affix that can be used with this word. In
206 * PREFIXTREE used for the required prefix ID.
207 *
208 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
209 * from HEADER.
210 *
211 * All text characters are in 'encoding', but stored as single bytes.
212 */
213
214/*
215 * Vim .sug file format: <SUGHEADER>
216 * <SUGWORDTREE>
217 * <SUGTABLE>
218 *
219 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
220 *
221 * <fileID> 6 bytes "VIMsug"
222 * <versionnr> 1 byte VIMSUGVERSION
223 * <timestamp> 8 bytes timestamp that must match with .spl file
224 *
225 *
226 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
227 *
228 *
229 * <SUGTABLE>: <sugwcount> <sugline> ...
230 *
231 * <sugwcount> 4 bytes number of <sugline> following
232 *
233 * <sugline>: <sugnr> ... NUL
234 *
235 * <sugnr>: X bytes word number that results in this soundfolded word,
236 * stored as an offset to the previous number in as
237 * few bytes as possible, see offset2bytes())
238 */
239
240#include "vim.h"
241
242#if defined(FEAT_SPELL) || defined(PROTO)
243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100244#ifndef UNIX // it's in os_unix.h for Unix
245# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200246#endif
247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100248#ifndef UNIX // it's in os_unix.h for Unix
249# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200250#endif
251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100252// Special byte values for <byte>. Some are only used in the tree for
253// postponed prefixes, some only in the other trees. This is a bit messy...
254#define BY_NOFLAGS 0 // end of word without flags or region; for
255 // postponed prefix: no <pflags>
256#define BY_INDEX 1 // child is shared, index follows
257#define BY_FLAGS 2 // end of word, <flags> byte follows; for
258 // postponed prefix: <pflags> follows
259#define BY_FLAGS2 3 // end of word, <flags> and <flags2> bytes
260 // follow; never used in prefix tree
261#define BY_SPECIAL BY_FLAGS2 // highest special byte value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200262
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +0100263#define ZERO_FLAG 65009 // used when flag is zero: "0"
264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100265// Flags used in .spl file for soundsalike flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200266#define SAL_F0LLOWUP 1
267#define SAL_COLLAPSE 2
268#define SAL_REM_ACCENTS 4
269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100270#define VIMSPELLMAGIC "VIMspell" // string at start of Vim spell file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200271#define VIMSPELLMAGICL 8
272#define VIMSPELLVERSION 50
273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274// Section IDs. Only renumber them when VIMSPELLVERSION changes!
275#define SN_REGION 0 // <regionname> section
276#define SN_CHARFLAGS 1 // charflags section
277#define SN_MIDWORD 2 // <midword> section
278#define SN_PREFCOND 3 // <prefcond> section
279#define SN_REP 4 // REP items section
280#define SN_SAL 5 // SAL items section
281#define SN_SOFO 6 // soundfolding section
282#define SN_MAP 7 // MAP items section
283#define SN_COMPOUND 8 // compound words section
284#define SN_SYLLABLE 9 // syllable section
285#define SN_NOBREAK 10 // NOBREAK section
286#define SN_SUGFILE 11 // timestamp for .sug file
287#define SN_REPSAL 12 // REPSAL items section
288#define SN_WORDS 13 // common words
289#define SN_NOSPLITSUGS 14 // don't split word for suggestions
290#define SN_INFO 15 // info section
291#define SN_NOCOMPOUNDSUGS 16 // don't compound for suggestions
292#define SN_END 255 // end of sections
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100294#define SNF_REQUIRED 1 // <sectionflags>: required section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200295
296#define CF_WORD 0x01
297#define CF_UPPER 0x02
298
299static int set_spell_finish(spelltab_T *new_st);
300static int write_spell_prefcond(FILE *fd, garray_T *gap);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200301static int read_region_section(FILE *fd, slang_T *slang, int len);
302static int read_charflags_section(FILE *fd);
303static int read_prefcond_section(FILE *fd, slang_T *lp);
304static int read_rep_section(FILE *fd, garray_T *gap, short *first);
305static int read_sal_section(FILE *fd, slang_T *slang);
306static int read_words_section(FILE *fd, slang_T *lp, int len);
307static int read_sofo_section(FILE *fd, slang_T *slang);
308static int read_compound(FILE *fd, slang_T *slang, int len);
309static int set_sofo(slang_T *lp, char_u *from, char_u *to);
310static void set_sal_first(slang_T *lp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200311static int *mb_str2wide(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200312static int spell_read_tree(FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt);
313static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200314static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
315static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
316static void set_map_str(slang_T *lp, char_u *map);
317
318
319static char *e_spell_trunc = N_("E758: Truncated spell file");
320static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
321static char *e_affname = N_("Affix name too long in %s line %d: %s");
322static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
323static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
324static char *msg_compressing = N_("Compressing word tree...");
325
326/*
327 * Load one spell file and store the info into a slang_T.
328 *
329 * This is invoked in three ways:
330 * - From spell_load_cb() to load a spell file for the first time. "lang" is
331 * the language name, "old_lp" is NULL. Will allocate an slang_T.
332 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
333 * points to the existing slang_T.
334 * - Just after writing a .spl file; it's read back to produce the .sug file.
335 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
336 *
337 * Returns the slang_T the spell file was loaded into. NULL for error.
338 */
339 slang_T *
340spell_load_file(
341 char_u *fname,
342 char_u *lang,
343 slang_T *old_lp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100344 int silent) // no error if file doesn't exist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200345{
346 FILE *fd;
347 char_u buf[VIMSPELLMAGICL];
348 char_u *p;
349 int i;
350 int n;
351 int len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200352 slang_T *lp = NULL;
353 int c = 0;
354 int res;
Bram Moolenaarce6db022020-01-07 20:11:42 +0100355 int did_estack_push = FALSE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200356
357 fd = mch_fopen((char *)fname, "r");
358 if (fd == NULL)
359 {
360 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200362 else if (p_verbose > 2)
363 {
364 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100365 smsg((const char *)e_notopen, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200366 verbose_leave();
367 }
368 goto endFAIL;
369 }
370 if (p_verbose > 2)
371 {
372 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100373 smsg(_("Reading spell file \"%s\""), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200374 verbose_leave();
375 }
376
377 if (old_lp == NULL)
378 {
379 lp = slang_alloc(lang);
380 if (lp == NULL)
381 goto endFAIL;
382
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100383 // Remember the file name, used to reload the file when it's updated.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200384 lp->sl_fname = vim_strsave(fname);
385 if (lp->sl_fname == NULL)
386 goto endFAIL;
387
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100388 // Check for .add.spl (_add.spl for VMS).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200389 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
390 }
391 else
392 lp = old_lp;
393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100394 // Set sourcing_name, so that error messages mention the file name.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100395 estack_push(ETYPE_SPELL, fname, 0);
Bram Moolenaarce6db022020-01-07 20:11:42 +0100396 did_estack_push = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200397
398 /*
399 * <HEADER>: <fileID>
400 */
401 for (i = 0; i < VIMSPELLMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100402 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200403 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100405 emsg(_("E757: This does not look like a spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200406 goto endFAIL;
407 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100408 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200409 if (c < VIMSPELLVERSION)
410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100411 emsg(_("E771: Old spell file, needs to be updated"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200412 goto endFAIL;
413 }
414 else if (c > VIMSPELLVERSION)
415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100416 emsg(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200417 goto endFAIL;
418 }
419
420
421 /*
422 * <SECTIONS>: <section> ... <sectionend>
423 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
424 */
425 for (;;)
426 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100427 n = getc(fd); // <sectionID> or <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200428 if (n == SN_END)
429 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100430 c = getc(fd); // <sectionflags>
431 len = get4c(fd); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200432 if (len < 0)
433 goto truncerr;
434
435 res = 0;
436 switch (n)
437 {
438 case SN_INFO:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100439 lp->sl_info = read_string(fd, len); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200440 if (lp->sl_info == NULL)
441 goto endFAIL;
442 break;
443
444 case SN_REGION:
445 res = read_region_section(fd, lp, len);
446 break;
447
448 case SN_CHARFLAGS:
449 res = read_charflags_section(fd);
450 break;
451
452 case SN_MIDWORD:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100453 lp->sl_midword = read_string(fd, len); // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200454 if (lp->sl_midword == NULL)
455 goto endFAIL;
456 break;
457
458 case SN_PREFCOND:
459 res = read_prefcond_section(fd, lp);
460 break;
461
462 case SN_REP:
463 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
464 break;
465
466 case SN_REPSAL:
467 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
468 break;
469
470 case SN_SAL:
471 res = read_sal_section(fd, lp);
472 break;
473
474 case SN_SOFO:
475 res = read_sofo_section(fd, lp);
476 break;
477
478 case SN_MAP:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100479 p = read_string(fd, len); // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200480 if (p == NULL)
481 goto endFAIL;
482 set_map_str(lp, p);
483 vim_free(p);
484 break;
485
486 case SN_WORDS:
487 res = read_words_section(fd, lp, len);
488 break;
489
490 case SN_SUGFILE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100491 lp->sl_sugtime = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200492 break;
493
494 case SN_NOSPLITSUGS:
495 lp->sl_nosplitsugs = TRUE;
496 break;
497
498 case SN_NOCOMPOUNDSUGS:
499 lp->sl_nocompoundsugs = TRUE;
500 break;
501
502 case SN_COMPOUND:
503 res = read_compound(fd, lp, len);
504 break;
505
506 case SN_NOBREAK:
507 lp->sl_nobreak = TRUE;
508 break;
509
510 case SN_SYLLABLE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100511 lp->sl_syllable = read_string(fd, len); // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200512 if (lp->sl_syllable == NULL)
513 goto endFAIL;
514 if (init_syl_tab(lp) == FAIL)
515 goto endFAIL;
516 break;
517
518 default:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100519 // Unsupported section. When it's required give an error
520 // message. When it's not required skip the contents.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200521 if (c & SNF_REQUIRED)
522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100523 emsg(_("E770: Unsupported section in spell file"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200524 goto endFAIL;
525 }
526 while (--len >= 0)
527 if (getc(fd) < 0)
528 goto truncerr;
529 break;
530 }
531someerror:
532 if (res == SP_FORMERROR)
533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100534 emsg(_(e_format));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200535 goto endFAIL;
536 }
537 if (res == SP_TRUNCERROR)
538 {
539truncerr:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100540 emsg(_(e_spell_trunc));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200541 goto endFAIL;
542 }
543 if (res == SP_OTHERERROR)
544 goto endFAIL;
545 }
546
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100547 // <LWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200548 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
549 if (res != 0)
550 goto someerror;
551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100552 // <KWORDTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200553 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
554 if (res != 0)
555 goto someerror;
556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100557 // <PREFIXTREE>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200558 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
559 lp->sl_prefixcnt);
560 if (res != 0)
561 goto someerror;
562
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100563 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200564 if (old_lp == NULL && lang != NULL)
565 {
566 lp->sl_next = first_lang;
567 first_lang = lp;
568 }
569
570 goto endOK;
571
572endFAIL:
573 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100574 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200575 *lang = NUL;
576 if (lp != NULL && old_lp == NULL)
577 slang_free(lp);
578 lp = NULL;
579
580endOK:
581 if (fd != NULL)
582 fclose(fd);
Bram Moolenaarce6db022020-01-07 20:11:42 +0100583 if (did_estack_push)
584 estack_pop();
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200585
586 return lp;
587}
588
589/*
590 * Fill in the wordcount fields for a trie.
591 * Returns the total number of words.
592 */
593 static void
594tree_count_words(char_u *byts, idx_T *idxs)
595{
596 int depth;
597 idx_T arridx[MAXWLEN];
598 int curi[MAXWLEN];
599 int c;
600 idx_T n;
601 int wordcount[MAXWLEN];
602
603 arridx[0] = 0;
604 curi[0] = 1;
605 wordcount[0] = 0;
606 depth = 0;
607 while (depth >= 0 && !got_int)
608 {
609 if (curi[depth] > byts[arridx[depth]])
610 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100611 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200612 idxs[arridx[depth]] = wordcount[depth];
613 if (depth > 0)
614 wordcount[depth - 1] += wordcount[depth];
615
616 --depth;
617 fast_breakcheck();
618 }
619 else
620 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100621 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200622 n = arridx[depth] + curi[depth];
623 ++curi[depth];
624
625 c = byts[n];
626 if (c == 0)
627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100628 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200629 ++wordcount[depth];
630
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100631 // Skip over any other NUL bytes (same word with different
632 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200633 while (byts[n + 1] == 0)
634 {
635 ++n;
636 ++curi[depth];
637 }
638 }
639 else
640 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100641 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200642 ++depth;
643 arridx[depth] = idxs[n];
644 curi[depth] = 1;
645 wordcount[depth] = 0;
646 }
647 }
648 }
649}
650
651/*
652 * Load the .sug files for languages that have one and weren't loaded yet.
653 */
654 void
655suggest_load_files(void)
656{
657 langp_T *lp;
658 int lpi;
659 slang_T *slang;
660 char_u *dotp;
661 FILE *fd;
662 char_u buf[MAXWLEN];
663 int i;
664 time_t timestamp;
665 int wcount;
666 int wordnr;
667 garray_T ga;
668 int c;
669
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100670 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200671 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
672 {
673 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
674 slang = lp->lp_slang;
675 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
676 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100677 // Change ".spl" to ".sug" and open the file. When the file isn't
678 // found silently skip it. Do set "sl_sugloaded" so that we
679 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200680 slang->sl_sugloaded = TRUE;
681
682 dotp = vim_strrchr(slang->sl_fname, '.');
683 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
684 continue;
685 STRCPY(dotp, ".sug");
686 fd = mch_fopen((char *)slang->sl_fname, "r");
687 if (fd == NULL)
688 goto nextone;
689
690 /*
691 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
692 */
693 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100694 buf[i] = getc(fd); // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200695 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100697 semsg(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200698 slang->sl_fname);
699 goto nextone;
700 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100701 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200702 if (c < VIMSUGVERSION)
703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100704 semsg(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200705 slang->sl_fname);
706 goto nextone;
707 }
708 else if (c > VIMSUGVERSION)
709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100710 semsg(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200711 slang->sl_fname);
712 goto nextone;
713 }
714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100715 // Check the timestamp, it must be exactly the same as the one in
716 // the .spl file. Otherwise the word numbers won't match.
717 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200718 if (timestamp != slang->sl_sugtime)
719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100720 semsg(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200721 slang->sl_fname);
722 goto nextone;
723 }
724
725 /*
726 * <SUGWORDTREE>: <wordtree>
727 * Read the trie with the soundfolded words.
728 */
729 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
730 FALSE, 0) != 0)
731 {
732someerror:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100733 semsg(_("E782: error while reading .sug file: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200734 slang->sl_fname);
735 slang_clear_sug(slang);
736 goto nextone;
737 }
738
739 /*
740 * <SUGTABLE>: <sugwcount> <sugline> ...
741 *
742 * Read the table with word numbers. We use a file buffer for
743 * this, because it's so much like a file with lines. Makes it
744 * possible to swap the info and save on memory use.
745 */
746 slang->sl_sugbuf = open_spellbuf();
747 if (slang->sl_sugbuf == NULL)
748 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100749 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200750 wcount = get4c(fd);
751 if (wcount < 0)
752 goto someerror;
753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100754 // Read all the wordnr lists into the buffer, one NUL terminated
755 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200756 ga_init2(&ga, 1, 100);
757 for (wordnr = 0; wordnr < wcount; ++wordnr)
758 {
759 ga.ga_len = 0;
760 for (;;)
761 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100762 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200763 if (c < 0 || ga_grow(&ga, 1) == FAIL)
764 goto someerror;
765 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
766 if (c == NUL)
767 break;
768 }
769 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
770 ga.ga_data, ga.ga_len, TRUE) == FAIL)
771 goto someerror;
772 }
773 ga_clear(&ga);
774
775 /*
776 * Need to put word counts in the word tries, so that we can find
777 * a word by its number.
778 */
779 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
780 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
781
782nextone:
783 if (fd != NULL)
784 fclose(fd);
785 STRCPY(dotp, ".spl");
786 }
787 }
788}
789
790
791/*
792 * Read a length field from "fd" in "cnt_bytes" bytes.
793 * Allocate memory, read the string into it and add a NUL at the end.
794 * Returns NULL when the count is zero.
795 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
796 * otherwise.
797 */
798 static char_u *
799read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
800{
801 int cnt = 0;
802 int i;
803 char_u *str;
804
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100805 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200806 for (i = 0; i < cnt_bytes; ++i)
807 cnt = (cnt << 8) + getc(fd);
808 if (cnt < 0)
809 {
810 *cntp = SP_TRUNCERROR;
811 return NULL;
812 }
813 *cntp = cnt;
814 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100815 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200816
817 str = read_string(fd, cnt);
818 if (str == NULL)
819 *cntp = SP_OTHERERROR;
820 return str;
821}
822
823/*
824 * Read SN_REGION: <regionname> ...
825 * Return SP_*ERROR flags.
826 */
827 static int
828read_region_section(FILE *fd, slang_T *lp, int len)
829{
830 int i;
831
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100832 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200833 return SP_FORMERROR;
834 for (i = 0; i < len; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100835 lp->sl_regions[i] = getc(fd); // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200836 lp->sl_regions[len] = NUL;
837 return 0;
838}
839
840/*
841 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
842 * <folcharslen> <folchars>
843 * Return SP_*ERROR flags.
844 */
845 static int
846read_charflags_section(FILE *fd)
847{
848 char_u *flags;
849 char_u *fol;
850 int flagslen, follen;
851
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100852 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200853 flags = read_cnt_string(fd, 1, &flagslen);
854 if (flagslen < 0)
855 return flagslen;
856
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100857 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200858 fol = read_cnt_string(fd, 2, &follen);
859 if (follen < 0)
860 {
861 vim_free(flags);
862 return follen;
863 }
864
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100865 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200866 if (flags != NULL && fol != NULL)
867 set_spell_charflags(flags, flagslen, fol);
868
869 vim_free(flags);
870 vim_free(fol);
871
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100872 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200873 if ((flags == NULL) != (fol == NULL))
874 return SP_FORMERROR;
875 return 0;
876}
877
878/*
879 * Read SN_PREFCOND section.
880 * Return SP_*ERROR flags.
881 */
882 static int
883read_prefcond_section(FILE *fd, slang_T *lp)
884{
885 int cnt;
886 int i;
887 int n;
888 char_u *p;
889 char_u buf[MAXWLEN + 1];
890
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100891 // <prefcondcnt> <prefcond> ...
892 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200893 if (cnt <= 0)
894 return SP_FORMERROR;
895
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200896 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200897 if (lp->sl_prefprog == NULL)
898 return SP_OTHERERROR;
899 lp->sl_prefixcnt = cnt;
900
901 for (i = 0; i < cnt; ++i)
902 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100903 // <prefcond> : <condlen> <condstr>
904 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200905 if (n < 0 || n >= MAXWLEN)
906 return SP_FORMERROR;
907
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100908 // When <condlen> is zero we have an empty condition. Otherwise
909 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200910 if (n > 0)
911 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100912 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200913 p = buf + 1;
914 while (n-- > 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100915 *p++ = getc(fd); // <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200916 *p = NUL;
917 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
918 }
919 }
920 return 0;
921}
922
923/*
924 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
925 * Return SP_*ERROR flags.
926 */
927 static int
928read_rep_section(FILE *fd, garray_T *gap, short *first)
929{
930 int cnt;
931 fromto_T *ftp;
932 int i;
933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100934 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200935 if (cnt < 0)
936 return SP_TRUNCERROR;
937
938 if (ga_grow(gap, cnt) == FAIL)
939 return SP_OTHERERROR;
940
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100941 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200942 for (; gap->ga_len < cnt; ++gap->ga_len)
943 {
944 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
945 ftp->ft_from = read_cnt_string(fd, 1, &i);
946 if (i < 0)
947 return i;
948 if (i == 0)
949 return SP_FORMERROR;
950 ftp->ft_to = read_cnt_string(fd, 1, &i);
951 if (i <= 0)
952 {
953 vim_free(ftp->ft_from);
954 if (i < 0)
955 return i;
956 return SP_FORMERROR;
957 }
958 }
959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100960 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200961 for (i = 0; i < 256; ++i)
962 first[i] = -1;
963 for (i = 0; i < gap->ga_len; ++i)
964 {
965 ftp = &((fromto_T *)gap->ga_data)[i];
966 if (first[*ftp->ft_from] == -1)
967 first[*ftp->ft_from] = i;
968 }
969 return 0;
970}
971
972/*
973 * Read SN_SAL section: <salflags> <salcount> <sal> ...
974 * Return SP_*ERROR flags.
975 */
976 static int
977read_sal_section(FILE *fd, slang_T *slang)
978{
979 int i;
980 int cnt;
981 garray_T *gap;
982 salitem_T *smp;
983 int ccnt;
984 char_u *p;
985 int c = NUL;
986
987 slang->sl_sofo = FALSE;
988
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100989 i = getc(fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200990 if (i & SAL_F0LLOWUP)
991 slang->sl_followup = TRUE;
992 if (i & SAL_COLLAPSE)
993 slang->sl_collapse = TRUE;
994 if (i & SAL_REM_ACCENTS)
995 slang->sl_rem_accents = TRUE;
996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100997 cnt = get2c(fd); // <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200998 if (cnt < 0)
999 return SP_TRUNCERROR;
1000
1001 gap = &slang->sl_sal;
1002 ga_init2(gap, sizeof(salitem_T), 10);
1003 if (ga_grow(gap, cnt + 1) == FAIL)
1004 return SP_OTHERERROR;
1005
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001006 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001007 for (; gap->ga_len < cnt; ++gap->ga_len)
1008 {
1009 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001010 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001011 if (ccnt < 0)
1012 return SP_TRUNCERROR;
1013 if ((p = alloc(ccnt + 2)) == NULL)
1014 return SP_OTHERERROR;
1015 smp->sm_lead = p;
1016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001017 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001018 for (i = 0; i < ccnt; ++i)
1019 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001020 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001021 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1022 break;
1023 *p++ = c;
1024 }
1025 smp->sm_leadlen = (int)(p - smp->sm_lead);
1026 *p++ = NUL;
1027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001028 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001029 if (c == '(')
1030 {
1031 smp->sm_oneof = p;
1032 for (++i; i < ccnt; ++i)
1033 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001034 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001035 if (c == ')')
1036 break;
1037 *p++ = c;
1038 }
1039 *p++ = NUL;
1040 if (++i < ccnt)
1041 c = getc(fd);
1042 }
1043 else
1044 smp->sm_oneof = NULL;
1045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001046 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001047 smp->sm_rules = p;
1048 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001049 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001050 *p++ = c;
1051 for (++i; i < ccnt; ++i)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001052 *p++ = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001053 *p++ = NUL;
1054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001055 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001056 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1057 if (ccnt < 0)
1058 {
1059 vim_free(smp->sm_lead);
1060 return ccnt;
1061 }
1062
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001063 if (has_mbyte)
1064 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001065 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001066 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1067 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1068 if (smp->sm_oneof == NULL)
1069 smp->sm_oneof_w = NULL;
1070 else
1071 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1072 if (smp->sm_to == NULL)
1073 smp->sm_to_w = NULL;
1074 else
1075 smp->sm_to_w = mb_str2wide(smp->sm_to);
1076 if (smp->sm_lead_w == NULL
1077 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1078 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1079 {
1080 vim_free(smp->sm_lead);
1081 vim_free(smp->sm_to);
1082 vim_free(smp->sm_lead_w);
1083 vim_free(smp->sm_oneof_w);
1084 vim_free(smp->sm_to_w);
1085 return SP_OTHERERROR;
1086 }
1087 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001088 }
1089
1090 if (gap->ga_len > 0)
1091 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001092 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1093 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001094 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1095 if ((p = alloc(1)) == NULL)
1096 return SP_OTHERERROR;
1097 p[0] = NUL;
1098 smp->sm_lead = p;
1099 smp->sm_leadlen = 0;
1100 smp->sm_oneof = NULL;
1101 smp->sm_rules = p;
1102 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001103 if (has_mbyte)
1104 {
1105 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1106 smp->sm_leadlen = 0;
1107 smp->sm_oneof_w = NULL;
1108 smp->sm_to_w = NULL;
1109 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001110 ++gap->ga_len;
1111 }
1112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001113 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001114 set_sal_first(slang);
1115
1116 return 0;
1117}
1118
1119/*
1120 * Read SN_WORDS: <word> ...
1121 * Return SP_*ERROR flags.
1122 */
1123 static int
1124read_words_section(FILE *fd, slang_T *lp, int len)
1125{
1126 int done = 0;
1127 int i;
1128 int c;
1129 char_u word[MAXWLEN];
1130
1131 while (done < len)
1132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001133 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001134 for (i = 0; ; ++i)
1135 {
1136 c = getc(fd);
1137 if (c == EOF)
1138 return SP_TRUNCERROR;
1139 word[i] = c;
1140 if (word[i] == NUL)
1141 break;
1142 if (i == MAXWLEN - 1)
1143 return SP_FORMERROR;
1144 }
1145
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001146 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001147 count_common_word(lp, word, -1, 10);
1148 done += i + 1;
1149 }
1150 return 0;
1151}
1152
1153/*
1154 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1155 * Return SP_*ERROR flags.
1156 */
1157 static int
1158read_sofo_section(FILE *fd, slang_T *slang)
1159{
1160 int cnt;
1161 char_u *from, *to;
1162 int res;
1163
1164 slang->sl_sofo = TRUE;
1165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001166 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001167 from = read_cnt_string(fd, 2, &cnt);
1168 if (cnt < 0)
1169 return cnt;
1170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001171 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001172 to = read_cnt_string(fd, 2, &cnt);
1173 if (cnt < 0)
1174 {
1175 vim_free(from);
1176 return cnt;
1177 }
1178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001179 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001180 if (from != NULL && to != NULL)
1181 res = set_sofo(slang, from, to);
1182 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001183 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001184 else
1185 res = 0;
1186
1187 vim_free(from);
1188 vim_free(to);
1189 return res;
1190}
1191
1192/*
1193 * Read the compound section from the .spl file:
1194 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1195 * Returns SP_*ERROR flags.
1196 */
1197 static int
1198read_compound(FILE *fd, slang_T *slang, int len)
1199{
1200 int todo = len;
1201 int c;
1202 int atstart;
1203 char_u *pat;
1204 char_u *pp;
1205 char_u *cp;
1206 char_u *ap;
1207 char_u *crp;
1208 int cnt;
1209 garray_T *gap;
1210
1211 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001212 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001213
1214 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001215 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001216 if (c < 2)
1217 c = MAXWLEN;
1218 slang->sl_compmax = c;
1219
1220 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001221 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001222 if (c < 1)
1223 c = 0;
1224 slang->sl_compminlen = c;
1225
1226 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001227 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001228 if (c < 1)
1229 c = MAXWLEN;
1230 slang->sl_compsylmax = c;
1231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001232 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001233 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001234 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001235 else
1236 {
1237 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001238 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001239 --todo;
1240 slang->sl_compoptions = c;
1241
1242 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001243 c = get2c(fd); // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001244 todo -= 2;
1245 ga_init2(gap, sizeof(char_u *), c);
1246 if (ga_grow(gap, c) == OK)
1247 while (--c >= 0)
1248 {
1249 ((char_u **)(gap->ga_data))[gap->ga_len++] =
1250 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001251 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001252 if (cnt < 0)
1253 return cnt;
1254 todo -= cnt + 1;
1255 }
1256 }
1257 if (todo < 0)
1258 return SP_FORMERROR;
1259
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001260 // Turn the COMPOUNDRULE items into a regexp pattern:
1261 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1262 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1263 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001264 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001265 if (enc_utf8)
1266 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001267 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001268 if (pat == NULL)
1269 return SP_OTHERERROR;
1270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001271 // We also need a list of all flags that can appear at the start and one
1272 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001273 cp = alloc(todo + 1);
1274 if (cp == NULL)
1275 {
1276 vim_free(pat);
1277 return SP_OTHERERROR;
1278 }
1279 slang->sl_compstartflags = cp;
1280 *cp = NUL;
1281
1282 ap = alloc(todo + 1);
1283 if (ap == NULL)
1284 {
1285 vim_free(pat);
1286 return SP_OTHERERROR;
1287 }
1288 slang->sl_compallflags = ap;
1289 *ap = NUL;
1290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001291 // And a list of all patterns in their original form, for checking whether
1292 // compounding may work in match_compoundrule(). This is freed when we
1293 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001294 crp = alloc(todo + 1);
1295 slang->sl_comprules = crp;
1296
1297 pp = pat;
1298 *pp++ = '^';
1299 *pp++ = '\\';
1300 *pp++ = '(';
1301
1302 atstart = 1;
1303 while (todo-- > 0)
1304 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001305 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001306 if (c == EOF)
1307 {
1308 vim_free(pat);
1309 return SP_TRUNCERROR;
1310 }
1311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001312 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001313 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1314 && !byte_in_str(slang->sl_compallflags, c))
1315 {
1316 *ap++ = c;
1317 *ap = NUL;
1318 }
1319
1320 if (atstart != 0)
1321 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001322 // At start of item: copy flags to "sl_compstartflags". For a
1323 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001324 if (c == '[')
1325 atstart = 2;
1326 else if (c == ']')
1327 atstart = 0;
1328 else
1329 {
1330 if (!byte_in_str(slang->sl_compstartflags, c))
1331 {
1332 *cp++ = c;
1333 *cp = NUL;
1334 }
1335 if (atstart == 1)
1336 atstart = 0;
1337 }
1338 }
1339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001341 if (crp != NULL)
1342 {
1343 if (c == '?' || c == '+' || c == '*')
1344 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001345 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001346 crp = NULL;
1347 }
1348 else
1349 *crp++ = c;
1350 }
1351
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001352 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001353 {
1354 *pp++ = '\\';
1355 *pp++ = '|';
1356 atstart = 1;
1357 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001358 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001359 {
1360 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001361 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001362 if (enc_utf8)
1363 pp += mb_char2bytes(c, pp);
1364 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001365 *pp++ = c;
1366 }
1367 }
1368
1369 *pp++ = '\\';
1370 *pp++ = ')';
1371 *pp++ = '$';
1372 *pp = NUL;
1373
1374 if (crp != NULL)
1375 *crp = NUL;
1376
1377 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1378 vim_free(pat);
1379 if (slang->sl_compprog == NULL)
1380 return SP_FORMERROR;
1381
1382 return 0;
1383}
1384
1385/*
1386 * Set the SOFOFROM and SOFOTO items in language "lp".
1387 * Returns SP_*ERROR flags when there is something wrong.
1388 */
1389 static int
1390set_sofo(slang_T *lp, char_u *from, char_u *to)
1391{
1392 int i;
1393
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001394 garray_T *gap;
1395 char_u *s;
1396 char_u *p;
1397 int c;
1398 int *inp;
1399
1400 if (has_mbyte)
1401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402 // Use "sl_sal" as an array with 256 pointers to a list of wide
1403 // characters. The index is the low byte of the character.
1404 // The list contains from-to pairs with a terminating NUL.
1405 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001406 gap = &lp->sl_sal;
1407 ga_init2(gap, sizeof(int *), 1);
1408 if (ga_grow(gap, 256) == FAIL)
1409 return SP_OTHERERROR;
1410 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1411 gap->ga_len = 256;
1412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001413 // First count the number of items for each list. Temporarily use
1414 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001415 for (p = from, s = to; *p != NUL && *s != NUL; )
1416 {
1417 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001418 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001419 if (c >= 256)
1420 ++lp->sl_sal_first[c & 0xff];
1421 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001423 return SP_FORMERROR;
1424
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001425 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001426 for (i = 0; i < 256; ++i)
1427 if (lp->sl_sal_first[i] > 0)
1428 {
1429 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1430 if (p == NULL)
1431 return SP_OTHERERROR;
1432 ((int **)gap->ga_data)[i] = (int *)p;
1433 *(int *)p = 0;
1434 }
1435
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001436 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1437 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001438 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1439 for (p = from, s = to; *p != NUL && *s != NUL; )
1440 {
1441 c = mb_cptr2char_adv(&p);
1442 i = mb_cptr2char_adv(&s);
1443 if (c >= 256)
1444 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001445 // Append the from-to chars at the end of the list with
1446 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001447 inp = ((int **)gap->ga_data)[c & 0xff];
1448 while (*inp != 0)
1449 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001450 *inp++ = c; // from char
1451 *inp++ = i; // to char
1452 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001453 }
1454 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001455 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001456 lp->sl_sal_first[c] = i;
1457 }
1458 }
1459 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001461 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001462 if (STRLEN(from) != STRLEN(to))
1463 return SP_FORMERROR;
1464
1465 for (i = 0; to[i] != NUL; ++i)
1466 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001467 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001468 }
1469
1470 return 0;
1471}
1472
1473/*
1474 * Fill the first-index table for "lp".
1475 */
1476 static void
1477set_sal_first(slang_T *lp)
1478{
1479 salfirst_T *sfirst;
1480 int i;
1481 salitem_T *smp;
1482 int c;
1483 garray_T *gap = &lp->sl_sal;
1484
1485 sfirst = lp->sl_sal_first;
1486 for (i = 0; i < 256; ++i)
1487 sfirst[i] = -1;
1488 smp = (salitem_T *)gap->ga_data;
1489 for (i = 0; i < gap->ga_len; ++i)
1490 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001491 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001492 // Use the lowest byte of the first character. For latin1 it's
1493 // the character, for other encodings it should differ for most
1494 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001495 c = *smp[i].sm_lead_w & 0xff;
1496 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001497 c = *smp[i].sm_lead;
1498 if (sfirst[c] == -1)
1499 {
1500 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001501 if (has_mbyte)
1502 {
1503 int n;
1504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001505 // Make sure all entries with this byte are following each
1506 // other. Move the ones that are in the wrong position. Do
1507 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001508 while (i + 1 < gap->ga_len
1509 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001510 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001511 ++i;
1512
1513 for (n = 1; i + n < gap->ga_len; ++n)
1514 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1515 {
1516 salitem_T tsal;
1517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001518 // Move entry with same index byte after the entries
1519 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001520 ++i;
1521 --n;
1522 tsal = smp[i + n];
1523 mch_memmove(smp + i + 1, smp + i,
1524 sizeof(salitem_T) * n);
1525 smp[i] = tsal;
1526 }
1527 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001528 }
1529 }
1530}
1531
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001532/*
1533 * Turn a multi-byte string into a wide character string.
1534 * Return it in allocated memory (NULL for out-of-memory)
1535 */
1536 static int *
1537mb_str2wide(char_u *s)
1538{
1539 int *res;
1540 char_u *p;
1541 int i = 0;
1542
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001543 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001544 if (res != NULL)
1545 {
1546 for (p = s; *p != NUL; )
1547 res[i++] = mb_ptr2char_adv(&p);
1548 res[i] = NUL;
1549 }
1550 return res;
1551}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001552
1553/*
1554 * Read a tree from the .spl or .sug file.
1555 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1556 * This is skipped when the tree has zero length.
1557 * Returns zero when OK, SP_ value for an error.
1558 */
1559 static int
1560spell_read_tree(
1561 FILE *fd,
1562 char_u **bytsp,
1563 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001564 int prefixtree, // TRUE for the prefix tree
1565 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001566{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001567 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001568 int idx;
1569 char_u *bp;
1570 idx_T *ip;
1571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001572 // The tree size was computed when writing the file, so that we can
1573 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001574 len = get4c(fd);
1575 if (len < 0)
1576 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001577 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001578 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001579 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001580 if (len > 0)
1581 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001582 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001583 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001584 if (bp == NULL)
1585 return SP_OTHERERROR;
1586 *bytsp = bp;
1587
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001588 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001589 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001590 if (ip == NULL)
1591 return SP_OTHERERROR;
1592 *idxsp = ip;
1593
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001594 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001595 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1596 if (idx < 0)
1597 return idx;
1598 }
1599 return 0;
1600}
1601
1602/*
1603 * Read one row of siblings from the spell file and store it in the byte array
1604 * "byts" and index array "idxs". Recursively read the children.
1605 *
1606 * NOTE: The code here must match put_node()!
1607 *
1608 * Returns the index (>= 0) following the siblings.
1609 * Returns SP_TRUNCERROR if the file is shorter than expected.
1610 * Returns SP_FORMERROR if there is a format error.
1611 */
1612 static idx_T
1613read_tree_node(
1614 FILE *fd,
1615 char_u *byts,
1616 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001617 int maxidx, // size of arrays
1618 idx_T startidx, // current index in "byts" and "idxs"
1619 int prefixtree, // TRUE for reading PREFIXTREE
1620 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001621{
1622 int len;
1623 int i;
1624 int n;
1625 idx_T idx = startidx;
1626 int c;
1627 int c2;
1628#define SHARED_MASK 0x8000000
1629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001630 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001631 if (len <= 0)
1632 return SP_TRUNCERROR;
1633
1634 if (startidx + len >= maxidx)
1635 return SP_FORMERROR;
1636 byts[idx++] = len;
1637
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001638 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001639 for (i = 1; i <= len; ++i)
1640 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001641 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001642 if (c < 0)
1643 return SP_TRUNCERROR;
1644 if (c <= BY_SPECIAL)
1645 {
1646 if (c == BY_NOFLAGS && !prefixtree)
1647 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001648 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001649 idxs[idx] = 0;
1650 c = 0;
1651 }
1652 else if (c != BY_INDEX)
1653 {
1654 if (prefixtree)
1655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001656 // Read the optional pflags byte, the prefix ID and the
1657 // condition nr. In idxs[] store the prefix ID in the low
1658 // byte, the condition index shifted up 8 bits, the flags
1659 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001660 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001661 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001662 else
1663 c = 0;
1664
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001665 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001666
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001667 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001668 if (n >= maxprefcondnr)
1669 return SP_FORMERROR;
1670 c |= (n << 8);
1671 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001672 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001673 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001674 // Read flags and optional region and prefix ID. In
1675 // idxs[] the flags go in the low two bytes, region above
1676 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001677 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001678 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001679 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001680 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001681 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001682 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001683 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001684 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001685 }
1686
1687 idxs[idx] = c;
1688 c = 0;
1689 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001691 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001692 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001693 n = get3c(fd);
1694 if (n < 0 || n >= maxidx)
1695 return SP_FORMERROR;
1696 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001697 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001698 }
1699 }
1700 byts[idx++] = c;
1701 }
1702
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001703 // Recursively read the children for non-shared siblings.
1704 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1705 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001706 for (i = 1; i <= len; ++i)
1707 if (byts[startidx + i] != 0)
1708 {
1709 if (idxs[startidx + i] & SHARED_MASK)
1710 idxs[startidx + i] &= ~SHARED_MASK;
1711 else
1712 {
1713 idxs[startidx + i] = idx;
1714 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1715 prefixtree, maxprefcondnr);
1716 if (idx < 0)
1717 break;
1718 }
1719 }
1720
1721 return idx;
1722}
1723
1724/*
1725 * Reload the spell file "fname" if it's loaded.
1726 */
1727 static void
1728spell_reload_one(
1729 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001730 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001731{
1732 slang_T *slang;
1733 int didit = FALSE;
1734
1735 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
1736 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001737 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001738 {
1739 slang_clear(slang);
1740 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001741 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001742 slang_clear(slang);
1743 redraw_all_later(SOME_VALID);
1744 didit = TRUE;
1745 }
1746 }
1747
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001748 // When "zg" was used and the file wasn't loaded yet, should redo
1749 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001750 if (added_word && !didit)
1751 did_set_spelllang(curwin);
1752}
1753
1754
1755/*
1756 * Functions for ":mkspell".
1757 */
1758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001759#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1760 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001761/*
1762 * Main structure to store the contents of a ".aff" file.
1763 */
1764typedef struct afffile_S
1765{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001766 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1767 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1768 unsigned af_rare; // RARE ID for rare word
1769 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1770 unsigned af_bad; // BAD ID for banned word
1771 unsigned af_needaffix; // NEEDAFFIX ID
1772 unsigned af_circumfix; // CIRCUMFIX ID
1773 unsigned af_needcomp; // NEEDCOMPOUND ID
1774 unsigned af_comproot; // COMPOUNDROOT ID
1775 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1776 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1777 unsigned af_nosuggest; // NOSUGGEST ID
1778 int af_pfxpostpone; // postpone prefixes without chop string and
1779 // without flags
1780 int af_ignoreextra; // IGNOREEXTRA present
1781 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1782 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1783 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001784} afffile_T;
1785
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001786#define AFT_CHAR 0 // flags are one character
1787#define AFT_LONG 1 // flags are two characters
1788#define AFT_CAPLONG 2 // flags are one or two characters
1789#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001790
1791typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001793struct affentry_S
1794{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001795 affentry_T *ae_next; // next affix with same name/number
1796 char_u *ae_chop; // text to chop off basic word (can be NULL)
1797 char_u *ae_add; // text to add to basic word (can be NULL)
1798 char_u *ae_flags; // flags on the affix (can be NULL)
1799 char_u *ae_cond; // condition (NULL for ".")
1800 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1801 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1802 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001803};
1804
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001805#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001806
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001807// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001808typedef struct affheader_S
1809{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001810 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1811 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1812 int ah_newID; // prefix ID after renumbering; 0 if not used
1813 int ah_combine; // suffix may combine with prefix
1814 int ah_follows; // another affix block should be following
1815 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001816} affheader_T;
1817
1818#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1819
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001820// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001821typedef struct compitem_S
1822{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001823 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1824 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1825 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001826} compitem_T;
1827
1828#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1829
1830/*
1831 * Structure that is used to store the items in the word tree. This avoids
1832 * the need to keep track of each allocated thing, everything is freed all at
1833 * once after ":mkspell" is done.
1834 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1835 * "sb_data" is correct for systems where pointers must be aligned on
1836 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1837 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001838#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001839typedef struct sblock_S sblock_T;
1840struct sblock_S
1841{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001842 int sb_used; // nr of bytes already in use
1843 sblock_T *sb_next; // next block in list
1844 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001845};
1846
1847/*
1848 * A node in the tree.
1849 */
1850typedef struct wordnode_S wordnode_T;
1851struct wordnode_S
1852{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001853 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001854 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001855 char_u hashkey[6]; // the hash key, only used while compressing
1856 int index; // index in written nodes (valid after first
1857 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001858 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001859 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001860 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001861 wordnode_T *next; // next node with same hash key
1862 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001863 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001864 wordnode_T *wn_child; // child (next byte in word)
1865 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1866 // always sorted)
1867 int wn_refs; // Nr. of references to this node. Only
1868 // relevant for first node in a list of
1869 // siblings, in following siblings it is
1870 // always one.
1871 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001872
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001873 // Info for when "wn_byte" is NUL.
1874 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1875 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1876 // "wn_region" the LSW of the wordnr.
1877 char_u wn_affixID; // supported/required prefix ID or 0
1878 short_u wn_flags; // WF_ flags
1879 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001880
1881#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001883#endif
1884};
1885
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001886#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001887
1888#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1889
1890/*
1891 * Info used while reading the spell files.
1892 */
1893typedef struct spellinfo_S
1894{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001895 wordnode_T *si_foldroot; // tree with case-folded words
1896 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001897
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001898 wordnode_T *si_keeproot; // tree with keep-case words
1899 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001900
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001901 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001902
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001903 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001905 sblock_T *si_blocks; // memory blocks used
1906 long si_blocks_cnt; // memory blocks allocated
1907 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001909 long si_compress_cnt; // words to add before lowering
1910 // compression limit
1911 wordnode_T *si_first_free; // List of nodes that have been freed during
1912 // compression, linked by "wn_child" field.
1913 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001914#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001915 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001916#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001917 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 int si_ascii; // handling only ASCII words
1920 int si_add; // addition file
1921 int si_clear_chartab; // when TRUE clear char tables
1922 int si_region; // region mask
1923 vimconv_T si_conv; // for conversion to 'encoding'
1924 int si_memtot; // runtime memory used
1925 int si_verbose; // verbose messages
1926 int si_msg_count; // number of words added since last message
1927 char_u *si_info; // info text chars or NULL
1928 int si_region_count; // number of regions supported (1 when there
1929 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001930 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001931 // region names; used only if
1932 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001934 garray_T si_rep; // list of fromto_T entries from REP lines
1935 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1936 garray_T si_sal; // list of fromto_T entries from SAL lines
1937 char_u *si_sofofr; // SOFOFROM text
1938 char_u *si_sofoto; // SOFOTO text
1939 int si_nosugfile; // NOSUGFILE item found
1940 int si_nosplitsugs; // NOSPLITSUGS item found
1941 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1942 int si_followup; // soundsalike: ?
1943 int si_collapse; // soundsalike: ?
1944 hashtab_T si_commonwords; // hashtable for common words
1945 time_t si_sugtime; // timestamp for .sug file
1946 int si_rem_accents; // soundsalike: remove accents
1947 garray_T si_map; // MAP info concatenated
1948 char_u *si_midword; // MIDWORD chars or NULL
1949 int si_compmax; // max nr of words for compounding
1950 int si_compminlen; // minimal length for compounding
1951 int si_compsylmax; // max nr of syllables for compounding
1952 int si_compoptions; // COMP_ flags
1953 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1954 // a string
1955 char_u *si_compflags; // flags used for compounding
1956 char_u si_nobreak; // NOBREAK
1957 char_u *si_syllable; // syllable string
1958 garray_T si_prefcond; // table with conditions for postponed
1959 // prefixes, each stored as a string
1960 int si_newprefID; // current value for ah_newID
1961 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001962} spellinfo_T;
1963
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001964static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1965static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1966static int spell_info_item(char_u *s);
1967static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1968static unsigned get_affitem(int flagtype, char_u **pp);
1969static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1970static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001971static void aff_check_number(int spinval, int affval, char *name);
1972static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1973static int str_equal(char_u *s1, char_u *s2);
1974static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1975static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001976static int get_affix_flags(afffile_T *affile, char_u *afflist);
1977static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1978static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
1979static 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 +02001980static void *getroom(spellinfo_T *spin, size_t len, int align);
1981static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001982static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
1983static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
1984static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001985static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
1986static void wordtree_compress(spellinfo_T *spin, wordnode_T *root);
1987static int node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot);
1988static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001989static void clear_node(wordnode_T *node);
1990static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001991static int sug_filltree(spellinfo_T *spin, slang_T *slang);
1992static int sug_maketable(spellinfo_T *spin);
1993static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
1994static int offset2bytes(int nr, char_u *buf);
1995static void sug_write(spellinfo_T *spin, char_u *fname);
1996static void spell_message(spellinfo_T *spin, char_u *str);
1997static void init_spellfile(void);
1998
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001999// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
2000// but it must be negative to indicate the prefix tree to tree_add_word().
2001// Use a negative number with the lower 8 bits zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002002#define PFX_FLAGS -256
2003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002004// flags for "condit" argument of store_aff_word()
2005#define CONDIT_COMB 1 // affix must combine
2006#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2007#define CONDIT_SUF 4 // add a suffix for matching flags
2008#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002009
2010/*
2011 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
2012 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002013static long compress_start = 30000; // memory / SBLOCKSIZE
2014static long compress_inc = 100; // memory / SBLOCKSIZE
2015static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002016
2017/*
2018 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2019 * Sets "sps_flags".
2020 */
2021 int
2022spell_check_msm(void)
2023{
2024 char_u *p = p_msm;
2025 long start = 0;
2026 long incr = 0;
2027 long added = 0;
2028
2029 if (!VIM_ISDIGIT(*p))
2030 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002031 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002032 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2033 if (*p != ',')
2034 return FAIL;
2035 ++p;
2036 if (!VIM_ISDIGIT(*p))
2037 return FAIL;
2038 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2039 if (*p != ',')
2040 return FAIL;
2041 ++p;
2042 if (!VIM_ISDIGIT(*p))
2043 return FAIL;
2044 added = getdigits(&p) * 1024;
2045 if (*p != NUL)
2046 return FAIL;
2047
2048 if (start == 0 || incr == 0 || added == 0 || incr > start)
2049 return FAIL;
2050
2051 compress_start = start;
2052 compress_inc = incr;
2053 compress_added = added;
2054 return OK;
2055}
2056
2057#ifdef SPELL_PRINTTREE
2058/*
2059 * For debugging the tree code: print the current tree in a (more or less)
2060 * readable format, so that we can see what happens when adding a word and/or
2061 * compressing the tree.
2062 * Based on code from Olaf Seibert.
2063 */
2064#define PRINTLINESIZE 1000
2065#define PRINTWIDTH 6
2066
2067#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2068 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2069
2070static char line1[PRINTLINESIZE];
2071static char line2[PRINTLINESIZE];
2072static char line3[PRINTLINESIZE];
2073
2074 static void
2075spell_clear_flags(wordnode_T *node)
2076{
2077 wordnode_T *np;
2078
2079 for (np = node; np != NULL; np = np->wn_sibling)
2080 {
2081 np->wn_u1.index = FALSE;
2082 spell_clear_flags(np->wn_child);
2083 }
2084}
2085
2086 static void
2087spell_print_node(wordnode_T *node, int depth)
2088{
2089 if (node->wn_u1.index)
2090 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002091 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002092 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2093 PRINTSOME(line2, depth, " ", 0, 0);
2094 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002095 msg(line1);
2096 msg(line2);
2097 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002098 }
2099 else
2100 {
2101 node->wn_u1.index = TRUE;
2102
2103 if (node->wn_byte != NUL)
2104 {
2105 if (node->wn_child != NULL)
2106 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2107 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002109 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2110 }
2111 else
2112 PRINTSOME(line1, depth, " $ ", 0, 0);
2113
2114 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2115
2116 if (node->wn_sibling != NULL)
2117 PRINTSOME(line3, depth, " | ", 0, 0);
2118 else
2119 PRINTSOME(line3, depth, " ", 0, 0);
2120
2121 if (node->wn_byte == NUL)
2122 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002123 msg(line1);
2124 msg(line2);
2125 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002126 }
2127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002128 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002129 if (node->wn_byte != NUL && node->wn_child != NULL)
2130 spell_print_node(node->wn_child, depth + 1);
2131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002133 if (node->wn_sibling != NULL)
2134 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002136 STRCPY(line1, line3);
2137 STRCPY(line2, line3);
2138 spell_print_node(node->wn_sibling, depth);
2139 }
2140 }
2141}
2142
2143 static void
2144spell_print_tree(wordnode_T *root)
2145{
2146 if (root != NULL)
2147 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002148 // Clear the "wn_u1.index" fields, used to remember what has been
2149 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002150 spell_clear_flags(root);
2151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002153 spell_print_node(root, 0);
2154 }
2155}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002156#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002157
2158/*
2159 * Read the affix file "fname".
2160 * Returns an afffile_T, NULL for complete failure.
2161 */
2162 static afffile_T *
2163spell_read_aff(spellinfo_T *spin, char_u *fname)
2164{
2165 FILE *fd;
2166 afffile_T *aff;
2167 char_u rline[MAXLINELEN];
2168 char_u *line;
2169 char_u *pc = NULL;
2170#define MAXITEMCNT 30
2171 char_u *(items[MAXITEMCNT]);
2172 int itemcnt;
2173 char_u *p;
2174 int lnum = 0;
2175 affheader_T *cur_aff = NULL;
2176 int did_postpone_prefix = FALSE;
2177 int aff_todo = 0;
2178 hashtab_T *tp;
2179 char_u *low = NULL;
2180 char_u *fol = NULL;
2181 char_u *upp = NULL;
2182 int do_rep;
2183 int do_repsal;
2184 int do_sal;
2185 int do_mapline;
2186 int found_map = FALSE;
2187 hashitem_T *hi;
2188 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002189 int compminlen = 0; // COMPOUNDMIN value
2190 int compsylmax = 0; // COMPOUNDSYLMAX value
2191 int compoptions = 0; // COMP_ flags
2192 int compmax = 0; // COMPOUNDWORDMAX value
2193 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2194 // concatenated
2195 char_u *midword = NULL; // MIDWORD value
2196 char_u *syllable = NULL; // SYLLABLE value
2197 char_u *sofofrom = NULL; // SOFOFROM value
2198 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002199
2200 /*
2201 * Open the file.
2202 */
2203 fd = mch_fopen((char *)fname, "r");
2204 if (fd == NULL)
2205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002207 return NULL;
2208 }
2209
Bram Moolenaarc1669272018-06-19 14:23:53 +02002210 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002211 spell_message(spin, IObuff);
2212
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002214 do_rep = spin->si_rep.ga_len == 0;
2215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002216 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002217 do_repsal = spin->si_repsal.ga_len == 0;
2218
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002219 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002220 do_sal = spin->si_sal.ga_len == 0;
2221
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002222 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002223 do_mapline = spin->si_map.ga_len == 0;
2224
2225 /*
2226 * Allocate and init the afffile_T structure.
2227 */
2228 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2229 if (aff == NULL)
2230 {
2231 fclose(fd);
2232 return NULL;
2233 }
2234 hash_init(&aff->af_pref);
2235 hash_init(&aff->af_suff);
2236 hash_init(&aff->af_comp);
2237
2238 /*
2239 * Read all the lines in the file one by one.
2240 */
2241 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2242 {
2243 line_breakcheck();
2244 ++lnum;
2245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002246 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002247 if (*rline == '#')
2248 continue;
2249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002251 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002252 if (spin->si_conv.vc_type != CONV_NONE)
2253 {
2254 pc = string_convert(&spin->si_conv, rline, NULL);
2255 if (pc == NULL)
2256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002257 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002258 fname, lnum, rline);
2259 continue;
2260 }
2261 line = pc;
2262 }
2263 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002264 {
2265 pc = NULL;
2266 line = rline;
2267 }
2268
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002269 // Split the line up in white separated items. Put a NUL after each
2270 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002271 itemcnt = 0;
2272 for (p = line; ; )
2273 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002274 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002275 ++p;
2276 if (*p == NUL)
2277 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002278 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002279 break;
2280 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002281 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002282 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002283 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002284 ++p;
2285 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002286 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002287 ++p;
2288 if (*p == NUL)
2289 break;
2290 *p++ = NUL;
2291 }
2292
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002294 if (itemcnt > 0)
2295 {
2296 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2297 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002298 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002299 aff->af_enc = enc_canonize(items[1]);
2300 if (aff->af_enc != NULL && !spin->si_ascii
2301 && convert_setup(&spin->si_conv, aff->af_enc,
2302 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002303 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002304 fname, aff->af_enc, p_enc);
2305 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002306 }
2307 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2308 && aff->af_flagtype == AFT_CHAR)
2309 {
2310 if (STRCMP(items[1], "long") == 0)
2311 aff->af_flagtype = AFT_LONG;
2312 else if (STRCMP(items[1], "num") == 0)
2313 aff->af_flagtype = AFT_NUM;
2314 else if (STRCMP(items[1], "caplong") == 0)
2315 aff->af_flagtype = AFT_CAPLONG;
2316 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002317 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002318 fname, lnum, items[1]);
2319 if (aff->af_rare != 0
2320 || aff->af_keepcase != 0
2321 || aff->af_bad != 0
2322 || aff->af_needaffix != 0
2323 || aff->af_circumfix != 0
2324 || aff->af_needcomp != 0
2325 || aff->af_comproot != 0
2326 || aff->af_nosuggest != 0
2327 || compflags != NULL
2328 || aff->af_suff.ht_used > 0
2329 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002331 fname, lnum, items[1]);
2332 }
2333 else if (spell_info_item(items[0]))
2334 {
2335 p = (char_u *)getroom(spin,
2336 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2337 + STRLEN(items[0])
2338 + STRLEN(items[1]) + 3, FALSE);
2339 if (p != NULL)
2340 {
2341 if (spin->si_info != NULL)
2342 {
2343 STRCPY(p, spin->si_info);
2344 STRCAT(p, "\n");
2345 }
2346 STRCAT(p, items[0]);
2347 STRCAT(p, " ");
2348 STRCAT(p, items[1]);
2349 spin->si_info = p;
2350 }
2351 }
2352 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2353 && midword == NULL)
2354 {
2355 midword = getroom_save(spin, items[1]);
2356 }
2357 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2358 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002359 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002360 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002361 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002362 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2363 || is_aff_rule(items, itemcnt, "RARE", 2))
2364 && aff->af_rare == 0)
2365 {
2366 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2367 fname, lnum);
2368 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002369 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002370 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2371 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2372 && aff->af_keepcase == 0)
2373 {
2374 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2375 fname, lnum);
2376 }
2377 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2378 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2379 && aff->af_bad == 0)
2380 {
2381 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2382 fname, lnum);
2383 }
2384 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2385 && aff->af_needaffix == 0)
2386 {
2387 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2388 fname, lnum);
2389 }
2390 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2391 && aff->af_circumfix == 0)
2392 {
2393 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2394 fname, lnum);
2395 }
2396 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2397 && aff->af_nosuggest == 0)
2398 {
2399 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2400 fname, lnum);
2401 }
2402 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2403 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2404 && aff->af_needcomp == 0)
2405 {
2406 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2407 fname, lnum);
2408 }
2409 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2410 && aff->af_comproot == 0)
2411 {
2412 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2413 fname, lnum);
2414 }
2415 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2416 && aff->af_compforbid == 0)
2417 {
2418 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2419 fname, lnum);
2420 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002421 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002422 fname, lnum);
2423 }
2424 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2425 && aff->af_comppermit == 0)
2426 {
2427 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2428 fname, lnum);
2429 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002430 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002431 fname, lnum);
2432 }
2433 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2434 && compflags == NULL)
2435 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002436 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2437 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002438 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2439 if (p != NULL)
2440 {
2441 STRCPY(p, items[1]);
2442 STRCAT(p, "+");
2443 compflags = p;
2444 }
2445 }
2446 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2447 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002448 // We don't use the count, but do check that it's a number and
2449 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002450 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002451 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002452 fname, lnum, items[1]);
2453 }
2454 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2455 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002456 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002457 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2458 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002459 // Concatenate this string to previously defined ones,
2460 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002461 l = (int)STRLEN(items[1]) + 1;
2462 if (compflags != NULL)
2463 l += (int)STRLEN(compflags) + 1;
2464 p = getroom(spin, l, FALSE);
2465 if (p != NULL)
2466 {
2467 if (compflags != NULL)
2468 {
2469 STRCPY(p, compflags);
2470 STRCAT(p, "/");
2471 }
2472 STRCAT(p, items[1]);
2473 compflags = p;
2474 }
2475 }
2476 }
2477 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2478 && compmax == 0)
2479 {
2480 compmax = atoi((char *)items[1]);
2481 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002482 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002483 fname, lnum, items[1]);
2484 }
2485 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2486 && compminlen == 0)
2487 {
2488 compminlen = atoi((char *)items[1]);
2489 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002490 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002491 fname, lnum, items[1]);
2492 }
2493 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2494 && compsylmax == 0)
2495 {
2496 compsylmax = atoi((char *)items[1]);
2497 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002498 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002499 fname, lnum, items[1]);
2500 }
2501 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2502 {
2503 compoptions |= COMP_CHECKDUP;
2504 }
2505 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2506 {
2507 compoptions |= COMP_CHECKREP;
2508 }
2509 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2510 {
2511 compoptions |= COMP_CHECKCASE;
2512 }
2513 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2514 {
2515 compoptions |= COMP_CHECKTRIPLE;
2516 }
2517 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2518 {
2519 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002520 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002521 fname, lnum, items[1]);
2522 }
2523 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2524 {
2525 garray_T *gap = &spin->si_comppat;
2526 int i;
2527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002528 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002529 for (i = 0; i < gap->ga_len - 1; i += 2)
2530 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2531 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2532 items[2]) == 0)
2533 break;
2534 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2535 {
2536 ((char_u **)(gap->ga_data))[gap->ga_len++]
2537 = getroom_save(spin, items[1]);
2538 ((char_u **)(gap->ga_data))[gap->ga_len++]
2539 = getroom_save(spin, items[2]);
2540 }
2541 }
2542 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2543 && syllable == NULL)
2544 {
2545 syllable = getroom_save(spin, items[1]);
2546 }
2547 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2548 {
2549 spin->si_nobreak = TRUE;
2550 }
2551 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2552 {
2553 spin->si_nosplitsugs = TRUE;
2554 }
2555 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2556 {
2557 spin->si_nocompoundsugs = TRUE;
2558 }
2559 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2560 {
2561 spin->si_nosugfile = TRUE;
2562 }
2563 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2564 {
2565 aff->af_pfxpostpone = TRUE;
2566 }
2567 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2568 {
2569 aff->af_ignoreextra = TRUE;
2570 }
2571 else if ((STRCMP(items[0], "PFX") == 0
2572 || STRCMP(items[0], "SFX") == 0)
2573 && aff_todo == 0
2574 && itemcnt >= 4)
2575 {
2576 int lasti = 4;
2577 char_u key[AH_KEY_LEN];
2578
2579 if (*items[0] == 'P')
2580 tp = &aff->af_pref;
2581 else
2582 tp = &aff->af_suff;
2583
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002584 // Myspell allows the same affix name to be used multiple
2585 // times. The affix files that do this have an undocumented
2586 // "S" flag on all but the last block, thus we check for that
2587 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002588 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2589 hi = hash_find(tp, key);
2590 if (!HASHITEM_EMPTY(hi))
2591 {
2592 cur_aff = HI2AH(hi);
2593 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002594 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002595 fname, lnum, items[1]);
2596 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002597 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002598 fname, lnum, items[1]);
2599 }
2600 else
2601 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002602 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002603 cur_aff = (affheader_T *)getroom(spin,
2604 sizeof(affheader_T), TRUE);
2605 if (cur_aff == NULL)
2606 break;
2607 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2608 fname, lnum);
2609 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2610 break;
2611 if (cur_aff->ah_flag == aff->af_bad
2612 || cur_aff->ah_flag == aff->af_rare
2613 || cur_aff->ah_flag == aff->af_keepcase
2614 || cur_aff->ah_flag == aff->af_needaffix
2615 || cur_aff->ah_flag == aff->af_circumfix
2616 || cur_aff->ah_flag == aff->af_nosuggest
2617 || cur_aff->ah_flag == aff->af_needcomp
2618 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002619 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002620 fname, lnum, items[1]);
2621 STRCPY(cur_aff->ah_key, items[1]);
2622 hash_add(tp, cur_aff->ah_key);
2623
2624 cur_aff->ah_combine = (*items[2] == 'Y');
2625 }
2626
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002627 // Check for the "S" flag, which apparently means that another
2628 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002629 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2630 {
2631 ++lasti;
2632 cur_aff->ah_follows = TRUE;
2633 }
2634 else
2635 cur_aff->ah_follows = FALSE;
2636
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002637 // Myspell allows extra text after the item, but that might
2638 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002639 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002640 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002641
2642 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002643 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002644 fname, lnum, items[2]);
2645
2646 if (*items[0] == 'P' && aff->af_pfxpostpone)
2647 {
2648 if (cur_aff->ah_newID == 0)
2649 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 // Use a new number in the .spl file later, to be able
2651 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002652 check_renumber(spin);
2653 cur_aff->ah_newID = ++spin->si_newprefID;
2654
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002655 // We only really use ah_newID if the prefix is
2656 // postponed. We know that only after handling all
2657 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002658 did_postpone_prefix = FALSE;
2659 }
2660 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002661 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002662 did_postpone_prefix = TRUE;
2663 }
2664
2665 aff_todo = atoi((char *)items[3]);
2666 }
2667 else if ((STRCMP(items[0], "PFX") == 0
2668 || STRCMP(items[0], "SFX") == 0)
2669 && aff_todo > 0
2670 && STRCMP(cur_aff->ah_key, items[1]) == 0
2671 && itemcnt >= 5)
2672 {
2673 affentry_T *aff_entry;
2674 int upper = FALSE;
2675 int lasti = 5;
2676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002677 // Myspell allows extra text after the item, but that might
2678 // mean mistakes go unnoticed. Require a comment-starter,
2679 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002680 if (itemcnt > lasti
2681 && !aff->af_ignoreextra
2682 && *items[lasti] != '#'
2683 && (STRCMP(items[lasti], "-") != 0
2684 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002685 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002687 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002688 --aff_todo;
2689 aff_entry = (affentry_T *)getroom(spin,
2690 sizeof(affentry_T), TRUE);
2691 if (aff_entry == NULL)
2692 break;
2693
2694 if (STRCMP(items[2], "0") != 0)
2695 aff_entry->ae_chop = getroom_save(spin, items[2]);
2696 if (STRCMP(items[3], "0") != 0)
2697 {
2698 aff_entry->ae_add = getroom_save(spin, items[3]);
2699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002701 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2702 if (aff_entry->ae_flags != NULL)
2703 {
2704 *aff_entry->ae_flags++ = NUL;
2705 aff_process_flags(aff, aff_entry);
2706 }
2707 }
2708
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002709 // Don't use an affix entry with non-ASCII characters when
2710 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002711 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2712 || has_non_ascii(aff_entry->ae_add)))
2713 {
2714 aff_entry->ae_next = cur_aff->ah_first;
2715 cur_aff->ah_first = aff_entry;
2716
2717 if (STRCMP(items[4], ".") != 0)
2718 {
2719 char_u buf[MAXLINELEN];
2720
2721 aff_entry->ae_cond = getroom_save(spin, items[4]);
2722 if (*items[0] == 'P')
2723 sprintf((char *)buf, "^%s", items[4]);
2724 else
2725 sprintf((char *)buf, "%s$", items[4]);
2726 aff_entry->ae_prog = vim_regcomp(buf,
2727 RE_MAGIC + RE_STRING + RE_STRICT);
2728 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002729 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002730 fname, lnum, items[4]);
2731 }
2732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 // For postponed prefixes we need an entry in si_prefcond
2734 // for the condition. Use an existing one if possible.
2735 // Can't be done for an affix with flags, ignoring
2736 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002737 if (*items[0] == 'P' && aff->af_pfxpostpone
2738 && aff_entry->ae_flags == NULL)
2739 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002740 // When the chop string is one lower-case letter and
2741 // the add string ends in the upper-case letter we set
2742 // the "upper" flag, clear "ae_chop" and remove the
2743 // letters from "ae_add". The condition must either
2744 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002745 if (aff_entry->ae_chop != NULL
2746 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002747 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002748 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002749 {
2750 int c, c_up;
2751
2752 c = PTR2CHAR(aff_entry->ae_chop);
2753 c_up = SPELL_TOUPPER(c);
2754 if (c_up != c
2755 && (aff_entry->ae_cond == NULL
2756 || PTR2CHAR(aff_entry->ae_cond) == c))
2757 {
2758 p = aff_entry->ae_add
2759 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002760 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002761 if (PTR2CHAR(p) == c_up)
2762 {
2763 upper = TRUE;
2764 aff_entry->ae_chop = NULL;
2765 *p = NUL;
2766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002767 // The condition is matched with the
2768 // actual word, thus must check for the
2769 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002770 if (aff_entry->ae_cond != NULL)
2771 {
2772 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002773
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002774 if (has_mbyte)
2775 {
2776 onecap_copy(items[4], buf, TRUE);
2777 aff_entry->ae_cond = getroom_save(
2778 spin, buf);
2779 }
2780 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002781 *aff_entry->ae_cond = c_up;
2782 if (aff_entry->ae_cond != NULL)
2783 {
2784 sprintf((char *)buf, "^%s",
2785 aff_entry->ae_cond);
2786 vim_regfree(aff_entry->ae_prog);
2787 aff_entry->ae_prog = vim_regcomp(
2788 buf, RE_MAGIC + RE_STRING);
2789 }
2790 }
2791 }
2792 }
2793 }
2794
2795 if (aff_entry->ae_chop == NULL
2796 && aff_entry->ae_flags == NULL)
2797 {
2798 int idx;
2799 char_u **pp;
2800 int n;
2801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002802 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002803 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2804 --idx)
2805 {
2806 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2807 if (str_equal(p, aff_entry->ae_cond))
2808 break;
2809 }
2810 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2811 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002812 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002813 idx = spin->si_prefcond.ga_len++;
2814 pp = ((char_u **)spin->si_prefcond.ga_data)
2815 + idx;
2816 if (aff_entry->ae_cond == NULL)
2817 *pp = NULL;
2818 else
2819 *pp = getroom_save(spin,
2820 aff_entry->ae_cond);
2821 }
2822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002823 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002824 if (aff_entry->ae_add == NULL)
2825 p = (char_u *)"";
2826 else
2827 p = aff_entry->ae_add;
2828
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002829 // PFX_FLAGS is a negative number, so that
2830 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002831 n = PFX_FLAGS;
2832 if (!cur_aff->ah_combine)
2833 n |= WFP_NC;
2834 if (upper)
2835 n |= WFP_UP;
2836 if (aff_entry->ae_comppermit)
2837 n |= WFP_COMPPERMIT;
2838 if (aff_entry->ae_compforbid)
2839 n |= WFP_COMPFORBID;
2840 tree_add_word(spin, p, spin->si_prefroot, n,
2841 idx, cur_aff->ah_newID);
2842 did_postpone_prefix = TRUE;
2843 }
2844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002845 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002846 if (aff_todo == 0 && !did_postpone_prefix)
2847 {
2848 --spin->si_newprefID;
2849 cur_aff->ah_newID = 0;
2850 }
2851 }
2852 }
2853 }
2854 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2855 {
2856 fol = vim_strsave(items[1]);
2857 }
2858 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2859 {
2860 low = vim_strsave(items[1]);
2861 }
2862 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2863 {
2864 upp = vim_strsave(items[1]);
2865 }
2866 else if (is_aff_rule(items, itemcnt, "REP", 2)
2867 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2868 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002869 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002870 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002871 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002872 fname, lnum);
2873 }
2874 else if ((STRCMP(items[0], "REP") == 0
2875 || STRCMP(items[0], "REPSAL") == 0)
2876 && itemcnt >= 3)
2877 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002878 // REP/REPSAL item
2879 // Myspell ignores extra arguments, we require it starts with
2880 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002881 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002882 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002883 if (items[0][3] == 'S' ? do_repsal : do_rep)
2884 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002885 // Replace underscore with space (can't include a space
2886 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002887 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002888 if (*p == '_')
2889 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002890 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002891 if (*p == '_')
2892 *p = ' ';
2893 add_fromto(spin, items[0][3] == 'S'
2894 ? &spin->si_repsal
2895 : &spin->si_rep, items[1], items[2]);
2896 }
2897 }
2898 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002900 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002901 if (!found_map)
2902 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002903 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002904 found_map = TRUE;
2905 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002907 fname, lnum);
2908 }
2909 else if (do_mapline)
2910 {
2911 int c;
2912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002913 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002914 for (p = items[1]; *p != NUL; )
2915 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002916 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002917 if ((spin->si_map.ga_len > 0
2918 && vim_strchr(spin->si_map.ga_data, c)
2919 != NULL)
2920 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002921 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002922 fname, lnum);
2923 }
2924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // We simply concatenate all the MAP strings, separated by
2926 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002927 ga_concat(&spin->si_map, items[1]);
2928 ga_append(&spin->si_map, '/');
2929 }
2930 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002931 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002932 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2933 {
2934 if (do_sal)
2935 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002936 // SAL item (sounds-a-like)
2937 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002938 if (STRCMP(items[1], "followup") == 0)
2939 spin->si_followup = sal_to_bool(items[2]);
2940 else if (STRCMP(items[1], "collapse_result") == 0)
2941 spin->si_collapse = sal_to_bool(items[2]);
2942 else if (STRCMP(items[1], "remove_accents") == 0)
2943 spin->si_rem_accents = sal_to_bool(items[2]);
2944 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002945 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002946 add_fromto(spin, &spin->si_sal, items[1],
2947 STRCMP(items[2], "_") == 0 ? (char_u *)""
2948 : items[2]);
2949 }
2950 }
2951 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2952 && sofofrom == NULL)
2953 {
2954 sofofrom = getroom_save(spin, items[1]);
2955 }
2956 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2957 && sofoto == NULL)
2958 {
2959 sofoto = getroom_save(spin, items[1]);
2960 }
2961 else if (STRCMP(items[0], "COMMON") == 0)
2962 {
2963 int i;
2964
2965 for (i = 1; i < itemcnt; ++i)
2966 {
2967 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2968 items[i])))
2969 {
2970 p = vim_strsave(items[i]);
2971 if (p == NULL)
2972 break;
2973 hash_add(&spin->si_commonwords, p);
2974 }
2975 }
2976 }
2977 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002978 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002979 fname, lnum, items[0]);
2980 }
2981 }
2982
2983 if (fol != NULL || low != NULL || upp != NULL)
2984 {
2985 if (spin->si_clear_chartab)
2986 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002987 // Clear the char type tables, don't want to use any of the
2988 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002989 init_spell_chartab();
2990 spin->si_clear_chartab = FALSE;
2991 }
2992
2993 /*
2994 * Don't write a word table for an ASCII file, so that we don't check
2995 * for conflicts with a word table that matches 'encoding'.
2996 * Don't write one for utf-8 either, we use utf_*() and
2997 * mb_get_class(), the list of chars in the file will be incomplete.
2998 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002999 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003000 {
3001 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003002 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003003 else
3004 (void)set_spell_chartab(fol, low, upp);
3005 }
3006
3007 vim_free(fol);
3008 vim_free(low);
3009 vim_free(upp);
3010 }
3011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003012 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003013 if (compmax != 0)
3014 {
3015 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3016 spin->si_compmax = compmax;
3017 }
3018
3019 if (compminlen != 0)
3020 {
3021 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3022 spin->si_compminlen = compminlen;
3023 }
3024
3025 if (compsylmax != 0)
3026 {
3027 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003028 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003029 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3030 spin->si_compsylmax = compsylmax;
3031 }
3032
3033 if (compoptions != 0)
3034 {
3035 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3036 spin->si_compoptions |= compoptions;
3037 }
3038
3039 if (compflags != NULL)
3040 process_compflags(spin, aff, compflags);
3041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003042 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003043 if (spin->si_newcompID < spin->si_newprefID)
3044 {
3045 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003046 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003047 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003048 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003049 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003050 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003051 }
3052
3053 if (syllable != NULL)
3054 {
3055 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3056 spin->si_syllable = syllable;
3057 }
3058
3059 if (sofofrom != NULL || sofoto != NULL)
3060 {
3061 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003063 sofofrom == NULL ? "FROM" : "TO", fname);
3064 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003065 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003066 else
3067 {
3068 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3069 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3070 spin->si_sofofr = sofofrom;
3071 spin->si_sofoto = sofoto;
3072 }
3073 }
3074
3075 if (midword != NULL)
3076 {
3077 aff_check_string(spin->si_midword, midword, "MIDWORD");
3078 spin->si_midword = midword;
3079 }
3080
3081 vim_free(pc);
3082 fclose(fd);
3083 return aff;
3084}
3085
3086/*
3087 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3088 * a comment is following after item "mincount".
3089 */
3090 static int
3091is_aff_rule(
3092 char_u **items,
3093 int itemcnt,
3094 char *rulename,
3095 int mincount)
3096{
3097 return (STRCMP(items[0], rulename) == 0
3098 && (itemcnt == mincount
3099 || (itemcnt > mincount && items[mincount][0] == '#')));
3100}
3101
3102/*
3103 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3104 * ae_flags to ae_comppermit and ae_compforbid.
3105 */
3106 static void
3107aff_process_flags(afffile_T *affile, affentry_T *entry)
3108{
3109 char_u *p;
3110 char_u *prevp;
3111 unsigned flag;
3112
3113 if (entry->ae_flags != NULL
3114 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3115 {
3116 for (p = entry->ae_flags; *p != NUL; )
3117 {
3118 prevp = p;
3119 flag = get_affitem(affile->af_flagtype, &p);
3120 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3121 {
3122 STRMOVE(prevp, p);
3123 p = prevp;
3124 if (flag == affile->af_comppermit)
3125 entry->ae_comppermit = TRUE;
3126 else
3127 entry->ae_compforbid = TRUE;
3128 }
3129 if (affile->af_flagtype == AFT_NUM && *p == ',')
3130 ++p;
3131 }
3132 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003133 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003134 }
3135}
3136
3137/*
3138 * Return TRUE if "s" is the name of an info item in the affix file.
3139 */
3140 static int
3141spell_info_item(char_u *s)
3142{
3143 return STRCMP(s, "NAME") == 0
3144 || STRCMP(s, "HOME") == 0
3145 || STRCMP(s, "VERSION") == 0
3146 || STRCMP(s, "AUTHOR") == 0
3147 || STRCMP(s, "EMAIL") == 0
3148 || STRCMP(s, "COPYRIGHT") == 0;
3149}
3150
3151/*
3152 * Turn an affix flag name into a number, according to the FLAG type.
3153 * returns zero for failure.
3154 */
3155 static unsigned
3156affitem2flag(
3157 int flagtype,
3158 char_u *item,
3159 char_u *fname,
3160 int lnum)
3161{
3162 unsigned res;
3163 char_u *p = item;
3164
3165 res = get_affitem(flagtype, &p);
3166 if (res == 0)
3167 {
3168 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003169 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003170 fname, lnum, item);
3171 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003172 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003173 fname, lnum, item);
3174 }
3175 if (*p != NUL)
3176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003177 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003178 return 0;
3179 }
3180
3181 return res;
3182}
3183
3184/*
3185 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003186 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003187 * Returns zero for an error, still advances the pointer then.
3188 */
3189 static unsigned
3190get_affitem(int flagtype, char_u **pp)
3191{
3192 int res;
3193
3194 if (flagtype == AFT_NUM)
3195 {
3196 if (!VIM_ISDIGIT(**pp))
3197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003198 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003199 return 0;
3200 }
3201 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003202 if (res == 0)
3203 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003204 }
3205 else
3206 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003207 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003208 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3209 && res >= 'A' && res <= 'Z'))
3210 {
3211 if (**pp == NUL)
3212 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003213 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003214 }
3215 }
3216 return res;
3217}
3218
3219/*
3220 * Process the "compflags" string used in an affix file and append it to
3221 * spin->si_compflags.
3222 * The processing involves changing the affix names to ID numbers, so that
3223 * they fit in one byte.
3224 */
3225 static void
3226process_compflags(
3227 spellinfo_T *spin,
3228 afffile_T *aff,
3229 char_u *compflags)
3230{
3231 char_u *p;
3232 char_u *prevp;
3233 unsigned flag;
3234 compitem_T *ci;
3235 int id;
3236 int len;
3237 char_u *tp;
3238 char_u key[AH_KEY_LEN];
3239 hashitem_T *hi;
3240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003241 // Make room for the old and the new compflags, concatenated with a / in
3242 // between. Processing it makes it shorter, but we don't know by how
3243 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003244 len = (int)STRLEN(compflags) + 1;
3245 if (spin->si_compflags != NULL)
3246 len += (int)STRLEN(spin->si_compflags) + 1;
3247 p = getroom(spin, len, FALSE);
3248 if (p == NULL)
3249 return;
3250 if (spin->si_compflags != NULL)
3251 {
3252 STRCPY(p, spin->si_compflags);
3253 STRCAT(p, "/");
3254 }
3255 spin->si_compflags = p;
3256 tp = p + STRLEN(p);
3257
3258 for (p = compflags; *p != NUL; )
3259 {
3260 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003261 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003262 *tp++ = *p++;
3263 else
3264 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003265 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003266 prevp = p;
3267 flag = get_affitem(aff->af_flagtype, &p);
3268 if (flag != 0)
3269 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003270 // Find the flag in the hashtable. If it was used before, use
3271 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003272 vim_strncpy(key, prevp, p - prevp);
3273 hi = hash_find(&aff->af_comp, key);
3274 if (!HASHITEM_EMPTY(hi))
3275 id = HI2CI(hi)->ci_newID;
3276 else
3277 {
3278 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3279 if (ci == NULL)
3280 break;
3281 STRCPY(ci->ci_key, key);
3282 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003283 // Avoid using a flag ID that has a special meaning in a
3284 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003285 do
3286 {
3287 check_renumber(spin);
3288 id = spin->si_newcompID--;
3289 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3290 ci->ci_newID = id;
3291 hash_add(&aff->af_comp, ci->ci_key);
3292 }
3293 *tp++ = id;
3294 }
3295 if (aff->af_flagtype == AFT_NUM && *p == ',')
3296 ++p;
3297 }
3298 }
3299
3300 *tp = NUL;
3301}
3302
3303/*
3304 * Check that the new IDs for postponed affixes and compounding don't overrun
3305 * each other. We have almost 255 available, but start at 0-127 to avoid
3306 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3307 * When that is used up an error message is given.
3308 */
3309 static void
3310check_renumber(spellinfo_T *spin)
3311{
3312 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3313 {
3314 spin->si_newprefID = 127;
3315 spin->si_newcompID = 255;
3316 }
3317}
3318
3319/*
3320 * Return TRUE if flag "flag" appears in affix list "afflist".
3321 */
3322 static int
3323flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3324{
3325 char_u *p;
3326 unsigned n;
3327
3328 switch (flagtype)
3329 {
3330 case AFT_CHAR:
3331 return vim_strchr(afflist, flag) != NULL;
3332
3333 case AFT_CAPLONG:
3334 case AFT_LONG:
3335 for (p = afflist; *p != NUL; )
3336 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003337 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003338 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3339 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003340 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003341 if (n == flag)
3342 return TRUE;
3343 }
3344 break;
3345
3346 case AFT_NUM:
3347 for (p = afflist; *p != NUL; )
3348 {
3349 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003350 if (n == 0)
3351 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003352 if (n == flag)
3353 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003354 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003355 ++p;
3356 }
3357 break;
3358 }
3359 return FALSE;
3360}
3361
3362/*
3363 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3364 */
3365 static void
3366aff_check_number(int spinval, int affval, char *name)
3367{
3368 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003369 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003370}
3371
3372/*
3373 * Give a warning when "spinval" and "affval" strings are set and not the same.
3374 */
3375 static void
3376aff_check_string(char_u *spinval, char_u *affval, char *name)
3377{
3378 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003379 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003380}
3381
3382/*
3383 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3384 * NULL as equal.
3385 */
3386 static int
3387str_equal(char_u *s1, char_u *s2)
3388{
3389 if (s1 == NULL || s2 == NULL)
3390 return s1 == s2;
3391 return STRCMP(s1, s2) == 0;
3392}
3393
3394/*
3395 * Add a from-to item to "gap". Used for REP and SAL items.
3396 * They are stored case-folded.
3397 */
3398 static void
3399add_fromto(
3400 spellinfo_T *spin,
3401 garray_T *gap,
3402 char_u *from,
3403 char_u *to)
3404{
3405 fromto_T *ftp;
3406 char_u word[MAXWLEN];
3407
3408 if (ga_grow(gap, 1) == OK)
3409 {
3410 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3411 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
3412 ftp->ft_from = getroom_save(spin, word);
3413 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
3414 ftp->ft_to = getroom_save(spin, word);
3415 ++gap->ga_len;
3416 }
3417}
3418
3419/*
3420 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3421 */
3422 static int
3423sal_to_bool(char_u *s)
3424{
3425 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3426}
3427
3428/*
3429 * Free the structure filled by spell_read_aff().
3430 */
3431 static void
3432spell_free_aff(afffile_T *aff)
3433{
3434 hashtab_T *ht;
3435 hashitem_T *hi;
3436 int todo;
3437 affheader_T *ah;
3438 affentry_T *ae;
3439
3440 vim_free(aff->af_enc);
3441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003442 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003443 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3444 {
3445 todo = (int)ht->ht_used;
3446 for (hi = ht->ht_array; todo > 0; ++hi)
3447 {
3448 if (!HASHITEM_EMPTY(hi))
3449 {
3450 --todo;
3451 ah = HI2AH(hi);
3452 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3453 vim_regfree(ae->ae_prog);
3454 }
3455 }
3456 if (ht == &aff->af_suff)
3457 break;
3458 }
3459
3460 hash_clear(&aff->af_pref);
3461 hash_clear(&aff->af_suff);
3462 hash_clear(&aff->af_comp);
3463}
3464
3465/*
3466 * Read dictionary file "fname".
3467 * Returns OK or FAIL;
3468 */
3469 static int
3470spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3471{
3472 hashtab_T ht;
3473 char_u line[MAXLINELEN];
3474 char_u *p;
3475 char_u *afflist;
3476 char_u store_afflist[MAXWLEN];
3477 int pfxlen;
3478 int need_affix;
3479 char_u *dw;
3480 char_u *pc;
3481 char_u *w;
3482 int l;
3483 hash_T hash;
3484 hashitem_T *hi;
3485 FILE *fd;
3486 int lnum = 1;
3487 int non_ascii = 0;
3488 int retval = OK;
3489 char_u message[MAXLINELEN + MAXWLEN];
3490 int flags;
3491 int duplicate = 0;
3492
3493 /*
3494 * Open the file.
3495 */
3496 fd = mch_fopen((char *)fname, "r");
3497 if (fd == NULL)
3498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003499 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003500 return FAIL;
3501 }
3502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003503 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003504 hash_init(&ht);
3505
3506 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003507 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003508 spell_message(spin, IObuff);
3509
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003510 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003511 spin->si_msg_count = 999999;
3512
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003513 // Read and ignore the first line: word count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003514 (void)vim_fgets(line, MAXLINELEN, fd);
3515 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003516 semsg(_("E760: No word count in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003517
3518 /*
3519 * Read all the lines in the file one by one.
3520 * The words are converted to 'encoding' here, before being added to
3521 * the hashtable.
3522 */
3523 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3524 {
3525 line_breakcheck();
3526 ++lnum;
3527 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003528 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003530 // Remove CR, LF and white space from the end. White space halfway
3531 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003532 l = (int)STRLEN(line);
3533 while (l > 0 && line[l - 1] <= ' ')
3534 --l;
3535 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003536 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003537 line[l] = NUL;
3538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003539 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003540 if (spin->si_conv.vc_type != CONV_NONE)
3541 {
3542 pc = string_convert(&spin->si_conv, line, NULL);
3543 if (pc == NULL)
3544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003545 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003546 fname, lnum, line);
3547 continue;
3548 }
3549 w = pc;
3550 }
3551 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003552 {
3553 pc = NULL;
3554 w = line;
3555 }
3556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003557 // Truncate the word at the "/", set "afflist" to what follows.
3558 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003559 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003560 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003561 {
3562 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3563 STRMOVE(p, p + 1);
3564 else if (*p == '/')
3565 {
3566 *p = NUL;
3567 afflist = p + 1;
3568 break;
3569 }
3570 }
3571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003572 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003573 if (spin->si_ascii && has_non_ascii(w))
3574 {
3575 ++non_ascii;
3576 vim_free(pc);
3577 continue;
3578 }
3579
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003580 // This takes time, print a message every 10000 words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003581 if (spin->si_verbose && spin->si_msg_count > 10000)
3582 {
3583 spin->si_msg_count = 0;
3584 vim_snprintf((char *)message, sizeof(message),
Bram Moolenaarea391762018-04-08 13:07:22 +02003585 _("line %6d, word %6ld - %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003586 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3587 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01003588 msg_outtrans_long_attr(message, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003589 msg_clr_eos();
3590 msg_didout = FALSE;
3591 msg_col = 0;
3592 out_flush();
3593 }
3594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003595 // Store the word in the hashtable to be able to find duplicates.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003596 dw = (char_u *)getroom_save(spin, w);
3597 if (dw == NULL)
3598 {
3599 retval = FAIL;
3600 vim_free(pc);
3601 break;
3602 }
3603
3604 hash = hash_hash(dw);
3605 hi = hash_lookup(&ht, dw, hash);
3606 if (!HASHITEM_EMPTY(hi))
3607 {
3608 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003609 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003610 fname, lnum, dw);
3611 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003612 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003613 fname, lnum, dw);
3614 ++duplicate;
3615 }
3616 else
3617 hash_add_item(&ht, hi, dw, hash);
3618
3619 flags = 0;
3620 store_afflist[0] = NUL;
3621 pfxlen = 0;
3622 need_affix = FALSE;
3623 if (afflist != NULL)
3624 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003625 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003626 flags |= get_affix_flags(affile, afflist);
3627
3628 if (affile->af_needaffix != 0 && flag_in_afflist(
3629 affile->af_flagtype, afflist, affile->af_needaffix))
3630 need_affix = TRUE;
3631
3632 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003633 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003634 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3635
3636 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003637 // Need to store the list of compound flags with the word.
3638 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003639 get_compflags(affile, afflist, store_afflist + pfxlen);
3640 }
3641
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003643 if (store_word(spin, dw, flags, spin->si_region,
3644 store_afflist, need_affix) == FAIL)
3645 retval = FAIL;
3646
3647 if (afflist != NULL)
3648 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003649 // Find all matching suffixes and add the resulting words.
3650 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003651 if (store_aff_word(spin, dw, afflist, affile,
3652 &affile->af_suff, &affile->af_pref,
3653 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3654 retval = FAIL;
3655
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003656 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003657 if (store_aff_word(spin, dw, afflist, affile,
3658 &affile->af_pref, NULL,
3659 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3660 retval = FAIL;
3661 }
3662
3663 vim_free(pc);
3664 }
3665
3666 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003667 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003668 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003669 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003670 non_ascii, fname);
3671 hash_clear(&ht);
3672
3673 fclose(fd);
3674 return retval;
3675}
3676
3677/*
3678 * Check for affix flags in "afflist" that are turned into word flags.
3679 * Return WF_ flags.
3680 */
3681 static int
3682get_affix_flags(afffile_T *affile, char_u *afflist)
3683{
3684 int flags = 0;
3685
3686 if (affile->af_keepcase != 0 && flag_in_afflist(
3687 affile->af_flagtype, afflist, affile->af_keepcase))
3688 flags |= WF_KEEPCAP | WF_FIXCAP;
3689 if (affile->af_rare != 0 && flag_in_afflist(
3690 affile->af_flagtype, afflist, affile->af_rare))
3691 flags |= WF_RARE;
3692 if (affile->af_bad != 0 && flag_in_afflist(
3693 affile->af_flagtype, afflist, affile->af_bad))
3694 flags |= WF_BANNED;
3695 if (affile->af_needcomp != 0 && flag_in_afflist(
3696 affile->af_flagtype, afflist, affile->af_needcomp))
3697 flags |= WF_NEEDCOMP;
3698 if (affile->af_comproot != 0 && flag_in_afflist(
3699 affile->af_flagtype, afflist, affile->af_comproot))
3700 flags |= WF_COMPROOT;
3701 if (affile->af_nosuggest != 0 && flag_in_afflist(
3702 affile->af_flagtype, afflist, affile->af_nosuggest))
3703 flags |= WF_NOSUGGEST;
3704 return flags;
3705}
3706
3707/*
3708 * Get the list of prefix IDs from the affix list "afflist".
3709 * Used for PFXPOSTPONE.
3710 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3711 * and return the number of affixes.
3712 */
3713 static int
3714get_pfxlist(
3715 afffile_T *affile,
3716 char_u *afflist,
3717 char_u *store_afflist)
3718{
3719 char_u *p;
3720 char_u *prevp;
3721 int cnt = 0;
3722 int id;
3723 char_u key[AH_KEY_LEN];
3724 hashitem_T *hi;
3725
3726 for (p = afflist; *p != NUL; )
3727 {
3728 prevp = p;
3729 if (get_affitem(affile->af_flagtype, &p) != 0)
3730 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003731 // A flag is a postponed prefix flag if it appears in "af_pref"
3732 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003733 vim_strncpy(key, prevp, p - prevp);
3734 hi = hash_find(&affile->af_pref, key);
3735 if (!HASHITEM_EMPTY(hi))
3736 {
3737 id = HI2AH(hi)->ah_newID;
3738 if (id != 0)
3739 store_afflist[cnt++] = id;
3740 }
3741 }
3742 if (affile->af_flagtype == AFT_NUM && *p == ',')
3743 ++p;
3744 }
3745
3746 store_afflist[cnt] = NUL;
3747 return cnt;
3748}
3749
3750/*
3751 * Get the list of compound IDs from the affix list "afflist" that are used
3752 * for compound words.
3753 * Puts the flags in "store_afflist[]".
3754 */
3755 static void
3756get_compflags(
3757 afffile_T *affile,
3758 char_u *afflist,
3759 char_u *store_afflist)
3760{
3761 char_u *p;
3762 char_u *prevp;
3763 int cnt = 0;
3764 char_u key[AH_KEY_LEN];
3765 hashitem_T *hi;
3766
3767 for (p = afflist; *p != NUL; )
3768 {
3769 prevp = p;
3770 if (get_affitem(affile->af_flagtype, &p) != 0)
3771 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003772 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003773 vim_strncpy(key, prevp, p - prevp);
3774 hi = hash_find(&affile->af_comp, key);
3775 if (!HASHITEM_EMPTY(hi))
3776 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3777 }
3778 if (affile->af_flagtype == AFT_NUM && *p == ',')
3779 ++p;
3780 }
3781
3782 store_afflist[cnt] = NUL;
3783}
3784
3785/*
3786 * Apply affixes to a word and store the resulting words.
3787 * "ht" is the hashtable with affentry_T that need to be applied, either
3788 * prefixes or suffixes.
3789 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3790 * the resulting words for combining affixes.
3791 *
3792 * Returns FAIL when out of memory.
3793 */
3794 static int
3795store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003796 spellinfo_T *spin, // spell info
3797 char_u *word, // basic word start
3798 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003799 afffile_T *affile,
3800 hashtab_T *ht,
3801 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003802 int condit, // CONDIT_SUF et al.
3803 int flags, // flags for the word
3804 char_u *pfxlist, // list of prefix IDs
3805 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3806 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003807{
3808 int todo;
3809 hashitem_T *hi;
3810 affheader_T *ah;
3811 affentry_T *ae;
3812 char_u newword[MAXWLEN];
3813 int retval = OK;
3814 int i, j;
3815 char_u *p;
3816 int use_flags;
3817 char_u *use_pfxlist;
3818 int use_pfxlen;
3819 int need_affix;
3820 char_u store_afflist[MAXWLEN];
3821 char_u pfx_pfxlist[MAXWLEN];
3822 size_t wordlen = STRLEN(word);
3823 int use_condit;
3824
3825 todo = (int)ht->ht_used;
3826 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3827 {
3828 if (!HASHITEM_EMPTY(hi))
3829 {
3830 --todo;
3831 ah = HI2AH(hi);
3832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003833 // Check that the affix combines, if required, and that the word
3834 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003835 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3836 && flag_in_afflist(affile->af_flagtype, afflist,
3837 ah->ah_flag))
3838 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003839 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003840 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3841 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003842 // Check the condition. It's not logical to match case
3843 // here, but it is required for compatibility with
3844 // Myspell.
3845 // Another requirement from Myspell is that the chop
3846 // string is shorter than the word itself.
3847 // For prefixes, when "PFXPOSTPONE" was used, only do
3848 // prefixes with a chop string and/or flags.
3849 // When a previously added affix had CIRCUMFIX this one
3850 // must have it too, if it had not then this one must not
3851 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003852 if ((xht != NULL || !affile->af_pfxpostpone
3853 || ae->ae_chop != NULL
3854 || ae->ae_flags != NULL)
3855 && (ae->ae_chop == NULL
3856 || STRLEN(ae->ae_chop) < wordlen)
3857 && (ae->ae_prog == NULL
3858 || vim_regexec_prog(&ae->ae_prog, FALSE,
3859 word, (colnr_T)0))
3860 && (((condit & CONDIT_CFIX) == 0)
3861 == ((condit & CONDIT_AFF) == 0
3862 || ae->ae_flags == NULL
3863 || !flag_in_afflist(affile->af_flagtype,
3864 ae->ae_flags, affile->af_circumfix))))
3865 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003866 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003867 if (xht == NULL)
3868 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003869 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003870 if (ae->ae_add == NULL)
3871 *newword = NUL;
3872 else
3873 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3874 p = word;
3875 if (ae->ae_chop != NULL)
3876 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003877 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003878 if (has_mbyte)
3879 {
3880 i = mb_charlen(ae->ae_chop);
3881 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003882 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003883 }
3884 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003885 p += STRLEN(ae->ae_chop);
3886 }
3887 STRCAT(newword, p);
3888 }
3889 else
3890 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003891 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003892 vim_strncpy(newword, word, MAXWLEN - 1);
3893 if (ae->ae_chop != NULL)
3894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003895 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003896 p = newword + STRLEN(newword);
3897 i = (int)MB_CHARLEN(ae->ae_chop);
3898 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003899 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003900 *p = NUL;
3901 }
3902 if (ae->ae_add != NULL)
3903 STRCAT(newword, ae->ae_add);
3904 }
3905
3906 use_flags = flags;
3907 use_pfxlist = pfxlist;
3908 use_pfxlen = pfxlen;
3909 need_affix = FALSE;
3910 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3911 if (ae->ae_flags != NULL)
3912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003913 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003914 use_flags |= get_affix_flags(affile, ae->ae_flags);
3915
3916 if (affile->af_needaffix != 0 && flag_in_afflist(
3917 affile->af_flagtype, ae->ae_flags,
3918 affile->af_needaffix))
3919 need_affix = TRUE;
3920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003921 // When there is a CIRCUMFIX flag the other affix
3922 // must also have it and we don't add the word
3923 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003924 if (affile->af_circumfix != 0 && flag_in_afflist(
3925 affile->af_flagtype, ae->ae_flags,
3926 affile->af_circumfix))
3927 {
3928 use_condit |= CONDIT_CFIX;
3929 if ((condit & CONDIT_CFIX) == 0)
3930 need_affix = TRUE;
3931 }
3932
3933 if (affile->af_pfxpostpone
3934 || spin->si_compflags != NULL)
3935 {
3936 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003937 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003938 use_pfxlen = get_pfxlist(affile,
3939 ae->ae_flags, store_afflist);
3940 else
3941 use_pfxlen = 0;
3942 use_pfxlist = store_afflist;
3943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003944 // Combine the prefix IDs. Avoid adding the
3945 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003946 for (i = 0; i < pfxlen; ++i)
3947 {
3948 for (j = 0; j < use_pfxlen; ++j)
3949 if (pfxlist[i] == use_pfxlist[j])
3950 break;
3951 if (j == use_pfxlen)
3952 use_pfxlist[use_pfxlen++] = pfxlist[i];
3953 }
3954
3955 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003956 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003957 get_compflags(affile, ae->ae_flags,
3958 use_pfxlist + use_pfxlen);
3959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003960 // Combine the list of compound flags.
3961 // Concatenate them to the prefix IDs list.
3962 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003963 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3964 {
3965 for (j = use_pfxlen;
3966 use_pfxlist[j] != NUL; ++j)
3967 if (pfxlist[i] == use_pfxlist[j])
3968 break;
3969 if (use_pfxlist[j] == NUL)
3970 {
3971 use_pfxlist[j++] = pfxlist[i];
3972 use_pfxlist[j] = NUL;
3973 }
3974 }
3975 }
3976 }
3977
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003978 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
3979 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003980 if (use_pfxlist != NULL && ae->ae_compforbid)
3981 {
3982 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
3983 use_pfxlist = pfx_pfxlist;
3984 }
3985
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003986 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003987 if (spin->si_prefroot != NULL
3988 && spin->si_prefroot->wn_sibling != NULL)
3989 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003990 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003991 use_flags |= WF_HAS_AFF;
3992
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003993 // ... don't use a prefix list if combining
3994 // affixes is not allowed. But do use the
3995 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003996 if (!ah->ah_combine && use_pfxlist != NULL)
3997 use_pfxlist += use_pfxlen;
3998 }
3999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004000 // When compounding is supported and there is no
4001 // "COMPOUNDPERMITFLAG" then forbid compounding on the
4002 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004003 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4004 {
4005 if (xht != NULL)
4006 use_flags |= WF_NOCOMPAFT;
4007 else
4008 use_flags |= WF_NOCOMPBEF;
4009 }
4010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004011 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004012 if (store_word(spin, newword, use_flags,
4013 spin->si_region, use_pfxlist,
4014 need_affix) == FAIL)
4015 retval = FAIL;
4016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004017 // When added a prefix or a first suffix and the affix
4018 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004019 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4020 if (store_aff_word(spin, newword, ae->ae_flags,
4021 affile, &affile->af_suff, xht,
4022 use_condit & (xht == NULL
4023 ? ~0 : ~CONDIT_SUF),
4024 use_flags, use_pfxlist, pfxlen) == FAIL)
4025 retval = FAIL;
4026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004027 // When added a suffix and combining is allowed also
4028 // try adding a prefix additionally. Both for the
4029 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004030 if (xht != NULL && ah->ah_combine)
4031 {
4032 if (store_aff_word(spin, newword,
4033 afflist, affile,
4034 xht, NULL, use_condit,
4035 use_flags, use_pfxlist,
4036 pfxlen) == FAIL
4037 || (ae->ae_flags != NULL
4038 && store_aff_word(spin, newword,
4039 ae->ae_flags, affile,
4040 xht, NULL, use_condit,
4041 use_flags, use_pfxlist,
4042 pfxlen) == FAIL))
4043 retval = FAIL;
4044 }
4045 }
4046 }
4047 }
4048 }
4049 }
4050
4051 return retval;
4052}
4053
4054/*
4055 * Read a file with a list of words.
4056 */
4057 static int
4058spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4059{
4060 FILE *fd;
4061 long lnum = 0;
4062 char_u rline[MAXLINELEN];
4063 char_u *line;
4064 char_u *pc = NULL;
4065 char_u *p;
4066 int l;
4067 int retval = OK;
4068 int did_word = FALSE;
4069 int non_ascii = 0;
4070 int flags;
4071 int regionmask;
4072
4073 /*
4074 * Open the file.
4075 */
4076 fd = mch_fopen((char *)fname, "r");
4077 if (fd == NULL)
4078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004079 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004080 return FAIL;
4081 }
4082
Bram Moolenaarc1669272018-06-19 14:23:53 +02004083 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004084 spell_message(spin, IObuff);
4085
4086 /*
4087 * Read all the lines in the file one by one.
4088 */
4089 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4090 {
4091 line_breakcheck();
4092 ++lnum;
4093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004094 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004095 if (*rline == '#')
4096 continue;
4097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004098 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004099 l = (int)STRLEN(rline);
4100 while (l > 0 && rline[l - 1] <= ' ')
4101 --l;
4102 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004103 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004104 rline[l] = NUL;
4105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004106 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004107 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004108 if (spin->si_conv.vc_type != CONV_NONE)
4109 {
4110 pc = string_convert(&spin->si_conv, rline, NULL);
4111 if (pc == NULL)
4112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004113 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004114 fname, lnum, rline);
4115 continue;
4116 }
4117 line = pc;
4118 }
4119 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004120 {
4121 pc = NULL;
4122 line = rline;
4123 }
4124
4125 if (*line == '/')
4126 {
4127 ++line;
4128 if (STRNCMP(line, "encoding=", 9) == 0)
4129 {
4130 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004131 smsg(_("Duplicate /encoding= line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004132 fname, lnum, line - 1);
4133 else if (did_word)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004134 smsg(_("/encoding= line after word ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004135 fname, lnum, line - 1);
4136 else
4137 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004138 char_u *enc;
4139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004140 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004141 line += 9;
4142 enc = enc_canonize(line);
4143 if (enc != NULL && !spin->si_ascii
4144 && convert_setup(&spin->si_conv, enc,
4145 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004146 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004147 fname, line, p_enc);
4148 vim_free(enc);
4149 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004150 }
4151 continue;
4152 }
4153
4154 if (STRNCMP(line, "regions=", 8) == 0)
4155 {
4156 if (spin->si_region_count > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004157 smsg(_("Duplicate /regions= line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004158 fname, lnum, line);
4159 else
4160 {
4161 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004162 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004163 smsg(_("Too many regions in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004164 fname, lnum, line);
4165 else
4166 {
4167 spin->si_region_count = (int)STRLEN(line) / 2;
4168 STRCPY(spin->si_region_name, line);
4169
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004170 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004171 spin->si_region = (1 << spin->si_region_count) - 1;
4172 }
4173 }
4174 continue;
4175 }
4176
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004177 smsg(_("/ line ignored in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004178 fname, lnum, line - 1);
4179 continue;
4180 }
4181
4182 flags = 0;
4183 regionmask = spin->si_region;
4184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004185 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004186 p = vim_strchr(line, '/');
4187 if (p != NULL)
4188 {
4189 *p++ = NUL;
4190 while (*p != NUL)
4191 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004192 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004193 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004194 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004195 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004196 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004197 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004198 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004199 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004200 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004201 regionmask = 0;
4202 flags |= WF_REGION;
4203
4204 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004205 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004207 smsg(_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004208 fname, lnum, p);
4209 break;
4210 }
4211 regionmask |= 1 << (l - 1);
4212 }
4213 else
4214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004215 smsg(_("Unrecognized flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004216 fname, lnum, p);
4217 break;
4218 }
4219 ++p;
4220 }
4221 }
4222
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004224 if (spin->si_ascii && has_non_ascii(line))
4225 {
4226 ++non_ascii;
4227 continue;
4228 }
4229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004230 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004231 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4232 {
4233 retval = FAIL;
4234 break;
4235 }
4236 did_word = TRUE;
4237 }
4238
4239 vim_free(pc);
4240 fclose(fd);
4241
4242 if (spin->si_ascii && non_ascii > 0)
4243 {
4244 vim_snprintf((char *)IObuff, IOSIZE,
4245 _("Ignored %d words with non-ASCII characters"), non_ascii);
4246 spell_message(spin, IObuff);
4247 }
4248
4249 return retval;
4250}
4251
4252/*
4253 * Get part of an sblock_T, "len" bytes long.
4254 * This avoids calling free() for every little struct we use (and keeping
4255 * track of them).
4256 * The memory is cleared to all zeros.
4257 * Returns NULL when out of memory.
4258 */
4259 static void *
4260getroom(
4261 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004262 size_t len, // length needed
4263 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004264{
4265 char_u *p;
4266 sblock_T *bl = spin->si_blocks;
4267
4268 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004269 // Round size up for alignment. On some systems structures need to be
4270 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004271 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4272 & ~(sizeof(char *) - 1);
4273
4274 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4275 {
4276 if (len >= SBLOCKSIZE)
4277 bl = NULL;
4278 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004279 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004280 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004281 if (bl == NULL)
4282 {
4283 if (!spin->si_did_emsg)
4284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004285 emsg(_("E845: Insufficient memory, word list will be incomplete"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004286 spin->si_did_emsg = TRUE;
4287 }
4288 return NULL;
4289 }
4290 bl->sb_next = spin->si_blocks;
4291 spin->si_blocks = bl;
4292 bl->sb_used = 0;
4293 ++spin->si_blocks_cnt;
4294 }
4295
4296 p = bl->sb_data + bl->sb_used;
4297 bl->sb_used += (int)len;
4298
4299 return p;
4300}
4301
4302/*
4303 * Make a copy of a string into memory allocated with getroom().
4304 * Returns NULL when out of memory.
4305 */
4306 static char_u *
4307getroom_save(spellinfo_T *spin, char_u *s)
4308{
4309 char_u *sc;
4310
4311 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4312 if (sc != NULL)
4313 STRCPY(sc, s);
4314 return sc;
4315}
4316
4317
4318/*
4319 * Free the list of allocated sblock_T.
4320 */
4321 static void
4322free_blocks(sblock_T *bl)
4323{
4324 sblock_T *next;
4325
4326 while (bl != NULL)
4327 {
4328 next = bl->sb_next;
4329 vim_free(bl);
4330 bl = next;
4331 }
4332}
4333
4334/*
4335 * Allocate the root of a word tree.
4336 * Returns NULL when out of memory.
4337 */
4338 static wordnode_T *
4339wordtree_alloc(spellinfo_T *spin)
4340{
4341 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4342}
4343
4344/*
4345 * Store a word in the tree(s).
4346 * Always store it in the case-folded tree. For a keep-case word this is
4347 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4348 * used to find suggestions.
4349 * For a keep-case word also store it in the keep-case tree.
4350 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4351 * compound flag.
4352 */
4353 static int
4354store_word(
4355 spellinfo_T *spin,
4356 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004357 int flags, // extra flags, WF_BANNED
4358 int region, // supported region(s)
4359 char_u *pfxlist, // list of prefix IDs or NULL
4360 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004361{
4362 int len = (int)STRLEN(word);
4363 int ct = captype(word, word + len);
4364 char_u foldword[MAXWLEN];
4365 int res = OK;
4366 char_u *p;
4367
4368 (void)spell_casefold(word, len, foldword, MAXWLEN);
4369 for (p = pfxlist; res == OK; ++p)
4370 {
4371 if (!need_affix || (p != NULL && *p != NUL))
4372 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4373 region, p == NULL ? 0 : *p);
4374 if (p == NULL || *p == NUL)
4375 break;
4376 }
4377 ++spin->si_foldwcount;
4378
4379 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4380 {
4381 for (p = pfxlist; res == OK; ++p)
4382 {
4383 if (!need_affix || (p != NULL && *p != NUL))
4384 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4385 region, p == NULL ? 0 : *p);
4386 if (p == NULL || *p == NUL)
4387 break;
4388 }
4389 ++spin->si_keepwcount;
4390 }
4391 return res;
4392}
4393
4394/*
4395 * Add word "word" to a word tree at "root".
4396 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4397 * "rare" and "region" is the condition nr.
4398 * Returns FAIL when out of memory.
4399 */
4400 static int
4401tree_add_word(
4402 spellinfo_T *spin,
4403 char_u *word,
4404 wordnode_T *root,
4405 int flags,
4406 int region,
4407 int affixID)
4408{
4409 wordnode_T *node = root;
4410 wordnode_T *np;
4411 wordnode_T *copyp, **copyprev;
4412 wordnode_T **prev = NULL;
4413 int i;
4414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004415 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004416 for (i = 0; ; ++i)
4417 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004418 // When there is more than one reference to this node we need to make
4419 // a copy, so that we can modify it. Copy the whole list of siblings
4420 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004421 if (node != NULL && node->wn_refs > 1)
4422 {
4423 --node->wn_refs;
4424 copyprev = prev;
4425 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4426 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004427 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004428 np = get_wordnode(spin);
4429 if (np == NULL)
4430 return FAIL;
4431 np->wn_child = copyp->wn_child;
4432 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004433 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004434 np->wn_byte = copyp->wn_byte;
4435 if (np->wn_byte == NUL)
4436 {
4437 np->wn_flags = copyp->wn_flags;
4438 np->wn_region = copyp->wn_region;
4439 np->wn_affixID = copyp->wn_affixID;
4440 }
4441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004442 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004443 np->wn_refs = 1;
4444 if (copyprev != NULL)
4445 *copyprev = np;
4446 copyprev = &np->wn_sibling;
4447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004448 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004449 if (copyp == node)
4450 node = np;
4451 }
4452 }
4453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004454 // Look for the sibling that has the same character. They are sorted
4455 // on byte value, thus stop searching when a sibling is found with a
4456 // higher byte value. For zero bytes (end of word) the sorting is
4457 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004458 while (node != NULL
4459 && (node->wn_byte < word[i]
4460 || (node->wn_byte == NUL
4461 && (flags < 0
4462 ? node->wn_affixID < (unsigned)affixID
4463 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4464 || (node->wn_flags == (flags & WN_MASK)
4465 && (spin->si_sugtree
4466 ? (node->wn_region & 0xffff) < region
4467 : node->wn_affixID
4468 < (unsigned)affixID)))))))
4469 {
4470 prev = &node->wn_sibling;
4471 node = *prev;
4472 }
4473 if (node == NULL
4474 || node->wn_byte != word[i]
4475 || (word[i] == NUL
4476 && (flags < 0
4477 || spin->si_sugtree
4478 || node->wn_flags != (flags & WN_MASK)
4479 || node->wn_affixID != affixID)))
4480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004481 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004482 np = get_wordnode(spin);
4483 if (np == NULL)
4484 return FAIL;
4485 np->wn_byte = word[i];
4486
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004487 // If "node" is NULL this is a new child or the end of the sibling
4488 // list: ref count is one. Otherwise use ref count of sibling and
4489 // make ref count of sibling one (matters when inserting in front
4490 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004491 if (node == NULL)
4492 np->wn_refs = 1;
4493 else
4494 {
4495 np->wn_refs = node->wn_refs;
4496 node->wn_refs = 1;
4497 }
4498 if (prev != NULL)
4499 *prev = np;
4500 np->wn_sibling = node;
4501 node = np;
4502 }
4503
4504 if (word[i] == NUL)
4505 {
4506 node->wn_flags = flags;
4507 node->wn_region |= region;
4508 node->wn_affixID = affixID;
4509 break;
4510 }
4511 prev = &node->wn_child;
4512 node = *prev;
4513 }
4514#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004516 spell_print_tree(root->wn_sibling);
4517#endif
4518
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004519 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004520 ++spin->si_msg_count;
4521
4522 if (spin->si_compress_cnt > 1)
4523 {
4524 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004525 // Did enough words to lower the block count limit.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004526 spin->si_blocks_cnt += compress_inc;
4527 }
4528
4529 /*
4530 * When we have allocated lots of memory we need to compress the word tree
4531 * to free up some room. But compression is slow, and we might actually
4532 * need that room, thus only compress in the following situations:
4533 * 1. When not compressed before (si_compress_cnt == 0): when using
4534 * "compress_start" blocks.
4535 * 2. When compressed before and used "compress_inc" blocks before
4536 * adding "compress_added" words (si_compress_cnt > 1).
4537 * 3. When compressed before, added "compress_added" words
4538 * (si_compress_cnt == 1) and the number of free nodes drops below the
4539 * maximum word length.
4540 */
4541#ifndef SPELL_COMPRESS_ALLWAYS
4542 if (spin->si_compress_cnt == 1
4543 ? spin->si_free_count < MAXWLEN
4544 : spin->si_blocks_cnt >= compress_start)
4545#endif
4546 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004547 // Decrement the block counter. The effect is that we compress again
4548 // when the freed up room has been used and another "compress_inc"
4549 // blocks have been allocated. Unless "compress_added" words have
4550 // been added, then the limit is put back again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004551 spin->si_blocks_cnt -= compress_inc;
4552 spin->si_compress_cnt = compress_added;
4553
4554 if (spin->si_verbose)
4555 {
4556 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004557 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004558 msg_clr_eos();
4559 msg_didout = FALSE;
4560 msg_col = 0;
4561 out_flush();
4562 }
4563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004564 // Compress both trees. Either they both have many nodes, which makes
4565 // compression useful, or one of them is small, which means
4566 // compression goes fast. But when filling the soundfold word tree
4567 // there is no keep-case tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004568 wordtree_compress(spin, spin->si_foldroot);
4569 if (affixID >= 0)
4570 wordtree_compress(spin, spin->si_keeproot);
4571 }
4572
4573 return OK;
4574}
4575
4576/*
4577 * Get a wordnode_T, either from the list of previously freed nodes or
4578 * allocate a new one.
4579 * Returns NULL when out of memory.
4580 */
4581 static wordnode_T *
4582get_wordnode(spellinfo_T *spin)
4583{
4584 wordnode_T *n;
4585
4586 if (spin->si_first_free == NULL)
4587 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4588 else
4589 {
4590 n = spin->si_first_free;
4591 spin->si_first_free = n->wn_child;
4592 vim_memset(n, 0, sizeof(wordnode_T));
4593 --spin->si_free_count;
4594 }
4595#ifdef SPELL_PRINTTREE
4596 if (n != NULL)
4597 n->wn_nr = ++spin->si_wordnode_nr;
4598#endif
4599 return n;
4600}
4601
4602/*
4603 * Decrement the reference count on a node (which is the head of a list of
4604 * siblings). If the reference count becomes zero free the node and its
4605 * siblings.
4606 * Returns the number of nodes actually freed.
4607 */
4608 static int
4609deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4610{
4611 wordnode_T *np;
4612 int cnt = 0;
4613
4614 if (--node->wn_refs == 0)
4615 {
4616 for (np = node; np != NULL; np = np->wn_sibling)
4617 {
4618 if (np->wn_child != NULL)
4619 cnt += deref_wordnode(spin, np->wn_child);
4620 free_wordnode(spin, np);
4621 ++cnt;
4622 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004623 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004624 }
4625 return cnt;
4626}
4627
4628/*
4629 * Free a wordnode_T for re-use later.
4630 * Only the "wn_child" field becomes invalid.
4631 */
4632 static void
4633free_wordnode(spellinfo_T *spin, wordnode_T *n)
4634{
4635 n->wn_child = spin->si_first_free;
4636 spin->si_first_free = n;
4637 ++spin->si_free_count;
4638}
4639
4640/*
4641 * Compress a tree: find tails that are identical and can be shared.
4642 */
4643 static void
4644wordtree_compress(spellinfo_T *spin, wordnode_T *root)
4645{
4646 hashtab_T ht;
4647 int n;
4648 int tot = 0;
4649 int perc;
4650
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004651 // Skip the root itself, it's not actually used. The first sibling is the
4652 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004653 if (root->wn_sibling != NULL)
4654 {
4655 hash_init(&ht);
4656 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4657
4658#ifndef SPELL_PRINTTREE
4659 if (spin->si_verbose || p_verbose > 2)
4660#endif
4661 {
4662 if (tot > 1000000)
4663 perc = (tot - n) / (tot / 100);
4664 else if (tot == 0)
4665 perc = 0;
4666 else
4667 perc = (tot - n) * 100 / tot;
4668 vim_snprintf((char *)IObuff, IOSIZE,
4669 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
4670 n, tot, tot - n, perc);
4671 spell_message(spin, IObuff);
4672 }
4673#ifdef SPELL_PRINTTREE
4674 spell_print_tree(root->wn_sibling);
4675#endif
4676 hash_clear(&ht);
4677 }
4678}
4679
4680/*
4681 * Compress a node, its siblings and its children, depth first.
4682 * Returns the number of compressed nodes.
4683 */
4684 static int
4685node_compress(
4686 spellinfo_T *spin,
4687 wordnode_T *node,
4688 hashtab_T *ht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004689 int *tot) // total count of nodes before compressing,
4690 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004691{
4692 wordnode_T *np;
4693 wordnode_T *tp;
4694 wordnode_T *child;
4695 hash_T hash;
4696 hashitem_T *hi;
4697 int len = 0;
4698 unsigned nr, n;
4699 int compressed = 0;
4700
4701 /*
4702 * Go through the list of siblings. Compress each child and then try
4703 * finding an identical child to replace it.
4704 * Note that with "child" we mean not just the node that is pointed to,
4705 * but the whole list of siblings of which the child node is the first.
4706 */
4707 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4708 {
4709 ++len;
4710 if ((child = np->wn_child) != NULL)
4711 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004712 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004713 compressed += node_compress(spin, child, ht, tot);
4714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004715 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004716 hash = hash_hash(child->wn_u1.hashkey);
4717 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4718 if (!HASHITEM_EMPTY(hi))
4719 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004720 // There are children we encountered before with a hash value
4721 // identical to the current child. Now check if there is one
4722 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004723 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4724 if (node_equal(child, tp))
4725 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004726 // Found one! Now use that child in place of the
4727 // current one. This means the current child and all
4728 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004729 ++tp->wn_refs;
4730 compressed += deref_wordnode(spin, child);
4731 np->wn_child = tp;
4732 break;
4733 }
4734 if (tp == NULL)
4735 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004736 // No other child with this hash value equals the child of
4737 // the node, add it to the linked list after the first
4738 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004739 tp = HI2WN(hi);
4740 child->wn_u2.next = tp->wn_u2.next;
4741 tp->wn_u2.next = child;
4742 }
4743 }
4744 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004745 // No other child has this hash value, add it to the
4746 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004747 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4748 }
4749 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004750 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004751
4752 /*
4753 * Make a hash key for the node and its siblings, so that we can quickly
4754 * find a lookalike node. This must be done after compressing the sibling
4755 * list, otherwise the hash key would become invalid by the compression.
4756 */
4757 node->wn_u1.hashkey[0] = len;
4758 nr = 0;
4759 for (np = node; np != NULL; np = np->wn_sibling)
4760 {
4761 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004762 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004763 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4764 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004765 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004766 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4767 nr = nr * 101 + n;
4768 }
4769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004770 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004771 n = nr & 0xff;
4772 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4773 n = (nr >> 8) & 0xff;
4774 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4775 n = (nr >> 16) & 0xff;
4776 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4777 n = (nr >> 24) & 0xff;
4778 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4779 node->wn_u1.hashkey[5] = NUL;
4780
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004781 // Check for CTRL-C pressed now and then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004782 fast_breakcheck();
4783
4784 return compressed;
4785}
4786
4787/*
4788 * Return TRUE when two nodes have identical siblings and children.
4789 */
4790 static int
4791node_equal(wordnode_T *n1, wordnode_T *n2)
4792{
4793 wordnode_T *p1;
4794 wordnode_T *p2;
4795
4796 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4797 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4798 if (p1->wn_byte != p2->wn_byte
4799 || (p1->wn_byte == NUL
4800 ? (p1->wn_flags != p2->wn_flags
4801 || p1->wn_region != p2->wn_region
4802 || p1->wn_affixID != p2->wn_affixID)
4803 : (p1->wn_child != p2->wn_child)))
4804 break;
4805
4806 return p1 == NULL && p2 == NULL;
4807}
4808
Bram Moolenaareae1b912019-05-09 15:12:55 +02004809static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004810
4811/*
4812 * Function given to qsort() to sort the REP items on "from" string.
4813 */
4814 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004815rep_compare(const void *s1, const void *s2)
4816{
4817 fromto_T *p1 = (fromto_T *)s1;
4818 fromto_T *p2 = (fromto_T *)s2;
4819
4820 return STRCMP(p1->ft_from, p2->ft_from);
4821}
4822
4823/*
4824 * Write the Vim .spl file "fname".
4825 * Return FAIL or OK;
4826 */
4827 static int
4828write_vim_spell(spellinfo_T *spin, char_u *fname)
4829{
4830 FILE *fd;
4831 int regionmask;
4832 int round;
4833 wordnode_T *tree;
4834 int nodecount;
4835 int i;
4836 int l;
4837 garray_T *gap;
4838 fromto_T *ftp;
4839 char_u *p;
4840 int rr;
4841 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004842 size_t fwv = 1; // collect return value of fwrite() to avoid
4843 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004844
4845 fd = mch_fopen((char *)fname, "w");
4846 if (fd == NULL)
4847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004848 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004849 return FAIL;
4850 }
4851
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004852 // <HEADER>: <fileID> <versionnr>
4853 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004854 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4855 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004856 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004857 goto theend;
4858
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004859 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004860
4861 /*
4862 * <SECTIONS>: <section> ... <sectionend>
4863 */
4864
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004865 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004866 if (spin->si_info != NULL)
4867 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004868 putc(SN_INFO, fd); // <sectionID>
4869 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004870
4871 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004872 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4873 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004874 }
4875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004876 // SN_REGION: <regionname> ...
4877 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004878 if (spin->si_region_count > 1)
4879 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004880 putc(SN_REGION, fd); // <sectionID>
4881 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004882 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004883 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004884 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004885 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004886 regionmask = (1 << spin->si_region_count) - 1;
4887 }
4888 else
4889 regionmask = 0;
4890
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004891 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4892 //
4893 // The table with character flags and the table for case folding.
4894 // This makes sure the same characters are recognized as word characters
4895 // when generating an when using a spell file.
4896 // Skip this for ASCII, the table may conflict with the one used for
4897 // 'encoding'.
4898 // Also skip this for an .add.spl file, the main spell file must contain
4899 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004900 if (!spin->si_ascii && !spin->si_add)
4901 {
4902 char_u folchars[128 * 8];
4903 int flags;
4904
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004905 putc(SN_CHARFLAGS, fd); // <sectionID>
4906 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004907
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004908 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004909 l = 0;
4910 for (i = 128; i < 256; ++i)
4911 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004912 if (has_mbyte)
4913 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4914 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004915 folchars[l++] = spelltab.st_fold[i];
4916 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004917 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004919 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004920 for (i = 128; i < 256; ++i)
4921 {
4922 flags = 0;
4923 if (spelltab.st_isw[i])
4924 flags |= CF_WORD;
4925 if (spelltab.st_isu[i])
4926 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004927 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004928 }
4929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004930 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4931 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004932 }
4933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004934 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004935 if (spin->si_midword != NULL)
4936 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004937 putc(SN_MIDWORD, fd); // <sectionID>
4938 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004939
4940 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004941 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004942 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004943 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004944 }
4945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004946 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004947 if (spin->si_prefcond.ga_len > 0)
4948 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004949 putc(SN_PREFCOND, fd); // <sectionID>
4950 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004951
4952 l = write_spell_prefcond(NULL, &spin->si_prefcond);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004953 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004954
4955 write_spell_prefcond(fd, &spin->si_prefcond);
4956 }
4957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004958 // SN_REP: <repcount> <rep> ...
4959 // SN_SAL: <salflags> <salcount> <sal> ...
4960 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004961
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004962 // round 1: SN_REP section
4963 // round 2: SN_SAL section (unless SN_SOFO is used)
4964 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004965 for (round = 1; round <= 3; ++round)
4966 {
4967 if (round == 1)
4968 gap = &spin->si_rep;
4969 else if (round == 2)
4970 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004971 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004972 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
4973 continue;
4974 gap = &spin->si_sal;
4975 }
4976 else
4977 gap = &spin->si_repsal;
4978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004979 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004980 if (gap->ga_len == 0)
4981 continue;
4982
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004983 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004984 if (round != 2)
4985 qsort(gap->ga_data, (size_t)gap->ga_len,
4986 sizeof(fromto_T), rep_compare);
4987
4988 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004989 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004991 // This is for making suggestions, section is not required.
4992 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004993
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004994 // Compute the length of what follows.
4995 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004996 for (i = 0; i < gap->ga_len; ++i)
4997 {
4998 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004999 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
5000 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005001 }
5002 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005003 ++l; // count <salflags>
5004 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005005
5006 if (round == 2)
5007 {
5008 i = 0;
5009 if (spin->si_followup)
5010 i |= SAL_F0LLOWUP;
5011 if (spin->si_collapse)
5012 i |= SAL_COLLAPSE;
5013 if (spin->si_rem_accents)
5014 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005015 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005016 }
5017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005018 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005019 for (i = 0; i < gap->ga_len; ++i)
5020 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005021 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5022 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005023 ftp = &((fromto_T *)gap->ga_data)[i];
5024 for (rr = 1; rr <= 2; ++rr)
5025 {
5026 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5027 l = (int)STRLEN(p);
5028 putc(l, fd);
5029 if (l > 0)
5030 fwv &= fwrite(p, l, (size_t)1, fd);
5031 }
5032 }
5033
5034 }
5035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005036 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5037 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005038 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5039 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005040 putc(SN_SOFO, fd); // <sectionID>
5041 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005042
5043 l = (int)STRLEN(spin->si_sofofr);
5044 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005045 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005047 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5048 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005049
5050 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005051 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5052 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005053 }
5054
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005055 // SN_WORDS: <word> ...
5056 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005057 if (spin->si_commonwords.ht_used > 0)
5058 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005059 putc(SN_WORDS, fd); // <sectionID>
5060 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005061
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005062 // round 1: count the bytes
5063 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005064 for (round = 1; round <= 2; ++round)
5065 {
5066 int todo;
5067 int len = 0;
5068 hashitem_T *hi;
5069
5070 todo = (int)spin->si_commonwords.ht_used;
5071 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5072 if (!HASHITEM_EMPTY(hi))
5073 {
5074 l = (int)STRLEN(hi->hi_key) + 1;
5075 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005076 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005077 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5078 --todo;
5079 }
5080 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005081 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005082 }
5083 }
5084
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005085 // SN_MAP: <mapstr>
5086 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005087 if (spin->si_map.ga_len > 0)
5088 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005089 putc(SN_MAP, fd); // <sectionID>
5090 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005091 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005092 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005093 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005094 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005095 }
5096
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005097 // SN_SUGFILE: <timestamp>
5098 // This is used to notify that a .sug file may be available and at the
5099 // same time allows for checking that a .sug file that is found matches
5100 // with this .spl file. That's because the word numbers must be exactly
5101 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005102 if (!spin->si_nosugfile
5103 && (spin->si_sal.ga_len > 0
5104 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5105 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005106 putc(SN_SUGFILE, fd); // <sectionID>
5107 putc(0, fd); // <sectionflags>
5108 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005109
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005110 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005111 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005113 }
5114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005115 // SN_NOSPLITSUGS: nothing
5116 // This is used to notify that no suggestions with word splits are to be
5117 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005118 if (spin->si_nosplitsugs)
5119 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005120 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5121 putc(0, fd); // <sectionflags>
5122 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005123 }
5124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005125 // SN_NOCOMPUNDSUGS: nothing
5126 // This is used to notify that no suggestions with compounds are to be
5127 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005128 if (spin->si_nocompoundsugs)
5129 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005130 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5131 putc(0, fd); // <sectionflags>
5132 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005133 }
5134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005135 // SN_COMPOUND: compound info.
5136 // We don't mark it required, when not supported all compound words will
5137 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005138 if (spin->si_compflags != NULL)
5139 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005140 putc(SN_COMPOUND, fd); // <sectionID>
5141 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005142
5143 l = (int)STRLEN(spin->si_compflags);
5144 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5145 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005146 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005148 putc(spin->si_compmax, fd); // <compmax>
5149 putc(spin->si_compminlen, fd); // <compminlen>
5150 putc(spin->si_compsylmax, fd); // <compsylmax>
5151 putc(0, fd); // for Vim 7.0b compatibility
5152 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005153 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005154 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005155 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5156 {
5157 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005158 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005159 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005160 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005161 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005162 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005163 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5164 (size_t)1, fd);
5165 }
5166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005167 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005168 if (spin->si_nobreak)
5169 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005170 putc(SN_NOBREAK, fd); // <sectionID>
5171 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005172
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005173 // It's empty, the presence of the section flags the feature.
5174 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005175 }
5176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005177 // SN_SYLLABLE: syllable info.
5178 // We don't mark it required, when not supported syllables will not be
5179 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005180 if (spin->si_syllable != NULL)
5181 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005182 putc(SN_SYLLABLE, fd); // <sectionID>
5183 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005184
5185 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005186 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005187 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005188 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005189 }
5190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005191 // end of <SECTIONS>
5192 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005193
5194
5195 /*
5196 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5197 */
5198 spin->si_memtot = 0;
5199 for (round = 1; round <= 3; ++round)
5200 {
5201 if (round == 1)
5202 tree = spin->si_foldroot->wn_sibling;
5203 else if (round == 2)
5204 tree = spin->si_keeproot->wn_sibling;
5205 else
5206 tree = spin->si_prefroot->wn_sibling;
5207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005208 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005209 clear_node(tree);
5210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005211 // Count the number of nodes. Needed to be able to allocate the
5212 // memory when reading the nodes. Also fills in index for shared
5213 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005214 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005216 // number of nodes in 4 bytes
5217 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005218 spin->si_memtot += nodecount + nodecount * sizeof(int);
5219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005220 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005221 (void)put_node(fd, tree, 0, regionmask, round == 3);
5222 }
5223
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005224 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005225 if (putc(0, fd) == EOF)
5226 retval = FAIL;
5227theend:
5228 if (fclose(fd) == EOF)
5229 retval = FAIL;
5230
5231 if (fwv != (size_t)1)
5232 retval = FAIL;
5233 if (retval == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005234 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005235
5236 return retval;
5237}
5238
5239/*
5240 * Clear the index and wnode fields of "node", it siblings and its
5241 * children. This is needed because they are a union with other items to save
5242 * space.
5243 */
5244 static void
5245clear_node(wordnode_T *node)
5246{
5247 wordnode_T *np;
5248
5249 if (node != NULL)
5250 for (np = node; np != NULL; np = np->wn_sibling)
5251 {
5252 np->wn_u1.index = 0;
5253 np->wn_u2.wnode = NULL;
5254
5255 if (np->wn_byte != NUL)
5256 clear_node(np->wn_child);
5257 }
5258}
5259
5260
5261/*
5262 * Dump a word tree at node "node".
5263 *
5264 * This first writes the list of possible bytes (siblings). Then for each
5265 * byte recursively write the children.
5266 *
5267 * NOTE: The code here must match the code in read_tree_node(), since
5268 * assumptions are made about the indexes (so that we don't have to write them
5269 * in the file).
5270 *
5271 * Returns the number of nodes used.
5272 */
5273 static int
5274put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005275 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005276 wordnode_T *node,
5277 int idx,
5278 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005279 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005280{
5281 int newindex = idx;
5282 int siblingcount = 0;
5283 wordnode_T *np;
5284 int flags;
5285
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005286 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005287 if (node == NULL)
5288 return 0;
5289
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005290 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005291 node->wn_u1.index = idx;
5292
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005293 // Count the number of siblings.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005294 for (np = node; np != NULL; np = np->wn_sibling)
5295 ++siblingcount;
5296
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005297 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005298 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005299 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005300
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005301 // Write each sibling byte and optionally extra info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005302 for (np = node; np != NULL; np = np->wn_sibling)
5303 {
5304 if (np->wn_byte == 0)
5305 {
5306 if (fd != NULL)
5307 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005308 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005309 if (prefixtree)
5310 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005311 // In PREFIXTREE write the required affixID and the
5312 // associated condition nr (stored in wn_region). The
5313 // byte value is misused to store the "rare" and "not
5314 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005315 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005316 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005317 else
5318 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005319 putc(BY_FLAGS, fd); // <byte>
5320 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005321 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005322 putc(np->wn_affixID, fd); // <affixID>
5323 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005324 }
5325 else
5326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005327 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005328 flags = np->wn_flags;
5329 if (regionmask != 0 && np->wn_region != regionmask)
5330 flags |= WF_REGION;
5331 if (np->wn_affixID != 0)
5332 flags |= WF_AFX;
5333 if (flags == 0)
5334 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005335 // word without flags or region
5336 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005337 }
5338 else
5339 {
5340 if (np->wn_flags >= 0x100)
5341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005342 putc(BY_FLAGS2, fd); // <byte>
5343 putc(flags, fd); // <flags>
5344 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005345 }
5346 else
5347 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 putc(BY_FLAGS, fd); // <byte>
5349 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005350 }
5351 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005352 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005353 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005354 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005355 }
5356 }
5357 }
5358 }
5359 else
5360 {
5361 if (np->wn_child->wn_u1.index != 0
5362 && np->wn_child->wn_u2.wnode != node)
5363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005364 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005365 if (fd != NULL)
5366 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005367 putc(BY_INDEX, fd); // <byte>
5368 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005369 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5370 }
5371 }
5372 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005373 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005374 np->wn_child->wn_u2.wnode = node;
5375
5376 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005377 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005379 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005380 return 0;
5381 }
5382 }
5383 }
5384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005385 // Space used in the array when reading: one for each sibling and one for
5386 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005387 newindex += siblingcount + 1;
5388
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005389 // Recursively dump the children of each sibling.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005390 for (np = node; np != NULL; np = np->wn_sibling)
5391 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5392 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5393 prefixtree);
5394
5395 return newindex;
5396}
5397
5398
5399/*
5400 * ":mkspell [-ascii] outfile infile ..."
5401 * ":mkspell [-ascii] addfile"
5402 */
5403 void
5404ex_mkspell(exarg_T *eap)
5405{
5406 int fcount;
5407 char_u **fnames;
5408 char_u *arg = eap->arg;
5409 int ascii = FALSE;
5410
5411 if (STRNCMP(arg, "-ascii", 6) == 0)
5412 {
5413 ascii = TRUE;
5414 arg = skipwhite(arg + 6);
5415 }
5416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005417 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005418 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5419 {
5420 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5421 FreeWild(fcount, fnames);
5422 }
5423}
5424
5425/*
5426 * Create the .sug file.
5427 * Uses the soundfold info in "spin".
5428 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5429 */
5430 static void
5431spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5432{
5433 char_u *fname = NULL;
5434 int len;
5435 slang_T *slang;
5436 int free_slang = FALSE;
5437
5438 /*
5439 * Read back the .spl file that was written. This fills the required
5440 * info for soundfolding. This also uses less memory than the
5441 * pointer-linked version of the trie. And it avoids having two versions
5442 * of the code for the soundfolding stuff.
5443 * It might have been done already by spell_reload_one().
5444 */
5445 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005446 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005447 break;
5448 if (slang == NULL)
5449 {
5450 spell_message(spin, (char_u *)_("Reading back spell file..."));
5451 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5452 if (slang == NULL)
5453 return;
5454 free_slang = TRUE;
5455 }
5456
5457 /*
5458 * Clear the info in "spin" that is used.
5459 */
5460 spin->si_blocks = NULL;
5461 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005462 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005463 spin->si_free_count = 0;
5464 spin->si_first_free = NULL;
5465 spin->si_foldwcount = 0;
5466
5467 /*
5468 * Go through the trie of good words, soundfold each word and add it to
5469 * the soundfold trie.
5470 */
5471 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5472 if (sug_filltree(spin, slang) == FAIL)
5473 goto theend;
5474
5475 /*
5476 * Create the table which links each soundfold word with a list of the
5477 * good words it may come from. Creates buffer "spin->si_spellbuf".
5478 * This also removes the wordnr from the NUL byte entries to make
5479 * compression possible.
5480 */
5481 if (sug_maketable(spin) == FAIL)
5482 goto theend;
5483
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005484 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005485 (long)spin->si_spellbuf->b_ml.ml_line_count);
5486
5487 /*
5488 * Compress the soundfold trie.
5489 */
5490 spell_message(spin, (char_u *)_(msg_compressing));
5491 wordtree_compress(spin, spin->si_foldroot);
5492
5493 /*
5494 * Write the .sug file.
5495 * Make the file name by changing ".spl" to ".sug".
5496 */
5497 fname = alloc(MAXPATHL);
5498 if (fname == NULL)
5499 goto theend;
5500 vim_strncpy(fname, wfname, MAXPATHL - 1);
5501 len = (int)STRLEN(fname);
5502 fname[len - 2] = 'u';
5503 fname[len - 1] = 'g';
5504 sug_write(spin, fname);
5505
5506theend:
5507 vim_free(fname);
5508 if (free_slang)
5509 slang_free(slang);
5510 free_blocks(spin->si_blocks);
5511 close_spellbuf(spin->si_spellbuf);
5512}
5513
5514/*
5515 * Build the soundfold trie for language "slang".
5516 */
5517 static int
5518sug_filltree(spellinfo_T *spin, slang_T *slang)
5519{
5520 char_u *byts;
5521 idx_T *idxs;
5522 int depth;
5523 idx_T arridx[MAXWLEN];
5524 int curi[MAXWLEN];
5525 char_u tword[MAXWLEN];
5526 char_u tsalword[MAXWLEN];
5527 int c;
5528 idx_T n;
5529 unsigned words_done = 0;
5530 int wordcount[MAXWLEN];
5531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005532 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005533 spin->si_foldroot = wordtree_alloc(spin);
5534 if (spin->si_foldroot == NULL)
5535 return FAIL;
5536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005537 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005538 spin->si_sugtree = TRUE;
5539
5540 /*
5541 * Go through the whole case-folded tree, soundfold each word and put it
5542 * in the trie.
5543 */
5544 byts = slang->sl_fbyts;
5545 idxs = slang->sl_fidxs;
5546
5547 arridx[0] = 0;
5548 curi[0] = 1;
5549 wordcount[0] = 0;
5550
5551 depth = 0;
5552 while (depth >= 0 && !got_int)
5553 {
5554 if (curi[depth] > byts[arridx[depth]])
5555 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005556 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005557 idxs[arridx[depth]] = wordcount[depth];
5558 if (depth > 0)
5559 wordcount[depth - 1] += wordcount[depth];
5560
5561 --depth;
5562 line_breakcheck();
5563 }
5564 else
5565 {
5566
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005567 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005568 n = arridx[depth] + curi[depth];
5569 ++curi[depth];
5570
5571 c = byts[n];
5572 if (c == 0)
5573 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005574 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005575 tword[depth] = NUL;
5576 spell_soundfold(slang, tword, TRUE, tsalword);
5577
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005578 // We use the "flags" field for the MSB of the wordnr,
5579 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005580 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5581 words_done >> 16, words_done & 0xffff,
5582 0) == FAIL)
5583 return FAIL;
5584
5585 ++words_done;
5586 ++wordcount[depth];
5587
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005588 // Reset the block count each time to avoid compression
5589 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005590 spin->si_blocks_cnt = 0;
5591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 // Skip over any other NUL bytes (same word with different
5593 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005594 while (byts[n + 1] == 0)
5595 {
5596 ++n;
5597 ++curi[depth];
5598 }
5599 }
5600 else
5601 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005602 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005603 tword[depth++] = c;
5604 arridx[depth] = idxs[n];
5605 curi[depth] = 1;
5606 wordcount[depth] = 0;
5607 }
5608 }
5609 }
5610
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005611 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005612
5613 return OK;
5614}
5615
5616/*
5617 * Make the table that links each word in the soundfold trie to the words it
5618 * can be produced from.
5619 * This is not unlike lines in a file, thus use a memfile to be able to access
5620 * the table efficiently.
5621 * Returns FAIL when out of memory.
5622 */
5623 static int
5624sug_maketable(spellinfo_T *spin)
5625{
5626 garray_T ga;
5627 int res = OK;
5628
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005629 // Allocate a buffer, open a memline for it and create the swap file
5630 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005631 spin->si_spellbuf = open_spellbuf();
5632 if (spin->si_spellbuf == NULL)
5633 return FAIL;
5634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005635 // Use a buffer to store the line info, avoids allocating many small
5636 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005637 ga_init2(&ga, 1, 100);
5638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005639 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005640 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5641 res = FAIL;
5642
5643 ga_clear(&ga);
5644 return res;
5645}
5646
5647/*
5648 * Fill the table for one node and its children.
5649 * Returns the wordnr at the start of the node.
5650 * Returns -1 when out of memory.
5651 */
5652 static int
5653sug_filltable(
5654 spellinfo_T *spin,
5655 wordnode_T *node,
5656 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005657 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005658{
5659 wordnode_T *p, *np;
5660 int wordnr = startwordnr;
5661 int nr;
5662 int prev_nr;
5663
5664 for (p = node; p != NULL; p = p->wn_sibling)
5665 {
5666 if (p->wn_byte == NUL)
5667 {
5668 gap->ga_len = 0;
5669 prev_nr = 0;
5670 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5671 {
5672 if (ga_grow(gap, 10) == FAIL)
5673 return -1;
5674
5675 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005676 // Compute the offset from the previous nr and store the
5677 // offset in a way that it takes a minimum number of bytes.
5678 // It's a bit like utf-8, but without the need to mark
5679 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005680 nr -= prev_nr;
5681 prev_nr += nr;
5682 gap->ga_len += offset2bytes(nr,
5683 (char_u *)gap->ga_data + gap->ga_len);
5684 }
5685
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005686 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005687 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5688
5689 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5690 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5691 return -1;
5692 ++wordnr;
5693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005694 // Remove extra NUL entries, we no longer need them. We don't
5695 // bother freeing the nodes, the won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005696 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5697 p->wn_sibling = p->wn_sibling->wn_sibling;
5698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005699 // Clear the flags on the remaining NUL node, so that compression
5700 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005701 p->wn_flags = 0;
5702 p->wn_region = 0;
5703 }
5704 else
5705 {
5706 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5707 if (wordnr == -1)
5708 return -1;
5709 }
5710 }
5711 return wordnr;
5712}
5713
5714/*
5715 * Convert an offset into a minimal number of bytes.
5716 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5717 * bytes.
5718 */
5719 static int
5720offset2bytes(int nr, char_u *buf)
5721{
5722 int rem;
5723 int b1, b2, b3, b4;
5724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005725 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005726 b1 = nr % 255 + 1;
5727 rem = nr / 255;
5728 b2 = rem % 255 + 1;
5729 rem = rem / 255;
5730 b3 = rem % 255 + 1;
5731 b4 = rem / 255 + 1;
5732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005733 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005734 {
5735 buf[0] = 0xe0 + b4;
5736 buf[1] = b3;
5737 buf[2] = b2;
5738 buf[3] = b1;
5739 return 4;
5740 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005741 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005742 {
5743 buf[0] = 0xc0 + b3;
5744 buf[1] = b2;
5745 buf[2] = b1;
5746 return 3;
5747 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005748 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005749 {
5750 buf[0] = 0x80 + b2;
5751 buf[1] = b1;
5752 return 2;
5753 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005754 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005755 buf[0] = b1;
5756 return 1;
5757}
5758
5759/*
5760 * Write the .sug file in "fname".
5761 */
5762 static void
5763sug_write(spellinfo_T *spin, char_u *fname)
5764{
5765 FILE *fd;
5766 wordnode_T *tree;
5767 int nodecount;
5768 int wcount;
5769 char_u *line;
5770 linenr_T lnum;
5771 int len;
5772
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005773 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005774 fd = mch_fopen((char *)fname, "w");
5775 if (fd == NULL)
5776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005777 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005778 return;
5779 }
5780
5781 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005782 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005783 spell_message(spin, IObuff);
5784
5785 /*
5786 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5787 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005788 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005790 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005791 goto theend;
5792 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005793 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005794
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005795 // Write si_sugtime to the file.
5796 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005797
5798 /*
5799 * <SUGWORDTREE>
5800 */
5801 spin->si_memtot = 0;
5802 tree = spin->si_foldroot->wn_sibling;
5803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005804 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005805 clear_node(tree);
5806
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005807 // Count the number of nodes. Needed to be able to allocate the
5808 // memory when reading the nodes. Also fills in index for shared
5809 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005810 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5811
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005812 // number of nodes in 4 bytes
5813 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005814 spin->si_memtot += nodecount + nodecount * sizeof(int);
5815
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005816 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005817 (void)put_node(fd, tree, 0, 0, FALSE);
5818
5819 /*
5820 * <SUGTABLE>: <sugwcount> <sugline> ...
5821 */
5822 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005823 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005824
5825 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5826 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005827 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005828 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5829 len = (int)STRLEN(line) + 1;
5830 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005832 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005833 goto theend;
5834 }
5835 spin->si_memtot += len;
5836 }
5837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005838 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005839 if (putc(0, fd) == EOF)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005840 emsg(_(e_write));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005841
5842 vim_snprintf((char *)IObuff, IOSIZE,
5843 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5844 spell_message(spin, IObuff);
5845
5846theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005847 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005848 fclose(fd);
5849}
5850
5851
5852/*
5853 * Create a Vim spell file from one or more word lists.
5854 * "fnames[0]" is the output file name.
5855 * "fnames[fcount - 1]" is the last input file name.
5856 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5857 * and ".spl" is appended to make the output file name.
5858 */
5859 void
5860mkspell(
5861 int fcount,
5862 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005863 int ascii, // -ascii argument given
5864 int over_write, // overwrite existing output file
5865 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005866{
5867 char_u *fname = NULL;
5868 char_u *wfname;
5869 char_u **innames;
5870 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005871 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005872 int i;
5873 int len;
5874 stat_T st;
5875 int error = FALSE;
5876 spellinfo_T spin;
5877
5878 vim_memset(&spin, 0, sizeof(spin));
5879 spin.si_verbose = !added_word;
5880 spin.si_ascii = ascii;
5881 spin.si_followup = TRUE;
5882 spin.si_rem_accents = TRUE;
5883 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5884 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
5885 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5886 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
5887 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
5888 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
5889 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005890 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005891
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005892 // default: fnames[0] is output file, following are input files
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005893 innames = &fnames[1];
5894 incount = fcount - 1;
5895
5896 wfname = alloc(MAXPATHL);
5897 if (wfname == NULL)
5898 return;
5899
5900 if (fcount >= 1)
5901 {
5902 len = (int)STRLEN(fnames[0]);
5903 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5904 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005905 // For ":mkspell path/en.latin1.add" output file is
5906 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005907 innames = &fnames[0];
5908 incount = 1;
5909 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5910 }
5911 else if (fcount == 1)
5912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005913 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005914 innames = &fnames[0];
5915 incount = 1;
5916 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5917 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5918 }
5919 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5920 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005921 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005922 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5923 }
5924 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005925 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005926 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5927 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005929 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005930 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5931 spin.si_ascii = TRUE;
5932
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005933 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005934 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5935 spin.si_add = TRUE;
5936 }
5937
5938 if (incount <= 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005939 emsg(_(e_invarg)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005940 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005941 emsg(_("E751: Output file name must not have region name"));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005942 else if (incount > MAXREGIONS)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005943 semsg(_("E754: Only up to %d regions supported"), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005944 else
5945 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005946 // Check for overwriting before doing things that may take a lot of
5947 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005948 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
5949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005950 emsg(_(e_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005951 goto theend;
5952 }
5953 if (mch_isdir(wfname))
5954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005955 semsg(_(e_isadir2), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005956 goto theend;
5957 }
5958
5959 fname = alloc(MAXPATHL);
5960 if (fname == NULL)
5961 goto theend;
5962
5963 /*
5964 * Init the aff and dic pointers.
5965 * Get the region names if there are more than 2 arguments.
5966 */
5967 for (i = 0; i < incount; ++i)
5968 {
5969 afile[i] = NULL;
5970
5971 if (incount > 1)
5972 {
5973 len = (int)STRLEN(innames[i]);
5974 if (STRLEN(gettail(innames[i])) < 5
5975 || innames[i][len - 3] != '_')
5976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005977 semsg(_("E755: Invalid region in %s"), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005978 goto theend;
5979 }
5980 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5981 spin.si_region_name[i * 2 + 1] =
5982 TOLOWER_ASC(innames[i][len - 1]);
5983 }
5984 }
5985 spin.si_region_count = incount;
5986
5987 spin.si_foldroot = wordtree_alloc(&spin);
5988 spin.si_keeproot = wordtree_alloc(&spin);
5989 spin.si_prefroot = wordtree_alloc(&spin);
5990 if (spin.si_foldroot == NULL
5991 || spin.si_keeproot == NULL
5992 || spin.si_prefroot == NULL)
5993 {
5994 free_blocks(spin.si_blocks);
5995 goto theend;
5996 }
5997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005998 // When not producing a .add.spl file clear the character table when
5999 // we encounter one in the .aff file. This means we dump the current
6000 // one in the .spl file if the .aff file doesn't define one. That's
6001 // better than guessing the contents, the table will match a
6002 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006003 if (!spin.si_add)
6004 spin.si_clear_chartab = TRUE;
6005
6006 /*
6007 * Read all the .aff and .dic files.
6008 * Text is converted to 'encoding'.
6009 * Words are stored in the case-folded and keep-case trees.
6010 */
6011 for (i = 0; i < incount && !error; ++i)
6012 {
6013 spin.si_conv.vc_type = CONV_NONE;
6014 spin.si_region = 1 << i;
6015
6016 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6017 if (mch_stat((char *)fname, &st) >= 0)
6018 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006019 // Read the .aff file. Will init "spin->si_conv" based on the
6020 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006021 afile[i] = spell_read_aff(&spin, fname);
6022 if (afile[i] == NULL)
6023 error = TRUE;
6024 else
6025 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006026 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006027 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6028 innames[i]);
6029 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6030 error = TRUE;
6031 }
6032 }
6033 else
6034 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006035 // No .aff file, try reading the file as a word list. Store
6036 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006037 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6038 error = TRUE;
6039 }
6040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006041 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006042 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006043 }
6044
6045 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006046 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006047
6048 if (!error && !got_int)
6049 {
6050 /*
6051 * Combine tails in the tree.
6052 */
6053 spell_message(&spin, (char_u *)_(msg_compressing));
6054 wordtree_compress(&spin, spin.si_foldroot);
6055 wordtree_compress(&spin, spin.si_keeproot);
6056 wordtree_compress(&spin, spin.si_prefroot);
6057 }
6058
6059 if (!error && !got_int)
6060 {
6061 /*
6062 * Write the info in the spell file.
6063 */
6064 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006065 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006066 spell_message(&spin, IObuff);
6067
6068 error = write_vim_spell(&spin, wfname) == FAIL;
6069
6070 spell_message(&spin, (char_u *)_("Done!"));
6071 vim_snprintf((char *)IObuff, IOSIZE,
6072 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6073 spell_message(&spin, IObuff);
6074
6075 /*
6076 * If the file is loaded need to reload it.
6077 */
6078 if (!error)
6079 spell_reload_one(wfname, added_word);
6080 }
6081
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006082 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006083 ga_clear(&spin.si_rep);
6084 ga_clear(&spin.si_repsal);
6085 ga_clear(&spin.si_sal);
6086 ga_clear(&spin.si_map);
6087 ga_clear(&spin.si_comppat);
6088 ga_clear(&spin.si_prefcond);
6089 hash_clear_all(&spin.si_commonwords, 0);
6090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006091 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006092 for (i = 0; i < incount; ++i)
6093 if (afile[i] != NULL)
6094 spell_free_aff(afile[i]);
6095
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006096 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006097 free_blocks(spin.si_blocks);
6098
6099 /*
6100 * If there is soundfolding info and no NOSUGFILE item create the
6101 * .sug file with the soundfolded word trie.
6102 */
6103 if (spin.si_sugtime != 0 && !error && !got_int)
6104 spell_make_sugfile(&spin, wfname);
6105
6106 }
6107
6108theend:
6109 vim_free(fname);
6110 vim_free(wfname);
6111}
6112
6113/*
6114 * Display a message for spell file processing when 'verbose' is set or using
6115 * ":mkspell". "str" can be IObuff.
6116 */
6117 static void
6118spell_message(spellinfo_T *spin, char_u *str)
6119{
6120 if (spin->si_verbose || p_verbose > 2)
6121 {
6122 if (!spin->si_verbose)
6123 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006124 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006125 out_flush();
6126 if (!spin->si_verbose)
6127 verbose_leave();
6128 }
6129}
6130
6131/*
6132 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006133 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006134 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006135 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006136 */
6137 void
6138ex_spell(exarg_T *eap)
6139{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006140 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6141 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6142 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006143 eap->forceit ? 0 : (int)eap->line2,
6144 eap->cmdidx == CMD_spellundo);
6145}
6146
6147/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006148 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006149 */
6150 void
6151spell_add_word(
6152 char_u *word,
6153 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006154 int what, // SPELL_ADD_ values
6155 int idx, // "zG" and "zW": zero, otherwise index in
6156 // 'spellfile'
6157 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006158{
6159 FILE *fd = NULL;
6160 buf_T *buf = NULL;
6161 int new_spf = FALSE;
6162 char_u *fname;
6163 char_u *fnamebuf = NULL;
6164 char_u line[MAXWLEN * 2];
6165 long fpos, fpos_next = 0;
6166 int i;
6167 char_u *spf;
6168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006169 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006170 {
6171 if (int_wordlist == NULL)
6172 {
6173 int_wordlist = vim_tempname('s', FALSE);
6174 if (int_wordlist == NULL)
6175 return;
6176 }
6177 fname = int_wordlist;
6178 }
6179 else
6180 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006181 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006182 if (*curwin->w_s->b_p_spf == NUL)
6183 {
6184 init_spellfile();
6185 new_spf = TRUE;
6186 }
6187
6188 if (*curwin->w_s->b_p_spf == NUL)
6189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006190 semsg(_(e_notset), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006191 return;
6192 }
6193 fnamebuf = alloc(MAXPATHL);
6194 if (fnamebuf == NULL)
6195 return;
6196
6197 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6198 {
6199 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6200 if (i == idx)
6201 break;
6202 if (*spf == NUL)
6203 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006204 semsg(_("E765: 'spellfile' does not have %d entries"), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006205 vim_free(fnamebuf);
6206 return;
6207 }
6208 }
6209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006210 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006211 buf = buflist_findname_exp(fnamebuf);
6212 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6213 buf = NULL;
6214 if (buf != NULL && bufIsChanged(buf))
6215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006216 emsg(_(e_bufloaded));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006217 vim_free(fnamebuf);
6218 return;
6219 }
6220
6221 fname = fnamebuf;
6222 }
6223
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006224 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006225 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006226 // When the word appears as good word we need to remove that one,
6227 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006228 fd = mch_fopen((char *)fname, "r");
6229 if (fd != NULL)
6230 {
6231 while (!vim_fgets(line, MAXWLEN * 2, fd))
6232 {
6233 fpos = fpos_next;
6234 fpos_next = ftell(fd);
6235 if (STRNCMP(word, line, len) == 0
6236 && (line[len] == '/' || line[len] < ' '))
6237 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006238 // Found duplicate word. Remove it by writing a '#' at
6239 // the start of the line. Mixing reading and writing
6240 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006241 fclose(fd);
6242 fd = mch_fopen((char *)fname, "r+");
6243 if (fd == NULL)
6244 break;
6245 if (fseek(fd, fpos, SEEK_SET) == 0)
6246 {
6247 fputc('#', fd);
6248 if (undo)
6249 {
6250 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006251 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006252 len, word, NameBuff);
6253 }
6254 }
6255 fseek(fd, fpos_next, SEEK_SET);
6256 }
6257 }
6258 if (fd != NULL)
6259 fclose(fd);
6260 }
6261 }
6262
6263 if (!undo)
6264 {
6265 fd = mch_fopen((char *)fname, "a");
6266 if (fd == NULL && new_spf)
6267 {
6268 char_u *p;
6269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006270 // We just initialized the 'spellfile' option and can't open the
6271 // file. We may need to create the "spell" directory first. We
6272 // already checked the runtime directory is writable in
6273 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006274 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6275 {
6276 int c = *p;
6277
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006278 // The directory doesn't exist. Try creating it and opening
6279 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006280 *p = NUL;
6281 vim_mkdir(fname, 0755);
6282 *p = c;
6283 fd = mch_fopen((char *)fname, "a");
6284 }
6285 }
6286
6287 if (fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006288 semsg(_(e_notopen), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006289 else
6290 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006291 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006292 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006293 else if (what == SPELL_ADD_RARE)
6294 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006295 else
6296 fprintf(fd, "%.*s\n", len, word);
6297 fclose(fd);
6298
6299 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006300 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006301 }
6302 }
6303
6304 if (fd != NULL)
6305 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006306 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006307 mkspell(1, &fname, FALSE, TRUE, TRUE);
6308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006309 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006310 if (buf != NULL)
6311 buf_reload(buf, buf->b_orig_mode);
6312
6313 redraw_all_later(SOME_VALID);
6314 }
6315 vim_free(fnamebuf);
6316}
6317
6318/*
6319 * Initialize 'spellfile' for the current buffer.
6320 */
6321 static void
6322init_spellfile(void)
6323{
6324 char_u *buf;
6325 int l;
6326 char_u *fname;
6327 char_u *rtp;
6328 char_u *lend;
6329 int aspath = FALSE;
6330 char_u *lstart = curbuf->b_s.b_p_spl;
6331
6332 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6333 {
6334 buf = alloc(MAXPATHL);
6335 if (buf == NULL)
6336 return;
6337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006338 // Find the end of the language name. Exclude the region. If there
6339 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006340 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6341 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6342 if (vim_ispathsep(*lend))
6343 {
6344 aspath = TRUE;
6345 lstart = lend + 1;
6346 }
6347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006348 // Loop over all entries in 'runtimepath'. Use the first one where we
6349 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006350 rtp = p_rtp;
6351 while (*rtp != NUL)
6352 {
6353 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006354 // Use directory of an entry with path, e.g., for
6355 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006356 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6357 lstart - curbuf->b_s.b_p_spl - 1);
6358 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006359 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006360 copy_option_part(&rtp, buf, MAXPATHL, ",");
6361 if (filewritable(buf) == 2)
6362 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006363 // Use the first language name from 'spelllang' and the
6364 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006365 if (aspath)
6366 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6367 lend - curbuf->b_s.b_p_spl);
6368 else
6369 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006370 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006371 l = (int)STRLEN(buf);
6372 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6373 if (filewritable(buf) != 2)
6374 vim_mkdir(buf, 0755);
6375
6376 l = (int)STRLEN(buf);
6377 vim_snprintf((char *)buf + l, MAXPATHL - l,
6378 "/%.*s", (int)(lend - lstart), lstart);
6379 }
6380 l = (int)STRLEN(buf);
6381 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6382 ->lp_slang->sl_fname;
6383 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6384 fname != NULL
6385 && strstr((char *)gettail(fname), ".ascii.") != NULL
6386 ? (char_u *)"ascii" : spell_enc());
6387 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
6388 break;
6389 }
6390 aspath = FALSE;
6391 }
6392
6393 vim_free(buf);
6394 }
6395}
6396
6397
6398
6399/*
6400 * Set the spell character tables from strings in the affix file.
6401 */
6402 static int
6403set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6404{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006405 // We build the new tables here first, so that we can compare with the
6406 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006407 spelltab_T new_st;
6408 char_u *pf = fol, *pl = low, *pu = upp;
6409 int f, l, u;
6410
6411 clear_spell_chartab(&new_st);
6412
6413 while (*pf != NUL)
6414 {
6415 if (*pl == NUL || *pu == NUL)
6416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006417 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006418 return FAIL;
6419 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006420 f = mb_ptr2char_adv(&pf);
6421 l = mb_ptr2char_adv(&pl);
6422 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006424 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006425 if (f < 256)
6426 new_st.st_isw[f] = TRUE;
6427 if (l < 256)
6428 new_st.st_isw[l] = TRUE;
6429 if (u < 256)
6430 new_st.st_isw[u] = TRUE;
6431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006432 // if "LOW" and "FOL" are not the same the "LOW" char needs
6433 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006434 if (l < 256 && l != f)
6435 {
6436 if (f >= 256)
6437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006438 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006439 return FAIL;
6440 }
6441 new_st.st_fold[l] = f;
6442 }
6443
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006444 // if "UPP" and "FOL" are not the same the "UPP" char needs
6445 // case-folding, it's upper case and the "UPP" is the upper case of
6446 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006447 if (u < 256 && u != f)
6448 {
6449 if (f >= 256)
6450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006451 emsg(_(e_affrange));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006452 return FAIL;
6453 }
6454 new_st.st_fold[u] = f;
6455 new_st.st_isu[u] = TRUE;
6456 new_st.st_upper[f] = u;
6457 }
6458 }
6459
6460 if (*pl != NUL || *pu != NUL)
6461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006462 emsg(_(e_affform));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006463 return FAIL;
6464 }
6465
6466 return set_spell_finish(&new_st);
6467}
6468
6469/*
6470 * Set the spell character tables from strings in the .spl file.
6471 */
6472 static void
6473set_spell_charflags(
6474 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006475 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006476 char_u *fol)
6477{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006478 // We build the new tables here first, so that we can compare with the
6479 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006480 spelltab_T new_st;
6481 int i;
6482 char_u *p = fol;
6483 int c;
6484
6485 clear_spell_chartab(&new_st);
6486
6487 for (i = 0; i < 128; ++i)
6488 {
6489 if (i < cnt)
6490 {
6491 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6492 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6493 }
6494
6495 if (*p != NUL)
6496 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006497 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006498 new_st.st_fold[i + 128] = c;
6499 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6500 new_st.st_upper[c] = i + 128;
6501 }
6502 }
6503
6504 (void)set_spell_finish(&new_st);
6505}
6506
6507 static int
6508set_spell_finish(spelltab_T *new_st)
6509{
6510 int i;
6511
6512 if (did_set_spelltab)
6513 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006514 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006515 for (i = 0; i < 256; ++i)
6516 {
6517 if (spelltab.st_isw[i] != new_st->st_isw[i]
6518 || spelltab.st_isu[i] != new_st->st_isu[i]
6519 || spelltab.st_fold[i] != new_st->st_fold[i]
6520 || spelltab.st_upper[i] != new_st->st_upper[i])
6521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006522 emsg(_("E763: Word characters differ between spell files"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006523 return FAIL;
6524 }
6525 }
6526 }
6527 else
6528 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006529 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006530 spelltab = *new_st;
6531 did_set_spelltab = TRUE;
6532 }
6533
6534 return OK;
6535}
6536
6537/*
6538 * Write the table with prefix conditions to the .spl file.
6539 * When "fd" is NULL only count the length of what is written.
6540 */
6541 static int
6542write_spell_prefcond(FILE *fd, garray_T *gap)
6543{
6544 int i;
6545 char_u *p;
6546 int len;
6547 int totlen;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006548 size_t x = 1; // collect return value of fwrite()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006549
6550 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006551 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006552
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006553 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006554
6555 for (i = 0; i < gap->ga_len; ++i)
6556 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006557 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006558 p = ((char_u **)gap->ga_data)[i];
6559 if (p != NULL)
6560 {
6561 len = (int)STRLEN(p);
6562 if (fd != NULL)
6563 {
6564 fputc(len, fd);
6565 x &= fwrite(p, (size_t)len, (size_t)1, fd);
6566 }
6567 totlen += len;
6568 }
6569 else if (fd != NULL)
6570 fputc(0, fd);
6571 }
6572
6573 return totlen;
6574}
6575
6576
6577/*
6578 * Use map string "map" for languages "lp".
6579 */
6580 static void
6581set_map_str(slang_T *lp, char_u *map)
6582{
6583 char_u *p;
6584 int headc = 0;
6585 int c;
6586 int i;
6587
6588 if (*map == NUL)
6589 {
6590 lp->sl_has_map = FALSE;
6591 return;
6592 }
6593 lp->sl_has_map = TRUE;
6594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006595 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006596 for (i = 0; i < 256; ++i)
6597 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006598 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006599
6600 /*
6601 * The similar characters are stored separated with slashes:
6602 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6603 * before the same slash. For characters above 255 sl_map_hash is used.
6604 */
6605 for (p = map; *p != NUL; )
6606 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006607 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006608 if (c == '/')
6609 headc = 0;
6610 else
6611 {
6612 if (headc == 0)
6613 headc = c;
6614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006615 // Characters above 255 don't fit in sl_map_array[], put them in
6616 // the hash table. Each entry is the char, a NUL the headchar and
6617 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006618 if (c >= 256)
6619 {
6620 int cl = mb_char2len(c);
6621 int headcl = mb_char2len(headc);
6622 char_u *b;
6623 hash_T hash;
6624 hashitem_T *hi;
6625
Bram Moolenaar964b3742019-05-24 18:54:09 +02006626 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006627 if (b == NULL)
6628 return;
6629 mb_char2bytes(c, b);
6630 b[cl] = NUL;
6631 mb_char2bytes(headc, b + cl + 1);
6632 b[cl + 1 + headcl] = NUL;
6633 hash = hash_hash(b);
6634 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6635 if (HASHITEM_EMPTY(hi))
6636 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6637 else
6638 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006639 // This should have been checked when generating the .spl
6640 // file.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006641 emsg(_("E783: duplicate char in MAP entry"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006642 vim_free(b);
6643 }
6644 }
6645 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006646 lp->sl_map_array[c] = headc;
6647 }
6648 }
6649}
6650
6651
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006652#endif // FEAT_SPELL