blob: 30b08ae982b645555a2c2751ff7b53c99ab11488 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000053/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000054 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000055#if 0
56# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000057#endif
58
Bram Moolenaar51485f02005-06-04 21:55:20 +000059/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000060 * Use this to adjust the score after finding suggestions, based on the
61 * suggested word sounding like the bad word. This is much faster than doing
62 * it for every possible suggestion.
63 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
64 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000065 * Used when 'spellsuggest' is set to "best".
66 */
67#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
68
69/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000070 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000071 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000072 * <LWORDTREE>
73 * <KWORDTREE>
74 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000075 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000076 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000077 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000078 * <fileID> 8 bytes "VIMspell"
79 * <versionnr> 1 byte VIMSPELLVERSION
80 *
81 *
82 * Sections make it possible to add information to the .spl file without
83 * making it incompatible with previous versions. There are two kinds of
84 * sections:
85 * 1. Not essential for correct spell checking. E.g. for making suggestions.
86 * These are skipped when not supported.
87 * 2. Optional information, but essential for spell checking when present.
88 * E.g. conditions for affixes. When this section is present but not
89 * supported an error message is given.
90 *
91 * <SECTIONS>: <section> ... <sectionend>
92 *
93 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
94 *
95 * <sectionID> 1 byte number from 0 to 254 identifying the section
96 *
97 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
98 * spell checking
99 *
100 * <sectionlen> 4 bytes length of section contents, MSB first
101 *
102 * <sectionend> 1 byte SN_END
103 *
104 *
105 * sectionID == SN_REGION: <regionname> ...
106 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000107 * First <regionname> is region 1.
108 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000109 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
110 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000111 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
112 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000113 * 0x01 word character CF_WORD
114 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000115 * <folcharslen> 2 bytes Number of bytes in <folchars>.
116 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000117 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000118 * sectionID == SN_MIDWORD: <midword>
119 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000120 * in the middle of a word.
121 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000122 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000123 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000124 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000125 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000126 * <condstr> N bytes Condition for the prefix.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_REP: <repcount> <rep> ...
129 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000130 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000131 * <repfromlen> 1 byte length of <repfrom>
132 * <repfrom> N bytes "from" part of replacement
133 * <reptolen> 1 byte length of <repto>
134 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000135 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000136 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
137 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000138 * SAL_F0LLOWUP
139 * SAL_COLLAPSE
140 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000142 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000143 * <salfromlen> 1 byte length of <salfrom>
144 * <salfrom> N bytes "from" part of soundsalike
145 * <saltolen> 1 byte length of <salto>
146 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000147 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000148 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
149 * <sofofromlen> 2 bytes length of <sofofrom>
150 * <sofofrom> N bytes "from" part of soundfold
151 * <sofotolen> 2 bytes length of <sofoto>
152 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000153 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000154 * sectionID == SN_MAP: <mapstr>
155 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000156 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
159 * <compmax> 1 byte Maximum nr of words in compound word.
160 * <compminlen> 1 byte Minimal word length for compounding.
161 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
162 * <compflags> N bytes Flags from COMPOUNDFLAGS items, separated by
163 * slashes.
164 *
165 * sectionID == SN_SYLLABLE: <syllable>
166 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000167 *
168 * <LWORDTREE>: <wordtree>
169 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000170 * <KWORDTREE>: <wordtree>
171 *
172 * <PREFIXTREE>: <wordtree>
173 *
174 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000175 * <wordtree>: <nodecount> <nodedata> ...
176 *
177 * <nodecount> 4 bytes Number of nodes following. MSB first.
178 *
179 * <nodedata>: <siblingcount> <sibling> ...
180 *
181 * <siblingcount> 1 byte Number of siblings in this node. The siblings
182 * follow in sorted order.
183 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000184 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000185 * | <flags> [<flags2>] [<region>] [<affixID>]
186 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
188 * <byte> 1 byte Byte value of the sibling. Special cases:
189 * BY_NOFLAGS: End of word without flags and for all
190 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000191 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000192 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000193 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000194 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000195 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000196 * BY_FLAGS2: End of word, <flags> and <flags2>
197 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000198 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000199 * and <xbyte> follow.
200 *
201 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
202 *
203 * <xbyte> 1 byte byte value of the sibling.
204 *
205 * <flags> 1 byte bitmask of:
206 * WF_ALLCAP word must have only capitals
207 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000208 * WF_KEEPCAP keep-case word
209 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000210 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000211 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000213 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000215 * <flags2> 1 byte Only used when there are postponed prefixes.
216 * Bitmask of:
217 * WF_HAS_AFF >> 8 word includes affix
218 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000219 * <pflags> 1 byte bitmask of:
220 * WFP_RARE rare prefix
221 * WFP_NC non-combining prefix
222 * WFP_UP letter after prefix made upper case
223 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000224 * <region> 1 byte Bitmask for regions in which word is valid. When
225 * omitted it's valid in all regions.
226 * Lowest bit is for region 1.
227 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000228 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000229 * PREFIXTREE used for the required prefix ID.
230 *
231 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
232 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000233 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000234 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235 */
236
Bram Moolenaare19defe2005-03-21 08:23:33 +0000237#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
238# include <io.h> /* for lseek(), must be before vim.h */
239#endif
240
241#include "vim.h"
242
243#if defined(FEAT_SYN_HL) || defined(PROTO)
244
245#ifdef HAVE_FCNTL_H
246# include <fcntl.h>
247#endif
248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000249#define MAXWLEN 250 /* Assume max. word len is this many bytes.
250 Some places assume a word length fits in a
251 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000252
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000253/* Type used for indexes in the word tree need to be at least 3 bytes. If int
254 * is 8 bytes we could use something smaller, but what? */
255#if SIZEOF_INT > 2
256typedef int idx_T;
257#else
258typedef long idx_T;
259#endif
260
261/* Flags used for a word. Only the lowest byte can be used, the region byte
262 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000263#define WF_REGION 0x01 /* region byte follows */
264#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
265#define WF_ALLCAP 0x04 /* word must be all capitals */
266#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000267#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000268#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000269#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000270#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000271
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000272/* for <flags2>, shifted up one byte to be used in wn_flags */
273#define WF_HAS_AFF 0x0100 /* word includes affix */
274
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000275#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276
Bram Moolenaar53805d12005-08-01 07:08:33 +0000277/* flags for <pflags> */
278#define WFP_RARE 0x01 /* rare prefix */
279#define WFP_NC 0x02 /* prefix is not combining */
280#define WFP_UP 0x04 /* to-upper prefix */
281
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000282/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000283 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000284#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
285 * postponed prefix */
286#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
287 * postponed prefix */
288#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
289 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000290
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000291/* Special byte values for <byte>. Some are only used in the tree for
292 * postponed prefixes, some only in the other trees. This is a bit messy... */
293#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000294 * postponed prefix: no <pflags> */
295#define BY_INDEX 1 /* child is shared, index follows */
296#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
297 * postponed prefix: <pflags> follows */
298#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
299 * follow; never used in prefix tree */
300#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000302/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000303 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304 * One replacement: from "ft_from" to "ft_to". */
305typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000306{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000307 char_u *ft_from;
308 char_u *ft_to;
309} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000310
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000311/* Info from "SAL" entries in ".aff" file used in sl_sal.
312 * The info is split for quick processing by spell_soundfold().
313 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
314typedef struct salitem_S
315{
316 char_u *sm_lead; /* leading letters */
317 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000318 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000319 char_u *sm_rules; /* rules like ^, $, priority */
320 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000321#ifdef FEAT_MBYTE
322 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000323 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000324 int *sm_to_w; /* wide character copy of "sm_to" */
325#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000326} salitem_T;
327
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000328#ifdef FEAT_MBYTE
329typedef int salfirst_T;
330#else
331typedef short salfirst_T;
332#endif
333
Bram Moolenaar5195e452005-08-19 20:32:47 +0000334/* Values for SP_*ERROR are negative, positive values are used by
335 * read_cnt_string(). */
336#define SP_TRUNCERROR -1 /* spell file truncated error */
337#define SP_FORMERROR -2 /* format error in spell file */
338#define SP_ERROR -3 /* other error while reading spell file */
339
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000340/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000341 * Structure used to store words and other info for one language, loaded from
342 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000343 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
344 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
345 *
346 * The "byts" array stores the possible bytes in each tree node, preceded by
347 * the number of possible bytes, sorted on byte value:
348 * <len> <byte1> <byte2> ...
349 * The "idxs" array stores the index of the child node corresponding to the
350 * byte in "byts".
351 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000352 * the flags, region mask and affixID for the word. There may be several
353 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000354 */
355typedef struct slang_S slang_T;
356struct slang_S
357{
358 slang_T *sl_next; /* next language */
359 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000360 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000361 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000362
Bram Moolenaar51485f02005-06-04 21:55:20 +0000363 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000364 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000365 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000366 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000367 char_u *sl_pbyts; /* prefix tree word bytes */
368 idx_T *sl_pidxs; /* prefix tree word indexes */
369
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000370 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000371
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000372 char_u *sl_midword; /* MIDWORD string or NULL */
373
Bram Moolenaar5195e452005-08-19 20:32:47 +0000374 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
375 int sl_compminlen; /* COMPOUNDMIN (default: MAXWLEN) */
376 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
377 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
378 * (NULL when no compounding) */
379 char_u *sl_compstartflags; /* flags for first compound word */
380 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
381 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000382
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000383 int sl_prefixcnt; /* number of items in "sl_prefprog" */
384 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
385
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000386 garray_T sl_rep; /* list of fromto_T entries from REP lines */
387 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
388 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000389 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000390 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000392 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
393 * "sl_sal_first" maps chars, when has_mbyte
394 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 int sl_followup; /* SAL followup */
396 int sl_collapse; /* SAL collapse_result */
397 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000398 int sl_has_map; /* TRUE if there is a MAP line */
399#ifdef FEAT_MBYTE
400 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
401 int sl_map_array[256]; /* MAP for first 256 chars */
402#else
403 char_u sl_map_array[256]; /* MAP for first 256 chars */
404#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000405};
406
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000407/* First language that is loaded, start of the linked list of loaded
408 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000409static slang_T *first_lang = NULL;
410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000411/* Flags used in .spl file for soundsalike flags. */
412#define SAL_F0LLOWUP 1
413#define SAL_COLLAPSE 2
414#define SAL_REM_ACCENTS 4
415
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000416/*
417 * Structure used in "b_langp", filled from 'spelllang'.
418 */
419typedef struct langp_S
420{
421 slang_T *lp_slang; /* info for this language (NULL for last one) */
422 int lp_region; /* bitmask for region or REGION_ALL */
423} langp_T;
424
425#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
426
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000427#define REGION_ALL 0xff /* word valid in all regions */
428
Bram Moolenaar5195e452005-08-19 20:32:47 +0000429#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
430#define VIMSPELLMAGICL 8
431#define VIMSPELLVERSION 50
432
433/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
434#define SN_REGION 0 /* <regionname> section */
435#define SN_CHARFLAGS 1 /* charflags section */
436#define SN_MIDWORD 2 /* <midword> section */
437#define SN_PREFCOND 3 /* <prefcond> section */
438#define SN_REP 4 /* REP items section */
439#define SN_SAL 5 /* SAL items section */
440#define SN_SOFO 6 /* soundfolding section */
441#define SN_MAP 7 /* MAP items section */
442#define SN_COMPOUND 8 /* compound words section */
443#define SN_SYLLABLE 9 /* syllable section */
444#define SN_END 255 /* end of sections */
445
446#define SNF_REQUIRED 1 /* <sectionflags>: required section */
447
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000448/* Result values. Lower number is accepted over higher one. */
449#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000450#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000451#define SP_RARE 1
452#define SP_LOCAL 2
453#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000454
Bram Moolenaar7887d882005-07-01 22:33:52 +0000455/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000456static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000457
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000458/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000459 * Information used when looking for suggestions.
460 */
461typedef struct suginfo_S
462{
463 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000464 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000465 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000466 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000467 char_u *su_badptr; /* start of bad word in line */
468 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000469 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000470 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
471 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
472 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473} suginfo_T;
474
475/* One word suggestion. Used in "si_ga". */
476typedef struct suggest_S
477{
478 char_u *st_word; /* suggested word, allocated string */
479 int st_orglen; /* length of replaced text */
480 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000481 int st_altscore; /* used when st_score compares equal */
482 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000483 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000484} suggest_T;
485
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000486#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000488/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
489 * called the score may change, thus we need to keep more than what is
490 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000491#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000492
493/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
494 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000495#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000496
497/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000498#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000499#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000500#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000501#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502#define SCORE_SWAP 90 /* swap two characters */
503#define SCORE_SWAP3 110 /* swap two characters in three */
504#define SCORE_REP 87 /* REP replacement */
505#define SCORE_SUBST 93 /* substitute a character */
506#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000507#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000508#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000509#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000510#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000511#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000512#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000513#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000514#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000516#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000517#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
518 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000519
520#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000521#define SCORE_MAXMAX 999999 /* accept any score */
522
523/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000524 * Structure to store info for word matching.
525 */
526typedef struct matchinf_S
527{
528 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000529
530 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000531 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000532 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000533 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000534 char_u *mi_cend; /* char after what was used for
535 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000536
537 /* case-folded text */
538 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000539 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000540
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000541 /* for when checking word after a prefix */
542 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000543 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000544 int mi_prefcnt; /* number of entries at mi_prefarridx */
545 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000546#ifdef FEAT_MBYTE
547 int mi_cprefixlen; /* byte length of prefix in original
548 case */
549#else
550# define mi_cprefixlen mi_prefixlen /* it's the same value */
551#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000552
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000553 /* for when checking a compound word */
554 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000555 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
556 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000557
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000558 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000559 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000560 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000561 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000562} matchinf_T;
563
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000564/*
565 * The tables used for recognizing word characters according to spelling.
566 * These are only used for the first 256 characters of 'encoding'.
567 */
568typedef struct spelltab_S
569{
570 char_u st_isw[256]; /* flags: is word char */
571 char_u st_isu[256]; /* flags: is uppercase char */
572 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000573 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000574} spelltab_T;
575
576static spelltab_T spelltab;
577static int did_set_spelltab;
578
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000579#define CF_WORD 0x01
580#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000581
582static void clear_spell_chartab __ARGS((spelltab_T *sp));
583static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000584static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
585static int spell_iswordp_nmw __ARGS((char_u *p));
586#ifdef FEAT_MBYTE
587static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
588#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000589static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590
591/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000592 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000593 */
594typedef enum
595{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000596 STATE_START = 0, /* At start of node check for NUL bytes (goodword
597 * ends); if badword ends there is a match, otherwise
598 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000599 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000601 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
602 STATE_PLAIN, /* Use each byte of the node. */
603 STATE_DEL, /* Delete a byte from the bad word. */
604 STATE_INS, /* Insert a byte in the bad word. */
605 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 STATE_UNSWAP, /* Undo swap two characters. */
607 STATE_SWAP3, /* Swap two characters over three. */
608 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000609 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000611 STATE_REP_INI, /* Prepare for using REP items. */
612 STATE_REP, /* Use matching REP items from the .aff file. */
613 STATE_REP_UNDO, /* Undo a REP item replacement. */
614 STATE_FINAL /* End of this node. */
615} state_T;
616
617/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000618 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619 */
620typedef struct trystate_S
621{
Bram Moolenaarea424162005-06-16 21:51:00 +0000622 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000623 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000625 short ts_curi; /* index in list of child nodes */
626 char_u ts_fidx; /* index in fword[], case-folded bad word */
627 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
628 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000629 char_u ts_prefixdepth; /* stack depth for end of prefix or
630 * PFD_PREFIXTREE or PFD_NOPREFIX or
631 * PFD_COMPOUND */
Bram Moolenaarea424162005-06-16 21:51:00 +0000632#ifdef FEAT_MBYTE
633 char_u ts_tcharlen; /* number of bytes in tword character */
634 char_u ts_tcharidx; /* current byte index in tword character */
635 char_u ts_isdiff; /* DIFF_ values */
636 char_u ts_fcharstart; /* index in fword where badword char started */
637#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000638 char_u ts_prewordlen; /* length of word in "preword[]" */
639 char_u ts_splitoff; /* index in "tword" after last split */
640 char_u ts_complen; /* nr of compound words used */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000641 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642} trystate_T;
643
Bram Moolenaarea424162005-06-16 21:51:00 +0000644/* values for ts_isdiff */
645#define DIFF_NONE 0 /* no different byte (yet) */
646#define DIFF_YES 1 /* different byte found */
647#define DIFF_INSERT 2 /* inserting character */
648
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000649/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000650#define PFD_COMPOUND 0xfd /* prefixed is a compound word */
651#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
652#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000653
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000654/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000655#define FIND_FOLDWORD 0 /* find word case-folded */
656#define FIND_KEEPWORD 1 /* find keep-case word */
657#define FIND_PREFIX 2 /* find word after prefix */
658#define FIND_COMPOUND 3 /* find case-folded compound word */
659#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000660
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000661static slang_T *slang_alloc __ARGS((char_u *lang));
662static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000663static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000664static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000665static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000666static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000667static void find_prefix __ARGS((matchinf_T *mip));
668static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000669static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000670static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000671static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000672static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000673static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000674static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000675static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000676static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000677static char_u *read_string __ARGS((FILE *fd, int cnt));
678static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
679static int read_charflags_section __ARGS((FILE *fd));
680static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
681static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
682static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
683static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
684static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
685static int init_syl_tab __ARGS((slang_T *slang));
686static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000687static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
688static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000689#ifdef FEAT_MBYTE
690static int *mb_str2wide __ARGS((char_u *s));
691#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000692static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000693static void clear_midword __ARGS((buf_T *buf));
694static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695static int find_region __ARGS((char_u *rp, char_u *region));
696static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000697static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000698static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000699static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000700static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000701static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000702static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000703static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000704#ifdef FEAT_EVAL
705static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
706#endif
707static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
708static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000709static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000710static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000711static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000712static void suggest_try_special __ARGS((suginfo_T *su));
713static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000714static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000715#ifdef FEAT_MBYTE
716static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
717#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000718static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000719static void score_comp_sal __ARGS((suginfo_T *su));
720static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000721static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000722static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000723static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000724static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000725static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000726static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000727static void add_banned __ARGS((suginfo_T *su, char_u *word));
728static int was_banned __ARGS((suginfo_T *su, char_u *word));
729static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000730static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000731static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000732static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
733static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
734static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000735#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000736static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000737#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000738static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000739static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000740static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
741static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000742
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000743/*
744 * Use our own character-case definitions, because the current locale may
745 * differ from what the .spl file uses.
746 * These must not be called with negative number!
747 */
748#ifndef FEAT_MBYTE
749/* Non-multi-byte implementation. */
750# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
751# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
752# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
753#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000754# if defined(HAVE_WCHAR_H)
755# include <wchar.h> /* for towupper() and towlower() */
756# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000757/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
758 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
759 * the "w" library function for characters above 255 if available. */
760# ifdef HAVE_TOWLOWER
761# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
762 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
763# else
764# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
765 : (c) < 256 ? spelltab.st_fold[c] : (c))
766# endif
767
768# ifdef HAVE_TOWUPPER
769# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
770 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
771# else
772# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
773 : (c) < 256 ? spelltab.st_upper[c] : (c))
774# endif
775
776# ifdef HAVE_ISWUPPER
777# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
778 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
779# else
780# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000781 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000782# endif
783#endif
784
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000785
786static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000787static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000788static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000789static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000790
791/*
792 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000793 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000794 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
795 * or when it's OK it remains unchanged.
796 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000797 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000798 * "capcol" is used to check for a Capitalised word after the end of a
799 * sentence. If it's zero then perform the check. Return the column where to
800 * check next, or -1 when no sentence end was found. If it's NULL then don't
801 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000802 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000803 * Returns the length of the word in bytes, also when it's OK, so that the
804 * caller can skip over the word.
805 */
806 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000807spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000808 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000809 char_u *ptr;
810 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000811 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000812{
813 matchinf_T mi; /* Most things are put in "mi" so that it can
814 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000815 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000816 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000817 int wrongcaplen = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000818
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000819 /* A word never starts at a space or a control character. Return quickly
820 * then, skipping over the character. */
821 if (*ptr <= ' ')
822 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000823 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000824
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000825 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000826 * 0X99FF. But when a word character follows do check spelling to find
827 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000828 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000829 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000830 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
831 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000832 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000833 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000834 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000835 nrlen = mi.mi_end - ptr;
836 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000837 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000838 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000839
840 /* Try including the digits in the word. */
841 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000842 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000843 else
844 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000845
Bram Moolenaar0c405862005-06-22 22:26:26 +0000846 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000847 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000848 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000849 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000850 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000852 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000853 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000854
855 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
856 {
857 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000858 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000859 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000860 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000861 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000862 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000863 if (capcol != NULL)
864 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000865
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866 /* We always use the characters up to the next non-word character,
867 * also for bad words. */
868 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000869
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000870 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000871 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000872
Bram Moolenaar5195e452005-08-19 20:32:47 +0000873 /* case-fold the word with one non-word character, so that we can check
874 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000875 if (*mi.mi_fend != NUL)
876 mb_ptr_adv(mi.mi_fend);
877
878 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
879 MAXWLEN + 1);
880 mi.mi_fwordlen = STRLEN(mi.mi_fword);
881
882 /* The word is bad unless we recognize it. */
883 mi.mi_result = SP_BAD;
884
885 /*
886 * Loop over the languages specified in 'spelllang'.
887 * We check them all, because a matching word may be longer than an
888 * already found matching word.
889 */
890 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
891 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
892 {
893 /* Check for a matching word in case-folded words. */
894 find_word(&mi, FIND_FOLDWORD);
895
896 /* Check for a matching word in keep-case words. */
897 find_word(&mi, FIND_KEEPWORD);
898
899 /* Check for matching prefixes. */
900 find_prefix(&mi);
901 }
902
903 if (mi.mi_result != SP_OK)
904 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000905 /* If we found a number skip over it. Allows for "42nd". Do flag
906 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000907 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000908 {
909 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
910 return nrlen;
911 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000912
913 /* When we are at a non-word character there is no error, just
914 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000915 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000916 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000917 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
918 {
919 regmatch_T regmatch;
920
921 /* Check for end of sentence. */
922 regmatch.regprog = wp->w_buffer->b_cap_prog;
923 regmatch.rm_ic = FALSE;
924 if (vim_regexec(&regmatch, ptr, 0))
925 *capcol = (int)(regmatch.endp[0] - ptr);
926 }
927
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000928#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000929 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000930 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000931#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000932 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000933 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000934 else if (mi.mi_end == ptr)
935 /* Always include at least one character. Required for when there
936 * is a mixup in "midword". */
937 mb_ptr_adv(mi.mi_end);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000938
939 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
940 *attrp = highlight_attr[HLF_SPB];
941 else if (mi.mi_result == SP_RARE)
942 *attrp = highlight_attr[HLF_SPR];
943 else
944 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000945 }
946
Bram Moolenaar5195e452005-08-19 20:32:47 +0000947 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
948 {
949 /* Report SpellCap only when the word isn't badly spelled. */
950 *attrp = highlight_attr[HLF_SPC];
951 return wrongcaplen;
952 }
953
Bram Moolenaar51485f02005-06-04 21:55:20 +0000954 return (int)(mi.mi_end - ptr);
955}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000956
Bram Moolenaar51485f02005-06-04 21:55:20 +0000957/*
958 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000959 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
960 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
961 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
962 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000963 *
964 * For a match mip->mi_result is updated.
965 */
966 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000967find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000968 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000969 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000970{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000971 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000972 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000973 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000974 int endidxcnt = 0;
975 int len;
976 int wlen = 0;
977 int flen;
978 int c;
979 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000980 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000981#ifdef FEAT_MBYTE
982 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000983 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000984#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000985 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000986 slang_T *slang = mip->mi_lp->lp_slang;
987 unsigned flags;
988 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000989 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000990 int word_ends;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000991
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000992 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000993 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000994 /* Check for word with matching case in keep-case tree. */
995 ptr = mip->mi_word;
996 flen = 9999; /* no case folding, always enough bytes */
997 byts = slang->sl_kbyts;
998 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000999
1000 if (mode == FIND_KEEPCOMPOUND)
1001 /* Skip over the previously found word(s). */
1002 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001003 }
1004 else
1005 {
1006 /* Check for case-folded in case-folded tree. */
1007 ptr = mip->mi_fword;
1008 flen = mip->mi_fwordlen; /* available case-folded bytes */
1009 byts = slang->sl_fbyts;
1010 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001011
1012 if (mode == FIND_PREFIX)
1013 {
1014 /* Skip over the prefix. */
1015 wlen = mip->mi_prefixlen;
1016 flen -= mip->mi_prefixlen;
1017 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001018 else if (mode == FIND_COMPOUND)
1019 {
1020 /* Skip over the previously found word(s). */
1021 wlen = mip->mi_compoff;
1022 flen -= mip->mi_compoff;
1023 }
1024
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001025 }
1026
Bram Moolenaar51485f02005-06-04 21:55:20 +00001027 if (byts == NULL)
1028 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001029
Bram Moolenaar51485f02005-06-04 21:55:20 +00001030 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001031 * Repeat advancing in the tree until:
1032 * - there is a byte that doesn't match,
1033 * - we reach the end of the tree,
1034 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001035 */
1036 for (;;)
1037 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001038 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001039 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040
1041 len = byts[arridx++];
1042
1043 /* If the first possible byte is a zero the word could end here.
1044 * Remember this index, we first check for the longest word. */
1045 if (byts[arridx] == 0)
1046 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001047 if (endidxcnt == MAXWLEN)
1048 {
1049 /* Must be a corrupted spell file. */
1050 EMSG(_(e_format));
1051 return;
1052 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001053 endlen[endidxcnt] = wlen;
1054 endidx[endidxcnt++] = arridx++;
1055 --len;
1056
1057 /* Skip over the zeros, there can be several flag/region
1058 * combinations. */
1059 while (len > 0 && byts[arridx] == 0)
1060 {
1061 ++arridx;
1062 --len;
1063 }
1064 if (len == 0)
1065 break; /* no children, word must end here */
1066 }
1067
1068 /* Stop looking at end of the line. */
1069 if (ptr[wlen] == NUL)
1070 break;
1071
1072 /* Perform a binary search in the list of accepted bytes. */
1073 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001074 if (c == TAB) /* <Tab> is handled like <Space> */
1075 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001076 lo = arridx;
1077 hi = arridx + len - 1;
1078 while (lo < hi)
1079 {
1080 m = (lo + hi) / 2;
1081 if (byts[m] > c)
1082 hi = m - 1;
1083 else if (byts[m] < c)
1084 lo = m + 1;
1085 else
1086 {
1087 lo = hi = m;
1088 break;
1089 }
1090 }
1091
1092 /* Stop if there is no matching byte. */
1093 if (hi < lo || byts[lo] != c)
1094 break;
1095
1096 /* Continue at the child (if there is one). */
1097 arridx = idxs[lo];
1098 ++wlen;
1099 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001100
1101 /* One space in the good word may stand for several spaces in the
1102 * checked word. */
1103 if (c == ' ')
1104 {
1105 for (;;)
1106 {
1107 if (flen <= 0 && *mip->mi_fend != NUL)
1108 flen = fold_more(mip);
1109 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1110 break;
1111 ++wlen;
1112 --flen;
1113 }
1114 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001115 }
1116
1117 /*
1118 * Verify that one of the possible endings is valid. Try the longest
1119 * first.
1120 */
1121 while (endidxcnt > 0)
1122 {
1123 --endidxcnt;
1124 arridx = endidx[endidxcnt];
1125 wlen = endlen[endidxcnt];
1126
1127#ifdef FEAT_MBYTE
1128 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1129 continue; /* not at first byte of character */
1130#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001131 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001132 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001133 if (slang->sl_compprog == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001134 continue; /* next char is a word character */
1135 word_ends = FALSE;
1136 }
1137 else
1138 word_ends = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001139
1140#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001141 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001142 {
1143 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001144 * when folding case. This can be slow, take a shortcut when the
1145 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001146 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001147 if (STRNCMP(ptr, p, wlen) != 0)
1148 {
1149 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1150 mb_ptr_adv(p);
1151 wlen = p - mip->mi_word;
1152 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001153 }
1154#endif
1155
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001156 /* Check flags and region. For FIND_PREFIX check the condition and
1157 * prefix ID.
1158 * Repeat this if there are more flags/region alternatives until there
1159 * is a match. */
1160 res = SP_BAD;
1161 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1162 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001163 {
1164 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001165
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001166 /* For the fold-case tree check that the case of the checked word
1167 * matches with what the word in the tree requires.
1168 * For keep-case tree the case is always right. For prefixes we
1169 * don't bother to check. */
1170 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001171 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001172 if (mip->mi_cend != mip->mi_word + wlen)
1173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001174 /* mi_capflags was set for a different word length, need
1175 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001176 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001177 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001178 }
1179
Bram Moolenaar0c405862005-06-22 22:26:26 +00001180 if (mip->mi_capflags == WF_KEEPCAP
1181 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001182 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001183 }
1184
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001185 /* When mode is FIND_PREFIX the word must support the prefix:
1186 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001187 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001188 else if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001189 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001190 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001191 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001192 mip->mi_word + mip->mi_cprefixlen, slang,
1193 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001194 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001195 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001196
1197 /* Use the WF_RARE flag for a rare prefix. */
1198 if (c & WF_RAREPFX)
1199 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001200 }
1201
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001202 if (mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1203 || !word_ends)
1204 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001205 /* If there is no flag or the word is shorter than
1206 * COMPOUNDMIN reject it quickly.
1207 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001208 * that's too short... Myspell compatibility requires this
1209 * anyway. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 if (((unsigned)flags >> 24) == 0 || wlen < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001211 continue;
1212
Bram Moolenaar5195e452005-08-19 20:32:47 +00001213 /* Limit the number of compound words to COMPOUNDMAX. */
1214 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001215 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001216
1217 /* At start of word quickly check if compounding is possible
1218 * with this flag. */
1219 if (mip->mi_complen == 0
1220 && vim_strchr(slang->sl_compstartflags,
1221 ((unsigned)flags >> 24)) == NULL)
1222 continue;
1223
1224 /* If the word ends the sequence of compound flags of the
1225 * words must match with one of the COMPOUNDFLAGS items and
1226 * the number of syllables must not be too large. */
1227 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1228 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1229 if (word_ends)
1230 {
1231 char_u fword[MAXWLEN];
1232
1233 if (slang->sl_compsylmax < MAXWLEN)
1234 {
1235 /* "fword" is only needed for checking syllables. */
1236 if (ptr == mip->mi_word)
1237 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1238 else
1239 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1240 }
1241 if (!can_compound(slang, fword, mip->mi_compflags))
1242 continue;
1243 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001244 }
1245
1246 if (!word_ends)
1247 {
1248 /* Check that a valid word follows. If there is one, it will
1249 * set "mi_result", thus we are always finished here.
1250 * Recursive! */
1251
1252 /* Find following word in case-folded tree. */
1253 mip->mi_compoff = endlen[endidxcnt];
1254#ifdef FEAT_MBYTE
1255 if (has_mbyte && mode == FIND_KEEPWORD)
1256 {
1257 /* Compute byte length in case-folded word from "wlen":
1258 * byte length in keep-case word. Length may change when
1259 * folding case. This can be slow, take a shortcut when
1260 * the case-folded word is equal to the keep-case word. */
1261 p = mip->mi_fword;
1262 if (STRNCMP(ptr, p, wlen) != 0)
1263 {
1264 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1265 mb_ptr_adv(p);
1266 mip->mi_compoff = p - mip->mi_fword;
1267 }
1268 }
1269#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001270 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001271 find_word(mip, FIND_COMPOUND);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001272 --mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001273 if (mip->mi_result == SP_OK)
1274 break;
1275
1276 /* Find following word in keep-case tree. */
1277 mip->mi_compoff = wlen;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001278 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001279 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001280 --mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001281 if (mip->mi_result == SP_OK)
1282 break;
1283 continue;
1284 }
1285
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001286 if (flags & WF_BANNED)
1287 res = SP_BANNED;
1288 else if (flags & WF_REGION)
1289 {
1290 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001291 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001292 res = SP_OK;
1293 else
1294 res = SP_LOCAL;
1295 }
1296 else if (flags & WF_RARE)
1297 res = SP_RARE;
1298 else
1299 res = SP_OK;
1300
1301 /* Always use the longest match and the best result. */
1302 if (mip->mi_result > res)
1303 {
1304 mip->mi_result = res;
1305 mip->mi_end = mip->mi_word + wlen;
1306 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001307 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001308 mip->mi_end = mip->mi_word + wlen;
1309
1310 if (res == SP_OK)
1311 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001312 }
1313
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001314 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001315 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001316 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001317}
1318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001319/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001320 * Return TRUE if "flags" is a valid sequence of compound flags and
1321 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001322 */
1323 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001324can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001325 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001326 char_u *word;
1327 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001328{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001329 regmatch_T regmatch;
1330
1331 if (slang->sl_compprog == NULL)
1332 return FALSE;
1333 regmatch.regprog = slang->sl_compprog;
1334 regmatch.rm_ic = FALSE;
1335 if (!vim_regexec(&regmatch, flags, 0))
1336 return FALSE;
1337
1338 /* Count the number of syllables. This may be slow, do it last. */
1339 if (slang->sl_compsylmax < MAXWLEN
1340 && count_syllables(slang, word) > slang->sl_compsylmax)
1341 return FALSE;
1342 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001343}
1344
1345/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001346 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1347 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001348 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001349 */
1350 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001351valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001352 int totprefcnt; /* nr of prefix IDs */
1353 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001354 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001355 char_u *word;
1356 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001357 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001358{
1359 int prefcnt;
1360 int pidx;
1361 regprog_T *rp;
1362 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001363 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001364
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001365 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001366 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1367 {
1368 pidx = slang->sl_pidxs[arridx + prefcnt];
1369
1370 /* Check the prefix ID. */
1371 if (prefid != (pidx & 0xff))
1372 continue;
1373
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001374 /* Check if the prefix doesn't combine and the word already has a
1375 * suffix. */
1376 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1377 continue;
1378
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001379 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001380 * stored in the two bytes above the prefix ID byte. */
1381 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001382 if (rp != NULL)
1383 {
1384 regmatch.regprog = rp;
1385 regmatch.rm_ic = FALSE;
1386 if (!vim_regexec(&regmatch, word, 0))
1387 continue;
1388 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001389 else if (cond_req)
1390 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001391
Bram Moolenaar53805d12005-08-01 07:08:33 +00001392 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001393 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001394 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001395 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001396}
1397
1398/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001399 * Check if the word at "mip->mi_word" has a matching prefix.
1400 * If it does, then check the following word.
1401 *
1402 * For a match mip->mi_result is updated.
1403 */
1404 static void
1405find_prefix(mip)
1406 matchinf_T *mip;
1407{
1408 idx_T arridx = 0;
1409 int len;
1410 int wlen = 0;
1411 int flen;
1412 int c;
1413 char_u *ptr;
1414 idx_T lo, hi, m;
1415 slang_T *slang = mip->mi_lp->lp_slang;
1416 char_u *byts;
1417 idx_T *idxs;
1418
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001419 byts = slang->sl_pbyts;
1420 if (byts == NULL)
1421 return; /* array is empty */
1422
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001423 /* We use the case-folded word here, since prefixes are always
1424 * case-folded. */
1425 ptr = mip->mi_fword;
1426 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001427 idxs = slang->sl_pidxs;
1428
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001429 /*
1430 * Repeat advancing in the tree until:
1431 * - there is a byte that doesn't match,
1432 * - we reach the end of the tree,
1433 * - or we reach the end of the line.
1434 */
1435 for (;;)
1436 {
1437 if (flen == 0 && *mip->mi_fend != NUL)
1438 flen = fold_more(mip);
1439
1440 len = byts[arridx++];
1441
1442 /* If the first possible byte is a zero the prefix could end here.
1443 * Check if the following word matches and supports the prefix. */
1444 if (byts[arridx] == 0)
1445 {
1446 /* There can be several prefixes with different conditions. We
1447 * try them all, since we don't know which one will give the
1448 * longest match. The word is the same each time, pass the list
1449 * of possible prefixes to find_word(). */
1450 mip->mi_prefarridx = arridx;
1451 mip->mi_prefcnt = len;
1452 while (len > 0 && byts[arridx] == 0)
1453 {
1454 ++arridx;
1455 --len;
1456 }
1457 mip->mi_prefcnt -= len;
1458
1459 /* Find the word that comes after the prefix. */
1460 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001461#ifdef FEAT_MBYTE
1462 if (has_mbyte)
1463 {
1464 /* Case-folded length may differ from original length. */
1465 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1466 mip->mi_word);
1467 }
1468 else
1469 mip->mi_cprefixlen = wlen;
1470#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001471 find_word(mip, FIND_PREFIX);
1472
1473
1474 if (len == 0)
1475 break; /* no children, word must end here */
1476 }
1477
1478 /* Stop looking at end of the line. */
1479 if (ptr[wlen] == NUL)
1480 break;
1481
1482 /* Perform a binary search in the list of accepted bytes. */
1483 c = ptr[wlen];
1484 lo = arridx;
1485 hi = arridx + len - 1;
1486 while (lo < hi)
1487 {
1488 m = (lo + hi) / 2;
1489 if (byts[m] > c)
1490 hi = m - 1;
1491 else if (byts[m] < c)
1492 lo = m + 1;
1493 else
1494 {
1495 lo = hi = m;
1496 break;
1497 }
1498 }
1499
1500 /* Stop if there is no matching byte. */
1501 if (hi < lo || byts[lo] != c)
1502 break;
1503
1504 /* Continue at the child (if there is one). */
1505 arridx = idxs[lo];
1506 ++wlen;
1507 --flen;
1508 }
1509}
1510
1511/*
1512 * Need to fold at least one more character. Do until next non-word character
1513 * for efficiency.
1514 * Return the length of the folded chars in bytes.
1515 */
1516 static int
1517fold_more(mip)
1518 matchinf_T *mip;
1519{
1520 int flen;
1521 char_u *p;
1522
1523 p = mip->mi_fend;
1524 do
1525 {
1526 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001527 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001528
1529 /* Include the non-word character so that we can check for the
1530 * word end. */
1531 if (*mip->mi_fend != NUL)
1532 mb_ptr_adv(mip->mi_fend);
1533
1534 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1535 mip->mi_fword + mip->mi_fwordlen,
1536 MAXWLEN - mip->mi_fwordlen);
1537 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1538 mip->mi_fwordlen += flen;
1539 return flen;
1540}
1541
1542/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 * Check case flags for a word. Return TRUE if the word has the requested
1544 * case.
1545 */
1546 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001547spell_valid_case(wordflags, treeflags)
1548 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001549 int treeflags; /* flags for the word in the spell tree */
1550{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001551 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001552 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001553 && ((treeflags & WF_ONECAP) == 0
1554 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001555}
1556
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001557/*
1558 * Return TRUE if spell checking is not enabled.
1559 */
1560 static int
1561no_spell_checking()
1562{
1563 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1564 {
1565 EMSG(_("E756: Spell checking is not enabled"));
1566 return TRUE;
1567 }
1568 return FALSE;
1569}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001570
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001571/*
1572 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001573 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001574 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1575 * to after badly spelled word before the cursor.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001576 * Return OK if found, FAIL otherwise.
1577 */
1578 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001579spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001580 int dir; /* FORWARD or BACKWARD */
1581 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001582 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001583{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001584 linenr_T lnum;
1585 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001586 char_u *line;
1587 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001588 char_u *endp;
1589 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001590 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001591 int has_syntax = syntax_present(curbuf);
1592 int col;
1593 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001594 char_u *buf = NULL;
1595 int buflen = 0;
1596 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001597 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001598
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001599 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001600 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001601
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001602 /*
1603 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001604 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001605 *
1606 * When searching backwards, we continue in the line to find the last
1607 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001608 *
1609 * We concatenate the start of the next line, so that wrapped words work
1610 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1611 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001612 */
1613 lnum = curwin->w_cursor.lnum;
1614 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001615
1616 while (!got_int)
1617 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001618 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001619
Bram Moolenaar0c405862005-06-22 22:26:26 +00001620 len = STRLEN(line);
1621 if (buflen < len + MAXWLEN + 2)
1622 {
1623 vim_free(buf);
1624 buflen = len + MAXWLEN + 2;
1625 buf = alloc(buflen);
1626 if (buf == NULL)
1627 break;
1628 }
1629
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001630 /* In first line check first word for Capital. */
1631 if (lnum == 1)
1632 capcol = 0;
1633
1634 /* For checking first word with a capital skip white space. */
1635 if (capcol == 0)
1636 capcol = skipwhite(line) - line;
1637
Bram Moolenaar0c405862005-06-22 22:26:26 +00001638 /* Copy the line into "buf" and append the start of the next line if
1639 * possible. */
1640 STRCPY(buf, line);
1641 if (lnum < curbuf->b_ml.ml_line_count)
1642 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1643
1644 p = buf + skip;
1645 endp = buf + len;
1646 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001647 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001648 /* When searching backward don't search after the cursor. */
1649 if (dir == BACKWARD
1650 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001651 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001652 break;
1653
1654 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001655 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001656 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001657
1658 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001659 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001660 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001661 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001662 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001663 /* When searching forward only accept a bad word after
1664 * the cursor. */
1665 if (dir == BACKWARD
1666 || lnum > curwin->w_cursor.lnum
1667 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001668 && (colnr_T)(curline ? p - buf + len
1669 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001670 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001672 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001673 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001674 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001675 (void)syn_get_id(lnum, (colnr_T)col,
1676 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001677 }
1678 else
1679 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001680
Bram Moolenaar51485f02005-06-04 21:55:20 +00001681 if (can_spell)
1682 {
1683 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001684 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001685#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001686 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001687#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001688 if (dir == FORWARD)
1689 {
1690 /* No need to search further. */
1691 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001692 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001693 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001694 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001695 else if (curline)
1696 /* Insert mode completion: put cursor after
1697 * the bad word. */
1698 found_pos.col += len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001699 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001700 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001701 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001702 }
1703
Bram Moolenaar51485f02005-06-04 21:55:20 +00001704 /* advance to character after the word */
1705 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001706 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001707 }
1708
Bram Moolenaar5195e452005-08-19 20:32:47 +00001709 if (dir == BACKWARD && found_pos.lnum != 0)
1710 {
1711 /* Use the last match in the line. */
1712 curwin->w_cursor = found_pos;
1713 vim_free(buf);
1714 return OK;
1715 }
1716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001717 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001718 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001719
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001720 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001721 if (dir == BACKWARD)
1722 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001723 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001724 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001725 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001726 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001727 }
1728 else
1729 {
1730 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001731 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001732 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001733
1734 /* Skip the characters at the start of the next line that were
1735 * included in a match crossing line boundaries. */
1736 if (attr == 0)
1737 skip = p - endp;
1738 else
1739 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001740
1741 /* Capscol skips over the inserted space. */
1742 --capcol;
1743
1744 /* But after empty line check first word in next line */
1745 if (*skipwhite(line) == NUL)
1746 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001747 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001748
1749 line_breakcheck();
1750 }
1751
Bram Moolenaar0c405862005-06-22 22:26:26 +00001752 vim_free(buf);
1753 return FAIL;
1754}
1755
1756/*
1757 * For spell checking: concatenate the start of the following line "line" into
1758 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1759 */
1760 void
1761spell_cat_line(buf, line, maxlen)
1762 char_u *buf;
1763 char_u *line;
1764 int maxlen;
1765{
1766 char_u *p;
1767 int n;
1768
1769 p = skipwhite(line);
1770 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1771 p = skipwhite(p + 1);
1772
1773 if (*p != NUL)
1774 {
1775 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001776 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001777 n = p - line;
1778 if (n >= maxlen)
1779 n = maxlen - 1;
1780 vim_memset(buf + 1, ' ', n);
1781 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782}
1783
1784/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001785 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001786 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001787 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001788 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001789spell_load_lang(lang)
1790 char_u *lang;
1791{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001792 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001793 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001794 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001795
Bram Moolenaarb765d632005-06-07 21:00:02 +00001796 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001797 * It's truncated when an error is detected. */
1798 STRCPY(langcp, lang);
1799
Bram Moolenaarb765d632005-06-07 21:00:02 +00001800 /*
1801 * Find the first spell file for "lang" in 'runtimepath' and load it.
1802 */
1803 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1804 "spell/%s.%s.spl", lang, spell_enc());
1805 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001807 if (r == FAIL && *langcp != NUL)
1808 {
1809 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001810 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001811 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001812 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001813 }
1814
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001815 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001816 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1817 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001818 else if (*langcp != NUL)
1819 {
1820 /* Load all the additions. */
1821 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1822 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1823 }
1824}
1825
1826/*
1827 * Return the encoding used for spell checking: Use 'encoding', except that we
1828 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1829 */
1830 static char_u *
1831spell_enc()
1832{
1833
1834#ifdef FEAT_MBYTE
1835 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1836 return p_enc;
1837#endif
1838 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001839}
1840
1841/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001842 * Get the name of the .spl file for the internal wordlist into
1843 * "fname[MAXPATHL]".
1844 */
1845 static void
1846int_wordlist_spl(fname)
1847 char_u *fname;
1848{
1849 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1850 int_wordlist, spell_enc());
1851}
1852
1853/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001854 * Allocate a new slang_T.
1855 * Caller must fill "sl_next".
1856 */
1857 static slang_T *
1858slang_alloc(lang)
1859 char_u *lang;
1860{
1861 slang_T *lp;
1862
Bram Moolenaar51485f02005-06-04 21:55:20 +00001863 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001864 if (lp != NULL)
1865 {
1866 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001867 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001868 lp->sl_compmax = MAXWLEN;
1869 lp->sl_compminlen = MAXWLEN;
1870 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001871 }
1872 return lp;
1873}
1874
1875/*
1876 * Free the contents of an slang_T and the structure itself.
1877 */
1878 static void
1879slang_free(lp)
1880 slang_T *lp;
1881{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001882 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001883 vim_free(lp->sl_fname);
1884 slang_clear(lp);
1885 vim_free(lp);
1886}
1887
1888/*
1889 * Clear an slang_T so that the file can be reloaded.
1890 */
1891 static void
1892slang_clear(lp)
1893 slang_T *lp;
1894{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001895 garray_T *gap;
1896 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001897 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001898 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001899
Bram Moolenaar51485f02005-06-04 21:55:20 +00001900 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001901 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001902 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001903 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001904 vim_free(lp->sl_pbyts);
1905 lp->sl_pbyts = NULL;
1906
Bram Moolenaar51485f02005-06-04 21:55:20 +00001907 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001908 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001909 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001910 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001911 vim_free(lp->sl_pidxs);
1912 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001913
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001914 gap = &lp->sl_rep;
1915 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001916 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001917 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1918 vim_free(ftp->ft_from);
1919 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001920 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001921 ga_clear(gap);
1922
1923 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001924 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001925 {
1926 /* "ga_len" is set to 1 without adding an item for latin1 */
1927 if (gap->ga_data != NULL)
1928 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1929 for (i = 0; i < gap->ga_len; ++i)
1930 vim_free(((int **)gap->ga_data)[i]);
1931 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001932 else
1933 /* SAL items: free salitem_T items */
1934 while (gap->ga_len > 0)
1935 {
1936 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1937 vim_free(smp->sm_lead);
1938 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1939 vim_free(smp->sm_to);
1940#ifdef FEAT_MBYTE
1941 vim_free(smp->sm_lead_w);
1942 vim_free(smp->sm_oneof_w);
1943 vim_free(smp->sm_to_w);
1944#endif
1945 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001946 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001947
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001948 for (i = 0; i < lp->sl_prefixcnt; ++i)
1949 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001950 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001951 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001952 lp->sl_prefprog = NULL;
1953
1954 vim_free(lp->sl_midword);
1955 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001956
Bram Moolenaar5195e452005-08-19 20:32:47 +00001957 vim_free(lp->sl_compprog);
1958 vim_free(lp->sl_compstartflags);
1959 lp->sl_compprog = NULL;
1960 lp->sl_compstartflags = NULL;
1961
1962 vim_free(lp->sl_syllable);
1963 lp->sl_syllable = NULL;
1964 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001965
Bram Moolenaarea424162005-06-16 21:51:00 +00001966#ifdef FEAT_MBYTE
1967 {
1968 int todo = lp->sl_map_hash.ht_used;
1969 hashitem_T *hi;
1970
1971 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1972 if (!HASHITEM_EMPTY(hi))
1973 {
1974 --todo;
1975 vim_free(hi->hi_key);
1976 }
1977 }
1978 hash_clear(&lp->sl_map_hash);
1979#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00001980
1981 lp->sl_compmax = MAXWLEN;
1982 lp->sl_compminlen = MAXWLEN;
1983 lp->sl_compsylmax = MAXWLEN;
1984 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001985}
1986
1987/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001988 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001989 * Invoked through do_in_runtimepath().
1990 */
1991 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001992spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001993 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001994 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001995{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001997}
1998
1999/*
2000 * Load one spell file and store the info into a slang_T.
2001 *
2002 * This is invoked in two ways:
2003 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2004 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2005 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2006 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002008 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002009 static slang_T *
2010spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002011 char_u *fname;
2012 char_u *lang;
2013 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002014 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002015{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002016 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002017 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002018 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002019 char_u *bp;
2020 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002021 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002022 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002023 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024 int round;
2025 char_u *save_sourcing_name = sourcing_name;
2026 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002027 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002028 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002029 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002030 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002031
Bram Moolenaarb765d632005-06-07 21:00:02 +00002032 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002033 if (fd == NULL)
2034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002035 if (!silent)
2036 EMSG2(_(e_notopen), fname);
2037 else if (p_verbose > 2)
2038 {
2039 verbose_enter();
2040 smsg((char_u *)e_notopen, fname);
2041 verbose_leave();
2042 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002043 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002044 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002045 if (p_verbose > 2)
2046 {
2047 verbose_enter();
2048 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2049 verbose_leave();
2050 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002051
Bram Moolenaarb765d632005-06-07 21:00:02 +00002052 if (old_lp == NULL)
2053 {
2054 lp = slang_alloc(lang);
2055 if (lp == NULL)
2056 goto endFAIL;
2057
2058 /* Remember the file name, used to reload the file when it's updated. */
2059 lp->sl_fname = vim_strsave(fname);
2060 if (lp->sl_fname == NULL)
2061 goto endFAIL;
2062
2063 /* Check for .add.spl. */
2064 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2065 }
2066 else
2067 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002068
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002069 /* Set sourcing_name, so that error messages mention the file name. */
2070 sourcing_name = fname;
2071 sourcing_lnum = 0;
2072
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002073 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002074 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075 for (i = 0; i < VIMSPELLMAGICL; ++i)
2076 buf[i] = getc(fd); /* <fileID> */
2077 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2078 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002079 EMSG(_("E757: This does not look like a spell file"));
2080 goto endFAIL;
2081 }
2082 c = getc(fd); /* <versionnr> */
2083 if (c < VIMSPELLVERSION)
2084 {
2085 EMSG(_("E771: Old spell file, needs to be updated"));
2086 goto endFAIL;
2087 }
2088 else if (c > VIMSPELLVERSION)
2089 {
2090 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002091 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002092 }
2093
Bram Moolenaar5195e452005-08-19 20:32:47 +00002094
2095 /*
2096 * <SECTIONS>: <section> ... <sectionend>
2097 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2098 */
2099 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002100 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002101 n = getc(fd); /* <sectionID> or <sectionend> */
2102 if (n == SN_END)
2103 break;
2104 c = getc(fd); /* <sectionflags> */
2105 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2106 /* <sectionlen> */
2107 if (len < 0)
2108 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002109
Bram Moolenaar5195e452005-08-19 20:32:47 +00002110 res = 0;
2111 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002112 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002113 case SN_REGION:
2114 res = read_region_section(fd, lp, len);
2115 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002116
Bram Moolenaar5195e452005-08-19 20:32:47 +00002117 case SN_CHARFLAGS:
2118 res = read_charflags_section(fd);
2119 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002120
Bram Moolenaar5195e452005-08-19 20:32:47 +00002121 case SN_MIDWORD:
2122 lp->sl_midword = read_string(fd, len); /* <midword> */
2123 if (lp->sl_midword == NULL)
2124 goto endFAIL;
2125 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002126
Bram Moolenaar5195e452005-08-19 20:32:47 +00002127 case SN_PREFCOND:
2128 res = read_prefcond_section(fd, lp);
2129 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002130
Bram Moolenaar5195e452005-08-19 20:32:47 +00002131 case SN_REP:
2132 res = read_rep_section(fd, lp);
2133 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002134
Bram Moolenaar5195e452005-08-19 20:32:47 +00002135 case SN_SAL:
2136 res = read_sal_section(fd, lp);
2137 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002138
Bram Moolenaar5195e452005-08-19 20:32:47 +00002139 case SN_SOFO:
2140 res = read_sofo_section(fd, lp);
2141 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002142
Bram Moolenaar5195e452005-08-19 20:32:47 +00002143 case SN_MAP:
2144 p = read_string(fd, len); /* <mapstr> */
2145 if (p == NULL)
2146 goto endFAIL;
2147 set_map_str(lp, p);
2148 vim_free(p);
2149 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002150
Bram Moolenaar5195e452005-08-19 20:32:47 +00002151 case SN_COMPOUND:
2152 res = read_compound(fd, lp, len);
2153 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002154
Bram Moolenaar5195e452005-08-19 20:32:47 +00002155 case SN_SYLLABLE:
2156 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2157 if (lp->sl_syllable == NULL)
2158 goto endFAIL;
2159 if (init_syl_tab(lp) == FAIL)
2160 goto endFAIL;
2161 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002162
Bram Moolenaar5195e452005-08-19 20:32:47 +00002163 default:
2164 /* Unsupported section. When it's required give an error
2165 * message. When it's not required skip the contents. */
2166 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002167 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002168 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002169 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002170 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002171 while (--len >= 0)
2172 if (getc(fd) < 0)
2173 goto truncerr;
2174 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002175 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002176 if (res == SP_FORMERROR)
2177 {
2178formerr:
2179 EMSG(_(e_format));
2180 goto endFAIL;
2181 }
2182 if (res == SP_TRUNCERROR)
2183 {
2184truncerr:
2185 EMSG(_(e_spell_trunc));
2186 goto endFAIL;
2187 }
2188 if (res == SP_ERROR)
2189 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002190 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002191
Bram Moolenaar51485f02005-06-04 21:55:20 +00002192 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002193 * round 2: <KWORDTREE>
2194 * round 3: <PREFIXTREE> */
2195 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002196 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002197 /* The tree size was computed when writing the file, so that we can
2198 * allocate it as one long block. <nodecount> */
2199 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2200 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002201 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002202 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002203 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002204 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002205 bp = lalloc((long_u)len, TRUE);
2206 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002207 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002208 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002209 lp->sl_fbyts = bp;
2210 else if (round == 2)
2211 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002212 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002213 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002214
2215 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002216 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2217 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002218 goto endFAIL;
2219 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002220 lp->sl_fidxs = ip;
2221 else if (round == 2)
2222 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002223 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002224 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002225
2226 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002227 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002228 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002229 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002230 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002231 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002232 }
2233 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002234
Bram Moolenaarb765d632005-06-07 21:00:02 +00002235 /* For a new file link it in the list of spell files. */
2236 if (old_lp == NULL)
2237 {
2238 lp->sl_next = first_lang;
2239 first_lang = lp;
2240 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002241
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002242 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002243
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002244endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002245 if (lang != NULL)
2246 /* truncating the name signals the error to spell_load_lang() */
2247 *lang = NUL;
2248 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002249 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002250 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002251 lp = NULL;
2252 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002253
2254endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002255 if (fd != NULL)
2256 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002257 sourcing_name = save_sourcing_name;
2258 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259
2260 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002261}
2262
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002263/*
2264 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002265 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002266 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002267 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2268 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002269 */
2270 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002271read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002272 FILE *fd;
2273 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002274 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002275{
2276 int cnt = 0;
2277 int i;
2278 char_u *str;
2279
2280 /* read the length bytes, MSB first */
2281 for (i = 0; i < cnt_bytes; ++i)
2282 cnt = (cnt << 8) + getc(fd);
2283 if (cnt < 0)
2284 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002285 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002286 return NULL;
2287 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002288 *cntp = cnt;
2289 if (cnt == 0)
2290 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002291
Bram Moolenaar5195e452005-08-19 20:32:47 +00002292 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002293 if (str == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002294 *cntp = SP_ERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002295 return str;
2296}
2297
Bram Moolenaar7887d882005-07-01 22:33:52 +00002298/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002299 * Read a string of length "cnt" from "fd" into allocated memory.
2300 * Returns NULL when out of memory.
2301 */
2302 static char_u *
2303read_string(fd, cnt)
2304 FILE *fd;
2305 int cnt;
2306{
2307 char_u *str;
2308 int i;
2309
2310 /* allocate memory */
2311 str = alloc((unsigned)cnt + 1);
2312 if (str != NULL)
2313 {
2314 /* Read the string. Doesn't check for truncated file. */
2315 for (i = 0; i < cnt; ++i)
2316 str[i] = getc(fd);
2317 str[i] = NUL;
2318 }
2319 return str;
2320}
2321
2322/*
2323 * Read SN_REGION: <regionname> ...
2324 * Return SP_*ERROR flags.
2325 */
2326 static int
2327read_region_section(fd, lp, len)
2328 FILE *fd;
2329 slang_T *lp;
2330 int len;
2331{
2332 int i;
2333
2334 if (len > 16)
2335 return SP_FORMERROR;
2336 for (i = 0; i < len; ++i)
2337 lp->sl_regions[i] = getc(fd); /* <regionname> */
2338 lp->sl_regions[len] = NUL;
2339 return 0;
2340}
2341
2342/*
2343 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2344 * <folcharslen> <folchars>
2345 * Return SP_*ERROR flags.
2346 */
2347 static int
2348read_charflags_section(fd)
2349 FILE *fd;
2350{
2351 char_u *flags;
2352 char_u *fol;
2353 int flagslen, follen;
2354
2355 /* <charflagslen> <charflags> */
2356 flags = read_cnt_string(fd, 1, &flagslen);
2357 if (flagslen < 0)
2358 return flagslen;
2359
2360 /* <folcharslen> <folchars> */
2361 fol = read_cnt_string(fd, 2, &follen);
2362 if (follen < 0)
2363 {
2364 vim_free(flags);
2365 return follen;
2366 }
2367
2368 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2369 if (flags != NULL && fol != NULL)
2370 set_spell_charflags(flags, flagslen, fol);
2371
2372 vim_free(flags);
2373 vim_free(fol);
2374
2375 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2376 if ((flags == NULL) != (fol == NULL))
2377 return SP_FORMERROR;
2378 return 0;
2379}
2380
2381/*
2382 * Read SN_PREFCOND section.
2383 * Return SP_*ERROR flags.
2384 */
2385 static int
2386read_prefcond_section(fd, lp)
2387 FILE *fd;
2388 slang_T *lp;
2389{
2390 int cnt;
2391 int i;
2392 int n;
2393 char_u *p;
2394 char_u buf[MAXWLEN + 1];
2395
2396 /* <prefcondcnt> <prefcond> ... */
2397 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2398 if (cnt <= 0)
2399 return SP_FORMERROR;
2400
2401 lp->sl_prefprog = (regprog_T **)alloc_clear(
2402 (unsigned)sizeof(regprog_T *) * cnt);
2403 if (lp->sl_prefprog == NULL)
2404 return SP_ERROR;
2405 lp->sl_prefixcnt = cnt;
2406
2407 for (i = 0; i < cnt; ++i)
2408 {
2409 /* <prefcond> : <condlen> <condstr> */
2410 n = getc(fd); /* <condlen> */
2411 if (n < 0 || n >= MAXWLEN)
2412 return SP_FORMERROR;
2413
2414 /* When <condlen> is zero we have an empty condition. Otherwise
2415 * compile the regexp program used to check for the condition. */
2416 if (n > 0)
2417 {
2418 buf[0] = '^'; /* always match at one position only */
2419 p = buf + 1;
2420 while (n-- > 0)
2421 *p++ = getc(fd); /* <condstr> */
2422 *p = NUL;
2423 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2424 }
2425 }
2426 return 0;
2427}
2428
2429/*
2430 * Read REP items section from "fd": <repcount> <rep> ...
2431 * Return SP_*ERROR flags.
2432 */
2433 static int
2434read_rep_section(fd, slang)
2435 FILE *fd;
2436 slang_T *slang;
2437{
2438 int cnt;
2439 garray_T *gap;
2440 fromto_T *ftp;
2441 short *first;
2442 int i;
2443
2444 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2445 if (cnt < 0)
2446 return SP_TRUNCERROR;
2447
2448 gap = &slang->sl_rep;
2449 if (ga_grow(gap, cnt) == FAIL)
2450 return SP_ERROR;
2451
2452 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2453 for (; gap->ga_len < cnt; ++gap->ga_len)
2454 {
2455 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2456 ftp->ft_from = read_cnt_string(fd, 1, &i);
2457 if (i < 0)
2458 return i;
2459 if (i == 0)
2460 return SP_FORMERROR;
2461 ftp->ft_to = read_cnt_string(fd, 1, &i);
2462 if (i <= 0)
2463 {
2464 vim_free(ftp->ft_from);
2465 if (i < 0)
2466 return i;
2467 return SP_FORMERROR;
2468 }
2469 }
2470
2471 /* Fill the first-index table. */
2472 first = slang->sl_rep_first;
2473 for (i = 0; i < 256; ++i)
2474 first[i] = -1;
2475 for (i = 0; i < gap->ga_len; ++i)
2476 {
2477 ftp = &((fromto_T *)gap->ga_data)[i];
2478 if (first[*ftp->ft_from] == -1)
2479 first[*ftp->ft_from] = i;
2480 }
2481 return 0;
2482}
2483
2484/*
2485 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2486 * Return SP_*ERROR flags.
2487 */
2488 static int
2489read_sal_section(fd, slang)
2490 FILE *fd;
2491 slang_T *slang;
2492{
2493 int i;
2494 int cnt;
2495 garray_T *gap;
2496 salitem_T *smp;
2497 int ccnt;
2498 char_u *p;
2499 int c;
2500
2501 slang->sl_sofo = FALSE;
2502
2503 i = getc(fd); /* <salflags> */
2504 if (i & SAL_F0LLOWUP)
2505 slang->sl_followup = TRUE;
2506 if (i & SAL_COLLAPSE)
2507 slang->sl_collapse = TRUE;
2508 if (i & SAL_REM_ACCENTS)
2509 slang->sl_rem_accents = TRUE;
2510
2511 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2512 if (cnt < 0)
2513 return SP_TRUNCERROR;
2514
2515 gap = &slang->sl_sal;
2516 ga_init2(gap, sizeof(salitem_T), 10);
2517 if (ga_grow(gap, cnt) == FAIL)
2518 return SP_ERROR;
2519
2520 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2521 for (; gap->ga_len < cnt; ++gap->ga_len)
2522 {
2523 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2524 ccnt = getc(fd); /* <salfromlen> */
2525 if (ccnt < 0)
2526 return SP_TRUNCERROR;
2527 if ((p = alloc(ccnt + 2)) == NULL)
2528 return SP_ERROR;
2529 smp->sm_lead = p;
2530
2531 /* Read up to the first special char into sm_lead. */
2532 for (i = 0; i < ccnt; ++i)
2533 {
2534 c = getc(fd); /* <salfrom> */
2535 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2536 break;
2537 *p++ = c;
2538 }
2539 smp->sm_leadlen = p - smp->sm_lead;
2540 *p++ = NUL;
2541
2542 /* Put (abc) chars in sm_oneof, if any. */
2543 if (c == '(')
2544 {
2545 smp->sm_oneof = p;
2546 for (++i; i < ccnt; ++i)
2547 {
2548 c = getc(fd); /* <salfrom> */
2549 if (c == ')')
2550 break;
2551 *p++ = c;
2552 }
2553 *p++ = NUL;
2554 if (++i < ccnt)
2555 c = getc(fd);
2556 }
2557 else
2558 smp->sm_oneof = NULL;
2559
2560 /* Any following chars go in sm_rules. */
2561 smp->sm_rules = p;
2562 if (i < ccnt)
2563 /* store the char we got while checking for end of sm_lead */
2564 *p++ = c;
2565 for (++i; i < ccnt; ++i)
2566 *p++ = getc(fd); /* <salfrom> */
2567 *p++ = NUL;
2568
2569 /* <saltolen> <salto> */
2570 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2571 if (ccnt < 0)
2572 {
2573 vim_free(smp->sm_lead);
2574 return ccnt;
2575 }
2576
2577#ifdef FEAT_MBYTE
2578 if (has_mbyte)
2579 {
2580 /* convert the multi-byte strings to wide char strings */
2581 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2582 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2583 if (smp->sm_oneof == NULL)
2584 smp->sm_oneof_w = NULL;
2585 else
2586 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2587 if (smp->sm_to == NULL)
2588 smp->sm_to_w = NULL;
2589 else
2590 smp->sm_to_w = mb_str2wide(smp->sm_to);
2591 if (smp->sm_lead_w == NULL
2592 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2593 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2594 {
2595 vim_free(smp->sm_lead);
2596 vim_free(smp->sm_to);
2597 vim_free(smp->sm_lead_w);
2598 vim_free(smp->sm_oneof_w);
2599 vim_free(smp->sm_to_w);
2600 return SP_ERROR;
2601 }
2602 }
2603#endif
2604 }
2605
2606 /* Fill the first-index table. */
2607 set_sal_first(slang);
2608
2609 return 0;
2610}
2611
2612/*
2613 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2614 * Return SP_*ERROR flags.
2615 */
2616 static int
2617read_sofo_section(fd, slang)
2618 FILE *fd;
2619 slang_T *slang;
2620{
2621 int cnt;
2622 char_u *from, *to;
2623 int res;
2624
2625 slang->sl_sofo = TRUE;
2626
2627 /* <sofofromlen> <sofofrom> */
2628 from = read_cnt_string(fd, 2, &cnt);
2629 if (cnt < 0)
2630 return cnt;
2631
2632 /* <sofotolen> <sofoto> */
2633 to = read_cnt_string(fd, 2, &cnt);
2634 if (cnt < 0)
2635 {
2636 vim_free(from);
2637 return cnt;
2638 }
2639
2640 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2641 if (from != NULL && to != NULL)
2642 res = set_sofo(slang, from, to);
2643 else if (from != NULL || to != NULL)
2644 res = SP_FORMERROR; /* only one of two strings is an error */
2645 else
2646 res = 0;
2647
2648 vim_free(from);
2649 vim_free(to);
2650 return res;
2651}
2652
2653/*
2654 * Read the compound section from the .spl file:
2655 * <compmax> <compminlen> <compsylmax> <compflags>
2656 * Returns SP_*ERROR flags.
2657 */
2658 static int
2659read_compound(fd, slang, len)
2660 FILE *fd;
2661 slang_T *slang;
2662 int len;
2663{
2664 int todo = len;
2665 int c;
2666 int atstart;
2667 char_u *pat;
2668 char_u *pp;
2669 char_u *cp;
2670
2671 if (todo < 2)
2672 return SP_FORMERROR; /* need at least two bytes */
2673
2674 --todo;
2675 c = getc(fd); /* <compmax> */
2676 if (c < 2)
2677 c = MAXWLEN;
2678 slang->sl_compmax = c;
2679
2680 --todo;
2681 c = getc(fd); /* <compminlen> */
2682 if (c < 1)
2683 c = 3;
2684 slang->sl_compminlen = c;
2685
2686 --todo;
2687 c = getc(fd); /* <compsylmax> */
2688 if (c < 1)
2689 c = MAXWLEN;
2690 slang->sl_compsylmax = c;
2691
2692 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2693 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
2694 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes. */
2695 pat = alloc((unsigned)todo * 2 + 7);
2696 if (pat == NULL)
2697 return SP_ERROR;
2698
2699 /* We also need a list of all flags that can appear at the start. */
2700 cp = alloc(todo + 1);
2701 if (cp == NULL)
2702 {
2703 vim_free(pat);
2704 return SP_ERROR;
2705 }
2706 slang->sl_compstartflags = cp;
2707 *cp = NUL;
2708
2709 pp = pat;
2710 *pp++ = '^';
2711 *pp++ = '\\';
2712 *pp++ = '(';
2713
2714 atstart = 1;
2715 while (todo-- > 0)
2716 {
2717 c = getc(fd); /* <compflags> */
2718 if (atstart != 0)
2719 {
2720 /* At start of item: copy flags to "sl_compstartflags". For a
2721 * [abc] item set "atstart" to 2 and copy up to the ']'. */
2722 if (c == '[')
2723 atstart = 2;
2724 else if (c == ']')
2725 atstart = 0;
2726 else
2727 {
2728 if (vim_strchr(slang->sl_compstartflags, c) == NULL)
2729 {
2730 *cp++ = c;
2731 *cp = NUL;
2732 }
2733 if (atstart == 1)
2734 atstart = 0;
2735 }
2736 }
2737 if (c == '/') /* slash separates two items */
2738 {
2739 *pp++ = '\\';
2740 *pp++ = '|';
2741 atstart = 1;
2742 }
2743 else /* normal char, "[abc]" and '*' are copied as-is */
2744 {
2745 if (c == '+')
2746 *pp++ = '\\'; /* "a+" becomes "a\+" */
2747 *pp++ = c;
2748 }
2749 }
2750
2751 *pp++ = '\\';
2752 *pp++ = ')';
2753 *pp++ = '$';
2754 *pp = NUL;
2755
2756 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
2757 vim_free(pat);
2758 if (slang->sl_compprog == NULL)
2759 return SP_FORMERROR;
2760
2761 return 0;
2762}
2763
2764#define SY_MAXLEN 30
2765typedef struct syl_item_S
2766{
2767 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2768 int sy_len;
2769} syl_item_T;
2770
2771/*
2772 * Truncate "slang->sl_syllable" at the first slash and put the following items
2773 * in "slang->sl_syl_items".
2774 */
2775 static int
2776init_syl_tab(slang)
2777 slang_T *slang;
2778{
2779 char_u *p;
2780 char_u *s;
2781 int l;
2782 syl_item_T *syl;
2783
2784 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2785 p = vim_strchr(slang->sl_syllable, '/');
2786 while (p != NULL)
2787 {
2788 *p++ = NUL;
2789 if (p == NUL)
2790 break;
2791 s = p;
2792 p = vim_strchr(p, '/');
2793 if (p == NULL)
2794 l = STRLEN(s);
2795 else
2796 l = p - s;
2797 if (l >= SY_MAXLEN)
2798 return SP_FORMERROR;
2799 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
2800 return SP_ERROR;
2801 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2802 + slang->sl_syl_items.ga_len++;
2803 vim_strncpy(syl->sy_chars, s, l);
2804 syl->sy_len = l;
2805 }
2806 return OK;
2807}
2808
2809/*
2810 * Count the number of syllables in "word".
2811 * When "word" contains spaces the syllables after the last space are counted.
2812 * Returns zero if syllables are not defines.
2813 */
2814 static int
2815count_syllables(slang, word)
2816 slang_T *slang;
2817 char_u *word;
2818{
2819 int cnt = 0;
2820 int skip = FALSE;
2821 char_u *p;
2822 int len;
2823 int i;
2824 syl_item_T *syl;
2825 int c;
2826
2827 if (slang->sl_syllable == NULL)
2828 return 0;
2829
2830 for (p = word; *p != NUL; p += len)
2831 {
2832 /* When running into a space reset counter. */
2833 if (*p == ' ')
2834 {
2835 len = 1;
2836 cnt = 0;
2837 continue;
2838 }
2839
2840 /* Find longest match of syllable items. */
2841 len = 0;
2842 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2843 {
2844 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2845 if (syl->sy_len > len
2846 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2847 len = syl->sy_len;
2848 }
2849 if (len != 0) /* found a match, count syllable */
2850 {
2851 ++cnt;
2852 skip = FALSE;
2853 }
2854 else
2855 {
2856 /* No recognized syllable item, at least a syllable char then? */
2857#ifdef FEAT_MBYTE
2858 c = mb_ptr2char(p);
2859 len = (*mb_ptr2len)(p);
2860#else
2861 c = *p;
2862 len = 1;
2863#endif
2864 if (vim_strchr(slang->sl_syllable, c) == NULL)
2865 skip = FALSE; /* No, search for next syllable */
2866 else if (!skip)
2867 {
2868 ++cnt; /* Yes, count it */
2869 skip = TRUE; /* don't count following syllable chars */
2870 }
2871 }
2872 }
2873 return cnt;
2874}
2875
2876/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00002877 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00002878 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002879 */
2880 static int
2881set_sofo(lp, from, to)
2882 slang_T *lp;
2883 char_u *from;
2884 char_u *to;
2885{
2886 int i;
2887
2888#ifdef FEAT_MBYTE
2889 garray_T *gap;
2890 char_u *s;
2891 char_u *p;
2892 int c;
2893 int *inp;
2894
2895 if (has_mbyte)
2896 {
2897 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2898 * characters. The index is the low byte of the character.
2899 * The list contains from-to pairs with a terminating NUL.
2900 * sl_sal_first[] is used for latin1 "from" characters. */
2901 gap = &lp->sl_sal;
2902 ga_init2(gap, sizeof(int *), 1);
2903 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002904 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002905 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2906 gap->ga_len = 256;
2907
2908 /* First count the number of items for each list. Temporarily use
2909 * sl_sal_first[] for this. */
2910 for (p = from, s = to; *p != NUL && *s != NUL; )
2911 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002912 c = mb_cptr2char_adv(&p);
2913 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002914 if (c >= 256)
2915 ++lp->sl_sal_first[c & 0xff];
2916 }
2917 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002918 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002919
2920 /* Allocate the lists. */
2921 for (i = 0; i < 256; ++i)
2922 if (lp->sl_sal_first[i] > 0)
2923 {
2924 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2925 if (p == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002926 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002927 ((int **)gap->ga_data)[i] = (int *)p;
2928 *(int *)p = 0;
2929 }
2930
2931 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2932 * list. */
2933 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2934 for (p = from, s = to; *p != NUL && *s != NUL; )
2935 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002936 c = mb_cptr2char_adv(&p);
2937 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002938 if (c >= 256)
2939 {
2940 /* Append the from-to chars at the end of the list with
2941 * the low byte. */
2942 inp = ((int **)gap->ga_data)[c & 0xff];
2943 while (*inp != 0)
2944 ++inp;
2945 *inp++ = c; /* from char */
2946 *inp++ = i; /* to char */
2947 *inp++ = NUL; /* NUL at the end */
2948 }
2949 else
2950 /* mapping byte to char is done in sl_sal_first[] */
2951 lp->sl_sal_first[c] = i;
2952 }
2953 }
2954 else
2955#endif
2956 {
2957 /* mapping bytes to bytes is done in sl_sal_first[] */
2958 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00002959 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002960
2961 for (i = 0; to[i] != NUL; ++i)
2962 lp->sl_sal_first[from[i]] = to[i];
2963 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2964 }
2965
Bram Moolenaar5195e452005-08-19 20:32:47 +00002966 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002967}
2968
2969/*
2970 * Fill the first-index table for "lp".
2971 */
2972 static void
2973set_sal_first(lp)
2974 slang_T *lp;
2975{
2976 salfirst_T *sfirst;
2977 int i;
2978 salitem_T *smp;
2979 int c;
2980 garray_T *gap = &lp->sl_sal;
2981
2982 sfirst = lp->sl_sal_first;
2983 for (i = 0; i < 256; ++i)
2984 sfirst[i] = -1;
2985 smp = (salitem_T *)gap->ga_data;
2986 for (i = 0; i < gap->ga_len; ++i)
2987 {
2988#ifdef FEAT_MBYTE
2989 if (has_mbyte)
2990 /* Use the lowest byte of the first character. For latin1 it's
2991 * the character, for other encodings it should differ for most
2992 * characters. */
2993 c = *smp[i].sm_lead_w & 0xff;
2994 else
2995#endif
2996 c = *smp[i].sm_lead;
2997 if (sfirst[c] == -1)
2998 {
2999 sfirst[c] = i;
3000#ifdef FEAT_MBYTE
3001 if (has_mbyte)
3002 {
3003 int n;
3004
3005 /* Make sure all entries with this byte are following each
3006 * other. Move the ones that are in the wrong position. Do
3007 * keep the same ordering! */
3008 while (i + 1 < gap->ga_len
3009 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3010 /* Skip over entry with same index byte. */
3011 ++i;
3012
3013 for (n = 1; i + n < gap->ga_len; ++n)
3014 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3015 {
3016 salitem_T tsal;
3017
3018 /* Move entry with same index byte after the entries
3019 * we already found. */
3020 ++i;
3021 --n;
3022 tsal = smp[i + n];
3023 mch_memmove(smp + i + 1, smp + i,
3024 sizeof(salitem_T) * n);
3025 smp[i] = tsal;
3026 }
3027 }
3028#endif
3029 }
3030 }
3031}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003032
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003033#ifdef FEAT_MBYTE
3034/*
3035 * Turn a multi-byte string into a wide character string.
3036 * Return it in allocated memory (NULL for out-of-memory)
3037 */
3038 static int *
3039mb_str2wide(s)
3040 char_u *s;
3041{
3042 int *res;
3043 char_u *p;
3044 int i = 0;
3045
3046 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3047 if (res != NULL)
3048 {
3049 for (p = s; *p != NUL; )
3050 res[i++] = mb_ptr2char_adv(&p);
3051 res[i] = NUL;
3052 }
3053 return res;
3054}
3055#endif
3056
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003057/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003058 * Read one row of siblings from the spell file and store it in the byte array
3059 * "byts" and index array "idxs". Recursively read the children.
3060 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003061 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003062 *
3063 * Returns the index follosing the siblings.
3064 * Returns -1 if the file is shorter than expected.
3065 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003066 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003067 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003068read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003069 FILE *fd;
3070 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003071 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003072 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003073 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003074 int prefixtree; /* TRUE for reading PREFIXTREE */
3075 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003076{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003077 int len;
3078 int i;
3079 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003080 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003081 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003082 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003083#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003084
Bram Moolenaar51485f02005-06-04 21:55:20 +00003085 len = getc(fd); /* <siblingcount> */
3086 if (len <= 0)
3087 return -1;
3088
3089 if (startidx + len >= maxidx)
3090 return -2;
3091 byts[idx++] = len;
3092
3093 /* Read the byte values, flag/region bytes and shared indexes. */
3094 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003095 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003096 c = getc(fd); /* <byte> */
3097 if (c < 0)
3098 return -1;
3099 if (c <= BY_SPECIAL)
3100 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003101 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003102 {
3103 /* No flags, all regions. */
3104 idxs[idx] = 0;
3105 c = 0;
3106 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003107 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003108 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003109 if (prefixtree)
3110 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003111 /* Read the optional pflags byte, the prefix ID and the
3112 * condition nr. In idxs[] store the prefix ID in the low
3113 * byte, the condition index shifted up 8 bits, the flags
3114 * shifted up 24 bits. */
3115 if (c == BY_FLAGS)
3116 c = getc(fd) << 24; /* <pflags> */
3117 else
3118 c = 0;
3119
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003120 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003121
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003122 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3123 if (n >= maxprefcondnr)
3124 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003125 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003126 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003127 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003128 {
3129 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003130 * idxs[] the flags go in the low two bytes, region above
3131 * that and prefix ID above the region. */
3132 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003133 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003134 if (c2 == BY_FLAGS2)
3135 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003136 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003137 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003138 if (c & WF_AFX)
3139 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003140 }
3141
Bram Moolenaar51485f02005-06-04 21:55:20 +00003142 idxs[idx] = c;
3143 c = 0;
3144 }
3145 else /* c == BY_INDEX */
3146 {
3147 /* <nodeidx> */
3148 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3149 if (n < 0 || n >= maxidx)
3150 return -2;
3151 idxs[idx] = n + SHARED_MASK;
3152 c = getc(fd); /* <xbyte> */
3153 }
3154 }
3155 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003156 }
3157
Bram Moolenaar51485f02005-06-04 21:55:20 +00003158 /* Recursively read the children for non-shared siblings.
3159 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3160 * remove SHARED_MASK) */
3161 for (i = 1; i <= len; ++i)
3162 if (byts[startidx + i] != 0)
3163 {
3164 if (idxs[startidx + i] & SHARED_MASK)
3165 idxs[startidx + i] &= ~SHARED_MASK;
3166 else
3167 {
3168 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003169 idx = read_tree(fd, byts, idxs, maxidx, idx,
3170 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003171 if (idx < 0)
3172 break;
3173 }
3174 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003175
Bram Moolenaar51485f02005-06-04 21:55:20 +00003176 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003177}
3178
3179/*
3180 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003181 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003182 */
3183 char_u *
3184did_set_spelllang(buf)
3185 buf_T *buf;
3186{
3187 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003188 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003189 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003190 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003191 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003192 int region_mask;
3193 slang_T *lp;
3194 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003195 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003196 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003197 int len;
3198 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003199 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003200 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003201 char_u *use_region = NULL;
3202 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003203
3204 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003205 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003206
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003207 /* loop over comma separated language names. */
3208 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003209 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003210 /* Get one language name. */
3211 copy_option_part(&splp, lang, MAXWLEN, ",");
3212
Bram Moolenaar5482f332005-04-17 20:18:43 +00003213 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003214 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003215
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003216 /* If the name ends in ".spl" use it as the name of the spell file.
3217 * If there is a region name let "region" point to it and remove it
3218 * from the name. */
3219 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3220 {
3221 filename = TRUE;
3222
Bram Moolenaarb6356332005-07-18 21:40:44 +00003223 /* Locate a region and remove it from the file name. */
3224 p = vim_strchr(gettail(lang), '_');
3225 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3226 && !ASCII_ISALPHA(p[3]))
3227 {
3228 vim_strncpy(region_cp, p + 1, 2);
3229 mch_memmove(p, p + 3, len - (p - lang) - 2);
3230 len -= 3;
3231 region = region_cp;
3232 }
3233 else
3234 dont_use_region = TRUE;
3235
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003236 /* Check if we loaded this language before. */
3237 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3238 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3239 break;
3240 }
3241 else
3242 {
3243 filename = FALSE;
3244 if (len > 3 && lang[len - 3] == '_')
3245 {
3246 region = lang + len - 2;
3247 len -= 3;
3248 lang[len] = NUL;
3249 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003250 else
3251 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003252
3253 /* Check if we loaded this language before. */
3254 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3255 if (STRICMP(lang, lp->sl_name) == 0)
3256 break;
3257 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003258
Bram Moolenaarb6356332005-07-18 21:40:44 +00003259 if (region != NULL)
3260 {
3261 /* If the region differs from what was used before then don't
3262 * use it for 'spellfile'. */
3263 if (use_region != NULL && STRCMP(region, use_region) != 0)
3264 dont_use_region = TRUE;
3265 use_region = region;
3266 }
3267
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003268 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003269 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003270 {
3271 if (filename)
3272 (void)spell_load_file(lang, lang, NULL, FALSE);
3273 else
3274 spell_load_lang(lang);
3275 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003276
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003277 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003278 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003279 */
3280 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003281 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3282 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003283 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003284 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003285 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003286 {
3287 /* find region in sl_regions */
3288 c = find_region(lp->sl_regions, region);
3289 if (c == REGION_ALL)
3290 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003291 if (lp->sl_add)
3292 {
3293 if (*lp->sl_regions != NUL)
3294 /* This addition file is for other regions. */
3295 region_mask = 0;
3296 }
3297 else
3298 /* This is probably an error. Give a warning and
3299 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003300 smsg((char_u *)
3301 _("Warning: region %s not supported"),
3302 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003303 }
3304 else
3305 region_mask = 1 << c;
3306 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003307
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003308 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003309 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003310 if (ga_grow(&ga, 1) == FAIL)
3311 {
3312 ga_clear(&ga);
3313 return e_outofmem;
3314 }
3315 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3316 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3317 ++ga.ga_len;
3318 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003319 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003320 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003321 }
3322
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003323 /* round 0: load int_wordlist, if possible.
3324 * round 1: load first name in 'spellfile'.
3325 * round 2: load second name in 'spellfile.
3326 * etc. */
3327 spf = curbuf->b_p_spf;
3328 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003329 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003330 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003331 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003332 /* Internal wordlist, if there is one. */
3333 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003334 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003335 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003336 }
3337 else
3338 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003339 /* One entry in 'spellfile'. */
3340 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3341 STRCAT(spf_name, ".spl");
3342
3343 /* If it was already found above then skip it. */
3344 for (c = 0; c < ga.ga_len; ++c)
3345 if (fullpathcmp(spf_name,
3346 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
3347 FALSE) == FPC_SAME)
3348 break;
3349 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003350 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003351 }
3352
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003353 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003354 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3355 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3356 break;
3357 if (lp == NULL)
3358 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003359 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003360 * region name, the region is ignored otherwise. for int_wordlist
3361 * use an arbitrary name. */
3362 if (round == 0)
3363 STRCPY(lang, "internal wordlist");
3364 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003365 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003366 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003367 p = vim_strchr(lang, '.');
3368 if (p != NULL)
3369 *p = NUL; /* truncate at ".encoding.add" */
3370 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003371 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003372 }
3373 if (lp != NULL && ga_grow(&ga, 1) == OK)
3374 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003375 region_mask = REGION_ALL;
3376 if (use_region != NULL && !dont_use_region)
3377 {
3378 /* find region in sl_regions */
3379 c = find_region(lp->sl_regions, use_region);
3380 if (c != REGION_ALL)
3381 region_mask = 1 << c;
3382 else if (*lp->sl_regions != NUL)
3383 /* This spell file is for other regions. */
3384 region_mask = 0;
3385 }
3386
3387 if (region_mask != 0)
3388 {
3389 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3390 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3391 ++ga.ga_len;
3392 use_midword(lp, buf);
3393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003394 }
3395 }
3396
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003397 /* Add a NULL entry to mark the end of the list. */
3398 if (ga_grow(&ga, 1) == FAIL)
3399 {
3400 ga_clear(&ga);
3401 return e_outofmem;
3402 }
3403 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
3404 ++ga.ga_len;
3405
3406 /* Everything is fine, store the new b_langp value. */
3407 ga_clear(&buf->b_langp);
3408 buf->b_langp = ga;
3409
3410 return NULL;
3411}
3412
3413/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003414 * Clear the midword characters for buffer "buf".
3415 */
3416 static void
3417clear_midword(buf)
3418 buf_T *buf;
3419{
3420 vim_memset(buf->b_spell_ismw, 0, 256);
3421#ifdef FEAT_MBYTE
3422 vim_free(buf->b_spell_ismw_mb);
3423 buf->b_spell_ismw_mb = NULL;
3424#endif
3425}
3426
3427/*
3428 * Use the "sl_midword" field of language "lp" for buffer "buf".
3429 * They add up to any currently used midword characters.
3430 */
3431 static void
3432use_midword(lp, buf)
3433 slang_T *lp;
3434 buf_T *buf;
3435{
3436 char_u *p;
3437
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003438 if (lp->sl_midword == NULL) /* there aren't any */
3439 return;
3440
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003441 for (p = lp->sl_midword; *p != NUL; )
3442#ifdef FEAT_MBYTE
3443 if (has_mbyte)
3444 {
3445 int c, l, n;
3446 char_u *bp;
3447
3448 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003449 l = (*mb_ptr2len)(p);
3450 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003451 buf->b_spell_ismw[c] = TRUE;
3452 else if (buf->b_spell_ismw_mb == NULL)
3453 /* First multi-byte char in "b_spell_ismw_mb". */
3454 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3455 else
3456 {
3457 /* Append multi-byte chars to "b_spell_ismw_mb". */
3458 n = STRLEN(buf->b_spell_ismw_mb);
3459 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3460 if (bp != NULL)
3461 {
3462 vim_free(buf->b_spell_ismw_mb);
3463 buf->b_spell_ismw_mb = bp;
3464 vim_strncpy(bp + n, p, l);
3465 }
3466 }
3467 p += l;
3468 }
3469 else
3470#endif
3471 buf->b_spell_ismw[*p++] = TRUE;
3472}
3473
3474/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003475 * Find the region "region[2]" in "rp" (points to "sl_regions").
3476 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003477 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003478 */
3479 static int
3480find_region(rp, region)
3481 char_u *rp;
3482 char_u *region;
3483{
3484 int i;
3485
3486 for (i = 0; ; i += 2)
3487 {
3488 if (rp[i] == NUL)
3489 return REGION_ALL;
3490 if (rp[i] == region[0] && rp[i + 1] == region[1])
3491 break;
3492 }
3493 return i / 2;
3494}
3495
3496/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003497 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003498 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003499 * Word WF_ONECAP
3500 * W WORD WF_ALLCAP
3501 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003502 */
3503 static int
3504captype(word, end)
3505 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003506 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003507{
3508 char_u *p;
3509 int c;
3510 int firstcap;
3511 int allcap;
3512 int past_second = FALSE; /* past second word char */
3513
3514 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003515 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003516 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003517 return 0; /* only non-word characters, illegal word */
3518#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003519 if (has_mbyte)
3520 c = mb_ptr2char_adv(&p);
3521 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003522#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003523 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003524 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003525
3526 /*
3527 * Need to check all letters to find a word with mixed upper/lower.
3528 * But a word with an upper char only at start is a ONECAP.
3529 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003530 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003531 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003532 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003533 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003534 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003535 {
3536 /* UUl -> KEEPCAP */
3537 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003538 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003539 allcap = FALSE;
3540 }
3541 else if (!allcap)
3542 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003543 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003544 past_second = TRUE;
3545 }
3546
3547 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003548 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003549 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003550 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003551 return 0;
3552}
3553
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003554/*
3555 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3556 * capital. So that make_case_word() can turn WOrd into Word.
3557 * Add ALLCAP for "WOrD".
3558 */
3559 static int
3560badword_captype(word, end)
3561 char_u *word;
3562 char_u *end;
3563{
3564 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003565 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003566 int l, u;
3567 int first;
3568 char_u *p;
3569
3570 if (flags & WF_KEEPCAP)
3571 {
3572 /* Count the number of UPPER and lower case letters. */
3573 l = u = 0;
3574 first = FALSE;
3575 for (p = word; p < end; mb_ptr_adv(p))
3576 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003577 c = PTR2CHAR(p);
3578 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003579 {
3580 ++u;
3581 if (p == word)
3582 first = TRUE;
3583 }
3584 else
3585 ++l;
3586 }
3587
3588 /* If there are more UPPER than lower case letters suggest an
3589 * ALLCAP word. Otherwise, if the first letter is UPPER then
3590 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3591 * require three upper case letters. */
3592 if (u > l && u > 2)
3593 flags |= WF_ALLCAP;
3594 else if (first)
3595 flags |= WF_ONECAP;
3596 }
3597 return flags;
3598}
3599
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003600# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3601/*
3602 * Free all languages.
3603 */
3604 void
3605spell_free_all()
3606{
3607 slang_T *lp;
3608 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003609 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003610
3611 /* Go through all buffers and handle 'spelllang'. */
3612 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3613 ga_clear(&buf->b_langp);
3614
3615 while (first_lang != NULL)
3616 {
3617 lp = first_lang;
3618 first_lang = lp->sl_next;
3619 slang_free(lp);
3620 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003621
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003622 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003623 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003624 /* Delete the internal wordlist and its .spl file */
3625 mch_remove(int_wordlist);
3626 int_wordlist_spl(fname);
3627 mch_remove(fname);
3628 vim_free(int_wordlist);
3629 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003630 }
3631
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003632 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003633}
3634# endif
3635
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003636# if defined(FEAT_MBYTE) || defined(PROTO)
3637/*
3638 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003639 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003640 */
3641 void
3642spell_reload()
3643{
3644 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003645 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003646
Bram Moolenaarea408852005-06-25 22:49:46 +00003647 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003648 init_spell_chartab();
3649
3650 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003651 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003652
3653 /* Go through all buffers and handle 'spelllang'. */
3654 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3655 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003656 /* Only load the wordlists when 'spelllang' is set and there is a
3657 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003658 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003659 {
3660 FOR_ALL_WINDOWS(wp)
3661 if (wp->w_buffer == buf && wp->w_p_spell)
3662 {
3663 (void)did_set_spelllang(buf);
3664# ifdef FEAT_WINDOWS
3665 break;
3666# endif
3667 }
3668 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003669 }
3670}
3671# endif
3672
Bram Moolenaarb765d632005-06-07 21:00:02 +00003673/*
3674 * Reload the spell file "fname" if it's loaded.
3675 */
3676 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003677spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003678 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003679 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003680{
3681 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003683
Bram Moolenaarb765d632005-06-07 21:00:02 +00003684 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3685 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3686 {
3687 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003688 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003689 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003691 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003692
3693 /* When "zg" was used and the file wasn't loaded yet, should redo
3694 * 'spelllang' to get it loaded. */
3695 if (added_word && !didit)
3696 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003697}
3698
3699
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003700/*
3701 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003702 */
3703
Bram Moolenaar51485f02005-06-04 21:55:20 +00003704#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003705 and .dic file. */
3706/*
3707 * Main structure to store the contents of a ".aff" file.
3708 */
3709typedef struct afffile_S
3710{
3711 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003712 int af_slash; /* character used in word for slash */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003713 int af_rar; /* RAR ID for rare word */
3714 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003715 int af_bad; /* BAD ID for banned word */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003716 int af_needaffix; /* NEEDAFFIX ID */
3717 char_u *af_compflags; /* COMPOUNDFLAG and COMPOUNDFLAGS concat'ed */
3718 int af_compmax; /* COMPOUNDMAX */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003719 int af_compminlen; /* COMPOUNDMIN */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003720 int af_compsylmax; /* COMPOUNDSYLMAX */
3721 char_u *af_syllable; /* SYLLABLE */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003722 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003723 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3724 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725} afffile_T;
3726
3727typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003728/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3729struct affentry_S
3730{
3731 affentry_T *ae_next; /* next affix with same name/number */
3732 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3733 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003734 char_u *ae_cond; /* condition (NULL for ".") */
3735 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003736 char_u ae_rare; /* rare affix */
3737 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003738};
3739
Bram Moolenaar53805d12005-08-01 07:08:33 +00003740#define AH_KEY_LEN 10
3741
Bram Moolenaar51485f02005-06-04 21:55:20 +00003742/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3743typedef struct affheader_S
3744{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003745 /* key for hashtable == name of affix entry */
3746#ifdef FEAT_MBYTE
3747 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3748#else
3749 char_u ah_key[2]; /* one byte char plus NUL */
3750#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003751 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003752 int ah_combine; /* suffix may combine with prefix */
3753 affentry_T *ah_first; /* first affix entry */
3754} affheader_T;
3755
3756#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3757
3758/*
3759 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003760 * the need to keep track of each allocated thing, everything is freed all at
3761 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003762 */
3763#define SBLOCKSIZE 16000 /* size of sb_data */
3764typedef struct sblock_S sblock_T;
3765struct sblock_S
3766{
3767 sblock_T *sb_next; /* next block in list */
3768 int sb_used; /* nr of bytes already in use */
3769 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003770};
3771
3772/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003773 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003774 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003775typedef struct wordnode_S wordnode_T;
3776struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003777{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003778 union /* shared to save space */
3779 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003780 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003781 int index; /* index in written nodes (valid after first
3782 round) */
3783 } wn_u1;
3784 union /* shared to save space */
3785 {
3786 wordnode_T *next; /* next node with same hash key */
3787 wordnode_T *wnode; /* parent node that will write this node */
3788 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003789 wordnode_T *wn_child; /* child (next byte in word) */
3790 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3791 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003792 int wn_refs; /* Nr. of references to this node. Only
3793 relevant for first node in a list of
3794 siblings, in following siblings it is
3795 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003796 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003797 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003798 prefix ID or 0 */
3799 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3800 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003801 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003802#ifdef SPELL_PRINTTREE
3803 int wn_nr; /* sequence nr for printing */
3804#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003805};
3806
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003807#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3808
Bram Moolenaar51485f02005-06-04 21:55:20 +00003809#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003810
Bram Moolenaar51485f02005-06-04 21:55:20 +00003811/*
3812 * Info used while reading the spell files.
3813 */
3814typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003815{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003816 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003817 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003818
Bram Moolenaar51485f02005-06-04 21:55:20 +00003819 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003820 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003821
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003822 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003823
Bram Moolenaar51485f02005-06-04 21:55:20 +00003824 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003825 long si_blocks_cnt; /* memory blocks allocated */
3826 long si_compress_cnt; /* words to add before lowering
3827 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003828 wordnode_T *si_first_free; /* List of nodes that have been freed during
3829 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003830 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831#ifdef SPELL_PRINTTREE
3832 int si_wordnode_nr; /* sequence nr for nodes */
3833#endif
3834
3835
Bram Moolenaar51485f02005-06-04 21:55:20 +00003836 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003837 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003838 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003839 int si_region; /* region mask */
3840 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003841 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003842 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003843 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003844 int si_region_count; /* number of regions supported (1 when there
3845 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003846 char_u si_region_name[16]; /* region names; used only if
3847 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003848
3849 garray_T si_rep; /* list of fromto_T entries from REP lines */
3850 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003851 char_u *si_sofofr; /* SOFOFROM text */
3852 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003853 int si_followup; /* soundsalike: ? */
3854 int si_collapse; /* soundsalike: ? */
3855 int si_rem_accents; /* soundsalike: remove accents */
3856 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003857 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003858 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003859 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003860 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003861 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003862 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003863 garray_T si_prefcond; /* table with conditions for postponed
3864 * prefixes, each stored as a string */
3865 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003866} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003867
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003868static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003869static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003870static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3871static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003872static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003873static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003874static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00003875static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
3876static void get_compflags __ARGS((spellinfo_T *spin, char_u *afflist, char_u *store_afflist));
3877static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003878static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3879static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3880static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003881static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003882static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00003883static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003884static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003885static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3886static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3887static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3888static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3889static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003890static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003891static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003892static void clear_node __ARGS((wordnode_T *node));
3893static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003894static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003895static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003896
Bram Moolenaar53805d12005-08-01 07:08:33 +00003897/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3898 * but it must be negative to indicate the prefix tree to tree_add_word().
3899 * Use a negative number with the lower 8 bits zero. */
3900#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003901
Bram Moolenaar5195e452005-08-19 20:32:47 +00003902/*
3903 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
3904 */
3905static long compress_start = 30000; /* memory / SBLOCKSIZE */
3906static long compress_inc = 100; /* memory / SBLOCKSIZE */
3907static long compress_added = 500000; /* word count */
3908
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003909#ifdef SPELL_PRINTTREE
3910/*
3911 * For debugging the tree code: print the current tree in a (more or less)
3912 * readable format, so that we can see what happens when adding a word and/or
3913 * compressing the tree.
3914 * Based on code from Olaf Seibert.
3915 */
3916#define PRINTLINESIZE 1000
3917#define PRINTWIDTH 6
3918
3919#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3920 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3921
3922static char line1[PRINTLINESIZE];
3923static char line2[PRINTLINESIZE];
3924static char line3[PRINTLINESIZE];
3925
3926 static void
3927spell_clear_flags(wordnode_T *node)
3928{
3929 wordnode_T *np;
3930
3931 for (np = node; np != NULL; np = np->wn_sibling)
3932 {
3933 np->wn_u1.index = FALSE;
3934 spell_clear_flags(np->wn_child);
3935 }
3936}
3937
3938 static void
3939spell_print_node(wordnode_T *node, int depth)
3940{
3941 if (node->wn_u1.index)
3942 {
3943 /* Done this node before, print the reference. */
3944 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3945 PRINTSOME(line2, depth, " ", 0, 0);
3946 PRINTSOME(line3, depth, " ", 0, 0);
3947 msg(line1);
3948 msg(line2);
3949 msg(line3);
3950 }
3951 else
3952 {
3953 node->wn_u1.index = TRUE;
3954
3955 if (node->wn_byte != NUL)
3956 {
3957 if (node->wn_child != NULL)
3958 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3959 else
3960 /* Cannot happen? */
3961 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3962 }
3963 else
3964 PRINTSOME(line1, depth, " $ ", 0, 0);
3965
3966 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3967
3968 if (node->wn_sibling != NULL)
3969 PRINTSOME(line3, depth, " | ", 0, 0);
3970 else
3971 PRINTSOME(line3, depth, " ", 0, 0);
3972
3973 if (node->wn_byte == NUL)
3974 {
3975 msg(line1);
3976 msg(line2);
3977 msg(line3);
3978 }
3979
3980 /* do the children */
3981 if (node->wn_byte != NUL && node->wn_child != NULL)
3982 spell_print_node(node->wn_child, depth + 1);
3983
3984 /* do the siblings */
3985 if (node->wn_sibling != NULL)
3986 {
3987 /* get rid of all parent details except | */
3988 STRCPY(line1, line3);
3989 STRCPY(line2, line3);
3990 spell_print_node(node->wn_sibling, depth);
3991 }
3992 }
3993}
3994
3995 static void
3996spell_print_tree(wordnode_T *root)
3997{
3998 if (root != NULL)
3999 {
4000 /* Clear the "wn_u1.index" fields, used to remember what has been
4001 * done. */
4002 spell_clear_flags(root);
4003
4004 /* Recursively print the tree. */
4005 spell_print_node(root, 0);
4006 }
4007}
4008#endif /* SPELL_PRINTTREE */
4009
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004010/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004011 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004012 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004013 */
4014 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004015spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004016 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004017 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004018{
4019 FILE *fd;
4020 afffile_T *aff;
4021 char_u rline[MAXLINELEN];
4022 char_u *line;
4023 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004024#define MAXITEMCNT 7
4025 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004026 int itemcnt;
4027 char_u *p;
4028 int lnum = 0;
4029 affheader_T *cur_aff = NULL;
4030 int aff_todo = 0;
4031 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004032 char_u *low = NULL;
4033 char_u *fol = NULL;
4034 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004035 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004036 int do_rep;
4037 int do_sal;
4038 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004039 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004040 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004041 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004042 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004043 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004044
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045 /*
4046 * Open the file.
4047 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004048 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004049 if (fd == NULL)
4050 {
4051 EMSG2(_(e_notopen), fname);
4052 return NULL;
4053 }
4054
Bram Moolenaarb765d632005-06-07 21:00:02 +00004055 if (spin->si_verbose || p_verbose > 2)
4056 {
4057 if (!spin->si_verbose)
4058 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004059 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004060 out_flush();
4061 if (!spin->si_verbose)
4062 verbose_leave();
4063 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004065 /* Only do REP lines when not done in another .aff file already. */
4066 do_rep = spin->si_rep.ga_len == 0;
4067
4068 /* Only do SAL lines when not done in another .aff file already. */
4069 do_sal = spin->si_sal.ga_len == 0;
4070
4071 /* Only do MAP lines when not done in another .aff file already. */
4072 do_map = spin->si_map.ga_len == 0;
4073
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004074 /* Only do MIDWORD line when not done in another .aff file already */
4075 do_midword = spin->si_midword == NULL;
4076
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004077 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
4078 do_sofo = spin->si_sofofr == NULL;
4079
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 /*
4081 * Allocate and init the afffile_T structure.
4082 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004083 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084 if (aff == NULL)
4085 return NULL;
4086 hash_init(&aff->af_pref);
4087 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088
4089 /*
4090 * Read all the lines in the file one by one.
4091 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004092 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004094 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095 ++lnum;
4096
4097 /* Skip comment lines. */
4098 if (*rline == '#')
4099 continue;
4100
4101 /* Convert from "SET" to 'encoding' when needed. */
4102 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004103#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004104 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004105 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004106 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004107 if (pc == NULL)
4108 {
4109 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4110 fname, lnum, rline);
4111 continue;
4112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004113 line = pc;
4114 }
4115 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004116#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004117 {
4118 pc = NULL;
4119 line = rline;
4120 }
4121
4122 /* Split the line up in white separated items. Put a NUL after each
4123 * item. */
4124 itemcnt = 0;
4125 for (p = line; ; )
4126 {
4127 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4128 ++p;
4129 if (*p == NUL)
4130 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004131 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004132 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004134 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004135 ++p;
4136 if (*p == NUL)
4137 break;
4138 *p++ = NUL;
4139 }
4140
4141 /* Handle non-empty lines. */
4142 if (itemcnt > 0)
4143 {
4144 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4145 && aff->af_enc == NULL)
4146 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004147#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004148 /* Setup for conversion from "ENC" to 'encoding'. */
4149 aff->af_enc = enc_canonize(items[1]);
4150 if (aff->af_enc != NULL && !spin->si_ascii
4151 && convert_setup(&spin->si_conv, aff->af_enc,
4152 p_enc) == FAIL)
4153 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4154 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004155 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004156#else
4157 smsg((char_u *)_("Conversion in %s not supported"), fname);
4158#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004159 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004160 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
4161 {
4162 if (do_midword)
4163 spin->si_midword = vim_strsave(items[1]);
4164 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004165 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004172 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004173 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4174 && aff->af_slash == 0)
4175 {
4176 aff->af_slash = items[1][0];
4177 if (items[1][1] != NUL)
4178 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4179 fname, lnum, items[1]);
4180 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004181 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4182 && aff->af_rar == 0)
4183 {
4184 aff->af_rar = items[1][0];
4185 if (items[1][1] != NUL)
4186 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4187 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004188 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4189 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004190 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004191 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004192 if (items[1][1] != NUL)
4193 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4194 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004195 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4196 && aff->af_bad == 0)
4197 {
4198 aff->af_bad = items[1][0];
4199 if (items[1][1] != NUL)
4200 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4201 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004202 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4203 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004204 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004205 aff->af_needaffix = items[1][0];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004206 if (items[1][1] != NUL)
4207 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4208 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004209 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
4210 && aff->af_compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004211 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004212 p = getroom(spin, 3, FALSE);
4213 if (p != NULL)
4214 {
4215 /* Turn single flag "c" into COMPOUNDFLAGS compatible
4216 * string "c+". */
4217 p[0] = items[1][0];
4218 p[1] = '+';
4219 p[2] = NUL;
4220 aff->af_compflags = p;
4221 }
4222 if (items[1][1] != NUL)
4223 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4224 }
4225 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4226 {
4227 /* Concatenate this string to previously defined ones, using a
4228 * slash to separate them. */
4229 l = STRLEN(items[1]) + 1;
4230 if (aff->af_compflags != NULL)
4231 l += STRLEN(aff->af_compflags) + 1;
4232 p = getroom(spin, l, FALSE);
4233 if (p != NULL)
4234 {
4235 if (aff->af_compflags != NULL)
4236 {
4237 STRCPY(p, aff->af_compflags);
4238 STRCAT(p, "/");
4239 }
4240 STRCAT(p, items[1]);
4241 aff->af_compflags = p;
4242 }
4243 }
4244 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
4245 && aff->af_compmax == 0)
4246 {
4247 aff->af_compmax = atoi((char *)items[1]);
4248 if (aff->af_compmax == 0)
4249 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4250 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004251 }
4252 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
4253 && aff->af_compminlen == 0)
4254 {
4255 aff->af_compminlen = atoi((char *)items[1]);
4256 if (aff->af_compminlen == 0)
4257 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4258 fname, lnum, items[1]);
4259 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004260 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
4261 && aff->af_compsylmax == 0)
4262 {
4263 aff->af_compsylmax = atoi((char *)items[1]);
4264 if (aff->af_compsylmax == 0)
4265 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4266 fname, lnum, items[1]);
4267 }
4268 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
4269 && aff->af_syllable == NULL)
4270 {
4271 aff->af_syllable = getroom_save(spin, items[1]);
4272 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004273 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4274 {
4275 aff->af_pfxpostpone = TRUE;
4276 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004277 else if ((STRCMP(items[0], "PFX") == 0
4278 || STRCMP(items[0], "SFX") == 0)
4279 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004280 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004281 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00004282 /* Myspell allows extra text after the item, but that might
4283 * mean mistakes go unnoticed. Require a comment-starter. */
4284 if (itemcnt > 4 && *items[4] != '#')
4285 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4286 fname, lnum, items[4]);
4287
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004288 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004289 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004290 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291 if (cur_aff == NULL)
4292 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004293#ifdef FEAT_MBYTE
4294 if (has_mbyte)
4295 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004296 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004297 if (l >= AH_KEY_LEN)
4298 l = 1; /* too long, must be an overlong sequence */
4299 else
4300 mch_memmove(cur_aff->ah_key, items[1], l);
4301 }
4302 else
4303#endif
4304 {
4305 *cur_aff->ah_key = *items[1];
4306 l = 1;
4307 }
4308 cur_aff->ah_key[l] = NUL;
4309 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004310 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004311 if (*items[2] == 'Y')
4312 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004313 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004314 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4315 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004316
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004317 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004318 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004319 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004320 /* Use a new number in the .spl file later, to be able to
4321 * handle multiple .aff files. */
4322 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004323 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004324 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004325 else
4326 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004327 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004328 hi = hash_find(tp, cur_aff->ah_key);
4329 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00004330 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004331 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4332 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004333 aff_todo = 0;
4334 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004335 else
4336 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004337 }
4338 else if ((STRCMP(items[0], "PFX") == 0
4339 || STRCMP(items[0], "SFX") == 0)
4340 && aff_todo > 0
4341 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004342 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004343 {
4344 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004345 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004346 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004347 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004348 int lasti = 5;
4349
Bram Moolenaar5195e452005-08-19 20:32:47 +00004350 /* Check for "rare" and "nocomp" after the other info. */
4351 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004352 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004353 if (!rare && STRICMP(items[lasti], "rare") == 0)
4354 {
4355 rare = TRUE;
4356 ++lasti;
4357 }
4358 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4359 {
4360 nocomp = TRUE;
4361 ++lasti;
4362 }
4363 else
4364 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004365 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004366
Bram Moolenaar8db73182005-06-17 21:51:16 +00004367 /* Myspell allows extra text after the item, but that might
4368 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004369 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004370 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004371
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004372 /* New item for an affix letter. */
4373 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004374 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004375 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004376 if (aff_entry == NULL)
4377 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004378 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004379 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004381 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004382 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004383 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004384 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004385
Bram Moolenaar51485f02005-06-04 21:55:20 +00004386 /* Don't use an affix entry with non-ASCII characters when
4387 * "spin->si_ascii" is TRUE. */
4388 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004389 || has_non_ascii(aff_entry->ae_add)))
4390 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004391 aff_entry->ae_next = cur_aff->ah_first;
4392 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004393
4394 if (STRCMP(items[4], ".") != 0)
4395 {
4396 char_u buf[MAXLINELEN];
4397
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004398 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004399 if (*items[0] == 'P')
4400 sprintf((char *)buf, "^%s", items[4]);
4401 else
4402 sprintf((char *)buf, "%s$", items[4]);
4403 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004404 RE_MAGIC + RE_STRING + RE_STRICT);
4405 if (aff_entry->ae_prog == NULL)
4406 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4407 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004408 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004409
4410 /* For postponed prefixes we need an entry in si_prefcond
4411 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004412 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004413 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004414 /* When the chop string is one lower-case letter and
4415 * the add string ends in the upper-case letter we set
4416 * the "upper" flag, clear "ae_chop" and remove the
4417 * letters from "ae_add". The condition must either
4418 * be empty or start with the same letter. */
4419 if (aff_entry->ae_chop != NULL
4420 && aff_entry->ae_add != NULL
4421#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004422 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004423 aff_entry->ae_chop)] == NUL
4424#else
4425 && aff_entry->ae_chop[1] == NUL
4426#endif
4427 )
4428 {
4429 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004430
Bram Moolenaar53805d12005-08-01 07:08:33 +00004431 c = PTR2CHAR(aff_entry->ae_chop);
4432 c_up = SPELL_TOUPPER(c);
4433 if (c_up != c
4434 && (aff_entry->ae_cond == NULL
4435 || PTR2CHAR(aff_entry->ae_cond) == c))
4436 {
4437 p = aff_entry->ae_add
4438 + STRLEN(aff_entry->ae_add);
4439 mb_ptr_back(aff_entry->ae_add, p);
4440 if (PTR2CHAR(p) == c_up)
4441 {
4442 upper = TRUE;
4443 aff_entry->ae_chop = NULL;
4444 *p = NUL;
4445
4446 /* The condition is matched with the
4447 * actual word, thus must check for the
4448 * upper-case letter. */
4449 if (aff_entry->ae_cond != NULL)
4450 {
4451 char_u buf[MAXLINELEN];
4452#ifdef FEAT_MBYTE
4453 if (has_mbyte)
4454 {
4455 onecap_copy(items[4], buf, TRUE);
4456 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004457 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004458 }
4459 else
4460#endif
4461 *aff_entry->ae_cond = c_up;
4462 if (aff_entry->ae_cond != NULL)
4463 {
4464 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004465 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004466 vim_free(aff_entry->ae_prog);
4467 aff_entry->ae_prog = vim_regcomp(
4468 buf, RE_MAGIC + RE_STRING);
4469 }
4470 }
4471 }
4472 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004473 }
4474
Bram Moolenaar53805d12005-08-01 07:08:33 +00004475 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004476 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004477 int idx;
4478 char_u **pp;
4479 int n;
4480
4481 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4482 --idx)
4483 {
4484 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4485 if (str_equal(p, aff_entry->ae_cond))
4486 break;
4487 }
4488 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4489 {
4490 /* Not found, add a new condition. */
4491 idx = spin->si_prefcond.ga_len++;
4492 pp = ((char_u **)spin->si_prefcond.ga_data)
4493 + idx;
4494 if (aff_entry->ae_cond == NULL)
4495 *pp = NULL;
4496 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004497 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004498 aff_entry->ae_cond);
4499 }
4500
4501 /* Add the prefix to the prefix tree. */
4502 if (aff_entry->ae_add == NULL)
4503 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004504 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004505 p = aff_entry->ae_add;
4506 /* PFX_FLAGS is a negative number, so that
4507 * tree_add_word() knows this is the prefix tree. */
4508 n = PFX_FLAGS;
4509 if (rare)
4510 n |= WFP_RARE;
4511 if (!cur_aff->ah_combine)
4512 n |= WFP_NC;
4513 if (upper)
4514 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004515 tree_add_word(spin, p, spin->si_prefroot, n,
4516 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004517 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004518 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004519 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004520 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004521 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
4522 {
4523 if (fol != NULL)
4524 smsg((char_u *)_("Duplicate FOL in %s line %d"),
4525 fname, lnum);
4526 else
4527 fol = vim_strsave(items[1]);
4528 }
4529 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
4530 {
4531 if (low != NULL)
4532 smsg((char_u *)_("Duplicate LOW in %s line %d"),
4533 fname, lnum);
4534 else
4535 low = vim_strsave(items[1]);
4536 }
4537 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
4538 {
4539 if (upp != NULL)
4540 smsg((char_u *)_("Duplicate UPP in %s line %d"),
4541 fname, lnum);
4542 else
4543 upp = vim_strsave(items[1]);
4544 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004545 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004546 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004547 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004548 if (!isdigit(*items[1]))
4549 smsg((char_u *)_("Expected REP count in %s line %d"),
4550 fname, lnum);
4551 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004552 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004553 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004554 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004555 /* Myspell ignores extra arguments, we require it starts with
4556 * # to detect mistakes. */
4557 if (itemcnt > 3 && items[3][0] != '#')
4558 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 if (do_rep)
4560 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4561 }
4562 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4563 {
4564 /* MAP item or count */
4565 if (!found_map)
4566 {
4567 /* First line contains the count. */
4568 found_map = TRUE;
4569 if (!isdigit(*items[1]))
4570 smsg((char_u *)_("Expected MAP count in %s line %d"),
4571 fname, lnum);
4572 }
4573 else if (do_map)
4574 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004575 int c;
4576
4577 /* Check that every character appears only once. */
4578 for (p = items[1]; *p != NUL; )
4579 {
4580#ifdef FEAT_MBYTE
4581 c = mb_ptr2char_adv(&p);
4582#else
4583 c = *p++;
4584#endif
4585 if ((spin->si_map.ga_len > 0
4586 && vim_strchr(spin->si_map.ga_data, c)
4587 != NULL)
4588 || vim_strchr(p, c) != NULL)
4589 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4590 fname, lnum);
4591 }
4592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004593 /* We simply concatenate all the MAP strings, separated by
4594 * slashes. */
4595 ga_concat(&spin->si_map, items[1]);
4596 ga_append(&spin->si_map, '/');
4597 }
4598 }
4599 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4600 {
4601 if (do_sal)
4602 {
4603 /* SAL item (sounds-a-like)
4604 * Either one of the known keys or a from-to pair. */
4605 if (STRCMP(items[1], "followup") == 0)
4606 spin->si_followup = sal_to_bool(items[2]);
4607 else if (STRCMP(items[1], "collapse_result") == 0)
4608 spin->si_collapse = sal_to_bool(items[2]);
4609 else if (STRCMP(items[1], "remove_accents") == 0)
4610 spin->si_rem_accents = sal_to_bool(items[2]);
4611 else
4612 /* when "to" is "_" it means empty */
4613 add_fromto(spin, &spin->si_sal, items[1],
4614 STRCMP(items[2], "_") == 0 ? (char_u *)""
4615 : items[2]);
4616 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004617 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004618 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
4619 && (!do_sofo || spin->si_sofofr == NULL))
4620 {
4621 if (do_sofo)
4622 spin->si_sofofr = vim_strsave(items[1]);
4623 }
4624 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
4625 && (!do_sofo || spin->si_sofoto == NULL))
4626 {
4627 if (do_sofo)
4628 spin->si_sofoto = vim_strsave(items[1]);
4629 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004630 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004631 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004632 fname, lnum, items[0]);
4633 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634 }
4635
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004636 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
4637 smsg((char_u *)_("Missing SOFO%s line in %s"),
4638 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
4639 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
4640 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
4641
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004642 if (fol != NULL || low != NULL || upp != NULL)
4643 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004644 if (spin->si_clear_chartab)
4645 {
4646 /* Clear the char type tables, don't want to use any of the
4647 * currently used spell properties. */
4648 init_spell_chartab();
4649 spin->si_clear_chartab = FALSE;
4650 }
4651
Bram Moolenaar3982c542005-06-08 21:56:31 +00004652 /*
4653 * Don't write a word table for an ASCII file, so that we don't check
4654 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004655 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004656 * mb_get_class(), the list of chars in the file will be incomplete.
4657 */
4658 if (!spin->si_ascii
4659#ifdef FEAT_MBYTE
4660 && !enc_utf8
4661#endif
4662 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004663 {
4664 if (fol == NULL || low == NULL || upp == NULL)
4665 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4666 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004667 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004668 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004669
4670 vim_free(fol);
4671 vim_free(low);
4672 vim_free(upp);
4673 }
4674
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004675 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004676 if (aff->af_compmax != 0)
4677 {
4678 if (spin->si_compmax != 0 && spin->si_compmax != aff->af_compmax)
4679 smsg((char_u *)_("COMPOUNDMAX value differs from what is used in another .aff file"));
4680 else
4681 spin->si_compmax = aff->af_compmax;
4682 }
4683
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004684 if (aff->af_compminlen != 0)
4685 {
4686 if (spin->si_compminlen != 0
4687 && spin->si_compminlen != aff->af_compminlen)
4688 smsg((char_u *)_("COMPOUNDMIN value differs from what is used in another .aff file"));
4689 else
4690 spin->si_compminlen = aff->af_compminlen;
4691 }
4692
Bram Moolenaar5195e452005-08-19 20:32:47 +00004693 if (aff->af_compsylmax != 0)
4694 {
4695 if (aff->af_syllable == NULL)
4696 smsg((char_u *)_("COMPOUNDSYLMAX without SYLLABLE"));
4697
4698 if (spin->si_compsylmax != 0
4699 && spin->si_compsylmax != aff->af_compsylmax)
4700 smsg((char_u *)_("COMPOUNDSYLMAX value differs from what is used in another .aff file"));
4701 else
4702 spin->si_compsylmax = aff->af_compsylmax;
4703 }
4704
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004705 if (aff->af_compflags != NULL)
4706 {
4707 if (spin->si_compflags != NULL
4708 && STRCMP(spin->si_compflags, aff->af_compflags) != 0)
4709 smsg((char_u *)_("COMPOUNDFLAG(S) value differs from what is used in another .aff file"));
4710 else
4711 spin->si_compflags = aff->af_compflags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004712 }
4713
Bram Moolenaar5195e452005-08-19 20:32:47 +00004714 if (aff->af_syllable != NULL)
4715 {
4716 if (spin->si_syllable != NULL
4717 && STRCMP(spin->si_syllable, aff->af_syllable) != 0)
4718 smsg((char_u *)_("SYLLABLE value differs from what is used in another .aff file"));
4719 else
4720 spin->si_syllable = aff->af_syllable;
4721 }
4722
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 vim_free(pc);
4724 fclose(fd);
4725 return aff;
4726}
4727
4728/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004729 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
4730 * NULL as equal.
4731 */
4732 static int
4733str_equal(s1, s2)
4734 char_u *s1;
4735 char_u *s2;
4736{
4737 if (s1 == NULL || s2 == NULL)
4738 return s1 == s2;
4739 return STRCMP(s1, s2) == 0;
4740}
4741
4742/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004743 * Add a from-to item to "gap". Used for REP and SAL items.
4744 * They are stored case-folded.
4745 */
4746 static void
4747add_fromto(spin, gap, from, to)
4748 spellinfo_T *spin;
4749 garray_T *gap;
4750 char_u *from;
4751 char_u *to;
4752{
4753 fromto_T *ftp;
4754 char_u word[MAXWLEN];
4755
4756 if (ga_grow(gap, 1) == OK)
4757 {
4758 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
4759 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004760 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004762 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 ++gap->ga_len;
4764 }
4765}
4766
4767/*
4768 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4769 */
4770 static int
4771sal_to_bool(s)
4772 char_u *s;
4773{
4774 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4775}
4776
4777/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004778 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4779 * When "s" is NULL FALSE is returned.
4780 */
4781 static int
4782has_non_ascii(s)
4783 char_u *s;
4784{
4785 char_u *p;
4786
4787 if (s != NULL)
4788 for (p = s; *p != NUL; ++p)
4789 if (*p >= 128)
4790 return TRUE;
4791 return FALSE;
4792}
4793
4794/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004795 * Free the structure filled by spell_read_aff().
4796 */
4797 static void
4798spell_free_aff(aff)
4799 afffile_T *aff;
4800{
4801 hashtab_T *ht;
4802 hashitem_T *hi;
4803 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004804 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004805 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806
4807 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004808
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004809 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004810 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4811 {
4812 todo = ht->ht_used;
4813 for (hi = ht->ht_array; todo > 0; ++hi)
4814 {
4815 if (!HASHITEM_EMPTY(hi))
4816 {
4817 --todo;
4818 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004819 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4820 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 }
4822 }
4823 if (ht == &aff->af_suff)
4824 break;
4825 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004826
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004827 hash_clear(&aff->af_pref);
4828 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004829}
4830
4831/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004832 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004833 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004834 */
4835 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004836spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004837 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004838 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004839 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004840{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004841 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004842 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004843 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004844 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004845 char_u store_afflist[MAXWLEN];
4846 int pfxlen;
4847 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004848 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004849 char_u *pc;
4850 char_u *w;
4851 int l;
4852 hash_T hash;
4853 hashitem_T *hi;
4854 FILE *fd;
4855 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004856 int non_ascii = 0;
4857 int retval = OK;
4858 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004859 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004860 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004861
Bram Moolenaar51485f02005-06-04 21:55:20 +00004862 /*
4863 * Open the file.
4864 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004865 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004866 if (fd == NULL)
4867 {
4868 EMSG2(_(e_notopen), fname);
4869 return FAIL;
4870 }
4871
Bram Moolenaar51485f02005-06-04 21:55:20 +00004872 /* The hashtable is only used to detect duplicated words. */
4873 hash_init(&ht);
4874
Bram Moolenaarb765d632005-06-07 21:00:02 +00004875 if (spin->si_verbose || p_verbose > 2)
4876 {
4877 if (!spin->si_verbose)
4878 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004879 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004880 out_flush();
4881 if (!spin->si_verbose)
4882 verbose_leave();
4883 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004884
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004885 /* start with a message for the first line */
4886 spin->si_msg_count = 999999;
4887
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004888 /* Read and ignore the first line: word count. */
4889 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004890 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004891 EMSG2(_("E760: No word count in %s"), fname);
4892
4893 /*
4894 * Read all the lines in the file one by one.
4895 * The words are converted to 'encoding' here, before being added to
4896 * the hashtable.
4897 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004898 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004899 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004900 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004901 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004902 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004903 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004904
Bram Moolenaar51485f02005-06-04 21:55:20 +00004905 /* Remove CR, LF and white space from the end. White space halfway
4906 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004907 l = STRLEN(line);
4908 while (l > 0 && line[l - 1] <= ' ')
4909 --l;
4910 if (l == 0)
4911 continue; /* empty line */
4912 line[l] = NUL;
4913
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004914 /* Find the optional affix names. Replace the SLASH character by a
4915 * slash. */
4916 afflist = NULL;
4917 for (p = line; *p != NUL; mb_ptr_adv(p))
4918 {
4919 if (*p == affile->af_slash)
4920 *p = '/';
4921 else if (*p == '/')
4922 {
4923 *p = NUL;
4924 afflist = p + 1;
4925 break;
4926 }
4927 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004928
4929 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4930 if (spin->si_ascii && has_non_ascii(line))
4931 {
4932 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004933 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004934 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004935
Bram Moolenaarb765d632005-06-07 21:00:02 +00004936#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004937 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004938 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004939 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004940 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004941 if (pc == NULL)
4942 {
4943 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4944 fname, lnum, line);
4945 continue;
4946 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004947 w = pc;
4948 }
4949 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004950#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004951 {
4952 pc = NULL;
4953 w = line;
4954 }
4955
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004956 /* This takes time, print a message every 10000 words. */
4957 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004958 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004959 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004960 vim_snprintf((char *)message, sizeof(message),
4961 _("line %6d, word %6d - %s"),
4962 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4963 msg_start();
4964 msg_puts_long_attr(message, 0);
4965 msg_clr_eos();
4966 msg_didout = FALSE;
4967 msg_col = 0;
4968 out_flush();
4969 }
4970
Bram Moolenaar51485f02005-06-04 21:55:20 +00004971 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004972 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004973 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004974 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004975 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004976 if (retval == FAIL)
4977 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004978
Bram Moolenaar51485f02005-06-04 21:55:20 +00004979 hash = hash_hash(dw);
4980 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004981 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004982 {
4983 if (p_verbose > 0)
4984 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004985 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004986 else if (duplicate == 0)
4987 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
4988 fname, lnum, dw);
4989 ++duplicate;
4990 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004991 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004992 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004993
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004994 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004995 store_afflist[0] = NUL;
4996 pfxlen = 0;
4997 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004998 if (afflist != NULL)
4999 {
5000 /* Check for affix name that stands for keep-case word and stands
5001 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005002 if (affile->af_kep != NUL
5003 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005004 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005005 if (affile->af_rar != NUL
5006 && vim_strchr(afflist, affile->af_rar) != NULL)
5007 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005008 if (affile->af_bad != NUL
5009 && vim_strchr(afflist, affile->af_bad) != NULL)
5010 flags |= WF_BANNED;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005011 if (affile->af_needaffix != NUL
5012 && vim_strchr(afflist, affile->af_needaffix) != NULL)
5013 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005014
5015 if (affile->af_pfxpostpone)
5016 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005017 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005018
Bram Moolenaar5195e452005-08-19 20:32:47 +00005019 if (spin->si_compflags != NULL)
5020 /* Need to store the list of compound flags with the word.
5021 * Concatenate them to the list of prefix IDs. */
5022 get_compflags(spin, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005023 }
5024
Bram Moolenaar51485f02005-06-04 21:55:20 +00005025 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005026 if (store_word(spin, dw, flags, spin->si_region,
5027 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005028 retval = FAIL;
5029
5030 if (afflist != NULL)
5031 {
5032 /* Find all matching suffixes and add the resulting words.
5033 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005034 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005035 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005036 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005037 retval = FAIL;
5038
5039 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005040 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005041 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005042 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005043 retval = FAIL;
5044 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005045 }
5046
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005047 if (duplicate > 0)
5048 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005049 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005050 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5051 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005052 hash_clear(&ht);
5053
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005054 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005055 return retval;
5056}
5057
5058/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005059 * Get the list of prefix IDs from the affix list "afflist".
5060 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005061 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5062 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005063 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005064 static int
5065get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005066 afffile_T *affile;
5067 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005068 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005069{
5070 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005071 int cnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005072 char_u key[2];
5073 hashitem_T *hi;
5074
5075 key[1] = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005076 for (p = afflist; *p != NUL; ++p)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005077 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005078 key[0] = *p;
5079 hi = hash_find(&affile->af_pref, key);
5080 if (!HASHITEM_EMPTY(hi))
5081 /* This is a prefix ID, use the new number. */
5082 store_afflist[cnt++] = HI2AH(hi)->ah_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005083 }
5084
Bram Moolenaar5195e452005-08-19 20:32:47 +00005085 store_afflist[cnt] = NUL;
5086 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005087}
5088
5089/*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005090 * Get the list of affix IDs from the affix list "afflist" that are used for
5091 * compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005092 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005093 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005094 static void
5095get_compflags(spin, afflist, store_afflist)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005096 spellinfo_T *spin;
5097 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005098 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005099{
5100 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005101 int cnt = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005102
Bram Moolenaar5195e452005-08-19 20:32:47 +00005103 for (p = afflist; *p != NUL; ++p)
5104 /* A flag is a compound flag if it appears in "si_compflags" and
5105 * it's not a special character. */
5106 if (vim_strchr(spin->si_compflags, *p) != NULL
5107 && vim_strchr((char_u *)"+*[]/", *p) == NULL)
5108 store_afflist[cnt++] = *p;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005109
Bram Moolenaar5195e452005-08-19 20:32:47 +00005110 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005111}
5112
5113/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005114 * Apply affixes to a word and store the resulting words.
5115 * "ht" is the hashtable with affentry_T that need to be applied, either
5116 * prefixes or suffixes.
5117 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5118 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005119 *
5120 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005121 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005122 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005123store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5124 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005125 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005126 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005127 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005128 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005129 hashtab_T *ht;
5130 hashtab_T *xht;
5131 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005132 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005133 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005134 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5135 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005136{
5137 int todo;
5138 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005139 affheader_T *ah;
5140 affentry_T *ae;
5141 regmatch_T regmatch;
5142 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005143 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005144 int i;
5145 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005146 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005147 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005148 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00005149 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005150 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005151
Bram Moolenaar51485f02005-06-04 21:55:20 +00005152 todo = ht->ht_used;
5153 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005154 {
5155 if (!HASHITEM_EMPTY(hi))
5156 {
5157 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005158 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005159
Bram Moolenaar51485f02005-06-04 21:55:20 +00005160 /* Check that the affix combines, if required, and that the word
5161 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005162 c = PTR2CHAR(ah->ah_key);
5163 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005164 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005165 /* Loop over all affix entries with this name. */
5166 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005167 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005168 /* Check the condition. It's not logical to match case
5169 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005170 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005171 * Another requirement from Myspell is that the chop
5172 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005173 * For prefixes, when "PFXPOSTPONE" was used, only do
5174 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005175 regmatch.regprog = ae->ae_prog;
5176 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005177 if ((xht != NULL || !affile->af_pfxpostpone
5178 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005179 && (ae->ae_chop == NULL
5180 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005181 && (ae->ae_prog == NULL
5182 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005183 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005184 /* Match. Remove the chop and add the affix. */
5185 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005186 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005187 /* prefix: chop/add at the start of the word */
5188 if (ae->ae_add == NULL)
5189 *newword = NUL;
5190 else
5191 STRCPY(newword, ae->ae_add);
5192 p = word;
5193 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005194 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005195 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005196#ifdef FEAT_MBYTE
5197 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005198 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005199 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005200 for ( ; i > 0; --i)
5201 mb_ptr_adv(p);
5202 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005203 else
5204#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005205 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005206 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005207 STRCAT(newword, p);
5208 }
5209 else
5210 {
5211 /* suffix: chop/add at the end of the word */
5212 STRCPY(newword, word);
5213 if (ae->ae_chop != NULL)
5214 {
5215 /* Remove chop string. */
5216 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005217 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005218 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005219 mb_ptr_back(newword, p);
5220 *p = NUL;
5221 }
5222 if (ae->ae_add != NULL)
5223 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005224 }
5225
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005226 /* Obey the "rare" flag of the affix. */
5227 if (ae->ae_rare)
5228 use_flags = flags | WF_RARE;
5229 else
5230 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005231
5232 /* Obey the "nocomp" flag of the affix: don't use the
5233 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005234 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005235 if (ae->ae_nocomp && pfxlist != NULL)
5236 {
5237 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5238 use_pfxlist = pfx_pfxlist;
5239 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005240
5241 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005242 if (spin->si_prefroot != NULL
5243 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005244 {
5245 /* ... add a flag to indicate an affix was used. */
5246 use_flags |= WF_HAS_AFF;
5247
5248 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005249 * affixes is not allowed. But do use the
5250 * compound flags after them. */
5251 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5252 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005253 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005254
Bram Moolenaar51485f02005-06-04 21:55:20 +00005255 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005256 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005257 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005258 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259
Bram Moolenaar51485f02005-06-04 21:55:20 +00005260 /* When added a suffix and combining is allowed also
5261 * try adding prefixes additionally. */
5262 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005263 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005264 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005265 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005266 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005267 }
5268 }
5269 }
5270 }
5271 }
5272
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005273 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005274}
5275
5276/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005277 * Read a file with a list of words.
5278 */
5279 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005280spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005281 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005282 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005283{
5284 FILE *fd;
5285 long lnum = 0;
5286 char_u rline[MAXLINELEN];
5287 char_u *line;
5288 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005289 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005290 int l;
5291 int retval = OK;
5292 int did_word = FALSE;
5293 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005294 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005295 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005296
5297 /*
5298 * Open the file.
5299 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005300 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005301 if (fd == NULL)
5302 {
5303 EMSG2(_(e_notopen), fname);
5304 return FAIL;
5305 }
5306
Bram Moolenaarb765d632005-06-07 21:00:02 +00005307 if (spin->si_verbose || p_verbose > 2)
5308 {
5309 if (!spin->si_verbose)
5310 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005311 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005312 out_flush();
5313 if (!spin->si_verbose)
5314 verbose_leave();
5315 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005316
5317 /*
5318 * Read all the lines in the file one by one.
5319 */
5320 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5321 {
5322 line_breakcheck();
5323 ++lnum;
5324
5325 /* Skip comment lines. */
5326 if (*rline == '#')
5327 continue;
5328
5329 /* Remove CR, LF and white space from the end. */
5330 l = STRLEN(rline);
5331 while (l > 0 && rline[l - 1] <= ' ')
5332 --l;
5333 if (l == 0)
5334 continue; /* empty or blank line */
5335 rline[l] = NUL;
5336
5337 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
5338 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005339#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005340 if (spin->si_conv.vc_type != CONV_NONE)
5341 {
5342 pc = string_convert(&spin->si_conv, rline, NULL);
5343 if (pc == NULL)
5344 {
5345 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5346 fname, lnum, rline);
5347 continue;
5348 }
5349 line = pc;
5350 }
5351 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005352#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005353 {
5354 pc = NULL;
5355 line = rline;
5356 }
5357
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005358 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00005359 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005360 ++line;
5361 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005362 {
5363 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005364 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
5365 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005366 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005367 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
5368 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005369 else
5370 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005371#ifdef FEAT_MBYTE
5372 char_u *enc;
5373
Bram Moolenaar51485f02005-06-04 21:55:20 +00005374 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005375 line += 10;
5376 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005377 if (enc != NULL && !spin->si_ascii
5378 && convert_setup(&spin->si_conv, enc,
5379 p_enc) == FAIL)
5380 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00005381 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005382 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005383 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005384#else
5385 smsg((char_u *)_("Conversion in %s not supported"), fname);
5386#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005387 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005388 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005389 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005390
Bram Moolenaar3982c542005-06-08 21:56:31 +00005391 if (STRNCMP(line, "regions=", 8) == 0)
5392 {
5393 if (spin->si_region_count > 1)
5394 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
5395 fname, lnum, line);
5396 else
5397 {
5398 line += 8;
5399 if (STRLEN(line) > 16)
5400 smsg((char_u *)_("Too many regions in %s line %d: %s"),
5401 fname, lnum, line);
5402 else
5403 {
5404 spin->si_region_count = STRLEN(line) / 2;
5405 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005406
5407 /* Adjust the mask for a word valid in all regions. */
5408 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005409 }
5410 }
5411 continue;
5412 }
5413
Bram Moolenaar7887d882005-07-01 22:33:52 +00005414 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
5415 fname, lnum, line - 1);
5416 continue;
5417 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005418
Bram Moolenaar7887d882005-07-01 22:33:52 +00005419 flags = 0;
5420 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005421
Bram Moolenaar7887d882005-07-01 22:33:52 +00005422 /* Check for flags and region after a slash. */
5423 p = vim_strchr(line, '/');
5424 if (p != NULL)
5425 {
5426 *p++ = NUL;
5427 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005428 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005429 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005430 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005431 else if (*p == '!') /* Bad, bad, wicked word. */
5432 flags |= WF_BANNED;
5433 else if (*p == '?') /* Rare word. */
5434 flags |= WF_RARE;
5435 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005436 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005437 if ((flags & WF_REGION) == 0) /* first one */
5438 regionmask = 0;
5439 flags |= WF_REGION;
5440
5441 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00005442 if (l > spin->si_region_count)
5443 {
5444 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00005445 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005446 break;
5447 }
5448 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005449 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005450 else
5451 {
5452 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
5453 fname, lnum, p);
5454 break;
5455 }
5456 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005457 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005458 }
5459
5460 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5461 if (spin->si_ascii && has_non_ascii(line))
5462 {
5463 ++non_ascii;
5464 continue;
5465 }
5466
5467 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005468 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005469 {
5470 retval = FAIL;
5471 break;
5472 }
5473 did_word = TRUE;
5474 }
5475
5476 vim_free(pc);
5477 fclose(fd);
5478
Bram Moolenaarb765d632005-06-07 21:00:02 +00005479 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
5480 {
5481 if (p_verbose > 2)
5482 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00005483 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
5484 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005485 if (p_verbose > 2)
5486 verbose_leave();
5487 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005488 return retval;
5489}
5490
5491/*
5492 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005493 * This avoids calling free() for every little struct we use (and keeping
5494 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00005495 * The memory is cleared to all zeros.
5496 * Returns NULL when out of memory.
5497 */
5498 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005499getroom(spin, len, align)
5500 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005501 size_t len; /* length needed */
5502 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005503{
5504 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005505 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005506
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005507 if (align && bl != NULL)
5508 /* Round size up for alignment. On some systems structures need to be
5509 * aligned to the size of a pointer (e.g., SPARC). */
5510 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
5511 & ~(sizeof(char *) - 1);
5512
Bram Moolenaar51485f02005-06-04 21:55:20 +00005513 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
5514 {
5515 /* Allocate a block of memory. This is not freed until much later. */
5516 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
5517 if (bl == NULL)
5518 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005519 bl->sb_next = spin->si_blocks;
5520 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005521 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005522 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005523 }
5524
5525 p = bl->sb_data + bl->sb_used;
5526 bl->sb_used += len;
5527
5528 return p;
5529}
5530
5531/*
5532 * Make a copy of a string into memory allocated with getroom().
5533 */
5534 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005535getroom_save(spin, s)
5536 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005537 char_u *s;
5538{
5539 char_u *sc;
5540
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005541 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005542 if (sc != NULL)
5543 STRCPY(sc, s);
5544 return sc;
5545}
5546
5547
5548/*
5549 * Free the list of allocated sblock_T.
5550 */
5551 static void
5552free_blocks(bl)
5553 sblock_T *bl;
5554{
5555 sblock_T *next;
5556
5557 while (bl != NULL)
5558 {
5559 next = bl->sb_next;
5560 vim_free(bl);
5561 bl = next;
5562 }
5563}
5564
5565/*
5566 * Allocate the root of a word tree.
5567 */
5568 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005569wordtree_alloc(spin)
5570 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005571{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005572 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005573}
5574
5575/*
5576 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005577 * Always store it in the case-folded tree. For a keep-case word this is
5578 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
5579 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005580 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005581 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
5582 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005583 */
5584 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005585store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005586 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005587 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005588 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005589 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005590 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005591 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005592{
5593 int len = STRLEN(word);
5594 int ct = captype(word, word + len);
5595 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005596 int res = OK;
5597 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005599 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005600 for (p = pfxlist; res == OK; ++p)
5601 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005602 if (!need_affix || (p != NULL && *p != NUL))
5603 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005604 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005605 if (p == NULL || *p == NUL)
5606 break;
5607 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005608 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005609
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005610 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00005611 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005612 for (p = pfxlist; res == OK; ++p)
5613 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005614 if (!need_affix || (p != NULL && *p != NUL))
5615 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005616 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005617 if (p == NULL || *p == NUL)
5618 break;
5619 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005620 ++spin->si_keepwcount;
5621 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005622 return res;
5623}
5624
5625/*
5626 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005627 * When "flags" < 0 we are adding to the prefix tree where flags is used for
5628 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005629 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005630 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005631 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005632tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005633 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005634 char_u *word;
5635 wordnode_T *root;
5636 int flags;
5637 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005638 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005639{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005640 wordnode_T *node = root;
5641 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005642 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005643 wordnode_T **prev = NULL;
5644 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005645
Bram Moolenaar51485f02005-06-04 21:55:20 +00005646 /* Add each byte of the word to the tree, including the NUL at the end. */
5647 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005648 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005649 /* When there is more than one reference to this node we need to make
5650 * a copy, so that we can modify it. Copy the whole list of siblings
5651 * (we don't optimize for a partly shared list of siblings). */
5652 if (node != NULL && node->wn_refs > 1)
5653 {
5654 --node->wn_refs;
5655 copyprev = prev;
5656 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
5657 {
5658 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005659 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005660 if (np == NULL)
5661 return FAIL;
5662 np->wn_child = copyp->wn_child;
5663 if (np->wn_child != NULL)
5664 ++np->wn_child->wn_refs; /* child gets extra ref */
5665 np->wn_byte = copyp->wn_byte;
5666 if (np->wn_byte == NUL)
5667 {
5668 np->wn_flags = copyp->wn_flags;
5669 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005670 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005671 }
5672
5673 /* Link the new node in the list, there will be one ref. */
5674 np->wn_refs = 1;
5675 *copyprev = np;
5676 copyprev = &np->wn_sibling;
5677
5678 /* Let "node" point to the head of the copied list. */
5679 if (copyp == node)
5680 node = np;
5681 }
5682 }
5683
Bram Moolenaar51485f02005-06-04 21:55:20 +00005684 /* Look for the sibling that has the same character. They are sorted
5685 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005686 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005687 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005688 while (node != NULL
5689 && (node->wn_byte < word[i]
5690 || (node->wn_byte == NUL
5691 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005692 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005693 : node->wn_flags < (flags & WN_MASK)
5694 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005695 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005697 prev = &node->wn_sibling;
5698 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005699 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005700 if (node == NULL
5701 || node->wn_byte != word[i]
5702 || (word[i] == NUL
5703 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005704 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005705 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005706 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005707 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005708 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005709 if (np == NULL)
5710 return FAIL;
5711 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005712
5713 /* If "node" is NULL this is a new child or the end of the sibling
5714 * list: ref count is one. Otherwise use ref count of sibling and
5715 * make ref count of sibling one (matters when inserting in front
5716 * of the list of siblings). */
5717 if (node == NULL)
5718 np->wn_refs = 1;
5719 else
5720 {
5721 np->wn_refs = node->wn_refs;
5722 node->wn_refs = 1;
5723 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005724 *prev = np;
5725 np->wn_sibling = node;
5726 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005727 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005728
Bram Moolenaar51485f02005-06-04 21:55:20 +00005729 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005730 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005731 node->wn_flags = flags;
5732 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005733 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005734 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00005735 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005736 prev = &node->wn_child;
5737 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005738 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005739#ifdef SPELL_PRINTTREE
5740 smsg("Added \"%s\"", word);
5741 spell_print_tree(root->wn_sibling);
5742#endif
5743
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005744 /* count nr of words added since last message */
5745 ++spin->si_msg_count;
5746
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005747 if (spin->si_compress_cnt > 1)
5748 {
5749 if (--spin->si_compress_cnt == 1)
5750 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005751 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005752 }
5753
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005754 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005755 * When we have allocated lots of memory we need to compress the word tree
5756 * to free up some room. But compression is slow, and we might actually
5757 * need that room, thus only compress in the following situations:
5758 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00005759 * "compress_start" blocks.
5760 * 2. When compressed before and used "compress_inc" blocks before
5761 * adding "compress_added" words (si_compress_cnt > 1).
5762 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005763 * (si_compress_cnt == 1) and the number of free nodes drops below the
5764 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005765 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005766#ifndef SPELL_PRINTTREE
5767 if (spin->si_compress_cnt == 1
5768 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00005769 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005770#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005771 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005772 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00005773 * when the freed up room has been used and another "compress_inc"
5774 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005775 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005776 spin->si_blocks_cnt -= compress_inc;
5777 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005778
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005779 if (spin->si_verbose)
5780 {
5781 msg_start();
5782 msg_puts((char_u *)_(msg_compressing));
5783 msg_clr_eos();
5784 msg_didout = FALSE;
5785 msg_col = 0;
5786 out_flush();
5787 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005788
5789 /* Compress both trees. Either they both have many nodes, which makes
5790 * compression useful, or one of them is small, which means
5791 * compression goes fast. */
5792 wordtree_compress(spin, spin->si_foldroot);
5793 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005794 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005795
5796 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005797}
5798
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005799/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00005800 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
5801 * Sets "sps_flags".
5802 */
5803 int
5804spell_check_msm()
5805{
5806 char_u *p = p_msm;
5807 long start = 0;
5808 long inc = 0;
5809 long added = 0;
5810
5811 if (!VIM_ISDIGIT(*p))
5812 return FAIL;
5813 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
5814 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
5815 if (*p != ',')
5816 return FAIL;
5817 ++p;
5818 if (!VIM_ISDIGIT(*p))
5819 return FAIL;
5820 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
5821 if (*p != ',')
5822 return FAIL;
5823 ++p;
5824 if (!VIM_ISDIGIT(*p))
5825 return FAIL;
5826 added = getdigits(&p) * 1024;
5827 if (*p != NUL)
5828 return FAIL;
5829
5830 if (start == 0 || inc == 0 || added == 0 || inc > start)
5831 return FAIL;
5832
5833 compress_start = start;
5834 compress_inc = inc;
5835 compress_added = added;
5836 return OK;
5837}
5838
5839
5840/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005841 * Get a wordnode_T, either from the list of previously freed nodes or
5842 * allocate a new one.
5843 */
5844 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005845get_wordnode(spin)
5846 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005847{
5848 wordnode_T *n;
5849
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005850 if (spin->si_first_free == NULL)
5851 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005852 else
5853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005854 n = spin->si_first_free;
5855 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005856 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005857 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005858 }
5859#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005860 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005861#endif
5862 return n;
5863}
5864
5865/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005866 * Decrement the reference count on a node (which is the head of a list of
5867 * siblings). If the reference count becomes zero free the node and its
5868 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005869 */
5870 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005871deref_wordnode(spin, node)
5872 spellinfo_T *spin;
5873 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005874{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005875 wordnode_T *np;
5876
5877 if (--node->wn_refs == 0)
5878 for (np = node; np != NULL; np = np->wn_sibling)
5879 {
5880 if (np->wn_child != NULL)
5881 deref_wordnode(spin, np->wn_child);
5882 free_wordnode(spin, np);
5883 }
5884}
5885
5886/*
5887 * Free a wordnode_T for re-use later.
5888 * Only the "wn_child" field becomes invalid.
5889 */
5890 static void
5891free_wordnode(spin, n)
5892 spellinfo_T *spin;
5893 wordnode_T *n;
5894{
5895 n->wn_child = spin->si_first_free;
5896 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005897 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005898}
5899
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005900/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005901 * Compress a tree: find tails that are identical and can be shared.
5902 */
5903 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005904wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005905 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005906 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005907{
5908 hashtab_T ht;
5909 int n;
5910 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005911 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005912
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005913 /* Skip the root itself, it's not actually used. The first sibling is the
5914 * start of the tree. */
5915 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005916 {
5917 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005918 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005919
5920#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005921 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005922#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005923 {
5924 if (!spin->si_verbose)
5925 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005926 if (tot > 1000000)
5927 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005928 else if (tot == 0)
5929 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005930 else
5931 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005932 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005933 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005934 if (p_verbose > 2)
5935 verbose_leave();
5936 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005937#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005938 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005939#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005940 hash_clear(&ht);
5941 }
5942}
5943
5944/*
5945 * Compress a node, its siblings and its children, depth first.
5946 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005947 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005948 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005949node_compress(spin, node, ht, tot)
5950 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005951 wordnode_T *node;
5952 hashtab_T *ht;
5953 int *tot; /* total count of nodes before compressing,
5954 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005955{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005956 wordnode_T *np;
5957 wordnode_T *tp;
5958 wordnode_T *child;
5959 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005960 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005961 int len = 0;
5962 unsigned nr, n;
5963 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005964
Bram Moolenaar51485f02005-06-04 21:55:20 +00005965 /*
5966 * Go through the list of siblings. Compress each child and then try
5967 * finding an identical child to replace it.
5968 * Note that with "child" we mean not just the node that is pointed to,
5969 * but the whole list of siblings, of which the node is the first.
5970 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005971 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005972 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005973 ++len;
5974 if ((child = np->wn_child) != NULL)
5975 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005976 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005977 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005978
5979 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005980 hash = hash_hash(child->wn_u1.hashkey);
5981 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005982 tp = NULL;
5983 if (!HASHITEM_EMPTY(hi))
5984 {
5985 /* There are children with an identical hash value. Now check
5986 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005987 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005988 if (node_equal(child, tp))
5989 {
5990 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005991 * current one. This means the current child and all
5992 * its siblings is unlinked from the tree. */
5993 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005994 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005995 np->wn_child = tp;
5996 ++compressed;
5997 break;
5998 }
5999 if (tp == NULL)
6000 {
6001 /* No other child with this hash value equals the child of
6002 * the node, add it to the linked list after the first
6003 * item. */
6004 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006005 child->wn_u2.next = tp->wn_u2.next;
6006 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006007 }
6008 }
6009 else
6010 /* No other child has this hash value, add it to the
6011 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006012 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006013 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006014 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006015 *tot += len;
6016
6017 /*
6018 * Make a hash key for the node and its siblings, so that we can quickly
6019 * find a lookalike node. This must be done after compressing the sibling
6020 * list, otherwise the hash key would become invalid by the compression.
6021 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006022 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006023 nr = 0;
6024 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006025 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006026 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006027 /* end node: use wn_flags, wn_region and wn_affixID */
6028 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006029 else
6030 /* byte node: use the byte value and the child pointer */
6031 n = np->wn_byte + ((long_u)np->wn_child << 8);
6032 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006033 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006034
6035 /* Avoid NUL bytes, it terminates the hash key. */
6036 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006037 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006038 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006039 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006040 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006041 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006042 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006043 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6044 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006045
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006046 /* Check for CTRL-C pressed now and then. */
6047 fast_breakcheck();
6048
Bram Moolenaar51485f02005-06-04 21:55:20 +00006049 return compressed;
6050}
6051
6052/*
6053 * Return TRUE when two nodes have identical siblings and children.
6054 */
6055 static int
6056node_equal(n1, n2)
6057 wordnode_T *n1;
6058 wordnode_T *n2;
6059{
6060 wordnode_T *p1;
6061 wordnode_T *p2;
6062
6063 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6064 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6065 if (p1->wn_byte != p2->wn_byte
6066 || (p1->wn_byte == NUL
6067 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006068 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006069 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006070 : (p1->wn_child != p2->wn_child)))
6071 break;
6072
6073 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006074}
6075
6076/*
6077 * Write a number to file "fd", MSB first, in "len" bytes.
6078 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006079 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006080put_bytes(fd, nr, len)
6081 FILE *fd;
6082 long_u nr;
6083 int len;
6084{
6085 int i;
6086
6087 for (i = len - 1; i >= 0; --i)
6088 putc((int)(nr >> (i * 8)), fd);
6089}
6090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006091static int
6092#ifdef __BORLANDC__
6093_RTLENTRYF
6094#endif
6095rep_compare __ARGS((const void *s1, const void *s2));
6096
6097/*
6098 * Function given to qsort() to sort the REP items on "from" string.
6099 */
6100 static int
6101#ifdef __BORLANDC__
6102_RTLENTRYF
6103#endif
6104rep_compare(s1, s2)
6105 const void *s1;
6106 const void *s2;
6107{
6108 fromto_T *p1 = (fromto_T *)s1;
6109 fromto_T *p2 = (fromto_T *)s2;
6110
6111 return STRCMP(p1->ft_from, p2->ft_from);
6112}
6113
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006114/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006115 * Write the Vim .spl file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006116 */
6117 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006118write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006119 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006120 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006121{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006122 FILE *fd;
6123 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006124 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006125 wordnode_T *tree;
6126 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006127 int i;
6128 int l;
6129 garray_T *gap;
6130 fromto_T *ftp;
6131 char_u *p;
6132 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006133
Bram Moolenaarb765d632005-06-07 21:00:02 +00006134 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006135 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006136 {
6137 EMSG2(_(e_notopen), fname);
6138 return;
6139 }
6140
Bram Moolenaar5195e452005-08-19 20:32:47 +00006141 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006142 /* <fileID> */
6143 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
6144 EMSG(_(e_write));
Bram Moolenaar5195e452005-08-19 20:32:47 +00006145 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006146
Bram Moolenaar5195e452005-08-19 20:32:47 +00006147 /*
6148 * <SECTIONS>: <section> ... <sectionend>
6149 */
6150
6151 /* SN_REGION: <regionname> ...
6152 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006153 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006154 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006155 putc(SN_REGION, fd); /* <sectionID> */
6156 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6157 l = spin->si_region_count * 2;
6158 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6159 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6160 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006161 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006162 }
6163 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006164 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006165
Bram Moolenaar5195e452005-08-19 20:32:47 +00006166 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6167 *
6168 * The table with character flags and the table for case folding.
6169 * This makes sure the same characters are recognized as word characters
6170 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006171 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006172 * 'encoding'.
6173 * Also skip this for an .add.spl file, the main spell file must contain
6174 * the table (avoids that it conflicts). File is shorter too.
6175 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006176 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006177 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006178 char_u folchars[128 * 8];
6179 int flags;
6180
6181 putc(SN_MIDWORD, fd); /* <sectionID> */
6182 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6183
6184 /* Form the <folchars> string first, we need to know its length. */
6185 l = 0;
6186 for (i = 128; i < 256; ++i)
6187 {
6188#ifdef FEAT_MBYTE
6189 if (has_mbyte)
6190 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6191 else
6192#endif
6193 folchars[l++] = spelltab.st_fold[i];
6194 }
6195 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6196
6197 fputc(128, fd); /* <charflagslen> */
6198 for (i = 128; i < 256; ++i)
6199 {
6200 flags = 0;
6201 if (spelltab.st_isw[i])
6202 flags |= CF_WORD;
6203 if (spelltab.st_isu[i])
6204 flags |= CF_UPPER;
6205 fputc(flags, fd); /* <charflags> */
6206 }
6207
6208 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6209 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006210 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006211
Bram Moolenaar5195e452005-08-19 20:32:47 +00006212 /* SN_MIDWORD: <midword> */
6213 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006214 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006215 putc(SN_MIDWORD, fd); /* <sectionID> */
6216 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6217
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006218 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006219 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006220 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6221 }
6222
Bram Moolenaar5195e452005-08-19 20:32:47 +00006223 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6224 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006225 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006226 putc(SN_PREFCOND, fd); /* <sectionID> */
6227 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6228
6229 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6230 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6231
6232 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006233 }
6234
Bram Moolenaar5195e452005-08-19 20:32:47 +00006235 /* SN_REP: <repcount> <rep> ...
6236 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006238 /* Sort the REP items. */
6239 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6240 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006241
Bram Moolenaar5195e452005-08-19 20:32:47 +00006242 /* round 1: SN_REP section
6243 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006244 for (round = 1; round <= 2; ++round)
6245 {
6246 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006248 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006249 putc(SN_REP, fd); /* <sectionID> */
6250 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006251 else
6252 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006253 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6254 /* using SN_SOFO section instead of SN_SAL */
6255 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006256 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006257 putc(SN_SAL, fd); /* <sectionID> */
6258 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006259
Bram Moolenaar5195e452005-08-19 20:32:47 +00006260 /* This is for making suggestions, section is not required. */
6261 putc(0, fd); /* <sectionflags> */
6262
6263 /* Compute the length of what follows. */
6264 l = 2; /* count <repcount> or <salcount> */
6265 for (i = 0; i < gap->ga_len; ++i)
6266 {
6267 ftp = &((fromto_T *)gap->ga_data)[i];
6268 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6269 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6270 }
6271 if (round == 2)
6272 ++l; /* count <salflags> */
6273 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6274
6275 if (round == 2)
6276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006277 i = 0;
6278 if (spin->si_followup)
6279 i |= SAL_F0LLOWUP;
6280 if (spin->si_collapse)
6281 i |= SAL_COLLAPSE;
6282 if (spin->si_rem_accents)
6283 i |= SAL_REM_ACCENTS;
6284 putc(i, fd); /* <salflags> */
6285 }
6286
6287 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6288 for (i = 0; i < gap->ga_len; ++i)
6289 {
6290 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6291 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6292 ftp = &((fromto_T *)gap->ga_data)[i];
6293 for (rr = 1; rr <= 2; ++rr)
6294 {
6295 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6296 l = STRLEN(p);
6297 putc(l, fd);
6298 fwrite(p, l, (size_t)1, fd);
6299 }
6300 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006302 }
6303
Bram Moolenaar5195e452005-08-19 20:32:47 +00006304 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6305 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006306 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6307 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006308 putc(SN_SOFO, fd); /* <sectionID> */
6309 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006310
6311 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006312 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6313 /* <sectionlen> */
6314
6315 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6316 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006317
6318 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006319 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
6320 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006321 }
6322
Bram Moolenaar5195e452005-08-19 20:32:47 +00006323 /* SN_MAP: <mapstr>
6324 * This is for making suggestions, section is not required. */
6325 if (spin->si_map.ga_len > 0)
6326 {
6327 putc(SN_MAP, fd); /* <sectionID> */
6328 putc(0, fd); /* <sectionflags> */
6329 l = spin->si_map.ga_len;
6330 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6331 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
6332 /* <mapstr> */
6333 }
6334
6335 /* SN_COMPOUND: compound info.
6336 * We don't mark it required, when not supported all compound words will
6337 * be bad words. */
6338 if (spin->si_compflags != NULL)
6339 {
6340 putc(SN_COMPOUND, fd); /* <sectionID> */
6341 putc(0, fd); /* <sectionflags> */
6342
6343 l = STRLEN(spin->si_compflags);
6344 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
6345 putc(spin->si_compmax, fd); /* <compmax> */
6346 putc(spin->si_compminlen, fd); /* <compminlen> */
6347 putc(spin->si_compsylmax, fd); /* <compsylmax> */
6348 /* <compflags> */
6349 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
6350 }
6351
6352 /* SN_SYLLABLE: syllable info.
6353 * We don't mark it required, when not supported syllables will not be
6354 * counted. */
6355 if (spin->si_syllable != NULL)
6356 {
6357 putc(SN_SYLLABLE, fd); /* <sectionID> */
6358 putc(0, fd); /* <sectionflags> */
6359
6360 l = STRLEN(spin->si_syllable);
6361 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6362 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
6363 }
6364
6365 /* end of <SECTIONS> */
6366 putc(SN_END, fd); /* <sectionend> */
6367
Bram Moolenaar50cde822005-06-05 21:54:54 +00006368
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006369 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006370 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006371 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006372 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006373 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006374 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006375 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006376 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006377 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006378 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006379 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006380 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006381
Bram Moolenaar0c405862005-06-22 22:26:26 +00006382 /* Clear the index and wnode fields in the tree. */
6383 clear_node(tree);
6384
Bram Moolenaar51485f02005-06-04 21:55:20 +00006385 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00006386 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00006387 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006388 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006389
Bram Moolenaar51485f02005-06-04 21:55:20 +00006390 /* number of nodes in 4 bytes */
6391 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00006392 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006393
Bram Moolenaar51485f02005-06-04 21:55:20 +00006394 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006395 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006396 }
6397
Bram Moolenaar51485f02005-06-04 21:55:20 +00006398 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006399}
6400
6401/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006402 * Clear the index and wnode fields of "node", it siblings and its
6403 * children. This is needed because they are a union with other items to save
6404 * space.
6405 */
6406 static void
6407clear_node(node)
6408 wordnode_T *node;
6409{
6410 wordnode_T *np;
6411
6412 if (node != NULL)
6413 for (np = node; np != NULL; np = np->wn_sibling)
6414 {
6415 np->wn_u1.index = 0;
6416 np->wn_u2.wnode = NULL;
6417
6418 if (np->wn_byte != NUL)
6419 clear_node(np->wn_child);
6420 }
6421}
6422
6423
6424/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006425 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006426 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00006427 * This first writes the list of possible bytes (siblings). Then for each
6428 * byte recursively write the children.
6429 *
6430 * NOTE: The code here must match the code in read_tree(), since assumptions
6431 * are made about the indexes (so that we don't have to write them in the
6432 * file).
6433 *
6434 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006435 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006436 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00006437put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006438 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006439 wordnode_T *node;
6440 int index;
6441 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006442 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006443{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006444 int newindex = index;
6445 int siblingcount = 0;
6446 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006447 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006448
Bram Moolenaar51485f02005-06-04 21:55:20 +00006449 /* If "node" is zero the tree is empty. */
6450 if (node == NULL)
6451 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006452
Bram Moolenaar51485f02005-06-04 21:55:20 +00006453 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006454 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006455
6456 /* Count the number of siblings. */
6457 for (np = node; np != NULL; np = np->wn_sibling)
6458 ++siblingcount;
6459
6460 /* Write the sibling count. */
6461 if (fd != NULL)
6462 putc(siblingcount, fd); /* <siblingcount> */
6463
6464 /* Write each sibling byte and optionally extra info. */
6465 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006466 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006467 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006468 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006469 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006470 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006471 /* For a NUL byte (end of word) write the flags etc. */
6472 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006473 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006474 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006475 * associated condition nr (stored in wn_region). The
6476 * byte value is misused to store the "rare" and "not
6477 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006478 if (np->wn_flags == (short_u)PFX_FLAGS)
6479 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006480 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00006481 {
6482 putc(BY_FLAGS, fd); /* <byte> */
6483 putc(np->wn_flags, fd); /* <pflags> */
6484 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006485 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006486 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006487 }
6488 else
6489 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006490 /* For word trees we write the flag/region items. */
6491 flags = np->wn_flags;
6492 if (regionmask != 0 && np->wn_region != regionmask)
6493 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006494 if (np->wn_affixID != 0)
6495 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006496 if (flags == 0)
6497 {
6498 /* word without flags or region */
6499 putc(BY_NOFLAGS, fd); /* <byte> */
6500 }
6501 else
6502 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006503 if (np->wn_flags >= 0x100)
6504 {
6505 putc(BY_FLAGS2, fd); /* <byte> */
6506 putc(flags, fd); /* <flags> */
6507 putc((unsigned)flags >> 8, fd); /* <flags2> */
6508 }
6509 else
6510 {
6511 putc(BY_FLAGS, fd); /* <byte> */
6512 putc(flags, fd); /* <flags> */
6513 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006514 if (flags & WF_REGION)
6515 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006516 if (flags & WF_AFX)
6517 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006518 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006519 }
6520 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006521 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006522 else
6523 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006524 if (np->wn_child->wn_u1.index != 0
6525 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006526 {
6527 /* The child is written elsewhere, write the reference. */
6528 if (fd != NULL)
6529 {
6530 putc(BY_INDEX, fd); /* <byte> */
6531 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006532 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006533 }
6534 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00006535 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006537 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006538
Bram Moolenaar51485f02005-06-04 21:55:20 +00006539 if (fd != NULL)
6540 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
6541 {
6542 EMSG(_(e_write));
6543 return 0;
6544 }
6545 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006546 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006547
6548 /* Space used in the array when reading: one for each sibling and one for
6549 * the count. */
6550 newindex += siblingcount + 1;
6551
6552 /* Recursively dump the children of each sibling. */
6553 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006554 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
6555 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006556 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006557
6558 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006559}
6560
6561
6562/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00006563 * ":mkspell [-ascii] outfile infile ..."
6564 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006565 */
6566 void
6567ex_mkspell(eap)
6568 exarg_T *eap;
6569{
6570 int fcount;
6571 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006572 char_u *arg = eap->arg;
6573 int ascii = FALSE;
6574
6575 if (STRNCMP(arg, "-ascii", 6) == 0)
6576 {
6577 ascii = TRUE;
6578 arg = skipwhite(arg + 6);
6579 }
6580
6581 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
6582 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
6583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006584 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006585 FreeWild(fcount, fnames);
6586 }
6587}
6588
6589/*
6590 * Create a Vim spell file from one or more word lists.
6591 * "fnames[0]" is the output file name.
6592 * "fnames[fcount - 1]" is the last input file name.
6593 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
6594 * and ".spl" is appended to make the output file name.
6595 */
6596 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006597mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006598 int fcount;
6599 char_u **fnames;
6600 int ascii; /* -ascii argument given */
6601 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006602 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006603{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006604 char_u fname[MAXPATHL];
6605 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00006606 char_u **innames;
6607 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006608 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006609 int i;
6610 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006611 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006612 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006613 spellinfo_T spin;
6614
6615 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006616 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006617 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006618 spin.si_followup = TRUE;
6619 spin.si_rem_accents = TRUE;
6620 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
6621 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
6622 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006623 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006624
Bram Moolenaarb765d632005-06-07 21:00:02 +00006625 /* default: fnames[0] is output file, following are input files */
6626 innames = &fnames[1];
6627 incount = fcount - 1;
6628
6629 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00006630 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006631 len = STRLEN(fnames[0]);
6632 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
6633 {
6634 /* For ":mkspell path/en.latin1.add" output file is
6635 * "path/en.latin1.add.spl". */
6636 innames = &fnames[0];
6637 incount = 1;
6638 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
6639 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006640 else if (fcount == 1)
6641 {
6642 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
6643 innames = &fnames[0];
6644 incount = 1;
6645 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6646 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6647 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006648 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
6649 {
6650 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006651 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006652 }
6653 else
6654 /* Name should be language, make the file name from it. */
6655 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6656 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6657
6658 /* Check for .ascii.spl. */
6659 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
6660 spin.si_ascii = TRUE;
6661
6662 /* Check for .add.spl. */
6663 if (strstr((char *)gettail(wfname), ".add.") != NULL)
6664 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006665 }
6666
Bram Moolenaarb765d632005-06-07 21:00:02 +00006667 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006668 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006669 else if (vim_strchr(gettail(wfname), '_') != NULL)
6670 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006671 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006672 EMSG(_("E754: Only up to 8 regions supported"));
6673 else
6674 {
6675 /* Check for overwriting before doing things that may take a lot of
6676 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006677 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006678 {
6679 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006680 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006681 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006682 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006683 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006684 EMSG2(_(e_isadir2), wfname);
6685 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006686 }
6687
6688 /*
6689 * Init the aff and dic pointers.
6690 * Get the region names if there are more than 2 arguments.
6691 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006692 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006693 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006694 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006695
Bram Moolenaar3982c542005-06-08 21:56:31 +00006696 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006697 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006698 len = STRLEN(innames[i]);
6699 if (STRLEN(gettail(innames[i])) < 5
6700 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006701 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006702 EMSG2(_("E755: Invalid region in %s"), innames[i]);
6703 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006704 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006705 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6706 spin.si_region_name[i * 2 + 1] =
6707 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006708 }
6709 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006710 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006711
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006712 spin.si_foldroot = wordtree_alloc(&spin);
6713 spin.si_keeproot = wordtree_alloc(&spin);
6714 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006715 if (spin.si_foldroot == NULL
6716 || spin.si_keeproot == NULL
6717 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006718 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006719 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006720 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006721 }
6722
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006723 /* When not producing a .add.spl file clear the character table when
6724 * we encounter one in the .aff file. This means we dump the current
6725 * one in the .spl file if the .aff file doesn't define one. That's
6726 * better than guessing the contents, the table will match a
6727 * previously loaded spell file. */
6728 if (!spin.si_add)
6729 spin.si_clear_chartab = TRUE;
6730
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006731 /*
6732 * Read all the .aff and .dic files.
6733 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006734 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006735 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006736 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006737 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006738 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006739 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006740
Bram Moolenaarb765d632005-06-07 21:00:02 +00006741 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006742 if (mch_stat((char *)fname, &st) >= 0)
6743 {
6744 /* Read the .aff file. Will init "spin->si_conv" based on the
6745 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006746 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006747 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006748 error = TRUE;
6749 else
6750 {
6751 /* Read the .dic file and store the words in the trees. */
6752 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00006753 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006754 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006755 error = TRUE;
6756 }
6757 }
6758 else
6759 {
6760 /* No .aff file, try reading the file as a word list. Store
6761 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006762 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006763 error = TRUE;
6764 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006765
Bram Moolenaarb765d632005-06-07 21:00:02 +00006766#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006767 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006769#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006770 }
6771
Bram Moolenaar51485f02005-06-04 21:55:20 +00006772 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006773 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006774 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006775 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006776 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006777 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006778 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006779 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006780 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006781 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006782 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006783 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006784 verbose_leave();
6785 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006786 wordtree_compress(&spin, spin.si_foldroot);
6787 wordtree_compress(&spin, spin.si_keeproot);
6788 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789 }
6790
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 if (!error)
6792 {
6793 /*
6794 * Write the info in the spell file.
6795 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006796 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006797 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006798 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006799 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006800 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006801 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006802 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006803 verbose_leave();
6804 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00006805
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006806 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006807
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006808 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006809 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006810 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006811 verbose_enter();
6812 MSG(_("Done!"));
6813 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00006814 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006815 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006816 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006817 verbose_leave();
6818 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006819
Bram Moolenaarb765d632005-06-07 21:00:02 +00006820 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006821 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006822 }
6823
6824 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006825 ga_clear(&spin.si_rep);
6826 ga_clear(&spin.si_sal);
6827 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006828 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006829 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006830 vim_free(spin.si_sofofr);
6831 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006832
6833 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006834 for (i = 0; i < incount; ++i)
6835 if (afile[i] != NULL)
6836 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006837
6838 /* Free all the bits and pieces at once. */
6839 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006840 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006841}
6842
Bram Moolenaarb765d632005-06-07 21:00:02 +00006843
6844/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006845 * ":[count]spellgood {word}"
6846 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00006847 */
6848 void
6849ex_spell(eap)
6850 exarg_T *eap;
6851{
Bram Moolenaar7887d882005-07-01 22:33:52 +00006852 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006853 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006854}
6855
6856/*
6857 * Add "word[len]" to 'spellfile' as a good or bad word.
6858 */
6859 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006860spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006861 char_u *word;
6862 int len;
6863 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006864 int index; /* "zG" and "zW": zero, otherwise index in
6865 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006866{
6867 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006868 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006869 int new_spf = FALSE;
6870 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006871 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006872 char_u fnamebuf[MAXPATHL];
6873 char_u line[MAXWLEN * 2];
6874 long fpos, fpos_next = 0;
6875 int i;
6876 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006877
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006878 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006879 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006880 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006881 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006882 int_wordlist = vim_tempname('s');
6883 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006884 return;
6885 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006886 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006887 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006888 else
6889 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006890 /* If 'spellfile' isn't set figure out a good default value. */
6891 if (*curbuf->b_p_spf == NUL)
6892 {
6893 init_spellfile();
6894 new_spf = TRUE;
6895 }
6896
6897 if (*curbuf->b_p_spf == NUL)
6898 {
6899 EMSG(_("E764: 'spellfile' is not set"));
6900 return;
6901 }
6902
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006903 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
6904 {
6905 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6906 if (i == index)
6907 break;
6908 if (*spf == NUL)
6909 {
6910 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
6911 return;
6912 }
6913 }
6914
Bram Moolenaarb765d632005-06-07 21:00:02 +00006915 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006916 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006917 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6918 buf = NULL;
6919 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00006920 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006921 EMSG(_(e_bufloaded));
6922 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006923 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006924
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006925 fname = fnamebuf;
6926 }
6927
6928 if (bad)
6929 {
6930 /* When the word also appears as good word we need to remove that one,
6931 * since its flags sort before the one with WF_BANNED. */
6932 fd = mch_fopen((char *)fname, "r");
6933 if (fd != NULL)
6934 {
6935 while (!vim_fgets(line, MAXWLEN * 2, fd))
6936 {
6937 fpos = fpos_next;
6938 fpos_next = ftell(fd);
6939 if (STRNCMP(word, line, len) == 0
6940 && (line[len] == '/' || line[len] < ' '))
6941 {
6942 /* Found duplicate word. Remove it by writing a '#' at
6943 * the start of the line. Mixing reading and writing
6944 * doesn't work for all systems, close the file first. */
6945 fclose(fd);
6946 fd = mch_fopen((char *)fname, "r+");
6947 if (fd == NULL)
6948 break;
6949 if (fseek(fd, fpos, SEEK_SET) == 0)
6950 fputc('#', fd);
6951 fseek(fd, fpos_next, SEEK_SET);
6952 }
6953 }
6954 fclose(fd);
6955 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006956 }
6957
6958 fd = mch_fopen((char *)fname, "a");
6959 if (fd == NULL && new_spf)
6960 {
6961 /* We just initialized the 'spellfile' option and can't open the file.
6962 * We may need to create the "spell" directory first. We already
6963 * checked the runtime directory is writable in init_spellfile(). */
6964 STRCPY(NameBuff, fname);
6965 *gettail_sep(NameBuff) = NUL;
6966 if (mch_stat((char *)NameBuff, &st) < 0)
6967 {
6968 /* The directory doesn't exist. Try creating it and opening the
6969 * file again. */
6970 vim_mkdir(NameBuff, 0755);
6971 fd = mch_fopen((char *)fname, "a");
6972 }
6973 }
6974
6975 if (fd == NULL)
6976 EMSG2(_(e_notopen), fname);
6977 else
6978 {
6979 if (bad)
6980 fprintf(fd, "%.*s/!\n", len, word);
6981 else
6982 fprintf(fd, "%.*s\n", len, word);
6983 fclose(fd);
6984
6985 /* Update the .add.spl file. */
6986 mkspell(1, &fname, FALSE, TRUE, TRUE);
6987
6988 /* If the .add file is edited somewhere, reload it. */
6989 if (buf != NULL)
6990 buf_reload(buf);
6991
6992 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006993 }
6994}
6995
6996/*
6997 * Initialize 'spellfile' for the current buffer.
6998 */
6999 static void
7000init_spellfile()
7001{
7002 char_u buf[MAXPATHL];
7003 int l;
7004 slang_T *sl;
7005 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007006 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007007
7008 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7009 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007010 /* Find the end of the language name. Exclude the region. */
7011 for (lend = curbuf->b_p_spl; *lend != NUL
7012 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7013 ;
7014
7015 /* Loop over all entries in 'runtimepath'. Use the first one where we
7016 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007017 rtp = p_rtp;
7018 while (*rtp != NUL)
7019 {
7020 /* Copy the path from 'runtimepath' to buf[]. */
7021 copy_option_part(&rtp, buf, MAXPATHL, ",");
7022 if (filewritable(buf) == 2)
7023 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007024 /* Use the first language name from 'spelllang' and the
7025 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007026 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
7027 l = STRLEN(buf);
7028 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007029 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007030 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00007031 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
7032 ? (char_u *)"ascii" : spell_enc());
7033 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7034 break;
7035 }
7036 }
7037 }
7038}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007039
Bram Moolenaar51485f02005-06-04 21:55:20 +00007040
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007041/*
7042 * Init the chartab used for spelling for ASCII.
7043 * EBCDIC is not supported!
7044 */
7045 static void
7046clear_spell_chartab(sp)
7047 spelltab_T *sp;
7048{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007049 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007050
7051 /* Init everything to FALSE. */
7052 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7053 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7054 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007055 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007056 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007057 sp->st_upper[i] = i;
7058 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007059
7060 /* We include digits. A word shouldn't start with a digit, but handling
7061 * that is done separately. */
7062 for (i = '0'; i <= '9'; ++i)
7063 sp->st_isw[i] = TRUE;
7064 for (i = 'A'; i <= 'Z'; ++i)
7065 {
7066 sp->st_isw[i] = TRUE;
7067 sp->st_isu[i] = TRUE;
7068 sp->st_fold[i] = i + 0x20;
7069 }
7070 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007071 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007072 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007073 sp->st_upper[i] = i - 0x20;
7074 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007075}
7076
7077/*
7078 * Init the chartab used for spelling. Only depends on 'encoding'.
7079 * Called once while starting up and when 'encoding' changes.
7080 * The default is to use isalpha(), but the spell file should define the word
7081 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007082 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007083 */
7084 void
7085init_spell_chartab()
7086{
7087 int i;
7088
7089 did_set_spelltab = FALSE;
7090 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007091#ifdef FEAT_MBYTE
7092 if (enc_dbcs)
7093 {
7094 /* DBCS: assume double-wide characters are word characters. */
7095 for (i = 128; i <= 255; ++i)
7096 if (MB_BYTE2LEN(i) == 2)
7097 spelltab.st_isw[i] = TRUE;
7098 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007099 else if (enc_utf8)
7100 {
7101 for (i = 128; i < 256; ++i)
7102 {
7103 spelltab.st_isu[i] = utf_isupper(i);
7104 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7105 spelltab.st_fold[i] = utf_fold(i);
7106 spelltab.st_upper[i] = utf_toupper(i);
7107 }
7108 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007109 else
7110#endif
7111 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007112 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007113 for (i = 128; i < 256; ++i)
7114 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007115 if (MB_ISUPPER(i))
7116 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007117 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007118 spelltab.st_isu[i] = TRUE;
7119 spelltab.st_fold[i] = MB_TOLOWER(i);
7120 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007121 else if (MB_ISLOWER(i))
7122 {
7123 spelltab.st_isw[i] = TRUE;
7124 spelltab.st_upper[i] = MB_TOUPPER(i);
7125 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007126 }
7127 }
7128}
7129
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007130static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
7131static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
7132
7133/*
7134 * Set the spell character tables from strings in the affix file.
7135 */
7136 static int
7137set_spell_chartab(fol, low, upp)
7138 char_u *fol;
7139 char_u *low;
7140 char_u *upp;
7141{
7142 /* We build the new tables here first, so that we can compare with the
7143 * previous one. */
7144 spelltab_T new_st;
7145 char_u *pf = fol, *pl = low, *pu = upp;
7146 int f, l, u;
7147
7148 clear_spell_chartab(&new_st);
7149
7150 while (*pf != NUL)
7151 {
7152 if (*pl == NUL || *pu == NUL)
7153 {
7154 EMSG(_(e_affform));
7155 return FAIL;
7156 }
7157#ifdef FEAT_MBYTE
7158 f = mb_ptr2char_adv(&pf);
7159 l = mb_ptr2char_adv(&pl);
7160 u = mb_ptr2char_adv(&pu);
7161#else
7162 f = *pf++;
7163 l = *pl++;
7164 u = *pu++;
7165#endif
7166 /* Every character that appears is a word character. */
7167 if (f < 256)
7168 new_st.st_isw[f] = TRUE;
7169 if (l < 256)
7170 new_st.st_isw[l] = TRUE;
7171 if (u < 256)
7172 new_st.st_isw[u] = TRUE;
7173
7174 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7175 * case-folding */
7176 if (l < 256 && l != f)
7177 {
7178 if (f >= 256)
7179 {
7180 EMSG(_(e_affrange));
7181 return FAIL;
7182 }
7183 new_st.st_fold[l] = f;
7184 }
7185
7186 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007187 * case-folding, it's upper case and the "UPP" is the upper case of
7188 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007189 if (u < 256 && u != f)
7190 {
7191 if (f >= 256)
7192 {
7193 EMSG(_(e_affrange));
7194 return FAIL;
7195 }
7196 new_st.st_fold[u] = f;
7197 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007198 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007199 }
7200 }
7201
7202 if (*pl != NUL || *pu != NUL)
7203 {
7204 EMSG(_(e_affform));
7205 return FAIL;
7206 }
7207
7208 return set_spell_finish(&new_st);
7209}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007210
7211/*
7212 * Set the spell character tables from strings in the .spl file.
7213 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007214 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007215set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007216 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007217 int cnt; /* length of "flags" */
7218 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007219{
7220 /* We build the new tables here first, so that we can compare with the
7221 * previous one. */
7222 spelltab_T new_st;
7223 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007224 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007225 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007226
7227 clear_spell_chartab(&new_st);
7228
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007229 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007230 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007231 if (i < cnt)
7232 {
7233 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7234 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7235 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007236
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007237 if (*p != NUL)
7238 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007239#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007240 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007241#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007242 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007243#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007244 new_st.st_fold[i + 128] = c;
7245 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7246 new_st.st_upper[c] = i + 128;
7247 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007248 }
7249
Bram Moolenaar5195e452005-08-19 20:32:47 +00007250 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007251}
7252
7253 static int
7254set_spell_finish(new_st)
7255 spelltab_T *new_st;
7256{
7257 int i;
7258
7259 if (did_set_spelltab)
7260 {
7261 /* check that it's the same table */
7262 for (i = 0; i < 256; ++i)
7263 {
7264 if (spelltab.st_isw[i] != new_st->st_isw[i]
7265 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007266 || spelltab.st_fold[i] != new_st->st_fold[i]
7267 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007268 {
7269 EMSG(_("E763: Word characters differ between spell files"));
7270 return FAIL;
7271 }
7272 }
7273 }
7274 else
7275 {
7276 /* copy the new spelltab into the one being used */
7277 spelltab = *new_st;
7278 did_set_spelltab = TRUE;
7279 }
7280
7281 return OK;
7282}
7283
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007284/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007285 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007286 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007287 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007288 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007289 */
7290 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007291spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007292 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007293 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007294{
Bram Moolenaarea408852005-06-25 22:49:46 +00007295#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007296 char_u *s;
7297 int l;
7298 int c;
7299
7300 if (has_mbyte)
7301 {
7302 l = MB_BYTE2LEN(*p);
7303 s = p;
7304 if (l == 1)
7305 {
7306 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007307 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007308 {
7309 s = p + 1; /* skip a mid-word character */
7310 l = MB_BYTE2LEN(*s);
7311 }
7312 }
7313 else
7314 {
7315 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007316 if (c < 256 ? buf->b_spell_ismw[c]
7317 : (buf->b_spell_ismw_mb != NULL
7318 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007319 {
7320 s = p + l;
7321 l = MB_BYTE2LEN(*s);
7322 }
7323 }
7324
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007325 c = mb_ptr2char(s);
7326 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007327 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007328 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007329 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007330#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007331
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007332 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
7333}
7334
7335/*
7336 * Return TRUE if "p" points to a word character.
7337 * Unlike spell_iswordp() this doesn't check for "midword" characters.
7338 */
7339 static int
7340spell_iswordp_nmw(p)
7341 char_u *p;
7342{
7343#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007344 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007345
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007346 if (has_mbyte)
7347 {
7348 c = mb_ptr2char(p);
7349 if (c > 255)
7350 return mb_get_class(p) >= 2;
7351 return spelltab.st_isw[c];
7352 }
7353#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007354 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00007355}
7356
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007357#ifdef FEAT_MBYTE
7358/*
7359 * Return TRUE if "p" points to a word character.
7360 * Wide version of spell_iswordp().
7361 */
7362 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007363spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007364 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007365 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007366{
7367 int *s;
7368
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007369 if (*p < 256 ? buf->b_spell_ismw[*p]
7370 : (buf->b_spell_ismw_mb != NULL
7371 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007372 s = p + 1;
7373 else
7374 s = p;
7375
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007376 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007377 {
7378 if (enc_utf8)
7379 return utf_class(*s) >= 2;
7380 if (enc_dbcs)
7381 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
7382 return 0;
7383 }
7384 return spelltab.st_isw[*s];
7385}
7386#endif
7387
Bram Moolenaarea408852005-06-25 22:49:46 +00007388/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007389 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007390 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007391 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007392 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007393write_spell_prefcond(fd, gap)
7394 FILE *fd;
7395 garray_T *gap;
7396{
7397 int i;
7398 char_u *p;
7399 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007400 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007401
Bram Moolenaar5195e452005-08-19 20:32:47 +00007402 if (fd != NULL)
7403 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
7404
7405 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007406
7407 for (i = 0; i < gap->ga_len; ++i)
7408 {
7409 /* <prefcond> : <condlen> <condstr> */
7410 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00007411 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007412 {
7413 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007414 if (fd != NULL)
7415 {
7416 fputc(len, fd);
7417 fwrite(p, (size_t)len, (size_t)1, fd);
7418 }
7419 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007420 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007421 else if (fd != NULL)
7422 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007423 }
7424
Bram Moolenaar5195e452005-08-19 20:32:47 +00007425 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007426}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007427
7428/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007429 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
7430 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007431 * When using a multi-byte 'encoding' the length may change!
7432 * Returns FAIL when something wrong.
7433 */
7434 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007435spell_casefold(str, len, buf, buflen)
7436 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007437 int len;
7438 char_u *buf;
7439 int buflen;
7440{
7441 int i;
7442
7443 if (len >= buflen)
7444 {
7445 buf[0] = NUL;
7446 return FAIL; /* result will not fit */
7447 }
7448
7449#ifdef FEAT_MBYTE
7450 if (has_mbyte)
7451 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007452 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007453 char_u *p;
7454 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007455
7456 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007457 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007458 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007459 if (outi + MB_MAXBYTES > buflen)
7460 {
7461 buf[outi] = NUL;
7462 return FAIL;
7463 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007464 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007465 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007466 }
7467 buf[outi] = NUL;
7468 }
7469 else
7470#endif
7471 {
7472 /* Be quick for non-multibyte encodings. */
7473 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007474 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007475 buf[i] = NUL;
7476 }
7477
7478 return OK;
7479}
7480
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007481#define SPS_BEST 1
7482#define SPS_FAST 2
7483#define SPS_DOUBLE 4
7484
7485static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007486static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007487
7488/*
7489 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007490 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007491 */
7492 int
7493spell_check_sps()
7494{
7495 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007496 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007497 char_u buf[MAXPATHL];
7498 int f;
7499
7500 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007501 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007502
7503 for (p = p_sps; *p != NUL; )
7504 {
7505 copy_option_part(&p, buf, MAXPATHL, ",");
7506
7507 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007508 if (VIM_ISDIGIT(*buf))
7509 {
7510 s = buf;
7511 sps_limit = getdigits(&s);
7512 if (*s != NUL && !VIM_ISDIGIT(*s))
7513 f = -1;
7514 }
7515 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007516 f = SPS_BEST;
7517 else if (STRCMP(buf, "fast") == 0)
7518 f = SPS_FAST;
7519 else if (STRCMP(buf, "double") == 0)
7520 f = SPS_DOUBLE;
7521 else if (STRNCMP(buf, "expr:", 5) != 0
7522 && STRNCMP(buf, "file:", 5) != 0)
7523 f = -1;
7524
7525 if (f == -1 || (sps_flags != 0 && f != 0))
7526 {
7527 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007528 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007529 return FAIL;
7530 }
7531 if (f != 0)
7532 sps_flags = f;
7533 }
7534
7535 if (sps_flags == 0)
7536 sps_flags = SPS_BEST;
7537
7538 return OK;
7539}
7540
7541/* Remember what "z?" replaced. */
7542static char_u *repl_from = NULL;
7543static char_u *repl_to = NULL;
7544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007545/*
7546 * "z?": Find badly spelled word under or after the cursor.
7547 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007548 */
7549 void
7550spell_suggest()
7551{
7552 char_u *line;
7553 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007554 char_u wcopy[MAXWLEN + 2];
7555 char_u *p;
7556 int i;
7557 int c;
7558 suginfo_T sug;
7559 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007560 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007561 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007562 int limit;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007563
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007564 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007565 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
7566 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007567 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007568 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
7569 return;
7570
7571 /* No bad word or it starts after the cursor: use the word under the
7572 * cursor. */
7573 curwin->w_cursor = prev_cursor;
7574 line = ml_get_curline();
7575 p = line + curwin->w_cursor.col;
7576 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007577 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007578 mb_ptr_back(line, p);
7579 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007580 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007581 mb_ptr_adv(p);
7582
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007583 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007584 {
7585 beep_flush();
7586 return;
7587 }
7588 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007589 }
7590
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007591 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007592
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007593 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007594 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007595
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007596 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007597
Bram Moolenaar5195e452005-08-19 20:32:47 +00007598 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
7599 * 'spellsuggest', whatever is smaller. */
7600 if (sps_limit > (int)Rows - 2)
7601 limit = (int)Rows - 2;
7602 else
7603 limit = sps_limit;
7604 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007605 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007606
7607 if (sug.su_ga.ga_len == 0)
7608 MSG(_("Sorry, no suggestions"));
7609 else
7610 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007611 vim_free(repl_from);
7612 repl_from = NULL;
7613 vim_free(repl_to);
7614 repl_to = NULL;
7615
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007616#ifdef FEAT_RIGHTLEFT
7617 /* When 'rightleft' is set the list is drawn right-left. */
7618 cmdmsg_rl = curwin->w_p_rl;
7619 if (cmdmsg_rl)
7620 msg_col = Columns - 1;
7621#endif
7622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007623 /* List the suggestions. */
7624 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007625 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007626 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
7627 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007628#ifdef FEAT_RIGHTLEFT
7629 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
7630 {
7631 /* And now the rabbit from the high hat: Avoid showing the
7632 * untranslated message rightleft. */
7633 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
7634 sug.su_badlen, sug.su_badptr);
7635 }
7636#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007637 msg_puts(IObuff);
7638 msg_clr_eos();
7639 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00007640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 msg_scroll = TRUE;
7642 for (i = 0; i < sug.su_ga.ga_len; ++i)
7643 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007644 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645
7646 /* The suggested word may replace only part of the bad word, add
7647 * the not replaced part. */
7648 STRCPY(wcopy, stp->st_word);
7649 if (sug.su_badlen > stp->st_orglen)
7650 vim_strncpy(wcopy + STRLEN(wcopy),
7651 sug.su_badptr + stp->st_orglen,
7652 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007653 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
7654#ifdef FEAT_RIGHTLEFT
7655 if (cmdmsg_rl)
7656 rl_mirror(IObuff);
7657#endif
7658 msg_puts(IObuff);
7659
7660 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007661 msg_puts(IObuff);
7662
7663 /* The word may replace more than "su_badlen". */
7664 if (sug.su_badlen < stp->st_orglen)
7665 {
7666 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
7667 stp->st_orglen, sug.su_badptr);
7668 msg_puts(IObuff);
7669 }
7670
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007671 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007672 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007673 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007674 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007675 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007676 stp->st_salscore ? "s " : "",
7677 stp->st_score, stp->st_altscore);
7678 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007679 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00007680 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007681#ifdef FEAT_RIGHTLEFT
7682 if (cmdmsg_rl)
7683 /* Mirror the numbers, but keep the leading space. */
7684 rl_mirror(IObuff + 1);
7685#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00007686 msg_advance(30);
7687 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007689 msg_putchar('\n');
7690 }
7691
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007692#ifdef FEAT_RIGHTLEFT
7693 cmdmsg_rl = FALSE;
7694 msg_col = 0;
7695#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007697 i = prompt_for_number(&mouse_used);
7698 if (mouse_used)
7699 i -= lines_left;
7700
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007701 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007702 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007703 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007704 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007705 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
7706 repl_to = vim_strsave(stp->st_word);
7707
7708 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007709 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
7710 if (p != NULL)
7711 {
7712 c = sug.su_badptr - line;
7713 mch_memmove(p, line, c);
7714 STRCPY(p + c, stp->st_word);
7715 STRCAT(p, sug.su_badptr + stp->st_orglen);
7716 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7717 curwin->w_cursor.col = c;
7718 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007719
7720 /* For redo we use a change-word command. */
7721 ResetRedobuff();
7722 AppendToRedobuff((char_u *)"ciw");
7723 AppendToRedobuff(stp->st_word);
7724 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007725 }
7726 }
7727 else
7728 curwin->w_cursor = prev_cursor;
7729 }
7730
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007731 spell_find_cleanup(&sug);
7732}
7733
7734/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007735 * Check if the word at line "lnum" column "col" is required to start with a
7736 * capital. This uses 'spellcapcheck' of the current buffer.
7737 */
7738 static int
7739check_need_cap(lnum, col)
7740 linenr_T lnum;
7741 colnr_T col;
7742{
7743 int need_cap = FALSE;
7744 char_u *line;
7745 char_u *line_copy = NULL;
7746 char_u *p;
7747 colnr_T endcol;
7748 regmatch_T regmatch;
7749
7750 if (curbuf->b_cap_prog == NULL)
7751 return FALSE;
7752
7753 line = ml_get_curline();
7754 endcol = 0;
7755 if ((int)(skipwhite(line) - line) >= (int)col)
7756 {
7757 /* At start of line, check if previous line is empty or sentence
7758 * ends there. */
7759 if (lnum == 1)
7760 need_cap = TRUE;
7761 else
7762 {
7763 line = ml_get(lnum - 1);
7764 if (*skipwhite(line) == NUL)
7765 need_cap = TRUE;
7766 else
7767 {
7768 /* Append a space in place of the line break. */
7769 line_copy = concat_str(line, (char_u *)" ");
7770 line = line_copy;
7771 endcol = STRLEN(line);
7772 }
7773 }
7774 }
7775 else
7776 endcol = col;
7777
7778 if (endcol > 0)
7779 {
7780 /* Check if sentence ends before the bad word. */
7781 regmatch.regprog = curbuf->b_cap_prog;
7782 regmatch.rm_ic = FALSE;
7783 p = line + endcol;
7784 for (;;)
7785 {
7786 mb_ptr_back(line, p);
7787 if (p == line || spell_iswordp_nmw(p))
7788 break;
7789 if (vim_regexec(&regmatch, p, 0)
7790 && regmatch.endp[0] == line + endcol)
7791 {
7792 need_cap = TRUE;
7793 break;
7794 }
7795 }
7796 }
7797
7798 vim_free(line_copy);
7799
7800 return need_cap;
7801}
7802
7803
7804/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007805 * ":spellrepall"
7806 */
7807/*ARGSUSED*/
7808 void
7809ex_spellrepall(eap)
7810 exarg_T *eap;
7811{
7812 pos_T pos = curwin->w_cursor;
7813 char_u *frompat;
7814 int addlen;
7815 char_u *line;
7816 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007817 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007818 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007819
7820 if (repl_from == NULL || repl_to == NULL)
7821 {
7822 EMSG(_("E752: No previous spell replacement"));
7823 return;
7824 }
7825 addlen = STRLEN(repl_to) - STRLEN(repl_from);
7826
7827 frompat = alloc(STRLEN(repl_from) + 7);
7828 if (frompat == NULL)
7829 return;
7830 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
7831 p_ws = FALSE;
7832
Bram Moolenaar5195e452005-08-19 20:32:47 +00007833 sub_nsubs = 0;
7834 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007835 curwin->w_cursor.lnum = 0;
7836 while (!got_int)
7837 {
7838 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
7839 || u_save_cursor() == FAIL)
7840 break;
7841
7842 /* Only replace when the right word isn't there yet. This happens
7843 * when changing "etc" to "etc.". */
7844 line = ml_get_curline();
7845 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
7846 repl_to, STRLEN(repl_to)) != 0)
7847 {
7848 p = alloc(STRLEN(line) + addlen + 1);
7849 if (p == NULL)
7850 break;
7851 mch_memmove(p, line, curwin->w_cursor.col);
7852 STRCPY(p + curwin->w_cursor.col, repl_to);
7853 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
7854 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7855 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007856
7857 if (curwin->w_cursor.lnum != prev_lnum)
7858 {
7859 ++sub_nlines;
7860 prev_lnum = curwin->w_cursor.lnum;
7861 }
7862 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007863 }
7864 curwin->w_cursor.col += STRLEN(repl_to);
7865 }
7866
7867 p_ws = save_ws;
7868 curwin->w_cursor = pos;
7869 vim_free(frompat);
7870
Bram Moolenaar5195e452005-08-19 20:32:47 +00007871 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007872 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007873 else
7874 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007875}
7876
7877/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007878 * Find spell suggestions for "word". Return them in the growarray "*gap" as
7879 * a list of allocated strings.
7880 */
7881 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007882spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007883 garray_T *gap;
7884 char_u *word;
7885 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007886 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007887{
7888 suginfo_T sug;
7889 int i;
7890 suggest_T *stp;
7891 char_u *wcopy;
7892
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007893 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007894
7895 /* Make room in "gap". */
7896 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
7897 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
7898 return;
7899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007900 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007901 {
7902 stp = &SUG(sug.su_ga, i);
7903
7904 /* The suggested word may replace only part of "word", add the not
7905 * replaced part. */
7906 wcopy = alloc(STRLEN(stp->st_word)
7907 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
7908 if (wcopy == NULL)
7909 break;
7910 STRCPY(wcopy, stp->st_word);
7911 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
7912 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
7913 }
7914
7915 spell_find_cleanup(&sug);
7916}
7917
7918/*
7919 * Find spell suggestions for the word at the start of "badptr".
7920 * Return the suggestions in "su->su_ga".
7921 * The maximum number of suggestions is "maxcount".
7922 * Note: does use info for the current window.
7923 * This is based on the mechanisms of Aspell, but completely reimplemented.
7924 */
7925 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007926spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007927 char_u *badptr;
7928 suginfo_T *su;
7929 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00007930 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007931 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007932{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007933 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007934 char_u buf[MAXPATHL];
7935 char_u *p;
7936 int do_combine = FALSE;
7937 char_u *sps_copy;
7938#ifdef FEAT_EVAL
7939 static int expr_busy = FALSE;
7940#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007941 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007942
7943 /*
7944 * Set the info in "*su".
7945 */
7946 vim_memset(su, 0, sizeof(suginfo_T));
7947 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
7948 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00007949 if (*badptr == NUL)
7950 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007951 hash_init(&su->su_banned);
7952
7953 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007954 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007955 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007956 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007957
7958 if (su->su_badlen >= MAXWLEN)
7959 su->su_badlen = MAXWLEN - 1; /* just in case */
7960 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
7961 (void)spell_casefold(su->su_badptr, su->su_badlen,
7962 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007963 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007964 su->su_badflags = badword_captype(su->su_badptr,
7965 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007966 if (need_cap)
7967 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007968
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007969 /* If the word is not capitalised and spell_check() doesn't consider the
7970 * word to be bad then it might need to be capitalised. Add a suggestion
7971 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007972 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007973 if (!SPELL_ISUPPER(c) && attr == 0)
7974 {
7975 make_case_word(su->su_badword, buf, WF_ONECAP);
7976 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
7977 0, TRUE);
7978 }
7979
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007980 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00007981 if (banbadword)
7982 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007983
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007984 /* Make a copy of 'spellsuggest', because the expression may change it. */
7985 sps_copy = vim_strsave(p_sps);
7986 if (sps_copy == NULL)
7987 return;
7988
7989 /* Loop over the items in 'spellsuggest'. */
7990 for (p = sps_copy; *p != NUL; )
7991 {
7992 copy_option_part(&p, buf, MAXPATHL, ",");
7993
7994 if (STRNCMP(buf, "expr:", 5) == 0)
7995 {
7996#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007997 /* Evaluate an expression. Skip this when called recursively,
7998 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007999 if (!expr_busy)
8000 {
8001 expr_busy = TRUE;
8002 spell_suggest_expr(su, buf + 5);
8003 expr_busy = FALSE;
8004 }
8005#endif
8006 }
8007 else if (STRNCMP(buf, "file:", 5) == 0)
8008 /* Use list of suggestions in a file. */
8009 spell_suggest_file(su, buf + 5);
8010 else
8011 {
8012 /* Use internal method. */
8013 spell_suggest_intern(su);
8014 if (sps_flags & SPS_DOUBLE)
8015 do_combine = TRUE;
8016 }
8017 }
8018
8019 vim_free(sps_copy);
8020
8021 if (do_combine)
8022 /* Combine the two list of suggestions. This must be done last,
8023 * because sorting changes the order again. */
8024 score_combine(su);
8025}
8026
8027#ifdef FEAT_EVAL
8028/*
8029 * Find suggestions by evaluating expression "expr".
8030 */
8031 static void
8032spell_suggest_expr(su, expr)
8033 suginfo_T *su;
8034 char_u *expr;
8035{
8036 list_T *list;
8037 listitem_T *li;
8038 int score;
8039 char_u *p;
8040
8041 /* The work is split up in a few parts to avoid having to export
8042 * suginfo_T.
8043 * First evaluate the expression and get the resulting list. */
8044 list = eval_spell_expr(su->su_badword, expr);
8045 if (list != NULL)
8046 {
8047 /* Loop over the items in the list. */
8048 for (li = list->lv_first; li != NULL; li = li->li_next)
8049 if (li->li_tv.v_type == VAR_LIST)
8050 {
8051 /* Get the word and the score from the items. */
8052 score = get_spellword(li->li_tv.vval.v_list, &p);
8053 if (score >= 0)
8054 add_suggestion(su, &su->su_ga, p,
8055 su->su_badlen, score, 0, TRUE);
8056 }
8057 list_unref(list);
8058 }
8059
8060 /* Sort the suggestions and truncate at "maxcount". */
8061 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8062}
8063#endif
8064
8065/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008066 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008067 */
8068 static void
8069spell_suggest_file(su, fname)
8070 suginfo_T *su;
8071 char_u *fname;
8072{
8073 FILE *fd;
8074 char_u line[MAXWLEN * 2];
8075 char_u *p;
8076 int len;
8077 char_u cword[MAXWLEN];
8078
8079 /* Open the file. */
8080 fd = mch_fopen((char *)fname, "r");
8081 if (fd == NULL)
8082 {
8083 EMSG2(_(e_notopen), fname);
8084 return;
8085 }
8086
8087 /* Read it line by line. */
8088 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8089 {
8090 line_breakcheck();
8091
8092 p = vim_strchr(line, '/');
8093 if (p == NULL)
8094 continue; /* No Tab found, just skip the line. */
8095 *p++ = NUL;
8096 if (STRICMP(su->su_badword, line) == 0)
8097 {
8098 /* Match! Isolate the good word, until CR or NL. */
8099 for (len = 0; p[len] >= ' '; ++len)
8100 ;
8101 p[len] = NUL;
8102
8103 /* If the suggestion doesn't have specific case duplicate the case
8104 * of the bad word. */
8105 if (captype(p, NULL) == 0)
8106 {
8107 make_case_word(p, cword, su->su_badflags);
8108 p = cword;
8109 }
8110
8111 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8112 SCORE_FILE, 0, TRUE);
8113 }
8114 }
8115
8116 fclose(fd);
8117
8118 /* Sort the suggestions and truncate at "maxcount". */
8119 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8120}
8121
8122/*
8123 * Find suggestions for the internal method indicated by "sps_flags".
8124 */
8125 static void
8126spell_suggest_intern(su)
8127 suginfo_T *su;
8128{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008129 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008130 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008131 *
8132 * Set a maximum score to limit the combination of operations that is
8133 * tried.
8134 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008135 suggest_try_special(su);
8136
8137 /*
8138 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8139 * from the .aff file and inserting a space (split the word).
8140 */
8141 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008142
8143 /* For the resulting top-scorers compute the sound-a-like score. */
8144 if (sps_flags & SPS_DOUBLE)
8145 score_comp_sal(su);
8146
8147 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008148 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008149 *
8150 * Only do this when we don't have a lot of suggestions yet, because it's
8151 * very slow and often doesn't find new suggestions.
8152 */
8153 if ((sps_flags & SPS_DOUBLE)
8154 || (!(sps_flags & SPS_FAST)
8155 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8156 {
8157 /* Allow a higher score now. */
8158 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008159 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008160 }
8161
8162 /* When CTRL-C was hit while searching do show the results. */
8163 ui_breakcheck();
8164 if (got_int)
8165 {
8166 (void)vgetc();
8167 got_int = FALSE;
8168 }
8169
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008170 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008171 {
8172 if (sps_flags & SPS_BEST)
8173 /* Adjust the word score for how it sounds like. */
8174 rescore_suggestions(su);
8175
8176 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008177 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008178 }
8179}
8180
8181/*
8182 * Free the info put in "*su" by spell_find_suggest().
8183 */
8184 static void
8185spell_find_cleanup(su)
8186 suginfo_T *su;
8187{
8188 int i;
8189
8190 /* Free the suggestions. */
8191 for (i = 0; i < su->su_ga.ga_len; ++i)
8192 vim_free(SUG(su->su_ga, i).st_word);
8193 ga_clear(&su->su_ga);
8194 for (i = 0; i < su->su_sga.ga_len; ++i)
8195 vim_free(SUG(su->su_sga, i).st_word);
8196 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008197
8198 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008199 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200}
8201
8202/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008203 * Make a copy of "word", with the first letter upper or lower cased, to
8204 * "wcopy[MAXWLEN]". "word" must not be empty.
8205 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008206 */
8207 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008208onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008209 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008210 char_u *wcopy;
8211 int upper; /* TRUE: first letter made upper case */
8212{
8213 char_u *p;
8214 int c;
8215 int l;
8216
8217 p = word;
8218#ifdef FEAT_MBYTE
8219 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008220 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008221 else
8222#endif
8223 c = *p++;
8224 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008225 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008226 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008227 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008228#ifdef FEAT_MBYTE
8229 if (has_mbyte)
8230 l = mb_char2bytes(c, wcopy);
8231 else
8232#endif
8233 {
8234 l = 1;
8235 wcopy[0] = c;
8236 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008237 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008238}
8239
8240/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008241 * Make a copy of "word" with all the letters upper cased into
8242 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008243 */
8244 static void
8245allcap_copy(word, wcopy)
8246 char_u *word;
8247 char_u *wcopy;
8248{
8249 char_u *s;
8250 char_u *d;
8251 int c;
8252
8253 d = wcopy;
8254 for (s = word; *s != NUL; )
8255 {
8256#ifdef FEAT_MBYTE
8257 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008258 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008259 else
8260#endif
8261 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008262 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008263
8264#ifdef FEAT_MBYTE
8265 if (has_mbyte)
8266 {
8267 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8268 break;
8269 d += mb_char2bytes(c, d);
8270 }
8271 else
8272#endif
8273 {
8274 if (d - wcopy >= MAXWLEN - 1)
8275 break;
8276 *d++ = c;
8277 }
8278 }
8279 *d = NUL;
8280}
8281
8282/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008283 * Try finding suggestions by recognizing specific situations.
8284 */
8285 static void
8286suggest_try_special(su)
8287 suginfo_T *su;
8288{
8289 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008290 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008291 int c;
8292 char_u word[MAXWLEN];
8293
8294 /*
8295 * Recognize a word that is repeated: "the the".
8296 */
8297 p = skiptowhite(su->su_fbadword);
8298 len = p - su->su_fbadword;
8299 p = skipwhite(p);
8300 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
8301 {
8302 /* Include badflags: if the badword is onecap or allcap
8303 * use that for the goodword too: "The the" -> "The". */
8304 c = su->su_fbadword[len];
8305 su->su_fbadword[len] = NUL;
8306 make_case_word(su->su_fbadword, word, su->su_badflags);
8307 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008308 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008309 }
8310}
8311
8312/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008313 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00008314 *
8315 * This uses a state machine. At each node in the tree we try various
8316 * operations. When trying if an operation work "depth" is increased and the
8317 * stack[] is used to store info. This allows combinations, thus insert one
8318 * character, replace one and delete another. The number of changes is
8319 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 */
8321 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008322suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008323 suginfo_T *su;
8324{
8325 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
8326 char_u tword[MAXWLEN]; /* good word collected so far */
8327 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008328 char_u preword[MAXWLEN * 3]; /* word found with proper case;
8329 * concatanation of prefix compound
8330 * words and split word. NUL terminated
8331 * when going deeper but not when coming
8332 * back. */
8333 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 trystate_T *sp;
8335 int newscore;
8336 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008337 char_u *byts, *fbyts, *pbyts;
8338 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008340 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008341 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008342 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008343 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008344 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008345 int len;
8346 char_u *p;
8347 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00008348 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008349 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008350 slang_T *slang;
8351 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008352
8353 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00008354 * to find matches (esp. REP items). Append some more text, changing
8355 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008356 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008357 n = STRLEN(fword);
8358 p = su->su_badptr + su->su_badlen;
8359 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008360
8361 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8362 lp->lp_slang != NULL; ++lp)
8363 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008364 slang = lp->lp_slang;
8365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008366 /*
8367 * Go through the whole case-fold tree, try changes at each node.
8368 * "tword[]" contains the word collected from nodes in the tree.
8369 * "fword[]" the word we are trying to match with (initially the bad
8370 * word).
8371 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008372 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008373 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008374 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008375 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008376 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008377
Bram Moolenaarea424162005-06-16 21:51:00 +00008378 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008379 * When there are postponed prefixes we need to use these first. At
8380 * the end of the prefix we continue in the case-fold tree.
8381 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008382 fbyts = slang->sl_fbyts;
8383 fidxs = slang->sl_fidxs;
8384 pbyts = slang->sl_pbyts;
8385 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008386 if (pbyts != NULL)
8387 {
8388 byts = pbyts;
8389 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008390 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008391 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
8392 }
8393 else
8394 {
8395 byts = fbyts;
8396 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008397 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008398 }
8399
8400 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00008401 * Loop to find all suggestions. At each round we either:
8402 * - For the current state try one operation, advance "ts_curi",
8403 * increase "depth".
8404 * - When a state is done go to the next, set "ts_state".
8405 * - When all states are tried decrease "depth".
8406 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 while (depth >= 0 && !got_int)
8408 {
8409 sp = &stack[depth];
8410 switch (sp->ts_state)
8411 {
8412 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008413 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 /*
8415 * Start of node: Deal with NUL bytes, which means
8416 * tword[] may end here.
8417 */
8418 arridx = sp->ts_arridx; /* current node in the tree */
8419 len = byts[arridx]; /* bytes in this node */
8420 arridx += sp->ts_curi; /* index of current byte */
8421
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008422 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008423 {
8424 /* Skip over the NUL bytes, we use them later. */
8425 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
8426 ;
8427 sp->ts_curi += n;
8428
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008429 /* Always past NUL bytes now. */
8430 n = (int)sp->ts_state;
8431 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008432 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008433
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008434 /* At end of a prefix or at start of prefixtree: check for
8435 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008436 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008437 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00008438 /* Set su->su_badflags to the caps type at this
8439 * position. Use the caps type until here for the
8440 * prefix itself. */
8441#ifdef FEAT_MBYTE
8442 if (has_mbyte)
8443 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
8444 else
8445#endif
8446 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008447 flags = badword_captype(su->su_badptr,
8448 su->su_badptr + n);
8449 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008450 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008451 ++depth;
8452 stack[depth] = stack[depth - 1];
8453 sp = &stack[depth];
8454 sp->ts_prefixdepth = depth - 1;
8455 byts = fbyts;
8456 idxs = fidxs;
8457 sp->ts_state = STATE_START;
8458 sp->ts_curi = 1; /* start just after length byte */
8459 sp->ts_arridx = 0;
8460
Bram Moolenaar53805d12005-08-01 07:08:33 +00008461 /* Move the prefix to preword[] with the right case
8462 * and make find_keepcap_word() works. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008463 sp->ts_splitoff = sp->ts_twordlen;
8464 tword[sp->ts_splitoff] = NUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008465 make_case_word(tword, preword, flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008466 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008467 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008468 break;
8469 }
8470
Bram Moolenaar0c405862005-06-22 22:26:26 +00008471 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 {
8473 /* Past bytes in node and/or past NUL bytes. */
8474 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008475 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 break;
8477 }
8478
8479 /*
8480 * End of word in tree.
8481 */
8482 ++sp->ts_curi; /* eat one NUL byte */
8483
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008484 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008485 fword_ends = (fword[sp->ts_fidx] == NUL
8486 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
8487 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008488
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008489 if (sp->ts_prefixdepth == PFD_COMPOUND)
8490 {
8491 /* There was a compound word before this word. If this
8492 * word does not support compounding then give up
8493 * (splitting is tried for the word without compound
8494 * flag). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008495 if (((unsigned)flags >> 24) == 0
8496 || sp->ts_twordlen - sp->ts_splitoff
8497 < slang->sl_compminlen)
8498 break;
8499 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8500 compflags[sp->ts_complen + 1] = NUL;
8501 if (fword_ends && !can_compound(slang, tword, compflags))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008502 break;
8503 }
8504 else if (sp->ts_prefixdepth < MAXWLEN)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008505 {
8506 /* There was a prefix before the word. Check that the
8507 * prefix can be used with this word. */
8508 /* Count the length of the NULs in the prefix. If there
8509 * are none this must be the first try without a prefix.
8510 */
8511 n = stack[sp->ts_prefixdepth].ts_arridx;
8512 len = pbyts[n++];
8513 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
8514 ;
8515 if (c > 0)
8516 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008517 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008518 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008519 if (c == 0)
8520 break;
8521
8522 /* Use the WF_RARE flag for a rare prefix. */
8523 if (c & WF_RAREPFX)
8524 flags |= WF_RARE;
8525 }
8526 }
8527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008528 /*
8529 * Form the word with proper case in preword.
8530 * If there is a word from a previous split, append.
8531 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008532 if (flags & WF_KEEPCAP)
8533 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008534 find_keepcap_word(slang, tword + sp->ts_splitoff,
8535 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008536 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00008537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008538 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00008539 * use that for the goodword too. But if the badword is
8540 * allcap and it's only one char long use onecap. */
8541 c = su->su_badflags;
8542 if ((c & WF_ALLCAP)
8543#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008544 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008545#else
8546 && su->su_badlen == 1
8547#endif
8548 )
8549 c = WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008550 make_case_word(tword + sp->ts_splitoff,
8551 preword + sp->ts_prewordlen, flags | c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008552 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553
8554 /* Don't use a banned word. It may appear again as a good
8555 * word, thus remember it. */
8556 if (flags & WF_BANNED)
8557 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008558 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008559 break;
8560 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008561 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008562 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008563 break;
8564
8565 newscore = 0;
8566 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008567 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008568 newscore += SCORE_REGION;
8569 if (flags & WF_RARE)
8570 newscore += SCORE_RARE;
8571
Bram Moolenaar0c405862005-06-22 22:26:26 +00008572 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008573 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008574 newscore += SCORE_ICASE;
8575
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008576 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008577 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008578 /* The badword also ends: add suggestions. Give a penalty
8579 * when changing non-word char to word char, e.g., "thes,"
8580 * -> "these". */
8581 p = fword + sp->ts_fidx;
8582#ifdef FEAT_MBYTE
8583 if (has_mbyte)
8584 mb_ptr_back(fword, p);
8585 else
8586#endif
8587 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008588 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008589 {
8590 p = preword + STRLEN(preword);
8591#ifdef FEAT_MBYTE
8592 if (has_mbyte)
8593 mb_ptr_back(preword, p);
8594 else
8595#endif
8596 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008597 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008598 newscore += SCORE_NONWORD;
8599 }
8600
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008601 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008602 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008603 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008605 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00008606#ifdef FEAT_MBYTE
8607 /* Don't split halfway a character. */
8608 && (!has_mbyte || sp->ts_tcharlen == 0)
8609#endif
8610 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008611 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008612 int try_compound;
8613
8614 /* Get here in two situations:
8615 * 1. The word in the tree ends but the badword continues:
8616 * If the word allows compounding try that. Otherwise
8617 * try a split by inserting a space. For both check
8618 * that a valid words starts at fword[sp->ts_fidx].
8619 * 2. The badword does end, but it was due to a change
8620 * (e.g., a swap). No need to split, but do check that
8621 * the following word is valid.
8622 */
8623 if (!fword_ends
Bram Moolenaar5195e452005-08-19 20:32:47 +00008624 && ((unsigned)flags >> 24) != 0
8625 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008626 >= slang->sl_compminlen
Bram Moolenaar5195e452005-08-19 20:32:47 +00008627 && sp->ts_complen + 1 <= slang->sl_compmax
8628 && (sp->ts_complen > 0
8629 || vim_strchr(slang->sl_compstartflags,
8630 ((unsigned)flags >> 24)) != NULL))
8631 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008632 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008633 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8634 compflags[sp->ts_complen + 1] = NUL;
8635 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008636 else
8637 {
8638 try_compound = FALSE;
8639 if (!fword_ends)
8640 newscore += SCORE_SPLIT;
8641 }
8642
8643 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 {
8645 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008646 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008647 sp->ts_state = STATE_SPLITUNDO;
8648
8649 ++depth;
8650 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008651
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008652 /* Append a space to preword when splitting. */
8653 if (!try_compound && !fword_ends)
8654 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00008655 sp->ts_prewordlen = STRLEN(preword);
8656 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008657
8658 /* If the badword has a non-word character at this
8659 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008660 * non-word character with a space. Always skip a
8661 * character when the word ends. */
8662 if ((!try_compound
8663 && !spell_iswordp_nmw(fword + sp->ts_fidx))
8664 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008665 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008666 int l;
8667
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008668#ifdef FEAT_MBYTE
8669 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008670 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008671 else
8672#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008673 l = 1;
8674 if (fword_ends)
8675 {
8676 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008677 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008678 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008679 sp->ts_prewordlen += l;
8680 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008681 }
8682 else
8683 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
8684 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008685 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008686
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008687 /* set flag to check compound flag on following word */
8688 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00008689 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008690 sp->ts_prefixdepth = PFD_COMPOUND;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008691 ++sp->ts_complen;
8692 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008693 else
8694 sp->ts_prefixdepth = PFD_NOPREFIX;
8695
Bram Moolenaar53805d12005-08-01 07:08:33 +00008696 /* set su->su_badflags to the caps type at this
8697 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008698#ifdef FEAT_MBYTE
8699 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00008700 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008701 else
8702#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00008703 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008704 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008705 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008707 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008708 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008709 }
8710 }
8711 break;
8712
8713 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008714 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008715 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716
8717 /* Continue looking for NUL bytes. */
8718 sp->ts_state = STATE_START;
8719 break;
8720
8721 case STATE_ENDNUL:
8722 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008723 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008724 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008725 {
8726 /* The badword ends, can't use the bytes in this node. */
8727 sp->ts_state = STATE_DEL;
8728 break;
8729 }
8730 sp->ts_state = STATE_PLAIN;
8731 /*FALLTHROUGH*/
8732
8733 case STATE_PLAIN:
8734 /*
8735 * Go over all possible bytes at this node, add each to
8736 * tword[] and use child node. "ts_curi" is the index.
8737 */
8738 arridx = sp->ts_arridx;
8739 if (sp->ts_curi > byts[arridx])
8740 {
8741 /* Done all bytes at this node, do next state. When still
8742 * at already changed bytes skip the other tricks. */
8743 if (sp->ts_fidx >= sp->ts_fidxtry)
8744 sp->ts_state = STATE_DEL;
8745 else
8746 sp->ts_state = STATE_FINAL;
8747 }
8748 else
8749 {
8750 arridx += sp->ts_curi++;
8751 c = byts[arridx];
8752
8753 /* Normal byte, go one level deeper. If it's not equal to
8754 * the byte in the bad word adjust the score. But don't
8755 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00008756 if (c == fword[sp->ts_fidx]
8757#ifdef FEAT_MBYTE
8758 || (sp->ts_tcharlen > 0
8759 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008760#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008761 )
8762 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008763 else
8764 newscore = SCORE_SUBST;
8765 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
8766 && try_deeper(su, stack, depth, newscore))
8767 {
8768 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008769 sp = &stack[depth];
8770 ++sp->ts_fidx;
8771 tword[sp->ts_twordlen++] = c;
8772 sp->ts_arridx = idxs[arridx];
8773#ifdef FEAT_MBYTE
8774 if (newscore == SCORE_SUBST)
8775 sp->ts_isdiff = DIFF_YES;
8776 if (has_mbyte)
8777 {
8778 /* Multi-byte characters are a bit complicated to
8779 * handle: They differ when any of the bytes
8780 * differ and then their length may also differ. */
8781 if (sp->ts_tcharlen == 0)
8782 {
8783 /* First byte. */
8784 sp->ts_tcharidx = 0;
8785 sp->ts_tcharlen = MB_BYTE2LEN(c);
8786 sp->ts_fcharstart = sp->ts_fidx - 1;
8787 sp->ts_isdiff = (newscore != 0)
8788 ? DIFF_YES : DIFF_NONE;
8789 }
8790 else if (sp->ts_isdiff == DIFF_INSERT)
8791 /* When inserting trail bytes don't advance in
8792 * the bad word. */
8793 --sp->ts_fidx;
8794 if (++sp->ts_tcharidx == sp->ts_tcharlen)
8795 {
8796 /* Last byte of character. */
8797 if (sp->ts_isdiff == DIFF_YES)
8798 {
8799 /* Correct ts_fidx for the byte length of
8800 * the character (we didn't check that
8801 * before). */
8802 sp->ts_fidx = sp->ts_fcharstart
8803 + MB_BYTE2LEN(
8804 fword[sp->ts_fcharstart]);
8805
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008806 /* For changing a composing character
8807 * adjust the score from SCORE_SUBST to
8808 * SCORE_SUBCOMP. */
8809 if (enc_utf8
8810 && utf_iscomposing(
8811 mb_ptr2char(tword
8812 + sp->ts_twordlen
8813 - sp->ts_tcharlen))
8814 && utf_iscomposing(
8815 mb_ptr2char(fword
8816 + sp->ts_fcharstart)))
8817 sp->ts_score -=
8818 SCORE_SUBST - SCORE_SUBCOMP;
8819
Bram Moolenaarea424162005-06-16 21:51:00 +00008820 /* For a similar character adjust score
8821 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008822 else if (slang->sl_has_map
8823 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00008824 mb_ptr2char(tword
8825 + sp->ts_twordlen
8826 - sp->ts_tcharlen),
8827 mb_ptr2char(fword
8828 + sp->ts_fcharstart)))
8829 sp->ts_score -=
8830 SCORE_SUBST - SCORE_SIMILAR;
8831 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008832 else if (sp->ts_isdiff == DIFF_INSERT
8833 && sp->ts_twordlen > sp->ts_tcharlen)
8834 {
Bram Moolenaarea408852005-06-25 22:49:46 +00008835 p = tword + sp->ts_twordlen
8836 - sp->ts_tcharlen;
8837 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008838 if (enc_utf8 && utf_iscomposing(c))
8839 {
8840 /* Inserting a composing char doesn't
8841 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008842 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008843 - SCORE_INSCOMP;
8844 }
8845 else
8846 {
8847 /* If the previous character was the
8848 * same, thus doubling a character,
8849 * give a bonus to the score. */
8850 mb_ptr_back(tword, p);
8851 if (c == mb_ptr2char(p))
8852 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00008853 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008854 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008855 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008856
8857 /* Starting a new char, reset the length. */
8858 sp->ts_tcharlen = 0;
8859 }
8860 }
8861 else
8862#endif
8863 {
8864 /* If we found a similar char adjust the score.
8865 * We do this after calling try_deeper() because
8866 * it's slow. */
8867 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008868 && slang->sl_has_map
8869 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00008870 c, fword[sp->ts_fidx - 1]))
8871 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
8872 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008873 }
8874 }
8875 break;
8876
8877 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00008878#ifdef FEAT_MBYTE
8879 /* When past the first byte of a multi-byte char don't try
8880 * delete/insert/swap a character. */
8881 if (has_mbyte && sp->ts_tcharlen > 0)
8882 {
8883 sp->ts_state = STATE_FINAL;
8884 break;
8885 }
8886#endif
8887 /*
8888 * Try skipping one character in the bad word (delete it).
8889 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008890 sp->ts_state = STATE_INS;
8891 sp->ts_curi = 1;
8892 if (fword[sp->ts_fidx] != NUL
8893 && try_deeper(su, stack, depth, SCORE_DEL))
8894 {
8895 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00008896
8897 /* Advance over the character in fword[]. Give a bonus to
8898 * the score if the same character is following "nn" ->
8899 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00008900#ifdef FEAT_MBYTE
8901 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00008902 {
8903 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00008904 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008905 if (enc_utf8 && utf_iscomposing(c))
8906 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
8907 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00008908 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
8909 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008910 else
8911#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00008912 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008913 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00008914 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
8915 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
8916 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 break;
8918 }
8919 /*FALLTHROUGH*/
8920
8921 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00008922 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008923 * node. */
8924 n = sp->ts_arridx;
8925 if (sp->ts_curi > byts[n])
8926 {
8927 /* Done all bytes at this node, do next state. */
8928 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008929 }
8930 else
8931 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008932 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 n += sp->ts_curi++;
8934 c = byts[n];
8935 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
8936 {
8937 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008938 sp = &stack[depth];
8939 tword[sp->ts_twordlen++] = c;
8940 sp->ts_arridx = idxs[n];
8941#ifdef FEAT_MBYTE
8942 if (has_mbyte)
8943 {
8944 fl = MB_BYTE2LEN(c);
8945 if (fl > 1)
8946 {
8947 /* There are following bytes for the same
8948 * character. We must find all bytes before
8949 * trying delete/insert/swap/etc. */
8950 sp->ts_tcharlen = fl;
8951 sp->ts_tcharidx = 1;
8952 sp->ts_isdiff = DIFF_INSERT;
8953 }
8954 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008955 else
8956 fl = 1;
8957 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00008958#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00008959 {
8960 /* If the previous character was the same, thus
8961 * doubling a character, give a bonus to the
8962 * score. */
8963 if (sp->ts_twordlen >= 2
8964 && tword[sp->ts_twordlen - 2] == c)
8965 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
8966 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 }
8968 }
8969 break;
8970
8971 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00008972 /*
8973 * Swap two bytes in the bad word: "12" -> "21".
8974 * We change "fword" here, it's changed back afterwards.
8975 */
8976 p = fword + sp->ts_fidx;
8977 c = *p;
8978 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 {
Bram Moolenaarea424162005-06-16 21:51:00 +00008980 /* End of word, can't swap or replace. */
8981 sp->ts_state = STATE_FINAL;
8982 break;
8983 }
8984#ifdef FEAT_MBYTE
8985 if (has_mbyte)
8986 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008987 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00008988 c = mb_ptr2char(p);
8989 c2 = mb_ptr2char(p + n);
8990 }
8991 else
8992#endif
8993 c2 = p[1];
8994 if (c == c2)
8995 {
8996 /* Characters are identical, swap won't do anything. */
8997 sp->ts_state = STATE_SWAP3;
8998 break;
8999 }
9000 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9001 {
9002 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009003 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009004#ifdef FEAT_MBYTE
9005 if (has_mbyte)
9006 {
9007 fl = mb_char2len(c2);
9008 mch_memmove(p, p + n, fl);
9009 mb_char2bytes(c, p + fl);
9010 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9011 }
9012 else
9013#endif
9014 {
9015 p[0] = c2;
9016 p[1] = c;
9017 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9018 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 }
9020 else
9021 /* If this swap doesn't work then SWAP3 won't either. */
9022 sp->ts_state = STATE_REP_INI;
9023 break;
9024
Bram Moolenaarea424162005-06-16 21:51:00 +00009025 case STATE_UNSWAP:
9026 /* Undo the STATE_SWAP swap: "21" -> "12". */
9027 p = fword + sp->ts_fidx;
9028#ifdef FEAT_MBYTE
9029 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009030 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009031 n = MB_BYTE2LEN(*p);
9032 c = mb_ptr2char(p + n);
9033 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9034 mb_char2bytes(c, p);
9035 }
9036 else
9037#endif
9038 {
9039 c = *p;
9040 *p = p[1];
9041 p[1] = c;
9042 }
9043 /*FALLTHROUGH*/
9044
9045 case STATE_SWAP3:
9046 /* Swap two bytes, skipping one: "123" -> "321". We change
9047 * "fword" here, it's changed back afterwards. */
9048 p = fword + sp->ts_fidx;
9049#ifdef FEAT_MBYTE
9050 if (has_mbyte)
9051 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009052 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009053 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009054 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009055 c2 = mb_ptr2char(p + n);
9056 c3 = mb_ptr2char(p + n + fl);
9057 }
9058 else
9059#endif
9060 {
9061 c = *p;
9062 c2 = p[1];
9063 c3 = p[2];
9064 }
9065
9066 /* When characters are identical: "121" then SWAP3 result is
9067 * identical, ROT3L result is same as SWAP: "211", ROT3L
9068 * result is same as SWAP on next char: "112". Thus skip all
9069 * swapping. Also skip when c3 is NUL. */
9070 if (c == c3 || c3 == NUL)
9071 {
9072 sp->ts_state = STATE_REP_INI;
9073 break;
9074 }
9075 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9076 {
9077 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009079#ifdef FEAT_MBYTE
9080 if (has_mbyte)
9081 {
9082 tl = mb_char2len(c3);
9083 mch_memmove(p, p + n + fl, tl);
9084 mb_char2bytes(c2, p + tl);
9085 mb_char2bytes(c, p + fl + tl);
9086 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9087 }
9088 else
9089#endif
9090 {
9091 p[0] = p[2];
9092 p[2] = c;
9093 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9094 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009095 }
9096 else
9097 sp->ts_state = STATE_REP_INI;
9098 break;
9099
Bram Moolenaarea424162005-06-16 21:51:00 +00009100 case STATE_UNSWAP3:
9101 /* Undo STATE_SWAP3: "321" -> "123" */
9102 p = fword + sp->ts_fidx;
9103#ifdef FEAT_MBYTE
9104 if (has_mbyte)
9105 {
9106 n = MB_BYTE2LEN(*p);
9107 c2 = mb_ptr2char(p + n);
9108 fl = MB_BYTE2LEN(p[n]);
9109 c = mb_ptr2char(p + n + fl);
9110 tl = MB_BYTE2LEN(p[n + fl]);
9111 mch_memmove(p + fl + tl, p, n);
9112 mb_char2bytes(c, p);
9113 mb_char2bytes(c2, p + tl);
9114 }
9115 else
9116#endif
9117 {
9118 c = *p;
9119 *p = p[2];
9120 p[2] = c;
9121 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009122
Bram Moolenaarea424162005-06-16 21:51:00 +00009123 /* Rotate three characters left: "123" -> "231". We change
9124 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9126 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009127 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009129 p = fword + sp->ts_fidx;
9130#ifdef FEAT_MBYTE
9131 if (has_mbyte)
9132 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009133 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009134 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009135 fl = mb_cptr2len(p + n);
9136 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009137 mch_memmove(p, p + n, fl);
9138 mb_char2bytes(c, p + fl);
9139 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9140 }
9141 else
9142#endif
9143 {
9144 c = *p;
9145 *p = p[1];
9146 p[1] = p[2];
9147 p[2] = c;
9148 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 }
9151 else
9152 sp->ts_state = STATE_REP_INI;
9153 break;
9154
Bram Moolenaarea424162005-06-16 21:51:00 +00009155 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009156 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009157 p = fword + sp->ts_fidx;
9158#ifdef FEAT_MBYTE
9159 if (has_mbyte)
9160 {
9161 n = MB_BYTE2LEN(*p);
9162 n += MB_BYTE2LEN(p[n]);
9163 c = mb_ptr2char(p + n);
9164 tl = MB_BYTE2LEN(p[n]);
9165 mch_memmove(p + tl, p, n);
9166 mb_char2bytes(c, p);
9167 }
9168 else
9169#endif
9170 {
9171 c = p[2];
9172 p[2] = p[1];
9173 p[1] = *p;
9174 *p = c;
9175 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00009178 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9180 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009181 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009183 p = fword + sp->ts_fidx;
9184#ifdef FEAT_MBYTE
9185 if (has_mbyte)
9186 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009187 n = mb_cptr2len(p);
9188 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009189 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009190 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009191 mch_memmove(p + tl, p, n);
9192 mb_char2bytes(c, p);
9193 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
9194 }
9195 else
9196#endif
9197 {
9198 c = p[2];
9199 p[2] = p[1];
9200 p[1] = *p;
9201 *p = c;
9202 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 }
9205 else
9206 sp->ts_state = STATE_REP_INI;
9207 break;
9208
Bram Moolenaarea424162005-06-16 21:51:00 +00009209 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009210 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009211 p = fword + sp->ts_fidx;
9212#ifdef FEAT_MBYTE
9213 if (has_mbyte)
9214 {
9215 c = mb_ptr2char(p);
9216 tl = MB_BYTE2LEN(*p);
9217 n = MB_BYTE2LEN(p[tl]);
9218 n += MB_BYTE2LEN(p[tl + n]);
9219 mch_memmove(p, p + tl, n);
9220 mb_char2bytes(c, p + n);
9221 }
9222 else
9223#endif
9224 {
9225 c = *p;
9226 *p = p[1];
9227 p[1] = p[2];
9228 p[2] = c;
9229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 /*FALLTHROUGH*/
9231
9232 case STATE_REP_INI:
9233 /* Check if matching with REP items from the .aff file would
9234 * work. Quickly skip if there are no REP items or the score
9235 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009236 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 if (gap->ga_len == 0
9238 || sp->ts_score + SCORE_REP >= su->su_maxscore)
9239 {
9240 sp->ts_state = STATE_FINAL;
9241 break;
9242 }
9243
9244 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00009245 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009246 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 if (sp->ts_curi < 0)
9248 {
9249 sp->ts_state = STATE_FINAL;
9250 break;
9251 }
9252
9253 sp->ts_state = STATE_REP;
9254 /*FALLTHROUGH*/
9255
9256 case STATE_REP:
9257 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00009258 * match replace the characters and check if the resulting
9259 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 p = fword + sp->ts_fidx;
9261
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009262 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 while (sp->ts_curi < gap->ga_len)
9264 {
9265 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
9266 if (*ftp->ft_from != *p)
9267 {
9268 /* past possible matching entries */
9269 sp->ts_curi = gap->ga_len;
9270 break;
9271 }
9272 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
9273 && try_deeper(su, stack, depth, SCORE_REP))
9274 {
9275 /* Need to undo this afterwards. */
9276 sp->ts_state = STATE_REP_UNDO;
9277
9278 /* Change the "from" to the "to" string. */
9279 ++depth;
9280 fl = STRLEN(ftp->ft_from);
9281 tl = STRLEN(ftp->ft_to);
9282 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009284 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009285 repextra += tl - fl;
9286 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009287 mch_memmove(p, ftp->ft_to, tl);
9288 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00009289#ifdef FEAT_MBYTE
9290 stack[depth].ts_tcharlen = 0;
9291#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009292 break;
9293 }
9294 }
9295
9296 if (sp->ts_curi >= gap->ga_len)
9297 /* No (more) matches. */
9298 sp->ts_state = STATE_FINAL;
9299
9300 break;
9301
9302 case STATE_REP_UNDO:
9303 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009304 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 fl = STRLEN(ftp->ft_from);
9306 tl = STRLEN(ftp->ft_to);
9307 p = fword + sp->ts_fidx;
9308 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009311 repextra -= tl - fl;
9312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009313 mch_memmove(p, ftp->ft_from, fl);
9314 sp->ts_state = STATE_REP;
9315 break;
9316
9317 default:
9318 /* Did all possible states at this level, go up one level. */
9319 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009320
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009321 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009322 {
9323 /* Continue in or go back to the prefix tree. */
9324 byts = pbyts;
9325 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009326 }
9327
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009328 /* Don't check for CTRL-C too often, it takes time. */
9329 line_breakcheck();
9330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009331 }
9332 }
9333}
9334
9335/*
9336 * Try going one level deeper in the tree.
9337 */
9338 static int
9339try_deeper(su, stack, depth, score_add)
9340 suginfo_T *su;
9341 trystate_T *stack;
9342 int depth;
9343 int score_add;
9344{
9345 int newscore;
9346
9347 /* Refuse to go deeper if the scrore is getting too big. */
9348 newscore = stack[depth].ts_score + score_add;
9349 if (newscore >= su->su_maxscore)
9350 return FALSE;
9351
Bram Moolenaarea424162005-06-16 21:51:00 +00009352 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353 stack[depth + 1].ts_state = STATE_START;
9354 stack[depth + 1].ts_score = newscore;
9355 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 return TRUE;
9357}
9358
Bram Moolenaar53805d12005-08-01 07:08:33 +00009359#ifdef FEAT_MBYTE
9360/*
9361 * Case-folding may change the number of bytes: Count nr of chars in
9362 * fword[flen] and return the byte length of that many chars in "word".
9363 */
9364 static int
9365nofold_len(fword, flen, word)
9366 char_u *fword;
9367 int flen;
9368 char_u *word;
9369{
9370 char_u *p;
9371 int i = 0;
9372
9373 for (p = fword; p < fword + flen; mb_ptr_adv(p))
9374 ++i;
9375 for (p = word; i > 0; mb_ptr_adv(p))
9376 --i;
9377 return (int)(p - word);
9378}
9379#endif
9380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381/*
9382 * "fword" is a good word with case folded. Find the matching keep-case
9383 * words and put it in "kword".
9384 * Theoretically there could be several keep-case words that result in the
9385 * same case-folded word, but we only find one...
9386 */
9387 static void
9388find_keepcap_word(slang, fword, kword)
9389 slang_T *slang;
9390 char_u *fword;
9391 char_u *kword;
9392{
9393 char_u uword[MAXWLEN]; /* "fword" in upper-case */
9394 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009395 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009396
9397 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009398 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009399 int round[MAXWLEN];
9400 int fwordidx[MAXWLEN];
9401 int uwordidx[MAXWLEN];
9402 int kwordlen[MAXWLEN];
9403
9404 int flen, ulen;
9405 int l;
9406 int len;
9407 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009408 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 char_u *p;
9410 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009411 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412
9413 if (byts == NULL)
9414 {
9415 /* array is empty: "cannot happen" */
9416 *kword = NUL;
9417 return;
9418 }
9419
9420 /* Make an all-cap version of "fword". */
9421 allcap_copy(fword, uword);
9422
9423 /*
9424 * Each character needs to be tried both case-folded and upper-case.
9425 * All this gets very complicated if we keep in mind that changing case
9426 * may change the byte length of a multi-byte character...
9427 */
9428 depth = 0;
9429 arridx[0] = 0;
9430 round[0] = 0;
9431 fwordidx[0] = 0;
9432 uwordidx[0] = 0;
9433 kwordlen[0] = 0;
9434 while (depth >= 0)
9435 {
9436 if (fword[fwordidx[depth]] == NUL)
9437 {
9438 /* We are at the end of "fword". If the tree allows a word to end
9439 * here we have found a match. */
9440 if (byts[arridx[depth] + 1] == 0)
9441 {
9442 kword[kwordlen[depth]] = NUL;
9443 return;
9444 }
9445
9446 /* kword is getting too long, continue one level up */
9447 --depth;
9448 }
9449 else if (++round[depth] > 2)
9450 {
9451 /* tried both fold-case and upper-case character, continue one
9452 * level up */
9453 --depth;
9454 }
9455 else
9456 {
9457 /*
9458 * round[depth] == 1: Try using the folded-case character.
9459 * round[depth] == 2: Try using the upper-case character.
9460 */
9461#ifdef FEAT_MBYTE
9462 if (has_mbyte)
9463 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009464 flen = mb_cptr2len(fword + fwordidx[depth]);
9465 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009466 }
9467 else
9468#endif
9469 ulen = flen = 1;
9470 if (round[depth] == 1)
9471 {
9472 p = fword + fwordidx[depth];
9473 l = flen;
9474 }
9475 else
9476 {
9477 p = uword + uwordidx[depth];
9478 l = ulen;
9479 }
9480
9481 for (tryidx = arridx[depth]; l > 0; --l)
9482 {
9483 /* Perform a binary search in the list of accepted bytes. */
9484 len = byts[tryidx++];
9485 c = *p++;
9486 lo = tryidx;
9487 hi = tryidx + len - 1;
9488 while (lo < hi)
9489 {
9490 m = (lo + hi) / 2;
9491 if (byts[m] > c)
9492 hi = m - 1;
9493 else if (byts[m] < c)
9494 lo = m + 1;
9495 else
9496 {
9497 lo = hi = m;
9498 break;
9499 }
9500 }
9501
9502 /* Stop if there is no matching byte. */
9503 if (hi < lo || byts[lo] != c)
9504 break;
9505
9506 /* Continue at the child (if there is one). */
9507 tryidx = idxs[lo];
9508 }
9509
9510 if (l == 0)
9511 {
9512 /*
9513 * Found the matching char. Copy it to "kword" and go a
9514 * level deeper.
9515 */
9516 if (round[depth] == 1)
9517 {
9518 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
9519 flen);
9520 kwordlen[depth + 1] = kwordlen[depth] + flen;
9521 }
9522 else
9523 {
9524 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
9525 ulen);
9526 kwordlen[depth + 1] = kwordlen[depth] + ulen;
9527 }
9528 fwordidx[depth + 1] = fwordidx[depth] + flen;
9529 uwordidx[depth + 1] = uwordidx[depth] + ulen;
9530
9531 ++depth;
9532 arridx[depth] = tryidx;
9533 round[depth] = 0;
9534 }
9535 }
9536 }
9537
9538 /* Didn't find it: "cannot happen". */
9539 *kword = NUL;
9540}
9541
9542/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009543 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
9544 * su->su_sga.
9545 */
9546 static void
9547score_comp_sal(su)
9548 suginfo_T *su;
9549{
9550 langp_T *lp;
9551 char_u badsound[MAXWLEN];
9552 int i;
9553 suggest_T *stp;
9554 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009555 int score;
9556
9557 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
9558 return;
9559
9560 /* Use the sound-folding of the first language that supports it. */
9561 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9562 lp->lp_slang != NULL; ++lp)
9563 if (lp->lp_slang->sl_sal.ga_len > 0)
9564 {
9565 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009566 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009567
9568 for (i = 0; i < su->su_ga.ga_len; ++i)
9569 {
9570 stp = &SUG(su->su_ga, i);
9571
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009572 /* Case-fold the suggested word, sound-fold it and compute the
9573 * sound-a-like score. */
9574 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009575 if (score < SCORE_MAXMAX)
9576 {
9577 /* Add the suggestion. */
9578 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
9579 sstp->st_word = vim_strsave(stp->st_word);
9580 if (sstp->st_word != NULL)
9581 {
9582 sstp->st_score = score;
9583 sstp->st_altscore = 0;
9584 sstp->st_orglen = stp->st_orglen;
9585 ++su->su_sga.ga_len;
9586 }
9587 }
9588 }
9589 break;
9590 }
9591}
9592
9593/*
9594 * Combine the list of suggestions in su->su_ga and su->su_sga.
9595 * They are intwined.
9596 */
9597 static void
9598score_combine(su)
9599 suginfo_T *su;
9600{
9601 int i;
9602 int j;
9603 garray_T ga;
9604 garray_T *gap;
9605 langp_T *lp;
9606 suggest_T *stp;
9607 char_u *p;
9608 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009609 int round;
9610
9611 /* Add the alternate score to su_ga. */
9612 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9613 lp->lp_slang != NULL; ++lp)
9614 {
9615 if (lp->lp_slang->sl_sal.ga_len > 0)
9616 {
9617 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009618 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009619
9620 for (i = 0; i < su->su_ga.ga_len; ++i)
9621 {
9622 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009623 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
9624 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009625 if (stp->st_altscore == SCORE_MAXMAX)
9626 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
9627 else
9628 stp->st_score = (stp->st_score * 3
9629 + stp->st_altscore) / 4;
9630 stp->st_salscore = FALSE;
9631 }
9632 break;
9633 }
9634 }
9635
9636 /* Add the alternate score to su_sga. */
9637 for (i = 0; i < su->su_sga.ga_len; ++i)
9638 {
9639 stp = &SUG(su->su_sga, i);
9640 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
9641 if (stp->st_score == SCORE_MAXMAX)
9642 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
9643 else
9644 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
9645 stp->st_salscore = TRUE;
9646 }
9647
9648 /* Sort the suggestions and truncate at "maxcount" for both lists. */
9649 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9650 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
9651
9652 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
9653 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
9654 return;
9655
9656 stp = &SUG(ga, 0);
9657 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
9658 {
9659 /* round 1: get a suggestion from su_ga
9660 * round 2: get a suggestion from su_sga */
9661 for (round = 1; round <= 2; ++round)
9662 {
9663 gap = round == 1 ? &su->su_ga : &su->su_sga;
9664 if (i < gap->ga_len)
9665 {
9666 /* Don't add a word if it's already there. */
9667 p = SUG(*gap, i).st_word;
9668 for (j = 0; j < ga.ga_len; ++j)
9669 if (STRCMP(stp[j].st_word, p) == 0)
9670 break;
9671 if (j == ga.ga_len)
9672 stp[ga.ga_len++] = SUG(*gap, i);
9673 else
9674 vim_free(p);
9675 }
9676 }
9677 }
9678
9679 ga_clear(&su->su_ga);
9680 ga_clear(&su->su_sga);
9681
9682 /* Truncate the list to the number of suggestions that will be displayed. */
9683 if (ga.ga_len > su->su_maxcount)
9684 {
9685 for (i = su->su_maxcount; i < ga.ga_len; ++i)
9686 vim_free(stp[i].st_word);
9687 ga.ga_len = su->su_maxcount;
9688 }
9689
9690 su->su_ga = ga;
9691}
9692
9693/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009694 * For the goodword in "stp" compute the soundalike score compared to the
9695 * badword.
9696 */
9697 static int
9698stp_sal_score(stp, su, slang, badsound)
9699 suggest_T *stp;
9700 suginfo_T *su;
9701 slang_T *slang;
9702 char_u *badsound; /* sound-folded badword */
9703{
9704 char_u *p;
9705 char_u badsound2[MAXWLEN];
9706 char_u fword[MAXWLEN];
9707 char_u goodsound[MAXWLEN];
9708
9709 if (stp->st_orglen <= su->su_badlen)
9710 p = badsound;
9711 else
9712 {
9713 /* soundfold the bad word with more characters following */
9714 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
9715
9716 /* When joining two words the sound often changes a lot. E.g., "t he"
9717 * sounds like "t h" while "the" sounds like "@". Avoid that by
9718 * removing the space. Don't do it when the good word also contains a
9719 * space. */
9720 if (vim_iswhite(su->su_badptr[su->su_badlen])
9721 && *skiptowhite(stp->st_word) == NUL)
9722 for (p = fword; *(p = skiptowhite(p)) != NUL; )
9723 mch_memmove(p, p + 1, STRLEN(p));
9724
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009725 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009726 p = badsound2;
9727 }
9728
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009729 /* Sound-fold the word and compute the score for the difference. */
9730 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009731
9732 return soundalike_score(goodsound, p);
9733}
9734
9735/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 * Find suggestions by comparing the word in a sound-a-like form.
9737 */
9738 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009739suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 suginfo_T *su;
9741{
9742 char_u salword[MAXWLEN];
9743 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009745 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009746 int curi[MAXWLEN];
9747 langp_T *lp;
9748 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009749 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 int depth;
9751 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009752 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009753 int round;
9754 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009755 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009756 int local_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009758 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9760 lp->lp_slang != NULL; ++lp)
9761 {
9762 if (lp->lp_slang->sl_sal.ga_len > 0)
9763 {
9764 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009765 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009766
9767 /*
9768 * Go through the whole tree, soundfold each word and compare.
9769 * round 1: use the case-folded tree.
9770 * round 2: use the keep-case tree.
9771 */
9772 for (round = 1; round <= 2; ++round)
9773 {
9774 if (round == 1)
9775 {
9776 byts = lp->lp_slang->sl_fbyts;
9777 idxs = lp->lp_slang->sl_fidxs;
9778 }
9779 else
9780 {
9781 byts = lp->lp_slang->sl_kbyts;
9782 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009783 if (byts == NULL) /* no keep-case words */
9784 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 }
9786
9787 depth = 0;
9788 arridx[0] = 0;
9789 curi[0] = 1;
9790 while (depth >= 0 && !got_int)
9791 {
9792 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 /* Done all bytes at this node, go up one level. */
9795 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009796 line_breakcheck();
9797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009798 else
9799 {
9800 /* Do one more byte at this node. */
9801 n = arridx[depth] + curi[depth];
9802 ++curi[depth];
9803 c = byts[n];
9804 if (c == 0)
9805 {
9806 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009807 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 if (round == 2 || (flags & WF_KEEPCAP) == 0)
9809 {
9810 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009811 /* Sound-fold. Only in keep-case tree need to
9812 * case-fold the word. */
9813 spell_soundfold(lp->lp_slang, tword,
9814 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009815
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009816 /* Compute the edit distance between the
9817 * sound-a-like words. */
9818 sound_score = soundalike_score(salword,
9819 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009820
9821 /* Add a penalty for words in another region. */
9822 if ((flags & WF_REGION) && (((unsigned)flags
9823 >> 16) & lp->lp_region) == 0)
9824 local_score = SCORE_REGION;
9825 else
9826 local_score = 0;
9827 sound_score += local_score;
9828
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009829 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009830 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009831 char_u cword[MAXWLEN];
9832 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009833 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009834
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009835 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009836 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009838 /* Need to fix case according to
9839 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009840 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009841 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009842 }
9843 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009844 p = tword;
9845
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009846 if (sps_flags & SPS_DOUBLE)
9847 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009848 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009849 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009850 else
9851 {
9852 /* Compute the score. */
9853 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +00009854 su->su_badword, p)
9855 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009856 if (sps_flags & SPS_BEST)
9857 /* give a bonus for the good word
9858 * sounding the same as the bad
9859 * word */
9860 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009861 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009862 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009863 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009864 else
9865 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009866 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009867 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009868 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009869 }
9870 }
9871
9872 /* Skip over other NUL bytes. */
9873 while (byts[n + 1] == 0)
9874 {
9875 ++n;
9876 ++curi[depth];
9877 }
9878 }
9879 else
9880 {
9881 /* Normal char, go one level deeper. */
9882 tword[depth++] = c;
9883 arridx[depth] = idxs[n];
9884 curi[depth] = 1;
9885 }
9886 }
9887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 }
9889 }
9890 }
9891}
9892
9893/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009894 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 */
9896 static void
9897make_case_word(fword, cword, flags)
9898 char_u *fword;
9899 char_u *cword;
9900 int flags;
9901{
9902 if (flags & WF_ALLCAP)
9903 /* Make it all upper-case */
9904 allcap_copy(fword, cword);
9905 else if (flags & WF_ONECAP)
9906 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009907 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009908 else
9909 /* Use goodword as-is. */
9910 STRCPY(cword, fword);
9911}
9912
Bram Moolenaarea424162005-06-16 21:51:00 +00009913/*
9914 * Use map string "map" for languages "lp".
9915 */
9916 static void
9917set_map_str(lp, map)
9918 slang_T *lp;
9919 char_u *map;
9920{
9921 char_u *p;
9922 int headc = 0;
9923 int c;
9924 int i;
9925
9926 if (*map == NUL)
9927 {
9928 lp->sl_has_map = FALSE;
9929 return;
9930 }
9931 lp->sl_has_map = TRUE;
9932
9933 /* Init the array and hash table empty. */
9934 for (i = 0; i < 256; ++i)
9935 lp->sl_map_array[i] = 0;
9936#ifdef FEAT_MBYTE
9937 hash_init(&lp->sl_map_hash);
9938#endif
9939
9940 /*
9941 * The similar characters are stored separated with slashes:
9942 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
9943 * before the same slash. For characters above 255 sl_map_hash is used.
9944 */
9945 for (p = map; *p != NUL; )
9946 {
9947#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009948 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009949#else
9950 c = *p++;
9951#endif
9952 if (c == '/')
9953 headc = 0;
9954 else
9955 {
9956 if (headc == 0)
9957 headc = c;
9958
9959#ifdef FEAT_MBYTE
9960 /* Characters above 255 don't fit in sl_map_array[], put them in
9961 * the hash table. Each entry is the char, a NUL the headchar and
9962 * a NUL. */
9963 if (c >= 256)
9964 {
9965 int cl = mb_char2len(c);
9966 int headcl = mb_char2len(headc);
9967 char_u *b;
9968 hash_T hash;
9969 hashitem_T *hi;
9970
9971 b = alloc((unsigned)(cl + headcl + 2));
9972 if (b == NULL)
9973 return;
9974 mb_char2bytes(c, b);
9975 b[cl] = NUL;
9976 mb_char2bytes(headc, b + cl + 1);
9977 b[cl + 1 + headcl] = NUL;
9978 hash = hash_hash(b);
9979 hi = hash_lookup(&lp->sl_map_hash, b, hash);
9980 if (HASHITEM_EMPTY(hi))
9981 hash_add_item(&lp->sl_map_hash, hi, b, hash);
9982 else
9983 {
9984 /* This should have been checked when generating the .spl
9985 * file. */
9986 EMSG(_("E999: duplicate char in MAP entry"));
9987 vim_free(b);
9988 }
9989 }
9990 else
9991#endif
9992 lp->sl_map_array[c] = headc;
9993 }
9994 }
9995}
9996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009997/*
9998 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
9999 * lines in the .aff file.
10000 */
10001 static int
10002similar_chars(slang, c1, c2)
10003 slang_T *slang;
10004 int c1;
10005 int c2;
10006{
Bram Moolenaarea424162005-06-16 21:51:00 +000010007 int m1, m2;
10008#ifdef FEAT_MBYTE
10009 char_u buf[MB_MAXBYTES];
10010 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011
Bram Moolenaarea424162005-06-16 21:51:00 +000010012 if (c1 >= 256)
10013 {
10014 buf[mb_char2bytes(c1, buf)] = 0;
10015 hi = hash_find(&slang->sl_map_hash, buf);
10016 if (HASHITEM_EMPTY(hi))
10017 m1 = 0;
10018 else
10019 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10020 }
10021 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010022#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010023 m1 = slang->sl_map_array[c1];
10024 if (m1 == 0)
10025 return FALSE;
10026
10027
10028#ifdef FEAT_MBYTE
10029 if (c2 >= 256)
10030 {
10031 buf[mb_char2bytes(c2, buf)] = 0;
10032 hi = hash_find(&slang->sl_map_hash, buf);
10033 if (HASHITEM_EMPTY(hi))
10034 m2 = 0;
10035 else
10036 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10037 }
10038 else
10039#endif
10040 m2 = slang->sl_map_array[c2];
10041
10042 return m1 == m2;
10043}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044
10045/*
10046 * Add a suggestion to the list of suggestions.
10047 * Do not add a duplicate suggestion or suggestions with a bad score.
10048 * When "use_score" is not zero it's used, otherwise the score is computed
10049 * with spell_edit_score().
10050 */
10051 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010052add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010054 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010056 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010057 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010058 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010059 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010060{
10061 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010062 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010063 char_u *p = NULL;
10064 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010065
10066 /* Check that the word wasn't banned. */
10067 if (was_banned(su, goodword))
10068 return;
10069
Bram Moolenaar0c405862005-06-22 22:26:26 +000010070 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10071 * Remove the common part from "goodword". */
10072 i = badlen - su->su_badlen;
10073 if (i > 0)
10074 {
10075 /* This assumes there was no case folding or it didn't change the
10076 * length... */
10077 p = goodword + STRLEN(goodword) - i;
10078 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10079 {
10080 badlen = su->su_badlen;
10081 c = *p;
10082 *p = NUL;
10083 }
10084 else
10085 p = NULL;
10086 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010087 else if (i < 0)
10088 {
10089 /* When replacing part of the word check that we actually change
10090 * something. For "the the" a suggestion can be replacing the first
10091 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010092 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010093 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10094 return;
10095 }
10096
Bram Moolenaar0c405862005-06-22 22:26:26 +000010097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010098 if (score <= su->su_maxscore)
10099 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010100 /* Check if the word is already there. Also check the length that is
10101 * being replaced "thes," -> "these" is a different suggestion from
10102 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010103 stp = &SUG(*gap, 0);
10104 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010105 if (STRCMP(stp[i].st_word, goodword) == 0
10106 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 {
10108 /* Found it. Remember the lowest score. */
10109 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010110 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010112 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010113 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010114 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 break;
10116 }
10117
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010118 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 {
10120 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010121 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 stp->st_word = vim_strsave(goodword);
10123 if (stp->st_word != NULL)
10124 {
10125 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010126 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010127 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010128 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010129 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010130
10131 /* If we have too many suggestions now, sort the list and keep
10132 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010133 if (gap->ga_len > SUG_MAX_COUNT(su))
10134 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10135 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 }
10137 }
10138 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000010139
10140 if (p != NULL)
10141 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142}
10143
10144/*
10145 * Add a word to be banned.
10146 */
10147 static void
10148add_banned(su, word)
10149 suginfo_T *su;
10150 char_u *word;
10151{
10152 char_u *s = vim_strsave(word);
10153 hash_T hash;
10154 hashitem_T *hi;
10155
10156 if (s != NULL)
10157 {
10158 hash = hash_hash(s);
10159 hi = hash_lookup(&su->su_banned, s, hash);
10160 if (HASHITEM_EMPTY(hi))
10161 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010162 else
10163 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010164 }
10165}
10166
10167/*
10168 * Return TRUE if a word appears in the list of banned words.
10169 */
10170 static int
10171was_banned(su, word)
10172 suginfo_T *su;
10173 char_u *word;
10174{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010175 hashitem_T *hi = hash_find(&su->su_banned, word);
10176
10177 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178}
10179
10180/*
10181 * Free the banned words in "su".
10182 */
10183 static void
10184free_banned(su)
10185 suginfo_T *su;
10186{
10187 int todo;
10188 hashitem_T *hi;
10189
10190 todo = su->su_banned.ht_used;
10191 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
10192 {
10193 if (!HASHITEM_EMPTY(hi))
10194 {
10195 vim_free(hi->hi_key);
10196 --todo;
10197 }
10198 }
10199 hash_clear(&su->su_banned);
10200}
10201
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010202/*
10203 * Recompute the score if sound-folding is possible. This is slow,
10204 * thus only done for the final results.
10205 */
10206 static void
10207rescore_suggestions(su)
10208 suginfo_T *su;
10209{
10210 langp_T *lp;
10211 suggest_T *stp;
10212 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010213 int i;
10214
10215 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10216 lp->lp_slang != NULL; ++lp)
10217 {
10218 if (lp->lp_slang->sl_sal.ga_len > 0)
10219 {
10220 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010221 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010222
10223 for (i = 0; i < su->su_ga.ga_len; ++i)
10224 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010225 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010226 if (!stp->st_had_bonus)
10227 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010228 stp->st_altscore = stp_sal_score(stp, su,
10229 lp->lp_slang, sal_badword);
10230 if (stp->st_altscore == SCORE_MAXMAX)
10231 stp->st_altscore = SCORE_BIG;
10232 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010233 }
10234 }
10235 break;
10236 }
10237 }
10238}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240static int
10241#ifdef __BORLANDC__
10242_RTLENTRYF
10243#endif
10244sug_compare __ARGS((const void *s1, const void *s2));
10245
10246/*
10247 * Function given to qsort() to sort the suggestions on st_score.
10248 */
10249 static int
10250#ifdef __BORLANDC__
10251_RTLENTRYF
10252#endif
10253sug_compare(s1, s2)
10254 const void *s1;
10255 const void *s2;
10256{
10257 suggest_T *p1 = (suggest_T *)s1;
10258 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010259 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010261 if (n == 0)
10262 return p1->st_altscore - p2->st_altscore;
10263 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264}
10265
10266/*
10267 * Cleanup the suggestions:
10268 * - Sort on score.
10269 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010270 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010271 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010272 static int
10273cleanup_suggestions(gap, maxscore, keep)
10274 garray_T *gap;
10275 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010276 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010278 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 int i;
10280
10281 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010282 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283
10284 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010285 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010286 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010287 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010288 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010289 gap->ga_len = keep;
10290 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010292 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293}
10294
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010295#if defined(FEAT_EVAL) || defined(PROTO)
10296/*
10297 * Soundfold a string, for soundfold().
10298 * Result is in allocated memory, NULL for an error.
10299 */
10300 char_u *
10301eval_soundfold(word)
10302 char_u *word;
10303{
10304 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010305 char_u sound[MAXWLEN];
10306
10307 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
10308 /* Use the sound-folding of the first language that supports it. */
10309 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10310 lp->lp_slang != NULL; ++lp)
10311 if (lp->lp_slang->sl_sal.ga_len > 0)
10312 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010313 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010314 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010315 return vim_strsave(sound);
10316 }
10317
10318 /* No language with sound folding, return word as-is. */
10319 return vim_strsave(word);
10320}
10321#endif
10322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323/*
10324 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
10325 */
10326 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010327spell_soundfold(slang, inword, folded, res)
10328 slang_T *slang;
10329 char_u *inword;
10330 int folded; /* "inword" is already case-folded */
10331 char_u *res;
10332{
10333 char_u fword[MAXWLEN];
10334 char_u *word;
10335
10336 if (slang->sl_sofo)
10337 /* SOFOFROM and SOFOTO used */
10338 spell_soundfold_sofo(slang, inword, res);
10339 else
10340 {
10341 /* SAL items used. Requires the word to be case-folded. */
10342 if (folded)
10343 word = inword;
10344 else
10345 {
10346 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
10347 word = fword;
10348 }
10349
10350#ifdef FEAT_MBYTE
10351 if (has_mbyte)
10352 spell_soundfold_wsal(slang, word, res);
10353 else
10354#endif
10355 spell_soundfold_sal(slang, word, res);
10356 }
10357}
10358
10359/*
10360 * Perform sound folding of "inword" into "res" according to SOFOFROM and
10361 * SOFOTO lines.
10362 */
10363 static void
10364spell_soundfold_sofo(slang, inword, res)
10365 slang_T *slang;
10366 char_u *inword;
10367 char_u *res;
10368{
10369 char_u *s;
10370 int ri = 0;
10371 int c;
10372
10373#ifdef FEAT_MBYTE
10374 if (has_mbyte)
10375 {
10376 int prevc = 0;
10377 int *ip;
10378
10379 /* The sl_sal_first[] table contains the translation for chars up to
10380 * 255, sl_sal the rest. */
10381 for (s = inword; *s != NUL; )
10382 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010383 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010384 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10385 c = ' ';
10386 else if (c < 256)
10387 c = slang->sl_sal_first[c];
10388 else
10389 {
10390 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
10391 if (ip == NULL) /* empty list, can't match */
10392 c = NUL;
10393 else
10394 for (;;) /* find "c" in the list */
10395 {
10396 if (*ip == 0) /* not found */
10397 {
10398 c = NUL;
10399 break;
10400 }
10401 if (*ip == c) /* match! */
10402 {
10403 c = ip[1];
10404 break;
10405 }
10406 ip += 2;
10407 }
10408 }
10409
10410 if (c != NUL && c != prevc)
10411 {
10412 ri += mb_char2bytes(c, res + ri);
10413 if (ri + MB_MAXBYTES > MAXWLEN)
10414 break;
10415 prevc = c;
10416 }
10417 }
10418 }
10419 else
10420#endif
10421 {
10422 /* The sl_sal_first[] table contains the translation. */
10423 for (s = inword; (c = *s) != NUL; ++s)
10424 {
10425 if (vim_iswhite(c))
10426 c = ' ';
10427 else
10428 c = slang->sl_sal_first[c];
10429 if (c != NUL && (ri == 0 || res[ri - 1] != c))
10430 res[ri++] = c;
10431 }
10432 }
10433
10434 res[ri] = NUL;
10435}
10436
10437 static void
10438spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 slang_T *slang;
10440 char_u *inword;
10441 char_u *res;
10442{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010443 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010445 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010447 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010449 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 int n, k = 0;
10451 int z0;
10452 int k0;
10453 int n0;
10454 int c;
10455 int pri;
10456 int p0 = -333;
10457 int c0;
10458
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010459 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010460 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 if (slang->sl_rem_accents)
10462 {
10463 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010464 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010466 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010467 {
10468 *t++ = ' ';
10469 s = skipwhite(s);
10470 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010471 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010473 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 *t++ = *s;
10475 ++s;
10476 }
10477 }
10478 *t = NUL;
10479 }
10480 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010481 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010483 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484
10485 /*
10486 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010487 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010489 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 while ((c = word[i]) != NUL)
10491 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010492 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010493 n = slang->sl_sal_first[c];
10494 z0 = 0;
10495
10496 if (n >= 0)
10497 {
10498 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010499 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010501 /* Quickly skip entries that don't match the word. Most
10502 * entries are less then three chars, optimize for that. */
10503 k = smp[n].sm_leadlen;
10504 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010506 if (word[i + 1] != s[1])
10507 continue;
10508 if (k > 2)
10509 {
10510 for (j = 2; j < k; ++j)
10511 if (word[i + j] != s[j])
10512 break;
10513 if (j < k)
10514 continue;
10515 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 }
10517
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010518 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010520 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010521 while (*pf != NUL && *pf != word[i + k])
10522 ++pf;
10523 if (*pf == NUL)
10524 continue;
10525 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010527 s = smp[n].sm_rules;
10528 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529
10530 p0 = *s;
10531 k0 = k;
10532 while (*s == '-' && k > 1)
10533 {
10534 k--;
10535 s++;
10536 }
10537 if (*s == '<')
10538 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010539 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 {
10541 /* determine priority */
10542 pri = *s - '0';
10543 s++;
10544 }
10545 if (*s == '^' && *(s + 1) == '^')
10546 s++;
10547
10548 if (*s == NUL
10549 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010550 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010551 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010553 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010555 && spell_iswordp(word + i - 1, curbuf)
10556 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010557 {
10558 /* search for followup rules, if: */
10559 /* followup and k > 1 and NO '-' in searchstring */
10560 c0 = word[i + k - 1];
10561 n0 = slang->sl_sal_first[c0];
10562
10563 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010564 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010565 {
10566 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010567 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010569 /* Quickly skip entries that don't match the word.
10570 * */
10571 k0 = smp[n0].sm_leadlen;
10572 if (k0 > 1)
10573 {
10574 if (word[i + k] != s[1])
10575 continue;
10576 if (k0 > 2)
10577 {
10578 pf = word + i + k + 1;
10579 for (j = 2; j < k0; ++j)
10580 if (*pf++ != s[j])
10581 break;
10582 if (j < k0)
10583 continue;
10584 }
10585 }
10586 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010588 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010589 {
10590 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010591 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010592 while (*pf != NUL && *pf != word[i + k0])
10593 ++pf;
10594 if (*pf == NUL)
10595 continue;
10596 ++k0;
10597 }
10598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010600 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 while (*s == '-')
10602 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010603 /* "k0" gets NOT reduced because
10604 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 s++;
10606 }
10607 if (*s == '<')
10608 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010609 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 {
10611 p0 = *s - '0';
10612 s++;
10613 }
10614
10615 if (*s == NUL
10616 /* *s == '^' cuts */
10617 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010618 && !spell_iswordp(word + i + k0,
10619 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 {
10621 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624
10625 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 /* rule fits; stop search */
10629 break;
10630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 }
10632
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010633 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 }
10636
10637 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010638 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010639 if (s == NULL)
10640 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010641 pf = smp[n].sm_rules;
10642 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 if (p0 == 1 && z == 0)
10644 {
10645 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010646 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
10647 || res[reslen - 1] == *s))
10648 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 z0 = 1;
10650 z = 1;
10651 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010652 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 {
10654 word[i + k0] = *s;
10655 k0++;
10656 s++;
10657 }
10658 if (k > k0)
10659 mch_memmove(word + i + k0, word + i + k,
10660 STRLEN(word + i + k) + 1);
10661
10662 /* new "actual letter" */
10663 c = word[i];
10664 }
10665 else
10666 {
10667 /* no '<' rule used */
10668 i += k - 1;
10669 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010670 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010672 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010673 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 s++;
10675 }
10676 /* new "actual letter" */
10677 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010678 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 {
10680 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010681 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010682 mch_memmove(word, word + i + 1,
10683 STRLEN(word + i + 1) + 1);
10684 i = 0;
10685 z0 = 1;
10686 }
10687 }
10688 break;
10689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 }
10691 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010692 else if (vim_iswhite(c))
10693 {
10694 c = ' ';
10695 k = 1;
10696 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010697
10698 if (z0 == 0)
10699 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010700 if (k && !p0 && reslen < MAXWLEN && c != NUL
10701 && (!slang->sl_collapse || reslen == 0
10702 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010704 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705
10706 i++;
10707 z = 0;
10708 k = 0;
10709 }
10710 }
10711
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010712 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713}
10714
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010715#ifdef FEAT_MBYTE
10716/*
10717 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
10718 * Multi-byte version of spell_soundfold().
10719 */
10720 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010721spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010722 slang_T *slang;
10723 char_u *inword;
10724 char_u *res;
10725{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010726 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010727 int word[MAXWLEN];
10728 int wres[MAXWLEN];
10729 int l;
10730 char_u *s;
10731 int *ws;
10732 char_u *t;
10733 int *pf;
10734 int i, j, z;
10735 int reslen;
10736 int n, k = 0;
10737 int z0;
10738 int k0;
10739 int n0;
10740 int c;
10741 int pri;
10742 int p0 = -333;
10743 int c0;
10744 int did_white = FALSE;
10745
10746 /*
10747 * Convert the multi-byte string to a wide-character string.
10748 * Remove accents, if wanted. We actually remove all non-word characters.
10749 * But keep white space.
10750 */
10751 n = 0;
10752 for (s = inword; *s != NUL; )
10753 {
10754 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010755 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010756 if (slang->sl_rem_accents)
10757 {
10758 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10759 {
10760 if (did_white)
10761 continue;
10762 c = ' ';
10763 did_white = TRUE;
10764 }
10765 else
10766 {
10767 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010768 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010769 continue;
10770 }
10771 }
10772 word[n++] = c;
10773 }
10774 word[n] = NUL;
10775
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010776 /*
10777 * This comes from Aspell phonet.cpp.
10778 * Converted from C++ to C. Added support for multi-byte chars.
10779 * Changed to keep spaces.
10780 */
10781 i = reslen = z = 0;
10782 while ((c = word[i]) != NUL)
10783 {
10784 /* Start with the first rule that has the character in the word. */
10785 n = slang->sl_sal_first[c & 0xff];
10786 z0 = 0;
10787
10788 if (n >= 0)
10789 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010790 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010791 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
10792 {
10793 /* Quickly skip entries that don't match the word. Most
10794 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010795 if (c != ws[0])
10796 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010797 k = smp[n].sm_leadlen;
10798 if (k > 1)
10799 {
10800 if (word[i + 1] != ws[1])
10801 continue;
10802 if (k > 2)
10803 {
10804 for (j = 2; j < k; ++j)
10805 if (word[i + j] != ws[j])
10806 break;
10807 if (j < k)
10808 continue;
10809 }
10810 }
10811
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010812 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010813 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010814 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010815 while (*pf != NUL && *pf != word[i + k])
10816 ++pf;
10817 if (*pf == NUL)
10818 continue;
10819 ++k;
10820 }
10821 s = smp[n].sm_rules;
10822 pri = 5; /* default priority */
10823
10824 p0 = *s;
10825 k0 = k;
10826 while (*s == '-' && k > 1)
10827 {
10828 k--;
10829 s++;
10830 }
10831 if (*s == '<')
10832 s++;
10833 if (VIM_ISDIGIT(*s))
10834 {
10835 /* determine priority */
10836 pri = *s - '0';
10837 s++;
10838 }
10839 if (*s == '^' && *(s + 1) == '^')
10840 s++;
10841
10842 if (*s == NUL
10843 || (*s == '^'
10844 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010845 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010846 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010847 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010848 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010849 && spell_iswordp_w(word + i - 1, curbuf)
10850 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010851 {
10852 /* search for followup rules, if: */
10853 /* followup and k > 1 and NO '-' in searchstring */
10854 c0 = word[i + k - 1];
10855 n0 = slang->sl_sal_first[c0 & 0xff];
10856
10857 if (slang->sl_followup && k > 1 && n0 >= 0
10858 && p0 != '-' && word[i + k] != NUL)
10859 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010860 /* Test follow-up rule for "word[i + k]"; loop over
10861 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010862 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
10863 == (c0 & 0xff); ++n0)
10864 {
10865 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010866 */
10867 if (c0 != ws[0])
10868 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010869 k0 = smp[n0].sm_leadlen;
10870 if (k0 > 1)
10871 {
10872 if (word[i + k] != ws[1])
10873 continue;
10874 if (k0 > 2)
10875 {
10876 pf = word + i + k + 1;
10877 for (j = 2; j < k0; ++j)
10878 if (*pf++ != ws[j])
10879 break;
10880 if (j < k0)
10881 continue;
10882 }
10883 }
10884 k0 += k - 1;
10885
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010886 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010887 {
10888 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010889 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010890 while (*pf != NUL && *pf != word[i + k0])
10891 ++pf;
10892 if (*pf == NUL)
10893 continue;
10894 ++k0;
10895 }
10896
10897 p0 = 5;
10898 s = smp[n0].sm_rules;
10899 while (*s == '-')
10900 {
10901 /* "k0" gets NOT reduced because
10902 * "if (k0 == k)" */
10903 s++;
10904 }
10905 if (*s == '<')
10906 s++;
10907 if (VIM_ISDIGIT(*s))
10908 {
10909 p0 = *s - '0';
10910 s++;
10911 }
10912
10913 if (*s == NUL
10914 /* *s == '^' cuts */
10915 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010916 && !spell_iswordp_w(word + i + k0,
10917 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010918 {
10919 if (k0 == k)
10920 /* this is just a piece of the string */
10921 continue;
10922
10923 if (p0 < pri)
10924 /* priority too low */
10925 continue;
10926 /* rule fits; stop search */
10927 break;
10928 }
10929 }
10930
10931 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
10932 == (c0 & 0xff))
10933 continue;
10934 }
10935
10936 /* replace string */
10937 ws = smp[n].sm_to_w;
10938 s = smp[n].sm_rules;
10939 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
10940 if (p0 == 1 && z == 0)
10941 {
10942 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010943 if (reslen > 0 && ws != NULL && *ws != NUL
10944 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010945 || wres[reslen - 1] == *ws))
10946 reslen--;
10947 z0 = 1;
10948 z = 1;
10949 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010950 if (ws != NULL)
10951 while (*ws != NUL && word[i + k0] != NUL)
10952 {
10953 word[i + k0] = *ws;
10954 k0++;
10955 ws++;
10956 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010957 if (k > k0)
10958 mch_memmove(word + i + k0, word + i + k,
10959 sizeof(int) * (STRLEN(word + i + k) + 1));
10960
10961 /* new "actual letter" */
10962 c = word[i];
10963 }
10964 else
10965 {
10966 /* no '<' rule used */
10967 i += k - 1;
10968 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010969 if (ws != NULL)
10970 while (*ws != NUL && ws[1] != NUL
10971 && reslen < MAXWLEN)
10972 {
10973 if (reslen == 0 || wres[reslen - 1] != *ws)
10974 wres[reslen++] = *ws;
10975 ws++;
10976 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010977 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010978 if (ws == NULL)
10979 c = NUL;
10980 else
10981 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010982 if (strstr((char *)s, "^^") != NULL)
10983 {
10984 if (c != NUL)
10985 wres[reslen++] = c;
10986 mch_memmove(word, word + i + 1,
10987 sizeof(int) * (STRLEN(word + i + 1) + 1));
10988 i = 0;
10989 z0 = 1;
10990 }
10991 }
10992 break;
10993 }
10994 }
10995 }
10996 else if (vim_iswhite(c))
10997 {
10998 c = ' ';
10999 k = 1;
11000 }
11001
11002 if (z0 == 0)
11003 {
11004 if (k && !p0 && reslen < MAXWLEN && c != NUL
11005 && (!slang->sl_collapse || reslen == 0
11006 || wres[reslen - 1] != c))
11007 /* condense only double letters */
11008 wres[reslen++] = c;
11009
11010 i++;
11011 z = 0;
11012 k = 0;
11013 }
11014 }
11015
11016 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11017 l = 0;
11018 for (n = 0; n < reslen; ++n)
11019 {
11020 l += mb_char2bytes(wres[n], res + l);
11021 if (l + MB_MAXBYTES > MAXWLEN)
11022 break;
11023 }
11024 res[l] = NUL;
11025}
11026#endif
11027
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011028/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011029 * Compute a score for two sound-a-like words.
11030 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11031 * Instead of a generic loop we write out the code. That keeps it fast by
11032 * avoiding checks that will not be possible.
11033 */
11034 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011035soundalike_score(goodstart, badstart)
11036 char_u *goodstart; /* sound-folded good word */
11037 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011038{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011039 char_u *goodsound = goodstart;
11040 char_u *badsound = badstart;
11041 int goodlen;
11042 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011043 int n;
11044 char_u *pl, *ps;
11045 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011046 int score = 0;
11047
11048 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11049 * counted so much, vowels halfway the word aren't counted at all. */
11050 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11051 {
11052 score = SCORE_DEL / 2;
11053 if (*badsound == '*')
11054 ++badsound;
11055 else
11056 ++goodsound;
11057 }
11058
11059 goodlen = STRLEN(goodsound);
11060 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011061
11062 /* Return quickly if the lenghts are too different to be fixed by two
11063 * changes. */
11064 n = goodlen - badlen;
11065 if (n < -2 || n > 2)
11066 return SCORE_MAXMAX;
11067
11068 if (n > 0)
11069 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011070 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011071 ps = badsound;
11072 }
11073 else
11074 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011075 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011076 ps = goodsound;
11077 }
11078
11079 /* Skip over the identical part. */
11080 while (*pl == *ps && *pl != NUL)
11081 {
11082 ++pl;
11083 ++ps;
11084 }
11085
11086 switch (n)
11087 {
11088 case -2:
11089 case 2:
11090 /*
11091 * Must delete two characters from "pl".
11092 */
11093 ++pl; /* first delete */
11094 while (*pl == *ps)
11095 {
11096 ++pl;
11097 ++ps;
11098 }
11099 /* strings must be equal after second delete */
11100 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011101 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011102
11103 /* Failed to compare. */
11104 break;
11105
11106 case -1:
11107 case 1:
11108 /*
11109 * Minimal one delete from "pl" required.
11110 */
11111
11112 /* 1: delete */
11113 pl2 = pl + 1;
11114 ps2 = ps;
11115 while (*pl2 == *ps2)
11116 {
11117 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011118 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011119 ++pl2;
11120 ++ps2;
11121 }
11122
11123 /* 2: delete then swap, then rest must be equal */
11124 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11125 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011126 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011127
11128 /* 3: delete then substitute, then the rest must be equal */
11129 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011130 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011131
11132 /* 4: first swap then delete */
11133 if (pl[0] == ps[1] && pl[1] == ps[0])
11134 {
11135 pl2 = pl + 2; /* swap, skip two chars */
11136 ps2 = ps + 2;
11137 while (*pl2 == *ps2)
11138 {
11139 ++pl2;
11140 ++ps2;
11141 }
11142 /* delete a char and then strings must be equal */
11143 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011144 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011145 }
11146
11147 /* 5: first substitute then delete */
11148 pl2 = pl + 1; /* substitute, skip one char */
11149 ps2 = ps + 1;
11150 while (*pl2 == *ps2)
11151 {
11152 ++pl2;
11153 ++ps2;
11154 }
11155 /* delete a char and then strings must be equal */
11156 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011157 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011158
11159 /* Failed to compare. */
11160 break;
11161
11162 case 0:
11163 /*
11164 * Lenghts are equal, thus changes must result in same length: An
11165 * insert is only possible in combination with a delete.
11166 * 1: check if for identical strings
11167 */
11168 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011169 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011170
11171 /* 2: swap */
11172 if (pl[0] == ps[1] && pl[1] == ps[0])
11173 {
11174 pl2 = pl + 2; /* swap, skip two chars */
11175 ps2 = ps + 2;
11176 while (*pl2 == *ps2)
11177 {
11178 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011179 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011180 ++pl2;
11181 ++ps2;
11182 }
11183 /* 3: swap and swap again */
11184 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11185 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011186 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011187
11188 /* 4: swap and substitute */
11189 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011190 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011191 }
11192
11193 /* 5: substitute */
11194 pl2 = pl + 1;
11195 ps2 = ps + 1;
11196 while (*pl2 == *ps2)
11197 {
11198 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011199 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011200 ++pl2;
11201 ++ps2;
11202 }
11203
11204 /* 6: substitute and swap */
11205 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11206 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011207 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011208
11209 /* 7: substitute and substitute */
11210 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011211 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011212
11213 /* 8: insert then delete */
11214 pl2 = pl;
11215 ps2 = ps + 1;
11216 while (*pl2 == *ps2)
11217 {
11218 ++pl2;
11219 ++ps2;
11220 }
11221 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011222 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011223
11224 /* 9: delete then insert */
11225 pl2 = pl + 1;
11226 ps2 = ps;
11227 while (*pl2 == *ps2)
11228 {
11229 ++pl2;
11230 ++ps2;
11231 }
11232 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011233 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011234
11235 /* Failed to compare. */
11236 break;
11237 }
11238
11239 return SCORE_MAXMAX;
11240}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011241
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242/*
11243 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011244 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011245 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011246 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011247 * It has been converted from C++ to C and modified to support multi-byte
11248 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 */
11250 static int
11251spell_edit_score(badword, goodword)
11252 char_u *badword;
11253 char_u *goodword;
11254{
11255 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011256 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 int j, i;
11258 int t;
11259 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011260 int pbc, pgc;
11261#ifdef FEAT_MBYTE
11262 char_u *p;
11263 int wbadword[MAXWLEN];
11264 int wgoodword[MAXWLEN];
11265
11266 if (has_mbyte)
11267 {
11268 /* Get the characters from the multi-byte strings and put them in an
11269 * int array for easy access. */
11270 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011271 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011272 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011273 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011274 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011275 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011276 }
11277 else
11278#endif
11279 {
11280 badlen = STRLEN(badword) + 1;
11281 goodlen = STRLEN(goodword) + 1;
11282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283
11284 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
11285#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011286 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
11287 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011288 if (cnt == NULL)
11289 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290
11291 CNT(0, 0) = 0;
11292 for (j = 1; j <= goodlen; ++j)
11293 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
11294
11295 for (i = 1; i <= badlen; ++i)
11296 {
11297 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
11298 for (j = 1; j <= goodlen; ++j)
11299 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011300#ifdef FEAT_MBYTE
11301 if (has_mbyte)
11302 {
11303 bc = wbadword[i - 1];
11304 gc = wgoodword[j - 1];
11305 }
11306 else
11307#endif
11308 {
11309 bc = badword[i - 1];
11310 gc = goodword[j - 1];
11311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 if (bc == gc)
11313 CNT(i, j) = CNT(i - 1, j - 1);
11314 else
11315 {
11316 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011317 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
11319 else
11320 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
11321
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011322 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011324#ifdef FEAT_MBYTE
11325 if (has_mbyte)
11326 {
11327 pbc = wbadword[i - 2];
11328 pgc = wgoodword[j - 2];
11329 }
11330 else
11331#endif
11332 {
11333 pbc = badword[i - 2];
11334 pgc = goodword[j - 2];
11335 }
11336 if (bc == pgc && pbc == gc)
11337 {
11338 t = SCORE_SWAP + CNT(i - 2, j - 2);
11339 if (t < CNT(i, j))
11340 CNT(i, j) = t;
11341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 }
11343 t = SCORE_DEL + CNT(i - 1, j);
11344 if (t < CNT(i, j))
11345 CNT(i, j) = t;
11346 t = SCORE_INS + CNT(i, j - 1);
11347 if (t < CNT(i, j))
11348 CNT(i, j) = t;
11349 }
11350 }
11351 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011352
11353 i = CNT(badlen - 1, goodlen - 1);
11354 vim_free(cnt);
11355 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000011357
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011358/*
11359 * ":spelldump"
11360 */
11361/*ARGSUSED*/
11362 void
11363ex_spelldump(eap)
11364 exarg_T *eap;
11365{
11366 buf_T *buf = curbuf;
11367 langp_T *lp;
11368 slang_T *slang;
11369 idx_T arridx[MAXWLEN];
11370 int curi[MAXWLEN];
11371 char_u word[MAXWLEN];
11372 int c;
11373 char_u *byts;
11374 idx_T *idxs;
11375 linenr_T lnum = 0;
11376 int round;
11377 int depth;
11378 int n;
11379 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011380 char_u *region_names = NULL; /* region names being used */
11381 int do_region = TRUE; /* dump region names and numbers */
11382 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011383
11384 if (no_spell_checking())
11385 return;
11386
11387 /* Create a new empty buffer by splitting the window. */
11388 do_cmdline_cmd((char_u *)"new");
11389 if (!bufempty() || !buf_valid(buf))
11390 return;
11391
Bram Moolenaar7887d882005-07-01 22:33:52 +000011392 /* Find out if we can support regions: All languages must support the same
11393 * regions or none at all. */
11394 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11395 {
11396 p = lp->lp_slang->sl_regions;
11397 if (p[0] != 0)
11398 {
11399 if (region_names == NULL) /* first language with regions */
11400 region_names = p;
11401 else if (STRCMP(region_names, p) != 0)
11402 {
11403 do_region = FALSE; /* region names are different */
11404 break;
11405 }
11406 }
11407 }
11408
11409 if (do_region && region_names != NULL)
11410 {
11411 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
11412 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11413 }
11414 else
11415 do_region = FALSE;
11416
11417 /*
11418 * Loop over all files loaded for the entries in 'spelllang'.
11419 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011420 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11421 {
11422 slang = lp->lp_slang;
11423
11424 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
11425 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11426
11427 /* round 1: case-folded tree
11428 * round 2: keep-case tree */
11429 for (round = 1; round <= 2; ++round)
11430 {
11431 if (round == 1)
11432 {
11433 byts = slang->sl_fbyts;
11434 idxs = slang->sl_fidxs;
11435 }
11436 else
11437 {
11438 byts = slang->sl_kbyts;
11439 idxs = slang->sl_kidxs;
11440 }
11441 if (byts == NULL)
11442 continue; /* array is empty */
11443
11444 depth = 0;
11445 arridx[0] = 0;
11446 curi[0] = 1;
11447 while (depth >= 0 && !got_int)
11448 {
11449 if (curi[depth] > byts[arridx[depth]])
11450 {
11451 /* Done all bytes at this node, go up one level. */
11452 --depth;
11453 line_breakcheck();
11454 }
11455 else
11456 {
11457 /* Do one more byte at this node. */
11458 n = arridx[depth] + curi[depth];
11459 ++curi[depth];
11460 c = byts[n];
11461 if (c == 0)
11462 {
11463 /* End of word, deal with the word.
11464 * Don't use keep-case words in the fold-case tree,
11465 * they will appear in the keep-case tree.
11466 * Only use the word when the region matches. */
11467 flags = (int)idxs[n];
11468 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000011469 && (do_region
11470 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011471 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011472 & lp->lp_region) != 0))
11473 {
11474 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011475 if (!do_region)
11476 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011477
11478 /* Dump the basic word if there is no prefix or
11479 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011480 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011481 if (c == 0 || curi[depth] == 2)
11482 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011483
11484 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011485 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011486 lnum = apply_prefixes(slang, word, round,
11487 flags, lnum);
11488 }
11489 }
11490 else
11491 {
11492 /* Normal char, go one level deeper. */
11493 word[depth++] = c;
11494 arridx[depth] = idxs[n];
11495 curi[depth] = 1;
11496 }
11497 }
11498 }
11499 }
11500 }
11501
11502 /* Delete the empty line that we started with. */
11503 if (curbuf->b_ml.ml_line_count > 1)
11504 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
11505
11506 redraw_later(NOT_VALID);
11507}
11508
11509/*
11510 * Dump one word: apply case modifications and append a line to the buffer.
11511 */
11512 static void
11513dump_word(word, round, flags, lnum)
11514 char_u *word;
11515 int round;
11516 int flags;
11517 linenr_T lnum;
11518{
11519 int keepcap = FALSE;
11520 char_u *p;
11521 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000011522 char_u badword[MAXWLEN + 10];
11523 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011524
11525 if (round == 1 && (flags & WF_CAPMASK) != 0)
11526 {
11527 /* Need to fix case according to "flags". */
11528 make_case_word(word, cword, flags);
11529 p = cword;
11530 }
11531 else
11532 {
11533 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011534 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
11535 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011536 keepcap = TRUE;
11537 }
11538
Bram Moolenaar7887d882005-07-01 22:33:52 +000011539 /* Add flags and regions after a slash. */
11540 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011541 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000011542 STRCPY(badword, p);
11543 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011544 if (keepcap)
11545 STRCAT(badword, "=");
11546 if (flags & WF_BANNED)
11547 STRCAT(badword, "!");
11548 else if (flags & WF_RARE)
11549 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000011550 if (flags & WF_REGION)
11551 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011552 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000011553 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011554 p = badword;
11555 }
11556
11557 ml_append(lnum, p, (colnr_T)0, FALSE);
11558}
11559
11560/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011561 * For ":spelldump": Find matching prefixes for "word". Prepend each to
11562 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011563 * Return the updated line number.
11564 */
11565 static linenr_T
11566apply_prefixes(slang, word, round, flags, startlnum)
11567 slang_T *slang;
11568 char_u *word; /* case-folded word */
11569 int round;
11570 int flags; /* flags with prefix ID */
11571 linenr_T startlnum;
11572{
11573 idx_T arridx[MAXWLEN];
11574 int curi[MAXWLEN];
11575 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000011576 char_u word_up[MAXWLEN];
11577 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011578 int c;
11579 char_u *byts;
11580 idx_T *idxs;
11581 linenr_T lnum = startlnum;
11582 int depth;
11583 int n;
11584 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011585 int i;
11586
Bram Moolenaar53805d12005-08-01 07:08:33 +000011587 /* if the word starts with a lower-case letter make the word with an
11588 * upper-case letter in word_up[]. */
11589 c = PTR2CHAR(word);
11590 if (SPELL_TOUPPER(c) != c)
11591 {
11592 onecap_copy(word, word_up, TRUE);
11593 has_word_up = TRUE;
11594 }
11595
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011596 byts = slang->sl_pbyts;
11597 idxs = slang->sl_pidxs;
11598 if (byts != NULL) /* array not is empty */
11599 {
11600 /*
11601 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011602 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011603 */
11604 depth = 0;
11605 arridx[0] = 0;
11606 curi[0] = 1;
11607 while (depth >= 0 && !got_int)
11608 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011609 n = arridx[depth];
11610 len = byts[n];
11611 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011612 {
11613 /* Done all bytes at this node, go up one level. */
11614 --depth;
11615 line_breakcheck();
11616 }
11617 else
11618 {
11619 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011620 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011621 ++curi[depth];
11622 c = byts[n];
11623 if (c == 0)
11624 {
11625 /* End of prefix, find out how many IDs there are. */
11626 for (i = 1; i < len; ++i)
11627 if (byts[n + i] != 0)
11628 break;
11629 curi[depth] += i - 1;
11630
Bram Moolenaar53805d12005-08-01 07:08:33 +000011631 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
11632 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011633 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011634 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011635 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011636 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011637 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011638 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011639
11640 /* Check for prefix that matches the word when the
11641 * first letter is upper-case, but only if the prefix has
11642 * a condition. */
11643 if (has_word_up)
11644 {
11645 c = valid_word_prefix(i, n, flags, word_up, slang,
11646 TRUE);
11647 if (c != 0)
11648 {
11649 vim_strncpy(prefix + depth, word_up,
11650 MAXWLEN - depth - 1);
11651 dump_word(prefix, round,
11652 (c & WF_RAREPFX) ? (flags | WF_RARE)
11653 : flags, lnum++);
11654 }
11655 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011656 }
11657 else
11658 {
11659 /* Normal char, go one level deeper. */
11660 prefix[depth++] = c;
11661 arridx[depth] = idxs[n];
11662 curi[depth] = 1;
11663 }
11664 }
11665 }
11666 }
11667
11668 return lnum;
11669}
11670
Bram Moolenaar8b59de92005-08-11 19:59:29 +000011671#if defined(FEAT_INS_EXPAND) || defined(PROTO)
11672static int spell_expand_need_cap;
11673
11674/*
11675 * Find start of the word in front of the cursor. We don't check if it is
11676 * badly spelled, with completion we can only change the word in front of the
11677 * cursor.
11678 * Used for Insert mode completion CTRL-X ?.
11679 * Returns the column number of the word.
11680 */
11681 int
11682spell_word_start(startcol)
11683 int startcol;
11684{
11685 char_u *line;
11686 char_u *p;
11687 int col = 0;
11688
11689 if (no_spell_checking())
11690 return startcol;
11691
11692 /* Find a word character before "startcol". */
11693 line = ml_get_curline();
11694 for (p = line + startcol; p > line; )
11695 {
11696 mb_ptr_back(line, p);
11697 if (spell_iswordp_nmw(p))
11698 break;
11699 }
11700
11701 /* Go back to start of the word. */
11702 while (p > line)
11703 {
11704 col = p - line;
11705 mb_ptr_back(line, p);
11706 if (!spell_iswordp(p, curbuf))
11707 break;
11708 col = 0;
11709 }
11710
11711 /* Need to check for 'spellcapcheck' now, the word is removed before
11712 * expand_spelling() is called. Therefore the ugly global variable. */
11713 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
11714
11715 return col;
11716}
11717
11718/*
11719 * Get list of spelling suggestions.
11720 * Used for Insert mode completion CTRL-X ?.
11721 * Returns the number of matches. The matches are in "matchp[]", array of
11722 * allocated strings.
11723 */
11724/*ARGSUSED*/
11725 int
11726expand_spelling(lnum, col, pat, matchp)
11727 linenr_T lnum;
11728 int col;
11729 char_u *pat;
11730 char_u ***matchp;
11731{
11732 garray_T ga;
11733
11734 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
11735 *matchp = ga.ga_data;
11736 return ga.ga_len;
11737}
11738#endif
11739
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000011740#endif /* FEAT_SYN_HL */