blob: dd0fe834f870f73d6fad6564cbed5c61dc43330e [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 Moolenaare52325c2005-08-22 22:54:29 +0000253/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000254 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000255#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000256typedef 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 Moolenaar1d73c882005-06-19 22:48:47 +0000989#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +0000990 char_u *p;
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 Moolenaare52325c2005-08-22 22:54:29 +00001221 if (((unsigned)flags >> 24) == 0
1222 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001223 continue;
1224
Bram Moolenaare52325c2005-08-22 22:54:29 +00001225 /* Limit the number of compound words to COMPOUNDMAX if no
1226 * maximum for syllables is specified. */
1227 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1228 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001229 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001230
Bram Moolenaard12a1322005-08-21 22:08:24 +00001231 /* Quickly check if compounding is possible with this flag. */
1232 if (vim_strchr(mip->mi_complen == 0
1233 ? slang->sl_compstartflags
1234 : slang->sl_compallflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00001235 ((unsigned)flags >> 24)) == NULL)
1236 continue;
1237
Bram Moolenaare52325c2005-08-22 22:54:29 +00001238 if (mode == FIND_COMPOUND)
1239 {
1240 int capflags;
1241
1242 /* Need to check the caps type of the appended compound
1243 * word. */
1244#ifdef FEAT_MBYTE
1245 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1246 mip->mi_compoff) != 0)
1247 {
1248 /* case folding may have changed the length */
1249 p = mip->mi_word;
1250 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1251 mb_ptr_adv(p);
1252 }
1253 else
1254#endif
1255 p = mip->mi_word + mip->mi_compoff;
1256 capflags = captype(p, mip->mi_word + wlen);
1257 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1258 && (flags & WF_FIXCAP) != 0))
1259 continue;
1260
1261 if (capflags != WF_ALLCAP)
1262 {
1263 /* When the character before the word is a word
1264 * character we do not accept a Onecap word. We do
1265 * accept a no-caps word, even when the dictionary
1266 * word specifies ONECAP. */
1267 mb_ptr_back(mip->mi_word, p);
1268 if (spell_iswordp_nmw(p)
1269 ? capflags == WF_ONECAP
1270 : (flags & WF_ONECAP) != 0
1271 && capflags != WF_ONECAP)
1272 continue;
1273 }
1274 }
1275
Bram Moolenaar5195e452005-08-19 20:32:47 +00001276 /* If the word ends the sequence of compound flags of the
1277 * words must match with one of the COMPOUNDFLAGS items and
1278 * the number of syllables must not be too large. */
1279 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1280 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1281 if (word_ends)
1282 {
1283 char_u fword[MAXWLEN];
1284
1285 if (slang->sl_compsylmax < MAXWLEN)
1286 {
1287 /* "fword" is only needed for checking syllables. */
1288 if (ptr == mip->mi_word)
1289 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1290 else
1291 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1292 }
1293 if (!can_compound(slang, fword, mip->mi_compflags))
1294 continue;
1295 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001296 }
1297
1298 if (!word_ends)
1299 {
1300 /* Check that a valid word follows. If there is one, it will
1301 * set "mi_result", thus we are always finished here.
1302 * Recursive! */
1303
1304 /* Find following word in case-folded tree. */
1305 mip->mi_compoff = endlen[endidxcnt];
1306#ifdef FEAT_MBYTE
1307 if (has_mbyte && mode == FIND_KEEPWORD)
1308 {
1309 /* Compute byte length in case-folded word from "wlen":
1310 * byte length in keep-case word. Length may change when
1311 * folding case. This can be slow, take a shortcut when
1312 * the case-folded word is equal to the keep-case word. */
1313 p = mip->mi_fword;
1314 if (STRNCMP(ptr, p, wlen) != 0)
1315 {
1316 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1317 mb_ptr_adv(p);
1318 mip->mi_compoff = p - mip->mi_fword;
1319 }
1320 }
1321#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001322 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001323 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001324 find_word(mip, FIND_COMPOUND);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001325
1326 /* Find following word in keep-case tree. */
1327 mip->mi_compoff = wlen;
1328 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001329
1330 /* Check for following word with prefix. */
1331 mip->mi_compoff = c;
1332 find_prefix(mip, FIND_COMPOUND);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001333 --mip->mi_complen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001334
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001335 if (mip->mi_result == SP_OK)
1336 break;
1337 continue;
1338 }
1339
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001340 if (flags & WF_BANNED)
1341 res = SP_BANNED;
1342 else if (flags & WF_REGION)
1343 {
1344 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001345 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001346 res = SP_OK;
1347 else
1348 res = SP_LOCAL;
1349 }
1350 else if (flags & WF_RARE)
1351 res = SP_RARE;
1352 else
1353 res = SP_OK;
1354
1355 /* Always use the longest match and the best result. */
1356 if (mip->mi_result > res)
1357 {
1358 mip->mi_result = res;
1359 mip->mi_end = mip->mi_word + wlen;
1360 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001361 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001362 mip->mi_end = mip->mi_word + wlen;
1363
1364 if (res == SP_OK)
1365 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001366 }
1367
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001368 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001369 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001370 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001371}
1372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001373/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001374 * Return TRUE if "flags" is a valid sequence of compound flags and
1375 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001376 */
1377 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001378can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001379 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001380 char_u *word;
1381 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001382{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001383 regmatch_T regmatch;
1384
1385 if (slang->sl_compprog == NULL)
1386 return FALSE;
1387 regmatch.regprog = slang->sl_compprog;
1388 regmatch.rm_ic = FALSE;
1389 if (!vim_regexec(&regmatch, flags, 0))
1390 return FALSE;
1391
Bram Moolenaare52325c2005-08-22 22:54:29 +00001392 /* Count the number of syllables. This may be slow, do it last. If there
1393 * are too many syllables AND the number of compound words is above
1394 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001395 if (slang->sl_compsylmax < MAXWLEN
1396 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaare52325c2005-08-22 22:54:29 +00001397 return STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001398 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001399}
1400
1401/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001402 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1403 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001404 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001405 */
1406 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001407valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001408 int totprefcnt; /* nr of prefix IDs */
1409 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001410 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001411 char_u *word;
1412 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001413 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001414{
1415 int prefcnt;
1416 int pidx;
1417 regprog_T *rp;
1418 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001419 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001420
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001421 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001422 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1423 {
1424 pidx = slang->sl_pidxs[arridx + prefcnt];
1425
1426 /* Check the prefix ID. */
1427 if (prefid != (pidx & 0xff))
1428 continue;
1429
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001430 /* Check if the prefix doesn't combine and the word already has a
1431 * suffix. */
1432 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1433 continue;
1434
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001435 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001436 * stored in the two bytes above the prefix ID byte. */
1437 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001438 if (rp != NULL)
1439 {
1440 regmatch.regprog = rp;
1441 regmatch.rm_ic = FALSE;
1442 if (!vim_regexec(&regmatch, word, 0))
1443 continue;
1444 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001445 else if (cond_req)
1446 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001447
Bram Moolenaar53805d12005-08-01 07:08:33 +00001448 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001449 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001450 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001451 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001452}
1453
1454/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001455 * Check if the word at "mip->mi_word" has a matching prefix.
1456 * If it does, then check the following word.
1457 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001458 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1459 * prefix in a compound word.
1460 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001461 * For a match mip->mi_result is updated.
1462 */
1463 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001464find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001465 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001466 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001467{
1468 idx_T arridx = 0;
1469 int len;
1470 int wlen = 0;
1471 int flen;
1472 int c;
1473 char_u *ptr;
1474 idx_T lo, hi, m;
1475 slang_T *slang = mip->mi_lp->lp_slang;
1476 char_u *byts;
1477 idx_T *idxs;
1478
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001479 byts = slang->sl_pbyts;
1480 if (byts == NULL)
1481 return; /* array is empty */
1482
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001483 /* We use the case-folded word here, since prefixes are always
1484 * case-folded. */
1485 ptr = mip->mi_fword;
1486 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001487 if (mode == FIND_COMPOUND)
1488 {
1489 /* Skip over the previously found word(s). */
1490 ptr += mip->mi_compoff;
1491 flen -= mip->mi_compoff;
1492 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001493 idxs = slang->sl_pidxs;
1494
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001495 /*
1496 * Repeat advancing in the tree until:
1497 * - there is a byte that doesn't match,
1498 * - we reach the end of the tree,
1499 * - or we reach the end of the line.
1500 */
1501 for (;;)
1502 {
1503 if (flen == 0 && *mip->mi_fend != NUL)
1504 flen = fold_more(mip);
1505
1506 len = byts[arridx++];
1507
1508 /* If the first possible byte is a zero the prefix could end here.
1509 * Check if the following word matches and supports the prefix. */
1510 if (byts[arridx] == 0)
1511 {
1512 /* There can be several prefixes with different conditions. We
1513 * try them all, since we don't know which one will give the
1514 * longest match. The word is the same each time, pass the list
1515 * of possible prefixes to find_word(). */
1516 mip->mi_prefarridx = arridx;
1517 mip->mi_prefcnt = len;
1518 while (len > 0 && byts[arridx] == 0)
1519 {
1520 ++arridx;
1521 --len;
1522 }
1523 mip->mi_prefcnt -= len;
1524
1525 /* Find the word that comes after the prefix. */
1526 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001527 if (mode == FIND_COMPOUND)
1528 /* Skip over the previously found word(s). */
1529 mip->mi_prefixlen += mip->mi_compoff;
1530
Bram Moolenaar53805d12005-08-01 07:08:33 +00001531#ifdef FEAT_MBYTE
1532 if (has_mbyte)
1533 {
1534 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001535 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1536 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001537 }
1538 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001539 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001540#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001541 find_word(mip, FIND_PREFIX);
1542
1543
1544 if (len == 0)
1545 break; /* no children, word must end here */
1546 }
1547
1548 /* Stop looking at end of the line. */
1549 if (ptr[wlen] == NUL)
1550 break;
1551
1552 /* Perform a binary search in the list of accepted bytes. */
1553 c = ptr[wlen];
1554 lo = arridx;
1555 hi = arridx + len - 1;
1556 while (lo < hi)
1557 {
1558 m = (lo + hi) / 2;
1559 if (byts[m] > c)
1560 hi = m - 1;
1561 else if (byts[m] < c)
1562 lo = m + 1;
1563 else
1564 {
1565 lo = hi = m;
1566 break;
1567 }
1568 }
1569
1570 /* Stop if there is no matching byte. */
1571 if (hi < lo || byts[lo] != c)
1572 break;
1573
1574 /* Continue at the child (if there is one). */
1575 arridx = idxs[lo];
1576 ++wlen;
1577 --flen;
1578 }
1579}
1580
1581/*
1582 * Need to fold at least one more character. Do until next non-word character
1583 * for efficiency.
1584 * Return the length of the folded chars in bytes.
1585 */
1586 static int
1587fold_more(mip)
1588 matchinf_T *mip;
1589{
1590 int flen;
1591 char_u *p;
1592
1593 p = mip->mi_fend;
1594 do
1595 {
1596 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001597 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001598
1599 /* Include the non-word character so that we can check for the
1600 * word end. */
1601 if (*mip->mi_fend != NUL)
1602 mb_ptr_adv(mip->mi_fend);
1603
1604 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1605 mip->mi_fword + mip->mi_fwordlen,
1606 MAXWLEN - mip->mi_fwordlen);
1607 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1608 mip->mi_fwordlen += flen;
1609 return flen;
1610}
1611
1612/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001613 * Check case flags for a word. Return TRUE if the word has the requested
1614 * case.
1615 */
1616 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001617spell_valid_case(wordflags, treeflags)
1618 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001619 int treeflags; /* flags for the word in the spell tree */
1620{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001621 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001622 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001623 && ((treeflags & WF_ONECAP) == 0
1624 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001625}
1626
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001627/*
1628 * Return TRUE if spell checking is not enabled.
1629 */
1630 static int
1631no_spell_checking()
1632{
1633 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1634 {
1635 EMSG(_("E756: Spell checking is not enabled"));
1636 return TRUE;
1637 }
1638 return FALSE;
1639}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001640
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001641/*
1642 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001643 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001644 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1645 * to after badly spelled word before the cursor.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001646 * Return OK if found, FAIL otherwise.
1647 */
1648 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001649spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001650 int dir; /* FORWARD or BACKWARD */
1651 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001652 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001653{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001654 linenr_T lnum;
1655 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001656 char_u *line;
1657 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001658 char_u *endp;
1659 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001660 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001661 int has_syntax = syntax_present(curbuf);
1662 int col;
1663 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001664 char_u *buf = NULL;
1665 int buflen = 0;
1666 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001667 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001668
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001669 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001670 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001671
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001672 /*
1673 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001674 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001675 *
1676 * When searching backwards, we continue in the line to find the last
1677 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001678 *
1679 * We concatenate the start of the next line, so that wrapped words work
1680 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1681 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001682 */
1683 lnum = curwin->w_cursor.lnum;
1684 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001685
1686 while (!got_int)
1687 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001688 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001689
Bram Moolenaar0c405862005-06-22 22:26:26 +00001690 len = STRLEN(line);
1691 if (buflen < len + MAXWLEN + 2)
1692 {
1693 vim_free(buf);
1694 buflen = len + MAXWLEN + 2;
1695 buf = alloc(buflen);
1696 if (buf == NULL)
1697 break;
1698 }
1699
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001700 /* In first line check first word for Capital. */
1701 if (lnum == 1)
1702 capcol = 0;
1703
1704 /* For checking first word with a capital skip white space. */
1705 if (capcol == 0)
1706 capcol = skipwhite(line) - line;
1707
Bram Moolenaar0c405862005-06-22 22:26:26 +00001708 /* Copy the line into "buf" and append the start of the next line if
1709 * possible. */
1710 STRCPY(buf, line);
1711 if (lnum < curbuf->b_ml.ml_line_count)
1712 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1713
1714 p = buf + skip;
1715 endp = buf + len;
1716 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001717 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001718 /* When searching backward don't search after the cursor. */
1719 if (dir == BACKWARD
1720 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001721 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001722 break;
1723
1724 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001725 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001726 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001727
1728 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001729 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001730 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001731 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001732 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001733 /* When searching forward only accept a bad word after
1734 * the cursor. */
1735 if (dir == BACKWARD
1736 || lnum > curwin->w_cursor.lnum
1737 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001738 && (colnr_T)(curline ? p - buf + len
1739 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001740 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001741 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001742 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001743 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001744 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001745 (void)syn_get_id(lnum, (colnr_T)col,
1746 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001747 }
1748 else
1749 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001750
Bram Moolenaar51485f02005-06-04 21:55:20 +00001751 if (can_spell)
1752 {
1753 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001754 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001755#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001756 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001757#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001758 if (dir == FORWARD)
1759 {
1760 /* No need to search further. */
1761 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001762 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001763 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001764 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001765 else if (curline)
1766 /* Insert mode completion: put cursor after
1767 * the bad word. */
1768 found_pos.col += len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001769 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001771 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001772 }
1773
Bram Moolenaar51485f02005-06-04 21:55:20 +00001774 /* advance to character after the word */
1775 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001776 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001777 }
1778
Bram Moolenaar5195e452005-08-19 20:32:47 +00001779 if (dir == BACKWARD && found_pos.lnum != 0)
1780 {
1781 /* Use the last match in the line. */
1782 curwin->w_cursor = found_pos;
1783 vim_free(buf);
1784 return OK;
1785 }
1786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001787 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001788 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001789
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001790 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001791 if (dir == BACKWARD)
1792 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001793 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001794 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001795 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001796 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001797 }
1798 else
1799 {
1800 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001801 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001802 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001803
1804 /* Skip the characters at the start of the next line that were
1805 * included in a match crossing line boundaries. */
1806 if (attr == 0)
1807 skip = p - endp;
1808 else
1809 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001810
1811 /* Capscol skips over the inserted space. */
1812 --capcol;
1813
1814 /* But after empty line check first word in next line */
1815 if (*skipwhite(line) == NUL)
1816 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001817 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001818
1819 line_breakcheck();
1820 }
1821
Bram Moolenaar0c405862005-06-22 22:26:26 +00001822 vim_free(buf);
1823 return FAIL;
1824}
1825
1826/*
1827 * For spell checking: concatenate the start of the following line "line" into
1828 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1829 */
1830 void
1831spell_cat_line(buf, line, maxlen)
1832 char_u *buf;
1833 char_u *line;
1834 int maxlen;
1835{
1836 char_u *p;
1837 int n;
1838
1839 p = skipwhite(line);
1840 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1841 p = skipwhite(p + 1);
1842
1843 if (*p != NUL)
1844 {
1845 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001846 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001847 n = p - line;
1848 if (n >= maxlen)
1849 n = maxlen - 1;
1850 vim_memset(buf + 1, ' ', n);
1851 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001852}
1853
1854/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001855 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001856 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001858 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001859spell_load_lang(lang)
1860 char_u *lang;
1861{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001862 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001863 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001864 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001865
Bram Moolenaarb765d632005-06-07 21:00:02 +00001866 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001867 * It's truncated when an error is detected. */
1868 STRCPY(langcp, lang);
1869
Bram Moolenaarb765d632005-06-07 21:00:02 +00001870 /*
1871 * Find the first spell file for "lang" in 'runtimepath' and load it.
1872 */
1873 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1874 "spell/%s.%s.spl", lang, spell_enc());
1875 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001876
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001877 if (r == FAIL && *langcp != NUL)
1878 {
1879 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001880 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001881 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001882 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001883 }
1884
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001885 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001886 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1887 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001888 else if (*langcp != NUL)
1889 {
1890 /* Load all the additions. */
1891 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1892 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1893 }
1894}
1895
1896/*
1897 * Return the encoding used for spell checking: Use 'encoding', except that we
1898 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1899 */
1900 static char_u *
1901spell_enc()
1902{
1903
1904#ifdef FEAT_MBYTE
1905 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1906 return p_enc;
1907#endif
1908 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001909}
1910
1911/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001912 * Get the name of the .spl file for the internal wordlist into
1913 * "fname[MAXPATHL]".
1914 */
1915 static void
1916int_wordlist_spl(fname)
1917 char_u *fname;
1918{
1919 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1920 int_wordlist, spell_enc());
1921}
1922
1923/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001924 * Allocate a new slang_T.
1925 * Caller must fill "sl_next".
1926 */
1927 static slang_T *
1928slang_alloc(lang)
1929 char_u *lang;
1930{
1931 slang_T *lp;
1932
Bram Moolenaar51485f02005-06-04 21:55:20 +00001933 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001934 if (lp != NULL)
1935 {
1936 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001937 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00001938 lp->sl_compmax = MAXWLEN;
1939 lp->sl_compminlen = MAXWLEN;
1940 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001941 }
1942 return lp;
1943}
1944
1945/*
1946 * Free the contents of an slang_T and the structure itself.
1947 */
1948 static void
1949slang_free(lp)
1950 slang_T *lp;
1951{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001953 vim_free(lp->sl_fname);
1954 slang_clear(lp);
1955 vim_free(lp);
1956}
1957
1958/*
1959 * Clear an slang_T so that the file can be reloaded.
1960 */
1961 static void
1962slang_clear(lp)
1963 slang_T *lp;
1964{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001965 garray_T *gap;
1966 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001967 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001968 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969
Bram Moolenaar51485f02005-06-04 21:55:20 +00001970 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001971 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001972 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001973 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001974 vim_free(lp->sl_pbyts);
1975 lp->sl_pbyts = NULL;
1976
Bram Moolenaar51485f02005-06-04 21:55:20 +00001977 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001978 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001979 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001980 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001981 vim_free(lp->sl_pidxs);
1982 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001983
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001984 gap = &lp->sl_rep;
1985 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001987 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1988 vim_free(ftp->ft_from);
1989 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001990 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001991 ga_clear(gap);
1992
1993 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001994 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001995 {
1996 /* "ga_len" is set to 1 without adding an item for latin1 */
1997 if (gap->ga_data != NULL)
1998 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1999 for (i = 0; i < gap->ga_len; ++i)
2000 vim_free(((int **)gap->ga_data)[i]);
2001 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002002 else
2003 /* SAL items: free salitem_T items */
2004 while (gap->ga_len > 0)
2005 {
2006 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2007 vim_free(smp->sm_lead);
2008 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2009 vim_free(smp->sm_to);
2010#ifdef FEAT_MBYTE
2011 vim_free(smp->sm_lead_w);
2012 vim_free(smp->sm_oneof_w);
2013 vim_free(smp->sm_to_w);
2014#endif
2015 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002016 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002017
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002018 for (i = 0; i < lp->sl_prefixcnt; ++i)
2019 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002020 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002021 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002022 lp->sl_prefprog = NULL;
2023
2024 vim_free(lp->sl_midword);
2025 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002026
Bram Moolenaar5195e452005-08-19 20:32:47 +00002027 vim_free(lp->sl_compprog);
2028 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002029 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002030 lp->sl_compprog = NULL;
2031 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002032 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002033
2034 vim_free(lp->sl_syllable);
2035 lp->sl_syllable = NULL;
2036 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002037
Bram Moolenaarea424162005-06-16 21:51:00 +00002038#ifdef FEAT_MBYTE
2039 {
2040 int todo = lp->sl_map_hash.ht_used;
2041 hashitem_T *hi;
2042
2043 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2044 if (!HASHITEM_EMPTY(hi))
2045 {
2046 --todo;
2047 vim_free(hi->hi_key);
2048 }
2049 }
2050 hash_clear(&lp->sl_map_hash);
2051#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002052
2053 lp->sl_compmax = MAXWLEN;
2054 lp->sl_compminlen = MAXWLEN;
2055 lp->sl_compsylmax = MAXWLEN;
2056 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002057}
2058
2059/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002060 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002061 * Invoked through do_in_runtimepath().
2062 */
2063 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002064spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002065 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002066 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002067{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002068 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002069}
2070
2071/*
2072 * Load one spell file and store the info into a slang_T.
2073 *
2074 * This is invoked in two ways:
2075 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2076 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2077 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2078 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002079 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002080 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002081 static slang_T *
2082spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002083 char_u *fname;
2084 char_u *lang;
2085 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002086 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002087{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002088 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002089 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002090 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002091 char_u *bp;
2092 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002093 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002094 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002095 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002096 int round;
2097 char_u *save_sourcing_name = sourcing_name;
2098 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002099 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002100 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002101 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002102 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103
Bram Moolenaarb765d632005-06-07 21:00:02 +00002104 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002105 if (fd == NULL)
2106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002107 if (!silent)
2108 EMSG2(_(e_notopen), fname);
2109 else if (p_verbose > 2)
2110 {
2111 verbose_enter();
2112 smsg((char_u *)e_notopen, fname);
2113 verbose_leave();
2114 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002115 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002117 if (p_verbose > 2)
2118 {
2119 verbose_enter();
2120 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2121 verbose_leave();
2122 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002123
Bram Moolenaarb765d632005-06-07 21:00:02 +00002124 if (old_lp == NULL)
2125 {
2126 lp = slang_alloc(lang);
2127 if (lp == NULL)
2128 goto endFAIL;
2129
2130 /* Remember the file name, used to reload the file when it's updated. */
2131 lp->sl_fname = vim_strsave(fname);
2132 if (lp->sl_fname == NULL)
2133 goto endFAIL;
2134
2135 /* Check for .add.spl. */
2136 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2137 }
2138 else
2139 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002140
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002141 /* Set sourcing_name, so that error messages mention the file name. */
2142 sourcing_name = fname;
2143 sourcing_lnum = 0;
2144
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002145 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002146 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002147 for (i = 0; i < VIMSPELLMAGICL; ++i)
2148 buf[i] = getc(fd); /* <fileID> */
2149 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2150 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002151 EMSG(_("E757: This does not look like a spell file"));
2152 goto endFAIL;
2153 }
2154 c = getc(fd); /* <versionnr> */
2155 if (c < VIMSPELLVERSION)
2156 {
2157 EMSG(_("E771: Old spell file, needs to be updated"));
2158 goto endFAIL;
2159 }
2160 else if (c > VIMSPELLVERSION)
2161 {
2162 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002163 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002164 }
2165
Bram Moolenaar5195e452005-08-19 20:32:47 +00002166
2167 /*
2168 * <SECTIONS>: <section> ... <sectionend>
2169 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2170 */
2171 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002172 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002173 n = getc(fd); /* <sectionID> or <sectionend> */
2174 if (n == SN_END)
2175 break;
2176 c = getc(fd); /* <sectionflags> */
2177 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2178 /* <sectionlen> */
2179 if (len < 0)
2180 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002181
Bram Moolenaar5195e452005-08-19 20:32:47 +00002182 res = 0;
2183 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002184 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002185 case SN_REGION:
2186 res = read_region_section(fd, lp, len);
2187 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002188
Bram Moolenaar5195e452005-08-19 20:32:47 +00002189 case SN_CHARFLAGS:
2190 res = read_charflags_section(fd);
2191 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002192
Bram Moolenaar5195e452005-08-19 20:32:47 +00002193 case SN_MIDWORD:
2194 lp->sl_midword = read_string(fd, len); /* <midword> */
2195 if (lp->sl_midword == NULL)
2196 goto endFAIL;
2197 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002198
Bram Moolenaar5195e452005-08-19 20:32:47 +00002199 case SN_PREFCOND:
2200 res = read_prefcond_section(fd, lp);
2201 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002202
Bram Moolenaar5195e452005-08-19 20:32:47 +00002203 case SN_REP:
2204 res = read_rep_section(fd, lp);
2205 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002206
Bram Moolenaar5195e452005-08-19 20:32:47 +00002207 case SN_SAL:
2208 res = read_sal_section(fd, lp);
2209 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002210
Bram Moolenaar5195e452005-08-19 20:32:47 +00002211 case SN_SOFO:
2212 res = read_sofo_section(fd, lp);
2213 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002214
Bram Moolenaar5195e452005-08-19 20:32:47 +00002215 case SN_MAP:
2216 p = read_string(fd, len); /* <mapstr> */
2217 if (p == NULL)
2218 goto endFAIL;
2219 set_map_str(lp, p);
2220 vim_free(p);
2221 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002222
Bram Moolenaar5195e452005-08-19 20:32:47 +00002223 case SN_COMPOUND:
2224 res = read_compound(fd, lp, len);
2225 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002226
Bram Moolenaar5195e452005-08-19 20:32:47 +00002227 case SN_SYLLABLE:
2228 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2229 if (lp->sl_syllable == NULL)
2230 goto endFAIL;
2231 if (init_syl_tab(lp) == FAIL)
2232 goto endFAIL;
2233 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002234
Bram Moolenaar5195e452005-08-19 20:32:47 +00002235 default:
2236 /* Unsupported section. When it's required give an error
2237 * message. When it's not required skip the contents. */
2238 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002239 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002240 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002241 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002242 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002243 while (--len >= 0)
2244 if (getc(fd) < 0)
2245 goto truncerr;
2246 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002247 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002248 if (res == SP_FORMERROR)
2249 {
2250formerr:
2251 EMSG(_(e_format));
2252 goto endFAIL;
2253 }
2254 if (res == SP_TRUNCERROR)
2255 {
2256truncerr:
2257 EMSG(_(e_spell_trunc));
2258 goto endFAIL;
2259 }
2260 if (res == SP_ERROR)
2261 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002262 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002263
Bram Moolenaar51485f02005-06-04 21:55:20 +00002264 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002265 * round 2: <KWORDTREE>
2266 * round 3: <PREFIXTREE> */
2267 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002268 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002269 /* The tree size was computed when writing the file, so that we can
2270 * allocate it as one long block. <nodecount> */
2271 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2272 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002273 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002274 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002276 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002277 bp = lalloc((long_u)len, TRUE);
2278 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002279 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002280 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002281 lp->sl_fbyts = bp;
2282 else if (round == 2)
2283 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002284 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002285 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002286
2287 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002288 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2289 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002290 goto endFAIL;
2291 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002292 lp->sl_fidxs = ip;
2293 else if (round == 2)
2294 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002295 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002296 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002297
2298 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002299 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002300 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002301 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002302 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002303 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002304 }
2305 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002306
Bram Moolenaarb765d632005-06-07 21:00:02 +00002307 /* For a new file link it in the list of spell files. */
2308 if (old_lp == NULL)
2309 {
2310 lp->sl_next = first_lang;
2311 first_lang = lp;
2312 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002313
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002314 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002316endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002317 if (lang != NULL)
2318 /* truncating the name signals the error to spell_load_lang() */
2319 *lang = NUL;
2320 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002321 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002322 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002323 lp = NULL;
2324 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002325
2326endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002327 if (fd != NULL)
2328 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002329 sourcing_name = save_sourcing_name;
2330 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002331
2332 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002333}
2334
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002335/*
2336 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002337 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002338 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002339 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2340 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002341 */
2342 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002343read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002344 FILE *fd;
2345 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002346 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002347{
2348 int cnt = 0;
2349 int i;
2350 char_u *str;
2351
2352 /* read the length bytes, MSB first */
2353 for (i = 0; i < cnt_bytes; ++i)
2354 cnt = (cnt << 8) + getc(fd);
2355 if (cnt < 0)
2356 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002357 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002358 return NULL;
2359 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002360 *cntp = cnt;
2361 if (cnt == 0)
2362 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002363
Bram Moolenaar5195e452005-08-19 20:32:47 +00002364 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002365 if (str == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002366 *cntp = SP_ERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002367 return str;
2368}
2369
Bram Moolenaar7887d882005-07-01 22:33:52 +00002370/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002371 * Read a string of length "cnt" from "fd" into allocated memory.
2372 * Returns NULL when out of memory.
2373 */
2374 static char_u *
2375read_string(fd, cnt)
2376 FILE *fd;
2377 int cnt;
2378{
2379 char_u *str;
2380 int i;
2381
2382 /* allocate memory */
2383 str = alloc((unsigned)cnt + 1);
2384 if (str != NULL)
2385 {
2386 /* Read the string. Doesn't check for truncated file. */
2387 for (i = 0; i < cnt; ++i)
2388 str[i] = getc(fd);
2389 str[i] = NUL;
2390 }
2391 return str;
2392}
2393
2394/*
2395 * Read SN_REGION: <regionname> ...
2396 * Return SP_*ERROR flags.
2397 */
2398 static int
2399read_region_section(fd, lp, len)
2400 FILE *fd;
2401 slang_T *lp;
2402 int len;
2403{
2404 int i;
2405
2406 if (len > 16)
2407 return SP_FORMERROR;
2408 for (i = 0; i < len; ++i)
2409 lp->sl_regions[i] = getc(fd); /* <regionname> */
2410 lp->sl_regions[len] = NUL;
2411 return 0;
2412}
2413
2414/*
2415 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2416 * <folcharslen> <folchars>
2417 * Return SP_*ERROR flags.
2418 */
2419 static int
2420read_charflags_section(fd)
2421 FILE *fd;
2422{
2423 char_u *flags;
2424 char_u *fol;
2425 int flagslen, follen;
2426
2427 /* <charflagslen> <charflags> */
2428 flags = read_cnt_string(fd, 1, &flagslen);
2429 if (flagslen < 0)
2430 return flagslen;
2431
2432 /* <folcharslen> <folchars> */
2433 fol = read_cnt_string(fd, 2, &follen);
2434 if (follen < 0)
2435 {
2436 vim_free(flags);
2437 return follen;
2438 }
2439
2440 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2441 if (flags != NULL && fol != NULL)
2442 set_spell_charflags(flags, flagslen, fol);
2443
2444 vim_free(flags);
2445 vim_free(fol);
2446
2447 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2448 if ((flags == NULL) != (fol == NULL))
2449 return SP_FORMERROR;
2450 return 0;
2451}
2452
2453/*
2454 * Read SN_PREFCOND section.
2455 * Return SP_*ERROR flags.
2456 */
2457 static int
2458read_prefcond_section(fd, lp)
2459 FILE *fd;
2460 slang_T *lp;
2461{
2462 int cnt;
2463 int i;
2464 int n;
2465 char_u *p;
2466 char_u buf[MAXWLEN + 1];
2467
2468 /* <prefcondcnt> <prefcond> ... */
2469 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2470 if (cnt <= 0)
2471 return SP_FORMERROR;
2472
2473 lp->sl_prefprog = (regprog_T **)alloc_clear(
2474 (unsigned)sizeof(regprog_T *) * cnt);
2475 if (lp->sl_prefprog == NULL)
2476 return SP_ERROR;
2477 lp->sl_prefixcnt = cnt;
2478
2479 for (i = 0; i < cnt; ++i)
2480 {
2481 /* <prefcond> : <condlen> <condstr> */
2482 n = getc(fd); /* <condlen> */
2483 if (n < 0 || n >= MAXWLEN)
2484 return SP_FORMERROR;
2485
2486 /* When <condlen> is zero we have an empty condition. Otherwise
2487 * compile the regexp program used to check for the condition. */
2488 if (n > 0)
2489 {
2490 buf[0] = '^'; /* always match at one position only */
2491 p = buf + 1;
2492 while (n-- > 0)
2493 *p++ = getc(fd); /* <condstr> */
2494 *p = NUL;
2495 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2496 }
2497 }
2498 return 0;
2499}
2500
2501/*
2502 * Read REP items section from "fd": <repcount> <rep> ...
2503 * Return SP_*ERROR flags.
2504 */
2505 static int
2506read_rep_section(fd, slang)
2507 FILE *fd;
2508 slang_T *slang;
2509{
2510 int cnt;
2511 garray_T *gap;
2512 fromto_T *ftp;
2513 short *first;
2514 int i;
2515
2516 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2517 if (cnt < 0)
2518 return SP_TRUNCERROR;
2519
2520 gap = &slang->sl_rep;
2521 if (ga_grow(gap, cnt) == FAIL)
2522 return SP_ERROR;
2523
2524 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2525 for (; gap->ga_len < cnt; ++gap->ga_len)
2526 {
2527 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2528 ftp->ft_from = read_cnt_string(fd, 1, &i);
2529 if (i < 0)
2530 return i;
2531 if (i == 0)
2532 return SP_FORMERROR;
2533 ftp->ft_to = read_cnt_string(fd, 1, &i);
2534 if (i <= 0)
2535 {
2536 vim_free(ftp->ft_from);
2537 if (i < 0)
2538 return i;
2539 return SP_FORMERROR;
2540 }
2541 }
2542
2543 /* Fill the first-index table. */
2544 first = slang->sl_rep_first;
2545 for (i = 0; i < 256; ++i)
2546 first[i] = -1;
2547 for (i = 0; i < gap->ga_len; ++i)
2548 {
2549 ftp = &((fromto_T *)gap->ga_data)[i];
2550 if (first[*ftp->ft_from] == -1)
2551 first[*ftp->ft_from] = i;
2552 }
2553 return 0;
2554}
2555
2556/*
2557 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2558 * Return SP_*ERROR flags.
2559 */
2560 static int
2561read_sal_section(fd, slang)
2562 FILE *fd;
2563 slang_T *slang;
2564{
2565 int i;
2566 int cnt;
2567 garray_T *gap;
2568 salitem_T *smp;
2569 int ccnt;
2570 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002571 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002572
2573 slang->sl_sofo = FALSE;
2574
2575 i = getc(fd); /* <salflags> */
2576 if (i & SAL_F0LLOWUP)
2577 slang->sl_followup = TRUE;
2578 if (i & SAL_COLLAPSE)
2579 slang->sl_collapse = TRUE;
2580 if (i & SAL_REM_ACCENTS)
2581 slang->sl_rem_accents = TRUE;
2582
2583 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2584 if (cnt < 0)
2585 return SP_TRUNCERROR;
2586
2587 gap = &slang->sl_sal;
2588 ga_init2(gap, sizeof(salitem_T), 10);
2589 if (ga_grow(gap, cnt) == FAIL)
2590 return SP_ERROR;
2591
2592 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2593 for (; gap->ga_len < cnt; ++gap->ga_len)
2594 {
2595 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2596 ccnt = getc(fd); /* <salfromlen> */
2597 if (ccnt < 0)
2598 return SP_TRUNCERROR;
2599 if ((p = alloc(ccnt + 2)) == NULL)
2600 return SP_ERROR;
2601 smp->sm_lead = p;
2602
2603 /* Read up to the first special char into sm_lead. */
2604 for (i = 0; i < ccnt; ++i)
2605 {
2606 c = getc(fd); /* <salfrom> */
2607 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2608 break;
2609 *p++ = c;
2610 }
2611 smp->sm_leadlen = p - smp->sm_lead;
2612 *p++ = NUL;
2613
2614 /* Put (abc) chars in sm_oneof, if any. */
2615 if (c == '(')
2616 {
2617 smp->sm_oneof = p;
2618 for (++i; i < ccnt; ++i)
2619 {
2620 c = getc(fd); /* <salfrom> */
2621 if (c == ')')
2622 break;
2623 *p++ = c;
2624 }
2625 *p++ = NUL;
2626 if (++i < ccnt)
2627 c = getc(fd);
2628 }
2629 else
2630 smp->sm_oneof = NULL;
2631
2632 /* Any following chars go in sm_rules. */
2633 smp->sm_rules = p;
2634 if (i < ccnt)
2635 /* store the char we got while checking for end of sm_lead */
2636 *p++ = c;
2637 for (++i; i < ccnt; ++i)
2638 *p++ = getc(fd); /* <salfrom> */
2639 *p++ = NUL;
2640
2641 /* <saltolen> <salto> */
2642 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2643 if (ccnt < 0)
2644 {
2645 vim_free(smp->sm_lead);
2646 return ccnt;
2647 }
2648
2649#ifdef FEAT_MBYTE
2650 if (has_mbyte)
2651 {
2652 /* convert the multi-byte strings to wide char strings */
2653 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2654 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2655 if (smp->sm_oneof == NULL)
2656 smp->sm_oneof_w = NULL;
2657 else
2658 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2659 if (smp->sm_to == NULL)
2660 smp->sm_to_w = NULL;
2661 else
2662 smp->sm_to_w = mb_str2wide(smp->sm_to);
2663 if (smp->sm_lead_w == NULL
2664 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2665 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2666 {
2667 vim_free(smp->sm_lead);
2668 vim_free(smp->sm_to);
2669 vim_free(smp->sm_lead_w);
2670 vim_free(smp->sm_oneof_w);
2671 vim_free(smp->sm_to_w);
2672 return SP_ERROR;
2673 }
2674 }
2675#endif
2676 }
2677
2678 /* Fill the first-index table. */
2679 set_sal_first(slang);
2680
2681 return 0;
2682}
2683
2684/*
2685 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2686 * Return SP_*ERROR flags.
2687 */
2688 static int
2689read_sofo_section(fd, slang)
2690 FILE *fd;
2691 slang_T *slang;
2692{
2693 int cnt;
2694 char_u *from, *to;
2695 int res;
2696
2697 slang->sl_sofo = TRUE;
2698
2699 /* <sofofromlen> <sofofrom> */
2700 from = read_cnt_string(fd, 2, &cnt);
2701 if (cnt < 0)
2702 return cnt;
2703
2704 /* <sofotolen> <sofoto> */
2705 to = read_cnt_string(fd, 2, &cnt);
2706 if (cnt < 0)
2707 {
2708 vim_free(from);
2709 return cnt;
2710 }
2711
2712 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2713 if (from != NULL && to != NULL)
2714 res = set_sofo(slang, from, to);
2715 else if (from != NULL || to != NULL)
2716 res = SP_FORMERROR; /* only one of two strings is an error */
2717 else
2718 res = 0;
2719
2720 vim_free(from);
2721 vim_free(to);
2722 return res;
2723}
2724
2725/*
2726 * Read the compound section from the .spl file:
2727 * <compmax> <compminlen> <compsylmax> <compflags>
2728 * Returns SP_*ERROR flags.
2729 */
2730 static int
2731read_compound(fd, slang, len)
2732 FILE *fd;
2733 slang_T *slang;
2734 int len;
2735{
2736 int todo = len;
2737 int c;
2738 int atstart;
2739 char_u *pat;
2740 char_u *pp;
2741 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002742 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002743
2744 if (todo < 2)
2745 return SP_FORMERROR; /* need at least two bytes */
2746
2747 --todo;
2748 c = getc(fd); /* <compmax> */
2749 if (c < 2)
2750 c = MAXWLEN;
2751 slang->sl_compmax = c;
2752
2753 --todo;
2754 c = getc(fd); /* <compminlen> */
2755 if (c < 1)
2756 c = 3;
2757 slang->sl_compminlen = c;
2758
2759 --todo;
2760 c = getc(fd); /* <compsylmax> */
2761 if (c < 1)
2762 c = MAXWLEN;
2763 slang->sl_compsylmax = c;
2764
2765 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2766 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
2767 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes. */
2768 pat = alloc((unsigned)todo * 2 + 7);
2769 if (pat == NULL)
2770 return SP_ERROR;
2771
Bram Moolenaard12a1322005-08-21 22:08:24 +00002772 /* We also need a list of all flags that can appear at the start and one
2773 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002774 cp = alloc(todo + 1);
2775 if (cp == NULL)
2776 {
2777 vim_free(pat);
2778 return SP_ERROR;
2779 }
2780 slang->sl_compstartflags = cp;
2781 *cp = NUL;
2782
Bram Moolenaard12a1322005-08-21 22:08:24 +00002783 ap = alloc(todo + 1);
2784 if (ap == NULL)
2785 {
2786 vim_free(pat);
2787 return SP_ERROR;
2788 }
2789 slang->sl_compallflags = ap;
2790 *ap = NUL;
2791
Bram Moolenaar5195e452005-08-19 20:32:47 +00002792 pp = pat;
2793 *pp++ = '^';
2794 *pp++ = '\\';
2795 *pp++ = '(';
2796
2797 atstart = 1;
2798 while (todo-- > 0)
2799 {
2800 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002801
2802 /* Add all flags to "sl_compallflags". */
2803 if (vim_strchr((char_u *)"+*[]/", c) == NULL
2804 && vim_strchr(slang->sl_compallflags, c) == NULL)
2805 {
2806 *ap++ = c;
2807 *ap = NUL;
2808 }
2809
Bram Moolenaar5195e452005-08-19 20:32:47 +00002810 if (atstart != 0)
2811 {
2812 /* At start of item: copy flags to "sl_compstartflags". For a
2813 * [abc] item set "atstart" to 2 and copy up to the ']'. */
2814 if (c == '[')
2815 atstart = 2;
2816 else if (c == ']')
2817 atstart = 0;
2818 else
2819 {
2820 if (vim_strchr(slang->sl_compstartflags, c) == NULL)
2821 {
2822 *cp++ = c;
2823 *cp = NUL;
2824 }
2825 if (atstart == 1)
2826 atstart = 0;
2827 }
2828 }
2829 if (c == '/') /* slash separates two items */
2830 {
2831 *pp++ = '\\';
2832 *pp++ = '|';
2833 atstart = 1;
2834 }
2835 else /* normal char, "[abc]" and '*' are copied as-is */
2836 {
2837 if (c == '+')
2838 *pp++ = '\\'; /* "a+" becomes "a\+" */
2839 *pp++ = c;
2840 }
2841 }
2842
2843 *pp++ = '\\';
2844 *pp++ = ')';
2845 *pp++ = '$';
2846 *pp = NUL;
2847
2848 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
2849 vim_free(pat);
2850 if (slang->sl_compprog == NULL)
2851 return SP_FORMERROR;
2852
2853 return 0;
2854}
2855
2856#define SY_MAXLEN 30
2857typedef struct syl_item_S
2858{
2859 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2860 int sy_len;
2861} syl_item_T;
2862
2863/*
2864 * Truncate "slang->sl_syllable" at the first slash and put the following items
2865 * in "slang->sl_syl_items".
2866 */
2867 static int
2868init_syl_tab(slang)
2869 slang_T *slang;
2870{
2871 char_u *p;
2872 char_u *s;
2873 int l;
2874 syl_item_T *syl;
2875
2876 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2877 p = vim_strchr(slang->sl_syllable, '/');
2878 while (p != NULL)
2879 {
2880 *p++ = NUL;
2881 if (p == NUL)
2882 break;
2883 s = p;
2884 p = vim_strchr(p, '/');
2885 if (p == NULL)
2886 l = STRLEN(s);
2887 else
2888 l = p - s;
2889 if (l >= SY_MAXLEN)
2890 return SP_FORMERROR;
2891 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
2892 return SP_ERROR;
2893 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2894 + slang->sl_syl_items.ga_len++;
2895 vim_strncpy(syl->sy_chars, s, l);
2896 syl->sy_len = l;
2897 }
2898 return OK;
2899}
2900
2901/*
2902 * Count the number of syllables in "word".
2903 * When "word" contains spaces the syllables after the last space are counted.
2904 * Returns zero if syllables are not defines.
2905 */
2906 static int
2907count_syllables(slang, word)
2908 slang_T *slang;
2909 char_u *word;
2910{
2911 int cnt = 0;
2912 int skip = FALSE;
2913 char_u *p;
2914 int len;
2915 int i;
2916 syl_item_T *syl;
2917 int c;
2918
2919 if (slang->sl_syllable == NULL)
2920 return 0;
2921
2922 for (p = word; *p != NUL; p += len)
2923 {
2924 /* When running into a space reset counter. */
2925 if (*p == ' ')
2926 {
2927 len = 1;
2928 cnt = 0;
2929 continue;
2930 }
2931
2932 /* Find longest match of syllable items. */
2933 len = 0;
2934 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
2935 {
2936 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
2937 if (syl->sy_len > len
2938 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
2939 len = syl->sy_len;
2940 }
2941 if (len != 0) /* found a match, count syllable */
2942 {
2943 ++cnt;
2944 skip = FALSE;
2945 }
2946 else
2947 {
2948 /* No recognized syllable item, at least a syllable char then? */
2949#ifdef FEAT_MBYTE
2950 c = mb_ptr2char(p);
2951 len = (*mb_ptr2len)(p);
2952#else
2953 c = *p;
2954 len = 1;
2955#endif
2956 if (vim_strchr(slang->sl_syllable, c) == NULL)
2957 skip = FALSE; /* No, search for next syllable */
2958 else if (!skip)
2959 {
2960 ++cnt; /* Yes, count it */
2961 skip = TRUE; /* don't count following syllable chars */
2962 }
2963 }
2964 }
2965 return cnt;
2966}
2967
2968/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00002969 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00002970 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002971 */
2972 static int
2973set_sofo(lp, from, to)
2974 slang_T *lp;
2975 char_u *from;
2976 char_u *to;
2977{
2978 int i;
2979
2980#ifdef FEAT_MBYTE
2981 garray_T *gap;
2982 char_u *s;
2983 char_u *p;
2984 int c;
2985 int *inp;
2986
2987 if (has_mbyte)
2988 {
2989 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2990 * characters. The index is the low byte of the character.
2991 * The list contains from-to pairs with a terminating NUL.
2992 * sl_sal_first[] is used for latin1 "from" characters. */
2993 gap = &lp->sl_sal;
2994 ga_init2(gap, sizeof(int *), 1);
2995 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002996 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002997 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2998 gap->ga_len = 256;
2999
3000 /* First count the number of items for each list. Temporarily use
3001 * sl_sal_first[] for this. */
3002 for (p = from, s = to; *p != NUL && *s != NUL; )
3003 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003004 c = mb_cptr2char_adv(&p);
3005 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003006 if (c >= 256)
3007 ++lp->sl_sal_first[c & 0xff];
3008 }
3009 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003011
3012 /* Allocate the lists. */
3013 for (i = 0; i < 256; ++i)
3014 if (lp->sl_sal_first[i] > 0)
3015 {
3016 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3017 if (p == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003018 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003019 ((int **)gap->ga_data)[i] = (int *)p;
3020 *(int *)p = 0;
3021 }
3022
3023 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3024 * list. */
3025 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3026 for (p = from, s = to; *p != NUL && *s != NUL; )
3027 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003028 c = mb_cptr2char_adv(&p);
3029 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003030 if (c >= 256)
3031 {
3032 /* Append the from-to chars at the end of the list with
3033 * the low byte. */
3034 inp = ((int **)gap->ga_data)[c & 0xff];
3035 while (*inp != 0)
3036 ++inp;
3037 *inp++ = c; /* from char */
3038 *inp++ = i; /* to char */
3039 *inp++ = NUL; /* NUL at the end */
3040 }
3041 else
3042 /* mapping byte to char is done in sl_sal_first[] */
3043 lp->sl_sal_first[c] = i;
3044 }
3045 }
3046 else
3047#endif
3048 {
3049 /* mapping bytes to bytes is done in sl_sal_first[] */
3050 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003051 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003052
3053 for (i = 0; to[i] != NUL; ++i)
3054 lp->sl_sal_first[from[i]] = to[i];
3055 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3056 }
3057
Bram Moolenaar5195e452005-08-19 20:32:47 +00003058 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003059}
3060
3061/*
3062 * Fill the first-index table for "lp".
3063 */
3064 static void
3065set_sal_first(lp)
3066 slang_T *lp;
3067{
3068 salfirst_T *sfirst;
3069 int i;
3070 salitem_T *smp;
3071 int c;
3072 garray_T *gap = &lp->sl_sal;
3073
3074 sfirst = lp->sl_sal_first;
3075 for (i = 0; i < 256; ++i)
3076 sfirst[i] = -1;
3077 smp = (salitem_T *)gap->ga_data;
3078 for (i = 0; i < gap->ga_len; ++i)
3079 {
3080#ifdef FEAT_MBYTE
3081 if (has_mbyte)
3082 /* Use the lowest byte of the first character. For latin1 it's
3083 * the character, for other encodings it should differ for most
3084 * characters. */
3085 c = *smp[i].sm_lead_w & 0xff;
3086 else
3087#endif
3088 c = *smp[i].sm_lead;
3089 if (sfirst[c] == -1)
3090 {
3091 sfirst[c] = i;
3092#ifdef FEAT_MBYTE
3093 if (has_mbyte)
3094 {
3095 int n;
3096
3097 /* Make sure all entries with this byte are following each
3098 * other. Move the ones that are in the wrong position. Do
3099 * keep the same ordering! */
3100 while (i + 1 < gap->ga_len
3101 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3102 /* Skip over entry with same index byte. */
3103 ++i;
3104
3105 for (n = 1; i + n < gap->ga_len; ++n)
3106 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3107 {
3108 salitem_T tsal;
3109
3110 /* Move entry with same index byte after the entries
3111 * we already found. */
3112 ++i;
3113 --n;
3114 tsal = smp[i + n];
3115 mch_memmove(smp + i + 1, smp + i,
3116 sizeof(salitem_T) * n);
3117 smp[i] = tsal;
3118 }
3119 }
3120#endif
3121 }
3122 }
3123}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003124
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003125#ifdef FEAT_MBYTE
3126/*
3127 * Turn a multi-byte string into a wide character string.
3128 * Return it in allocated memory (NULL for out-of-memory)
3129 */
3130 static int *
3131mb_str2wide(s)
3132 char_u *s;
3133{
3134 int *res;
3135 char_u *p;
3136 int i = 0;
3137
3138 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3139 if (res != NULL)
3140 {
3141 for (p = s; *p != NUL; )
3142 res[i++] = mb_ptr2char_adv(&p);
3143 res[i] = NUL;
3144 }
3145 return res;
3146}
3147#endif
3148
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003149/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003150 * Read one row of siblings from the spell file and store it in the byte array
3151 * "byts" and index array "idxs". Recursively read the children.
3152 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003153 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003154 *
3155 * Returns the index follosing the siblings.
3156 * Returns -1 if the file is shorter than expected.
3157 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003158 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003159 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003160read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003161 FILE *fd;
3162 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003163 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003164 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003165 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003166 int prefixtree; /* TRUE for reading PREFIXTREE */
3167 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003168{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003169 int len;
3170 int i;
3171 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003172 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003173 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003174 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003175#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003176
Bram Moolenaar51485f02005-06-04 21:55:20 +00003177 len = getc(fd); /* <siblingcount> */
3178 if (len <= 0)
3179 return -1;
3180
3181 if (startidx + len >= maxidx)
3182 return -2;
3183 byts[idx++] = len;
3184
3185 /* Read the byte values, flag/region bytes and shared indexes. */
3186 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003187 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003188 c = getc(fd); /* <byte> */
3189 if (c < 0)
3190 return -1;
3191 if (c <= BY_SPECIAL)
3192 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003193 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003194 {
3195 /* No flags, all regions. */
3196 idxs[idx] = 0;
3197 c = 0;
3198 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003199 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003200 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003201 if (prefixtree)
3202 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003203 /* Read the optional pflags byte, the prefix ID and the
3204 * condition nr. In idxs[] store the prefix ID in the low
3205 * byte, the condition index shifted up 8 bits, the flags
3206 * shifted up 24 bits. */
3207 if (c == BY_FLAGS)
3208 c = getc(fd) << 24; /* <pflags> */
3209 else
3210 c = 0;
3211
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003212 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003213
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003214 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3215 if (n >= maxprefcondnr)
3216 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003217 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003218 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003219 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003220 {
3221 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003222 * idxs[] the flags go in the low two bytes, region above
3223 * that and prefix ID above the region. */
3224 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003225 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003226 if (c2 == BY_FLAGS2)
3227 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003228 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003229 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003230 if (c & WF_AFX)
3231 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003232 }
3233
Bram Moolenaar51485f02005-06-04 21:55:20 +00003234 idxs[idx] = c;
3235 c = 0;
3236 }
3237 else /* c == BY_INDEX */
3238 {
3239 /* <nodeidx> */
3240 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3241 if (n < 0 || n >= maxidx)
3242 return -2;
3243 idxs[idx] = n + SHARED_MASK;
3244 c = getc(fd); /* <xbyte> */
3245 }
3246 }
3247 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003248 }
3249
Bram Moolenaar51485f02005-06-04 21:55:20 +00003250 /* Recursively read the children for non-shared siblings.
3251 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3252 * remove SHARED_MASK) */
3253 for (i = 1; i <= len; ++i)
3254 if (byts[startidx + i] != 0)
3255 {
3256 if (idxs[startidx + i] & SHARED_MASK)
3257 idxs[startidx + i] &= ~SHARED_MASK;
3258 else
3259 {
3260 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003261 idx = read_tree(fd, byts, idxs, maxidx, idx,
3262 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003263 if (idx < 0)
3264 break;
3265 }
3266 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003267
Bram Moolenaar51485f02005-06-04 21:55:20 +00003268 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003269}
3270
3271/*
3272 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003273 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003274 */
3275 char_u *
3276did_set_spelllang(buf)
3277 buf_T *buf;
3278{
3279 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003280 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003281 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003282 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003283 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003284 int region_mask;
3285 slang_T *lp;
3286 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003287 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003289 int len;
3290 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003291 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003292 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003293 char_u *use_region = NULL;
3294 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003295
3296 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003297 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003298
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003299 /* loop over comma separated language names. */
3300 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003301 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003302 /* Get one language name. */
3303 copy_option_part(&splp, lang, MAXWLEN, ",");
3304
Bram Moolenaar5482f332005-04-17 20:18:43 +00003305 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003306 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003307
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003308 /* If the name ends in ".spl" use it as the name of the spell file.
3309 * If there is a region name let "region" point to it and remove it
3310 * from the name. */
3311 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3312 {
3313 filename = TRUE;
3314
Bram Moolenaarb6356332005-07-18 21:40:44 +00003315 /* Locate a region and remove it from the file name. */
3316 p = vim_strchr(gettail(lang), '_');
3317 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3318 && !ASCII_ISALPHA(p[3]))
3319 {
3320 vim_strncpy(region_cp, p + 1, 2);
3321 mch_memmove(p, p + 3, len - (p - lang) - 2);
3322 len -= 3;
3323 region = region_cp;
3324 }
3325 else
3326 dont_use_region = TRUE;
3327
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003328 /* Check if we loaded this language before. */
3329 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3330 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3331 break;
3332 }
3333 else
3334 {
3335 filename = FALSE;
3336 if (len > 3 && lang[len - 3] == '_')
3337 {
3338 region = lang + len - 2;
3339 len -= 3;
3340 lang[len] = NUL;
3341 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003342 else
3343 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003344
3345 /* Check if we loaded this language before. */
3346 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3347 if (STRICMP(lang, lp->sl_name) == 0)
3348 break;
3349 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003350
Bram Moolenaarb6356332005-07-18 21:40:44 +00003351 if (region != NULL)
3352 {
3353 /* If the region differs from what was used before then don't
3354 * use it for 'spellfile'. */
3355 if (use_region != NULL && STRCMP(region, use_region) != 0)
3356 dont_use_region = TRUE;
3357 use_region = region;
3358 }
3359
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003360 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003361 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003362 {
3363 if (filename)
3364 (void)spell_load_file(lang, lang, NULL, FALSE);
3365 else
3366 spell_load_lang(lang);
3367 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003368
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003369 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003370 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003371 */
3372 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003373 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3374 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003375 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003376 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003377 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003378 {
3379 /* find region in sl_regions */
3380 c = find_region(lp->sl_regions, region);
3381 if (c == REGION_ALL)
3382 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003383 if (lp->sl_add)
3384 {
3385 if (*lp->sl_regions != NUL)
3386 /* This addition file is for other regions. */
3387 region_mask = 0;
3388 }
3389 else
3390 /* This is probably an error. Give a warning and
3391 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003392 smsg((char_u *)
3393 _("Warning: region %s not supported"),
3394 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003395 }
3396 else
3397 region_mask = 1 << c;
3398 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003399
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003400 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003401 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003402 if (ga_grow(&ga, 1) == FAIL)
3403 {
3404 ga_clear(&ga);
3405 return e_outofmem;
3406 }
3407 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3408 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3409 ++ga.ga_len;
3410 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003411 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003412 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003413 }
3414
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003415 /* round 0: load int_wordlist, if possible.
3416 * round 1: load first name in 'spellfile'.
3417 * round 2: load second name in 'spellfile.
3418 * etc. */
3419 spf = curbuf->b_p_spf;
3420 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003421 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003422 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003423 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003424 /* Internal wordlist, if there is one. */
3425 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003426 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003427 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003428 }
3429 else
3430 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003431 /* One entry in 'spellfile'. */
3432 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3433 STRCAT(spf_name, ".spl");
3434
3435 /* If it was already found above then skip it. */
3436 for (c = 0; c < ga.ga_len; ++c)
3437 if (fullpathcmp(spf_name,
3438 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
3439 FALSE) == FPC_SAME)
3440 break;
3441 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003442 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003443 }
3444
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003445 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003446 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3447 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3448 break;
3449 if (lp == NULL)
3450 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003451 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003452 * region name, the region is ignored otherwise. for int_wordlist
3453 * use an arbitrary name. */
3454 if (round == 0)
3455 STRCPY(lang, "internal wordlist");
3456 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003457 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003458 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003459 p = vim_strchr(lang, '.');
3460 if (p != NULL)
3461 *p = NUL; /* truncate at ".encoding.add" */
3462 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003463 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003464 }
3465 if (lp != NULL && ga_grow(&ga, 1) == OK)
3466 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003467 region_mask = REGION_ALL;
3468 if (use_region != NULL && !dont_use_region)
3469 {
3470 /* find region in sl_regions */
3471 c = find_region(lp->sl_regions, use_region);
3472 if (c != REGION_ALL)
3473 region_mask = 1 << c;
3474 else if (*lp->sl_regions != NUL)
3475 /* This spell file is for other regions. */
3476 region_mask = 0;
3477 }
3478
3479 if (region_mask != 0)
3480 {
3481 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3482 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3483 ++ga.ga_len;
3484 use_midword(lp, buf);
3485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003486 }
3487 }
3488
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003489 /* Add a NULL entry to mark the end of the list. */
3490 if (ga_grow(&ga, 1) == FAIL)
3491 {
3492 ga_clear(&ga);
3493 return e_outofmem;
3494 }
3495 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
3496 ++ga.ga_len;
3497
3498 /* Everything is fine, store the new b_langp value. */
3499 ga_clear(&buf->b_langp);
3500 buf->b_langp = ga;
3501
3502 return NULL;
3503}
3504
3505/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003506 * Clear the midword characters for buffer "buf".
3507 */
3508 static void
3509clear_midword(buf)
3510 buf_T *buf;
3511{
3512 vim_memset(buf->b_spell_ismw, 0, 256);
3513#ifdef FEAT_MBYTE
3514 vim_free(buf->b_spell_ismw_mb);
3515 buf->b_spell_ismw_mb = NULL;
3516#endif
3517}
3518
3519/*
3520 * Use the "sl_midword" field of language "lp" for buffer "buf".
3521 * They add up to any currently used midword characters.
3522 */
3523 static void
3524use_midword(lp, buf)
3525 slang_T *lp;
3526 buf_T *buf;
3527{
3528 char_u *p;
3529
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003530 if (lp->sl_midword == NULL) /* there aren't any */
3531 return;
3532
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003533 for (p = lp->sl_midword; *p != NUL; )
3534#ifdef FEAT_MBYTE
3535 if (has_mbyte)
3536 {
3537 int c, l, n;
3538 char_u *bp;
3539
3540 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003541 l = (*mb_ptr2len)(p);
3542 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003543 buf->b_spell_ismw[c] = TRUE;
3544 else if (buf->b_spell_ismw_mb == NULL)
3545 /* First multi-byte char in "b_spell_ismw_mb". */
3546 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3547 else
3548 {
3549 /* Append multi-byte chars to "b_spell_ismw_mb". */
3550 n = STRLEN(buf->b_spell_ismw_mb);
3551 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3552 if (bp != NULL)
3553 {
3554 vim_free(buf->b_spell_ismw_mb);
3555 buf->b_spell_ismw_mb = bp;
3556 vim_strncpy(bp + n, p, l);
3557 }
3558 }
3559 p += l;
3560 }
3561 else
3562#endif
3563 buf->b_spell_ismw[*p++] = TRUE;
3564}
3565
3566/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003567 * Find the region "region[2]" in "rp" (points to "sl_regions").
3568 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003569 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003570 */
3571 static int
3572find_region(rp, region)
3573 char_u *rp;
3574 char_u *region;
3575{
3576 int i;
3577
3578 for (i = 0; ; i += 2)
3579 {
3580 if (rp[i] == NUL)
3581 return REGION_ALL;
3582 if (rp[i] == region[0] && rp[i + 1] == region[1])
3583 break;
3584 }
3585 return i / 2;
3586}
3587
3588/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003590 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003591 * Word WF_ONECAP
3592 * W WORD WF_ALLCAP
3593 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003594 */
3595 static int
3596captype(word, end)
3597 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003599{
3600 char_u *p;
3601 int c;
3602 int firstcap;
3603 int allcap;
3604 int past_second = FALSE; /* past second word char */
3605
3606 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003607 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609 return 0; /* only non-word characters, illegal word */
3610#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003611 if (has_mbyte)
3612 c = mb_ptr2char_adv(&p);
3613 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003614#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003615 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003616 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003617
3618 /*
3619 * Need to check all letters to find a word with mixed upper/lower.
3620 * But a word with an upper char only at start is a ONECAP.
3621 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003623 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003624 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003625 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003626 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003627 {
3628 /* UUl -> KEEPCAP */
3629 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003630 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003631 allcap = FALSE;
3632 }
3633 else if (!allcap)
3634 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003635 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003636 past_second = TRUE;
3637 }
3638
3639 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003640 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003641 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003642 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003643 return 0;
3644}
3645
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003646/*
3647 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3648 * capital. So that make_case_word() can turn WOrd into Word.
3649 * Add ALLCAP for "WOrD".
3650 */
3651 static int
3652badword_captype(word, end)
3653 char_u *word;
3654 char_u *end;
3655{
3656 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003657 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003658 int l, u;
3659 int first;
3660 char_u *p;
3661
3662 if (flags & WF_KEEPCAP)
3663 {
3664 /* Count the number of UPPER and lower case letters. */
3665 l = u = 0;
3666 first = FALSE;
3667 for (p = word; p < end; mb_ptr_adv(p))
3668 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003669 c = PTR2CHAR(p);
3670 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003671 {
3672 ++u;
3673 if (p == word)
3674 first = TRUE;
3675 }
3676 else
3677 ++l;
3678 }
3679
3680 /* If there are more UPPER than lower case letters suggest an
3681 * ALLCAP word. Otherwise, if the first letter is UPPER then
3682 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3683 * require three upper case letters. */
3684 if (u > l && u > 2)
3685 flags |= WF_ALLCAP;
3686 else if (first)
3687 flags |= WF_ONECAP;
3688 }
3689 return flags;
3690}
3691
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003692# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3693/*
3694 * Free all languages.
3695 */
3696 void
3697spell_free_all()
3698{
3699 slang_T *lp;
3700 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003701 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003702
3703 /* Go through all buffers and handle 'spelllang'. */
3704 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3705 ga_clear(&buf->b_langp);
3706
3707 while (first_lang != NULL)
3708 {
3709 lp = first_lang;
3710 first_lang = lp->sl_next;
3711 slang_free(lp);
3712 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003713
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003714 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003715 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003716 /* Delete the internal wordlist and its .spl file */
3717 mch_remove(int_wordlist);
3718 int_wordlist_spl(fname);
3719 mch_remove(fname);
3720 vim_free(int_wordlist);
3721 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003722 }
3723
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003724 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003725}
3726# endif
3727
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003728# if defined(FEAT_MBYTE) || defined(PROTO)
3729/*
3730 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003731 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003732 */
3733 void
3734spell_reload()
3735{
3736 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003737 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003738
Bram Moolenaarea408852005-06-25 22:49:46 +00003739 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003740 init_spell_chartab();
3741
3742 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003743 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003744
3745 /* Go through all buffers and handle 'spelllang'. */
3746 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3747 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003748 /* Only load the wordlists when 'spelllang' is set and there is a
3749 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003750 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003751 {
3752 FOR_ALL_WINDOWS(wp)
3753 if (wp->w_buffer == buf && wp->w_p_spell)
3754 {
3755 (void)did_set_spelllang(buf);
3756# ifdef FEAT_WINDOWS
3757 break;
3758# endif
3759 }
3760 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003761 }
3762}
3763# endif
3764
Bram Moolenaarb765d632005-06-07 21:00:02 +00003765/*
3766 * Reload the spell file "fname" if it's loaded.
3767 */
3768 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003769spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003770 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003771 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003772{
3773 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003775
Bram Moolenaarb765d632005-06-07 21:00:02 +00003776 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3777 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3778 {
3779 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003780 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003781 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003784
3785 /* When "zg" was used and the file wasn't loaded yet, should redo
3786 * 'spelllang' to get it loaded. */
3787 if (added_word && !didit)
3788 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003789}
3790
3791
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003792/*
3793 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003794 */
3795
Bram Moolenaar51485f02005-06-04 21:55:20 +00003796#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003797 and .dic file. */
3798/*
3799 * Main structure to store the contents of a ".aff" file.
3800 */
3801typedef struct afffile_S
3802{
3803 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003804 int af_slash; /* character used in word for slash */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003805 int af_rar; /* RAR ID for rare word */
3806 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003807 int af_bad; /* BAD ID for banned word */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003808 int af_needaffix; /* NEEDAFFIX ID */
3809 char_u *af_compflags; /* COMPOUNDFLAG and COMPOUNDFLAGS concat'ed */
3810 int af_compmax; /* COMPOUNDMAX */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003811 int af_compminlen; /* COMPOUNDMIN */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003812 int af_compsylmax; /* COMPOUNDSYLMAX */
3813 char_u *af_syllable; /* SYLLABLE */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003814 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003815 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3816 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003817} afffile_T;
3818
3819typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003820/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3821struct affentry_S
3822{
3823 affentry_T *ae_next; /* next affix with same name/number */
3824 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3825 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003826 char_u *ae_cond; /* condition (NULL for ".") */
3827 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003828 char_u ae_rare; /* rare affix */
3829 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003830};
3831
Bram Moolenaar53805d12005-08-01 07:08:33 +00003832#define AH_KEY_LEN 10
3833
Bram Moolenaar51485f02005-06-04 21:55:20 +00003834/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3835typedef struct affheader_S
3836{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003837 /* key for hashtable == name of affix entry */
3838#ifdef FEAT_MBYTE
3839 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3840#else
3841 char_u ah_key[2]; /* one byte char plus NUL */
3842#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003843 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003844 int ah_combine; /* suffix may combine with prefix */
3845 affentry_T *ah_first; /* first affix entry */
3846} affheader_T;
3847
3848#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3849
3850/*
3851 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003852 * the need to keep track of each allocated thing, everything is freed all at
3853 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003854 */
3855#define SBLOCKSIZE 16000 /* size of sb_data */
3856typedef struct sblock_S sblock_T;
3857struct sblock_S
3858{
3859 sblock_T *sb_next; /* next block in list */
3860 int sb_used; /* nr of bytes already in use */
3861 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003862};
3863
3864/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003865 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003866 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003867typedef struct wordnode_S wordnode_T;
3868struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003869{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003870 union /* shared to save space */
3871 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003872 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003873 int index; /* index in written nodes (valid after first
3874 round) */
3875 } wn_u1;
3876 union /* shared to save space */
3877 {
3878 wordnode_T *next; /* next node with same hash key */
3879 wordnode_T *wnode; /* parent node that will write this node */
3880 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003881 wordnode_T *wn_child; /* child (next byte in word) */
3882 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3883 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003884 int wn_refs; /* Nr. of references to this node. Only
3885 relevant for first node in a list of
3886 siblings, in following siblings it is
3887 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003888 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003889 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003890 prefix ID or 0 */
3891 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3892 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003893 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003894#ifdef SPELL_PRINTTREE
3895 int wn_nr; /* sequence nr for printing */
3896#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003897};
3898
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003899#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3900
Bram Moolenaar51485f02005-06-04 21:55:20 +00003901#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003902
Bram Moolenaar51485f02005-06-04 21:55:20 +00003903/*
3904 * Info used while reading the spell files.
3905 */
3906typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003907{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003908 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003909 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003910
Bram Moolenaar51485f02005-06-04 21:55:20 +00003911 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003912 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003913
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003914 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003915
Bram Moolenaar51485f02005-06-04 21:55:20 +00003916 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003917 long si_blocks_cnt; /* memory blocks allocated */
3918 long si_compress_cnt; /* words to add before lowering
3919 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003920 wordnode_T *si_first_free; /* List of nodes that have been freed during
3921 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003922 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003923#ifdef SPELL_PRINTTREE
3924 int si_wordnode_nr; /* sequence nr for nodes */
3925#endif
3926
3927
Bram Moolenaar51485f02005-06-04 21:55:20 +00003928 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003929 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003930 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003931 int si_region; /* region mask */
3932 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003933 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003934 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003935 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003936 int si_region_count; /* number of regions supported (1 when there
3937 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003938 char_u si_region_name[16]; /* region names; used only if
3939 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940
3941 garray_T si_rep; /* list of fromto_T entries from REP lines */
3942 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003943 char_u *si_sofofr; /* SOFOFROM text */
3944 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003945 int si_followup; /* soundsalike: ? */
3946 int si_collapse; /* soundsalike: ? */
3947 int si_rem_accents; /* soundsalike: remove accents */
3948 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003949 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003950 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003951 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003952 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003953 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003954 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003955 garray_T si_prefcond; /* table with conditions for postponed
3956 * prefixes, each stored as a string */
3957 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003958} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003959
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003960static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003961static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003962static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3963static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003964static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003965static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003966static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00003967static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
3968static void get_compflags __ARGS((spellinfo_T *spin, char_u *afflist, char_u *store_afflist));
3969static 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 +00003970static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
3971static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
3972static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003973static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003974static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00003975static 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 +00003976static 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 +00003977static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
3978static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
3979static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
3980static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
3981static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003982static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003983static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003984static void clear_node __ARGS((wordnode_T *node));
3985static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003986static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003987static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003988
Bram Moolenaar53805d12005-08-01 07:08:33 +00003989/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3990 * but it must be negative to indicate the prefix tree to tree_add_word().
3991 * Use a negative number with the lower 8 bits zero. */
3992#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003993
Bram Moolenaar5195e452005-08-19 20:32:47 +00003994/*
3995 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
3996 */
3997static long compress_start = 30000; /* memory / SBLOCKSIZE */
3998static long compress_inc = 100; /* memory / SBLOCKSIZE */
3999static long compress_added = 500000; /* word count */
4000
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004001#ifdef SPELL_PRINTTREE
4002/*
4003 * For debugging the tree code: print the current tree in a (more or less)
4004 * readable format, so that we can see what happens when adding a word and/or
4005 * compressing the tree.
4006 * Based on code from Olaf Seibert.
4007 */
4008#define PRINTLINESIZE 1000
4009#define PRINTWIDTH 6
4010
4011#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4012 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4013
4014static char line1[PRINTLINESIZE];
4015static char line2[PRINTLINESIZE];
4016static char line3[PRINTLINESIZE];
4017
4018 static void
4019spell_clear_flags(wordnode_T *node)
4020{
4021 wordnode_T *np;
4022
4023 for (np = node; np != NULL; np = np->wn_sibling)
4024 {
4025 np->wn_u1.index = FALSE;
4026 spell_clear_flags(np->wn_child);
4027 }
4028}
4029
4030 static void
4031spell_print_node(wordnode_T *node, int depth)
4032{
4033 if (node->wn_u1.index)
4034 {
4035 /* Done this node before, print the reference. */
4036 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4037 PRINTSOME(line2, depth, " ", 0, 0);
4038 PRINTSOME(line3, depth, " ", 0, 0);
4039 msg(line1);
4040 msg(line2);
4041 msg(line3);
4042 }
4043 else
4044 {
4045 node->wn_u1.index = TRUE;
4046
4047 if (node->wn_byte != NUL)
4048 {
4049 if (node->wn_child != NULL)
4050 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4051 else
4052 /* Cannot happen? */
4053 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4054 }
4055 else
4056 PRINTSOME(line1, depth, " $ ", 0, 0);
4057
4058 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4059
4060 if (node->wn_sibling != NULL)
4061 PRINTSOME(line3, depth, " | ", 0, 0);
4062 else
4063 PRINTSOME(line3, depth, " ", 0, 0);
4064
4065 if (node->wn_byte == NUL)
4066 {
4067 msg(line1);
4068 msg(line2);
4069 msg(line3);
4070 }
4071
4072 /* do the children */
4073 if (node->wn_byte != NUL && node->wn_child != NULL)
4074 spell_print_node(node->wn_child, depth + 1);
4075
4076 /* do the siblings */
4077 if (node->wn_sibling != NULL)
4078 {
4079 /* get rid of all parent details except | */
4080 STRCPY(line1, line3);
4081 STRCPY(line2, line3);
4082 spell_print_node(node->wn_sibling, depth);
4083 }
4084 }
4085}
4086
4087 static void
4088spell_print_tree(wordnode_T *root)
4089{
4090 if (root != NULL)
4091 {
4092 /* Clear the "wn_u1.index" fields, used to remember what has been
4093 * done. */
4094 spell_clear_flags(root);
4095
4096 /* Recursively print the tree. */
4097 spell_print_node(root, 0);
4098 }
4099}
4100#endif /* SPELL_PRINTTREE */
4101
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004102/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004104 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004105 */
4106 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004107spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004108 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004109 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110{
4111 FILE *fd;
4112 afffile_T *aff;
4113 char_u rline[MAXLINELEN];
4114 char_u *line;
4115 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004116#define MAXITEMCNT 7
4117 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004118 int itemcnt;
4119 char_u *p;
4120 int lnum = 0;
4121 affheader_T *cur_aff = NULL;
4122 int aff_todo = 0;
4123 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004124 char_u *low = NULL;
4125 char_u *fol = NULL;
4126 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004127 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 int do_rep;
4129 int do_sal;
4130 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004131 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004132 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004133 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004134 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004135 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004136
Bram Moolenaar51485f02005-06-04 21:55:20 +00004137 /*
4138 * Open the file.
4139 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004140 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004141 if (fd == NULL)
4142 {
4143 EMSG2(_(e_notopen), fname);
4144 return NULL;
4145 }
4146
Bram Moolenaarb765d632005-06-07 21:00:02 +00004147 if (spin->si_verbose || p_verbose > 2)
4148 {
4149 if (!spin->si_verbose)
4150 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004151 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004152 out_flush();
4153 if (!spin->si_verbose)
4154 verbose_leave();
4155 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 /* Only do REP lines when not done in another .aff file already. */
4158 do_rep = spin->si_rep.ga_len == 0;
4159
4160 /* Only do SAL lines when not done in another .aff file already. */
4161 do_sal = spin->si_sal.ga_len == 0;
4162
4163 /* Only do MAP lines when not done in another .aff file already. */
4164 do_map = spin->si_map.ga_len == 0;
4165
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004166 /* Only do MIDWORD line when not done in another .aff file already */
4167 do_midword = spin->si_midword == NULL;
4168
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004169 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
4170 do_sofo = spin->si_sofofr == NULL;
4171
Bram Moolenaar51485f02005-06-04 21:55:20 +00004172 /*
4173 * Allocate and init the afffile_T structure.
4174 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004175 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004176 if (aff == NULL)
4177 return NULL;
4178 hash_init(&aff->af_pref);
4179 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004180
4181 /*
4182 * Read all the lines in the file one by one.
4183 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004184 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004185 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004186 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004187 ++lnum;
4188
4189 /* Skip comment lines. */
4190 if (*rline == '#')
4191 continue;
4192
4193 /* Convert from "SET" to 'encoding' when needed. */
4194 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004195#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004196 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004198 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004199 if (pc == NULL)
4200 {
4201 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4202 fname, lnum, rline);
4203 continue;
4204 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004205 line = pc;
4206 }
4207 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004208#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209 {
4210 pc = NULL;
4211 line = rline;
4212 }
4213
4214 /* Split the line up in white separated items. Put a NUL after each
4215 * item. */
4216 itemcnt = 0;
4217 for (p = line; ; )
4218 {
4219 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4220 ++p;
4221 if (*p == NUL)
4222 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004223 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004224 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004225 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004226 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227 ++p;
4228 if (*p == NUL)
4229 break;
4230 *p++ = NUL;
4231 }
4232
4233 /* Handle non-empty lines. */
4234 if (itemcnt > 0)
4235 {
4236 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4237 && aff->af_enc == NULL)
4238 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004239#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004240 /* Setup for conversion from "ENC" to 'encoding'. */
4241 aff->af_enc = enc_canonize(items[1]);
4242 if (aff->af_enc != NULL && !spin->si_ascii
4243 && convert_setup(&spin->si_conv, aff->af_enc,
4244 p_enc) == FAIL)
4245 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4246 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004247 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004248#else
4249 smsg((char_u *)_("Conversion in %s not supported"), fname);
4250#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004251 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004252 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
4253 {
4254 if (do_midword)
4255 spin->si_midword = vim_strsave(items[1]);
4256 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004257 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004265 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4266 && aff->af_slash == 0)
4267 {
4268 aff->af_slash = items[1][0];
4269 if (items[1][1] != NUL)
4270 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4271 fname, lnum, items[1]);
4272 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004273 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4274 && aff->af_rar == 0)
4275 {
4276 aff->af_rar = items[1][0];
4277 if (items[1][1] != NUL)
4278 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4279 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004280 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4281 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004282 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004283 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004284 if (items[1][1] != NUL)
4285 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4286 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004287 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4288 && aff->af_bad == 0)
4289 {
4290 aff->af_bad = items[1][0];
4291 if (items[1][1] != NUL)
4292 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4293 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004294 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4295 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004296 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004297 aff->af_needaffix = items[1][0];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004298 if (items[1][1] != NUL)
4299 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4300 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004301 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
4302 && aff->af_compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004303 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004304 p = getroom(spin, 3, FALSE);
4305 if (p != NULL)
4306 {
4307 /* Turn single flag "c" into COMPOUNDFLAGS compatible
4308 * string "c+". */
4309 p[0] = items[1][0];
4310 p[1] = '+';
4311 p[2] = NUL;
4312 aff->af_compflags = p;
4313 }
4314 if (items[1][1] != NUL)
4315 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4316 }
4317 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4318 {
4319 /* Concatenate this string to previously defined ones, using a
4320 * slash to separate them. */
4321 l = STRLEN(items[1]) + 1;
4322 if (aff->af_compflags != NULL)
4323 l += STRLEN(aff->af_compflags) + 1;
4324 p = getroom(spin, l, FALSE);
4325 if (p != NULL)
4326 {
4327 if (aff->af_compflags != NULL)
4328 {
4329 STRCPY(p, aff->af_compflags);
4330 STRCAT(p, "/");
4331 }
4332 STRCAT(p, items[1]);
4333 aff->af_compflags = p;
4334 }
4335 }
4336 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
4337 && aff->af_compmax == 0)
4338 {
4339 aff->af_compmax = atoi((char *)items[1]);
4340 if (aff->af_compmax == 0)
4341 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4342 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004343 }
4344 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
4345 && aff->af_compminlen == 0)
4346 {
4347 aff->af_compminlen = atoi((char *)items[1]);
4348 if (aff->af_compminlen == 0)
4349 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4350 fname, lnum, items[1]);
4351 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004352 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
4353 && aff->af_compsylmax == 0)
4354 {
4355 aff->af_compsylmax = atoi((char *)items[1]);
4356 if (aff->af_compsylmax == 0)
4357 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4358 fname, lnum, items[1]);
4359 }
4360 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
4361 && aff->af_syllable == NULL)
4362 {
4363 aff->af_syllable = getroom_save(spin, items[1]);
4364 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004365 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4366 {
4367 aff->af_pfxpostpone = TRUE;
4368 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004369 else if ((STRCMP(items[0], "PFX") == 0
4370 || STRCMP(items[0], "SFX") == 0)
4371 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004372 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004373 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00004374 /* Myspell allows extra text after the item, but that might
4375 * mean mistakes go unnoticed. Require a comment-starter. */
4376 if (itemcnt > 4 && *items[4] != '#')
4377 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4378 fname, lnum, items[4]);
4379
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004380 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004381 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004382 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004383 if (cur_aff == NULL)
4384 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004385#ifdef FEAT_MBYTE
4386 if (has_mbyte)
4387 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004388 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004389 if (l >= AH_KEY_LEN)
4390 l = 1; /* too long, must be an overlong sequence */
4391 else
4392 mch_memmove(cur_aff->ah_key, items[1], l);
4393 }
4394 else
4395#endif
4396 {
4397 *cur_aff->ah_key = *items[1];
4398 l = 1;
4399 }
4400 cur_aff->ah_key[l] = NUL;
4401 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004402 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004403 if (*items[2] == 'Y')
4404 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004405 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004406 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4407 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004408
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004409 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004410 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004411 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004412 /* Use a new number in the .spl file later, to be able to
4413 * handle multiple .aff files. */
4414 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004415 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004416 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004417 else
4418 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004419 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004420 hi = hash_find(tp, cur_aff->ah_key);
4421 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00004422 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004423 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4424 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004425 aff_todo = 0;
4426 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004427 else
4428 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004429 }
4430 else if ((STRCMP(items[0], "PFX") == 0
4431 || STRCMP(items[0], "SFX") == 0)
4432 && aff_todo > 0
4433 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004434 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004435 {
4436 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004437 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004438 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004439 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004440 int lasti = 5;
4441
Bram Moolenaar5195e452005-08-19 20:32:47 +00004442 /* Check for "rare" and "nocomp" after the other info. */
4443 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004444 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004445 if (!rare && STRICMP(items[lasti], "rare") == 0)
4446 {
4447 rare = TRUE;
4448 ++lasti;
4449 }
4450 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4451 {
4452 nocomp = TRUE;
4453 ++lasti;
4454 }
4455 else
4456 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004457 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004458
Bram Moolenaar8db73182005-06-17 21:51:16 +00004459 /* Myspell allows extra text after the item, but that might
4460 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004461 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004462 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004463
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 /* New item for an affix letter. */
4465 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004466 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004467 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004468 if (aff_entry == NULL)
4469 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004470 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004471 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004472
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004473 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004474 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004476 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004477
Bram Moolenaar51485f02005-06-04 21:55:20 +00004478 /* Don't use an affix entry with non-ASCII characters when
4479 * "spin->si_ascii" is TRUE. */
4480 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004481 || has_non_ascii(aff_entry->ae_add)))
4482 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004483 aff_entry->ae_next = cur_aff->ah_first;
4484 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485
4486 if (STRCMP(items[4], ".") != 0)
4487 {
4488 char_u buf[MAXLINELEN];
4489
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004490 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004491 if (*items[0] == 'P')
4492 sprintf((char *)buf, "^%s", items[4]);
4493 else
4494 sprintf((char *)buf, "%s$", items[4]);
4495 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004496 RE_MAGIC + RE_STRING + RE_STRICT);
4497 if (aff_entry->ae_prog == NULL)
4498 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4499 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004500 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004501
4502 /* For postponed prefixes we need an entry in si_prefcond
4503 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004504 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004505 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004506 /* When the chop string is one lower-case letter and
4507 * the add string ends in the upper-case letter we set
4508 * the "upper" flag, clear "ae_chop" and remove the
4509 * letters from "ae_add". The condition must either
4510 * be empty or start with the same letter. */
4511 if (aff_entry->ae_chop != NULL
4512 && aff_entry->ae_add != NULL
4513#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004514 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004515 aff_entry->ae_chop)] == NUL
4516#else
4517 && aff_entry->ae_chop[1] == NUL
4518#endif
4519 )
4520 {
4521 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004522
Bram Moolenaar53805d12005-08-01 07:08:33 +00004523 c = PTR2CHAR(aff_entry->ae_chop);
4524 c_up = SPELL_TOUPPER(c);
4525 if (c_up != c
4526 && (aff_entry->ae_cond == NULL
4527 || PTR2CHAR(aff_entry->ae_cond) == c))
4528 {
4529 p = aff_entry->ae_add
4530 + STRLEN(aff_entry->ae_add);
4531 mb_ptr_back(aff_entry->ae_add, p);
4532 if (PTR2CHAR(p) == c_up)
4533 {
4534 upper = TRUE;
4535 aff_entry->ae_chop = NULL;
4536 *p = NUL;
4537
4538 /* The condition is matched with the
4539 * actual word, thus must check for the
4540 * upper-case letter. */
4541 if (aff_entry->ae_cond != NULL)
4542 {
4543 char_u buf[MAXLINELEN];
4544#ifdef FEAT_MBYTE
4545 if (has_mbyte)
4546 {
4547 onecap_copy(items[4], buf, TRUE);
4548 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004549 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004550 }
4551 else
4552#endif
4553 *aff_entry->ae_cond = c_up;
4554 if (aff_entry->ae_cond != NULL)
4555 {
4556 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004557 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004558 vim_free(aff_entry->ae_prog);
4559 aff_entry->ae_prog = vim_regcomp(
4560 buf, RE_MAGIC + RE_STRING);
4561 }
4562 }
4563 }
4564 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004565 }
4566
Bram Moolenaar53805d12005-08-01 07:08:33 +00004567 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004568 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004569 int idx;
4570 char_u **pp;
4571 int n;
4572
4573 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4574 --idx)
4575 {
4576 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4577 if (str_equal(p, aff_entry->ae_cond))
4578 break;
4579 }
4580 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4581 {
4582 /* Not found, add a new condition. */
4583 idx = spin->si_prefcond.ga_len++;
4584 pp = ((char_u **)spin->si_prefcond.ga_data)
4585 + idx;
4586 if (aff_entry->ae_cond == NULL)
4587 *pp = NULL;
4588 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004589 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004590 aff_entry->ae_cond);
4591 }
4592
4593 /* Add the prefix to the prefix tree. */
4594 if (aff_entry->ae_add == NULL)
4595 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004596 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004597 p = aff_entry->ae_add;
4598 /* PFX_FLAGS is a negative number, so that
4599 * tree_add_word() knows this is the prefix tree. */
4600 n = PFX_FLAGS;
4601 if (rare)
4602 n |= WFP_RARE;
4603 if (!cur_aff->ah_combine)
4604 n |= WFP_NC;
4605 if (upper)
4606 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004607 tree_add_word(spin, p, spin->si_prefroot, n,
4608 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004609 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004610 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004611 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004612 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004613 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
4614 {
4615 if (fol != NULL)
4616 smsg((char_u *)_("Duplicate FOL in %s line %d"),
4617 fname, lnum);
4618 else
4619 fol = vim_strsave(items[1]);
4620 }
4621 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
4622 {
4623 if (low != NULL)
4624 smsg((char_u *)_("Duplicate LOW in %s line %d"),
4625 fname, lnum);
4626 else
4627 low = vim_strsave(items[1]);
4628 }
4629 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
4630 {
4631 if (upp != NULL)
4632 smsg((char_u *)_("Duplicate UPP in %s line %d"),
4633 fname, lnum);
4634 else
4635 upp = vim_strsave(items[1]);
4636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004639 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004640 if (!isdigit(*items[1]))
4641 smsg((char_u *)_("Expected REP count in %s line %d"),
4642 fname, lnum);
4643 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004644 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004645 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004647 /* Myspell ignores extra arguments, we require it starts with
4648 * # to detect mistakes. */
4649 if (itemcnt > 3 && items[3][0] != '#')
4650 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004651 if (do_rep)
4652 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4653 }
4654 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4655 {
4656 /* MAP item or count */
4657 if (!found_map)
4658 {
4659 /* First line contains the count. */
4660 found_map = TRUE;
4661 if (!isdigit(*items[1]))
4662 smsg((char_u *)_("Expected MAP count in %s line %d"),
4663 fname, lnum);
4664 }
4665 else if (do_map)
4666 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004667 int c;
4668
4669 /* Check that every character appears only once. */
4670 for (p = items[1]; *p != NUL; )
4671 {
4672#ifdef FEAT_MBYTE
4673 c = mb_ptr2char_adv(&p);
4674#else
4675 c = *p++;
4676#endif
4677 if ((spin->si_map.ga_len > 0
4678 && vim_strchr(spin->si_map.ga_data, c)
4679 != NULL)
4680 || vim_strchr(p, c) != NULL)
4681 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4682 fname, lnum);
4683 }
4684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004685 /* We simply concatenate all the MAP strings, separated by
4686 * slashes. */
4687 ga_concat(&spin->si_map, items[1]);
4688 ga_append(&spin->si_map, '/');
4689 }
4690 }
4691 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4692 {
4693 if (do_sal)
4694 {
4695 /* SAL item (sounds-a-like)
4696 * Either one of the known keys or a from-to pair. */
4697 if (STRCMP(items[1], "followup") == 0)
4698 spin->si_followup = sal_to_bool(items[2]);
4699 else if (STRCMP(items[1], "collapse_result") == 0)
4700 spin->si_collapse = sal_to_bool(items[2]);
4701 else if (STRCMP(items[1], "remove_accents") == 0)
4702 spin->si_rem_accents = sal_to_bool(items[2]);
4703 else
4704 /* when "to" is "_" it means empty */
4705 add_fromto(spin, &spin->si_sal, items[1],
4706 STRCMP(items[2], "_") == 0 ? (char_u *)""
4707 : items[2]);
4708 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004709 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004710 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
4711 && (!do_sofo || spin->si_sofofr == NULL))
4712 {
4713 if (do_sofo)
4714 spin->si_sofofr = vim_strsave(items[1]);
4715 }
4716 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
4717 && (!do_sofo || spin->si_sofoto == NULL))
4718 {
4719 if (do_sofo)
4720 spin->si_sofoto = vim_strsave(items[1]);
4721 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004722 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004723 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004724 fname, lnum, items[0]);
4725 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004726 }
4727
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004728 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
4729 smsg((char_u *)_("Missing SOFO%s line in %s"),
4730 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
4731 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
4732 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
4733
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004734 if (fol != NULL || low != NULL || upp != NULL)
4735 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004736 if (spin->si_clear_chartab)
4737 {
4738 /* Clear the char type tables, don't want to use any of the
4739 * currently used spell properties. */
4740 init_spell_chartab();
4741 spin->si_clear_chartab = FALSE;
4742 }
4743
Bram Moolenaar3982c542005-06-08 21:56:31 +00004744 /*
4745 * Don't write a word table for an ASCII file, so that we don't check
4746 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004747 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004748 * mb_get_class(), the list of chars in the file will be incomplete.
4749 */
4750 if (!spin->si_ascii
4751#ifdef FEAT_MBYTE
4752 && !enc_utf8
4753#endif
4754 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004755 {
4756 if (fol == NULL || low == NULL || upp == NULL)
4757 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4758 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004759 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004760 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004761
4762 vim_free(fol);
4763 vim_free(low);
4764 vim_free(upp);
4765 }
4766
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004767 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004768 if (aff->af_compmax != 0)
4769 {
4770 if (spin->si_compmax != 0 && spin->si_compmax != aff->af_compmax)
4771 smsg((char_u *)_("COMPOUNDMAX value differs from what is used in another .aff file"));
4772 else
4773 spin->si_compmax = aff->af_compmax;
4774 }
4775
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004776 if (aff->af_compminlen != 0)
4777 {
4778 if (spin->si_compminlen != 0
4779 && spin->si_compminlen != aff->af_compminlen)
4780 smsg((char_u *)_("COMPOUNDMIN value differs from what is used in another .aff file"));
4781 else
4782 spin->si_compminlen = aff->af_compminlen;
4783 }
4784
Bram Moolenaar5195e452005-08-19 20:32:47 +00004785 if (aff->af_compsylmax != 0)
4786 {
4787 if (aff->af_syllable == NULL)
4788 smsg((char_u *)_("COMPOUNDSYLMAX without SYLLABLE"));
4789
4790 if (spin->si_compsylmax != 0
4791 && spin->si_compsylmax != aff->af_compsylmax)
4792 smsg((char_u *)_("COMPOUNDSYLMAX value differs from what is used in another .aff file"));
4793 else
4794 spin->si_compsylmax = aff->af_compsylmax;
4795 }
4796
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004797 if (aff->af_compflags != NULL)
4798 {
4799 if (spin->si_compflags != NULL
4800 && STRCMP(spin->si_compflags, aff->af_compflags) != 0)
4801 smsg((char_u *)_("COMPOUNDFLAG(S) value differs from what is used in another .aff file"));
4802 else
4803 spin->si_compflags = aff->af_compflags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004804 }
4805
Bram Moolenaar5195e452005-08-19 20:32:47 +00004806 if (aff->af_syllable != NULL)
4807 {
4808 if (spin->si_syllable != NULL
4809 && STRCMP(spin->si_syllable, aff->af_syllable) != 0)
4810 smsg((char_u *)_("SYLLABLE value differs from what is used in another .aff file"));
4811 else
4812 spin->si_syllable = aff->af_syllable;
4813 }
4814
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004815 vim_free(pc);
4816 fclose(fd);
4817 return aff;
4818}
4819
4820/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004821 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
4822 * NULL as equal.
4823 */
4824 static int
4825str_equal(s1, s2)
4826 char_u *s1;
4827 char_u *s2;
4828{
4829 if (s1 == NULL || s2 == NULL)
4830 return s1 == s2;
4831 return STRCMP(s1, s2) == 0;
4832}
4833
4834/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 * Add a from-to item to "gap". Used for REP and SAL items.
4836 * They are stored case-folded.
4837 */
4838 static void
4839add_fromto(spin, gap, from, to)
4840 spellinfo_T *spin;
4841 garray_T *gap;
4842 char_u *from;
4843 char_u *to;
4844{
4845 fromto_T *ftp;
4846 char_u word[MAXWLEN];
4847
4848 if (ga_grow(gap, 1) == OK)
4849 {
4850 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
4851 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004852 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004853 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004854 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855 ++gap->ga_len;
4856 }
4857}
4858
4859/*
4860 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4861 */
4862 static int
4863sal_to_bool(s)
4864 char_u *s;
4865{
4866 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4867}
4868
4869/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004870 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4871 * When "s" is NULL FALSE is returned.
4872 */
4873 static int
4874has_non_ascii(s)
4875 char_u *s;
4876{
4877 char_u *p;
4878
4879 if (s != NULL)
4880 for (p = s; *p != NUL; ++p)
4881 if (*p >= 128)
4882 return TRUE;
4883 return FALSE;
4884}
4885
4886/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004887 * Free the structure filled by spell_read_aff().
4888 */
4889 static void
4890spell_free_aff(aff)
4891 afffile_T *aff;
4892{
4893 hashtab_T *ht;
4894 hashitem_T *hi;
4895 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004898
4899 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004900
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004901 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004902 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
4903 {
4904 todo = ht->ht_used;
4905 for (hi = ht->ht_array; todo > 0; ++hi)
4906 {
4907 if (!HASHITEM_EMPTY(hi))
4908 {
4909 --todo;
4910 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004911 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
4912 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004913 }
4914 }
4915 if (ht == &aff->af_suff)
4916 break;
4917 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004918
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004919 hash_clear(&aff->af_pref);
4920 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004921}
4922
4923/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004924 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004925 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004926 */
4927 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004928spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004929 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004930 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004931 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004932{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004933 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004934 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004935 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004936 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004937 char_u store_afflist[MAXWLEN];
4938 int pfxlen;
4939 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004940 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004941 char_u *pc;
4942 char_u *w;
4943 int l;
4944 hash_T hash;
4945 hashitem_T *hi;
4946 FILE *fd;
4947 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004948 int non_ascii = 0;
4949 int retval = OK;
4950 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004951 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004952 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004953
Bram Moolenaar51485f02005-06-04 21:55:20 +00004954 /*
4955 * Open the file.
4956 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004957 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004958 if (fd == NULL)
4959 {
4960 EMSG2(_(e_notopen), fname);
4961 return FAIL;
4962 }
4963
Bram Moolenaar51485f02005-06-04 21:55:20 +00004964 /* The hashtable is only used to detect duplicated words. */
4965 hash_init(&ht);
4966
Bram Moolenaarb765d632005-06-07 21:00:02 +00004967 if (spin->si_verbose || p_verbose > 2)
4968 {
4969 if (!spin->si_verbose)
4970 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004971 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004972 out_flush();
4973 if (!spin->si_verbose)
4974 verbose_leave();
4975 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004977 /* start with a message for the first line */
4978 spin->si_msg_count = 999999;
4979
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004980 /* Read and ignore the first line: word count. */
4981 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004982 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004983 EMSG2(_("E760: No word count in %s"), fname);
4984
4985 /*
4986 * Read all the lines in the file one by one.
4987 * The words are converted to 'encoding' here, before being added to
4988 * the hashtable.
4989 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004990 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004991 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004992 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004993 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004994 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004995 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004996
Bram Moolenaar51485f02005-06-04 21:55:20 +00004997 /* Remove CR, LF and white space from the end. White space halfway
4998 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004999 l = STRLEN(line);
5000 while (l > 0 && line[l - 1] <= ' ')
5001 --l;
5002 if (l == 0)
5003 continue; /* empty line */
5004 line[l] = NUL;
5005
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005006 /* Find the optional affix names. Replace the SLASH character by a
5007 * slash. */
5008 afflist = NULL;
5009 for (p = line; *p != NUL; mb_ptr_adv(p))
5010 {
5011 if (*p == affile->af_slash)
5012 *p = '/';
5013 else if (*p == '/')
5014 {
5015 *p = NUL;
5016 afflist = p + 1;
5017 break;
5018 }
5019 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005020
5021 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5022 if (spin->si_ascii && has_non_ascii(line))
5023 {
5024 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005025 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005026 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005027
Bram Moolenaarb765d632005-06-07 21:00:02 +00005028#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005029 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005030 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005032 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005033 if (pc == NULL)
5034 {
5035 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5036 fname, lnum, line);
5037 continue;
5038 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005039 w = pc;
5040 }
5041 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005042#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005043 {
5044 pc = NULL;
5045 w = line;
5046 }
5047
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005048 /* This takes time, print a message every 10000 words. */
5049 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005050 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005051 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005052 vim_snprintf((char *)message, sizeof(message),
5053 _("line %6d, word %6d - %s"),
5054 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5055 msg_start();
5056 msg_puts_long_attr(message, 0);
5057 msg_clr_eos();
5058 msg_didout = FALSE;
5059 msg_col = 0;
5060 out_flush();
5061 }
5062
Bram Moolenaar51485f02005-06-04 21:55:20 +00005063 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005064 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005065 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005066 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005067 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005068 if (retval == FAIL)
5069 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005070
Bram Moolenaar51485f02005-06-04 21:55:20 +00005071 hash = hash_hash(dw);
5072 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005073 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005074 {
5075 if (p_verbose > 0)
5076 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005077 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005078 else if (duplicate == 0)
5079 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5080 fname, lnum, dw);
5081 ++duplicate;
5082 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005083 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005084 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005085
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005086 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005087 store_afflist[0] = NUL;
5088 pfxlen = 0;
5089 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005090 if (afflist != NULL)
5091 {
5092 /* Check for affix name that stands for keep-case word and stands
5093 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005094 if (affile->af_kep != NUL
5095 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005096 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005097 if (affile->af_rar != NUL
5098 && vim_strchr(afflist, affile->af_rar) != NULL)
5099 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005100 if (affile->af_bad != NUL
5101 && vim_strchr(afflist, affile->af_bad) != NULL)
5102 flags |= WF_BANNED;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005103 if (affile->af_needaffix != NUL
5104 && vim_strchr(afflist, affile->af_needaffix) != NULL)
5105 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005106
5107 if (affile->af_pfxpostpone)
5108 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005109 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005110
Bram Moolenaar5195e452005-08-19 20:32:47 +00005111 if (spin->si_compflags != NULL)
5112 /* Need to store the list of compound flags with the word.
5113 * Concatenate them to the list of prefix IDs. */
5114 get_compflags(spin, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005115 }
5116
Bram Moolenaar51485f02005-06-04 21:55:20 +00005117 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005118 if (store_word(spin, dw, flags, spin->si_region,
5119 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005120 retval = FAIL;
5121
5122 if (afflist != NULL)
5123 {
5124 /* Find all matching suffixes and add the resulting words.
5125 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005126 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005127 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005128 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005129 retval = FAIL;
5130
5131 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005132 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005133 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005134 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005135 retval = FAIL;
5136 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005137 }
5138
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005139 if (duplicate > 0)
5140 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005141 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005142 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5143 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005144 hash_clear(&ht);
5145
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005146 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005147 return retval;
5148}
5149
5150/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005151 * Get the list of prefix IDs from the affix list "afflist".
5152 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005153 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5154 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005155 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005156 static int
5157get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005158 afffile_T *affile;
5159 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005160 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005161{
5162 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005163 int cnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005164 char_u key[2];
5165 hashitem_T *hi;
5166
5167 key[1] = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005168 for (p = afflist; *p != NUL; ++p)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005169 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005170 key[0] = *p;
5171 hi = hash_find(&affile->af_pref, key);
5172 if (!HASHITEM_EMPTY(hi))
5173 /* This is a prefix ID, use the new number. */
5174 store_afflist[cnt++] = HI2AH(hi)->ah_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005175 }
5176
Bram Moolenaar5195e452005-08-19 20:32:47 +00005177 store_afflist[cnt] = NUL;
5178 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005179}
5180
5181/*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005182 * Get the list of affix IDs from the affix list "afflist" that are used for
5183 * compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005184 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005185 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005186 static void
5187get_compflags(spin, afflist, store_afflist)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005188 spellinfo_T *spin;
5189 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005190 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005191{
5192 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005193 int cnt = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005194
Bram Moolenaar5195e452005-08-19 20:32:47 +00005195 for (p = afflist; *p != NUL; ++p)
5196 /* A flag is a compound flag if it appears in "si_compflags" and
5197 * it's not a special character. */
5198 if (vim_strchr(spin->si_compflags, *p) != NULL
5199 && vim_strchr((char_u *)"+*[]/", *p) == NULL)
5200 store_afflist[cnt++] = *p;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005201
Bram Moolenaar5195e452005-08-19 20:32:47 +00005202 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005203}
5204
5205/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005206 * Apply affixes to a word and store the resulting words.
5207 * "ht" is the hashtable with affentry_T that need to be applied, either
5208 * prefixes or suffixes.
5209 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5210 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005211 *
5212 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005213 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005214 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005215store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5216 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005217 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005218 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005219 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005220 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005221 hashtab_T *ht;
5222 hashtab_T *xht;
5223 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005224 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005225 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005226 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5227 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005228{
5229 int todo;
5230 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005231 affheader_T *ah;
5232 affentry_T *ae;
5233 regmatch_T regmatch;
5234 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005235 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005236 int i;
5237 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005238 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005239 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005240 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00005241 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005242 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005243
Bram Moolenaar51485f02005-06-04 21:55:20 +00005244 todo = ht->ht_used;
5245 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005246 {
5247 if (!HASHITEM_EMPTY(hi))
5248 {
5249 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005250 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005251
Bram Moolenaar51485f02005-06-04 21:55:20 +00005252 /* Check that the affix combines, if required, and that the word
5253 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005254 c = PTR2CHAR(ah->ah_key);
5255 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005256 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005257 /* Loop over all affix entries with this name. */
5258 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005260 /* Check the condition. It's not logical to match case
5261 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005262 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005263 * Another requirement from Myspell is that the chop
5264 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005265 * For prefixes, when "PFXPOSTPONE" was used, only do
5266 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005267 regmatch.regprog = ae->ae_prog;
5268 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005269 if ((xht != NULL || !affile->af_pfxpostpone
5270 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005271 && (ae->ae_chop == NULL
5272 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005273 && (ae->ae_prog == NULL
5274 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005275 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005276 /* Match. Remove the chop and add the affix. */
5277 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005278 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005279 /* prefix: chop/add at the start of the word */
5280 if (ae->ae_add == NULL)
5281 *newword = NUL;
5282 else
5283 STRCPY(newword, ae->ae_add);
5284 p = word;
5285 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005286 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005287 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005288#ifdef FEAT_MBYTE
5289 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005290 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005291 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005292 for ( ; i > 0; --i)
5293 mb_ptr_adv(p);
5294 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005295 else
5296#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005297 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005298 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005299 STRCAT(newword, p);
5300 }
5301 else
5302 {
5303 /* suffix: chop/add at the end of the word */
5304 STRCPY(newword, word);
5305 if (ae->ae_chop != NULL)
5306 {
5307 /* Remove chop string. */
5308 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005309 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005310 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005311 mb_ptr_back(newword, p);
5312 *p = NUL;
5313 }
5314 if (ae->ae_add != NULL)
5315 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005316 }
5317
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005318 /* Obey the "rare" flag of the affix. */
5319 if (ae->ae_rare)
5320 use_flags = flags | WF_RARE;
5321 else
5322 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005323
5324 /* Obey the "nocomp" flag of the affix: don't use the
5325 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005326 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005327 if (ae->ae_nocomp && pfxlist != NULL)
5328 {
5329 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5330 use_pfxlist = pfx_pfxlist;
5331 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005332
5333 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005334 if (spin->si_prefroot != NULL
5335 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005336 {
5337 /* ... add a flag to indicate an affix was used. */
5338 use_flags |= WF_HAS_AFF;
5339
5340 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005341 * affixes is not allowed. But do use the
5342 * compound flags after them. */
5343 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5344 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005345 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005346
Bram Moolenaar51485f02005-06-04 21:55:20 +00005347 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005348 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005349 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005350 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005351
Bram Moolenaar51485f02005-06-04 21:55:20 +00005352 /* When added a suffix and combining is allowed also
5353 * try adding prefixes additionally. */
5354 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005355 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005356 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005357 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005358 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005359 }
5360 }
5361 }
5362 }
5363 }
5364
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005365 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005366}
5367
5368/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005369 * Read a file with a list of words.
5370 */
5371 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005372spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005373 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005374 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005375{
5376 FILE *fd;
5377 long lnum = 0;
5378 char_u rline[MAXLINELEN];
5379 char_u *line;
5380 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005381 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005382 int l;
5383 int retval = OK;
5384 int did_word = FALSE;
5385 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005386 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005387 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005388
5389 /*
5390 * Open the file.
5391 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005392 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005393 if (fd == NULL)
5394 {
5395 EMSG2(_(e_notopen), fname);
5396 return FAIL;
5397 }
5398
Bram Moolenaarb765d632005-06-07 21:00:02 +00005399 if (spin->si_verbose || p_verbose > 2)
5400 {
5401 if (!spin->si_verbose)
5402 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005403 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005404 out_flush();
5405 if (!spin->si_verbose)
5406 verbose_leave();
5407 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005408
5409 /*
5410 * Read all the lines in the file one by one.
5411 */
5412 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5413 {
5414 line_breakcheck();
5415 ++lnum;
5416
5417 /* Skip comment lines. */
5418 if (*rline == '#')
5419 continue;
5420
5421 /* Remove CR, LF and white space from the end. */
5422 l = STRLEN(rline);
5423 while (l > 0 && rline[l - 1] <= ' ')
5424 --l;
5425 if (l == 0)
5426 continue; /* empty or blank line */
5427 rline[l] = NUL;
5428
5429 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
5430 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005431#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005432 if (spin->si_conv.vc_type != CONV_NONE)
5433 {
5434 pc = string_convert(&spin->si_conv, rline, NULL);
5435 if (pc == NULL)
5436 {
5437 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5438 fname, lnum, rline);
5439 continue;
5440 }
5441 line = pc;
5442 }
5443 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005444#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005445 {
5446 pc = NULL;
5447 line = rline;
5448 }
5449
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005450 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00005451 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005452 ++line;
5453 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005454 {
5455 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005456 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
5457 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005458 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005459 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
5460 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005461 else
5462 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005463#ifdef FEAT_MBYTE
5464 char_u *enc;
5465
Bram Moolenaar51485f02005-06-04 21:55:20 +00005466 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005467 line += 10;
5468 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005469 if (enc != NULL && !spin->si_ascii
5470 && convert_setup(&spin->si_conv, enc,
5471 p_enc) == FAIL)
5472 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00005473 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005474 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005475 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005476#else
5477 smsg((char_u *)_("Conversion in %s not supported"), fname);
5478#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005479 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005480 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005481 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005482
Bram Moolenaar3982c542005-06-08 21:56:31 +00005483 if (STRNCMP(line, "regions=", 8) == 0)
5484 {
5485 if (spin->si_region_count > 1)
5486 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
5487 fname, lnum, line);
5488 else
5489 {
5490 line += 8;
5491 if (STRLEN(line) > 16)
5492 smsg((char_u *)_("Too many regions in %s line %d: %s"),
5493 fname, lnum, line);
5494 else
5495 {
5496 spin->si_region_count = STRLEN(line) / 2;
5497 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005498
5499 /* Adjust the mask for a word valid in all regions. */
5500 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005501 }
5502 }
5503 continue;
5504 }
5505
Bram Moolenaar7887d882005-07-01 22:33:52 +00005506 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
5507 fname, lnum, line - 1);
5508 continue;
5509 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005510
Bram Moolenaar7887d882005-07-01 22:33:52 +00005511 flags = 0;
5512 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005513
Bram Moolenaar7887d882005-07-01 22:33:52 +00005514 /* Check for flags and region after a slash. */
5515 p = vim_strchr(line, '/');
5516 if (p != NULL)
5517 {
5518 *p++ = NUL;
5519 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005520 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005521 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005522 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005523 else if (*p == '!') /* Bad, bad, wicked word. */
5524 flags |= WF_BANNED;
5525 else if (*p == '?') /* Rare word. */
5526 flags |= WF_RARE;
5527 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005528 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005529 if ((flags & WF_REGION) == 0) /* first one */
5530 regionmask = 0;
5531 flags |= WF_REGION;
5532
5533 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00005534 if (l > spin->si_region_count)
5535 {
5536 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00005537 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005538 break;
5539 }
5540 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005541 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005542 else
5543 {
5544 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
5545 fname, lnum, p);
5546 break;
5547 }
5548 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005549 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005550 }
5551
5552 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5553 if (spin->si_ascii && has_non_ascii(line))
5554 {
5555 ++non_ascii;
5556 continue;
5557 }
5558
5559 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005560 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005561 {
5562 retval = FAIL;
5563 break;
5564 }
5565 did_word = TRUE;
5566 }
5567
5568 vim_free(pc);
5569 fclose(fd);
5570
Bram Moolenaarb765d632005-06-07 21:00:02 +00005571 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
5572 {
5573 if (p_verbose > 2)
5574 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00005575 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
5576 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005577 if (p_verbose > 2)
5578 verbose_leave();
5579 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005580 return retval;
5581}
5582
5583/*
5584 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005585 * This avoids calling free() for every little struct we use (and keeping
5586 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00005587 * The memory is cleared to all zeros.
5588 * Returns NULL when out of memory.
5589 */
5590 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005591getroom(spin, len, align)
5592 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005593 size_t len; /* length needed */
5594 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005595{
5596 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005597 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005598
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005599 if (align && bl != NULL)
5600 /* Round size up for alignment. On some systems structures need to be
5601 * aligned to the size of a pointer (e.g., SPARC). */
5602 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
5603 & ~(sizeof(char *) - 1);
5604
Bram Moolenaar51485f02005-06-04 21:55:20 +00005605 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
5606 {
5607 /* Allocate a block of memory. This is not freed until much later. */
5608 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
5609 if (bl == NULL)
5610 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005611 bl->sb_next = spin->si_blocks;
5612 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005613 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005614 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005615 }
5616
5617 p = bl->sb_data + bl->sb_used;
5618 bl->sb_used += len;
5619
5620 return p;
5621}
5622
5623/*
5624 * Make a copy of a string into memory allocated with getroom().
5625 */
5626 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005627getroom_save(spin, s)
5628 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005629 char_u *s;
5630{
5631 char_u *sc;
5632
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005633 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005634 if (sc != NULL)
5635 STRCPY(sc, s);
5636 return sc;
5637}
5638
5639
5640/*
5641 * Free the list of allocated sblock_T.
5642 */
5643 static void
5644free_blocks(bl)
5645 sblock_T *bl;
5646{
5647 sblock_T *next;
5648
5649 while (bl != NULL)
5650 {
5651 next = bl->sb_next;
5652 vim_free(bl);
5653 bl = next;
5654 }
5655}
5656
5657/*
5658 * Allocate the root of a word tree.
5659 */
5660 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005661wordtree_alloc(spin)
5662 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005663{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005664 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005665}
5666
5667/*
5668 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005669 * Always store it in the case-folded tree. For a keep-case word this is
5670 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
5671 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005672 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005673 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
5674 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005675 */
5676 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005677store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005678 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005679 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005680 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005681 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005682 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005683 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005684{
5685 int len = STRLEN(word);
5686 int ct = captype(word, word + len);
5687 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005688 int res = OK;
5689 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005691 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005692 for (p = pfxlist; res == OK; ++p)
5693 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005694 if (!need_affix || (p != NULL && *p != NUL))
5695 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005696 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005697 if (p == NULL || *p == NUL)
5698 break;
5699 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005700 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005701
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005702 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00005703 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005704 for (p = pfxlist; res == OK; ++p)
5705 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005706 if (!need_affix || (p != NULL && *p != NUL))
5707 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005708 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005709 if (p == NULL || *p == NUL)
5710 break;
5711 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005712 ++spin->si_keepwcount;
5713 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005714 return res;
5715}
5716
5717/*
5718 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005719 * When "flags" < 0 we are adding to the prefix tree where flags is used for
5720 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005721 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005722 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005723 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005724tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005725 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005726 char_u *word;
5727 wordnode_T *root;
5728 int flags;
5729 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005730 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005731{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005732 wordnode_T *node = root;
5733 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005734 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 wordnode_T **prev = NULL;
5736 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005737
Bram Moolenaar51485f02005-06-04 21:55:20 +00005738 /* Add each byte of the word to the tree, including the NUL at the end. */
5739 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005740 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005741 /* When there is more than one reference to this node we need to make
5742 * a copy, so that we can modify it. Copy the whole list of siblings
5743 * (we don't optimize for a partly shared list of siblings). */
5744 if (node != NULL && node->wn_refs > 1)
5745 {
5746 --node->wn_refs;
5747 copyprev = prev;
5748 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
5749 {
5750 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005751 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005752 if (np == NULL)
5753 return FAIL;
5754 np->wn_child = copyp->wn_child;
5755 if (np->wn_child != NULL)
5756 ++np->wn_child->wn_refs; /* child gets extra ref */
5757 np->wn_byte = copyp->wn_byte;
5758 if (np->wn_byte == NUL)
5759 {
5760 np->wn_flags = copyp->wn_flags;
5761 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005762 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005763 }
5764
5765 /* Link the new node in the list, there will be one ref. */
5766 np->wn_refs = 1;
5767 *copyprev = np;
5768 copyprev = &np->wn_sibling;
5769
5770 /* Let "node" point to the head of the copied list. */
5771 if (copyp == node)
5772 node = np;
5773 }
5774 }
5775
Bram Moolenaar51485f02005-06-04 21:55:20 +00005776 /* Look for the sibling that has the same character. They are sorted
5777 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005778 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005779 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005780 while (node != NULL
5781 && (node->wn_byte < word[i]
5782 || (node->wn_byte == NUL
5783 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005784 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005785 : node->wn_flags < (flags & WN_MASK)
5786 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005787 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005788 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005789 prev = &node->wn_sibling;
5790 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005791 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005792 if (node == NULL
5793 || node->wn_byte != word[i]
5794 || (word[i] == NUL
5795 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005796 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005797 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005798 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005799 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005800 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005801 if (np == NULL)
5802 return FAIL;
5803 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005804
5805 /* If "node" is NULL this is a new child or the end of the sibling
5806 * list: ref count is one. Otherwise use ref count of sibling and
5807 * make ref count of sibling one (matters when inserting in front
5808 * of the list of siblings). */
5809 if (node == NULL)
5810 np->wn_refs = 1;
5811 else
5812 {
5813 np->wn_refs = node->wn_refs;
5814 node->wn_refs = 1;
5815 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005816 *prev = np;
5817 np->wn_sibling = node;
5818 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005819 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005820
Bram Moolenaar51485f02005-06-04 21:55:20 +00005821 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005823 node->wn_flags = flags;
5824 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005825 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005826 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00005827 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005828 prev = &node->wn_child;
5829 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005830 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005831#ifdef SPELL_PRINTTREE
5832 smsg("Added \"%s\"", word);
5833 spell_print_tree(root->wn_sibling);
5834#endif
5835
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005836 /* count nr of words added since last message */
5837 ++spin->si_msg_count;
5838
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005839 if (spin->si_compress_cnt > 1)
5840 {
5841 if (--spin->si_compress_cnt == 1)
5842 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005843 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005844 }
5845
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005846 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005847 * When we have allocated lots of memory we need to compress the word tree
5848 * to free up some room. But compression is slow, and we might actually
5849 * need that room, thus only compress in the following situations:
5850 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00005851 * "compress_start" blocks.
5852 * 2. When compressed before and used "compress_inc" blocks before
5853 * adding "compress_added" words (si_compress_cnt > 1).
5854 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005855 * (si_compress_cnt == 1) and the number of free nodes drops below the
5856 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005857 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005858#ifndef SPELL_PRINTTREE
5859 if (spin->si_compress_cnt == 1
5860 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00005861 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005862#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005863 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005864 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00005865 * when the freed up room has been used and another "compress_inc"
5866 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005867 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005868 spin->si_blocks_cnt -= compress_inc;
5869 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005870
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005871 if (spin->si_verbose)
5872 {
5873 msg_start();
5874 msg_puts((char_u *)_(msg_compressing));
5875 msg_clr_eos();
5876 msg_didout = FALSE;
5877 msg_col = 0;
5878 out_flush();
5879 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005880
5881 /* Compress both trees. Either they both have many nodes, which makes
5882 * compression useful, or one of them is small, which means
5883 * compression goes fast. */
5884 wordtree_compress(spin, spin->si_foldroot);
5885 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005886 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005887
5888 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005889}
5890
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005891/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00005892 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
5893 * Sets "sps_flags".
5894 */
5895 int
5896spell_check_msm()
5897{
5898 char_u *p = p_msm;
5899 long start = 0;
5900 long inc = 0;
5901 long added = 0;
5902
5903 if (!VIM_ISDIGIT(*p))
5904 return FAIL;
5905 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
5906 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
5907 if (*p != ',')
5908 return FAIL;
5909 ++p;
5910 if (!VIM_ISDIGIT(*p))
5911 return FAIL;
5912 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
5913 if (*p != ',')
5914 return FAIL;
5915 ++p;
5916 if (!VIM_ISDIGIT(*p))
5917 return FAIL;
5918 added = getdigits(&p) * 1024;
5919 if (*p != NUL)
5920 return FAIL;
5921
5922 if (start == 0 || inc == 0 || added == 0 || inc > start)
5923 return FAIL;
5924
5925 compress_start = start;
5926 compress_inc = inc;
5927 compress_added = added;
5928 return OK;
5929}
5930
5931
5932/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005933 * Get a wordnode_T, either from the list of previously freed nodes or
5934 * allocate a new one.
5935 */
5936 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005937get_wordnode(spin)
5938 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005939{
5940 wordnode_T *n;
5941
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005942 if (spin->si_first_free == NULL)
5943 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005944 else
5945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005946 n = spin->si_first_free;
5947 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005948 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005949 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005950 }
5951#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005952 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005953#endif
5954 return n;
5955}
5956
5957/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005958 * Decrement the reference count on a node (which is the head of a list of
5959 * siblings). If the reference count becomes zero free the node and its
5960 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005961 */
5962 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005963deref_wordnode(spin, node)
5964 spellinfo_T *spin;
5965 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005966{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005967 wordnode_T *np;
5968
5969 if (--node->wn_refs == 0)
5970 for (np = node; np != NULL; np = np->wn_sibling)
5971 {
5972 if (np->wn_child != NULL)
5973 deref_wordnode(spin, np->wn_child);
5974 free_wordnode(spin, np);
5975 }
5976}
5977
5978/*
5979 * Free a wordnode_T for re-use later.
5980 * Only the "wn_child" field becomes invalid.
5981 */
5982 static void
5983free_wordnode(spin, n)
5984 spellinfo_T *spin;
5985 wordnode_T *n;
5986{
5987 n->wn_child = spin->si_first_free;
5988 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005989 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005990}
5991
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005992/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005993 * Compress a tree: find tails that are identical and can be shared.
5994 */
5995 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005996wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005997 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005998 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005999{
6000 hashtab_T ht;
6001 int n;
6002 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006003 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006004
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006005 /* Skip the root itself, it's not actually used. The first sibling is the
6006 * start of the tree. */
6007 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006008 {
6009 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006010 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006011
6012#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006013 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006014#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006015 {
6016 if (!spin->si_verbose)
6017 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006018 if (tot > 1000000)
6019 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006020 else if (tot == 0)
6021 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006022 else
6023 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006024 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006025 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006026 if (p_verbose > 2)
6027 verbose_leave();
6028 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006029#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006030 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006031#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006032 hash_clear(&ht);
6033 }
6034}
6035
6036/*
6037 * Compress a node, its siblings and its children, depth first.
6038 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006039 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006040 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006041node_compress(spin, node, ht, tot)
6042 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006043 wordnode_T *node;
6044 hashtab_T *ht;
6045 int *tot; /* total count of nodes before compressing,
6046 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006047{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006048 wordnode_T *np;
6049 wordnode_T *tp;
6050 wordnode_T *child;
6051 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006052 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006053 int len = 0;
6054 unsigned nr, n;
6055 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006056
Bram Moolenaar51485f02005-06-04 21:55:20 +00006057 /*
6058 * Go through the list of siblings. Compress each child and then try
6059 * finding an identical child to replace it.
6060 * Note that with "child" we mean not just the node that is pointed to,
6061 * but the whole list of siblings, of which the node is the first.
6062 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006063 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006064 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006065 ++len;
6066 if ((child = np->wn_child) != NULL)
6067 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006068 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006069 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006070
6071 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006072 hash = hash_hash(child->wn_u1.hashkey);
6073 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006074 tp = NULL;
6075 if (!HASHITEM_EMPTY(hi))
6076 {
6077 /* There are children with an identical hash value. Now check
6078 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006079 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006080 if (node_equal(child, tp))
6081 {
6082 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006083 * current one. This means the current child and all
6084 * its siblings is unlinked from the tree. */
6085 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006086 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006087 np->wn_child = tp;
6088 ++compressed;
6089 break;
6090 }
6091 if (tp == NULL)
6092 {
6093 /* No other child with this hash value equals the child of
6094 * the node, add it to the linked list after the first
6095 * item. */
6096 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006097 child->wn_u2.next = tp->wn_u2.next;
6098 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006099 }
6100 }
6101 else
6102 /* No other child has this hash value, add it to the
6103 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006104 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006105 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006106 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006107 *tot += len;
6108
6109 /*
6110 * Make a hash key for the node and its siblings, so that we can quickly
6111 * find a lookalike node. This must be done after compressing the sibling
6112 * list, otherwise the hash key would become invalid by the compression.
6113 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006114 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006115 nr = 0;
6116 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006117 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006118 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006119 /* end node: use wn_flags, wn_region and wn_affixID */
6120 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006121 else
6122 /* byte node: use the byte value and the child pointer */
6123 n = np->wn_byte + ((long_u)np->wn_child << 8);
6124 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006125 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006126
6127 /* Avoid NUL bytes, it terminates the hash key. */
6128 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006129 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006130 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006131 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006132 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006133 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006134 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006135 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6136 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006137
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006138 /* Check for CTRL-C pressed now and then. */
6139 fast_breakcheck();
6140
Bram Moolenaar51485f02005-06-04 21:55:20 +00006141 return compressed;
6142}
6143
6144/*
6145 * Return TRUE when two nodes have identical siblings and children.
6146 */
6147 static int
6148node_equal(n1, n2)
6149 wordnode_T *n1;
6150 wordnode_T *n2;
6151{
6152 wordnode_T *p1;
6153 wordnode_T *p2;
6154
6155 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6156 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6157 if (p1->wn_byte != p2->wn_byte
6158 || (p1->wn_byte == NUL
6159 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006160 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006161 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006162 : (p1->wn_child != p2->wn_child)))
6163 break;
6164
6165 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006166}
6167
6168/*
6169 * Write a number to file "fd", MSB first, in "len" bytes.
6170 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006171 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006172put_bytes(fd, nr, len)
6173 FILE *fd;
6174 long_u nr;
6175 int len;
6176{
6177 int i;
6178
6179 for (i = len - 1; i >= 0; --i)
6180 putc((int)(nr >> (i * 8)), fd);
6181}
6182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183static int
6184#ifdef __BORLANDC__
6185_RTLENTRYF
6186#endif
6187rep_compare __ARGS((const void *s1, const void *s2));
6188
6189/*
6190 * Function given to qsort() to sort the REP items on "from" string.
6191 */
6192 static int
6193#ifdef __BORLANDC__
6194_RTLENTRYF
6195#endif
6196rep_compare(s1, s2)
6197 const void *s1;
6198 const void *s2;
6199{
6200 fromto_T *p1 = (fromto_T *)s1;
6201 fromto_T *p2 = (fromto_T *)s2;
6202
6203 return STRCMP(p1->ft_from, p2->ft_from);
6204}
6205
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006206/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006207 * Write the Vim .spl file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006208 */
6209 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006210write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006211 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006212 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006213{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006214 FILE *fd;
6215 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006216 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006217 wordnode_T *tree;
6218 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006219 int i;
6220 int l;
6221 garray_T *gap;
6222 fromto_T *ftp;
6223 char_u *p;
6224 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006225
Bram Moolenaarb765d632005-06-07 21:00:02 +00006226 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006227 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006228 {
6229 EMSG2(_(e_notopen), fname);
6230 return;
6231 }
6232
Bram Moolenaar5195e452005-08-19 20:32:47 +00006233 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006234 /* <fileID> */
6235 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
6236 EMSG(_(e_write));
Bram Moolenaar5195e452005-08-19 20:32:47 +00006237 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006238
Bram Moolenaar5195e452005-08-19 20:32:47 +00006239 /*
6240 * <SECTIONS>: <section> ... <sectionend>
6241 */
6242
6243 /* SN_REGION: <regionname> ...
6244 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006245 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006246 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006247 putc(SN_REGION, fd); /* <sectionID> */
6248 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6249 l = spin->si_region_count * 2;
6250 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6251 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6252 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006253 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006254 }
6255 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006256 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006257
Bram Moolenaar5195e452005-08-19 20:32:47 +00006258 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6259 *
6260 * The table with character flags and the table for case folding.
6261 * This makes sure the same characters are recognized as word characters
6262 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006263 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006264 * 'encoding'.
6265 * Also skip this for an .add.spl file, the main spell file must contain
6266 * the table (avoids that it conflicts). File is shorter too.
6267 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006268 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006269 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006270 char_u folchars[128 * 8];
6271 int flags;
6272
Bram Moolenaard12a1322005-08-21 22:08:24 +00006273 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006274 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6275
6276 /* Form the <folchars> string first, we need to know its length. */
6277 l = 0;
6278 for (i = 128; i < 256; ++i)
6279 {
6280#ifdef FEAT_MBYTE
6281 if (has_mbyte)
6282 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6283 else
6284#endif
6285 folchars[l++] = spelltab.st_fold[i];
6286 }
6287 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6288
6289 fputc(128, fd); /* <charflagslen> */
6290 for (i = 128; i < 256; ++i)
6291 {
6292 flags = 0;
6293 if (spelltab.st_isw[i])
6294 flags |= CF_WORD;
6295 if (spelltab.st_isu[i])
6296 flags |= CF_UPPER;
6297 fputc(flags, fd); /* <charflags> */
6298 }
6299
6300 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6301 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006302 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006303
Bram Moolenaar5195e452005-08-19 20:32:47 +00006304 /* SN_MIDWORD: <midword> */
6305 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006306 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006307 putc(SN_MIDWORD, fd); /* <sectionID> */
6308 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6309
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006310 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006311 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006312 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6313 }
6314
Bram Moolenaar5195e452005-08-19 20:32:47 +00006315 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6316 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006317 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006318 putc(SN_PREFCOND, fd); /* <sectionID> */
6319 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6320
6321 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6322 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6323
6324 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006325 }
6326
Bram Moolenaar5195e452005-08-19 20:32:47 +00006327 /* SN_REP: <repcount> <rep> ...
6328 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006330 /* Sort the REP items. */
6331 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6332 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006333
Bram Moolenaar5195e452005-08-19 20:32:47 +00006334 /* round 1: SN_REP section
6335 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006336 for (round = 1; round <= 2; ++round)
6337 {
6338 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006340 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006341 putc(SN_REP, fd); /* <sectionID> */
6342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006343 else
6344 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006345 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6346 /* using SN_SOFO section instead of SN_SAL */
6347 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006348 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006349 putc(SN_SAL, fd); /* <sectionID> */
6350 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006351
Bram Moolenaar5195e452005-08-19 20:32:47 +00006352 /* This is for making suggestions, section is not required. */
6353 putc(0, fd); /* <sectionflags> */
6354
6355 /* Compute the length of what follows. */
6356 l = 2; /* count <repcount> or <salcount> */
6357 for (i = 0; i < gap->ga_len; ++i)
6358 {
6359 ftp = &((fromto_T *)gap->ga_data)[i];
6360 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6361 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6362 }
6363 if (round == 2)
6364 ++l; /* count <salflags> */
6365 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6366
6367 if (round == 2)
6368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006369 i = 0;
6370 if (spin->si_followup)
6371 i |= SAL_F0LLOWUP;
6372 if (spin->si_collapse)
6373 i |= SAL_COLLAPSE;
6374 if (spin->si_rem_accents)
6375 i |= SAL_REM_ACCENTS;
6376 putc(i, fd); /* <salflags> */
6377 }
6378
6379 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6380 for (i = 0; i < gap->ga_len; ++i)
6381 {
6382 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6383 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6384 ftp = &((fromto_T *)gap->ga_data)[i];
6385 for (rr = 1; rr <= 2; ++rr)
6386 {
6387 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6388 l = STRLEN(p);
6389 putc(l, fd);
6390 fwrite(p, l, (size_t)1, fd);
6391 }
6392 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006393
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006394 }
6395
Bram Moolenaar5195e452005-08-19 20:32:47 +00006396 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6397 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006398 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6399 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006400 putc(SN_SOFO, fd); /* <sectionID> */
6401 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006402
6403 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006404 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6405 /* <sectionlen> */
6406
6407 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6408 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006409
6410 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006411 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
6412 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006413 }
6414
Bram Moolenaar5195e452005-08-19 20:32:47 +00006415 /* SN_MAP: <mapstr>
6416 * This is for making suggestions, section is not required. */
6417 if (spin->si_map.ga_len > 0)
6418 {
6419 putc(SN_MAP, fd); /* <sectionID> */
6420 putc(0, fd); /* <sectionflags> */
6421 l = spin->si_map.ga_len;
6422 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6423 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
6424 /* <mapstr> */
6425 }
6426
6427 /* SN_COMPOUND: compound info.
6428 * We don't mark it required, when not supported all compound words will
6429 * be bad words. */
6430 if (spin->si_compflags != NULL)
6431 {
6432 putc(SN_COMPOUND, fd); /* <sectionID> */
6433 putc(0, fd); /* <sectionflags> */
6434
6435 l = STRLEN(spin->si_compflags);
6436 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
6437 putc(spin->si_compmax, fd); /* <compmax> */
6438 putc(spin->si_compminlen, fd); /* <compminlen> */
6439 putc(spin->si_compsylmax, fd); /* <compsylmax> */
6440 /* <compflags> */
6441 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
6442 }
6443
6444 /* SN_SYLLABLE: syllable info.
6445 * We don't mark it required, when not supported syllables will not be
6446 * counted. */
6447 if (spin->si_syllable != NULL)
6448 {
6449 putc(SN_SYLLABLE, fd); /* <sectionID> */
6450 putc(0, fd); /* <sectionflags> */
6451
6452 l = STRLEN(spin->si_syllable);
6453 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6454 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
6455 }
6456
6457 /* end of <SECTIONS> */
6458 putc(SN_END, fd); /* <sectionend> */
6459
Bram Moolenaar50cde822005-06-05 21:54:54 +00006460
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006461 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006462 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006463 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006464 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006465 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006466 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006467 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006468 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006469 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006470 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006471 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006472 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006473
Bram Moolenaar0c405862005-06-22 22:26:26 +00006474 /* Clear the index and wnode fields in the tree. */
6475 clear_node(tree);
6476
Bram Moolenaar51485f02005-06-04 21:55:20 +00006477 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00006478 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00006479 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006480 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006481
Bram Moolenaar51485f02005-06-04 21:55:20 +00006482 /* number of nodes in 4 bytes */
6483 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00006484 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006485
Bram Moolenaar51485f02005-06-04 21:55:20 +00006486 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006487 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006488 }
6489
Bram Moolenaar51485f02005-06-04 21:55:20 +00006490 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006491}
6492
6493/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006494 * Clear the index and wnode fields of "node", it siblings and its
6495 * children. This is needed because they are a union with other items to save
6496 * space.
6497 */
6498 static void
6499clear_node(node)
6500 wordnode_T *node;
6501{
6502 wordnode_T *np;
6503
6504 if (node != NULL)
6505 for (np = node; np != NULL; np = np->wn_sibling)
6506 {
6507 np->wn_u1.index = 0;
6508 np->wn_u2.wnode = NULL;
6509
6510 if (np->wn_byte != NUL)
6511 clear_node(np->wn_child);
6512 }
6513}
6514
6515
6516/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006517 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00006519 * This first writes the list of possible bytes (siblings). Then for each
6520 * byte recursively write the children.
6521 *
6522 * NOTE: The code here must match the code in read_tree(), since assumptions
6523 * are made about the indexes (so that we don't have to write them in the
6524 * file).
6525 *
6526 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006527 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006528 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00006529put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006530 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006531 wordnode_T *node;
6532 int index;
6533 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006534 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006535{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536 int newindex = index;
6537 int siblingcount = 0;
6538 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006539 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006540
Bram Moolenaar51485f02005-06-04 21:55:20 +00006541 /* If "node" is zero the tree is empty. */
6542 if (node == NULL)
6543 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006544
Bram Moolenaar51485f02005-06-04 21:55:20 +00006545 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006546 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006547
6548 /* Count the number of siblings. */
6549 for (np = node; np != NULL; np = np->wn_sibling)
6550 ++siblingcount;
6551
6552 /* Write the sibling count. */
6553 if (fd != NULL)
6554 putc(siblingcount, fd); /* <siblingcount> */
6555
6556 /* Write each sibling byte and optionally extra info. */
6557 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006558 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006559 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006560 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006563 /* For a NUL byte (end of word) write the flags etc. */
6564 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006565 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006566 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006567 * associated condition nr (stored in wn_region). The
6568 * byte value is misused to store the "rare" and "not
6569 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006570 if (np->wn_flags == (short_u)PFX_FLAGS)
6571 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006572 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00006573 {
6574 putc(BY_FLAGS, fd); /* <byte> */
6575 putc(np->wn_flags, fd); /* <pflags> */
6576 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006577 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006578 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006579 }
6580 else
6581 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006582 /* For word trees we write the flag/region items. */
6583 flags = np->wn_flags;
6584 if (regionmask != 0 && np->wn_region != regionmask)
6585 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006586 if (np->wn_affixID != 0)
6587 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006588 if (flags == 0)
6589 {
6590 /* word without flags or region */
6591 putc(BY_NOFLAGS, fd); /* <byte> */
6592 }
6593 else
6594 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006595 if (np->wn_flags >= 0x100)
6596 {
6597 putc(BY_FLAGS2, fd); /* <byte> */
6598 putc(flags, fd); /* <flags> */
6599 putc((unsigned)flags >> 8, fd); /* <flags2> */
6600 }
6601 else
6602 {
6603 putc(BY_FLAGS, fd); /* <byte> */
6604 putc(flags, fd); /* <flags> */
6605 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006606 if (flags & WF_REGION)
6607 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006608 if (flags & WF_AFX)
6609 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006610 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006611 }
6612 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006613 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006614 else
6615 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006616 if (np->wn_child->wn_u1.index != 0
6617 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006618 {
6619 /* The child is written elsewhere, write the reference. */
6620 if (fd != NULL)
6621 {
6622 putc(BY_INDEX, fd); /* <byte> */
6623 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006624 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006625 }
6626 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00006627 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006628 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006629 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006630
Bram Moolenaar51485f02005-06-04 21:55:20 +00006631 if (fd != NULL)
6632 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
6633 {
6634 EMSG(_(e_write));
6635 return 0;
6636 }
6637 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006638 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006639
6640 /* Space used in the array when reading: one for each sibling and one for
6641 * the count. */
6642 newindex += siblingcount + 1;
6643
6644 /* Recursively dump the children of each sibling. */
6645 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006646 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
6647 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006648 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006649
6650 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006651}
6652
6653
6654/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00006655 * ":mkspell [-ascii] outfile infile ..."
6656 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006657 */
6658 void
6659ex_mkspell(eap)
6660 exarg_T *eap;
6661{
6662 int fcount;
6663 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006664 char_u *arg = eap->arg;
6665 int ascii = FALSE;
6666
6667 if (STRNCMP(arg, "-ascii", 6) == 0)
6668 {
6669 ascii = TRUE;
6670 arg = skipwhite(arg + 6);
6671 }
6672
6673 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
6674 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
6675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006676 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006677 FreeWild(fcount, fnames);
6678 }
6679}
6680
6681/*
6682 * Create a Vim spell file from one or more word lists.
6683 * "fnames[0]" is the output file name.
6684 * "fnames[fcount - 1]" is the last input file name.
6685 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
6686 * and ".spl" is appended to make the output file name.
6687 */
6688 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006689mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006690 int fcount;
6691 char_u **fnames;
6692 int ascii; /* -ascii argument given */
6693 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006694 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006695{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006696 char_u fname[MAXPATHL];
6697 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00006698 char_u **innames;
6699 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006701 int i;
6702 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006703 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006704 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006705 spellinfo_T spin;
6706
6707 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006708 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006709 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006710 spin.si_followup = TRUE;
6711 spin.si_rem_accents = TRUE;
6712 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
6713 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
6714 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006715 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006716
Bram Moolenaarb765d632005-06-07 21:00:02 +00006717 /* default: fnames[0] is output file, following are input files */
6718 innames = &fnames[1];
6719 incount = fcount - 1;
6720
6721 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00006722 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006723 len = STRLEN(fnames[0]);
6724 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
6725 {
6726 /* For ":mkspell path/en.latin1.add" output file is
6727 * "path/en.latin1.add.spl". */
6728 innames = &fnames[0];
6729 incount = 1;
6730 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
6731 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006732 else if (fcount == 1)
6733 {
6734 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
6735 innames = &fnames[0];
6736 incount = 1;
6737 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6738 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6739 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006740 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
6741 {
6742 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006743 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006744 }
6745 else
6746 /* Name should be language, make the file name from it. */
6747 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6748 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6749
6750 /* Check for .ascii.spl. */
6751 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
6752 spin.si_ascii = TRUE;
6753
6754 /* Check for .add.spl. */
6755 if (strstr((char *)gettail(wfname), ".add.") != NULL)
6756 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006757 }
6758
Bram Moolenaarb765d632005-06-07 21:00:02 +00006759 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006760 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006761 else if (vim_strchr(gettail(wfname), '_') != NULL)
6762 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006763 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006764 EMSG(_("E754: Only up to 8 regions supported"));
6765 else
6766 {
6767 /* Check for overwriting before doing things that may take a lot of
6768 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006769 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006770 {
6771 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006772 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006773 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006774 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006775 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006776 EMSG2(_(e_isadir2), wfname);
6777 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006778 }
6779
6780 /*
6781 * Init the aff and dic pointers.
6782 * Get the region names if there are more than 2 arguments.
6783 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006784 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006785 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006786 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006787
Bram Moolenaar3982c542005-06-08 21:56:31 +00006788 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006790 len = STRLEN(innames[i]);
6791 if (STRLEN(gettail(innames[i])) < 5
6792 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006793 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006794 EMSG2(_("E755: Invalid region in %s"), innames[i]);
6795 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006796 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006797 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6798 spin.si_region_name[i * 2 + 1] =
6799 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006800 }
6801 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006802 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006803
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006804 spin.si_foldroot = wordtree_alloc(&spin);
6805 spin.si_keeproot = wordtree_alloc(&spin);
6806 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006807 if (spin.si_foldroot == NULL
6808 || spin.si_keeproot == NULL
6809 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006810 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006811 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006812 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006813 }
6814
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006815 /* When not producing a .add.spl file clear the character table when
6816 * we encounter one in the .aff file. This means we dump the current
6817 * one in the .spl file if the .aff file doesn't define one. That's
6818 * better than guessing the contents, the table will match a
6819 * previously loaded spell file. */
6820 if (!spin.si_add)
6821 spin.si_clear_chartab = TRUE;
6822
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006823 /*
6824 * Read all the .aff and .dic files.
6825 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006826 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006827 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006828 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006829 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006830 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006831 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006832
Bram Moolenaarb765d632005-06-07 21:00:02 +00006833 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006834 if (mch_stat((char *)fname, &st) >= 0)
6835 {
6836 /* Read the .aff file. Will init "spin->si_conv" based on the
6837 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006838 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006839 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006840 error = TRUE;
6841 else
6842 {
6843 /* Read the .dic file and store the words in the trees. */
6844 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00006845 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006846 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006847 error = TRUE;
6848 }
6849 }
6850 else
6851 {
6852 /* No .aff file, try reading the file as a word list. Store
6853 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006854 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006855 error = TRUE;
6856 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006857
Bram Moolenaarb765d632005-06-07 21:00:02 +00006858#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006859 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006860 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006861#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006862 }
6863
Bram Moolenaar51485f02005-06-04 21:55:20 +00006864 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006865 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006866 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006867 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006868 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006869 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006870 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006871 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006872 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006873 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006874 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006875 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006876 verbose_leave();
6877 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006878 wordtree_compress(&spin, spin.si_foldroot);
6879 wordtree_compress(&spin, spin.si_keeproot);
6880 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006881 }
6882
Bram Moolenaar51485f02005-06-04 21:55:20 +00006883 if (!error)
6884 {
6885 /*
6886 * Write the info in the spell file.
6887 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006888 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006889 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006890 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006891 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006892 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006893 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006894 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006895 verbose_leave();
6896 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00006897
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006898 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006899
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006900 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006901 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006902 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006903 verbose_enter();
6904 MSG(_("Done!"));
6905 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00006906 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006907 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006908 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006909 verbose_leave();
6910 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006911
Bram Moolenaarb765d632005-06-07 21:00:02 +00006912 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006913 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006914 }
6915
6916 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006917 ga_clear(&spin.si_rep);
6918 ga_clear(&spin.si_sal);
6919 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006920 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006921 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006922 vim_free(spin.si_sofofr);
6923 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006924
6925 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006926 for (i = 0; i < incount; ++i)
6927 if (afile[i] != NULL)
6928 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006929
6930 /* Free all the bits and pieces at once. */
6931 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006932 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006933}
6934
Bram Moolenaarb765d632005-06-07 21:00:02 +00006935
6936/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006937 * ":[count]spellgood {word}"
6938 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00006939 */
6940 void
6941ex_spell(eap)
6942 exarg_T *eap;
6943{
Bram Moolenaar7887d882005-07-01 22:33:52 +00006944 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006945 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006946}
6947
6948/*
6949 * Add "word[len]" to 'spellfile' as a good or bad word.
6950 */
6951 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006952spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006953 char_u *word;
6954 int len;
6955 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006956 int index; /* "zG" and "zW": zero, otherwise index in
6957 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006958{
6959 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006960 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006961 int new_spf = FALSE;
6962 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006963 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006964 char_u fnamebuf[MAXPATHL];
6965 char_u line[MAXWLEN * 2];
6966 long fpos, fpos_next = 0;
6967 int i;
6968 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006969
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006970 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006971 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006972 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006973 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006974 int_wordlist = vim_tempname('s');
6975 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00006976 return;
6977 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006978 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006979 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006980 else
6981 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006982 /* If 'spellfile' isn't set figure out a good default value. */
6983 if (*curbuf->b_p_spf == NUL)
6984 {
6985 init_spellfile();
6986 new_spf = TRUE;
6987 }
6988
6989 if (*curbuf->b_p_spf == NUL)
6990 {
6991 EMSG(_("E764: 'spellfile' is not set"));
6992 return;
6993 }
6994
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006995 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
6996 {
6997 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
6998 if (i == index)
6999 break;
7000 if (*spf == NUL)
7001 {
7002 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
7003 return;
7004 }
7005 }
7006
Bram Moolenaarb765d632005-06-07 21:00:02 +00007007 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007008 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007009 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7010 buf = NULL;
7011 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007012 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007013 EMSG(_(e_bufloaded));
7014 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007015 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007016
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007017 fname = fnamebuf;
7018 }
7019
7020 if (bad)
7021 {
7022 /* When the word also appears as good word we need to remove that one,
7023 * since its flags sort before the one with WF_BANNED. */
7024 fd = mch_fopen((char *)fname, "r");
7025 if (fd != NULL)
7026 {
7027 while (!vim_fgets(line, MAXWLEN * 2, fd))
7028 {
7029 fpos = fpos_next;
7030 fpos_next = ftell(fd);
7031 if (STRNCMP(word, line, len) == 0
7032 && (line[len] == '/' || line[len] < ' '))
7033 {
7034 /* Found duplicate word. Remove it by writing a '#' at
7035 * the start of the line. Mixing reading and writing
7036 * doesn't work for all systems, close the file first. */
7037 fclose(fd);
7038 fd = mch_fopen((char *)fname, "r+");
7039 if (fd == NULL)
7040 break;
7041 if (fseek(fd, fpos, SEEK_SET) == 0)
7042 fputc('#', fd);
7043 fseek(fd, fpos_next, SEEK_SET);
7044 }
7045 }
7046 fclose(fd);
7047 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007048 }
7049
7050 fd = mch_fopen((char *)fname, "a");
7051 if (fd == NULL && new_spf)
7052 {
7053 /* We just initialized the 'spellfile' option and can't open the file.
7054 * We may need to create the "spell" directory first. We already
7055 * checked the runtime directory is writable in init_spellfile(). */
7056 STRCPY(NameBuff, fname);
7057 *gettail_sep(NameBuff) = NUL;
7058 if (mch_stat((char *)NameBuff, &st) < 0)
7059 {
7060 /* The directory doesn't exist. Try creating it and opening the
7061 * file again. */
7062 vim_mkdir(NameBuff, 0755);
7063 fd = mch_fopen((char *)fname, "a");
7064 }
7065 }
7066
7067 if (fd == NULL)
7068 EMSG2(_(e_notopen), fname);
7069 else
7070 {
7071 if (bad)
7072 fprintf(fd, "%.*s/!\n", len, word);
7073 else
7074 fprintf(fd, "%.*s\n", len, word);
7075 fclose(fd);
7076
7077 /* Update the .add.spl file. */
7078 mkspell(1, &fname, FALSE, TRUE, TRUE);
7079
7080 /* If the .add file is edited somewhere, reload it. */
7081 if (buf != NULL)
7082 buf_reload(buf);
7083
7084 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007085 }
7086}
7087
7088/*
7089 * Initialize 'spellfile' for the current buffer.
7090 */
7091 static void
7092init_spellfile()
7093{
7094 char_u buf[MAXPATHL];
7095 int l;
7096 slang_T *sl;
7097 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007098 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007099
7100 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7101 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007102 /* Find the end of the language name. Exclude the region. */
7103 for (lend = curbuf->b_p_spl; *lend != NUL
7104 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7105 ;
7106
7107 /* Loop over all entries in 'runtimepath'. Use the first one where we
7108 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007109 rtp = p_rtp;
7110 while (*rtp != NUL)
7111 {
7112 /* Copy the path from 'runtimepath' to buf[]. */
7113 copy_option_part(&rtp, buf, MAXPATHL, ",");
7114 if (filewritable(buf) == 2)
7115 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007116 /* Use the first language name from 'spelllang' and the
7117 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007118 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
7119 l = STRLEN(buf);
7120 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007121 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007122 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00007123 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
7124 ? (char_u *)"ascii" : spell_enc());
7125 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7126 break;
7127 }
7128 }
7129 }
7130}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007131
Bram Moolenaar51485f02005-06-04 21:55:20 +00007132
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007133/*
7134 * Init the chartab used for spelling for ASCII.
7135 * EBCDIC is not supported!
7136 */
7137 static void
7138clear_spell_chartab(sp)
7139 spelltab_T *sp;
7140{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007141 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007142
7143 /* Init everything to FALSE. */
7144 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7145 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7146 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007147 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007148 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007149 sp->st_upper[i] = i;
7150 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007151
7152 /* We include digits. A word shouldn't start with a digit, but handling
7153 * that is done separately. */
7154 for (i = '0'; i <= '9'; ++i)
7155 sp->st_isw[i] = TRUE;
7156 for (i = 'A'; i <= 'Z'; ++i)
7157 {
7158 sp->st_isw[i] = TRUE;
7159 sp->st_isu[i] = TRUE;
7160 sp->st_fold[i] = i + 0x20;
7161 }
7162 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007163 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007164 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007165 sp->st_upper[i] = i - 0x20;
7166 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007167}
7168
7169/*
7170 * Init the chartab used for spelling. Only depends on 'encoding'.
7171 * Called once while starting up and when 'encoding' changes.
7172 * The default is to use isalpha(), but the spell file should define the word
7173 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007174 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007175 */
7176 void
7177init_spell_chartab()
7178{
7179 int i;
7180
7181 did_set_spelltab = FALSE;
7182 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007183#ifdef FEAT_MBYTE
7184 if (enc_dbcs)
7185 {
7186 /* DBCS: assume double-wide characters are word characters. */
7187 for (i = 128; i <= 255; ++i)
7188 if (MB_BYTE2LEN(i) == 2)
7189 spelltab.st_isw[i] = TRUE;
7190 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007191 else if (enc_utf8)
7192 {
7193 for (i = 128; i < 256; ++i)
7194 {
7195 spelltab.st_isu[i] = utf_isupper(i);
7196 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7197 spelltab.st_fold[i] = utf_fold(i);
7198 spelltab.st_upper[i] = utf_toupper(i);
7199 }
7200 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007201 else
7202#endif
7203 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007204 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007205 for (i = 128; i < 256; ++i)
7206 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007207 if (MB_ISUPPER(i))
7208 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007209 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007210 spelltab.st_isu[i] = TRUE;
7211 spelltab.st_fold[i] = MB_TOLOWER(i);
7212 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007213 else if (MB_ISLOWER(i))
7214 {
7215 spelltab.st_isw[i] = TRUE;
7216 spelltab.st_upper[i] = MB_TOUPPER(i);
7217 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007218 }
7219 }
7220}
7221
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007222static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
7223static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
7224
7225/*
7226 * Set the spell character tables from strings in the affix file.
7227 */
7228 static int
7229set_spell_chartab(fol, low, upp)
7230 char_u *fol;
7231 char_u *low;
7232 char_u *upp;
7233{
7234 /* We build the new tables here first, so that we can compare with the
7235 * previous one. */
7236 spelltab_T new_st;
7237 char_u *pf = fol, *pl = low, *pu = upp;
7238 int f, l, u;
7239
7240 clear_spell_chartab(&new_st);
7241
7242 while (*pf != NUL)
7243 {
7244 if (*pl == NUL || *pu == NUL)
7245 {
7246 EMSG(_(e_affform));
7247 return FAIL;
7248 }
7249#ifdef FEAT_MBYTE
7250 f = mb_ptr2char_adv(&pf);
7251 l = mb_ptr2char_adv(&pl);
7252 u = mb_ptr2char_adv(&pu);
7253#else
7254 f = *pf++;
7255 l = *pl++;
7256 u = *pu++;
7257#endif
7258 /* Every character that appears is a word character. */
7259 if (f < 256)
7260 new_st.st_isw[f] = TRUE;
7261 if (l < 256)
7262 new_st.st_isw[l] = TRUE;
7263 if (u < 256)
7264 new_st.st_isw[u] = TRUE;
7265
7266 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7267 * case-folding */
7268 if (l < 256 && l != f)
7269 {
7270 if (f >= 256)
7271 {
7272 EMSG(_(e_affrange));
7273 return FAIL;
7274 }
7275 new_st.st_fold[l] = f;
7276 }
7277
7278 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007279 * case-folding, it's upper case and the "UPP" is the upper case of
7280 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007281 if (u < 256 && u != f)
7282 {
7283 if (f >= 256)
7284 {
7285 EMSG(_(e_affrange));
7286 return FAIL;
7287 }
7288 new_st.st_fold[u] = f;
7289 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007290 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007291 }
7292 }
7293
7294 if (*pl != NUL || *pu != NUL)
7295 {
7296 EMSG(_(e_affform));
7297 return FAIL;
7298 }
7299
7300 return set_spell_finish(&new_st);
7301}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007302
7303/*
7304 * Set the spell character tables from strings in the .spl file.
7305 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007306 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007307set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007308 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007309 int cnt; /* length of "flags" */
7310 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007311{
7312 /* We build the new tables here first, so that we can compare with the
7313 * previous one. */
7314 spelltab_T new_st;
7315 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007316 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007317 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007318
7319 clear_spell_chartab(&new_st);
7320
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007321 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007322 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007323 if (i < cnt)
7324 {
7325 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7326 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7327 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007328
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007329 if (*p != NUL)
7330 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007331#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007332 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007333#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007334 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007335#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007336 new_st.st_fold[i + 128] = c;
7337 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7338 new_st.st_upper[c] = i + 128;
7339 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007340 }
7341
Bram Moolenaar5195e452005-08-19 20:32:47 +00007342 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007343}
7344
7345 static int
7346set_spell_finish(new_st)
7347 spelltab_T *new_st;
7348{
7349 int i;
7350
7351 if (did_set_spelltab)
7352 {
7353 /* check that it's the same table */
7354 for (i = 0; i < 256; ++i)
7355 {
7356 if (spelltab.st_isw[i] != new_st->st_isw[i]
7357 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007358 || spelltab.st_fold[i] != new_st->st_fold[i]
7359 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007360 {
7361 EMSG(_("E763: Word characters differ between spell files"));
7362 return FAIL;
7363 }
7364 }
7365 }
7366 else
7367 {
7368 /* copy the new spelltab into the one being used */
7369 spelltab = *new_st;
7370 did_set_spelltab = TRUE;
7371 }
7372
7373 return OK;
7374}
7375
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007376/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007377 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007378 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007379 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007380 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007381 */
7382 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007383spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007384 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007385 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007386{
Bram Moolenaarea408852005-06-25 22:49:46 +00007387#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007388 char_u *s;
7389 int l;
7390 int c;
7391
7392 if (has_mbyte)
7393 {
7394 l = MB_BYTE2LEN(*p);
7395 s = p;
7396 if (l == 1)
7397 {
7398 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007399 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007400 {
7401 s = p + 1; /* skip a mid-word character */
7402 l = MB_BYTE2LEN(*s);
7403 }
7404 }
7405 else
7406 {
7407 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007408 if (c < 256 ? buf->b_spell_ismw[c]
7409 : (buf->b_spell_ismw_mb != NULL
7410 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007411 {
7412 s = p + l;
7413 l = MB_BYTE2LEN(*s);
7414 }
7415 }
7416
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007417 c = mb_ptr2char(s);
7418 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007419 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007420 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007421 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007422#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007423
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007424 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
7425}
7426
7427/*
7428 * Return TRUE if "p" points to a word character.
7429 * Unlike spell_iswordp() this doesn't check for "midword" characters.
7430 */
7431 static int
7432spell_iswordp_nmw(p)
7433 char_u *p;
7434{
7435#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007436 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007437
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007438 if (has_mbyte)
7439 {
7440 c = mb_ptr2char(p);
7441 if (c > 255)
7442 return mb_get_class(p) >= 2;
7443 return spelltab.st_isw[c];
7444 }
7445#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007446 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00007447}
7448
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007449#ifdef FEAT_MBYTE
7450/*
7451 * Return TRUE if "p" points to a word character.
7452 * Wide version of spell_iswordp().
7453 */
7454 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007455spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007456 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007457 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007458{
7459 int *s;
7460
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007461 if (*p < 256 ? buf->b_spell_ismw[*p]
7462 : (buf->b_spell_ismw_mb != NULL
7463 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007464 s = p + 1;
7465 else
7466 s = p;
7467
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007468 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007469 {
7470 if (enc_utf8)
7471 return utf_class(*s) >= 2;
7472 if (enc_dbcs)
7473 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
7474 return 0;
7475 }
7476 return spelltab.st_isw[*s];
7477}
7478#endif
7479
Bram Moolenaarea408852005-06-25 22:49:46 +00007480/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007481 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007482 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007483 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007484 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007485write_spell_prefcond(fd, gap)
7486 FILE *fd;
7487 garray_T *gap;
7488{
7489 int i;
7490 char_u *p;
7491 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007492 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007493
Bram Moolenaar5195e452005-08-19 20:32:47 +00007494 if (fd != NULL)
7495 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
7496
7497 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007498
7499 for (i = 0; i < gap->ga_len; ++i)
7500 {
7501 /* <prefcond> : <condlen> <condstr> */
7502 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00007503 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007504 {
7505 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007506 if (fd != NULL)
7507 {
7508 fputc(len, fd);
7509 fwrite(p, (size_t)len, (size_t)1, fd);
7510 }
7511 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007512 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007513 else if (fd != NULL)
7514 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007515 }
7516
Bram Moolenaar5195e452005-08-19 20:32:47 +00007517 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007518}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007519
7520/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007521 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
7522 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007523 * When using a multi-byte 'encoding' the length may change!
7524 * Returns FAIL when something wrong.
7525 */
7526 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007527spell_casefold(str, len, buf, buflen)
7528 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007529 int len;
7530 char_u *buf;
7531 int buflen;
7532{
7533 int i;
7534
7535 if (len >= buflen)
7536 {
7537 buf[0] = NUL;
7538 return FAIL; /* result will not fit */
7539 }
7540
7541#ifdef FEAT_MBYTE
7542 if (has_mbyte)
7543 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007544 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007545 char_u *p;
7546 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007547
7548 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007549 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007550 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007551 if (outi + MB_MAXBYTES > buflen)
7552 {
7553 buf[outi] = NUL;
7554 return FAIL;
7555 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007556 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007557 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007558 }
7559 buf[outi] = NUL;
7560 }
7561 else
7562#endif
7563 {
7564 /* Be quick for non-multibyte encodings. */
7565 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007566 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007567 buf[i] = NUL;
7568 }
7569
7570 return OK;
7571}
7572
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007573#define SPS_BEST 1
7574#define SPS_FAST 2
7575#define SPS_DOUBLE 4
7576
7577static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007578static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007579
7580/*
7581 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007582 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007583 */
7584 int
7585spell_check_sps()
7586{
7587 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007588 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007589 char_u buf[MAXPATHL];
7590 int f;
7591
7592 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007593 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007594
7595 for (p = p_sps; *p != NUL; )
7596 {
7597 copy_option_part(&p, buf, MAXPATHL, ",");
7598
7599 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007600 if (VIM_ISDIGIT(*buf))
7601 {
7602 s = buf;
7603 sps_limit = getdigits(&s);
7604 if (*s != NUL && !VIM_ISDIGIT(*s))
7605 f = -1;
7606 }
7607 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007608 f = SPS_BEST;
7609 else if (STRCMP(buf, "fast") == 0)
7610 f = SPS_FAST;
7611 else if (STRCMP(buf, "double") == 0)
7612 f = SPS_DOUBLE;
7613 else if (STRNCMP(buf, "expr:", 5) != 0
7614 && STRNCMP(buf, "file:", 5) != 0)
7615 f = -1;
7616
7617 if (f == -1 || (sps_flags != 0 && f != 0))
7618 {
7619 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007620 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007621 return FAIL;
7622 }
7623 if (f != 0)
7624 sps_flags = f;
7625 }
7626
7627 if (sps_flags == 0)
7628 sps_flags = SPS_BEST;
7629
7630 return OK;
7631}
7632
7633/* Remember what "z?" replaced. */
7634static char_u *repl_from = NULL;
7635static char_u *repl_to = NULL;
7636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007637/*
7638 * "z?": Find badly spelled word under or after the cursor.
7639 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00007640 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 */
7642 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00007643spell_suggest(count)
7644 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645{
7646 char_u *line;
7647 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007648 char_u wcopy[MAXWLEN + 2];
7649 char_u *p;
7650 int i;
7651 int c;
7652 suginfo_T sug;
7653 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007654 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007655 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007656 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00007657 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007658
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007659 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007660 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
7661 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007662 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007663 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
7664 return;
7665
7666 /* No bad word or it starts after the cursor: use the word under the
7667 * cursor. */
7668 curwin->w_cursor = prev_cursor;
7669 line = ml_get_curline();
7670 p = line + curwin->w_cursor.col;
7671 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007672 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007673 mb_ptr_back(line, p);
7674 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007675 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007676 mb_ptr_adv(p);
7677
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007678 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007679 {
7680 beep_flush();
7681 return;
7682 }
7683 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 }
7685
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007686 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007687
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007688 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007689 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007690
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007691 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007692
Bram Moolenaar5195e452005-08-19 20:32:47 +00007693 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
7694 * 'spellsuggest', whatever is smaller. */
7695 if (sps_limit > (int)Rows - 2)
7696 limit = (int)Rows - 2;
7697 else
7698 limit = sps_limit;
7699 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007700 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007701
7702 if (sug.su_ga.ga_len == 0)
7703 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00007704 else if (count > 0)
7705 {
7706 if (count > sug.su_ga.ga_len)
7707 smsg((char_u *)_("Sorry, only %ld suggestions"),
7708 (long)sug.su_ga.ga_len);
7709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007710 else
7711 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007712 vim_free(repl_from);
7713 repl_from = NULL;
7714 vim_free(repl_to);
7715 repl_to = NULL;
7716
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007717#ifdef FEAT_RIGHTLEFT
7718 /* When 'rightleft' is set the list is drawn right-left. */
7719 cmdmsg_rl = curwin->w_p_rl;
7720 if (cmdmsg_rl)
7721 msg_col = Columns - 1;
7722#endif
7723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007724 /* List the suggestions. */
7725 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007726 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007727 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
7728 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007729#ifdef FEAT_RIGHTLEFT
7730 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
7731 {
7732 /* And now the rabbit from the high hat: Avoid showing the
7733 * untranslated message rightleft. */
7734 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
7735 sug.su_badlen, sug.su_badptr);
7736 }
7737#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738 msg_puts(IObuff);
7739 msg_clr_eos();
7740 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00007741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007742 msg_scroll = TRUE;
7743 for (i = 0; i < sug.su_ga.ga_len; ++i)
7744 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007745 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007746
7747 /* The suggested word may replace only part of the bad word, add
7748 * the not replaced part. */
7749 STRCPY(wcopy, stp->st_word);
7750 if (sug.su_badlen > stp->st_orglen)
7751 vim_strncpy(wcopy + STRLEN(wcopy),
7752 sug.su_badptr + stp->st_orglen,
7753 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007754 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
7755#ifdef FEAT_RIGHTLEFT
7756 if (cmdmsg_rl)
7757 rl_mirror(IObuff);
7758#endif
7759 msg_puts(IObuff);
7760
7761 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007762 msg_puts(IObuff);
7763
7764 /* The word may replace more than "su_badlen". */
7765 if (sug.su_badlen < stp->st_orglen)
7766 {
7767 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
7768 stp->st_orglen, sug.su_badptr);
7769 msg_puts(IObuff);
7770 }
7771
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007772 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007773 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007774 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007775 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007776 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007777 stp->st_salscore ? "s " : "",
7778 stp->st_score, stp->st_altscore);
7779 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007780 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00007781 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007782#ifdef FEAT_RIGHTLEFT
7783 if (cmdmsg_rl)
7784 /* Mirror the numbers, but keep the leading space. */
7785 rl_mirror(IObuff + 1);
7786#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00007787 msg_advance(30);
7788 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007790 msg_putchar('\n');
7791 }
7792
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007793#ifdef FEAT_RIGHTLEFT
7794 cmdmsg_rl = FALSE;
7795 msg_col = 0;
7796#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007797 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00007798 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007799 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00007800 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007801 }
7802
Bram Moolenaard12a1322005-08-21 22:08:24 +00007803 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
7804 {
7805 /* Save the from and to text for :spellrepall. */
7806 stp = &SUG(sug.su_ga, selected - 1);
7807 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
7808 repl_to = vim_strsave(stp->st_word);
7809
7810 /* Replace the word. */
7811 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
7812 if (p != NULL)
7813 {
7814 c = sug.su_badptr - line;
7815 mch_memmove(p, line, c);
7816 STRCPY(p + c, stp->st_word);
7817 STRCAT(p, sug.su_badptr + stp->st_orglen);
7818 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7819 curwin->w_cursor.col = c;
7820 changed_bytes(curwin->w_cursor.lnum, c);
7821
7822 /* For redo we use a change-word command. */
7823 ResetRedobuff();
7824 AppendToRedobuff((char_u *)"ciw");
7825 AppendToRedobuff(stp->st_word);
7826 AppendCharToRedobuff(ESC);
7827 }
7828 }
7829 else
7830 curwin->w_cursor = prev_cursor;
7831
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007832 spell_find_cleanup(&sug);
7833}
7834
7835/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007836 * Check if the word at line "lnum" column "col" is required to start with a
7837 * capital. This uses 'spellcapcheck' of the current buffer.
7838 */
7839 static int
7840check_need_cap(lnum, col)
7841 linenr_T lnum;
7842 colnr_T col;
7843{
7844 int need_cap = FALSE;
7845 char_u *line;
7846 char_u *line_copy = NULL;
7847 char_u *p;
7848 colnr_T endcol;
7849 regmatch_T regmatch;
7850
7851 if (curbuf->b_cap_prog == NULL)
7852 return FALSE;
7853
7854 line = ml_get_curline();
7855 endcol = 0;
7856 if ((int)(skipwhite(line) - line) >= (int)col)
7857 {
7858 /* At start of line, check if previous line is empty or sentence
7859 * ends there. */
7860 if (lnum == 1)
7861 need_cap = TRUE;
7862 else
7863 {
7864 line = ml_get(lnum - 1);
7865 if (*skipwhite(line) == NUL)
7866 need_cap = TRUE;
7867 else
7868 {
7869 /* Append a space in place of the line break. */
7870 line_copy = concat_str(line, (char_u *)" ");
7871 line = line_copy;
7872 endcol = STRLEN(line);
7873 }
7874 }
7875 }
7876 else
7877 endcol = col;
7878
7879 if (endcol > 0)
7880 {
7881 /* Check if sentence ends before the bad word. */
7882 regmatch.regprog = curbuf->b_cap_prog;
7883 regmatch.rm_ic = FALSE;
7884 p = line + endcol;
7885 for (;;)
7886 {
7887 mb_ptr_back(line, p);
7888 if (p == line || spell_iswordp_nmw(p))
7889 break;
7890 if (vim_regexec(&regmatch, p, 0)
7891 && regmatch.endp[0] == line + endcol)
7892 {
7893 need_cap = TRUE;
7894 break;
7895 }
7896 }
7897 }
7898
7899 vim_free(line_copy);
7900
7901 return need_cap;
7902}
7903
7904
7905/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007906 * ":spellrepall"
7907 */
7908/*ARGSUSED*/
7909 void
7910ex_spellrepall(eap)
7911 exarg_T *eap;
7912{
7913 pos_T pos = curwin->w_cursor;
7914 char_u *frompat;
7915 int addlen;
7916 char_u *line;
7917 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007918 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007919 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007920
7921 if (repl_from == NULL || repl_to == NULL)
7922 {
7923 EMSG(_("E752: No previous spell replacement"));
7924 return;
7925 }
7926 addlen = STRLEN(repl_to) - STRLEN(repl_from);
7927
7928 frompat = alloc(STRLEN(repl_from) + 7);
7929 if (frompat == NULL)
7930 return;
7931 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
7932 p_ws = FALSE;
7933
Bram Moolenaar5195e452005-08-19 20:32:47 +00007934 sub_nsubs = 0;
7935 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007936 curwin->w_cursor.lnum = 0;
7937 while (!got_int)
7938 {
7939 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
7940 || u_save_cursor() == FAIL)
7941 break;
7942
7943 /* Only replace when the right word isn't there yet. This happens
7944 * when changing "etc" to "etc.". */
7945 line = ml_get_curline();
7946 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
7947 repl_to, STRLEN(repl_to)) != 0)
7948 {
7949 p = alloc(STRLEN(line) + addlen + 1);
7950 if (p == NULL)
7951 break;
7952 mch_memmove(p, line, curwin->w_cursor.col);
7953 STRCPY(p + curwin->w_cursor.col, repl_to);
7954 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
7955 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7956 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007957
7958 if (curwin->w_cursor.lnum != prev_lnum)
7959 {
7960 ++sub_nlines;
7961 prev_lnum = curwin->w_cursor.lnum;
7962 }
7963 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007964 }
7965 curwin->w_cursor.col += STRLEN(repl_to);
7966 }
7967
7968 p_ws = save_ws;
7969 curwin->w_cursor = pos;
7970 vim_free(frompat);
7971
Bram Moolenaar5195e452005-08-19 20:32:47 +00007972 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007973 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007974 else
7975 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007976}
7977
7978/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007979 * Find spell suggestions for "word". Return them in the growarray "*gap" as
7980 * a list of allocated strings.
7981 */
7982 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007983spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007984 garray_T *gap;
7985 char_u *word;
7986 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007987 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007988{
7989 suginfo_T sug;
7990 int i;
7991 suggest_T *stp;
7992 char_u *wcopy;
7993
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007994 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007995
7996 /* Make room in "gap". */
7997 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
7998 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
7999 return;
8000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008001 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008002 {
8003 stp = &SUG(sug.su_ga, i);
8004
8005 /* The suggested word may replace only part of "word", add the not
8006 * replaced part. */
8007 wcopy = alloc(STRLEN(stp->st_word)
8008 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8009 if (wcopy == NULL)
8010 break;
8011 STRCPY(wcopy, stp->st_word);
8012 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8013 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8014 }
8015
8016 spell_find_cleanup(&sug);
8017}
8018
8019/*
8020 * Find spell suggestions for the word at the start of "badptr".
8021 * Return the suggestions in "su->su_ga".
8022 * The maximum number of suggestions is "maxcount".
8023 * Note: does use info for the current window.
8024 * This is based on the mechanisms of Aspell, but completely reimplemented.
8025 */
8026 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008027spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008028 char_u *badptr;
8029 suginfo_T *su;
8030 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008031 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008032 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008033{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008034 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008035 char_u buf[MAXPATHL];
8036 char_u *p;
8037 int do_combine = FALSE;
8038 char_u *sps_copy;
8039#ifdef FEAT_EVAL
8040 static int expr_busy = FALSE;
8041#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008042 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008043
8044 /*
8045 * Set the info in "*su".
8046 */
8047 vim_memset(su, 0, sizeof(suginfo_T));
8048 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8049 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008050 if (*badptr == NUL)
8051 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008052 hash_init(&su->su_banned);
8053
8054 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008055 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008056 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008057 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008058
8059 if (su->su_badlen >= MAXWLEN)
8060 su->su_badlen = MAXWLEN - 1; /* just in case */
8061 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8062 (void)spell_casefold(su->su_badptr, su->su_badlen,
8063 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008064 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008065 su->su_badflags = badword_captype(su->su_badptr,
8066 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008067 if (need_cap)
8068 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008069
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008070 /* If the word is not capitalised and spell_check() doesn't consider the
8071 * word to be bad then it might need to be capitalised. Add a suggestion
8072 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008073 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008074 if (!SPELL_ISUPPER(c) && attr == 0)
8075 {
8076 make_case_word(su->su_badword, buf, WF_ONECAP);
8077 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8078 0, TRUE);
8079 }
8080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008081 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008082 if (banbadword)
8083 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008084
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008085 /* Make a copy of 'spellsuggest', because the expression may change it. */
8086 sps_copy = vim_strsave(p_sps);
8087 if (sps_copy == NULL)
8088 return;
8089
8090 /* Loop over the items in 'spellsuggest'. */
8091 for (p = sps_copy; *p != NUL; )
8092 {
8093 copy_option_part(&p, buf, MAXPATHL, ",");
8094
8095 if (STRNCMP(buf, "expr:", 5) == 0)
8096 {
8097#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008098 /* Evaluate an expression. Skip this when called recursively,
8099 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008100 if (!expr_busy)
8101 {
8102 expr_busy = TRUE;
8103 spell_suggest_expr(su, buf + 5);
8104 expr_busy = FALSE;
8105 }
8106#endif
8107 }
8108 else if (STRNCMP(buf, "file:", 5) == 0)
8109 /* Use list of suggestions in a file. */
8110 spell_suggest_file(su, buf + 5);
8111 else
8112 {
8113 /* Use internal method. */
8114 spell_suggest_intern(su);
8115 if (sps_flags & SPS_DOUBLE)
8116 do_combine = TRUE;
8117 }
8118 }
8119
8120 vim_free(sps_copy);
8121
8122 if (do_combine)
8123 /* Combine the two list of suggestions. This must be done last,
8124 * because sorting changes the order again. */
8125 score_combine(su);
8126}
8127
8128#ifdef FEAT_EVAL
8129/*
8130 * Find suggestions by evaluating expression "expr".
8131 */
8132 static void
8133spell_suggest_expr(su, expr)
8134 suginfo_T *su;
8135 char_u *expr;
8136{
8137 list_T *list;
8138 listitem_T *li;
8139 int score;
8140 char_u *p;
8141
8142 /* The work is split up in a few parts to avoid having to export
8143 * suginfo_T.
8144 * First evaluate the expression and get the resulting list. */
8145 list = eval_spell_expr(su->su_badword, expr);
8146 if (list != NULL)
8147 {
8148 /* Loop over the items in the list. */
8149 for (li = list->lv_first; li != NULL; li = li->li_next)
8150 if (li->li_tv.v_type == VAR_LIST)
8151 {
8152 /* Get the word and the score from the items. */
8153 score = get_spellword(li->li_tv.vval.v_list, &p);
8154 if (score >= 0)
8155 add_suggestion(su, &su->su_ga, p,
8156 su->su_badlen, score, 0, TRUE);
8157 }
8158 list_unref(list);
8159 }
8160
8161 /* Sort the suggestions and truncate at "maxcount". */
8162 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8163}
8164#endif
8165
8166/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008167 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008168 */
8169 static void
8170spell_suggest_file(su, fname)
8171 suginfo_T *su;
8172 char_u *fname;
8173{
8174 FILE *fd;
8175 char_u line[MAXWLEN * 2];
8176 char_u *p;
8177 int len;
8178 char_u cword[MAXWLEN];
8179
8180 /* Open the file. */
8181 fd = mch_fopen((char *)fname, "r");
8182 if (fd == NULL)
8183 {
8184 EMSG2(_(e_notopen), fname);
8185 return;
8186 }
8187
8188 /* Read it line by line. */
8189 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8190 {
8191 line_breakcheck();
8192
8193 p = vim_strchr(line, '/');
8194 if (p == NULL)
8195 continue; /* No Tab found, just skip the line. */
8196 *p++ = NUL;
8197 if (STRICMP(su->su_badword, line) == 0)
8198 {
8199 /* Match! Isolate the good word, until CR or NL. */
8200 for (len = 0; p[len] >= ' '; ++len)
8201 ;
8202 p[len] = NUL;
8203
8204 /* If the suggestion doesn't have specific case duplicate the case
8205 * of the bad word. */
8206 if (captype(p, NULL) == 0)
8207 {
8208 make_case_word(p, cword, su->su_badflags);
8209 p = cword;
8210 }
8211
8212 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8213 SCORE_FILE, 0, TRUE);
8214 }
8215 }
8216
8217 fclose(fd);
8218
8219 /* Sort the suggestions and truncate at "maxcount". */
8220 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8221}
8222
8223/*
8224 * Find suggestions for the internal method indicated by "sps_flags".
8225 */
8226 static void
8227spell_suggest_intern(su)
8228 suginfo_T *su;
8229{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008230 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008231 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008232 *
8233 * Set a maximum score to limit the combination of operations that is
8234 * tried.
8235 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008236 suggest_try_special(su);
8237
8238 /*
8239 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8240 * from the .aff file and inserting a space (split the word).
8241 */
8242 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008243
8244 /* For the resulting top-scorers compute the sound-a-like score. */
8245 if (sps_flags & SPS_DOUBLE)
8246 score_comp_sal(su);
8247
8248 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008249 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008250 *
8251 * Only do this when we don't have a lot of suggestions yet, because it's
8252 * very slow and often doesn't find new suggestions.
8253 */
8254 if ((sps_flags & SPS_DOUBLE)
8255 || (!(sps_flags & SPS_FAST)
8256 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8257 {
8258 /* Allow a higher score now. */
8259 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008260 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008261 }
8262
8263 /* When CTRL-C was hit while searching do show the results. */
8264 ui_breakcheck();
8265 if (got_int)
8266 {
8267 (void)vgetc();
8268 got_int = FALSE;
8269 }
8270
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008271 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008272 {
8273 if (sps_flags & SPS_BEST)
8274 /* Adjust the word score for how it sounds like. */
8275 rescore_suggestions(su);
8276
8277 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008278 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008279 }
8280}
8281
8282/*
8283 * Free the info put in "*su" by spell_find_suggest().
8284 */
8285 static void
8286spell_find_cleanup(su)
8287 suginfo_T *su;
8288{
8289 int i;
8290
8291 /* Free the suggestions. */
8292 for (i = 0; i < su->su_ga.ga_len; ++i)
8293 vim_free(SUG(su->su_ga, i).st_word);
8294 ga_clear(&su->su_ga);
8295 for (i = 0; i < su->su_sga.ga_len; ++i)
8296 vim_free(SUG(su->su_sga, i).st_word);
8297 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008298
8299 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008300 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008301}
8302
8303/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008304 * Make a copy of "word", with the first letter upper or lower cased, to
8305 * "wcopy[MAXWLEN]". "word" must not be empty.
8306 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008307 */
8308 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008309onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008310 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 char_u *wcopy;
8312 int upper; /* TRUE: first letter made upper case */
8313{
8314 char_u *p;
8315 int c;
8316 int l;
8317
8318 p = word;
8319#ifdef FEAT_MBYTE
8320 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008321 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008322 else
8323#endif
8324 c = *p++;
8325 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008326 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008327 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008328 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008329#ifdef FEAT_MBYTE
8330 if (has_mbyte)
8331 l = mb_char2bytes(c, wcopy);
8332 else
8333#endif
8334 {
8335 l = 1;
8336 wcopy[0] = c;
8337 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008338 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008339}
8340
8341/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008342 * Make a copy of "word" with all the letters upper cased into
8343 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008344 */
8345 static void
8346allcap_copy(word, wcopy)
8347 char_u *word;
8348 char_u *wcopy;
8349{
8350 char_u *s;
8351 char_u *d;
8352 int c;
8353
8354 d = wcopy;
8355 for (s = word; *s != NUL; )
8356 {
8357#ifdef FEAT_MBYTE
8358 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008359 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008360 else
8361#endif
8362 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008363 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008364
8365#ifdef FEAT_MBYTE
8366 if (has_mbyte)
8367 {
8368 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8369 break;
8370 d += mb_char2bytes(c, d);
8371 }
8372 else
8373#endif
8374 {
8375 if (d - wcopy >= MAXWLEN - 1)
8376 break;
8377 *d++ = c;
8378 }
8379 }
8380 *d = NUL;
8381}
8382
8383/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008384 * Try finding suggestions by recognizing specific situations.
8385 */
8386 static void
8387suggest_try_special(su)
8388 suginfo_T *su;
8389{
8390 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008391 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008392 int c;
8393 char_u word[MAXWLEN];
8394
8395 /*
8396 * Recognize a word that is repeated: "the the".
8397 */
8398 p = skiptowhite(su->su_fbadword);
8399 len = p - su->su_fbadword;
8400 p = skipwhite(p);
8401 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
8402 {
8403 /* Include badflags: if the badword is onecap or allcap
8404 * use that for the goodword too: "The the" -> "The". */
8405 c = su->su_fbadword[len];
8406 su->su_fbadword[len] = NUL;
8407 make_case_word(su->su_fbadword, word, su->su_badflags);
8408 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008409 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008410 }
8411}
8412
8413/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00008415 *
8416 * This uses a state machine. At each node in the tree we try various
8417 * operations. When trying if an operation work "depth" is increased and the
8418 * stack[] is used to store info. This allows combinations, thus insert one
8419 * character, replace one and delete another. The number of changes is
8420 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00008421 *
8422 * After implementing this I noticed an article by Kemal Oflazer that
8423 * describes something similar: "Error-tolerant Finite State Recognition with
8424 * Applications to Morphological Analysis and Spelling Correction" (1996).
8425 * The implementation in the article is simplified and requires a stack of
8426 * unknown depth. The implementation here only needs a stack depth of the
8427 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 */
8429 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008430suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 suginfo_T *su;
8432{
8433 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
8434 char_u tword[MAXWLEN]; /* good word collected so far */
8435 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008436 char_u preword[MAXWLEN * 3]; /* word found with proper case;
8437 * concatanation of prefix compound
8438 * words and split word. NUL terminated
8439 * when going deeper but not when coming
8440 * back. */
8441 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008442 trystate_T *sp;
8443 int newscore;
8444 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008445 char_u *byts, *fbyts, *pbyts;
8446 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008447 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008448 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008449 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008452 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008453 int len;
8454 char_u *p;
8455 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00008456 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008457 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008458 slang_T *slang;
8459 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460
8461 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00008462 * to find matches (esp. REP items). Append some more text, changing
8463 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008464 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008465 n = STRLEN(fword);
8466 p = su->su_badptr + su->su_badlen;
8467 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468
8469 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8470 lp->lp_slang != NULL; ++lp)
8471 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008472 slang = lp->lp_slang;
8473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008474 /*
8475 * Go through the whole case-fold tree, try changes at each node.
8476 * "tword[]" contains the word collected from nodes in the tree.
8477 * "fword[]" the word we are trying to match with (initially the bad
8478 * word).
8479 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008481 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008482 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008483 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484
Bram Moolenaarea424162005-06-16 21:51:00 +00008485 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008486 * When there are postponed prefixes we need to use these first. At
8487 * the end of the prefix we continue in the case-fold tree.
8488 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008489 fbyts = slang->sl_fbyts;
8490 fidxs = slang->sl_fidxs;
8491 pbyts = slang->sl_pbyts;
8492 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008493 if (pbyts != NULL)
8494 {
8495 byts = pbyts;
8496 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008497 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008498 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
8499 }
8500 else
8501 {
8502 byts = fbyts;
8503 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008504 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008505 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008506 }
8507
8508 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00008509 * Loop to find all suggestions. At each round we either:
8510 * - For the current state try one operation, advance "ts_curi",
8511 * increase "depth".
8512 * - When a state is done go to the next, set "ts_state".
8513 * - When all states are tried decrease "depth".
8514 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 while (depth >= 0 && !got_int)
8516 {
8517 sp = &stack[depth];
8518 switch (sp->ts_state)
8519 {
8520 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008521 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008522 /*
8523 * Start of node: Deal with NUL bytes, which means
8524 * tword[] may end here.
8525 */
8526 arridx = sp->ts_arridx; /* current node in the tree */
8527 len = byts[arridx]; /* bytes in this node */
8528 arridx += sp->ts_curi; /* index of current byte */
8529
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008530 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008531 {
8532 /* Skip over the NUL bytes, we use them later. */
8533 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
8534 ;
8535 sp->ts_curi += n;
8536
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008537 /* Always past NUL bytes now. */
8538 n = (int)sp->ts_state;
8539 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008540 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008541
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008542 /* At end of a prefix or at start of prefixtree: check for
8543 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008544 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008545 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00008546 /* Set su->su_badflags to the caps type at this
8547 * position. Use the caps type until here for the
8548 * prefix itself. */
8549#ifdef FEAT_MBYTE
8550 if (has_mbyte)
8551 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
8552 else
8553#endif
8554 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008555 flags = badword_captype(su->su_badptr,
8556 su->su_badptr + n);
8557 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008558 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008559 ++depth;
8560 stack[depth] = stack[depth - 1];
8561 sp = &stack[depth];
8562 sp->ts_prefixdepth = depth - 1;
8563 byts = fbyts;
8564 idxs = fidxs;
8565 sp->ts_state = STATE_START;
8566 sp->ts_curi = 1; /* start just after length byte */
8567 sp->ts_arridx = 0;
8568
Bram Moolenaar53805d12005-08-01 07:08:33 +00008569 /* Move the prefix to preword[] with the right case
8570 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008571 tword[sp->ts_twordlen] = NUL;
8572 make_case_word(tword + sp->ts_splitoff,
8573 preword + sp->ts_prewordlen,
8574 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008575 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00008576 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008577 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008578 break;
8579 }
8580
Bram Moolenaar0c405862005-06-22 22:26:26 +00008581 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008582 {
8583 /* Past bytes in node and/or past NUL bytes. */
8584 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008585 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008586 break;
8587 }
8588
8589 /*
8590 * End of word in tree.
8591 */
8592 ++sp->ts_curi; /* eat one NUL byte */
8593
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008594 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008595 fword_ends = (fword[sp->ts_fidx] == NUL
8596 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
8597 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008598
Bram Moolenaard12a1322005-08-21 22:08:24 +00008599 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
8600 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008601 {
8602 /* There was a prefix before the word. Check that the
8603 * prefix can be used with this word. */
8604 /* Count the length of the NULs in the prefix. If there
8605 * are none this must be the first try without a prefix.
8606 */
8607 n = stack[sp->ts_prefixdepth].ts_arridx;
8608 len = pbyts[n++];
8609 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
8610 ;
8611 if (c > 0)
8612 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008613 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008614 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008615 if (c == 0)
8616 break;
8617
8618 /* Use the WF_RARE flag for a rare prefix. */
8619 if (c & WF_RAREPFX)
8620 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008621
8622 /* Tricky: when checking for both prefix and
8623 * compounding we run into the prefix flag first.
8624 * Remember that it's OK, so that we accept the prefix
8625 * when arriving at a compound flag. */
8626 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008627 }
8628 }
8629
Bram Moolenaard12a1322005-08-21 22:08:24 +00008630 if (sp->ts_complen > sp->ts_compsplit)
8631 {
8632 /* There was a compound word before this word. If this
8633 * word does not support compounding then give up
8634 * (splitting is tried for the word without compound
8635 * flag). */
8636 if (((unsigned)flags >> 24) == 0
8637 || sp->ts_twordlen - sp->ts_splitoff
8638 < slang->sl_compminlen)
8639 break;
8640 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8641 compflags[sp->ts_complen + 1] = NUL;
Bram Moolenaare52325c2005-08-22 22:54:29 +00008642 vim_strncpy(preword + sp->ts_prewordlen,
8643 tword + sp->ts_splitoff,
8644 sp->ts_twordlen - sp->ts_splitoff);
8645 p = preword;
8646 while (*skiptowhite(p) != NUL)
8647 p = skipwhite(skiptowhite(p));
8648 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00008649 compflags + sp->ts_compsplit))
8650 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00008651
8652 /* Get pointer to last char of previous word. */
8653 p = preword + sp->ts_prewordlen;
8654 mb_ptr_back(preword, p);
Bram Moolenaard12a1322005-08-21 22:08:24 +00008655 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00008656 else
8657 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008659 /*
8660 * Form the word with proper case in preword.
8661 * If there is a word from a previous split, append.
8662 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008663 if (flags & WF_KEEPCAP)
8664 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008665 find_keepcap_word(slang, tword + sp->ts_splitoff,
8666 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008667 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00008668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008669 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00008670 * use that for the goodword too. But if the badword is
8671 * allcap and it's only one char long use onecap. */
8672 c = su->su_badflags;
8673 if ((c & WF_ALLCAP)
8674#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008675 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008676#else
8677 && su->su_badlen == 1
8678#endif
8679 )
8680 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00008681 c |= flags;
8682
8683 /* When appending a compound word after a word character
8684 * don't use Onecap. */
8685 if (p != NULL && spell_iswordp_nmw(p))
8686 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008687 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00008688 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008690
8691 /* Don't use a banned word. It may appear again as a good
8692 * word, thus remember it. */
8693 if (flags & WF_BANNED)
8694 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008695 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008696 break;
8697 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008698 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008699 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 break;
8701
8702 newscore = 0;
8703 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008704 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008705 newscore += SCORE_REGION;
8706 if (flags & WF_RARE)
8707 newscore += SCORE_RARE;
8708
Bram Moolenaar0c405862005-06-22 22:26:26 +00008709 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008710 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008711 newscore += SCORE_ICASE;
8712
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008713 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008714 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008715 /* The badword also ends: add suggestions. Give a penalty
8716 * when changing non-word char to word char, e.g., "thes,"
8717 * -> "these". */
8718 p = fword + sp->ts_fidx;
8719#ifdef FEAT_MBYTE
8720 if (has_mbyte)
8721 mb_ptr_back(fword, p);
8722 else
8723#endif
8724 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008725 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008726 {
8727 p = preword + STRLEN(preword);
8728#ifdef FEAT_MBYTE
8729 if (has_mbyte)
8730 mb_ptr_back(preword, p);
8731 else
8732#endif
8733 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008734 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008735 newscore += SCORE_NONWORD;
8736 }
8737
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008738 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008739 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008740 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008742 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00008743#ifdef FEAT_MBYTE
8744 /* Don't split halfway a character. */
8745 && (!has_mbyte || sp->ts_tcharlen == 0)
8746#endif
8747 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008748 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008749 int try_compound;
8750
8751 /* Get here in two situations:
8752 * 1. The word in the tree ends but the badword continues:
8753 * If the word allows compounding try that. Otherwise
8754 * try a split by inserting a space. For both check
8755 * that a valid words starts at fword[sp->ts_fidx].
8756 * 2. The badword does end, but it was due to a change
8757 * (e.g., a swap). No need to split, but do check that
8758 * the following word is valid.
8759 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008760 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008761 if (!fword_ends
Bram Moolenaar5195e452005-08-19 20:32:47 +00008762 && ((unsigned)flags >> 24) != 0
8763 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008764 >= slang->sl_compminlen
Bram Moolenaare52325c2005-08-22 22:54:29 +00008765 && (slang->sl_compsylmax < MAXWLEN
8766 || sp->ts_complen + 1 - sp->ts_compsplit
8767 < slang->sl_compmax)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008768 && (vim_strchr(sp->ts_complen == sp->ts_compsplit
8769 ? slang->sl_compstartflags
8770 : slang->sl_compallflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008771 ((unsigned)flags >> 24)) != NULL))
8772 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008773 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008774 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8775 compflags[sp->ts_complen + 1] = NUL;
8776 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008777
8778 /* If we could add a compound word, and it's also possible
8779 * to split at this point, do the split first and set
8780 * TSF_DIDSPLIT to avoid doing it again. */
8781 if (!fword_ends
8782 && try_compound
8783 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008784 {
8785 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008786 sp->ts_flags |= TSF_DIDSPLIT;
8787 --sp->ts_curi; /* do the same NUL again */
8788 compflags[sp->ts_complen] = NUL;
8789 }
8790 else
8791 sp->ts_flags &= ~TSF_DIDSPLIT;
8792
8793 if (!try_compound && !fword_ends)
8794 {
8795 /* If we're going to split need to check that the
8796 * words so far are valid for compounding. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00008797 p = preword;
8798 while (*skiptowhite(p) != NUL)
8799 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008800 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00008801 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00008802 compflags + sp->ts_compsplit))
8803 break;
8804 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008805 }
8806
8807 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008808 {
8809 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008810 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008811 sp->ts_state = STATE_SPLITUNDO;
8812
8813 ++depth;
8814 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008815
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008816 /* Append a space to preword when splitting. */
8817 if (!try_compound && !fword_ends)
8818 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00008819 sp->ts_prewordlen = STRLEN(preword);
8820 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008821
8822 /* If the badword has a non-word character at this
8823 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008824 * non-word character with a space. Always skip a
8825 * character when the word ends. */
8826 if ((!try_compound
8827 && !spell_iswordp_nmw(fword + sp->ts_fidx))
8828 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008829 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008830 int l;
8831
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008832#ifdef FEAT_MBYTE
8833 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008834 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008835 else
8836#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008837 l = 1;
8838 if (fword_ends)
8839 {
8840 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008841 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008842 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008843 sp->ts_prewordlen += l;
8844 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008845 }
8846 else
8847 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
8848 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008849 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00008850
Bram Moolenaard12a1322005-08-21 22:08:24 +00008851 /* When compounding include compound flag in
8852 * compflags[] (already set above). When splitting we
8853 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008854 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00008855 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008856 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00008857 sp->ts_compsplit = sp->ts_complen;
8858 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008859
Bram Moolenaar53805d12005-08-01 07:08:33 +00008860 /* set su->su_badflags to the caps type at this
8861 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008862#ifdef FEAT_MBYTE
8863 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00008864 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008865 else
8866#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00008867 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008868 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008869 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008871 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008872 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008873
8874 /* If there are postponed prefixes, try these too. */
8875 if (pbyts != NULL)
8876 {
8877 byts = pbyts;
8878 idxs = pidxs;
8879 sp->ts_prefixdepth = PFD_PREFIXTREE;
8880 sp->ts_state = STATE_NOPREFIX;
8881 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008882 }
8883 }
8884 break;
8885
8886 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008887 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008888 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889
8890 /* Continue looking for NUL bytes. */
8891 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008892
8893 /* In case we went into the prefix tree. */
8894 byts = fbyts;
8895 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 break;
8897
8898 case STATE_ENDNUL:
8899 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008900 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008901 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 {
8903 /* The badword ends, can't use the bytes in this node. */
8904 sp->ts_state = STATE_DEL;
8905 break;
8906 }
8907 sp->ts_state = STATE_PLAIN;
8908 /*FALLTHROUGH*/
8909
8910 case STATE_PLAIN:
8911 /*
8912 * Go over all possible bytes at this node, add each to
8913 * tword[] and use child node. "ts_curi" is the index.
8914 */
8915 arridx = sp->ts_arridx;
8916 if (sp->ts_curi > byts[arridx])
8917 {
8918 /* Done all bytes at this node, do next state. When still
8919 * at already changed bytes skip the other tricks. */
8920 if (sp->ts_fidx >= sp->ts_fidxtry)
8921 sp->ts_state = STATE_DEL;
8922 else
8923 sp->ts_state = STATE_FINAL;
8924 }
8925 else
8926 {
8927 arridx += sp->ts_curi++;
8928 c = byts[arridx];
8929
8930 /* Normal byte, go one level deeper. If it's not equal to
8931 * the byte in the bad word adjust the score. But don't
8932 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00008933 if (c == fword[sp->ts_fidx]
8934#ifdef FEAT_MBYTE
8935 || (sp->ts_tcharlen > 0
8936 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008937#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008938 )
8939 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 else
8941 newscore = SCORE_SUBST;
8942 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
8943 && try_deeper(su, stack, depth, newscore))
8944 {
8945 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008946 sp = &stack[depth];
8947 ++sp->ts_fidx;
8948 tword[sp->ts_twordlen++] = c;
8949 sp->ts_arridx = idxs[arridx];
8950#ifdef FEAT_MBYTE
8951 if (newscore == SCORE_SUBST)
8952 sp->ts_isdiff = DIFF_YES;
8953 if (has_mbyte)
8954 {
8955 /* Multi-byte characters are a bit complicated to
8956 * handle: They differ when any of the bytes
8957 * differ and then their length may also differ. */
8958 if (sp->ts_tcharlen == 0)
8959 {
8960 /* First byte. */
8961 sp->ts_tcharidx = 0;
8962 sp->ts_tcharlen = MB_BYTE2LEN(c);
8963 sp->ts_fcharstart = sp->ts_fidx - 1;
8964 sp->ts_isdiff = (newscore != 0)
8965 ? DIFF_YES : DIFF_NONE;
8966 }
8967 else if (sp->ts_isdiff == DIFF_INSERT)
8968 /* When inserting trail bytes don't advance in
8969 * the bad word. */
8970 --sp->ts_fidx;
8971 if (++sp->ts_tcharidx == sp->ts_tcharlen)
8972 {
8973 /* Last byte of character. */
8974 if (sp->ts_isdiff == DIFF_YES)
8975 {
8976 /* Correct ts_fidx for the byte length of
8977 * the character (we didn't check that
8978 * before). */
8979 sp->ts_fidx = sp->ts_fcharstart
8980 + MB_BYTE2LEN(
8981 fword[sp->ts_fcharstart]);
8982
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00008983 /* For changing a composing character
8984 * adjust the score from SCORE_SUBST to
8985 * SCORE_SUBCOMP. */
8986 if (enc_utf8
8987 && utf_iscomposing(
8988 mb_ptr2char(tword
8989 + sp->ts_twordlen
8990 - sp->ts_tcharlen))
8991 && utf_iscomposing(
8992 mb_ptr2char(fword
8993 + sp->ts_fcharstart)))
8994 sp->ts_score -=
8995 SCORE_SUBST - SCORE_SUBCOMP;
8996
Bram Moolenaarea424162005-06-16 21:51:00 +00008997 /* For a similar character adjust score
8998 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008999 else if (slang->sl_has_map
9000 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009001 mb_ptr2char(tword
9002 + sp->ts_twordlen
9003 - sp->ts_tcharlen),
9004 mb_ptr2char(fword
9005 + sp->ts_fcharstart)))
9006 sp->ts_score -=
9007 SCORE_SUBST - SCORE_SIMILAR;
9008 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009009 else if (sp->ts_isdiff == DIFF_INSERT
9010 && sp->ts_twordlen > sp->ts_tcharlen)
9011 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009012 p = tword + sp->ts_twordlen
9013 - sp->ts_tcharlen;
9014 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009015 if (enc_utf8 && utf_iscomposing(c))
9016 {
9017 /* Inserting a composing char doesn't
9018 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009019 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009020 - SCORE_INSCOMP;
9021 }
9022 else
9023 {
9024 /* If the previous character was the
9025 * same, thus doubling a character,
9026 * give a bonus to the score. */
9027 mb_ptr_back(tword, p);
9028 if (c == mb_ptr2char(p))
9029 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009030 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009031 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009032 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009033
9034 /* Starting a new char, reset the length. */
9035 sp->ts_tcharlen = 0;
9036 }
9037 }
9038 else
9039#endif
9040 {
9041 /* If we found a similar char adjust the score.
9042 * We do this after calling try_deeper() because
9043 * it's slow. */
9044 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009045 && slang->sl_has_map
9046 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009047 c, fword[sp->ts_fidx - 1]))
9048 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9049 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 }
9051 }
9052 break;
9053
9054 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009055#ifdef FEAT_MBYTE
9056 /* When past the first byte of a multi-byte char don't try
9057 * delete/insert/swap a character. */
9058 if (has_mbyte && sp->ts_tcharlen > 0)
9059 {
9060 sp->ts_state = STATE_FINAL;
9061 break;
9062 }
9063#endif
9064 /*
9065 * Try skipping one character in the bad word (delete it).
9066 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 sp->ts_state = STATE_INS;
9068 sp->ts_curi = 1;
9069 if (fword[sp->ts_fidx] != NUL
9070 && try_deeper(su, stack, depth, SCORE_DEL))
9071 {
9072 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009073
9074 /* Advance over the character in fword[]. Give a bonus to
9075 * the score if the same character is following "nn" ->
9076 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009077#ifdef FEAT_MBYTE
9078 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009079 {
9080 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009081 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009082 if (enc_utf8 && utf_iscomposing(c))
9083 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9084 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009085 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9086 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009087 else
9088#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009089 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009090 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009091 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9092 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009094 break;
9095 }
9096 /*FALLTHROUGH*/
9097
9098 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009099 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009100 * node. */
9101 n = sp->ts_arridx;
9102 if (sp->ts_curi > byts[n])
9103 {
9104 /* Done all bytes at this node, do next state. */
9105 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 }
9107 else
9108 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009109 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 n += sp->ts_curi++;
9111 c = byts[n];
9112 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9113 {
9114 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009115 sp = &stack[depth];
9116 tword[sp->ts_twordlen++] = c;
9117 sp->ts_arridx = idxs[n];
9118#ifdef FEAT_MBYTE
9119 if (has_mbyte)
9120 {
9121 fl = MB_BYTE2LEN(c);
9122 if (fl > 1)
9123 {
9124 /* There are following bytes for the same
9125 * character. We must find all bytes before
9126 * trying delete/insert/swap/etc. */
9127 sp->ts_tcharlen = fl;
9128 sp->ts_tcharidx = 1;
9129 sp->ts_isdiff = DIFF_INSERT;
9130 }
9131 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009132 else
9133 fl = 1;
9134 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009135#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009136 {
9137 /* If the previous character was the same, thus
9138 * doubling a character, give a bonus to the
9139 * score. */
9140 if (sp->ts_twordlen >= 2
9141 && tword[sp->ts_twordlen - 2] == c)
9142 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 }
9145 }
9146 break;
9147
9148 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009149 /*
9150 * Swap two bytes in the bad word: "12" -> "21".
9151 * We change "fword" here, it's changed back afterwards.
9152 */
9153 p = fword + sp->ts_fidx;
9154 c = *p;
9155 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009156 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009157 /* End of word, can't swap or replace. */
9158 sp->ts_state = STATE_FINAL;
9159 break;
9160 }
9161#ifdef FEAT_MBYTE
9162 if (has_mbyte)
9163 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009164 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009165 c = mb_ptr2char(p);
9166 c2 = mb_ptr2char(p + n);
9167 }
9168 else
9169#endif
9170 c2 = p[1];
9171 if (c == c2)
9172 {
9173 /* Characters are identical, swap won't do anything. */
9174 sp->ts_state = STATE_SWAP3;
9175 break;
9176 }
9177 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9178 {
9179 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009181#ifdef FEAT_MBYTE
9182 if (has_mbyte)
9183 {
9184 fl = mb_char2len(c2);
9185 mch_memmove(p, p + n, fl);
9186 mb_char2bytes(c, p + fl);
9187 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9188 }
9189 else
9190#endif
9191 {
9192 p[0] = c2;
9193 p[1] = c;
9194 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 }
9197 else
9198 /* If this swap doesn't work then SWAP3 won't either. */
9199 sp->ts_state = STATE_REP_INI;
9200 break;
9201
Bram Moolenaarea424162005-06-16 21:51:00 +00009202 case STATE_UNSWAP:
9203 /* Undo the STATE_SWAP swap: "21" -> "12". */
9204 p = fword + sp->ts_fidx;
9205#ifdef FEAT_MBYTE
9206 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009208 n = MB_BYTE2LEN(*p);
9209 c = mb_ptr2char(p + n);
9210 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9211 mb_char2bytes(c, p);
9212 }
9213 else
9214#endif
9215 {
9216 c = *p;
9217 *p = p[1];
9218 p[1] = c;
9219 }
9220 /*FALLTHROUGH*/
9221
9222 case STATE_SWAP3:
9223 /* Swap two bytes, skipping one: "123" -> "321". We change
9224 * "fword" here, it's changed back afterwards. */
9225 p = fword + sp->ts_fidx;
9226#ifdef FEAT_MBYTE
9227 if (has_mbyte)
9228 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009229 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009230 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009231 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009232 c2 = mb_ptr2char(p + n);
9233 c3 = mb_ptr2char(p + n + fl);
9234 }
9235 else
9236#endif
9237 {
9238 c = *p;
9239 c2 = p[1];
9240 c3 = p[2];
9241 }
9242
9243 /* When characters are identical: "121" then SWAP3 result is
9244 * identical, ROT3L result is same as SWAP: "211", ROT3L
9245 * result is same as SWAP on next char: "112". Thus skip all
9246 * swapping. Also skip when c3 is NUL. */
9247 if (c == c3 || c3 == NUL)
9248 {
9249 sp->ts_state = STATE_REP_INI;
9250 break;
9251 }
9252 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9253 {
9254 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009256#ifdef FEAT_MBYTE
9257 if (has_mbyte)
9258 {
9259 tl = mb_char2len(c3);
9260 mch_memmove(p, p + n + fl, tl);
9261 mb_char2bytes(c2, p + tl);
9262 mb_char2bytes(c, p + fl + tl);
9263 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9264 }
9265 else
9266#endif
9267 {
9268 p[0] = p[2];
9269 p[2] = c;
9270 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 }
9273 else
9274 sp->ts_state = STATE_REP_INI;
9275 break;
9276
Bram Moolenaarea424162005-06-16 21:51:00 +00009277 case STATE_UNSWAP3:
9278 /* Undo STATE_SWAP3: "321" -> "123" */
9279 p = fword + sp->ts_fidx;
9280#ifdef FEAT_MBYTE
9281 if (has_mbyte)
9282 {
9283 n = MB_BYTE2LEN(*p);
9284 c2 = mb_ptr2char(p + n);
9285 fl = MB_BYTE2LEN(p[n]);
9286 c = mb_ptr2char(p + n + fl);
9287 tl = MB_BYTE2LEN(p[n + fl]);
9288 mch_memmove(p + fl + tl, p, n);
9289 mb_char2bytes(c, p);
9290 mb_char2bytes(c2, p + tl);
9291 }
9292 else
9293#endif
9294 {
9295 c = *p;
9296 *p = p[2];
9297 p[2] = c;
9298 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009299
Bram Moolenaarea424162005-06-16 21:51:00 +00009300 /* Rotate three characters left: "123" -> "231". We change
9301 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9303 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009304 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009305 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009306 p = fword + sp->ts_fidx;
9307#ifdef FEAT_MBYTE
9308 if (has_mbyte)
9309 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009310 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009311 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009312 fl = mb_cptr2len(p + n);
9313 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009314 mch_memmove(p, p + n, fl);
9315 mb_char2bytes(c, p + fl);
9316 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9317 }
9318 else
9319#endif
9320 {
9321 c = *p;
9322 *p = p[1];
9323 p[1] = p[2];
9324 p[2] = c;
9325 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 }
9328 else
9329 sp->ts_state = STATE_REP_INI;
9330 break;
9331
Bram Moolenaarea424162005-06-16 21:51:00 +00009332 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009333 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009334 p = fword + sp->ts_fidx;
9335#ifdef FEAT_MBYTE
9336 if (has_mbyte)
9337 {
9338 n = MB_BYTE2LEN(*p);
9339 n += MB_BYTE2LEN(p[n]);
9340 c = mb_ptr2char(p + n);
9341 tl = MB_BYTE2LEN(p[n]);
9342 mch_memmove(p + tl, p, n);
9343 mb_char2bytes(c, p);
9344 }
9345 else
9346#endif
9347 {
9348 c = p[2];
9349 p[2] = p[1];
9350 p[1] = *p;
9351 *p = c;
9352 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009354 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00009355 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9357 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009358 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009360 p = fword + sp->ts_fidx;
9361#ifdef FEAT_MBYTE
9362 if (has_mbyte)
9363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009364 n = mb_cptr2len(p);
9365 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009366 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009367 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009368 mch_memmove(p + tl, p, n);
9369 mb_char2bytes(c, p);
9370 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
9371 }
9372 else
9373#endif
9374 {
9375 c = p[2];
9376 p[2] = p[1];
9377 p[1] = *p;
9378 *p = c;
9379 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9380 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 }
9382 else
9383 sp->ts_state = STATE_REP_INI;
9384 break;
9385
Bram Moolenaarea424162005-06-16 21:51:00 +00009386 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009387 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009388 p = fword + sp->ts_fidx;
9389#ifdef FEAT_MBYTE
9390 if (has_mbyte)
9391 {
9392 c = mb_ptr2char(p);
9393 tl = MB_BYTE2LEN(*p);
9394 n = MB_BYTE2LEN(p[tl]);
9395 n += MB_BYTE2LEN(p[tl + n]);
9396 mch_memmove(p, p + tl, n);
9397 mb_char2bytes(c, p + n);
9398 }
9399 else
9400#endif
9401 {
9402 c = *p;
9403 *p = p[1];
9404 p[1] = p[2];
9405 p[2] = c;
9406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009407 /*FALLTHROUGH*/
9408
9409 case STATE_REP_INI:
9410 /* Check if matching with REP items from the .aff file would
9411 * work. Quickly skip if there are no REP items or the score
9412 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009413 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 if (gap->ga_len == 0
9415 || sp->ts_score + SCORE_REP >= su->su_maxscore)
9416 {
9417 sp->ts_state = STATE_FINAL;
9418 break;
9419 }
9420
9421 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00009422 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009423 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009424 if (sp->ts_curi < 0)
9425 {
9426 sp->ts_state = STATE_FINAL;
9427 break;
9428 }
9429
9430 sp->ts_state = STATE_REP;
9431 /*FALLTHROUGH*/
9432
9433 case STATE_REP:
9434 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00009435 * match replace the characters and check if the resulting
9436 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 p = fword + sp->ts_fidx;
9438
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009439 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 while (sp->ts_curi < gap->ga_len)
9441 {
9442 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
9443 if (*ftp->ft_from != *p)
9444 {
9445 /* past possible matching entries */
9446 sp->ts_curi = gap->ga_len;
9447 break;
9448 }
9449 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
9450 && try_deeper(su, stack, depth, SCORE_REP))
9451 {
9452 /* Need to undo this afterwards. */
9453 sp->ts_state = STATE_REP_UNDO;
9454
9455 /* Change the "from" to the "to" string. */
9456 ++depth;
9457 fl = STRLEN(ftp->ft_from);
9458 tl = STRLEN(ftp->ft_to);
9459 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009462 repextra += tl - fl;
9463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009464 mch_memmove(p, ftp->ft_to, tl);
9465 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00009466#ifdef FEAT_MBYTE
9467 stack[depth].ts_tcharlen = 0;
9468#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009469 break;
9470 }
9471 }
9472
9473 if (sp->ts_curi >= gap->ga_len)
9474 /* No (more) matches. */
9475 sp->ts_state = STATE_FINAL;
9476
9477 break;
9478
9479 case STATE_REP_UNDO:
9480 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009481 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 fl = STRLEN(ftp->ft_from);
9483 tl = STRLEN(ftp->ft_to);
9484 p = fword + sp->ts_fidx;
9485 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009487 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009488 repextra -= tl - fl;
9489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009490 mch_memmove(p, ftp->ft_from, fl);
9491 sp->ts_state = STATE_REP;
9492 break;
9493
9494 default:
9495 /* Did all possible states at this level, go up one level. */
9496 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009497
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009498 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009499 {
9500 /* Continue in or go back to the prefix tree. */
9501 byts = pbyts;
9502 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009503 }
9504
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009505 /* Don't check for CTRL-C too often, it takes time. */
9506 line_breakcheck();
9507 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009508 }
9509 }
9510}
9511
9512/*
9513 * Try going one level deeper in the tree.
9514 */
9515 static int
9516try_deeper(su, stack, depth, score_add)
9517 suginfo_T *su;
9518 trystate_T *stack;
9519 int depth;
9520 int score_add;
9521{
9522 int newscore;
9523
9524 /* Refuse to go deeper if the scrore is getting too big. */
9525 newscore = stack[depth].ts_score + score_add;
9526 if (newscore >= su->su_maxscore)
9527 return FALSE;
9528
Bram Moolenaarea424162005-06-16 21:51:00 +00009529 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 stack[depth + 1].ts_state = STATE_START;
9531 stack[depth + 1].ts_score = newscore;
9532 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009533 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 return TRUE;
9535}
9536
Bram Moolenaar53805d12005-08-01 07:08:33 +00009537#ifdef FEAT_MBYTE
9538/*
9539 * Case-folding may change the number of bytes: Count nr of chars in
9540 * fword[flen] and return the byte length of that many chars in "word".
9541 */
9542 static int
9543nofold_len(fword, flen, word)
9544 char_u *fword;
9545 int flen;
9546 char_u *word;
9547{
9548 char_u *p;
9549 int i = 0;
9550
9551 for (p = fword; p < fword + flen; mb_ptr_adv(p))
9552 ++i;
9553 for (p = word; i > 0; mb_ptr_adv(p))
9554 --i;
9555 return (int)(p - word);
9556}
9557#endif
9558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559/*
9560 * "fword" is a good word with case folded. Find the matching keep-case
9561 * words and put it in "kword".
9562 * Theoretically there could be several keep-case words that result in the
9563 * same case-folded word, but we only find one...
9564 */
9565 static void
9566find_keepcap_word(slang, fword, kword)
9567 slang_T *slang;
9568 char_u *fword;
9569 char_u *kword;
9570{
9571 char_u uword[MAXWLEN]; /* "fword" in upper-case */
9572 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009573 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574
9575 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009576 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009577 int round[MAXWLEN];
9578 int fwordidx[MAXWLEN];
9579 int uwordidx[MAXWLEN];
9580 int kwordlen[MAXWLEN];
9581
9582 int flen, ulen;
9583 int l;
9584 int len;
9585 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009586 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 char_u *p;
9588 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009589 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590
9591 if (byts == NULL)
9592 {
9593 /* array is empty: "cannot happen" */
9594 *kword = NUL;
9595 return;
9596 }
9597
9598 /* Make an all-cap version of "fword". */
9599 allcap_copy(fword, uword);
9600
9601 /*
9602 * Each character needs to be tried both case-folded and upper-case.
9603 * All this gets very complicated if we keep in mind that changing case
9604 * may change the byte length of a multi-byte character...
9605 */
9606 depth = 0;
9607 arridx[0] = 0;
9608 round[0] = 0;
9609 fwordidx[0] = 0;
9610 uwordidx[0] = 0;
9611 kwordlen[0] = 0;
9612 while (depth >= 0)
9613 {
9614 if (fword[fwordidx[depth]] == NUL)
9615 {
9616 /* We are at the end of "fword". If the tree allows a word to end
9617 * here we have found a match. */
9618 if (byts[arridx[depth] + 1] == 0)
9619 {
9620 kword[kwordlen[depth]] = NUL;
9621 return;
9622 }
9623
9624 /* kword is getting too long, continue one level up */
9625 --depth;
9626 }
9627 else if (++round[depth] > 2)
9628 {
9629 /* tried both fold-case and upper-case character, continue one
9630 * level up */
9631 --depth;
9632 }
9633 else
9634 {
9635 /*
9636 * round[depth] == 1: Try using the folded-case character.
9637 * round[depth] == 2: Try using the upper-case character.
9638 */
9639#ifdef FEAT_MBYTE
9640 if (has_mbyte)
9641 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009642 flen = mb_cptr2len(fword + fwordidx[depth]);
9643 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 }
9645 else
9646#endif
9647 ulen = flen = 1;
9648 if (round[depth] == 1)
9649 {
9650 p = fword + fwordidx[depth];
9651 l = flen;
9652 }
9653 else
9654 {
9655 p = uword + uwordidx[depth];
9656 l = ulen;
9657 }
9658
9659 for (tryidx = arridx[depth]; l > 0; --l)
9660 {
9661 /* Perform a binary search in the list of accepted bytes. */
9662 len = byts[tryidx++];
9663 c = *p++;
9664 lo = tryidx;
9665 hi = tryidx + len - 1;
9666 while (lo < hi)
9667 {
9668 m = (lo + hi) / 2;
9669 if (byts[m] > c)
9670 hi = m - 1;
9671 else if (byts[m] < c)
9672 lo = m + 1;
9673 else
9674 {
9675 lo = hi = m;
9676 break;
9677 }
9678 }
9679
9680 /* Stop if there is no matching byte. */
9681 if (hi < lo || byts[lo] != c)
9682 break;
9683
9684 /* Continue at the child (if there is one). */
9685 tryidx = idxs[lo];
9686 }
9687
9688 if (l == 0)
9689 {
9690 /*
9691 * Found the matching char. Copy it to "kword" and go a
9692 * level deeper.
9693 */
9694 if (round[depth] == 1)
9695 {
9696 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
9697 flen);
9698 kwordlen[depth + 1] = kwordlen[depth] + flen;
9699 }
9700 else
9701 {
9702 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
9703 ulen);
9704 kwordlen[depth + 1] = kwordlen[depth] + ulen;
9705 }
9706 fwordidx[depth + 1] = fwordidx[depth] + flen;
9707 uwordidx[depth + 1] = uwordidx[depth] + ulen;
9708
9709 ++depth;
9710 arridx[depth] = tryidx;
9711 round[depth] = 0;
9712 }
9713 }
9714 }
9715
9716 /* Didn't find it: "cannot happen". */
9717 *kword = NUL;
9718}
9719
9720/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009721 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
9722 * su->su_sga.
9723 */
9724 static void
9725score_comp_sal(su)
9726 suginfo_T *su;
9727{
9728 langp_T *lp;
9729 char_u badsound[MAXWLEN];
9730 int i;
9731 suggest_T *stp;
9732 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009733 int score;
9734
9735 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
9736 return;
9737
9738 /* Use the sound-folding of the first language that supports it. */
9739 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9740 lp->lp_slang != NULL; ++lp)
9741 if (lp->lp_slang->sl_sal.ga_len > 0)
9742 {
9743 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009744 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009745
9746 for (i = 0; i < su->su_ga.ga_len; ++i)
9747 {
9748 stp = &SUG(su->su_ga, i);
9749
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009750 /* Case-fold the suggested word, sound-fold it and compute the
9751 * sound-a-like score. */
9752 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009753 if (score < SCORE_MAXMAX)
9754 {
9755 /* Add the suggestion. */
9756 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
9757 sstp->st_word = vim_strsave(stp->st_word);
9758 if (sstp->st_word != NULL)
9759 {
9760 sstp->st_score = score;
9761 sstp->st_altscore = 0;
9762 sstp->st_orglen = stp->st_orglen;
9763 ++su->su_sga.ga_len;
9764 }
9765 }
9766 }
9767 break;
9768 }
9769}
9770
9771/*
9772 * Combine the list of suggestions in su->su_ga and su->su_sga.
9773 * They are intwined.
9774 */
9775 static void
9776score_combine(su)
9777 suginfo_T *su;
9778{
9779 int i;
9780 int j;
9781 garray_T ga;
9782 garray_T *gap;
9783 langp_T *lp;
9784 suggest_T *stp;
9785 char_u *p;
9786 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009787 int round;
9788
9789 /* Add the alternate score to su_ga. */
9790 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9791 lp->lp_slang != NULL; ++lp)
9792 {
9793 if (lp->lp_slang->sl_sal.ga_len > 0)
9794 {
9795 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009796 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009797
9798 for (i = 0; i < su->su_ga.ga_len; ++i)
9799 {
9800 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009801 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
9802 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009803 if (stp->st_altscore == SCORE_MAXMAX)
9804 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
9805 else
9806 stp->st_score = (stp->st_score * 3
9807 + stp->st_altscore) / 4;
9808 stp->st_salscore = FALSE;
9809 }
9810 break;
9811 }
9812 }
9813
9814 /* Add the alternate score to su_sga. */
9815 for (i = 0; i < su->su_sga.ga_len; ++i)
9816 {
9817 stp = &SUG(su->su_sga, i);
9818 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
9819 if (stp->st_score == SCORE_MAXMAX)
9820 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
9821 else
9822 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
9823 stp->st_salscore = TRUE;
9824 }
9825
9826 /* Sort the suggestions and truncate at "maxcount" for both lists. */
9827 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9828 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
9829
9830 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
9831 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
9832 return;
9833
9834 stp = &SUG(ga, 0);
9835 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
9836 {
9837 /* round 1: get a suggestion from su_ga
9838 * round 2: get a suggestion from su_sga */
9839 for (round = 1; round <= 2; ++round)
9840 {
9841 gap = round == 1 ? &su->su_ga : &su->su_sga;
9842 if (i < gap->ga_len)
9843 {
9844 /* Don't add a word if it's already there. */
9845 p = SUG(*gap, i).st_word;
9846 for (j = 0; j < ga.ga_len; ++j)
9847 if (STRCMP(stp[j].st_word, p) == 0)
9848 break;
9849 if (j == ga.ga_len)
9850 stp[ga.ga_len++] = SUG(*gap, i);
9851 else
9852 vim_free(p);
9853 }
9854 }
9855 }
9856
9857 ga_clear(&su->su_ga);
9858 ga_clear(&su->su_sga);
9859
9860 /* Truncate the list to the number of suggestions that will be displayed. */
9861 if (ga.ga_len > su->su_maxcount)
9862 {
9863 for (i = su->su_maxcount; i < ga.ga_len; ++i)
9864 vim_free(stp[i].st_word);
9865 ga.ga_len = su->su_maxcount;
9866 }
9867
9868 su->su_ga = ga;
9869}
9870
9871/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009872 * For the goodword in "stp" compute the soundalike score compared to the
9873 * badword.
9874 */
9875 static int
9876stp_sal_score(stp, su, slang, badsound)
9877 suggest_T *stp;
9878 suginfo_T *su;
9879 slang_T *slang;
9880 char_u *badsound; /* sound-folded badword */
9881{
9882 char_u *p;
9883 char_u badsound2[MAXWLEN];
9884 char_u fword[MAXWLEN];
9885 char_u goodsound[MAXWLEN];
9886
9887 if (stp->st_orglen <= su->su_badlen)
9888 p = badsound;
9889 else
9890 {
9891 /* soundfold the bad word with more characters following */
9892 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
9893
9894 /* When joining two words the sound often changes a lot. E.g., "t he"
9895 * sounds like "t h" while "the" sounds like "@". Avoid that by
9896 * removing the space. Don't do it when the good word also contains a
9897 * space. */
9898 if (vim_iswhite(su->su_badptr[su->su_badlen])
9899 && *skiptowhite(stp->st_word) == NUL)
9900 for (p = fword; *(p = skiptowhite(p)) != NUL; )
9901 mch_memmove(p, p + 1, STRLEN(p));
9902
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009903 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009904 p = badsound2;
9905 }
9906
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009907 /* Sound-fold the word and compute the score for the difference. */
9908 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009909
9910 return soundalike_score(goodsound, p);
9911}
9912
9913/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 * Find suggestions by comparing the word in a sound-a-like form.
9915 */
9916 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009917suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 suginfo_T *su;
9919{
9920 char_u salword[MAXWLEN];
9921 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009923 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 int curi[MAXWLEN];
9925 langp_T *lp;
9926 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009927 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009928 int depth;
9929 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009930 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009931 int round;
9932 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009933 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009934 int local_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009936 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009937 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9938 lp->lp_slang != NULL; ++lp)
9939 {
9940 if (lp->lp_slang->sl_sal.ga_len > 0)
9941 {
9942 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009943 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944
9945 /*
9946 * Go through the whole tree, soundfold each word and compare.
9947 * round 1: use the case-folded tree.
9948 * round 2: use the keep-case tree.
9949 */
9950 for (round = 1; round <= 2; ++round)
9951 {
9952 if (round == 1)
9953 {
9954 byts = lp->lp_slang->sl_fbyts;
9955 idxs = lp->lp_slang->sl_fidxs;
9956 }
9957 else
9958 {
9959 byts = lp->lp_slang->sl_kbyts;
9960 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009961 if (byts == NULL) /* no keep-case words */
9962 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009963 }
9964
9965 depth = 0;
9966 arridx[0] = 0;
9967 curi[0] = 1;
9968 while (depth >= 0 && !got_int)
9969 {
9970 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009972 /* Done all bytes at this node, go up one level. */
9973 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009974 line_breakcheck();
9975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 else
9977 {
9978 /* Do one more byte at this node. */
9979 n = arridx[depth] + curi[depth];
9980 ++curi[depth];
9981 c = byts[n];
9982 if (c == 0)
9983 {
9984 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009985 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009986 if (round == 2 || (flags & WF_KEEPCAP) == 0)
9987 {
9988 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009989 /* Sound-fold. Only in keep-case tree need to
9990 * case-fold the word. */
9991 spell_soundfold(lp->lp_slang, tword,
9992 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009993
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009994 /* Compute the edit distance between the
9995 * sound-a-like words. */
9996 sound_score = soundalike_score(salword,
9997 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009998
9999 /* Add a penalty for words in another region. */
10000 if ((flags & WF_REGION) && (((unsigned)flags
10001 >> 16) & lp->lp_region) == 0)
10002 local_score = SCORE_REGION;
10003 else
10004 local_score = 0;
10005 sound_score += local_score;
10006
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010007 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010009 char_u cword[MAXWLEN];
10010 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010011 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010012
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010013 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010014 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010016 /* Need to fix case according to
10017 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010019 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 }
10021 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010022 p = tword;
10023
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010024 if (sps_flags & SPS_DOUBLE)
10025 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010026 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010027 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010028 else
10029 {
10030 /* Compute the score. */
10031 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010032 su->su_badword, p)
10033 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010034 if (sps_flags & SPS_BEST)
10035 /* give a bonus for the good word
10036 * sounding the same as the bad
10037 * word */
10038 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010039 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010040 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010041 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010042 else
10043 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010044 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010045 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010047 }
10048 }
10049
10050 /* Skip over other NUL bytes. */
10051 while (byts[n + 1] == 0)
10052 {
10053 ++n;
10054 ++curi[depth];
10055 }
10056 }
10057 else
10058 {
10059 /* Normal char, go one level deeper. */
10060 tword[depth++] = c;
10061 arridx[depth] = idxs[n];
10062 curi[depth] = 1;
10063 }
10064 }
10065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 }
10067 }
10068 }
10069}
10070
10071/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010072 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010073 */
10074 static void
10075make_case_word(fword, cword, flags)
10076 char_u *fword;
10077 char_u *cword;
10078 int flags;
10079{
10080 if (flags & WF_ALLCAP)
10081 /* Make it all upper-case */
10082 allcap_copy(fword, cword);
10083 else if (flags & WF_ONECAP)
10084 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010085 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 else
10087 /* Use goodword as-is. */
10088 STRCPY(cword, fword);
10089}
10090
Bram Moolenaarea424162005-06-16 21:51:00 +000010091/*
10092 * Use map string "map" for languages "lp".
10093 */
10094 static void
10095set_map_str(lp, map)
10096 slang_T *lp;
10097 char_u *map;
10098{
10099 char_u *p;
10100 int headc = 0;
10101 int c;
10102 int i;
10103
10104 if (*map == NUL)
10105 {
10106 lp->sl_has_map = FALSE;
10107 return;
10108 }
10109 lp->sl_has_map = TRUE;
10110
10111 /* Init the array and hash table empty. */
10112 for (i = 0; i < 256; ++i)
10113 lp->sl_map_array[i] = 0;
10114#ifdef FEAT_MBYTE
10115 hash_init(&lp->sl_map_hash);
10116#endif
10117
10118 /*
10119 * The similar characters are stored separated with slashes:
10120 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10121 * before the same slash. For characters above 255 sl_map_hash is used.
10122 */
10123 for (p = map; *p != NUL; )
10124 {
10125#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010126 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010127#else
10128 c = *p++;
10129#endif
10130 if (c == '/')
10131 headc = 0;
10132 else
10133 {
10134 if (headc == 0)
10135 headc = c;
10136
10137#ifdef FEAT_MBYTE
10138 /* Characters above 255 don't fit in sl_map_array[], put them in
10139 * the hash table. Each entry is the char, a NUL the headchar and
10140 * a NUL. */
10141 if (c >= 256)
10142 {
10143 int cl = mb_char2len(c);
10144 int headcl = mb_char2len(headc);
10145 char_u *b;
10146 hash_T hash;
10147 hashitem_T *hi;
10148
10149 b = alloc((unsigned)(cl + headcl + 2));
10150 if (b == NULL)
10151 return;
10152 mb_char2bytes(c, b);
10153 b[cl] = NUL;
10154 mb_char2bytes(headc, b + cl + 1);
10155 b[cl + 1 + headcl] = NUL;
10156 hash = hash_hash(b);
10157 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10158 if (HASHITEM_EMPTY(hi))
10159 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10160 else
10161 {
10162 /* This should have been checked when generating the .spl
10163 * file. */
10164 EMSG(_("E999: duplicate char in MAP entry"));
10165 vim_free(b);
10166 }
10167 }
10168 else
10169#endif
10170 lp->sl_map_array[c] = headc;
10171 }
10172 }
10173}
10174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175/*
10176 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10177 * lines in the .aff file.
10178 */
10179 static int
10180similar_chars(slang, c1, c2)
10181 slang_T *slang;
10182 int c1;
10183 int c2;
10184{
Bram Moolenaarea424162005-06-16 21:51:00 +000010185 int m1, m2;
10186#ifdef FEAT_MBYTE
10187 char_u buf[MB_MAXBYTES];
10188 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189
Bram Moolenaarea424162005-06-16 21:51:00 +000010190 if (c1 >= 256)
10191 {
10192 buf[mb_char2bytes(c1, buf)] = 0;
10193 hi = hash_find(&slang->sl_map_hash, buf);
10194 if (HASHITEM_EMPTY(hi))
10195 m1 = 0;
10196 else
10197 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10198 }
10199 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010200#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010201 m1 = slang->sl_map_array[c1];
10202 if (m1 == 0)
10203 return FALSE;
10204
10205
10206#ifdef FEAT_MBYTE
10207 if (c2 >= 256)
10208 {
10209 buf[mb_char2bytes(c2, buf)] = 0;
10210 hi = hash_find(&slang->sl_map_hash, buf);
10211 if (HASHITEM_EMPTY(hi))
10212 m2 = 0;
10213 else
10214 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10215 }
10216 else
10217#endif
10218 m2 = slang->sl_map_array[c2];
10219
10220 return m1 == m2;
10221}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222
10223/*
10224 * Add a suggestion to the list of suggestions.
10225 * Do not add a duplicate suggestion or suggestions with a bad score.
10226 * When "use_score" is not zero it's used, otherwise the score is computed
10227 * with spell_edit_score().
10228 */
10229 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010230add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010232 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010233 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010234 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010235 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010236 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010237 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238{
10239 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010241 char_u *p = NULL;
10242 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010243
10244 /* Check that the word wasn't banned. */
10245 if (was_banned(su, goodword))
10246 return;
10247
Bram Moolenaar0c405862005-06-22 22:26:26 +000010248 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10249 * Remove the common part from "goodword". */
10250 i = badlen - su->su_badlen;
10251 if (i > 0)
10252 {
10253 /* This assumes there was no case folding or it didn't change the
10254 * length... */
10255 p = goodword + STRLEN(goodword) - i;
10256 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10257 {
10258 badlen = su->su_badlen;
10259 c = *p;
10260 *p = NUL;
10261 }
10262 else
10263 p = NULL;
10264 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010265 else if (i < 0)
10266 {
10267 /* When replacing part of the word check that we actually change
10268 * something. For "the the" a suggestion can be replacing the first
10269 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010270 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010271 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10272 return;
10273 }
10274
Bram Moolenaar0c405862005-06-22 22:26:26 +000010275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 if (score <= su->su_maxscore)
10277 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010278 /* Check if the word is already there. Also check the length that is
10279 * being replaced "thes," -> "these" is a different suggestion from
10280 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010281 stp = &SUG(*gap, 0);
10282 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010283 if (STRCMP(stp[i].st_word, goodword) == 0
10284 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010285 {
10286 /* Found it. Remember the lowest score. */
10287 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010290 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010291 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010292 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 break;
10294 }
10295
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010296 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 {
10298 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010299 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 stp->st_word = vim_strsave(goodword);
10301 if (stp->st_word != NULL)
10302 {
10303 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010304 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010305 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010306 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010307 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308
10309 /* If we have too many suggestions now, sort the list and keep
10310 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010311 if (gap->ga_len > SUG_MAX_COUNT(su))
10312 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10313 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 }
10315 }
10316 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000010317
10318 if (p != NULL)
10319 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010320}
10321
10322/*
10323 * Add a word to be banned.
10324 */
10325 static void
10326add_banned(su, word)
10327 suginfo_T *su;
10328 char_u *word;
10329{
10330 char_u *s = vim_strsave(word);
10331 hash_T hash;
10332 hashitem_T *hi;
10333
10334 if (s != NULL)
10335 {
10336 hash = hash_hash(s);
10337 hi = hash_lookup(&su->su_banned, s, hash);
10338 if (HASHITEM_EMPTY(hi))
10339 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010340 else
10341 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 }
10343}
10344
10345/*
10346 * Return TRUE if a word appears in the list of banned words.
10347 */
10348 static int
10349was_banned(su, word)
10350 suginfo_T *su;
10351 char_u *word;
10352{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010353 hashitem_T *hi = hash_find(&su->su_banned, word);
10354
10355 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010356}
10357
10358/*
10359 * Free the banned words in "su".
10360 */
10361 static void
10362free_banned(su)
10363 suginfo_T *su;
10364{
10365 int todo;
10366 hashitem_T *hi;
10367
10368 todo = su->su_banned.ht_used;
10369 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
10370 {
10371 if (!HASHITEM_EMPTY(hi))
10372 {
10373 vim_free(hi->hi_key);
10374 --todo;
10375 }
10376 }
10377 hash_clear(&su->su_banned);
10378}
10379
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010380/*
10381 * Recompute the score if sound-folding is possible. This is slow,
10382 * thus only done for the final results.
10383 */
10384 static void
10385rescore_suggestions(su)
10386 suginfo_T *su;
10387{
10388 langp_T *lp;
10389 suggest_T *stp;
10390 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010391 int i;
10392
10393 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10394 lp->lp_slang != NULL; ++lp)
10395 {
10396 if (lp->lp_slang->sl_sal.ga_len > 0)
10397 {
10398 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010399 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010400
10401 for (i = 0; i < su->su_ga.ga_len; ++i)
10402 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010403 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010404 if (!stp->st_had_bonus)
10405 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010406 stp->st_altscore = stp_sal_score(stp, su,
10407 lp->lp_slang, sal_badword);
10408 if (stp->st_altscore == SCORE_MAXMAX)
10409 stp->st_altscore = SCORE_BIG;
10410 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010411 }
10412 }
10413 break;
10414 }
10415 }
10416}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418static int
10419#ifdef __BORLANDC__
10420_RTLENTRYF
10421#endif
10422sug_compare __ARGS((const void *s1, const void *s2));
10423
10424/*
10425 * Function given to qsort() to sort the suggestions on st_score.
10426 */
10427 static int
10428#ifdef __BORLANDC__
10429_RTLENTRYF
10430#endif
10431sug_compare(s1, s2)
10432 const void *s1;
10433 const void *s2;
10434{
10435 suggest_T *p1 = (suggest_T *)s1;
10436 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010437 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010439 if (n == 0)
10440 return p1->st_altscore - p2->st_altscore;
10441 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442}
10443
10444/*
10445 * Cleanup the suggestions:
10446 * - Sort on score.
10447 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010448 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010450 static int
10451cleanup_suggestions(gap, maxscore, keep)
10452 garray_T *gap;
10453 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010454 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010456 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 int i;
10458
10459 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010460 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461
10462 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010463 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010465 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010467 gap->ga_len = keep;
10468 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010470 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471}
10472
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010473#if defined(FEAT_EVAL) || defined(PROTO)
10474/*
10475 * Soundfold a string, for soundfold().
10476 * Result is in allocated memory, NULL for an error.
10477 */
10478 char_u *
10479eval_soundfold(word)
10480 char_u *word;
10481{
10482 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010483 char_u sound[MAXWLEN];
10484
10485 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
10486 /* Use the sound-folding of the first language that supports it. */
10487 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10488 lp->lp_slang != NULL; ++lp)
10489 if (lp->lp_slang->sl_sal.ga_len > 0)
10490 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010491 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010492 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010493 return vim_strsave(sound);
10494 }
10495
10496 /* No language with sound folding, return word as-is. */
10497 return vim_strsave(word);
10498}
10499#endif
10500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010501/*
10502 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000010503 *
10504 * There are many ways to turn a word into a sound-a-like representation. The
10505 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
10506 * swedish name matching - survey and test of different algorithms" by Klas
10507 * Erikson.
10508 *
10509 * We support two methods:
10510 * 1. SOFOFROM/SOFOTO do a simple character mapping.
10511 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 */
10513 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010514spell_soundfold(slang, inword, folded, res)
10515 slang_T *slang;
10516 char_u *inword;
10517 int folded; /* "inword" is already case-folded */
10518 char_u *res;
10519{
10520 char_u fword[MAXWLEN];
10521 char_u *word;
10522
10523 if (slang->sl_sofo)
10524 /* SOFOFROM and SOFOTO used */
10525 spell_soundfold_sofo(slang, inword, res);
10526 else
10527 {
10528 /* SAL items used. Requires the word to be case-folded. */
10529 if (folded)
10530 word = inword;
10531 else
10532 {
10533 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
10534 word = fword;
10535 }
10536
10537#ifdef FEAT_MBYTE
10538 if (has_mbyte)
10539 spell_soundfold_wsal(slang, word, res);
10540 else
10541#endif
10542 spell_soundfold_sal(slang, word, res);
10543 }
10544}
10545
10546/*
10547 * Perform sound folding of "inword" into "res" according to SOFOFROM and
10548 * SOFOTO lines.
10549 */
10550 static void
10551spell_soundfold_sofo(slang, inword, res)
10552 slang_T *slang;
10553 char_u *inword;
10554 char_u *res;
10555{
10556 char_u *s;
10557 int ri = 0;
10558 int c;
10559
10560#ifdef FEAT_MBYTE
10561 if (has_mbyte)
10562 {
10563 int prevc = 0;
10564 int *ip;
10565
10566 /* The sl_sal_first[] table contains the translation for chars up to
10567 * 255, sl_sal the rest. */
10568 for (s = inword; *s != NUL; )
10569 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010570 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010571 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10572 c = ' ';
10573 else if (c < 256)
10574 c = slang->sl_sal_first[c];
10575 else
10576 {
10577 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
10578 if (ip == NULL) /* empty list, can't match */
10579 c = NUL;
10580 else
10581 for (;;) /* find "c" in the list */
10582 {
10583 if (*ip == 0) /* not found */
10584 {
10585 c = NUL;
10586 break;
10587 }
10588 if (*ip == c) /* match! */
10589 {
10590 c = ip[1];
10591 break;
10592 }
10593 ip += 2;
10594 }
10595 }
10596
10597 if (c != NUL && c != prevc)
10598 {
10599 ri += mb_char2bytes(c, res + ri);
10600 if (ri + MB_MAXBYTES > MAXWLEN)
10601 break;
10602 prevc = c;
10603 }
10604 }
10605 }
10606 else
10607#endif
10608 {
10609 /* The sl_sal_first[] table contains the translation. */
10610 for (s = inword; (c = *s) != NUL; ++s)
10611 {
10612 if (vim_iswhite(c))
10613 c = ' ';
10614 else
10615 c = slang->sl_sal_first[c];
10616 if (c != NUL && (ri == 0 || res[ri - 1] != c))
10617 res[ri++] = c;
10618 }
10619 }
10620
10621 res[ri] = NUL;
10622}
10623
10624 static void
10625spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 slang_T *slang;
10627 char_u *inword;
10628 char_u *res;
10629{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010630 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010632 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010634 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010636 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010637 int n, k = 0;
10638 int z0;
10639 int k0;
10640 int n0;
10641 int c;
10642 int pri;
10643 int p0 = -333;
10644 int c0;
10645
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010646 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010647 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 if (slang->sl_rem_accents)
10649 {
10650 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010651 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010652 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010653 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010654 {
10655 *t++ = ' ';
10656 s = skipwhite(s);
10657 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010658 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010659 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010660 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010661 *t++ = *s;
10662 ++s;
10663 }
10664 }
10665 *t = NUL;
10666 }
10667 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010668 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010670 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671
10672 /*
10673 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010674 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010676 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010677 while ((c = word[i]) != NUL)
10678 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010679 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010680 n = slang->sl_sal_first[c];
10681 z0 = 0;
10682
10683 if (n >= 0)
10684 {
10685 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010686 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010688 /* Quickly skip entries that don't match the word. Most
10689 * entries are less then three chars, optimize for that. */
10690 k = smp[n].sm_leadlen;
10691 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010693 if (word[i + 1] != s[1])
10694 continue;
10695 if (k > 2)
10696 {
10697 for (j = 2; j < k; ++j)
10698 if (word[i + j] != s[j])
10699 break;
10700 if (j < k)
10701 continue;
10702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 }
10704
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010705 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010706 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010707 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010708 while (*pf != NUL && *pf != word[i + k])
10709 ++pf;
10710 if (*pf == NUL)
10711 continue;
10712 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010714 s = smp[n].sm_rules;
10715 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010716
10717 p0 = *s;
10718 k0 = k;
10719 while (*s == '-' && k > 1)
10720 {
10721 k--;
10722 s++;
10723 }
10724 if (*s == '<')
10725 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010726 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 {
10728 /* determine priority */
10729 pri = *s - '0';
10730 s++;
10731 }
10732 if (*s == '^' && *(s + 1) == '^')
10733 s++;
10734
10735 if (*s == NUL
10736 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010737 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010738 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010739 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010740 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010742 && spell_iswordp(word + i - 1, curbuf)
10743 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 {
10745 /* search for followup rules, if: */
10746 /* followup and k > 1 and NO '-' in searchstring */
10747 c0 = word[i + k - 1];
10748 n0 = slang->sl_sal_first[c0];
10749
10750 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010751 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 {
10753 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010754 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010756 /* Quickly skip entries that don't match the word.
10757 * */
10758 k0 = smp[n0].sm_leadlen;
10759 if (k0 > 1)
10760 {
10761 if (word[i + k] != s[1])
10762 continue;
10763 if (k0 > 2)
10764 {
10765 pf = word + i + k + 1;
10766 for (j = 2; j < k0; ++j)
10767 if (*pf++ != s[j])
10768 break;
10769 if (j < k0)
10770 continue;
10771 }
10772 }
10773 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010774
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010775 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010776 {
10777 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010778 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010779 while (*pf != NUL && *pf != word[i + k0])
10780 ++pf;
10781 if (*pf == NUL)
10782 continue;
10783 ++k0;
10784 }
10785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010786 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010787 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 while (*s == '-')
10789 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010790 /* "k0" gets NOT reduced because
10791 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010792 s++;
10793 }
10794 if (*s == '<')
10795 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010796 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010797 {
10798 p0 = *s - '0';
10799 s++;
10800 }
10801
10802 if (*s == NUL
10803 /* *s == '^' cuts */
10804 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010805 && !spell_iswordp(word + i + k0,
10806 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 {
10808 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010810 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811
10812 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010815 /* rule fits; stop search */
10816 break;
10817 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010818 }
10819
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010820 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 }
10823
10824 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010825 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010826 if (s == NULL)
10827 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010828 pf = smp[n].sm_rules;
10829 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 if (p0 == 1 && z == 0)
10831 {
10832 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010833 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
10834 || res[reslen - 1] == *s))
10835 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 z0 = 1;
10837 z = 1;
10838 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010839 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 {
10841 word[i + k0] = *s;
10842 k0++;
10843 s++;
10844 }
10845 if (k > k0)
10846 mch_memmove(word + i + k0, word + i + k,
10847 STRLEN(word + i + k) + 1);
10848
10849 /* new "actual letter" */
10850 c = word[i];
10851 }
10852 else
10853 {
10854 /* no '<' rule used */
10855 i += k - 1;
10856 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010857 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010859 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010860 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 s++;
10862 }
10863 /* new "actual letter" */
10864 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010865 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 {
10867 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010868 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 mch_memmove(word, word + i + 1,
10870 STRLEN(word + i + 1) + 1);
10871 i = 0;
10872 z0 = 1;
10873 }
10874 }
10875 break;
10876 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 }
10878 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010879 else if (vim_iswhite(c))
10880 {
10881 c = ' ';
10882 k = 1;
10883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884
10885 if (z0 == 0)
10886 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010887 if (k && !p0 && reslen < MAXWLEN && c != NUL
10888 && (!slang->sl_collapse || reslen == 0
10889 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010891 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892
10893 i++;
10894 z = 0;
10895 k = 0;
10896 }
10897 }
10898
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010899 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900}
10901
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010902#ifdef FEAT_MBYTE
10903/*
10904 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
10905 * Multi-byte version of spell_soundfold().
10906 */
10907 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010908spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010909 slang_T *slang;
10910 char_u *inword;
10911 char_u *res;
10912{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010913 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010914 int word[MAXWLEN];
10915 int wres[MAXWLEN];
10916 int l;
10917 char_u *s;
10918 int *ws;
10919 char_u *t;
10920 int *pf;
10921 int i, j, z;
10922 int reslen;
10923 int n, k = 0;
10924 int z0;
10925 int k0;
10926 int n0;
10927 int c;
10928 int pri;
10929 int p0 = -333;
10930 int c0;
10931 int did_white = FALSE;
10932
10933 /*
10934 * Convert the multi-byte string to a wide-character string.
10935 * Remove accents, if wanted. We actually remove all non-word characters.
10936 * But keep white space.
10937 */
10938 n = 0;
10939 for (s = inword; *s != NUL; )
10940 {
10941 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010942 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010943 if (slang->sl_rem_accents)
10944 {
10945 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10946 {
10947 if (did_white)
10948 continue;
10949 c = ' ';
10950 did_white = TRUE;
10951 }
10952 else
10953 {
10954 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010955 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010956 continue;
10957 }
10958 }
10959 word[n++] = c;
10960 }
10961 word[n] = NUL;
10962
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010963 /*
10964 * This comes from Aspell phonet.cpp.
10965 * Converted from C++ to C. Added support for multi-byte chars.
10966 * Changed to keep spaces.
10967 */
10968 i = reslen = z = 0;
10969 while ((c = word[i]) != NUL)
10970 {
10971 /* Start with the first rule that has the character in the word. */
10972 n = slang->sl_sal_first[c & 0xff];
10973 z0 = 0;
10974
10975 if (n >= 0)
10976 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010977 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010978 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
10979 {
10980 /* Quickly skip entries that don't match the word. Most
10981 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010982 if (c != ws[0])
10983 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010984 k = smp[n].sm_leadlen;
10985 if (k > 1)
10986 {
10987 if (word[i + 1] != ws[1])
10988 continue;
10989 if (k > 2)
10990 {
10991 for (j = 2; j < k; ++j)
10992 if (word[i + j] != ws[j])
10993 break;
10994 if (j < k)
10995 continue;
10996 }
10997 }
10998
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010999 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011000 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011001 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011002 while (*pf != NUL && *pf != word[i + k])
11003 ++pf;
11004 if (*pf == NUL)
11005 continue;
11006 ++k;
11007 }
11008 s = smp[n].sm_rules;
11009 pri = 5; /* default priority */
11010
11011 p0 = *s;
11012 k0 = k;
11013 while (*s == '-' && k > 1)
11014 {
11015 k--;
11016 s++;
11017 }
11018 if (*s == '<')
11019 s++;
11020 if (VIM_ISDIGIT(*s))
11021 {
11022 /* determine priority */
11023 pri = *s - '0';
11024 s++;
11025 }
11026 if (*s == '^' && *(s + 1) == '^')
11027 s++;
11028
11029 if (*s == NUL
11030 || (*s == '^'
11031 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011032 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011033 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011034 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011035 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011036 && spell_iswordp_w(word + i - 1, curbuf)
11037 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011038 {
11039 /* search for followup rules, if: */
11040 /* followup and k > 1 and NO '-' in searchstring */
11041 c0 = word[i + k - 1];
11042 n0 = slang->sl_sal_first[c0 & 0xff];
11043
11044 if (slang->sl_followup && k > 1 && n0 >= 0
11045 && p0 != '-' && word[i + k] != NUL)
11046 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011047 /* Test follow-up rule for "word[i + k]"; loop over
11048 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011049 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11050 == (c0 & 0xff); ++n0)
11051 {
11052 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011053 */
11054 if (c0 != ws[0])
11055 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011056 k0 = smp[n0].sm_leadlen;
11057 if (k0 > 1)
11058 {
11059 if (word[i + k] != ws[1])
11060 continue;
11061 if (k0 > 2)
11062 {
11063 pf = word + i + k + 1;
11064 for (j = 2; j < k0; ++j)
11065 if (*pf++ != ws[j])
11066 break;
11067 if (j < k0)
11068 continue;
11069 }
11070 }
11071 k0 += k - 1;
11072
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011073 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011074 {
11075 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011076 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011077 while (*pf != NUL && *pf != word[i + k0])
11078 ++pf;
11079 if (*pf == NUL)
11080 continue;
11081 ++k0;
11082 }
11083
11084 p0 = 5;
11085 s = smp[n0].sm_rules;
11086 while (*s == '-')
11087 {
11088 /* "k0" gets NOT reduced because
11089 * "if (k0 == k)" */
11090 s++;
11091 }
11092 if (*s == '<')
11093 s++;
11094 if (VIM_ISDIGIT(*s))
11095 {
11096 p0 = *s - '0';
11097 s++;
11098 }
11099
11100 if (*s == NUL
11101 /* *s == '^' cuts */
11102 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011103 && !spell_iswordp_w(word + i + k0,
11104 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011105 {
11106 if (k0 == k)
11107 /* this is just a piece of the string */
11108 continue;
11109
11110 if (p0 < pri)
11111 /* priority too low */
11112 continue;
11113 /* rule fits; stop search */
11114 break;
11115 }
11116 }
11117
11118 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11119 == (c0 & 0xff))
11120 continue;
11121 }
11122
11123 /* replace string */
11124 ws = smp[n].sm_to_w;
11125 s = smp[n].sm_rules;
11126 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11127 if (p0 == 1 && z == 0)
11128 {
11129 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011130 if (reslen > 0 && ws != NULL && *ws != NUL
11131 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011132 || wres[reslen - 1] == *ws))
11133 reslen--;
11134 z0 = 1;
11135 z = 1;
11136 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011137 if (ws != NULL)
11138 while (*ws != NUL && word[i + k0] != NUL)
11139 {
11140 word[i + k0] = *ws;
11141 k0++;
11142 ws++;
11143 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011144 if (k > k0)
11145 mch_memmove(word + i + k0, word + i + k,
11146 sizeof(int) * (STRLEN(word + i + k) + 1));
11147
11148 /* new "actual letter" */
11149 c = word[i];
11150 }
11151 else
11152 {
11153 /* no '<' rule used */
11154 i += k - 1;
11155 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011156 if (ws != NULL)
11157 while (*ws != NUL && ws[1] != NUL
11158 && reslen < MAXWLEN)
11159 {
11160 if (reslen == 0 || wres[reslen - 1] != *ws)
11161 wres[reslen++] = *ws;
11162 ws++;
11163 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011164 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011165 if (ws == NULL)
11166 c = NUL;
11167 else
11168 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011169 if (strstr((char *)s, "^^") != NULL)
11170 {
11171 if (c != NUL)
11172 wres[reslen++] = c;
11173 mch_memmove(word, word + i + 1,
11174 sizeof(int) * (STRLEN(word + i + 1) + 1));
11175 i = 0;
11176 z0 = 1;
11177 }
11178 }
11179 break;
11180 }
11181 }
11182 }
11183 else if (vim_iswhite(c))
11184 {
11185 c = ' ';
11186 k = 1;
11187 }
11188
11189 if (z0 == 0)
11190 {
11191 if (k && !p0 && reslen < MAXWLEN && c != NUL
11192 && (!slang->sl_collapse || reslen == 0
11193 || wres[reslen - 1] != c))
11194 /* condense only double letters */
11195 wres[reslen++] = c;
11196
11197 i++;
11198 z = 0;
11199 k = 0;
11200 }
11201 }
11202
11203 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11204 l = 0;
11205 for (n = 0; n < reslen; ++n)
11206 {
11207 l += mb_char2bytes(wres[n], res + l);
11208 if (l + MB_MAXBYTES > MAXWLEN)
11209 break;
11210 }
11211 res[l] = NUL;
11212}
11213#endif
11214
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011215/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011216 * Compute a score for two sound-a-like words.
11217 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11218 * Instead of a generic loop we write out the code. That keeps it fast by
11219 * avoiding checks that will not be possible.
11220 */
11221 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011222soundalike_score(goodstart, badstart)
11223 char_u *goodstart; /* sound-folded good word */
11224 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011225{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011226 char_u *goodsound = goodstart;
11227 char_u *badsound = badstart;
11228 int goodlen;
11229 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011230 int n;
11231 char_u *pl, *ps;
11232 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011233 int score = 0;
11234
11235 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11236 * counted so much, vowels halfway the word aren't counted at all. */
11237 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11238 {
11239 score = SCORE_DEL / 2;
11240 if (*badsound == '*')
11241 ++badsound;
11242 else
11243 ++goodsound;
11244 }
11245
11246 goodlen = STRLEN(goodsound);
11247 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011248
11249 /* Return quickly if the lenghts are too different to be fixed by two
11250 * changes. */
11251 n = goodlen - badlen;
11252 if (n < -2 || n > 2)
11253 return SCORE_MAXMAX;
11254
11255 if (n > 0)
11256 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011257 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011258 ps = badsound;
11259 }
11260 else
11261 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011262 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011263 ps = goodsound;
11264 }
11265
11266 /* Skip over the identical part. */
11267 while (*pl == *ps && *pl != NUL)
11268 {
11269 ++pl;
11270 ++ps;
11271 }
11272
11273 switch (n)
11274 {
11275 case -2:
11276 case 2:
11277 /*
11278 * Must delete two characters from "pl".
11279 */
11280 ++pl; /* first delete */
11281 while (*pl == *ps)
11282 {
11283 ++pl;
11284 ++ps;
11285 }
11286 /* strings must be equal after second delete */
11287 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011288 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011289
11290 /* Failed to compare. */
11291 break;
11292
11293 case -1:
11294 case 1:
11295 /*
11296 * Minimal one delete from "pl" required.
11297 */
11298
11299 /* 1: delete */
11300 pl2 = pl + 1;
11301 ps2 = ps;
11302 while (*pl2 == *ps2)
11303 {
11304 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011305 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011306 ++pl2;
11307 ++ps2;
11308 }
11309
11310 /* 2: delete then swap, then rest must be equal */
11311 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11312 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011313 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011314
11315 /* 3: delete then substitute, then the rest must be equal */
11316 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011317 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011318
11319 /* 4: first swap then delete */
11320 if (pl[0] == ps[1] && pl[1] == ps[0])
11321 {
11322 pl2 = pl + 2; /* swap, skip two chars */
11323 ps2 = ps + 2;
11324 while (*pl2 == *ps2)
11325 {
11326 ++pl2;
11327 ++ps2;
11328 }
11329 /* delete a char and then strings must be equal */
11330 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011331 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011332 }
11333
11334 /* 5: first substitute then delete */
11335 pl2 = pl + 1; /* substitute, skip one char */
11336 ps2 = ps + 1;
11337 while (*pl2 == *ps2)
11338 {
11339 ++pl2;
11340 ++ps2;
11341 }
11342 /* delete a char and then strings must be equal */
11343 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011344 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011345
11346 /* Failed to compare. */
11347 break;
11348
11349 case 0:
11350 /*
11351 * Lenghts are equal, thus changes must result in same length: An
11352 * insert is only possible in combination with a delete.
11353 * 1: check if for identical strings
11354 */
11355 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011356 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011357
11358 /* 2: swap */
11359 if (pl[0] == ps[1] && pl[1] == ps[0])
11360 {
11361 pl2 = pl + 2; /* swap, skip two chars */
11362 ps2 = ps + 2;
11363 while (*pl2 == *ps2)
11364 {
11365 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011366 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011367 ++pl2;
11368 ++ps2;
11369 }
11370 /* 3: swap and swap again */
11371 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11372 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011373 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011374
11375 /* 4: swap and substitute */
11376 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011377 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011378 }
11379
11380 /* 5: substitute */
11381 pl2 = pl + 1;
11382 ps2 = ps + 1;
11383 while (*pl2 == *ps2)
11384 {
11385 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011386 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011387 ++pl2;
11388 ++ps2;
11389 }
11390
11391 /* 6: substitute and swap */
11392 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11393 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011394 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011395
11396 /* 7: substitute and substitute */
11397 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011398 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011399
11400 /* 8: insert then delete */
11401 pl2 = pl;
11402 ps2 = ps + 1;
11403 while (*pl2 == *ps2)
11404 {
11405 ++pl2;
11406 ++ps2;
11407 }
11408 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011409 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011410
11411 /* 9: delete then insert */
11412 pl2 = pl + 1;
11413 ps2 = ps;
11414 while (*pl2 == *ps2)
11415 {
11416 ++pl2;
11417 ++ps2;
11418 }
11419 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011420 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011421
11422 /* Failed to compare. */
11423 break;
11424 }
11425
11426 return SCORE_MAXMAX;
11427}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011429/*
11430 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011431 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011432 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000011433 * The algorithm is described by Du and Chang, 1992.
11434 * The implementation of the algorithm comes from Aspell editdist.cpp,
11435 * edit_distance(). It has been converted from C++ to C and modified to
11436 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 */
11438 static int
11439spell_edit_score(badword, goodword)
11440 char_u *badword;
11441 char_u *goodword;
11442{
11443 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011444 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011445 int j, i;
11446 int t;
11447 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011448 int pbc, pgc;
11449#ifdef FEAT_MBYTE
11450 char_u *p;
11451 int wbadword[MAXWLEN];
11452 int wgoodword[MAXWLEN];
11453
11454 if (has_mbyte)
11455 {
11456 /* Get the characters from the multi-byte strings and put them in an
11457 * int array for easy access. */
11458 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011459 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011460 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011461 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011462 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011463 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011464 }
11465 else
11466#endif
11467 {
11468 badlen = STRLEN(badword) + 1;
11469 goodlen = STRLEN(goodword) + 1;
11470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471
11472 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
11473#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
11475 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011476 if (cnt == NULL)
11477 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478
11479 CNT(0, 0) = 0;
11480 for (j = 1; j <= goodlen; ++j)
11481 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
11482
11483 for (i = 1; i <= badlen; ++i)
11484 {
11485 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
11486 for (j = 1; j <= goodlen; ++j)
11487 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011488#ifdef FEAT_MBYTE
11489 if (has_mbyte)
11490 {
11491 bc = wbadword[i - 1];
11492 gc = wgoodword[j - 1];
11493 }
11494 else
11495#endif
11496 {
11497 bc = badword[i - 1];
11498 gc = goodword[j - 1];
11499 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 if (bc == gc)
11501 CNT(i, j) = CNT(i - 1, j - 1);
11502 else
11503 {
11504 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011505 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
11507 else
11508 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
11509
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011510 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011511 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011512#ifdef FEAT_MBYTE
11513 if (has_mbyte)
11514 {
11515 pbc = wbadword[i - 2];
11516 pgc = wgoodword[j - 2];
11517 }
11518 else
11519#endif
11520 {
11521 pbc = badword[i - 2];
11522 pgc = goodword[j - 2];
11523 }
11524 if (bc == pgc && pbc == gc)
11525 {
11526 t = SCORE_SWAP + CNT(i - 2, j - 2);
11527 if (t < CNT(i, j))
11528 CNT(i, j) = t;
11529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011530 }
11531 t = SCORE_DEL + CNT(i - 1, j);
11532 if (t < CNT(i, j))
11533 CNT(i, j) = t;
11534 t = SCORE_INS + CNT(i, j - 1);
11535 if (t < CNT(i, j))
11536 CNT(i, j) = t;
11537 }
11538 }
11539 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011540
11541 i = CNT(badlen - 1, goodlen - 1);
11542 vim_free(cnt);
11543 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000011545
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011546/*
11547 * ":spelldump"
11548 */
11549/*ARGSUSED*/
11550 void
11551ex_spelldump(eap)
11552 exarg_T *eap;
11553{
11554 buf_T *buf = curbuf;
11555 langp_T *lp;
11556 slang_T *slang;
11557 idx_T arridx[MAXWLEN];
11558 int curi[MAXWLEN];
11559 char_u word[MAXWLEN];
11560 int c;
11561 char_u *byts;
11562 idx_T *idxs;
11563 linenr_T lnum = 0;
11564 int round;
11565 int depth;
11566 int n;
11567 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011568 char_u *region_names = NULL; /* region names being used */
11569 int do_region = TRUE; /* dump region names and numbers */
11570 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011571
11572 if (no_spell_checking())
11573 return;
11574
11575 /* Create a new empty buffer by splitting the window. */
11576 do_cmdline_cmd((char_u *)"new");
11577 if (!bufempty() || !buf_valid(buf))
11578 return;
11579
Bram Moolenaar7887d882005-07-01 22:33:52 +000011580 /* Find out if we can support regions: All languages must support the same
11581 * regions or none at all. */
11582 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11583 {
11584 p = lp->lp_slang->sl_regions;
11585 if (p[0] != 0)
11586 {
11587 if (region_names == NULL) /* first language with regions */
11588 region_names = p;
11589 else if (STRCMP(region_names, p) != 0)
11590 {
11591 do_region = FALSE; /* region names are different */
11592 break;
11593 }
11594 }
11595 }
11596
11597 if (do_region && region_names != NULL)
11598 {
11599 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
11600 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11601 }
11602 else
11603 do_region = FALSE;
11604
11605 /*
11606 * Loop over all files loaded for the entries in 'spelllang'.
11607 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011608 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11609 {
11610 slang = lp->lp_slang;
11611
11612 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
11613 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11614
11615 /* round 1: case-folded tree
11616 * round 2: keep-case tree */
11617 for (round = 1; round <= 2; ++round)
11618 {
11619 if (round == 1)
11620 {
11621 byts = slang->sl_fbyts;
11622 idxs = slang->sl_fidxs;
11623 }
11624 else
11625 {
11626 byts = slang->sl_kbyts;
11627 idxs = slang->sl_kidxs;
11628 }
11629 if (byts == NULL)
11630 continue; /* array is empty */
11631
11632 depth = 0;
11633 arridx[0] = 0;
11634 curi[0] = 1;
11635 while (depth >= 0 && !got_int)
11636 {
11637 if (curi[depth] > byts[arridx[depth]])
11638 {
11639 /* Done all bytes at this node, go up one level. */
11640 --depth;
11641 line_breakcheck();
11642 }
11643 else
11644 {
11645 /* Do one more byte at this node. */
11646 n = arridx[depth] + curi[depth];
11647 ++curi[depth];
11648 c = byts[n];
11649 if (c == 0)
11650 {
11651 /* End of word, deal with the word.
11652 * Don't use keep-case words in the fold-case tree,
11653 * they will appear in the keep-case tree.
11654 * Only use the word when the region matches. */
11655 flags = (int)idxs[n];
11656 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000011657 && (do_region
11658 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011659 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011660 & lp->lp_region) != 0))
11661 {
11662 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011663 if (!do_region)
11664 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011665
11666 /* Dump the basic word if there is no prefix or
11667 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011668 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011669 if (c == 0 || curi[depth] == 2)
11670 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011671
11672 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011673 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011674 lnum = apply_prefixes(slang, word, round,
11675 flags, lnum);
11676 }
11677 }
11678 else
11679 {
11680 /* Normal char, go one level deeper. */
11681 word[depth++] = c;
11682 arridx[depth] = idxs[n];
11683 curi[depth] = 1;
11684 }
11685 }
11686 }
11687 }
11688 }
11689
11690 /* Delete the empty line that we started with. */
11691 if (curbuf->b_ml.ml_line_count > 1)
11692 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
11693
11694 redraw_later(NOT_VALID);
11695}
11696
11697/*
11698 * Dump one word: apply case modifications and append a line to the buffer.
11699 */
11700 static void
11701dump_word(word, round, flags, lnum)
11702 char_u *word;
11703 int round;
11704 int flags;
11705 linenr_T lnum;
11706{
11707 int keepcap = FALSE;
11708 char_u *p;
11709 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000011710 char_u badword[MAXWLEN + 10];
11711 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011712
11713 if (round == 1 && (flags & WF_CAPMASK) != 0)
11714 {
11715 /* Need to fix case according to "flags". */
11716 make_case_word(word, cword, flags);
11717 p = cword;
11718 }
11719 else
11720 {
11721 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011722 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
11723 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011724 keepcap = TRUE;
11725 }
11726
Bram Moolenaar7887d882005-07-01 22:33:52 +000011727 /* Add flags and regions after a slash. */
11728 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011729 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000011730 STRCPY(badword, p);
11731 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011732 if (keepcap)
11733 STRCAT(badword, "=");
11734 if (flags & WF_BANNED)
11735 STRCAT(badword, "!");
11736 else if (flags & WF_RARE)
11737 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000011738 if (flags & WF_REGION)
11739 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011740 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000011741 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011742 p = badword;
11743 }
11744
11745 ml_append(lnum, p, (colnr_T)0, FALSE);
11746}
11747
11748/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011749 * For ":spelldump": Find matching prefixes for "word". Prepend each to
11750 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011751 * Return the updated line number.
11752 */
11753 static linenr_T
11754apply_prefixes(slang, word, round, flags, startlnum)
11755 slang_T *slang;
11756 char_u *word; /* case-folded word */
11757 int round;
11758 int flags; /* flags with prefix ID */
11759 linenr_T startlnum;
11760{
11761 idx_T arridx[MAXWLEN];
11762 int curi[MAXWLEN];
11763 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000011764 char_u word_up[MAXWLEN];
11765 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011766 int c;
11767 char_u *byts;
11768 idx_T *idxs;
11769 linenr_T lnum = startlnum;
11770 int depth;
11771 int n;
11772 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011773 int i;
11774
Bram Moolenaar53805d12005-08-01 07:08:33 +000011775 /* if the word starts with a lower-case letter make the word with an
11776 * upper-case letter in word_up[]. */
11777 c = PTR2CHAR(word);
11778 if (SPELL_TOUPPER(c) != c)
11779 {
11780 onecap_copy(word, word_up, TRUE);
11781 has_word_up = TRUE;
11782 }
11783
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011784 byts = slang->sl_pbyts;
11785 idxs = slang->sl_pidxs;
11786 if (byts != NULL) /* array not is empty */
11787 {
11788 /*
11789 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011790 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011791 */
11792 depth = 0;
11793 arridx[0] = 0;
11794 curi[0] = 1;
11795 while (depth >= 0 && !got_int)
11796 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011797 n = arridx[depth];
11798 len = byts[n];
11799 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011800 {
11801 /* Done all bytes at this node, go up one level. */
11802 --depth;
11803 line_breakcheck();
11804 }
11805 else
11806 {
11807 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011808 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011809 ++curi[depth];
11810 c = byts[n];
11811 if (c == 0)
11812 {
11813 /* End of prefix, find out how many IDs there are. */
11814 for (i = 1; i < len; ++i)
11815 if (byts[n + i] != 0)
11816 break;
11817 curi[depth] += i - 1;
11818
Bram Moolenaar53805d12005-08-01 07:08:33 +000011819 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
11820 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011821 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011822 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011823 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011824 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011825 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011826 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011827
11828 /* Check for prefix that matches the word when the
11829 * first letter is upper-case, but only if the prefix has
11830 * a condition. */
11831 if (has_word_up)
11832 {
11833 c = valid_word_prefix(i, n, flags, word_up, slang,
11834 TRUE);
11835 if (c != 0)
11836 {
11837 vim_strncpy(prefix + depth, word_up,
11838 MAXWLEN - depth - 1);
11839 dump_word(prefix, round,
11840 (c & WF_RAREPFX) ? (flags | WF_RARE)
11841 : flags, lnum++);
11842 }
11843 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011844 }
11845 else
11846 {
11847 /* Normal char, go one level deeper. */
11848 prefix[depth++] = c;
11849 arridx[depth] = idxs[n];
11850 curi[depth] = 1;
11851 }
11852 }
11853 }
11854 }
11855
11856 return lnum;
11857}
11858
Bram Moolenaar8b59de92005-08-11 19:59:29 +000011859#if defined(FEAT_INS_EXPAND) || defined(PROTO)
11860static int spell_expand_need_cap;
11861
11862/*
11863 * Find start of the word in front of the cursor. We don't check if it is
11864 * badly spelled, with completion we can only change the word in front of the
11865 * cursor.
11866 * Used for Insert mode completion CTRL-X ?.
11867 * Returns the column number of the word.
11868 */
11869 int
11870spell_word_start(startcol)
11871 int startcol;
11872{
11873 char_u *line;
11874 char_u *p;
11875 int col = 0;
11876
11877 if (no_spell_checking())
11878 return startcol;
11879
11880 /* Find a word character before "startcol". */
11881 line = ml_get_curline();
11882 for (p = line + startcol; p > line; )
11883 {
11884 mb_ptr_back(line, p);
11885 if (spell_iswordp_nmw(p))
11886 break;
11887 }
11888
11889 /* Go back to start of the word. */
11890 while (p > line)
11891 {
11892 col = p - line;
11893 mb_ptr_back(line, p);
11894 if (!spell_iswordp(p, curbuf))
11895 break;
11896 col = 0;
11897 }
11898
11899 /* Need to check for 'spellcapcheck' now, the word is removed before
11900 * expand_spelling() is called. Therefore the ugly global variable. */
11901 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
11902
11903 return col;
11904}
11905
11906/*
11907 * Get list of spelling suggestions.
11908 * Used for Insert mode completion CTRL-X ?.
11909 * Returns the number of matches. The matches are in "matchp[]", array of
11910 * allocated strings.
11911 */
11912/*ARGSUSED*/
11913 int
11914expand_spelling(lnum, col, pat, matchp)
11915 linenr_T lnum;
11916 int col;
11917 char_u *pat;
11918 char_u ***matchp;
11919{
11920 garray_T ga;
11921
11922 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
11923 *matchp = ga.ga_data;
11924 return ga.ga_len;
11925}
11926#endif
11927
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000011928#endif /* FEAT_SYN_HL */