blob: 2d20e9a0bdcfc69ee4888273fdc8e7179e304439 [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 */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000380 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000381 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
382 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000383
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000384 int sl_prefixcnt; /* number of items in "sl_prefprog" */
385 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000387 garray_T sl_rep; /* list of fromto_T entries from REP lines */
388 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
389 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000390 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000391 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000392 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000393 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
394 * "sl_sal_first" maps chars, when has_mbyte
395 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000396 int sl_followup; /* SAL followup */
397 int sl_collapse; /* SAL collapse_result */
398 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000399 int sl_has_map; /* TRUE if there is a MAP line */
400#ifdef FEAT_MBYTE
401 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
402 int sl_map_array[256]; /* MAP for first 256 chars */
403#else
404 char_u sl_map_array[256]; /* MAP for first 256 chars */
405#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000406};
407
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000408/* First language that is loaded, start of the linked list of loaded
409 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410static slang_T *first_lang = NULL;
411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000412/* Flags used in .spl file for soundsalike flags. */
413#define SAL_F0LLOWUP 1
414#define SAL_COLLAPSE 2
415#define SAL_REM_ACCENTS 4
416
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000417/*
418 * Structure used in "b_langp", filled from 'spelllang'.
419 */
420typedef struct langp_S
421{
422 slang_T *lp_slang; /* info for this language (NULL for last one) */
423 int lp_region; /* bitmask for region or REGION_ALL */
424} langp_T;
425
426#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
427
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000428#define REGION_ALL 0xff /* word valid in all regions */
429
Bram Moolenaar5195e452005-08-19 20:32:47 +0000430#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
431#define VIMSPELLMAGICL 8
432#define VIMSPELLVERSION 50
433
434/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
435#define SN_REGION 0 /* <regionname> section */
436#define SN_CHARFLAGS 1 /* charflags section */
437#define SN_MIDWORD 2 /* <midword> section */
438#define SN_PREFCOND 3 /* <prefcond> section */
439#define SN_REP 4 /* REP items section */
440#define SN_SAL 5 /* SAL items section */
441#define SN_SOFO 6 /* soundfolding section */
442#define SN_MAP 7 /* MAP items section */
443#define SN_COMPOUND 8 /* compound words section */
444#define SN_SYLLABLE 9 /* syllable section */
445#define SN_END 255 /* end of sections */
446
447#define SNF_REQUIRED 1 /* <sectionflags>: required section */
448
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000449/* Result values. Lower number is accepted over higher one. */
450#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000451#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000452#define SP_RARE 1
453#define SP_LOCAL 2
454#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000455
Bram Moolenaar7887d882005-07-01 22:33:52 +0000456/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000457static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000458
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460 * Information used when looking for suggestions.
461 */
462typedef struct suginfo_S
463{
464 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000465 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000466 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000468 char_u *su_badptr; /* start of bad word in line */
469 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000470 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
472 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
473 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000474} suginfo_T;
475
476/* One word suggestion. Used in "si_ga". */
477typedef struct suggest_S
478{
479 char_u *st_word; /* suggested word, allocated string */
480 int st_orglen; /* length of replaced text */
481 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000482 int st_altscore; /* used when st_score compares equal */
483 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000484 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485} suggest_T;
486
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000487#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000489/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
490 * called the score may change, thus we need to keep more than what is
491 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000492#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000493
494/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
495 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000496#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497
498/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000499#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000500#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000501#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000503#define SCORE_SWAP 90 /* swap two characters */
504#define SCORE_SWAP3 110 /* swap two characters in three */
505#define SCORE_REP 87 /* REP replacement */
506#define SCORE_SUBST 93 /* substitute a character */
507#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000508#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000509#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000510#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000511#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000512#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000513#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000514#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000515#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000516
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000517#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000518#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
519 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000520
521#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000522#define SCORE_MAXMAX 999999 /* accept any score */
523
524/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525 * Structure to store info for word matching.
526 */
527typedef struct matchinf_S
528{
529 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000530
531 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000532 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000533 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000534 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000535 char_u *mi_cend; /* char after what was used for
536 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000537
538 /* case-folded text */
539 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000540 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000541
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000542 /* for when checking word after a prefix */
543 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000544 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000545 int mi_prefcnt; /* number of entries at mi_prefarridx */
546 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000547#ifdef FEAT_MBYTE
548 int mi_cprefixlen; /* byte length of prefix in original
549 case */
550#else
551# define mi_cprefixlen mi_prefixlen /* it's the same value */
552#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000553
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000554 /* for when checking a compound word */
555 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000556 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
557 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000558
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000559 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000560 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000561 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000562 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000563} matchinf_T;
564
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000565/*
566 * The tables used for recognizing word characters according to spelling.
567 * These are only used for the first 256 characters of 'encoding'.
568 */
569typedef struct spelltab_S
570{
571 char_u st_isw[256]; /* flags: is word char */
572 char_u st_isu[256]; /* flags: is uppercase char */
573 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000574 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000575} spelltab_T;
576
577static spelltab_T spelltab;
578static int did_set_spelltab;
579
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000580#define CF_WORD 0x01
581#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000582
583static void clear_spell_chartab __ARGS((spelltab_T *sp));
584static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000585static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
586static int spell_iswordp_nmw __ARGS((char_u *p));
587#ifdef FEAT_MBYTE
588static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
589#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000590static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000591
592/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000593 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000594 */
595typedef enum
596{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 STATE_START = 0, /* At start of node check for NUL bytes (goodword
598 * ends); if badword ends there is a match, otherwise
599 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000600 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000601 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000602 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
603 STATE_PLAIN, /* Use each byte of the node. */
604 STATE_DEL, /* Delete a byte from the bad word. */
605 STATE_INS, /* Insert a byte in the bad word. */
606 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000607 STATE_UNSWAP, /* Undo swap two characters. */
608 STATE_SWAP3, /* Swap two characters over three. */
609 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000611 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000612 STATE_REP_INI, /* Prepare for using REP items. */
613 STATE_REP, /* Use matching REP items from the .aff file. */
614 STATE_REP_UNDO, /* Undo a REP item replacement. */
615 STATE_FINAL /* End of this node. */
616} state_T;
617
618/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000619 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000620 */
621typedef struct trystate_S
622{
Bram Moolenaarea424162005-06-16 21:51:00 +0000623 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000624 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000626 short ts_curi; /* index in list of child nodes */
627 char_u ts_fidx; /* index in fword[], case-folded bad word */
628 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
629 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000630 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000631 * PFD_PREFIXTREE or PFD_NOPREFIX */
632 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000633#ifdef FEAT_MBYTE
634 char_u ts_tcharlen; /* number of bytes in tword character */
635 char_u ts_tcharidx; /* current byte index in tword character */
636 char_u ts_isdiff; /* DIFF_ values */
637 char_u ts_fcharstart; /* index in fword where badword char started */
638#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000639 char_u ts_prewordlen; /* length of word in "preword[]" */
640 char_u ts_splitoff; /* index in "tword" after last split */
641 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000642 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000643 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644} trystate_T;
645
Bram Moolenaarea424162005-06-16 21:51:00 +0000646/* values for ts_isdiff */
647#define DIFF_NONE 0 /* no different byte (yet) */
648#define DIFF_YES 1 /* different byte found */
649#define DIFF_INSERT 2 /* inserting character */
650
Bram Moolenaard12a1322005-08-21 22:08:24 +0000651/* values for ts_flags */
652#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
653#define TSF_DIDSPLIT 2 /* tried split at this point */
654
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000655/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000656#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000657#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
658#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000659
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000660/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000661#define FIND_FOLDWORD 0 /* find word case-folded */
662#define FIND_KEEPWORD 1 /* find keep-case word */
663#define FIND_PREFIX 2 /* find word after prefix */
664#define FIND_COMPOUND 3 /* find case-folded compound word */
665#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000666
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000667static slang_T *slang_alloc __ARGS((char_u *lang));
668static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000669static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000670static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000671static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000672static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000673static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000674static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000675static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000676static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000677static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000678static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000679static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000680static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681static 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 +0000682static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000683static char_u *read_string __ARGS((FILE *fd, int cnt));
684static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
685static int read_charflags_section __ARGS((FILE *fd));
686static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
687static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
688static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
689static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
690static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
691static int init_syl_tab __ARGS((slang_T *slang));
692static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000693static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
694static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000695#ifdef FEAT_MBYTE
696static int *mb_str2wide __ARGS((char_u *s));
697#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000698static 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 +0000699static void clear_midword __ARGS((buf_T *buf));
700static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000701static int find_region __ARGS((char_u *rp, char_u *region));
702static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000703static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000704static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000705static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000706static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000707static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000708static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000709static 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 +0000710#ifdef FEAT_EVAL
711static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
712#endif
713static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
714static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000715static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000716static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000717static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000718static void suggest_try_special __ARGS((suginfo_T *su));
719static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000720static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000721#ifdef FEAT_MBYTE
722static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
723#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000724static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725static void score_comp_sal __ARGS((suginfo_T *su));
726static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000727static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000728static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000729static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000730static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000731static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000732static 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 +0000733static void add_banned __ARGS((suginfo_T *su, char_u *word));
734static int was_banned __ARGS((suginfo_T *su, char_u *word));
735static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000736static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000737static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000738static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
739static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
740static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000741#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000742static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000743#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000744static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000745static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000746static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
747static 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 +0000748
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000749/*
750 * Use our own character-case definitions, because the current locale may
751 * differ from what the .spl file uses.
752 * These must not be called with negative number!
753 */
754#ifndef FEAT_MBYTE
755/* Non-multi-byte implementation. */
756# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
757# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
758# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
759#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000760# if defined(HAVE_WCHAR_H)
761# include <wchar.h> /* for towupper() and towlower() */
762# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000763/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
764 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
765 * the "w" library function for characters above 255 if available. */
766# ifdef HAVE_TOWLOWER
767# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
768 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
769# else
770# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
771 : (c) < 256 ? spelltab.st_fold[c] : (c))
772# endif
773
774# ifdef HAVE_TOWUPPER
775# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
776 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
777# else
778# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
779 : (c) < 256 ? spelltab.st_upper[c] : (c))
780# endif
781
782# ifdef HAVE_ISWUPPER
783# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
784 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
785# else
786# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000787 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000788# endif
789#endif
790
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000791
792static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000793static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000794static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000795static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000796
797/*
798 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000799 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000800 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
801 * or when it's OK it remains unchanged.
802 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000803 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000804 * "capcol" is used to check for a Capitalised word after the end of a
805 * sentence. If it's zero then perform the check. Return the column where to
806 * check next, or -1 when no sentence end was found. If it's NULL then don't
807 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000808 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000809 * Returns the length of the word in bytes, also when it's OK, so that the
810 * caller can skip over the word.
811 */
812 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000813spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000815 char_u *ptr;
816 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000817 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000818{
819 matchinf_T mi; /* Most things are put in "mi" so that it can
820 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000821 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000822 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000823 int wrongcaplen = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000824
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000825 /* A word never starts at a space or a control character. Return quickly
826 * then, skipping over the character. */
827 if (*ptr <= ' ')
828 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000829 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000830
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000831 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000832 * 0X99FF. But when a word character follows do check spelling to find
833 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000834 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000835 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000836 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
837 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000838 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000839 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000840 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000841 nrlen = mi.mi_end - ptr;
842 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000843 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000844 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000845
846 /* Try including the digits in the word. */
847 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000848 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000849 else
850 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000851
Bram Moolenaar0c405862005-06-22 22:26:26 +0000852 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000853 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000854 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000855 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000856 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000858 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000859 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000860
861 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
862 {
863 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000864 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000865 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000867 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000868 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000869 if (capcol != NULL)
870 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000871
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000872 /* We always use the characters up to the next non-word character,
873 * also for bad words. */
874 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000875
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000876 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000877 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000878
Bram Moolenaar5195e452005-08-19 20:32:47 +0000879 /* case-fold the word with one non-word character, so that we can check
880 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000881 if (*mi.mi_fend != NUL)
882 mb_ptr_adv(mi.mi_fend);
883
884 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
885 MAXWLEN + 1);
886 mi.mi_fwordlen = STRLEN(mi.mi_fword);
887
888 /* The word is bad unless we recognize it. */
889 mi.mi_result = SP_BAD;
890
891 /*
892 * Loop over the languages specified in 'spelllang'.
893 * We check them all, because a matching word may be longer than an
894 * already found matching word.
895 */
896 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
897 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
898 {
899 /* Check for a matching word in case-folded words. */
900 find_word(&mi, FIND_FOLDWORD);
901
902 /* Check for a matching word in keep-case words. */
903 find_word(&mi, FIND_KEEPWORD);
904
905 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000906 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000907 }
908
909 if (mi.mi_result != SP_OK)
910 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000911 /* If we found a number skip over it. Allows for "42nd". Do flag
912 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000914 {
915 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
916 return nrlen;
917 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000918
919 /* When we are at a non-word character there is no error, just
920 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000921 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000922 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000923 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
924 {
925 regmatch_T regmatch;
926
927 /* Check for end of sentence. */
928 regmatch.regprog = wp->w_buffer->b_cap_prog;
929 regmatch.rm_ic = FALSE;
930 if (vim_regexec(&regmatch, ptr, 0))
931 *capcol = (int)(regmatch.endp[0] - ptr);
932 }
933
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000934#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000935 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000936 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000937#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000938 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000939 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000940 else if (mi.mi_end == ptr)
941 /* Always include at least one character. Required for when there
942 * is a mixup in "midword". */
943 mb_ptr_adv(mi.mi_end);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000944
945 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
946 *attrp = highlight_attr[HLF_SPB];
947 else if (mi.mi_result == SP_RARE)
948 *attrp = highlight_attr[HLF_SPR];
949 else
950 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000951 }
952
Bram Moolenaar5195e452005-08-19 20:32:47 +0000953 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
954 {
955 /* Report SpellCap only when the word isn't badly spelled. */
956 *attrp = highlight_attr[HLF_SPC];
957 return wrongcaplen;
958 }
959
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 return (int)(mi.mi_end - ptr);
961}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962
Bram Moolenaar51485f02005-06-04 21:55:20 +0000963/*
964 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000965 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
966 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
967 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
968 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000969 *
970 * For a match mip->mi_result is updated.
971 */
972 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000973find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000974 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000975 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000976{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000977 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000978 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000979 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000980 int endidxcnt = 0;
981 int len;
982 int wlen = 0;
983 int flen;
984 int c;
985 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000986 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000987#ifdef FEAT_MBYTE
988 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000989 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000990#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000991 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000992 slang_T *slang = mip->mi_lp->lp_slang;
993 unsigned flags;
994 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000995 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000996 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +0000997 int prefix_found;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000998
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000999 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001000 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001001 /* Check for word with matching case in keep-case tree. */
1002 ptr = mip->mi_word;
1003 flen = 9999; /* no case folding, always enough bytes */
1004 byts = slang->sl_kbyts;
1005 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001006
1007 if (mode == FIND_KEEPCOMPOUND)
1008 /* Skip over the previously found word(s). */
1009 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001010 }
1011 else
1012 {
1013 /* Check for case-folded in case-folded tree. */
1014 ptr = mip->mi_fword;
1015 flen = mip->mi_fwordlen; /* available case-folded bytes */
1016 byts = slang->sl_fbyts;
1017 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001018
1019 if (mode == FIND_PREFIX)
1020 {
1021 /* Skip over the prefix. */
1022 wlen = mip->mi_prefixlen;
1023 flen -= mip->mi_prefixlen;
1024 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001025 else if (mode == FIND_COMPOUND)
1026 {
1027 /* Skip over the previously found word(s). */
1028 wlen = mip->mi_compoff;
1029 flen -= mip->mi_compoff;
1030 }
1031
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001032 }
1033
Bram Moolenaar51485f02005-06-04 21:55:20 +00001034 if (byts == NULL)
1035 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001036
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001038 * Repeat advancing in the tree until:
1039 * - there is a byte that doesn't match,
1040 * - we reach the end of the tree,
1041 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 */
1043 for (;;)
1044 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001045 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001046 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001047
1048 len = byts[arridx++];
1049
1050 /* If the first possible byte is a zero the word could end here.
1051 * Remember this index, we first check for the longest word. */
1052 if (byts[arridx] == 0)
1053 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001054 if (endidxcnt == MAXWLEN)
1055 {
1056 /* Must be a corrupted spell file. */
1057 EMSG(_(e_format));
1058 return;
1059 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 endlen[endidxcnt] = wlen;
1061 endidx[endidxcnt++] = arridx++;
1062 --len;
1063
1064 /* Skip over the zeros, there can be several flag/region
1065 * combinations. */
1066 while (len > 0 && byts[arridx] == 0)
1067 {
1068 ++arridx;
1069 --len;
1070 }
1071 if (len == 0)
1072 break; /* no children, word must end here */
1073 }
1074
1075 /* Stop looking at end of the line. */
1076 if (ptr[wlen] == NUL)
1077 break;
1078
1079 /* Perform a binary search in the list of accepted bytes. */
1080 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001081 if (c == TAB) /* <Tab> is handled like <Space> */
1082 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001083 lo = arridx;
1084 hi = arridx + len - 1;
1085 while (lo < hi)
1086 {
1087 m = (lo + hi) / 2;
1088 if (byts[m] > c)
1089 hi = m - 1;
1090 else if (byts[m] < c)
1091 lo = m + 1;
1092 else
1093 {
1094 lo = hi = m;
1095 break;
1096 }
1097 }
1098
1099 /* Stop if there is no matching byte. */
1100 if (hi < lo || byts[lo] != c)
1101 break;
1102
1103 /* Continue at the child (if there is one). */
1104 arridx = idxs[lo];
1105 ++wlen;
1106 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001107
1108 /* One space in the good word may stand for several spaces in the
1109 * checked word. */
1110 if (c == ' ')
1111 {
1112 for (;;)
1113 {
1114 if (flen <= 0 && *mip->mi_fend != NUL)
1115 flen = fold_more(mip);
1116 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1117 break;
1118 ++wlen;
1119 --flen;
1120 }
1121 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 }
1123
1124 /*
1125 * Verify that one of the possible endings is valid. Try the longest
1126 * first.
1127 */
1128 while (endidxcnt > 0)
1129 {
1130 --endidxcnt;
1131 arridx = endidx[endidxcnt];
1132 wlen = endlen[endidxcnt];
1133
1134#ifdef FEAT_MBYTE
1135 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1136 continue; /* not at first byte of character */
1137#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001138 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001139 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001140 if (slang->sl_compprog == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001141 continue; /* next char is a word character */
1142 word_ends = FALSE;
1143 }
1144 else
1145 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001146 /* The prefix flag is before compound flags. Once a valid prefix flag
1147 * has been found we try compound flags. */
1148 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001149
1150#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001151 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001152 {
1153 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001154 * when folding case. This can be slow, take a shortcut when the
1155 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001156 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001157 if (STRNCMP(ptr, p, wlen) != 0)
1158 {
1159 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1160 mb_ptr_adv(p);
1161 wlen = p - mip->mi_word;
1162 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001163 }
1164#endif
1165
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001166 /* Check flags and region. For FIND_PREFIX check the condition and
1167 * prefix ID.
1168 * Repeat this if there are more flags/region alternatives until there
1169 * is a match. */
1170 res = SP_BAD;
1171 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1172 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001173 {
1174 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001175
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001176 /* For the fold-case tree check that the case of the checked word
1177 * matches with what the word in the tree requires.
1178 * For keep-case tree the case is always right. For prefixes we
1179 * don't bother to check. */
1180 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001181 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001182 if (mip->mi_cend != mip->mi_word + wlen)
1183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001184 /* mi_capflags was set for a different word length, need
1185 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001186 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001187 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001188 }
1189
Bram Moolenaar0c405862005-06-22 22:26:26 +00001190 if (mip->mi_capflags == WF_KEEPCAP
1191 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001192 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001193 }
1194
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001195 /* When mode is FIND_PREFIX the word must support the prefix:
1196 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001197 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001198 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001199 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001200 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001201 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001202 mip->mi_word + mip->mi_cprefixlen, slang,
1203 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001204 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001205 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001206
1207 /* Use the WF_RARE flag for a rare prefix. */
1208 if (c & WF_RAREPFX)
1209 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001210 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001211 }
1212
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001213 if (mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1214 || !word_ends)
1215 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001216 /* If there is no flag or the word is shorter than
1217 * COMPOUNDMIN reject it quickly.
1218 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001219 * that's too short... Myspell compatibility requires this
1220 * anyway. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001221 if (((unsigned)flags >> 24) == 0 || wlen < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001222 continue;
1223
Bram Moolenaar5195e452005-08-19 20:32:47 +00001224 /* Limit the number of compound words to COMPOUNDMAX. */
1225 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001226 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001227
Bram Moolenaard12a1322005-08-21 22:08:24 +00001228 /* Quickly check if compounding is possible with this flag. */
1229 if (vim_strchr(mip->mi_complen == 0
1230 ? slang->sl_compstartflags
1231 : slang->sl_compallflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00001232 ((unsigned)flags >> 24)) == NULL)
1233 continue;
1234
1235 /* If the word ends the sequence of compound flags of the
1236 * words must match with one of the COMPOUNDFLAGS items and
1237 * the number of syllables must not be too large. */
1238 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1239 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1240 if (word_ends)
1241 {
1242 char_u fword[MAXWLEN];
1243
1244 if (slang->sl_compsylmax < MAXWLEN)
1245 {
1246 /* "fword" is only needed for checking syllables. */
1247 if (ptr == mip->mi_word)
1248 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1249 else
1250 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1251 }
1252 if (!can_compound(slang, fword, mip->mi_compflags))
1253 continue;
1254 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001255 }
1256
1257 if (!word_ends)
1258 {
1259 /* Check that a valid word follows. If there is one, it will
1260 * set "mi_result", thus we are always finished here.
1261 * Recursive! */
1262
1263 /* Find following word in case-folded tree. */
1264 mip->mi_compoff = endlen[endidxcnt];
1265#ifdef FEAT_MBYTE
1266 if (has_mbyte && mode == FIND_KEEPWORD)
1267 {
1268 /* Compute byte length in case-folded word from "wlen":
1269 * byte length in keep-case word. Length may change when
1270 * folding case. This can be slow, take a shortcut when
1271 * the case-folded word is equal to the keep-case word. */
1272 p = mip->mi_fword;
1273 if (STRNCMP(ptr, p, wlen) != 0)
1274 {
1275 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1276 mb_ptr_adv(p);
1277 mip->mi_compoff = p - mip->mi_fword;
1278 }
1279 }
1280#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001281 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001282 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001283 find_word(mip, FIND_COMPOUND);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001284
1285 /* Find following word in keep-case tree. */
1286 mip->mi_compoff = wlen;
1287 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001288
1289 /* Check for following word with prefix. */
1290 mip->mi_compoff = c;
1291 find_prefix(mip, FIND_COMPOUND);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001292 --mip->mi_complen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001293
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001294 if (mip->mi_result == SP_OK)
1295 break;
1296 continue;
1297 }
1298
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001299 if (flags & WF_BANNED)
1300 res = SP_BANNED;
1301 else if (flags & WF_REGION)
1302 {
1303 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001304 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001305 res = SP_OK;
1306 else
1307 res = SP_LOCAL;
1308 }
1309 else if (flags & WF_RARE)
1310 res = SP_RARE;
1311 else
1312 res = SP_OK;
1313
1314 /* Always use the longest match and the best result. */
1315 if (mip->mi_result > res)
1316 {
1317 mip->mi_result = res;
1318 mip->mi_end = mip->mi_word + wlen;
1319 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001320 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001321 mip->mi_end = mip->mi_word + wlen;
1322
1323 if (res == SP_OK)
1324 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001325 }
1326
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001327 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001328 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001329 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001330}
1331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001332/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001333 * Return TRUE if "flags" is a valid sequence of compound flags and
1334 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001335 */
1336 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001337can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001338 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001339 char_u *word;
1340 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001341{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001342 regmatch_T regmatch;
1343
1344 if (slang->sl_compprog == NULL)
1345 return FALSE;
1346 regmatch.regprog = slang->sl_compprog;
1347 regmatch.rm_ic = FALSE;
1348 if (!vim_regexec(&regmatch, flags, 0))
1349 return FALSE;
1350
1351 /* Count the number of syllables. This may be slow, do it last. */
1352 if (slang->sl_compsylmax < MAXWLEN
1353 && count_syllables(slang, word) > slang->sl_compsylmax)
1354 return FALSE;
1355 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001356}
1357
1358/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001359 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1360 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001361 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001362 */
1363 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001364valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001365 int totprefcnt; /* nr of prefix IDs */
1366 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001367 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001368 char_u *word;
1369 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001370 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001371{
1372 int prefcnt;
1373 int pidx;
1374 regprog_T *rp;
1375 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001376 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001377
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001378 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001379 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1380 {
1381 pidx = slang->sl_pidxs[arridx + prefcnt];
1382
1383 /* Check the prefix ID. */
1384 if (prefid != (pidx & 0xff))
1385 continue;
1386
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001387 /* Check if the prefix doesn't combine and the word already has a
1388 * suffix. */
1389 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1390 continue;
1391
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001392 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001393 * stored in the two bytes above the prefix ID byte. */
1394 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001395 if (rp != NULL)
1396 {
1397 regmatch.regprog = rp;
1398 regmatch.rm_ic = FALSE;
1399 if (!vim_regexec(&regmatch, word, 0))
1400 continue;
1401 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001402 else if (cond_req)
1403 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001404
Bram Moolenaar53805d12005-08-01 07:08:33 +00001405 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001406 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001407 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001408 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001409}
1410
1411/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 * Check if the word at "mip->mi_word" has a matching prefix.
1413 * If it does, then check the following word.
1414 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001415 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1416 * prefix in a compound word.
1417 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001418 * For a match mip->mi_result is updated.
1419 */
1420 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001421find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001422 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001423 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424{
1425 idx_T arridx = 0;
1426 int len;
1427 int wlen = 0;
1428 int flen;
1429 int c;
1430 char_u *ptr;
1431 idx_T lo, hi, m;
1432 slang_T *slang = mip->mi_lp->lp_slang;
1433 char_u *byts;
1434 idx_T *idxs;
1435
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001436 byts = slang->sl_pbyts;
1437 if (byts == NULL)
1438 return; /* array is empty */
1439
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001440 /* We use the case-folded word here, since prefixes are always
1441 * case-folded. */
1442 ptr = mip->mi_fword;
1443 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001444 if (mode == FIND_COMPOUND)
1445 {
1446 /* Skip over the previously found word(s). */
1447 ptr += mip->mi_compoff;
1448 flen -= mip->mi_compoff;
1449 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001450 idxs = slang->sl_pidxs;
1451
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001452 /*
1453 * Repeat advancing in the tree until:
1454 * - there is a byte that doesn't match,
1455 * - we reach the end of the tree,
1456 * - or we reach the end of the line.
1457 */
1458 for (;;)
1459 {
1460 if (flen == 0 && *mip->mi_fend != NUL)
1461 flen = fold_more(mip);
1462
1463 len = byts[arridx++];
1464
1465 /* If the first possible byte is a zero the prefix could end here.
1466 * Check if the following word matches and supports the prefix. */
1467 if (byts[arridx] == 0)
1468 {
1469 /* There can be several prefixes with different conditions. We
1470 * try them all, since we don't know which one will give the
1471 * longest match. The word is the same each time, pass the list
1472 * of possible prefixes to find_word(). */
1473 mip->mi_prefarridx = arridx;
1474 mip->mi_prefcnt = len;
1475 while (len > 0 && byts[arridx] == 0)
1476 {
1477 ++arridx;
1478 --len;
1479 }
1480 mip->mi_prefcnt -= len;
1481
1482 /* Find the word that comes after the prefix. */
1483 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001484 if (mode == FIND_COMPOUND)
1485 /* Skip over the previously found word(s). */
1486 mip->mi_prefixlen += mip->mi_compoff;
1487
Bram Moolenaar53805d12005-08-01 07:08:33 +00001488#ifdef FEAT_MBYTE
1489 if (has_mbyte)
1490 {
1491 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001492 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1493 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001494 }
1495 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001496 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001497#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001498 find_word(mip, FIND_PREFIX);
1499
1500
1501 if (len == 0)
1502 break; /* no children, word must end here */
1503 }
1504
1505 /* Stop looking at end of the line. */
1506 if (ptr[wlen] == NUL)
1507 break;
1508
1509 /* Perform a binary search in the list of accepted bytes. */
1510 c = ptr[wlen];
1511 lo = arridx;
1512 hi = arridx + len - 1;
1513 while (lo < hi)
1514 {
1515 m = (lo + hi) / 2;
1516 if (byts[m] > c)
1517 hi = m - 1;
1518 else if (byts[m] < c)
1519 lo = m + 1;
1520 else
1521 {
1522 lo = hi = m;
1523 break;
1524 }
1525 }
1526
1527 /* Stop if there is no matching byte. */
1528 if (hi < lo || byts[lo] != c)
1529 break;
1530
1531 /* Continue at the child (if there is one). */
1532 arridx = idxs[lo];
1533 ++wlen;
1534 --flen;
1535 }
1536}
1537
1538/*
1539 * Need to fold at least one more character. Do until next non-word character
1540 * for efficiency.
1541 * Return the length of the folded chars in bytes.
1542 */
1543 static int
1544fold_more(mip)
1545 matchinf_T *mip;
1546{
1547 int flen;
1548 char_u *p;
1549
1550 p = mip->mi_fend;
1551 do
1552 {
1553 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001554 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001555
1556 /* Include the non-word character so that we can check for the
1557 * word end. */
1558 if (*mip->mi_fend != NUL)
1559 mb_ptr_adv(mip->mi_fend);
1560
1561 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1562 mip->mi_fword + mip->mi_fwordlen,
1563 MAXWLEN - mip->mi_fwordlen);
1564 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1565 mip->mi_fwordlen += flen;
1566 return flen;
1567}
1568
1569/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001570 * Check case flags for a word. Return TRUE if the word has the requested
1571 * case.
1572 */
1573 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001574spell_valid_case(wordflags, treeflags)
1575 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001576 int treeflags; /* flags for the word in the spell tree */
1577{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001578 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001579 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001580 && ((treeflags & WF_ONECAP) == 0
1581 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001582}
1583
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001584/*
1585 * Return TRUE if spell checking is not enabled.
1586 */
1587 static int
1588no_spell_checking()
1589{
1590 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1591 {
1592 EMSG(_("E756: Spell checking is not enabled"));
1593 return TRUE;
1594 }
1595 return FALSE;
1596}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001597
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001598/*
1599 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001600 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001601 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1602 * to after badly spelled word before the cursor.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001603 * Return OK if found, FAIL otherwise.
1604 */
1605 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001606spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001607 int dir; /* FORWARD or BACKWARD */
1608 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001609 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001610{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001611 linenr_T lnum;
1612 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001613 char_u *line;
1614 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001615 char_u *endp;
1616 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001617 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001618 int has_syntax = syntax_present(curbuf);
1619 int col;
1620 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001621 char_u *buf = NULL;
1622 int buflen = 0;
1623 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001624 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001625
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001626 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001627 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001628
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001629 /*
1630 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001631 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001632 *
1633 * When searching backwards, we continue in the line to find the last
1634 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001635 *
1636 * We concatenate the start of the next line, so that wrapped words work
1637 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1638 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001639 */
1640 lnum = curwin->w_cursor.lnum;
1641 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001642
1643 while (!got_int)
1644 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001645 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001646
Bram Moolenaar0c405862005-06-22 22:26:26 +00001647 len = STRLEN(line);
1648 if (buflen < len + MAXWLEN + 2)
1649 {
1650 vim_free(buf);
1651 buflen = len + MAXWLEN + 2;
1652 buf = alloc(buflen);
1653 if (buf == NULL)
1654 break;
1655 }
1656
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001657 /* In first line check first word for Capital. */
1658 if (lnum == 1)
1659 capcol = 0;
1660
1661 /* For checking first word with a capital skip white space. */
1662 if (capcol == 0)
1663 capcol = skipwhite(line) - line;
1664
Bram Moolenaar0c405862005-06-22 22:26:26 +00001665 /* Copy the line into "buf" and append the start of the next line if
1666 * possible. */
1667 STRCPY(buf, line);
1668 if (lnum < curbuf->b_ml.ml_line_count)
1669 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1670
1671 p = buf + skip;
1672 endp = buf + len;
1673 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001674 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001675 /* When searching backward don't search after the cursor. */
1676 if (dir == BACKWARD
1677 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001678 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001679 break;
1680
1681 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001682 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001683 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001684
1685 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001686 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001687 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001688 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001689 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001690 /* When searching forward only accept a bad word after
1691 * the cursor. */
1692 if (dir == BACKWARD
1693 || lnum > curwin->w_cursor.lnum
1694 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001695 && (colnr_T)(curline ? p - buf + len
1696 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001697 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001698 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001699 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001700 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001701 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001702 (void)syn_get_id(lnum, (colnr_T)col,
1703 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001704 }
1705 else
1706 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001707
Bram Moolenaar51485f02005-06-04 21:55:20 +00001708 if (can_spell)
1709 {
1710 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001711 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001712#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001713 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001714#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001715 if (dir == FORWARD)
1716 {
1717 /* No need to search further. */
1718 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001719 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001720 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001721 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001722 else if (curline)
1723 /* Insert mode completion: put cursor after
1724 * the bad word. */
1725 found_pos.col += len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001726 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001727 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001728 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001729 }
1730
Bram Moolenaar51485f02005-06-04 21:55:20 +00001731 /* advance to character after the word */
1732 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001733 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001734 }
1735
Bram Moolenaar5195e452005-08-19 20:32:47 +00001736 if (dir == BACKWARD && found_pos.lnum != 0)
1737 {
1738 /* Use the last match in the line. */
1739 curwin->w_cursor = found_pos;
1740 vim_free(buf);
1741 return OK;
1742 }
1743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001744 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001745 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001747 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001748 if (dir == BACKWARD)
1749 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001750 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001751 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001752 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001753 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001754 }
1755 else
1756 {
1757 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001758 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001759 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001760
1761 /* Skip the characters at the start of the next line that were
1762 * included in a match crossing line boundaries. */
1763 if (attr == 0)
1764 skip = p - endp;
1765 else
1766 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001767
1768 /* Capscol skips over the inserted space. */
1769 --capcol;
1770
1771 /* But after empty line check first word in next line */
1772 if (*skipwhite(line) == NUL)
1773 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001774 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001775
1776 line_breakcheck();
1777 }
1778
Bram Moolenaar0c405862005-06-22 22:26:26 +00001779 vim_free(buf);
1780 return FAIL;
1781}
1782
1783/*
1784 * For spell checking: concatenate the start of the following line "line" into
1785 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1786 */
1787 void
1788spell_cat_line(buf, line, maxlen)
1789 char_u *buf;
1790 char_u *line;
1791 int maxlen;
1792{
1793 char_u *p;
1794 int n;
1795
1796 p = skipwhite(line);
1797 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1798 p = skipwhite(p + 1);
1799
1800 if (*p != NUL)
1801 {
1802 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001803 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001804 n = p - line;
1805 if (n >= maxlen)
1806 n = maxlen - 1;
1807 vim_memset(buf + 1, ' ', n);
1808 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001809}
1810
1811/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001812 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001813 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001814 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001815 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001816spell_load_lang(lang)
1817 char_u *lang;
1818{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001819 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001821 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822
Bram Moolenaarb765d632005-06-07 21:00:02 +00001823 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001824 * It's truncated when an error is detected. */
1825 STRCPY(langcp, lang);
1826
Bram Moolenaarb765d632005-06-07 21:00:02 +00001827 /*
1828 * Find the first spell file for "lang" in 'runtimepath' and load it.
1829 */
1830 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1831 "spell/%s.%s.spl", lang, spell_enc());
1832 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001833
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001834 if (r == FAIL && *langcp != NUL)
1835 {
1836 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001837 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001838 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001839 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840 }
1841
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001842 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001843 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1844 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001845 else if (*langcp != NUL)
1846 {
1847 /* Load all the additions. */
1848 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1849 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1850 }
1851}
1852
1853/*
1854 * Return the encoding used for spell checking: Use 'encoding', except that we
1855 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1856 */
1857 static char_u *
1858spell_enc()
1859{
1860
1861#ifdef FEAT_MBYTE
1862 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1863 return p_enc;
1864#endif
1865 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001866}
1867
1868/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001869 * Get the name of the .spl file for the internal wordlist into
1870 * "fname[MAXPATHL]".
1871 */
1872 static void
1873int_wordlist_spl(fname)
1874 char_u *fname;
1875{
1876 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1877 int_wordlist, spell_enc());
1878}
1879
1880/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001881 * Allocate a new slang_T.
1882 * Caller must fill "sl_next".
1883 */
1884 static slang_T *
1885slang_alloc(lang)
1886 char_u *lang;
1887{
1888 slang_T *lp;
1889
Bram Moolenaar51485f02005-06-04 21:55:20 +00001890 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001891 if (lp != NULL)
1892 {
1893 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001894 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001895 lp->sl_compmax = MAXWLEN;
1896 lp->sl_compminlen = MAXWLEN;
1897 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001898 }
1899 return lp;
1900}
1901
1902/*
1903 * Free the contents of an slang_T and the structure itself.
1904 */
1905 static void
1906slang_free(lp)
1907 slang_T *lp;
1908{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001909 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001910 vim_free(lp->sl_fname);
1911 slang_clear(lp);
1912 vim_free(lp);
1913}
1914
1915/*
1916 * Clear an slang_T so that the file can be reloaded.
1917 */
1918 static void
1919slang_clear(lp)
1920 slang_T *lp;
1921{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001922 garray_T *gap;
1923 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001924 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001925 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001926
Bram Moolenaar51485f02005-06-04 21:55:20 +00001927 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001928 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001929 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001930 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001931 vim_free(lp->sl_pbyts);
1932 lp->sl_pbyts = NULL;
1933
Bram Moolenaar51485f02005-06-04 21:55:20 +00001934 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001935 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001936 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001937 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001938 vim_free(lp->sl_pidxs);
1939 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001940
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001941 gap = &lp->sl_rep;
1942 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001943 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001944 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1945 vim_free(ftp->ft_from);
1946 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001947 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001948 ga_clear(gap);
1949
1950 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001951 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001952 {
1953 /* "ga_len" is set to 1 without adding an item for latin1 */
1954 if (gap->ga_data != NULL)
1955 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1956 for (i = 0; i < gap->ga_len; ++i)
1957 vim_free(((int **)gap->ga_data)[i]);
1958 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001959 else
1960 /* SAL items: free salitem_T items */
1961 while (gap->ga_len > 0)
1962 {
1963 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1964 vim_free(smp->sm_lead);
1965 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1966 vim_free(smp->sm_to);
1967#ifdef FEAT_MBYTE
1968 vim_free(smp->sm_lead_w);
1969 vim_free(smp->sm_oneof_w);
1970 vim_free(smp->sm_to_w);
1971#endif
1972 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001973 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001974
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975 for (i = 0; i < lp->sl_prefixcnt; ++i)
1976 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001977 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001978 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001979 lp->sl_prefprog = NULL;
1980
1981 vim_free(lp->sl_midword);
1982 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001983
Bram Moolenaar5195e452005-08-19 20:32:47 +00001984 vim_free(lp->sl_compprog);
1985 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001986 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001987 lp->sl_compprog = NULL;
1988 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001989 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001990
1991 vim_free(lp->sl_syllable);
1992 lp->sl_syllable = NULL;
1993 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001994
Bram Moolenaarea424162005-06-16 21:51:00 +00001995#ifdef FEAT_MBYTE
1996 {
1997 int todo = lp->sl_map_hash.ht_used;
1998 hashitem_T *hi;
1999
2000 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2001 if (!HASHITEM_EMPTY(hi))
2002 {
2003 --todo;
2004 vim_free(hi->hi_key);
2005 }
2006 }
2007 hash_clear(&lp->sl_map_hash);
2008#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002009
2010 lp->sl_compmax = MAXWLEN;
2011 lp->sl_compminlen = MAXWLEN;
2012 lp->sl_compsylmax = MAXWLEN;
2013 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002014}
2015
2016/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002017 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002018 * Invoked through do_in_runtimepath().
2019 */
2020 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002021spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002022 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002023 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002025 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002026}
2027
2028/*
2029 * Load one spell file and store the info into a slang_T.
2030 *
2031 * This is invoked in two ways:
2032 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2033 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2034 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2035 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002037 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002038 static slang_T *
2039spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002040 char_u *fname;
2041 char_u *lang;
2042 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002043 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002044{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002045 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002046 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002047 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002048 char_u *bp;
2049 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002050 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002051 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002052 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002053 int round;
2054 char_u *save_sourcing_name = sourcing_name;
2055 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002056 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002057 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002058 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002059 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002060
Bram Moolenaarb765d632005-06-07 21:00:02 +00002061 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062 if (fd == NULL)
2063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002064 if (!silent)
2065 EMSG2(_(e_notopen), fname);
2066 else if (p_verbose > 2)
2067 {
2068 verbose_enter();
2069 smsg((char_u *)e_notopen, fname);
2070 verbose_leave();
2071 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002072 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002073 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002074 if (p_verbose > 2)
2075 {
2076 verbose_enter();
2077 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2078 verbose_leave();
2079 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002080
Bram Moolenaarb765d632005-06-07 21:00:02 +00002081 if (old_lp == NULL)
2082 {
2083 lp = slang_alloc(lang);
2084 if (lp == NULL)
2085 goto endFAIL;
2086
2087 /* Remember the file name, used to reload the file when it's updated. */
2088 lp->sl_fname = vim_strsave(fname);
2089 if (lp->sl_fname == NULL)
2090 goto endFAIL;
2091
2092 /* Check for .add.spl. */
2093 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2094 }
2095 else
2096 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002097
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098 /* Set sourcing_name, so that error messages mention the file name. */
2099 sourcing_name = fname;
2100 sourcing_lnum = 0;
2101
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002102 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002103 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002104 for (i = 0; i < VIMSPELLMAGICL; ++i)
2105 buf[i] = getc(fd); /* <fileID> */
2106 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2107 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002108 EMSG(_("E757: This does not look like a spell file"));
2109 goto endFAIL;
2110 }
2111 c = getc(fd); /* <versionnr> */
2112 if (c < VIMSPELLVERSION)
2113 {
2114 EMSG(_("E771: Old spell file, needs to be updated"));
2115 goto endFAIL;
2116 }
2117 else if (c > VIMSPELLVERSION)
2118 {
2119 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002120 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 }
2122
Bram Moolenaar5195e452005-08-19 20:32:47 +00002123
2124 /*
2125 * <SECTIONS>: <section> ... <sectionend>
2126 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2127 */
2128 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002130 n = getc(fd); /* <sectionID> or <sectionend> */
2131 if (n == SN_END)
2132 break;
2133 c = getc(fd); /* <sectionflags> */
2134 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2135 /* <sectionlen> */
2136 if (len < 0)
2137 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002138
Bram Moolenaar5195e452005-08-19 20:32:47 +00002139 res = 0;
2140 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002141 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002142 case SN_REGION:
2143 res = read_region_section(fd, lp, len);
2144 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002145
Bram Moolenaar5195e452005-08-19 20:32:47 +00002146 case SN_CHARFLAGS:
2147 res = read_charflags_section(fd);
2148 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002149
Bram Moolenaar5195e452005-08-19 20:32:47 +00002150 case SN_MIDWORD:
2151 lp->sl_midword = read_string(fd, len); /* <midword> */
2152 if (lp->sl_midword == NULL)
2153 goto endFAIL;
2154 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002155
Bram Moolenaar5195e452005-08-19 20:32:47 +00002156 case SN_PREFCOND:
2157 res = read_prefcond_section(fd, lp);
2158 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002159
Bram Moolenaar5195e452005-08-19 20:32:47 +00002160 case SN_REP:
2161 res = read_rep_section(fd, lp);
2162 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002163
Bram Moolenaar5195e452005-08-19 20:32:47 +00002164 case SN_SAL:
2165 res = read_sal_section(fd, lp);
2166 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002167
Bram Moolenaar5195e452005-08-19 20:32:47 +00002168 case SN_SOFO:
2169 res = read_sofo_section(fd, lp);
2170 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002171
Bram Moolenaar5195e452005-08-19 20:32:47 +00002172 case SN_MAP:
2173 p = read_string(fd, len); /* <mapstr> */
2174 if (p == NULL)
2175 goto endFAIL;
2176 set_map_str(lp, p);
2177 vim_free(p);
2178 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002179
Bram Moolenaar5195e452005-08-19 20:32:47 +00002180 case SN_COMPOUND:
2181 res = read_compound(fd, lp, len);
2182 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002183
Bram Moolenaar5195e452005-08-19 20:32:47 +00002184 case SN_SYLLABLE:
2185 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2186 if (lp->sl_syllable == NULL)
2187 goto endFAIL;
2188 if (init_syl_tab(lp) == FAIL)
2189 goto endFAIL;
2190 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002191
Bram Moolenaar5195e452005-08-19 20:32:47 +00002192 default:
2193 /* Unsupported section. When it's required give an error
2194 * message. When it's not required skip the contents. */
2195 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002196 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002197 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002198 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002199 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002200 while (--len >= 0)
2201 if (getc(fd) < 0)
2202 goto truncerr;
2203 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002204 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002205 if (res == SP_FORMERROR)
2206 {
2207formerr:
2208 EMSG(_(e_format));
2209 goto endFAIL;
2210 }
2211 if (res == SP_TRUNCERROR)
2212 {
2213truncerr:
2214 EMSG(_(e_spell_trunc));
2215 goto endFAIL;
2216 }
2217 if (res == SP_ERROR)
2218 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002219 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002220
Bram Moolenaar51485f02005-06-04 21:55:20 +00002221 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002222 * round 2: <KWORDTREE>
2223 * round 3: <PREFIXTREE> */
2224 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002225 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002226 /* The tree size was computed when writing the file, so that we can
2227 * allocate it as one long block. <nodecount> */
2228 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2229 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002230 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002231 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002232 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002233 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002234 bp = lalloc((long_u)len, TRUE);
2235 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002236 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002237 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002238 lp->sl_fbyts = bp;
2239 else if (round == 2)
2240 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002241 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002242 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002243
2244 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002245 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2246 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002247 goto endFAIL;
2248 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002249 lp->sl_fidxs = ip;
2250 else if (round == 2)
2251 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002252 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002253 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002254
2255 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002256 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002257 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002258 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002259 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002260 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002261 }
2262 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002263
Bram Moolenaarb765d632005-06-07 21:00:02 +00002264 /* For a new file link it in the list of spell files. */
2265 if (old_lp == NULL)
2266 {
2267 lp->sl_next = first_lang;
2268 first_lang = lp;
2269 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002270
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002271 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002272
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002273endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002274 if (lang != NULL)
2275 /* truncating the name signals the error to spell_load_lang() */
2276 *lang = NUL;
2277 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002278 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002279 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 lp = NULL;
2281 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002282
2283endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002284 if (fd != NULL)
2285 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002286 sourcing_name = save_sourcing_name;
2287 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002288
2289 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002290}
2291
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002292/*
2293 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002294 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002295 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002296 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2297 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002298 */
2299 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002300read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002301 FILE *fd;
2302 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002303 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002304{
2305 int cnt = 0;
2306 int i;
2307 char_u *str;
2308
2309 /* read the length bytes, MSB first */
2310 for (i = 0; i < cnt_bytes; ++i)
2311 cnt = (cnt << 8) + getc(fd);
2312 if (cnt < 0)
2313 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002314 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002315 return NULL;
2316 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002317 *cntp = cnt;
2318 if (cnt == 0)
2319 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002320
Bram Moolenaar5195e452005-08-19 20:32:47 +00002321 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002322 if (str == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002323 *cntp = SP_ERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002324 return str;
2325}
2326
Bram Moolenaar7887d882005-07-01 22:33:52 +00002327/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002328 * Read a string of length "cnt" from "fd" into allocated memory.
2329 * Returns NULL when out of memory.
2330 */
2331 static char_u *
2332read_string(fd, cnt)
2333 FILE *fd;
2334 int cnt;
2335{
2336 char_u *str;
2337 int i;
2338
2339 /* allocate memory */
2340 str = alloc((unsigned)cnt + 1);
2341 if (str != NULL)
2342 {
2343 /* Read the string. Doesn't check for truncated file. */
2344 for (i = 0; i < cnt; ++i)
2345 str[i] = getc(fd);
2346 str[i] = NUL;
2347 }
2348 return str;
2349}
2350
2351/*
2352 * Read SN_REGION: <regionname> ...
2353 * Return SP_*ERROR flags.
2354 */
2355 static int
2356read_region_section(fd, lp, len)
2357 FILE *fd;
2358 slang_T *lp;
2359 int len;
2360{
2361 int i;
2362
2363 if (len > 16)
2364 return SP_FORMERROR;
2365 for (i = 0; i < len; ++i)
2366 lp->sl_regions[i] = getc(fd); /* <regionname> */
2367 lp->sl_regions[len] = NUL;
2368 return 0;
2369}
2370
2371/*
2372 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2373 * <folcharslen> <folchars>
2374 * Return SP_*ERROR flags.
2375 */
2376 static int
2377read_charflags_section(fd)
2378 FILE *fd;
2379{
2380 char_u *flags;
2381 char_u *fol;
2382 int flagslen, follen;
2383
2384 /* <charflagslen> <charflags> */
2385 flags = read_cnt_string(fd, 1, &flagslen);
2386 if (flagslen < 0)
2387 return flagslen;
2388
2389 /* <folcharslen> <folchars> */
2390 fol = read_cnt_string(fd, 2, &follen);
2391 if (follen < 0)
2392 {
2393 vim_free(flags);
2394 return follen;
2395 }
2396
2397 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2398 if (flags != NULL && fol != NULL)
2399 set_spell_charflags(flags, flagslen, fol);
2400
2401 vim_free(flags);
2402 vim_free(fol);
2403
2404 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2405 if ((flags == NULL) != (fol == NULL))
2406 return SP_FORMERROR;
2407 return 0;
2408}
2409
2410/*
2411 * Read SN_PREFCOND section.
2412 * Return SP_*ERROR flags.
2413 */
2414 static int
2415read_prefcond_section(fd, lp)
2416 FILE *fd;
2417 slang_T *lp;
2418{
2419 int cnt;
2420 int i;
2421 int n;
2422 char_u *p;
2423 char_u buf[MAXWLEN + 1];
2424
2425 /* <prefcondcnt> <prefcond> ... */
2426 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2427 if (cnt <= 0)
2428 return SP_FORMERROR;
2429
2430 lp->sl_prefprog = (regprog_T **)alloc_clear(
2431 (unsigned)sizeof(regprog_T *) * cnt);
2432 if (lp->sl_prefprog == NULL)
2433 return SP_ERROR;
2434 lp->sl_prefixcnt = cnt;
2435
2436 for (i = 0; i < cnt; ++i)
2437 {
2438 /* <prefcond> : <condlen> <condstr> */
2439 n = getc(fd); /* <condlen> */
2440 if (n < 0 || n >= MAXWLEN)
2441 return SP_FORMERROR;
2442
2443 /* When <condlen> is zero we have an empty condition. Otherwise
2444 * compile the regexp program used to check for the condition. */
2445 if (n > 0)
2446 {
2447 buf[0] = '^'; /* always match at one position only */
2448 p = buf + 1;
2449 while (n-- > 0)
2450 *p++ = getc(fd); /* <condstr> */
2451 *p = NUL;
2452 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2453 }
2454 }
2455 return 0;
2456}
2457
2458/*
2459 * Read REP items section from "fd": <repcount> <rep> ...
2460 * Return SP_*ERROR flags.
2461 */
2462 static int
2463read_rep_section(fd, slang)
2464 FILE *fd;
2465 slang_T *slang;
2466{
2467 int cnt;
2468 garray_T *gap;
2469 fromto_T *ftp;
2470 short *first;
2471 int i;
2472
2473 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2474 if (cnt < 0)
2475 return SP_TRUNCERROR;
2476
2477 gap = &slang->sl_rep;
2478 if (ga_grow(gap, cnt) == FAIL)
2479 return SP_ERROR;
2480
2481 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2482 for (; gap->ga_len < cnt; ++gap->ga_len)
2483 {
2484 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2485 ftp->ft_from = read_cnt_string(fd, 1, &i);
2486 if (i < 0)
2487 return i;
2488 if (i == 0)
2489 return SP_FORMERROR;
2490 ftp->ft_to = read_cnt_string(fd, 1, &i);
2491 if (i <= 0)
2492 {
2493 vim_free(ftp->ft_from);
2494 if (i < 0)
2495 return i;
2496 return SP_FORMERROR;
2497 }
2498 }
2499
2500 /* Fill the first-index table. */
2501 first = slang->sl_rep_first;
2502 for (i = 0; i < 256; ++i)
2503 first[i] = -1;
2504 for (i = 0; i < gap->ga_len; ++i)
2505 {
2506 ftp = &((fromto_T *)gap->ga_data)[i];
2507 if (first[*ftp->ft_from] == -1)
2508 first[*ftp->ft_from] = i;
2509 }
2510 return 0;
2511}
2512
2513/*
2514 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2515 * Return SP_*ERROR flags.
2516 */
2517 static int
2518read_sal_section(fd, slang)
2519 FILE *fd;
2520 slang_T *slang;
2521{
2522 int i;
2523 int cnt;
2524 garray_T *gap;
2525 salitem_T *smp;
2526 int ccnt;
2527 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002528 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002529
2530 slang->sl_sofo = FALSE;
2531
2532 i = getc(fd); /* <salflags> */
2533 if (i & SAL_F0LLOWUP)
2534 slang->sl_followup = TRUE;
2535 if (i & SAL_COLLAPSE)
2536 slang->sl_collapse = TRUE;
2537 if (i & SAL_REM_ACCENTS)
2538 slang->sl_rem_accents = TRUE;
2539
2540 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2541 if (cnt < 0)
2542 return SP_TRUNCERROR;
2543
2544 gap = &slang->sl_sal;
2545 ga_init2(gap, sizeof(salitem_T), 10);
2546 if (ga_grow(gap, cnt) == FAIL)
2547 return SP_ERROR;
2548
2549 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2550 for (; gap->ga_len < cnt; ++gap->ga_len)
2551 {
2552 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2553 ccnt = getc(fd); /* <salfromlen> */
2554 if (ccnt < 0)
2555 return SP_TRUNCERROR;
2556 if ((p = alloc(ccnt + 2)) == NULL)
2557 return SP_ERROR;
2558 smp->sm_lead = p;
2559
2560 /* Read up to the first special char into sm_lead. */
2561 for (i = 0; i < ccnt; ++i)
2562 {
2563 c = getc(fd); /* <salfrom> */
2564 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2565 break;
2566 *p++ = c;
2567 }
2568 smp->sm_leadlen = p - smp->sm_lead;
2569 *p++ = NUL;
2570
2571 /* Put (abc) chars in sm_oneof, if any. */
2572 if (c == '(')
2573 {
2574 smp->sm_oneof = p;
2575 for (++i; i < ccnt; ++i)
2576 {
2577 c = getc(fd); /* <salfrom> */
2578 if (c == ')')
2579 break;
2580 *p++ = c;
2581 }
2582 *p++ = NUL;
2583 if (++i < ccnt)
2584 c = getc(fd);
2585 }
2586 else
2587 smp->sm_oneof = NULL;
2588
2589 /* Any following chars go in sm_rules. */
2590 smp->sm_rules = p;
2591 if (i < ccnt)
2592 /* store the char we got while checking for end of sm_lead */
2593 *p++ = c;
2594 for (++i; i < ccnt; ++i)
2595 *p++ = getc(fd); /* <salfrom> */
2596 *p++ = NUL;
2597
2598 /* <saltolen> <salto> */
2599 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2600 if (ccnt < 0)
2601 {
2602 vim_free(smp->sm_lead);
2603 return ccnt;
2604 }
2605
2606#ifdef FEAT_MBYTE
2607 if (has_mbyte)
2608 {
2609 /* convert the multi-byte strings to wide char strings */
2610 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2611 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2612 if (smp->sm_oneof == NULL)
2613 smp->sm_oneof_w = NULL;
2614 else
2615 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2616 if (smp->sm_to == NULL)
2617 smp->sm_to_w = NULL;
2618 else
2619 smp->sm_to_w = mb_str2wide(smp->sm_to);
2620 if (smp->sm_lead_w == NULL
2621 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2622 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2623 {
2624 vim_free(smp->sm_lead);
2625 vim_free(smp->sm_to);
2626 vim_free(smp->sm_lead_w);
2627 vim_free(smp->sm_oneof_w);
2628 vim_free(smp->sm_to_w);
2629 return SP_ERROR;
2630 }
2631 }
2632#endif
2633 }
2634
2635 /* Fill the first-index table. */
2636 set_sal_first(slang);
2637
2638 return 0;
2639}
2640
2641/*
2642 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2643 * Return SP_*ERROR flags.
2644 */
2645 static int
2646read_sofo_section(fd, slang)
2647 FILE *fd;
2648 slang_T *slang;
2649{
2650 int cnt;
2651 char_u *from, *to;
2652 int res;
2653
2654 slang->sl_sofo = TRUE;
2655
2656 /* <sofofromlen> <sofofrom> */
2657 from = read_cnt_string(fd, 2, &cnt);
2658 if (cnt < 0)
2659 return cnt;
2660
2661 /* <sofotolen> <sofoto> */
2662 to = read_cnt_string(fd, 2, &cnt);
2663 if (cnt < 0)
2664 {
2665 vim_free(from);
2666 return cnt;
2667 }
2668
2669 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2670 if (from != NULL && to != NULL)
2671 res = set_sofo(slang, from, to);
2672 else if (from != NULL || to != NULL)
2673 res = SP_FORMERROR; /* only one of two strings is an error */
2674 else
2675 res = 0;
2676
2677 vim_free(from);
2678 vim_free(to);
2679 return res;
2680}
2681
2682/*
2683 * Read the compound section from the .spl file:
2684 * <compmax> <compminlen> <compsylmax> <compflags>
2685 * Returns SP_*ERROR flags.
2686 */
2687 static int
2688read_compound(fd, slang, len)
2689 FILE *fd;
2690 slang_T *slang;
2691 int len;
2692{
2693 int todo = len;
2694 int c;
2695 int atstart;
2696 char_u *pat;
2697 char_u *pp;
2698 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002699 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002700
2701 if (todo < 2)
2702 return SP_FORMERROR; /* need at least two bytes */
2703
2704 --todo;
2705 c = getc(fd); /* <compmax> */
2706 if (c < 2)
2707 c = MAXWLEN;
2708 slang->sl_compmax = c;
2709
2710 --todo;
2711 c = getc(fd); /* <compminlen> */
2712 if (c < 1)
2713 c = 3;
2714 slang->sl_compminlen = c;
2715
2716 --todo;
2717 c = getc(fd); /* <compsylmax> */
2718 if (c < 1)
2719 c = MAXWLEN;
2720 slang->sl_compsylmax = c;
2721
2722 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2723 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
2724 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes. */
2725 pat = alloc((unsigned)todo * 2 + 7);
2726 if (pat == NULL)
2727 return SP_ERROR;
2728
Bram Moolenaard12a1322005-08-21 22:08:24 +00002729 /* We also need a list of all flags that can appear at the start and one
2730 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002731 cp = alloc(todo + 1);
2732 if (cp == NULL)
2733 {
2734 vim_free(pat);
2735 return SP_ERROR;
2736 }
2737 slang->sl_compstartflags = cp;
2738 *cp = NUL;
2739
Bram Moolenaard12a1322005-08-21 22:08:24 +00002740 ap = alloc(todo + 1);
2741 if (ap == NULL)
2742 {
2743 vim_free(pat);
2744 return SP_ERROR;
2745 }
2746 slang->sl_compallflags = ap;
2747 *ap = NUL;
2748
Bram Moolenaar5195e452005-08-19 20:32:47 +00002749 pp = pat;
2750 *pp++ = '^';
2751 *pp++ = '\\';
2752 *pp++ = '(';
2753
2754 atstart = 1;
2755 while (todo-- > 0)
2756 {
2757 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002758
2759 /* Add all flags to "sl_compallflags". */
2760 if (vim_strchr((char_u *)"+*[]/", c) == NULL
2761 && vim_strchr(slang->sl_compallflags, c) == NULL)
2762 {
2763 *ap++ = c;
2764 *ap = NUL;
2765 }
2766
Bram Moolenaar5195e452005-08-19 20:32:47 +00002767 if (atstart != 0)
2768 {
2769 /* At start of item: copy flags to "sl_compstartflags". For a
2770 * [abc] item set "atstart" to 2 and copy up to the ']'. */
2771 if (c == '[')
2772 atstart = 2;
2773 else if (c == ']')
2774 atstart = 0;
2775 else
2776 {
2777 if (vim_strchr(slang->sl_compstartflags, c) == NULL)
2778 {
2779 *cp++ = c;
2780 *cp = NUL;
2781 }
2782 if (atstart == 1)
2783 atstart = 0;
2784 }
2785 }
2786 if (c == '/') /* slash separates two items */
2787 {
2788 *pp++ = '\\';
2789 *pp++ = '|';
2790 atstart = 1;
2791 }
2792 else /* normal char, "[abc]" and '*' are copied as-is */
2793 {
2794 if (c == '+')
2795 *pp++ = '\\'; /* "a+" becomes "a\+" */
2796 *pp++ = c;
2797 }
2798 }
2799
2800 *pp++ = '\\';
2801 *pp++ = ')';
2802 *pp++ = '$';
2803 *pp = NUL;
2804
2805 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
2806 vim_free(pat);
2807 if (slang->sl_compprog == NULL)
2808 return SP_FORMERROR;
2809
2810 return 0;
2811}
2812
2813#define SY_MAXLEN 30
2814typedef struct syl_item_S
2815{
2816 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2817 int sy_len;
2818} syl_item_T;
2819
2820/*
2821 * Truncate "slang->sl_syllable" at the first slash and put the following items
2822 * in "slang->sl_syl_items".
2823 */
2824 static int
2825init_syl_tab(slang)
2826 slang_T *slang;
2827{
2828 char_u *p;
2829 char_u *s;
2830 int l;
2831 syl_item_T *syl;
2832
2833 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2834 p = vim_strchr(slang->sl_syllable, '/');
2835 while (p != NULL)
2836 {
2837 *p++ = NUL;
2838 if (p == NUL)
2839 break;
2840 s = p;
2841 p = vim_strchr(p, '/');
2842 if (p == NULL)
2843 l = STRLEN(s);
2844 else
2845 l = p - s;
2846 if (l >= SY_MAXLEN)
2847 return SP_FORMERROR;
2848 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
2849 return SP_ERROR;
2850 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2851 + slang->sl_syl_items.ga_len++;
2852 vim_strncpy(syl->sy_chars, s, l);
2853 syl->sy_len = l;
2854 }
2855 return OK;
2856}
2857
2858/*
2859 * Count the number of syllables in "word".
2860 * When "word" contains spaces the syllables after the last space are counted.
2861 * Returns zero if syllables are not defines.
2862 */
2863 static int
2864count_syllables(slang, word)
2865 slang_T *slang;
2866 char_u *word;
2867{
2868 int cnt = 0;
2869 int skip = FALSE;
2870 char_u *p;
2871 int len;
2872 int i;
2873 syl_item_T *syl;
2874 int c;
2875
2876 if (slang->sl_syllable == NULL)
2877 return 0;
2878
2879 for (p = word; *p != NUL; p += len)
2880 {
2881 /* When running into a space reset counter. */
2882 if (*p == ' ')
2883 {
2884 len = 1;
2885 cnt = 0;
2886 continue;
2887 }
2888
2889 /* Find longest match of syllable items. */
2890 len = 0;
2891 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2892 {
2893 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2894 if (syl->sy_len > len
2895 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2896 len = syl->sy_len;
2897 }
2898 if (len != 0) /* found a match, count syllable */
2899 {
2900 ++cnt;
2901 skip = FALSE;
2902 }
2903 else
2904 {
2905 /* No recognized syllable item, at least a syllable char then? */
2906#ifdef FEAT_MBYTE
2907 c = mb_ptr2char(p);
2908 len = (*mb_ptr2len)(p);
2909#else
2910 c = *p;
2911 len = 1;
2912#endif
2913 if (vim_strchr(slang->sl_syllable, c) == NULL)
2914 skip = FALSE; /* No, search for next syllable */
2915 else if (!skip)
2916 {
2917 ++cnt; /* Yes, count it */
2918 skip = TRUE; /* don't count following syllable chars */
2919 }
2920 }
2921 }
2922 return cnt;
2923}
2924
2925/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00002926 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00002927 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002928 */
2929 static int
2930set_sofo(lp, from, to)
2931 slang_T *lp;
2932 char_u *from;
2933 char_u *to;
2934{
2935 int i;
2936
2937#ifdef FEAT_MBYTE
2938 garray_T *gap;
2939 char_u *s;
2940 char_u *p;
2941 int c;
2942 int *inp;
2943
2944 if (has_mbyte)
2945 {
2946 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2947 * characters. The index is the low byte of the character.
2948 * The list contains from-to pairs with a terminating NUL.
2949 * sl_sal_first[] is used for latin1 "from" characters. */
2950 gap = &lp->sl_sal;
2951 ga_init2(gap, sizeof(int *), 1);
2952 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002953 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002954 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2955 gap->ga_len = 256;
2956
2957 /* First count the number of items for each list. Temporarily use
2958 * sl_sal_first[] for this. */
2959 for (p = from, s = to; *p != NUL && *s != NUL; )
2960 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002961 c = mb_cptr2char_adv(&p);
2962 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002963 if (c >= 256)
2964 ++lp->sl_sal_first[c & 0xff];
2965 }
2966 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002967 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002968
2969 /* Allocate the lists. */
2970 for (i = 0; i < 256; ++i)
2971 if (lp->sl_sal_first[i] > 0)
2972 {
2973 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2974 if (p == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002975 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002976 ((int **)gap->ga_data)[i] = (int *)p;
2977 *(int *)p = 0;
2978 }
2979
2980 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2981 * list. */
2982 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2983 for (p = from, s = to; *p != NUL && *s != NUL; )
2984 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002985 c = mb_cptr2char_adv(&p);
2986 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002987 if (c >= 256)
2988 {
2989 /* Append the from-to chars at the end of the list with
2990 * the low byte. */
2991 inp = ((int **)gap->ga_data)[c & 0xff];
2992 while (*inp != 0)
2993 ++inp;
2994 *inp++ = c; /* from char */
2995 *inp++ = i; /* to char */
2996 *inp++ = NUL; /* NUL at the end */
2997 }
2998 else
2999 /* mapping byte to char is done in sl_sal_first[] */
3000 lp->sl_sal_first[c] = i;
3001 }
3002 }
3003 else
3004#endif
3005 {
3006 /* mapping bytes to bytes is done in sl_sal_first[] */
3007 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003008 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003009
3010 for (i = 0; to[i] != NUL; ++i)
3011 lp->sl_sal_first[from[i]] = to[i];
3012 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3013 }
3014
Bram Moolenaar5195e452005-08-19 20:32:47 +00003015 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003016}
3017
3018/*
3019 * Fill the first-index table for "lp".
3020 */
3021 static void
3022set_sal_first(lp)
3023 slang_T *lp;
3024{
3025 salfirst_T *sfirst;
3026 int i;
3027 salitem_T *smp;
3028 int c;
3029 garray_T *gap = &lp->sl_sal;
3030
3031 sfirst = lp->sl_sal_first;
3032 for (i = 0; i < 256; ++i)
3033 sfirst[i] = -1;
3034 smp = (salitem_T *)gap->ga_data;
3035 for (i = 0; i < gap->ga_len; ++i)
3036 {
3037#ifdef FEAT_MBYTE
3038 if (has_mbyte)
3039 /* Use the lowest byte of the first character. For latin1 it's
3040 * the character, for other encodings it should differ for most
3041 * characters. */
3042 c = *smp[i].sm_lead_w & 0xff;
3043 else
3044#endif
3045 c = *smp[i].sm_lead;
3046 if (sfirst[c] == -1)
3047 {
3048 sfirst[c] = i;
3049#ifdef FEAT_MBYTE
3050 if (has_mbyte)
3051 {
3052 int n;
3053
3054 /* Make sure all entries with this byte are following each
3055 * other. Move the ones that are in the wrong position. Do
3056 * keep the same ordering! */
3057 while (i + 1 < gap->ga_len
3058 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3059 /* Skip over entry with same index byte. */
3060 ++i;
3061
3062 for (n = 1; i + n < gap->ga_len; ++n)
3063 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3064 {
3065 salitem_T tsal;
3066
3067 /* Move entry with same index byte after the entries
3068 * we already found. */
3069 ++i;
3070 --n;
3071 tsal = smp[i + n];
3072 mch_memmove(smp + i + 1, smp + i,
3073 sizeof(salitem_T) * n);
3074 smp[i] = tsal;
3075 }
3076 }
3077#endif
3078 }
3079 }
3080}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003081
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003082#ifdef FEAT_MBYTE
3083/*
3084 * Turn a multi-byte string into a wide character string.
3085 * Return it in allocated memory (NULL for out-of-memory)
3086 */
3087 static int *
3088mb_str2wide(s)
3089 char_u *s;
3090{
3091 int *res;
3092 char_u *p;
3093 int i = 0;
3094
3095 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3096 if (res != NULL)
3097 {
3098 for (p = s; *p != NUL; )
3099 res[i++] = mb_ptr2char_adv(&p);
3100 res[i] = NUL;
3101 }
3102 return res;
3103}
3104#endif
3105
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003106/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003107 * Read one row of siblings from the spell file and store it in the byte array
3108 * "byts" and index array "idxs". Recursively read the children.
3109 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003110 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003111 *
3112 * Returns the index follosing the siblings.
3113 * Returns -1 if the file is shorter than expected.
3114 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003115 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003116 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003117read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003118 FILE *fd;
3119 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003120 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003121 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003122 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003123 int prefixtree; /* TRUE for reading PREFIXTREE */
3124 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003125{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003126 int len;
3127 int i;
3128 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003129 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003130 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003131 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003132#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003133
Bram Moolenaar51485f02005-06-04 21:55:20 +00003134 len = getc(fd); /* <siblingcount> */
3135 if (len <= 0)
3136 return -1;
3137
3138 if (startidx + len >= maxidx)
3139 return -2;
3140 byts[idx++] = len;
3141
3142 /* Read the byte values, flag/region bytes and shared indexes. */
3143 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003144 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003145 c = getc(fd); /* <byte> */
3146 if (c < 0)
3147 return -1;
3148 if (c <= BY_SPECIAL)
3149 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003150 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003151 {
3152 /* No flags, all regions. */
3153 idxs[idx] = 0;
3154 c = 0;
3155 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003156 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003157 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003158 if (prefixtree)
3159 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003160 /* Read the optional pflags byte, the prefix ID and the
3161 * condition nr. In idxs[] store the prefix ID in the low
3162 * byte, the condition index shifted up 8 bits, the flags
3163 * shifted up 24 bits. */
3164 if (c == BY_FLAGS)
3165 c = getc(fd) << 24; /* <pflags> */
3166 else
3167 c = 0;
3168
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003169 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003170
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003171 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3172 if (n >= maxprefcondnr)
3173 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003174 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003175 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003176 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003177 {
3178 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003179 * idxs[] the flags go in the low two bytes, region above
3180 * that and prefix ID above the region. */
3181 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003182 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003183 if (c2 == BY_FLAGS2)
3184 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003185 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003186 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003187 if (c & WF_AFX)
3188 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003189 }
3190
Bram Moolenaar51485f02005-06-04 21:55:20 +00003191 idxs[idx] = c;
3192 c = 0;
3193 }
3194 else /* c == BY_INDEX */
3195 {
3196 /* <nodeidx> */
3197 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3198 if (n < 0 || n >= maxidx)
3199 return -2;
3200 idxs[idx] = n + SHARED_MASK;
3201 c = getc(fd); /* <xbyte> */
3202 }
3203 }
3204 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003205 }
3206
Bram Moolenaar51485f02005-06-04 21:55:20 +00003207 /* Recursively read the children for non-shared siblings.
3208 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3209 * remove SHARED_MASK) */
3210 for (i = 1; i <= len; ++i)
3211 if (byts[startidx + i] != 0)
3212 {
3213 if (idxs[startidx + i] & SHARED_MASK)
3214 idxs[startidx + i] &= ~SHARED_MASK;
3215 else
3216 {
3217 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003218 idx = read_tree(fd, byts, idxs, maxidx, idx,
3219 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003220 if (idx < 0)
3221 break;
3222 }
3223 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003224
Bram Moolenaar51485f02005-06-04 21:55:20 +00003225 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003226}
3227
3228/*
3229 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003230 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003231 */
3232 char_u *
3233did_set_spelllang(buf)
3234 buf_T *buf;
3235{
3236 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003237 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003238 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003239 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003240 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003241 int region_mask;
3242 slang_T *lp;
3243 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003244 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003245 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003246 int len;
3247 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003248 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003249 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003250 char_u *use_region = NULL;
3251 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003252
3253 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003254 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003255
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003256 /* loop over comma separated language names. */
3257 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003258 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003259 /* Get one language name. */
3260 copy_option_part(&splp, lang, MAXWLEN, ",");
3261
Bram Moolenaar5482f332005-04-17 20:18:43 +00003262 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003263 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003264
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003265 /* If the name ends in ".spl" use it as the name of the spell file.
3266 * If there is a region name let "region" point to it and remove it
3267 * from the name. */
3268 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3269 {
3270 filename = TRUE;
3271
Bram Moolenaarb6356332005-07-18 21:40:44 +00003272 /* Locate a region and remove it from the file name. */
3273 p = vim_strchr(gettail(lang), '_');
3274 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3275 && !ASCII_ISALPHA(p[3]))
3276 {
3277 vim_strncpy(region_cp, p + 1, 2);
3278 mch_memmove(p, p + 3, len - (p - lang) - 2);
3279 len -= 3;
3280 region = region_cp;
3281 }
3282 else
3283 dont_use_region = TRUE;
3284
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003285 /* Check if we loaded this language before. */
3286 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3287 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3288 break;
3289 }
3290 else
3291 {
3292 filename = FALSE;
3293 if (len > 3 && lang[len - 3] == '_')
3294 {
3295 region = lang + len - 2;
3296 len -= 3;
3297 lang[len] = NUL;
3298 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003299 else
3300 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003301
3302 /* Check if we loaded this language before. */
3303 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3304 if (STRICMP(lang, lp->sl_name) == 0)
3305 break;
3306 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003307
Bram Moolenaarb6356332005-07-18 21:40:44 +00003308 if (region != NULL)
3309 {
3310 /* If the region differs from what was used before then don't
3311 * use it for 'spellfile'. */
3312 if (use_region != NULL && STRCMP(region, use_region) != 0)
3313 dont_use_region = TRUE;
3314 use_region = region;
3315 }
3316
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003317 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003318 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003319 {
3320 if (filename)
3321 (void)spell_load_file(lang, lang, NULL, FALSE);
3322 else
3323 spell_load_lang(lang);
3324 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003325
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003326 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003327 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003328 */
3329 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003330 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3331 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003332 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003333 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003334 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003335 {
3336 /* find region in sl_regions */
3337 c = find_region(lp->sl_regions, region);
3338 if (c == REGION_ALL)
3339 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003340 if (lp->sl_add)
3341 {
3342 if (*lp->sl_regions != NUL)
3343 /* This addition file is for other regions. */
3344 region_mask = 0;
3345 }
3346 else
3347 /* This is probably an error. Give a warning and
3348 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003349 smsg((char_u *)
3350 _("Warning: region %s not supported"),
3351 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003352 }
3353 else
3354 region_mask = 1 << c;
3355 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003356
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003357 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003358 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003359 if (ga_grow(&ga, 1) == FAIL)
3360 {
3361 ga_clear(&ga);
3362 return e_outofmem;
3363 }
3364 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3365 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3366 ++ga.ga_len;
3367 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003368 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003369 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003370 }
3371
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003372 /* round 0: load int_wordlist, if possible.
3373 * round 1: load first name in 'spellfile'.
3374 * round 2: load second name in 'spellfile.
3375 * etc. */
3376 spf = curbuf->b_p_spf;
3377 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003379 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003380 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003381 /* Internal wordlist, if there is one. */
3382 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003383 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003384 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003385 }
3386 else
3387 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003388 /* One entry in 'spellfile'. */
3389 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3390 STRCAT(spf_name, ".spl");
3391
3392 /* If it was already found above then skip it. */
3393 for (c = 0; c < ga.ga_len; ++c)
3394 if (fullpathcmp(spf_name,
3395 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
3396 FALSE) == FPC_SAME)
3397 break;
3398 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003399 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003400 }
3401
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003402 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3404 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3405 break;
3406 if (lp == NULL)
3407 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003408 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003409 * region name, the region is ignored otherwise. for int_wordlist
3410 * use an arbitrary name. */
3411 if (round == 0)
3412 STRCPY(lang, "internal wordlist");
3413 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003414 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003415 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003416 p = vim_strchr(lang, '.');
3417 if (p != NULL)
3418 *p = NUL; /* truncate at ".encoding.add" */
3419 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003420 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003421 }
3422 if (lp != NULL && ga_grow(&ga, 1) == OK)
3423 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003424 region_mask = REGION_ALL;
3425 if (use_region != NULL && !dont_use_region)
3426 {
3427 /* find region in sl_regions */
3428 c = find_region(lp->sl_regions, use_region);
3429 if (c != REGION_ALL)
3430 region_mask = 1 << c;
3431 else if (*lp->sl_regions != NUL)
3432 /* This spell file is for other regions. */
3433 region_mask = 0;
3434 }
3435
3436 if (region_mask != 0)
3437 {
3438 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3439 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3440 ++ga.ga_len;
3441 use_midword(lp, buf);
3442 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003443 }
3444 }
3445
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003446 /* Add a NULL entry to mark the end of the list. */
3447 if (ga_grow(&ga, 1) == FAIL)
3448 {
3449 ga_clear(&ga);
3450 return e_outofmem;
3451 }
3452 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
3453 ++ga.ga_len;
3454
3455 /* Everything is fine, store the new b_langp value. */
3456 ga_clear(&buf->b_langp);
3457 buf->b_langp = ga;
3458
3459 return NULL;
3460}
3461
3462/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003463 * Clear the midword characters for buffer "buf".
3464 */
3465 static void
3466clear_midword(buf)
3467 buf_T *buf;
3468{
3469 vim_memset(buf->b_spell_ismw, 0, 256);
3470#ifdef FEAT_MBYTE
3471 vim_free(buf->b_spell_ismw_mb);
3472 buf->b_spell_ismw_mb = NULL;
3473#endif
3474}
3475
3476/*
3477 * Use the "sl_midword" field of language "lp" for buffer "buf".
3478 * They add up to any currently used midword characters.
3479 */
3480 static void
3481use_midword(lp, buf)
3482 slang_T *lp;
3483 buf_T *buf;
3484{
3485 char_u *p;
3486
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003487 if (lp->sl_midword == NULL) /* there aren't any */
3488 return;
3489
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003490 for (p = lp->sl_midword; *p != NUL; )
3491#ifdef FEAT_MBYTE
3492 if (has_mbyte)
3493 {
3494 int c, l, n;
3495 char_u *bp;
3496
3497 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003498 l = (*mb_ptr2len)(p);
3499 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003500 buf->b_spell_ismw[c] = TRUE;
3501 else if (buf->b_spell_ismw_mb == NULL)
3502 /* First multi-byte char in "b_spell_ismw_mb". */
3503 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3504 else
3505 {
3506 /* Append multi-byte chars to "b_spell_ismw_mb". */
3507 n = STRLEN(buf->b_spell_ismw_mb);
3508 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3509 if (bp != NULL)
3510 {
3511 vim_free(buf->b_spell_ismw_mb);
3512 buf->b_spell_ismw_mb = bp;
3513 vim_strncpy(bp + n, p, l);
3514 }
3515 }
3516 p += l;
3517 }
3518 else
3519#endif
3520 buf->b_spell_ismw[*p++] = TRUE;
3521}
3522
3523/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003524 * Find the region "region[2]" in "rp" (points to "sl_regions").
3525 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003526 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003527 */
3528 static int
3529find_region(rp, region)
3530 char_u *rp;
3531 char_u *region;
3532{
3533 int i;
3534
3535 for (i = 0; ; i += 2)
3536 {
3537 if (rp[i] == NUL)
3538 return REGION_ALL;
3539 if (rp[i] == region[0] && rp[i + 1] == region[1])
3540 break;
3541 }
3542 return i / 2;
3543}
3544
3545/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003546 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003547 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003548 * Word WF_ONECAP
3549 * W WORD WF_ALLCAP
3550 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003551 */
3552 static int
3553captype(word, end)
3554 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003555 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003556{
3557 char_u *p;
3558 int c;
3559 int firstcap;
3560 int allcap;
3561 int past_second = FALSE; /* past second word char */
3562
3563 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003564 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003565 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003566 return 0; /* only non-word characters, illegal word */
3567#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003568 if (has_mbyte)
3569 c = mb_ptr2char_adv(&p);
3570 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003571#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003572 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003573 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003574
3575 /*
3576 * Need to check all letters to find a word with mixed upper/lower.
3577 * But a word with an upper char only at start is a ONECAP.
3578 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003579 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003580 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003581 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003582 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003583 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003584 {
3585 /* UUl -> KEEPCAP */
3586 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003587 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003588 allcap = FALSE;
3589 }
3590 else if (!allcap)
3591 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003592 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003593 past_second = TRUE;
3594 }
3595
3596 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003597 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003598 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003599 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003600 return 0;
3601}
3602
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003603/*
3604 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3605 * capital. So that make_case_word() can turn WOrd into Word.
3606 * Add ALLCAP for "WOrD".
3607 */
3608 static int
3609badword_captype(word, end)
3610 char_u *word;
3611 char_u *end;
3612{
3613 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003614 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003615 int l, u;
3616 int first;
3617 char_u *p;
3618
3619 if (flags & WF_KEEPCAP)
3620 {
3621 /* Count the number of UPPER and lower case letters. */
3622 l = u = 0;
3623 first = FALSE;
3624 for (p = word; p < end; mb_ptr_adv(p))
3625 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003626 c = PTR2CHAR(p);
3627 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003628 {
3629 ++u;
3630 if (p == word)
3631 first = TRUE;
3632 }
3633 else
3634 ++l;
3635 }
3636
3637 /* If there are more UPPER than lower case letters suggest an
3638 * ALLCAP word. Otherwise, if the first letter is UPPER then
3639 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3640 * require three upper case letters. */
3641 if (u > l && u > 2)
3642 flags |= WF_ALLCAP;
3643 else if (first)
3644 flags |= WF_ONECAP;
3645 }
3646 return flags;
3647}
3648
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003649# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3650/*
3651 * Free all languages.
3652 */
3653 void
3654spell_free_all()
3655{
3656 slang_T *lp;
3657 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003658 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003659
3660 /* Go through all buffers and handle 'spelllang'. */
3661 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3662 ga_clear(&buf->b_langp);
3663
3664 while (first_lang != NULL)
3665 {
3666 lp = first_lang;
3667 first_lang = lp->sl_next;
3668 slang_free(lp);
3669 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003670
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003671 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003672 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003673 /* Delete the internal wordlist and its .spl file */
3674 mch_remove(int_wordlist);
3675 int_wordlist_spl(fname);
3676 mch_remove(fname);
3677 vim_free(int_wordlist);
3678 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003679 }
3680
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003681 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003682}
3683# endif
3684
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003685# if defined(FEAT_MBYTE) || defined(PROTO)
3686/*
3687 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003688 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003689 */
3690 void
3691spell_reload()
3692{
3693 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003694 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003695
Bram Moolenaarea408852005-06-25 22:49:46 +00003696 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003697 init_spell_chartab();
3698
3699 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003700 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003701
3702 /* Go through all buffers and handle 'spelllang'. */
3703 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3704 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003705 /* Only load the wordlists when 'spelllang' is set and there is a
3706 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003707 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003708 {
3709 FOR_ALL_WINDOWS(wp)
3710 if (wp->w_buffer == buf && wp->w_p_spell)
3711 {
3712 (void)did_set_spelllang(buf);
3713# ifdef FEAT_WINDOWS
3714 break;
3715# endif
3716 }
3717 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003718 }
3719}
3720# endif
3721
Bram Moolenaarb765d632005-06-07 21:00:02 +00003722/*
3723 * Reload the spell file "fname" if it's loaded.
3724 */
3725 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003727 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003728 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003729{
3730 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003732
Bram Moolenaarb765d632005-06-07 21:00:02 +00003733 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3734 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3735 {
3736 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003737 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003738 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003739 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741
3742 /* When "zg" was used and the file wasn't loaded yet, should redo
3743 * 'spelllang' to get it loaded. */
3744 if (added_word && !didit)
3745 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003746}
3747
3748
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003749/*
3750 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003751 */
3752
Bram Moolenaar51485f02005-06-04 21:55:20 +00003753#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754 and .dic file. */
3755/*
3756 * Main structure to store the contents of a ".aff" file.
3757 */
3758typedef struct afffile_S
3759{
3760 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003761 int af_slash; /* character used in word for slash */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003762 int af_rar; /* RAR ID for rare word */
3763 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003764 int af_bad; /* BAD ID for banned word */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003765 int af_needaffix; /* NEEDAFFIX ID */
3766 char_u *af_compflags; /* COMPOUNDFLAG and COMPOUNDFLAGS concat'ed */
3767 int af_compmax; /* COMPOUNDMAX */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003768 int af_compminlen; /* COMPOUNDMIN */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003769 int af_compsylmax; /* COMPOUNDSYLMAX */
3770 char_u *af_syllable; /* SYLLABLE */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003771 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003772 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3773 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003774} afffile_T;
3775
3776typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003777/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3778struct affentry_S
3779{
3780 affentry_T *ae_next; /* next affix with same name/number */
3781 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3782 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003783 char_u *ae_cond; /* condition (NULL for ".") */
3784 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003785 char_u ae_rare; /* rare affix */
3786 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003787};
3788
Bram Moolenaar53805d12005-08-01 07:08:33 +00003789#define AH_KEY_LEN 10
3790
Bram Moolenaar51485f02005-06-04 21:55:20 +00003791/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3792typedef struct affheader_S
3793{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003794 /* key for hashtable == name of affix entry */
3795#ifdef FEAT_MBYTE
3796 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3797#else
3798 char_u ah_key[2]; /* one byte char plus NUL */
3799#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003800 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003801 int ah_combine; /* suffix may combine with prefix */
3802 affentry_T *ah_first; /* first affix entry */
3803} affheader_T;
3804
3805#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3806
3807/*
3808 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003809 * the need to keep track of each allocated thing, everything is freed all at
3810 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003811 */
3812#define SBLOCKSIZE 16000 /* size of sb_data */
3813typedef struct sblock_S sblock_T;
3814struct sblock_S
3815{
3816 sblock_T *sb_next; /* next block in list */
3817 int sb_used; /* nr of bytes already in use */
3818 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003819};
3820
3821/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003822 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003823 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003824typedef struct wordnode_S wordnode_T;
3825struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003826{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003827 union /* shared to save space */
3828 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003829 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003830 int index; /* index in written nodes (valid after first
3831 round) */
3832 } wn_u1;
3833 union /* shared to save space */
3834 {
3835 wordnode_T *next; /* next node with same hash key */
3836 wordnode_T *wnode; /* parent node that will write this node */
3837 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003838 wordnode_T *wn_child; /* child (next byte in word) */
3839 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3840 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003841 int wn_refs; /* Nr. of references to this node. Only
3842 relevant for first node in a list of
3843 siblings, in following siblings it is
3844 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003845 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003846 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003847 prefix ID or 0 */
3848 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3849 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003850 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003851#ifdef SPELL_PRINTTREE
3852 int wn_nr; /* sequence nr for printing */
3853#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003854};
3855
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003856#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3857
Bram Moolenaar51485f02005-06-04 21:55:20 +00003858#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003859
Bram Moolenaar51485f02005-06-04 21:55:20 +00003860/*
3861 * Info used while reading the spell files.
3862 */
3863typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003864{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003865 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003866 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003867
Bram Moolenaar51485f02005-06-04 21:55:20 +00003868 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003869 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003870
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003871 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003872
Bram Moolenaar51485f02005-06-04 21:55:20 +00003873 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003874 long si_blocks_cnt; /* memory blocks allocated */
3875 long si_compress_cnt; /* words to add before lowering
3876 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003877 wordnode_T *si_first_free; /* List of nodes that have been freed during
3878 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003879 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003880#ifdef SPELL_PRINTTREE
3881 int si_wordnode_nr; /* sequence nr for nodes */
3882#endif
3883
3884
Bram Moolenaar51485f02005-06-04 21:55:20 +00003885 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003886 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003887 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003888 int si_region; /* region mask */
3889 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003890 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003891 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003892 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003893 int si_region_count; /* number of regions supported (1 when there
3894 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003895 char_u si_region_name[16]; /* region names; used only if
3896 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003897
3898 garray_T si_rep; /* list of fromto_T entries from REP lines */
3899 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003900 char_u *si_sofofr; /* SOFOFROM text */
3901 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003902 int si_followup; /* soundsalike: ? */
3903 int si_collapse; /* soundsalike: ? */
3904 int si_rem_accents; /* soundsalike: remove accents */
3905 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003906 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003907 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003908 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003909 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003910 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003911 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003912 garray_T si_prefcond; /* table with conditions for postponed
3913 * prefixes, each stored as a string */
3914 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003915} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003916
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003917static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003918static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003919static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3920static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003921static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003922static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003923static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00003924static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
3925static void get_compflags __ARGS((spellinfo_T *spin, char_u *afflist, char_u *store_afflist));
3926static 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 +00003927static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3928static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3929static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003930static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003931static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00003932static 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 +00003933static 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 +00003934static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3935static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3936static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3937static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3938static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003940static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003941static void clear_node __ARGS((wordnode_T *node));
3942static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003944static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003945
Bram Moolenaar53805d12005-08-01 07:08:33 +00003946/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3947 * but it must be negative to indicate the prefix tree to tree_add_word().
3948 * Use a negative number with the lower 8 bits zero. */
3949#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003950
Bram Moolenaar5195e452005-08-19 20:32:47 +00003951/*
3952 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
3953 */
3954static long compress_start = 30000; /* memory / SBLOCKSIZE */
3955static long compress_inc = 100; /* memory / SBLOCKSIZE */
3956static long compress_added = 500000; /* word count */
3957
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003958#ifdef SPELL_PRINTTREE
3959/*
3960 * For debugging the tree code: print the current tree in a (more or less)
3961 * readable format, so that we can see what happens when adding a word and/or
3962 * compressing the tree.
3963 * Based on code from Olaf Seibert.
3964 */
3965#define PRINTLINESIZE 1000
3966#define PRINTWIDTH 6
3967
3968#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3969 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3970
3971static char line1[PRINTLINESIZE];
3972static char line2[PRINTLINESIZE];
3973static char line3[PRINTLINESIZE];
3974
3975 static void
3976spell_clear_flags(wordnode_T *node)
3977{
3978 wordnode_T *np;
3979
3980 for (np = node; np != NULL; np = np->wn_sibling)
3981 {
3982 np->wn_u1.index = FALSE;
3983 spell_clear_flags(np->wn_child);
3984 }
3985}
3986
3987 static void
3988spell_print_node(wordnode_T *node, int depth)
3989{
3990 if (node->wn_u1.index)
3991 {
3992 /* Done this node before, print the reference. */
3993 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3994 PRINTSOME(line2, depth, " ", 0, 0);
3995 PRINTSOME(line3, depth, " ", 0, 0);
3996 msg(line1);
3997 msg(line2);
3998 msg(line3);
3999 }
4000 else
4001 {
4002 node->wn_u1.index = TRUE;
4003
4004 if (node->wn_byte != NUL)
4005 {
4006 if (node->wn_child != NULL)
4007 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4008 else
4009 /* Cannot happen? */
4010 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4011 }
4012 else
4013 PRINTSOME(line1, depth, " $ ", 0, 0);
4014
4015 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4016
4017 if (node->wn_sibling != NULL)
4018 PRINTSOME(line3, depth, " | ", 0, 0);
4019 else
4020 PRINTSOME(line3, depth, " ", 0, 0);
4021
4022 if (node->wn_byte == NUL)
4023 {
4024 msg(line1);
4025 msg(line2);
4026 msg(line3);
4027 }
4028
4029 /* do the children */
4030 if (node->wn_byte != NUL && node->wn_child != NULL)
4031 spell_print_node(node->wn_child, depth + 1);
4032
4033 /* do the siblings */
4034 if (node->wn_sibling != NULL)
4035 {
4036 /* get rid of all parent details except | */
4037 STRCPY(line1, line3);
4038 STRCPY(line2, line3);
4039 spell_print_node(node->wn_sibling, depth);
4040 }
4041 }
4042}
4043
4044 static void
4045spell_print_tree(wordnode_T *root)
4046{
4047 if (root != NULL)
4048 {
4049 /* Clear the "wn_u1.index" fields, used to remember what has been
4050 * done. */
4051 spell_clear_flags(root);
4052
4053 /* Recursively print the tree. */
4054 spell_print_node(root, 0);
4055 }
4056}
4057#endif /* SPELL_PRINTTREE */
4058
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004060 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004061 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062 */
4063 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004064spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004065 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004066 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004067{
4068 FILE *fd;
4069 afffile_T *aff;
4070 char_u rline[MAXLINELEN];
4071 char_u *line;
4072 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004073#define MAXITEMCNT 7
4074 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 int itemcnt;
4076 char_u *p;
4077 int lnum = 0;
4078 affheader_T *cur_aff = NULL;
4079 int aff_todo = 0;
4080 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004081 char_u *low = NULL;
4082 char_u *fol = NULL;
4083 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004084 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 int do_rep;
4086 int do_sal;
4087 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004088 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004089 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004091 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004092 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093
Bram Moolenaar51485f02005-06-04 21:55:20 +00004094 /*
4095 * Open the file.
4096 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004097 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004098 if (fd == NULL)
4099 {
4100 EMSG2(_(e_notopen), fname);
4101 return NULL;
4102 }
4103
Bram Moolenaarb765d632005-06-07 21:00:02 +00004104 if (spin->si_verbose || p_verbose > 2)
4105 {
4106 if (!spin->si_verbose)
4107 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004108 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004109 out_flush();
4110 if (!spin->si_verbose)
4111 verbose_leave();
4112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 /* Only do REP lines when not done in another .aff file already. */
4115 do_rep = spin->si_rep.ga_len == 0;
4116
4117 /* Only do SAL lines when not done in another .aff file already. */
4118 do_sal = spin->si_sal.ga_len == 0;
4119
4120 /* Only do MAP lines when not done in another .aff file already. */
4121 do_map = spin->si_map.ga_len == 0;
4122
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004123 /* Only do MIDWORD line when not done in another .aff file already */
4124 do_midword = spin->si_midword == NULL;
4125
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004126 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
4127 do_sofo = spin->si_sofofr == NULL;
4128
Bram Moolenaar51485f02005-06-04 21:55:20 +00004129 /*
4130 * Allocate and init the afffile_T structure.
4131 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004132 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 if (aff == NULL)
4134 return NULL;
4135 hash_init(&aff->af_pref);
4136 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004137
4138 /*
4139 * Read all the lines in the file one by one.
4140 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004141 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004142 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004143 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004144 ++lnum;
4145
4146 /* Skip comment lines. */
4147 if (*rline == '#')
4148 continue;
4149
4150 /* Convert from "SET" to 'encoding' when needed. */
4151 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004152#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004153 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004154 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004155 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004156 if (pc == NULL)
4157 {
4158 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4159 fname, lnum, rline);
4160 continue;
4161 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004162 line = pc;
4163 }
4164 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004165#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004166 {
4167 pc = NULL;
4168 line = rline;
4169 }
4170
4171 /* Split the line up in white separated items. Put a NUL after each
4172 * item. */
4173 itemcnt = 0;
4174 for (p = line; ; )
4175 {
4176 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4177 ++p;
4178 if (*p == NUL)
4179 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004180 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004181 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004182 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004183 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004184 ++p;
4185 if (*p == NUL)
4186 break;
4187 *p++ = NUL;
4188 }
4189
4190 /* Handle non-empty lines. */
4191 if (itemcnt > 0)
4192 {
4193 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4194 && aff->af_enc == NULL)
4195 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004196#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004197 /* Setup for conversion from "ENC" to 'encoding'. */
4198 aff->af_enc = enc_canonize(items[1]);
4199 if (aff->af_enc != NULL && !spin->si_ascii
4200 && convert_setup(&spin->si_conv, aff->af_enc,
4201 p_enc) == FAIL)
4202 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4203 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004204 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004205#else
4206 smsg((char_u *)_("Conversion in %s not supported"), fname);
4207#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004208 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004209 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
4210 {
4211 if (do_midword)
4212 spin->si_midword = vim_strsave(items[1]);
4213 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004214 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004221 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004222 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4223 && aff->af_slash == 0)
4224 {
4225 aff->af_slash = items[1][0];
4226 if (items[1][1] != NUL)
4227 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4228 fname, lnum, items[1]);
4229 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004230 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4231 && aff->af_rar == 0)
4232 {
4233 aff->af_rar = items[1][0];
4234 if (items[1][1] != NUL)
4235 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4236 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004237 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4238 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004239 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004240 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004241 if (items[1][1] != NUL)
4242 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4243 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004244 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4245 && aff->af_bad == 0)
4246 {
4247 aff->af_bad = items[1][0];
4248 if (items[1][1] != NUL)
4249 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4250 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004251 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4252 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004253 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004254 aff->af_needaffix = items[1][0];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004255 if (items[1][1] != NUL)
4256 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4257 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004258 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
4259 && aff->af_compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004260 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004261 p = getroom(spin, 3, FALSE);
4262 if (p != NULL)
4263 {
4264 /* Turn single flag "c" into COMPOUNDFLAGS compatible
4265 * string "c+". */
4266 p[0] = items[1][0];
4267 p[1] = '+';
4268 p[2] = NUL;
4269 aff->af_compflags = p;
4270 }
4271 if (items[1][1] != NUL)
4272 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4273 }
4274 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4275 {
4276 /* Concatenate this string to previously defined ones, using a
4277 * slash to separate them. */
4278 l = STRLEN(items[1]) + 1;
4279 if (aff->af_compflags != NULL)
4280 l += STRLEN(aff->af_compflags) + 1;
4281 p = getroom(spin, l, FALSE);
4282 if (p != NULL)
4283 {
4284 if (aff->af_compflags != NULL)
4285 {
4286 STRCPY(p, aff->af_compflags);
4287 STRCAT(p, "/");
4288 }
4289 STRCAT(p, items[1]);
4290 aff->af_compflags = p;
4291 }
4292 }
4293 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
4294 && aff->af_compmax == 0)
4295 {
4296 aff->af_compmax = atoi((char *)items[1]);
4297 if (aff->af_compmax == 0)
4298 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4299 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004300 }
4301 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
4302 && aff->af_compminlen == 0)
4303 {
4304 aff->af_compminlen = atoi((char *)items[1]);
4305 if (aff->af_compminlen == 0)
4306 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4307 fname, lnum, items[1]);
4308 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004309 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
4310 && aff->af_compsylmax == 0)
4311 {
4312 aff->af_compsylmax = atoi((char *)items[1]);
4313 if (aff->af_compsylmax == 0)
4314 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4315 fname, lnum, items[1]);
4316 }
4317 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
4318 && aff->af_syllable == NULL)
4319 {
4320 aff->af_syllable = getroom_save(spin, items[1]);
4321 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004322 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4323 {
4324 aff->af_pfxpostpone = TRUE;
4325 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004326 else if ((STRCMP(items[0], "PFX") == 0
4327 || STRCMP(items[0], "SFX") == 0)
4328 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004329 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004330 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00004331 /* Myspell allows extra text after the item, but that might
4332 * mean mistakes go unnoticed. Require a comment-starter. */
4333 if (itemcnt > 4 && *items[4] != '#')
4334 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4335 fname, lnum, items[4]);
4336
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004337 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004338 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004339 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004340 if (cur_aff == NULL)
4341 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004342#ifdef FEAT_MBYTE
4343 if (has_mbyte)
4344 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004345 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004346 if (l >= AH_KEY_LEN)
4347 l = 1; /* too long, must be an overlong sequence */
4348 else
4349 mch_memmove(cur_aff->ah_key, items[1], l);
4350 }
4351 else
4352#endif
4353 {
4354 *cur_aff->ah_key = *items[1];
4355 l = 1;
4356 }
4357 cur_aff->ah_key[l] = NUL;
4358 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004359 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004360 if (*items[2] == 'Y')
4361 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004362 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4364 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004365
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004366 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004367 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004368 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004369 /* Use a new number in the .spl file later, to be able to
4370 * handle multiple .aff files. */
4371 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004372 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004373 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004374 else
4375 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004376 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004377 hi = hash_find(tp, cur_aff->ah_key);
4378 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00004379 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004380 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4381 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004382 aff_todo = 0;
4383 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004384 else
4385 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004386 }
4387 else if ((STRCMP(items[0], "PFX") == 0
4388 || STRCMP(items[0], "SFX") == 0)
4389 && aff_todo > 0
4390 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004391 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 {
4393 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004394 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004395 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004396 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004397 int lasti = 5;
4398
Bram Moolenaar5195e452005-08-19 20:32:47 +00004399 /* Check for "rare" and "nocomp" after the other info. */
4400 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004401 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004402 if (!rare && STRICMP(items[lasti], "rare") == 0)
4403 {
4404 rare = TRUE;
4405 ++lasti;
4406 }
4407 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4408 {
4409 nocomp = TRUE;
4410 ++lasti;
4411 }
4412 else
4413 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004414 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004415
Bram Moolenaar8db73182005-06-17 21:51:16 +00004416 /* Myspell allows extra text after the item, but that might
4417 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004418 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004419 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004420
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004421 /* New item for an affix letter. */
4422 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004423 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004424 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004425 if (aff_entry == NULL)
4426 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004427 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004428 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004429
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004430 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004431 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004432 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004433 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004434
Bram Moolenaar51485f02005-06-04 21:55:20 +00004435 /* Don't use an affix entry with non-ASCII characters when
4436 * "spin->si_ascii" is TRUE. */
4437 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004438 || has_non_ascii(aff_entry->ae_add)))
4439 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004440 aff_entry->ae_next = cur_aff->ah_first;
4441 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004442
4443 if (STRCMP(items[4], ".") != 0)
4444 {
4445 char_u buf[MAXLINELEN];
4446
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004447 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004448 if (*items[0] == 'P')
4449 sprintf((char *)buf, "^%s", items[4]);
4450 else
4451 sprintf((char *)buf, "%s$", items[4]);
4452 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004453 RE_MAGIC + RE_STRING + RE_STRICT);
4454 if (aff_entry->ae_prog == NULL)
4455 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4456 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004457 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004458
4459 /* For postponed prefixes we need an entry in si_prefcond
4460 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004461 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004462 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004463 /* When the chop string is one lower-case letter and
4464 * the add string ends in the upper-case letter we set
4465 * the "upper" flag, clear "ae_chop" and remove the
4466 * letters from "ae_add". The condition must either
4467 * be empty or start with the same letter. */
4468 if (aff_entry->ae_chop != NULL
4469 && aff_entry->ae_add != NULL
4470#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004471 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004472 aff_entry->ae_chop)] == NUL
4473#else
4474 && aff_entry->ae_chop[1] == NUL
4475#endif
4476 )
4477 {
4478 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004479
Bram Moolenaar53805d12005-08-01 07:08:33 +00004480 c = PTR2CHAR(aff_entry->ae_chop);
4481 c_up = SPELL_TOUPPER(c);
4482 if (c_up != c
4483 && (aff_entry->ae_cond == NULL
4484 || PTR2CHAR(aff_entry->ae_cond) == c))
4485 {
4486 p = aff_entry->ae_add
4487 + STRLEN(aff_entry->ae_add);
4488 mb_ptr_back(aff_entry->ae_add, p);
4489 if (PTR2CHAR(p) == c_up)
4490 {
4491 upper = TRUE;
4492 aff_entry->ae_chop = NULL;
4493 *p = NUL;
4494
4495 /* The condition is matched with the
4496 * actual word, thus must check for the
4497 * upper-case letter. */
4498 if (aff_entry->ae_cond != NULL)
4499 {
4500 char_u buf[MAXLINELEN];
4501#ifdef FEAT_MBYTE
4502 if (has_mbyte)
4503 {
4504 onecap_copy(items[4], buf, TRUE);
4505 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004506 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004507 }
4508 else
4509#endif
4510 *aff_entry->ae_cond = c_up;
4511 if (aff_entry->ae_cond != NULL)
4512 {
4513 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004514 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004515 vim_free(aff_entry->ae_prog);
4516 aff_entry->ae_prog = vim_regcomp(
4517 buf, RE_MAGIC + RE_STRING);
4518 }
4519 }
4520 }
4521 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004522 }
4523
Bram Moolenaar53805d12005-08-01 07:08:33 +00004524 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004525 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004526 int idx;
4527 char_u **pp;
4528 int n;
4529
4530 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4531 --idx)
4532 {
4533 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4534 if (str_equal(p, aff_entry->ae_cond))
4535 break;
4536 }
4537 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4538 {
4539 /* Not found, add a new condition. */
4540 idx = spin->si_prefcond.ga_len++;
4541 pp = ((char_u **)spin->si_prefcond.ga_data)
4542 + idx;
4543 if (aff_entry->ae_cond == NULL)
4544 *pp = NULL;
4545 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004546 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004547 aff_entry->ae_cond);
4548 }
4549
4550 /* Add the prefix to the prefix tree. */
4551 if (aff_entry->ae_add == NULL)
4552 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004553 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004554 p = aff_entry->ae_add;
4555 /* PFX_FLAGS is a negative number, so that
4556 * tree_add_word() knows this is the prefix tree. */
4557 n = PFX_FLAGS;
4558 if (rare)
4559 n |= WFP_RARE;
4560 if (!cur_aff->ah_combine)
4561 n |= WFP_NC;
4562 if (upper)
4563 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004564 tree_add_word(spin, p, spin->si_prefroot, n,
4565 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004566 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004567 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004570 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
4571 {
4572 if (fol != NULL)
4573 smsg((char_u *)_("Duplicate FOL in %s line %d"),
4574 fname, lnum);
4575 else
4576 fol = vim_strsave(items[1]);
4577 }
4578 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
4579 {
4580 if (low != NULL)
4581 smsg((char_u *)_("Duplicate LOW in %s line %d"),
4582 fname, lnum);
4583 else
4584 low = vim_strsave(items[1]);
4585 }
4586 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
4587 {
4588 if (upp != NULL)
4589 smsg((char_u *)_("Duplicate UPP in %s line %d"),
4590 fname, lnum);
4591 else
4592 upp = vim_strsave(items[1]);
4593 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004595 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004596 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004597 if (!isdigit(*items[1]))
4598 smsg((char_u *)_("Expected REP count in %s line %d"),
4599 fname, lnum);
4600 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004601 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004602 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004603 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004604 /* Myspell ignores extra arguments, we require it starts with
4605 * # to detect mistakes. */
4606 if (itemcnt > 3 && items[3][0] != '#')
4607 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004608 if (do_rep)
4609 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4610 }
4611 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4612 {
4613 /* MAP item or count */
4614 if (!found_map)
4615 {
4616 /* First line contains the count. */
4617 found_map = TRUE;
4618 if (!isdigit(*items[1]))
4619 smsg((char_u *)_("Expected MAP count in %s line %d"),
4620 fname, lnum);
4621 }
4622 else if (do_map)
4623 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004624 int c;
4625
4626 /* Check that every character appears only once. */
4627 for (p = items[1]; *p != NUL; )
4628 {
4629#ifdef FEAT_MBYTE
4630 c = mb_ptr2char_adv(&p);
4631#else
4632 c = *p++;
4633#endif
4634 if ((spin->si_map.ga_len > 0
4635 && vim_strchr(spin->si_map.ga_data, c)
4636 != NULL)
4637 || vim_strchr(p, c) != NULL)
4638 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4639 fname, lnum);
4640 }
4641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004642 /* We simply concatenate all the MAP strings, separated by
4643 * slashes. */
4644 ga_concat(&spin->si_map, items[1]);
4645 ga_append(&spin->si_map, '/');
4646 }
4647 }
4648 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4649 {
4650 if (do_sal)
4651 {
4652 /* SAL item (sounds-a-like)
4653 * Either one of the known keys or a from-to pair. */
4654 if (STRCMP(items[1], "followup") == 0)
4655 spin->si_followup = sal_to_bool(items[2]);
4656 else if (STRCMP(items[1], "collapse_result") == 0)
4657 spin->si_collapse = sal_to_bool(items[2]);
4658 else if (STRCMP(items[1], "remove_accents") == 0)
4659 spin->si_rem_accents = sal_to_bool(items[2]);
4660 else
4661 /* when "to" is "_" it means empty */
4662 add_fromto(spin, &spin->si_sal, items[1],
4663 STRCMP(items[2], "_") == 0 ? (char_u *)""
4664 : items[2]);
4665 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004666 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004667 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
4668 && (!do_sofo || spin->si_sofofr == NULL))
4669 {
4670 if (do_sofo)
4671 spin->si_sofofr = vim_strsave(items[1]);
4672 }
4673 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
4674 && (!do_sofo || spin->si_sofoto == NULL))
4675 {
4676 if (do_sofo)
4677 spin->si_sofoto = vim_strsave(items[1]);
4678 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004679 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004680 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004681 fname, lnum, items[0]);
4682 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004683 }
4684
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004685 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
4686 smsg((char_u *)_("Missing SOFO%s line in %s"),
4687 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
4688 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
4689 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
4690
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004691 if (fol != NULL || low != NULL || upp != NULL)
4692 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004693 if (spin->si_clear_chartab)
4694 {
4695 /* Clear the char type tables, don't want to use any of the
4696 * currently used spell properties. */
4697 init_spell_chartab();
4698 spin->si_clear_chartab = FALSE;
4699 }
4700
Bram Moolenaar3982c542005-06-08 21:56:31 +00004701 /*
4702 * Don't write a word table for an ASCII file, so that we don't check
4703 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004704 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004705 * mb_get_class(), the list of chars in the file will be incomplete.
4706 */
4707 if (!spin->si_ascii
4708#ifdef FEAT_MBYTE
4709 && !enc_utf8
4710#endif
4711 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004712 {
4713 if (fol == NULL || low == NULL || upp == NULL)
4714 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4715 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004716 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004717 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004718
4719 vim_free(fol);
4720 vim_free(low);
4721 vim_free(upp);
4722 }
4723
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004724 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004725 if (aff->af_compmax != 0)
4726 {
4727 if (spin->si_compmax != 0 && spin->si_compmax != aff->af_compmax)
4728 smsg((char_u *)_("COMPOUNDMAX value differs from what is used in another .aff file"));
4729 else
4730 spin->si_compmax = aff->af_compmax;
4731 }
4732
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004733 if (aff->af_compminlen != 0)
4734 {
4735 if (spin->si_compminlen != 0
4736 && spin->si_compminlen != aff->af_compminlen)
4737 smsg((char_u *)_("COMPOUNDMIN value differs from what is used in another .aff file"));
4738 else
4739 spin->si_compminlen = aff->af_compminlen;
4740 }
4741
Bram Moolenaar5195e452005-08-19 20:32:47 +00004742 if (aff->af_compsylmax != 0)
4743 {
4744 if (aff->af_syllable == NULL)
4745 smsg((char_u *)_("COMPOUNDSYLMAX without SYLLABLE"));
4746
4747 if (spin->si_compsylmax != 0
4748 && spin->si_compsylmax != aff->af_compsylmax)
4749 smsg((char_u *)_("COMPOUNDSYLMAX value differs from what is used in another .aff file"));
4750 else
4751 spin->si_compsylmax = aff->af_compsylmax;
4752 }
4753
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004754 if (aff->af_compflags != NULL)
4755 {
4756 if (spin->si_compflags != NULL
4757 && STRCMP(spin->si_compflags, aff->af_compflags) != 0)
4758 smsg((char_u *)_("COMPOUNDFLAG(S) value differs from what is used in another .aff file"));
4759 else
4760 spin->si_compflags = aff->af_compflags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004761 }
4762
Bram Moolenaar5195e452005-08-19 20:32:47 +00004763 if (aff->af_syllable != NULL)
4764 {
4765 if (spin->si_syllable != NULL
4766 && STRCMP(spin->si_syllable, aff->af_syllable) != 0)
4767 smsg((char_u *)_("SYLLABLE value differs from what is used in another .aff file"));
4768 else
4769 spin->si_syllable = aff->af_syllable;
4770 }
4771
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004772 vim_free(pc);
4773 fclose(fd);
4774 return aff;
4775}
4776
4777/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004778 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
4779 * NULL as equal.
4780 */
4781 static int
4782str_equal(s1, s2)
4783 char_u *s1;
4784 char_u *s2;
4785{
4786 if (s1 == NULL || s2 == NULL)
4787 return s1 == s2;
4788 return STRCMP(s1, s2) == 0;
4789}
4790
4791/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 * Add a from-to item to "gap". Used for REP and SAL items.
4793 * They are stored case-folded.
4794 */
4795 static void
4796add_fromto(spin, gap, from, to)
4797 spellinfo_T *spin;
4798 garray_T *gap;
4799 char_u *from;
4800 char_u *to;
4801{
4802 fromto_T *ftp;
4803 char_u word[MAXWLEN];
4804
4805 if (ga_grow(gap, 1) == OK)
4806 {
4807 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
4808 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004809 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004811 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 ++gap->ga_len;
4813 }
4814}
4815
4816/*
4817 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4818 */
4819 static int
4820sal_to_bool(s)
4821 char_u *s;
4822{
4823 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4824}
4825
4826/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004827 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4828 * When "s" is NULL FALSE is returned.
4829 */
4830 static int
4831has_non_ascii(s)
4832 char_u *s;
4833{
4834 char_u *p;
4835
4836 if (s != NULL)
4837 for (p = s; *p != NUL; ++p)
4838 if (*p >= 128)
4839 return TRUE;
4840 return FALSE;
4841}
4842
4843/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004844 * Free the structure filled by spell_read_aff().
4845 */
4846 static void
4847spell_free_aff(aff)
4848 afffile_T *aff;
4849{
4850 hashtab_T *ht;
4851 hashitem_T *hi;
4852 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004853 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004854 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004855
4856 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004857
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004858 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004859 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4860 {
4861 todo = ht->ht_used;
4862 for (hi = ht->ht_array; todo > 0; ++hi)
4863 {
4864 if (!HASHITEM_EMPTY(hi))
4865 {
4866 --todo;
4867 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004868 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4869 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004870 }
4871 }
4872 if (ht == &aff->af_suff)
4873 break;
4874 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004875
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004876 hash_clear(&aff->af_pref);
4877 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004878}
4879
4880/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004881 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004882 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004883 */
4884 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004885spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004886 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004887 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004888 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004889{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004890 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004891 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004892 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004893 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004894 char_u store_afflist[MAXWLEN];
4895 int pfxlen;
4896 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004898 char_u *pc;
4899 char_u *w;
4900 int l;
4901 hash_T hash;
4902 hashitem_T *hi;
4903 FILE *fd;
4904 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004905 int non_ascii = 0;
4906 int retval = OK;
4907 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004908 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004909 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004910
Bram Moolenaar51485f02005-06-04 21:55:20 +00004911 /*
4912 * Open the file.
4913 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004914 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915 if (fd == NULL)
4916 {
4917 EMSG2(_(e_notopen), fname);
4918 return FAIL;
4919 }
4920
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 /* The hashtable is only used to detect duplicated words. */
4922 hash_init(&ht);
4923
Bram Moolenaarb765d632005-06-07 21:00:02 +00004924 if (spin->si_verbose || p_verbose > 2)
4925 {
4926 if (!spin->si_verbose)
4927 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004928 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004929 out_flush();
4930 if (!spin->si_verbose)
4931 verbose_leave();
4932 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004933
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004934 /* start with a message for the first line */
4935 spin->si_msg_count = 999999;
4936
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004937 /* Read and ignore the first line: word count. */
4938 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004939 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004940 EMSG2(_("E760: No word count in %s"), fname);
4941
4942 /*
4943 * Read all the lines in the file one by one.
4944 * The words are converted to 'encoding' here, before being added to
4945 * the hashtable.
4946 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004947 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004948 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004949 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004951 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004952 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004953
Bram Moolenaar51485f02005-06-04 21:55:20 +00004954 /* Remove CR, LF and white space from the end. White space halfway
4955 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004956 l = STRLEN(line);
4957 while (l > 0 && line[l - 1] <= ' ')
4958 --l;
4959 if (l == 0)
4960 continue; /* empty line */
4961 line[l] = NUL;
4962
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004963 /* Find the optional affix names. Replace the SLASH character by a
4964 * slash. */
4965 afflist = NULL;
4966 for (p = line; *p != NUL; mb_ptr_adv(p))
4967 {
4968 if (*p == affile->af_slash)
4969 *p = '/';
4970 else if (*p == '/')
4971 {
4972 *p = NUL;
4973 afflist = p + 1;
4974 break;
4975 }
4976 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004977
4978 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4979 if (spin->si_ascii && has_non_ascii(line))
4980 {
4981 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004982 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004983 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004984
Bram Moolenaarb765d632005-06-07 21:00:02 +00004985#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004986 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004987 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004988 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004989 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004990 if (pc == NULL)
4991 {
4992 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4993 fname, lnum, line);
4994 continue;
4995 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004996 w = pc;
4997 }
4998 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004999#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005000 {
5001 pc = NULL;
5002 w = line;
5003 }
5004
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005005 /* This takes time, print a message every 10000 words. */
5006 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005007 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005008 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005009 vim_snprintf((char *)message, sizeof(message),
5010 _("line %6d, word %6d - %s"),
5011 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5012 msg_start();
5013 msg_puts_long_attr(message, 0);
5014 msg_clr_eos();
5015 msg_didout = FALSE;
5016 msg_col = 0;
5017 out_flush();
5018 }
5019
Bram Moolenaar51485f02005-06-04 21:55:20 +00005020 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005021 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005022 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005023 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005024 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005025 if (retval == FAIL)
5026 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005027
Bram Moolenaar51485f02005-06-04 21:55:20 +00005028 hash = hash_hash(dw);
5029 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005030 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005031 {
5032 if (p_verbose > 0)
5033 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005034 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005035 else if (duplicate == 0)
5036 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5037 fname, lnum, dw);
5038 ++duplicate;
5039 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005040 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005041 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005042
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005043 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005044 store_afflist[0] = NUL;
5045 pfxlen = 0;
5046 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005047 if (afflist != NULL)
5048 {
5049 /* Check for affix name that stands for keep-case word and stands
5050 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005051 if (affile->af_kep != NUL
5052 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005053 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005054 if (affile->af_rar != NUL
5055 && vim_strchr(afflist, affile->af_rar) != NULL)
5056 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005057 if (affile->af_bad != NUL
5058 && vim_strchr(afflist, affile->af_bad) != NULL)
5059 flags |= WF_BANNED;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005060 if (affile->af_needaffix != NUL
5061 && vim_strchr(afflist, affile->af_needaffix) != NULL)
5062 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005063
5064 if (affile->af_pfxpostpone)
5065 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005066 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005067
Bram Moolenaar5195e452005-08-19 20:32:47 +00005068 if (spin->si_compflags != NULL)
5069 /* Need to store the list of compound flags with the word.
5070 * Concatenate them to the list of prefix IDs. */
5071 get_compflags(spin, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005072 }
5073
Bram Moolenaar51485f02005-06-04 21:55:20 +00005074 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005075 if (store_word(spin, dw, flags, spin->si_region,
5076 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005077 retval = FAIL;
5078
5079 if (afflist != NULL)
5080 {
5081 /* Find all matching suffixes and add the resulting words.
5082 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005083 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005084 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005085 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005086 retval = FAIL;
5087
5088 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005089 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005090 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005091 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005092 retval = FAIL;
5093 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005094 }
5095
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005096 if (duplicate > 0)
5097 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005098 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005099 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5100 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005101 hash_clear(&ht);
5102
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005103 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005104 return retval;
5105}
5106
5107/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005108 * Get the list of prefix IDs from the affix list "afflist".
5109 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005110 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5111 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005112 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005113 static int
5114get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005115 afffile_T *affile;
5116 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005117 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005118{
5119 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005120 int cnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005121 char_u key[2];
5122 hashitem_T *hi;
5123
5124 key[1] = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005125 for (p = afflist; *p != NUL; ++p)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005126 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005127 key[0] = *p;
5128 hi = hash_find(&affile->af_pref, key);
5129 if (!HASHITEM_EMPTY(hi))
5130 /* This is a prefix ID, use the new number. */
5131 store_afflist[cnt++] = HI2AH(hi)->ah_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005132 }
5133
Bram Moolenaar5195e452005-08-19 20:32:47 +00005134 store_afflist[cnt] = NUL;
5135 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005136}
5137
5138/*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005139 * Get the list of affix IDs from the affix list "afflist" that are used for
5140 * compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005141 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005142 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005143 static void
5144get_compflags(spin, afflist, store_afflist)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005145 spellinfo_T *spin;
5146 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005147 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005148{
5149 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005150 int cnt = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005151
Bram Moolenaar5195e452005-08-19 20:32:47 +00005152 for (p = afflist; *p != NUL; ++p)
5153 /* A flag is a compound flag if it appears in "si_compflags" and
5154 * it's not a special character. */
5155 if (vim_strchr(spin->si_compflags, *p) != NULL
5156 && vim_strchr((char_u *)"+*[]/", *p) == NULL)
5157 store_afflist[cnt++] = *p;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005158
Bram Moolenaar5195e452005-08-19 20:32:47 +00005159 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005160}
5161
5162/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005163 * Apply affixes to a word and store the resulting words.
5164 * "ht" is the hashtable with affentry_T that need to be applied, either
5165 * prefixes or suffixes.
5166 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5167 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005168 *
5169 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005170 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005171 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005172store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5173 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005174 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005175 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005176 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005177 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005178 hashtab_T *ht;
5179 hashtab_T *xht;
5180 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005181 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005182 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005183 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5184 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005185{
5186 int todo;
5187 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005188 affheader_T *ah;
5189 affentry_T *ae;
5190 regmatch_T regmatch;
5191 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005192 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005193 int i;
5194 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005195 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005196 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005197 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00005198 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005199 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005200
Bram Moolenaar51485f02005-06-04 21:55:20 +00005201 todo = ht->ht_used;
5202 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005203 {
5204 if (!HASHITEM_EMPTY(hi))
5205 {
5206 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005207 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005208
Bram Moolenaar51485f02005-06-04 21:55:20 +00005209 /* Check that the affix combines, if required, and that the word
5210 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005211 c = PTR2CHAR(ah->ah_key);
5212 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005213 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005214 /* Loop over all affix entries with this name. */
5215 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005216 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005217 /* Check the condition. It's not logical to match case
5218 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005219 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005220 * Another requirement from Myspell is that the chop
5221 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005222 * For prefixes, when "PFXPOSTPONE" was used, only do
5223 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005224 regmatch.regprog = ae->ae_prog;
5225 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005226 if ((xht != NULL || !affile->af_pfxpostpone
5227 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005228 && (ae->ae_chop == NULL
5229 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005230 && (ae->ae_prog == NULL
5231 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005233 /* Match. Remove the chop and add the affix. */
5234 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005235 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005236 /* prefix: chop/add at the start of the word */
5237 if (ae->ae_add == NULL)
5238 *newword = NUL;
5239 else
5240 STRCPY(newword, ae->ae_add);
5241 p = word;
5242 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005243 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005244 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005245#ifdef FEAT_MBYTE
5246 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005247 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005248 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005249 for ( ; i > 0; --i)
5250 mb_ptr_adv(p);
5251 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005252 else
5253#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005254 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005255 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005256 STRCAT(newword, p);
5257 }
5258 else
5259 {
5260 /* suffix: chop/add at the end of the word */
5261 STRCPY(newword, word);
5262 if (ae->ae_chop != NULL)
5263 {
5264 /* Remove chop string. */
5265 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005266 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005267 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005268 mb_ptr_back(newword, p);
5269 *p = NUL;
5270 }
5271 if (ae->ae_add != NULL)
5272 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005273 }
5274
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005275 /* Obey the "rare" flag of the affix. */
5276 if (ae->ae_rare)
5277 use_flags = flags | WF_RARE;
5278 else
5279 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005280
5281 /* Obey the "nocomp" flag of the affix: don't use the
5282 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005283 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005284 if (ae->ae_nocomp && pfxlist != NULL)
5285 {
5286 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5287 use_pfxlist = pfx_pfxlist;
5288 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005289
5290 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005291 if (spin->si_prefroot != NULL
5292 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005293 {
5294 /* ... add a flag to indicate an affix was used. */
5295 use_flags |= WF_HAS_AFF;
5296
5297 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005298 * affixes is not allowed. But do use the
5299 * compound flags after them. */
5300 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5301 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005302 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005303
Bram Moolenaar51485f02005-06-04 21:55:20 +00005304 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005305 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005306 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005307 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005308
Bram Moolenaar51485f02005-06-04 21:55:20 +00005309 /* When added a suffix and combining is allowed also
5310 * try adding prefixes additionally. */
5311 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005312 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005313 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005314 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005315 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005316 }
5317 }
5318 }
5319 }
5320 }
5321
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005322 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005323}
5324
5325/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005326 * Read a file with a list of words.
5327 */
5328 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005329spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005330 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005331 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005332{
5333 FILE *fd;
5334 long lnum = 0;
5335 char_u rline[MAXLINELEN];
5336 char_u *line;
5337 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005338 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005339 int l;
5340 int retval = OK;
5341 int did_word = FALSE;
5342 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005343 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005344 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005345
5346 /*
5347 * Open the file.
5348 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005349 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005350 if (fd == NULL)
5351 {
5352 EMSG2(_(e_notopen), fname);
5353 return FAIL;
5354 }
5355
Bram Moolenaarb765d632005-06-07 21:00:02 +00005356 if (spin->si_verbose || p_verbose > 2)
5357 {
5358 if (!spin->si_verbose)
5359 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005360 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005361 out_flush();
5362 if (!spin->si_verbose)
5363 verbose_leave();
5364 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005365
5366 /*
5367 * Read all the lines in the file one by one.
5368 */
5369 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5370 {
5371 line_breakcheck();
5372 ++lnum;
5373
5374 /* Skip comment lines. */
5375 if (*rline == '#')
5376 continue;
5377
5378 /* Remove CR, LF and white space from the end. */
5379 l = STRLEN(rline);
5380 while (l > 0 && rline[l - 1] <= ' ')
5381 --l;
5382 if (l == 0)
5383 continue; /* empty or blank line */
5384 rline[l] = NUL;
5385
5386 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
5387 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005388#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005389 if (spin->si_conv.vc_type != CONV_NONE)
5390 {
5391 pc = string_convert(&spin->si_conv, rline, NULL);
5392 if (pc == NULL)
5393 {
5394 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5395 fname, lnum, rline);
5396 continue;
5397 }
5398 line = pc;
5399 }
5400 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005401#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005402 {
5403 pc = NULL;
5404 line = rline;
5405 }
5406
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005407 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00005408 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005409 ++line;
5410 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005411 {
5412 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005413 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
5414 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005415 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005416 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
5417 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005418 else
5419 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005420#ifdef FEAT_MBYTE
5421 char_u *enc;
5422
Bram Moolenaar51485f02005-06-04 21:55:20 +00005423 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005424 line += 10;
5425 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005426 if (enc != NULL && !spin->si_ascii
5427 && convert_setup(&spin->si_conv, enc,
5428 p_enc) == FAIL)
5429 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00005430 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005431 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005432 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005433#else
5434 smsg((char_u *)_("Conversion in %s not supported"), fname);
5435#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005436 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005437 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005438 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005439
Bram Moolenaar3982c542005-06-08 21:56:31 +00005440 if (STRNCMP(line, "regions=", 8) == 0)
5441 {
5442 if (spin->si_region_count > 1)
5443 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
5444 fname, lnum, line);
5445 else
5446 {
5447 line += 8;
5448 if (STRLEN(line) > 16)
5449 smsg((char_u *)_("Too many regions in %s line %d: %s"),
5450 fname, lnum, line);
5451 else
5452 {
5453 spin->si_region_count = STRLEN(line) / 2;
5454 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005455
5456 /* Adjust the mask for a word valid in all regions. */
5457 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005458 }
5459 }
5460 continue;
5461 }
5462
Bram Moolenaar7887d882005-07-01 22:33:52 +00005463 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
5464 fname, lnum, line - 1);
5465 continue;
5466 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005467
Bram Moolenaar7887d882005-07-01 22:33:52 +00005468 flags = 0;
5469 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005470
Bram Moolenaar7887d882005-07-01 22:33:52 +00005471 /* Check for flags and region after a slash. */
5472 p = vim_strchr(line, '/');
5473 if (p != NULL)
5474 {
5475 *p++ = NUL;
5476 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005477 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005478 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005479 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005480 else if (*p == '!') /* Bad, bad, wicked word. */
5481 flags |= WF_BANNED;
5482 else if (*p == '?') /* Rare word. */
5483 flags |= WF_RARE;
5484 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005485 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005486 if ((flags & WF_REGION) == 0) /* first one */
5487 regionmask = 0;
5488 flags |= WF_REGION;
5489
5490 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00005491 if (l > spin->si_region_count)
5492 {
5493 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00005494 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005495 break;
5496 }
5497 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005498 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005499 else
5500 {
5501 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
5502 fname, lnum, p);
5503 break;
5504 }
5505 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005506 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005507 }
5508
5509 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5510 if (spin->si_ascii && has_non_ascii(line))
5511 {
5512 ++non_ascii;
5513 continue;
5514 }
5515
5516 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005517 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005518 {
5519 retval = FAIL;
5520 break;
5521 }
5522 did_word = TRUE;
5523 }
5524
5525 vim_free(pc);
5526 fclose(fd);
5527
Bram Moolenaarb765d632005-06-07 21:00:02 +00005528 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
5529 {
5530 if (p_verbose > 2)
5531 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00005532 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
5533 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005534 if (p_verbose > 2)
5535 verbose_leave();
5536 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005537 return retval;
5538}
5539
5540/*
5541 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005542 * This avoids calling free() for every little struct we use (and keeping
5543 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00005544 * The memory is cleared to all zeros.
5545 * Returns NULL when out of memory.
5546 */
5547 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005548getroom(spin, len, align)
5549 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005550 size_t len; /* length needed */
5551 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005552{
5553 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005554 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005555
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005556 if (align && bl != NULL)
5557 /* Round size up for alignment. On some systems structures need to be
5558 * aligned to the size of a pointer (e.g., SPARC). */
5559 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
5560 & ~(sizeof(char *) - 1);
5561
Bram Moolenaar51485f02005-06-04 21:55:20 +00005562 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
5563 {
5564 /* Allocate a block of memory. This is not freed until much later. */
5565 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
5566 if (bl == NULL)
5567 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005568 bl->sb_next = spin->si_blocks;
5569 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005570 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005571 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005572 }
5573
5574 p = bl->sb_data + bl->sb_used;
5575 bl->sb_used += len;
5576
5577 return p;
5578}
5579
5580/*
5581 * Make a copy of a string into memory allocated with getroom().
5582 */
5583 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005584getroom_save(spin, s)
5585 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005586 char_u *s;
5587{
5588 char_u *sc;
5589
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005590 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005591 if (sc != NULL)
5592 STRCPY(sc, s);
5593 return sc;
5594}
5595
5596
5597/*
5598 * Free the list of allocated sblock_T.
5599 */
5600 static void
5601free_blocks(bl)
5602 sblock_T *bl;
5603{
5604 sblock_T *next;
5605
5606 while (bl != NULL)
5607 {
5608 next = bl->sb_next;
5609 vim_free(bl);
5610 bl = next;
5611 }
5612}
5613
5614/*
5615 * Allocate the root of a word tree.
5616 */
5617 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005618wordtree_alloc(spin)
5619 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005620{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005621 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005622}
5623
5624/*
5625 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005626 * Always store it in the case-folded tree. For a keep-case word this is
5627 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
5628 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005629 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005630 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
5631 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005632 */
5633 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005634store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005635 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005636 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005637 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005638 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005639 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005640 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005641{
5642 int len = STRLEN(word);
5643 int ct = captype(word, word + len);
5644 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005645 int res = OK;
5646 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005648 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005649 for (p = pfxlist; res == OK; ++p)
5650 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005651 if (!need_affix || (p != NULL && *p != NUL))
5652 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005653 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005654 if (p == NULL || *p == NUL)
5655 break;
5656 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005657 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005658
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005659 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00005660 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005661 for (p = pfxlist; res == OK; ++p)
5662 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005663 if (!need_affix || (p != NULL && *p != NUL))
5664 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005665 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005666 if (p == NULL || *p == NUL)
5667 break;
5668 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005669 ++spin->si_keepwcount;
5670 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005671 return res;
5672}
5673
5674/*
5675 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005676 * When "flags" < 0 we are adding to the prefix tree where flags is used for
5677 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005678 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005679 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005680 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005681tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005682 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005683 char_u *word;
5684 wordnode_T *root;
5685 int flags;
5686 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005687 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005689 wordnode_T *node = root;
5690 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005691 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005692 wordnode_T **prev = NULL;
5693 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005694
Bram Moolenaar51485f02005-06-04 21:55:20 +00005695 /* Add each byte of the word to the tree, including the NUL at the end. */
5696 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005697 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005698 /* When there is more than one reference to this node we need to make
5699 * a copy, so that we can modify it. Copy the whole list of siblings
5700 * (we don't optimize for a partly shared list of siblings). */
5701 if (node != NULL && node->wn_refs > 1)
5702 {
5703 --node->wn_refs;
5704 copyprev = prev;
5705 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
5706 {
5707 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005708 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005709 if (np == NULL)
5710 return FAIL;
5711 np->wn_child = copyp->wn_child;
5712 if (np->wn_child != NULL)
5713 ++np->wn_child->wn_refs; /* child gets extra ref */
5714 np->wn_byte = copyp->wn_byte;
5715 if (np->wn_byte == NUL)
5716 {
5717 np->wn_flags = copyp->wn_flags;
5718 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005719 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005720 }
5721
5722 /* Link the new node in the list, there will be one ref. */
5723 np->wn_refs = 1;
5724 *copyprev = np;
5725 copyprev = &np->wn_sibling;
5726
5727 /* Let "node" point to the head of the copied list. */
5728 if (copyp == node)
5729 node = np;
5730 }
5731 }
5732
Bram Moolenaar51485f02005-06-04 21:55:20 +00005733 /* Look for the sibling that has the same character. They are sorted
5734 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005735 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005736 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005737 while (node != NULL
5738 && (node->wn_byte < word[i]
5739 || (node->wn_byte == NUL
5740 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005741 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005742 : node->wn_flags < (flags & WN_MASK)
5743 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005744 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005745 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005746 prev = &node->wn_sibling;
5747 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005748 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005749 if (node == NULL
5750 || node->wn_byte != word[i]
5751 || (word[i] == NUL
5752 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005753 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005754 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005755 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005756 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005757 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005758 if (np == NULL)
5759 return FAIL;
5760 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005761
5762 /* If "node" is NULL this is a new child or the end of the sibling
5763 * list: ref count is one. Otherwise use ref count of sibling and
5764 * make ref count of sibling one (matters when inserting in front
5765 * of the list of siblings). */
5766 if (node == NULL)
5767 np->wn_refs = 1;
5768 else
5769 {
5770 np->wn_refs = node->wn_refs;
5771 node->wn_refs = 1;
5772 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005773 *prev = np;
5774 np->wn_sibling = node;
5775 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005776 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005777
Bram Moolenaar51485f02005-06-04 21:55:20 +00005778 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005779 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005780 node->wn_flags = flags;
5781 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005782 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005783 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00005784 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005785 prev = &node->wn_child;
5786 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005787 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005788#ifdef SPELL_PRINTTREE
5789 smsg("Added \"%s\"", word);
5790 spell_print_tree(root->wn_sibling);
5791#endif
5792
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005793 /* count nr of words added since last message */
5794 ++spin->si_msg_count;
5795
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005796 if (spin->si_compress_cnt > 1)
5797 {
5798 if (--spin->si_compress_cnt == 1)
5799 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005800 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005801 }
5802
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005803 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005804 * When we have allocated lots of memory we need to compress the word tree
5805 * to free up some room. But compression is slow, and we might actually
5806 * need that room, thus only compress in the following situations:
5807 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00005808 * "compress_start" blocks.
5809 * 2. When compressed before and used "compress_inc" blocks before
5810 * adding "compress_added" words (si_compress_cnt > 1).
5811 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005812 * (si_compress_cnt == 1) and the number of free nodes drops below the
5813 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005814 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005815#ifndef SPELL_PRINTTREE
5816 if (spin->si_compress_cnt == 1
5817 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00005818 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005819#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005820 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005821 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00005822 * when the freed up room has been used and another "compress_inc"
5823 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005824 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005825 spin->si_blocks_cnt -= compress_inc;
5826 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005827
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005828 if (spin->si_verbose)
5829 {
5830 msg_start();
5831 msg_puts((char_u *)_(msg_compressing));
5832 msg_clr_eos();
5833 msg_didout = FALSE;
5834 msg_col = 0;
5835 out_flush();
5836 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005837
5838 /* Compress both trees. Either they both have many nodes, which makes
5839 * compression useful, or one of them is small, which means
5840 * compression goes fast. */
5841 wordtree_compress(spin, spin->si_foldroot);
5842 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005843 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005844
5845 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005846}
5847
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005848/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00005849 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
5850 * Sets "sps_flags".
5851 */
5852 int
5853spell_check_msm()
5854{
5855 char_u *p = p_msm;
5856 long start = 0;
5857 long inc = 0;
5858 long added = 0;
5859
5860 if (!VIM_ISDIGIT(*p))
5861 return FAIL;
5862 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
5863 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
5864 if (*p != ',')
5865 return FAIL;
5866 ++p;
5867 if (!VIM_ISDIGIT(*p))
5868 return FAIL;
5869 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
5870 if (*p != ',')
5871 return FAIL;
5872 ++p;
5873 if (!VIM_ISDIGIT(*p))
5874 return FAIL;
5875 added = getdigits(&p) * 1024;
5876 if (*p != NUL)
5877 return FAIL;
5878
5879 if (start == 0 || inc == 0 || added == 0 || inc > start)
5880 return FAIL;
5881
5882 compress_start = start;
5883 compress_inc = inc;
5884 compress_added = added;
5885 return OK;
5886}
5887
5888
5889/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005890 * Get a wordnode_T, either from the list of previously freed nodes or
5891 * allocate a new one.
5892 */
5893 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005894get_wordnode(spin)
5895 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005896{
5897 wordnode_T *n;
5898
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005899 if (spin->si_first_free == NULL)
5900 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005901 else
5902 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005903 n = spin->si_first_free;
5904 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005905 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005906 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005907 }
5908#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005909 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005910#endif
5911 return n;
5912}
5913
5914/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005915 * Decrement the reference count on a node (which is the head of a list of
5916 * siblings). If the reference count becomes zero free the node and its
5917 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005918 */
5919 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005920deref_wordnode(spin, node)
5921 spellinfo_T *spin;
5922 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005923{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005924 wordnode_T *np;
5925
5926 if (--node->wn_refs == 0)
5927 for (np = node; np != NULL; np = np->wn_sibling)
5928 {
5929 if (np->wn_child != NULL)
5930 deref_wordnode(spin, np->wn_child);
5931 free_wordnode(spin, np);
5932 }
5933}
5934
5935/*
5936 * Free a wordnode_T for re-use later.
5937 * Only the "wn_child" field becomes invalid.
5938 */
5939 static void
5940free_wordnode(spin, n)
5941 spellinfo_T *spin;
5942 wordnode_T *n;
5943{
5944 n->wn_child = spin->si_first_free;
5945 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005946 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005947}
5948
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005949/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005950 * Compress a tree: find tails that are identical and can be shared.
5951 */
5952 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005953wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005954 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005955 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005956{
5957 hashtab_T ht;
5958 int n;
5959 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005960 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005961
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005962 /* Skip the root itself, it's not actually used. The first sibling is the
5963 * start of the tree. */
5964 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005965 {
5966 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005967 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005968
5969#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00005970 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005971#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00005972 {
5973 if (!spin->si_verbose)
5974 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005975 if (tot > 1000000)
5976 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005977 else if (tot == 0)
5978 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005979 else
5980 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005981 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005982 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005983 if (p_verbose > 2)
5984 verbose_leave();
5985 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005986#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005987 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005988#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005989 hash_clear(&ht);
5990 }
5991}
5992
5993/*
5994 * Compress a node, its siblings and its children, depth first.
5995 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005996 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005997 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005998node_compress(spin, node, ht, tot)
5999 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006000 wordnode_T *node;
6001 hashtab_T *ht;
6002 int *tot; /* total count of nodes before compressing,
6003 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006004{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006005 wordnode_T *np;
6006 wordnode_T *tp;
6007 wordnode_T *child;
6008 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006009 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006010 int len = 0;
6011 unsigned nr, n;
6012 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006013
Bram Moolenaar51485f02005-06-04 21:55:20 +00006014 /*
6015 * Go through the list of siblings. Compress each child and then try
6016 * finding an identical child to replace it.
6017 * Note that with "child" we mean not just the node that is pointed to,
6018 * but the whole list of siblings, of which the node is the first.
6019 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006020 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006021 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006022 ++len;
6023 if ((child = np->wn_child) != NULL)
6024 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006025 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006026 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006027
6028 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006029 hash = hash_hash(child->wn_u1.hashkey);
6030 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006031 tp = NULL;
6032 if (!HASHITEM_EMPTY(hi))
6033 {
6034 /* There are children with an identical hash value. Now check
6035 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006036 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006037 if (node_equal(child, tp))
6038 {
6039 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006040 * current one. This means the current child and all
6041 * its siblings is unlinked from the tree. */
6042 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006043 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006044 np->wn_child = tp;
6045 ++compressed;
6046 break;
6047 }
6048 if (tp == NULL)
6049 {
6050 /* No other child with this hash value equals the child of
6051 * the node, add it to the linked list after the first
6052 * item. */
6053 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006054 child->wn_u2.next = tp->wn_u2.next;
6055 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006056 }
6057 }
6058 else
6059 /* No other child has this hash value, add it to the
6060 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006061 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006062 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006063 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006064 *tot += len;
6065
6066 /*
6067 * Make a hash key for the node and its siblings, so that we can quickly
6068 * find a lookalike node. This must be done after compressing the sibling
6069 * list, otherwise the hash key would become invalid by the compression.
6070 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006071 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006072 nr = 0;
6073 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006074 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006075 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006076 /* end node: use wn_flags, wn_region and wn_affixID */
6077 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006078 else
6079 /* byte node: use the byte value and the child pointer */
6080 n = np->wn_byte + ((long_u)np->wn_child << 8);
6081 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006082 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006083
6084 /* Avoid NUL bytes, it terminates the hash key. */
6085 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006086 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006087 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006088 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006089 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006090 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006091 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006092 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6093 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006094
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006095 /* Check for CTRL-C pressed now and then. */
6096 fast_breakcheck();
6097
Bram Moolenaar51485f02005-06-04 21:55:20 +00006098 return compressed;
6099}
6100
6101/*
6102 * Return TRUE when two nodes have identical siblings and children.
6103 */
6104 static int
6105node_equal(n1, n2)
6106 wordnode_T *n1;
6107 wordnode_T *n2;
6108{
6109 wordnode_T *p1;
6110 wordnode_T *p2;
6111
6112 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6113 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6114 if (p1->wn_byte != p2->wn_byte
6115 || (p1->wn_byte == NUL
6116 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006117 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006118 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006119 : (p1->wn_child != p2->wn_child)))
6120 break;
6121
6122 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006123}
6124
6125/*
6126 * Write a number to file "fd", MSB first, in "len" bytes.
6127 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006128 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006129put_bytes(fd, nr, len)
6130 FILE *fd;
6131 long_u nr;
6132 int len;
6133{
6134 int i;
6135
6136 for (i = len - 1; i >= 0; --i)
6137 putc((int)(nr >> (i * 8)), fd);
6138}
6139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006140static int
6141#ifdef __BORLANDC__
6142_RTLENTRYF
6143#endif
6144rep_compare __ARGS((const void *s1, const void *s2));
6145
6146/*
6147 * Function given to qsort() to sort the REP items on "from" string.
6148 */
6149 static int
6150#ifdef __BORLANDC__
6151_RTLENTRYF
6152#endif
6153rep_compare(s1, s2)
6154 const void *s1;
6155 const void *s2;
6156{
6157 fromto_T *p1 = (fromto_T *)s1;
6158 fromto_T *p2 = (fromto_T *)s2;
6159
6160 return STRCMP(p1->ft_from, p2->ft_from);
6161}
6162
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006163/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006164 * Write the Vim .spl file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006165 */
6166 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006167write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006168 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006169 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006170{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006171 FILE *fd;
6172 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006173 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006174 wordnode_T *tree;
6175 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006176 int i;
6177 int l;
6178 garray_T *gap;
6179 fromto_T *ftp;
6180 char_u *p;
6181 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006182
Bram Moolenaarb765d632005-06-07 21:00:02 +00006183 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006184 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006185 {
6186 EMSG2(_(e_notopen), fname);
6187 return;
6188 }
6189
Bram Moolenaar5195e452005-08-19 20:32:47 +00006190 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006191 /* <fileID> */
6192 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
6193 EMSG(_(e_write));
Bram Moolenaar5195e452005-08-19 20:32:47 +00006194 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006195
Bram Moolenaar5195e452005-08-19 20:32:47 +00006196 /*
6197 * <SECTIONS>: <section> ... <sectionend>
6198 */
6199
6200 /* SN_REGION: <regionname> ...
6201 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006202 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006203 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006204 putc(SN_REGION, fd); /* <sectionID> */
6205 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6206 l = spin->si_region_count * 2;
6207 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6208 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6209 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006210 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006211 }
6212 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006213 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006214
Bram Moolenaar5195e452005-08-19 20:32:47 +00006215 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6216 *
6217 * The table with character flags and the table for case folding.
6218 * This makes sure the same characters are recognized as word characters
6219 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006220 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006221 * 'encoding'.
6222 * Also skip this for an .add.spl file, the main spell file must contain
6223 * the table (avoids that it conflicts). File is shorter too.
6224 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006225 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006226 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006227 char_u folchars[128 * 8];
6228 int flags;
6229
Bram Moolenaard12a1322005-08-21 22:08:24 +00006230 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006231 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6232
6233 /* Form the <folchars> string first, we need to know its length. */
6234 l = 0;
6235 for (i = 128; i < 256; ++i)
6236 {
6237#ifdef FEAT_MBYTE
6238 if (has_mbyte)
6239 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6240 else
6241#endif
6242 folchars[l++] = spelltab.st_fold[i];
6243 }
6244 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6245
6246 fputc(128, fd); /* <charflagslen> */
6247 for (i = 128; i < 256; ++i)
6248 {
6249 flags = 0;
6250 if (spelltab.st_isw[i])
6251 flags |= CF_WORD;
6252 if (spelltab.st_isu[i])
6253 flags |= CF_UPPER;
6254 fputc(flags, fd); /* <charflags> */
6255 }
6256
6257 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6258 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006259 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006260
Bram Moolenaar5195e452005-08-19 20:32:47 +00006261 /* SN_MIDWORD: <midword> */
6262 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006263 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006264 putc(SN_MIDWORD, fd); /* <sectionID> */
6265 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6266
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006267 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006268 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006269 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6270 }
6271
Bram Moolenaar5195e452005-08-19 20:32:47 +00006272 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6273 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006274 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006275 putc(SN_PREFCOND, fd); /* <sectionID> */
6276 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6277
6278 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6279 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6280
6281 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006282 }
6283
Bram Moolenaar5195e452005-08-19 20:32:47 +00006284 /* SN_REP: <repcount> <rep> ...
6285 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006287 /* Sort the REP items. */
6288 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6289 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006290
Bram Moolenaar5195e452005-08-19 20:32:47 +00006291 /* round 1: SN_REP section
6292 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006293 for (round = 1; round <= 2; ++round)
6294 {
6295 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006297 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006298 putc(SN_REP, fd); /* <sectionID> */
6299 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006300 else
6301 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006302 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6303 /* using SN_SOFO section instead of SN_SAL */
6304 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006305 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006306 putc(SN_SAL, fd); /* <sectionID> */
6307 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006308
Bram Moolenaar5195e452005-08-19 20:32:47 +00006309 /* This is for making suggestions, section is not required. */
6310 putc(0, fd); /* <sectionflags> */
6311
6312 /* Compute the length of what follows. */
6313 l = 2; /* count <repcount> or <salcount> */
6314 for (i = 0; i < gap->ga_len; ++i)
6315 {
6316 ftp = &((fromto_T *)gap->ga_data)[i];
6317 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6318 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6319 }
6320 if (round == 2)
6321 ++l; /* count <salflags> */
6322 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6323
6324 if (round == 2)
6325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006326 i = 0;
6327 if (spin->si_followup)
6328 i |= SAL_F0LLOWUP;
6329 if (spin->si_collapse)
6330 i |= SAL_COLLAPSE;
6331 if (spin->si_rem_accents)
6332 i |= SAL_REM_ACCENTS;
6333 putc(i, fd); /* <salflags> */
6334 }
6335
6336 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6337 for (i = 0; i < gap->ga_len; ++i)
6338 {
6339 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6340 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6341 ftp = &((fromto_T *)gap->ga_data)[i];
6342 for (rr = 1; rr <= 2; ++rr)
6343 {
6344 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6345 l = STRLEN(p);
6346 putc(l, fd);
6347 fwrite(p, l, (size_t)1, fd);
6348 }
6349 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006351 }
6352
Bram Moolenaar5195e452005-08-19 20:32:47 +00006353 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6354 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006355 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6356 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006357 putc(SN_SOFO, fd); /* <sectionID> */
6358 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006359
6360 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006361 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6362 /* <sectionlen> */
6363
6364 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6365 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006366
6367 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006368 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
6369 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006370 }
6371
Bram Moolenaar5195e452005-08-19 20:32:47 +00006372 /* SN_MAP: <mapstr>
6373 * This is for making suggestions, section is not required. */
6374 if (spin->si_map.ga_len > 0)
6375 {
6376 putc(SN_MAP, fd); /* <sectionID> */
6377 putc(0, fd); /* <sectionflags> */
6378 l = spin->si_map.ga_len;
6379 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6380 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
6381 /* <mapstr> */
6382 }
6383
6384 /* SN_COMPOUND: compound info.
6385 * We don't mark it required, when not supported all compound words will
6386 * be bad words. */
6387 if (spin->si_compflags != NULL)
6388 {
6389 putc(SN_COMPOUND, fd); /* <sectionID> */
6390 putc(0, fd); /* <sectionflags> */
6391
6392 l = STRLEN(spin->si_compflags);
6393 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
6394 putc(spin->si_compmax, fd); /* <compmax> */
6395 putc(spin->si_compminlen, fd); /* <compminlen> */
6396 putc(spin->si_compsylmax, fd); /* <compsylmax> */
6397 /* <compflags> */
6398 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
6399 }
6400
6401 /* SN_SYLLABLE: syllable info.
6402 * We don't mark it required, when not supported syllables will not be
6403 * counted. */
6404 if (spin->si_syllable != NULL)
6405 {
6406 putc(SN_SYLLABLE, fd); /* <sectionID> */
6407 putc(0, fd); /* <sectionflags> */
6408
6409 l = STRLEN(spin->si_syllable);
6410 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6411 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
6412 }
6413
6414 /* end of <SECTIONS> */
6415 putc(SN_END, fd); /* <sectionend> */
6416
Bram Moolenaar50cde822005-06-05 21:54:54 +00006417
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006418 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006419 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006420 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006421 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006422 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006423 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006424 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006425 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006426 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006427 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006428 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006429 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006430
Bram Moolenaar0c405862005-06-22 22:26:26 +00006431 /* Clear the index and wnode fields in the tree. */
6432 clear_node(tree);
6433
Bram Moolenaar51485f02005-06-04 21:55:20 +00006434 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00006435 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00006436 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006437 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006438
Bram Moolenaar51485f02005-06-04 21:55:20 +00006439 /* number of nodes in 4 bytes */
6440 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00006441 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006442
Bram Moolenaar51485f02005-06-04 21:55:20 +00006443 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006444 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006445 }
6446
Bram Moolenaar51485f02005-06-04 21:55:20 +00006447 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006448}
6449
6450/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006451 * Clear the index and wnode fields of "node", it siblings and its
6452 * children. This is needed because they are a union with other items to save
6453 * space.
6454 */
6455 static void
6456clear_node(node)
6457 wordnode_T *node;
6458{
6459 wordnode_T *np;
6460
6461 if (node != NULL)
6462 for (np = node; np != NULL; np = np->wn_sibling)
6463 {
6464 np->wn_u1.index = 0;
6465 np->wn_u2.wnode = NULL;
6466
6467 if (np->wn_byte != NUL)
6468 clear_node(np->wn_child);
6469 }
6470}
6471
6472
6473/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006474 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006475 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00006476 * This first writes the list of possible bytes (siblings). Then for each
6477 * byte recursively write the children.
6478 *
6479 * NOTE: The code here must match the code in read_tree(), since assumptions
6480 * are made about the indexes (so that we don't have to write them in the
6481 * file).
6482 *
6483 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006484 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006485 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00006486put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006487 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006488 wordnode_T *node;
6489 int index;
6490 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006491 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006492{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006493 int newindex = index;
6494 int siblingcount = 0;
6495 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006496 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006497
Bram Moolenaar51485f02005-06-04 21:55:20 +00006498 /* If "node" is zero the tree is empty. */
6499 if (node == NULL)
6500 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006501
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006503 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006504
6505 /* Count the number of siblings. */
6506 for (np = node; np != NULL; np = np->wn_sibling)
6507 ++siblingcount;
6508
6509 /* Write the sibling count. */
6510 if (fd != NULL)
6511 putc(siblingcount, fd); /* <siblingcount> */
6512
6513 /* Write each sibling byte and optionally extra info. */
6514 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006515 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006517 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006518 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006519 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006520 /* For a NUL byte (end of word) write the flags etc. */
6521 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006522 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006523 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006524 * associated condition nr (stored in wn_region). The
6525 * byte value is misused to store the "rare" and "not
6526 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006527 if (np->wn_flags == (short_u)PFX_FLAGS)
6528 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006529 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00006530 {
6531 putc(BY_FLAGS, fd); /* <byte> */
6532 putc(np->wn_flags, fd); /* <pflags> */
6533 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006534 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006535 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536 }
6537 else
6538 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006539 /* For word trees we write the flag/region items. */
6540 flags = np->wn_flags;
6541 if (regionmask != 0 && np->wn_region != regionmask)
6542 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006543 if (np->wn_affixID != 0)
6544 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006545 if (flags == 0)
6546 {
6547 /* word without flags or region */
6548 putc(BY_NOFLAGS, fd); /* <byte> */
6549 }
6550 else
6551 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006552 if (np->wn_flags >= 0x100)
6553 {
6554 putc(BY_FLAGS2, fd); /* <byte> */
6555 putc(flags, fd); /* <flags> */
6556 putc((unsigned)flags >> 8, fd); /* <flags2> */
6557 }
6558 else
6559 {
6560 putc(BY_FLAGS, fd); /* <byte> */
6561 putc(flags, fd); /* <flags> */
6562 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006563 if (flags & WF_REGION)
6564 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006565 if (flags & WF_AFX)
6566 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006567 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006568 }
6569 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006570 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006571 else
6572 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006573 if (np->wn_child->wn_u1.index != 0
6574 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006575 {
6576 /* The child is written elsewhere, write the reference. */
6577 if (fd != NULL)
6578 {
6579 putc(BY_INDEX, fd); /* <byte> */
6580 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006581 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006582 }
6583 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00006584 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006585 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006586 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006587
Bram Moolenaar51485f02005-06-04 21:55:20 +00006588 if (fd != NULL)
6589 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
6590 {
6591 EMSG(_(e_write));
6592 return 0;
6593 }
6594 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006595 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006596
6597 /* Space used in the array when reading: one for each sibling and one for
6598 * the count. */
6599 newindex += siblingcount + 1;
6600
6601 /* Recursively dump the children of each sibling. */
6602 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006603 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
6604 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006605 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006606
6607 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006608}
6609
6610
6611/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00006612 * ":mkspell [-ascii] outfile infile ..."
6613 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006614 */
6615 void
6616ex_mkspell(eap)
6617 exarg_T *eap;
6618{
6619 int fcount;
6620 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006621 char_u *arg = eap->arg;
6622 int ascii = FALSE;
6623
6624 if (STRNCMP(arg, "-ascii", 6) == 0)
6625 {
6626 ascii = TRUE;
6627 arg = skipwhite(arg + 6);
6628 }
6629
6630 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
6631 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
6632 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006633 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006634 FreeWild(fcount, fnames);
6635 }
6636}
6637
6638/*
6639 * Create a Vim spell file from one or more word lists.
6640 * "fnames[0]" is the output file name.
6641 * "fnames[fcount - 1]" is the last input file name.
6642 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
6643 * and ".spl" is appended to make the output file name.
6644 */
6645 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006646mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006647 int fcount;
6648 char_u **fnames;
6649 int ascii; /* -ascii argument given */
6650 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006651 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006652{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006653 char_u fname[MAXPATHL];
6654 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00006655 char_u **innames;
6656 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006657 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006658 int i;
6659 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006660 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006661 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006662 spellinfo_T spin;
6663
6664 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006665 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006666 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006667 spin.si_followup = TRUE;
6668 spin.si_rem_accents = TRUE;
6669 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
6670 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
6671 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006672 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006673
Bram Moolenaarb765d632005-06-07 21:00:02 +00006674 /* default: fnames[0] is output file, following are input files */
6675 innames = &fnames[1];
6676 incount = fcount - 1;
6677
6678 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00006679 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006680 len = STRLEN(fnames[0]);
6681 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
6682 {
6683 /* For ":mkspell path/en.latin1.add" output file is
6684 * "path/en.latin1.add.spl". */
6685 innames = &fnames[0];
6686 incount = 1;
6687 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
6688 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006689 else if (fcount == 1)
6690 {
6691 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
6692 innames = &fnames[0];
6693 incount = 1;
6694 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6695 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6696 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006697 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
6698 {
6699 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006700 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006701 }
6702 else
6703 /* Name should be language, make the file name from it. */
6704 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6705 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6706
6707 /* Check for .ascii.spl. */
6708 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
6709 spin.si_ascii = TRUE;
6710
6711 /* Check for .add.spl. */
6712 if (strstr((char *)gettail(wfname), ".add.") != NULL)
6713 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006714 }
6715
Bram Moolenaarb765d632005-06-07 21:00:02 +00006716 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006717 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006718 else if (vim_strchr(gettail(wfname), '_') != NULL)
6719 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006720 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006721 EMSG(_("E754: Only up to 8 regions supported"));
6722 else
6723 {
6724 /* Check for overwriting before doing things that may take a lot of
6725 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006726 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006727 {
6728 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006729 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006730 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006731 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006732 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006733 EMSG2(_(e_isadir2), wfname);
6734 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006735 }
6736
6737 /*
6738 * Init the aff and dic pointers.
6739 * Get the region names if there are more than 2 arguments.
6740 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006741 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006742 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006743 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006744
Bram Moolenaar3982c542005-06-08 21:56:31 +00006745 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006746 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006747 len = STRLEN(innames[i]);
6748 if (STRLEN(gettail(innames[i])) < 5
6749 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006750 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006751 EMSG2(_("E755: Invalid region in %s"), innames[i]);
6752 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006753 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006754 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6755 spin.si_region_name[i * 2 + 1] =
6756 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006757 }
6758 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006759 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006760
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006761 spin.si_foldroot = wordtree_alloc(&spin);
6762 spin.si_keeproot = wordtree_alloc(&spin);
6763 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006764 if (spin.si_foldroot == NULL
6765 || spin.si_keeproot == NULL
6766 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006767 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006768 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006769 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006770 }
6771
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006772 /* When not producing a .add.spl file clear the character table when
6773 * we encounter one in the .aff file. This means we dump the current
6774 * one in the .spl file if the .aff file doesn't define one. That's
6775 * better than guessing the contents, the table will match a
6776 * previously loaded spell file. */
6777 if (!spin.si_add)
6778 spin.si_clear_chartab = TRUE;
6779
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006780 /*
6781 * Read all the .aff and .dic files.
6782 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006783 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006784 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006785 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006786 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006787 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006788 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789
Bram Moolenaarb765d632005-06-07 21:00:02 +00006790 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 if (mch_stat((char *)fname, &st) >= 0)
6792 {
6793 /* Read the .aff file. Will init "spin->si_conv" based on the
6794 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006795 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006796 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006797 error = TRUE;
6798 else
6799 {
6800 /* Read the .dic file and store the words in the trees. */
6801 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00006802 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006803 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006804 error = TRUE;
6805 }
6806 }
6807 else
6808 {
6809 /* No .aff file, try reading the file as a word list. Store
6810 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006811 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006812 error = TRUE;
6813 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006814
Bram Moolenaarb765d632005-06-07 21:00:02 +00006815#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006816 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006817 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006818#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006819 }
6820
Bram Moolenaar51485f02005-06-04 21:55:20 +00006821 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006823 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006824 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006825 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006826 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006827 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006828 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006829 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006830 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006831 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006832 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006833 verbose_leave();
6834 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006835 wordtree_compress(&spin, spin.si_foldroot);
6836 wordtree_compress(&spin, spin.si_keeproot);
6837 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006838 }
6839
Bram Moolenaar51485f02005-06-04 21:55:20 +00006840 if (!error)
6841 {
6842 /*
6843 * Write the info in the spell file.
6844 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006845 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006846 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006847 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006848 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006849 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006850 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006851 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006852 verbose_leave();
6853 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00006854
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006855 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006856
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006857 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006858 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006859 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006860 verbose_enter();
6861 MSG(_("Done!"));
6862 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00006863 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006864 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006865 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006866 verbose_leave();
6867 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006868
Bram Moolenaarb765d632005-06-07 21:00:02 +00006869 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006870 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006871 }
6872
6873 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006874 ga_clear(&spin.si_rep);
6875 ga_clear(&spin.si_sal);
6876 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006877 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006878 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006879 vim_free(spin.si_sofofr);
6880 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006881
6882 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006883 for (i = 0; i < incount; ++i)
6884 if (afile[i] != NULL)
6885 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006886
6887 /* Free all the bits and pieces at once. */
6888 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006889 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006890}
6891
Bram Moolenaarb765d632005-06-07 21:00:02 +00006892
6893/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006894 * ":[count]spellgood {word}"
6895 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00006896 */
6897 void
6898ex_spell(eap)
6899 exarg_T *eap;
6900{
Bram Moolenaar7887d882005-07-01 22:33:52 +00006901 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006902 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006903}
6904
6905/*
6906 * Add "word[len]" to 'spellfile' as a good or bad word.
6907 */
6908 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006909spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006910 char_u *word;
6911 int len;
6912 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006913 int index; /* "zG" and "zW": zero, otherwise index in
6914 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006915{
6916 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006917 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006918 int new_spf = FALSE;
6919 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006920 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006921 char_u fnamebuf[MAXPATHL];
6922 char_u line[MAXWLEN * 2];
6923 long fpos, fpos_next = 0;
6924 int i;
6925 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006926
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006927 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006928 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006929 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006930 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006931 int_wordlist = vim_tempname('s');
6932 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006933 return;
6934 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006935 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006936 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006937 else
6938 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006939 /* If 'spellfile' isn't set figure out a good default value. */
6940 if (*curbuf->b_p_spf == NUL)
6941 {
6942 init_spellfile();
6943 new_spf = TRUE;
6944 }
6945
6946 if (*curbuf->b_p_spf == NUL)
6947 {
6948 EMSG(_("E764: 'spellfile' is not set"));
6949 return;
6950 }
6951
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006952 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
6953 {
6954 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6955 if (i == index)
6956 break;
6957 if (*spf == NUL)
6958 {
6959 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
6960 return;
6961 }
6962 }
6963
Bram Moolenaarb765d632005-06-07 21:00:02 +00006964 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006965 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006966 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
6967 buf = NULL;
6968 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00006969 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006970 EMSG(_(e_bufloaded));
6971 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006972 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006973
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006974 fname = fnamebuf;
6975 }
6976
6977 if (bad)
6978 {
6979 /* When the word also appears as good word we need to remove that one,
6980 * since its flags sort before the one with WF_BANNED. */
6981 fd = mch_fopen((char *)fname, "r");
6982 if (fd != NULL)
6983 {
6984 while (!vim_fgets(line, MAXWLEN * 2, fd))
6985 {
6986 fpos = fpos_next;
6987 fpos_next = ftell(fd);
6988 if (STRNCMP(word, line, len) == 0
6989 && (line[len] == '/' || line[len] < ' '))
6990 {
6991 /* Found duplicate word. Remove it by writing a '#' at
6992 * the start of the line. Mixing reading and writing
6993 * doesn't work for all systems, close the file first. */
6994 fclose(fd);
6995 fd = mch_fopen((char *)fname, "r+");
6996 if (fd == NULL)
6997 break;
6998 if (fseek(fd, fpos, SEEK_SET) == 0)
6999 fputc('#', fd);
7000 fseek(fd, fpos_next, SEEK_SET);
7001 }
7002 }
7003 fclose(fd);
7004 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007005 }
7006
7007 fd = mch_fopen((char *)fname, "a");
7008 if (fd == NULL && new_spf)
7009 {
7010 /* We just initialized the 'spellfile' option and can't open the file.
7011 * We may need to create the "spell" directory first. We already
7012 * checked the runtime directory is writable in init_spellfile(). */
7013 STRCPY(NameBuff, fname);
7014 *gettail_sep(NameBuff) = NUL;
7015 if (mch_stat((char *)NameBuff, &st) < 0)
7016 {
7017 /* The directory doesn't exist. Try creating it and opening the
7018 * file again. */
7019 vim_mkdir(NameBuff, 0755);
7020 fd = mch_fopen((char *)fname, "a");
7021 }
7022 }
7023
7024 if (fd == NULL)
7025 EMSG2(_(e_notopen), fname);
7026 else
7027 {
7028 if (bad)
7029 fprintf(fd, "%.*s/!\n", len, word);
7030 else
7031 fprintf(fd, "%.*s\n", len, word);
7032 fclose(fd);
7033
7034 /* Update the .add.spl file. */
7035 mkspell(1, &fname, FALSE, TRUE, TRUE);
7036
7037 /* If the .add file is edited somewhere, reload it. */
7038 if (buf != NULL)
7039 buf_reload(buf);
7040
7041 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007042 }
7043}
7044
7045/*
7046 * Initialize 'spellfile' for the current buffer.
7047 */
7048 static void
7049init_spellfile()
7050{
7051 char_u buf[MAXPATHL];
7052 int l;
7053 slang_T *sl;
7054 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007055 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007056
7057 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7058 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007059 /* Find the end of the language name. Exclude the region. */
7060 for (lend = curbuf->b_p_spl; *lend != NUL
7061 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7062 ;
7063
7064 /* Loop over all entries in 'runtimepath'. Use the first one where we
7065 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007066 rtp = p_rtp;
7067 while (*rtp != NUL)
7068 {
7069 /* Copy the path from 'runtimepath' to buf[]. */
7070 copy_option_part(&rtp, buf, MAXPATHL, ",");
7071 if (filewritable(buf) == 2)
7072 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007073 /* Use the first language name from 'spelllang' and the
7074 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007075 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
7076 l = STRLEN(buf);
7077 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007078 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007079 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00007080 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
7081 ? (char_u *)"ascii" : spell_enc());
7082 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7083 break;
7084 }
7085 }
7086 }
7087}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007088
Bram Moolenaar51485f02005-06-04 21:55:20 +00007089
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007090/*
7091 * Init the chartab used for spelling for ASCII.
7092 * EBCDIC is not supported!
7093 */
7094 static void
7095clear_spell_chartab(sp)
7096 spelltab_T *sp;
7097{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007098 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007099
7100 /* Init everything to FALSE. */
7101 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7102 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7103 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007104 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007105 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007106 sp->st_upper[i] = i;
7107 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007108
7109 /* We include digits. A word shouldn't start with a digit, but handling
7110 * that is done separately. */
7111 for (i = '0'; i <= '9'; ++i)
7112 sp->st_isw[i] = TRUE;
7113 for (i = 'A'; i <= 'Z'; ++i)
7114 {
7115 sp->st_isw[i] = TRUE;
7116 sp->st_isu[i] = TRUE;
7117 sp->st_fold[i] = i + 0x20;
7118 }
7119 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007120 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007121 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007122 sp->st_upper[i] = i - 0x20;
7123 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007124}
7125
7126/*
7127 * Init the chartab used for spelling. Only depends on 'encoding'.
7128 * Called once while starting up and when 'encoding' changes.
7129 * The default is to use isalpha(), but the spell file should define the word
7130 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007131 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007132 */
7133 void
7134init_spell_chartab()
7135{
7136 int i;
7137
7138 did_set_spelltab = FALSE;
7139 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007140#ifdef FEAT_MBYTE
7141 if (enc_dbcs)
7142 {
7143 /* DBCS: assume double-wide characters are word characters. */
7144 for (i = 128; i <= 255; ++i)
7145 if (MB_BYTE2LEN(i) == 2)
7146 spelltab.st_isw[i] = TRUE;
7147 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007148 else if (enc_utf8)
7149 {
7150 for (i = 128; i < 256; ++i)
7151 {
7152 spelltab.st_isu[i] = utf_isupper(i);
7153 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7154 spelltab.st_fold[i] = utf_fold(i);
7155 spelltab.st_upper[i] = utf_toupper(i);
7156 }
7157 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007158 else
7159#endif
7160 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007161 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007162 for (i = 128; i < 256; ++i)
7163 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007164 if (MB_ISUPPER(i))
7165 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007166 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007167 spelltab.st_isu[i] = TRUE;
7168 spelltab.st_fold[i] = MB_TOLOWER(i);
7169 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007170 else if (MB_ISLOWER(i))
7171 {
7172 spelltab.st_isw[i] = TRUE;
7173 spelltab.st_upper[i] = MB_TOUPPER(i);
7174 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007175 }
7176 }
7177}
7178
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007179static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
7180static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
7181
7182/*
7183 * Set the spell character tables from strings in the affix file.
7184 */
7185 static int
7186set_spell_chartab(fol, low, upp)
7187 char_u *fol;
7188 char_u *low;
7189 char_u *upp;
7190{
7191 /* We build the new tables here first, so that we can compare with the
7192 * previous one. */
7193 spelltab_T new_st;
7194 char_u *pf = fol, *pl = low, *pu = upp;
7195 int f, l, u;
7196
7197 clear_spell_chartab(&new_st);
7198
7199 while (*pf != NUL)
7200 {
7201 if (*pl == NUL || *pu == NUL)
7202 {
7203 EMSG(_(e_affform));
7204 return FAIL;
7205 }
7206#ifdef FEAT_MBYTE
7207 f = mb_ptr2char_adv(&pf);
7208 l = mb_ptr2char_adv(&pl);
7209 u = mb_ptr2char_adv(&pu);
7210#else
7211 f = *pf++;
7212 l = *pl++;
7213 u = *pu++;
7214#endif
7215 /* Every character that appears is a word character. */
7216 if (f < 256)
7217 new_st.st_isw[f] = TRUE;
7218 if (l < 256)
7219 new_st.st_isw[l] = TRUE;
7220 if (u < 256)
7221 new_st.st_isw[u] = TRUE;
7222
7223 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7224 * case-folding */
7225 if (l < 256 && l != f)
7226 {
7227 if (f >= 256)
7228 {
7229 EMSG(_(e_affrange));
7230 return FAIL;
7231 }
7232 new_st.st_fold[l] = f;
7233 }
7234
7235 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007236 * case-folding, it's upper case and the "UPP" is the upper case of
7237 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007238 if (u < 256 && u != f)
7239 {
7240 if (f >= 256)
7241 {
7242 EMSG(_(e_affrange));
7243 return FAIL;
7244 }
7245 new_st.st_fold[u] = f;
7246 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007247 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007248 }
7249 }
7250
7251 if (*pl != NUL || *pu != NUL)
7252 {
7253 EMSG(_(e_affform));
7254 return FAIL;
7255 }
7256
7257 return set_spell_finish(&new_st);
7258}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007259
7260/*
7261 * Set the spell character tables from strings in the .spl file.
7262 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007263 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007264set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007265 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007266 int cnt; /* length of "flags" */
7267 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007268{
7269 /* We build the new tables here first, so that we can compare with the
7270 * previous one. */
7271 spelltab_T new_st;
7272 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007273 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007274 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007275
7276 clear_spell_chartab(&new_st);
7277
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007278 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007279 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007280 if (i < cnt)
7281 {
7282 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7283 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7284 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007285
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007286 if (*p != NUL)
7287 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007288#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007289 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007290#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007291 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007292#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007293 new_st.st_fold[i + 128] = c;
7294 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7295 new_st.st_upper[c] = i + 128;
7296 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007297 }
7298
Bram Moolenaar5195e452005-08-19 20:32:47 +00007299 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007300}
7301
7302 static int
7303set_spell_finish(new_st)
7304 spelltab_T *new_st;
7305{
7306 int i;
7307
7308 if (did_set_spelltab)
7309 {
7310 /* check that it's the same table */
7311 for (i = 0; i < 256; ++i)
7312 {
7313 if (spelltab.st_isw[i] != new_st->st_isw[i]
7314 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007315 || spelltab.st_fold[i] != new_st->st_fold[i]
7316 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007317 {
7318 EMSG(_("E763: Word characters differ between spell files"));
7319 return FAIL;
7320 }
7321 }
7322 }
7323 else
7324 {
7325 /* copy the new spelltab into the one being used */
7326 spelltab = *new_st;
7327 did_set_spelltab = TRUE;
7328 }
7329
7330 return OK;
7331}
7332
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007333/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007334 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007335 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007336 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007337 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007338 */
7339 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007340spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007341 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007342 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007343{
Bram Moolenaarea408852005-06-25 22:49:46 +00007344#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007345 char_u *s;
7346 int l;
7347 int c;
7348
7349 if (has_mbyte)
7350 {
7351 l = MB_BYTE2LEN(*p);
7352 s = p;
7353 if (l == 1)
7354 {
7355 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007356 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007357 {
7358 s = p + 1; /* skip a mid-word character */
7359 l = MB_BYTE2LEN(*s);
7360 }
7361 }
7362 else
7363 {
7364 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007365 if (c < 256 ? buf->b_spell_ismw[c]
7366 : (buf->b_spell_ismw_mb != NULL
7367 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007368 {
7369 s = p + l;
7370 l = MB_BYTE2LEN(*s);
7371 }
7372 }
7373
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007374 c = mb_ptr2char(s);
7375 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007376 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007377 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007378 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007379#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007380
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007381 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
7382}
7383
7384/*
7385 * Return TRUE if "p" points to a word character.
7386 * Unlike spell_iswordp() this doesn't check for "midword" characters.
7387 */
7388 static int
7389spell_iswordp_nmw(p)
7390 char_u *p;
7391{
7392#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007393 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007394
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007395 if (has_mbyte)
7396 {
7397 c = mb_ptr2char(p);
7398 if (c > 255)
7399 return mb_get_class(p) >= 2;
7400 return spelltab.st_isw[c];
7401 }
7402#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007403 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00007404}
7405
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007406#ifdef FEAT_MBYTE
7407/*
7408 * Return TRUE if "p" points to a word character.
7409 * Wide version of spell_iswordp().
7410 */
7411 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007412spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007413 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007414 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007415{
7416 int *s;
7417
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007418 if (*p < 256 ? buf->b_spell_ismw[*p]
7419 : (buf->b_spell_ismw_mb != NULL
7420 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007421 s = p + 1;
7422 else
7423 s = p;
7424
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007425 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007426 {
7427 if (enc_utf8)
7428 return utf_class(*s) >= 2;
7429 if (enc_dbcs)
7430 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
7431 return 0;
7432 }
7433 return spelltab.st_isw[*s];
7434}
7435#endif
7436
Bram Moolenaarea408852005-06-25 22:49:46 +00007437/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007438 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007439 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007440 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007441 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007442write_spell_prefcond(fd, gap)
7443 FILE *fd;
7444 garray_T *gap;
7445{
7446 int i;
7447 char_u *p;
7448 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007449 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007450
Bram Moolenaar5195e452005-08-19 20:32:47 +00007451 if (fd != NULL)
7452 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
7453
7454 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007455
7456 for (i = 0; i < gap->ga_len; ++i)
7457 {
7458 /* <prefcond> : <condlen> <condstr> */
7459 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00007460 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007461 {
7462 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007463 if (fd != NULL)
7464 {
7465 fputc(len, fd);
7466 fwrite(p, (size_t)len, (size_t)1, fd);
7467 }
7468 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007469 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007470 else if (fd != NULL)
7471 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007472 }
7473
Bram Moolenaar5195e452005-08-19 20:32:47 +00007474 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007475}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007476
7477/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007478 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
7479 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007480 * When using a multi-byte 'encoding' the length may change!
7481 * Returns FAIL when something wrong.
7482 */
7483 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007484spell_casefold(str, len, buf, buflen)
7485 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007486 int len;
7487 char_u *buf;
7488 int buflen;
7489{
7490 int i;
7491
7492 if (len >= buflen)
7493 {
7494 buf[0] = NUL;
7495 return FAIL; /* result will not fit */
7496 }
7497
7498#ifdef FEAT_MBYTE
7499 if (has_mbyte)
7500 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007501 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007502 char_u *p;
7503 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007504
7505 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007506 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007507 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007508 if (outi + MB_MAXBYTES > buflen)
7509 {
7510 buf[outi] = NUL;
7511 return FAIL;
7512 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007513 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007514 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007515 }
7516 buf[outi] = NUL;
7517 }
7518 else
7519#endif
7520 {
7521 /* Be quick for non-multibyte encodings. */
7522 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007523 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007524 buf[i] = NUL;
7525 }
7526
7527 return OK;
7528}
7529
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007530#define SPS_BEST 1
7531#define SPS_FAST 2
7532#define SPS_DOUBLE 4
7533
7534static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007535static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007536
7537/*
7538 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007539 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007540 */
7541 int
7542spell_check_sps()
7543{
7544 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007545 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007546 char_u buf[MAXPATHL];
7547 int f;
7548
7549 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007550 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007551
7552 for (p = p_sps; *p != NUL; )
7553 {
7554 copy_option_part(&p, buf, MAXPATHL, ",");
7555
7556 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007557 if (VIM_ISDIGIT(*buf))
7558 {
7559 s = buf;
7560 sps_limit = getdigits(&s);
7561 if (*s != NUL && !VIM_ISDIGIT(*s))
7562 f = -1;
7563 }
7564 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007565 f = SPS_BEST;
7566 else if (STRCMP(buf, "fast") == 0)
7567 f = SPS_FAST;
7568 else if (STRCMP(buf, "double") == 0)
7569 f = SPS_DOUBLE;
7570 else if (STRNCMP(buf, "expr:", 5) != 0
7571 && STRNCMP(buf, "file:", 5) != 0)
7572 f = -1;
7573
7574 if (f == -1 || (sps_flags != 0 && f != 0))
7575 {
7576 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007577 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007578 return FAIL;
7579 }
7580 if (f != 0)
7581 sps_flags = f;
7582 }
7583
7584 if (sps_flags == 0)
7585 sps_flags = SPS_BEST;
7586
7587 return OK;
7588}
7589
7590/* Remember what "z?" replaced. */
7591static char_u *repl_from = NULL;
7592static char_u *repl_to = NULL;
7593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007594/*
7595 * "z?": Find badly spelled word under or after the cursor.
7596 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00007597 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007598 */
7599 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00007600spell_suggest(count)
7601 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007602{
7603 char_u *line;
7604 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007605 char_u wcopy[MAXWLEN + 2];
7606 char_u *p;
7607 int i;
7608 int c;
7609 suginfo_T sug;
7610 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007611 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007612 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007613 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00007614 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007615
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007616 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007617 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
7618 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007619 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007620 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
7621 return;
7622
7623 /* No bad word or it starts after the cursor: use the word under the
7624 * cursor. */
7625 curwin->w_cursor = prev_cursor;
7626 line = ml_get_curline();
7627 p = line + curwin->w_cursor.col;
7628 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007629 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007630 mb_ptr_back(line, p);
7631 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007632 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007633 mb_ptr_adv(p);
7634
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007635 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007636 {
7637 beep_flush();
7638 return;
7639 }
7640 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 }
7642
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007643 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007644
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007645 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007646 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007647
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007648 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007649
Bram Moolenaar5195e452005-08-19 20:32:47 +00007650 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
7651 * 'spellsuggest', whatever is smaller. */
7652 if (sps_limit > (int)Rows - 2)
7653 limit = (int)Rows - 2;
7654 else
7655 limit = sps_limit;
7656 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007657 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007658
7659 if (sug.su_ga.ga_len == 0)
7660 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00007661 else if (count > 0)
7662 {
7663 if (count > sug.su_ga.ga_len)
7664 smsg((char_u *)_("Sorry, only %ld suggestions"),
7665 (long)sug.su_ga.ga_len);
7666 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007667 else
7668 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007669 vim_free(repl_from);
7670 repl_from = NULL;
7671 vim_free(repl_to);
7672 repl_to = NULL;
7673
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007674#ifdef FEAT_RIGHTLEFT
7675 /* When 'rightleft' is set the list is drawn right-left. */
7676 cmdmsg_rl = curwin->w_p_rl;
7677 if (cmdmsg_rl)
7678 msg_col = Columns - 1;
7679#endif
7680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007681 /* List the suggestions. */
7682 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007683 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
7685 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007686#ifdef FEAT_RIGHTLEFT
7687 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
7688 {
7689 /* And now the rabbit from the high hat: Avoid showing the
7690 * untranslated message rightleft. */
7691 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
7692 sug.su_badlen, sug.su_badptr);
7693 }
7694#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007695 msg_puts(IObuff);
7696 msg_clr_eos();
7697 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00007698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007699 msg_scroll = TRUE;
7700 for (i = 0; i < sug.su_ga.ga_len; ++i)
7701 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007702 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007703
7704 /* The suggested word may replace only part of the bad word, add
7705 * the not replaced part. */
7706 STRCPY(wcopy, stp->st_word);
7707 if (sug.su_badlen > stp->st_orglen)
7708 vim_strncpy(wcopy + STRLEN(wcopy),
7709 sug.su_badptr + stp->st_orglen,
7710 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007711 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
7712#ifdef FEAT_RIGHTLEFT
7713 if (cmdmsg_rl)
7714 rl_mirror(IObuff);
7715#endif
7716 msg_puts(IObuff);
7717
7718 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007719 msg_puts(IObuff);
7720
7721 /* The word may replace more than "su_badlen". */
7722 if (sug.su_badlen < stp->st_orglen)
7723 {
7724 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
7725 stp->st_orglen, sug.su_badptr);
7726 msg_puts(IObuff);
7727 }
7728
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007729 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007730 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007731 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007732 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007733 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007734 stp->st_salscore ? "s " : "",
7735 stp->st_score, stp->st_altscore);
7736 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007737 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00007738 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007739#ifdef FEAT_RIGHTLEFT
7740 if (cmdmsg_rl)
7741 /* Mirror the numbers, but keep the leading space. */
7742 rl_mirror(IObuff + 1);
7743#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00007744 msg_advance(30);
7745 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007747 msg_putchar('\n');
7748 }
7749
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007750#ifdef FEAT_RIGHTLEFT
7751 cmdmsg_rl = FALSE;
7752 msg_col = 0;
7753#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007754 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00007755 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007756 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00007757 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007758 }
7759
Bram Moolenaard12a1322005-08-21 22:08:24 +00007760 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
7761 {
7762 /* Save the from and to text for :spellrepall. */
7763 stp = &SUG(sug.su_ga, selected - 1);
7764 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
7765 repl_to = vim_strsave(stp->st_word);
7766
7767 /* Replace the word. */
7768 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
7769 if (p != NULL)
7770 {
7771 c = sug.su_badptr - line;
7772 mch_memmove(p, line, c);
7773 STRCPY(p + c, stp->st_word);
7774 STRCAT(p, sug.su_badptr + stp->st_orglen);
7775 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7776 curwin->w_cursor.col = c;
7777 changed_bytes(curwin->w_cursor.lnum, c);
7778
7779 /* For redo we use a change-word command. */
7780 ResetRedobuff();
7781 AppendToRedobuff((char_u *)"ciw");
7782 AppendToRedobuff(stp->st_word);
7783 AppendCharToRedobuff(ESC);
7784 }
7785 }
7786 else
7787 curwin->w_cursor = prev_cursor;
7788
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007789 spell_find_cleanup(&sug);
7790}
7791
7792/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007793 * Check if the word at line "lnum" column "col" is required to start with a
7794 * capital. This uses 'spellcapcheck' of the current buffer.
7795 */
7796 static int
7797check_need_cap(lnum, col)
7798 linenr_T lnum;
7799 colnr_T col;
7800{
7801 int need_cap = FALSE;
7802 char_u *line;
7803 char_u *line_copy = NULL;
7804 char_u *p;
7805 colnr_T endcol;
7806 regmatch_T regmatch;
7807
7808 if (curbuf->b_cap_prog == NULL)
7809 return FALSE;
7810
7811 line = ml_get_curline();
7812 endcol = 0;
7813 if ((int)(skipwhite(line) - line) >= (int)col)
7814 {
7815 /* At start of line, check if previous line is empty or sentence
7816 * ends there. */
7817 if (lnum == 1)
7818 need_cap = TRUE;
7819 else
7820 {
7821 line = ml_get(lnum - 1);
7822 if (*skipwhite(line) == NUL)
7823 need_cap = TRUE;
7824 else
7825 {
7826 /* Append a space in place of the line break. */
7827 line_copy = concat_str(line, (char_u *)" ");
7828 line = line_copy;
7829 endcol = STRLEN(line);
7830 }
7831 }
7832 }
7833 else
7834 endcol = col;
7835
7836 if (endcol > 0)
7837 {
7838 /* Check if sentence ends before the bad word. */
7839 regmatch.regprog = curbuf->b_cap_prog;
7840 regmatch.rm_ic = FALSE;
7841 p = line + endcol;
7842 for (;;)
7843 {
7844 mb_ptr_back(line, p);
7845 if (p == line || spell_iswordp_nmw(p))
7846 break;
7847 if (vim_regexec(&regmatch, p, 0)
7848 && regmatch.endp[0] == line + endcol)
7849 {
7850 need_cap = TRUE;
7851 break;
7852 }
7853 }
7854 }
7855
7856 vim_free(line_copy);
7857
7858 return need_cap;
7859}
7860
7861
7862/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007863 * ":spellrepall"
7864 */
7865/*ARGSUSED*/
7866 void
7867ex_spellrepall(eap)
7868 exarg_T *eap;
7869{
7870 pos_T pos = curwin->w_cursor;
7871 char_u *frompat;
7872 int addlen;
7873 char_u *line;
7874 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007875 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007876 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007877
7878 if (repl_from == NULL || repl_to == NULL)
7879 {
7880 EMSG(_("E752: No previous spell replacement"));
7881 return;
7882 }
7883 addlen = STRLEN(repl_to) - STRLEN(repl_from);
7884
7885 frompat = alloc(STRLEN(repl_from) + 7);
7886 if (frompat == NULL)
7887 return;
7888 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
7889 p_ws = FALSE;
7890
Bram Moolenaar5195e452005-08-19 20:32:47 +00007891 sub_nsubs = 0;
7892 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007893 curwin->w_cursor.lnum = 0;
7894 while (!got_int)
7895 {
7896 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
7897 || u_save_cursor() == FAIL)
7898 break;
7899
7900 /* Only replace when the right word isn't there yet. This happens
7901 * when changing "etc" to "etc.". */
7902 line = ml_get_curline();
7903 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
7904 repl_to, STRLEN(repl_to)) != 0)
7905 {
7906 p = alloc(STRLEN(line) + addlen + 1);
7907 if (p == NULL)
7908 break;
7909 mch_memmove(p, line, curwin->w_cursor.col);
7910 STRCPY(p + curwin->w_cursor.col, repl_to);
7911 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
7912 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7913 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007914
7915 if (curwin->w_cursor.lnum != prev_lnum)
7916 {
7917 ++sub_nlines;
7918 prev_lnum = curwin->w_cursor.lnum;
7919 }
7920 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007921 }
7922 curwin->w_cursor.col += STRLEN(repl_to);
7923 }
7924
7925 p_ws = save_ws;
7926 curwin->w_cursor = pos;
7927 vim_free(frompat);
7928
Bram Moolenaar5195e452005-08-19 20:32:47 +00007929 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007930 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007931 else
7932 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007933}
7934
7935/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007936 * Find spell suggestions for "word". Return them in the growarray "*gap" as
7937 * a list of allocated strings.
7938 */
7939 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007940spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007941 garray_T *gap;
7942 char_u *word;
7943 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007944 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007945{
7946 suginfo_T sug;
7947 int i;
7948 suggest_T *stp;
7949 char_u *wcopy;
7950
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007951 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007952
7953 /* Make room in "gap". */
7954 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
7955 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
7956 return;
7957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007958 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007959 {
7960 stp = &SUG(sug.su_ga, i);
7961
7962 /* The suggested word may replace only part of "word", add the not
7963 * replaced part. */
7964 wcopy = alloc(STRLEN(stp->st_word)
7965 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
7966 if (wcopy == NULL)
7967 break;
7968 STRCPY(wcopy, stp->st_word);
7969 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
7970 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
7971 }
7972
7973 spell_find_cleanup(&sug);
7974}
7975
7976/*
7977 * Find spell suggestions for the word at the start of "badptr".
7978 * Return the suggestions in "su->su_ga".
7979 * The maximum number of suggestions is "maxcount".
7980 * Note: does use info for the current window.
7981 * This is based on the mechanisms of Aspell, but completely reimplemented.
7982 */
7983 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007984spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007985 char_u *badptr;
7986 suginfo_T *su;
7987 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00007988 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007989 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007990{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007991 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007992 char_u buf[MAXPATHL];
7993 char_u *p;
7994 int do_combine = FALSE;
7995 char_u *sps_copy;
7996#ifdef FEAT_EVAL
7997 static int expr_busy = FALSE;
7998#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007999 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008000
8001 /*
8002 * Set the info in "*su".
8003 */
8004 vim_memset(su, 0, sizeof(suginfo_T));
8005 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8006 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008007 if (*badptr == NUL)
8008 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008009 hash_init(&su->su_banned);
8010
8011 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008012 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008013 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008014 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008015
8016 if (su->su_badlen >= MAXWLEN)
8017 su->su_badlen = MAXWLEN - 1; /* just in case */
8018 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8019 (void)spell_casefold(su->su_badptr, su->su_badlen,
8020 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008021 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008022 su->su_badflags = badword_captype(su->su_badptr,
8023 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008024 if (need_cap)
8025 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008026
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008027 /* If the word is not capitalised and spell_check() doesn't consider the
8028 * word to be bad then it might need to be capitalised. Add a suggestion
8029 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008030 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008031 if (!SPELL_ISUPPER(c) && attr == 0)
8032 {
8033 make_case_word(su->su_badword, buf, WF_ONECAP);
8034 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8035 0, TRUE);
8036 }
8037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008038 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008039 if (banbadword)
8040 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008041
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008042 /* Make a copy of 'spellsuggest', because the expression may change it. */
8043 sps_copy = vim_strsave(p_sps);
8044 if (sps_copy == NULL)
8045 return;
8046
8047 /* Loop over the items in 'spellsuggest'. */
8048 for (p = sps_copy; *p != NUL; )
8049 {
8050 copy_option_part(&p, buf, MAXPATHL, ",");
8051
8052 if (STRNCMP(buf, "expr:", 5) == 0)
8053 {
8054#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008055 /* Evaluate an expression. Skip this when called recursively,
8056 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008057 if (!expr_busy)
8058 {
8059 expr_busy = TRUE;
8060 spell_suggest_expr(su, buf + 5);
8061 expr_busy = FALSE;
8062 }
8063#endif
8064 }
8065 else if (STRNCMP(buf, "file:", 5) == 0)
8066 /* Use list of suggestions in a file. */
8067 spell_suggest_file(su, buf + 5);
8068 else
8069 {
8070 /* Use internal method. */
8071 spell_suggest_intern(su);
8072 if (sps_flags & SPS_DOUBLE)
8073 do_combine = TRUE;
8074 }
8075 }
8076
8077 vim_free(sps_copy);
8078
8079 if (do_combine)
8080 /* Combine the two list of suggestions. This must be done last,
8081 * because sorting changes the order again. */
8082 score_combine(su);
8083}
8084
8085#ifdef FEAT_EVAL
8086/*
8087 * Find suggestions by evaluating expression "expr".
8088 */
8089 static void
8090spell_suggest_expr(su, expr)
8091 suginfo_T *su;
8092 char_u *expr;
8093{
8094 list_T *list;
8095 listitem_T *li;
8096 int score;
8097 char_u *p;
8098
8099 /* The work is split up in a few parts to avoid having to export
8100 * suginfo_T.
8101 * First evaluate the expression and get the resulting list. */
8102 list = eval_spell_expr(su->su_badword, expr);
8103 if (list != NULL)
8104 {
8105 /* Loop over the items in the list. */
8106 for (li = list->lv_first; li != NULL; li = li->li_next)
8107 if (li->li_tv.v_type == VAR_LIST)
8108 {
8109 /* Get the word and the score from the items. */
8110 score = get_spellword(li->li_tv.vval.v_list, &p);
8111 if (score >= 0)
8112 add_suggestion(su, &su->su_ga, p,
8113 su->su_badlen, score, 0, TRUE);
8114 }
8115 list_unref(list);
8116 }
8117
8118 /* Sort the suggestions and truncate at "maxcount". */
8119 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8120}
8121#endif
8122
8123/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008124 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008125 */
8126 static void
8127spell_suggest_file(su, fname)
8128 suginfo_T *su;
8129 char_u *fname;
8130{
8131 FILE *fd;
8132 char_u line[MAXWLEN * 2];
8133 char_u *p;
8134 int len;
8135 char_u cword[MAXWLEN];
8136
8137 /* Open the file. */
8138 fd = mch_fopen((char *)fname, "r");
8139 if (fd == NULL)
8140 {
8141 EMSG2(_(e_notopen), fname);
8142 return;
8143 }
8144
8145 /* Read it line by line. */
8146 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8147 {
8148 line_breakcheck();
8149
8150 p = vim_strchr(line, '/');
8151 if (p == NULL)
8152 continue; /* No Tab found, just skip the line. */
8153 *p++ = NUL;
8154 if (STRICMP(su->su_badword, line) == 0)
8155 {
8156 /* Match! Isolate the good word, until CR or NL. */
8157 for (len = 0; p[len] >= ' '; ++len)
8158 ;
8159 p[len] = NUL;
8160
8161 /* If the suggestion doesn't have specific case duplicate the case
8162 * of the bad word. */
8163 if (captype(p, NULL) == 0)
8164 {
8165 make_case_word(p, cword, su->su_badflags);
8166 p = cword;
8167 }
8168
8169 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8170 SCORE_FILE, 0, TRUE);
8171 }
8172 }
8173
8174 fclose(fd);
8175
8176 /* Sort the suggestions and truncate at "maxcount". */
8177 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8178}
8179
8180/*
8181 * Find suggestions for the internal method indicated by "sps_flags".
8182 */
8183 static void
8184spell_suggest_intern(su)
8185 suginfo_T *su;
8186{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008187 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008188 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008189 *
8190 * Set a maximum score to limit the combination of operations that is
8191 * tried.
8192 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008193 suggest_try_special(su);
8194
8195 /*
8196 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8197 * from the .aff file and inserting a space (split the word).
8198 */
8199 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008200
8201 /* For the resulting top-scorers compute the sound-a-like score. */
8202 if (sps_flags & SPS_DOUBLE)
8203 score_comp_sal(su);
8204
8205 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008206 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008207 *
8208 * Only do this when we don't have a lot of suggestions yet, because it's
8209 * very slow and often doesn't find new suggestions.
8210 */
8211 if ((sps_flags & SPS_DOUBLE)
8212 || (!(sps_flags & SPS_FAST)
8213 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8214 {
8215 /* Allow a higher score now. */
8216 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008217 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008218 }
8219
8220 /* When CTRL-C was hit while searching do show the results. */
8221 ui_breakcheck();
8222 if (got_int)
8223 {
8224 (void)vgetc();
8225 got_int = FALSE;
8226 }
8227
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008228 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008229 {
8230 if (sps_flags & SPS_BEST)
8231 /* Adjust the word score for how it sounds like. */
8232 rescore_suggestions(su);
8233
8234 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008235 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008236 }
8237}
8238
8239/*
8240 * Free the info put in "*su" by spell_find_suggest().
8241 */
8242 static void
8243spell_find_cleanup(su)
8244 suginfo_T *su;
8245{
8246 int i;
8247
8248 /* Free the suggestions. */
8249 for (i = 0; i < su->su_ga.ga_len; ++i)
8250 vim_free(SUG(su->su_ga, i).st_word);
8251 ga_clear(&su->su_ga);
8252 for (i = 0; i < su->su_sga.ga_len; ++i)
8253 vim_free(SUG(su->su_sga, i).st_word);
8254 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008255
8256 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008257 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258}
8259
8260/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008261 * Make a copy of "word", with the first letter upper or lower cased, to
8262 * "wcopy[MAXWLEN]". "word" must not be empty.
8263 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008264 */
8265 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008266onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008267 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268 char_u *wcopy;
8269 int upper; /* TRUE: first letter made upper case */
8270{
8271 char_u *p;
8272 int c;
8273 int l;
8274
8275 p = word;
8276#ifdef FEAT_MBYTE
8277 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008278 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008279 else
8280#endif
8281 c = *p++;
8282 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008283 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008284 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008285 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008286#ifdef FEAT_MBYTE
8287 if (has_mbyte)
8288 l = mb_char2bytes(c, wcopy);
8289 else
8290#endif
8291 {
8292 l = 1;
8293 wcopy[0] = c;
8294 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008295 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008296}
8297
8298/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008299 * Make a copy of "word" with all the letters upper cased into
8300 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008301 */
8302 static void
8303allcap_copy(word, wcopy)
8304 char_u *word;
8305 char_u *wcopy;
8306{
8307 char_u *s;
8308 char_u *d;
8309 int c;
8310
8311 d = wcopy;
8312 for (s = word; *s != NUL; )
8313 {
8314#ifdef FEAT_MBYTE
8315 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008316 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317 else
8318#endif
8319 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008320 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008321
8322#ifdef FEAT_MBYTE
8323 if (has_mbyte)
8324 {
8325 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8326 break;
8327 d += mb_char2bytes(c, d);
8328 }
8329 else
8330#endif
8331 {
8332 if (d - wcopy >= MAXWLEN - 1)
8333 break;
8334 *d++ = c;
8335 }
8336 }
8337 *d = NUL;
8338}
8339
8340/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008341 * Try finding suggestions by recognizing specific situations.
8342 */
8343 static void
8344suggest_try_special(su)
8345 suginfo_T *su;
8346{
8347 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008348 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008349 int c;
8350 char_u word[MAXWLEN];
8351
8352 /*
8353 * Recognize a word that is repeated: "the the".
8354 */
8355 p = skiptowhite(su->su_fbadword);
8356 len = p - su->su_fbadword;
8357 p = skipwhite(p);
8358 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
8359 {
8360 /* Include badflags: if the badword is onecap or allcap
8361 * use that for the goodword too: "The the" -> "The". */
8362 c = su->su_fbadword[len];
8363 su->su_fbadword[len] = NUL;
8364 make_case_word(su->su_fbadword, word, su->su_badflags);
8365 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008366 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008367 }
8368}
8369
8370/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008371 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00008372 *
8373 * This uses a state machine. At each node in the tree we try various
8374 * operations. When trying if an operation work "depth" is increased and the
8375 * stack[] is used to store info. This allows combinations, thus insert one
8376 * character, replace one and delete another. The number of changes is
8377 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00008378 *
8379 * After implementing this I noticed an article by Kemal Oflazer that
8380 * describes something similar: "Error-tolerant Finite State Recognition with
8381 * Applications to Morphological Analysis and Spelling Correction" (1996).
8382 * The implementation in the article is simplified and requires a stack of
8383 * unknown depth. The implementation here only needs a stack depth of the
8384 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008385 */
8386 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008387suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008388 suginfo_T *su;
8389{
8390 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
8391 char_u tword[MAXWLEN]; /* good word collected so far */
8392 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008393 char_u preword[MAXWLEN * 3]; /* word found with proper case;
8394 * concatanation of prefix compound
8395 * words and split word. NUL terminated
8396 * when going deeper but not when coming
8397 * back. */
8398 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 trystate_T *sp;
8400 int newscore;
8401 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008402 char_u *byts, *fbyts, *pbyts;
8403 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008404 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008405 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008406 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008408 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008409 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008410 int len;
8411 char_u *p;
8412 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00008413 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008414 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008415 slang_T *slang;
8416 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417
8418 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00008419 * to find matches (esp. REP items). Append some more text, changing
8420 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008421 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008422 n = STRLEN(fword);
8423 p = su->su_badptr + su->su_badlen;
8424 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425
8426 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8427 lp->lp_slang != NULL; ++lp)
8428 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008429 slang = lp->lp_slang;
8430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 /*
8432 * Go through the whole case-fold tree, try changes at each node.
8433 * "tword[]" contains the word collected from nodes in the tree.
8434 * "fword[]" the word we are trying to match with (initially the bad
8435 * word).
8436 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008437 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008438 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008439 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008440 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008441
Bram Moolenaarea424162005-06-16 21:51:00 +00008442 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008443 * When there are postponed prefixes we need to use these first. At
8444 * the end of the prefix we continue in the case-fold tree.
8445 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008446 fbyts = slang->sl_fbyts;
8447 fidxs = slang->sl_fidxs;
8448 pbyts = slang->sl_pbyts;
8449 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008450 if (pbyts != NULL)
8451 {
8452 byts = pbyts;
8453 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008454 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008455 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
8456 }
8457 else
8458 {
8459 byts = fbyts;
8460 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008461 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008462 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008463 }
8464
8465 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00008466 * Loop to find all suggestions. At each round we either:
8467 * - For the current state try one operation, advance "ts_curi",
8468 * increase "depth".
8469 * - When a state is done go to the next, set "ts_state".
8470 * - When all states are tried decrease "depth".
8471 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008472 while (depth >= 0 && !got_int)
8473 {
8474 sp = &stack[depth];
8475 switch (sp->ts_state)
8476 {
8477 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008478 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008479 /*
8480 * Start of node: Deal with NUL bytes, which means
8481 * tword[] may end here.
8482 */
8483 arridx = sp->ts_arridx; /* current node in the tree */
8484 len = byts[arridx]; /* bytes in this node */
8485 arridx += sp->ts_curi; /* index of current byte */
8486
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008487 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008488 {
8489 /* Skip over the NUL bytes, we use them later. */
8490 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
8491 ;
8492 sp->ts_curi += n;
8493
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008494 /* Always past NUL bytes now. */
8495 n = (int)sp->ts_state;
8496 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008497 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008498
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008499 /* At end of a prefix or at start of prefixtree: check for
8500 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008501 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008502 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00008503 /* Set su->su_badflags to the caps type at this
8504 * position. Use the caps type until here for the
8505 * prefix itself. */
8506#ifdef FEAT_MBYTE
8507 if (has_mbyte)
8508 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
8509 else
8510#endif
8511 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008512 flags = badword_captype(su->su_badptr,
8513 su->su_badptr + n);
8514 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008515 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008516 ++depth;
8517 stack[depth] = stack[depth - 1];
8518 sp = &stack[depth];
8519 sp->ts_prefixdepth = depth - 1;
8520 byts = fbyts;
8521 idxs = fidxs;
8522 sp->ts_state = STATE_START;
8523 sp->ts_curi = 1; /* start just after length byte */
8524 sp->ts_arridx = 0;
8525
Bram Moolenaar53805d12005-08-01 07:08:33 +00008526 /* Move the prefix to preword[] with the right case
8527 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008528 tword[sp->ts_twordlen] = NUL;
8529 make_case_word(tword + sp->ts_splitoff,
8530 preword + sp->ts_prewordlen,
8531 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008532 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00008533 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008534 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008535 break;
8536 }
8537
Bram Moolenaar0c405862005-06-22 22:26:26 +00008538 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 {
8540 /* Past bytes in node and/or past NUL bytes. */
8541 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008542 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008543 break;
8544 }
8545
8546 /*
8547 * End of word in tree.
8548 */
8549 ++sp->ts_curi; /* eat one NUL byte */
8550
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008551 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008552 fword_ends = (fword[sp->ts_fidx] == NUL
8553 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
8554 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008555
Bram Moolenaard12a1322005-08-21 22:08:24 +00008556 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
8557 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008558 {
8559 /* There was a prefix before the word. Check that the
8560 * prefix can be used with this word. */
8561 /* Count the length of the NULs in the prefix. If there
8562 * are none this must be the first try without a prefix.
8563 */
8564 n = stack[sp->ts_prefixdepth].ts_arridx;
8565 len = pbyts[n++];
8566 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
8567 ;
8568 if (c > 0)
8569 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008570 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008571 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008572 if (c == 0)
8573 break;
8574
8575 /* Use the WF_RARE flag for a rare prefix. */
8576 if (c & WF_RAREPFX)
8577 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008578
8579 /* Tricky: when checking for both prefix and
8580 * compounding we run into the prefix flag first.
8581 * Remember that it's OK, so that we accept the prefix
8582 * when arriving at a compound flag. */
8583 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008584 }
8585 }
8586
Bram Moolenaard12a1322005-08-21 22:08:24 +00008587 if (sp->ts_complen > sp->ts_compsplit)
8588 {
8589 /* There was a compound word before this word. If this
8590 * word does not support compounding then give up
8591 * (splitting is tried for the word without compound
8592 * flag). */
8593 if (((unsigned)flags >> 24) == 0
8594 || sp->ts_twordlen - sp->ts_splitoff
8595 < slang->sl_compminlen)
8596 break;
8597 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8598 compflags[sp->ts_complen + 1] = NUL;
8599 if (fword_ends && !can_compound(slang,
8600 tword + sp->ts_splitoff,
8601 compflags + sp->ts_compsplit))
8602 break;
8603 }
8604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008605 /*
8606 * Form the word with proper case in preword.
8607 * If there is a word from a previous split, append.
8608 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008609 if (flags & WF_KEEPCAP)
8610 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008611 find_keepcap_word(slang, tword + sp->ts_splitoff,
8612 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008613 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00008614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008615 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00008616 * use that for the goodword too. But if the badword is
8617 * allcap and it's only one char long use onecap. */
8618 c = su->su_badflags;
8619 if ((c & WF_ALLCAP)
8620#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008621 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008622#else
8623 && su->su_badlen == 1
8624#endif
8625 )
8626 c = WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008627 make_case_word(tword + sp->ts_splitoff,
8628 preword + sp->ts_prewordlen, flags | c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008629 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008630
8631 /* Don't use a banned word. It may appear again as a good
8632 * word, thus remember it. */
8633 if (flags & WF_BANNED)
8634 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008635 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008636 break;
8637 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008638 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008639 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008640 break;
8641
8642 newscore = 0;
8643 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008644 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008645 newscore += SCORE_REGION;
8646 if (flags & WF_RARE)
8647 newscore += SCORE_RARE;
8648
Bram Moolenaar0c405862005-06-22 22:26:26 +00008649 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008650 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008651 newscore += SCORE_ICASE;
8652
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008653 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008655 /* The badword also ends: add suggestions. Give a penalty
8656 * when changing non-word char to word char, e.g., "thes,"
8657 * -> "these". */
8658 p = fword + sp->ts_fidx;
8659#ifdef FEAT_MBYTE
8660 if (has_mbyte)
8661 mb_ptr_back(fword, p);
8662 else
8663#endif
8664 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008665 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008666 {
8667 p = preword + STRLEN(preword);
8668#ifdef FEAT_MBYTE
8669 if (has_mbyte)
8670 mb_ptr_back(preword, p);
8671 else
8672#endif
8673 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008674 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008675 newscore += SCORE_NONWORD;
8676 }
8677
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008678 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008679 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008680 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008682 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00008683#ifdef FEAT_MBYTE
8684 /* Don't split halfway a character. */
8685 && (!has_mbyte || sp->ts_tcharlen == 0)
8686#endif
8687 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008688 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008689 int try_compound;
8690
8691 /* Get here in two situations:
8692 * 1. The word in the tree ends but the badword continues:
8693 * If the word allows compounding try that. Otherwise
8694 * try a split by inserting a space. For both check
8695 * that a valid words starts at fword[sp->ts_fidx].
8696 * 2. The badword does end, but it was due to a change
8697 * (e.g., a swap). No need to split, but do check that
8698 * the following word is valid.
8699 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008700 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008701 if (!fword_ends
Bram Moolenaar5195e452005-08-19 20:32:47 +00008702 && ((unsigned)flags >> 24) != 0
8703 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008704 >= slang->sl_compminlen
Bram Moolenaard12a1322005-08-21 22:08:24 +00008705 && sp->ts_complen + 1 - sp->ts_compsplit
8706 < slang->sl_compmax
8707 && (vim_strchr(sp->ts_complen == sp->ts_compsplit
8708 ? slang->sl_compstartflags
8709 : slang->sl_compallflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008710 ((unsigned)flags >> 24)) != NULL))
8711 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008712 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008713 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8714 compflags[sp->ts_complen + 1] = NUL;
8715 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008716
8717 /* If we could add a compound word, and it's also possible
8718 * to split at this point, do the split first and set
8719 * TSF_DIDSPLIT to avoid doing it again. */
8720 if (!fword_ends
8721 && try_compound
8722 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008723 {
8724 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008725 sp->ts_flags |= TSF_DIDSPLIT;
8726 --sp->ts_curi; /* do the same NUL again */
8727 compflags[sp->ts_complen] = NUL;
8728 }
8729 else
8730 sp->ts_flags &= ~TSF_DIDSPLIT;
8731
8732 if (!try_compound && !fword_ends)
8733 {
8734 /* If we're going to split need to check that the
8735 * words so far are valid for compounding. */
8736 if (sp->ts_complen > sp->ts_compsplit
8737 && !can_compound(slang,
8738 tword + sp->ts_splitoff,
8739 compflags + sp->ts_compsplit))
8740 break;
8741 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008742 }
8743
8744 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008745 {
8746 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008747 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008748 sp->ts_state = STATE_SPLITUNDO;
8749
8750 ++depth;
8751 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008753 /* Append a space to preword when splitting. */
8754 if (!try_compound && !fword_ends)
8755 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00008756 sp->ts_prewordlen = STRLEN(preword);
8757 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008758
8759 /* If the badword has a non-word character at this
8760 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008761 * non-word character with a space. Always skip a
8762 * character when the word ends. */
8763 if ((!try_compound
8764 && !spell_iswordp_nmw(fword + sp->ts_fidx))
8765 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008766 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008767 int l;
8768
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008769#ifdef FEAT_MBYTE
8770 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008771 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008772 else
8773#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008774 l = 1;
8775 if (fword_ends)
8776 {
8777 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008778 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008779 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008780 sp->ts_prewordlen += l;
8781 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008782 }
8783 else
8784 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
8785 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008786 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008787
Bram Moolenaard12a1322005-08-21 22:08:24 +00008788 /* When compounding include compound flag in
8789 * compflags[] (already set above). When splitting we
8790 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008791 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00008792 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008793 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00008794 sp->ts_compsplit = sp->ts_complen;
8795 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008796
Bram Moolenaar53805d12005-08-01 07:08:33 +00008797 /* set su->su_badflags to the caps type at this
8798 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008799#ifdef FEAT_MBYTE
8800 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00008801 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008802 else
8803#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00008804 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008805 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008806 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008808 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008809 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008810
8811 /* If there are postponed prefixes, try these too. */
8812 if (pbyts != NULL)
8813 {
8814 byts = pbyts;
8815 idxs = pidxs;
8816 sp->ts_prefixdepth = PFD_PREFIXTREE;
8817 sp->ts_state = STATE_NOPREFIX;
8818 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008819 }
8820 }
8821 break;
8822
8823 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008824 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008825 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826
8827 /* Continue looking for NUL bytes. */
8828 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008829
8830 /* In case we went into the prefix tree. */
8831 byts = fbyts;
8832 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833 break;
8834
8835 case STATE_ENDNUL:
8836 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008837 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008838 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008839 {
8840 /* The badword ends, can't use the bytes in this node. */
8841 sp->ts_state = STATE_DEL;
8842 break;
8843 }
8844 sp->ts_state = STATE_PLAIN;
8845 /*FALLTHROUGH*/
8846
8847 case STATE_PLAIN:
8848 /*
8849 * Go over all possible bytes at this node, add each to
8850 * tword[] and use child node. "ts_curi" is the index.
8851 */
8852 arridx = sp->ts_arridx;
8853 if (sp->ts_curi > byts[arridx])
8854 {
8855 /* Done all bytes at this node, do next state. When still
8856 * at already changed bytes skip the other tricks. */
8857 if (sp->ts_fidx >= sp->ts_fidxtry)
8858 sp->ts_state = STATE_DEL;
8859 else
8860 sp->ts_state = STATE_FINAL;
8861 }
8862 else
8863 {
8864 arridx += sp->ts_curi++;
8865 c = byts[arridx];
8866
8867 /* Normal byte, go one level deeper. If it's not equal to
8868 * the byte in the bad word adjust the score. But don't
8869 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00008870 if (c == fword[sp->ts_fidx]
8871#ifdef FEAT_MBYTE
8872 || (sp->ts_tcharlen > 0
8873 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008874#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008875 )
8876 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 else
8878 newscore = SCORE_SUBST;
8879 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
8880 && try_deeper(su, stack, depth, newscore))
8881 {
8882 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008883 sp = &stack[depth];
8884 ++sp->ts_fidx;
8885 tword[sp->ts_twordlen++] = c;
8886 sp->ts_arridx = idxs[arridx];
8887#ifdef FEAT_MBYTE
8888 if (newscore == SCORE_SUBST)
8889 sp->ts_isdiff = DIFF_YES;
8890 if (has_mbyte)
8891 {
8892 /* Multi-byte characters are a bit complicated to
8893 * handle: They differ when any of the bytes
8894 * differ and then their length may also differ. */
8895 if (sp->ts_tcharlen == 0)
8896 {
8897 /* First byte. */
8898 sp->ts_tcharidx = 0;
8899 sp->ts_tcharlen = MB_BYTE2LEN(c);
8900 sp->ts_fcharstart = sp->ts_fidx - 1;
8901 sp->ts_isdiff = (newscore != 0)
8902 ? DIFF_YES : DIFF_NONE;
8903 }
8904 else if (sp->ts_isdiff == DIFF_INSERT)
8905 /* When inserting trail bytes don't advance in
8906 * the bad word. */
8907 --sp->ts_fidx;
8908 if (++sp->ts_tcharidx == sp->ts_tcharlen)
8909 {
8910 /* Last byte of character. */
8911 if (sp->ts_isdiff == DIFF_YES)
8912 {
8913 /* Correct ts_fidx for the byte length of
8914 * the character (we didn't check that
8915 * before). */
8916 sp->ts_fidx = sp->ts_fcharstart
8917 + MB_BYTE2LEN(
8918 fword[sp->ts_fcharstart]);
8919
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008920 /* For changing a composing character
8921 * adjust the score from SCORE_SUBST to
8922 * SCORE_SUBCOMP. */
8923 if (enc_utf8
8924 && utf_iscomposing(
8925 mb_ptr2char(tword
8926 + sp->ts_twordlen
8927 - sp->ts_tcharlen))
8928 && utf_iscomposing(
8929 mb_ptr2char(fword
8930 + sp->ts_fcharstart)))
8931 sp->ts_score -=
8932 SCORE_SUBST - SCORE_SUBCOMP;
8933
Bram Moolenaarea424162005-06-16 21:51:00 +00008934 /* For a similar character adjust score
8935 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008936 else if (slang->sl_has_map
8937 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00008938 mb_ptr2char(tword
8939 + sp->ts_twordlen
8940 - sp->ts_tcharlen),
8941 mb_ptr2char(fword
8942 + sp->ts_fcharstart)))
8943 sp->ts_score -=
8944 SCORE_SUBST - SCORE_SIMILAR;
8945 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008946 else if (sp->ts_isdiff == DIFF_INSERT
8947 && sp->ts_twordlen > sp->ts_tcharlen)
8948 {
Bram Moolenaarea408852005-06-25 22:49:46 +00008949 p = tword + sp->ts_twordlen
8950 - sp->ts_tcharlen;
8951 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008952 if (enc_utf8 && utf_iscomposing(c))
8953 {
8954 /* Inserting a composing char doesn't
8955 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008956 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008957 - SCORE_INSCOMP;
8958 }
8959 else
8960 {
8961 /* If the previous character was the
8962 * same, thus doubling a character,
8963 * give a bonus to the score. */
8964 mb_ptr_back(tword, p);
8965 if (c == mb_ptr2char(p))
8966 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00008967 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008968 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008969 }
Bram Moolenaarea424162005-06-16 21:51:00 +00008970
8971 /* Starting a new char, reset the length. */
8972 sp->ts_tcharlen = 0;
8973 }
8974 }
8975 else
8976#endif
8977 {
8978 /* If we found a similar char adjust the score.
8979 * We do this after calling try_deeper() because
8980 * it's slow. */
8981 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008982 && slang->sl_has_map
8983 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00008984 c, fword[sp->ts_fidx - 1]))
8985 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
8986 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 }
8988 }
8989 break;
8990
8991 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00008992#ifdef FEAT_MBYTE
8993 /* When past the first byte of a multi-byte char don't try
8994 * delete/insert/swap a character. */
8995 if (has_mbyte && sp->ts_tcharlen > 0)
8996 {
8997 sp->ts_state = STATE_FINAL;
8998 break;
8999 }
9000#endif
9001 /*
9002 * Try skipping one character in the bad word (delete it).
9003 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009004 sp->ts_state = STATE_INS;
9005 sp->ts_curi = 1;
9006 if (fword[sp->ts_fidx] != NUL
9007 && try_deeper(su, stack, depth, SCORE_DEL))
9008 {
9009 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009010
9011 /* Advance over the character in fword[]. Give a bonus to
9012 * the score if the same character is following "nn" ->
9013 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009014#ifdef FEAT_MBYTE
9015 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009016 {
9017 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009018 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009019 if (enc_utf8 && utf_iscomposing(c))
9020 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9021 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009022 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9023 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009024 else
9025#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009026 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009027 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009028 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9029 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009031 break;
9032 }
9033 /*FALLTHROUGH*/
9034
9035 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009036 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009037 * node. */
9038 n = sp->ts_arridx;
9039 if (sp->ts_curi > byts[n])
9040 {
9041 /* Done all bytes at this node, do next state. */
9042 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 }
9044 else
9045 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009046 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 n += sp->ts_curi++;
9048 c = byts[n];
9049 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9050 {
9051 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009052 sp = &stack[depth];
9053 tword[sp->ts_twordlen++] = c;
9054 sp->ts_arridx = idxs[n];
9055#ifdef FEAT_MBYTE
9056 if (has_mbyte)
9057 {
9058 fl = MB_BYTE2LEN(c);
9059 if (fl > 1)
9060 {
9061 /* There are following bytes for the same
9062 * character. We must find all bytes before
9063 * trying delete/insert/swap/etc. */
9064 sp->ts_tcharlen = fl;
9065 sp->ts_tcharidx = 1;
9066 sp->ts_isdiff = DIFF_INSERT;
9067 }
9068 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009069 else
9070 fl = 1;
9071 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009072#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009073 {
9074 /* If the previous character was the same, thus
9075 * doubling a character, give a bonus to the
9076 * score. */
9077 if (sp->ts_twordlen >= 2
9078 && tword[sp->ts_twordlen - 2] == c)
9079 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 }
9082 }
9083 break;
9084
9085 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009086 /*
9087 * Swap two bytes in the bad word: "12" -> "21".
9088 * We change "fword" here, it's changed back afterwards.
9089 */
9090 p = fword + sp->ts_fidx;
9091 c = *p;
9092 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009093 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009094 /* End of word, can't swap or replace. */
9095 sp->ts_state = STATE_FINAL;
9096 break;
9097 }
9098#ifdef FEAT_MBYTE
9099 if (has_mbyte)
9100 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009101 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009102 c = mb_ptr2char(p);
9103 c2 = mb_ptr2char(p + n);
9104 }
9105 else
9106#endif
9107 c2 = p[1];
9108 if (c == c2)
9109 {
9110 /* Characters are identical, swap won't do anything. */
9111 sp->ts_state = STATE_SWAP3;
9112 break;
9113 }
9114 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9115 {
9116 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009118#ifdef FEAT_MBYTE
9119 if (has_mbyte)
9120 {
9121 fl = mb_char2len(c2);
9122 mch_memmove(p, p + n, fl);
9123 mb_char2bytes(c, p + fl);
9124 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9125 }
9126 else
9127#endif
9128 {
9129 p[0] = c2;
9130 p[1] = c;
9131 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9132 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009133 }
9134 else
9135 /* If this swap doesn't work then SWAP3 won't either. */
9136 sp->ts_state = STATE_REP_INI;
9137 break;
9138
Bram Moolenaarea424162005-06-16 21:51:00 +00009139 case STATE_UNSWAP:
9140 /* Undo the STATE_SWAP swap: "21" -> "12". */
9141 p = fword + sp->ts_fidx;
9142#ifdef FEAT_MBYTE
9143 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009145 n = MB_BYTE2LEN(*p);
9146 c = mb_ptr2char(p + n);
9147 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9148 mb_char2bytes(c, p);
9149 }
9150 else
9151#endif
9152 {
9153 c = *p;
9154 *p = p[1];
9155 p[1] = c;
9156 }
9157 /*FALLTHROUGH*/
9158
9159 case STATE_SWAP3:
9160 /* Swap two bytes, skipping one: "123" -> "321". We change
9161 * "fword" here, it's changed back afterwards. */
9162 p = fword + sp->ts_fidx;
9163#ifdef FEAT_MBYTE
9164 if (has_mbyte)
9165 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009166 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009167 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009168 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009169 c2 = mb_ptr2char(p + n);
9170 c3 = mb_ptr2char(p + n + fl);
9171 }
9172 else
9173#endif
9174 {
9175 c = *p;
9176 c2 = p[1];
9177 c3 = p[2];
9178 }
9179
9180 /* When characters are identical: "121" then SWAP3 result is
9181 * identical, ROT3L result is same as SWAP: "211", ROT3L
9182 * result is same as SWAP on next char: "112". Thus skip all
9183 * swapping. Also skip when c3 is NUL. */
9184 if (c == c3 || c3 == NUL)
9185 {
9186 sp->ts_state = STATE_REP_INI;
9187 break;
9188 }
9189 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9190 {
9191 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009193#ifdef FEAT_MBYTE
9194 if (has_mbyte)
9195 {
9196 tl = mb_char2len(c3);
9197 mch_memmove(p, p + n + fl, tl);
9198 mb_char2bytes(c2, p + tl);
9199 mb_char2bytes(c, p + fl + tl);
9200 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9201 }
9202 else
9203#endif
9204 {
9205 p[0] = p[2];
9206 p[2] = c;
9207 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9208 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 }
9210 else
9211 sp->ts_state = STATE_REP_INI;
9212 break;
9213
Bram Moolenaarea424162005-06-16 21:51:00 +00009214 case STATE_UNSWAP3:
9215 /* Undo STATE_SWAP3: "321" -> "123" */
9216 p = fword + sp->ts_fidx;
9217#ifdef FEAT_MBYTE
9218 if (has_mbyte)
9219 {
9220 n = MB_BYTE2LEN(*p);
9221 c2 = mb_ptr2char(p + n);
9222 fl = MB_BYTE2LEN(p[n]);
9223 c = mb_ptr2char(p + n + fl);
9224 tl = MB_BYTE2LEN(p[n + fl]);
9225 mch_memmove(p + fl + tl, p, n);
9226 mb_char2bytes(c, p);
9227 mb_char2bytes(c2, p + tl);
9228 }
9229 else
9230#endif
9231 {
9232 c = *p;
9233 *p = p[2];
9234 p[2] = c;
9235 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009236
Bram Moolenaarea424162005-06-16 21:51:00 +00009237 /* Rotate three characters left: "123" -> "231". We change
9238 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9240 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009241 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009243 p = fword + sp->ts_fidx;
9244#ifdef FEAT_MBYTE
9245 if (has_mbyte)
9246 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009247 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009248 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009249 fl = mb_cptr2len(p + n);
9250 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009251 mch_memmove(p, p + n, fl);
9252 mb_char2bytes(c, p + fl);
9253 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9254 }
9255 else
9256#endif
9257 {
9258 c = *p;
9259 *p = p[1];
9260 p[1] = p[2];
9261 p[2] = c;
9262 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9263 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 }
9265 else
9266 sp->ts_state = STATE_REP_INI;
9267 break;
9268
Bram Moolenaarea424162005-06-16 21:51:00 +00009269 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009270 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009271 p = fword + sp->ts_fidx;
9272#ifdef FEAT_MBYTE
9273 if (has_mbyte)
9274 {
9275 n = MB_BYTE2LEN(*p);
9276 n += MB_BYTE2LEN(p[n]);
9277 c = mb_ptr2char(p + n);
9278 tl = MB_BYTE2LEN(p[n]);
9279 mch_memmove(p + tl, p, n);
9280 mb_char2bytes(c, p);
9281 }
9282 else
9283#endif
9284 {
9285 c = p[2];
9286 p[2] = p[1];
9287 p[1] = *p;
9288 *p = c;
9289 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00009292 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9294 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009295 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009297 p = fword + sp->ts_fidx;
9298#ifdef FEAT_MBYTE
9299 if (has_mbyte)
9300 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009301 n = mb_cptr2len(p);
9302 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009303 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009304 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009305 mch_memmove(p + tl, p, n);
9306 mb_char2bytes(c, p);
9307 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
9308 }
9309 else
9310#endif
9311 {
9312 c = p[2];
9313 p[2] = p[1];
9314 p[1] = *p;
9315 *p = c;
9316 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009318 }
9319 else
9320 sp->ts_state = STATE_REP_INI;
9321 break;
9322
Bram Moolenaarea424162005-06-16 21:51:00 +00009323 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009324 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009325 p = fword + sp->ts_fidx;
9326#ifdef FEAT_MBYTE
9327 if (has_mbyte)
9328 {
9329 c = mb_ptr2char(p);
9330 tl = MB_BYTE2LEN(*p);
9331 n = MB_BYTE2LEN(p[tl]);
9332 n += MB_BYTE2LEN(p[tl + n]);
9333 mch_memmove(p, p + tl, n);
9334 mb_char2bytes(c, p + n);
9335 }
9336 else
9337#endif
9338 {
9339 c = *p;
9340 *p = p[1];
9341 p[1] = p[2];
9342 p[2] = c;
9343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 /*FALLTHROUGH*/
9345
9346 case STATE_REP_INI:
9347 /* Check if matching with REP items from the .aff file would
9348 * work. Quickly skip if there are no REP items or the score
9349 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009350 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 if (gap->ga_len == 0
9352 || sp->ts_score + SCORE_REP >= su->su_maxscore)
9353 {
9354 sp->ts_state = STATE_FINAL;
9355 break;
9356 }
9357
9358 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00009359 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009360 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 if (sp->ts_curi < 0)
9362 {
9363 sp->ts_state = STATE_FINAL;
9364 break;
9365 }
9366
9367 sp->ts_state = STATE_REP;
9368 /*FALLTHROUGH*/
9369
9370 case STATE_REP:
9371 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00009372 * match replace the characters and check if the resulting
9373 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 p = fword + sp->ts_fidx;
9375
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009376 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 while (sp->ts_curi < gap->ga_len)
9378 {
9379 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
9380 if (*ftp->ft_from != *p)
9381 {
9382 /* past possible matching entries */
9383 sp->ts_curi = gap->ga_len;
9384 break;
9385 }
9386 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
9387 && try_deeper(su, stack, depth, SCORE_REP))
9388 {
9389 /* Need to undo this afterwards. */
9390 sp->ts_state = STATE_REP_UNDO;
9391
9392 /* Change the "from" to the "to" string. */
9393 ++depth;
9394 fl = STRLEN(ftp->ft_from);
9395 tl = STRLEN(ftp->ft_to);
9396 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009399 repextra += tl - fl;
9400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009401 mch_memmove(p, ftp->ft_to, tl);
9402 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00009403#ifdef FEAT_MBYTE
9404 stack[depth].ts_tcharlen = 0;
9405#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009406 break;
9407 }
9408 }
9409
9410 if (sp->ts_curi >= gap->ga_len)
9411 /* No (more) matches. */
9412 sp->ts_state = STATE_FINAL;
9413
9414 break;
9415
9416 case STATE_REP_UNDO:
9417 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009418 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419 fl = STRLEN(ftp->ft_from);
9420 tl = STRLEN(ftp->ft_to);
9421 p = fword + sp->ts_fidx;
9422 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009424 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009425 repextra -= tl - fl;
9426 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009427 mch_memmove(p, ftp->ft_from, fl);
9428 sp->ts_state = STATE_REP;
9429 break;
9430
9431 default:
9432 /* Did all possible states at this level, go up one level. */
9433 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009435 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009436 {
9437 /* Continue in or go back to the prefix tree. */
9438 byts = pbyts;
9439 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009440 }
9441
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009442 /* Don't check for CTRL-C too often, it takes time. */
9443 line_breakcheck();
9444 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009445 }
9446 }
9447}
9448
9449/*
9450 * Try going one level deeper in the tree.
9451 */
9452 static int
9453try_deeper(su, stack, depth, score_add)
9454 suginfo_T *su;
9455 trystate_T *stack;
9456 int depth;
9457 int score_add;
9458{
9459 int newscore;
9460
9461 /* Refuse to go deeper if the scrore is getting too big. */
9462 newscore = stack[depth].ts_score + score_add;
9463 if (newscore >= su->su_maxscore)
9464 return FALSE;
9465
Bram Moolenaarea424162005-06-16 21:51:00 +00009466 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 stack[depth + 1].ts_state = STATE_START;
9468 stack[depth + 1].ts_score = newscore;
9469 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009470 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471 return TRUE;
9472}
9473
Bram Moolenaar53805d12005-08-01 07:08:33 +00009474#ifdef FEAT_MBYTE
9475/*
9476 * Case-folding may change the number of bytes: Count nr of chars in
9477 * fword[flen] and return the byte length of that many chars in "word".
9478 */
9479 static int
9480nofold_len(fword, flen, word)
9481 char_u *fword;
9482 int flen;
9483 char_u *word;
9484{
9485 char_u *p;
9486 int i = 0;
9487
9488 for (p = fword; p < fword + flen; mb_ptr_adv(p))
9489 ++i;
9490 for (p = word; i > 0; mb_ptr_adv(p))
9491 --i;
9492 return (int)(p - word);
9493}
9494#endif
9495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496/*
9497 * "fword" is a good word with case folded. Find the matching keep-case
9498 * words and put it in "kword".
9499 * Theoretically there could be several keep-case words that result in the
9500 * same case-folded word, but we only find one...
9501 */
9502 static void
9503find_keepcap_word(slang, fword, kword)
9504 slang_T *slang;
9505 char_u *fword;
9506 char_u *kword;
9507{
9508 char_u uword[MAXWLEN]; /* "fword" in upper-case */
9509 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009510 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009511
9512 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009513 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 int round[MAXWLEN];
9515 int fwordidx[MAXWLEN];
9516 int uwordidx[MAXWLEN];
9517 int kwordlen[MAXWLEN];
9518
9519 int flen, ulen;
9520 int l;
9521 int len;
9522 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009523 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 char_u *p;
9525 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009526 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527
9528 if (byts == NULL)
9529 {
9530 /* array is empty: "cannot happen" */
9531 *kword = NUL;
9532 return;
9533 }
9534
9535 /* Make an all-cap version of "fword". */
9536 allcap_copy(fword, uword);
9537
9538 /*
9539 * Each character needs to be tried both case-folded and upper-case.
9540 * All this gets very complicated if we keep in mind that changing case
9541 * may change the byte length of a multi-byte character...
9542 */
9543 depth = 0;
9544 arridx[0] = 0;
9545 round[0] = 0;
9546 fwordidx[0] = 0;
9547 uwordidx[0] = 0;
9548 kwordlen[0] = 0;
9549 while (depth >= 0)
9550 {
9551 if (fword[fwordidx[depth]] == NUL)
9552 {
9553 /* We are at the end of "fword". If the tree allows a word to end
9554 * here we have found a match. */
9555 if (byts[arridx[depth] + 1] == 0)
9556 {
9557 kword[kwordlen[depth]] = NUL;
9558 return;
9559 }
9560
9561 /* kword is getting too long, continue one level up */
9562 --depth;
9563 }
9564 else if (++round[depth] > 2)
9565 {
9566 /* tried both fold-case and upper-case character, continue one
9567 * level up */
9568 --depth;
9569 }
9570 else
9571 {
9572 /*
9573 * round[depth] == 1: Try using the folded-case character.
9574 * round[depth] == 2: Try using the upper-case character.
9575 */
9576#ifdef FEAT_MBYTE
9577 if (has_mbyte)
9578 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009579 flen = mb_cptr2len(fword + fwordidx[depth]);
9580 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 }
9582 else
9583#endif
9584 ulen = flen = 1;
9585 if (round[depth] == 1)
9586 {
9587 p = fword + fwordidx[depth];
9588 l = flen;
9589 }
9590 else
9591 {
9592 p = uword + uwordidx[depth];
9593 l = ulen;
9594 }
9595
9596 for (tryidx = arridx[depth]; l > 0; --l)
9597 {
9598 /* Perform a binary search in the list of accepted bytes. */
9599 len = byts[tryidx++];
9600 c = *p++;
9601 lo = tryidx;
9602 hi = tryidx + len - 1;
9603 while (lo < hi)
9604 {
9605 m = (lo + hi) / 2;
9606 if (byts[m] > c)
9607 hi = m - 1;
9608 else if (byts[m] < c)
9609 lo = m + 1;
9610 else
9611 {
9612 lo = hi = m;
9613 break;
9614 }
9615 }
9616
9617 /* Stop if there is no matching byte. */
9618 if (hi < lo || byts[lo] != c)
9619 break;
9620
9621 /* Continue at the child (if there is one). */
9622 tryidx = idxs[lo];
9623 }
9624
9625 if (l == 0)
9626 {
9627 /*
9628 * Found the matching char. Copy it to "kword" and go a
9629 * level deeper.
9630 */
9631 if (round[depth] == 1)
9632 {
9633 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
9634 flen);
9635 kwordlen[depth + 1] = kwordlen[depth] + flen;
9636 }
9637 else
9638 {
9639 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
9640 ulen);
9641 kwordlen[depth + 1] = kwordlen[depth] + ulen;
9642 }
9643 fwordidx[depth + 1] = fwordidx[depth] + flen;
9644 uwordidx[depth + 1] = uwordidx[depth] + ulen;
9645
9646 ++depth;
9647 arridx[depth] = tryidx;
9648 round[depth] = 0;
9649 }
9650 }
9651 }
9652
9653 /* Didn't find it: "cannot happen". */
9654 *kword = NUL;
9655}
9656
9657/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009658 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
9659 * su->su_sga.
9660 */
9661 static void
9662score_comp_sal(su)
9663 suginfo_T *su;
9664{
9665 langp_T *lp;
9666 char_u badsound[MAXWLEN];
9667 int i;
9668 suggest_T *stp;
9669 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009670 int score;
9671
9672 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
9673 return;
9674
9675 /* Use the sound-folding of the first language that supports it. */
9676 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9677 lp->lp_slang != NULL; ++lp)
9678 if (lp->lp_slang->sl_sal.ga_len > 0)
9679 {
9680 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009681 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009682
9683 for (i = 0; i < su->su_ga.ga_len; ++i)
9684 {
9685 stp = &SUG(su->su_ga, i);
9686
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009687 /* Case-fold the suggested word, sound-fold it and compute the
9688 * sound-a-like score. */
9689 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009690 if (score < SCORE_MAXMAX)
9691 {
9692 /* Add the suggestion. */
9693 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
9694 sstp->st_word = vim_strsave(stp->st_word);
9695 if (sstp->st_word != NULL)
9696 {
9697 sstp->st_score = score;
9698 sstp->st_altscore = 0;
9699 sstp->st_orglen = stp->st_orglen;
9700 ++su->su_sga.ga_len;
9701 }
9702 }
9703 }
9704 break;
9705 }
9706}
9707
9708/*
9709 * Combine the list of suggestions in su->su_ga and su->su_sga.
9710 * They are intwined.
9711 */
9712 static void
9713score_combine(su)
9714 suginfo_T *su;
9715{
9716 int i;
9717 int j;
9718 garray_T ga;
9719 garray_T *gap;
9720 langp_T *lp;
9721 suggest_T *stp;
9722 char_u *p;
9723 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009724 int round;
9725
9726 /* Add the alternate score to su_ga. */
9727 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9728 lp->lp_slang != NULL; ++lp)
9729 {
9730 if (lp->lp_slang->sl_sal.ga_len > 0)
9731 {
9732 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009733 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009734
9735 for (i = 0; i < su->su_ga.ga_len; ++i)
9736 {
9737 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009738 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
9739 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009740 if (stp->st_altscore == SCORE_MAXMAX)
9741 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
9742 else
9743 stp->st_score = (stp->st_score * 3
9744 + stp->st_altscore) / 4;
9745 stp->st_salscore = FALSE;
9746 }
9747 break;
9748 }
9749 }
9750
9751 /* Add the alternate score to su_sga. */
9752 for (i = 0; i < su->su_sga.ga_len; ++i)
9753 {
9754 stp = &SUG(su->su_sga, i);
9755 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
9756 if (stp->st_score == SCORE_MAXMAX)
9757 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
9758 else
9759 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
9760 stp->st_salscore = TRUE;
9761 }
9762
9763 /* Sort the suggestions and truncate at "maxcount" for both lists. */
9764 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9765 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
9766
9767 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
9768 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
9769 return;
9770
9771 stp = &SUG(ga, 0);
9772 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
9773 {
9774 /* round 1: get a suggestion from su_ga
9775 * round 2: get a suggestion from su_sga */
9776 for (round = 1; round <= 2; ++round)
9777 {
9778 gap = round == 1 ? &su->su_ga : &su->su_sga;
9779 if (i < gap->ga_len)
9780 {
9781 /* Don't add a word if it's already there. */
9782 p = SUG(*gap, i).st_word;
9783 for (j = 0; j < ga.ga_len; ++j)
9784 if (STRCMP(stp[j].st_word, p) == 0)
9785 break;
9786 if (j == ga.ga_len)
9787 stp[ga.ga_len++] = SUG(*gap, i);
9788 else
9789 vim_free(p);
9790 }
9791 }
9792 }
9793
9794 ga_clear(&su->su_ga);
9795 ga_clear(&su->su_sga);
9796
9797 /* Truncate the list to the number of suggestions that will be displayed. */
9798 if (ga.ga_len > su->su_maxcount)
9799 {
9800 for (i = su->su_maxcount; i < ga.ga_len; ++i)
9801 vim_free(stp[i].st_word);
9802 ga.ga_len = su->su_maxcount;
9803 }
9804
9805 su->su_ga = ga;
9806}
9807
9808/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009809 * For the goodword in "stp" compute the soundalike score compared to the
9810 * badword.
9811 */
9812 static int
9813stp_sal_score(stp, su, slang, badsound)
9814 suggest_T *stp;
9815 suginfo_T *su;
9816 slang_T *slang;
9817 char_u *badsound; /* sound-folded badword */
9818{
9819 char_u *p;
9820 char_u badsound2[MAXWLEN];
9821 char_u fword[MAXWLEN];
9822 char_u goodsound[MAXWLEN];
9823
9824 if (stp->st_orglen <= su->su_badlen)
9825 p = badsound;
9826 else
9827 {
9828 /* soundfold the bad word with more characters following */
9829 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
9830
9831 /* When joining two words the sound often changes a lot. E.g., "t he"
9832 * sounds like "t h" while "the" sounds like "@". Avoid that by
9833 * removing the space. Don't do it when the good word also contains a
9834 * space. */
9835 if (vim_iswhite(su->su_badptr[su->su_badlen])
9836 && *skiptowhite(stp->st_word) == NUL)
9837 for (p = fword; *(p = skiptowhite(p)) != NUL; )
9838 mch_memmove(p, p + 1, STRLEN(p));
9839
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009840 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009841 p = badsound2;
9842 }
9843
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009844 /* Sound-fold the word and compute the score for the difference. */
9845 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009846
9847 return soundalike_score(goodsound, p);
9848}
9849
9850/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009851 * Find suggestions by comparing the word in a sound-a-like form.
9852 */
9853 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009854suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009855 suginfo_T *su;
9856{
9857 char_u salword[MAXWLEN];
9858 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009860 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 int curi[MAXWLEN];
9862 langp_T *lp;
9863 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009864 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009865 int depth;
9866 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009867 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009868 int round;
9869 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009870 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009871 int local_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009872
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009873 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9875 lp->lp_slang != NULL; ++lp)
9876 {
9877 if (lp->lp_slang->sl_sal.ga_len > 0)
9878 {
9879 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009880 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881
9882 /*
9883 * Go through the whole tree, soundfold each word and compare.
9884 * round 1: use the case-folded tree.
9885 * round 2: use the keep-case tree.
9886 */
9887 for (round = 1; round <= 2; ++round)
9888 {
9889 if (round == 1)
9890 {
9891 byts = lp->lp_slang->sl_fbyts;
9892 idxs = lp->lp_slang->sl_fidxs;
9893 }
9894 else
9895 {
9896 byts = lp->lp_slang->sl_kbyts;
9897 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009898 if (byts == NULL) /* no keep-case words */
9899 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 }
9901
9902 depth = 0;
9903 arridx[0] = 0;
9904 curi[0] = 1;
9905 while (depth >= 0 && !got_int)
9906 {
9907 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 /* Done all bytes at this node, go up one level. */
9910 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009911 line_breakcheck();
9912 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 else
9914 {
9915 /* Do one more byte at this node. */
9916 n = arridx[depth] + curi[depth];
9917 ++curi[depth];
9918 c = byts[n];
9919 if (c == 0)
9920 {
9921 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009922 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 if (round == 2 || (flags & WF_KEEPCAP) == 0)
9924 {
9925 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009926 /* Sound-fold. Only in keep-case tree need to
9927 * case-fold the word. */
9928 spell_soundfold(lp->lp_slang, tword,
9929 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009930
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009931 /* Compute the edit distance between the
9932 * sound-a-like words. */
9933 sound_score = soundalike_score(salword,
9934 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009935
9936 /* Add a penalty for words in another region. */
9937 if ((flags & WF_REGION) && (((unsigned)flags
9938 >> 16) & lp->lp_region) == 0)
9939 local_score = SCORE_REGION;
9940 else
9941 local_score = 0;
9942 sound_score += local_score;
9943
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009944 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009946 char_u cword[MAXWLEN];
9947 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009948 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009949
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009950 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009951 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009953 /* Need to fix case according to
9954 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009956 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 }
9958 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009959 p = tword;
9960
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009961 if (sps_flags & SPS_DOUBLE)
9962 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009963 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009964 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009965 else
9966 {
9967 /* Compute the score. */
9968 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +00009969 su->su_badword, p)
9970 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009971 if (sps_flags & SPS_BEST)
9972 /* give a bonus for the good word
9973 * sounding the same as the bad
9974 * word */
9975 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009976 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009977 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009978 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009979 else
9980 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009981 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009982 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009983 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 }
9985 }
9986
9987 /* Skip over other NUL bytes. */
9988 while (byts[n + 1] == 0)
9989 {
9990 ++n;
9991 ++curi[depth];
9992 }
9993 }
9994 else
9995 {
9996 /* Normal char, go one level deeper. */
9997 tword[depth++] = c;
9998 arridx[depth] = idxs[n];
9999 curi[depth] = 1;
10000 }
10001 }
10002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010003 }
10004 }
10005 }
10006}
10007
10008/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010009 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 */
10011 static void
10012make_case_word(fword, cword, flags)
10013 char_u *fword;
10014 char_u *cword;
10015 int flags;
10016{
10017 if (flags & WF_ALLCAP)
10018 /* Make it all upper-case */
10019 allcap_copy(fword, cword);
10020 else if (flags & WF_ONECAP)
10021 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010022 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010023 else
10024 /* Use goodword as-is. */
10025 STRCPY(cword, fword);
10026}
10027
Bram Moolenaarea424162005-06-16 21:51:00 +000010028/*
10029 * Use map string "map" for languages "lp".
10030 */
10031 static void
10032set_map_str(lp, map)
10033 slang_T *lp;
10034 char_u *map;
10035{
10036 char_u *p;
10037 int headc = 0;
10038 int c;
10039 int i;
10040
10041 if (*map == NUL)
10042 {
10043 lp->sl_has_map = FALSE;
10044 return;
10045 }
10046 lp->sl_has_map = TRUE;
10047
10048 /* Init the array and hash table empty. */
10049 for (i = 0; i < 256; ++i)
10050 lp->sl_map_array[i] = 0;
10051#ifdef FEAT_MBYTE
10052 hash_init(&lp->sl_map_hash);
10053#endif
10054
10055 /*
10056 * The similar characters are stored separated with slashes:
10057 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10058 * before the same slash. For characters above 255 sl_map_hash is used.
10059 */
10060 for (p = map; *p != NUL; )
10061 {
10062#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010063 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010064#else
10065 c = *p++;
10066#endif
10067 if (c == '/')
10068 headc = 0;
10069 else
10070 {
10071 if (headc == 0)
10072 headc = c;
10073
10074#ifdef FEAT_MBYTE
10075 /* Characters above 255 don't fit in sl_map_array[], put them in
10076 * the hash table. Each entry is the char, a NUL the headchar and
10077 * a NUL. */
10078 if (c >= 256)
10079 {
10080 int cl = mb_char2len(c);
10081 int headcl = mb_char2len(headc);
10082 char_u *b;
10083 hash_T hash;
10084 hashitem_T *hi;
10085
10086 b = alloc((unsigned)(cl + headcl + 2));
10087 if (b == NULL)
10088 return;
10089 mb_char2bytes(c, b);
10090 b[cl] = NUL;
10091 mb_char2bytes(headc, b + cl + 1);
10092 b[cl + 1 + headcl] = NUL;
10093 hash = hash_hash(b);
10094 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10095 if (HASHITEM_EMPTY(hi))
10096 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10097 else
10098 {
10099 /* This should have been checked when generating the .spl
10100 * file. */
10101 EMSG(_("E999: duplicate char in MAP entry"));
10102 vim_free(b);
10103 }
10104 }
10105 else
10106#endif
10107 lp->sl_map_array[c] = headc;
10108 }
10109 }
10110}
10111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010112/*
10113 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10114 * lines in the .aff file.
10115 */
10116 static int
10117similar_chars(slang, c1, c2)
10118 slang_T *slang;
10119 int c1;
10120 int c2;
10121{
Bram Moolenaarea424162005-06-16 21:51:00 +000010122 int m1, m2;
10123#ifdef FEAT_MBYTE
10124 char_u buf[MB_MAXBYTES];
10125 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010126
Bram Moolenaarea424162005-06-16 21:51:00 +000010127 if (c1 >= 256)
10128 {
10129 buf[mb_char2bytes(c1, buf)] = 0;
10130 hi = hash_find(&slang->sl_map_hash, buf);
10131 if (HASHITEM_EMPTY(hi))
10132 m1 = 0;
10133 else
10134 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10135 }
10136 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010137#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010138 m1 = slang->sl_map_array[c1];
10139 if (m1 == 0)
10140 return FALSE;
10141
10142
10143#ifdef FEAT_MBYTE
10144 if (c2 >= 256)
10145 {
10146 buf[mb_char2bytes(c2, buf)] = 0;
10147 hi = hash_find(&slang->sl_map_hash, buf);
10148 if (HASHITEM_EMPTY(hi))
10149 m2 = 0;
10150 else
10151 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10152 }
10153 else
10154#endif
10155 m2 = slang->sl_map_array[c2];
10156
10157 return m1 == m2;
10158}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159
10160/*
10161 * Add a suggestion to the list of suggestions.
10162 * Do not add a duplicate suggestion or suggestions with a bad score.
10163 * When "use_score" is not zero it's used, otherwise the score is computed
10164 * with spell_edit_score().
10165 */
10166 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010167add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010169 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010170 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010171 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010172 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010173 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010174 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175{
10176 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010178 char_u *p = NULL;
10179 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010180
10181 /* Check that the word wasn't banned. */
10182 if (was_banned(su, goodword))
10183 return;
10184
Bram Moolenaar0c405862005-06-22 22:26:26 +000010185 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10186 * Remove the common part from "goodword". */
10187 i = badlen - su->su_badlen;
10188 if (i > 0)
10189 {
10190 /* This assumes there was no case folding or it didn't change the
10191 * length... */
10192 p = goodword + STRLEN(goodword) - i;
10193 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10194 {
10195 badlen = su->su_badlen;
10196 c = *p;
10197 *p = NUL;
10198 }
10199 else
10200 p = NULL;
10201 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010202 else if (i < 0)
10203 {
10204 /* When replacing part of the word check that we actually change
10205 * something. For "the the" a suggestion can be replacing the first
10206 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010207 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010208 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10209 return;
10210 }
10211
Bram Moolenaar0c405862005-06-22 22:26:26 +000010212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 if (score <= su->su_maxscore)
10214 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010215 /* Check if the word is already there. Also check the length that is
10216 * being replaced "thes," -> "these" is a different suggestion from
10217 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010218 stp = &SUG(*gap, 0);
10219 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010220 if (STRCMP(stp[i].st_word, goodword) == 0
10221 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 {
10223 /* Found it. Remember the lowest score. */
10224 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010227 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010228 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 break;
10231 }
10232
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010233 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 {
10235 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010236 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 stp->st_word = vim_strsave(goodword);
10238 if (stp->st_word != NULL)
10239 {
10240 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010241 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010242 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010243 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010244 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245
10246 /* If we have too many suggestions now, sort the list and keep
10247 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010248 if (gap->ga_len > SUG_MAX_COUNT(su))
10249 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10250 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 }
10252 }
10253 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000010254
10255 if (p != NULL)
10256 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257}
10258
10259/*
10260 * Add a word to be banned.
10261 */
10262 static void
10263add_banned(su, word)
10264 suginfo_T *su;
10265 char_u *word;
10266{
10267 char_u *s = vim_strsave(word);
10268 hash_T hash;
10269 hashitem_T *hi;
10270
10271 if (s != NULL)
10272 {
10273 hash = hash_hash(s);
10274 hi = hash_lookup(&su->su_banned, s, hash);
10275 if (HASHITEM_EMPTY(hi))
10276 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010277 else
10278 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 }
10280}
10281
10282/*
10283 * Return TRUE if a word appears in the list of banned words.
10284 */
10285 static int
10286was_banned(su, word)
10287 suginfo_T *su;
10288 char_u *word;
10289{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010290 hashitem_T *hi = hash_find(&su->su_banned, word);
10291
10292 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293}
10294
10295/*
10296 * Free the banned words in "su".
10297 */
10298 static void
10299free_banned(su)
10300 suginfo_T *su;
10301{
10302 int todo;
10303 hashitem_T *hi;
10304
10305 todo = su->su_banned.ht_used;
10306 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
10307 {
10308 if (!HASHITEM_EMPTY(hi))
10309 {
10310 vim_free(hi->hi_key);
10311 --todo;
10312 }
10313 }
10314 hash_clear(&su->su_banned);
10315}
10316
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010317/*
10318 * Recompute the score if sound-folding is possible. This is slow,
10319 * thus only done for the final results.
10320 */
10321 static void
10322rescore_suggestions(su)
10323 suginfo_T *su;
10324{
10325 langp_T *lp;
10326 suggest_T *stp;
10327 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010328 int i;
10329
10330 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10331 lp->lp_slang != NULL; ++lp)
10332 {
10333 if (lp->lp_slang->sl_sal.ga_len > 0)
10334 {
10335 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010336 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010337
10338 for (i = 0; i < su->su_ga.ga_len; ++i)
10339 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010340 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010341 if (!stp->st_had_bonus)
10342 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010343 stp->st_altscore = stp_sal_score(stp, su,
10344 lp->lp_slang, sal_badword);
10345 if (stp->st_altscore == SCORE_MAXMAX)
10346 stp->st_altscore = SCORE_BIG;
10347 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010348 }
10349 }
10350 break;
10351 }
10352 }
10353}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010355static int
10356#ifdef __BORLANDC__
10357_RTLENTRYF
10358#endif
10359sug_compare __ARGS((const void *s1, const void *s2));
10360
10361/*
10362 * Function given to qsort() to sort the suggestions on st_score.
10363 */
10364 static int
10365#ifdef __BORLANDC__
10366_RTLENTRYF
10367#endif
10368sug_compare(s1, s2)
10369 const void *s1;
10370 const void *s2;
10371{
10372 suggest_T *p1 = (suggest_T *)s1;
10373 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010374 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010375
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010376 if (n == 0)
10377 return p1->st_altscore - p2->st_altscore;
10378 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010379}
10380
10381/*
10382 * Cleanup the suggestions:
10383 * - Sort on score.
10384 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010385 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010387 static int
10388cleanup_suggestions(gap, maxscore, keep)
10389 garray_T *gap;
10390 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010391 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010392{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010393 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010394 int i;
10395
10396 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010397 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398
10399 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010400 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010402 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010403 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010404 gap->ga_len = keep;
10405 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010406 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010407 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408}
10409
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010410#if defined(FEAT_EVAL) || defined(PROTO)
10411/*
10412 * Soundfold a string, for soundfold().
10413 * Result is in allocated memory, NULL for an error.
10414 */
10415 char_u *
10416eval_soundfold(word)
10417 char_u *word;
10418{
10419 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010420 char_u sound[MAXWLEN];
10421
10422 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
10423 /* Use the sound-folding of the first language that supports it. */
10424 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10425 lp->lp_slang != NULL; ++lp)
10426 if (lp->lp_slang->sl_sal.ga_len > 0)
10427 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010428 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010429 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010430 return vim_strsave(sound);
10431 }
10432
10433 /* No language with sound folding, return word as-is. */
10434 return vim_strsave(word);
10435}
10436#endif
10437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438/*
10439 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000010440 *
10441 * There are many ways to turn a word into a sound-a-like representation. The
10442 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
10443 * swedish name matching - survey and test of different algorithms" by Klas
10444 * Erikson.
10445 *
10446 * We support two methods:
10447 * 1. SOFOFROM/SOFOTO do a simple character mapping.
10448 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 */
10450 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010451spell_soundfold(slang, inword, folded, res)
10452 slang_T *slang;
10453 char_u *inword;
10454 int folded; /* "inword" is already case-folded */
10455 char_u *res;
10456{
10457 char_u fword[MAXWLEN];
10458 char_u *word;
10459
10460 if (slang->sl_sofo)
10461 /* SOFOFROM and SOFOTO used */
10462 spell_soundfold_sofo(slang, inword, res);
10463 else
10464 {
10465 /* SAL items used. Requires the word to be case-folded. */
10466 if (folded)
10467 word = inword;
10468 else
10469 {
10470 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
10471 word = fword;
10472 }
10473
10474#ifdef FEAT_MBYTE
10475 if (has_mbyte)
10476 spell_soundfold_wsal(slang, word, res);
10477 else
10478#endif
10479 spell_soundfold_sal(slang, word, res);
10480 }
10481}
10482
10483/*
10484 * Perform sound folding of "inword" into "res" according to SOFOFROM and
10485 * SOFOTO lines.
10486 */
10487 static void
10488spell_soundfold_sofo(slang, inword, res)
10489 slang_T *slang;
10490 char_u *inword;
10491 char_u *res;
10492{
10493 char_u *s;
10494 int ri = 0;
10495 int c;
10496
10497#ifdef FEAT_MBYTE
10498 if (has_mbyte)
10499 {
10500 int prevc = 0;
10501 int *ip;
10502
10503 /* The sl_sal_first[] table contains the translation for chars up to
10504 * 255, sl_sal the rest. */
10505 for (s = inword; *s != NUL; )
10506 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010507 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010508 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10509 c = ' ';
10510 else if (c < 256)
10511 c = slang->sl_sal_first[c];
10512 else
10513 {
10514 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
10515 if (ip == NULL) /* empty list, can't match */
10516 c = NUL;
10517 else
10518 for (;;) /* find "c" in the list */
10519 {
10520 if (*ip == 0) /* not found */
10521 {
10522 c = NUL;
10523 break;
10524 }
10525 if (*ip == c) /* match! */
10526 {
10527 c = ip[1];
10528 break;
10529 }
10530 ip += 2;
10531 }
10532 }
10533
10534 if (c != NUL && c != prevc)
10535 {
10536 ri += mb_char2bytes(c, res + ri);
10537 if (ri + MB_MAXBYTES > MAXWLEN)
10538 break;
10539 prevc = c;
10540 }
10541 }
10542 }
10543 else
10544#endif
10545 {
10546 /* The sl_sal_first[] table contains the translation. */
10547 for (s = inword; (c = *s) != NUL; ++s)
10548 {
10549 if (vim_iswhite(c))
10550 c = ' ';
10551 else
10552 c = slang->sl_sal_first[c];
10553 if (c != NUL && (ri == 0 || res[ri - 1] != c))
10554 res[ri++] = c;
10555 }
10556 }
10557
10558 res[ri] = NUL;
10559}
10560
10561 static void
10562spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 slang_T *slang;
10564 char_u *inword;
10565 char_u *res;
10566{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010567 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010569 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010571 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010573 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 int n, k = 0;
10575 int z0;
10576 int k0;
10577 int n0;
10578 int c;
10579 int pri;
10580 int p0 = -333;
10581 int c0;
10582
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010583 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010584 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 if (slang->sl_rem_accents)
10586 {
10587 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010588 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010590 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010591 {
10592 *t++ = ' ';
10593 s = skipwhite(s);
10594 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010595 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010596 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010597 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 *t++ = *s;
10599 ++s;
10600 }
10601 }
10602 *t = NUL;
10603 }
10604 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010605 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010607 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608
10609 /*
10610 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010611 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010613 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 while ((c = word[i]) != NUL)
10615 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010616 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 n = slang->sl_sal_first[c];
10618 z0 = 0;
10619
10620 if (n >= 0)
10621 {
10622 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010623 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010625 /* Quickly skip entries that don't match the word. Most
10626 * entries are less then three chars, optimize for that. */
10627 k = smp[n].sm_leadlen;
10628 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010630 if (word[i + 1] != s[1])
10631 continue;
10632 if (k > 2)
10633 {
10634 for (j = 2; j < k; ++j)
10635 if (word[i + j] != s[j])
10636 break;
10637 if (j < k)
10638 continue;
10639 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 }
10641
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010642 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010644 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010645 while (*pf != NUL && *pf != word[i + k])
10646 ++pf;
10647 if (*pf == NUL)
10648 continue;
10649 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010651 s = smp[n].sm_rules;
10652 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653
10654 p0 = *s;
10655 k0 = k;
10656 while (*s == '-' && k > 1)
10657 {
10658 k--;
10659 s++;
10660 }
10661 if (*s == '<')
10662 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010663 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010664 {
10665 /* determine priority */
10666 pri = *s - '0';
10667 s++;
10668 }
10669 if (*s == '^' && *(s + 1) == '^')
10670 s++;
10671
10672 if (*s == NUL
10673 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010674 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010675 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010676 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010677 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010678 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010679 && spell_iswordp(word + i - 1, curbuf)
10680 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 {
10682 /* search for followup rules, if: */
10683 /* followup and k > 1 and NO '-' in searchstring */
10684 c0 = word[i + k - 1];
10685 n0 = slang->sl_sal_first[c0];
10686
10687 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010688 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 {
10690 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010691 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010693 /* Quickly skip entries that don't match the word.
10694 * */
10695 k0 = smp[n0].sm_leadlen;
10696 if (k0 > 1)
10697 {
10698 if (word[i + k] != s[1])
10699 continue;
10700 if (k0 > 2)
10701 {
10702 pf = word + i + k + 1;
10703 for (j = 2; j < k0; ++j)
10704 if (*pf++ != s[j])
10705 break;
10706 if (j < k0)
10707 continue;
10708 }
10709 }
10710 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010712 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010713 {
10714 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010715 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010716 while (*pf != NUL && *pf != word[i + k0])
10717 ++pf;
10718 if (*pf == NUL)
10719 continue;
10720 ++k0;
10721 }
10722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010724 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 while (*s == '-')
10726 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010727 /* "k0" gets NOT reduced because
10728 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 s++;
10730 }
10731 if (*s == '<')
10732 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010733 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 {
10735 p0 = *s - '0';
10736 s++;
10737 }
10738
10739 if (*s == NUL
10740 /* *s == '^' cuts */
10741 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010742 && !spell_iswordp(word + i + k0,
10743 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 {
10745 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010747 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010748
10749 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 /* rule fits; stop search */
10753 break;
10754 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 }
10756
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010757 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010758 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 }
10760
10761 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010762 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010763 if (s == NULL)
10764 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010765 pf = smp[n].sm_rules;
10766 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010767 if (p0 == 1 && z == 0)
10768 {
10769 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010770 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
10771 || res[reslen - 1] == *s))
10772 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 z0 = 1;
10774 z = 1;
10775 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010776 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 {
10778 word[i + k0] = *s;
10779 k0++;
10780 s++;
10781 }
10782 if (k > k0)
10783 mch_memmove(word + i + k0, word + i + k,
10784 STRLEN(word + i + k) + 1);
10785
10786 /* new "actual letter" */
10787 c = word[i];
10788 }
10789 else
10790 {
10791 /* no '<' rule used */
10792 i += k - 1;
10793 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010794 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010795 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010796 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010797 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 s++;
10799 }
10800 /* new "actual letter" */
10801 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010802 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 {
10804 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010805 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 mch_memmove(word, word + i + 1,
10807 STRLEN(word + i + 1) + 1);
10808 i = 0;
10809 z0 = 1;
10810 }
10811 }
10812 break;
10813 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 }
10815 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010816 else if (vim_iswhite(c))
10817 {
10818 c = ' ';
10819 k = 1;
10820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821
10822 if (z0 == 0)
10823 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010824 if (k && !p0 && reslen < MAXWLEN && c != NUL
10825 && (!slang->sl_collapse || reslen == 0
10826 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010828 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010829
10830 i++;
10831 z = 0;
10832 k = 0;
10833 }
10834 }
10835
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010836 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837}
10838
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010839#ifdef FEAT_MBYTE
10840/*
10841 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
10842 * Multi-byte version of spell_soundfold().
10843 */
10844 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010845spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010846 slang_T *slang;
10847 char_u *inword;
10848 char_u *res;
10849{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010850 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010851 int word[MAXWLEN];
10852 int wres[MAXWLEN];
10853 int l;
10854 char_u *s;
10855 int *ws;
10856 char_u *t;
10857 int *pf;
10858 int i, j, z;
10859 int reslen;
10860 int n, k = 0;
10861 int z0;
10862 int k0;
10863 int n0;
10864 int c;
10865 int pri;
10866 int p0 = -333;
10867 int c0;
10868 int did_white = FALSE;
10869
10870 /*
10871 * Convert the multi-byte string to a wide-character string.
10872 * Remove accents, if wanted. We actually remove all non-word characters.
10873 * But keep white space.
10874 */
10875 n = 0;
10876 for (s = inword; *s != NUL; )
10877 {
10878 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010879 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010880 if (slang->sl_rem_accents)
10881 {
10882 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10883 {
10884 if (did_white)
10885 continue;
10886 c = ' ';
10887 did_white = TRUE;
10888 }
10889 else
10890 {
10891 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010892 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010893 continue;
10894 }
10895 }
10896 word[n++] = c;
10897 }
10898 word[n] = NUL;
10899
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010900 /*
10901 * This comes from Aspell phonet.cpp.
10902 * Converted from C++ to C. Added support for multi-byte chars.
10903 * Changed to keep spaces.
10904 */
10905 i = reslen = z = 0;
10906 while ((c = word[i]) != NUL)
10907 {
10908 /* Start with the first rule that has the character in the word. */
10909 n = slang->sl_sal_first[c & 0xff];
10910 z0 = 0;
10911
10912 if (n >= 0)
10913 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010914 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010915 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
10916 {
10917 /* Quickly skip entries that don't match the word. Most
10918 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010919 if (c != ws[0])
10920 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010921 k = smp[n].sm_leadlen;
10922 if (k > 1)
10923 {
10924 if (word[i + 1] != ws[1])
10925 continue;
10926 if (k > 2)
10927 {
10928 for (j = 2; j < k; ++j)
10929 if (word[i + j] != ws[j])
10930 break;
10931 if (j < k)
10932 continue;
10933 }
10934 }
10935
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010936 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010937 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010938 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010939 while (*pf != NUL && *pf != word[i + k])
10940 ++pf;
10941 if (*pf == NUL)
10942 continue;
10943 ++k;
10944 }
10945 s = smp[n].sm_rules;
10946 pri = 5; /* default priority */
10947
10948 p0 = *s;
10949 k0 = k;
10950 while (*s == '-' && k > 1)
10951 {
10952 k--;
10953 s++;
10954 }
10955 if (*s == '<')
10956 s++;
10957 if (VIM_ISDIGIT(*s))
10958 {
10959 /* determine priority */
10960 pri = *s - '0';
10961 s++;
10962 }
10963 if (*s == '^' && *(s + 1) == '^')
10964 s++;
10965
10966 if (*s == NUL
10967 || (*s == '^'
10968 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010969 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010970 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010971 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010972 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010973 && spell_iswordp_w(word + i - 1, curbuf)
10974 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010975 {
10976 /* search for followup rules, if: */
10977 /* followup and k > 1 and NO '-' in searchstring */
10978 c0 = word[i + k - 1];
10979 n0 = slang->sl_sal_first[c0 & 0xff];
10980
10981 if (slang->sl_followup && k > 1 && n0 >= 0
10982 && p0 != '-' && word[i + k] != NUL)
10983 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010984 /* Test follow-up rule for "word[i + k]"; loop over
10985 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010986 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
10987 == (c0 & 0xff); ++n0)
10988 {
10989 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010990 */
10991 if (c0 != ws[0])
10992 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010993 k0 = smp[n0].sm_leadlen;
10994 if (k0 > 1)
10995 {
10996 if (word[i + k] != ws[1])
10997 continue;
10998 if (k0 > 2)
10999 {
11000 pf = word + i + k + 1;
11001 for (j = 2; j < k0; ++j)
11002 if (*pf++ != ws[j])
11003 break;
11004 if (j < k0)
11005 continue;
11006 }
11007 }
11008 k0 += k - 1;
11009
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011010 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011011 {
11012 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011013 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011014 while (*pf != NUL && *pf != word[i + k0])
11015 ++pf;
11016 if (*pf == NUL)
11017 continue;
11018 ++k0;
11019 }
11020
11021 p0 = 5;
11022 s = smp[n0].sm_rules;
11023 while (*s == '-')
11024 {
11025 /* "k0" gets NOT reduced because
11026 * "if (k0 == k)" */
11027 s++;
11028 }
11029 if (*s == '<')
11030 s++;
11031 if (VIM_ISDIGIT(*s))
11032 {
11033 p0 = *s - '0';
11034 s++;
11035 }
11036
11037 if (*s == NUL
11038 /* *s == '^' cuts */
11039 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011040 && !spell_iswordp_w(word + i + k0,
11041 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011042 {
11043 if (k0 == k)
11044 /* this is just a piece of the string */
11045 continue;
11046
11047 if (p0 < pri)
11048 /* priority too low */
11049 continue;
11050 /* rule fits; stop search */
11051 break;
11052 }
11053 }
11054
11055 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11056 == (c0 & 0xff))
11057 continue;
11058 }
11059
11060 /* replace string */
11061 ws = smp[n].sm_to_w;
11062 s = smp[n].sm_rules;
11063 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11064 if (p0 == 1 && z == 0)
11065 {
11066 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011067 if (reslen > 0 && ws != NULL && *ws != NUL
11068 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011069 || wres[reslen - 1] == *ws))
11070 reslen--;
11071 z0 = 1;
11072 z = 1;
11073 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011074 if (ws != NULL)
11075 while (*ws != NUL && word[i + k0] != NUL)
11076 {
11077 word[i + k0] = *ws;
11078 k0++;
11079 ws++;
11080 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011081 if (k > k0)
11082 mch_memmove(word + i + k0, word + i + k,
11083 sizeof(int) * (STRLEN(word + i + k) + 1));
11084
11085 /* new "actual letter" */
11086 c = word[i];
11087 }
11088 else
11089 {
11090 /* no '<' rule used */
11091 i += k - 1;
11092 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011093 if (ws != NULL)
11094 while (*ws != NUL && ws[1] != NUL
11095 && reslen < MAXWLEN)
11096 {
11097 if (reslen == 0 || wres[reslen - 1] != *ws)
11098 wres[reslen++] = *ws;
11099 ws++;
11100 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011101 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011102 if (ws == NULL)
11103 c = NUL;
11104 else
11105 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011106 if (strstr((char *)s, "^^") != NULL)
11107 {
11108 if (c != NUL)
11109 wres[reslen++] = c;
11110 mch_memmove(word, word + i + 1,
11111 sizeof(int) * (STRLEN(word + i + 1) + 1));
11112 i = 0;
11113 z0 = 1;
11114 }
11115 }
11116 break;
11117 }
11118 }
11119 }
11120 else if (vim_iswhite(c))
11121 {
11122 c = ' ';
11123 k = 1;
11124 }
11125
11126 if (z0 == 0)
11127 {
11128 if (k && !p0 && reslen < MAXWLEN && c != NUL
11129 && (!slang->sl_collapse || reslen == 0
11130 || wres[reslen - 1] != c))
11131 /* condense only double letters */
11132 wres[reslen++] = c;
11133
11134 i++;
11135 z = 0;
11136 k = 0;
11137 }
11138 }
11139
11140 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11141 l = 0;
11142 for (n = 0; n < reslen; ++n)
11143 {
11144 l += mb_char2bytes(wres[n], res + l);
11145 if (l + MB_MAXBYTES > MAXWLEN)
11146 break;
11147 }
11148 res[l] = NUL;
11149}
11150#endif
11151
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011152/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011153 * Compute a score for two sound-a-like words.
11154 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11155 * Instead of a generic loop we write out the code. That keeps it fast by
11156 * avoiding checks that will not be possible.
11157 */
11158 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011159soundalike_score(goodstart, badstart)
11160 char_u *goodstart; /* sound-folded good word */
11161 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011162{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011163 char_u *goodsound = goodstart;
11164 char_u *badsound = badstart;
11165 int goodlen;
11166 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011167 int n;
11168 char_u *pl, *ps;
11169 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011170 int score = 0;
11171
11172 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11173 * counted so much, vowels halfway the word aren't counted at all. */
11174 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11175 {
11176 score = SCORE_DEL / 2;
11177 if (*badsound == '*')
11178 ++badsound;
11179 else
11180 ++goodsound;
11181 }
11182
11183 goodlen = STRLEN(goodsound);
11184 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011185
11186 /* Return quickly if the lenghts are too different to be fixed by two
11187 * changes. */
11188 n = goodlen - badlen;
11189 if (n < -2 || n > 2)
11190 return SCORE_MAXMAX;
11191
11192 if (n > 0)
11193 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011194 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011195 ps = badsound;
11196 }
11197 else
11198 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011199 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011200 ps = goodsound;
11201 }
11202
11203 /* Skip over the identical part. */
11204 while (*pl == *ps && *pl != NUL)
11205 {
11206 ++pl;
11207 ++ps;
11208 }
11209
11210 switch (n)
11211 {
11212 case -2:
11213 case 2:
11214 /*
11215 * Must delete two characters from "pl".
11216 */
11217 ++pl; /* first delete */
11218 while (*pl == *ps)
11219 {
11220 ++pl;
11221 ++ps;
11222 }
11223 /* strings must be equal after second delete */
11224 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011225 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011226
11227 /* Failed to compare. */
11228 break;
11229
11230 case -1:
11231 case 1:
11232 /*
11233 * Minimal one delete from "pl" required.
11234 */
11235
11236 /* 1: delete */
11237 pl2 = pl + 1;
11238 ps2 = ps;
11239 while (*pl2 == *ps2)
11240 {
11241 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011242 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011243 ++pl2;
11244 ++ps2;
11245 }
11246
11247 /* 2: delete then swap, then rest must be equal */
11248 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11249 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011250 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011251
11252 /* 3: delete then substitute, then the rest must be equal */
11253 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011254 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011255
11256 /* 4: first swap then delete */
11257 if (pl[0] == ps[1] && pl[1] == ps[0])
11258 {
11259 pl2 = pl + 2; /* swap, skip two chars */
11260 ps2 = ps + 2;
11261 while (*pl2 == *ps2)
11262 {
11263 ++pl2;
11264 ++ps2;
11265 }
11266 /* delete a char and then strings must be equal */
11267 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011268 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011269 }
11270
11271 /* 5: first substitute then delete */
11272 pl2 = pl + 1; /* substitute, skip one char */
11273 ps2 = ps + 1;
11274 while (*pl2 == *ps2)
11275 {
11276 ++pl2;
11277 ++ps2;
11278 }
11279 /* delete a char and then strings must be equal */
11280 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011281 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011282
11283 /* Failed to compare. */
11284 break;
11285
11286 case 0:
11287 /*
11288 * Lenghts are equal, thus changes must result in same length: An
11289 * insert is only possible in combination with a delete.
11290 * 1: check if for identical strings
11291 */
11292 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011293 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011294
11295 /* 2: swap */
11296 if (pl[0] == ps[1] && pl[1] == ps[0])
11297 {
11298 pl2 = pl + 2; /* swap, skip two chars */
11299 ps2 = ps + 2;
11300 while (*pl2 == *ps2)
11301 {
11302 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011303 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011304 ++pl2;
11305 ++ps2;
11306 }
11307 /* 3: swap and swap again */
11308 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11309 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011310 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011311
11312 /* 4: swap and substitute */
11313 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011314 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011315 }
11316
11317 /* 5: substitute */
11318 pl2 = pl + 1;
11319 ps2 = ps + 1;
11320 while (*pl2 == *ps2)
11321 {
11322 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011323 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011324 ++pl2;
11325 ++ps2;
11326 }
11327
11328 /* 6: substitute and swap */
11329 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11330 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011331 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011332
11333 /* 7: substitute and substitute */
11334 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011335 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011336
11337 /* 8: insert then delete */
11338 pl2 = pl;
11339 ps2 = ps + 1;
11340 while (*pl2 == *ps2)
11341 {
11342 ++pl2;
11343 ++ps2;
11344 }
11345 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011346 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011347
11348 /* 9: delete then insert */
11349 pl2 = pl + 1;
11350 ps2 = ps;
11351 while (*pl2 == *ps2)
11352 {
11353 ++pl2;
11354 ++ps2;
11355 }
11356 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011357 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011358
11359 /* Failed to compare. */
11360 break;
11361 }
11362
11363 return SCORE_MAXMAX;
11364}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366/*
11367 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011368 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011369 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000011370 * The algorithm is described by Du and Chang, 1992.
11371 * The implementation of the algorithm comes from Aspell editdist.cpp,
11372 * edit_distance(). It has been converted from C++ to C and modified to
11373 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 */
11375 static int
11376spell_edit_score(badword, goodword)
11377 char_u *badword;
11378 char_u *goodword;
11379{
11380 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011381 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 int j, i;
11383 int t;
11384 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011385 int pbc, pgc;
11386#ifdef FEAT_MBYTE
11387 char_u *p;
11388 int wbadword[MAXWLEN];
11389 int wgoodword[MAXWLEN];
11390
11391 if (has_mbyte)
11392 {
11393 /* Get the characters from the multi-byte strings and put them in an
11394 * int array for easy access. */
11395 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011396 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011397 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011398 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011399 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011400 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011401 }
11402 else
11403#endif
11404 {
11405 badlen = STRLEN(badword) + 1;
11406 goodlen = STRLEN(goodword) + 1;
11407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408
11409 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
11410#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
11412 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011413 if (cnt == NULL)
11414 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011415
11416 CNT(0, 0) = 0;
11417 for (j = 1; j <= goodlen; ++j)
11418 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
11419
11420 for (i = 1; i <= badlen; ++i)
11421 {
11422 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
11423 for (j = 1; j <= goodlen; ++j)
11424 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011425#ifdef FEAT_MBYTE
11426 if (has_mbyte)
11427 {
11428 bc = wbadword[i - 1];
11429 gc = wgoodword[j - 1];
11430 }
11431 else
11432#endif
11433 {
11434 bc = badword[i - 1];
11435 gc = goodword[j - 1];
11436 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 if (bc == gc)
11438 CNT(i, j) = CNT(i - 1, j - 1);
11439 else
11440 {
11441 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011442 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
11444 else
11445 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
11446
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011447 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011449#ifdef FEAT_MBYTE
11450 if (has_mbyte)
11451 {
11452 pbc = wbadword[i - 2];
11453 pgc = wgoodword[j - 2];
11454 }
11455 else
11456#endif
11457 {
11458 pbc = badword[i - 2];
11459 pgc = goodword[j - 2];
11460 }
11461 if (bc == pgc && pbc == gc)
11462 {
11463 t = SCORE_SWAP + CNT(i - 2, j - 2);
11464 if (t < CNT(i, j))
11465 CNT(i, j) = t;
11466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 }
11468 t = SCORE_DEL + CNT(i - 1, j);
11469 if (t < CNT(i, j))
11470 CNT(i, j) = t;
11471 t = SCORE_INS + CNT(i, j - 1);
11472 if (t < CNT(i, j))
11473 CNT(i, j) = t;
11474 }
11475 }
11476 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011477
11478 i = CNT(badlen - 1, goodlen - 1);
11479 vim_free(cnt);
11480 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000011482
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011483/*
11484 * ":spelldump"
11485 */
11486/*ARGSUSED*/
11487 void
11488ex_spelldump(eap)
11489 exarg_T *eap;
11490{
11491 buf_T *buf = curbuf;
11492 langp_T *lp;
11493 slang_T *slang;
11494 idx_T arridx[MAXWLEN];
11495 int curi[MAXWLEN];
11496 char_u word[MAXWLEN];
11497 int c;
11498 char_u *byts;
11499 idx_T *idxs;
11500 linenr_T lnum = 0;
11501 int round;
11502 int depth;
11503 int n;
11504 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011505 char_u *region_names = NULL; /* region names being used */
11506 int do_region = TRUE; /* dump region names and numbers */
11507 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011508
11509 if (no_spell_checking())
11510 return;
11511
11512 /* Create a new empty buffer by splitting the window. */
11513 do_cmdline_cmd((char_u *)"new");
11514 if (!bufempty() || !buf_valid(buf))
11515 return;
11516
Bram Moolenaar7887d882005-07-01 22:33:52 +000011517 /* Find out if we can support regions: All languages must support the same
11518 * regions or none at all. */
11519 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11520 {
11521 p = lp->lp_slang->sl_regions;
11522 if (p[0] != 0)
11523 {
11524 if (region_names == NULL) /* first language with regions */
11525 region_names = p;
11526 else if (STRCMP(region_names, p) != 0)
11527 {
11528 do_region = FALSE; /* region names are different */
11529 break;
11530 }
11531 }
11532 }
11533
11534 if (do_region && region_names != NULL)
11535 {
11536 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
11537 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11538 }
11539 else
11540 do_region = FALSE;
11541
11542 /*
11543 * Loop over all files loaded for the entries in 'spelllang'.
11544 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011545 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11546 {
11547 slang = lp->lp_slang;
11548
11549 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
11550 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11551
11552 /* round 1: case-folded tree
11553 * round 2: keep-case tree */
11554 for (round = 1; round <= 2; ++round)
11555 {
11556 if (round == 1)
11557 {
11558 byts = slang->sl_fbyts;
11559 idxs = slang->sl_fidxs;
11560 }
11561 else
11562 {
11563 byts = slang->sl_kbyts;
11564 idxs = slang->sl_kidxs;
11565 }
11566 if (byts == NULL)
11567 continue; /* array is empty */
11568
11569 depth = 0;
11570 arridx[0] = 0;
11571 curi[0] = 1;
11572 while (depth >= 0 && !got_int)
11573 {
11574 if (curi[depth] > byts[arridx[depth]])
11575 {
11576 /* Done all bytes at this node, go up one level. */
11577 --depth;
11578 line_breakcheck();
11579 }
11580 else
11581 {
11582 /* Do one more byte at this node. */
11583 n = arridx[depth] + curi[depth];
11584 ++curi[depth];
11585 c = byts[n];
11586 if (c == 0)
11587 {
11588 /* End of word, deal with the word.
11589 * Don't use keep-case words in the fold-case tree,
11590 * they will appear in the keep-case tree.
11591 * Only use the word when the region matches. */
11592 flags = (int)idxs[n];
11593 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000011594 && (do_region
11595 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011596 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011597 & lp->lp_region) != 0))
11598 {
11599 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011600 if (!do_region)
11601 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011602
11603 /* Dump the basic word if there is no prefix or
11604 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011605 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011606 if (c == 0 || curi[depth] == 2)
11607 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011608
11609 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011610 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011611 lnum = apply_prefixes(slang, word, round,
11612 flags, lnum);
11613 }
11614 }
11615 else
11616 {
11617 /* Normal char, go one level deeper. */
11618 word[depth++] = c;
11619 arridx[depth] = idxs[n];
11620 curi[depth] = 1;
11621 }
11622 }
11623 }
11624 }
11625 }
11626
11627 /* Delete the empty line that we started with. */
11628 if (curbuf->b_ml.ml_line_count > 1)
11629 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
11630
11631 redraw_later(NOT_VALID);
11632}
11633
11634/*
11635 * Dump one word: apply case modifications and append a line to the buffer.
11636 */
11637 static void
11638dump_word(word, round, flags, lnum)
11639 char_u *word;
11640 int round;
11641 int flags;
11642 linenr_T lnum;
11643{
11644 int keepcap = FALSE;
11645 char_u *p;
11646 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000011647 char_u badword[MAXWLEN + 10];
11648 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011649
11650 if (round == 1 && (flags & WF_CAPMASK) != 0)
11651 {
11652 /* Need to fix case according to "flags". */
11653 make_case_word(word, cword, flags);
11654 p = cword;
11655 }
11656 else
11657 {
11658 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011659 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
11660 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011661 keepcap = TRUE;
11662 }
11663
Bram Moolenaar7887d882005-07-01 22:33:52 +000011664 /* Add flags and regions after a slash. */
11665 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011666 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000011667 STRCPY(badword, p);
11668 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011669 if (keepcap)
11670 STRCAT(badword, "=");
11671 if (flags & WF_BANNED)
11672 STRCAT(badword, "!");
11673 else if (flags & WF_RARE)
11674 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000011675 if (flags & WF_REGION)
11676 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011677 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000011678 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011679 p = badword;
11680 }
11681
11682 ml_append(lnum, p, (colnr_T)0, FALSE);
11683}
11684
11685/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011686 * For ":spelldump": Find matching prefixes for "word". Prepend each to
11687 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011688 * Return the updated line number.
11689 */
11690 static linenr_T
11691apply_prefixes(slang, word, round, flags, startlnum)
11692 slang_T *slang;
11693 char_u *word; /* case-folded word */
11694 int round;
11695 int flags; /* flags with prefix ID */
11696 linenr_T startlnum;
11697{
11698 idx_T arridx[MAXWLEN];
11699 int curi[MAXWLEN];
11700 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000011701 char_u word_up[MAXWLEN];
11702 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011703 int c;
11704 char_u *byts;
11705 idx_T *idxs;
11706 linenr_T lnum = startlnum;
11707 int depth;
11708 int n;
11709 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011710 int i;
11711
Bram Moolenaar53805d12005-08-01 07:08:33 +000011712 /* if the word starts with a lower-case letter make the word with an
11713 * upper-case letter in word_up[]. */
11714 c = PTR2CHAR(word);
11715 if (SPELL_TOUPPER(c) != c)
11716 {
11717 onecap_copy(word, word_up, TRUE);
11718 has_word_up = TRUE;
11719 }
11720
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011721 byts = slang->sl_pbyts;
11722 idxs = slang->sl_pidxs;
11723 if (byts != NULL) /* array not is empty */
11724 {
11725 /*
11726 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011727 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011728 */
11729 depth = 0;
11730 arridx[0] = 0;
11731 curi[0] = 1;
11732 while (depth >= 0 && !got_int)
11733 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011734 n = arridx[depth];
11735 len = byts[n];
11736 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011737 {
11738 /* Done all bytes at this node, go up one level. */
11739 --depth;
11740 line_breakcheck();
11741 }
11742 else
11743 {
11744 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011745 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011746 ++curi[depth];
11747 c = byts[n];
11748 if (c == 0)
11749 {
11750 /* End of prefix, find out how many IDs there are. */
11751 for (i = 1; i < len; ++i)
11752 if (byts[n + i] != 0)
11753 break;
11754 curi[depth] += i - 1;
11755
Bram Moolenaar53805d12005-08-01 07:08:33 +000011756 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
11757 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011758 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011759 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011760 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011761 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011762 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011763 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011764
11765 /* Check for prefix that matches the word when the
11766 * first letter is upper-case, but only if the prefix has
11767 * a condition. */
11768 if (has_word_up)
11769 {
11770 c = valid_word_prefix(i, n, flags, word_up, slang,
11771 TRUE);
11772 if (c != 0)
11773 {
11774 vim_strncpy(prefix + depth, word_up,
11775 MAXWLEN - depth - 1);
11776 dump_word(prefix, round,
11777 (c & WF_RAREPFX) ? (flags | WF_RARE)
11778 : flags, lnum++);
11779 }
11780 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011781 }
11782 else
11783 {
11784 /* Normal char, go one level deeper. */
11785 prefix[depth++] = c;
11786 arridx[depth] = idxs[n];
11787 curi[depth] = 1;
11788 }
11789 }
11790 }
11791 }
11792
11793 return lnum;
11794}
11795
Bram Moolenaar8b59de92005-08-11 19:59:29 +000011796#if defined(FEAT_INS_EXPAND) || defined(PROTO)
11797static int spell_expand_need_cap;
11798
11799/*
11800 * Find start of the word in front of the cursor. We don't check if it is
11801 * badly spelled, with completion we can only change the word in front of the
11802 * cursor.
11803 * Used for Insert mode completion CTRL-X ?.
11804 * Returns the column number of the word.
11805 */
11806 int
11807spell_word_start(startcol)
11808 int startcol;
11809{
11810 char_u *line;
11811 char_u *p;
11812 int col = 0;
11813
11814 if (no_spell_checking())
11815 return startcol;
11816
11817 /* Find a word character before "startcol". */
11818 line = ml_get_curline();
11819 for (p = line + startcol; p > line; )
11820 {
11821 mb_ptr_back(line, p);
11822 if (spell_iswordp_nmw(p))
11823 break;
11824 }
11825
11826 /* Go back to start of the word. */
11827 while (p > line)
11828 {
11829 col = p - line;
11830 mb_ptr_back(line, p);
11831 if (!spell_iswordp(p, curbuf))
11832 break;
11833 col = 0;
11834 }
11835
11836 /* Need to check for 'spellcapcheck' now, the word is removed before
11837 * expand_spelling() is called. Therefore the ugly global variable. */
11838 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
11839
11840 return col;
11841}
11842
11843/*
11844 * Get list of spelling suggestions.
11845 * Used for Insert mode completion CTRL-X ?.
11846 * Returns the number of matches. The matches are in "matchp[]", array of
11847 * allocated strings.
11848 */
11849/*ARGSUSED*/
11850 int
11851expand_spelling(lnum, col, pat, matchp)
11852 linenr_T lnum;
11853 int col;
11854 char_u *pat;
11855 char_u ***matchp;
11856{
11857 garray_T ga;
11858
11859 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
11860 *matchp = ga.ga_data;
11861 return ga.ga_len;
11862}
11863#endif
11864
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000011865#endif /* FEAT_SYN_HL */