blob: b5db526ed953f7a31a20b48c0ac53a1a04fbccb7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spellfile.c: code for reading and writing spell files.
12 *
13 * See spell.c for information about spell checking.
14 */
15
16/*
17 * Vim spell file format: <HEADER>
18 * <SECTIONS>
19 * <LWORDTREE>
20 * <KWORDTREE>
21 * <PREFIXTREE>
22 *
23 * <HEADER>: <fileID> <versionnr>
24 *
25 * <fileID> 8 bytes "VIMspell"
26 * <versionnr> 1 byte VIMSPELLVERSION
27 *
28 *
29 * Sections make it possible to add information to the .spl file without
30 * making it incompatible with previous versions. There are two kinds of
31 * sections:
32 * 1. Not essential for correct spell checking. E.g. for making suggestions.
33 * These are skipped when not supported.
34 * 2. Optional information, but essential for spell checking when present.
35 * E.g. conditions for affixes. When this section is present but not
36 * supported an error message is given.
37 *
38 * <SECTIONS>: <section> ... <sectionend>
39 *
40 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
41 *
42 * <sectionID> 1 byte number from 0 to 254 identifying the section
43 *
44 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
45 * spell checking
46 *
47 * <sectionlen> 4 bytes length of section contents, MSB first
48 *
49 * <sectionend> 1 byte SN_END
50 *
51 *
52 * sectionID == SN_INFO: <infotext>
53 * <infotext> N bytes free format text with spell file info (version,
54 * website, etc)
55 *
56 * sectionID == SN_REGION: <regionname> ...
Bram Moolenaar2993ac52018-02-10 14:12:43 +010057 * <regionname> 2 bytes Up to MAXREGIONS region names: ca, au, etc. Lower
58 * case. First <regionname> is region 1.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +020059 *
60 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
61 * <folcharslen> <folchars>
62 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
63 * <charflags> N bytes List of flags (first one is for character 128):
64 * 0x01 word character CF_WORD
65 * 0x02 upper-case character CF_UPPER
66 * <folcharslen> 2 bytes Number of bytes in <folchars>.
67 * <folchars> N bytes Folded characters, first one is for character 128.
68 *
69 * sectionID == SN_MIDWORD: <midword>
70 * <midword> N bytes Characters that are word characters only when used
71 * in the middle of a word.
72 *
73 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
74 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
75 * <prefcond> : <condlen> <condstr>
76 * <condlen> 1 byte Length of <condstr>.
77 * <condstr> N bytes Condition for the prefix.
78 *
79 * sectionID == SN_REP: <repcount> <rep> ...
80 * <repcount> 2 bytes number of <rep> items, MSB first.
81 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
82 * <repfromlen> 1 byte length of <repfrom>
83 * <repfrom> N bytes "from" part of replacement
84 * <reptolen> 1 byte length of <repto>
85 * <repto> N bytes "to" part of replacement
86 *
87 * sectionID == SN_REPSAL: <repcount> <rep> ...
88 * just like SN_REP but for soundfolded words
89 *
90 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
91 * <salflags> 1 byte flags for soundsalike conversion:
92 * SAL_F0LLOWUP
93 * SAL_COLLAPSE
94 * SAL_REM_ACCENTS
95 * <salcount> 2 bytes number of <sal> items following
96 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
97 * <salfromlen> 1 byte length of <salfrom>
98 * <salfrom> N bytes "from" part of soundsalike
99 * <saltolen> 1 byte length of <salto>
100 * <salto> N bytes "to" part of soundsalike
101 *
102 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
103 * <sofofromlen> 2 bytes length of <sofofrom>
104 * <sofofrom> N bytes "from" part of soundfold
105 * <sofotolen> 2 bytes length of <sofoto>
106 * <sofoto> N bytes "to" part of soundfold
107 *
108 * sectionID == SN_SUGFILE: <timestamp>
109 * <timestamp> 8 bytes time in seconds that must match with .sug file
110 *
111 * sectionID == SN_NOSPLITSUGS: nothing
112 *
113 * sectionID == SN_NOCOMPOUNDSUGS: nothing
114 *
115 * sectionID == SN_WORDS: <word> ...
116 * <word> N bytes NUL terminated common word
117 *
118 * sectionID == SN_MAP: <mapstr>
119 * <mapstr> N bytes String with sequences of similar characters,
120 * separated by slashes.
121 *
122 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
123 * <comppatcount> <comppattern> ... <compflags>
124 * <compmax> 1 byte Maximum nr of words in compound word.
125 * <compminlen> 1 byte Minimal word length for compounding.
126 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
127 * <compoptions> 2 bytes COMP_ flags.
128 * <comppatcount> 2 bytes number of <comppattern> following
129 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
130 * slashes.
131 *
132 * <comppattern>: <comppatlen> <comppattext>
133 * <comppatlen> 1 byte length of <comppattext>
134 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
135 *
136 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
137 *
138 * sectionID == SN_SYLLABLE: <syllable>
139 * <syllable> N bytes String from SYLLABLE item.
140 *
141 * <LWORDTREE>: <wordtree>
142 *
143 * <KWORDTREE>: <wordtree>
144 *
145 * <PREFIXTREE>: <wordtree>
146 *
147 *
148 * <wordtree>: <nodecount> <nodedata> ...
149 *
150 * <nodecount> 4 bytes Number of nodes following. MSB first.
151 *
152 * <nodedata>: <siblingcount> <sibling> ...
153 *
154 * <siblingcount> 1 byte Number of siblings in this node. The siblings
155 * follow in sorted order.
156 *
157 * <sibling>: <byte> [ <nodeidx> <xbyte>
158 * | <flags> [<flags2>] [<region>] [<affixID>]
159 * | [<pflags>] <affixID> <prefcondnr> ]
160 *
161 * <byte> 1 byte Byte value of the sibling. Special cases:
162 * BY_NOFLAGS: End of word without flags and for all
163 * regions.
164 * For PREFIXTREE <affixID> and
165 * <prefcondnr> follow.
166 * BY_FLAGS: End of word, <flags> follow.
167 * For PREFIXTREE <pflags>, <affixID>
168 * and <prefcondnr> follow.
169 * BY_FLAGS2: End of word, <flags> and <flags2>
170 * follow. Not used in PREFIXTREE.
171 * BY_INDEX: Child of sibling is shared, <nodeidx>
172 * and <xbyte> follow.
173 *
174 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
175 *
176 * <xbyte> 1 byte byte value of the sibling.
177 *
178 * <flags> 1 byte bitmask of:
179 * WF_ALLCAP word must have only capitals
180 * WF_ONECAP first char of word must be capital
181 * WF_KEEPCAP keep-case word
182 * WF_FIXCAP keep-case word, all caps not allowed
183 * WF_RARE rare word
184 * WF_BANNED bad word
185 * WF_REGION <region> follows
186 * WF_AFX <affixID> follows
187 *
188 * <flags2> 1 byte Bitmask of:
189 * WF_HAS_AFF >> 8 word includes affix
190 * WF_NEEDCOMP >> 8 word only valid in compound
191 * WF_NOSUGGEST >> 8 word not used for suggestions
192 * WF_COMPROOT >> 8 word already a compound
193 * WF_NOCOMPBEF >> 8 no compounding before this word
194 * WF_NOCOMPAFT >> 8 no compounding after this word
195 *
196 * <pflags> 1 byte bitmask of:
197 * WFP_RARE rare prefix
198 * WFP_NC non-combining prefix
199 * WFP_UP letter after prefix made upper case
200 *
201 * <region> 1 byte Bitmask for regions in which word is valid. When
202 * omitted it's valid in all regions.
203 * Lowest bit is for region 1.
204 *
205 * <affixID> 1 byte ID of affix that can be used with this word. In
206 * PREFIXTREE used for the required prefix ID.
207 *
208 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
209 * from HEADER.
210 *
211 * All text characters are in 'encoding', but stored as single bytes.
212 */
213
214/*
215 * Vim .sug file format: <SUGHEADER>
216 * <SUGWORDTREE>
217 * <SUGTABLE>
218 *
219 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
220 *
221 * <fileID> 6 bytes "VIMsug"
222 * <versionnr> 1 byte VIMSUGVERSION
223 * <timestamp> 8 bytes timestamp that must match with .spl file
224 *
225 *
226 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
227 *
228 *
229 * <SUGTABLE>: <sugwcount> <sugline> ...
230 *
231 * <sugwcount> 4 bytes number of <sugline> following
232 *
233 * <sugline>: <sugnr> ... NUL
234 *
235 * <sugnr>: X bytes word number that results in this soundfolded word,
236 * stored as an offset to the previous number in as
237 * few bytes as possible, see offset2bytes())
238 */
239
240#include "vim.h"
241
242#if defined(FEAT_SPELL) || defined(PROTO)
243
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100244#ifndef UNIX // it's in os_unix.h for Unix
245# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200246#endif
247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100248#ifndef UNIX // it's in os_unix.h for Unix
249# include <time.h> // for time_t
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200250#endif
251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100252// Special byte values for <byte>. Some are only used in the tree for
253// postponed prefixes, some only in the other trees. This is a bit messy...
254#define BY_NOFLAGS 0 // end of word without flags or region; for
255 // postponed prefix: no <pflags>
256#define BY_INDEX 1 // child is shared, index follows
257#define BY_FLAGS 2 // end of word, <flags> byte follows; for
258 // postponed prefix: <pflags> follows
259#define BY_FLAGS2 3 // end of word, <flags> and <flags2> bytes
260 // follow; never used in prefix tree
261#define BY_SPECIAL BY_FLAGS2 // highest special byte value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200262
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +0100263#define ZERO_FLAG 65009 // used when flag is zero: "0"
264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100265// Flags used in .spl file for soundsalike flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200266#define SAL_F0LLOWUP 1
267#define SAL_COLLAPSE 2
268#define SAL_REM_ACCENTS 4
269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100270#define VIMSPELLMAGIC "VIMspell" // string at start of Vim spell file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200271#define VIMSPELLMAGICL 8
272#define VIMSPELLVERSION 50
273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100274// Section IDs. Only renumber them when VIMSPELLVERSION changes!
275#define SN_REGION 0 // <regionname> section
276#define SN_CHARFLAGS 1 // charflags section
277#define SN_MIDWORD 2 // <midword> section
278#define SN_PREFCOND 3 // <prefcond> section
279#define SN_REP 4 // REP items section
280#define SN_SAL 5 // SAL items section
281#define SN_SOFO 6 // soundfolding section
282#define SN_MAP 7 // MAP items section
283#define SN_COMPOUND 8 // compound words section
284#define SN_SYLLABLE 9 // syllable section
285#define SN_NOBREAK 10 // NOBREAK section
286#define SN_SUGFILE 11 // timestamp for .sug file
287#define SN_REPSAL 12 // REPSAL items section
288#define SN_WORDS 13 // common words
289#define SN_NOSPLITSUGS 14 // don't split word for suggestions
290#define SN_INFO 15 // info section
291#define SN_NOCOMPOUNDSUGS 16 // don't compound for suggestions
292#define SN_END 255 // end of sections
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200293
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100294#define SNF_REQUIRED 1 // <sectionflags>: required section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200295
296#define CF_WORD 0x01
297#define CF_UPPER 0x02
298
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200299/*
300 * Loop through all the siblings of a node (including the node)
301 */
302#define FOR_ALL_NODE_SIBLINGS(node, np) \
303 for ((np) = (node); (np) != NULL; (np) = (np)->wn_sibling)
304
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200305static int set_spell_finish(spelltab_T *new_st);
=?UTF-8?q?Bj=C3=B6rn=20Linse?=1daedc82021-12-10 20:39:17 +0000306static int write_spell_prefcond(FILE *fd, garray_T *gap, size_t *fwv);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200307static int read_region_section(FILE *fd, slang_T *slang, int len);
308static int read_charflags_section(FILE *fd);
309static int read_prefcond_section(FILE *fd, slang_T *lp);
310static int read_rep_section(FILE *fd, garray_T *gap, short *first);
311static int read_sal_section(FILE *fd, slang_T *slang);
312static int read_words_section(FILE *fd, slang_T *lp, int len);
313static int read_sofo_section(FILE *fd, slang_T *slang);
314static int read_compound(FILE *fd, slang_T *slang, int len);
315static int set_sofo(slang_T *lp, char_u *from, char_u *to);
316static void set_sal_first(slang_T *lp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200317static int *mb_str2wide(char_u *s);
Bram Moolenaar07399e72020-08-24 20:05:50 +0200318static int spell_read_tree(FILE *fd, char_u **bytsp, long *bytsp_len, idx_T **idxsp, int prefixtree, int prefixcnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200319static idx_T read_tree_node(FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200320static void set_spell_charflags(char_u *flags, int cnt, char_u *upp);
321static int set_spell_chartab(char_u *fol, char_u *low, char_u *upp);
322static void set_map_str(slang_T *lp, char_u *map);
323
324
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200325static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
326static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200327static char *msg_compressing = N_("Compressing word tree...");
328
329/*
330 * Load one spell file and store the info into a slang_T.
331 *
332 * This is invoked in three ways:
333 * - From spell_load_cb() to load a spell file for the first time. "lang" is
334 * the language name, "old_lp" is NULL. Will allocate an slang_T.
335 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
336 * points to the existing slang_T.
337 * - Just after writing a .spl file; it's read back to produce the .sug file.
338 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
339 *
340 * Returns the slang_T the spell file was loaded into. NULL for error.
341 */
342 slang_T *
343spell_load_file(
344 char_u *fname,
345 char_u *lang,
346 slang_T *old_lp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100347 int silent) // no error if file doesn't exist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200348{
349 FILE *fd;
350 char_u buf[VIMSPELLMAGICL];
351 char_u *p;
352 int i;
353 int n;
354 int len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200355 slang_T *lp = NULL;
356 int c = 0;
357 int res;
Bram Moolenaarce6db022020-01-07 20:11:42 +0100358 int did_estack_push = FALSE;
Bram Moolenaare31ee862020-01-07 20:59:34 +0100359 ESTACK_CHECK_DECLARATION
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200360
361 fd = mch_fopen((char *)fname, "r");
362 if (fd == NULL)
363 {
364 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000365 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200366 else if (p_verbose > 2)
367 {
368 verbose_enter();
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000369 smsg((const char *)e_cant_open_file_str, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200370 verbose_leave();
371 }
372 goto endFAIL;
373 }
374 if (p_verbose > 2)
375 {
376 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100377 smsg(_("Reading spell file \"%s\""), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200378 verbose_leave();
379 }
380
381 if (old_lp == NULL)
382 {
383 lp = slang_alloc(lang);
384 if (lp == NULL)
385 goto endFAIL;
386
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100387 // Remember the file name, used to reload the file when it's updated.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200388 lp->sl_fname = vim_strsave(fname);
389 if (lp->sl_fname == NULL)
390 goto endFAIL;
391
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100392 // Check for .add.spl (_add.spl for VMS).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200393 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
394 }
395 else
396 lp = old_lp;
397
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100398 // Set sourcing_name, so that error messages mention the file name.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100399 estack_push(ETYPE_SPELL, fname, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +0100400 ESTACK_CHECK_SETUP
Bram Moolenaarce6db022020-01-07 20:11:42 +0100401 did_estack_push = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200402
403 /*
404 * <HEADER>: <fileID>
405 */
406 for (i = 0; i < VIMSPELLMAGICL; ++i)
Bram Moolenaar963ab262022-09-05 10:55:27 +0100407 buf[i] = (c = getc(fd)) == EOF ? 0 : c; // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200408 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
409 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000410 emsg(_(e_this_does_not_look_like_spell_file));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200411 goto endFAIL;
412 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100413 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200414 if (c < VIMSPELLVERSION)
415 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000416 emsg(_(e_old_spell_file_needs_to_be_updated));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200417 goto endFAIL;
418 }
419 else if (c > VIMSPELLVERSION)
420 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000421 emsg(_(e_spell_file_is_for_newer_version_of_vim));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200422 goto endFAIL;
423 }
424
425
426 /*
427 * <SECTIONS>: <section> ... <sectionend>
428 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
429 */
430 for (;;)
431 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100432 n = getc(fd); // <sectionID> or <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200433 if (n == SN_END)
434 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100435 c = getc(fd); // <sectionflags>
436 len = get4c(fd); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200437 if (len < 0)
438 goto truncerr;
439
440 res = 0;
441 switch (n)
442 {
443 case SN_INFO:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100444 lp->sl_info = read_string(fd, len); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200445 if (lp->sl_info == NULL)
446 goto endFAIL;
447 break;
448
449 case SN_REGION:
450 res = read_region_section(fd, lp, len);
451 break;
452
453 case SN_CHARFLAGS:
454 res = read_charflags_section(fd);
455 break;
456
457 case SN_MIDWORD:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100458 lp->sl_midword = read_string(fd, len); // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200459 if (lp->sl_midword == NULL)
460 goto endFAIL;
461 break;
462
463 case SN_PREFCOND:
464 res = read_prefcond_section(fd, lp);
465 break;
466
467 case SN_REP:
468 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
469 break;
470
471 case SN_REPSAL:
472 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
473 break;
474
475 case SN_SAL:
476 res = read_sal_section(fd, lp);
477 break;
478
479 case SN_SOFO:
480 res = read_sofo_section(fd, lp);
481 break;
482
483 case SN_MAP:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100484 p = read_string(fd, len); // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200485 if (p == NULL)
486 goto endFAIL;
487 set_map_str(lp, p);
488 vim_free(p);
489 break;
490
491 case SN_WORDS:
492 res = read_words_section(fd, lp, len);
493 break;
494
495 case SN_SUGFILE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100496 lp->sl_sugtime = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200497 break;
498
499 case SN_NOSPLITSUGS:
500 lp->sl_nosplitsugs = TRUE;
501 break;
502
503 case SN_NOCOMPOUNDSUGS:
504 lp->sl_nocompoundsugs = TRUE;
505 break;
506
507 case SN_COMPOUND:
508 res = read_compound(fd, lp, len);
509 break;
510
511 case SN_NOBREAK:
512 lp->sl_nobreak = TRUE;
513 break;
514
515 case SN_SYLLABLE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100516 lp->sl_syllable = read_string(fd, len); // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200517 if (lp->sl_syllable == NULL)
518 goto endFAIL;
Bram Moolenaarfc2a47f2020-08-20 15:41:55 +0200519 if (init_syl_tab(lp) != OK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200520 goto endFAIL;
521 break;
522
523 default:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100524 // Unsupported section. When it's required give an error
525 // message. When it's not required skip the contents.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200526 if (c & SNF_REQUIRED)
527 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000528 emsg(_(e_unsupported_section_in_spell_file));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200529 goto endFAIL;
530 }
531 while (--len >= 0)
532 if (getc(fd) < 0)
533 goto truncerr;
534 break;
535 }
536someerror:
537 if (res == SP_FORMERROR)
538 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000539 emsg(_(e_format_error_in_spell_file));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200540 goto endFAIL;
541 }
542 if (res == SP_TRUNCERROR)
543 {
544truncerr:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000545 emsg(_(e_truncated_spell_file));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200546 goto endFAIL;
547 }
548 if (res == SP_OTHERERROR)
549 goto endFAIL;
550 }
551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100552 // <LWORDTREE>
Bram Moolenaar07399e72020-08-24 20:05:50 +0200553 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fbyts_len,
554 &lp->sl_fidxs, FALSE, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200555 if (res != 0)
556 goto someerror;
557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100558 // <KWORDTREE>
Bram Moolenaar07399e72020-08-24 20:05:50 +0200559 res = spell_read_tree(fd, &lp->sl_kbyts, NULL, &lp->sl_kidxs, FALSE, 0);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200560 if (res != 0)
561 goto someerror;
562
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100563 // <PREFIXTREE>
Bram Moolenaar07399e72020-08-24 20:05:50 +0200564 res = spell_read_tree(fd, &lp->sl_pbyts, NULL, &lp->sl_pidxs, TRUE,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200565 lp->sl_prefixcnt);
566 if (res != 0)
567 goto someerror;
568
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100569 // For a new file link it in the list of spell files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200570 if (old_lp == NULL && lang != NULL)
571 {
572 lp->sl_next = first_lang;
573 first_lang = lp;
574 }
575
576 goto endOK;
577
578endFAIL:
579 if (lang != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100580 // truncating the name signals the error to spell_load_lang()
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200581 *lang = NUL;
582 if (lp != NULL && old_lp == NULL)
583 slang_free(lp);
584 lp = NULL;
585
586endOK:
587 if (fd != NULL)
588 fclose(fd);
Bram Moolenaarce6db022020-01-07 20:11:42 +0100589 if (did_estack_push)
Bram Moolenaare31ee862020-01-07 20:59:34 +0100590 {
591 ESTACK_CHECK_NOW
Bram Moolenaarce6db022020-01-07 20:11:42 +0100592 estack_pop();
Bram Moolenaare31ee862020-01-07 20:59:34 +0100593 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200594
595 return lp;
596}
597
598/*
599 * Fill in the wordcount fields for a trie.
600 * Returns the total number of words.
601 */
602 static void
603tree_count_words(char_u *byts, idx_T *idxs)
604{
605 int depth;
606 idx_T arridx[MAXWLEN];
607 int curi[MAXWLEN];
608 int c;
609 idx_T n;
610 int wordcount[MAXWLEN];
611
612 arridx[0] = 0;
613 curi[0] = 1;
614 wordcount[0] = 0;
615 depth = 0;
616 while (depth >= 0 && !got_int)
617 {
618 if (curi[depth] > byts[arridx[depth]])
619 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100620 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200621 idxs[arridx[depth]] = wordcount[depth];
622 if (depth > 0)
623 wordcount[depth - 1] += wordcount[depth];
624
625 --depth;
626 fast_breakcheck();
627 }
628 else
629 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100630 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200631 n = arridx[depth] + curi[depth];
632 ++curi[depth];
633
634 c = byts[n];
635 if (c == 0)
636 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100637 // End of word, count it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200638 ++wordcount[depth];
639
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100640 // Skip over any other NUL bytes (same word with different
641 // flags).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200642 while (byts[n + 1] == 0)
643 {
644 ++n;
645 ++curi[depth];
646 }
647 }
648 else
649 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100650 // Normal char, go one level deeper to count the words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200651 ++depth;
652 arridx[depth] = idxs[n];
653 curi[depth] = 1;
654 wordcount[depth] = 0;
655 }
656 }
657 }
658}
659
660/*
661 * Load the .sug files for languages that have one and weren't loaded yet.
662 */
663 void
664suggest_load_files(void)
665{
666 langp_T *lp;
667 int lpi;
668 slang_T *slang;
669 char_u *dotp;
670 FILE *fd;
671 char_u buf[MAXWLEN];
672 int i;
673 time_t timestamp;
674 int wcount;
675 int wordnr;
676 garray_T ga;
677 int c;
678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100679 // Do this for all languages that support sound folding.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200680 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
681 {
682 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
683 slang = lp->lp_slang;
684 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
685 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100686 // Change ".spl" to ".sug" and open the file. When the file isn't
687 // found silently skip it. Do set "sl_sugloaded" so that we
688 // don't try again and again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200689 slang->sl_sugloaded = TRUE;
690
691 dotp = vim_strrchr(slang->sl_fname, '.');
692 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
693 continue;
694 STRCPY(dotp, ".sug");
695 fd = mch_fopen((char *)slang->sl_fname, "r");
696 if (fd == NULL)
697 goto nextone;
698
699 /*
700 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
701 */
702 for (i = 0; i < VIMSUGMAGICL; ++i)
Bram Moolenaar963ab262022-09-05 10:55:27 +0100703 buf[i] = (c = getc(fd)) == EOF ? 0 : c; // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200704 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
705 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000706 semsg(_(e_this_does_not_look_like_sug_file_str),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200707 slang->sl_fname);
708 goto nextone;
709 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100710 c = getc(fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200711 if (c < VIMSUGVERSION)
712 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000713 semsg(_(e_old_sug_file_needs_to_be_updated_str),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200714 slang->sl_fname);
715 goto nextone;
716 }
717 else if (c > VIMSUGVERSION)
718 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000719 semsg(_(e_sug_file_is_for_newer_version_of_vim_str),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200720 slang->sl_fname);
721 goto nextone;
722 }
723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100724 // Check the timestamp, it must be exactly the same as the one in
725 // the .spl file. Otherwise the word numbers won't match.
726 timestamp = get8ctime(fd); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200727 if (timestamp != slang->sl_sugtime)
728 {
Bram Moolenaar677658a2022-01-05 16:09:06 +0000729 semsg(_(e_sug_file_doesnt_match_spl_file_str),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200730 slang->sl_fname);
731 goto nextone;
732 }
733
734 /*
735 * <SUGWORDTREE>: <wordtree>
736 * Read the trie with the soundfolded words.
737 */
Bram Moolenaar07399e72020-08-24 20:05:50 +0200738 if (spell_read_tree(fd, &slang->sl_sbyts, NULL, &slang->sl_sidxs,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200739 FALSE, 0) != 0)
740 {
741someerror:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000742 semsg(_(e_error_while_reading_sug_file_str),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200743 slang->sl_fname);
744 slang_clear_sug(slang);
745 goto nextone;
746 }
747
748 /*
749 * <SUGTABLE>: <sugwcount> <sugline> ...
750 *
751 * Read the table with word numbers. We use a file buffer for
752 * this, because it's so much like a file with lines. Makes it
753 * possible to swap the info and save on memory use.
754 */
755 slang->sl_sugbuf = open_spellbuf();
756 if (slang->sl_sugbuf == NULL)
757 goto someerror;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100758 // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200759 wcount = get4c(fd);
760 if (wcount < 0)
761 goto someerror;
762
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100763 // Read all the wordnr lists into the buffer, one NUL terminated
764 // list per line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200765 ga_init2(&ga, 1, 100);
766 for (wordnr = 0; wordnr < wcount; ++wordnr)
767 {
768 ga.ga_len = 0;
769 for (;;)
770 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100771 c = getc(fd); // <sugline>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200772 if (c < 0 || ga_grow(&ga, 1) == FAIL)
773 goto someerror;
774 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
775 if (c == NUL)
776 break;
777 }
778 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
779 ga.ga_data, ga.ga_len, TRUE) == FAIL)
780 goto someerror;
781 }
782 ga_clear(&ga);
783
784 /*
785 * Need to put word counts in the word tries, so that we can find
786 * a word by its number.
787 */
788 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
789 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
790
791nextone:
792 if (fd != NULL)
793 fclose(fd);
794 STRCPY(dotp, ".spl");
795 }
796 }
797}
798
799
800/*
801 * Read a length field from "fd" in "cnt_bytes" bytes.
802 * Allocate memory, read the string into it and add a NUL at the end.
803 * Returns NULL when the count is zero.
804 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
805 * otherwise.
806 */
807 static char_u *
808read_cnt_string(FILE *fd, int cnt_bytes, int *cntp)
809{
810 int cnt = 0;
811 int i;
812 char_u *str;
813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100814 // read the length bytes, MSB first
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200815 for (i = 0; i < cnt_bytes; ++i)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200816 {
Bram Moolenaar4ad739f2020-09-02 10:25:45 +0200817 int c = getc(fd);
818
819 if (c == EOF)
820 {
821 *cntp = SP_TRUNCERROR;
822 return NULL;
823 }
824 cnt = (cnt << 8) + (unsigned)c;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200825 }
826 *cntp = cnt;
827 if (cnt == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100828 return NULL; // nothing to read, return NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200829
830 str = read_string(fd, cnt);
831 if (str == NULL)
832 *cntp = SP_OTHERERROR;
833 return str;
834}
835
836/*
837 * Read SN_REGION: <regionname> ...
838 * Return SP_*ERROR flags.
839 */
840 static int
841read_region_section(FILE *fd, slang_T *lp, int len)
842{
843 int i;
Bram Moolenaarc1eb1312022-09-04 13:45:15 +0100844 int c = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200845
Bram Moolenaar2993ac52018-02-10 14:12:43 +0100846 if (len > MAXREGIONS * 2)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200847 return SP_FORMERROR;
848 for (i = 0; i < len; ++i)
Bram Moolenaarc7d2ff22022-09-05 11:04:14 +0100849 lp->sl_regions[i] = (c = getc(fd)) == EOF ? 0 : c; // <regionname>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200850 lp->sl_regions[len] = NUL;
Bram Moolenaar3c770762022-09-04 11:55:19 +0100851 return c == EOF ? SP_TRUNCERROR : 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200852}
853
854/*
855 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
856 * <folcharslen> <folchars>
857 * Return SP_*ERROR flags.
858 */
859 static int
860read_charflags_section(FILE *fd)
861{
862 char_u *flags;
863 char_u *fol;
864 int flagslen, follen;
865
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100866 // <charflagslen> <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200867 flags = read_cnt_string(fd, 1, &flagslen);
868 if (flagslen < 0)
869 return flagslen;
870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100871 // <folcharslen> <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200872 fol = read_cnt_string(fd, 2, &follen);
873 if (follen < 0)
874 {
875 vim_free(flags);
876 return follen;
877 }
878
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100879 // Set the word-char flags and fill SPELL_ISUPPER() table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200880 if (flags != NULL && fol != NULL)
881 set_spell_charflags(flags, flagslen, fol);
882
883 vim_free(flags);
884 vim_free(fol);
885
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100886 // When <charflagslen> is zero then <fcharlen> must also be zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200887 if ((flags == NULL) != (fol == NULL))
888 return SP_FORMERROR;
889 return 0;
890}
891
892/*
893 * Read SN_PREFCOND section.
894 * Return SP_*ERROR flags.
895 */
896 static int
897read_prefcond_section(FILE *fd, slang_T *lp)
898{
899 int cnt;
900 int i;
901 int n;
Bram Moolenaar3c770762022-09-04 11:55:19 +0100902 int c;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200903 char_u *p;
904 char_u buf[MAXWLEN + 1];
905
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100906 // <prefcondcnt> <prefcond> ...
907 cnt = get2c(fd); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200908 if (cnt <= 0)
909 return SP_FORMERROR;
910
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200911 lp->sl_prefprog = ALLOC_CLEAR_MULT(regprog_T *, cnt);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200912 if (lp->sl_prefprog == NULL)
913 return SP_OTHERERROR;
914 lp->sl_prefixcnt = cnt;
915
916 for (i = 0; i < cnt; ++i)
917 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100918 // <prefcond> : <condlen> <condstr>
919 n = getc(fd); // <condlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200920 if (n < 0 || n >= MAXWLEN)
921 return SP_FORMERROR;
922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100923 // When <condlen> is zero we have an empty condition. Otherwise
924 // compile the regexp program used to check for the condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200925 if (n > 0)
926 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100927 buf[0] = '^'; // always match at one position only
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200928 p = buf + 1;
929 while (n-- > 0)
Bram Moolenaarc7d2ff22022-09-05 11:04:14 +0100930 *p++ = (c = getc(fd)) == EOF ? 0 : c; // <condstr>
Bram Moolenaar3c770762022-09-04 11:55:19 +0100931 if (c == EOF)
932 break;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200933 *p = NUL;
934 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
935 }
936 }
937 return 0;
938}
939
940/*
941 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
942 * Return SP_*ERROR flags.
943 */
944 static int
945read_rep_section(FILE *fd, garray_T *gap, short *first)
946{
947 int cnt;
948 fromto_T *ftp;
949 int i;
950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100951 cnt = get2c(fd); // <repcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200952 if (cnt < 0)
953 return SP_TRUNCERROR;
954
955 if (ga_grow(gap, cnt) == FAIL)
956 return SP_OTHERERROR;
957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100958 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200959 for (; gap->ga_len < cnt; ++gap->ga_len)
960 {
961 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
962 ftp->ft_from = read_cnt_string(fd, 1, &i);
963 if (i < 0)
964 return i;
965 if (i == 0)
966 return SP_FORMERROR;
967 ftp->ft_to = read_cnt_string(fd, 1, &i);
968 if (i <= 0)
969 {
970 vim_free(ftp->ft_from);
971 if (i < 0)
972 return i;
973 return SP_FORMERROR;
974 }
975 }
976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100977 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +0200978 for (i = 0; i < 256; ++i)
979 first[i] = -1;
980 for (i = 0; i < gap->ga_len; ++i)
981 {
982 ftp = &((fromto_T *)gap->ga_data)[i];
983 if (first[*ftp->ft_from] == -1)
984 first[*ftp->ft_from] = i;
985 }
986 return 0;
987}
988
989/*
990 * Read SN_SAL section: <salflags> <salcount> <sal> ...
991 * Return SP_*ERROR flags.
992 */
993 static int
994read_sal_section(FILE *fd, slang_T *slang)
995{
996 int i;
997 int cnt;
998 garray_T *gap;
999 salitem_T *smp;
1000 int ccnt;
1001 char_u *p;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001002
1003 slang->sl_sofo = FALSE;
1004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001005 i = getc(fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001006 if (i & SAL_F0LLOWUP)
1007 slang->sl_followup = TRUE;
1008 if (i & SAL_COLLAPSE)
1009 slang->sl_collapse = TRUE;
1010 if (i & SAL_REM_ACCENTS)
1011 slang->sl_rem_accents = TRUE;
1012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001013 cnt = get2c(fd); // <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001014 if (cnt < 0)
1015 return SP_TRUNCERROR;
1016
1017 gap = &slang->sl_sal;
1018 ga_init2(gap, sizeof(salitem_T), 10);
1019 if (ga_grow(gap, cnt + 1) == FAIL)
1020 return SP_OTHERERROR;
1021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001022 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001023 for (; gap->ga_len < cnt; ++gap->ga_len)
1024 {
Bram Moolenaar97d2f342020-07-10 20:03:03 +02001025 int c = NUL;
1026
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001027 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001028 ccnt = getc(fd); // <salfromlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001029 if (ccnt < 0)
1030 return SP_TRUNCERROR;
1031 if ((p = alloc(ccnt + 2)) == NULL)
1032 return SP_OTHERERROR;
1033 smp->sm_lead = p;
1034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001035 // Read up to the first special char into sm_lead.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001036 for (i = 0; i < ccnt; ++i)
1037 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001038 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001039 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
1040 break;
1041 *p++ = c;
1042 }
1043 smp->sm_leadlen = (int)(p - smp->sm_lead);
1044 *p++ = NUL;
1045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001046 // Put (abc) chars in sm_oneof, if any.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001047 if (c == '(')
1048 {
1049 smp->sm_oneof = p;
1050 for (++i; i < ccnt; ++i)
1051 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001052 c = getc(fd); // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001053 if (c == ')')
1054 break;
1055 *p++ = c;
1056 }
1057 *p++ = NUL;
1058 if (++i < ccnt)
1059 c = getc(fd);
1060 }
1061 else
1062 smp->sm_oneof = NULL;
1063
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001064 // Any following chars go in sm_rules.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001065 smp->sm_rules = p;
1066 if (i < ccnt)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001067 // store the char we got while checking for end of sm_lead
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001068 *p++ = c;
1069 for (++i; i < ccnt; ++i)
Bram Moolenaarc7d2ff22022-09-05 11:04:14 +01001070 *p++ = (c = getc(fd)) == EOF ? 0 : c; // <salfrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001071 *p++ = NUL;
1072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001073 // <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001074 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
1075 if (ccnt < 0)
1076 {
1077 vim_free(smp->sm_lead);
1078 return ccnt;
1079 }
1080
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001081 if (has_mbyte)
1082 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001083 // convert the multi-byte strings to wide char strings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001084 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1085 smp->sm_leadlen = mb_charlen(smp->sm_lead);
1086 if (smp->sm_oneof == NULL)
1087 smp->sm_oneof_w = NULL;
1088 else
1089 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
1090 if (smp->sm_to == NULL)
1091 smp->sm_to_w = NULL;
1092 else
1093 smp->sm_to_w = mb_str2wide(smp->sm_to);
1094 if (smp->sm_lead_w == NULL
1095 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
1096 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
1097 {
1098 vim_free(smp->sm_lead);
1099 vim_free(smp->sm_to);
1100 vim_free(smp->sm_lead_w);
1101 vim_free(smp->sm_oneof_w);
1102 vim_free(smp->sm_to_w);
1103 return SP_OTHERERROR;
1104 }
1105 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001106 }
1107
1108 if (gap->ga_len > 0)
1109 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001110 // Add one extra entry to mark the end with an empty sm_lead. Avoids
1111 // that we need to check the index every time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001112 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
1113 if ((p = alloc(1)) == NULL)
1114 return SP_OTHERERROR;
1115 p[0] = NUL;
1116 smp->sm_lead = p;
1117 smp->sm_leadlen = 0;
1118 smp->sm_oneof = NULL;
1119 smp->sm_rules = p;
1120 smp->sm_to = NULL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001121 if (has_mbyte)
1122 {
1123 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
1124 smp->sm_leadlen = 0;
1125 smp->sm_oneof_w = NULL;
1126 smp->sm_to_w = NULL;
1127 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001128 ++gap->ga_len;
1129 }
1130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001131 // Fill the first-index table.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001132 set_sal_first(slang);
1133
1134 return 0;
1135}
1136
1137/*
1138 * Read SN_WORDS: <word> ...
1139 * Return SP_*ERROR flags.
1140 */
1141 static int
1142read_words_section(FILE *fd, slang_T *lp, int len)
1143{
1144 int done = 0;
1145 int i;
1146 int c;
1147 char_u word[MAXWLEN];
1148
1149 while (done < len)
1150 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001151 // Read one word at a time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001152 for (i = 0; ; ++i)
1153 {
1154 c = getc(fd);
1155 if (c == EOF)
1156 return SP_TRUNCERROR;
1157 word[i] = c;
1158 if (word[i] == NUL)
1159 break;
1160 if (i == MAXWLEN - 1)
1161 return SP_FORMERROR;
1162 }
1163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001164 // Init the count to 10.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001165 count_common_word(lp, word, -1, 10);
1166 done += i + 1;
1167 }
1168 return 0;
1169}
1170
1171/*
1172 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
1173 * Return SP_*ERROR flags.
1174 */
1175 static int
1176read_sofo_section(FILE *fd, slang_T *slang)
1177{
1178 int cnt;
1179 char_u *from, *to;
1180 int res;
1181
1182 slang->sl_sofo = TRUE;
1183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001184 // <sofofromlen> <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001185 from = read_cnt_string(fd, 2, &cnt);
1186 if (cnt < 0)
1187 return cnt;
1188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // <sofotolen> <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001190 to = read_cnt_string(fd, 2, &cnt);
1191 if (cnt < 0)
1192 {
1193 vim_free(from);
1194 return cnt;
1195 }
1196
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001197 // Store the info in slang->sl_sal and/or slang->sl_sal_first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001198 if (from != NULL && to != NULL)
1199 res = set_sofo(slang, from, to);
1200 else if (from != NULL || to != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001201 res = SP_FORMERROR; // only one of two strings is an error
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001202 else
1203 res = 0;
1204
1205 vim_free(from);
1206 vim_free(to);
1207 return res;
1208}
1209
1210/*
1211 * Read the compound section from the .spl file:
1212 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
1213 * Returns SP_*ERROR flags.
1214 */
1215 static int
1216read_compound(FILE *fd, slang_T *slang, int len)
1217{
1218 int todo = len;
1219 int c;
1220 int atstart;
1221 char_u *pat;
1222 char_u *pp;
1223 char_u *cp;
1224 char_u *ap;
1225 char_u *crp;
1226 int cnt;
1227 garray_T *gap;
1228
1229 if (todo < 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001230 return SP_FORMERROR; // need at least two bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001231
1232 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001233 c = getc(fd); // <compmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001234 if (c < 2)
1235 c = MAXWLEN;
1236 slang->sl_compmax = c;
1237
1238 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001239 c = getc(fd); // <compminlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001240 if (c < 1)
1241 c = 0;
1242 slang->sl_compminlen = c;
1243
1244 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001245 c = getc(fd); // <compsylmax>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001246 if (c < 1)
1247 c = MAXWLEN;
1248 slang->sl_compsylmax = c;
1249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001250 c = getc(fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001251 if (c != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001252 ungetc(c, fd); // be backwards compatible with Vim 7.0b
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001253 else
1254 {
1255 --todo;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001256 c = getc(fd); // only use the lower byte for now
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001257 --todo;
1258 slang->sl_compoptions = c;
1259
1260 gap = &slang->sl_comppat;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001261 c = get2c(fd); // <comppatcount>
Bram Moolenaarb85d3622021-08-11 15:54:59 +02001262 if (c < 0)
1263 return SP_TRUNCERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001264 todo -= 2;
1265 ga_init2(gap, sizeof(char_u *), c);
1266 if (ga_grow(gap, c) == OK)
1267 while (--c >= 0)
1268 {
1269 ((char_u **)(gap->ga_data))[gap->ga_len++] =
Bram Moolenaarb85d3622021-08-11 15:54:59 +02001270 read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001271 // <comppatlen> <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001272 if (cnt < 0)
1273 return cnt;
1274 todo -= cnt + 1;
1275 }
1276 }
1277 if (todo < 0)
1278 return SP_FORMERROR;
1279
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001280 // Turn the COMPOUNDRULE items into a regexp pattern:
1281 // "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
1282 // Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
1283 // Conversion to utf-8 may double the size.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001284 c = todo * 2 + 7;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001285 if (enc_utf8)
1286 c += todo * 2;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001287 pat = alloc(c);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001288 if (pat == NULL)
1289 return SP_OTHERERROR;
1290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001291 // We also need a list of all flags that can appear at the start and one
1292 // for all flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001293 cp = alloc(todo + 1);
1294 if (cp == NULL)
1295 {
1296 vim_free(pat);
1297 return SP_OTHERERROR;
1298 }
1299 slang->sl_compstartflags = cp;
1300 *cp = NUL;
1301
1302 ap = alloc(todo + 1);
1303 if (ap == NULL)
1304 {
1305 vim_free(pat);
1306 return SP_OTHERERROR;
1307 }
1308 slang->sl_compallflags = ap;
1309 *ap = NUL;
1310
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001311 // And a list of all patterns in their original form, for checking whether
1312 // compounding may work in match_compoundrule(). This is freed when we
1313 // encounter a wildcard, the check doesn't work then.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001314 crp = alloc(todo + 1);
1315 slang->sl_comprules = crp;
1316
1317 pp = pat;
1318 *pp++ = '^';
1319 *pp++ = '\\';
1320 *pp++ = '(';
1321
1322 atstart = 1;
1323 while (todo-- > 0)
1324 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001325 c = getc(fd); // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001326 if (c == EOF)
1327 {
1328 vim_free(pat);
1329 return SP_TRUNCERROR;
1330 }
1331
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001332 // Add all flags to "sl_compallflags".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001333 if (vim_strchr((char_u *)"?*+[]/", c) == NULL
1334 && !byte_in_str(slang->sl_compallflags, c))
1335 {
1336 *ap++ = c;
1337 *ap = NUL;
1338 }
1339
1340 if (atstart != 0)
1341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001342 // At start of item: copy flags to "sl_compstartflags". For a
1343 // [abc] item set "atstart" to 2 and copy up to the ']'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001344 if (c == '[')
1345 atstart = 2;
1346 else if (c == ']')
1347 atstart = 0;
1348 else
1349 {
1350 if (!byte_in_str(slang->sl_compstartflags, c))
1351 {
1352 *cp++ = c;
1353 *cp = NUL;
1354 }
1355 if (atstart == 1)
1356 atstart = 0;
1357 }
1358 }
1359
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001360 // Copy flag to "sl_comprules", unless we run into a wildcard.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001361 if (crp != NULL)
1362 {
1363 if (c == '?' || c == '+' || c == '*')
1364 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001365 VIM_CLEAR(slang->sl_comprules);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001366 crp = NULL;
1367 }
1368 else
1369 *crp++ = c;
1370 }
1371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001372 if (c == '/') // slash separates two items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001373 {
1374 *pp++ = '\\';
1375 *pp++ = '|';
1376 atstart = 1;
1377 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001378 else // normal char, "[abc]" and '*' are copied as-is
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001379 {
1380 if (c == '?' || c == '+' || c == '~')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001381 *pp++ = '\\'; // "a?" becomes "a\?", "a+" becomes "a\+"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001382 if (enc_utf8)
1383 pp += mb_char2bytes(c, pp);
1384 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001385 *pp++ = c;
1386 }
1387 }
1388
1389 *pp++ = '\\';
1390 *pp++ = ')';
1391 *pp++ = '$';
1392 *pp = NUL;
1393
1394 if (crp != NULL)
1395 *crp = NUL;
1396
1397 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
1398 vim_free(pat);
1399 if (slang->sl_compprog == NULL)
1400 return SP_FORMERROR;
1401
1402 return 0;
1403}
1404
1405/*
1406 * Set the SOFOFROM and SOFOTO items in language "lp".
1407 * Returns SP_*ERROR flags when there is something wrong.
1408 */
1409 static int
1410set_sofo(slang_T *lp, char_u *from, char_u *to)
1411{
1412 int i;
1413
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001414 garray_T *gap;
1415 char_u *s;
1416 char_u *p;
1417 int c;
1418 int *inp;
1419
1420 if (has_mbyte)
1421 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 // Use "sl_sal" as an array with 256 pointers to a list of wide
1423 // characters. The index is the low byte of the character.
1424 // The list contains from-to pairs with a terminating NUL.
1425 // sl_sal_first[] is used for latin1 "from" characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001426 gap = &lp->sl_sal;
1427 ga_init2(gap, sizeof(int *), 1);
1428 if (ga_grow(gap, 256) == FAIL)
1429 return SP_OTHERERROR;
1430 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
1431 gap->ga_len = 256;
1432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001433 // First count the number of items for each list. Temporarily use
1434 // sl_sal_first[] for this.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001435 for (p = from, s = to; *p != NUL && *s != NUL; )
1436 {
1437 c = mb_cptr2char_adv(&p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001438 MB_CPTR_ADV(s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001439 if (c >= 256)
1440 ++lp->sl_sal_first[c & 0xff];
1441 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001442 if (*p != NUL || *s != NUL) // lengths differ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001443 return SP_FORMERROR;
1444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001445 // Allocate the lists.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001446 for (i = 0; i < 256; ++i)
1447 if (lp->sl_sal_first[i] > 0)
1448 {
1449 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
1450 if (p == NULL)
1451 return SP_OTHERERROR;
1452 ((int **)gap->ga_data)[i] = (int *)p;
1453 *(int *)p = 0;
1454 }
1455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001456 // Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
1457 // list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001458 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
1459 for (p = from, s = to; *p != NUL && *s != NUL; )
1460 {
1461 c = mb_cptr2char_adv(&p);
1462 i = mb_cptr2char_adv(&s);
1463 if (c >= 256)
1464 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001465 // Append the from-to chars at the end of the list with
1466 // the low byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001467 inp = ((int **)gap->ga_data)[c & 0xff];
1468 while (*inp != 0)
1469 ++inp;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001470 *inp++ = c; // from char
1471 *inp++ = i; // to char
1472 *inp++ = NUL; // NUL at the end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001473 }
1474 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001475 // mapping byte to char is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001476 lp->sl_sal_first[c] = i;
1477 }
1478 }
1479 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001481 // mapping bytes to bytes is done in sl_sal_first[]
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001482 if (STRLEN(from) != STRLEN(to))
1483 return SP_FORMERROR;
1484
1485 for (i = 0; to[i] != NUL; ++i)
1486 lp->sl_sal_first[from[i]] = to[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001487 lp->sl_sal.ga_len = 1; // indicates we have soundfolding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001488 }
1489
1490 return 0;
1491}
1492
1493/*
1494 * Fill the first-index table for "lp".
1495 */
1496 static void
1497set_sal_first(slang_T *lp)
1498{
1499 salfirst_T *sfirst;
1500 int i;
1501 salitem_T *smp;
1502 int c;
1503 garray_T *gap = &lp->sl_sal;
1504
1505 sfirst = lp->sl_sal_first;
1506 for (i = 0; i < 256; ++i)
1507 sfirst[i] = -1;
1508 smp = (salitem_T *)gap->ga_data;
1509 for (i = 0; i < gap->ga_len; ++i)
1510 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001511 if (has_mbyte)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001512 // Use the lowest byte of the first character. For latin1 it's
1513 // the character, for other encodings it should differ for most
1514 // characters.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001515 c = *smp[i].sm_lead_w & 0xff;
1516 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001517 c = *smp[i].sm_lead;
1518 if (sfirst[c] == -1)
1519 {
1520 sfirst[c] = i;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001521 if (has_mbyte)
1522 {
1523 int n;
1524
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001525 // Make sure all entries with this byte are following each
1526 // other. Move the ones that are in the wrong position. Do
1527 // keep the same ordering!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001528 while (i + 1 < gap->ga_len
1529 && (*smp[i + 1].sm_lead_w & 0xff) == c)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001530 // Skip over entry with same index byte.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001531 ++i;
1532
1533 for (n = 1; i + n < gap->ga_len; ++n)
1534 if ((*smp[i + n].sm_lead_w & 0xff) == c)
1535 {
1536 salitem_T tsal;
1537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001538 // Move entry with same index byte after the entries
1539 // we already found.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001540 ++i;
1541 --n;
1542 tsal = smp[i + n];
1543 mch_memmove(smp + i + 1, smp + i,
1544 sizeof(salitem_T) * n);
1545 smp[i] = tsal;
1546 }
1547 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001548 }
1549 }
1550}
1551
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001552/*
1553 * Turn a multi-byte string into a wide character string.
1554 * Return it in allocated memory (NULL for out-of-memory)
1555 */
1556 static int *
1557mb_str2wide(char_u *s)
1558{
1559 int *res;
1560 char_u *p;
1561 int i = 0;
1562
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001563 res = ALLOC_MULT(int, mb_charlen(s) + 1);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001564 if (res != NULL)
1565 {
1566 for (p = s; *p != NUL; )
1567 res[i++] = mb_ptr2char_adv(&p);
1568 res[i] = NUL;
1569 }
1570 return res;
1571}
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001572
1573/*
1574 * Read a tree from the .spl or .sug file.
1575 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
1576 * This is skipped when the tree has zero length.
1577 * Returns zero when OK, SP_ value for an error.
1578 */
1579 static int
1580spell_read_tree(
1581 FILE *fd,
1582 char_u **bytsp,
Bram Moolenaar07399e72020-08-24 20:05:50 +02001583 long *bytsp_len,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001584 idx_T **idxsp,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001585 int prefixtree, // TRUE for the prefix tree
1586 int prefixcnt) // when "prefixtree" is TRUE: prefix count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001587{
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001588 long len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001589 int idx;
1590 char_u *bp;
1591 idx_T *ip;
1592
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001593 // The tree size was computed when writing the file, so that we can
1594 // allocate it as one long block. <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001595 len = get4c(fd);
1596 if (len < 0)
1597 return SP_TRUNCERROR;
Bram Moolenaar6d3c8582017-02-26 15:27:23 +01001598 if (len >= LONG_MAX / (long)sizeof(int))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001599 // Invalid length, multiply with sizeof(int) would overflow.
Bram Moolenaar399c2972017-02-09 21:07:12 +01001600 return SP_FORMERROR;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001601 if (len > 0)
1602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001603 // Allocate the byte array.
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02001604 bp = alloc(len);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001605 if (bp == NULL)
1606 return SP_OTHERERROR;
1607 *bytsp = bp;
Bram Moolenaar07399e72020-08-24 20:05:50 +02001608 if (bytsp_len != NULL)
1609 *bytsp_len = len;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001610
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001611 // Allocate the index array.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001612 ip = lalloc_clear(len * sizeof(int), TRUE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001613 if (ip == NULL)
1614 return SP_OTHERERROR;
1615 *idxsp = ip;
1616
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001617 // Recursively read the tree and store it in the array.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001618 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
1619 if (idx < 0)
1620 return idx;
1621 }
1622 return 0;
1623}
1624
1625/*
1626 * Read one row of siblings from the spell file and store it in the byte array
1627 * "byts" and index array "idxs". Recursively read the children.
1628 *
1629 * NOTE: The code here must match put_node()!
1630 *
1631 * Returns the index (>= 0) following the siblings.
1632 * Returns SP_TRUNCERROR if the file is shorter than expected.
1633 * Returns SP_FORMERROR if there is a format error.
1634 */
1635 static idx_T
1636read_tree_node(
1637 FILE *fd,
1638 char_u *byts,
1639 idx_T *idxs,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001640 int maxidx, // size of arrays
1641 idx_T startidx, // current index in "byts" and "idxs"
1642 int prefixtree, // TRUE for reading PREFIXTREE
1643 int maxprefcondnr) // maximum for <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001644{
1645 int len;
1646 int i;
1647 int n;
1648 idx_T idx = startidx;
1649 int c;
1650 int c2;
1651#define SHARED_MASK 0x8000000
1652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001653 len = getc(fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001654 if (len <= 0)
1655 return SP_TRUNCERROR;
1656
1657 if (startidx + len >= maxidx)
1658 return SP_FORMERROR;
1659 byts[idx++] = len;
1660
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001661 // Read the byte values, flag/region bytes and shared indexes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001662 for (i = 1; i <= len; ++i)
1663 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001664 c = getc(fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001665 if (c < 0)
1666 return SP_TRUNCERROR;
1667 if (c <= BY_SPECIAL)
1668 {
1669 if (c == BY_NOFLAGS && !prefixtree)
1670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001671 // No flags, all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001672 idxs[idx] = 0;
1673 c = 0;
1674 }
1675 else if (c != BY_INDEX)
1676 {
1677 if (prefixtree)
1678 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001679 // Read the optional pflags byte, the prefix ID and the
1680 // condition nr. In idxs[] store the prefix ID in the low
1681 // byte, the condition index shifted up 8 bits, the flags
1682 // shifted up 24 bits.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001683 if (c == BY_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001684 c = getc(fd) << 24; // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001685 else
1686 c = 0;
1687
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001688 c |= getc(fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001689
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 n = get2c(fd); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001691 if (n >= maxprefcondnr)
1692 return SP_FORMERROR;
1693 c |= (n << 8);
1694 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001695 else // c must be BY_FLAGS or BY_FLAGS2
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001696 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001697 // Read flags and optional region and prefix ID. In
1698 // idxs[] the flags go in the low two bytes, region above
1699 // that and prefix ID above the region.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001700 c2 = c;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001701 c = getc(fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001702 if (c2 == BY_FLAGS2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001703 c = (getc(fd) << 8) + c; // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001704 if (c & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001705 c = (getc(fd) << 16) + c; // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001706 if (c & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001707 c = (getc(fd) << 24) + c; // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001708 }
1709
1710 idxs[idx] = c;
1711 c = 0;
1712 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001713 else // c == BY_INDEX
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001714 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001716 n = get3c(fd);
1717 if (n < 0 || n >= maxidx)
1718 return SP_FORMERROR;
1719 idxs[idx] = n + SHARED_MASK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001720 c = getc(fd); // <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001721 }
1722 }
1723 byts[idx++] = c;
1724 }
1725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001726 // Recursively read the children for non-shared siblings.
1727 // Skip the end-of-word ones (zero byte value) and the shared ones (and
1728 // remove SHARED_MASK)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001729 for (i = 1; i <= len; ++i)
1730 if (byts[startidx + i] != 0)
1731 {
1732 if (idxs[startidx + i] & SHARED_MASK)
1733 idxs[startidx + i] &= ~SHARED_MASK;
1734 else
1735 {
1736 idxs[startidx + i] = idx;
1737 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
1738 prefixtree, maxprefcondnr);
1739 if (idx < 0)
1740 break;
1741 }
1742 }
1743
1744 return idx;
1745}
1746
1747/*
1748 * Reload the spell file "fname" if it's loaded.
1749 */
1750 static void
1751spell_reload_one(
1752 char_u *fname,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001753 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001754{
1755 slang_T *slang;
1756 int didit = FALSE;
1757
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001758 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001759 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001760 if (fullpathcmp(fname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001761 {
1762 slang_clear(slang);
1763 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 // reloading failed, clear the language
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001765 slang_clear(slang);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001766 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001767 didit = TRUE;
1768 }
1769 }
1770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001771 // When "zg" was used and the file wasn't loaded yet, should redo
1772 // 'spelllang' to load it now.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001773 if (added_word && !didit)
1774 did_set_spelllang(curwin);
1775}
1776
1777
1778/*
1779 * Functions for ":mkspell".
1780 */
1781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001782#define MAXLINELEN 500 // Maximum length in bytes of a line in a .aff
1783 // and .dic file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001784/*
1785 * Main structure to store the contents of a ".aff" file.
1786 */
1787typedef struct afffile_S
1788{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001789 char_u *af_enc; // "SET", normalized, alloc'ed string or NULL
1790 int af_flagtype; // AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG
1791 unsigned af_rare; // RARE ID for rare word
1792 unsigned af_keepcase; // KEEPCASE ID for keep-case word
1793 unsigned af_bad; // BAD ID for banned word
1794 unsigned af_needaffix; // NEEDAFFIX ID
1795 unsigned af_circumfix; // CIRCUMFIX ID
1796 unsigned af_needcomp; // NEEDCOMPOUND ID
1797 unsigned af_comproot; // COMPOUNDROOT ID
1798 unsigned af_compforbid; // COMPOUNDFORBIDFLAG ID
1799 unsigned af_comppermit; // COMPOUNDPERMITFLAG ID
1800 unsigned af_nosuggest; // NOSUGGEST ID
1801 int af_pfxpostpone; // postpone prefixes without chop string and
1802 // without flags
1803 int af_ignoreextra; // IGNOREEXTRA present
1804 hashtab_T af_pref; // hashtable for prefixes, affheader_T
1805 hashtab_T af_suff; // hashtable for suffixes, affheader_T
1806 hashtab_T af_comp; // hashtable for compound flags, compitem_T
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001807} afffile_T;
1808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001809#define AFT_CHAR 0 // flags are one character
1810#define AFT_LONG 1 // flags are two characters
1811#define AFT_CAPLONG 2 // flags are one or two characters
1812#define AFT_NUM 3 // flags are numbers, comma separated
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001813
1814typedef struct affentry_S affentry_T;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001815// Affix entry from ".aff" file. Used for prefixes and suffixes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001816struct affentry_S
1817{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 affentry_T *ae_next; // next affix with same name/number
1819 char_u *ae_chop; // text to chop off basic word (can be NULL)
1820 char_u *ae_add; // text to add to basic word (can be NULL)
1821 char_u *ae_flags; // flags on the affix (can be NULL)
1822 char_u *ae_cond; // condition (NULL for ".")
1823 regprog_T *ae_prog; // regexp program for ae_cond or NULL
1824 char ae_compforbid; // COMPOUNDFORBIDFLAG found
1825 char ae_comppermit; // COMPOUNDPERMITFLAG found
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001826};
1827
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001828#define AH_KEY_LEN 17 // 2 x 8 bytes + NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001830// Affix header from ".aff" file. Used for af_pref and af_suff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001831typedef struct affheader_S
1832{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001833 char_u ah_key[AH_KEY_LEN]; // key for hashtab == name of affix
1834 unsigned ah_flag; // affix name as number, uses "af_flagtype"
1835 int ah_newID; // prefix ID after renumbering; 0 if not used
1836 int ah_combine; // suffix may combine with prefix
1837 int ah_follows; // another affix block should be following
1838 affentry_T *ah_first; // first affix entry
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001839} affheader_T;
1840
1841#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
1842
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001843// Flag used in compound items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001844typedef struct compitem_S
1845{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001846 char_u ci_key[AH_KEY_LEN]; // key for hashtab == name of compound
1847 unsigned ci_flag; // affix name as number, uses "af_flagtype"
1848 int ci_newID; // affix ID after renumbering.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001849} compitem_T;
1850
1851#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
1852
1853/*
1854 * Structure that is used to store the items in the word tree. This avoids
1855 * the need to keep track of each allocated thing, everything is freed all at
1856 * once after ":mkspell" is done.
1857 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
1858 * "sb_data" is correct for systems where pointers must be aligned on
1859 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
1860 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001861#define SBLOCKSIZE 16000 // size of sb_data
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001862typedef struct sblock_S sblock_T;
1863struct sblock_S
1864{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001865 int sb_used; // nr of bytes already in use
1866 sblock_T *sb_next; // next block in list
1867 char_u sb_data[1]; // data, actually longer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001868};
1869
1870/*
1871 * A node in the tree.
1872 */
1873typedef struct wordnode_S wordnode_T;
1874struct wordnode_S
1875{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001876 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001877 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001878 char_u hashkey[6]; // the hash key, only used while compressing
1879 int index; // index in written nodes (valid after first
1880 // round)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001881 } wn_u1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001882 union // shared to save space
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001883 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001884 wordnode_T *next; // next node with same hash key
1885 wordnode_T *wnode; // parent node that will write this node
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001886 } wn_u2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001887 wordnode_T *wn_child; // child (next byte in word)
1888 wordnode_T *wn_sibling; // next sibling (alternate byte in word,
1889 // always sorted)
1890 int wn_refs; // Nr. of references to this node. Only
1891 // relevant for first node in a list of
1892 // siblings, in following siblings it is
1893 // always one.
1894 char_u wn_byte; // Byte for this node. NUL for word end
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001895
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001896 // Info for when "wn_byte" is NUL.
1897 // In PREFIXTREE "wn_region" is used for the prefcondnr.
1898 // In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
1899 // "wn_region" the LSW of the wordnr.
1900 char_u wn_affixID; // supported/required prefix ID or 0
1901 short_u wn_flags; // WF_ flags
1902 short wn_region; // region mask
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001903
1904#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001905 int wn_nr; // sequence nr for printing
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001906#endif
1907};
1908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001909#define WN_MASK 0xffff // mask relevant bits of "wn_flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001910
1911#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
1912
1913/*
1914 * Info used while reading the spell files.
1915 */
1916typedef struct spellinfo_S
1917{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001918 wordnode_T *si_foldroot; // tree with case-folded words
1919 long si_foldwcount; // nr of words in si_foldroot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001920
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001921 wordnode_T *si_keeproot; // tree with keep-case words
1922 long si_keepwcount; // nr of words in si_keeproot
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 wordnode_T *si_prefroot; // tree with postponed prefixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001926 long si_sugtree; // creating the soundfolding trie
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001927
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 sblock_T *si_blocks; // memory blocks used
1929 long si_blocks_cnt; // memory blocks allocated
1930 int si_did_emsg; // TRUE when ran out of memory
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001932 long si_compress_cnt; // words to add before lowering
1933 // compression limit
1934 wordnode_T *si_first_free; // List of nodes that have been freed during
1935 // compression, linked by "wn_child" field.
1936 long si_free_count; // number of nodes in si_first_free
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001937#ifdef SPELL_PRINTTREE
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001938 int si_wordnode_nr; // sequence nr for nodes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001939#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001940 buf_T *si_spellbuf; // buffer used to store soundfold word table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001941
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001942 int si_ascii; // handling only ASCII words
1943 int si_add; // addition file
1944 int si_clear_chartab; // when TRUE clear char tables
1945 int si_region; // region mask
1946 vimconv_T si_conv; // for conversion to 'encoding'
1947 int si_memtot; // runtime memory used
1948 int si_verbose; // verbose messages
1949 int si_msg_count; // number of words added since last message
1950 char_u *si_info; // info text chars or NULL
1951 int si_region_count; // number of regions supported (1 when there
1952 // are no regions)
Bram Moolenaar2993ac52018-02-10 14:12:43 +01001953 char_u si_region_name[MAXREGIONS * 2 + 1];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001954 // region names; used only if
1955 // si_region_count > 1)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001956
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001957 garray_T si_rep; // list of fromto_T entries from REP lines
1958 garray_T si_repsal; // list of fromto_T entries from REPSAL lines
1959 garray_T si_sal; // list of fromto_T entries from SAL lines
1960 char_u *si_sofofr; // SOFOFROM text
1961 char_u *si_sofoto; // SOFOTO text
1962 int si_nosugfile; // NOSUGFILE item found
1963 int si_nosplitsugs; // NOSPLITSUGS item found
1964 int si_nocompoundsugs; // NOCOMPOUNDSUGS item found
1965 int si_followup; // soundsalike: ?
1966 int si_collapse; // soundsalike: ?
1967 hashtab_T si_commonwords; // hashtable for common words
1968 time_t si_sugtime; // timestamp for .sug file
1969 int si_rem_accents; // soundsalike: remove accents
1970 garray_T si_map; // MAP info concatenated
1971 char_u *si_midword; // MIDWORD chars or NULL
1972 int si_compmax; // max nr of words for compounding
1973 int si_compminlen; // minimal length for compounding
1974 int si_compsylmax; // max nr of syllables for compounding
1975 int si_compoptions; // COMP_ flags
1976 garray_T si_comppat; // CHECKCOMPOUNDPATTERN items, each stored as
1977 // a string
1978 char_u *si_compflags; // flags used for compounding
1979 char_u si_nobreak; // NOBREAK
1980 char_u *si_syllable; // syllable string
1981 garray_T si_prefcond; // table with conditions for postponed
1982 // prefixes, each stored as a string
1983 int si_newprefID; // current value for ah_newID
1984 int si_newcompID; // current value for compound ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001985} spellinfo_T;
1986
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001987static int is_aff_rule(char_u **items, int itemcnt, char *rulename, int mincount);
1988static void aff_process_flags(afffile_T *affile, affentry_T *entry);
1989static int spell_info_item(char_u *s);
1990static unsigned affitem2flag(int flagtype, char_u *item, char_u *fname, int lnum);
1991static unsigned get_affitem(int flagtype, char_u **pp);
1992static void process_compflags(spellinfo_T *spin, afffile_T *aff, char_u *compflags);
1993static void check_renumber(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001994static void aff_check_number(int spinval, int affval, char *name);
1995static void aff_check_string(char_u *spinval, char_u *affval, char *name);
1996static int str_equal(char_u *s1, char_u *s2);
1997static void add_fromto(spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to);
1998static int sal_to_bool(char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02001999static int get_affix_flags(afffile_T *affile, char_u *afflist);
2000static int get_pfxlist(afffile_T *affile, char_u *afflist, char_u *store_afflist);
2001static void get_compflags(afffile_T *affile, char_u *afflist, char_u *store_afflist);
2002static 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 +02002003static void *getroom(spellinfo_T *spin, size_t len, int align);
2004static char_u *getroom_save(spellinfo_T *spin, char_u *s);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002005static int store_word(spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix);
2006static int tree_add_word(spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID);
2007static wordnode_T *get_wordnode(spellinfo_T *spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002008static void free_wordnode(spellinfo_T *spin, wordnode_T *n);
Bram Moolenaar408c23b2020-06-03 22:15:45 +02002009static void wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name);
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02002010static long node_compress(spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, long *tot);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002011static int node_equal(wordnode_T *n1, wordnode_T *n2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002012static void clear_node(wordnode_T *node);
2013static int put_node(FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002014static int sug_filltree(spellinfo_T *spin, slang_T *slang);
2015static int sug_maketable(spellinfo_T *spin);
2016static int sug_filltable(spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap);
2017static int offset2bytes(int nr, char_u *buf);
2018static void sug_write(spellinfo_T *spin, char_u *fname);
2019static void spell_message(spellinfo_T *spin, char_u *str);
2020static void init_spellfile(void);
2021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002022// In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
2023// but it must be negative to indicate the prefix tree to tree_add_word().
2024// Use a negative number with the lower 8 bits zero.
kylo252ae6f1d82022-02-16 19:24:07 +00002025#define PFX_FLAGS (-256)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002027// flags for "condit" argument of store_aff_word()
2028#define CONDIT_COMB 1 // affix must combine
2029#define CONDIT_CFIX 2 // affix must have CIRCUMFIX flag
2030#define CONDIT_SUF 4 // add a suffix for matching flags
2031#define CONDIT_AFF 8 // word already has an affix
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002032
2033/*
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02002034 * Tunable parameters for when the tree is compressed. Filled from the
2035 * 'mkspellmem' option.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002036 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002037static long compress_start = 30000; // memory / SBLOCKSIZE
2038static long compress_inc = 100; // memory / SBLOCKSIZE
2039static long compress_added = 500000; // word count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002040
2041/*
2042 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
2043 * Sets "sps_flags".
2044 */
2045 int
2046spell_check_msm(void)
2047{
2048 char_u *p = p_msm;
2049 long start = 0;
2050 long incr = 0;
2051 long added = 0;
2052
2053 if (!VIM_ISDIGIT(*p))
2054 return FAIL;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002055 // block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002056 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
2057 if (*p != ',')
2058 return FAIL;
2059 ++p;
2060 if (!VIM_ISDIGIT(*p))
2061 return FAIL;
2062 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
2063 if (*p != ',')
2064 return FAIL;
2065 ++p;
2066 if (!VIM_ISDIGIT(*p))
2067 return FAIL;
2068 added = getdigits(&p) * 1024;
2069 if (*p != NUL)
2070 return FAIL;
2071
2072 if (start == 0 || incr == 0 || added == 0 || incr > start)
2073 return FAIL;
2074
2075 compress_start = start;
2076 compress_inc = incr;
2077 compress_added = added;
2078 return OK;
2079}
2080
2081#ifdef SPELL_PRINTTREE
2082/*
2083 * For debugging the tree code: print the current tree in a (more or less)
2084 * readable format, so that we can see what happens when adding a word and/or
2085 * compressing the tree.
2086 * Based on code from Olaf Seibert.
2087 */
2088#define PRINTLINESIZE 1000
2089#define PRINTWIDTH 6
2090
2091#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
2092 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
2093
2094static char line1[PRINTLINESIZE];
2095static char line2[PRINTLINESIZE];
2096static char line3[PRINTLINESIZE];
2097
2098 static void
2099spell_clear_flags(wordnode_T *node)
2100{
2101 wordnode_T *np;
2102
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002103 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002104 {
2105 np->wn_u1.index = FALSE;
2106 spell_clear_flags(np->wn_child);
2107 }
2108}
2109
2110 static void
2111spell_print_node(wordnode_T *node, int depth)
2112{
2113 if (node->wn_u1.index)
2114 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002115 // Done this node before, print the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002116 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
2117 PRINTSOME(line2, depth, " ", 0, 0);
2118 PRINTSOME(line3, depth, " ", 0, 0);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002119 msg(line1);
2120 msg(line2);
2121 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002122 }
2123 else
2124 {
2125 node->wn_u1.index = TRUE;
2126
2127 if (node->wn_byte != NUL)
2128 {
2129 if (node->wn_child != NULL)
2130 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
2131 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // Cannot happen?
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002133 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
2134 }
2135 else
2136 PRINTSOME(line1, depth, " $ ", 0, 0);
2137
2138 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
2139
2140 if (node->wn_sibling != NULL)
2141 PRINTSOME(line3, depth, " | ", 0, 0);
2142 else
2143 PRINTSOME(line3, depth, " ", 0, 0);
2144
2145 if (node->wn_byte == NUL)
2146 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002147 msg(line1);
2148 msg(line2);
2149 msg(line3);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002150 }
2151
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002152 // do the children
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002153 if (node->wn_byte != NUL && node->wn_child != NULL)
2154 spell_print_node(node->wn_child, depth + 1);
2155
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002156 // do the siblings
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002157 if (node->wn_sibling != NULL)
2158 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002159 // get rid of all parent details except |
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002160 STRCPY(line1, line3);
2161 STRCPY(line2, line3);
2162 spell_print_node(node->wn_sibling, depth);
2163 }
2164 }
2165}
2166
2167 static void
2168spell_print_tree(wordnode_T *root)
2169{
2170 if (root != NULL)
2171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002172 // Clear the "wn_u1.index" fields, used to remember what has been
2173 // done.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002174 spell_clear_flags(root);
2175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002176 // Recursively print the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002177 spell_print_node(root, 0);
2178 }
2179}
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002180#endif // SPELL_PRINTTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002181
2182/*
2183 * Read the affix file "fname".
2184 * Returns an afffile_T, NULL for complete failure.
2185 */
2186 static afffile_T *
2187spell_read_aff(spellinfo_T *spin, char_u *fname)
2188{
2189 FILE *fd;
2190 afffile_T *aff;
2191 char_u rline[MAXLINELEN];
2192 char_u *line;
2193 char_u *pc = NULL;
2194#define MAXITEMCNT 30
2195 char_u *(items[MAXITEMCNT]);
2196 int itemcnt;
2197 char_u *p;
2198 int lnum = 0;
2199 affheader_T *cur_aff = NULL;
2200 int did_postpone_prefix = FALSE;
2201 int aff_todo = 0;
2202 hashtab_T *tp;
2203 char_u *low = NULL;
2204 char_u *fol = NULL;
2205 char_u *upp = NULL;
2206 int do_rep;
2207 int do_repsal;
2208 int do_sal;
2209 int do_mapline;
2210 int found_map = FALSE;
2211 hashitem_T *hi;
2212 int l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002213 int compminlen = 0; // COMPOUNDMIN value
2214 int compsylmax = 0; // COMPOUNDSYLMAX value
2215 int compoptions = 0; // COMP_ flags
2216 int compmax = 0; // COMPOUNDWORDMAX value
2217 char_u *compflags = NULL; // COMPOUNDFLAG and COMPOUNDRULE
2218 // concatenated
2219 char_u *midword = NULL; // MIDWORD value
2220 char_u *syllable = NULL; // SYLLABLE value
2221 char_u *sofofrom = NULL; // SOFOFROM value
2222 char_u *sofoto = NULL; // SOFOTO value
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002223
2224 /*
2225 * Open the file.
2226 */
2227 fd = mch_fopen((char *)fname, "r");
2228 if (fd == NULL)
2229 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002230 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002231 return NULL;
2232 }
2233
Bram Moolenaarc1669272018-06-19 14:23:53 +02002234 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002235 spell_message(spin, IObuff);
2236
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002237 // Only do REP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002238 do_rep = spin->si_rep.ga_len == 0;
2239
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002240 // Only do REPSAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002241 do_repsal = spin->si_repsal.ga_len == 0;
2242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002243 // Only do SAL lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002244 do_sal = spin->si_sal.ga_len == 0;
2245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002246 // Only do MAP lines when not done in another .aff file already.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002247 do_mapline = spin->si_map.ga_len == 0;
2248
2249 /*
2250 * Allocate and init the afffile_T structure.
2251 */
2252 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
2253 if (aff == NULL)
2254 {
2255 fclose(fd);
2256 return NULL;
2257 }
2258 hash_init(&aff->af_pref);
2259 hash_init(&aff->af_suff);
2260 hash_init(&aff->af_comp);
2261
2262 /*
2263 * Read all the lines in the file one by one.
2264 */
2265 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
2266 {
2267 line_breakcheck();
2268 ++lnum;
2269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002271 if (*rline == '#')
2272 continue;
2273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002274 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002275 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002276 if (spin->si_conv.vc_type != CONV_NONE)
2277 {
2278 pc = string_convert(&spin->si_conv, rline, NULL);
2279 if (pc == NULL)
2280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002282 fname, lnum, rline);
2283 continue;
2284 }
2285 line = pc;
2286 }
2287 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002288 {
2289 pc = NULL;
2290 line = rline;
2291 }
2292
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002293 // Split the line up in white separated items. Put a NUL after each
2294 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002295 itemcnt = 0;
2296 for (p = line; ; )
2297 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002298 while (*p != NUL && *p <= ' ') // skip white space and CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002299 ++p;
2300 if (*p == NUL)
2301 break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002302 if (itemcnt == MAXITEMCNT) // too many items
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002303 break;
2304 items[itemcnt++] = p;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002305 // A few items have arbitrary text argument, don't split them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002306 if (itemcnt == 2 && spell_info_item(items[0]))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002307 while (*p >= ' ' || *p == TAB) // skip until CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002308 ++p;
2309 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002310 while (*p > ' ') // skip until white space or CR/NL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002311 ++p;
2312 if (*p == NUL)
2313 break;
2314 *p++ = NUL;
2315 }
2316
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002317 // Handle non-empty lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002318 if (itemcnt > 0)
2319 {
2320 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
2321 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002322 // Setup for conversion from "ENC" to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002323 aff->af_enc = enc_canonize(items[1]);
2324 if (aff->af_enc != NULL && !spin->si_ascii
2325 && convert_setup(&spin->si_conv, aff->af_enc,
2326 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002327 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002328 fname, aff->af_enc, p_enc);
2329 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002330 }
2331 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
2332 && aff->af_flagtype == AFT_CHAR)
2333 {
2334 if (STRCMP(items[1], "long") == 0)
2335 aff->af_flagtype = AFT_LONG;
2336 else if (STRCMP(items[1], "num") == 0)
2337 aff->af_flagtype = AFT_NUM;
2338 else if (STRCMP(items[1], "caplong") == 0)
2339 aff->af_flagtype = AFT_CAPLONG;
2340 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002341 smsg(_("Invalid value for FLAG in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002342 fname, lnum, items[1]);
2343 if (aff->af_rare != 0
2344 || aff->af_keepcase != 0
2345 || aff->af_bad != 0
2346 || aff->af_needaffix != 0
2347 || aff->af_circumfix != 0
2348 || aff->af_needcomp != 0
2349 || aff->af_comproot != 0
2350 || aff->af_nosuggest != 0
2351 || compflags != NULL
2352 || aff->af_suff.ht_used > 0
2353 || aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002354 smsg(_("FLAG after using flags in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002355 fname, lnum, items[1]);
2356 }
2357 else if (spell_info_item(items[0]))
2358 {
2359 p = (char_u *)getroom(spin,
2360 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
2361 + STRLEN(items[0])
2362 + STRLEN(items[1]) + 3, FALSE);
2363 if (p != NULL)
2364 {
2365 if (spin->si_info != NULL)
2366 {
2367 STRCPY(p, spin->si_info);
2368 STRCAT(p, "\n");
2369 }
2370 STRCAT(p, items[0]);
2371 STRCAT(p, " ");
2372 STRCAT(p, items[1]);
2373 spin->si_info = p;
2374 }
2375 }
2376 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
2377 && midword == NULL)
2378 {
2379 midword = getroom_save(spin, items[1]);
2380 }
2381 else if (is_aff_rule(items, itemcnt, "TRY", 2))
2382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002383 // ignored, we look in the tree for what chars may appear
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002384 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002385 // TODO: remove "RAR" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002386 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
2387 || is_aff_rule(items, itemcnt, "RARE", 2))
2388 && aff->af_rare == 0)
2389 {
2390 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
2391 fname, lnum);
2392 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002393 // TODO: remove "KEP" later
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002394 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
2395 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
2396 && aff->af_keepcase == 0)
2397 {
2398 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
2399 fname, lnum);
2400 }
2401 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
2402 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
2403 && aff->af_bad == 0)
2404 {
2405 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
2406 fname, lnum);
2407 }
2408 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
2409 && aff->af_needaffix == 0)
2410 {
2411 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
2412 fname, lnum);
2413 }
2414 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
2415 && aff->af_circumfix == 0)
2416 {
2417 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
2418 fname, lnum);
2419 }
2420 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
2421 && aff->af_nosuggest == 0)
2422 {
2423 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
2424 fname, lnum);
2425 }
2426 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
2427 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
2428 && aff->af_needcomp == 0)
2429 {
2430 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
2431 fname, lnum);
2432 }
2433 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
2434 && aff->af_comproot == 0)
2435 {
2436 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
2437 fname, lnum);
2438 }
2439 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
2440 && aff->af_compforbid == 0)
2441 {
2442 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
2443 fname, lnum);
2444 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002445 smsg(_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002446 fname, lnum);
2447 }
2448 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
2449 && aff->af_comppermit == 0)
2450 {
2451 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
2452 fname, lnum);
2453 if (aff->af_pref.ht_used > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002454 smsg(_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002455 fname, lnum);
2456 }
2457 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
2458 && compflags == NULL)
2459 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002460 // Turn flag "c" into COMPOUNDRULE compatible string "c+",
2461 // "Na" into "Na+", "1234" into "1234+".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002462 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
2463 if (p != NULL)
2464 {
2465 STRCPY(p, items[1]);
2466 STRCAT(p, "+");
2467 compflags = p;
2468 }
2469 }
2470 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
2471 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002472 // We don't use the count, but do check that it's a number and
2473 // not COMPOUNDRULE mistyped.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002474 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002475 smsg(_("Wrong COMPOUNDRULES value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002476 fname, lnum, items[1]);
2477 }
2478 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
2479 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002480 // Don't use the first rule if it is a number.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002481 if (compflags != NULL || *skipdigits(items[1]) != NUL)
2482 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002483 // Concatenate this string to previously defined ones,
2484 // using a slash to separate them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002485 l = (int)STRLEN(items[1]) + 1;
2486 if (compflags != NULL)
2487 l += (int)STRLEN(compflags) + 1;
2488 p = getroom(spin, l, FALSE);
2489 if (p != NULL)
2490 {
2491 if (compflags != NULL)
2492 {
2493 STRCPY(p, compflags);
2494 STRCAT(p, "/");
2495 }
2496 STRCAT(p, items[1]);
2497 compflags = p;
2498 }
2499 }
2500 }
2501 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
2502 && compmax == 0)
2503 {
2504 compmax = atoi((char *)items[1]);
2505 if (compmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002506 smsg(_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002507 fname, lnum, items[1]);
2508 }
2509 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
2510 && compminlen == 0)
2511 {
2512 compminlen = atoi((char *)items[1]);
2513 if (compminlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002514 smsg(_("Wrong COMPOUNDMIN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002515 fname, lnum, items[1]);
2516 }
2517 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
2518 && compsylmax == 0)
2519 {
2520 compsylmax = atoi((char *)items[1]);
2521 if (compsylmax == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 smsg(_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002523 fname, lnum, items[1]);
2524 }
2525 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
2526 {
2527 compoptions |= COMP_CHECKDUP;
2528 }
2529 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
2530 {
2531 compoptions |= COMP_CHECKREP;
2532 }
2533 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
2534 {
2535 compoptions |= COMP_CHECKCASE;
2536 }
2537 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
2538 {
2539 compoptions |= COMP_CHECKTRIPLE;
2540 }
2541 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
2542 {
2543 if (atoi((char *)items[1]) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002544 smsg(_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002545 fname, lnum, items[1]);
2546 }
2547 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
2548 {
2549 garray_T *gap = &spin->si_comppat;
2550 int i;
2551
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002552 // Only add the couple if it isn't already there.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002553 for (i = 0; i < gap->ga_len - 1; i += 2)
2554 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
2555 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
2556 items[2]) == 0)
2557 break;
2558 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
2559 {
2560 ((char_u **)(gap->ga_data))[gap->ga_len++]
2561 = getroom_save(spin, items[1]);
2562 ((char_u **)(gap->ga_data))[gap->ga_len++]
2563 = getroom_save(spin, items[2]);
2564 }
2565 }
2566 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
2567 && syllable == NULL)
2568 {
2569 syllable = getroom_save(spin, items[1]);
2570 }
2571 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
2572 {
2573 spin->si_nobreak = TRUE;
2574 }
2575 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
2576 {
2577 spin->si_nosplitsugs = TRUE;
2578 }
2579 else if (is_aff_rule(items, itemcnt, "NOCOMPOUNDSUGS", 1))
2580 {
2581 spin->si_nocompoundsugs = TRUE;
2582 }
2583 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
2584 {
2585 spin->si_nosugfile = TRUE;
2586 }
2587 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
2588 {
2589 aff->af_pfxpostpone = TRUE;
2590 }
2591 else if (is_aff_rule(items, itemcnt, "IGNOREEXTRA", 1))
2592 {
2593 aff->af_ignoreextra = TRUE;
2594 }
2595 else if ((STRCMP(items[0], "PFX") == 0
2596 || STRCMP(items[0], "SFX") == 0)
2597 && aff_todo == 0
2598 && itemcnt >= 4)
2599 {
2600 int lasti = 4;
2601 char_u key[AH_KEY_LEN];
2602
2603 if (*items[0] == 'P')
2604 tp = &aff->af_pref;
2605 else
2606 tp = &aff->af_suff;
2607
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002608 // Myspell allows the same affix name to be used multiple
2609 // times. The affix files that do this have an undocumented
2610 // "S" flag on all but the last block, thus we check for that
2611 // and store it in ah_follows.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002612 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
2613 hi = hash_find(tp, key);
2614 if (!HASHITEM_EMPTY(hi))
2615 {
2616 cur_aff = HI2AH(hi);
2617 if (cur_aff->ah_combine != (*items[2] == 'Y'))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002618 smsg(_("Different combining flag in continued affix block in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002619 fname, lnum, items[1]);
2620 if (!cur_aff->ah_follows)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002621 smsg(_("Duplicate affix in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002622 fname, lnum, items[1]);
2623 }
2624 else
2625 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // New affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002627 cur_aff = (affheader_T *)getroom(spin,
2628 sizeof(affheader_T), TRUE);
2629 if (cur_aff == NULL)
2630 break;
2631 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
2632 fname, lnum);
2633 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
2634 break;
2635 if (cur_aff->ah_flag == aff->af_bad
2636 || cur_aff->ah_flag == aff->af_rare
2637 || cur_aff->ah_flag == aff->af_keepcase
2638 || cur_aff->ah_flag == aff->af_needaffix
2639 || cur_aff->ah_flag == aff->af_circumfix
2640 || cur_aff->ah_flag == aff->af_nosuggest
2641 || cur_aff->ah_flag == aff->af_needcomp
2642 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002643 smsg(_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002644 fname, lnum, items[1]);
2645 STRCPY(cur_aff->ah_key, items[1]);
2646 hash_add(tp, cur_aff->ah_key);
2647
2648 cur_aff->ah_combine = (*items[2] == 'Y');
2649 }
2650
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002651 // Check for the "S" flag, which apparently means that another
2652 // block with the same affix name is following.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002653 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
2654 {
2655 ++lasti;
2656 cur_aff->ah_follows = TRUE;
2657 }
2658 else
2659 cur_aff->ah_follows = FALSE;
2660
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002661 // Myspell allows extra text after the item, but that might
2662 // mean mistakes go unnoticed. Require a comment-starter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002663 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002664 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002665
2666 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002667 smsg(_("Expected Y or N in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002668 fname, lnum, items[2]);
2669
2670 if (*items[0] == 'P' && aff->af_pfxpostpone)
2671 {
2672 if (cur_aff->ah_newID == 0)
2673 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002674 // Use a new number in the .spl file later, to be able
2675 // to handle multiple .aff files.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002676 check_renumber(spin);
2677 cur_aff->ah_newID = ++spin->si_newprefID;
2678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002679 // We only really use ah_newID if the prefix is
2680 // postponed. We know that only after handling all
2681 // the items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002682 did_postpone_prefix = FALSE;
2683 }
2684 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002685 // Did use the ID in a previous block.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002686 did_postpone_prefix = TRUE;
2687 }
2688
2689 aff_todo = atoi((char *)items[3]);
2690 }
2691 else if ((STRCMP(items[0], "PFX") == 0
2692 || STRCMP(items[0], "SFX") == 0)
2693 && aff_todo > 0
2694 && STRCMP(cur_aff->ah_key, items[1]) == 0
2695 && itemcnt >= 5)
2696 {
2697 affentry_T *aff_entry;
2698 int upper = FALSE;
2699 int lasti = 5;
2700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002701 // Myspell allows extra text after the item, but that might
2702 // mean mistakes go unnoticed. Require a comment-starter,
2703 // unless IGNOREEXTRA is used. Hunspell uses a "-" item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002704 if (itemcnt > lasti
2705 && !aff->af_ignoreextra
2706 && *items[lasti] != '#'
2707 && (STRCMP(items[lasti], "-") != 0
2708 || itemcnt != lasti + 1))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002709 smsg(_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002710
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002711 // New item for an affix letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002712 --aff_todo;
2713 aff_entry = (affentry_T *)getroom(spin,
2714 sizeof(affentry_T), TRUE);
2715 if (aff_entry == NULL)
2716 break;
2717
2718 if (STRCMP(items[2], "0") != 0)
2719 aff_entry->ae_chop = getroom_save(spin, items[2]);
2720 if (STRCMP(items[3], "0") != 0)
2721 {
2722 aff_entry->ae_add = getroom_save(spin, items[3]);
2723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002724 // Recognize flags on the affix: abcd/XYZ
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002725 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
2726 if (aff_entry->ae_flags != NULL)
2727 {
2728 *aff_entry->ae_flags++ = NUL;
2729 aff_process_flags(aff, aff_entry);
2730 }
2731 }
2732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 // Don't use an affix entry with non-ASCII characters when
2734 // "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002735 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
2736 || has_non_ascii(aff_entry->ae_add)))
2737 {
2738 aff_entry->ae_next = cur_aff->ah_first;
2739 cur_aff->ah_first = aff_entry;
2740
2741 if (STRCMP(items[4], ".") != 0)
2742 {
2743 char_u buf[MAXLINELEN];
2744
2745 aff_entry->ae_cond = getroom_save(spin, items[4]);
2746 if (*items[0] == 'P')
2747 sprintf((char *)buf, "^%s", items[4]);
2748 else
2749 sprintf((char *)buf, "%s$", items[4]);
2750 aff_entry->ae_prog = vim_regcomp(buf,
2751 RE_MAGIC + RE_STRING + RE_STRICT);
2752 if (aff_entry->ae_prog == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002753 smsg(_("Broken condition in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002754 fname, lnum, items[4]);
2755 }
2756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002757 // For postponed prefixes we need an entry in si_prefcond
2758 // for the condition. Use an existing one if possible.
2759 // Can't be done for an affix with flags, ignoring
2760 // COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002761 if (*items[0] == 'P' && aff->af_pfxpostpone
2762 && aff_entry->ae_flags == NULL)
2763 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002764 // When the chop string is one lower-case letter and
2765 // the add string ends in the upper-case letter we set
2766 // the "upper" flag, clear "ae_chop" and remove the
2767 // letters from "ae_add". The condition must either
2768 // be empty or start with the same letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002769 if (aff_entry->ae_chop != NULL
2770 && aff_entry->ae_add != NULL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002771 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002772 aff_entry->ae_chop)] == NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002773 {
2774 int c, c_up;
2775
2776 c = PTR2CHAR(aff_entry->ae_chop);
2777 c_up = SPELL_TOUPPER(c);
2778 if (c_up != c
2779 && (aff_entry->ae_cond == NULL
2780 || PTR2CHAR(aff_entry->ae_cond) == c))
2781 {
2782 p = aff_entry->ae_add
2783 + STRLEN(aff_entry->ae_add);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002784 MB_PTR_BACK(aff_entry->ae_add, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002785 if (PTR2CHAR(p) == c_up)
2786 {
2787 upper = TRUE;
2788 aff_entry->ae_chop = NULL;
2789 *p = NUL;
2790
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002791 // The condition is matched with the
2792 // actual word, thus must check for the
2793 // upper-case letter.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002794 if (aff_entry->ae_cond != NULL)
2795 {
2796 char_u buf[MAXLINELEN];
Bram Moolenaar264b74f2019-01-24 17:18:42 +01002797
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002798 if (has_mbyte)
2799 {
2800 onecap_copy(items[4], buf, TRUE);
2801 aff_entry->ae_cond = getroom_save(
2802 spin, buf);
2803 }
2804 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002805 *aff_entry->ae_cond = c_up;
2806 if (aff_entry->ae_cond != NULL)
2807 {
2808 sprintf((char *)buf, "^%s",
2809 aff_entry->ae_cond);
2810 vim_regfree(aff_entry->ae_prog);
2811 aff_entry->ae_prog = vim_regcomp(
2812 buf, RE_MAGIC + RE_STRING);
2813 }
2814 }
2815 }
2816 }
2817 }
2818
2819 if (aff_entry->ae_chop == NULL
2820 && aff_entry->ae_flags == NULL)
2821 {
2822 int idx;
2823 char_u **pp;
2824 int n;
2825
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002826 // Find a previously used condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002827 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
2828 --idx)
2829 {
2830 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
2831 if (str_equal(p, aff_entry->ae_cond))
2832 break;
2833 }
2834 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
2835 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002836 // Not found, add a new condition.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002837 idx = spin->si_prefcond.ga_len++;
2838 pp = ((char_u **)spin->si_prefcond.ga_data)
2839 + idx;
2840 if (aff_entry->ae_cond == NULL)
2841 *pp = NULL;
2842 else
2843 *pp = getroom_save(spin,
2844 aff_entry->ae_cond);
2845 }
2846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002847 // Add the prefix to the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002848 if (aff_entry->ae_add == NULL)
2849 p = (char_u *)"";
2850 else
2851 p = aff_entry->ae_add;
2852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002853 // PFX_FLAGS is a negative number, so that
2854 // tree_add_word() knows this is the prefix tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002855 n = PFX_FLAGS;
2856 if (!cur_aff->ah_combine)
2857 n |= WFP_NC;
2858 if (upper)
2859 n |= WFP_UP;
2860 if (aff_entry->ae_comppermit)
2861 n |= WFP_COMPPERMIT;
2862 if (aff_entry->ae_compforbid)
2863 n |= WFP_COMPFORBID;
2864 tree_add_word(spin, p, spin->si_prefroot, n,
2865 idx, cur_aff->ah_newID);
2866 did_postpone_prefix = TRUE;
2867 }
2868
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002869 // Didn't actually use ah_newID, backup si_newprefID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002870 if (aff_todo == 0 && !did_postpone_prefix)
2871 {
2872 --spin->si_newprefID;
2873 cur_aff->ah_newID = 0;
2874 }
2875 }
2876 }
2877 }
2878 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
2879 {
2880 fol = vim_strsave(items[1]);
2881 }
2882 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
2883 {
2884 low = vim_strsave(items[1]);
2885 }
2886 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
2887 {
2888 upp = vim_strsave(items[1]);
2889 }
2890 else if (is_aff_rule(items, itemcnt, "REP", 2)
2891 || is_aff_rule(items, itemcnt, "REPSAL", 2))
2892 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002893 // Ignore REP/REPSAL count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002894 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002895 smsg(_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002896 fname, lnum);
2897 }
2898 else if ((STRCMP(items[0], "REP") == 0
2899 || STRCMP(items[0], "REPSAL") == 0)
2900 && itemcnt >= 3)
2901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002902 // REP/REPSAL item
2903 // Myspell ignores extra arguments, we require it starts with
2904 // # to detect mistakes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002905 if (itemcnt > 3 && items[3][0] != '#')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906 smsg(_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002907 if (items[0][3] == 'S' ? do_repsal : do_rep)
2908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002909 // Replace underscore with space (can't include a space
2910 // directly).
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002911 for (p = items[1]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002912 if (*p == '_')
2913 *p = ' ';
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002914 for (p = items[2]; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002915 if (*p == '_')
2916 *p = ' ';
2917 add_fromto(spin, items[0][3] == 'S'
2918 ? &spin->si_repsal
2919 : &spin->si_rep, items[1], items[2]);
2920 }
2921 }
2922 else if (is_aff_rule(items, itemcnt, "MAP", 2))
2923 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002924 // MAP item or count
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002925 if (!found_map)
2926 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002927 // First line contains the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002928 found_map = TRUE;
2929 if (!isdigit(*items[1]))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002930 smsg(_("Expected MAP count in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002931 fname, lnum);
2932 }
2933 else if (do_mapline)
2934 {
2935 int c;
2936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002937 // Check that every character appears only once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002938 for (p = items[1]; *p != NUL; )
2939 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002940 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002941 if ((spin->si_map.ga_len > 0
2942 && vim_strchr(spin->si_map.ga_data, c)
2943 != NULL)
2944 || vim_strchr(p, c) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002945 smsg(_("Duplicate character in MAP in %s line %d"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002946 fname, lnum);
2947 }
2948
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002949 // We simply concatenate all the MAP strings, separated by
2950 // slashes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002951 ga_concat(&spin->si_map, items[1]);
2952 ga_append(&spin->si_map, '/');
2953 }
2954 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002955 // Accept "SAL from to" and "SAL from to #comment".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002956 else if (is_aff_rule(items, itemcnt, "SAL", 3))
2957 {
2958 if (do_sal)
2959 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002960 // SAL item (sounds-a-like)
2961 // Either one of the known keys or a from-to pair.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002962 if (STRCMP(items[1], "followup") == 0)
2963 spin->si_followup = sal_to_bool(items[2]);
2964 else if (STRCMP(items[1], "collapse_result") == 0)
2965 spin->si_collapse = sal_to_bool(items[2]);
2966 else if (STRCMP(items[1], "remove_accents") == 0)
2967 spin->si_rem_accents = sal_to_bool(items[2]);
2968 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002969 // when "to" is "_" it means empty
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02002970 add_fromto(spin, &spin->si_sal, items[1],
2971 STRCMP(items[2], "_") == 0 ? (char_u *)""
2972 : items[2]);
2973 }
2974 }
2975 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
2976 && sofofrom == NULL)
2977 {
2978 sofofrom = getroom_save(spin, items[1]);
2979 }
2980 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
2981 && sofoto == NULL)
2982 {
2983 sofoto = getroom_save(spin, items[1]);
2984 }
2985 else if (STRCMP(items[0], "COMMON") == 0)
2986 {
2987 int i;
2988
2989 for (i = 1; i < itemcnt; ++i)
2990 {
2991 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
2992 items[i])))
2993 {
2994 p = vim_strsave(items[i]);
2995 if (p == NULL)
2996 break;
2997 hash_add(&spin->si_commonwords, p);
2998 }
2999 }
3000 }
3001 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003002 smsg(_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003003 fname, lnum, items[0]);
3004 }
3005 }
3006
3007 if (fol != NULL || low != NULL || upp != NULL)
3008 {
3009 if (spin->si_clear_chartab)
3010 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003011 // Clear the char type tables, don't want to use any of the
3012 // currently used spell properties.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003013 init_spell_chartab();
3014 spin->si_clear_chartab = FALSE;
3015 }
3016
3017 /*
3018 * Don't write a word table for an ASCII file, so that we don't check
3019 * for conflicts with a word table that matches 'encoding'.
3020 * Don't write one for utf-8 either, we use utf_*() and
3021 * mb_get_class(), the list of chars in the file will be incomplete.
3022 */
Bram Moolenaar264b74f2019-01-24 17:18:42 +01003023 if (!spin->si_ascii && !enc_utf8)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003024 {
3025 if (fol == NULL || low == NULL || upp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003026 smsg(_("Missing FOL/LOW/UPP line in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003027 else
3028 (void)set_spell_chartab(fol, low, upp);
3029 }
3030
3031 vim_free(fol);
3032 vim_free(low);
3033 vim_free(upp);
3034 }
3035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003036 // Use compound specifications of the .aff file for the spell info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003037 if (compmax != 0)
3038 {
3039 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
3040 spin->si_compmax = compmax;
3041 }
3042
3043 if (compminlen != 0)
3044 {
3045 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
3046 spin->si_compminlen = compminlen;
3047 }
3048
3049 if (compsylmax != 0)
3050 {
3051 if (syllable == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003052 smsg(_("COMPOUNDSYLMAX used without SYLLABLE"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003053 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
3054 spin->si_compsylmax = compsylmax;
3055 }
3056
3057 if (compoptions != 0)
3058 {
3059 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
3060 spin->si_compoptions |= compoptions;
3061 }
3062
3063 if (compflags != NULL)
3064 process_compflags(spin, aff, compflags);
3065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003066 // Check that we didn't use too many renumbered flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003067 if (spin->si_newcompID < spin->si_newprefID)
3068 {
3069 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003070 msg(_("Too many postponed prefixes"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003071 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003072 msg(_("Too many compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003073 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003074 msg(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003075 }
3076
3077 if (syllable != NULL)
3078 {
3079 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
3080 spin->si_syllable = syllable;
3081 }
3082
3083 if (sofofrom != NULL || sofoto != NULL)
3084 {
3085 if (sofofrom == NULL || sofoto == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003086 smsg(_("Missing SOFO%s line in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003087 sofofrom == NULL ? "FROM" : "TO", fname);
3088 else if (spin->si_sal.ga_len > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003089 smsg(_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003090 else
3091 {
3092 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
3093 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
3094 spin->si_sofofr = sofofrom;
3095 spin->si_sofoto = sofoto;
3096 }
3097 }
3098
3099 if (midword != NULL)
3100 {
3101 aff_check_string(spin->si_midword, midword, "MIDWORD");
3102 spin->si_midword = midword;
3103 }
3104
3105 vim_free(pc);
3106 fclose(fd);
3107 return aff;
3108}
3109
3110/*
3111 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
3112 * a comment is following after item "mincount".
3113 */
3114 static int
3115is_aff_rule(
3116 char_u **items,
3117 int itemcnt,
3118 char *rulename,
3119 int mincount)
3120{
3121 return (STRCMP(items[0], rulename) == 0
3122 && (itemcnt == mincount
3123 || (itemcnt > mincount && items[mincount][0] == '#')));
3124}
3125
3126/*
3127 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
3128 * ae_flags to ae_comppermit and ae_compforbid.
3129 */
3130 static void
3131aff_process_flags(afffile_T *affile, affentry_T *entry)
3132{
3133 char_u *p;
3134 char_u *prevp;
3135 unsigned flag;
3136
3137 if (entry->ae_flags != NULL
3138 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
3139 {
3140 for (p = entry->ae_flags; *p != NUL; )
3141 {
3142 prevp = p;
3143 flag = get_affitem(affile->af_flagtype, &p);
3144 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
3145 {
3146 STRMOVE(prevp, p);
3147 p = prevp;
3148 if (flag == affile->af_comppermit)
3149 entry->ae_comppermit = TRUE;
3150 else
3151 entry->ae_compforbid = TRUE;
3152 }
3153 if (affile->af_flagtype == AFT_NUM && *p == ',')
3154 ++p;
3155 }
3156 if (*entry->ae_flags == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003157 entry->ae_flags = NULL; // nothing left
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003158 }
3159}
3160
3161/*
3162 * Return TRUE if "s" is the name of an info item in the affix file.
3163 */
3164 static int
3165spell_info_item(char_u *s)
3166{
3167 return STRCMP(s, "NAME") == 0
3168 || STRCMP(s, "HOME") == 0
3169 || STRCMP(s, "VERSION") == 0
3170 || STRCMP(s, "AUTHOR") == 0
3171 || STRCMP(s, "EMAIL") == 0
3172 || STRCMP(s, "COPYRIGHT") == 0;
3173}
3174
3175/*
3176 * Turn an affix flag name into a number, according to the FLAG type.
3177 * returns zero for failure.
3178 */
3179 static unsigned
3180affitem2flag(
3181 int flagtype,
3182 char_u *item,
3183 char_u *fname,
3184 int lnum)
3185{
3186 unsigned res;
3187 char_u *p = item;
3188
3189 res = get_affitem(flagtype, &p);
3190 if (res == 0)
3191 {
3192 if (flagtype == AFT_NUM)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003193 smsg(_("Flag is not a number in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003194 fname, lnum, item);
3195 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003196 smsg(_("Illegal flag in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003197 fname, lnum, item);
3198 }
3199 if (*p != NUL)
3200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003201 smsg(_(e_affname), fname, lnum, item);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003202 return 0;
3203 }
3204
3205 return res;
3206}
3207
3208/*
3209 * Get one affix name from "*pp" and advance the pointer.
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003210 * Returns ZERO_FLAG for "0".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003211 * Returns zero for an error, still advances the pointer then.
3212 */
3213 static unsigned
3214get_affitem(int flagtype, char_u **pp)
3215{
3216 int res;
3217
3218 if (flagtype == AFT_NUM)
3219 {
3220 if (!VIM_ISDIGIT(**pp))
3221 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003222 ++*pp; // always advance, avoid getting stuck
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003223 return 0;
3224 }
3225 res = getdigits(pp);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003226 if (res == 0)
3227 res = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003228 }
3229 else
3230 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003231 res = mb_ptr2char_adv(pp);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003232 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
3233 && res >= 'A' && res <= 'Z'))
3234 {
3235 if (**pp == NUL)
3236 return 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003237 res = mb_ptr2char_adv(pp) + (res << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003238 }
3239 }
3240 return res;
3241}
3242
3243/*
3244 * Process the "compflags" string used in an affix file and append it to
3245 * spin->si_compflags.
3246 * The processing involves changing the affix names to ID numbers, so that
3247 * they fit in one byte.
3248 */
3249 static void
3250process_compflags(
3251 spellinfo_T *spin,
3252 afffile_T *aff,
3253 char_u *compflags)
3254{
3255 char_u *p;
3256 char_u *prevp;
3257 unsigned flag;
3258 compitem_T *ci;
3259 int id;
3260 int len;
3261 char_u *tp;
3262 char_u key[AH_KEY_LEN];
3263 hashitem_T *hi;
3264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003265 // Make room for the old and the new compflags, concatenated with a / in
3266 // between. Processing it makes it shorter, but we don't know by how
3267 // much, thus allocate the maximum.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003268 len = (int)STRLEN(compflags) + 1;
3269 if (spin->si_compflags != NULL)
3270 len += (int)STRLEN(spin->si_compflags) + 1;
3271 p = getroom(spin, len, FALSE);
3272 if (p == NULL)
3273 return;
3274 if (spin->si_compflags != NULL)
3275 {
3276 STRCPY(p, spin->si_compflags);
3277 STRCAT(p, "/");
3278 }
3279 spin->si_compflags = p;
3280 tp = p + STRLEN(p);
3281
3282 for (p = compflags; *p != NUL; )
3283 {
3284 if (vim_strchr((char_u *)"/?*+[]", *p) != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003285 // Copy non-flag characters directly.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003286 *tp++ = *p++;
3287 else
3288 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003289 // First get the flag number, also checks validity.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003290 prevp = p;
3291 flag = get_affitem(aff->af_flagtype, &p);
3292 if (flag != 0)
3293 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003294 // Find the flag in the hashtable. If it was used before, use
3295 // the existing ID. Otherwise add a new entry.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003296 vim_strncpy(key, prevp, p - prevp);
3297 hi = hash_find(&aff->af_comp, key);
3298 if (!HASHITEM_EMPTY(hi))
3299 id = HI2CI(hi)->ci_newID;
3300 else
3301 {
3302 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
3303 if (ci == NULL)
3304 break;
3305 STRCPY(ci->ci_key, key);
3306 ci->ci_flag = flag;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003307 // Avoid using a flag ID that has a special meaning in a
3308 // regexp (also inside []).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003309 do
3310 {
3311 check_renumber(spin);
3312 id = spin->si_newcompID--;
3313 } while (vim_strchr((char_u *)"/?*+[]\\-^", id) != NULL);
3314 ci->ci_newID = id;
3315 hash_add(&aff->af_comp, ci->ci_key);
3316 }
3317 *tp++ = id;
3318 }
3319 if (aff->af_flagtype == AFT_NUM && *p == ',')
3320 ++p;
3321 }
3322 }
3323
3324 *tp = NUL;
3325}
3326
3327/*
3328 * Check that the new IDs for postponed affixes and compounding don't overrun
3329 * each other. We have almost 255 available, but start at 0-127 to avoid
3330 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
3331 * When that is used up an error message is given.
3332 */
3333 static void
3334check_renumber(spellinfo_T *spin)
3335{
3336 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
3337 {
3338 spin->si_newprefID = 127;
3339 spin->si_newcompID = 255;
3340 }
3341}
3342
3343/*
3344 * Return TRUE if flag "flag" appears in affix list "afflist".
3345 */
3346 static int
3347flag_in_afflist(int flagtype, char_u *afflist, unsigned flag)
3348{
3349 char_u *p;
3350 unsigned n;
3351
3352 switch (flagtype)
3353 {
3354 case AFT_CHAR:
3355 return vim_strchr(afflist, flag) != NULL;
3356
3357 case AFT_CAPLONG:
3358 case AFT_LONG:
3359 for (p = afflist; *p != NUL; )
3360 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003361 n = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003362 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
3363 && *p != NUL)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003364 n = mb_ptr2char_adv(&p) + (n << 16);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003365 if (n == flag)
3366 return TRUE;
3367 }
3368 break;
3369
3370 case AFT_NUM:
3371 for (p = afflist; *p != NUL; )
3372 {
3373 n = getdigits(&p);
Bram Moolenaar3d2a47c2019-11-07 20:48:42 +01003374 if (n == 0)
3375 n = ZERO_FLAG;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003376 if (n == flag)
3377 return TRUE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003378 if (*p != NUL) // skip over comma
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003379 ++p;
3380 }
3381 break;
3382 }
3383 return FALSE;
3384}
3385
3386/*
3387 * Give a warning when "spinval" and "affval" numbers are set and not the same.
3388 */
3389 static void
3390aff_check_number(int spinval, int affval, char *name)
3391{
3392 if (spinval != 0 && spinval != affval)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003393 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003394}
3395
3396/*
3397 * Give a warning when "spinval" and "affval" strings are set and not the same.
3398 */
3399 static void
3400aff_check_string(char_u *spinval, char_u *affval, char *name)
3401{
3402 if (spinval != NULL && STRCMP(spinval, affval) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003403 smsg(_("%s value differs from what is used in another .aff file"), name);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003404}
3405
3406/*
3407 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3408 * NULL as equal.
3409 */
3410 static int
3411str_equal(char_u *s1, char_u *s2)
3412{
3413 if (s1 == NULL || s2 == NULL)
3414 return s1 == s2;
3415 return STRCMP(s1, s2) == 0;
3416}
3417
3418/*
3419 * Add a from-to item to "gap". Used for REP and SAL items.
3420 * They are stored case-folded.
3421 */
3422 static void
3423add_fromto(
3424 spellinfo_T *spin,
3425 garray_T *gap,
3426 char_u *from,
3427 char_u *to)
3428{
3429 fromto_T *ftp;
3430 char_u word[MAXWLEN];
3431
3432 if (ga_grow(gap, 1) == OK)
3433 {
3434 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaar4f135272021-06-11 19:07:40 +02003435 (void)spell_casefold(curwin, from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003436 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar4f135272021-06-11 19:07:40 +02003437 (void)spell_casefold(curwin, to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003438 ftp->ft_to = getroom_save(spin, word);
3439 ++gap->ga_len;
3440 }
3441}
3442
3443/*
3444 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3445 */
3446 static int
3447sal_to_bool(char_u *s)
3448{
3449 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3450}
3451
3452/*
3453 * Free the structure filled by spell_read_aff().
3454 */
3455 static void
3456spell_free_aff(afffile_T *aff)
3457{
3458 hashtab_T *ht;
3459 hashitem_T *hi;
3460 int todo;
3461 affheader_T *ah;
3462 affentry_T *ae;
3463
3464 vim_free(aff->af_enc);
3465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003466 // All this trouble to free the "ae_prog" items...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003467 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3468 {
3469 todo = (int)ht->ht_used;
3470 for (hi = ht->ht_array; todo > 0; ++hi)
3471 {
3472 if (!HASHITEM_EMPTY(hi))
3473 {
3474 --todo;
3475 ah = HI2AH(hi);
3476 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3477 vim_regfree(ae->ae_prog);
3478 }
3479 }
3480 if (ht == &aff->af_suff)
3481 break;
3482 }
3483
3484 hash_clear(&aff->af_pref);
3485 hash_clear(&aff->af_suff);
3486 hash_clear(&aff->af_comp);
3487}
3488
3489/*
3490 * Read dictionary file "fname".
3491 * Returns OK or FAIL;
3492 */
3493 static int
3494spell_read_dic(spellinfo_T *spin, char_u *fname, afffile_T *affile)
3495{
3496 hashtab_T ht;
3497 char_u line[MAXLINELEN];
3498 char_u *p;
3499 char_u *afflist;
3500 char_u store_afflist[MAXWLEN];
3501 int pfxlen;
3502 int need_affix;
3503 char_u *dw;
3504 char_u *pc;
3505 char_u *w;
3506 int l;
3507 hash_T hash;
3508 hashitem_T *hi;
3509 FILE *fd;
3510 int lnum = 1;
3511 int non_ascii = 0;
3512 int retval = OK;
3513 char_u message[MAXLINELEN + MAXWLEN];
3514 int flags;
3515 int duplicate = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003516 time_T last_msg_time = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003517
3518 /*
3519 * Open the file.
3520 */
3521 fd = mch_fopen((char *)fname, "r");
3522 if (fd == NULL)
3523 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003524 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003525 return FAIL;
3526 }
3527
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003528 // The hashtable is only used to detect duplicated words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003529 hash_init(&ht);
3530
3531 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02003532 _("Reading dictionary file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003533 spell_message(spin, IObuff);
3534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003535 // start with a message for the first line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003536 spin->si_msg_count = 999999;
3537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003538 // Read and ignore the first line: word count.
Bram Moolenaare90d63e2020-09-02 12:58:48 +02003539 if (vim_fgets(line, MAXLINELEN, fd) || !vim_isdigit(*skipwhite(line)))
Bram Moolenaar677658a2022-01-05 16:09:06 +00003540 semsg(_(e_no_word_count_in_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003541
3542 /*
3543 * Read all the lines in the file one by one.
3544 * The words are converted to 'encoding' here, before being added to
3545 * the hashtable.
3546 */
3547 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
3548 {
3549 line_breakcheck();
3550 ++lnum;
3551 if (line[0] == '#' || line[0] == '/')
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003552 continue; // comment line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003554 // Remove CR, LF and white space from the end. White space halfway
3555 // the word is kept to allow e.g., "et al.".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003556 l = (int)STRLEN(line);
3557 while (l > 0 && line[l - 1] <= ' ')
3558 --l;
3559 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003560 continue; // empty line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003561 line[l] = NUL;
3562
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003563 // Convert from "SET" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003564 if (spin->si_conv.vc_type != CONV_NONE)
3565 {
3566 pc = string_convert(&spin->si_conv, line, NULL);
3567 if (pc == NULL)
3568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003569 smsg(_("Conversion failure for word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003570 fname, lnum, line);
3571 continue;
3572 }
3573 w = pc;
3574 }
3575 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003576 {
3577 pc = NULL;
3578 w = line;
3579 }
3580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003581 // Truncate the word at the "/", set "afflist" to what follows.
3582 // Replace "\/" by "/" and "\\" by "\".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003583 afflist = NULL;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003584 for (p = w; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003585 {
3586 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
3587 STRMOVE(p, p + 1);
3588 else if (*p == '/')
3589 {
3590 *p = NUL;
3591 afflist = p + 1;
3592 break;
3593 }
3594 }
3595
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003596 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003597 if (spin->si_ascii && has_non_ascii(w))
3598 {
3599 ++non_ascii;
3600 vim_free(pc);
3601 continue;
3602 }
3603
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003604 // This takes time, print a message every 10000 words, but not more
3605 // often than once per second.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003606 if (spin->si_verbose && spin->si_msg_count > 10000)
3607 {
3608 spin->si_msg_count = 0;
Bram Moolenaar408c23b2020-06-03 22:15:45 +02003609 if (vim_time() > last_msg_time)
3610 {
3611 last_msg_time = vim_time();
3612 vim_snprintf((char *)message, sizeof(message),
3613 _("line %6d, word %6ld - %s"),
3614 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
3615 msg_start();
3616 msg_outtrans_long_attr(message, 0);
3617 msg_clr_eos();
3618 msg_didout = FALSE;
3619 msg_col = 0;
3620 out_flush();
3621 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003622 }
3623
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003624 // Store the word in the hashtable to be able to find duplicates.
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003625 dw = getroom_save(spin, w);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003626 if (dw == NULL)
3627 {
3628 retval = FAIL;
3629 vim_free(pc);
3630 break;
3631 }
3632
3633 hash = hash_hash(dw);
3634 hi = hash_lookup(&ht, dw, hash);
3635 if (!HASHITEM_EMPTY(hi))
3636 {
3637 if (p_verbose > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003638 smsg(_("Duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003639 fname, lnum, dw);
3640 else if (duplicate == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003641 smsg(_("First duplicate word in %s line %d: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003642 fname, lnum, dw);
3643 ++duplicate;
3644 }
3645 else
3646 hash_add_item(&ht, hi, dw, hash);
3647
3648 flags = 0;
3649 store_afflist[0] = NUL;
3650 pfxlen = 0;
3651 need_affix = FALSE;
3652 if (afflist != NULL)
3653 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003654 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003655 flags |= get_affix_flags(affile, afflist);
3656
3657 if (affile->af_needaffix != 0 && flag_in_afflist(
3658 affile->af_flagtype, afflist, affile->af_needaffix))
3659 need_affix = TRUE;
3660
3661 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003662 // Need to store the list of prefix IDs with the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003663 pfxlen = get_pfxlist(affile, afflist, store_afflist);
3664
3665 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003666 // Need to store the list of compound flags with the word.
3667 // Concatenate them to the list of prefix IDs.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003668 get_compflags(affile, afflist, store_afflist + pfxlen);
3669 }
3670
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003671 // Add the word to the word tree(s).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003672 if (store_word(spin, dw, flags, spin->si_region,
3673 store_afflist, need_affix) == FAIL)
3674 retval = FAIL;
3675
3676 if (afflist != NULL)
3677 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003678 // Find all matching suffixes and add the resulting words.
3679 // Additionally do matching prefixes that combine.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003680 if (store_aff_word(spin, dw, afflist, affile,
3681 &affile->af_suff, &affile->af_pref,
3682 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3683 retval = FAIL;
3684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003685 // Find all matching prefixes and add the resulting words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003686 if (store_aff_word(spin, dw, afflist, affile,
3687 &affile->af_pref, NULL,
3688 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
3689 retval = FAIL;
3690 }
3691
3692 vim_free(pc);
3693 }
3694
3695 if (duplicate > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003696 smsg(_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003697 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003698 smsg(_("Ignored %d word(s) with non-ASCII characters in %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003699 non_ascii, fname);
3700 hash_clear(&ht);
3701
3702 fclose(fd);
3703 return retval;
3704}
3705
3706/*
3707 * Check for affix flags in "afflist" that are turned into word flags.
3708 * Return WF_ flags.
3709 */
3710 static int
3711get_affix_flags(afffile_T *affile, char_u *afflist)
3712{
3713 int flags = 0;
3714
3715 if (affile->af_keepcase != 0 && flag_in_afflist(
3716 affile->af_flagtype, afflist, affile->af_keepcase))
3717 flags |= WF_KEEPCAP | WF_FIXCAP;
3718 if (affile->af_rare != 0 && flag_in_afflist(
3719 affile->af_flagtype, afflist, affile->af_rare))
3720 flags |= WF_RARE;
3721 if (affile->af_bad != 0 && flag_in_afflist(
3722 affile->af_flagtype, afflist, affile->af_bad))
3723 flags |= WF_BANNED;
3724 if (affile->af_needcomp != 0 && flag_in_afflist(
3725 affile->af_flagtype, afflist, affile->af_needcomp))
3726 flags |= WF_NEEDCOMP;
3727 if (affile->af_comproot != 0 && flag_in_afflist(
3728 affile->af_flagtype, afflist, affile->af_comproot))
3729 flags |= WF_COMPROOT;
3730 if (affile->af_nosuggest != 0 && flag_in_afflist(
3731 affile->af_flagtype, afflist, affile->af_nosuggest))
3732 flags |= WF_NOSUGGEST;
3733 return flags;
3734}
3735
3736/*
3737 * Get the list of prefix IDs from the affix list "afflist".
3738 * Used for PFXPOSTPONE.
3739 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
3740 * and return the number of affixes.
3741 */
3742 static int
3743get_pfxlist(
3744 afffile_T *affile,
3745 char_u *afflist,
3746 char_u *store_afflist)
3747{
3748 char_u *p;
3749 char_u *prevp;
3750 int cnt = 0;
3751 int id;
3752 char_u key[AH_KEY_LEN];
3753 hashitem_T *hi;
3754
3755 for (p = afflist; *p != NUL; )
3756 {
3757 prevp = p;
3758 if (get_affitem(affile->af_flagtype, &p) != 0)
3759 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003760 // A flag is a postponed prefix flag if it appears in "af_pref"
3761 // and its ID is not zero.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003762 vim_strncpy(key, prevp, p - prevp);
3763 hi = hash_find(&affile->af_pref, key);
3764 if (!HASHITEM_EMPTY(hi))
3765 {
3766 id = HI2AH(hi)->ah_newID;
3767 if (id != 0)
3768 store_afflist[cnt++] = id;
3769 }
3770 }
3771 if (affile->af_flagtype == AFT_NUM && *p == ',')
3772 ++p;
3773 }
3774
3775 store_afflist[cnt] = NUL;
3776 return cnt;
3777}
3778
3779/*
3780 * Get the list of compound IDs from the affix list "afflist" that are used
3781 * for compound words.
3782 * Puts the flags in "store_afflist[]".
3783 */
3784 static void
3785get_compflags(
3786 afffile_T *affile,
3787 char_u *afflist,
3788 char_u *store_afflist)
3789{
3790 char_u *p;
3791 char_u *prevp;
3792 int cnt = 0;
3793 char_u key[AH_KEY_LEN];
3794 hashitem_T *hi;
3795
3796 for (p = afflist; *p != NUL; )
3797 {
3798 prevp = p;
3799 if (get_affitem(affile->af_flagtype, &p) != 0)
3800 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003801 // A flag is a compound flag if it appears in "af_comp".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003802 vim_strncpy(key, prevp, p - prevp);
3803 hi = hash_find(&affile->af_comp, key);
3804 if (!HASHITEM_EMPTY(hi))
3805 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
3806 }
3807 if (affile->af_flagtype == AFT_NUM && *p == ',')
3808 ++p;
3809 }
3810
3811 store_afflist[cnt] = NUL;
3812}
3813
3814/*
3815 * Apply affixes to a word and store the resulting words.
3816 * "ht" is the hashtable with affentry_T that need to be applied, either
3817 * prefixes or suffixes.
3818 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
3819 * the resulting words for combining affixes.
3820 *
3821 * Returns FAIL when out of memory.
3822 */
3823 static int
3824store_aff_word(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003825 spellinfo_T *spin, // spell info
3826 char_u *word, // basic word start
3827 char_u *afflist, // list of names of supported affixes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003828 afffile_T *affile,
3829 hashtab_T *ht,
3830 hashtab_T *xht,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003831 int condit, // CONDIT_SUF et al.
3832 int flags, // flags for the word
3833 char_u *pfxlist, // list of prefix IDs
3834 int pfxlen) // nr of flags in "pfxlist" for prefixes, rest
3835 // is compound flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003836{
3837 int todo;
3838 hashitem_T *hi;
3839 affheader_T *ah;
3840 affentry_T *ae;
3841 char_u newword[MAXWLEN];
3842 int retval = OK;
3843 int i, j;
3844 char_u *p;
3845 int use_flags;
3846 char_u *use_pfxlist;
3847 int use_pfxlen;
3848 int need_affix;
3849 char_u store_afflist[MAXWLEN];
3850 char_u pfx_pfxlist[MAXWLEN];
3851 size_t wordlen = STRLEN(word);
3852 int use_condit;
3853
3854 todo = (int)ht->ht_used;
3855 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
3856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
3860 ah = HI2AH(hi);
3861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003862 // Check that the affix combines, if required, and that the word
3863 // supports this affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003864 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
3865 && flag_in_afflist(affile->af_flagtype, afflist,
3866 ah->ah_flag))
3867 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003868 // Loop over all affix entries with this name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003869 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3870 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003871 // Check the condition. It's not logical to match case
3872 // here, but it is required for compatibility with
3873 // Myspell.
3874 // Another requirement from Myspell is that the chop
3875 // string is shorter than the word itself.
3876 // For prefixes, when "PFXPOSTPONE" was used, only do
3877 // prefixes with a chop string and/or flags.
3878 // When a previously added affix had CIRCUMFIX this one
3879 // must have it too, if it had not then this one must not
3880 // have one either.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003881 if ((xht != NULL || !affile->af_pfxpostpone
3882 || ae->ae_chop != NULL
3883 || ae->ae_flags != NULL)
3884 && (ae->ae_chop == NULL
3885 || STRLEN(ae->ae_chop) < wordlen)
3886 && (ae->ae_prog == NULL
3887 || vim_regexec_prog(&ae->ae_prog, FALSE,
3888 word, (colnr_T)0))
3889 && (((condit & CONDIT_CFIX) == 0)
3890 == ((condit & CONDIT_AFF) == 0
3891 || ae->ae_flags == NULL
3892 || !flag_in_afflist(affile->af_flagtype,
3893 ae->ae_flags, affile->af_circumfix))))
3894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003895 // Match. Remove the chop and add the affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003896 if (xht == NULL)
3897 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003898 // prefix: chop/add at the start of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003899 if (ae->ae_add == NULL)
3900 *newword = NUL;
3901 else
3902 vim_strncpy(newword, ae->ae_add, MAXWLEN - 1);
3903 p = word;
3904 if (ae->ae_chop != NULL)
3905 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003906 // Skip chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003907 if (has_mbyte)
3908 {
3909 i = mb_charlen(ae->ae_chop);
3910 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003911 MB_PTR_ADV(p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003912 }
3913 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003914 p += STRLEN(ae->ae_chop);
3915 }
3916 STRCAT(newword, p);
3917 }
3918 else
3919 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003920 // suffix: chop/add at the end of the word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003921 vim_strncpy(newword, word, MAXWLEN - 1);
3922 if (ae->ae_chop != NULL)
3923 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003924 // Remove chop string.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003925 p = newword + STRLEN(newword);
3926 i = (int)MB_CHARLEN(ae->ae_chop);
3927 for ( ; i > 0; --i)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003928 MB_PTR_BACK(newword, p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003929 *p = NUL;
3930 }
3931 if (ae->ae_add != NULL)
3932 STRCAT(newword, ae->ae_add);
3933 }
3934
3935 use_flags = flags;
3936 use_pfxlist = pfxlist;
3937 use_pfxlen = pfxlen;
3938 need_affix = FALSE;
3939 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
3940 if (ae->ae_flags != NULL)
3941 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003942 // Extract flags from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003943 use_flags |= get_affix_flags(affile, ae->ae_flags);
3944
3945 if (affile->af_needaffix != 0 && flag_in_afflist(
3946 affile->af_flagtype, ae->ae_flags,
3947 affile->af_needaffix))
3948 need_affix = TRUE;
3949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003950 // When there is a CIRCUMFIX flag the other affix
3951 // must also have it and we don't add the word
3952 // with one affix.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003953 if (affile->af_circumfix != 0 && flag_in_afflist(
3954 affile->af_flagtype, ae->ae_flags,
3955 affile->af_circumfix))
3956 {
3957 use_condit |= CONDIT_CFIX;
3958 if ((condit & CONDIT_CFIX) == 0)
3959 need_affix = TRUE;
3960 }
3961
3962 if (affile->af_pfxpostpone
3963 || spin->si_compflags != NULL)
3964 {
3965 if (affile->af_pfxpostpone)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003966 // Get prefix IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003967 use_pfxlen = get_pfxlist(affile,
3968 ae->ae_flags, store_afflist);
3969 else
3970 use_pfxlen = 0;
3971 use_pfxlist = store_afflist;
3972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003973 // Combine the prefix IDs. Avoid adding the
3974 // same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003975 for (i = 0; i < pfxlen; ++i)
3976 {
3977 for (j = 0; j < use_pfxlen; ++j)
3978 if (pfxlist[i] == use_pfxlist[j])
3979 break;
3980 if (j == use_pfxlen)
3981 use_pfxlist[use_pfxlen++] = pfxlist[i];
3982 }
3983
3984 if (spin->si_compflags != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003985 // Get compound IDS from the affix list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003986 get_compflags(affile, ae->ae_flags,
3987 use_pfxlist + use_pfxlen);
3988
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003989 // Combine the list of compound flags.
3990 // Concatenate them to the prefix IDs list.
3991 // Avoid adding the same ID twice.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02003992 for (i = pfxlen; pfxlist[i] != NUL; ++i)
3993 {
3994 for (j = use_pfxlen;
3995 use_pfxlist[j] != NUL; ++j)
3996 if (pfxlist[i] == use_pfxlist[j])
3997 break;
3998 if (use_pfxlist[j] == NUL)
3999 {
4000 use_pfxlist[j++] = pfxlist[i];
4001 use_pfxlist[j] = NUL;
4002 }
4003 }
4004 }
4005 }
4006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004007 // Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
4008 // use the compound flags.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004009 if (use_pfxlist != NULL && ae->ae_compforbid)
4010 {
4011 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
4012 use_pfxlist = pfx_pfxlist;
4013 }
4014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004015 // When there are postponed prefixes...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004016 if (spin->si_prefroot != NULL
4017 && spin->si_prefroot->wn_sibling != NULL)
4018 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004019 // ... add a flag to indicate an affix was used.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004020 use_flags |= WF_HAS_AFF;
4021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004022 // ... don't use a prefix list if combining
4023 // affixes is not allowed. But do use the
4024 // compound flags after them.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004025 if (!ah->ah_combine && use_pfxlist != NULL)
4026 use_pfxlist += use_pfxlen;
4027 }
4028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004029 // When compounding is supported and there is no
4030 // "COMPOUNDPERMITFLAG" then forbid compounding on the
4031 // side where the affix is applied.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004032 if (spin->si_compflags != NULL && !ae->ae_comppermit)
4033 {
4034 if (xht != NULL)
4035 use_flags |= WF_NOCOMPAFT;
4036 else
4037 use_flags |= WF_NOCOMPBEF;
4038 }
4039
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004040 // Store the modified word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004041 if (store_word(spin, newword, use_flags,
4042 spin->si_region, use_pfxlist,
4043 need_affix) == FAIL)
4044 retval = FAIL;
4045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004046 // When added a prefix or a first suffix and the affix
4047 // has flags may add a(nother) suffix. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004048 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
4049 if (store_aff_word(spin, newword, ae->ae_flags,
4050 affile, &affile->af_suff, xht,
4051 use_condit & (xht == NULL
4052 ? ~0 : ~CONDIT_SUF),
4053 use_flags, use_pfxlist, pfxlen) == FAIL)
4054 retval = FAIL;
4055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004056 // When added a suffix and combining is allowed also
4057 // try adding a prefix additionally. Both for the
4058 // word flags and for the affix flags. RECURSIVE!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004059 if (xht != NULL && ah->ah_combine)
4060 {
4061 if (store_aff_word(spin, newword,
4062 afflist, affile,
4063 xht, NULL, use_condit,
4064 use_flags, use_pfxlist,
4065 pfxlen) == FAIL
4066 || (ae->ae_flags != NULL
4067 && store_aff_word(spin, newword,
4068 ae->ae_flags, affile,
4069 xht, NULL, use_condit,
4070 use_flags, use_pfxlist,
4071 pfxlen) == FAIL))
4072 retval = FAIL;
4073 }
4074 }
4075 }
4076 }
4077 }
4078 }
4079
4080 return retval;
4081}
4082
4083/*
4084 * Read a file with a list of words.
4085 */
4086 static int
4087spell_read_wordfile(spellinfo_T *spin, char_u *fname)
4088{
4089 FILE *fd;
4090 long lnum = 0;
4091 char_u rline[MAXLINELEN];
4092 char_u *line;
4093 char_u *pc = NULL;
4094 char_u *p;
4095 int l;
4096 int retval = OK;
4097 int did_word = FALSE;
4098 int non_ascii = 0;
4099 int flags;
4100 int regionmask;
4101
4102 /*
4103 * Open the file.
4104 */
4105 fd = mch_fopen((char *)fname, "r");
4106 if (fd == NULL)
4107 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004108 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004109 return FAIL;
4110 }
4111
Bram Moolenaarc1669272018-06-19 14:23:53 +02004112 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004113 spell_message(spin, IObuff);
4114
4115 /*
4116 * Read all the lines in the file one by one.
4117 */
4118 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4119 {
4120 line_breakcheck();
4121 ++lnum;
4122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004123 // Skip comment lines.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004124 if (*rline == '#')
4125 continue;
4126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004127 // Remove CR, LF and white space from the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004128 l = (int)STRLEN(rline);
4129 while (l > 0 && rline[l - 1] <= ' ')
4130 --l;
4131 if (l == 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004132 continue; // empty or blank line
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004133 rline[l] = NUL;
4134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004135 // Convert from "/encoding={encoding}" to 'encoding' when needed.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004136 vim_free(pc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004137 if (spin->si_conv.vc_type != CONV_NONE)
4138 {
4139 pc = string_convert(&spin->si_conv, rline, NULL);
4140 if (pc == NULL)
4141 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004142 smsg(_("Conversion failure for word in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004143 fname, lnum, rline);
4144 continue;
4145 }
4146 line = pc;
4147 }
4148 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004149 {
4150 pc = NULL;
4151 line = rline;
4152 }
4153
4154 if (*line == '/')
4155 {
4156 ++line;
4157 if (STRNCMP(line, "encoding=", 9) == 0)
4158 {
4159 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004160 smsg(_("Duplicate /encoding= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004161 fname, lnum, line - 1);
4162 else if (did_word)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004163 smsg(_("/encoding= line after word ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004164 fname, lnum, line - 1);
4165 else
4166 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004167 char_u *enc;
4168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004169 // Setup for conversion to 'encoding'.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004170 line += 9;
4171 enc = enc_canonize(line);
4172 if (enc != NULL && !spin->si_ascii
4173 && convert_setup(&spin->si_conv, enc,
4174 p_enc) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 smsg(_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004176 fname, line, p_enc);
4177 vim_free(enc);
4178 spin->si_conv.vc_fail = TRUE;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004179 }
4180 continue;
4181 }
4182
4183 if (STRNCMP(line, "regions=", 8) == 0)
4184 {
4185 if (spin->si_region_count > 1)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004186 smsg(_("Duplicate /regions= line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004187 fname, lnum, line);
4188 else
4189 {
4190 line += 8;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01004191 if (STRLEN(line) > MAXREGIONS * 2)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004192 smsg(_("Too many regions in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004193 fname, lnum, line);
4194 else
4195 {
4196 spin->si_region_count = (int)STRLEN(line) / 2;
4197 STRCPY(spin->si_region_name, line);
4198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004199 // Adjust the mask for a word valid in all regions.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004200 spin->si_region = (1 << spin->si_region_count) - 1;
4201 }
4202 }
4203 continue;
4204 }
4205
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004206 smsg(_("/ line ignored in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004207 fname, lnum, line - 1);
4208 continue;
4209 }
4210
4211 flags = 0;
4212 regionmask = spin->si_region;
4213
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004214 // Check for flags and region after a slash.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004215 p = vim_strchr(line, '/');
4216 if (p != NULL)
4217 {
4218 *p++ = NUL;
4219 while (*p != NUL)
4220 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004221 if (*p == '=') // keep-case word
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004222 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004223 else if (*p == '!') // Bad, bad, wicked word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004224 flags |= WF_BANNED;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004225 else if (*p == '?') // Rare word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004226 flags |= WF_RARE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004227 else if (VIM_ISDIGIT(*p)) // region number(s)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 if ((flags & WF_REGION) == 0) // first one
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004230 regionmask = 0;
4231 flags |= WF_REGION;
4232
4233 l = *p - '0';
Bram Moolenaaree03b942017-10-27 00:57:05 +02004234 if (l == 0 || l > spin->si_region_count)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004235 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004236 smsg(_("Invalid region nr in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004237 fname, lnum, p);
4238 break;
4239 }
4240 regionmask |= 1 << (l - 1);
4241 }
4242 else
4243 {
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004244 smsg(_("Unrecognized flags in %s line %ld: %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004245 fname, lnum, p);
4246 break;
4247 }
4248 ++p;
4249 }
4250 }
4251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004252 // Skip non-ASCII words when "spin->si_ascii" is TRUE.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004253 if (spin->si_ascii && has_non_ascii(line))
4254 {
4255 ++non_ascii;
4256 continue;
4257 }
4258
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004259 // Normal word: store it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004260 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
4261 {
4262 retval = FAIL;
4263 break;
4264 }
4265 did_word = TRUE;
4266 }
4267
4268 vim_free(pc);
4269 fclose(fd);
4270
4271 if (spin->si_ascii && non_ascii > 0)
4272 {
4273 vim_snprintf((char *)IObuff, IOSIZE,
4274 _("Ignored %d words with non-ASCII characters"), non_ascii);
4275 spell_message(spin, IObuff);
4276 }
4277
4278 return retval;
4279}
4280
4281/*
4282 * Get part of an sblock_T, "len" bytes long.
4283 * This avoids calling free() for every little struct we use (and keeping
4284 * track of them).
4285 * The memory is cleared to all zeros.
4286 * Returns NULL when out of memory.
4287 */
4288 static void *
4289getroom(
4290 spellinfo_T *spin,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004291 size_t len, // length needed
4292 int align) // align for pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004293{
4294 char_u *p;
4295 sblock_T *bl = spin->si_blocks;
4296
4297 if (align && bl != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004298 // Round size up for alignment. On some systems structures need to be
4299 // aligned to the size of a pointer (e.g., SPARC).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004300 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4301 & ~(sizeof(char *) - 1);
4302
4303 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4304 {
4305 if (len >= SBLOCKSIZE)
4306 bl = NULL;
4307 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004308 // Allocate a block of memory. It is not freed until much later.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004309 bl = alloc_clear(sizeof(sblock_T) + SBLOCKSIZE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004310 if (bl == NULL)
4311 {
4312 if (!spin->si_did_emsg)
4313 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004314 emsg(_(e_insufficient_memory_word_list_will_be_incomplete));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004315 spin->si_did_emsg = TRUE;
4316 }
4317 return NULL;
4318 }
4319 bl->sb_next = spin->si_blocks;
4320 spin->si_blocks = bl;
4321 bl->sb_used = 0;
4322 ++spin->si_blocks_cnt;
4323 }
4324
4325 p = bl->sb_data + bl->sb_used;
4326 bl->sb_used += (int)len;
4327
4328 return p;
4329}
4330
4331/*
4332 * Make a copy of a string into memory allocated with getroom().
4333 * Returns NULL when out of memory.
4334 */
4335 static char_u *
4336getroom_save(spellinfo_T *spin, char_u *s)
4337{
4338 char_u *sc;
4339
4340 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
4341 if (sc != NULL)
4342 STRCPY(sc, s);
4343 return sc;
4344}
4345
4346
4347/*
4348 * Free the list of allocated sblock_T.
4349 */
4350 static void
4351free_blocks(sblock_T *bl)
4352{
4353 sblock_T *next;
4354
4355 while (bl != NULL)
4356 {
4357 next = bl->sb_next;
4358 vim_free(bl);
4359 bl = next;
4360 }
4361}
4362
4363/*
4364 * Allocate the root of a word tree.
4365 * Returns NULL when out of memory.
4366 */
4367 static wordnode_T *
4368wordtree_alloc(spellinfo_T *spin)
4369{
4370 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4371}
4372
4373/*
Bram Moolenaar5e59ea52022-07-01 22:26:20 +01004374 * Return TRUE if "word" contains valid word characters.
4375 * Control characters and trailing '/' are invalid. Space is OK.
4376 */
4377 static int
K.Takata2ebcc352022-07-14 17:25:14 +01004378valid_spell_word(char_u *word, char_u *end)
Bram Moolenaar5e59ea52022-07-01 22:26:20 +01004379{
4380 char_u *p;
4381
K.Takata2ebcc352022-07-14 17:25:14 +01004382 if (enc_utf8 && !utf_valid_string(word, end))
Bram Moolenaar5e59ea52022-07-01 22:26:20 +01004383 return FALSE;
K.Takata2ebcc352022-07-14 17:25:14 +01004384 for (p = word; *p != NUL && p < end; p += mb_ptr2len(p))
Bram Moolenaar5e59ea52022-07-01 22:26:20 +01004385 if (*p < ' ' || (p[0] == '/' && p[1] == NUL))
4386 return FALSE;
4387 return TRUE;
4388}
4389
4390/*
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004391 * Store a word in the tree(s).
4392 * Always store it in the case-folded tree. For a keep-case word this is
4393 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4394 * used to find suggestions.
4395 * For a keep-case word also store it in the keep-case tree.
4396 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
4397 * compound flag.
4398 */
4399 static int
4400store_word(
4401 spellinfo_T *spin,
4402 char_u *word,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004403 int flags, // extra flags, WF_BANNED
4404 int region, // supported region(s)
4405 char_u *pfxlist, // list of prefix IDs or NULL
4406 int need_affix) // only store word with affix ID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004407{
4408 int len = (int)STRLEN(word);
4409 int ct = captype(word, word + len);
4410 char_u foldword[MAXWLEN];
4411 int res = OK;
4412 char_u *p;
4413
Bram Moolenaar7c824682022-05-08 22:32:58 +01004414 // Avoid adding illegal bytes to the word tree.
K.Takata2ebcc352022-07-14 17:25:14 +01004415 if (!valid_spell_word(word, word + len))
Bram Moolenaar7c824682022-05-08 22:32:58 +01004416 return FAIL;
4417
Bram Moolenaar4f135272021-06-11 19:07:40 +02004418 (void)spell_casefold(curwin, word, len, foldword, MAXWLEN);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004419 for (p = pfxlist; res == OK; ++p)
4420 {
4421 if (!need_affix || (p != NULL && *p != NUL))
4422 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
4423 region, p == NULL ? 0 : *p);
4424 if (p == NULL || *p == NUL)
4425 break;
4426 }
4427 ++spin->si_foldwcount;
4428
4429 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
4430 {
4431 for (p = pfxlist; res == OK; ++p)
4432 {
4433 if (!need_affix || (p != NULL && *p != NUL))
4434 res = tree_add_word(spin, word, spin->si_keeproot, flags,
4435 region, p == NULL ? 0 : *p);
4436 if (p == NULL || *p == NUL)
4437 break;
4438 }
4439 ++spin->si_keepwcount;
4440 }
4441 return res;
4442}
4443
4444/*
4445 * Add word "word" to a word tree at "root".
4446 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
4447 * "rare" and "region" is the condition nr.
4448 * Returns FAIL when out of memory.
4449 */
4450 static int
4451tree_add_word(
4452 spellinfo_T *spin,
4453 char_u *word,
4454 wordnode_T *root,
4455 int flags,
4456 int region,
4457 int affixID)
4458{
4459 wordnode_T *node = root;
4460 wordnode_T *np;
4461 wordnode_T *copyp, **copyprev;
4462 wordnode_T **prev = NULL;
4463 int i;
4464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004465 // Add each byte of the word to the tree, including the NUL at the end.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004466 for (i = 0; ; ++i)
4467 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004468 // When there is more than one reference to this node we need to make
4469 // a copy, so that we can modify it. Copy the whole list of siblings
4470 // (we don't optimize for a partly shared list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004471 if (node != NULL && node->wn_refs > 1)
4472 {
4473 --node->wn_refs;
4474 copyprev = prev;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004475 FOR_ALL_NODE_SIBLINGS(node, copyp)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004476 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004477 // Allocate a new node and copy the info.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004478 np = get_wordnode(spin);
4479 if (np == NULL)
4480 return FAIL;
4481 np->wn_child = copyp->wn_child;
4482 if (np->wn_child != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004483 ++np->wn_child->wn_refs; // child gets extra ref
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004484 np->wn_byte = copyp->wn_byte;
4485 if (np->wn_byte == NUL)
4486 {
4487 np->wn_flags = copyp->wn_flags;
4488 np->wn_region = copyp->wn_region;
4489 np->wn_affixID = copyp->wn_affixID;
4490 }
4491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004492 // Link the new node in the list, there will be one ref.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004493 np->wn_refs = 1;
4494 if (copyprev != NULL)
4495 *copyprev = np;
4496 copyprev = &np->wn_sibling;
4497
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004498 // Let "node" point to the head of the copied list.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004499 if (copyp == node)
4500 node = np;
4501 }
4502 }
4503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004504 // Look for the sibling that has the same character. They are sorted
4505 // on byte value, thus stop searching when a sibling is found with a
4506 // higher byte value. For zero bytes (end of word) the sorting is
4507 // done on flags and then on affixID.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004508 while (node != NULL
4509 && (node->wn_byte < word[i]
4510 || (node->wn_byte == NUL
4511 && (flags < 0
4512 ? node->wn_affixID < (unsigned)affixID
4513 : (node->wn_flags < (unsigned)(flags & WN_MASK)
4514 || (node->wn_flags == (flags & WN_MASK)
4515 && (spin->si_sugtree
4516 ? (node->wn_region & 0xffff) < region
4517 : node->wn_affixID
4518 < (unsigned)affixID)))))))
4519 {
4520 prev = &node->wn_sibling;
4521 node = *prev;
4522 }
4523 if (node == NULL
4524 || node->wn_byte != word[i]
4525 || (word[i] == NUL
4526 && (flags < 0
4527 || spin->si_sugtree
4528 || node->wn_flags != (flags & WN_MASK)
4529 || node->wn_affixID != affixID)))
4530 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004531 // Allocate a new node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004532 np = get_wordnode(spin);
4533 if (np == NULL)
4534 return FAIL;
4535 np->wn_byte = word[i];
4536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004537 // If "node" is NULL this is a new child or the end of the sibling
4538 // list: ref count is one. Otherwise use ref count of sibling and
4539 // make ref count of sibling one (matters when inserting in front
4540 // of the list of siblings).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004541 if (node == NULL)
4542 np->wn_refs = 1;
4543 else
4544 {
4545 np->wn_refs = node->wn_refs;
4546 node->wn_refs = 1;
4547 }
4548 if (prev != NULL)
4549 *prev = np;
4550 np->wn_sibling = node;
4551 node = np;
4552 }
4553
4554 if (word[i] == NUL)
4555 {
4556 node->wn_flags = flags;
4557 node->wn_region |= region;
4558 node->wn_affixID = affixID;
4559 break;
4560 }
4561 prev = &node->wn_child;
4562 node = *prev;
4563 }
4564#ifdef SPELL_PRINTTREE
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004565 smsg("Added \"%s\"", word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004566 spell_print_tree(root->wn_sibling);
4567#endif
4568
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004569 // count nr of words added since last message
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004570 ++spin->si_msg_count;
4571
4572 if (spin->si_compress_cnt > 1)
4573 {
4574 if (--spin->si_compress_cnt == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004575 // Did enough words to lower the block count limit.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004576 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004577 }
4578
4579 /*
4580 * When we have allocated lots of memory we need to compress the word tree
4581 * to free up some room. But compression is slow, and we might actually
4582 * need that room, thus only compress in the following situations:
4583 * 1. When not compressed before (si_compress_cnt == 0): when using
4584 * "compress_start" blocks.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004585 * 2. When compressed before and used "compress_inc" blocks before
4586 * adding "compress_added" words (si_compress_cnt > 1).
4587 * 3. When compressed before, added "compress_added" words
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004588 * (si_compress_cnt == 1) and the number of free nodes drops below the
4589 * maximum word length.
4590 */
dundargocc57b5bc2022-11-02 13:30:51 +00004591#ifndef SPELL_COMPRESS_ALWAYS
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004592 if (spin->si_compress_cnt == 1
4593 ? spin->si_free_count < MAXWLEN
4594 : spin->si_blocks_cnt >= compress_start)
4595#endif
4596 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004597 // Decrement the block counter. The effect is that we compress again
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004598 // when the freed up room has been used and another "compress_inc"
4599 // blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004600 // been added, then the limit is put back again.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004601 spin->si_blocks_cnt -= compress_inc;
4602 spin->si_compress_cnt = compress_added;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004603
4604 if (spin->si_verbose)
4605 {
4606 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01004607 msg_puts(_(msg_compressing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004608 msg_clr_eos();
4609 msg_didout = FALSE;
4610 msg_col = 0;
4611 out_flush();
4612 }
4613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004614 // Compress both trees. Either they both have many nodes, which makes
4615 // compression useful, or one of them is small, which means
4616 // compression goes fast. But when filling the soundfold word tree
4617 // there is no keep-case tree.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004618 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004619 if (affixID >= 0)
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004620 wordtree_compress(spin, spin->si_keeproot, "keep-case");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004621 }
4622
4623 return OK;
4624}
4625
4626/*
4627 * Get a wordnode_T, either from the list of previously freed nodes or
4628 * allocate a new one.
4629 * Returns NULL when out of memory.
4630 */
4631 static wordnode_T *
4632get_wordnode(spellinfo_T *spin)
4633{
4634 wordnode_T *n;
4635
4636 if (spin->si_first_free == NULL)
4637 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
4638 else
4639 {
4640 n = spin->si_first_free;
4641 spin->si_first_free = n->wn_child;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004642 CLEAR_POINTER(n);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004643 --spin->si_free_count;
4644 }
4645#ifdef SPELL_PRINTTREE
4646 if (n != NULL)
4647 n->wn_nr = ++spin->si_wordnode_nr;
4648#endif
4649 return n;
4650}
4651
4652/*
4653 * Decrement the reference count on a node (which is the head of a list of
4654 * siblings). If the reference count becomes zero free the node and its
4655 * siblings.
4656 * Returns the number of nodes actually freed.
4657 */
4658 static int
4659deref_wordnode(spellinfo_T *spin, wordnode_T *node)
4660{
4661 wordnode_T *np;
4662 int cnt = 0;
4663
4664 if (--node->wn_refs == 0)
4665 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004666 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004667 {
4668 if (np->wn_child != NULL)
4669 cnt += deref_wordnode(spin, np->wn_child);
4670 free_wordnode(spin, np);
4671 ++cnt;
4672 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004673 ++cnt; // length field
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004674 }
4675 return cnt;
4676}
4677
4678/*
4679 * Free a wordnode_T for re-use later.
4680 * Only the "wn_child" field becomes invalid.
4681 */
4682 static void
4683free_wordnode(spellinfo_T *spin, wordnode_T *n)
4684{
4685 n->wn_child = spin->si_first_free;
4686 spin->si_first_free = n;
4687 ++spin->si_free_count;
4688}
4689
4690/*
4691 * Compress a tree: find tails that are identical and can be shared.
4692 */
4693 static void
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004694wordtree_compress(spellinfo_T *spin, wordnode_T *root, char *name)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004695{
4696 hashtab_T ht;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004697 long n;
4698 long tot = 0;
4699 long perc;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004701 // Skip the root itself, it's not actually used. The first sibling is the
4702 // start of the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004703 if (root->wn_sibling != NULL)
4704 {
4705 hash_init(&ht);
4706 n = node_compress(spin, root->wn_sibling, &ht, &tot);
4707
4708#ifndef SPELL_PRINTTREE
4709 if (spin->si_verbose || p_verbose > 2)
4710#endif
4711 {
4712 if (tot > 1000000)
4713 perc = (tot - n) / (tot / 100);
4714 else if (tot == 0)
4715 perc = 0;
4716 else
4717 perc = (tot - n) * 100 / tot;
4718 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004719 _("Compressed %s: %ld of %ld nodes; %ld (%ld%%) remaining"),
4720 name, n, tot, tot - n, perc);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004721 spell_message(spin, IObuff);
4722 }
4723#ifdef SPELL_PRINTTREE
4724 spell_print_tree(root->wn_sibling);
4725#endif
4726 hash_clear(&ht);
4727 }
4728}
4729
4730/*
4731 * Compress a node, its siblings and its children, depth first.
4732 * Returns the number of compressed nodes.
4733 */
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004734 static long
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004735node_compress(
4736 spellinfo_T *spin,
4737 wordnode_T *node,
4738 hashtab_T *ht,
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004739 long *tot) // total count of nodes before compressing,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004740 // incremented while going through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004741{
4742 wordnode_T *np;
4743 wordnode_T *tp;
4744 wordnode_T *child;
4745 hash_T hash;
4746 hashitem_T *hi;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004747 long len = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004748 unsigned nr, n;
Bram Moolenaar59f88fb2020-06-03 20:51:11 +02004749 long compressed = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004750
4751 /*
4752 * Go through the list of siblings. Compress each child and then try
4753 * finding an identical child to replace it.
4754 * Note that with "child" we mean not just the node that is pointed to,
4755 * but the whole list of siblings of which the child node is the first.
4756 */
4757 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
4758 {
4759 ++len;
4760 if ((child = np->wn_child) != NULL)
4761 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004762 // Compress the child first. This fills hashkey.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004763 compressed += node_compress(spin, child, ht, tot);
4764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004765 // Try to find an identical child.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004766 hash = hash_hash(child->wn_u1.hashkey);
4767 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
4768 if (!HASHITEM_EMPTY(hi))
4769 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004770 // There are children we encountered before with a hash value
4771 // identical to the current child. Now check if there is one
4772 // that is really identical.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004773 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
4774 if (node_equal(child, tp))
4775 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004776 // Found one! Now use that child in place of the
4777 // current one. This means the current child and all
4778 // its siblings is unlinked from the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004779 ++tp->wn_refs;
4780 compressed += deref_wordnode(spin, child);
4781 np->wn_child = tp;
4782 break;
4783 }
4784 if (tp == NULL)
4785 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004786 // No other child with this hash value equals the child of
4787 // the node, add it to the linked list after the first
4788 // item.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004789 tp = HI2WN(hi);
4790 child->wn_u2.next = tp->wn_u2.next;
4791 tp->wn_u2.next = child;
4792 }
4793 }
4794 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004795 // No other child has this hash value, add it to the
4796 // hashtable.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004797 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
4798 }
4799 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004800 *tot += len + 1; // add one for the node that stores the length
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004801
4802 /*
4803 * Make a hash key for the node and its siblings, so that we can quickly
4804 * find a lookalike node. This must be done after compressing the sibling
4805 * list, otherwise the hash key would become invalid by the compression.
4806 */
4807 node->wn_u1.hashkey[0] = len;
4808 nr = 0;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004809 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004810 {
4811 if (np->wn_byte == NUL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004812 // end node: use wn_flags, wn_region and wn_affixID
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004813 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
4814 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004815 // byte node: use the byte value and the child pointer
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004816 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
4817 nr = nr * 101 + n;
4818 }
4819
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004820 // Avoid NUL bytes, it terminates the hash key.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004821 n = nr & 0xff;
4822 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
4823 n = (nr >> 8) & 0xff;
4824 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
4825 n = (nr >> 16) & 0xff;
4826 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
4827 n = (nr >> 24) & 0xff;
4828 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
4829 node->wn_u1.hashkey[5] = NUL;
4830
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004831 // Check for CTRL-C pressed now and then.
Bram Moolenaar408c23b2020-06-03 22:15:45 +02004832 veryfast_breakcheck();
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004833
4834 return compressed;
4835}
4836
4837/*
4838 * Return TRUE when two nodes have identical siblings and children.
4839 */
4840 static int
4841node_equal(wordnode_T *n1, wordnode_T *n2)
4842{
4843 wordnode_T *p1;
4844 wordnode_T *p2;
4845
4846 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
4847 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
4848 if (p1->wn_byte != p2->wn_byte
4849 || (p1->wn_byte == NUL
4850 ? (p1->wn_flags != p2->wn_flags
4851 || p1->wn_region != p2->wn_region
4852 || p1->wn_affixID != p2->wn_affixID)
4853 : (p1->wn_child != p2->wn_child)))
4854 break;
4855
4856 return p1 == NULL && p2 == NULL;
4857}
4858
Bram Moolenaareae1b912019-05-09 15:12:55 +02004859static int rep_compare(const void *s1, const void *s2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004860
4861/*
4862 * Function given to qsort() to sort the REP items on "from" string.
4863 */
4864 static int
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004865rep_compare(const void *s1, const void *s2)
4866{
4867 fromto_T *p1 = (fromto_T *)s1;
4868 fromto_T *p2 = (fromto_T *)s2;
4869
4870 return STRCMP(p1->ft_from, p2->ft_from);
4871}
4872
4873/*
4874 * Write the Vim .spl file "fname".
4875 * Return FAIL or OK;
4876 */
4877 static int
4878write_vim_spell(spellinfo_T *spin, char_u *fname)
4879{
4880 FILE *fd;
4881 int regionmask;
4882 int round;
4883 wordnode_T *tree;
4884 int nodecount;
4885 int i;
4886 int l;
4887 garray_T *gap;
4888 fromto_T *ftp;
4889 char_u *p;
4890 int rr;
4891 int retval = OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004892 size_t fwv = 1; // collect return value of fwrite() to avoid
4893 // warnings from picky compiler
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004894
4895 fd = mch_fopen((char *)fname, "w");
4896 if (fd == NULL)
4897 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004898 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004899 return FAIL;
4900 }
4901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004902 // <HEADER>: <fileID> <versionnr>
4903 // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004904 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
4905 if (fwv != (size_t)1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004906 // Catch first write error, don't try writing more.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004907 goto theend;
4908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004909 putc(VIMSPELLVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004910
4911 /*
4912 * <SECTIONS>: <section> ... <sectionend>
4913 */
4914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004915 // SN_INFO: <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004916 if (spin->si_info != NULL)
4917 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004918 putc(SN_INFO, fd); // <sectionID>
4919 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004920
4921 i = (int)STRLEN(spin->si_info);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004922 put_bytes(fd, (long_u)i, 4); // <sectionlen>
4923 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); // <infotext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004924 }
4925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004926 // SN_REGION: <regionname> ...
4927 // Write the region names only if there is more than one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004928 if (spin->si_region_count > 1)
4929 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004930 putc(SN_REGION, fd); // <sectionID>
4931 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004932 l = spin->si_region_count * 2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004933 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004934 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004935 // <regionname> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004936 regionmask = (1 << spin->si_region_count) - 1;
4937 }
4938 else
4939 regionmask = 0;
4940
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004941 // SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
4942 //
4943 // The table with character flags and the table for case folding.
4944 // This makes sure the same characters are recognized as word characters
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00004945 // when generating and when using a spell file.
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004946 // Skip this for ASCII, the table may conflict with the one used for
4947 // 'encoding'.
4948 // Also skip this for an .add.spl file, the main spell file must contain
4949 // the table (avoids that it conflicts). File is shorter too.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004950 if (!spin->si_ascii && !spin->si_add)
4951 {
4952 char_u folchars[128 * 8];
4953 int flags;
4954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004955 putc(SN_CHARFLAGS, fd); // <sectionID>
4956 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004958 // Form the <folchars> string first, we need to know its length.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004959 l = 0;
4960 for (i = 128; i < 256; ++i)
4961 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004962 if (has_mbyte)
4963 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
4964 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004965 folchars[l++] = spelltab.st_fold[i];
4966 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004967 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004969 fputc(128, fd); // <charflagslen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004970 for (i = 128; i < 256; ++i)
4971 {
4972 flags = 0;
4973 if (spelltab.st_isw[i])
4974 flags |= CF_WORD;
4975 if (spelltab.st_isu[i])
4976 flags |= CF_UPPER;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004977 fputc(flags, fd); // <charflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004978 }
4979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004980 put_bytes(fd, (long_u)l, 2); // <folcharslen>
4981 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); // <folchars>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004982 }
4983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004984 // SN_MIDWORD: <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004985 if (spin->si_midword != NULL)
4986 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004987 putc(SN_MIDWORD, fd); // <sectionID>
4988 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004989
4990 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004991 put_bytes(fd, (long_u)i, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004992 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004993 // <midword>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004994 }
4995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004996 // SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02004997 if (spin->si_prefcond.ga_len > 0)
4998 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004999 putc(SN_PREFCOND, fd); // <sectionID>
5000 putc(SNF_REQUIRED, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005001
=?UTF-8?q?Bj=C3=B6rn=20Linse?=1daedc82021-12-10 20:39:17 +00005002 l = write_spell_prefcond(NULL, &spin->si_prefcond, &fwv);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005003 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005004
=?UTF-8?q?Bj=C3=B6rn=20Linse?=1daedc82021-12-10 20:39:17 +00005005 write_spell_prefcond(fd, &spin->si_prefcond, &fwv);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005006 }
5007
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005008 // SN_REP: <repcount> <rep> ...
5009 // SN_SAL: <salflags> <salcount> <sal> ...
5010 // SN_REPSAL: <repcount> <rep> ...
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 // round 1: SN_REP section
5013 // round 2: SN_SAL section (unless SN_SOFO is used)
5014 // round 3: SN_REPSAL section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005015 for (round = 1; round <= 3; ++round)
5016 {
5017 if (round == 1)
5018 gap = &spin->si_rep;
5019 else if (round == 2)
5020 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005021 // Don't write SN_SAL when using a SN_SOFO section
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005022 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5023 continue;
5024 gap = &spin->si_sal;
5025 }
5026 else
5027 gap = &spin->si_repsal;
5028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005029 // Don't write the section if there are no items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005030 if (gap->ga_len == 0)
5031 continue;
5032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005033 // Sort the REP/REPSAL items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005034 if (round != 2)
5035 qsort(gap->ga_data, (size_t)gap->ga_len,
5036 sizeof(fromto_T), rep_compare);
5037
5038 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005039 putc(i, fd); // <sectionID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005041 // This is for making suggestions, section is not required.
5042 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005044 // Compute the length of what follows.
5045 l = 2; // count <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005046 for (i = 0; i < gap->ga_len; ++i)
5047 {
5048 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005049 l += 1 + (int)STRLEN(ftp->ft_from); // count <*fromlen> and <*from>
5050 l += 1 + (int)STRLEN(ftp->ft_to); // count <*tolen> and <*to>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005051 }
5052 if (round == 2)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005053 ++l; // count <salflags>
5054 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005055
5056 if (round == 2)
5057 {
5058 i = 0;
5059 if (spin->si_followup)
5060 i |= SAL_F0LLOWUP;
5061 if (spin->si_collapse)
5062 i |= SAL_COLLAPSE;
5063 if (spin->si_rem_accents)
5064 i |= SAL_REM_ACCENTS;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005065 putc(i, fd); // <salflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005066 }
5067
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005068 put_bytes(fd, (long_u)gap->ga_len, 2); // <repcount> or <salcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005069 for (i = 0; i < gap->ga_len; ++i)
5070 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005071 // <rep> : <repfromlen> <repfrom> <reptolen> <repto>
5072 // <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005073 ftp = &((fromto_T *)gap->ga_data)[i];
5074 for (rr = 1; rr <= 2; ++rr)
5075 {
5076 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5077 l = (int)STRLEN(p);
5078 putc(l, fd);
5079 if (l > 0)
5080 fwv &= fwrite(p, l, (size_t)1, fd);
5081 }
5082 }
5083
5084 }
5085
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005086 // SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
5087 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005088 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5089 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005090 putc(SN_SOFO, fd); // <sectionID>
5091 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005092
5093 l = (int)STRLEN(spin->si_sofofr);
5094 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005095 // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005096
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005097 put_bytes(fd, (long_u)l, 2); // <sofofromlen>
5098 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); // <sofofrom>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005099
5100 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005101 put_bytes(fd, (long_u)l, 2); // <sofotolen>
5102 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); // <sofoto>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005103 }
5104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005105 // SN_WORDS: <word> ...
5106 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005107 if (spin->si_commonwords.ht_used > 0)
5108 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005109 putc(SN_WORDS, fd); // <sectionID>
5110 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 // round 1: count the bytes
5113 // round 2: write the bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005114 for (round = 1; round <= 2; ++round)
5115 {
5116 int todo;
5117 int len = 0;
5118 hashitem_T *hi;
5119
5120 todo = (int)spin->si_commonwords.ht_used;
5121 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
5122 if (!HASHITEM_EMPTY(hi))
5123 {
5124 l = (int)STRLEN(hi->hi_key) + 1;
5125 len += l;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005126 if (round == 2) // <word>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005127 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
5128 --todo;
5129 }
5130 if (round == 1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005131 put_bytes(fd, (long_u)len, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005132 }
5133 }
5134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005135 // SN_MAP: <mapstr>
5136 // This is for making suggestions, section is not required.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005137 if (spin->si_map.ga_len > 0)
5138 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005139 putc(SN_MAP, fd); // <sectionID>
5140 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005141 l = spin->si_map.ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005142 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005143 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005144 // <mapstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005145 }
5146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005147 // SN_SUGFILE: <timestamp>
5148 // This is used to notify that a .sug file may be available and at the
5149 // same time allows for checking that a .sug file that is found matches
5150 // with this .spl file. That's because the word numbers must be exactly
5151 // right.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005152 if (!spin->si_nosugfile
5153 && (spin->si_sal.ga_len > 0
5154 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
5155 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005156 putc(SN_SUGFILE, fd); // <sectionID>
5157 putc(0, fd); // <sectionflags>
5158 put_bytes(fd, (long_u)8, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005160 // Set si_sugtime and write it to the file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005161 spin->si_sugtime = time(NULL);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005162 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005163 }
5164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005165 // SN_NOSPLITSUGS: nothing
5166 // This is used to notify that no suggestions with word splits are to be
5167 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005168 if (spin->si_nosplitsugs)
5169 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005170 putc(SN_NOSPLITSUGS, fd); // <sectionID>
5171 putc(0, fd); // <sectionflags>
5172 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005173 }
5174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005175 // SN_NOCOMPUNDSUGS: nothing
5176 // This is used to notify that no suggestions with compounds are to be
5177 // made.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005178 if (spin->si_nocompoundsugs)
5179 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005180 putc(SN_NOCOMPOUNDSUGS, fd); // <sectionID>
5181 putc(0, fd); // <sectionflags>
5182 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005183 }
5184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005185 // SN_COMPOUND: compound info.
5186 // We don't mark it required, when not supported all compound words will
5187 // be bad words.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005188 if (spin->si_compflags != NULL)
5189 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005190 putc(SN_COMPOUND, fd); // <sectionID>
5191 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005192
5193 l = (int)STRLEN(spin->si_compflags);
5194 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5195 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005196 put_bytes(fd, (long_u)(l + 7), 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005198 putc(spin->si_compmax, fd); // <compmax>
5199 putc(spin->si_compminlen, fd); // <compminlen>
5200 putc(spin->si_compsylmax, fd); // <compsylmax>
5201 putc(0, fd); // for Vim 7.0b compatibility
5202 putc(spin->si_compoptions, fd); // <compoptions>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005203 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005204 // <comppatcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005205 for (i = 0; i < spin->si_comppat.ga_len; ++i)
5206 {
5207 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005208 putc((int)STRLEN(p), fd); // <comppatlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005209 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005210 // <comppattext>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005211 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005212 // <compflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005213 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
5214 (size_t)1, fd);
5215 }
5216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005217 // SN_NOBREAK: NOBREAK flag
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005218 if (spin->si_nobreak)
5219 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005220 putc(SN_NOBREAK, fd); // <sectionID>
5221 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005222
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005223 // It's empty, the presence of the section flags the feature.
5224 put_bytes(fd, (long_u)0, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005225 }
5226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005227 // SN_SYLLABLE: syllable info.
5228 // We don't mark it required, when not supported syllables will not be
5229 // counted.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005230 if (spin->si_syllable != NULL)
5231 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005232 putc(SN_SYLLABLE, fd); // <sectionID>
5233 putc(0, fd); // <sectionflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005234
5235 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005236 put_bytes(fd, (long_u)l, 4); // <sectionlen>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005237 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005238 // <syllable>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005239 }
5240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005241 // end of <SECTIONS>
5242 putc(SN_END, fd); // <sectionend>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005243
5244
5245 /*
5246 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
5247 */
5248 spin->si_memtot = 0;
5249 for (round = 1; round <= 3; ++round)
5250 {
5251 if (round == 1)
5252 tree = spin->si_foldroot->wn_sibling;
5253 else if (round == 2)
5254 tree = spin->si_keeproot->wn_sibling;
5255 else
5256 tree = spin->si_prefroot->wn_sibling;
5257
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005258 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005259 clear_node(tree);
5260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005261 // Count the number of nodes. Needed to be able to allocate the
5262 // memory when reading the nodes. Also fills in index for shared
5263 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005264 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
5265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005266 // number of nodes in 4 bytes
5267 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005268 spin->si_memtot += nodecount + nodecount * sizeof(int);
5269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005270 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005271 (void)put_node(fd, tree, 0, regionmask, round == 3);
5272 }
5273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005274 // Write another byte to check for errors (file system full).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005275 if (putc(0, fd) == EOF)
5276 retval = FAIL;
5277theend:
5278 if (fclose(fd) == EOF)
5279 retval = FAIL;
5280
5281 if (fwv != (size_t)1)
5282 retval = FAIL;
5283 if (retval == FAIL)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005284 emsg(_(e_error_while_writing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005285
5286 return retval;
5287}
5288
5289/*
5290 * Clear the index and wnode fields of "node", it siblings and its
5291 * children. This is needed because they are a union with other items to save
5292 * space.
5293 */
5294 static void
5295clear_node(wordnode_T *node)
5296{
5297 wordnode_T *np;
5298
5299 if (node != NULL)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005300 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005301 {
5302 np->wn_u1.index = 0;
5303 np->wn_u2.wnode = NULL;
5304
5305 if (np->wn_byte != NUL)
5306 clear_node(np->wn_child);
5307 }
5308}
5309
5310
5311/*
5312 * Dump a word tree at node "node".
5313 *
5314 * This first writes the list of possible bytes (siblings). Then for each
5315 * byte recursively write the children.
5316 *
5317 * NOTE: The code here must match the code in read_tree_node(), since
5318 * assumptions are made about the indexes (so that we don't have to write them
5319 * in the file).
5320 *
5321 * Returns the number of nodes used.
5322 */
5323 static int
5324put_node(
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005325 FILE *fd, // NULL when only counting
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005326 wordnode_T *node,
5327 int idx,
5328 int regionmask,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 int prefixtree) // TRUE for PREFIXTREE
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005330{
5331 int newindex = idx;
5332 int siblingcount = 0;
5333 wordnode_T *np;
5334 int flags;
5335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005336 // If "node" is zero the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005337 if (node == NULL)
5338 return 0;
5339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005340 // Store the index where this node is written.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005341 node->wn_u1.index = idx;
5342
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005343 // Count the number of siblings.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005344 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005345 ++siblingcount;
5346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005347 // Write the sibling count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005348 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005349 putc(siblingcount, fd); // <siblingcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005351 // Write each sibling byte and optionally extra info.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005352 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005353 {
5354 if (np->wn_byte == 0)
5355 {
5356 if (fd != NULL)
5357 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005358 // For a NUL byte (end of word) write the flags etc.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005359 if (prefixtree)
5360 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005361 // In PREFIXTREE write the required affixID and the
5362 // associated condition nr (stored in wn_region). The
5363 // byte value is misused to store the "rare" and "not
5364 // combining" flags
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005365 if (np->wn_flags == (short_u)PFX_FLAGS)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005366 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005367 else
5368 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005369 putc(BY_FLAGS, fd); // <byte>
5370 putc(np->wn_flags, fd); // <pflags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005371 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005372 putc(np->wn_affixID, fd); // <affixID>
5373 put_bytes(fd, (long_u)np->wn_region, 2); // <prefcondnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005374 }
5375 else
5376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005377 // For word trees we write the flag/region items.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005378 flags = np->wn_flags;
5379 if (regionmask != 0 && np->wn_region != regionmask)
5380 flags |= WF_REGION;
5381 if (np->wn_affixID != 0)
5382 flags |= WF_AFX;
5383 if (flags == 0)
5384 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005385 // word without flags or region
5386 putc(BY_NOFLAGS, fd); // <byte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005387 }
5388 else
5389 {
5390 if (np->wn_flags >= 0x100)
5391 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005392 putc(BY_FLAGS2, fd); // <byte>
5393 putc(flags, fd); // <flags>
5394 putc((unsigned)flags >> 8, fd); // <flags2>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005395 }
5396 else
5397 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005398 putc(BY_FLAGS, fd); // <byte>
5399 putc(flags, fd); // <flags>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005400 }
5401 if (flags & WF_REGION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005402 putc(np->wn_region, fd); // <region>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005403 if (flags & WF_AFX)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005404 putc(np->wn_affixID, fd); // <affixID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005405 }
5406 }
5407 }
5408 }
5409 else
5410 {
5411 if (np->wn_child->wn_u1.index != 0
5412 && np->wn_child->wn_u2.wnode != node)
5413 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005414 // The child is written elsewhere, write the reference.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005415 if (fd != NULL)
5416 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005417 putc(BY_INDEX, fd); // <byte>
5418 // <nodeidx>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005419 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
5420 }
5421 }
5422 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005423 // We will write the child below and give it an index.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005424 np->wn_child->wn_u2.wnode = node;
5425
5426 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005427 if (putc(np->wn_byte, fd) == EOF) // <byte> or <xbyte>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005428 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005429 emsg(_(e_error_while_writing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005430 return 0;
5431 }
5432 }
5433 }
5434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005435 // Space used in the array when reading: one for each sibling and one for
5436 // the count.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005437 newindex += siblingcount + 1;
5438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005439 // Recursively dump the children of each sibling.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005440 FOR_ALL_NODE_SIBLINGS(node, np)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005441 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5442 newindex = put_node(fd, np->wn_child, newindex, regionmask,
5443 prefixtree);
5444
5445 return newindex;
5446}
5447
5448
5449/*
5450 * ":mkspell [-ascii] outfile infile ..."
5451 * ":mkspell [-ascii] addfile"
5452 */
5453 void
5454ex_mkspell(exarg_T *eap)
5455{
5456 int fcount;
5457 char_u **fnames;
5458 char_u *arg = eap->arg;
5459 int ascii = FALSE;
5460
5461 if (STRNCMP(arg, "-ascii", 6) == 0)
5462 {
5463 ascii = TRUE;
5464 arg = skipwhite(arg + 6);
5465 }
5466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005467 // Expand all the remaining arguments (e.g., $VIMRUNTIME).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005468 if (get_arglist_exp(arg, &fcount, &fnames, FALSE) == OK)
5469 {
5470 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
5471 FreeWild(fcount, fnames);
5472 }
5473}
5474
5475/*
5476 * Create the .sug file.
5477 * Uses the soundfold info in "spin".
5478 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
5479 */
5480 static void
5481spell_make_sugfile(spellinfo_T *spin, char_u *wfname)
5482{
5483 char_u *fname = NULL;
5484 int len;
5485 slang_T *slang;
5486 int free_slang = FALSE;
5487
5488 /*
5489 * Read back the .spl file that was written. This fills the required
5490 * info for soundfolding. This also uses less memory than the
5491 * pointer-linked version of the trie. And it avoids having two versions
5492 * of the code for the soundfolding stuff.
5493 * It might have been done already by spell_reload_one().
5494 */
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005495 FOR_ALL_SPELL_LANGS(slang)
Bram Moolenaar99499b12019-05-23 21:35:48 +02005496 if (fullpathcmp(wfname, slang->sl_fname, FALSE, TRUE) == FPC_SAME)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005497 break;
5498 if (slang == NULL)
5499 {
5500 spell_message(spin, (char_u *)_("Reading back spell file..."));
5501 slang = spell_load_file(wfname, NULL, NULL, FALSE);
5502 if (slang == NULL)
5503 return;
5504 free_slang = TRUE;
5505 }
5506
5507 /*
5508 * Clear the info in "spin" that is used.
5509 */
5510 spin->si_blocks = NULL;
5511 spin->si_blocks_cnt = 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005512 spin->si_compress_cnt = 0; // will stay at 0 all the time
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005513 spin->si_free_count = 0;
5514 spin->si_first_free = NULL;
5515 spin->si_foldwcount = 0;
5516
5517 /*
5518 * Go through the trie of good words, soundfold each word and add it to
5519 * the soundfold trie.
5520 */
5521 spell_message(spin, (char_u *)_("Performing soundfolding..."));
5522 if (sug_filltree(spin, slang) == FAIL)
5523 goto theend;
5524
5525 /*
5526 * Create the table which links each soundfold word with a list of the
5527 * good words it may come from. Creates buffer "spin->si_spellbuf".
5528 * This also removes the wordnr from the NUL byte entries to make
5529 * compression possible.
5530 */
5531 if (sug_maketable(spin) == FAIL)
5532 goto theend;
5533
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005534 smsg(_("Number of words after soundfolding: %ld"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005535 (long)spin->si_spellbuf->b_ml.ml_line_count);
5536
5537 /*
5538 * Compress the soundfold trie.
5539 */
5540 spell_message(spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02005541 wordtree_compress(spin, spin->si_foldroot, "case-folded");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005542
5543 /*
5544 * Write the .sug file.
5545 * Make the file name by changing ".spl" to ".sug".
5546 */
5547 fname = alloc(MAXPATHL);
5548 if (fname == NULL)
5549 goto theend;
5550 vim_strncpy(fname, wfname, MAXPATHL - 1);
5551 len = (int)STRLEN(fname);
5552 fname[len - 2] = 'u';
5553 fname[len - 1] = 'g';
5554 sug_write(spin, fname);
5555
5556theend:
5557 vim_free(fname);
5558 if (free_slang)
5559 slang_free(slang);
5560 free_blocks(spin->si_blocks);
5561 close_spellbuf(spin->si_spellbuf);
5562}
5563
5564/*
5565 * Build the soundfold trie for language "slang".
5566 */
5567 static int
5568sug_filltree(spellinfo_T *spin, slang_T *slang)
5569{
5570 char_u *byts;
5571 idx_T *idxs;
5572 int depth;
5573 idx_T arridx[MAXWLEN];
5574 int curi[MAXWLEN];
5575 char_u tword[MAXWLEN];
5576 char_u tsalword[MAXWLEN];
5577 int c;
5578 idx_T n;
5579 unsigned words_done = 0;
5580 int wordcount[MAXWLEN];
5581
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005582 // We use si_foldroot for the soundfolded trie.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005583 spin->si_foldroot = wordtree_alloc(spin);
5584 if (spin->si_foldroot == NULL)
5585 return FAIL;
5586
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005587 // let tree_add_word() know we're adding to the soundfolded tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005588 spin->si_sugtree = TRUE;
5589
5590 /*
5591 * Go through the whole case-folded tree, soundfold each word and put it
Bram Moolenaar6669de12022-08-21 20:33:47 +01005592 * in the trie. Bail out if the tree is empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005593 */
5594 byts = slang->sl_fbyts;
5595 idxs = slang->sl_fidxs;
Bram Moolenaar6669de12022-08-21 20:33:47 +01005596 if (byts == NULL || idxs == NULL)
5597 return FAIL;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005598
5599 arridx[0] = 0;
5600 curi[0] = 1;
5601 wordcount[0] = 0;
5602
5603 depth = 0;
5604 while (depth >= 0 && !got_int)
5605 {
5606 if (curi[depth] > byts[arridx[depth]])
5607 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005608 // Done all bytes at this node, go up one level.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005609 idxs[arridx[depth]] = wordcount[depth];
5610 if (depth > 0)
5611 wordcount[depth - 1] += wordcount[depth];
5612
5613 --depth;
5614 line_breakcheck();
5615 }
5616 else
5617 {
5618
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005619 // Do one more byte at this node.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005620 n = arridx[depth] + curi[depth];
5621 ++curi[depth];
5622
5623 c = byts[n];
5624 if (c == 0)
5625 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005626 // Sound-fold the word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005627 tword[depth] = NUL;
5628 spell_soundfold(slang, tword, TRUE, tsalword);
5629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005630 // We use the "flags" field for the MSB of the wordnr,
5631 // "region" for the LSB of the wordnr.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005632 if (tree_add_word(spin, tsalword, spin->si_foldroot,
5633 words_done >> 16, words_done & 0xffff,
5634 0) == FAIL)
5635 return FAIL;
5636
5637 ++words_done;
5638 ++wordcount[depth];
5639
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005640 // Reset the block count each time to avoid compression
5641 // kicking in.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005642 spin->si_blocks_cnt = 0;
5643
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005644 // Skip over any other NUL bytes (same word with different
Bram Moolenaar07399e72020-08-24 20:05:50 +02005645 // flags). But don't go over the end.
5646 while (n + 1 < slang->sl_fbyts_len && byts[n + 1] == 0)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005647 {
5648 ++n;
5649 ++curi[depth];
5650 }
5651 }
5652 else
5653 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005654 // Normal char, go one level deeper.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005655 tword[depth++] = c;
5656 arridx[depth] = idxs[n];
5657 curi[depth] = 1;
5658 wordcount[depth] = 0;
5659 }
5660 }
5661 }
5662
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005663 smsg(_("Total number of words: %d"), words_done);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005664
5665 return OK;
5666}
5667
5668/*
5669 * Make the table that links each word in the soundfold trie to the words it
5670 * can be produced from.
5671 * This is not unlike lines in a file, thus use a memfile to be able to access
5672 * the table efficiently.
5673 * Returns FAIL when out of memory.
5674 */
5675 static int
5676sug_maketable(spellinfo_T *spin)
5677{
5678 garray_T ga;
5679 int res = OK;
5680
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005681 // Allocate a buffer, open a memline for it and create the swap file
5682 // (uses a temp file, not a .swp file).
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005683 spin->si_spellbuf = open_spellbuf();
5684 if (spin->si_spellbuf == NULL)
5685 return FAIL;
5686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005687 // Use a buffer to store the line info, avoids allocating many small
5688 // pieces of memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005689 ga_init2(&ga, 1, 100);
5690
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005691 // recursively go through the tree
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005692 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
5693 res = FAIL;
5694
5695 ga_clear(&ga);
5696 return res;
5697}
5698
5699/*
5700 * Fill the table for one node and its children.
5701 * Returns the wordnr at the start of the node.
5702 * Returns -1 when out of memory.
5703 */
5704 static int
5705sug_filltable(
5706 spellinfo_T *spin,
5707 wordnode_T *node,
5708 int startwordnr,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005709 garray_T *gap) // place to store line of numbers
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005710{
5711 wordnode_T *p, *np;
5712 int wordnr = startwordnr;
5713 int nr;
5714 int prev_nr;
5715
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005716 FOR_ALL_NODE_SIBLINGS(node, p)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005717 {
5718 if (p->wn_byte == NUL)
5719 {
5720 gap->ga_len = 0;
5721 prev_nr = 0;
5722 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
5723 {
5724 if (ga_grow(gap, 10) == FAIL)
5725 return -1;
5726
5727 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005728 // Compute the offset from the previous nr and store the
5729 // offset in a way that it takes a minimum number of bytes.
5730 // It's a bit like utf-8, but without the need to mark
5731 // following bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005732 nr -= prev_nr;
5733 prev_nr += nr;
5734 gap->ga_len += offset2bytes(nr,
5735 (char_u *)gap->ga_data + gap->ga_len);
5736 }
5737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005738 // add the NUL byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005739 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
5740
5741 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
5742 gap->ga_data, gap->ga_len, TRUE) == FAIL)
5743 return -1;
5744 ++wordnr;
5745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005746 // Remove extra NUL entries, we no longer need them. We don't
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005747 // bother freeing the nodes, they won't be reused anyway.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005748 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
5749 p->wn_sibling = p->wn_sibling->wn_sibling;
5750
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005751 // Clear the flags on the remaining NUL node, so that compression
5752 // works a lot better.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005753 p->wn_flags = 0;
5754 p->wn_region = 0;
5755 }
5756 else
5757 {
5758 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
5759 if (wordnr == -1)
5760 return -1;
5761 }
5762 }
5763 return wordnr;
5764}
5765
5766/*
5767 * Convert an offset into a minimal number of bytes.
5768 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
5769 * bytes.
5770 */
5771 static int
5772offset2bytes(int nr, char_u *buf)
5773{
5774 int rem;
5775 int b1, b2, b3, b4;
5776
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005777 // Split the number in parts of base 255. We need to avoid NUL bytes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005778 b1 = nr % 255 + 1;
5779 rem = nr / 255;
5780 b2 = rem % 255 + 1;
5781 rem = rem / 255;
5782 b3 = rem % 255 + 1;
5783 b4 = rem / 255 + 1;
5784
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005785 if (b4 > 1 || b3 > 0x1f) // 4 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005786 {
5787 buf[0] = 0xe0 + b4;
5788 buf[1] = b3;
5789 buf[2] = b2;
5790 buf[3] = b1;
5791 return 4;
5792 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005793 if (b3 > 1 || b2 > 0x3f ) // 3 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005794 {
5795 buf[0] = 0xc0 + b3;
5796 buf[1] = b2;
5797 buf[2] = b1;
5798 return 3;
5799 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005800 if (b2 > 1 || b1 > 0x7f ) // 2 bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005801 {
5802 buf[0] = 0x80 + b2;
5803 buf[1] = b1;
5804 return 2;
5805 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005806 // 1 byte
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005807 buf[0] = b1;
5808 return 1;
5809}
5810
5811/*
5812 * Write the .sug file in "fname".
5813 */
5814 static void
5815sug_write(spellinfo_T *spin, char_u *fname)
5816{
5817 FILE *fd;
5818 wordnode_T *tree;
5819 int nodecount;
5820 int wcount;
5821 char_u *line;
5822 linenr_T lnum;
5823 int len;
5824
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005825 // Create the file. Note that an existing file is silently overwritten!
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005826 fd = mch_fopen((char *)fname, "w");
5827 if (fd == NULL)
5828 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005829 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005830 return;
5831 }
5832
5833 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02005834 _("Writing suggestion file %s..."), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005835 spell_message(spin, IObuff);
5836
5837 /*
5838 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
5839 */
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005840 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) // <fileID>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005841 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005842 emsg(_(e_error_while_writing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005843 goto theend;
5844 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005845 putc(VIMSUGVERSION, fd); // <versionnr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005847 // Write si_sugtime to the file.
5848 put_time(fd, spin->si_sugtime); // <timestamp>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005849
5850 /*
5851 * <SUGWORDTREE>
5852 */
5853 spin->si_memtot = 0;
5854 tree = spin->si_foldroot->wn_sibling;
5855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005856 // Clear the index and wnode fields in the tree.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005857 clear_node(tree);
5858
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005859 // Count the number of nodes. Needed to be able to allocate the
5860 // memory when reading the nodes. Also fills in index for shared
5861 // nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005862 nodecount = put_node(NULL, tree, 0, 0, FALSE);
5863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005864 // number of nodes in 4 bytes
5865 put_bytes(fd, (long_u)nodecount, 4); // <nodecount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005866 spin->si_memtot += nodecount + nodecount * sizeof(int);
5867
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005868 // Write the nodes.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005869 (void)put_node(fd, tree, 0, 0, FALSE);
5870
5871 /*
5872 * <SUGTABLE>: <sugwcount> <sugline> ...
5873 */
5874 wcount = spin->si_spellbuf->b_ml.ml_line_count;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005875 put_bytes(fd, (long_u)wcount, 4); // <sugwcount>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005876
5877 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
5878 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005879 // <sugline>: <sugnr> ... NUL
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005880 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
5881 len = (int)STRLEN(line) + 1;
5882 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
5883 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005884 emsg(_(e_error_while_writing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005885 goto theend;
5886 }
5887 spin->si_memtot += len;
5888 }
5889
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005890 // Write another byte to check for errors.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005891 if (putc(0, fd) == EOF)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00005892 emsg(_(e_error_while_writing));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005893
5894 vim_snprintf((char *)IObuff, IOSIZE,
5895 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
5896 spell_message(spin, IObuff);
5897
5898theend:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005899 // close the file
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005900 fclose(fd);
5901}
5902
5903
5904/*
5905 * Create a Vim spell file from one or more word lists.
5906 * "fnames[0]" is the output file name.
5907 * "fnames[fcount - 1]" is the last input file name.
5908 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5909 * and ".spl" is appended to make the output file name.
5910 */
5911 void
5912mkspell(
5913 int fcount,
5914 char_u **fnames,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005915 int ascii, // -ascii argument given
5916 int over_write, // overwrite existing output file
5917 int added_word) // invoked through "zg"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005918{
5919 char_u *fname = NULL;
5920 char_u *wfname;
5921 char_u **innames;
5922 int incount;
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005923 afffile_T *(afile[MAXREGIONS]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005924 int i;
5925 int len;
5926 stat_T st;
5927 int error = FALSE;
5928 spellinfo_T spin;
5929
Bram Moolenaara80faa82020-04-12 19:37:17 +02005930 CLEAR_FIELD(spin);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005931 spin.si_verbose = !added_word;
5932 spin.si_ascii = ascii;
5933 spin.si_followup = TRUE;
5934 spin.si_rem_accents = TRUE;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005935 ga_init2(&spin.si_rep, sizeof(fromto_T), 20);
5936 ga_init2(&spin.si_repsal, sizeof(fromto_T), 20);
5937 ga_init2(&spin.si_sal, sizeof(fromto_T), 20);
5938 ga_init2(&spin.si_map, sizeof(char_u), 100);
5939 ga_init2(&spin.si_comppat, sizeof(char_u *), 20);
5940 ga_init2(&spin.si_prefcond, sizeof(char_u *), 50);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005941 hash_init(&spin.si_commonwords);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005942 spin.si_newcompID = 127; // start compound ID at first maximum
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005944 // default: fnames[0] is output file, following are input files
Bram Moolenaar927b7dd2020-06-29 22:24:56 +02005945 // When "fcount" is 1 there is only one file.
5946 innames = &fnames[fcount == 1 ? 0 : 1];
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005947 incount = fcount - 1;
5948
5949 wfname = alloc(MAXPATHL);
5950 if (wfname == NULL)
5951 return;
5952
5953 if (fcount >= 1)
5954 {
5955 len = (int)STRLEN(fnames[0]);
5956 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5957 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005958 // For ":mkspell path/en.latin1.add" output file is
5959 // "path/en.latin1.add.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005960 incount = 1;
5961 vim_snprintf((char *)wfname, MAXPATHL, "%s.spl", fnames[0]);
5962 }
5963 else if (fcount == 1)
5964 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005965 // For ":mkspell path/vim" output file is "path/vim.latin1.spl".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005966 incount = 1;
5967 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5968 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5969 }
5970 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5971 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005972 // Name ends in ".spl", use as the file name.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005973 vim_strncpy(wfname, fnames[0], MAXPATHL - 1);
5974 }
5975 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005976 // Name should be language, make the file name from it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005977 vim_snprintf((char *)wfname, MAXPATHL, SPL_FNAME_TMPL,
5978 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005980 // Check for .ascii.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005981 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
5982 spin.si_ascii = TRUE;
5983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005984 // Check for .add.spl.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005985 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
5986 spin.si_add = TRUE;
5987 }
5988
5989 if (incount <= 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005990 emsg(_(e_invalid_argument)); // need at least output and input names
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005991 else if (vim_strchr(gettail(wfname), '_') != NULL)
Bram Moolenaar677658a2022-01-05 16:09:06 +00005992 emsg(_(e_output_file_name_must_not_have_region_name));
Bram Moolenaar2993ac52018-02-10 14:12:43 +01005993 else if (incount > MAXREGIONS)
Bram Moolenaar677658a2022-01-05 16:09:06 +00005994 semsg(_(e_only_up_to_nr_regions_supported), MAXREGIONS);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005995 else
5996 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005997 // Check for overwriting before doing things that may take a lot of
5998 // time.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02005999 if (!over_write && mch_stat((char *)wfname, &st) >= 0)
6000 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02006001 emsg(_(e_file_exists));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006002 goto theend;
6003 }
6004 if (mch_isdir(wfname))
6005 {
Bram Moolenaar4dea2d92022-03-31 11:37:57 +01006006 semsg(_(e_str_is_directory), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006007 goto theend;
6008 }
6009
6010 fname = alloc(MAXPATHL);
6011 if (fname == NULL)
6012 goto theend;
6013
6014 /*
6015 * Init the aff and dic pointers.
6016 * Get the region names if there are more than 2 arguments.
6017 */
6018 for (i = 0; i < incount; ++i)
6019 {
6020 afile[i] = NULL;
6021
6022 if (incount > 1)
6023 {
6024 len = (int)STRLEN(innames[i]);
6025 if (STRLEN(gettail(innames[i])) < 5
6026 || innames[i][len - 3] != '_')
6027 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006028 semsg(_(e_invalid_region_in_str), innames[i]);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006029 goto theend;
6030 }
6031 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6032 spin.si_region_name[i * 2 + 1] =
6033 TOLOWER_ASC(innames[i][len - 1]);
6034 }
6035 }
6036 spin.si_region_count = incount;
6037
6038 spin.si_foldroot = wordtree_alloc(&spin);
6039 spin.si_keeproot = wordtree_alloc(&spin);
6040 spin.si_prefroot = wordtree_alloc(&spin);
6041 if (spin.si_foldroot == NULL
6042 || spin.si_keeproot == NULL
6043 || spin.si_prefroot == NULL)
6044 {
6045 free_blocks(spin.si_blocks);
6046 goto theend;
6047 }
6048
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006049 // When not producing a .add.spl file clear the character table when
6050 // we encounter one in the .aff file. This means we dump the current
6051 // one in the .spl file if the .aff file doesn't define one. That's
6052 // better than guessing the contents, the table will match a
6053 // previously loaded spell file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006054 if (!spin.si_add)
6055 spin.si_clear_chartab = TRUE;
6056
6057 /*
6058 * Read all the .aff and .dic files.
6059 * Text is converted to 'encoding'.
6060 * Words are stored in the case-folded and keep-case trees.
6061 */
6062 for (i = 0; i < incount && !error; ++i)
6063 {
6064 spin.si_conv.vc_type = CONV_NONE;
6065 spin.si_region = 1 << i;
6066
6067 vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]);
6068 if (mch_stat((char *)fname, &st) >= 0)
6069 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006070 // Read the .aff file. Will init "spin->si_conv" based on the
6071 // "SET" line.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006072 afile[i] = spell_read_aff(&spin, fname);
6073 if (afile[i] == NULL)
6074 error = TRUE;
6075 else
6076 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006077 // Read the .dic file and store the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006078 vim_snprintf((char *)fname, MAXPATHL, "%s.dic",
6079 innames[i]);
6080 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
6081 error = TRUE;
6082 }
6083 }
6084 else
6085 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006086 // No .aff file, try reading the file as a word list. Store
6087 // the words in the trees.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006088 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
6089 error = TRUE;
6090 }
6091
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006092 // Free any conversion stuff.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006093 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006094 }
6095
6096 if (spin.si_compflags != NULL && spin.si_nobreak)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006097 msg(_("Warning: both compounding and NOBREAK specified"));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006098
6099 if (!error && !got_int)
6100 {
6101 /*
6102 * Combine tails in the tree.
6103 */
6104 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar408c23b2020-06-03 22:15:45 +02006105 wordtree_compress(&spin, spin.si_foldroot, "case-folded");
6106 wordtree_compress(&spin, spin.si_keeproot, "keep-case");
6107 wordtree_compress(&spin, spin.si_prefroot, "prefixes");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006108 }
6109
6110 if (!error && !got_int)
6111 {
6112 /*
6113 * Write the info in the spell file.
6114 */
6115 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaarc1669272018-06-19 14:23:53 +02006116 _("Writing spell file %s..."), wfname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006117 spell_message(&spin, IObuff);
6118
6119 error = write_vim_spell(&spin, wfname) == FAIL;
6120
6121 spell_message(&spin, (char_u *)_("Done!"));
6122 vim_snprintf((char *)IObuff, IOSIZE,
6123 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
6124 spell_message(&spin, IObuff);
6125
6126 /*
6127 * If the file is loaded need to reload it.
6128 */
6129 if (!error)
6130 spell_reload_one(wfname, added_word);
6131 }
6132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006133 // Free the allocated memory.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006134 ga_clear(&spin.si_rep);
6135 ga_clear(&spin.si_repsal);
6136 ga_clear(&spin.si_sal);
6137 ga_clear(&spin.si_map);
6138 ga_clear(&spin.si_comppat);
6139 ga_clear(&spin.si_prefcond);
6140 hash_clear_all(&spin.si_commonwords, 0);
6141
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006142 // Free the .aff file structures.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006143 for (i = 0; i < incount; ++i)
6144 if (afile[i] != NULL)
6145 spell_free_aff(afile[i]);
6146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006147 // Free all the bits and pieces at once.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006148 free_blocks(spin.si_blocks);
6149
6150 /*
6151 * If there is soundfolding info and no NOSUGFILE item create the
6152 * .sug file with the soundfolded word trie.
6153 */
6154 if (spin.si_sugtime != 0 && !error && !got_int)
6155 spell_make_sugfile(&spin, wfname);
6156
6157 }
6158
6159theend:
6160 vim_free(fname);
6161 vim_free(wfname);
6162}
6163
6164/*
6165 * Display a message for spell file processing when 'verbose' is set or using
6166 * ":mkspell". "str" can be IObuff.
6167 */
6168 static void
6169spell_message(spellinfo_T *spin, char_u *str)
6170{
6171 if (spin->si_verbose || p_verbose > 2)
6172 {
6173 if (!spin->si_verbose)
6174 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006175 msg((char *)str);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006176 out_flush();
6177 if (!spin->si_verbose)
6178 verbose_leave();
6179 }
6180}
6181
6182/*
6183 * ":[count]spellgood {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006184 * ":[count]spellwrong {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006185 * ":[count]spellundo {word}"
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006186 * ":[count]spellrare {word}"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006187 */
6188 void
6189ex_spell(exarg_T *eap)
6190{
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006191 spell_add_word(eap->arg, (int)STRLEN(eap->arg),
6192 eap->cmdidx == CMD_spellwrong ? SPELL_ADD_BAD :
6193 eap->cmdidx == CMD_spellrare ? SPELL_ADD_RARE : SPELL_ADD_GOOD,
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006194 eap->forceit ? 0 : (int)eap->line2,
6195 eap->cmdidx == CMD_spellundo);
6196}
6197
6198/*
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006199 * Add "word[len]" to 'spellfile' as a good, rare or bad word.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006200 */
6201 void
6202spell_add_word(
6203 char_u *word,
6204 int len,
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006205 int what, // SPELL_ADD_ values
6206 int idx, // "zG" and "zW": zero, otherwise index in
6207 // 'spellfile'
6208 int undo) // TRUE for "zug", "zuG", "zuw" and "zuW"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006209{
6210 FILE *fd = NULL;
6211 buf_T *buf = NULL;
6212 int new_spf = FALSE;
6213 char_u *fname;
6214 char_u *fnamebuf = NULL;
6215 char_u line[MAXWLEN * 2];
6216 long fpos, fpos_next = 0;
6217 int i;
6218 char_u *spf;
6219
K.Takata2ebcc352022-07-14 17:25:14 +01006220 if (!valid_spell_word(word, word + len))
Bram Moolenaar7c824682022-05-08 22:32:58 +01006221 {
6222 emsg(_(e_illegal_character_in_word));
6223 return;
6224 }
6225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006226 if (idx == 0) // use internal wordlist
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006227 {
6228 if (int_wordlist == NULL)
6229 {
6230 int_wordlist = vim_tempname('s', FALSE);
6231 if (int_wordlist == NULL)
6232 return;
6233 }
6234 fname = int_wordlist;
6235 }
6236 else
6237 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006238 // If 'spellfile' isn't set figure out a good default value.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006239 if (*curwin->w_s->b_p_spf == NUL)
6240 {
6241 init_spellfile();
6242 new_spf = TRUE;
6243 }
6244
6245 if (*curwin->w_s->b_p_spf == NUL)
6246 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00006247 semsg(_(e_option_str_is_not_set), "spellfile");
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006248 return;
6249 }
6250 fnamebuf = alloc(MAXPATHL);
6251 if (fnamebuf == NULL)
6252 return;
6253
6254 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
6255 {
6256 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6257 if (i == idx)
6258 break;
6259 if (*spf == NUL)
6260 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006261 semsg(_(e_spellfile_does_not_have_nr_entries), idx);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006262 vim_free(fnamebuf);
6263 return;
6264 }
6265 }
6266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267 // Check that the user isn't editing the .add file somewhere.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006268 buf = buflist_findname_exp(fnamebuf);
6269 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6270 buf = NULL;
6271 if (buf != NULL && bufIsChanged(buf))
6272 {
Bram Moolenaareb822a22021-12-31 15:09:27 +00006273 emsg(_(e_file_is_loaded_in_another_buffer));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006274 vim_free(fnamebuf);
6275 return;
6276 }
6277
6278 fname = fnamebuf;
6279 }
6280
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006281 if (what == SPELL_ADD_BAD || undo)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006282 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006283 // When the word appears as good word we need to remove that one,
6284 // since its flags sort before the one with WF_BANNED.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006285 fd = mch_fopen((char *)fname, "r");
6286 if (fd != NULL)
6287 {
6288 while (!vim_fgets(line, MAXWLEN * 2, fd))
6289 {
6290 fpos = fpos_next;
6291 fpos_next = ftell(fd);
Bram Moolenaar416b5f42022-02-25 21:47:48 +00006292 if (fpos_next < 0)
6293 break; // should never happen
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006294 if (STRNCMP(word, line, len) == 0
6295 && (line[len] == '/' || line[len] < ' '))
6296 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006297 // Found duplicate word. Remove it by writing a '#' at
6298 // the start of the line. Mixing reading and writing
6299 // doesn't work for all systems, close the file first.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006300 fclose(fd);
6301 fd = mch_fopen((char *)fname, "r+");
6302 if (fd == NULL)
6303 break;
6304 if (fseek(fd, fpos, SEEK_SET) == 0)
6305 {
6306 fputc('#', fd);
6307 if (undo)
6308 {
6309 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006310 smsg(_("Word '%.*s' removed from %s"),
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006311 len, word, NameBuff);
6312 }
6313 }
Bram Moolenaar2c363a22021-02-03 20:14:23 +01006314 if (fseek(fd, fpos_next, SEEK_SET) != 0)
6315 {
6316 PERROR(_("Seek error in spellfile"));
6317 break;
6318 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006319 }
6320 }
6321 if (fd != NULL)
6322 fclose(fd);
6323 }
6324 }
6325
6326 if (!undo)
6327 {
6328 fd = mch_fopen((char *)fname, "a");
6329 if (fd == NULL && new_spf)
6330 {
6331 char_u *p;
6332
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006333 // We just initialized the 'spellfile' option and can't open the
6334 // file. We may need to create the "spell" directory first. We
6335 // already checked the runtime directory is writable in
6336 // init_spellfile().
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006337 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
6338 {
6339 int c = *p;
6340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006341 // The directory doesn't exist. Try creating it and opening
6342 // the file again.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006343 *p = NUL;
6344 vim_mkdir(fname, 0755);
6345 *p = c;
6346 fd = mch_fopen((char *)fname, "a");
6347 }
6348 }
6349
6350 if (fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006351 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006352 else
6353 {
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006354 if (what == SPELL_ADD_BAD)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006355 fprintf(fd, "%.*s/!\n", len, word);
Bram Moolenaar08cc3742019-08-11 22:51:14 +02006356 else if (what == SPELL_ADD_RARE)
6357 fprintf(fd, "%.*s/?\n", len, word);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006358 else
6359 fprintf(fd, "%.*s\n", len, word);
6360 fclose(fd);
6361
6362 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006363 smsg(_("Word '%.*s' added to %s"), len, word, NameBuff);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006364 }
6365 }
6366
6367 if (fd != NULL)
6368 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006369 // Update the .add.spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006370 mkspell(1, &fname, FALSE, TRUE, TRUE);
6371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006372 // If the .add file is edited somewhere, reload it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006373 if (buf != NULL)
Rob Pilling8196e942022-02-11 15:12:10 +00006374 buf_reload(buf, buf->b_orig_mode, FALSE);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006375
Bram Moolenaara4d158b2022-08-14 14:17:45 +01006376 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006377 }
6378 vim_free(fnamebuf);
6379}
6380
6381/*
6382 * Initialize 'spellfile' for the current buffer.
6383 */
6384 static void
6385init_spellfile(void)
6386{
6387 char_u *buf;
6388 int l;
6389 char_u *fname;
6390 char_u *rtp;
6391 char_u *lend;
6392 int aspath = FALSE;
6393 char_u *lstart = curbuf->b_s.b_p_spl;
6394
6395 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
6396 {
6397 buf = alloc(MAXPATHL);
6398 if (buf == NULL)
6399 return;
6400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006401 // Find the end of the language name. Exclude the region. If there
6402 // is a path separator remember the start of the tail.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006403 for (lend = curwin->w_s->b_p_spl; *lend != NUL
6404 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
6405 if (vim_ispathsep(*lend))
6406 {
6407 aspath = TRUE;
6408 lstart = lend + 1;
6409 }
6410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006411 // Loop over all entries in 'runtimepath'. Use the first one where we
6412 // are allowed to write.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006413 rtp = p_rtp;
6414 while (*rtp != NUL)
6415 {
6416 if (aspath)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006417 // Use directory of an entry with path, e.g., for
6418 // "/dir/lg.utf-8.spl" use "/dir".
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006419 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6420 lstart - curbuf->b_s.b_p_spl - 1);
6421 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006422 // Copy the path from 'runtimepath' to buf[].
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006423 copy_option_part(&rtp, buf, MAXPATHL, ",");
6424 if (filewritable(buf) == 2)
6425 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006426 // Use the first language name from 'spelllang' and the
6427 // encoding used in the first loaded .spl file.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006428 if (aspath)
6429 vim_strncpy(buf, curbuf->b_s.b_p_spl,
6430 lend - curbuf->b_s.b_p_spl);
6431 else
6432 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006433 // Create the "spell" directory if it doesn't exist yet.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006434 l = (int)STRLEN(buf);
6435 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
6436 if (filewritable(buf) != 2)
6437 vim_mkdir(buf, 0755);
6438
6439 l = (int)STRLEN(buf);
6440 vim_snprintf((char *)buf + l, MAXPATHL - l,
6441 "/%.*s", (int)(lend - lstart), lstart);
6442 }
6443 l = (int)STRLEN(buf);
6444 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)
6445 ->lp_slang->sl_fname;
6446 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
6447 fname != NULL
6448 && strstr((char *)gettail(fname), ".ascii.") != NULL
6449 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaar31e5c602022-04-15 13:53:33 +01006450 set_option_value_give_err((char_u *)"spellfile",
6451 0L, buf, OPT_LOCAL);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006452 break;
6453 }
6454 aspath = FALSE;
6455 }
6456
6457 vim_free(buf);
6458 }
6459}
6460
6461
6462
6463/*
6464 * Set the spell character tables from strings in the affix file.
6465 */
6466 static int
6467set_spell_chartab(char_u *fol, char_u *low, char_u *upp)
6468{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006469 // We build the new tables here first, so that we can compare with the
6470 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006471 spelltab_T new_st;
6472 char_u *pf = fol, *pl = low, *pu = upp;
6473 int f, l, u;
6474
6475 clear_spell_chartab(&new_st);
6476
6477 while (*pf != NUL)
6478 {
6479 if (*pl == NUL || *pu == NUL)
6480 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006481 emsg(_(e_format_error_in_affix_file_fol_low_or_upp));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006482 return FAIL;
6483 }
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006484 f = mb_ptr2char_adv(&pf);
6485 l = mb_ptr2char_adv(&pl);
6486 u = mb_ptr2char_adv(&pu);
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006487
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006488 // Every character that appears is a word character.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006489 if (f < 256)
6490 new_st.st_isw[f] = TRUE;
6491 if (l < 256)
6492 new_st.st_isw[l] = TRUE;
6493 if (u < 256)
6494 new_st.st_isw[u] = TRUE;
6495
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006496 // if "LOW" and "FOL" are not the same the "LOW" char needs
6497 // case-folding
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006498 if (l < 256 && l != f)
6499 {
6500 if (f >= 256)
6501 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006502 emsg(_(e_character_in_fol_low_or_upp_is_out_of_range));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006503 return FAIL;
6504 }
6505 new_st.st_fold[l] = f;
6506 }
6507
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006508 // if "UPP" and "FOL" are not the same the "UPP" char needs
6509 // case-folding, it's upper case and the "UPP" is the upper case of
6510 // "FOL" .
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006511 if (u < 256 && u != f)
6512 {
6513 if (f >= 256)
6514 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006515 emsg(_(e_character_in_fol_low_or_upp_is_out_of_range));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006516 return FAIL;
6517 }
6518 new_st.st_fold[u] = f;
6519 new_st.st_isu[u] = TRUE;
6520 new_st.st_upper[f] = u;
6521 }
6522 }
6523
6524 if (*pl != NUL || *pu != NUL)
6525 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006526 emsg(_(e_format_error_in_affix_file_fol_low_or_upp));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006527 return FAIL;
6528 }
6529
6530 return set_spell_finish(&new_st);
6531}
6532
6533/*
6534 * Set the spell character tables from strings in the .spl file.
6535 */
6536 static void
6537set_spell_charflags(
6538 char_u *flags,
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006539 int cnt, // length of "flags"
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006540 char_u *fol)
6541{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006542 // We build the new tables here first, so that we can compare with the
6543 // previous one.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006544 spelltab_T new_st;
6545 int i;
6546 char_u *p = fol;
6547 int c;
6548
6549 clear_spell_chartab(&new_st);
6550
6551 for (i = 0; i < 128; ++i)
6552 {
6553 if (i < cnt)
6554 {
6555 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6556 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6557 }
6558
6559 if (*p != NUL)
6560 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006561 c = mb_ptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006562 new_st.st_fold[i + 128] = c;
6563 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6564 new_st.st_upper[c] = i + 128;
6565 }
6566 }
6567
6568 (void)set_spell_finish(&new_st);
6569}
6570
6571 static int
6572set_spell_finish(spelltab_T *new_st)
6573{
6574 int i;
6575
6576 if (did_set_spelltab)
6577 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006578 // check that it's the same table
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006579 for (i = 0; i < 256; ++i)
6580 {
6581 if (spelltab.st_isw[i] != new_st->st_isw[i]
6582 || spelltab.st_isu[i] != new_st->st_isu[i]
6583 || spelltab.st_fold[i] != new_st->st_fold[i]
6584 || spelltab.st_upper[i] != new_st->st_upper[i])
6585 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00006586 emsg(_(e_word_characters_differ_between_spell_files));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006587 return FAIL;
6588 }
6589 }
6590 }
6591 else
6592 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006593 // copy the new spelltab into the one being used
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006594 spelltab = *new_st;
6595 did_set_spelltab = TRUE;
6596 }
6597
6598 return OK;
6599}
6600
6601/*
6602 * Write the table with prefix conditions to the .spl file.
=?UTF-8?q?Bj=C3=B6rn=20Linse?=1daedc82021-12-10 20:39:17 +00006603 * When "fd" is NULL only count the length of what is written and return it.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006604 */
6605 static int
=?UTF-8?q?Bj=C3=B6rn=20Linse?=1daedc82021-12-10 20:39:17 +00006606write_spell_prefcond(FILE *fd, garray_T *gap, size_t *fwv)
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006607{
6608 int i;
6609 char_u *p;
6610 int len;
6611 int totlen;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006612
6613 if (fd != NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006614 put_bytes(fd, (long_u)gap->ga_len, 2); // <prefcondcnt>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006615
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006616 totlen = 2 + gap->ga_len; // length of <prefcondcnt> and <condlen> bytes
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006617
6618 for (i = 0; i < gap->ga_len; ++i)
6619 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006620 // <prefcond> : <condlen> <condstr>
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006621 p = ((char_u **)gap->ga_data)[i];
6622 if (p != NULL)
6623 {
6624 len = (int)STRLEN(p);
6625 if (fd != NULL)
6626 {
6627 fputc(len, fd);
=?UTF-8?q?Bj=C3=B6rn=20Linse?=1daedc82021-12-10 20:39:17 +00006628 *fwv &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006629 }
6630 totlen += len;
6631 }
6632 else if (fd != NULL)
6633 fputc(0, fd);
6634 }
6635
6636 return totlen;
6637}
6638
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006639/*
6640 * Use map string "map" for languages "lp".
6641 */
6642 static void
6643set_map_str(slang_T *lp, char_u *map)
6644{
6645 char_u *p;
6646 int headc = 0;
6647 int c;
6648 int i;
6649
6650 if (*map == NUL)
6651 {
6652 lp->sl_has_map = FALSE;
6653 return;
6654 }
6655 lp->sl_has_map = TRUE;
6656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006657 // Init the array and hash tables empty.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006658 for (i = 0; i < 256; ++i)
6659 lp->sl_map_array[i] = 0;
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006660 hash_init(&lp->sl_map_hash);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006661
6662 /*
6663 * The similar characters are stored separated with slashes:
6664 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
6665 * before the same slash. For characters above 255 sl_map_hash is used.
6666 */
6667 for (p = map; *p != NUL; )
6668 {
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006669 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006670 if (c == '/')
6671 headc = 0;
6672 else
6673 {
6674 if (headc == 0)
6675 headc = c;
6676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006677 // Characters above 255 don't fit in sl_map_array[], put them in
6678 // the hash table. Each entry is the char, a NUL the headchar and
6679 // a NUL.
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006680 if (c >= 256)
6681 {
6682 int cl = mb_char2len(c);
6683 int headcl = mb_char2len(headc);
6684 char_u *b;
6685 hash_T hash;
6686 hashitem_T *hi;
6687
Bram Moolenaar964b3742019-05-24 18:54:09 +02006688 b = alloc(cl + headcl + 2);
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006689 if (b == NULL)
6690 return;
6691 mb_char2bytes(c, b);
6692 b[cl] = NUL;
6693 mb_char2bytes(headc, b + cl + 1);
6694 b[cl + 1 + headcl] = NUL;
6695 hash = hash_hash(b);
6696 hi = hash_lookup(&lp->sl_map_hash, b, hash);
6697 if (HASHITEM_EMPTY(hi))
6698 hash_add_item(&lp->sl_map_hash, hi, b, hash);
6699 else
6700 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006701 // This should have been checked when generating the .spl
6702 // file.
Bram Moolenaar677658a2022-01-05 16:09:06 +00006703 emsg(_(e_duplicate_char_in_map_entry));
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006704 vim_free(b);
6705 }
6706 }
6707 else
Bram Moolenaar9ccfebd2016-07-19 16:39:08 +02006708 lp->sl_map_array[c] = headc;
6709 }
6710 }
6711}
6712
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006713#endif // FEAT_SPELL