blob: 26d80500f1e8e9b436cdf5763db65bf76888fd4a [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 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000165 * sectionID == SN_NOBREAK: (empty, its presence is enough)
166 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000167 * sectionID == SN_SYLLABLE: <syllable>
168 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000169 *
170 * <LWORDTREE>: <wordtree>
171 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000172 * <KWORDTREE>: <wordtree>
173 *
174 * <PREFIXTREE>: <wordtree>
175 *
176 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000177 * <wordtree>: <nodecount> <nodedata> ...
178 *
179 * <nodecount> 4 bytes Number of nodes following. MSB first.
180 *
181 * <nodedata>: <siblingcount> <sibling> ...
182 *
183 * <siblingcount> 1 byte Number of siblings in this node. The siblings
184 * follow in sorted order.
185 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000186 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000187 * | <flags> [<flags2>] [<region>] [<affixID>]
188 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189 *
190 * <byte> 1 byte Byte value of the sibling. Special cases:
191 * BY_NOFLAGS: End of word without flags and for all
192 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000193 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000194 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000195 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000196 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000197 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000198 * BY_FLAGS2: End of word, <flags> and <flags2>
199 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000200 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * and <xbyte> follow.
202 *
203 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
204 *
205 * <xbyte> 1 byte byte value of the sibling.
206 *
207 * <flags> 1 byte bitmask of:
208 * WF_ALLCAP word must have only capitals
209 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000210 * WF_KEEPCAP keep-case word
211 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000213 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000215 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000216 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000217 * <flags2> 1 byte Only used when there are postponed prefixes.
218 * Bitmask of:
219 * WF_HAS_AFF >> 8 word includes affix
220 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000221 * <pflags> 1 byte bitmask of:
222 * WFP_RARE rare prefix
223 * WFP_NC non-combining prefix
224 * WFP_UP letter after prefix made upper case
225 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 * <region> 1 byte Bitmask for regions in which word is valid. When
227 * omitted it's valid in all regions.
228 * Lowest bit is for region 1.
229 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000231 * PREFIXTREE used for the required prefix ID.
232 *
233 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
234 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000236 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000237 */
238
Bram Moolenaare19defe2005-03-21 08:23:33 +0000239#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
240# include <io.h> /* for lseek(), must be before vim.h */
241#endif
242
243#include "vim.h"
244
245#if defined(FEAT_SYN_HL) || defined(PROTO)
246
247#ifdef HAVE_FCNTL_H
248# include <fcntl.h>
249#endif
250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000251#define MAXWLEN 250 /* Assume max. word len is this many bytes.
252 Some places assume a word length fits in a
253 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000254
Bram Moolenaare52325c2005-08-22 22:54:29 +0000255/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000256 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000257#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000258typedef int idx_T;
259#else
260typedef long idx_T;
261#endif
262
263/* Flags used for a word. Only the lowest byte can be used, the region byte
264 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000265#define WF_REGION 0x01 /* region byte follows */
266#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
267#define WF_ALLCAP 0x04 /* word must be all capitals */
268#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000269#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000270#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000271#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000272#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000274/* for <flags2>, shifted up one byte to be used in wn_flags */
275#define WF_HAS_AFF 0x0100 /* word includes affix */
276
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000277#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278
Bram Moolenaar53805d12005-08-01 07:08:33 +0000279/* flags for <pflags> */
280#define WFP_RARE 0x01 /* rare prefix */
281#define WFP_NC 0x02 /* prefix is not combining */
282#define WFP_UP 0x04 /* to-upper prefix */
283
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000284/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000285 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000286#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
287 * postponed prefix */
288#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
289 * postponed prefix */
290#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
291 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000292
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000293/* Special byte values for <byte>. Some are only used in the tree for
294 * postponed prefixes, some only in the other trees. This is a bit messy... */
295#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000296 * postponed prefix: no <pflags> */
297#define BY_INDEX 1 /* child is shared, index follows */
298#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
299 * postponed prefix: <pflags> follows */
300#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
301 * follow; never used in prefix tree */
302#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000305 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000306 * One replacement: from "ft_from" to "ft_to". */
307typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000308{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000309 char_u *ft_from;
310 char_u *ft_to;
311} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000312
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000313/* Info from "SAL" entries in ".aff" file used in sl_sal.
314 * The info is split for quick processing by spell_soundfold().
315 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
316typedef struct salitem_S
317{
318 char_u *sm_lead; /* leading letters */
319 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000321 char_u *sm_rules; /* rules like ^, $, priority */
322 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000323#ifdef FEAT_MBYTE
324 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000325 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000326 int *sm_to_w; /* wide character copy of "sm_to" */
327#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000328} salitem_T;
329
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000330#ifdef FEAT_MBYTE
331typedef int salfirst_T;
332#else
333typedef short salfirst_T;
334#endif
335
Bram Moolenaar5195e452005-08-19 20:32:47 +0000336/* Values for SP_*ERROR are negative, positive values are used by
337 * read_cnt_string(). */
338#define SP_TRUNCERROR -1 /* spell file truncated error */
339#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000340#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000341
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000342/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000343 * Structure used to store words and other info for one language, loaded from
344 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000345 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
346 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
347 *
348 * The "byts" array stores the possible bytes in each tree node, preceded by
349 * the number of possible bytes, sorted on byte value:
350 * <len> <byte1> <byte2> ...
351 * The "idxs" array stores the index of the child node corresponding to the
352 * byte in "byts".
353 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000354 * the flags, region mask and affixID for the word. There may be several
355 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000356 */
357typedef struct slang_S slang_T;
358struct slang_S
359{
360 slang_T *sl_next; /* next language */
361 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000362 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000363 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000364
Bram Moolenaar51485f02005-06-04 21:55:20 +0000365 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000366 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000367 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000368 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000369 char_u *sl_pbyts; /* prefix tree word bytes */
370 idx_T *sl_pidxs; /* prefix tree word indexes */
371
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000372 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000373
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000374 char_u *sl_midword; /* MIDWORD string or NULL */
375
Bram Moolenaar5195e452005-08-19 20:32:47 +0000376 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
377 int sl_compminlen; /* COMPOUNDMIN (default: MAXWLEN) */
378 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
379 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
380 * (NULL when no compounding) */
381 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000382 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000383 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000384 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
385 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000386
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000387 int sl_prefixcnt; /* number of items in "sl_prefprog" */
388 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390 garray_T sl_rep; /* list of fromto_T entries from REP lines */
391 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
392 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000393 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000394 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000396 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
397 * "sl_sal_first" maps chars, when has_mbyte
398 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000399 int sl_followup; /* SAL followup */
400 int sl_collapse; /* SAL collapse_result */
401 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000402 int sl_has_map; /* TRUE if there is a MAP line */
403#ifdef FEAT_MBYTE
404 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
405 int sl_map_array[256]; /* MAP for first 256 chars */
406#else
407 char_u sl_map_array[256]; /* MAP for first 256 chars */
408#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000409};
410
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000411/* First language that is loaded, start of the linked list of loaded
412 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000413static slang_T *first_lang = NULL;
414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000415/* Flags used in .spl file for soundsalike flags. */
416#define SAL_F0LLOWUP 1
417#define SAL_COLLAPSE 2
418#define SAL_REM_ACCENTS 4
419
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000420/*
421 * Structure used in "b_langp", filled from 'spelllang'.
422 */
423typedef struct langp_S
424{
425 slang_T *lp_slang; /* info for this language (NULL for last one) */
426 int lp_region; /* bitmask for region or REGION_ALL */
427} langp_T;
428
429#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
430
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000431#define REGION_ALL 0xff /* word valid in all regions */
432
Bram Moolenaar5195e452005-08-19 20:32:47 +0000433#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
434#define VIMSPELLMAGICL 8
435#define VIMSPELLVERSION 50
436
437/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
438#define SN_REGION 0 /* <regionname> section */
439#define SN_CHARFLAGS 1 /* charflags section */
440#define SN_MIDWORD 2 /* <midword> section */
441#define SN_PREFCOND 3 /* <prefcond> section */
442#define SN_REP 4 /* REP items section */
443#define SN_SAL 5 /* SAL items section */
444#define SN_SOFO 6 /* soundfolding section */
445#define SN_MAP 7 /* MAP items section */
446#define SN_COMPOUND 8 /* compound words section */
447#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000448#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000449#define SN_END 255 /* end of sections */
450
451#define SNF_REQUIRED 1 /* <sectionflags>: required section */
452
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000453/* Result values. Lower number is accepted over higher one. */
454#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000455#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000456#define SP_RARE 1
457#define SP_LOCAL 2
458#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459
Bram Moolenaar7887d882005-07-01 22:33:52 +0000460/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000461static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000463/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464 * Information used when looking for suggestions.
465 */
466typedef struct suginfo_S
467{
468 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000469 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000470 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000471 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000472 char_u *su_badptr; /* start of bad word in line */
473 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000474 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
476 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
477 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000478} suginfo_T;
479
480/* One word suggestion. Used in "si_ga". */
481typedef struct suggest_S
482{
483 char_u *st_word; /* suggested word, allocated string */
484 int st_orglen; /* length of replaced text */
485 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000486 int st_altscore; /* used when st_score compares equal */
487 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000488 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000489} suggest_T;
490
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000492
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000493/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
494 * called the score may change, thus we need to keep more than what is
495 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000496#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000497
498/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
499 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000500#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000501
502/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000503#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000504#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000505#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000506#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000507#define SCORE_SWAP 90 /* swap two characters */
508#define SCORE_SWAP3 110 /* swap two characters in three */
509#define SCORE_REP 87 /* REP replacement */
510#define SCORE_SUBST 93 /* substitute a character */
511#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000512#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000513#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000514#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000515#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000516#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000517#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000518#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000519#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000520
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000521#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000522#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
523 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524
525#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000526#define SCORE_MAXMAX 999999 /* accept any score */
527
528/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529 * Structure to store info for word matching.
530 */
531typedef struct matchinf_S
532{
533 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000534
535 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000536 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000537 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000538 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000539 char_u *mi_cend; /* char after what was used for
540 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000541
542 /* case-folded text */
543 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000544 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000545
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000546 /* for when checking word after a prefix */
547 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000548 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000549 int mi_prefcnt; /* number of entries at mi_prefarridx */
550 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000551#ifdef FEAT_MBYTE
552 int mi_cprefixlen; /* byte length of prefix in original
553 case */
554#else
555# define mi_cprefixlen mi_prefixlen /* it's the same value */
556#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000557
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000558 /* for when checking a compound word */
559 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000560 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
561 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000562
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000563 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000564 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000565 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000566 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000567
568 /* for NOBREAK */
569 int mi_result2; /* "mi_resul" without following word */
570 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571} matchinf_T;
572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573/*
574 * The tables used for recognizing word characters according to spelling.
575 * These are only used for the first 256 characters of 'encoding'.
576 */
577typedef struct spelltab_S
578{
579 char_u st_isw[256]; /* flags: is word char */
580 char_u st_isu[256]; /* flags: is uppercase char */
581 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000582 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000583} spelltab_T;
584
585static spelltab_T spelltab;
586static int did_set_spelltab;
587
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000588#define CF_WORD 0x01
589#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590
591static void clear_spell_chartab __ARGS((spelltab_T *sp));
592static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000593static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
594static int spell_iswordp_nmw __ARGS((char_u *p));
595#ifdef FEAT_MBYTE
596static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
597#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000598static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000599
600/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000601 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000602 */
603typedef enum
604{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000605 STATE_START = 0, /* At start of node check for NUL bytes (goodword
606 * ends); if badword ends there is a match, otherwise
607 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000608 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000609 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000610 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
611 STATE_PLAIN, /* Use each byte of the node. */
612 STATE_DEL, /* Delete a byte from the bad word. */
613 STATE_INS, /* Insert a byte in the bad word. */
614 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000615 STATE_UNSWAP, /* Undo swap two characters. */
616 STATE_SWAP3, /* Swap two characters over three. */
617 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000619 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000620 STATE_REP_INI, /* Prepare for using REP items. */
621 STATE_REP, /* Use matching REP items from the .aff file. */
622 STATE_REP_UNDO, /* Undo a REP item replacement. */
623 STATE_FINAL /* End of this node. */
624} state_T;
625
626/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000627 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628 */
629typedef struct trystate_S
630{
Bram Moolenaarea424162005-06-16 21:51:00 +0000631 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000632 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000633 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000634 short ts_curi; /* index in list of child nodes */
635 char_u ts_fidx; /* index in fword[], case-folded bad word */
636 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
637 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000638 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000639 * PFD_PREFIXTREE or PFD_NOPREFIX */
640 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000641#ifdef FEAT_MBYTE
642 char_u ts_tcharlen; /* number of bytes in tword character */
643 char_u ts_tcharidx; /* current byte index in tword character */
644 char_u ts_isdiff; /* DIFF_ values */
645 char_u ts_fcharstart; /* index in fword where badword char started */
646#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000647 char_u ts_prewordlen; /* length of word in "preword[]" */
648 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000649 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000650 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000651 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000652 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653} trystate_T;
654
Bram Moolenaarea424162005-06-16 21:51:00 +0000655/* values for ts_isdiff */
656#define DIFF_NONE 0 /* no different byte (yet) */
657#define DIFF_YES 1 /* different byte found */
658#define DIFF_INSERT 2 /* inserting character */
659
Bram Moolenaard12a1322005-08-21 22:08:24 +0000660/* values for ts_flags */
661#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
662#define TSF_DIDSPLIT 2 /* tried split at this point */
663
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000664/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000665#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000666#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
667#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000668
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000669/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670#define FIND_FOLDWORD 0 /* find word case-folded */
671#define FIND_KEEPWORD 1 /* find keep-case word */
672#define FIND_PREFIX 2 /* find word after prefix */
673#define FIND_COMPOUND 3 /* find case-folded compound word */
674#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000675
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000676static slang_T *slang_alloc __ARGS((char_u *lang));
677static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000678static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000679static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000680static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000681static 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 +0000682static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000683static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000684static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000685static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000686static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000687static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000688static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000689static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000690static 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 +0000691static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000692static char_u *read_string __ARGS((FILE *fd, int cnt));
693static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
694static int read_charflags_section __ARGS((FILE *fd));
695static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
696static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
697static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
698static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
699static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000700static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000701static int init_syl_tab __ARGS((slang_T *slang));
702static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000703static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
704static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000705#ifdef FEAT_MBYTE
706static int *mb_str2wide __ARGS((char_u *s));
707#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708static 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 +0000709static void clear_midword __ARGS((buf_T *buf));
710static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000711static int find_region __ARGS((char_u *rp, char_u *region));
712static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000713static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000714static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000715static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000716static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000718static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000719static 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 +0000720#ifdef FEAT_EVAL
721static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
722#endif
723static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
724static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000726static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000728static void suggest_try_special __ARGS((suginfo_T *su));
729static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000730static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000731#ifdef FEAT_MBYTE
732static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
733#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000734static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000735static void score_comp_sal __ARGS((suginfo_T *su));
736static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000737static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000738static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000739static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000740static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000741static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000742static 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 +0000743static void add_banned __ARGS((suginfo_T *su, char_u *word));
744static int was_banned __ARGS((suginfo_T *su, char_u *word));
745static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000746static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000748static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
749static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
750static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000751#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000752static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000753#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000754static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000755static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000756static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
757static 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 +0000758
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000759/*
760 * Use our own character-case definitions, because the current locale may
761 * differ from what the .spl file uses.
762 * These must not be called with negative number!
763 */
764#ifndef FEAT_MBYTE
765/* Non-multi-byte implementation. */
766# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
767# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
768# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
769#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000770# if defined(HAVE_WCHAR_H)
771# include <wchar.h> /* for towupper() and towlower() */
772# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000773/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
774 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
775 * the "w" library function for characters above 255 if available. */
776# ifdef HAVE_TOWLOWER
777# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
778 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
779# else
780# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
781 : (c) < 256 ? spelltab.st_fold[c] : (c))
782# endif
783
784# ifdef HAVE_TOWUPPER
785# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
786 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
787# else
788# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
789 : (c) < 256 ? spelltab.st_upper[c] : (c))
790# endif
791
792# ifdef HAVE_ISWUPPER
793# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
794 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
795# else
796# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000797 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000798# endif
799#endif
800
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000801
802static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000803static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000804static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000805static char *e_affname = N_("Affix name too long in %s line %d: %s");
806static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
807static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000808static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000809
810/*
811 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000812 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000813 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
814 * or when it's OK it remains unchanged.
815 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000817 * "capcol" is used to check for a Capitalised word after the end of a
818 * sentence. If it's zero then perform the check. Return the column where to
819 * check next, or -1 when no sentence end was found. If it's NULL then don't
820 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000821 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000822 * Returns the length of the word in bytes, also when it's OK, so that the
823 * caller can skip over the word.
824 */
825 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000826spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000827 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000828 char_u *ptr;
829 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000830 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000831{
832 matchinf_T mi; /* Most things are put in "mi" so that it can
833 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000834 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000835 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000836 int wrongcaplen = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000837
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000838 /* A word never starts at a space or a control character. Return quickly
839 * then, skipping over the character. */
840 if (*ptr <= ' ')
841 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000842 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000843
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000844 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000845 * 0X99FF. But when a word character follows do check spelling to find
846 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000847 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000848 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000849 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
850 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000852 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000854 nrlen = mi.mi_end - ptr;
855 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000856 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000857 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000858
859 /* Try including the digits in the word. */
860 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000861 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000862 else
863 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000864
Bram Moolenaar0c405862005-06-22 22:26:26 +0000865 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000867 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000868 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000869 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000871 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000872 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000873
874 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
875 {
876 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000877 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000878 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000879 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000880 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000881 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000882 if (capcol != NULL)
883 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000884
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000885 /* We always use the characters up to the next non-word character,
886 * also for bad words. */
887 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000888
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000889 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000890 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000891
Bram Moolenaar5195e452005-08-19 20:32:47 +0000892 /* case-fold the word with one non-word character, so that we can check
893 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000894 if (*mi.mi_fend != NUL)
895 mb_ptr_adv(mi.mi_fend);
896
897 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
898 MAXWLEN + 1);
899 mi.mi_fwordlen = STRLEN(mi.mi_fword);
900
901 /* The word is bad unless we recognize it. */
902 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000903 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000904
905 /*
906 * Loop over the languages specified in 'spelllang'.
907 * We check them all, because a matching word may be longer than an
908 * already found matching word.
909 */
910 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
911 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
912 {
913 /* Check for a matching word in case-folded words. */
914 find_word(&mi, FIND_FOLDWORD);
915
916 /* Check for a matching word in keep-case words. */
917 find_word(&mi, FIND_KEEPWORD);
918
919 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000920 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000921
922 /* For a NOBREAK language, may want to use a word without a following
923 * word as a backup. */
924 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
925 && mi.mi_result2 != SP_BAD)
926 {
927 mi.mi_result = mi.mi_result2;
928 mi.mi_end = mi.mi_end2;
929 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930 }
931
932 if (mi.mi_result != SP_OK)
933 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000934 /* If we found a number skip over it. Allows for "42nd". Do flag
935 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000937 {
938 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
939 return nrlen;
940 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000941
942 /* When we are at a non-word character there is no error, just
943 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000944 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000945 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000946 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
947 {
948 regmatch_T regmatch;
949
950 /* Check for end of sentence. */
951 regmatch.regprog = wp->w_buffer->b_cap_prog;
952 regmatch.rm_ic = FALSE;
953 if (vim_regexec(&regmatch, ptr, 0))
954 *capcol = (int)(regmatch.endp[0] - ptr);
955 }
956
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000957#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000958 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000959 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000960#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000961 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000963 else if (mi.mi_end == ptr)
964 /* Always include at least one character. Required for when there
965 * is a mixup in "midword". */
966 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000967 else if (mi.mi_result == SP_BAD
968 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
969 {
970 char_u *p, *fp;
971 int save_result = mi.mi_result;
972
973 /* First language in 'spelllang' is NOBREAK. Find first position
974 * at which any word would be valid. */
975 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
976 p = mi.mi_word;
977 fp = mi.mi_fword;
978 for (;;)
979 {
980 mb_ptr_adv(p);
981 mb_ptr_adv(fp);
982 if (p >= mi.mi_end)
983 break;
984 mi.mi_compoff = fp - mi.mi_fword;
985 find_word(&mi, FIND_COMPOUND);
986 if (mi.mi_result != SP_BAD)
987 {
988 mi.mi_end = p;
989 break;
990 }
991 }
992 mi.mi_result = save_result;
993 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000994
995 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
996 *attrp = highlight_attr[HLF_SPB];
997 else if (mi.mi_result == SP_RARE)
998 *attrp = highlight_attr[HLF_SPR];
999 else
1000 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 }
1002
Bram Moolenaar5195e452005-08-19 20:32:47 +00001003 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1004 {
1005 /* Report SpellCap only when the word isn't badly spelled. */
1006 *attrp = highlight_attr[HLF_SPC];
1007 return wrongcaplen;
1008 }
1009
Bram Moolenaar51485f02005-06-04 21:55:20 +00001010 return (int)(mi.mi_end - ptr);
1011}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012
Bram Moolenaar51485f02005-06-04 21:55:20 +00001013/*
1014 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001015 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1016 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1017 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1018 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001019 *
1020 * For a match mip->mi_result is updated.
1021 */
1022 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001023find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001025 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001026{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001027 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001029 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001030 int endidxcnt = 0;
1031 int len;
1032 int wlen = 0;
1033 int flen;
1034 int c;
1035 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001036 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037#ifdef FEAT_MBYTE
1038 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001039#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001040 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001041 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 slang_T *slang = mip->mi_lp->lp_slang;
1043 unsigned flags;
1044 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001045 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001046 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001047 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001048 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001050 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001051 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 /* Check for word with matching case in keep-case tree. */
1053 ptr = mip->mi_word;
1054 flen = 9999; /* no case folding, always enough bytes */
1055 byts = slang->sl_kbyts;
1056 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001057
1058 if (mode == FIND_KEEPCOMPOUND)
1059 /* Skip over the previously found word(s). */
1060 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 }
1062 else
1063 {
1064 /* Check for case-folded in case-folded tree. */
1065 ptr = mip->mi_fword;
1066 flen = mip->mi_fwordlen; /* available case-folded bytes */
1067 byts = slang->sl_fbyts;
1068 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001069
1070 if (mode == FIND_PREFIX)
1071 {
1072 /* Skip over the prefix. */
1073 wlen = mip->mi_prefixlen;
1074 flen -= mip->mi_prefixlen;
1075 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001076 else if (mode == FIND_COMPOUND)
1077 {
1078 /* Skip over the previously found word(s). */
1079 wlen = mip->mi_compoff;
1080 flen -= mip->mi_compoff;
1081 }
1082
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001083 }
1084
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085 if (byts == NULL)
1086 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001087
Bram Moolenaar51485f02005-06-04 21:55:20 +00001088 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001089 * Repeat advancing in the tree until:
1090 * - there is a byte that doesn't match,
1091 * - we reach the end of the tree,
1092 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001093 */
1094 for (;;)
1095 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001096 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001097 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001098
1099 len = byts[arridx++];
1100
1101 /* If the first possible byte is a zero the word could end here.
1102 * Remember this index, we first check for the longest word. */
1103 if (byts[arridx] == 0)
1104 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001105 if (endidxcnt == MAXWLEN)
1106 {
1107 /* Must be a corrupted spell file. */
1108 EMSG(_(e_format));
1109 return;
1110 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001111 endlen[endidxcnt] = wlen;
1112 endidx[endidxcnt++] = arridx++;
1113 --len;
1114
1115 /* Skip over the zeros, there can be several flag/region
1116 * combinations. */
1117 while (len > 0 && byts[arridx] == 0)
1118 {
1119 ++arridx;
1120 --len;
1121 }
1122 if (len == 0)
1123 break; /* no children, word must end here */
1124 }
1125
1126 /* Stop looking at end of the line. */
1127 if (ptr[wlen] == NUL)
1128 break;
1129
1130 /* Perform a binary search in the list of accepted bytes. */
1131 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001132 if (c == TAB) /* <Tab> is handled like <Space> */
1133 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001134 lo = arridx;
1135 hi = arridx + len - 1;
1136 while (lo < hi)
1137 {
1138 m = (lo + hi) / 2;
1139 if (byts[m] > c)
1140 hi = m - 1;
1141 else if (byts[m] < c)
1142 lo = m + 1;
1143 else
1144 {
1145 lo = hi = m;
1146 break;
1147 }
1148 }
1149
1150 /* Stop if there is no matching byte. */
1151 if (hi < lo || byts[lo] != c)
1152 break;
1153
1154 /* Continue at the child (if there is one). */
1155 arridx = idxs[lo];
1156 ++wlen;
1157 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001158
1159 /* One space in the good word may stand for several spaces in the
1160 * checked word. */
1161 if (c == ' ')
1162 {
1163 for (;;)
1164 {
1165 if (flen <= 0 && *mip->mi_fend != NUL)
1166 flen = fold_more(mip);
1167 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1168 break;
1169 ++wlen;
1170 --flen;
1171 }
1172 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001173 }
1174
1175 /*
1176 * Verify that one of the possible endings is valid. Try the longest
1177 * first.
1178 */
1179 while (endidxcnt > 0)
1180 {
1181 --endidxcnt;
1182 arridx = endidx[endidxcnt];
1183 wlen = endlen[endidxcnt];
1184
1185#ifdef FEAT_MBYTE
1186 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1187 continue; /* not at first byte of character */
1188#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001189 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001190 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001191 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001192 continue; /* next char is a word character */
1193 word_ends = FALSE;
1194 }
1195 else
1196 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001197 /* The prefix flag is before compound flags. Once a valid prefix flag
1198 * has been found we try compound flags. */
1199 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001200
1201#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001202 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001203 {
1204 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001205 * when folding case. This can be slow, take a shortcut when the
1206 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001207 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001208 if (STRNCMP(ptr, p, wlen) != 0)
1209 {
1210 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1211 mb_ptr_adv(p);
1212 wlen = p - mip->mi_word;
1213 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 }
1215#endif
1216
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001217 /* Check flags and region. For FIND_PREFIX check the condition and
1218 * prefix ID.
1219 * Repeat this if there are more flags/region alternatives until there
1220 * is a match. */
1221 res = SP_BAD;
1222 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1223 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 {
1225 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001226
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001227 /* For the fold-case tree check that the case of the checked word
1228 * matches with what the word in the tree requires.
1229 * For keep-case tree the case is always right. For prefixes we
1230 * don't bother to check. */
1231 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001232 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 if (mip->mi_cend != mip->mi_word + wlen)
1234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001235 /* mi_capflags was set for a different word length, need
1236 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001238 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 }
1240
Bram Moolenaar0c405862005-06-22 22:26:26 +00001241 if (mip->mi_capflags == WF_KEEPCAP
1242 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001243 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 }
1245
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001246 /* When mode is FIND_PREFIX the word must support the prefix:
1247 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001248 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001249 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001251 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001252 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001253 mip->mi_word + mip->mi_cprefixlen, slang,
1254 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001255 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001256 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001257
1258 /* Use the WF_RARE flag for a rare prefix. */
1259 if (c & WF_RAREPFX)
1260 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001261 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001262 }
1263
Bram Moolenaar78622822005-08-23 21:00:13 +00001264 if (slang->sl_nobreak)
1265 {
1266 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1267 && (flags & WF_BANNED) == 0)
1268 {
1269 /* NOBREAK: found a valid following word. That's all we
1270 * need to know, so return. */
1271 mip->mi_result = SP_OK;
1272 break;
1273 }
1274 }
1275
1276 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1277 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001278 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001279 /* If there is no flag or the word is shorter than
1280 * COMPOUNDMIN reject it quickly.
1281 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001282 * that's too short... Myspell compatibility requires this
1283 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001284 if (((unsigned)flags >> 24) == 0
1285 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001286 continue;
1287
Bram Moolenaare52325c2005-08-22 22:54:29 +00001288 /* Limit the number of compound words to COMPOUNDMAX if no
1289 * maximum for syllables is specified. */
1290 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1291 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001292 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001293
Bram Moolenaard12a1322005-08-21 22:08:24 +00001294 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001295 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001296 ? slang->sl_compstartflags
1297 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001298 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001299 continue;
1300
Bram Moolenaare52325c2005-08-22 22:54:29 +00001301 if (mode == FIND_COMPOUND)
1302 {
1303 int capflags;
1304
1305 /* Need to check the caps type of the appended compound
1306 * word. */
1307#ifdef FEAT_MBYTE
1308 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1309 mip->mi_compoff) != 0)
1310 {
1311 /* case folding may have changed the length */
1312 p = mip->mi_word;
1313 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1314 mb_ptr_adv(p);
1315 }
1316 else
1317#endif
1318 p = mip->mi_word + mip->mi_compoff;
1319 capflags = captype(p, mip->mi_word + wlen);
1320 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1321 && (flags & WF_FIXCAP) != 0))
1322 continue;
1323
1324 if (capflags != WF_ALLCAP)
1325 {
1326 /* When the character before the word is a word
1327 * character we do not accept a Onecap word. We do
1328 * accept a no-caps word, even when the dictionary
1329 * word specifies ONECAP. */
1330 mb_ptr_back(mip->mi_word, p);
1331 if (spell_iswordp_nmw(p)
1332 ? capflags == WF_ONECAP
1333 : (flags & WF_ONECAP) != 0
1334 && capflags != WF_ONECAP)
1335 continue;
1336 }
1337 }
1338
Bram Moolenaar5195e452005-08-19 20:32:47 +00001339 /* If the word ends the sequence of compound flags of the
1340 * words must match with one of the COMPOUNDFLAGS items and
1341 * the number of syllables must not be too large. */
1342 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1343 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1344 if (word_ends)
1345 {
1346 char_u fword[MAXWLEN];
1347
1348 if (slang->sl_compsylmax < MAXWLEN)
1349 {
1350 /* "fword" is only needed for checking syllables. */
1351 if (ptr == mip->mi_word)
1352 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1353 else
1354 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1355 }
1356 if (!can_compound(slang, fword, mip->mi_compflags))
1357 continue;
1358 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001359 }
1360
Bram Moolenaar78622822005-08-23 21:00:13 +00001361 nobreak_result = SP_OK;
1362
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001363 if (!word_ends)
1364 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001365 int save_result = mip->mi_result;
1366 char_u *save_end = mip->mi_end;
1367
1368 /* Check that a valid word follows. If there is one and we
1369 * are compounding, it will set "mi_result", thus we are
1370 * always finished here. For NOBREAK we only check that a
1371 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001372 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001373 if (slang->sl_nobreak)
1374 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001375
1376 /* Find following word in case-folded tree. */
1377 mip->mi_compoff = endlen[endidxcnt];
1378#ifdef FEAT_MBYTE
1379 if (has_mbyte && mode == FIND_KEEPWORD)
1380 {
1381 /* Compute byte length in case-folded word from "wlen":
1382 * byte length in keep-case word. Length may change when
1383 * folding case. This can be slow, take a shortcut when
1384 * the case-folded word is equal to the keep-case word. */
1385 p = mip->mi_fword;
1386 if (STRNCMP(ptr, p, wlen) != 0)
1387 {
1388 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1389 mb_ptr_adv(p);
1390 mip->mi_compoff = p - mip->mi_fword;
1391 }
1392 }
1393#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001394 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001395 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001396 find_word(mip, FIND_COMPOUND);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 /* When NOBREAK any word that matches is OK. Otherwise we
1399 * need to find the longest match, thus try with keep-case and
1400 * prefix too. */
1401 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1402 {
1403 /* Find following word in keep-case tree. */
1404 mip->mi_compoff = wlen;
1405 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001406
Bram Moolenaar78622822005-08-23 21:00:13 +00001407 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1408 {
1409 /* Check for following word with prefix. */
1410 mip->mi_compoff = c;
1411 find_prefix(mip, FIND_COMPOUND);
1412 }
1413 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001414 --mip->mi_complen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001415
Bram Moolenaar78622822005-08-23 21:00:13 +00001416 if (slang->sl_nobreak)
1417 {
1418 nobreak_result = mip->mi_result;
1419 mip->mi_result = save_result;
1420 mip->mi_end = save_end;
1421 }
1422 else
1423 {
1424 if (mip->mi_result == SP_OK)
1425 break;
1426 continue;
1427 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001428 }
1429
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 if (flags & WF_BANNED)
1431 res = SP_BANNED;
1432 else if (flags & WF_REGION)
1433 {
1434 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001435 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001436 res = SP_OK;
1437 else
1438 res = SP_LOCAL;
1439 }
1440 else if (flags & WF_RARE)
1441 res = SP_RARE;
1442 else
1443 res = SP_OK;
1444
Bram Moolenaar78622822005-08-23 21:00:13 +00001445 /* Always use the longest match and the best result. For NOBREAK
1446 * we separately keep the longest match without a following good
1447 * word as a fall-back. */
1448 if (nobreak_result == SP_BAD)
1449 {
1450 if (mip->mi_result2 > res)
1451 {
1452 mip->mi_result2 = res;
1453 mip->mi_end2 = mip->mi_word + wlen;
1454 }
1455 else if (mip->mi_result2 == res
1456 && mip->mi_end2 < mip->mi_word + wlen)
1457 mip->mi_end2 = mip->mi_word + wlen;
1458 }
1459 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460 {
1461 mip->mi_result = res;
1462 mip->mi_end = mip->mi_word + wlen;
1463 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001464 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001465 mip->mi_end = mip->mi_word + wlen;
1466
Bram Moolenaar78622822005-08-23 21:00:13 +00001467 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001468 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469 }
1470
Bram Moolenaar78622822005-08-23 21:00:13 +00001471 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001472 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001473 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001474}
1475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001476/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001477 * Return TRUE if "flags" is a valid sequence of compound flags and
1478 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001479 */
1480 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001481can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001482 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001483 char_u *word;
1484 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001485{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001486 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001487#ifdef FEAT_MBYTE
1488 char_u uflags[MAXWLEN * 2];
1489 int i;
1490#endif
1491 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001492
1493 if (slang->sl_compprog == NULL)
1494 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001495#ifdef FEAT_MBYTE
1496 if (enc_utf8)
1497 {
1498 /* Need to convert the single byte flags to utf8 characters. */
1499 p = uflags;
1500 for (i = 0; flags[i] != NUL; ++i)
1501 p += mb_char2bytes(flags[i], p);
1502 *p = NUL;
1503 p = uflags;
1504 }
1505 else
1506#endif
1507 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001508 regmatch.regprog = slang->sl_compprog;
1509 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001510 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001511 return FALSE;
1512
Bram Moolenaare52325c2005-08-22 22:54:29 +00001513 /* Count the number of syllables. This may be slow, do it last. If there
1514 * are too many syllables AND the number of compound words is above
1515 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001516 if (slang->sl_compsylmax < MAXWLEN
1517 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001518 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001519 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001520}
1521
1522/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001523 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1524 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001525 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001526 */
1527 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001528valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001529 int totprefcnt; /* nr of prefix IDs */
1530 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001531 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001532 char_u *word;
1533 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001534 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001535{
1536 int prefcnt;
1537 int pidx;
1538 regprog_T *rp;
1539 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001540 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001541
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001542 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001543 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1544 {
1545 pidx = slang->sl_pidxs[arridx + prefcnt];
1546
1547 /* Check the prefix ID. */
1548 if (prefid != (pidx & 0xff))
1549 continue;
1550
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001551 /* Check if the prefix doesn't combine and the word already has a
1552 * suffix. */
1553 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1554 continue;
1555
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001556 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001557 * stored in the two bytes above the prefix ID byte. */
1558 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001559 if (rp != NULL)
1560 {
1561 regmatch.regprog = rp;
1562 regmatch.rm_ic = FALSE;
1563 if (!vim_regexec(&regmatch, word, 0))
1564 continue;
1565 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001566 else if (cond_req)
1567 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001568
Bram Moolenaar53805d12005-08-01 07:08:33 +00001569 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001570 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001571 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001572 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001573}
1574
1575/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001576 * Check if the word at "mip->mi_word" has a matching prefix.
1577 * If it does, then check the following word.
1578 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001579 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1580 * prefix in a compound word.
1581 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001582 * For a match mip->mi_result is updated.
1583 */
1584 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001585find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001586 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001587 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001588{
1589 idx_T arridx = 0;
1590 int len;
1591 int wlen = 0;
1592 int flen;
1593 int c;
1594 char_u *ptr;
1595 idx_T lo, hi, m;
1596 slang_T *slang = mip->mi_lp->lp_slang;
1597 char_u *byts;
1598 idx_T *idxs;
1599
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001600 byts = slang->sl_pbyts;
1601 if (byts == NULL)
1602 return; /* array is empty */
1603
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001604 /* We use the case-folded word here, since prefixes are always
1605 * case-folded. */
1606 ptr = mip->mi_fword;
1607 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001608 if (mode == FIND_COMPOUND)
1609 {
1610 /* Skip over the previously found word(s). */
1611 ptr += mip->mi_compoff;
1612 flen -= mip->mi_compoff;
1613 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001614 idxs = slang->sl_pidxs;
1615
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001616 /*
1617 * Repeat advancing in the tree until:
1618 * - there is a byte that doesn't match,
1619 * - we reach the end of the tree,
1620 * - or we reach the end of the line.
1621 */
1622 for (;;)
1623 {
1624 if (flen == 0 && *mip->mi_fend != NUL)
1625 flen = fold_more(mip);
1626
1627 len = byts[arridx++];
1628
1629 /* If the first possible byte is a zero the prefix could end here.
1630 * Check if the following word matches and supports the prefix. */
1631 if (byts[arridx] == 0)
1632 {
1633 /* There can be several prefixes with different conditions. We
1634 * try them all, since we don't know which one will give the
1635 * longest match. The word is the same each time, pass the list
1636 * of possible prefixes to find_word(). */
1637 mip->mi_prefarridx = arridx;
1638 mip->mi_prefcnt = len;
1639 while (len > 0 && byts[arridx] == 0)
1640 {
1641 ++arridx;
1642 --len;
1643 }
1644 mip->mi_prefcnt -= len;
1645
1646 /* Find the word that comes after the prefix. */
1647 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001648 if (mode == FIND_COMPOUND)
1649 /* Skip over the previously found word(s). */
1650 mip->mi_prefixlen += mip->mi_compoff;
1651
Bram Moolenaar53805d12005-08-01 07:08:33 +00001652#ifdef FEAT_MBYTE
1653 if (has_mbyte)
1654 {
1655 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001656 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1657 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001658 }
1659 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001660 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001661#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001662 find_word(mip, FIND_PREFIX);
1663
1664
1665 if (len == 0)
1666 break; /* no children, word must end here */
1667 }
1668
1669 /* Stop looking at end of the line. */
1670 if (ptr[wlen] == NUL)
1671 break;
1672
1673 /* Perform a binary search in the list of accepted bytes. */
1674 c = ptr[wlen];
1675 lo = arridx;
1676 hi = arridx + len - 1;
1677 while (lo < hi)
1678 {
1679 m = (lo + hi) / 2;
1680 if (byts[m] > c)
1681 hi = m - 1;
1682 else if (byts[m] < c)
1683 lo = m + 1;
1684 else
1685 {
1686 lo = hi = m;
1687 break;
1688 }
1689 }
1690
1691 /* Stop if there is no matching byte. */
1692 if (hi < lo || byts[lo] != c)
1693 break;
1694
1695 /* Continue at the child (if there is one). */
1696 arridx = idxs[lo];
1697 ++wlen;
1698 --flen;
1699 }
1700}
1701
1702/*
1703 * Need to fold at least one more character. Do until next non-word character
1704 * for efficiency.
1705 * Return the length of the folded chars in bytes.
1706 */
1707 static int
1708fold_more(mip)
1709 matchinf_T *mip;
1710{
1711 int flen;
1712 char_u *p;
1713
1714 p = mip->mi_fend;
1715 do
1716 {
1717 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001718 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001719
1720 /* Include the non-word character so that we can check for the
1721 * word end. */
1722 if (*mip->mi_fend != NUL)
1723 mb_ptr_adv(mip->mi_fend);
1724
1725 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1726 mip->mi_fword + mip->mi_fwordlen,
1727 MAXWLEN - mip->mi_fwordlen);
1728 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1729 mip->mi_fwordlen += flen;
1730 return flen;
1731}
1732
1733/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001734 * Check case flags for a word. Return TRUE if the word has the requested
1735 * case.
1736 */
1737 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001738spell_valid_case(wordflags, treeflags)
1739 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001740 int treeflags; /* flags for the word in the spell tree */
1741{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001742 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001743 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001744 && ((treeflags & WF_ONECAP) == 0
1745 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746}
1747
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001748/*
1749 * Return TRUE if spell checking is not enabled.
1750 */
1751 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001752no_spell_checking(wp)
1753 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001754{
Bram Moolenaar95529562005-08-25 21:21:38 +00001755 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001756 {
1757 EMSG(_("E756: Spell checking is not enabled"));
1758 return TRUE;
1759 }
1760 return FALSE;
1761}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001762
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001763/*
1764 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001765 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001766 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1767 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001768 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001769 */
1770 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001771spell_move_to(wp, dir, allwords, curline, attrp)
1772 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001773 int dir; /* FORWARD or BACKWARD */
1774 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001775 int curline;
Bram Moolenaar95529562005-08-25 21:21:38 +00001776 int *attrp; /* return: attributes of bad word or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001777{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001778 linenr_T lnum;
1779 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001780 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001781 char_u *line;
1782 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001783 char_u *endp;
1784 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001785 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001786 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001787 int col;
1788 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001789 char_u *buf = NULL;
1790 int buflen = 0;
1791 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001792 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001793
Bram Moolenaar95529562005-08-25 21:21:38 +00001794 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001795 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001796
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001797 /*
1798 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001799 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001800 *
1801 * When searching backwards, we continue in the line to find the last
1802 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001803 *
1804 * We concatenate the start of the next line, so that wrapped words work
1805 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1806 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001807 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001808 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001809 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001810
1811 while (!got_int)
1812 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001813 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001814
Bram Moolenaar0c405862005-06-22 22:26:26 +00001815 len = STRLEN(line);
1816 if (buflen < len + MAXWLEN + 2)
1817 {
1818 vim_free(buf);
1819 buflen = len + MAXWLEN + 2;
1820 buf = alloc(buflen);
1821 if (buf == NULL)
1822 break;
1823 }
1824
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001825 /* In first line check first word for Capital. */
1826 if (lnum == 1)
1827 capcol = 0;
1828
1829 /* For checking first word with a capital skip white space. */
1830 if (capcol == 0)
1831 capcol = skipwhite(line) - line;
1832
Bram Moolenaar0c405862005-06-22 22:26:26 +00001833 /* Copy the line into "buf" and append the start of the next line if
1834 * possible. */
1835 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001836 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001837 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1838
1839 p = buf + skip;
1840 endp = buf + len;
1841 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001842 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001843 /* When searching backward don't search after the cursor. */
1844 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001845 && lnum == wp->w_cursor.lnum
1846 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001847 break;
1848
1849 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001850 attr = 0;
Bram Moolenaar95529562005-08-25 21:21:38 +00001851 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001852
1853 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001854 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001855 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001856 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001858 /* When searching forward only accept a bad word after
1859 * the cursor. */
1860 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001861 || lnum > wp->w_cursor.lnum
1862 || (lnum == wp->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001863 && (colnr_T)(curline ? p - buf + len
1864 : p - buf)
Bram Moolenaar95529562005-08-25 21:21:38 +00001865 > wp->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001866 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001867 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001868 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001869 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001870 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001871 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001872 }
1873 else
1874 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001875
Bram Moolenaar51485f02005-06-04 21:55:20 +00001876 if (can_spell)
1877 {
1878 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001879 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001880#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001881 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001882#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001883 if (dir == FORWARD)
1884 {
1885 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001886 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001887 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001888 if (attrp != NULL)
1889 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001890 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001891 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001892 else if (curline)
1893 /* Insert mode completion: put cursor after
1894 * the bad word. */
1895 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001896 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001897 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001898 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001899 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001900 }
1901
Bram Moolenaar51485f02005-06-04 21:55:20 +00001902 /* advance to character after the word */
1903 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001904 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001905 }
1906
Bram Moolenaar5195e452005-08-19 20:32:47 +00001907 if (dir == BACKWARD && found_pos.lnum != 0)
1908 {
1909 /* Use the last match in the line. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001910 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001911 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001912 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001913 }
1914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001915 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001916 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001917
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001918 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001919 if (dir == BACKWARD)
1920 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001921 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001922 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001923 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001924 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001925 }
1926 else
1927 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001928 if (lnum == wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001929 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001930 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001931
1932 /* Skip the characters at the start of the next line that were
1933 * included in a match crossing line boundaries. */
1934 if (attr == 0)
1935 skip = p - endp;
1936 else
1937 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001938
1939 /* Capscol skips over the inserted space. */
1940 --capcol;
1941
1942 /* But after empty line check first word in next line */
1943 if (*skipwhite(line) == NUL)
1944 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001945 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001946
1947 line_breakcheck();
1948 }
1949
Bram Moolenaar0c405862005-06-22 22:26:26 +00001950 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001951 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001952}
1953
1954/*
1955 * For spell checking: concatenate the start of the following line "line" into
1956 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1957 */
1958 void
1959spell_cat_line(buf, line, maxlen)
1960 char_u *buf;
1961 char_u *line;
1962 int maxlen;
1963{
1964 char_u *p;
1965 int n;
1966
1967 p = skipwhite(line);
1968 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1969 p = skipwhite(p + 1);
1970
1971 if (*p != NUL)
1972 {
1973 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001974 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001975 n = p - line;
1976 if (n >= maxlen)
1977 n = maxlen - 1;
1978 vim_memset(buf + 1, ' ', n);
1979 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980}
1981
1982/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001983 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001984 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001985 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001986 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001987spell_load_lang(lang)
1988 char_u *lang;
1989{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001990 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001991 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001992 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001993
Bram Moolenaarb765d632005-06-07 21:00:02 +00001994 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001995 * It's truncated when an error is detected. */
1996 STRCPY(langcp, lang);
1997
Bram Moolenaarb765d632005-06-07 21:00:02 +00001998 /*
1999 * Find the first spell file for "lang" in 'runtimepath' and load it.
2000 */
2001 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2002 "spell/%s.%s.spl", lang, spell_enc());
2003 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002004
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002005 if (r == FAIL && *langcp != NUL)
2006 {
2007 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002008 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002009 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002010 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002011 }
2012
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002013 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002014 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2015 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002016 else if (*langcp != NUL)
2017 {
2018 /* Load all the additions. */
2019 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
2020 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
2021 }
2022}
2023
2024/*
2025 * Return the encoding used for spell checking: Use 'encoding', except that we
2026 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2027 */
2028 static char_u *
2029spell_enc()
2030{
2031
2032#ifdef FEAT_MBYTE
2033 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2034 return p_enc;
2035#endif
2036 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002037}
2038
2039/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002040 * Get the name of the .spl file for the internal wordlist into
2041 * "fname[MAXPATHL]".
2042 */
2043 static void
2044int_wordlist_spl(fname)
2045 char_u *fname;
2046{
2047 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2048 int_wordlist, spell_enc());
2049}
2050
2051/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002052 * Allocate a new slang_T.
2053 * Caller must fill "sl_next".
2054 */
2055 static slang_T *
2056slang_alloc(lang)
2057 char_u *lang;
2058{
2059 slang_T *lp;
2060
Bram Moolenaar51485f02005-06-04 21:55:20 +00002061 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062 if (lp != NULL)
2063 {
2064 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002065 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002066 lp->sl_compmax = MAXWLEN;
2067 lp->sl_compminlen = MAXWLEN;
2068 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002069 }
2070 return lp;
2071}
2072
2073/*
2074 * Free the contents of an slang_T and the structure itself.
2075 */
2076 static void
2077slang_free(lp)
2078 slang_T *lp;
2079{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002080 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002081 vim_free(lp->sl_fname);
2082 slang_clear(lp);
2083 vim_free(lp);
2084}
2085
2086/*
2087 * Clear an slang_T so that the file can be reloaded.
2088 */
2089 static void
2090slang_clear(lp)
2091 slang_T *lp;
2092{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002093 garray_T *gap;
2094 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002095 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002096 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002097
Bram Moolenaar51485f02005-06-04 21:55:20 +00002098 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002099 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002100 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002101 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002102 vim_free(lp->sl_pbyts);
2103 lp->sl_pbyts = NULL;
2104
Bram Moolenaar51485f02005-06-04 21:55:20 +00002105 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002106 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002107 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002108 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002109 vim_free(lp->sl_pidxs);
2110 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002111
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002112 gap = &lp->sl_rep;
2113 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002114 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002115 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2116 vim_free(ftp->ft_from);
2117 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002118 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002119 ga_clear(gap);
2120
2121 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002122 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002123 {
2124 /* "ga_len" is set to 1 without adding an item for latin1 */
2125 if (gap->ga_data != NULL)
2126 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2127 for (i = 0; i < gap->ga_len; ++i)
2128 vim_free(((int **)gap->ga_data)[i]);
2129 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002130 else
2131 /* SAL items: free salitem_T items */
2132 while (gap->ga_len > 0)
2133 {
2134 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2135 vim_free(smp->sm_lead);
2136 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2137 vim_free(smp->sm_to);
2138#ifdef FEAT_MBYTE
2139 vim_free(smp->sm_lead_w);
2140 vim_free(smp->sm_oneof_w);
2141 vim_free(smp->sm_to_w);
2142#endif
2143 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002144 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002145
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002146 for (i = 0; i < lp->sl_prefixcnt; ++i)
2147 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002148 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002149 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002150 lp->sl_prefprog = NULL;
2151
2152 vim_free(lp->sl_midword);
2153 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002154
Bram Moolenaar5195e452005-08-19 20:32:47 +00002155 vim_free(lp->sl_compprog);
2156 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002157 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002158 lp->sl_compprog = NULL;
2159 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002160 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002161
2162 vim_free(lp->sl_syllable);
2163 lp->sl_syllable = NULL;
2164 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002165
Bram Moolenaarea424162005-06-16 21:51:00 +00002166#ifdef FEAT_MBYTE
2167 {
2168 int todo = lp->sl_map_hash.ht_used;
2169 hashitem_T *hi;
2170
2171 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2172 if (!HASHITEM_EMPTY(hi))
2173 {
2174 --todo;
2175 vim_free(hi->hi_key);
2176 }
2177 }
2178 hash_clear(&lp->sl_map_hash);
2179#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002180
2181 lp->sl_compmax = MAXWLEN;
2182 lp->sl_compminlen = MAXWLEN;
2183 lp->sl_compsylmax = MAXWLEN;
2184 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185}
2186
2187/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002188 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002189 * Invoked through do_in_runtimepath().
2190 */
2191 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002192spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002193 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002194 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002195{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002197}
2198
2199/*
2200 * Load one spell file and store the info into a slang_T.
2201 *
2202 * This is invoked in two ways:
2203 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2204 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2205 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2206 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002207 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002208 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 static slang_T *
2210spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002211 char_u *fname;
2212 char_u *lang;
2213 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002214 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002215{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002216 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002217 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002218 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002219 char_u *bp;
2220 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002221 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002222 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002223 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002224 int round;
2225 char_u *save_sourcing_name = sourcing_name;
2226 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002227 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002228 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002229 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002230 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002231
Bram Moolenaarb765d632005-06-07 21:00:02 +00002232 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002233 if (fd == NULL)
2234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235 if (!silent)
2236 EMSG2(_(e_notopen), fname);
2237 else if (p_verbose > 2)
2238 {
2239 verbose_enter();
2240 smsg((char_u *)e_notopen, fname);
2241 verbose_leave();
2242 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002243 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002244 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002245 if (p_verbose > 2)
2246 {
2247 verbose_enter();
2248 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2249 verbose_leave();
2250 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002251
Bram Moolenaarb765d632005-06-07 21:00:02 +00002252 if (old_lp == NULL)
2253 {
2254 lp = slang_alloc(lang);
2255 if (lp == NULL)
2256 goto endFAIL;
2257
2258 /* Remember the file name, used to reload the file when it's updated. */
2259 lp->sl_fname = vim_strsave(fname);
2260 if (lp->sl_fname == NULL)
2261 goto endFAIL;
2262
2263 /* Check for .add.spl. */
2264 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2265 }
2266 else
2267 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002268
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002269 /* Set sourcing_name, so that error messages mention the file name. */
2270 sourcing_name = fname;
2271 sourcing_lnum = 0;
2272
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002273 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002274 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275 for (i = 0; i < VIMSPELLMAGICL; ++i)
2276 buf[i] = getc(fd); /* <fileID> */
2277 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2278 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002279 EMSG(_("E757: This does not look like a spell file"));
2280 goto endFAIL;
2281 }
2282 c = getc(fd); /* <versionnr> */
2283 if (c < VIMSPELLVERSION)
2284 {
2285 EMSG(_("E771: Old spell file, needs to be updated"));
2286 goto endFAIL;
2287 }
2288 else if (c > VIMSPELLVERSION)
2289 {
2290 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002291 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002292 }
2293
Bram Moolenaar5195e452005-08-19 20:32:47 +00002294
2295 /*
2296 * <SECTIONS>: <section> ... <sectionend>
2297 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2298 */
2299 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002300 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002301 n = getc(fd); /* <sectionID> or <sectionend> */
2302 if (n == SN_END)
2303 break;
2304 c = getc(fd); /* <sectionflags> */
2305 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2306 /* <sectionlen> */
2307 if (len < 0)
2308 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002309
Bram Moolenaar5195e452005-08-19 20:32:47 +00002310 res = 0;
2311 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002312 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002313 case SN_REGION:
2314 res = read_region_section(fd, lp, len);
2315 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002316
Bram Moolenaar5195e452005-08-19 20:32:47 +00002317 case SN_CHARFLAGS:
2318 res = read_charflags_section(fd);
2319 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002320
Bram Moolenaar5195e452005-08-19 20:32:47 +00002321 case SN_MIDWORD:
2322 lp->sl_midword = read_string(fd, len); /* <midword> */
2323 if (lp->sl_midword == NULL)
2324 goto endFAIL;
2325 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002326
Bram Moolenaar5195e452005-08-19 20:32:47 +00002327 case SN_PREFCOND:
2328 res = read_prefcond_section(fd, lp);
2329 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002330
Bram Moolenaar5195e452005-08-19 20:32:47 +00002331 case SN_REP:
2332 res = read_rep_section(fd, lp);
2333 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002334
Bram Moolenaar5195e452005-08-19 20:32:47 +00002335 case SN_SAL:
2336 res = read_sal_section(fd, lp);
2337 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002338
Bram Moolenaar5195e452005-08-19 20:32:47 +00002339 case SN_SOFO:
2340 res = read_sofo_section(fd, lp);
2341 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002342
Bram Moolenaar5195e452005-08-19 20:32:47 +00002343 case SN_MAP:
2344 p = read_string(fd, len); /* <mapstr> */
2345 if (p == NULL)
2346 goto endFAIL;
2347 set_map_str(lp, p);
2348 vim_free(p);
2349 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002350
Bram Moolenaar5195e452005-08-19 20:32:47 +00002351 case SN_COMPOUND:
2352 res = read_compound(fd, lp, len);
2353 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002354
Bram Moolenaar78622822005-08-23 21:00:13 +00002355 case SN_NOBREAK:
2356 lp->sl_nobreak = TRUE;
2357 break;
2358
Bram Moolenaar5195e452005-08-19 20:32:47 +00002359 case SN_SYLLABLE:
2360 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2361 if (lp->sl_syllable == NULL)
2362 goto endFAIL;
2363 if (init_syl_tab(lp) == FAIL)
2364 goto endFAIL;
2365 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002366
Bram Moolenaar5195e452005-08-19 20:32:47 +00002367 default:
2368 /* Unsupported section. When it's required give an error
2369 * message. When it's not required skip the contents. */
2370 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002371 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002372 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002373 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002374 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002375 while (--len >= 0)
2376 if (getc(fd) < 0)
2377 goto truncerr;
2378 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002379 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002380 if (res == SP_FORMERROR)
2381 {
2382formerr:
2383 EMSG(_(e_format));
2384 goto endFAIL;
2385 }
2386 if (res == SP_TRUNCERROR)
2387 {
2388truncerr:
2389 EMSG(_(e_spell_trunc));
2390 goto endFAIL;
2391 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002392 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002393 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002394 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395
Bram Moolenaar51485f02005-06-04 21:55:20 +00002396 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002397 * round 2: <KWORDTREE>
2398 * round 3: <PREFIXTREE> */
2399 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002401 /* The tree size was computed when writing the file, so that we can
2402 * allocate it as one long block. <nodecount> */
2403 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2404 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002406 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002407 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002408 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002409 bp = lalloc((long_u)len, TRUE);
2410 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002411 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002412 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002413 lp->sl_fbyts = bp;
2414 else if (round == 2)
2415 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002416 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002417 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002418
2419 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002420 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2421 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002422 goto endFAIL;
2423 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002424 lp->sl_fidxs = ip;
2425 else if (round == 2)
2426 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002427 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002428 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002429
2430 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002431 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002432 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002433 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002434 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002435 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002436 }
2437 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002438
Bram Moolenaarb765d632005-06-07 21:00:02 +00002439 /* For a new file link it in the list of spell files. */
2440 if (old_lp == NULL)
2441 {
2442 lp->sl_next = first_lang;
2443 first_lang = lp;
2444 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002445
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002446 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002447
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002448endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002449 if (lang != NULL)
2450 /* truncating the name signals the error to spell_load_lang() */
2451 *lang = NUL;
2452 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002454 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 lp = NULL;
2456 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002457
2458endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002459 if (fd != NULL)
2460 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002461 sourcing_name = save_sourcing_name;
2462 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463
2464 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465}
2466
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002467/*
2468 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002469 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002470 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002471 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2472 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002473 */
2474 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002475read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002476 FILE *fd;
2477 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002478 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002479{
2480 int cnt = 0;
2481 int i;
2482 char_u *str;
2483
2484 /* read the length bytes, MSB first */
2485 for (i = 0; i < cnt_bytes; ++i)
2486 cnt = (cnt << 8) + getc(fd);
2487 if (cnt < 0)
2488 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002489 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002490 return NULL;
2491 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002492 *cntp = cnt;
2493 if (cnt == 0)
2494 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002495
Bram Moolenaar5195e452005-08-19 20:32:47 +00002496 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002497 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002498 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002499 return str;
2500}
2501
Bram Moolenaar7887d882005-07-01 22:33:52 +00002502/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002503 * Read a string of length "cnt" from "fd" into allocated memory.
2504 * Returns NULL when out of memory.
2505 */
2506 static char_u *
2507read_string(fd, cnt)
2508 FILE *fd;
2509 int cnt;
2510{
2511 char_u *str;
2512 int i;
2513
2514 /* allocate memory */
2515 str = alloc((unsigned)cnt + 1);
2516 if (str != NULL)
2517 {
2518 /* Read the string. Doesn't check for truncated file. */
2519 for (i = 0; i < cnt; ++i)
2520 str[i] = getc(fd);
2521 str[i] = NUL;
2522 }
2523 return str;
2524}
2525
2526/*
2527 * Read SN_REGION: <regionname> ...
2528 * Return SP_*ERROR flags.
2529 */
2530 static int
2531read_region_section(fd, lp, len)
2532 FILE *fd;
2533 slang_T *lp;
2534 int len;
2535{
2536 int i;
2537
2538 if (len > 16)
2539 return SP_FORMERROR;
2540 for (i = 0; i < len; ++i)
2541 lp->sl_regions[i] = getc(fd); /* <regionname> */
2542 lp->sl_regions[len] = NUL;
2543 return 0;
2544}
2545
2546/*
2547 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2548 * <folcharslen> <folchars>
2549 * Return SP_*ERROR flags.
2550 */
2551 static int
2552read_charflags_section(fd)
2553 FILE *fd;
2554{
2555 char_u *flags;
2556 char_u *fol;
2557 int flagslen, follen;
2558
2559 /* <charflagslen> <charflags> */
2560 flags = read_cnt_string(fd, 1, &flagslen);
2561 if (flagslen < 0)
2562 return flagslen;
2563
2564 /* <folcharslen> <folchars> */
2565 fol = read_cnt_string(fd, 2, &follen);
2566 if (follen < 0)
2567 {
2568 vim_free(flags);
2569 return follen;
2570 }
2571
2572 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2573 if (flags != NULL && fol != NULL)
2574 set_spell_charflags(flags, flagslen, fol);
2575
2576 vim_free(flags);
2577 vim_free(fol);
2578
2579 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2580 if ((flags == NULL) != (fol == NULL))
2581 return SP_FORMERROR;
2582 return 0;
2583}
2584
2585/*
2586 * Read SN_PREFCOND section.
2587 * Return SP_*ERROR flags.
2588 */
2589 static int
2590read_prefcond_section(fd, lp)
2591 FILE *fd;
2592 slang_T *lp;
2593{
2594 int cnt;
2595 int i;
2596 int n;
2597 char_u *p;
2598 char_u buf[MAXWLEN + 1];
2599
2600 /* <prefcondcnt> <prefcond> ... */
2601 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2602 if (cnt <= 0)
2603 return SP_FORMERROR;
2604
2605 lp->sl_prefprog = (regprog_T **)alloc_clear(
2606 (unsigned)sizeof(regprog_T *) * cnt);
2607 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002608 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002609 lp->sl_prefixcnt = cnt;
2610
2611 for (i = 0; i < cnt; ++i)
2612 {
2613 /* <prefcond> : <condlen> <condstr> */
2614 n = getc(fd); /* <condlen> */
2615 if (n < 0 || n >= MAXWLEN)
2616 return SP_FORMERROR;
2617
2618 /* When <condlen> is zero we have an empty condition. Otherwise
2619 * compile the regexp program used to check for the condition. */
2620 if (n > 0)
2621 {
2622 buf[0] = '^'; /* always match at one position only */
2623 p = buf + 1;
2624 while (n-- > 0)
2625 *p++ = getc(fd); /* <condstr> */
2626 *p = NUL;
2627 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2628 }
2629 }
2630 return 0;
2631}
2632
2633/*
2634 * Read REP items section from "fd": <repcount> <rep> ...
2635 * Return SP_*ERROR flags.
2636 */
2637 static int
2638read_rep_section(fd, slang)
2639 FILE *fd;
2640 slang_T *slang;
2641{
2642 int cnt;
2643 garray_T *gap;
2644 fromto_T *ftp;
2645 short *first;
2646 int i;
2647
2648 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2649 if (cnt < 0)
2650 return SP_TRUNCERROR;
2651
2652 gap = &slang->sl_rep;
2653 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002654 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002655
2656 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2657 for (; gap->ga_len < cnt; ++gap->ga_len)
2658 {
2659 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2660 ftp->ft_from = read_cnt_string(fd, 1, &i);
2661 if (i < 0)
2662 return i;
2663 if (i == 0)
2664 return SP_FORMERROR;
2665 ftp->ft_to = read_cnt_string(fd, 1, &i);
2666 if (i <= 0)
2667 {
2668 vim_free(ftp->ft_from);
2669 if (i < 0)
2670 return i;
2671 return SP_FORMERROR;
2672 }
2673 }
2674
2675 /* Fill the first-index table. */
2676 first = slang->sl_rep_first;
2677 for (i = 0; i < 256; ++i)
2678 first[i] = -1;
2679 for (i = 0; i < gap->ga_len; ++i)
2680 {
2681 ftp = &((fromto_T *)gap->ga_data)[i];
2682 if (first[*ftp->ft_from] == -1)
2683 first[*ftp->ft_from] = i;
2684 }
2685 return 0;
2686}
2687
2688/*
2689 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2690 * Return SP_*ERROR flags.
2691 */
2692 static int
2693read_sal_section(fd, slang)
2694 FILE *fd;
2695 slang_T *slang;
2696{
2697 int i;
2698 int cnt;
2699 garray_T *gap;
2700 salitem_T *smp;
2701 int ccnt;
2702 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002703 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002704
2705 slang->sl_sofo = FALSE;
2706
2707 i = getc(fd); /* <salflags> */
2708 if (i & SAL_F0LLOWUP)
2709 slang->sl_followup = TRUE;
2710 if (i & SAL_COLLAPSE)
2711 slang->sl_collapse = TRUE;
2712 if (i & SAL_REM_ACCENTS)
2713 slang->sl_rem_accents = TRUE;
2714
2715 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2716 if (cnt < 0)
2717 return SP_TRUNCERROR;
2718
2719 gap = &slang->sl_sal;
2720 ga_init2(gap, sizeof(salitem_T), 10);
2721 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002722 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002723
2724 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2725 for (; gap->ga_len < cnt; ++gap->ga_len)
2726 {
2727 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2728 ccnt = getc(fd); /* <salfromlen> */
2729 if (ccnt < 0)
2730 return SP_TRUNCERROR;
2731 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002732 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002733 smp->sm_lead = p;
2734
2735 /* Read up to the first special char into sm_lead. */
2736 for (i = 0; i < ccnt; ++i)
2737 {
2738 c = getc(fd); /* <salfrom> */
2739 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2740 break;
2741 *p++ = c;
2742 }
2743 smp->sm_leadlen = p - smp->sm_lead;
2744 *p++ = NUL;
2745
2746 /* Put (abc) chars in sm_oneof, if any. */
2747 if (c == '(')
2748 {
2749 smp->sm_oneof = p;
2750 for (++i; i < ccnt; ++i)
2751 {
2752 c = getc(fd); /* <salfrom> */
2753 if (c == ')')
2754 break;
2755 *p++ = c;
2756 }
2757 *p++ = NUL;
2758 if (++i < ccnt)
2759 c = getc(fd);
2760 }
2761 else
2762 smp->sm_oneof = NULL;
2763
2764 /* Any following chars go in sm_rules. */
2765 smp->sm_rules = p;
2766 if (i < ccnt)
2767 /* store the char we got while checking for end of sm_lead */
2768 *p++ = c;
2769 for (++i; i < ccnt; ++i)
2770 *p++ = getc(fd); /* <salfrom> */
2771 *p++ = NUL;
2772
2773 /* <saltolen> <salto> */
2774 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2775 if (ccnt < 0)
2776 {
2777 vim_free(smp->sm_lead);
2778 return ccnt;
2779 }
2780
2781#ifdef FEAT_MBYTE
2782 if (has_mbyte)
2783 {
2784 /* convert the multi-byte strings to wide char strings */
2785 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2786 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2787 if (smp->sm_oneof == NULL)
2788 smp->sm_oneof_w = NULL;
2789 else
2790 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2791 if (smp->sm_to == NULL)
2792 smp->sm_to_w = NULL;
2793 else
2794 smp->sm_to_w = mb_str2wide(smp->sm_to);
2795 if (smp->sm_lead_w == NULL
2796 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2797 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2798 {
2799 vim_free(smp->sm_lead);
2800 vim_free(smp->sm_to);
2801 vim_free(smp->sm_lead_w);
2802 vim_free(smp->sm_oneof_w);
2803 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002804 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002805 }
2806 }
2807#endif
2808 }
2809
2810 /* Fill the first-index table. */
2811 set_sal_first(slang);
2812
2813 return 0;
2814}
2815
2816/*
2817 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2818 * Return SP_*ERROR flags.
2819 */
2820 static int
2821read_sofo_section(fd, slang)
2822 FILE *fd;
2823 slang_T *slang;
2824{
2825 int cnt;
2826 char_u *from, *to;
2827 int res;
2828
2829 slang->sl_sofo = TRUE;
2830
2831 /* <sofofromlen> <sofofrom> */
2832 from = read_cnt_string(fd, 2, &cnt);
2833 if (cnt < 0)
2834 return cnt;
2835
2836 /* <sofotolen> <sofoto> */
2837 to = read_cnt_string(fd, 2, &cnt);
2838 if (cnt < 0)
2839 {
2840 vim_free(from);
2841 return cnt;
2842 }
2843
2844 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2845 if (from != NULL && to != NULL)
2846 res = set_sofo(slang, from, to);
2847 else if (from != NULL || to != NULL)
2848 res = SP_FORMERROR; /* only one of two strings is an error */
2849 else
2850 res = 0;
2851
2852 vim_free(from);
2853 vim_free(to);
2854 return res;
2855}
2856
2857/*
2858 * Read the compound section from the .spl file:
2859 * <compmax> <compminlen> <compsylmax> <compflags>
2860 * Returns SP_*ERROR flags.
2861 */
2862 static int
2863read_compound(fd, slang, len)
2864 FILE *fd;
2865 slang_T *slang;
2866 int len;
2867{
2868 int todo = len;
2869 int c;
2870 int atstart;
2871 char_u *pat;
2872 char_u *pp;
2873 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002874 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002875
2876 if (todo < 2)
2877 return SP_FORMERROR; /* need at least two bytes */
2878
2879 --todo;
2880 c = getc(fd); /* <compmax> */
2881 if (c < 2)
2882 c = MAXWLEN;
2883 slang->sl_compmax = c;
2884
2885 --todo;
2886 c = getc(fd); /* <compminlen> */
2887 if (c < 1)
2888 c = 3;
2889 slang->sl_compminlen = c;
2890
2891 --todo;
2892 c = getc(fd); /* <compsylmax> */
2893 if (c < 1)
2894 c = MAXWLEN;
2895 slang->sl_compsylmax = c;
2896
2897 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2898 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002899 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
2900 * Conversion to utf-8 may double the size. */
2901 c = todo * 2 + 7;
2902#ifdef FEAT_MBYTE
2903 if (enc_utf8)
2904 c += todo * 2;
2905#endif
2906 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002907 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002908 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002909
Bram Moolenaard12a1322005-08-21 22:08:24 +00002910 /* We also need a list of all flags that can appear at the start and one
2911 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002912 cp = alloc(todo + 1);
2913 if (cp == NULL)
2914 {
2915 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002916 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002917 }
2918 slang->sl_compstartflags = cp;
2919 *cp = NUL;
2920
Bram Moolenaard12a1322005-08-21 22:08:24 +00002921 ap = alloc(todo + 1);
2922 if (ap == NULL)
2923 {
2924 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002925 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002926 }
2927 slang->sl_compallflags = ap;
2928 *ap = NUL;
2929
Bram Moolenaar5195e452005-08-19 20:32:47 +00002930 pp = pat;
2931 *pp++ = '^';
2932 *pp++ = '\\';
2933 *pp++ = '(';
2934
2935 atstart = 1;
2936 while (todo-- > 0)
2937 {
2938 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002939
2940 /* Add all flags to "sl_compallflags". */
2941 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00002942 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002943 {
2944 *ap++ = c;
2945 *ap = NUL;
2946 }
2947
Bram Moolenaar5195e452005-08-19 20:32:47 +00002948 if (atstart != 0)
2949 {
2950 /* At start of item: copy flags to "sl_compstartflags". For a
2951 * [abc] item set "atstart" to 2 and copy up to the ']'. */
2952 if (c == '[')
2953 atstart = 2;
2954 else if (c == ']')
2955 atstart = 0;
2956 else
2957 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00002958 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00002959 {
2960 *cp++ = c;
2961 *cp = NUL;
2962 }
2963 if (atstart == 1)
2964 atstart = 0;
2965 }
2966 }
2967 if (c == '/') /* slash separates two items */
2968 {
2969 *pp++ = '\\';
2970 *pp++ = '|';
2971 atstart = 1;
2972 }
2973 else /* normal char, "[abc]" and '*' are copied as-is */
2974 {
2975 if (c == '+')
2976 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00002977#ifdef FEAT_MBYTE
2978 if (enc_utf8)
2979 pp += mb_char2bytes(c, pp);
2980 else
2981#endif
2982 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002983 }
2984 }
2985
2986 *pp++ = '\\';
2987 *pp++ = ')';
2988 *pp++ = '$';
2989 *pp = NUL;
2990
2991 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
2992 vim_free(pat);
2993 if (slang->sl_compprog == NULL)
2994 return SP_FORMERROR;
2995
2996 return 0;
2997}
2998
Bram Moolenaar6de68532005-08-24 22:08:48 +00002999/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003000 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003001 * Like strchr() but independent of locale.
3002 */
3003 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003004byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003005 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003006 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003007{
3008 char_u *p;
3009
3010 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003011 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003012 return TRUE;
3013 return FALSE;
3014}
3015
Bram Moolenaar5195e452005-08-19 20:32:47 +00003016#define SY_MAXLEN 30
3017typedef struct syl_item_S
3018{
3019 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3020 int sy_len;
3021} syl_item_T;
3022
3023/*
3024 * Truncate "slang->sl_syllable" at the first slash and put the following items
3025 * in "slang->sl_syl_items".
3026 */
3027 static int
3028init_syl_tab(slang)
3029 slang_T *slang;
3030{
3031 char_u *p;
3032 char_u *s;
3033 int l;
3034 syl_item_T *syl;
3035
3036 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3037 p = vim_strchr(slang->sl_syllable, '/');
3038 while (p != NULL)
3039 {
3040 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003041 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003042 break;
3043 s = p;
3044 p = vim_strchr(p, '/');
3045 if (p == NULL)
3046 l = STRLEN(s);
3047 else
3048 l = p - s;
3049 if (l >= SY_MAXLEN)
3050 return SP_FORMERROR;
3051 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003052 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003053 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3054 + slang->sl_syl_items.ga_len++;
3055 vim_strncpy(syl->sy_chars, s, l);
3056 syl->sy_len = l;
3057 }
3058 return OK;
3059}
3060
3061/*
3062 * Count the number of syllables in "word".
3063 * When "word" contains spaces the syllables after the last space are counted.
3064 * Returns zero if syllables are not defines.
3065 */
3066 static int
3067count_syllables(slang, word)
3068 slang_T *slang;
3069 char_u *word;
3070{
3071 int cnt = 0;
3072 int skip = FALSE;
3073 char_u *p;
3074 int len;
3075 int i;
3076 syl_item_T *syl;
3077 int c;
3078
3079 if (slang->sl_syllable == NULL)
3080 return 0;
3081
3082 for (p = word; *p != NUL; p += len)
3083 {
3084 /* When running into a space reset counter. */
3085 if (*p == ' ')
3086 {
3087 len = 1;
3088 cnt = 0;
3089 continue;
3090 }
3091
3092 /* Find longest match of syllable items. */
3093 len = 0;
3094 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3095 {
3096 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3097 if (syl->sy_len > len
3098 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3099 len = syl->sy_len;
3100 }
3101 if (len != 0) /* found a match, count syllable */
3102 {
3103 ++cnt;
3104 skip = FALSE;
3105 }
3106 else
3107 {
3108 /* No recognized syllable item, at least a syllable char then? */
3109#ifdef FEAT_MBYTE
3110 c = mb_ptr2char(p);
3111 len = (*mb_ptr2len)(p);
3112#else
3113 c = *p;
3114 len = 1;
3115#endif
3116 if (vim_strchr(slang->sl_syllable, c) == NULL)
3117 skip = FALSE; /* No, search for next syllable */
3118 else if (!skip)
3119 {
3120 ++cnt; /* Yes, count it */
3121 skip = TRUE; /* don't count following syllable chars */
3122 }
3123 }
3124 }
3125 return cnt;
3126}
3127
3128/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003129 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003130 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003131 */
3132 static int
3133set_sofo(lp, from, to)
3134 slang_T *lp;
3135 char_u *from;
3136 char_u *to;
3137{
3138 int i;
3139
3140#ifdef FEAT_MBYTE
3141 garray_T *gap;
3142 char_u *s;
3143 char_u *p;
3144 int c;
3145 int *inp;
3146
3147 if (has_mbyte)
3148 {
3149 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3150 * characters. The index is the low byte of the character.
3151 * The list contains from-to pairs with a terminating NUL.
3152 * sl_sal_first[] is used for latin1 "from" characters. */
3153 gap = &lp->sl_sal;
3154 ga_init2(gap, sizeof(int *), 1);
3155 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003156 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003157 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3158 gap->ga_len = 256;
3159
3160 /* First count the number of items for each list. Temporarily use
3161 * sl_sal_first[] for this. */
3162 for (p = from, s = to; *p != NUL && *s != NUL; )
3163 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003164 c = mb_cptr2char_adv(&p);
3165 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003166 if (c >= 256)
3167 ++lp->sl_sal_first[c & 0xff];
3168 }
3169 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003170 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003171
3172 /* Allocate the lists. */
3173 for (i = 0; i < 256; ++i)
3174 if (lp->sl_sal_first[i] > 0)
3175 {
3176 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3177 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003178 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003179 ((int **)gap->ga_data)[i] = (int *)p;
3180 *(int *)p = 0;
3181 }
3182
3183 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3184 * list. */
3185 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3186 for (p = from, s = to; *p != NUL && *s != NUL; )
3187 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003188 c = mb_cptr2char_adv(&p);
3189 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003190 if (c >= 256)
3191 {
3192 /* Append the from-to chars at the end of the list with
3193 * the low byte. */
3194 inp = ((int **)gap->ga_data)[c & 0xff];
3195 while (*inp != 0)
3196 ++inp;
3197 *inp++ = c; /* from char */
3198 *inp++ = i; /* to char */
3199 *inp++ = NUL; /* NUL at the end */
3200 }
3201 else
3202 /* mapping byte to char is done in sl_sal_first[] */
3203 lp->sl_sal_first[c] = i;
3204 }
3205 }
3206 else
3207#endif
3208 {
3209 /* mapping bytes to bytes is done in sl_sal_first[] */
3210 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003211 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003212
3213 for (i = 0; to[i] != NUL; ++i)
3214 lp->sl_sal_first[from[i]] = to[i];
3215 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3216 }
3217
Bram Moolenaar5195e452005-08-19 20:32:47 +00003218 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003219}
3220
3221/*
3222 * Fill the first-index table for "lp".
3223 */
3224 static void
3225set_sal_first(lp)
3226 slang_T *lp;
3227{
3228 salfirst_T *sfirst;
3229 int i;
3230 salitem_T *smp;
3231 int c;
3232 garray_T *gap = &lp->sl_sal;
3233
3234 sfirst = lp->sl_sal_first;
3235 for (i = 0; i < 256; ++i)
3236 sfirst[i] = -1;
3237 smp = (salitem_T *)gap->ga_data;
3238 for (i = 0; i < gap->ga_len; ++i)
3239 {
3240#ifdef FEAT_MBYTE
3241 if (has_mbyte)
3242 /* Use the lowest byte of the first character. For latin1 it's
3243 * the character, for other encodings it should differ for most
3244 * characters. */
3245 c = *smp[i].sm_lead_w & 0xff;
3246 else
3247#endif
3248 c = *smp[i].sm_lead;
3249 if (sfirst[c] == -1)
3250 {
3251 sfirst[c] = i;
3252#ifdef FEAT_MBYTE
3253 if (has_mbyte)
3254 {
3255 int n;
3256
3257 /* Make sure all entries with this byte are following each
3258 * other. Move the ones that are in the wrong position. Do
3259 * keep the same ordering! */
3260 while (i + 1 < gap->ga_len
3261 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3262 /* Skip over entry with same index byte. */
3263 ++i;
3264
3265 for (n = 1; i + n < gap->ga_len; ++n)
3266 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3267 {
3268 salitem_T tsal;
3269
3270 /* Move entry with same index byte after the entries
3271 * we already found. */
3272 ++i;
3273 --n;
3274 tsal = smp[i + n];
3275 mch_memmove(smp + i + 1, smp + i,
3276 sizeof(salitem_T) * n);
3277 smp[i] = tsal;
3278 }
3279 }
3280#endif
3281 }
3282 }
3283}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003284
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003285#ifdef FEAT_MBYTE
3286/*
3287 * Turn a multi-byte string into a wide character string.
3288 * Return it in allocated memory (NULL for out-of-memory)
3289 */
3290 static int *
3291mb_str2wide(s)
3292 char_u *s;
3293{
3294 int *res;
3295 char_u *p;
3296 int i = 0;
3297
3298 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3299 if (res != NULL)
3300 {
3301 for (p = s; *p != NUL; )
3302 res[i++] = mb_ptr2char_adv(&p);
3303 res[i] = NUL;
3304 }
3305 return res;
3306}
3307#endif
3308
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003309/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003310 * Read one row of siblings from the spell file and store it in the byte array
3311 * "byts" and index array "idxs". Recursively read the children.
3312 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003313 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003314 *
3315 * Returns the index follosing the siblings.
3316 * Returns -1 if the file is shorter than expected.
3317 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003318 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003319 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003320read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003321 FILE *fd;
3322 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003323 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003324 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003325 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003326 int prefixtree; /* TRUE for reading PREFIXTREE */
3327 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003328{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003329 int len;
3330 int i;
3331 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003332 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003333 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003334 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003335#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003336
Bram Moolenaar51485f02005-06-04 21:55:20 +00003337 len = getc(fd); /* <siblingcount> */
3338 if (len <= 0)
3339 return -1;
3340
3341 if (startidx + len >= maxidx)
3342 return -2;
3343 byts[idx++] = len;
3344
3345 /* Read the byte values, flag/region bytes and shared indexes. */
3346 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003347 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003348 c = getc(fd); /* <byte> */
3349 if (c < 0)
3350 return -1;
3351 if (c <= BY_SPECIAL)
3352 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003353 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003354 {
3355 /* No flags, all regions. */
3356 idxs[idx] = 0;
3357 c = 0;
3358 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003359 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003360 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003361 if (prefixtree)
3362 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003363 /* Read the optional pflags byte, the prefix ID and the
3364 * condition nr. In idxs[] store the prefix ID in the low
3365 * byte, the condition index shifted up 8 bits, the flags
3366 * shifted up 24 bits. */
3367 if (c == BY_FLAGS)
3368 c = getc(fd) << 24; /* <pflags> */
3369 else
3370 c = 0;
3371
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003372 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003373
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003374 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3375 if (n >= maxprefcondnr)
3376 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003377 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003378 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003379 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003380 {
3381 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003382 * idxs[] the flags go in the low two bytes, region above
3383 * that and prefix ID above the region. */
3384 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003385 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003386 if (c2 == BY_FLAGS2)
3387 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003388 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003389 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003390 if (c & WF_AFX)
3391 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003392 }
3393
Bram Moolenaar51485f02005-06-04 21:55:20 +00003394 idxs[idx] = c;
3395 c = 0;
3396 }
3397 else /* c == BY_INDEX */
3398 {
3399 /* <nodeidx> */
3400 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3401 if (n < 0 || n >= maxidx)
3402 return -2;
3403 idxs[idx] = n + SHARED_MASK;
3404 c = getc(fd); /* <xbyte> */
3405 }
3406 }
3407 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003408 }
3409
Bram Moolenaar51485f02005-06-04 21:55:20 +00003410 /* Recursively read the children for non-shared siblings.
3411 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3412 * remove SHARED_MASK) */
3413 for (i = 1; i <= len; ++i)
3414 if (byts[startidx + i] != 0)
3415 {
3416 if (idxs[startidx + i] & SHARED_MASK)
3417 idxs[startidx + i] &= ~SHARED_MASK;
3418 else
3419 {
3420 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003421 idx = read_tree(fd, byts, idxs, maxidx, idx,
3422 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003423 if (idx < 0)
3424 break;
3425 }
3426 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003427
Bram Moolenaar51485f02005-06-04 21:55:20 +00003428 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003429}
3430
3431/*
3432 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003433 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003434 */
3435 char_u *
3436did_set_spelllang(buf)
3437 buf_T *buf;
3438{
3439 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003440 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003441 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003442 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003443 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003444 int region_mask;
3445 slang_T *lp;
3446 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003447 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003448 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003449 int len;
3450 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003451 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003452 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003453 char_u *use_region = NULL;
3454 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003455
3456 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003457 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003458
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003459 /* loop over comma separated language names. */
3460 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003461 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003462 /* Get one language name. */
3463 copy_option_part(&splp, lang, MAXWLEN, ",");
3464
Bram Moolenaar5482f332005-04-17 20:18:43 +00003465 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003466 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003467
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003468 /* If the name ends in ".spl" use it as the name of the spell file.
3469 * If there is a region name let "region" point to it and remove it
3470 * from the name. */
3471 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3472 {
3473 filename = TRUE;
3474
Bram Moolenaarb6356332005-07-18 21:40:44 +00003475 /* Locate a region and remove it from the file name. */
3476 p = vim_strchr(gettail(lang), '_');
3477 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3478 && !ASCII_ISALPHA(p[3]))
3479 {
3480 vim_strncpy(region_cp, p + 1, 2);
3481 mch_memmove(p, p + 3, len - (p - lang) - 2);
3482 len -= 3;
3483 region = region_cp;
3484 }
3485 else
3486 dont_use_region = TRUE;
3487
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003488 /* Check if we loaded this language before. */
3489 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3490 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3491 break;
3492 }
3493 else
3494 {
3495 filename = FALSE;
3496 if (len > 3 && lang[len - 3] == '_')
3497 {
3498 region = lang + len - 2;
3499 len -= 3;
3500 lang[len] = NUL;
3501 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003502 else
3503 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003504
3505 /* Check if we loaded this language before. */
3506 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3507 if (STRICMP(lang, lp->sl_name) == 0)
3508 break;
3509 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003510
Bram Moolenaarb6356332005-07-18 21:40:44 +00003511 if (region != NULL)
3512 {
3513 /* If the region differs from what was used before then don't
3514 * use it for 'spellfile'. */
3515 if (use_region != NULL && STRCMP(region, use_region) != 0)
3516 dont_use_region = TRUE;
3517 use_region = region;
3518 }
3519
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003520 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003521 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003522 {
3523 if (filename)
3524 (void)spell_load_file(lang, lang, NULL, FALSE);
3525 else
3526 spell_load_lang(lang);
3527 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003528
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003529 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003530 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003531 */
3532 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003533 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3534 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003535 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003536 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003537 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003538 {
3539 /* find region in sl_regions */
3540 c = find_region(lp->sl_regions, region);
3541 if (c == REGION_ALL)
3542 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003543 if (lp->sl_add)
3544 {
3545 if (*lp->sl_regions != NUL)
3546 /* This addition file is for other regions. */
3547 region_mask = 0;
3548 }
3549 else
3550 /* This is probably an error. Give a warning and
3551 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003552 smsg((char_u *)
3553 _("Warning: region %s not supported"),
3554 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003555 }
3556 else
3557 region_mask = 1 << c;
3558 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003559
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003560 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003561 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003562 if (ga_grow(&ga, 1) == FAIL)
3563 {
3564 ga_clear(&ga);
3565 return e_outofmem;
3566 }
3567 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3568 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3569 ++ga.ga_len;
3570 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003571 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003572 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573 }
3574
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003575 /* round 0: load int_wordlist, if possible.
3576 * round 1: load first name in 'spellfile'.
3577 * round 2: load second name in 'spellfile.
3578 * etc. */
3579 spf = curbuf->b_p_spf;
3580 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003582 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003583 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003584 /* Internal wordlist, if there is one. */
3585 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003586 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003587 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003588 }
3589 else
3590 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003591 /* One entry in 'spellfile'. */
3592 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3593 STRCAT(spf_name, ".spl");
3594
3595 /* If it was already found above then skip it. */
3596 for (c = 0; c < ga.ga_len; ++c)
3597 if (fullpathcmp(spf_name,
3598 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
3599 FALSE) == FPC_SAME)
3600 break;
3601 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003602 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003603 }
3604
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003605 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3607 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3608 break;
3609 if (lp == NULL)
3610 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003611 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003612 * region name, the region is ignored otherwise. for int_wordlist
3613 * use an arbitrary name. */
3614 if (round == 0)
3615 STRCPY(lang, "internal wordlist");
3616 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003617 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003618 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003619 p = vim_strchr(lang, '.');
3620 if (p != NULL)
3621 *p = NUL; /* truncate at ".encoding.add" */
3622 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003623 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 }
3625 if (lp != NULL && ga_grow(&ga, 1) == OK)
3626 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003627 region_mask = REGION_ALL;
3628 if (use_region != NULL && !dont_use_region)
3629 {
3630 /* find region in sl_regions */
3631 c = find_region(lp->sl_regions, use_region);
3632 if (c != REGION_ALL)
3633 region_mask = 1 << c;
3634 else if (*lp->sl_regions != NUL)
3635 /* This spell file is for other regions. */
3636 region_mask = 0;
3637 }
3638
3639 if (region_mask != 0)
3640 {
3641 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3642 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3643 ++ga.ga_len;
3644 use_midword(lp, buf);
3645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003646 }
3647 }
3648
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003649 /* Add a NULL entry to mark the end of the list. */
3650 if (ga_grow(&ga, 1) == FAIL)
3651 {
3652 ga_clear(&ga);
3653 return e_outofmem;
3654 }
3655 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
3656 ++ga.ga_len;
3657
3658 /* Everything is fine, store the new b_langp value. */
3659 ga_clear(&buf->b_langp);
3660 buf->b_langp = ga;
3661
3662 return NULL;
3663}
3664
3665/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003666 * Clear the midword characters for buffer "buf".
3667 */
3668 static void
3669clear_midword(buf)
3670 buf_T *buf;
3671{
3672 vim_memset(buf->b_spell_ismw, 0, 256);
3673#ifdef FEAT_MBYTE
3674 vim_free(buf->b_spell_ismw_mb);
3675 buf->b_spell_ismw_mb = NULL;
3676#endif
3677}
3678
3679/*
3680 * Use the "sl_midword" field of language "lp" for buffer "buf".
3681 * They add up to any currently used midword characters.
3682 */
3683 static void
3684use_midword(lp, buf)
3685 slang_T *lp;
3686 buf_T *buf;
3687{
3688 char_u *p;
3689
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003690 if (lp->sl_midword == NULL) /* there aren't any */
3691 return;
3692
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003693 for (p = lp->sl_midword; *p != NUL; )
3694#ifdef FEAT_MBYTE
3695 if (has_mbyte)
3696 {
3697 int c, l, n;
3698 char_u *bp;
3699
3700 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003701 l = (*mb_ptr2len)(p);
3702 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003703 buf->b_spell_ismw[c] = TRUE;
3704 else if (buf->b_spell_ismw_mb == NULL)
3705 /* First multi-byte char in "b_spell_ismw_mb". */
3706 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3707 else
3708 {
3709 /* Append multi-byte chars to "b_spell_ismw_mb". */
3710 n = STRLEN(buf->b_spell_ismw_mb);
3711 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3712 if (bp != NULL)
3713 {
3714 vim_free(buf->b_spell_ismw_mb);
3715 buf->b_spell_ismw_mb = bp;
3716 vim_strncpy(bp + n, p, l);
3717 }
3718 }
3719 p += l;
3720 }
3721 else
3722#endif
3723 buf->b_spell_ismw[*p++] = TRUE;
3724}
3725
3726/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003727 * Find the region "region[2]" in "rp" (points to "sl_regions").
3728 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003729 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003730 */
3731 static int
3732find_region(rp, region)
3733 char_u *rp;
3734 char_u *region;
3735{
3736 int i;
3737
3738 for (i = 0; ; i += 2)
3739 {
3740 if (rp[i] == NUL)
3741 return REGION_ALL;
3742 if (rp[i] == region[0] && rp[i + 1] == region[1])
3743 break;
3744 }
3745 return i / 2;
3746}
3747
3748/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003750 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003751 * Word WF_ONECAP
3752 * W WORD WF_ALLCAP
3753 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754 */
3755 static int
3756captype(word, end)
3757 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003758 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003759{
3760 char_u *p;
3761 int c;
3762 int firstcap;
3763 int allcap;
3764 int past_second = FALSE; /* past second word char */
3765
3766 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003767 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003768 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003769 return 0; /* only non-word characters, illegal word */
3770#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003771 if (has_mbyte)
3772 c = mb_ptr2char_adv(&p);
3773 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003774#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003775 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003776 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003777
3778 /*
3779 * Need to check all letters to find a word with mixed upper/lower.
3780 * But a word with an upper char only at start is a ONECAP.
3781 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003783 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003784 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003785 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003786 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003787 {
3788 /* UUl -> KEEPCAP */
3789 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003790 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003791 allcap = FALSE;
3792 }
3793 else if (!allcap)
3794 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003795 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003796 past_second = TRUE;
3797 }
3798
3799 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003800 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003801 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003802 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003803 return 0;
3804}
3805
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003806/*
3807 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3808 * capital. So that make_case_word() can turn WOrd into Word.
3809 * Add ALLCAP for "WOrD".
3810 */
3811 static int
3812badword_captype(word, end)
3813 char_u *word;
3814 char_u *end;
3815{
3816 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003817 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003818 int l, u;
3819 int first;
3820 char_u *p;
3821
3822 if (flags & WF_KEEPCAP)
3823 {
3824 /* Count the number of UPPER and lower case letters. */
3825 l = u = 0;
3826 first = FALSE;
3827 for (p = word; p < end; mb_ptr_adv(p))
3828 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003829 c = PTR2CHAR(p);
3830 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831 {
3832 ++u;
3833 if (p == word)
3834 first = TRUE;
3835 }
3836 else
3837 ++l;
3838 }
3839
3840 /* If there are more UPPER than lower case letters suggest an
3841 * ALLCAP word. Otherwise, if the first letter is UPPER then
3842 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3843 * require three upper case letters. */
3844 if (u > l && u > 2)
3845 flags |= WF_ALLCAP;
3846 else if (first)
3847 flags |= WF_ONECAP;
3848 }
3849 return flags;
3850}
3851
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003852# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3853/*
3854 * Free all languages.
3855 */
3856 void
3857spell_free_all()
3858{
3859 slang_T *lp;
3860 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003861 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003862
3863 /* Go through all buffers and handle 'spelllang'. */
3864 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3865 ga_clear(&buf->b_langp);
3866
3867 while (first_lang != NULL)
3868 {
3869 lp = first_lang;
3870 first_lang = lp->sl_next;
3871 slang_free(lp);
3872 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003873
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003874 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003875 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003876 /* Delete the internal wordlist and its .spl file */
3877 mch_remove(int_wordlist);
3878 int_wordlist_spl(fname);
3879 mch_remove(fname);
3880 vim_free(int_wordlist);
3881 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003882 }
3883
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003884 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003885}
3886# endif
3887
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003888# if defined(FEAT_MBYTE) || defined(PROTO)
3889/*
3890 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003891 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892 */
3893 void
3894spell_reload()
3895{
3896 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003897 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003898
Bram Moolenaarea408852005-06-25 22:49:46 +00003899 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003900 init_spell_chartab();
3901
3902 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003903 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003904
3905 /* Go through all buffers and handle 'spelllang'. */
3906 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3907 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003908 /* Only load the wordlists when 'spelllang' is set and there is a
3909 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003911 {
3912 FOR_ALL_WINDOWS(wp)
3913 if (wp->w_buffer == buf && wp->w_p_spell)
3914 {
3915 (void)did_set_spelllang(buf);
3916# ifdef FEAT_WINDOWS
3917 break;
3918# endif
3919 }
3920 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003921 }
3922}
3923# endif
3924
Bram Moolenaarb765d632005-06-07 21:00:02 +00003925/*
3926 * Reload the spell file "fname" if it's loaded.
3927 */
3928 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003929spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003930 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003931 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003932{
3933 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003934 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003935
Bram Moolenaarb765d632005-06-07 21:00:02 +00003936 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3937 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3938 {
3939 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003940 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003941 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003942 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944
3945 /* When "zg" was used and the file wasn't loaded yet, should redo
3946 * 'spelllang' to get it loaded. */
3947 if (added_word && !didit)
3948 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003949}
3950
3951
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952/*
3953 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003954 */
3955
Bram Moolenaar51485f02005-06-04 21:55:20 +00003956#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003957 and .dic file. */
3958/*
3959 * Main structure to store the contents of a ".aff" file.
3960 */
3961typedef struct afffile_S
3962{
3963 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00003964 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003965 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003966 unsigned af_rar; /* RAR ID for rare word */
3967 unsigned af_kep; /* KEP ID for keep-case word */
3968 unsigned af_bad; /* BAD ID for banned word */
3969 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003970 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3972 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003973 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003974} afffile_T;
3975
Bram Moolenaar6de68532005-08-24 22:08:48 +00003976#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00003977#define AFT_LONG 1 /* flags are two characters */
3978#define AFT_CAPLONG 2 /* flags are one or two characters */
3979#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003980
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003981typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003982/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3983struct affentry_S
3984{
3985 affentry_T *ae_next; /* next affix with same name/number */
3986 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3987 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003988 char_u *ae_cond; /* condition (NULL for ".") */
3989 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003990 char_u ae_rare; /* rare affix */
3991 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003992};
3993
Bram Moolenaar6de68532005-08-24 22:08:48 +00003994#ifdef FEAT_MBYTE
3995# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
3996#else
Bram Moolenaar95529562005-08-25 21:21:38 +00003997# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003998#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00003999
Bram Moolenaar51485f02005-06-04 21:55:20 +00004000/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4001typedef struct affheader_S
4002{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004003 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4004 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4005 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004006 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004007 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004008 affentry_T *ah_first; /* first affix entry */
4009} affheader_T;
4010
4011#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4012
Bram Moolenaar6de68532005-08-24 22:08:48 +00004013/* Flag used in compound items. */
4014typedef struct compitem_S
4015{
4016 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4017 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4018 int ci_newID; /* affix ID after renumbering. */
4019} compitem_T;
4020
4021#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4022
Bram Moolenaar51485f02005-06-04 21:55:20 +00004023/*
4024 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004025 * the need to keep track of each allocated thing, everything is freed all at
4026 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004027 */
4028#define SBLOCKSIZE 16000 /* size of sb_data */
4029typedef struct sblock_S sblock_T;
4030struct sblock_S
4031{
4032 sblock_T *sb_next; /* next block in list */
4033 int sb_used; /* nr of bytes already in use */
4034 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004035};
4036
4037/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004039 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004040typedef struct wordnode_S wordnode_T;
4041struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004042{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004043 union /* shared to save space */
4044 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004045 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004046 int index; /* index in written nodes (valid after first
4047 round) */
4048 } wn_u1;
4049 union /* shared to save space */
4050 {
4051 wordnode_T *next; /* next node with same hash key */
4052 wordnode_T *wnode; /* parent node that will write this node */
4053 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004054 wordnode_T *wn_child; /* child (next byte in word) */
4055 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4056 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004057 int wn_refs; /* Nr. of references to this node. Only
4058 relevant for first node in a list of
4059 siblings, in following siblings it is
4060 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004061 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004062 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004063 prefix ID or 0 */
4064 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4065 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004066 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004067#ifdef SPELL_PRINTTREE
4068 int wn_nr; /* sequence nr for printing */
4069#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004070};
4071
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004072#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4073
Bram Moolenaar51485f02005-06-04 21:55:20 +00004074#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075
Bram Moolenaar51485f02005-06-04 21:55:20 +00004076/*
4077 * Info used while reading the spell files.
4078 */
4079typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004080{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004081 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004082 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004083
Bram Moolenaar51485f02005-06-04 21:55:20 +00004084 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004085 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004086
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004087 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004088
Bram Moolenaar51485f02005-06-04 21:55:20 +00004089 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004090 long si_blocks_cnt; /* memory blocks allocated */
4091 long si_compress_cnt; /* words to add before lowering
4092 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004093 wordnode_T *si_first_free; /* List of nodes that have been freed during
4094 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004095 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004096#ifdef SPELL_PRINTTREE
4097 int si_wordnode_nr; /* sequence nr for nodes */
4098#endif
4099
4100
Bram Moolenaar51485f02005-06-04 21:55:20 +00004101 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004102 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004103 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004104 int si_region; /* region mask */
4105 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004106 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004107 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004108 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004109 int si_region_count; /* number of regions supported (1 when there
4110 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004111 char_u si_region_name[16]; /* region names; used only if
4112 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113
4114 garray_T si_rep; /* list of fromto_T entries from REP lines */
4115 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004116 char_u *si_sofofr; /* SOFOFROM text */
4117 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004118 int si_followup; /* soundsalike: ? */
4119 int si_collapse; /* soundsalike: ? */
4120 int si_rem_accents; /* soundsalike: remove accents */
4121 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004122 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004123 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004124 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004125 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004126 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004127 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004128 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004129 garray_T si_prefcond; /* table with conditions for postponed
4130 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004131 int si_newprefID; /* current value for ah_newID */
4132 int si_compID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004133} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004134
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004135static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004136static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4137static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4138static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
4139static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4140static void aff_check_number __ARGS((int spinval, int affval, char *name));
4141static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004142static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4144static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004145static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004146static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004147static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004148static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004149static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004150static 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 +00004151static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4152static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4153static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004154static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004155static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004156static 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 +00004157static 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 +00004158static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4159static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4160static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4161static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4162static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004163static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004164static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004165static void clear_node __ARGS((wordnode_T *node));
4166static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004168static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004169
Bram Moolenaar53805d12005-08-01 07:08:33 +00004170/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4171 * but it must be negative to indicate the prefix tree to tree_add_word().
4172 * Use a negative number with the lower 8 bits zero. */
4173#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004174
Bram Moolenaar5195e452005-08-19 20:32:47 +00004175/*
4176 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4177 */
4178static long compress_start = 30000; /* memory / SBLOCKSIZE */
4179static long compress_inc = 100; /* memory / SBLOCKSIZE */
4180static long compress_added = 500000; /* word count */
4181
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004182#ifdef SPELL_PRINTTREE
4183/*
4184 * For debugging the tree code: print the current tree in a (more or less)
4185 * readable format, so that we can see what happens when adding a word and/or
4186 * compressing the tree.
4187 * Based on code from Olaf Seibert.
4188 */
4189#define PRINTLINESIZE 1000
4190#define PRINTWIDTH 6
4191
4192#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4193 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4194
4195static char line1[PRINTLINESIZE];
4196static char line2[PRINTLINESIZE];
4197static char line3[PRINTLINESIZE];
4198
4199 static void
4200spell_clear_flags(wordnode_T *node)
4201{
4202 wordnode_T *np;
4203
4204 for (np = node; np != NULL; np = np->wn_sibling)
4205 {
4206 np->wn_u1.index = FALSE;
4207 spell_clear_flags(np->wn_child);
4208 }
4209}
4210
4211 static void
4212spell_print_node(wordnode_T *node, int depth)
4213{
4214 if (node->wn_u1.index)
4215 {
4216 /* Done this node before, print the reference. */
4217 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4218 PRINTSOME(line2, depth, " ", 0, 0);
4219 PRINTSOME(line3, depth, " ", 0, 0);
4220 msg(line1);
4221 msg(line2);
4222 msg(line3);
4223 }
4224 else
4225 {
4226 node->wn_u1.index = TRUE;
4227
4228 if (node->wn_byte != NUL)
4229 {
4230 if (node->wn_child != NULL)
4231 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4232 else
4233 /* Cannot happen? */
4234 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4235 }
4236 else
4237 PRINTSOME(line1, depth, " $ ", 0, 0);
4238
4239 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4240
4241 if (node->wn_sibling != NULL)
4242 PRINTSOME(line3, depth, " | ", 0, 0);
4243 else
4244 PRINTSOME(line3, depth, " ", 0, 0);
4245
4246 if (node->wn_byte == NUL)
4247 {
4248 msg(line1);
4249 msg(line2);
4250 msg(line3);
4251 }
4252
4253 /* do the children */
4254 if (node->wn_byte != NUL && node->wn_child != NULL)
4255 spell_print_node(node->wn_child, depth + 1);
4256
4257 /* do the siblings */
4258 if (node->wn_sibling != NULL)
4259 {
4260 /* get rid of all parent details except | */
4261 STRCPY(line1, line3);
4262 STRCPY(line2, line3);
4263 spell_print_node(node->wn_sibling, depth);
4264 }
4265 }
4266}
4267
4268 static void
4269spell_print_tree(wordnode_T *root)
4270{
4271 if (root != NULL)
4272 {
4273 /* Clear the "wn_u1.index" fields, used to remember what has been
4274 * done. */
4275 spell_clear_flags(root);
4276
4277 /* Recursively print the tree. */
4278 spell_print_node(root, 0);
4279 }
4280}
4281#endif /* SPELL_PRINTTREE */
4282
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004283/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004285 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004286 */
4287 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004288spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004289 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004290 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291{
4292 FILE *fd;
4293 afffile_T *aff;
4294 char_u rline[MAXLINELEN];
4295 char_u *line;
4296 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004297#define MAXITEMCNT 7
4298 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004299 int itemcnt;
4300 char_u *p;
4301 int lnum = 0;
4302 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004303 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004304 int aff_todo = 0;
4305 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004306 char_u *low = NULL;
4307 char_u *fol = NULL;
4308 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 int do_rep;
4310 int do_sal;
4311 int do_map;
4312 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004313 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004314 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004315 int compminlen = 0; /* COMPOUNDMIN value */
4316 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4317 int compmax = 0; /* COMPOUNDMAX value */
4318 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4319 concatenated */
4320 char_u *midword = NULL; /* MIDWORD value */
4321 char_u *syllable = NULL; /* SYLLABLE value */
4322 char_u *sofofrom = NULL; /* SOFOFROM value */
4323 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004324
Bram Moolenaar51485f02005-06-04 21:55:20 +00004325 /*
4326 * Open the file.
4327 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004328 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329 if (fd == NULL)
4330 {
4331 EMSG2(_(e_notopen), fname);
4332 return NULL;
4333 }
4334
Bram Moolenaarb765d632005-06-07 21:00:02 +00004335 if (spin->si_verbose || p_verbose > 2)
4336 {
4337 if (!spin->si_verbose)
4338 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004339 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004340 out_flush();
4341 if (!spin->si_verbose)
4342 verbose_leave();
4343 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 /* Only do REP lines when not done in another .aff file already. */
4346 do_rep = spin->si_rep.ga_len == 0;
4347
4348 /* Only do SAL lines when not done in another .aff file already. */
4349 do_sal = spin->si_sal.ga_len == 0;
4350
4351 /* Only do MAP lines when not done in another .aff file already. */
4352 do_map = spin->si_map.ga_len == 0;
4353
Bram Moolenaar51485f02005-06-04 21:55:20 +00004354 /*
4355 * Allocate and init the afffile_T structure.
4356 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004357 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004358 if (aff == NULL)
4359 return NULL;
4360 hash_init(&aff->af_pref);
4361 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004362 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004363
4364 /*
4365 * Read all the lines in the file one by one.
4366 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004367 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004368 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004369 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004370 ++lnum;
4371
4372 /* Skip comment lines. */
4373 if (*rline == '#')
4374 continue;
4375
4376 /* Convert from "SET" to 'encoding' when needed. */
4377 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004378#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004379 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004380 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004381 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004382 if (pc == NULL)
4383 {
4384 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4385 fname, lnum, rline);
4386 continue;
4387 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004388 line = pc;
4389 }
4390 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004391#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004392 {
4393 pc = NULL;
4394 line = rline;
4395 }
4396
4397 /* Split the line up in white separated items. Put a NUL after each
4398 * item. */
4399 itemcnt = 0;
4400 for (p = line; ; )
4401 {
4402 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4403 ++p;
4404 if (*p == NUL)
4405 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004406 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004407 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004408 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004409 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004410 ++p;
4411 if (*p == NUL)
4412 break;
4413 *p++ = NUL;
4414 }
4415
4416 /* Handle non-empty lines. */
4417 if (itemcnt > 0)
4418 {
4419 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4420 && aff->af_enc == NULL)
4421 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004422#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004423 /* Setup for conversion from "ENC" to 'encoding'. */
4424 aff->af_enc = enc_canonize(items[1]);
4425 if (aff->af_enc != NULL && !spin->si_ascii
4426 && convert_setup(&spin->si_conv, aff->af_enc,
4427 p_enc) == FAIL)
4428 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4429 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004430 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004431#else
4432 smsg((char_u *)_("Conversion in %s not supported"), fname);
4433#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004434 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004435 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4436 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004437 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004438 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004439 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004440 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004441 aff->af_flagtype = AFT_NUM;
4442 else if (STRCMP(items[1], "caplong") == 0)
4443 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004444 else
4445 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4446 fname, lnum, items[1]);
4447 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
4448 || aff->af_needaffix != 0 || compflags != NULL
4449 || aff->af_suff.ht_used > 0
4450 || aff->af_pref.ht_used > 0)
4451 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4452 fname, lnum, items[1]);
4453 }
4454 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4455 && midword == NULL)
4456 {
4457 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004458 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004459 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004461 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004462 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004463 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004466 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004467 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4468 && aff->af_slash == 0)
4469 {
4470 aff->af_slash = items[1][0];
4471 if (items[1][1] != NUL)
4472 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4473 fname, lnum, items[1]);
4474 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004475 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4476 && aff->af_rar == 0)
4477 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004478 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4479 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004480 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004481 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4482 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004483 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004484 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4485 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004486 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004487 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4488 && aff->af_bad == 0)
4489 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004490 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4491 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004492 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004493 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4494 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004495 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004496 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4497 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004498 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004499 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004500 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004501 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004502 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4503 * "Na" into "Na+", "1234" into "1234+". */
4504 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004505 if (p != NULL)
4506 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004507 STRCPY(p, items[1]);
4508 STRCAT(p, "+");
4509 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004510 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004511 }
4512 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4513 {
4514 /* Concatenate this string to previously defined ones, using a
4515 * slash to separate them. */
4516 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004517 if (compflags != NULL)
4518 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004519 p = getroom(spin, l, FALSE);
4520 if (p != NULL)
4521 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004522 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004523 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004524 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004525 STRCAT(p, "/");
4526 }
4527 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004528 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004529 }
4530 }
4531 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004532 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004533 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004534 compmax = atoi((char *)items[1]);
4535 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004536 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4537 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004538 }
4539 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004540 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004541 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004542 compminlen = atoi((char *)items[1]);
4543 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004544 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4545 fname, lnum, items[1]);
4546 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004547 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004548 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004549 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004550 compsylmax = atoi((char *)items[1]);
4551 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004552 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4553 fname, lnum, items[1]);
4554 }
4555 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004556 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004557 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004558 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004559 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004560 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4561 {
4562 spin->si_nobreak = TRUE;
4563 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004564 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4565 {
4566 aff->af_pfxpostpone = TRUE;
4567 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568 else if ((STRCMP(items[0], "PFX") == 0
4569 || STRCMP(items[0], "SFX") == 0)
4570 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004571 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004572 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004573 int lasti = 4;
4574 char_u key[AH_KEY_LEN];
4575
4576 if (*items[0] == 'P')
4577 tp = &aff->af_pref;
4578 else
4579 tp = &aff->af_suff;
4580
4581 /* Myspell allows the same affix name to be used multiple
4582 * times. The affix files that do this have an undocumented
4583 * "S" flag on all but the last block, thus we check for that
4584 * and store it in ah_follows. */
4585 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4586 hi = hash_find(tp, key);
4587 if (!HASHITEM_EMPTY(hi))
4588 {
4589 cur_aff = HI2AH(hi);
4590 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4591 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4592 fname, lnum, items[1]);
4593 if (!cur_aff->ah_follows)
4594 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4595 fname, lnum, items[1]);
4596 }
4597 else
4598 {
4599 /* New affix letter. */
4600 cur_aff = (affheader_T *)getroom(spin,
4601 sizeof(affheader_T), TRUE);
4602 if (cur_aff == NULL)
4603 break;
4604 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4605 fname, lnum);
4606 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4607 break;
4608 if (cur_aff->ah_flag == aff->af_bad
4609 || cur_aff->ah_flag == aff->af_rar
4610 || cur_aff->ah_flag == aff->af_kep
4611 || cur_aff->ah_flag == aff->af_needaffix)
4612 smsg((char_u *)_("Affix also used for BAD/RAR/KEP/NEEDAFFIX in %s line %d: %s"),
4613 fname, lnum, items[1]);
4614 STRCPY(cur_aff->ah_key, items[1]);
4615 hash_add(tp, cur_aff->ah_key);
4616
4617 cur_aff->ah_combine = (*items[2] == 'Y');
4618 }
4619
4620 /* Check for the "S" flag, which apparently means that another
4621 * block with the same affix name is following. */
4622 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4623 {
4624 ++lasti;
4625 cur_aff->ah_follows = TRUE;
4626 }
4627 else
4628 cur_aff->ah_follows = FALSE;
4629
Bram Moolenaar8db73182005-06-17 21:51:16 +00004630 /* Myspell allows extra text after the item, but that might
4631 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004632 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004633 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4634 fname, lnum, items[4]);
4635
Bram Moolenaar95529562005-08-25 21:21:38 +00004636 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4638 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004639
Bram Moolenaar95529562005-08-25 21:21:38 +00004640 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004641 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004642 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004643 {
4644 /* Use a new number in the .spl file later, to be able
4645 * to handle multiple .aff files. */
4646 cur_aff->ah_newID = ++spin->si_newprefID;
4647
4648 /* We only really use ah_newID if the prefix is
4649 * postponed. We know that only after handling all
4650 * the items. */
4651 did_postpone_prefix = FALSE;
4652 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004653 else
4654 /* Did use the ID in a previous block. */
4655 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004656 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004657
Bram Moolenaar51485f02005-06-04 21:55:20 +00004658 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659 }
4660 else if ((STRCMP(items[0], "PFX") == 0
4661 || STRCMP(items[0], "SFX") == 0)
4662 && aff_todo > 0
4663 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004664 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004665 {
4666 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004667 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004668 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004669 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004670 int lasti = 5;
4671
Bram Moolenaar5195e452005-08-19 20:32:47 +00004672 /* Check for "rare" and "nocomp" after the other info. */
4673 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004674 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004675 if (!rare && STRICMP(items[lasti], "rare") == 0)
4676 {
4677 rare = TRUE;
4678 ++lasti;
4679 }
4680 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4681 {
4682 nocomp = TRUE;
4683 ++lasti;
4684 }
4685 else
4686 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004687 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004688
Bram Moolenaar8db73182005-06-17 21:51:16 +00004689 /* Myspell allows extra text after the item, but that might
4690 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004691 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004692 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004693
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004694 /* New item for an affix letter. */
4695 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004696 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004697 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004698 if (aff_entry == NULL)
4699 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004700 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004701 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004702
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004703 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004704 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004705 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004706 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004707
Bram Moolenaar51485f02005-06-04 21:55:20 +00004708 /* Don't use an affix entry with non-ASCII characters when
4709 * "spin->si_ascii" is TRUE. */
4710 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004711 || has_non_ascii(aff_entry->ae_add)))
4712 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004713 aff_entry->ae_next = cur_aff->ah_first;
4714 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004715
4716 if (STRCMP(items[4], ".") != 0)
4717 {
4718 char_u buf[MAXLINELEN];
4719
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004720 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004721 if (*items[0] == 'P')
4722 sprintf((char *)buf, "^%s", items[4]);
4723 else
4724 sprintf((char *)buf, "%s$", items[4]);
4725 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004726 RE_MAGIC + RE_STRING + RE_STRICT);
4727 if (aff_entry->ae_prog == NULL)
4728 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4729 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004730 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004731
4732 /* For postponed prefixes we need an entry in si_prefcond
4733 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004734 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004735 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004736 /* When the chop string is one lower-case letter and
4737 * the add string ends in the upper-case letter we set
4738 * the "upper" flag, clear "ae_chop" and remove the
4739 * letters from "ae_add". The condition must either
4740 * be empty or start with the same letter. */
4741 if (aff_entry->ae_chop != NULL
4742 && aff_entry->ae_add != NULL
4743#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004744 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004745 aff_entry->ae_chop)] == NUL
4746#else
4747 && aff_entry->ae_chop[1] == NUL
4748#endif
4749 )
4750 {
4751 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004752
Bram Moolenaar53805d12005-08-01 07:08:33 +00004753 c = PTR2CHAR(aff_entry->ae_chop);
4754 c_up = SPELL_TOUPPER(c);
4755 if (c_up != c
4756 && (aff_entry->ae_cond == NULL
4757 || PTR2CHAR(aff_entry->ae_cond) == c))
4758 {
4759 p = aff_entry->ae_add
4760 + STRLEN(aff_entry->ae_add);
4761 mb_ptr_back(aff_entry->ae_add, p);
4762 if (PTR2CHAR(p) == c_up)
4763 {
4764 upper = TRUE;
4765 aff_entry->ae_chop = NULL;
4766 *p = NUL;
4767
4768 /* The condition is matched with the
4769 * actual word, thus must check for the
4770 * upper-case letter. */
4771 if (aff_entry->ae_cond != NULL)
4772 {
4773 char_u buf[MAXLINELEN];
4774#ifdef FEAT_MBYTE
4775 if (has_mbyte)
4776 {
4777 onecap_copy(items[4], buf, TRUE);
4778 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004779 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004780 }
4781 else
4782#endif
4783 *aff_entry->ae_cond = c_up;
4784 if (aff_entry->ae_cond != NULL)
4785 {
4786 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004787 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004788 vim_free(aff_entry->ae_prog);
4789 aff_entry->ae_prog = vim_regcomp(
4790 buf, RE_MAGIC + RE_STRING);
4791 }
4792 }
4793 }
4794 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004795 }
4796
Bram Moolenaar53805d12005-08-01 07:08:33 +00004797 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004798 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004799 int idx;
4800 char_u **pp;
4801 int n;
4802
Bram Moolenaar6de68532005-08-24 22:08:48 +00004803 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004804 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4805 --idx)
4806 {
4807 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4808 if (str_equal(p, aff_entry->ae_cond))
4809 break;
4810 }
4811 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4812 {
4813 /* Not found, add a new condition. */
4814 idx = spin->si_prefcond.ga_len++;
4815 pp = ((char_u **)spin->si_prefcond.ga_data)
4816 + idx;
4817 if (aff_entry->ae_cond == NULL)
4818 *pp = NULL;
4819 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004820 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004821 aff_entry->ae_cond);
4822 }
4823
4824 /* Add the prefix to the prefix tree. */
4825 if (aff_entry->ae_add == NULL)
4826 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004827 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004828 p = aff_entry->ae_add;
4829 /* PFX_FLAGS is a negative number, so that
4830 * tree_add_word() knows this is the prefix tree. */
4831 n = PFX_FLAGS;
4832 if (rare)
4833 n |= WFP_RARE;
4834 if (!cur_aff->ah_combine)
4835 n |= WFP_NC;
4836 if (upper)
4837 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004838 tree_add_word(spin, p, spin->si_prefroot, n,
4839 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004840 did_postpone_prefix = TRUE;
4841 }
4842
4843 /* Didn't actually use ah_newID, backup si_newprefID. */
4844 if (aff_todo == 0 && !did_postpone_prefix)
4845 {
4846 --spin->si_newprefID;
4847 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004848 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004849 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004850 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004851 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004852 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
4853 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004854 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004855 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004856 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004857 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
4858 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004859 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004860 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004861 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004862 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
4863 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004864 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004865 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004866 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004867 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004868 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004869 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 if (!isdigit(*items[1]))
4871 smsg((char_u *)_("Expected REP count in %s line %d"),
4872 fname, lnum);
4873 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004874 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004876 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004877 /* Myspell ignores extra arguments, we require it starts with
4878 * # to detect mistakes. */
4879 if (itemcnt > 3 && items[3][0] != '#')
4880 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004881 if (do_rep)
4882 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4883 }
4884 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4885 {
4886 /* MAP item or count */
4887 if (!found_map)
4888 {
4889 /* First line contains the count. */
4890 found_map = TRUE;
4891 if (!isdigit(*items[1]))
4892 smsg((char_u *)_("Expected MAP count in %s line %d"),
4893 fname, lnum);
4894 }
4895 else if (do_map)
4896 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004897 int c;
4898
4899 /* Check that every character appears only once. */
4900 for (p = items[1]; *p != NUL; )
4901 {
4902#ifdef FEAT_MBYTE
4903 c = mb_ptr2char_adv(&p);
4904#else
4905 c = *p++;
4906#endif
4907 if ((spin->si_map.ga_len > 0
4908 && vim_strchr(spin->si_map.ga_data, c)
4909 != NULL)
4910 || vim_strchr(p, c) != NULL)
4911 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4912 fname, lnum);
4913 }
4914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 /* We simply concatenate all the MAP strings, separated by
4916 * slashes. */
4917 ga_concat(&spin->si_map, items[1]);
4918 ga_append(&spin->si_map, '/');
4919 }
4920 }
4921 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4922 {
4923 if (do_sal)
4924 {
4925 /* SAL item (sounds-a-like)
4926 * Either one of the known keys or a from-to pair. */
4927 if (STRCMP(items[1], "followup") == 0)
4928 spin->si_followup = sal_to_bool(items[2]);
4929 else if (STRCMP(items[1], "collapse_result") == 0)
4930 spin->si_collapse = sal_to_bool(items[2]);
4931 else if (STRCMP(items[1], "remove_accents") == 0)
4932 spin->si_rem_accents = sal_to_bool(items[2]);
4933 else
4934 /* when "to" is "_" it means empty */
4935 add_fromto(spin, &spin->si_sal, items[1],
4936 STRCMP(items[2], "_") == 0 ? (char_u *)""
4937 : items[2]);
4938 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004939 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004940 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004941 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004942 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004943 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004944 }
4945 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004946 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004947 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004948 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004949 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004950 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004951 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952 fname, lnum, items[0]);
4953 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004954 }
4955
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004956 if (fol != NULL || low != NULL || upp != NULL)
4957 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004958 if (spin->si_clear_chartab)
4959 {
4960 /* Clear the char type tables, don't want to use any of the
4961 * currently used spell properties. */
4962 init_spell_chartab();
4963 spin->si_clear_chartab = FALSE;
4964 }
4965
Bram Moolenaar3982c542005-06-08 21:56:31 +00004966 /*
4967 * Don't write a word table for an ASCII file, so that we don't check
4968 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004969 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004970 * mb_get_class(), the list of chars in the file will be incomplete.
4971 */
4972 if (!spin->si_ascii
4973#ifdef FEAT_MBYTE
4974 && !enc_utf8
4975#endif
4976 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004977 {
4978 if (fol == NULL || low == NULL || upp == NULL)
4979 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4980 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004981 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004982 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004983
4984 vim_free(fol);
4985 vim_free(low);
4986 vim_free(upp);
4987 }
4988
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004989 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004990 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004991 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004992 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
4993 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004994 }
4995
Bram Moolenaar6de68532005-08-24 22:08:48 +00004996 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004997 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004998 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
4999 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005000 }
5001
Bram Moolenaar6de68532005-08-24 22:08:48 +00005002 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005003 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005004 if (syllable == NULL)
5005 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5006 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5007 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005008 }
5009
Bram Moolenaar6de68532005-08-24 22:08:48 +00005010 if (compflags != NULL)
5011 process_compflags(spin, aff, compflags);
5012
5013 /* Check that we didn't use too many renumbered flags. */
5014 if (spin->si_compID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005015 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005016 if (spin->si_compID == 255)
5017 MSG(_("Too many postponed prefixes"));
5018 else if (spin->si_newprefID == 0)
5019 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005020 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005021 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005022 }
5023
Bram Moolenaar6de68532005-08-24 22:08:48 +00005024 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005025 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005026 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5027 spin->si_syllable = syllable;
5028 }
5029
5030 if (sofofrom != NULL || sofoto != NULL)
5031 {
5032 if (sofofrom == NULL || sofoto == NULL)
5033 smsg((char_u *)_("Missing SOFO%s line in %s"),
5034 sofofrom == NULL ? "FROM" : "TO", fname);
5035 else if (spin->si_sal.ga_len > 0)
5036 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005037 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005038 {
5039 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5040 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5041 spin->si_sofofr = sofofrom;
5042 spin->si_sofoto = sofoto;
5043 }
5044 }
5045
5046 if (midword != NULL)
5047 {
5048 aff_check_string(spin->si_midword, midword, "MIDWORD");
5049 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005050 }
5051
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005052 vim_free(pc);
5053 fclose(fd);
5054 return aff;
5055}
5056
5057/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005058 * Turn an affix flag name into a number, according to the FLAG type.
5059 * returns zero for failure.
5060 */
5061 static unsigned
5062affitem2flag(flagtype, item, fname, lnum)
5063 int flagtype;
5064 char_u *item;
5065 char_u *fname;
5066 int lnum;
5067{
5068 unsigned res;
5069 char_u *p = item;
5070
5071 res = get_affitem(flagtype, &p);
5072 if (res == 0)
5073 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005074 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005075 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5076 fname, lnum, item);
5077 else
5078 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5079 fname, lnum, item);
5080 }
5081 if (*p != NUL)
5082 {
5083 smsg((char_u *)_(e_affname), fname, lnum, item);
5084 return 0;
5085 }
5086
5087 return res;
5088}
5089
5090/*
5091 * Get one affix name from "*pp" and advance the pointer.
5092 * Returns zero for an error, still advances the pointer then.
5093 */
5094 static unsigned
5095get_affitem(flagtype, pp)
5096 int flagtype;
5097 char_u **pp;
5098{
5099 int res;
5100
Bram Moolenaar95529562005-08-25 21:21:38 +00005101 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005102 {
5103 if (!VIM_ISDIGIT(**pp))
5104 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005105 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005106 return 0;
5107 }
5108 res = getdigits(pp);
5109 }
5110 else
5111 {
5112#ifdef FEAT_MBYTE
5113 res = mb_ptr2char_adv(pp);
5114#else
5115 res = *(*pp)++;
5116#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005117 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005118 && res >= 'A' && res <= 'Z'))
5119 {
5120 if (**pp == NUL)
5121 return 0;
5122#ifdef FEAT_MBYTE
5123 res = mb_ptr2char_adv(pp) + (res << 16);
5124#else
5125 res = *(*pp)++ + (res << 16);
5126#endif
5127 }
5128 }
5129 return res;
5130}
5131
5132/*
5133 * Process the "compflags" string used in an affix file and append it to
5134 * spin->si_compflags.
5135 * The processing involves changing the affix names to ID numbers, so that
5136 * they fit in one byte.
5137 */
5138 static void
5139process_compflags(spin, aff, compflags)
5140 spellinfo_T *spin;
5141 afffile_T *aff;
5142 char_u *compflags;
5143{
5144 char_u *p;
5145 char_u *prevp;
5146 unsigned flag;
5147 compitem_T *ci;
5148 int id;
5149 int len;
5150 char_u *tp;
5151 char_u key[AH_KEY_LEN];
5152 hashitem_T *hi;
5153
5154 /* Make room for the old and the new compflags, concatenated with a / in
5155 * between. Processing it makes it shorter, but we don't know by how
5156 * much, thus allocate the maximum. */
5157 len = STRLEN(compflags) + 1;
5158 if (spin->si_compflags != NULL)
5159 len += STRLEN(spin->si_compflags) + 1;
5160 p = getroom(spin, len, FALSE);
5161 if (p == NULL)
5162 return;
5163 if (spin->si_compflags != NULL)
5164 {
5165 STRCPY(p, spin->si_compflags);
5166 STRCAT(p, "/");
5167 }
5168 else
5169 *p = NUL;
5170 spin->si_compflags = p;
5171 tp = p + STRLEN(p);
5172
5173 for (p = compflags; *p != NUL; )
5174 {
5175 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5176 /* Copy non-flag characters directly. */
5177 *tp++ = *p++;
5178 else
5179 {
5180 /* First get the flag number, also checks validity. */
5181 prevp = p;
5182 flag = get_affitem(aff->af_flagtype, &p);
5183 if (flag != 0)
5184 {
5185 /* Find the flag in the hashtable. If it was used before, use
5186 * the existing ID. Otherwise add a new entry. */
5187 vim_strncpy(key, prevp, p - prevp);
5188 hi = hash_find(&aff->af_comp, key);
5189 if (!HASHITEM_EMPTY(hi))
5190 id = HI2CI(hi)->ci_newID;
5191 else
5192 {
5193 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5194 if (ci == NULL)
5195 break;
5196 STRCPY(ci->ci_key, key);
5197 ci->ci_flag = flag;
5198 /* Avoid using a flag ID that has a special meaning in a
5199 * regexp (also inside []). */
5200 do
5201 {
5202 id = spin->si_compID--;
5203 } while (vim_strchr((char_u *)"/*+[]\\-^", id) != NULL);
5204 ci->ci_newID = id;
5205 hash_add(&aff->af_comp, ci->ci_key);
5206 }
5207 *tp++ = id;
5208 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005209 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005210 ++p;
5211 }
5212 }
5213
5214 *tp = NUL;
5215}
5216
5217/*
5218 * Return TRUE if flag "flag" appears in affix list "afflist".
5219 */
5220 static int
5221flag_in_afflist(flagtype, afflist, flag)
5222 int flagtype;
5223 char_u *afflist;
5224 unsigned flag;
5225{
5226 char_u *p;
5227 unsigned n;
5228
5229 switch (flagtype)
5230 {
5231 case AFT_CHAR:
5232 return vim_strchr(afflist, flag) != NULL;
5233
Bram Moolenaar95529562005-08-25 21:21:38 +00005234 case AFT_CAPLONG:
5235 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005236 for (p = afflist; *p != NUL; )
5237 {
5238#ifdef FEAT_MBYTE
5239 n = mb_ptr2char_adv(&p);
5240#else
5241 n = *p++;
5242#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005243 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005244 && *p != NUL)
5245#ifdef FEAT_MBYTE
5246 n = mb_ptr2char_adv(&p) + (n << 16);
5247#else
5248 n = *p++ + (n << 16);
5249#endif
5250 if (n == flag)
5251 return TRUE;
5252 }
5253 break;
5254
Bram Moolenaar95529562005-08-25 21:21:38 +00005255 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005256 for (p = afflist; *p != NUL; )
5257 {
5258 n = getdigits(&p);
5259 if (n == flag)
5260 return TRUE;
5261 if (*p != NUL) /* skip over comma */
5262 ++p;
5263 }
5264 break;
5265 }
5266 return FALSE;
5267}
5268
5269/*
5270 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5271 */
5272 static void
5273aff_check_number(spinval, affval, name)
5274 int spinval;
5275 int affval;
5276 char *name;
5277{
5278 if (spinval != 0 && spinval != affval)
5279 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5280}
5281
5282/*
5283 * Give a warning when "spinval" and "affval" strings are set and not the same.
5284 */
5285 static void
5286aff_check_string(spinval, affval, name)
5287 char_u *spinval;
5288 char_u *affval;
5289 char *name;
5290{
5291 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5292 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5293}
5294
5295/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005296 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5297 * NULL as equal.
5298 */
5299 static int
5300str_equal(s1, s2)
5301 char_u *s1;
5302 char_u *s2;
5303{
5304 if (s1 == NULL || s2 == NULL)
5305 return s1 == s2;
5306 return STRCMP(s1, s2) == 0;
5307}
5308
5309/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 * Add a from-to item to "gap". Used for REP and SAL items.
5311 * They are stored case-folded.
5312 */
5313 static void
5314add_fromto(spin, gap, from, to)
5315 spellinfo_T *spin;
5316 garray_T *gap;
5317 char_u *from;
5318 char_u *to;
5319{
5320 fromto_T *ftp;
5321 char_u word[MAXWLEN];
5322
5323 if (ga_grow(gap, 1) == OK)
5324 {
5325 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5326 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005327 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005329 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 ++gap->ga_len;
5331 }
5332}
5333
5334/*
5335 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5336 */
5337 static int
5338sal_to_bool(s)
5339 char_u *s;
5340{
5341 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5342}
5343
5344/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005345 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5346 * When "s" is NULL FALSE is returned.
5347 */
5348 static int
5349has_non_ascii(s)
5350 char_u *s;
5351{
5352 char_u *p;
5353
5354 if (s != NULL)
5355 for (p = s; *p != NUL; ++p)
5356 if (*p >= 128)
5357 return TRUE;
5358 return FALSE;
5359}
5360
5361/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005362 * Free the structure filled by spell_read_aff().
5363 */
5364 static void
5365spell_free_aff(aff)
5366 afffile_T *aff;
5367{
5368 hashtab_T *ht;
5369 hashitem_T *hi;
5370 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005371 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005372 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005373
5374 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005375
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005376 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005377 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5378 {
5379 todo = ht->ht_used;
5380 for (hi = ht->ht_array; todo > 0; ++hi)
5381 {
5382 if (!HASHITEM_EMPTY(hi))
5383 {
5384 --todo;
5385 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005386 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5387 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005388 }
5389 }
5390 if (ht == &aff->af_suff)
5391 break;
5392 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005393
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005394 hash_clear(&aff->af_pref);
5395 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005396 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005397}
5398
5399/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005400 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005401 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005402 */
5403 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005404spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005405 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005406 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005407 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005408{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005409 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005410 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005411 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005412 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005413 char_u store_afflist[MAXWLEN];
5414 int pfxlen;
5415 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005416 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005417 char_u *pc;
5418 char_u *w;
5419 int l;
5420 hash_T hash;
5421 hashitem_T *hi;
5422 FILE *fd;
5423 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005424 int non_ascii = 0;
5425 int retval = OK;
5426 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005427 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005428 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005429
Bram Moolenaar51485f02005-06-04 21:55:20 +00005430 /*
5431 * Open the file.
5432 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005433 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005434 if (fd == NULL)
5435 {
5436 EMSG2(_(e_notopen), fname);
5437 return FAIL;
5438 }
5439
Bram Moolenaar51485f02005-06-04 21:55:20 +00005440 /* The hashtable is only used to detect duplicated words. */
5441 hash_init(&ht);
5442
Bram Moolenaarb765d632005-06-07 21:00:02 +00005443 if (spin->si_verbose || p_verbose > 2)
5444 {
5445 if (!spin->si_verbose)
5446 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005447 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005448 out_flush();
5449 if (!spin->si_verbose)
5450 verbose_leave();
5451 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005452
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005453 /* start with a message for the first line */
5454 spin->si_msg_count = 999999;
5455
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005456 /* Read and ignore the first line: word count. */
5457 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005458 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005459 EMSG2(_("E760: No word count in %s"), fname);
5460
5461 /*
5462 * Read all the lines in the file one by one.
5463 * The words are converted to 'encoding' here, before being added to
5464 * the hashtable.
5465 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005466 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005467 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005468 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005469 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005470 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005471 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005472
Bram Moolenaar51485f02005-06-04 21:55:20 +00005473 /* Remove CR, LF and white space from the end. White space halfway
5474 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005475 l = STRLEN(line);
5476 while (l > 0 && line[l - 1] <= ' ')
5477 --l;
5478 if (l == 0)
5479 continue; /* empty line */
5480 line[l] = NUL;
5481
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005482 /* Find the optional affix names. Replace the SLASH character by a
5483 * slash. */
5484 afflist = NULL;
5485 for (p = line; *p != NUL; mb_ptr_adv(p))
5486 {
5487 if (*p == affile->af_slash)
5488 *p = '/';
5489 else if (*p == '/')
5490 {
5491 *p = NUL;
5492 afflist = p + 1;
5493 break;
5494 }
5495 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005496
5497 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5498 if (spin->si_ascii && has_non_ascii(line))
5499 {
5500 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005501 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005502 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005503
Bram Moolenaarb765d632005-06-07 21:00:02 +00005504#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005505 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005506 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005507 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005508 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005509 if (pc == NULL)
5510 {
5511 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5512 fname, lnum, line);
5513 continue;
5514 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005515 w = pc;
5516 }
5517 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005518#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005519 {
5520 pc = NULL;
5521 w = line;
5522 }
5523
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005524 /* This takes time, print a message every 10000 words. */
5525 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005526 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005527 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005528 vim_snprintf((char *)message, sizeof(message),
5529 _("line %6d, word %6d - %s"),
5530 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5531 msg_start();
5532 msg_puts_long_attr(message, 0);
5533 msg_clr_eos();
5534 msg_didout = FALSE;
5535 msg_col = 0;
5536 out_flush();
5537 }
5538
Bram Moolenaar51485f02005-06-04 21:55:20 +00005539 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005540 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005541 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005542 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005543 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005544 if (retval == FAIL)
5545 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005546
Bram Moolenaar51485f02005-06-04 21:55:20 +00005547 hash = hash_hash(dw);
5548 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005549 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005550 {
5551 if (p_verbose > 0)
5552 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005553 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005554 else if (duplicate == 0)
5555 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5556 fname, lnum, dw);
5557 ++duplicate;
5558 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005559 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005560 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005561
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005562 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005563 store_afflist[0] = NUL;
5564 pfxlen = 0;
5565 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005566 if (afflist != NULL)
5567 {
5568 /* Check for affix name that stands for keep-case word and stands
5569 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005570 if (affile->af_kep != 0 && flag_in_afflist(
5571 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005572 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005573 if (affile->af_rar != 0 && flag_in_afflist(
5574 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005575 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005576 if (affile->af_bad != 0 && flag_in_afflist(
5577 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005578 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005579 if (affile->af_needaffix != 0 && flag_in_afflist(
5580 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005581 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005582
5583 if (affile->af_pfxpostpone)
5584 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005585 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005586
Bram Moolenaar5195e452005-08-19 20:32:47 +00005587 if (spin->si_compflags != NULL)
5588 /* Need to store the list of compound flags with the word.
5589 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005590 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005591 }
5592
Bram Moolenaar51485f02005-06-04 21:55:20 +00005593 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005594 if (store_word(spin, dw, flags, spin->si_region,
5595 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005596 retval = FAIL;
5597
5598 if (afflist != NULL)
5599 {
5600 /* Find all matching suffixes and add the resulting words.
5601 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005602 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005603 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005604 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005605 retval = FAIL;
5606
5607 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005608 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005609 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005610 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005611 retval = FAIL;
5612 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005613 }
5614
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005615 if (duplicate > 0)
5616 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005617 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005618 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5619 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005620 hash_clear(&ht);
5621
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005622 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005623 return retval;
5624}
5625
5626/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005627 * Get the list of prefix IDs from the affix list "afflist".
5628 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005629 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5630 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005631 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005632 static int
5633get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005634 afffile_T *affile;
5635 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005636 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005637{
5638 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005639 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005640 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005641 int id;
5642 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005643 hashitem_T *hi;
5644
Bram Moolenaar6de68532005-08-24 22:08:48 +00005645 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005646 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005647 prevp = p;
5648 if (get_affitem(affile->af_flagtype, &p) != 0)
5649 {
5650 /* A flag is a postponed prefix flag if it appears in "af_pref"
5651 * and it's ID is not zero. */
5652 vim_strncpy(key, prevp, p - prevp);
5653 hi = hash_find(&affile->af_pref, key);
5654 if (!HASHITEM_EMPTY(hi))
5655 {
5656 id = HI2AH(hi)->ah_newID;
5657 if (id != 0)
5658 store_afflist[cnt++] = id;
5659 }
5660 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005661 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005662 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005663 }
5664
Bram Moolenaar5195e452005-08-19 20:32:47 +00005665 store_afflist[cnt] = NUL;
5666 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005667}
5668
5669/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005670 * Get the list of compound IDs from the affix list "afflist" that are used
5671 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005672 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005673 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005674 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005675get_compflags(affile, afflist, store_afflist)
5676 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005677 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005678 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005679{
5680 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005681 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005682 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005683 char_u key[AH_KEY_LEN];
5684 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005685
Bram Moolenaar6de68532005-08-24 22:08:48 +00005686 for (p = afflist; *p != NUL; )
5687 {
5688 prevp = p;
5689 if (get_affitem(affile->af_flagtype, &p) != 0)
5690 {
5691 /* A flag is a compound flag if it appears in "af_comp". */
5692 vim_strncpy(key, prevp, p - prevp);
5693 hi = hash_find(&affile->af_comp, key);
5694 if (!HASHITEM_EMPTY(hi))
5695 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5696 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005697 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005698 ++p;
5699 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005700
Bram Moolenaar5195e452005-08-19 20:32:47 +00005701 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005702}
5703
5704/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005705 * Apply affixes to a word and store the resulting words.
5706 * "ht" is the hashtable with affentry_T that need to be applied, either
5707 * prefixes or suffixes.
5708 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5709 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005710 *
5711 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005712 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005713 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005714store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5715 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005716 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005717 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005718 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005719 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005720 hashtab_T *ht;
5721 hashtab_T *xht;
5722 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005723 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005724 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005725 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5726 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005727{
5728 int todo;
5729 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005730 affheader_T *ah;
5731 affentry_T *ae;
5732 regmatch_T regmatch;
5733 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005734 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 int i;
5736 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005737 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005738 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005739 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005740 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005741
Bram Moolenaar51485f02005-06-04 21:55:20 +00005742 todo = ht->ht_used;
5743 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005744 {
5745 if (!HASHITEM_EMPTY(hi))
5746 {
5747 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005748 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005749
Bram Moolenaar51485f02005-06-04 21:55:20 +00005750 /* Check that the affix combines, if required, and that the word
5751 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005752 if ((!comb || ah->ah_combine) && flag_in_afflist(
5753 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005754 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005755 /* Loop over all affix entries with this name. */
5756 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005757 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005758 /* Check the condition. It's not logical to match case
5759 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005760 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005761 * Another requirement from Myspell is that the chop
5762 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005763 * For prefixes, when "PFXPOSTPONE" was used, only do
5764 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005765 regmatch.regprog = ae->ae_prog;
5766 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005767 if ((xht != NULL || !affile->af_pfxpostpone
5768 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005769 && (ae->ae_chop == NULL
5770 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005771 && (ae->ae_prog == NULL
5772 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005773 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005774 /* Match. Remove the chop and add the affix. */
5775 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005776 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005777 /* prefix: chop/add at the start of the word */
5778 if (ae->ae_add == NULL)
5779 *newword = NUL;
5780 else
5781 STRCPY(newword, ae->ae_add);
5782 p = word;
5783 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005784 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005785 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005786#ifdef FEAT_MBYTE
5787 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005788 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005789 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005790 for ( ; i > 0; --i)
5791 mb_ptr_adv(p);
5792 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005793 else
5794#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005795 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005796 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005797 STRCAT(newword, p);
5798 }
5799 else
5800 {
5801 /* suffix: chop/add at the end of the word */
5802 STRCPY(newword, word);
5803 if (ae->ae_chop != NULL)
5804 {
5805 /* Remove chop string. */
5806 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005807 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005808 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005809 mb_ptr_back(newword, p);
5810 *p = NUL;
5811 }
5812 if (ae->ae_add != NULL)
5813 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005814 }
5815
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005816 /* Obey the "rare" flag of the affix. */
5817 if (ae->ae_rare)
5818 use_flags = flags | WF_RARE;
5819 else
5820 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005821
5822 /* Obey the "nocomp" flag of the affix: don't use the
5823 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005824 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005825 if (ae->ae_nocomp && pfxlist != NULL)
5826 {
5827 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5828 use_pfxlist = pfx_pfxlist;
5829 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005830
5831 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005832 if (spin->si_prefroot != NULL
5833 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005834 {
5835 /* ... add a flag to indicate an affix was used. */
5836 use_flags |= WF_HAS_AFF;
5837
5838 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005839 * affixes is not allowed. But do use the
5840 * compound flags after them. */
5841 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5842 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005843 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005844
Bram Moolenaar51485f02005-06-04 21:55:20 +00005845 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005846 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005847 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005848 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005849
Bram Moolenaar51485f02005-06-04 21:55:20 +00005850 /* When added a suffix and combining is allowed also
5851 * try adding prefixes additionally. */
5852 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005853 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005854 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005855 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005856 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005857 }
5858 }
5859 }
5860 }
5861 }
5862
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005863 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005864}
5865
5866/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005867 * Read a file with a list of words.
5868 */
5869 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005870spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005871 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005872 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005873{
5874 FILE *fd;
5875 long lnum = 0;
5876 char_u rline[MAXLINELEN];
5877 char_u *line;
5878 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005879 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005880 int l;
5881 int retval = OK;
5882 int did_word = FALSE;
5883 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005884 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005885 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005886
5887 /*
5888 * Open the file.
5889 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005890 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005891 if (fd == NULL)
5892 {
5893 EMSG2(_(e_notopen), fname);
5894 return FAIL;
5895 }
5896
Bram Moolenaarb765d632005-06-07 21:00:02 +00005897 if (spin->si_verbose || p_verbose > 2)
5898 {
5899 if (!spin->si_verbose)
5900 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005901 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005902 out_flush();
5903 if (!spin->si_verbose)
5904 verbose_leave();
5905 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005906
5907 /*
5908 * Read all the lines in the file one by one.
5909 */
5910 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5911 {
5912 line_breakcheck();
5913 ++lnum;
5914
5915 /* Skip comment lines. */
5916 if (*rline == '#')
5917 continue;
5918
5919 /* Remove CR, LF and white space from the end. */
5920 l = STRLEN(rline);
5921 while (l > 0 && rline[l - 1] <= ' ')
5922 --l;
5923 if (l == 0)
5924 continue; /* empty or blank line */
5925 rline[l] = NUL;
5926
5927 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
5928 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005929#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005930 if (spin->si_conv.vc_type != CONV_NONE)
5931 {
5932 pc = string_convert(&spin->si_conv, rline, NULL);
5933 if (pc == NULL)
5934 {
5935 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5936 fname, lnum, rline);
5937 continue;
5938 }
5939 line = pc;
5940 }
5941 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005942#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005943 {
5944 pc = NULL;
5945 line = rline;
5946 }
5947
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005948 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00005949 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005950 ++line;
5951 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005952 {
5953 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005954 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
5955 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005956 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005957 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
5958 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005959 else
5960 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005961#ifdef FEAT_MBYTE
5962 char_u *enc;
5963
Bram Moolenaar51485f02005-06-04 21:55:20 +00005964 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005965 line += 10;
5966 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005967 if (enc != NULL && !spin->si_ascii
5968 && convert_setup(&spin->si_conv, enc,
5969 p_enc) == FAIL)
5970 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00005971 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005972 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005973 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005974#else
5975 smsg((char_u *)_("Conversion in %s not supported"), fname);
5976#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005977 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005978 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005979 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005980
Bram Moolenaar3982c542005-06-08 21:56:31 +00005981 if (STRNCMP(line, "regions=", 8) == 0)
5982 {
5983 if (spin->si_region_count > 1)
5984 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
5985 fname, lnum, line);
5986 else
5987 {
5988 line += 8;
5989 if (STRLEN(line) > 16)
5990 smsg((char_u *)_("Too many regions in %s line %d: %s"),
5991 fname, lnum, line);
5992 else
5993 {
5994 spin->si_region_count = STRLEN(line) / 2;
5995 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005996
5997 /* Adjust the mask for a word valid in all regions. */
5998 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005999 }
6000 }
6001 continue;
6002 }
6003
Bram Moolenaar7887d882005-07-01 22:33:52 +00006004 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6005 fname, lnum, line - 1);
6006 continue;
6007 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006008
Bram Moolenaar7887d882005-07-01 22:33:52 +00006009 flags = 0;
6010 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006011
Bram Moolenaar7887d882005-07-01 22:33:52 +00006012 /* Check for flags and region after a slash. */
6013 p = vim_strchr(line, '/');
6014 if (p != NULL)
6015 {
6016 *p++ = NUL;
6017 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006018 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006019 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006020 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006021 else if (*p == '!') /* Bad, bad, wicked word. */
6022 flags |= WF_BANNED;
6023 else if (*p == '?') /* Rare word. */
6024 flags |= WF_RARE;
6025 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006026 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006027 if ((flags & WF_REGION) == 0) /* first one */
6028 regionmask = 0;
6029 flags |= WF_REGION;
6030
6031 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006032 if (l > spin->si_region_count)
6033 {
6034 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006035 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006036 break;
6037 }
6038 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006039 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006040 else
6041 {
6042 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6043 fname, lnum, p);
6044 break;
6045 }
6046 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006047 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006048 }
6049
6050 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6051 if (spin->si_ascii && has_non_ascii(line))
6052 {
6053 ++non_ascii;
6054 continue;
6055 }
6056
6057 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006058 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006059 {
6060 retval = FAIL;
6061 break;
6062 }
6063 did_word = TRUE;
6064 }
6065
6066 vim_free(pc);
6067 fclose(fd);
6068
Bram Moolenaarb765d632005-06-07 21:00:02 +00006069 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6070 {
6071 if (p_verbose > 2)
6072 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006073 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6074 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006075 if (p_verbose > 2)
6076 verbose_leave();
6077 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006078 return retval;
6079}
6080
6081/*
6082 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006083 * This avoids calling free() for every little struct we use (and keeping
6084 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006085 * The memory is cleared to all zeros.
6086 * Returns NULL when out of memory.
6087 */
6088 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006089getroom(spin, len, align)
6090 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006091 size_t len; /* length needed */
6092 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006093{
6094 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006095 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006096
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006097 if (align && bl != NULL)
6098 /* Round size up for alignment. On some systems structures need to be
6099 * aligned to the size of a pointer (e.g., SPARC). */
6100 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6101 & ~(sizeof(char *) - 1);
6102
Bram Moolenaar51485f02005-06-04 21:55:20 +00006103 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6104 {
6105 /* Allocate a block of memory. This is not freed until much later. */
6106 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6107 if (bl == NULL)
6108 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006109 bl->sb_next = spin->si_blocks;
6110 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006111 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006112 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006113 }
6114
6115 p = bl->sb_data + bl->sb_used;
6116 bl->sb_used += len;
6117
6118 return p;
6119}
6120
6121/*
6122 * Make a copy of a string into memory allocated with getroom().
6123 */
6124 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006125getroom_save(spin, s)
6126 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006127 char_u *s;
6128{
6129 char_u *sc;
6130
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006131 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006132 if (sc != NULL)
6133 STRCPY(sc, s);
6134 return sc;
6135}
6136
6137
6138/*
6139 * Free the list of allocated sblock_T.
6140 */
6141 static void
6142free_blocks(bl)
6143 sblock_T *bl;
6144{
6145 sblock_T *next;
6146
6147 while (bl != NULL)
6148 {
6149 next = bl->sb_next;
6150 vim_free(bl);
6151 bl = next;
6152 }
6153}
6154
6155/*
6156 * Allocate the root of a word tree.
6157 */
6158 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006159wordtree_alloc(spin)
6160 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006161{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006162 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006163}
6164
6165/*
6166 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006167 * Always store it in the case-folded tree. For a keep-case word this is
6168 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6169 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006170 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006171 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6172 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006173 */
6174 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006175store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006176 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006177 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006178 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006179 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006180 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006181 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006182{
6183 int len = STRLEN(word);
6184 int ct = captype(word, word + len);
6185 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006186 int res = OK;
6187 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006189 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006190 for (p = pfxlist; res == OK; ++p)
6191 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006192 if (!need_affix || (p != NULL && *p != NUL))
6193 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006194 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006195 if (p == NULL || *p == NUL)
6196 break;
6197 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006198 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006199
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006200 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006201 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006202 for (p = pfxlist; res == OK; ++p)
6203 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006204 if (!need_affix || (p != NULL && *p != NUL))
6205 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006206 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006207 if (p == NULL || *p == NUL)
6208 break;
6209 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006210 ++spin->si_keepwcount;
6211 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006212 return res;
6213}
6214
6215/*
6216 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006217 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6218 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006219 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006220 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006221 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006222tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006223 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006224 char_u *word;
6225 wordnode_T *root;
6226 int flags;
6227 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006228 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006229{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006230 wordnode_T *node = root;
6231 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006232 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006233 wordnode_T **prev = NULL;
6234 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006235
Bram Moolenaar51485f02005-06-04 21:55:20 +00006236 /* Add each byte of the word to the tree, including the NUL at the end. */
6237 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006238 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006239 /* When there is more than one reference to this node we need to make
6240 * a copy, so that we can modify it. Copy the whole list of siblings
6241 * (we don't optimize for a partly shared list of siblings). */
6242 if (node != NULL && node->wn_refs > 1)
6243 {
6244 --node->wn_refs;
6245 copyprev = prev;
6246 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6247 {
6248 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006249 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006250 if (np == NULL)
6251 return FAIL;
6252 np->wn_child = copyp->wn_child;
6253 if (np->wn_child != NULL)
6254 ++np->wn_child->wn_refs; /* child gets extra ref */
6255 np->wn_byte = copyp->wn_byte;
6256 if (np->wn_byte == NUL)
6257 {
6258 np->wn_flags = copyp->wn_flags;
6259 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006260 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006261 }
6262
6263 /* Link the new node in the list, there will be one ref. */
6264 np->wn_refs = 1;
6265 *copyprev = np;
6266 copyprev = &np->wn_sibling;
6267
6268 /* Let "node" point to the head of the copied list. */
6269 if (copyp == node)
6270 node = np;
6271 }
6272 }
6273
Bram Moolenaar51485f02005-06-04 21:55:20 +00006274 /* Look for the sibling that has the same character. They are sorted
6275 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006276 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006277 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006278 while (node != NULL
6279 && (node->wn_byte < word[i]
6280 || (node->wn_byte == NUL
6281 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006282 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006283 : node->wn_flags < (flags & WN_MASK)
6284 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006285 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006286 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006287 prev = &node->wn_sibling;
6288 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006289 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006290 if (node == NULL
6291 || node->wn_byte != word[i]
6292 || (word[i] == NUL
6293 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006294 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006295 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006296 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006297 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006298 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006299 if (np == NULL)
6300 return FAIL;
6301 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006302
6303 /* If "node" is NULL this is a new child or the end of the sibling
6304 * list: ref count is one. Otherwise use ref count of sibling and
6305 * make ref count of sibling one (matters when inserting in front
6306 * of the list of siblings). */
6307 if (node == NULL)
6308 np->wn_refs = 1;
6309 else
6310 {
6311 np->wn_refs = node->wn_refs;
6312 node->wn_refs = 1;
6313 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006314 *prev = np;
6315 np->wn_sibling = node;
6316 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006317 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006318
Bram Moolenaar51485f02005-06-04 21:55:20 +00006319 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006320 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006321 node->wn_flags = flags;
6322 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006323 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006324 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006325 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006326 prev = &node->wn_child;
6327 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006328 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006329#ifdef SPELL_PRINTTREE
6330 smsg("Added \"%s\"", word);
6331 spell_print_tree(root->wn_sibling);
6332#endif
6333
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006334 /* count nr of words added since last message */
6335 ++spin->si_msg_count;
6336
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006337 if (spin->si_compress_cnt > 1)
6338 {
6339 if (--spin->si_compress_cnt == 1)
6340 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006341 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006342 }
6343
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006344 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006345 * When we have allocated lots of memory we need to compress the word tree
6346 * to free up some room. But compression is slow, and we might actually
6347 * need that room, thus only compress in the following situations:
6348 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006349 * "compress_start" blocks.
6350 * 2. When compressed before and used "compress_inc" blocks before
6351 * adding "compress_added" words (si_compress_cnt > 1).
6352 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006353 * (si_compress_cnt == 1) and the number of free nodes drops below the
6354 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006355 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006356#ifndef SPELL_PRINTTREE
6357 if (spin->si_compress_cnt == 1
6358 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006359 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006360#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006361 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006362 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006363 * when the freed up room has been used and another "compress_inc"
6364 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006365 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006366 spin->si_blocks_cnt -= compress_inc;
6367 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006368
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006369 if (spin->si_verbose)
6370 {
6371 msg_start();
6372 msg_puts((char_u *)_(msg_compressing));
6373 msg_clr_eos();
6374 msg_didout = FALSE;
6375 msg_col = 0;
6376 out_flush();
6377 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006378
6379 /* Compress both trees. Either they both have many nodes, which makes
6380 * compression useful, or one of them is small, which means
6381 * compression goes fast. */
6382 wordtree_compress(spin, spin->si_foldroot);
6383 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006384 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006385
6386 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006387}
6388
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006389/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006390 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6391 * Sets "sps_flags".
6392 */
6393 int
6394spell_check_msm()
6395{
6396 char_u *p = p_msm;
6397 long start = 0;
6398 long inc = 0;
6399 long added = 0;
6400
6401 if (!VIM_ISDIGIT(*p))
6402 return FAIL;
6403 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6404 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6405 if (*p != ',')
6406 return FAIL;
6407 ++p;
6408 if (!VIM_ISDIGIT(*p))
6409 return FAIL;
6410 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6411 if (*p != ',')
6412 return FAIL;
6413 ++p;
6414 if (!VIM_ISDIGIT(*p))
6415 return FAIL;
6416 added = getdigits(&p) * 1024;
6417 if (*p != NUL)
6418 return FAIL;
6419
6420 if (start == 0 || inc == 0 || added == 0 || inc > start)
6421 return FAIL;
6422
6423 compress_start = start;
6424 compress_inc = inc;
6425 compress_added = added;
6426 return OK;
6427}
6428
6429
6430/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006431 * Get a wordnode_T, either from the list of previously freed nodes or
6432 * allocate a new one.
6433 */
6434 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006435get_wordnode(spin)
6436 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006437{
6438 wordnode_T *n;
6439
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006440 if (spin->si_first_free == NULL)
6441 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006442 else
6443 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006444 n = spin->si_first_free;
6445 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006446 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006447 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006448 }
6449#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006450 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006451#endif
6452 return n;
6453}
6454
6455/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006456 * Decrement the reference count on a node (which is the head of a list of
6457 * siblings). If the reference count becomes zero free the node and its
6458 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006459 */
6460 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006461deref_wordnode(spin, node)
6462 spellinfo_T *spin;
6463 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006464{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006465 wordnode_T *np;
6466
6467 if (--node->wn_refs == 0)
6468 for (np = node; np != NULL; np = np->wn_sibling)
6469 {
6470 if (np->wn_child != NULL)
6471 deref_wordnode(spin, np->wn_child);
6472 free_wordnode(spin, np);
6473 }
6474}
6475
6476/*
6477 * Free a wordnode_T for re-use later.
6478 * Only the "wn_child" field becomes invalid.
6479 */
6480 static void
6481free_wordnode(spin, n)
6482 spellinfo_T *spin;
6483 wordnode_T *n;
6484{
6485 n->wn_child = spin->si_first_free;
6486 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006487 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006488}
6489
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006490/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006491 * Compress a tree: find tails that are identical and can be shared.
6492 */
6493 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006494wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006495 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006496 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006497{
6498 hashtab_T ht;
6499 int n;
6500 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006501 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006503 /* Skip the root itself, it's not actually used. The first sibling is the
6504 * start of the tree. */
6505 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006506 {
6507 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006508 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006509
6510#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006511 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006512#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006513 {
6514 if (!spin->si_verbose)
6515 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006516 if (tot > 1000000)
6517 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006518 else if (tot == 0)
6519 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006520 else
6521 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006522 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006523 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006524 if (p_verbose > 2)
6525 verbose_leave();
6526 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006527#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006528 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006529#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006530 hash_clear(&ht);
6531 }
6532}
6533
6534/*
6535 * Compress a node, its siblings and its children, depth first.
6536 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006537 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006538 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006539node_compress(spin, node, ht, tot)
6540 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006541 wordnode_T *node;
6542 hashtab_T *ht;
6543 int *tot; /* total count of nodes before compressing,
6544 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006545{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 wordnode_T *np;
6547 wordnode_T *tp;
6548 wordnode_T *child;
6549 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006550 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006551 int len = 0;
6552 unsigned nr, n;
6553 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006554
Bram Moolenaar51485f02005-06-04 21:55:20 +00006555 /*
6556 * Go through the list of siblings. Compress each child and then try
6557 * finding an identical child to replace it.
6558 * Note that with "child" we mean not just the node that is pointed to,
6559 * but the whole list of siblings, of which the node is the first.
6560 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006561 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006563 ++len;
6564 if ((child = np->wn_child) != NULL)
6565 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006566 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006567 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006568
6569 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006570 hash = hash_hash(child->wn_u1.hashkey);
6571 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006572 tp = NULL;
6573 if (!HASHITEM_EMPTY(hi))
6574 {
6575 /* There are children with an identical hash value. Now check
6576 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006577 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006578 if (node_equal(child, tp))
6579 {
6580 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006581 * current one. This means the current child and all
6582 * its siblings is unlinked from the tree. */
6583 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006584 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006585 np->wn_child = tp;
6586 ++compressed;
6587 break;
6588 }
6589 if (tp == NULL)
6590 {
6591 /* No other child with this hash value equals the child of
6592 * the node, add it to the linked list after the first
6593 * item. */
6594 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006595 child->wn_u2.next = tp->wn_u2.next;
6596 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006597 }
6598 }
6599 else
6600 /* No other child has this hash value, add it to the
6601 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006602 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006603 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006604 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006605 *tot += len;
6606
6607 /*
6608 * Make a hash key for the node and its siblings, so that we can quickly
6609 * find a lookalike node. This must be done after compressing the sibling
6610 * list, otherwise the hash key would become invalid by the compression.
6611 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006612 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006613 nr = 0;
6614 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006615 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006616 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006617 /* end node: use wn_flags, wn_region and wn_affixID */
6618 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006619 else
6620 /* byte node: use the byte value and the child pointer */
6621 n = np->wn_byte + ((long_u)np->wn_child << 8);
6622 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006623 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006624
6625 /* Avoid NUL bytes, it terminates the hash key. */
6626 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006627 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006628 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006629 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006630 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006631 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006632 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006633 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6634 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006635
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006636 /* Check for CTRL-C pressed now and then. */
6637 fast_breakcheck();
6638
Bram Moolenaar51485f02005-06-04 21:55:20 +00006639 return compressed;
6640}
6641
6642/*
6643 * Return TRUE when two nodes have identical siblings and children.
6644 */
6645 static int
6646node_equal(n1, n2)
6647 wordnode_T *n1;
6648 wordnode_T *n2;
6649{
6650 wordnode_T *p1;
6651 wordnode_T *p2;
6652
6653 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6654 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6655 if (p1->wn_byte != p2->wn_byte
6656 || (p1->wn_byte == NUL
6657 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006658 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006659 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006660 : (p1->wn_child != p2->wn_child)))
6661 break;
6662
6663 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006664}
6665
6666/*
6667 * Write a number to file "fd", MSB first, in "len" bytes.
6668 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006669 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006670put_bytes(fd, nr, len)
6671 FILE *fd;
6672 long_u nr;
6673 int len;
6674{
6675 int i;
6676
6677 for (i = len - 1; i >= 0; --i)
6678 putc((int)(nr >> (i * 8)), fd);
6679}
6680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006681static int
6682#ifdef __BORLANDC__
6683_RTLENTRYF
6684#endif
6685rep_compare __ARGS((const void *s1, const void *s2));
6686
6687/*
6688 * Function given to qsort() to sort the REP items on "from" string.
6689 */
6690 static int
6691#ifdef __BORLANDC__
6692_RTLENTRYF
6693#endif
6694rep_compare(s1, s2)
6695 const void *s1;
6696 const void *s2;
6697{
6698 fromto_T *p1 = (fromto_T *)s1;
6699 fromto_T *p2 = (fromto_T *)s2;
6700
6701 return STRCMP(p1->ft_from, p2->ft_from);
6702}
6703
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006704/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006705 * Write the Vim .spl file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006706 */
6707 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006708write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006709 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006710 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006711{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006712 FILE *fd;
6713 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006714 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 wordnode_T *tree;
6716 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006717 int i;
6718 int l;
6719 garray_T *gap;
6720 fromto_T *ftp;
6721 char_u *p;
6722 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006723
Bram Moolenaarb765d632005-06-07 21:00:02 +00006724 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006725 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006726 {
6727 EMSG2(_(e_notopen), fname);
6728 return;
6729 }
6730
Bram Moolenaar5195e452005-08-19 20:32:47 +00006731 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006732 /* <fileID> */
6733 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
6734 EMSG(_(e_write));
Bram Moolenaar5195e452005-08-19 20:32:47 +00006735 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006736
Bram Moolenaar5195e452005-08-19 20:32:47 +00006737 /*
6738 * <SECTIONS>: <section> ... <sectionend>
6739 */
6740
6741 /* SN_REGION: <regionname> ...
6742 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006743 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006744 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006745 putc(SN_REGION, fd); /* <sectionID> */
6746 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6747 l = spin->si_region_count * 2;
6748 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6749 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6750 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006751 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006752 }
6753 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006754 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006755
Bram Moolenaar5195e452005-08-19 20:32:47 +00006756 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6757 *
6758 * The table with character flags and the table for case folding.
6759 * This makes sure the same characters are recognized as word characters
6760 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006761 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006762 * 'encoding'.
6763 * Also skip this for an .add.spl file, the main spell file must contain
6764 * the table (avoids that it conflicts). File is shorter too.
6765 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006766 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006767 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006768 char_u folchars[128 * 8];
6769 int flags;
6770
Bram Moolenaard12a1322005-08-21 22:08:24 +00006771 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006772 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6773
6774 /* Form the <folchars> string first, we need to know its length. */
6775 l = 0;
6776 for (i = 128; i < 256; ++i)
6777 {
6778#ifdef FEAT_MBYTE
6779 if (has_mbyte)
6780 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6781 else
6782#endif
6783 folchars[l++] = spelltab.st_fold[i];
6784 }
6785 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6786
6787 fputc(128, fd); /* <charflagslen> */
6788 for (i = 128; i < 256; ++i)
6789 {
6790 flags = 0;
6791 if (spelltab.st_isw[i])
6792 flags |= CF_WORD;
6793 if (spelltab.st_isu[i])
6794 flags |= CF_UPPER;
6795 fputc(flags, fd); /* <charflags> */
6796 }
6797
6798 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6799 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006800 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006801
Bram Moolenaar5195e452005-08-19 20:32:47 +00006802 /* SN_MIDWORD: <midword> */
6803 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006804 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006805 putc(SN_MIDWORD, fd); /* <sectionID> */
6806 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6807
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006808 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006809 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006810 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6811 }
6812
Bram Moolenaar5195e452005-08-19 20:32:47 +00006813 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6814 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006815 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006816 putc(SN_PREFCOND, fd); /* <sectionID> */
6817 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6818
6819 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6820 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6821
6822 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006823 }
6824
Bram Moolenaar5195e452005-08-19 20:32:47 +00006825 /* SN_REP: <repcount> <rep> ...
6826 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006828 /* Sort the REP items. */
6829 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6830 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006831
Bram Moolenaar5195e452005-08-19 20:32:47 +00006832 /* round 1: SN_REP section
6833 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006834 for (round = 1; round <= 2; ++round)
6835 {
6836 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006838 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006839 putc(SN_REP, fd); /* <sectionID> */
6840 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006841 else
6842 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006843 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6844 /* using SN_SOFO section instead of SN_SAL */
6845 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006846 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006847 putc(SN_SAL, fd); /* <sectionID> */
6848 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006849
Bram Moolenaar5195e452005-08-19 20:32:47 +00006850 /* This is for making suggestions, section is not required. */
6851 putc(0, fd); /* <sectionflags> */
6852
6853 /* Compute the length of what follows. */
6854 l = 2; /* count <repcount> or <salcount> */
6855 for (i = 0; i < gap->ga_len; ++i)
6856 {
6857 ftp = &((fromto_T *)gap->ga_data)[i];
6858 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6859 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6860 }
6861 if (round == 2)
6862 ++l; /* count <salflags> */
6863 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6864
6865 if (round == 2)
6866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006867 i = 0;
6868 if (spin->si_followup)
6869 i |= SAL_F0LLOWUP;
6870 if (spin->si_collapse)
6871 i |= SAL_COLLAPSE;
6872 if (spin->si_rem_accents)
6873 i |= SAL_REM_ACCENTS;
6874 putc(i, fd); /* <salflags> */
6875 }
6876
6877 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6878 for (i = 0; i < gap->ga_len; ++i)
6879 {
6880 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6881 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6882 ftp = &((fromto_T *)gap->ga_data)[i];
6883 for (rr = 1; rr <= 2; ++rr)
6884 {
6885 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6886 l = STRLEN(p);
6887 putc(l, fd);
6888 fwrite(p, l, (size_t)1, fd);
6889 }
6890 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006892 }
6893
Bram Moolenaar5195e452005-08-19 20:32:47 +00006894 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6895 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006896 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6897 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006898 putc(SN_SOFO, fd); /* <sectionID> */
6899 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006900
6901 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006902 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6903 /* <sectionlen> */
6904
6905 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6906 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006907
6908 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006909 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
6910 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006911 }
6912
Bram Moolenaar5195e452005-08-19 20:32:47 +00006913 /* SN_MAP: <mapstr>
6914 * This is for making suggestions, section is not required. */
6915 if (spin->si_map.ga_len > 0)
6916 {
6917 putc(SN_MAP, fd); /* <sectionID> */
6918 putc(0, fd); /* <sectionflags> */
6919 l = spin->si_map.ga_len;
6920 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6921 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
6922 /* <mapstr> */
6923 }
6924
6925 /* SN_COMPOUND: compound info.
6926 * We don't mark it required, when not supported all compound words will
6927 * be bad words. */
6928 if (spin->si_compflags != NULL)
6929 {
6930 putc(SN_COMPOUND, fd); /* <sectionID> */
6931 putc(0, fd); /* <sectionflags> */
6932
6933 l = STRLEN(spin->si_compflags);
6934 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
6935 putc(spin->si_compmax, fd); /* <compmax> */
6936 putc(spin->si_compminlen, fd); /* <compminlen> */
6937 putc(spin->si_compsylmax, fd); /* <compsylmax> */
6938 /* <compflags> */
6939 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
6940 }
6941
Bram Moolenaar78622822005-08-23 21:00:13 +00006942 /* SN_NOBREAK: NOBREAK flag */
6943 if (spin->si_nobreak)
6944 {
6945 putc(SN_NOBREAK, fd); /* <sectionID> */
6946 putc(0, fd); /* <sectionflags> */
6947
6948 /* It's empty, the precense of the section flags the feature. */
6949 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
6950 }
6951
Bram Moolenaar5195e452005-08-19 20:32:47 +00006952 /* SN_SYLLABLE: syllable info.
6953 * We don't mark it required, when not supported syllables will not be
6954 * counted. */
6955 if (spin->si_syllable != NULL)
6956 {
6957 putc(SN_SYLLABLE, fd); /* <sectionID> */
6958 putc(0, fd); /* <sectionflags> */
6959
6960 l = STRLEN(spin->si_syllable);
6961 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6962 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
6963 }
6964
6965 /* end of <SECTIONS> */
6966 putc(SN_END, fd); /* <sectionend> */
6967
Bram Moolenaar50cde822005-06-05 21:54:54 +00006968
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006969 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006970 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006971 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006972 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006973 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006974 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006975 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006976 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006977 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006978 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006979 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006980 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006981
Bram Moolenaar0c405862005-06-22 22:26:26 +00006982 /* Clear the index and wnode fields in the tree. */
6983 clear_node(tree);
6984
Bram Moolenaar51485f02005-06-04 21:55:20 +00006985 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00006986 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00006987 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006988 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006989
Bram Moolenaar51485f02005-06-04 21:55:20 +00006990 /* number of nodes in 4 bytes */
6991 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00006992 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006993
Bram Moolenaar51485f02005-06-04 21:55:20 +00006994 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006995 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006996 }
6997
Bram Moolenaar51485f02005-06-04 21:55:20 +00006998 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006999}
7000
7001/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007002 * Clear the index and wnode fields of "node", it siblings and its
7003 * children. This is needed because they are a union with other items to save
7004 * space.
7005 */
7006 static void
7007clear_node(node)
7008 wordnode_T *node;
7009{
7010 wordnode_T *np;
7011
7012 if (node != NULL)
7013 for (np = node; np != NULL; np = np->wn_sibling)
7014 {
7015 np->wn_u1.index = 0;
7016 np->wn_u2.wnode = NULL;
7017
7018 if (np->wn_byte != NUL)
7019 clear_node(np->wn_child);
7020 }
7021}
7022
7023
7024/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007025 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007026 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007027 * This first writes the list of possible bytes (siblings). Then for each
7028 * byte recursively write the children.
7029 *
7030 * NOTE: The code here must match the code in read_tree(), since assumptions
7031 * are made about the indexes (so that we don't have to write them in the
7032 * file).
7033 *
7034 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007035 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007036 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007037put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007038 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007039 wordnode_T *node;
7040 int index;
7041 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007042 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007043{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007044 int newindex = index;
7045 int siblingcount = 0;
7046 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007047 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007048
Bram Moolenaar51485f02005-06-04 21:55:20 +00007049 /* If "node" is zero the tree is empty. */
7050 if (node == NULL)
7051 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007052
Bram Moolenaar51485f02005-06-04 21:55:20 +00007053 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007054 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007055
7056 /* Count the number of siblings. */
7057 for (np = node; np != NULL; np = np->wn_sibling)
7058 ++siblingcount;
7059
7060 /* Write the sibling count. */
7061 if (fd != NULL)
7062 putc(siblingcount, fd); /* <siblingcount> */
7063
7064 /* Write each sibling byte and optionally extra info. */
7065 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007066 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007067 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007068 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007069 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007070 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007071 /* For a NUL byte (end of word) write the flags etc. */
7072 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007073 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007074 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007075 * associated condition nr (stored in wn_region). The
7076 * byte value is misused to store the "rare" and "not
7077 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007078 if (np->wn_flags == (short_u)PFX_FLAGS)
7079 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007080 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007081 {
7082 putc(BY_FLAGS, fd); /* <byte> */
7083 putc(np->wn_flags, fd); /* <pflags> */
7084 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007085 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007086 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 }
7088 else
7089 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007090 /* For word trees we write the flag/region items. */
7091 flags = np->wn_flags;
7092 if (regionmask != 0 && np->wn_region != regionmask)
7093 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007094 if (np->wn_affixID != 0)
7095 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007096 if (flags == 0)
7097 {
7098 /* word without flags or region */
7099 putc(BY_NOFLAGS, fd); /* <byte> */
7100 }
7101 else
7102 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007103 if (np->wn_flags >= 0x100)
7104 {
7105 putc(BY_FLAGS2, fd); /* <byte> */
7106 putc(flags, fd); /* <flags> */
7107 putc((unsigned)flags >> 8, fd); /* <flags2> */
7108 }
7109 else
7110 {
7111 putc(BY_FLAGS, fd); /* <byte> */
7112 putc(flags, fd); /* <flags> */
7113 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007114 if (flags & WF_REGION)
7115 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007116 if (flags & WF_AFX)
7117 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007118 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007119 }
7120 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007121 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007122 else
7123 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007124 if (np->wn_child->wn_u1.index != 0
7125 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007126 {
7127 /* The child is written elsewhere, write the reference. */
7128 if (fd != NULL)
7129 {
7130 putc(BY_INDEX, fd); /* <byte> */
7131 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007132 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007133 }
7134 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007135 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007136 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007137 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007138
Bram Moolenaar51485f02005-06-04 21:55:20 +00007139 if (fd != NULL)
7140 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7141 {
7142 EMSG(_(e_write));
7143 return 0;
7144 }
7145 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007146 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007147
7148 /* Space used in the array when reading: one for each sibling and one for
7149 * the count. */
7150 newindex += siblingcount + 1;
7151
7152 /* Recursively dump the children of each sibling. */
7153 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007154 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7155 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007156 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007157
7158 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007159}
7160
7161
7162/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007163 * ":mkspell [-ascii] outfile infile ..."
7164 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007165 */
7166 void
7167ex_mkspell(eap)
7168 exarg_T *eap;
7169{
7170 int fcount;
7171 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007172 char_u *arg = eap->arg;
7173 int ascii = FALSE;
7174
7175 if (STRNCMP(arg, "-ascii", 6) == 0)
7176 {
7177 ascii = TRUE;
7178 arg = skipwhite(arg + 6);
7179 }
7180
7181 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7182 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007184 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007185 FreeWild(fcount, fnames);
7186 }
7187}
7188
7189/*
7190 * Create a Vim spell file from one or more word lists.
7191 * "fnames[0]" is the output file name.
7192 * "fnames[fcount - 1]" is the last input file name.
7193 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7194 * and ".spl" is appended to make the output file name.
7195 */
7196 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007197mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007198 int fcount;
7199 char_u **fnames;
7200 int ascii; /* -ascii argument given */
7201 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007202 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007203{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007204 char_u fname[MAXPATHL];
7205 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007206 char_u **innames;
7207 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007208 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007209 int i;
7210 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007211 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007212 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007213 spellinfo_T spin;
7214
7215 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007216 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007217 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007218 spin.si_followup = TRUE;
7219 spin.si_rem_accents = TRUE;
7220 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7221 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7222 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007223 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar6de68532005-08-24 22:08:48 +00007224 spin.si_compID = 255; /* start compound ID at maximum, going down */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007225
Bram Moolenaarb765d632005-06-07 21:00:02 +00007226 /* default: fnames[0] is output file, following are input files */
7227 innames = &fnames[1];
7228 incount = fcount - 1;
7229
7230 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007231 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007232 len = STRLEN(fnames[0]);
7233 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7234 {
7235 /* For ":mkspell path/en.latin1.add" output file is
7236 * "path/en.latin1.add.spl". */
7237 innames = &fnames[0];
7238 incount = 1;
7239 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7240 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007241 else if (fcount == 1)
7242 {
7243 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7244 innames = &fnames[0];
7245 incount = 1;
7246 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7247 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7248 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007249 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7250 {
7251 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007252 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007253 }
7254 else
7255 /* Name should be language, make the file name from it. */
7256 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7257 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7258
7259 /* Check for .ascii.spl. */
7260 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7261 spin.si_ascii = TRUE;
7262
7263 /* Check for .add.spl. */
7264 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7265 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007266 }
7267
Bram Moolenaarb765d632005-06-07 21:00:02 +00007268 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007269 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007270 else if (vim_strchr(gettail(wfname), '_') != NULL)
7271 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007272 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007273 EMSG(_("E754: Only up to 8 regions supported"));
7274 else
7275 {
7276 /* Check for overwriting before doing things that may take a lot of
7277 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007278 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007279 {
7280 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007281 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007282 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007283 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007284 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007285 EMSG2(_(e_isadir2), wfname);
7286 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007287 }
7288
7289 /*
7290 * Init the aff and dic pointers.
7291 * Get the region names if there are more than 2 arguments.
7292 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007293 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007294 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007295 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007296
Bram Moolenaar3982c542005-06-08 21:56:31 +00007297 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007298 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007299 len = STRLEN(innames[i]);
7300 if (STRLEN(gettail(innames[i])) < 5
7301 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007302 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007303 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7304 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007305 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007306 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7307 spin.si_region_name[i * 2 + 1] =
7308 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007309 }
7310 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007311 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007312
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007313 spin.si_foldroot = wordtree_alloc(&spin);
7314 spin.si_keeproot = wordtree_alloc(&spin);
7315 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007316 if (spin.si_foldroot == NULL
7317 || spin.si_keeproot == NULL
7318 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007319 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007320 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007321 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007322 }
7323
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007324 /* When not producing a .add.spl file clear the character table when
7325 * we encounter one in the .aff file. This means we dump the current
7326 * one in the .spl file if the .aff file doesn't define one. That's
7327 * better than guessing the contents, the table will match a
7328 * previously loaded spell file. */
7329 if (!spin.si_add)
7330 spin.si_clear_chartab = TRUE;
7331
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007332 /*
7333 * Read all the .aff and .dic files.
7334 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007335 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007336 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007337 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007338 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007339 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007340 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007341
Bram Moolenaarb765d632005-06-07 21:00:02 +00007342 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007343 if (mch_stat((char *)fname, &st) >= 0)
7344 {
7345 /* Read the .aff file. Will init "spin->si_conv" based on the
7346 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007347 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007348 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007349 error = TRUE;
7350 else
7351 {
7352 /* Read the .dic file and store the words in the trees. */
7353 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007354 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007355 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007356 error = TRUE;
7357 }
7358 }
7359 else
7360 {
7361 /* No .aff file, try reading the file as a word list. Store
7362 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007363 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007364 error = TRUE;
7365 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007366
Bram Moolenaarb765d632005-06-07 21:00:02 +00007367#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007368 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007369 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007370#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007371 }
7372
Bram Moolenaar78622822005-08-23 21:00:13 +00007373 if (spin.si_compflags != NULL && spin.si_nobreak)
7374 MSG(_("Warning: both compounding and NOBREAK specified"));
7375
Bram Moolenaar51485f02005-06-04 21:55:20 +00007376 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007377 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007378 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007379 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007380 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007381 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007382 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007383 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007384 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007385 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007386 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007387 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007388 verbose_leave();
7389 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007390 wordtree_compress(&spin, spin.si_foldroot);
7391 wordtree_compress(&spin, spin.si_keeproot);
7392 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007393 }
7394
Bram Moolenaar51485f02005-06-04 21:55:20 +00007395 if (!error)
7396 {
7397 /*
7398 * Write the info in the spell file.
7399 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007400 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007401 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007402 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007403 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007404 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007405 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007406 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007407 verbose_leave();
7408 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007409
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007410 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007411
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007412 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007413 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007414 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007415 verbose_enter();
7416 MSG(_("Done!"));
7417 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007418 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007419 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007420 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007421 verbose_leave();
7422 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007423
Bram Moolenaarb765d632005-06-07 21:00:02 +00007424 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007425 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007426 }
7427
7428 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007429 ga_clear(&spin.si_rep);
7430 ga_clear(&spin.si_sal);
7431 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007432 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007433
7434 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007435 for (i = 0; i < incount; ++i)
7436 if (afile[i] != NULL)
7437 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007438
7439 /* Free all the bits and pieces at once. */
7440 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007441 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007442}
7443
Bram Moolenaarb765d632005-06-07 21:00:02 +00007444
7445/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007446 * ":[count]spellgood {word}"
7447 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007448 */
7449 void
7450ex_spell(eap)
7451 exarg_T *eap;
7452{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007453 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007454 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007455}
7456
7457/*
7458 * Add "word[len]" to 'spellfile' as a good or bad word.
7459 */
7460 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007461spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007462 char_u *word;
7463 int len;
7464 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007465 int index; /* "zG" and "zW": zero, otherwise index in
7466 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007467{
7468 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007469 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007470 int new_spf = FALSE;
7471 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007472 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007473 char_u fnamebuf[MAXPATHL];
7474 char_u line[MAXWLEN * 2];
7475 long fpos, fpos_next = 0;
7476 int i;
7477 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007478
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007479 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007480 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007481 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007482 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007483 int_wordlist = vim_tempname('s');
7484 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007485 return;
7486 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007487 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007488 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007489 else
7490 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007491 /* If 'spellfile' isn't set figure out a good default value. */
7492 if (*curbuf->b_p_spf == NUL)
7493 {
7494 init_spellfile();
7495 new_spf = TRUE;
7496 }
7497
7498 if (*curbuf->b_p_spf == NUL)
7499 {
7500 EMSG(_("E764: 'spellfile' is not set"));
7501 return;
7502 }
7503
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007504 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7505 {
7506 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7507 if (i == index)
7508 break;
7509 if (*spf == NUL)
7510 {
7511 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
7512 return;
7513 }
7514 }
7515
Bram Moolenaarb765d632005-06-07 21:00:02 +00007516 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007517 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007518 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7519 buf = NULL;
7520 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007521 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007522 EMSG(_(e_bufloaded));
7523 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007524 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007525
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007526 fname = fnamebuf;
7527 }
7528
7529 if (bad)
7530 {
7531 /* When the word also appears as good word we need to remove that one,
7532 * since its flags sort before the one with WF_BANNED. */
7533 fd = mch_fopen((char *)fname, "r");
7534 if (fd != NULL)
7535 {
7536 while (!vim_fgets(line, MAXWLEN * 2, fd))
7537 {
7538 fpos = fpos_next;
7539 fpos_next = ftell(fd);
7540 if (STRNCMP(word, line, len) == 0
7541 && (line[len] == '/' || line[len] < ' '))
7542 {
7543 /* Found duplicate word. Remove it by writing a '#' at
7544 * the start of the line. Mixing reading and writing
7545 * doesn't work for all systems, close the file first. */
7546 fclose(fd);
7547 fd = mch_fopen((char *)fname, "r+");
7548 if (fd == NULL)
7549 break;
7550 if (fseek(fd, fpos, SEEK_SET) == 0)
7551 fputc('#', fd);
7552 fseek(fd, fpos_next, SEEK_SET);
7553 }
7554 }
7555 fclose(fd);
7556 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007557 }
7558
7559 fd = mch_fopen((char *)fname, "a");
7560 if (fd == NULL && new_spf)
7561 {
7562 /* We just initialized the 'spellfile' option and can't open the file.
7563 * We may need to create the "spell" directory first. We already
7564 * checked the runtime directory is writable in init_spellfile(). */
7565 STRCPY(NameBuff, fname);
7566 *gettail_sep(NameBuff) = NUL;
7567 if (mch_stat((char *)NameBuff, &st) < 0)
7568 {
7569 /* The directory doesn't exist. Try creating it and opening the
7570 * file again. */
7571 vim_mkdir(NameBuff, 0755);
7572 fd = mch_fopen((char *)fname, "a");
7573 }
7574 }
7575
7576 if (fd == NULL)
7577 EMSG2(_(e_notopen), fname);
7578 else
7579 {
7580 if (bad)
7581 fprintf(fd, "%.*s/!\n", len, word);
7582 else
7583 fprintf(fd, "%.*s\n", len, word);
7584 fclose(fd);
7585
7586 /* Update the .add.spl file. */
7587 mkspell(1, &fname, FALSE, TRUE, TRUE);
7588
7589 /* If the .add file is edited somewhere, reload it. */
7590 if (buf != NULL)
7591 buf_reload(buf);
7592
7593 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007594 }
7595}
7596
7597/*
7598 * Initialize 'spellfile' for the current buffer.
7599 */
7600 static void
7601init_spellfile()
7602{
7603 char_u buf[MAXPATHL];
7604 int l;
7605 slang_T *sl;
7606 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007607 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007608
7609 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7610 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007611 /* Find the end of the language name. Exclude the region. */
7612 for (lend = curbuf->b_p_spl; *lend != NUL
7613 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7614 ;
7615
7616 /* Loop over all entries in 'runtimepath'. Use the first one where we
7617 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007618 rtp = p_rtp;
7619 while (*rtp != NUL)
7620 {
7621 /* Copy the path from 'runtimepath' to buf[]. */
7622 copy_option_part(&rtp, buf, MAXPATHL, ",");
7623 if (filewritable(buf) == 2)
7624 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007625 /* Use the first language name from 'spelllang' and the
7626 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007627 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
7628 l = STRLEN(buf);
7629 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007630 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007631 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00007632 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
7633 ? (char_u *)"ascii" : spell_enc());
7634 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7635 break;
7636 }
7637 }
7638 }
7639}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007640
Bram Moolenaar51485f02005-06-04 21:55:20 +00007641
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007642/*
7643 * Init the chartab used for spelling for ASCII.
7644 * EBCDIC is not supported!
7645 */
7646 static void
7647clear_spell_chartab(sp)
7648 spelltab_T *sp;
7649{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007650 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007651
7652 /* Init everything to FALSE. */
7653 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7654 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7655 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007656 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007657 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007658 sp->st_upper[i] = i;
7659 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007660
7661 /* We include digits. A word shouldn't start with a digit, but handling
7662 * that is done separately. */
7663 for (i = '0'; i <= '9'; ++i)
7664 sp->st_isw[i] = TRUE;
7665 for (i = 'A'; i <= 'Z'; ++i)
7666 {
7667 sp->st_isw[i] = TRUE;
7668 sp->st_isu[i] = TRUE;
7669 sp->st_fold[i] = i + 0x20;
7670 }
7671 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007672 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007673 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007674 sp->st_upper[i] = i - 0x20;
7675 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007676}
7677
7678/*
7679 * Init the chartab used for spelling. Only depends on 'encoding'.
7680 * Called once while starting up and when 'encoding' changes.
7681 * The default is to use isalpha(), but the spell file should define the word
7682 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007683 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007684 */
7685 void
7686init_spell_chartab()
7687{
7688 int i;
7689
7690 did_set_spelltab = FALSE;
7691 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007692#ifdef FEAT_MBYTE
7693 if (enc_dbcs)
7694 {
7695 /* DBCS: assume double-wide characters are word characters. */
7696 for (i = 128; i <= 255; ++i)
7697 if (MB_BYTE2LEN(i) == 2)
7698 spelltab.st_isw[i] = TRUE;
7699 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007700 else if (enc_utf8)
7701 {
7702 for (i = 128; i < 256; ++i)
7703 {
7704 spelltab.st_isu[i] = utf_isupper(i);
7705 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7706 spelltab.st_fold[i] = utf_fold(i);
7707 spelltab.st_upper[i] = utf_toupper(i);
7708 }
7709 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007710 else
7711#endif
7712 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007713 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007714 for (i = 128; i < 256; ++i)
7715 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007716 if (MB_ISUPPER(i))
7717 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007718 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007719 spelltab.st_isu[i] = TRUE;
7720 spelltab.st_fold[i] = MB_TOLOWER(i);
7721 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007722 else if (MB_ISLOWER(i))
7723 {
7724 spelltab.st_isw[i] = TRUE;
7725 spelltab.st_upper[i] = MB_TOUPPER(i);
7726 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007727 }
7728 }
7729}
7730
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007731/*
7732 * Set the spell character tables from strings in the affix file.
7733 */
7734 static int
7735set_spell_chartab(fol, low, upp)
7736 char_u *fol;
7737 char_u *low;
7738 char_u *upp;
7739{
7740 /* We build the new tables here first, so that we can compare with the
7741 * previous one. */
7742 spelltab_T new_st;
7743 char_u *pf = fol, *pl = low, *pu = upp;
7744 int f, l, u;
7745
7746 clear_spell_chartab(&new_st);
7747
7748 while (*pf != NUL)
7749 {
7750 if (*pl == NUL || *pu == NUL)
7751 {
7752 EMSG(_(e_affform));
7753 return FAIL;
7754 }
7755#ifdef FEAT_MBYTE
7756 f = mb_ptr2char_adv(&pf);
7757 l = mb_ptr2char_adv(&pl);
7758 u = mb_ptr2char_adv(&pu);
7759#else
7760 f = *pf++;
7761 l = *pl++;
7762 u = *pu++;
7763#endif
7764 /* Every character that appears is a word character. */
7765 if (f < 256)
7766 new_st.st_isw[f] = TRUE;
7767 if (l < 256)
7768 new_st.st_isw[l] = TRUE;
7769 if (u < 256)
7770 new_st.st_isw[u] = TRUE;
7771
7772 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7773 * case-folding */
7774 if (l < 256 && l != f)
7775 {
7776 if (f >= 256)
7777 {
7778 EMSG(_(e_affrange));
7779 return FAIL;
7780 }
7781 new_st.st_fold[l] = f;
7782 }
7783
7784 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007785 * case-folding, it's upper case and the "UPP" is the upper case of
7786 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007787 if (u < 256 && u != f)
7788 {
7789 if (f >= 256)
7790 {
7791 EMSG(_(e_affrange));
7792 return FAIL;
7793 }
7794 new_st.st_fold[u] = f;
7795 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007796 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007797 }
7798 }
7799
7800 if (*pl != NUL || *pu != NUL)
7801 {
7802 EMSG(_(e_affform));
7803 return FAIL;
7804 }
7805
7806 return set_spell_finish(&new_st);
7807}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007808
7809/*
7810 * Set the spell character tables from strings in the .spl file.
7811 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007812 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007813set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007814 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007815 int cnt; /* length of "flags" */
7816 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007817{
7818 /* We build the new tables here first, so that we can compare with the
7819 * previous one. */
7820 spelltab_T new_st;
7821 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007822 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007823 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007824
7825 clear_spell_chartab(&new_st);
7826
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007827 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007828 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007829 if (i < cnt)
7830 {
7831 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7832 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7833 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007834
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007835 if (*p != NUL)
7836 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007837#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007838 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007839#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007840 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007841#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007842 new_st.st_fold[i + 128] = c;
7843 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7844 new_st.st_upper[c] = i + 128;
7845 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007846 }
7847
Bram Moolenaar5195e452005-08-19 20:32:47 +00007848 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007849}
7850
7851 static int
7852set_spell_finish(new_st)
7853 spelltab_T *new_st;
7854{
7855 int i;
7856
7857 if (did_set_spelltab)
7858 {
7859 /* check that it's the same table */
7860 for (i = 0; i < 256; ++i)
7861 {
7862 if (spelltab.st_isw[i] != new_st->st_isw[i]
7863 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007864 || spelltab.st_fold[i] != new_st->st_fold[i]
7865 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007866 {
7867 EMSG(_("E763: Word characters differ between spell files"));
7868 return FAIL;
7869 }
7870 }
7871 }
7872 else
7873 {
7874 /* copy the new spelltab into the one being used */
7875 spelltab = *new_st;
7876 did_set_spelltab = TRUE;
7877 }
7878
7879 return OK;
7880}
7881
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007882/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007883 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007884 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007885 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007886 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007887 */
7888 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007889spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007890 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007891 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007892{
Bram Moolenaarea408852005-06-25 22:49:46 +00007893#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007894 char_u *s;
7895 int l;
7896 int c;
7897
7898 if (has_mbyte)
7899 {
7900 l = MB_BYTE2LEN(*p);
7901 s = p;
7902 if (l == 1)
7903 {
7904 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007905 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007906 {
7907 s = p + 1; /* skip a mid-word character */
7908 l = MB_BYTE2LEN(*s);
7909 }
7910 }
7911 else
7912 {
7913 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007914 if (c < 256 ? buf->b_spell_ismw[c]
7915 : (buf->b_spell_ismw_mb != NULL
7916 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007917 {
7918 s = p + l;
7919 l = MB_BYTE2LEN(*s);
7920 }
7921 }
7922
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007923 c = mb_ptr2char(s);
7924 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007925 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007926 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007927 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007928#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007929
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007930 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
7931}
7932
7933/*
7934 * Return TRUE if "p" points to a word character.
7935 * Unlike spell_iswordp() this doesn't check for "midword" characters.
7936 */
7937 static int
7938spell_iswordp_nmw(p)
7939 char_u *p;
7940{
7941#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007942 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007943
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007944 if (has_mbyte)
7945 {
7946 c = mb_ptr2char(p);
7947 if (c > 255)
7948 return mb_get_class(p) >= 2;
7949 return spelltab.st_isw[c];
7950 }
7951#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007952 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00007953}
7954
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007955#ifdef FEAT_MBYTE
7956/*
7957 * Return TRUE if "p" points to a word character.
7958 * Wide version of spell_iswordp().
7959 */
7960 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007961spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007962 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007963 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007964{
7965 int *s;
7966
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007967 if (*p < 256 ? buf->b_spell_ismw[*p]
7968 : (buf->b_spell_ismw_mb != NULL
7969 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007970 s = p + 1;
7971 else
7972 s = p;
7973
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007974 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007975 {
7976 if (enc_utf8)
7977 return utf_class(*s) >= 2;
7978 if (enc_dbcs)
7979 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
7980 return 0;
7981 }
7982 return spelltab.st_isw[*s];
7983}
7984#endif
7985
Bram Moolenaarea408852005-06-25 22:49:46 +00007986/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007987 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007988 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007989 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007990 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007991write_spell_prefcond(fd, gap)
7992 FILE *fd;
7993 garray_T *gap;
7994{
7995 int i;
7996 char_u *p;
7997 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007998 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007999
Bram Moolenaar5195e452005-08-19 20:32:47 +00008000 if (fd != NULL)
8001 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8002
8003 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008004
8005 for (i = 0; i < gap->ga_len; ++i)
8006 {
8007 /* <prefcond> : <condlen> <condstr> */
8008 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008009 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008010 {
8011 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008012 if (fd != NULL)
8013 {
8014 fputc(len, fd);
8015 fwrite(p, (size_t)len, (size_t)1, fd);
8016 }
8017 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008018 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008019 else if (fd != NULL)
8020 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008021 }
8022
Bram Moolenaar5195e452005-08-19 20:32:47 +00008023 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008024}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008025
8026/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008027 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8028 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008029 * When using a multi-byte 'encoding' the length may change!
8030 * Returns FAIL when something wrong.
8031 */
8032 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008033spell_casefold(str, len, buf, buflen)
8034 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008035 int len;
8036 char_u *buf;
8037 int buflen;
8038{
8039 int i;
8040
8041 if (len >= buflen)
8042 {
8043 buf[0] = NUL;
8044 return FAIL; /* result will not fit */
8045 }
8046
8047#ifdef FEAT_MBYTE
8048 if (has_mbyte)
8049 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008050 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008051 char_u *p;
8052 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008053
8054 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008055 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008057 if (outi + MB_MAXBYTES > buflen)
8058 {
8059 buf[outi] = NUL;
8060 return FAIL;
8061 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008062 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008063 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008064 }
8065 buf[outi] = NUL;
8066 }
8067 else
8068#endif
8069 {
8070 /* Be quick for non-multibyte encodings. */
8071 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008072 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008073 buf[i] = NUL;
8074 }
8075
8076 return OK;
8077}
8078
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008079#define SPS_BEST 1
8080#define SPS_FAST 2
8081#define SPS_DOUBLE 4
8082
8083static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008084static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008085
8086/*
8087 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008088 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008089 */
8090 int
8091spell_check_sps()
8092{
8093 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008094 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008095 char_u buf[MAXPATHL];
8096 int f;
8097
8098 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008099 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008100
8101 for (p = p_sps; *p != NUL; )
8102 {
8103 copy_option_part(&p, buf, MAXPATHL, ",");
8104
8105 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008106 if (VIM_ISDIGIT(*buf))
8107 {
8108 s = buf;
8109 sps_limit = getdigits(&s);
8110 if (*s != NUL && !VIM_ISDIGIT(*s))
8111 f = -1;
8112 }
8113 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008114 f = SPS_BEST;
8115 else if (STRCMP(buf, "fast") == 0)
8116 f = SPS_FAST;
8117 else if (STRCMP(buf, "double") == 0)
8118 f = SPS_DOUBLE;
8119 else if (STRNCMP(buf, "expr:", 5) != 0
8120 && STRNCMP(buf, "file:", 5) != 0)
8121 f = -1;
8122
8123 if (f == -1 || (sps_flags != 0 && f != 0))
8124 {
8125 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008126 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008127 return FAIL;
8128 }
8129 if (f != 0)
8130 sps_flags = f;
8131 }
8132
8133 if (sps_flags == 0)
8134 sps_flags = SPS_BEST;
8135
8136 return OK;
8137}
8138
8139/* Remember what "z?" replaced. */
8140static char_u *repl_from = NULL;
8141static char_u *repl_to = NULL;
8142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008143/*
8144 * "z?": Find badly spelled word under or after the cursor.
8145 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008146 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008147 */
8148 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008149spell_suggest(count)
8150 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151{
8152 char_u *line;
8153 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154 char_u wcopy[MAXWLEN + 2];
8155 char_u *p;
8156 int i;
8157 int c;
8158 suginfo_T sug;
8159 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008160 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008161 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008162 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008163 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008164
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008165 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008166 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008167 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008168 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008169 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8170 return;
8171
8172 /* No bad word or it starts after the cursor: use the word under the
8173 * cursor. */
8174 curwin->w_cursor = prev_cursor;
8175 line = ml_get_curline();
8176 p = line + curwin->w_cursor.col;
8177 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008178 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008179 mb_ptr_back(line, p);
8180 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008181 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008182 mb_ptr_adv(p);
8183
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008184 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008185 {
8186 beep_flush();
8187 return;
8188 }
8189 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008190 }
8191
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008192 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008193
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008194 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008195 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008196
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008197 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008198
Bram Moolenaar5195e452005-08-19 20:32:47 +00008199 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8200 * 'spellsuggest', whatever is smaller. */
8201 if (sps_limit > (int)Rows - 2)
8202 limit = (int)Rows - 2;
8203 else
8204 limit = sps_limit;
8205 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008206 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008207
8208 if (sug.su_ga.ga_len == 0)
8209 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008210 else if (count > 0)
8211 {
8212 if (count > sug.su_ga.ga_len)
8213 smsg((char_u *)_("Sorry, only %ld suggestions"),
8214 (long)sug.su_ga.ga_len);
8215 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008216 else
8217 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008218 vim_free(repl_from);
8219 repl_from = NULL;
8220 vim_free(repl_to);
8221 repl_to = NULL;
8222
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008223#ifdef FEAT_RIGHTLEFT
8224 /* When 'rightleft' is set the list is drawn right-left. */
8225 cmdmsg_rl = curwin->w_p_rl;
8226 if (cmdmsg_rl)
8227 msg_col = Columns - 1;
8228#endif
8229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008230 /* List the suggestions. */
8231 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008232 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008233 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8234 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008235#ifdef FEAT_RIGHTLEFT
8236 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8237 {
8238 /* And now the rabbit from the high hat: Avoid showing the
8239 * untranslated message rightleft. */
8240 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8241 sug.su_badlen, sug.su_badptr);
8242 }
8243#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008244 msg_puts(IObuff);
8245 msg_clr_eos();
8246 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008248 msg_scroll = TRUE;
8249 for (i = 0; i < sug.su_ga.ga_len; ++i)
8250 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008251 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252
8253 /* The suggested word may replace only part of the bad word, add
8254 * the not replaced part. */
8255 STRCPY(wcopy, stp->st_word);
8256 if (sug.su_badlen > stp->st_orglen)
8257 vim_strncpy(wcopy + STRLEN(wcopy),
8258 sug.su_badptr + stp->st_orglen,
8259 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008260 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8261#ifdef FEAT_RIGHTLEFT
8262 if (cmdmsg_rl)
8263 rl_mirror(IObuff);
8264#endif
8265 msg_puts(IObuff);
8266
8267 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008268 msg_puts(IObuff);
8269
8270 /* The word may replace more than "su_badlen". */
8271 if (sug.su_badlen < stp->st_orglen)
8272 {
8273 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8274 stp->st_orglen, sug.su_badptr);
8275 msg_puts(IObuff);
8276 }
8277
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008278 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008279 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008280 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008281 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008282 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008283 stp->st_salscore ? "s " : "",
8284 stp->st_score, stp->st_altscore);
8285 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008286 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008287 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008288#ifdef FEAT_RIGHTLEFT
8289 if (cmdmsg_rl)
8290 /* Mirror the numbers, but keep the leading space. */
8291 rl_mirror(IObuff + 1);
8292#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008293 msg_advance(30);
8294 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008296 msg_putchar('\n');
8297 }
8298
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008299#ifdef FEAT_RIGHTLEFT
8300 cmdmsg_rl = FALSE;
8301 msg_col = 0;
8302#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008303 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008304 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008305 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008306 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008307 }
8308
Bram Moolenaard12a1322005-08-21 22:08:24 +00008309 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8310 {
8311 /* Save the from and to text for :spellrepall. */
8312 stp = &SUG(sug.su_ga, selected - 1);
8313 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8314 repl_to = vim_strsave(stp->st_word);
8315
8316 /* Replace the word. */
8317 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8318 if (p != NULL)
8319 {
8320 c = sug.su_badptr - line;
8321 mch_memmove(p, line, c);
8322 STRCPY(p + c, stp->st_word);
8323 STRCAT(p, sug.su_badptr + stp->st_orglen);
8324 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8325 curwin->w_cursor.col = c;
8326 changed_bytes(curwin->w_cursor.lnum, c);
8327
8328 /* For redo we use a change-word command. */
8329 ResetRedobuff();
8330 AppendToRedobuff((char_u *)"ciw");
8331 AppendToRedobuff(stp->st_word);
8332 AppendCharToRedobuff(ESC);
8333 }
8334 }
8335 else
8336 curwin->w_cursor = prev_cursor;
8337
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008338 spell_find_cleanup(&sug);
8339}
8340
8341/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008342 * Check if the word at line "lnum" column "col" is required to start with a
8343 * capital. This uses 'spellcapcheck' of the current buffer.
8344 */
8345 static int
8346check_need_cap(lnum, col)
8347 linenr_T lnum;
8348 colnr_T col;
8349{
8350 int need_cap = FALSE;
8351 char_u *line;
8352 char_u *line_copy = NULL;
8353 char_u *p;
8354 colnr_T endcol;
8355 regmatch_T regmatch;
8356
8357 if (curbuf->b_cap_prog == NULL)
8358 return FALSE;
8359
8360 line = ml_get_curline();
8361 endcol = 0;
8362 if ((int)(skipwhite(line) - line) >= (int)col)
8363 {
8364 /* At start of line, check if previous line is empty or sentence
8365 * ends there. */
8366 if (lnum == 1)
8367 need_cap = TRUE;
8368 else
8369 {
8370 line = ml_get(lnum - 1);
8371 if (*skipwhite(line) == NUL)
8372 need_cap = TRUE;
8373 else
8374 {
8375 /* Append a space in place of the line break. */
8376 line_copy = concat_str(line, (char_u *)" ");
8377 line = line_copy;
8378 endcol = STRLEN(line);
8379 }
8380 }
8381 }
8382 else
8383 endcol = col;
8384
8385 if (endcol > 0)
8386 {
8387 /* Check if sentence ends before the bad word. */
8388 regmatch.regprog = curbuf->b_cap_prog;
8389 regmatch.rm_ic = FALSE;
8390 p = line + endcol;
8391 for (;;)
8392 {
8393 mb_ptr_back(line, p);
8394 if (p == line || spell_iswordp_nmw(p))
8395 break;
8396 if (vim_regexec(&regmatch, p, 0)
8397 && regmatch.endp[0] == line + endcol)
8398 {
8399 need_cap = TRUE;
8400 break;
8401 }
8402 }
8403 }
8404
8405 vim_free(line_copy);
8406
8407 return need_cap;
8408}
8409
8410
8411/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008412 * ":spellrepall"
8413 */
8414/*ARGSUSED*/
8415 void
8416ex_spellrepall(eap)
8417 exarg_T *eap;
8418{
8419 pos_T pos = curwin->w_cursor;
8420 char_u *frompat;
8421 int addlen;
8422 char_u *line;
8423 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008424 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008425 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008426
8427 if (repl_from == NULL || repl_to == NULL)
8428 {
8429 EMSG(_("E752: No previous spell replacement"));
8430 return;
8431 }
8432 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8433
8434 frompat = alloc(STRLEN(repl_from) + 7);
8435 if (frompat == NULL)
8436 return;
8437 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8438 p_ws = FALSE;
8439
Bram Moolenaar5195e452005-08-19 20:32:47 +00008440 sub_nsubs = 0;
8441 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008442 curwin->w_cursor.lnum = 0;
8443 while (!got_int)
8444 {
8445 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8446 || u_save_cursor() == FAIL)
8447 break;
8448
8449 /* Only replace when the right word isn't there yet. This happens
8450 * when changing "etc" to "etc.". */
8451 line = ml_get_curline();
8452 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8453 repl_to, STRLEN(repl_to)) != 0)
8454 {
8455 p = alloc(STRLEN(line) + addlen + 1);
8456 if (p == NULL)
8457 break;
8458 mch_memmove(p, line, curwin->w_cursor.col);
8459 STRCPY(p + curwin->w_cursor.col, repl_to);
8460 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8461 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8462 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008463
8464 if (curwin->w_cursor.lnum != prev_lnum)
8465 {
8466 ++sub_nlines;
8467 prev_lnum = curwin->w_cursor.lnum;
8468 }
8469 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008470 }
8471 curwin->w_cursor.col += STRLEN(repl_to);
8472 }
8473
8474 p_ws = save_ws;
8475 curwin->w_cursor = pos;
8476 vim_free(frompat);
8477
Bram Moolenaar5195e452005-08-19 20:32:47 +00008478 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008479 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008480 else
8481 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008482}
8483
8484/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008485 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8486 * a list of allocated strings.
8487 */
8488 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008489spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008490 garray_T *gap;
8491 char_u *word;
8492 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008493 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008494{
8495 suginfo_T sug;
8496 int i;
8497 suggest_T *stp;
8498 char_u *wcopy;
8499
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008500 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008501
8502 /* Make room in "gap". */
8503 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8504 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8505 return;
8506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008507 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008508 {
8509 stp = &SUG(sug.su_ga, i);
8510
8511 /* The suggested word may replace only part of "word", add the not
8512 * replaced part. */
8513 wcopy = alloc(STRLEN(stp->st_word)
8514 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8515 if (wcopy == NULL)
8516 break;
8517 STRCPY(wcopy, stp->st_word);
8518 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8519 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8520 }
8521
8522 spell_find_cleanup(&sug);
8523}
8524
8525/*
8526 * Find spell suggestions for the word at the start of "badptr".
8527 * Return the suggestions in "su->su_ga".
8528 * The maximum number of suggestions is "maxcount".
8529 * Note: does use info for the current window.
8530 * This is based on the mechanisms of Aspell, but completely reimplemented.
8531 */
8532 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008533spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008534 char_u *badptr;
8535 suginfo_T *su;
8536 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008537 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008538 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008539{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008540 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008541 char_u buf[MAXPATHL];
8542 char_u *p;
8543 int do_combine = FALSE;
8544 char_u *sps_copy;
8545#ifdef FEAT_EVAL
8546 static int expr_busy = FALSE;
8547#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008548 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008549
8550 /*
8551 * Set the info in "*su".
8552 */
8553 vim_memset(su, 0, sizeof(suginfo_T));
8554 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8555 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008556 if (*badptr == NUL)
8557 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008558 hash_init(&su->su_banned);
8559
8560 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008561 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008562 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008563 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008564
8565 if (su->su_badlen >= MAXWLEN)
8566 su->su_badlen = MAXWLEN - 1; /* just in case */
8567 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8568 (void)spell_casefold(su->su_badptr, su->su_badlen,
8569 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008570 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008571 su->su_badflags = badword_captype(su->su_badptr,
8572 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008573 if (need_cap)
8574 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008575
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008576 /* If the word is not capitalised and spell_check() doesn't consider the
8577 * word to be bad then it might need to be capitalised. Add a suggestion
8578 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008579 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008580 if (!SPELL_ISUPPER(c) && attr == 0)
8581 {
8582 make_case_word(su->su_badword, buf, WF_ONECAP);
8583 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8584 0, TRUE);
8585 }
8586
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008587 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008588 if (banbadword)
8589 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008590
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008591 /* Make a copy of 'spellsuggest', because the expression may change it. */
8592 sps_copy = vim_strsave(p_sps);
8593 if (sps_copy == NULL)
8594 return;
8595
8596 /* Loop over the items in 'spellsuggest'. */
8597 for (p = sps_copy; *p != NUL; )
8598 {
8599 copy_option_part(&p, buf, MAXPATHL, ",");
8600
8601 if (STRNCMP(buf, "expr:", 5) == 0)
8602 {
8603#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008604 /* Evaluate an expression. Skip this when called recursively,
8605 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008606 if (!expr_busy)
8607 {
8608 expr_busy = TRUE;
8609 spell_suggest_expr(su, buf + 5);
8610 expr_busy = FALSE;
8611 }
8612#endif
8613 }
8614 else if (STRNCMP(buf, "file:", 5) == 0)
8615 /* Use list of suggestions in a file. */
8616 spell_suggest_file(su, buf + 5);
8617 else
8618 {
8619 /* Use internal method. */
8620 spell_suggest_intern(su);
8621 if (sps_flags & SPS_DOUBLE)
8622 do_combine = TRUE;
8623 }
8624 }
8625
8626 vim_free(sps_copy);
8627
8628 if (do_combine)
8629 /* Combine the two list of suggestions. This must be done last,
8630 * because sorting changes the order again. */
8631 score_combine(su);
8632}
8633
8634#ifdef FEAT_EVAL
8635/*
8636 * Find suggestions by evaluating expression "expr".
8637 */
8638 static void
8639spell_suggest_expr(su, expr)
8640 suginfo_T *su;
8641 char_u *expr;
8642{
8643 list_T *list;
8644 listitem_T *li;
8645 int score;
8646 char_u *p;
8647
8648 /* The work is split up in a few parts to avoid having to export
8649 * suginfo_T.
8650 * First evaluate the expression and get the resulting list. */
8651 list = eval_spell_expr(su->su_badword, expr);
8652 if (list != NULL)
8653 {
8654 /* Loop over the items in the list. */
8655 for (li = list->lv_first; li != NULL; li = li->li_next)
8656 if (li->li_tv.v_type == VAR_LIST)
8657 {
8658 /* Get the word and the score from the items. */
8659 score = get_spellword(li->li_tv.vval.v_list, &p);
8660 if (score >= 0)
8661 add_suggestion(su, &su->su_ga, p,
8662 su->su_badlen, score, 0, TRUE);
8663 }
8664 list_unref(list);
8665 }
8666
8667 /* Sort the suggestions and truncate at "maxcount". */
8668 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8669}
8670#endif
8671
8672/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008673 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008674 */
8675 static void
8676spell_suggest_file(su, fname)
8677 suginfo_T *su;
8678 char_u *fname;
8679{
8680 FILE *fd;
8681 char_u line[MAXWLEN * 2];
8682 char_u *p;
8683 int len;
8684 char_u cword[MAXWLEN];
8685
8686 /* Open the file. */
8687 fd = mch_fopen((char *)fname, "r");
8688 if (fd == NULL)
8689 {
8690 EMSG2(_(e_notopen), fname);
8691 return;
8692 }
8693
8694 /* Read it line by line. */
8695 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8696 {
8697 line_breakcheck();
8698
8699 p = vim_strchr(line, '/');
8700 if (p == NULL)
8701 continue; /* No Tab found, just skip the line. */
8702 *p++ = NUL;
8703 if (STRICMP(su->su_badword, line) == 0)
8704 {
8705 /* Match! Isolate the good word, until CR or NL. */
8706 for (len = 0; p[len] >= ' '; ++len)
8707 ;
8708 p[len] = NUL;
8709
8710 /* If the suggestion doesn't have specific case duplicate the case
8711 * of the bad word. */
8712 if (captype(p, NULL) == 0)
8713 {
8714 make_case_word(p, cword, su->su_badflags);
8715 p = cword;
8716 }
8717
8718 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8719 SCORE_FILE, 0, TRUE);
8720 }
8721 }
8722
8723 fclose(fd);
8724
8725 /* Sort the suggestions and truncate at "maxcount". */
8726 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8727}
8728
8729/*
8730 * Find suggestions for the internal method indicated by "sps_flags".
8731 */
8732 static void
8733spell_suggest_intern(su)
8734 suginfo_T *su;
8735{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008736 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008737 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008738 *
8739 * Set a maximum score to limit the combination of operations that is
8740 * tried.
8741 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008742 suggest_try_special(su);
8743
8744 /*
8745 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8746 * from the .aff file and inserting a space (split the word).
8747 */
8748 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008749
8750 /* For the resulting top-scorers compute the sound-a-like score. */
8751 if (sps_flags & SPS_DOUBLE)
8752 score_comp_sal(su);
8753
8754 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008755 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008756 *
8757 * Only do this when we don't have a lot of suggestions yet, because it's
8758 * very slow and often doesn't find new suggestions.
8759 */
8760 if ((sps_flags & SPS_DOUBLE)
8761 || (!(sps_flags & SPS_FAST)
8762 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8763 {
8764 /* Allow a higher score now. */
8765 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008766 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008767 }
8768
8769 /* When CTRL-C was hit while searching do show the results. */
8770 ui_breakcheck();
8771 if (got_int)
8772 {
8773 (void)vgetc();
8774 got_int = FALSE;
8775 }
8776
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008777 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008778 {
8779 if (sps_flags & SPS_BEST)
8780 /* Adjust the word score for how it sounds like. */
8781 rescore_suggestions(su);
8782
8783 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008784 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008785 }
8786}
8787
8788/*
8789 * Free the info put in "*su" by spell_find_suggest().
8790 */
8791 static void
8792spell_find_cleanup(su)
8793 suginfo_T *su;
8794{
8795 int i;
8796
8797 /* Free the suggestions. */
8798 for (i = 0; i < su->su_ga.ga_len; ++i)
8799 vim_free(SUG(su->su_ga, i).st_word);
8800 ga_clear(&su->su_ga);
8801 for (i = 0; i < su->su_sga.ga_len; ++i)
8802 vim_free(SUG(su->su_sga, i).st_word);
8803 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008804
8805 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008806 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008807}
8808
8809/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008810 * Make a copy of "word", with the first letter upper or lower cased, to
8811 * "wcopy[MAXWLEN]". "word" must not be empty.
8812 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 */
8814 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008815onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008817 char_u *wcopy;
8818 int upper; /* TRUE: first letter made upper case */
8819{
8820 char_u *p;
8821 int c;
8822 int l;
8823
8824 p = word;
8825#ifdef FEAT_MBYTE
8826 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008827 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008828 else
8829#endif
8830 c = *p++;
8831 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008832 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008833 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008834 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835#ifdef FEAT_MBYTE
8836 if (has_mbyte)
8837 l = mb_char2bytes(c, wcopy);
8838 else
8839#endif
8840 {
8841 l = 1;
8842 wcopy[0] = c;
8843 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008844 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845}
8846
8847/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008848 * Make a copy of "word" with all the letters upper cased into
8849 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008850 */
8851 static void
8852allcap_copy(word, wcopy)
8853 char_u *word;
8854 char_u *wcopy;
8855{
8856 char_u *s;
8857 char_u *d;
8858 int c;
8859
8860 d = wcopy;
8861 for (s = word; *s != NUL; )
8862 {
8863#ifdef FEAT_MBYTE
8864 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008865 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008866 else
8867#endif
8868 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00008869
8870#ifdef FEAT_MBYTE
8871 /* We only change ß to SS when we are certain latin1 is used. It
8872 * would cause weird errors in other 8-bit encodings. */
8873 if (enc_latin1like && c == 0xdf)
8874 {
8875 c = 'S';
8876 if (d - wcopy >= MAXWLEN - 1)
8877 break;
8878 *d++ = c;
8879 }
8880 else
8881#endif
8882 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008883
8884#ifdef FEAT_MBYTE
8885 if (has_mbyte)
8886 {
8887 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8888 break;
8889 d += mb_char2bytes(c, d);
8890 }
8891 else
8892#endif
8893 {
8894 if (d - wcopy >= MAXWLEN - 1)
8895 break;
8896 *d++ = c;
8897 }
8898 }
8899 *d = NUL;
8900}
8901
8902/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008903 * Try finding suggestions by recognizing specific situations.
8904 */
8905 static void
8906suggest_try_special(su)
8907 suginfo_T *su;
8908{
8909 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008910 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008911 int c;
8912 char_u word[MAXWLEN];
8913
8914 /*
8915 * Recognize a word that is repeated: "the the".
8916 */
8917 p = skiptowhite(su->su_fbadword);
8918 len = p - su->su_fbadword;
8919 p = skipwhite(p);
8920 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
8921 {
8922 /* Include badflags: if the badword is onecap or allcap
8923 * use that for the goodword too: "The the" -> "The". */
8924 c = su->su_fbadword[len];
8925 su->su_fbadword[len] = NUL;
8926 make_case_word(su->su_fbadword, word, su->su_badflags);
8927 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008928 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008929 }
8930}
8931
8932/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00008934 *
8935 * This uses a state machine. At each node in the tree we try various
8936 * operations. When trying if an operation work "depth" is increased and the
8937 * stack[] is used to store info. This allows combinations, thus insert one
8938 * character, replace one and delete another. The number of changes is
8939 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00008940 *
8941 * After implementing this I noticed an article by Kemal Oflazer that
8942 * describes something similar: "Error-tolerant Finite State Recognition with
8943 * Applications to Morphological Analysis and Spelling Correction" (1996).
8944 * The implementation in the article is simplified and requires a stack of
8945 * unknown depth. The implementation here only needs a stack depth of the
8946 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947 */
8948 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008949suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950 suginfo_T *su;
8951{
8952 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
8953 char_u tword[MAXWLEN]; /* good word collected so far */
8954 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008955 char_u preword[MAXWLEN * 3]; /* word found with proper case;
8956 * concatanation of prefix compound
8957 * words and split word. NUL terminated
8958 * when going deeper but not when coming
8959 * back. */
8960 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008961 trystate_T *sp;
8962 int newscore;
8963 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008964 char_u *byts, *fbyts, *pbyts;
8965 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008967 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008968 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008969 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008971 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 int len;
8973 char_u *p;
8974 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00008975 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008976 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008977 slang_T *slang;
8978 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979
8980 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00008981 * to find matches (esp. REP items). Append some more text, changing
8982 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008984 n = STRLEN(fword);
8985 p = su->su_badptr + su->su_badlen;
8986 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987
8988 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8989 lp->lp_slang != NULL; ++lp)
8990 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008991 slang = lp->lp_slang;
8992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008993 /*
8994 * Go through the whole case-fold tree, try changes at each node.
8995 * "tword[]" contains the word collected from nodes in the tree.
8996 * "fword[]" the word we are trying to match with (initially the bad
8997 * word).
8998 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009000 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009001 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009002 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009003
Bram Moolenaarea424162005-06-16 21:51:00 +00009004 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009005 * When there are postponed prefixes we need to use these first. At
9006 * the end of the prefix we continue in the case-fold tree.
9007 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009008 fbyts = slang->sl_fbyts;
9009 fidxs = slang->sl_fidxs;
9010 pbyts = slang->sl_pbyts;
9011 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009012 if (pbyts != NULL)
9013 {
9014 byts = pbyts;
9015 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009016 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009017 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9018 }
9019 else
9020 {
9021 byts = fbyts;
9022 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009023 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009024 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009025 }
9026
9027 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009028 * Loop to find all suggestions. At each round we either:
9029 * - For the current state try one operation, advance "ts_curi",
9030 * increase "depth".
9031 * - When a state is done go to the next, set "ts_state".
9032 * - When all states are tried decrease "depth".
9033 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009034 while (depth >= 0 && !got_int)
9035 {
9036 sp = &stack[depth];
9037 switch (sp->ts_state)
9038 {
9039 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009040 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 /*
9042 * Start of node: Deal with NUL bytes, which means
9043 * tword[] may end here.
9044 */
9045 arridx = sp->ts_arridx; /* current node in the tree */
9046 len = byts[arridx]; /* bytes in this node */
9047 arridx += sp->ts_curi; /* index of current byte */
9048
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009049 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009050 {
9051 /* Skip over the NUL bytes, we use them later. */
9052 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9053 ;
9054 sp->ts_curi += n;
9055
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009056 /* Always past NUL bytes now. */
9057 n = (int)sp->ts_state;
9058 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009059 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009060
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009061 /* At end of a prefix or at start of prefixtree: check for
9062 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009063 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009064 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009065 /* Set su->su_badflags to the caps type at this
9066 * position. Use the caps type until here for the
9067 * prefix itself. */
9068#ifdef FEAT_MBYTE
9069 if (has_mbyte)
9070 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9071 else
9072#endif
9073 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009074 flags = badword_captype(su->su_badptr,
9075 su->su_badptr + n);
9076 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009077 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009078 ++depth;
9079 stack[depth] = stack[depth - 1];
9080 sp = &stack[depth];
9081 sp->ts_prefixdepth = depth - 1;
9082 byts = fbyts;
9083 idxs = fidxs;
9084 sp->ts_state = STATE_START;
9085 sp->ts_curi = 1; /* start just after length byte */
9086 sp->ts_arridx = 0;
9087
Bram Moolenaar53805d12005-08-01 07:08:33 +00009088 /* Move the prefix to preword[] with the right case
9089 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009090 tword[sp->ts_twordlen] = NUL;
9091 make_case_word(tword + sp->ts_splitoff,
9092 preword + sp->ts_prewordlen,
9093 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009094 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009095 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009096 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009097 break;
9098 }
9099
Bram Moolenaar0c405862005-06-22 22:26:26 +00009100 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009101 {
9102 /* Past bytes in node and/or past NUL bytes. */
9103 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009104 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 break;
9106 }
9107
9108 /*
9109 * End of word in tree.
9110 */
9111 ++sp->ts_curi; /* eat one NUL byte */
9112
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009113 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009114 fword_ends = (fword[sp->ts_fidx] == NUL
9115 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9116 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117
Bram Moolenaard12a1322005-08-21 22:08:24 +00009118 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9119 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009120 {
9121 /* There was a prefix before the word. Check that the
9122 * prefix can be used with this word. */
9123 /* Count the length of the NULs in the prefix. If there
9124 * are none this must be the first try without a prefix.
9125 */
9126 n = stack[sp->ts_prefixdepth].ts_arridx;
9127 len = pbyts[n++];
9128 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9129 ;
9130 if (c > 0)
9131 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009132 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009133 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009134 if (c == 0)
9135 break;
9136
9137 /* Use the WF_RARE flag for a rare prefix. */
9138 if (c & WF_RAREPFX)
9139 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009140
9141 /* Tricky: when checking for both prefix and
9142 * compounding we run into the prefix flag first.
9143 * Remember that it's OK, so that we accept the prefix
9144 * when arriving at a compound flag. */
9145 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009146 }
9147 }
9148
Bram Moolenaard12a1322005-08-21 22:08:24 +00009149 if (sp->ts_complen > sp->ts_compsplit)
9150 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009151 if (slang->sl_nobreak)
9152 {
9153 /* There was a word before this word. When there was
9154 * no change in this word (it was correct) add the
9155 * first word as a suggestion. If this word was
9156 * corrected too, we need to check if a correct word
9157 * follows. */
9158 if (sp->ts_fidx - sp->ts_splitfidx
9159 == sp->ts_twordlen - sp->ts_splitoff
9160 && STRNCMP(fword + sp->ts_splitfidx,
9161 tword + sp->ts_splitoff,
9162 sp->ts_fidx - sp->ts_splitfidx) == 0)
9163 {
9164 preword[sp->ts_prewordlen] = NUL;
9165 add_suggestion(su, &su->su_ga, preword,
9166 sp->ts_splitfidx - repextra,
9167 sp->ts_score, 0, FALSE);
9168 break;
9169 }
9170 }
9171 else
9172 {
9173 /* There was a compound word before this word. If
9174 * this word does not support compounding then give up
9175 * (splitting is tried for the word without compound
9176 * flag). */
9177 if (((unsigned)flags >> 24) == 0
9178 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009179 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009180 break;
9181 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9182 compflags[sp->ts_complen + 1] = NUL;
9183 vim_strncpy(preword + sp->ts_prewordlen,
9184 tword + sp->ts_splitoff,
9185 sp->ts_twordlen - sp->ts_splitoff);
9186 p = preword;
9187 while (*skiptowhite(p) != NUL)
9188 p = skipwhite(skiptowhite(p));
9189 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009190 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009191 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009192
Bram Moolenaar78622822005-08-23 21:00:13 +00009193 /* Get pointer to last char of previous word. */
9194 p = preword + sp->ts_prewordlen;
9195 mb_ptr_back(preword, p);
9196 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009197 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009198 else
9199 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201 /*
9202 * Form the word with proper case in preword.
9203 * If there is a word from a previous split, append.
9204 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009205 if (flags & WF_KEEPCAP)
9206 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009207 find_keepcap_word(slang, tword + sp->ts_splitoff,
9208 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009212 * use that for the goodword too. But if the badword is
9213 * allcap and it's only one char long use onecap. */
9214 c = su->su_badflags;
9215 if ((c & WF_ALLCAP)
9216#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009217 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009218#else
9219 && su->su_badlen == 1
9220#endif
9221 )
9222 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009223 c |= flags;
9224
9225 /* When appending a compound word after a word character
9226 * don't use Onecap. */
9227 if (p != NULL && spell_iswordp_nmw(p))
9228 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009229 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009230 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009232
9233 /* Don't use a banned word. It may appear again as a good
9234 * word, thus remember it. */
9235 if (flags & WF_BANNED)
9236 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009237 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009238 break;
9239 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009240 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009241 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 break;
9243
9244 newscore = 0;
9245 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009246 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009247 newscore += SCORE_REGION;
9248 if (flags & WF_RARE)
9249 newscore += SCORE_RARE;
9250
Bram Moolenaar0c405862005-06-22 22:26:26 +00009251 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009252 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 newscore += SCORE_ICASE;
9254
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009255 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009257 /* The badword also ends: add suggestions. Give a penalty
9258 * when changing non-word char to word char, e.g., "thes,"
9259 * -> "these". */
9260 p = fword + sp->ts_fidx;
9261#ifdef FEAT_MBYTE
9262 if (has_mbyte)
9263 mb_ptr_back(fword, p);
9264 else
9265#endif
9266 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009267 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009268 {
9269 p = preword + STRLEN(preword);
9270#ifdef FEAT_MBYTE
9271 if (has_mbyte)
9272 mb_ptr_back(preword, p);
9273 else
9274#endif
9275 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009276 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009277 newscore += SCORE_NONWORD;
9278 }
9279
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009280 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009281 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009282 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009284 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009285#ifdef FEAT_MBYTE
9286 /* Don't split halfway a character. */
9287 && (!has_mbyte || sp->ts_tcharlen == 0)
9288#endif
9289 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009290 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009291 int try_compound;
9292
9293 /* Get here in two situations:
9294 * 1. The word in the tree ends but the badword continues:
9295 * If the word allows compounding try that. Otherwise
9296 * try a split by inserting a space. For both check
9297 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009298 * For NOBREAK do like compounding to be able to check
9299 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009300 * 2. The badword does end, but it was due to a change
9301 * (e.g., a swap). No need to split, but do check that
9302 * the following word is valid.
9303 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009304 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009305 if (!fword_ends
Bram Moolenaar6de68532005-08-24 22:08:48 +00009306 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009307 && ((unsigned)flags >> 24) != 0
9308 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009309 >= slang->sl_compminlen
Bram Moolenaare52325c2005-08-22 22:54:29 +00009310 && (slang->sl_compsylmax < MAXWLEN
9311 || sp->ts_complen + 1 - sp->ts_compsplit
9312 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009313 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009314 ? slang->sl_compstartflags
9315 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009316 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009317 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009318 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009319 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9320 compflags[sp->ts_complen + 1] = NUL;
9321 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009322
Bram Moolenaar78622822005-08-23 21:00:13 +00009323 /* For NOBREAK we never try splitting, it won't make any
9324 * word valid. */
9325 if (slang->sl_nobreak)
9326 try_compound = TRUE;
9327
Bram Moolenaard12a1322005-08-21 22:08:24 +00009328 /* If we could add a compound word, and it's also possible
9329 * to split at this point, do the split first and set
9330 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009331 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009332 && try_compound
9333 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009334 {
9335 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009336 sp->ts_flags |= TSF_DIDSPLIT;
9337 --sp->ts_curi; /* do the same NUL again */
9338 compflags[sp->ts_complen] = NUL;
9339 }
9340 else
9341 sp->ts_flags &= ~TSF_DIDSPLIT;
9342
9343 if (!try_compound && !fword_ends)
9344 {
9345 /* If we're going to split need to check that the
9346 * words so far are valid for compounding. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00009347 p = preword;
9348 while (*skiptowhite(p) != NUL)
9349 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009350 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009351 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009352 compflags + sp->ts_compsplit))
9353 break;
9354 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009355 }
9356
9357 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 {
9359 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009360 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009361 sp->ts_state = STATE_SPLITUNDO;
9362
9363 ++depth;
9364 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009365
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009366 /* Append a space to preword when splitting. */
9367 if (!try_compound && !fword_ends)
9368 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009369 sp->ts_prewordlen = STRLEN(preword);
9370 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009371 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009372
9373 /* If the badword has a non-word character at this
9374 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009375 * non-word character with a space. Always skip a
9376 * character when the word ends. */
9377 if ((!try_compound
9378 && !spell_iswordp_nmw(fword + sp->ts_fidx))
9379 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009380 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009381 int l;
9382
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009383#ifdef FEAT_MBYTE
9384 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009385 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009386 else
9387#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009388 l = 1;
9389 if (fword_ends)
9390 {
9391 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009392 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009393 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009394 sp->ts_prewordlen += l;
9395 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009396 }
9397 else
9398 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9399 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009400 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009401
Bram Moolenaard12a1322005-08-21 22:08:24 +00009402 /* When compounding include compound flag in
9403 * compflags[] (already set above). When splitting we
9404 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009405 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009406 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009407 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009408 sp->ts_compsplit = sp->ts_complen;
9409 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009410
Bram Moolenaar53805d12005-08-01 07:08:33 +00009411 /* set su->su_badflags to the caps type at this
9412 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009413#ifdef FEAT_MBYTE
9414 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009415 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009416 else
9417#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009418 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009419 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009420 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009422 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009423 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009424
9425 /* If there are postponed prefixes, try these too. */
9426 if (pbyts != NULL)
9427 {
9428 byts = pbyts;
9429 idxs = pidxs;
9430 sp->ts_prefixdepth = PFD_PREFIXTREE;
9431 sp->ts_state = STATE_NOPREFIX;
9432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 }
9434 }
9435 break;
9436
9437 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009438 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009439 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440
9441 /* Continue looking for NUL bytes. */
9442 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009443
9444 /* In case we went into the prefix tree. */
9445 byts = fbyts;
9446 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009447 break;
9448
9449 case STATE_ENDNUL:
9450 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009451 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009452 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009453 {
9454 /* The badword ends, can't use the bytes in this node. */
9455 sp->ts_state = STATE_DEL;
9456 break;
9457 }
9458 sp->ts_state = STATE_PLAIN;
9459 /*FALLTHROUGH*/
9460
9461 case STATE_PLAIN:
9462 /*
9463 * Go over all possible bytes at this node, add each to
9464 * tword[] and use child node. "ts_curi" is the index.
9465 */
9466 arridx = sp->ts_arridx;
9467 if (sp->ts_curi > byts[arridx])
9468 {
9469 /* Done all bytes at this node, do next state. When still
9470 * at already changed bytes skip the other tricks. */
9471 if (sp->ts_fidx >= sp->ts_fidxtry)
9472 sp->ts_state = STATE_DEL;
9473 else
9474 sp->ts_state = STATE_FINAL;
9475 }
9476 else
9477 {
9478 arridx += sp->ts_curi++;
9479 c = byts[arridx];
9480
9481 /* Normal byte, go one level deeper. If it's not equal to
9482 * the byte in the bad word adjust the score. But don't
9483 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009484 if (c == fword[sp->ts_fidx]
9485#ifdef FEAT_MBYTE
9486 || (sp->ts_tcharlen > 0
9487 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009488#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009489 )
9490 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009491 else
9492 newscore = SCORE_SUBST;
9493 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9494 && try_deeper(su, stack, depth, newscore))
9495 {
9496 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009497 sp = &stack[depth];
9498 ++sp->ts_fidx;
9499 tword[sp->ts_twordlen++] = c;
9500 sp->ts_arridx = idxs[arridx];
9501#ifdef FEAT_MBYTE
9502 if (newscore == SCORE_SUBST)
9503 sp->ts_isdiff = DIFF_YES;
9504 if (has_mbyte)
9505 {
9506 /* Multi-byte characters are a bit complicated to
9507 * handle: They differ when any of the bytes
9508 * differ and then their length may also differ. */
9509 if (sp->ts_tcharlen == 0)
9510 {
9511 /* First byte. */
9512 sp->ts_tcharidx = 0;
9513 sp->ts_tcharlen = MB_BYTE2LEN(c);
9514 sp->ts_fcharstart = sp->ts_fidx - 1;
9515 sp->ts_isdiff = (newscore != 0)
9516 ? DIFF_YES : DIFF_NONE;
9517 }
9518 else if (sp->ts_isdiff == DIFF_INSERT)
9519 /* When inserting trail bytes don't advance in
9520 * the bad word. */
9521 --sp->ts_fidx;
9522 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9523 {
9524 /* Last byte of character. */
9525 if (sp->ts_isdiff == DIFF_YES)
9526 {
9527 /* Correct ts_fidx for the byte length of
9528 * the character (we didn't check that
9529 * before). */
9530 sp->ts_fidx = sp->ts_fcharstart
9531 + MB_BYTE2LEN(
9532 fword[sp->ts_fcharstart]);
9533
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009534 /* For changing a composing character
9535 * adjust the score from SCORE_SUBST to
9536 * SCORE_SUBCOMP. */
9537 if (enc_utf8
9538 && utf_iscomposing(
9539 mb_ptr2char(tword
9540 + sp->ts_twordlen
9541 - sp->ts_tcharlen))
9542 && utf_iscomposing(
9543 mb_ptr2char(fword
9544 + sp->ts_fcharstart)))
9545 sp->ts_score -=
9546 SCORE_SUBST - SCORE_SUBCOMP;
9547
Bram Moolenaarea424162005-06-16 21:51:00 +00009548 /* For a similar character adjust score
9549 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009550 else if (slang->sl_has_map
9551 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009552 mb_ptr2char(tword
9553 + sp->ts_twordlen
9554 - sp->ts_tcharlen),
9555 mb_ptr2char(fword
9556 + sp->ts_fcharstart)))
9557 sp->ts_score -=
9558 SCORE_SUBST - SCORE_SIMILAR;
9559 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009560 else if (sp->ts_isdiff == DIFF_INSERT
9561 && sp->ts_twordlen > sp->ts_tcharlen)
9562 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009563 p = tword + sp->ts_twordlen
9564 - sp->ts_tcharlen;
9565 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009566 if (enc_utf8 && utf_iscomposing(c))
9567 {
9568 /* Inserting a composing char doesn't
9569 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009570 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009571 - SCORE_INSCOMP;
9572 }
9573 else
9574 {
9575 /* If the previous character was the
9576 * same, thus doubling a character,
9577 * give a bonus to the score. */
9578 mb_ptr_back(tword, p);
9579 if (c == mb_ptr2char(p))
9580 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009581 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009582 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009583 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009584
9585 /* Starting a new char, reset the length. */
9586 sp->ts_tcharlen = 0;
9587 }
9588 }
9589 else
9590#endif
9591 {
9592 /* If we found a similar char adjust the score.
9593 * We do this after calling try_deeper() because
9594 * it's slow. */
9595 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009596 && slang->sl_has_map
9597 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009598 c, fword[sp->ts_fidx - 1]))
9599 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9600 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 }
9602 }
9603 break;
9604
9605 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009606#ifdef FEAT_MBYTE
9607 /* When past the first byte of a multi-byte char don't try
9608 * delete/insert/swap a character. */
9609 if (has_mbyte && sp->ts_tcharlen > 0)
9610 {
9611 sp->ts_state = STATE_FINAL;
9612 break;
9613 }
9614#endif
9615 /*
9616 * Try skipping one character in the bad word (delete it).
9617 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 sp->ts_state = STATE_INS;
9619 sp->ts_curi = 1;
9620 if (fword[sp->ts_fidx] != NUL
9621 && try_deeper(su, stack, depth, SCORE_DEL))
9622 {
9623 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009624
9625 /* Advance over the character in fword[]. Give a bonus to
9626 * the score if the same character is following "nn" ->
9627 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009628#ifdef FEAT_MBYTE
9629 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009630 {
9631 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009632 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009633 if (enc_utf8 && utf_iscomposing(c))
9634 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9635 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009636 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9637 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009638 else
9639#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009640 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009641 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009642 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9643 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 break;
9646 }
9647 /*FALLTHROUGH*/
9648
9649 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009650 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 * node. */
9652 n = sp->ts_arridx;
9653 if (sp->ts_curi > byts[n])
9654 {
9655 /* Done all bytes at this node, do next state. */
9656 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009657 }
9658 else
9659 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009660 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009661 n += sp->ts_curi++;
9662 c = byts[n];
9663 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9664 {
9665 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009666 sp = &stack[depth];
9667 tword[sp->ts_twordlen++] = c;
9668 sp->ts_arridx = idxs[n];
9669#ifdef FEAT_MBYTE
9670 if (has_mbyte)
9671 {
9672 fl = MB_BYTE2LEN(c);
9673 if (fl > 1)
9674 {
9675 /* There are following bytes for the same
9676 * character. We must find all bytes before
9677 * trying delete/insert/swap/etc. */
9678 sp->ts_tcharlen = fl;
9679 sp->ts_tcharidx = 1;
9680 sp->ts_isdiff = DIFF_INSERT;
9681 }
9682 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009683 else
9684 fl = 1;
9685 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009686#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009687 {
9688 /* If the previous character was the same, thus
9689 * doubling a character, give a bonus to the
9690 * score. */
9691 if (sp->ts_twordlen >= 2
9692 && tword[sp->ts_twordlen - 2] == c)
9693 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 }
9696 }
9697 break;
9698
9699 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009700 /*
9701 * Swap two bytes in the bad word: "12" -> "21".
9702 * We change "fword" here, it's changed back afterwards.
9703 */
9704 p = fword + sp->ts_fidx;
9705 c = *p;
9706 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009707 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009708 /* End of word, can't swap or replace. */
9709 sp->ts_state = STATE_FINAL;
9710 break;
9711 }
9712#ifdef FEAT_MBYTE
9713 if (has_mbyte)
9714 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009715 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009716 c = mb_ptr2char(p);
9717 c2 = mb_ptr2char(p + n);
9718 }
9719 else
9720#endif
9721 c2 = p[1];
9722 if (c == c2)
9723 {
9724 /* Characters are identical, swap won't do anything. */
9725 sp->ts_state = STATE_SWAP3;
9726 break;
9727 }
9728 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9729 {
9730 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009732#ifdef FEAT_MBYTE
9733 if (has_mbyte)
9734 {
9735 fl = mb_char2len(c2);
9736 mch_memmove(p, p + n, fl);
9737 mb_char2bytes(c, p + fl);
9738 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9739 }
9740 else
9741#endif
9742 {
9743 p[0] = c2;
9744 p[1] = c;
9745 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 }
9748 else
9749 /* If this swap doesn't work then SWAP3 won't either. */
9750 sp->ts_state = STATE_REP_INI;
9751 break;
9752
Bram Moolenaarea424162005-06-16 21:51:00 +00009753 case STATE_UNSWAP:
9754 /* Undo the STATE_SWAP swap: "21" -> "12". */
9755 p = fword + sp->ts_fidx;
9756#ifdef FEAT_MBYTE
9757 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009759 n = MB_BYTE2LEN(*p);
9760 c = mb_ptr2char(p + n);
9761 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9762 mb_char2bytes(c, p);
9763 }
9764 else
9765#endif
9766 {
9767 c = *p;
9768 *p = p[1];
9769 p[1] = c;
9770 }
9771 /*FALLTHROUGH*/
9772
9773 case STATE_SWAP3:
9774 /* Swap two bytes, skipping one: "123" -> "321". We change
9775 * "fword" here, it's changed back afterwards. */
9776 p = fword + sp->ts_fidx;
9777#ifdef FEAT_MBYTE
9778 if (has_mbyte)
9779 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009780 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009781 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009782 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009783 c2 = mb_ptr2char(p + n);
9784 c3 = mb_ptr2char(p + n + fl);
9785 }
9786 else
9787#endif
9788 {
9789 c = *p;
9790 c2 = p[1];
9791 c3 = p[2];
9792 }
9793
9794 /* When characters are identical: "121" then SWAP3 result is
9795 * identical, ROT3L result is same as SWAP: "211", ROT3L
9796 * result is same as SWAP on next char: "112". Thus skip all
9797 * swapping. Also skip when c3 is NUL. */
9798 if (c == c3 || c3 == NUL)
9799 {
9800 sp->ts_state = STATE_REP_INI;
9801 break;
9802 }
9803 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9804 {
9805 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009806 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009807#ifdef FEAT_MBYTE
9808 if (has_mbyte)
9809 {
9810 tl = mb_char2len(c3);
9811 mch_memmove(p, p + n + fl, tl);
9812 mb_char2bytes(c2, p + tl);
9813 mb_char2bytes(c, p + fl + tl);
9814 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9815 }
9816 else
9817#endif
9818 {
9819 p[0] = p[2];
9820 p[2] = c;
9821 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9822 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 }
9824 else
9825 sp->ts_state = STATE_REP_INI;
9826 break;
9827
Bram Moolenaarea424162005-06-16 21:51:00 +00009828 case STATE_UNSWAP3:
9829 /* Undo STATE_SWAP3: "321" -> "123" */
9830 p = fword + sp->ts_fidx;
9831#ifdef FEAT_MBYTE
9832 if (has_mbyte)
9833 {
9834 n = MB_BYTE2LEN(*p);
9835 c2 = mb_ptr2char(p + n);
9836 fl = MB_BYTE2LEN(p[n]);
9837 c = mb_ptr2char(p + n + fl);
9838 tl = MB_BYTE2LEN(p[n + fl]);
9839 mch_memmove(p + fl + tl, p, n);
9840 mb_char2bytes(c, p);
9841 mb_char2bytes(c2, p + tl);
9842 }
9843 else
9844#endif
9845 {
9846 c = *p;
9847 *p = p[2];
9848 p[2] = c;
9849 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009850
Bram Moolenaarea424162005-06-16 21:51:00 +00009851 /* Rotate three characters left: "123" -> "231". We change
9852 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9854 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009855 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009857 p = fword + sp->ts_fidx;
9858#ifdef FEAT_MBYTE
9859 if (has_mbyte)
9860 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009861 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009862 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009863 fl = mb_cptr2len(p + n);
9864 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009865 mch_memmove(p, p + n, fl);
9866 mb_char2bytes(c, p + fl);
9867 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9868 }
9869 else
9870#endif
9871 {
9872 c = *p;
9873 *p = p[1];
9874 p[1] = p[2];
9875 p[2] = c;
9876 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9877 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 }
9879 else
9880 sp->ts_state = STATE_REP_INI;
9881 break;
9882
Bram Moolenaarea424162005-06-16 21:51:00 +00009883 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009884 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009885 p = fword + sp->ts_fidx;
9886#ifdef FEAT_MBYTE
9887 if (has_mbyte)
9888 {
9889 n = MB_BYTE2LEN(*p);
9890 n += MB_BYTE2LEN(p[n]);
9891 c = mb_ptr2char(p + n);
9892 tl = MB_BYTE2LEN(p[n]);
9893 mch_memmove(p + tl, p, n);
9894 mb_char2bytes(c, p);
9895 }
9896 else
9897#endif
9898 {
9899 c = p[2];
9900 p[2] = p[1];
9901 p[1] = *p;
9902 *p = c;
9903 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009905 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00009906 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009907 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9908 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009909 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009910 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009911 p = fword + sp->ts_fidx;
9912#ifdef FEAT_MBYTE
9913 if (has_mbyte)
9914 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009915 n = mb_cptr2len(p);
9916 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009917 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009918 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009919 mch_memmove(p + tl, p, n);
9920 mb_char2bytes(c, p);
9921 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
9922 }
9923 else
9924#endif
9925 {
9926 c = p[2];
9927 p[2] = p[1];
9928 p[1] = *p;
9929 *p = c;
9930 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009932 }
9933 else
9934 sp->ts_state = STATE_REP_INI;
9935 break;
9936
Bram Moolenaarea424162005-06-16 21:51:00 +00009937 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009938 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009939 p = fword + sp->ts_fidx;
9940#ifdef FEAT_MBYTE
9941 if (has_mbyte)
9942 {
9943 c = mb_ptr2char(p);
9944 tl = MB_BYTE2LEN(*p);
9945 n = MB_BYTE2LEN(p[tl]);
9946 n += MB_BYTE2LEN(p[tl + n]);
9947 mch_memmove(p, p + tl, n);
9948 mb_char2bytes(c, p + n);
9949 }
9950 else
9951#endif
9952 {
9953 c = *p;
9954 *p = p[1];
9955 p[1] = p[2];
9956 p[2] = c;
9957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 /*FALLTHROUGH*/
9959
9960 case STATE_REP_INI:
9961 /* Check if matching with REP items from the .aff file would
9962 * work. Quickly skip if there are no REP items or the score
9963 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009964 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 if (gap->ga_len == 0
9966 || sp->ts_score + SCORE_REP >= su->su_maxscore)
9967 {
9968 sp->ts_state = STATE_FINAL;
9969 break;
9970 }
9971
9972 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00009973 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009974 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 if (sp->ts_curi < 0)
9976 {
9977 sp->ts_state = STATE_FINAL;
9978 break;
9979 }
9980
9981 sp->ts_state = STATE_REP;
9982 /*FALLTHROUGH*/
9983
9984 case STATE_REP:
9985 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00009986 * match replace the characters and check if the resulting
9987 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009988 p = fword + sp->ts_fidx;
9989
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009990 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 while (sp->ts_curi < gap->ga_len)
9992 {
9993 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
9994 if (*ftp->ft_from != *p)
9995 {
9996 /* past possible matching entries */
9997 sp->ts_curi = gap->ga_len;
9998 break;
9999 }
10000 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10001 && try_deeper(su, stack, depth, SCORE_REP))
10002 {
10003 /* Need to undo this afterwards. */
10004 sp->ts_state = STATE_REP_UNDO;
10005
10006 /* Change the "from" to the "to" string. */
10007 ++depth;
10008 fl = STRLEN(ftp->ft_from);
10009 tl = STRLEN(ftp->ft_to);
10010 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010013 repextra += tl - fl;
10014 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 mch_memmove(p, ftp->ft_to, tl);
10016 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010017#ifdef FEAT_MBYTE
10018 stack[depth].ts_tcharlen = 0;
10019#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 break;
10021 }
10022 }
10023
10024 if (sp->ts_curi >= gap->ga_len)
10025 /* No (more) matches. */
10026 sp->ts_state = STATE_FINAL;
10027
10028 break;
10029
10030 case STATE_REP_UNDO:
10031 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010032 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 fl = STRLEN(ftp->ft_from);
10034 tl = STRLEN(ftp->ft_to);
10035 p = fword + sp->ts_fidx;
10036 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010039 repextra -= tl - fl;
10040 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 mch_memmove(p, ftp->ft_from, fl);
10042 sp->ts_state = STATE_REP;
10043 break;
10044
10045 default:
10046 /* Did all possible states at this level, go up one level. */
10047 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010048
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010049 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010050 {
10051 /* Continue in or go back to the prefix tree. */
10052 byts = pbyts;
10053 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010054 }
10055
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010056 /* Don't check for CTRL-C too often, it takes time. */
10057 line_breakcheck();
10058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 }
10060 }
10061}
10062
10063/*
10064 * Try going one level deeper in the tree.
10065 */
10066 static int
10067try_deeper(su, stack, depth, score_add)
10068 suginfo_T *su;
10069 trystate_T *stack;
10070 int depth;
10071 int score_add;
10072{
10073 int newscore;
10074
10075 /* Refuse to go deeper if the scrore is getting too big. */
10076 newscore = stack[depth].ts_score + score_add;
10077 if (newscore >= su->su_maxscore)
10078 return FALSE;
10079
Bram Moolenaarea424162005-06-16 21:51:00 +000010080 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 stack[depth + 1].ts_state = STATE_START;
10082 stack[depth + 1].ts_score = newscore;
10083 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010084 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 return TRUE;
10086}
10087
Bram Moolenaar53805d12005-08-01 07:08:33 +000010088#ifdef FEAT_MBYTE
10089/*
10090 * Case-folding may change the number of bytes: Count nr of chars in
10091 * fword[flen] and return the byte length of that many chars in "word".
10092 */
10093 static int
10094nofold_len(fword, flen, word)
10095 char_u *fword;
10096 int flen;
10097 char_u *word;
10098{
10099 char_u *p;
10100 int i = 0;
10101
10102 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10103 ++i;
10104 for (p = word; i > 0; mb_ptr_adv(p))
10105 --i;
10106 return (int)(p - word);
10107}
10108#endif
10109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110/*
10111 * "fword" is a good word with case folded. Find the matching keep-case
10112 * words and put it in "kword".
10113 * Theoretically there could be several keep-case words that result in the
10114 * same case-folded word, but we only find one...
10115 */
10116 static void
10117find_keepcap_word(slang, fword, kword)
10118 slang_T *slang;
10119 char_u *fword;
10120 char_u *kword;
10121{
10122 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10123 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010124 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125
10126 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010127 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 int round[MAXWLEN];
10129 int fwordidx[MAXWLEN];
10130 int uwordidx[MAXWLEN];
10131 int kwordlen[MAXWLEN];
10132
10133 int flen, ulen;
10134 int l;
10135 int len;
10136 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010137 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 char_u *p;
10139 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010140 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141
10142 if (byts == NULL)
10143 {
10144 /* array is empty: "cannot happen" */
10145 *kword = NUL;
10146 return;
10147 }
10148
10149 /* Make an all-cap version of "fword". */
10150 allcap_copy(fword, uword);
10151
10152 /*
10153 * Each character needs to be tried both case-folded and upper-case.
10154 * All this gets very complicated if we keep in mind that changing case
10155 * may change the byte length of a multi-byte character...
10156 */
10157 depth = 0;
10158 arridx[0] = 0;
10159 round[0] = 0;
10160 fwordidx[0] = 0;
10161 uwordidx[0] = 0;
10162 kwordlen[0] = 0;
10163 while (depth >= 0)
10164 {
10165 if (fword[fwordidx[depth]] == NUL)
10166 {
10167 /* We are at the end of "fword". If the tree allows a word to end
10168 * here we have found a match. */
10169 if (byts[arridx[depth] + 1] == 0)
10170 {
10171 kword[kwordlen[depth]] = NUL;
10172 return;
10173 }
10174
10175 /* kword is getting too long, continue one level up */
10176 --depth;
10177 }
10178 else if (++round[depth] > 2)
10179 {
10180 /* tried both fold-case and upper-case character, continue one
10181 * level up */
10182 --depth;
10183 }
10184 else
10185 {
10186 /*
10187 * round[depth] == 1: Try using the folded-case character.
10188 * round[depth] == 2: Try using the upper-case character.
10189 */
10190#ifdef FEAT_MBYTE
10191 if (has_mbyte)
10192 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010193 flen = mb_cptr2len(fword + fwordidx[depth]);
10194 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 }
10196 else
10197#endif
10198 ulen = flen = 1;
10199 if (round[depth] == 1)
10200 {
10201 p = fword + fwordidx[depth];
10202 l = flen;
10203 }
10204 else
10205 {
10206 p = uword + uwordidx[depth];
10207 l = ulen;
10208 }
10209
10210 for (tryidx = arridx[depth]; l > 0; --l)
10211 {
10212 /* Perform a binary search in the list of accepted bytes. */
10213 len = byts[tryidx++];
10214 c = *p++;
10215 lo = tryidx;
10216 hi = tryidx + len - 1;
10217 while (lo < hi)
10218 {
10219 m = (lo + hi) / 2;
10220 if (byts[m] > c)
10221 hi = m - 1;
10222 else if (byts[m] < c)
10223 lo = m + 1;
10224 else
10225 {
10226 lo = hi = m;
10227 break;
10228 }
10229 }
10230
10231 /* Stop if there is no matching byte. */
10232 if (hi < lo || byts[lo] != c)
10233 break;
10234
10235 /* Continue at the child (if there is one). */
10236 tryidx = idxs[lo];
10237 }
10238
10239 if (l == 0)
10240 {
10241 /*
10242 * Found the matching char. Copy it to "kword" and go a
10243 * level deeper.
10244 */
10245 if (round[depth] == 1)
10246 {
10247 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10248 flen);
10249 kwordlen[depth + 1] = kwordlen[depth] + flen;
10250 }
10251 else
10252 {
10253 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10254 ulen);
10255 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10256 }
10257 fwordidx[depth + 1] = fwordidx[depth] + flen;
10258 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10259
10260 ++depth;
10261 arridx[depth] = tryidx;
10262 round[depth] = 0;
10263 }
10264 }
10265 }
10266
10267 /* Didn't find it: "cannot happen". */
10268 *kword = NUL;
10269}
10270
10271/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010272 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10273 * su->su_sga.
10274 */
10275 static void
10276score_comp_sal(su)
10277 suginfo_T *su;
10278{
10279 langp_T *lp;
10280 char_u badsound[MAXWLEN];
10281 int i;
10282 suggest_T *stp;
10283 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010284 int score;
10285
10286 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10287 return;
10288
10289 /* Use the sound-folding of the first language that supports it. */
10290 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10291 lp->lp_slang != NULL; ++lp)
10292 if (lp->lp_slang->sl_sal.ga_len > 0)
10293 {
10294 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010295 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010296
10297 for (i = 0; i < su->su_ga.ga_len; ++i)
10298 {
10299 stp = &SUG(su->su_ga, i);
10300
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010301 /* Case-fold the suggested word, sound-fold it and compute the
10302 * sound-a-like score. */
10303 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010304 if (score < SCORE_MAXMAX)
10305 {
10306 /* Add the suggestion. */
10307 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10308 sstp->st_word = vim_strsave(stp->st_word);
10309 if (sstp->st_word != NULL)
10310 {
10311 sstp->st_score = score;
10312 sstp->st_altscore = 0;
10313 sstp->st_orglen = stp->st_orglen;
10314 ++su->su_sga.ga_len;
10315 }
10316 }
10317 }
10318 break;
10319 }
10320}
10321
10322/*
10323 * Combine the list of suggestions in su->su_ga and su->su_sga.
10324 * They are intwined.
10325 */
10326 static void
10327score_combine(su)
10328 suginfo_T *su;
10329{
10330 int i;
10331 int j;
10332 garray_T ga;
10333 garray_T *gap;
10334 langp_T *lp;
10335 suggest_T *stp;
10336 char_u *p;
10337 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010338 int round;
10339
10340 /* Add the alternate score to su_ga. */
10341 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10342 lp->lp_slang != NULL; ++lp)
10343 {
10344 if (lp->lp_slang->sl_sal.ga_len > 0)
10345 {
10346 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010347 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010348
10349 for (i = 0; i < su->su_ga.ga_len; ++i)
10350 {
10351 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010352 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10353 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010354 if (stp->st_altscore == SCORE_MAXMAX)
10355 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10356 else
10357 stp->st_score = (stp->st_score * 3
10358 + stp->st_altscore) / 4;
10359 stp->st_salscore = FALSE;
10360 }
10361 break;
10362 }
10363 }
10364
10365 /* Add the alternate score to su_sga. */
10366 for (i = 0; i < su->su_sga.ga_len; ++i)
10367 {
10368 stp = &SUG(su->su_sga, i);
10369 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10370 if (stp->st_score == SCORE_MAXMAX)
10371 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10372 else
10373 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10374 stp->st_salscore = TRUE;
10375 }
10376
10377 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10378 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10379 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10380
10381 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10382 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10383 return;
10384
10385 stp = &SUG(ga, 0);
10386 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10387 {
10388 /* round 1: get a suggestion from su_ga
10389 * round 2: get a suggestion from su_sga */
10390 for (round = 1; round <= 2; ++round)
10391 {
10392 gap = round == 1 ? &su->su_ga : &su->su_sga;
10393 if (i < gap->ga_len)
10394 {
10395 /* Don't add a word if it's already there. */
10396 p = SUG(*gap, i).st_word;
10397 for (j = 0; j < ga.ga_len; ++j)
10398 if (STRCMP(stp[j].st_word, p) == 0)
10399 break;
10400 if (j == ga.ga_len)
10401 stp[ga.ga_len++] = SUG(*gap, i);
10402 else
10403 vim_free(p);
10404 }
10405 }
10406 }
10407
10408 ga_clear(&su->su_ga);
10409 ga_clear(&su->su_sga);
10410
10411 /* Truncate the list to the number of suggestions that will be displayed. */
10412 if (ga.ga_len > su->su_maxcount)
10413 {
10414 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10415 vim_free(stp[i].st_word);
10416 ga.ga_len = su->su_maxcount;
10417 }
10418
10419 su->su_ga = ga;
10420}
10421
10422/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010423 * For the goodword in "stp" compute the soundalike score compared to the
10424 * badword.
10425 */
10426 static int
10427stp_sal_score(stp, su, slang, badsound)
10428 suggest_T *stp;
10429 suginfo_T *su;
10430 slang_T *slang;
10431 char_u *badsound; /* sound-folded badword */
10432{
10433 char_u *p;
10434 char_u badsound2[MAXWLEN];
10435 char_u fword[MAXWLEN];
10436 char_u goodsound[MAXWLEN];
10437
10438 if (stp->st_orglen <= su->su_badlen)
10439 p = badsound;
10440 else
10441 {
10442 /* soundfold the bad word with more characters following */
10443 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10444
10445 /* When joining two words the sound often changes a lot. E.g., "t he"
10446 * sounds like "t h" while "the" sounds like "@". Avoid that by
10447 * removing the space. Don't do it when the good word also contains a
10448 * space. */
10449 if (vim_iswhite(su->su_badptr[su->su_badlen])
10450 && *skiptowhite(stp->st_word) == NUL)
10451 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10452 mch_memmove(p, p + 1, STRLEN(p));
10453
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010454 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010455 p = badsound2;
10456 }
10457
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010458 /* Sound-fold the word and compute the score for the difference. */
10459 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010460
10461 return soundalike_score(goodsound, p);
10462}
10463
10464/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 * Find suggestions by comparing the word in a sound-a-like form.
10466 */
10467 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010468suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 suginfo_T *su;
10470{
10471 char_u salword[MAXWLEN];
10472 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010474 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 int curi[MAXWLEN];
10476 langp_T *lp;
10477 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010478 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 int depth;
10480 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010481 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 int round;
10483 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010484 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010485 int local_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010487 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10489 lp->lp_slang != NULL; ++lp)
10490 {
10491 if (lp->lp_slang->sl_sal.ga_len > 0)
10492 {
10493 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010494 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495
10496 /*
10497 * Go through the whole tree, soundfold each word and compare.
10498 * round 1: use the case-folded tree.
10499 * round 2: use the keep-case tree.
10500 */
10501 for (round = 1; round <= 2; ++round)
10502 {
10503 if (round == 1)
10504 {
10505 byts = lp->lp_slang->sl_fbyts;
10506 idxs = lp->lp_slang->sl_fidxs;
10507 }
10508 else
10509 {
10510 byts = lp->lp_slang->sl_kbyts;
10511 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010512 if (byts == NULL) /* no keep-case words */
10513 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 }
10515
10516 depth = 0;
10517 arridx[0] = 0;
10518 curi[0] = 1;
10519 while (depth >= 0 && !got_int)
10520 {
10521 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 /* Done all bytes at this node, go up one level. */
10524 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010525 line_breakcheck();
10526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 else
10528 {
10529 /* Do one more byte at this node. */
10530 n = arridx[depth] + curi[depth];
10531 ++curi[depth];
10532 c = byts[n];
10533 if (c == 0)
10534 {
10535 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010536 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10538 {
10539 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010540 /* Sound-fold. Only in keep-case tree need to
10541 * case-fold the word. */
10542 spell_soundfold(lp->lp_slang, tword,
10543 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010545 /* Compute the edit distance between the
10546 * sound-a-like words. */
10547 sound_score = soundalike_score(salword,
10548 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010549
10550 /* Add a penalty for words in another region. */
10551 if ((flags & WF_REGION) && (((unsigned)flags
10552 >> 16) & lp->lp_region) == 0)
10553 local_score = SCORE_REGION;
10554 else
10555 local_score = 0;
10556 sound_score += local_score;
10557
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010558 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010560 char_u cword[MAXWLEN];
10561 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010562 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010563
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010564 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010565 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010567 /* Need to fix case according to
10568 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010569 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010570 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 }
10572 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010573 p = tword;
10574
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010575 if (sps_flags & SPS_DOUBLE)
10576 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010577 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010578 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010579 else
10580 {
10581 /* Compute the score. */
10582 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010583 su->su_badword, p)
10584 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010585 if (sps_flags & SPS_BEST)
10586 /* give a bonus for the good word
10587 * sounding the same as the bad
10588 * word */
10589 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010590 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010591 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010592 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010593 else
10594 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010595 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010596 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010597 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 }
10599 }
10600
10601 /* Skip over other NUL bytes. */
10602 while (byts[n + 1] == 0)
10603 {
10604 ++n;
10605 ++curi[depth];
10606 }
10607 }
10608 else
10609 {
10610 /* Normal char, go one level deeper. */
10611 tword[depth++] = c;
10612 arridx[depth] = idxs[n];
10613 curi[depth] = 1;
10614 }
10615 }
10616 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 }
10618 }
10619 }
10620}
10621
10622/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010623 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 */
10625 static void
10626make_case_word(fword, cword, flags)
10627 char_u *fword;
10628 char_u *cword;
10629 int flags;
10630{
10631 if (flags & WF_ALLCAP)
10632 /* Make it all upper-case */
10633 allcap_copy(fword, cword);
10634 else if (flags & WF_ONECAP)
10635 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010636 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010637 else
10638 /* Use goodword as-is. */
10639 STRCPY(cword, fword);
10640}
10641
Bram Moolenaarea424162005-06-16 21:51:00 +000010642/*
10643 * Use map string "map" for languages "lp".
10644 */
10645 static void
10646set_map_str(lp, map)
10647 slang_T *lp;
10648 char_u *map;
10649{
10650 char_u *p;
10651 int headc = 0;
10652 int c;
10653 int i;
10654
10655 if (*map == NUL)
10656 {
10657 lp->sl_has_map = FALSE;
10658 return;
10659 }
10660 lp->sl_has_map = TRUE;
10661
10662 /* Init the array and hash table empty. */
10663 for (i = 0; i < 256; ++i)
10664 lp->sl_map_array[i] = 0;
10665#ifdef FEAT_MBYTE
10666 hash_init(&lp->sl_map_hash);
10667#endif
10668
10669 /*
10670 * The similar characters are stored separated with slashes:
10671 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10672 * before the same slash. For characters above 255 sl_map_hash is used.
10673 */
10674 for (p = map; *p != NUL; )
10675 {
10676#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010677 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010678#else
10679 c = *p++;
10680#endif
10681 if (c == '/')
10682 headc = 0;
10683 else
10684 {
10685 if (headc == 0)
10686 headc = c;
10687
10688#ifdef FEAT_MBYTE
10689 /* Characters above 255 don't fit in sl_map_array[], put them in
10690 * the hash table. Each entry is the char, a NUL the headchar and
10691 * a NUL. */
10692 if (c >= 256)
10693 {
10694 int cl = mb_char2len(c);
10695 int headcl = mb_char2len(headc);
10696 char_u *b;
10697 hash_T hash;
10698 hashitem_T *hi;
10699
10700 b = alloc((unsigned)(cl + headcl + 2));
10701 if (b == NULL)
10702 return;
10703 mb_char2bytes(c, b);
10704 b[cl] = NUL;
10705 mb_char2bytes(headc, b + cl + 1);
10706 b[cl + 1 + headcl] = NUL;
10707 hash = hash_hash(b);
10708 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10709 if (HASHITEM_EMPTY(hi))
10710 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10711 else
10712 {
10713 /* This should have been checked when generating the .spl
10714 * file. */
10715 EMSG(_("E999: duplicate char in MAP entry"));
10716 vim_free(b);
10717 }
10718 }
10719 else
10720#endif
10721 lp->sl_map_array[c] = headc;
10722 }
10723 }
10724}
10725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010726/*
10727 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10728 * lines in the .aff file.
10729 */
10730 static int
10731similar_chars(slang, c1, c2)
10732 slang_T *slang;
10733 int c1;
10734 int c2;
10735{
Bram Moolenaarea424162005-06-16 21:51:00 +000010736 int m1, m2;
10737#ifdef FEAT_MBYTE
10738 char_u buf[MB_MAXBYTES];
10739 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740
Bram Moolenaarea424162005-06-16 21:51:00 +000010741 if (c1 >= 256)
10742 {
10743 buf[mb_char2bytes(c1, buf)] = 0;
10744 hi = hash_find(&slang->sl_map_hash, buf);
10745 if (HASHITEM_EMPTY(hi))
10746 m1 = 0;
10747 else
10748 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10749 }
10750 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010751#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010752 m1 = slang->sl_map_array[c1];
10753 if (m1 == 0)
10754 return FALSE;
10755
10756
10757#ifdef FEAT_MBYTE
10758 if (c2 >= 256)
10759 {
10760 buf[mb_char2bytes(c2, buf)] = 0;
10761 hi = hash_find(&slang->sl_map_hash, buf);
10762 if (HASHITEM_EMPTY(hi))
10763 m2 = 0;
10764 else
10765 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10766 }
10767 else
10768#endif
10769 m2 = slang->sl_map_array[c2];
10770
10771 return m1 == m2;
10772}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773
10774/*
10775 * Add a suggestion to the list of suggestions.
10776 * Do not add a duplicate suggestion or suggestions with a bad score.
10777 * When "use_score" is not zero it's used, otherwise the score is computed
10778 * with spell_edit_score().
10779 */
10780 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010781add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010782 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010783 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010785 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010786 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010787 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010788 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010789{
10790 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010792 char_u *p = NULL;
10793 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794
10795 /* Check that the word wasn't banned. */
10796 if (was_banned(su, goodword))
10797 return;
10798
Bram Moolenaar0c405862005-06-22 22:26:26 +000010799 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10800 * Remove the common part from "goodword". */
10801 i = badlen - su->su_badlen;
10802 if (i > 0)
10803 {
10804 /* This assumes there was no case folding or it didn't change the
10805 * length... */
10806 p = goodword + STRLEN(goodword) - i;
10807 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10808 {
10809 badlen = su->su_badlen;
10810 c = *p;
10811 *p = NUL;
10812 }
10813 else
10814 p = NULL;
10815 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010816 else if (i < 0)
10817 {
10818 /* When replacing part of the word check that we actually change
10819 * something. For "the the" a suggestion can be replacing the first
10820 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010821 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010822 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10823 return;
10824 }
10825
Bram Moolenaar0c405862005-06-22 22:26:26 +000010826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 if (score <= su->su_maxscore)
10828 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010829 /* Check if the word is already there. Also check the length that is
10830 * being replaced "thes," -> "these" is a different suggestion from
10831 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010832 stp = &SUG(*gap, 0);
10833 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010834 if (STRCMP(stp[i].st_word, goodword) == 0
10835 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 {
10837 /* Found it. Remember the lowest score. */
10838 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010841 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010842 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010843 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 break;
10845 }
10846
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010847 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010848 {
10849 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010850 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 stp->st_word = vim_strsave(goodword);
10852 if (stp->st_word != NULL)
10853 {
10854 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010855 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010856 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010857 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010858 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010859
10860 /* If we have too many suggestions now, sort the list and keep
10861 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010862 if (gap->ga_len > SUG_MAX_COUNT(su))
10863 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10864 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 }
10866 }
10867 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000010868
10869 if (p != NULL)
10870 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871}
10872
10873/*
10874 * Add a word to be banned.
10875 */
10876 static void
10877add_banned(su, word)
10878 suginfo_T *su;
10879 char_u *word;
10880{
10881 char_u *s = vim_strsave(word);
10882 hash_T hash;
10883 hashitem_T *hi;
10884
10885 if (s != NULL)
10886 {
10887 hash = hash_hash(s);
10888 hi = hash_lookup(&su->su_banned, s, hash);
10889 if (HASHITEM_EMPTY(hi))
10890 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010891 else
10892 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 }
10894}
10895
10896/*
10897 * Return TRUE if a word appears in the list of banned words.
10898 */
10899 static int
10900was_banned(su, word)
10901 suginfo_T *su;
10902 char_u *word;
10903{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010904 hashitem_T *hi = hash_find(&su->su_banned, word);
10905
10906 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907}
10908
10909/*
10910 * Free the banned words in "su".
10911 */
10912 static void
10913free_banned(su)
10914 suginfo_T *su;
10915{
10916 int todo;
10917 hashitem_T *hi;
10918
10919 todo = su->su_banned.ht_used;
10920 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
10921 {
10922 if (!HASHITEM_EMPTY(hi))
10923 {
10924 vim_free(hi->hi_key);
10925 --todo;
10926 }
10927 }
10928 hash_clear(&su->su_banned);
10929}
10930
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010931/*
10932 * Recompute the score if sound-folding is possible. This is slow,
10933 * thus only done for the final results.
10934 */
10935 static void
10936rescore_suggestions(su)
10937 suginfo_T *su;
10938{
10939 langp_T *lp;
10940 suggest_T *stp;
10941 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010942 int i;
10943
10944 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10945 lp->lp_slang != NULL; ++lp)
10946 {
10947 if (lp->lp_slang->sl_sal.ga_len > 0)
10948 {
10949 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010950 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010951
10952 for (i = 0; i < su->su_ga.ga_len; ++i)
10953 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010954 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010955 if (!stp->st_had_bonus)
10956 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010957 stp->st_altscore = stp_sal_score(stp, su,
10958 lp->lp_slang, sal_badword);
10959 if (stp->st_altscore == SCORE_MAXMAX)
10960 stp->st_altscore = SCORE_BIG;
10961 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010962 }
10963 }
10964 break;
10965 }
10966 }
10967}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969static int
10970#ifdef __BORLANDC__
10971_RTLENTRYF
10972#endif
10973sug_compare __ARGS((const void *s1, const void *s2));
10974
10975/*
10976 * Function given to qsort() to sort the suggestions on st_score.
10977 */
10978 static int
10979#ifdef __BORLANDC__
10980_RTLENTRYF
10981#endif
10982sug_compare(s1, s2)
10983 const void *s1;
10984 const void *s2;
10985{
10986 suggest_T *p1 = (suggest_T *)s1;
10987 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010988 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010990 if (n == 0)
10991 return p1->st_altscore - p2->st_altscore;
10992 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010993}
10994
10995/*
10996 * Cleanup the suggestions:
10997 * - Sort on score.
10998 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010999 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011000 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011001 static int
11002cleanup_suggestions(gap, maxscore, keep)
11003 garray_T *gap;
11004 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011005 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011007 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 int i;
11009
11010 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011011 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011012
11013 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011014 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011016 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011018 gap->ga_len = keep;
11019 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011021 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011022}
11023
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011024#if defined(FEAT_EVAL) || defined(PROTO)
11025/*
11026 * Soundfold a string, for soundfold().
11027 * Result is in allocated memory, NULL for an error.
11028 */
11029 char_u *
11030eval_soundfold(word)
11031 char_u *word;
11032{
11033 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011034 char_u sound[MAXWLEN];
11035
11036 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11037 /* Use the sound-folding of the first language that supports it. */
11038 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
11039 lp->lp_slang != NULL; ++lp)
11040 if (lp->lp_slang->sl_sal.ga_len > 0)
11041 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011042 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011043 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011044 return vim_strsave(sound);
11045 }
11046
11047 /* No language with sound folding, return word as-is. */
11048 return vim_strsave(word);
11049}
11050#endif
11051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052/*
11053 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011054 *
11055 * There are many ways to turn a word into a sound-a-like representation. The
11056 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11057 * swedish name matching - survey and test of different algorithms" by Klas
11058 * Erikson.
11059 *
11060 * We support two methods:
11061 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11062 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 */
11064 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011065spell_soundfold(slang, inword, folded, res)
11066 slang_T *slang;
11067 char_u *inword;
11068 int folded; /* "inword" is already case-folded */
11069 char_u *res;
11070{
11071 char_u fword[MAXWLEN];
11072 char_u *word;
11073
11074 if (slang->sl_sofo)
11075 /* SOFOFROM and SOFOTO used */
11076 spell_soundfold_sofo(slang, inword, res);
11077 else
11078 {
11079 /* SAL items used. Requires the word to be case-folded. */
11080 if (folded)
11081 word = inword;
11082 else
11083 {
11084 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11085 word = fword;
11086 }
11087
11088#ifdef FEAT_MBYTE
11089 if (has_mbyte)
11090 spell_soundfold_wsal(slang, word, res);
11091 else
11092#endif
11093 spell_soundfold_sal(slang, word, res);
11094 }
11095}
11096
11097/*
11098 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11099 * SOFOTO lines.
11100 */
11101 static void
11102spell_soundfold_sofo(slang, inword, res)
11103 slang_T *slang;
11104 char_u *inword;
11105 char_u *res;
11106{
11107 char_u *s;
11108 int ri = 0;
11109 int c;
11110
11111#ifdef FEAT_MBYTE
11112 if (has_mbyte)
11113 {
11114 int prevc = 0;
11115 int *ip;
11116
11117 /* The sl_sal_first[] table contains the translation for chars up to
11118 * 255, sl_sal the rest. */
11119 for (s = inword; *s != NUL; )
11120 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011121 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011122 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11123 c = ' ';
11124 else if (c < 256)
11125 c = slang->sl_sal_first[c];
11126 else
11127 {
11128 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11129 if (ip == NULL) /* empty list, can't match */
11130 c = NUL;
11131 else
11132 for (;;) /* find "c" in the list */
11133 {
11134 if (*ip == 0) /* not found */
11135 {
11136 c = NUL;
11137 break;
11138 }
11139 if (*ip == c) /* match! */
11140 {
11141 c = ip[1];
11142 break;
11143 }
11144 ip += 2;
11145 }
11146 }
11147
11148 if (c != NUL && c != prevc)
11149 {
11150 ri += mb_char2bytes(c, res + ri);
11151 if (ri + MB_MAXBYTES > MAXWLEN)
11152 break;
11153 prevc = c;
11154 }
11155 }
11156 }
11157 else
11158#endif
11159 {
11160 /* The sl_sal_first[] table contains the translation. */
11161 for (s = inword; (c = *s) != NUL; ++s)
11162 {
11163 if (vim_iswhite(c))
11164 c = ' ';
11165 else
11166 c = slang->sl_sal_first[c];
11167 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11168 res[ri++] = c;
11169 }
11170 }
11171
11172 res[ri] = NUL;
11173}
11174
11175 static void
11176spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 slang_T *slang;
11178 char_u *inword;
11179 char_u *res;
11180{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011181 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011183 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011185 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011187 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 int n, k = 0;
11189 int z0;
11190 int k0;
11191 int n0;
11192 int c;
11193 int pri;
11194 int p0 = -333;
11195 int c0;
11196
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011197 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011198 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011199 if (slang->sl_rem_accents)
11200 {
11201 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011202 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011203 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011204 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011205 {
11206 *t++ = ' ';
11207 s = skipwhite(s);
11208 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011209 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011211 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 *t++ = *s;
11213 ++s;
11214 }
11215 }
11216 *t = NUL;
11217 }
11218 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011219 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011221 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222
11223 /*
11224 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011225 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011227 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 while ((c = word[i]) != NUL)
11229 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011230 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231 n = slang->sl_sal_first[c];
11232 z0 = 0;
11233
11234 if (n >= 0)
11235 {
11236 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011237 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011239 /* Quickly skip entries that don't match the word. Most
11240 * entries are less then three chars, optimize for that. */
11241 k = smp[n].sm_leadlen;
11242 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011243 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011244 if (word[i + 1] != s[1])
11245 continue;
11246 if (k > 2)
11247 {
11248 for (j = 2; j < k; ++j)
11249 if (word[i + j] != s[j])
11250 break;
11251 if (j < k)
11252 continue;
11253 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011254 }
11255
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011256 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011258 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011259 while (*pf != NUL && *pf != word[i + k])
11260 ++pf;
11261 if (*pf == NUL)
11262 continue;
11263 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011265 s = smp[n].sm_rules;
11266 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267
11268 p0 = *s;
11269 k0 = k;
11270 while (*s == '-' && k > 1)
11271 {
11272 k--;
11273 s++;
11274 }
11275 if (*s == '<')
11276 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011277 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 {
11279 /* determine priority */
11280 pri = *s - '0';
11281 s++;
11282 }
11283 if (*s == '^' && *(s + 1) == '^')
11284 s++;
11285
11286 if (*s == NUL
11287 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011288 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011289 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011290 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011291 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011293 && spell_iswordp(word + i - 1, curbuf)
11294 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011295 {
11296 /* search for followup rules, if: */
11297 /* followup and k > 1 and NO '-' in searchstring */
11298 c0 = word[i + k - 1];
11299 n0 = slang->sl_sal_first[c0];
11300
11301 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011302 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011303 {
11304 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011305 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011307 /* Quickly skip entries that don't match the word.
11308 * */
11309 k0 = smp[n0].sm_leadlen;
11310 if (k0 > 1)
11311 {
11312 if (word[i + k] != s[1])
11313 continue;
11314 if (k0 > 2)
11315 {
11316 pf = word + i + k + 1;
11317 for (j = 2; j < k0; ++j)
11318 if (*pf++ != s[j])
11319 break;
11320 if (j < k0)
11321 continue;
11322 }
11323 }
11324 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011326 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011327 {
11328 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011329 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011330 while (*pf != NUL && *pf != word[i + k0])
11331 ++pf;
11332 if (*pf == NUL)
11333 continue;
11334 ++k0;
11335 }
11336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011338 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 while (*s == '-')
11340 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011341 /* "k0" gets NOT reduced because
11342 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 s++;
11344 }
11345 if (*s == '<')
11346 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011347 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011348 {
11349 p0 = *s - '0';
11350 s++;
11351 }
11352
11353 if (*s == NUL
11354 /* *s == '^' cuts */
11355 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011356 && !spell_iswordp(word + i + k0,
11357 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358 {
11359 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011360 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011361 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362
11363 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 /* rule fits; stop search */
11367 break;
11368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 }
11370
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011371 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 }
11374
11375 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011376 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011377 if (s == NULL)
11378 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011379 pf = smp[n].sm_rules;
11380 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 if (p0 == 1 && z == 0)
11382 {
11383 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011384 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11385 || res[reslen - 1] == *s))
11386 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011387 z0 = 1;
11388 z = 1;
11389 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011390 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 {
11392 word[i + k0] = *s;
11393 k0++;
11394 s++;
11395 }
11396 if (k > k0)
11397 mch_memmove(word + i + k0, word + i + k,
11398 STRLEN(word + i + k) + 1);
11399
11400 /* new "actual letter" */
11401 c = word[i];
11402 }
11403 else
11404 {
11405 /* no '<' rule used */
11406 i += k - 1;
11407 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011408 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011409 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011410 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011411 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 s++;
11413 }
11414 /* new "actual letter" */
11415 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011416 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 {
11418 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011419 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 mch_memmove(word, word + i + 1,
11421 STRLEN(word + i + 1) + 1);
11422 i = 0;
11423 z0 = 1;
11424 }
11425 }
11426 break;
11427 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 }
11429 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011430 else if (vim_iswhite(c))
11431 {
11432 c = ' ';
11433 k = 1;
11434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435
11436 if (z0 == 0)
11437 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011438 if (k && !p0 && reslen < MAXWLEN && c != NUL
11439 && (!slang->sl_collapse || reslen == 0
11440 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011442 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443
11444 i++;
11445 z = 0;
11446 k = 0;
11447 }
11448 }
11449
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011450 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011451}
11452
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011453#ifdef FEAT_MBYTE
11454/*
11455 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11456 * Multi-byte version of spell_soundfold().
11457 */
11458 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011459spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011460 slang_T *slang;
11461 char_u *inword;
11462 char_u *res;
11463{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011464 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011465 int word[MAXWLEN];
11466 int wres[MAXWLEN];
11467 int l;
11468 char_u *s;
11469 int *ws;
11470 char_u *t;
11471 int *pf;
11472 int i, j, z;
11473 int reslen;
11474 int n, k = 0;
11475 int z0;
11476 int k0;
11477 int n0;
11478 int c;
11479 int pri;
11480 int p0 = -333;
11481 int c0;
11482 int did_white = FALSE;
11483
11484 /*
11485 * Convert the multi-byte string to a wide-character string.
11486 * Remove accents, if wanted. We actually remove all non-word characters.
11487 * But keep white space.
11488 */
11489 n = 0;
11490 for (s = inword; *s != NUL; )
11491 {
11492 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011493 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011494 if (slang->sl_rem_accents)
11495 {
11496 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11497 {
11498 if (did_white)
11499 continue;
11500 c = ' ';
11501 did_white = TRUE;
11502 }
11503 else
11504 {
11505 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011506 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011507 continue;
11508 }
11509 }
11510 word[n++] = c;
11511 }
11512 word[n] = NUL;
11513
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011514 /*
11515 * This comes from Aspell phonet.cpp.
11516 * Converted from C++ to C. Added support for multi-byte chars.
11517 * Changed to keep spaces.
11518 */
11519 i = reslen = z = 0;
11520 while ((c = word[i]) != NUL)
11521 {
11522 /* Start with the first rule that has the character in the word. */
11523 n = slang->sl_sal_first[c & 0xff];
11524 z0 = 0;
11525
11526 if (n >= 0)
11527 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011528 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011529 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
11530 {
11531 /* Quickly skip entries that don't match the word. Most
11532 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011533 if (c != ws[0])
11534 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011535 k = smp[n].sm_leadlen;
11536 if (k > 1)
11537 {
11538 if (word[i + 1] != ws[1])
11539 continue;
11540 if (k > 2)
11541 {
11542 for (j = 2; j < k; ++j)
11543 if (word[i + j] != ws[j])
11544 break;
11545 if (j < k)
11546 continue;
11547 }
11548 }
11549
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011550 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011551 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011552 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011553 while (*pf != NUL && *pf != word[i + k])
11554 ++pf;
11555 if (*pf == NUL)
11556 continue;
11557 ++k;
11558 }
11559 s = smp[n].sm_rules;
11560 pri = 5; /* default priority */
11561
11562 p0 = *s;
11563 k0 = k;
11564 while (*s == '-' && k > 1)
11565 {
11566 k--;
11567 s++;
11568 }
11569 if (*s == '<')
11570 s++;
11571 if (VIM_ISDIGIT(*s))
11572 {
11573 /* determine priority */
11574 pri = *s - '0';
11575 s++;
11576 }
11577 if (*s == '^' && *(s + 1) == '^')
11578 s++;
11579
11580 if (*s == NUL
11581 || (*s == '^'
11582 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011583 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011584 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011585 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011586 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011587 && spell_iswordp_w(word + i - 1, curbuf)
11588 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011589 {
11590 /* search for followup rules, if: */
11591 /* followup and k > 1 and NO '-' in searchstring */
11592 c0 = word[i + k - 1];
11593 n0 = slang->sl_sal_first[c0 & 0xff];
11594
11595 if (slang->sl_followup && k > 1 && n0 >= 0
11596 && p0 != '-' && word[i + k] != NUL)
11597 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011598 /* Test follow-up rule for "word[i + k]"; loop over
11599 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011600 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11601 == (c0 & 0xff); ++n0)
11602 {
11603 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011604 */
11605 if (c0 != ws[0])
11606 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011607 k0 = smp[n0].sm_leadlen;
11608 if (k0 > 1)
11609 {
11610 if (word[i + k] != ws[1])
11611 continue;
11612 if (k0 > 2)
11613 {
11614 pf = word + i + k + 1;
11615 for (j = 2; j < k0; ++j)
11616 if (*pf++ != ws[j])
11617 break;
11618 if (j < k0)
11619 continue;
11620 }
11621 }
11622 k0 += k - 1;
11623
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011624 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011625 {
11626 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011627 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011628 while (*pf != NUL && *pf != word[i + k0])
11629 ++pf;
11630 if (*pf == NUL)
11631 continue;
11632 ++k0;
11633 }
11634
11635 p0 = 5;
11636 s = smp[n0].sm_rules;
11637 while (*s == '-')
11638 {
11639 /* "k0" gets NOT reduced because
11640 * "if (k0 == k)" */
11641 s++;
11642 }
11643 if (*s == '<')
11644 s++;
11645 if (VIM_ISDIGIT(*s))
11646 {
11647 p0 = *s - '0';
11648 s++;
11649 }
11650
11651 if (*s == NUL
11652 /* *s == '^' cuts */
11653 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011654 && !spell_iswordp_w(word + i + k0,
11655 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011656 {
11657 if (k0 == k)
11658 /* this is just a piece of the string */
11659 continue;
11660
11661 if (p0 < pri)
11662 /* priority too low */
11663 continue;
11664 /* rule fits; stop search */
11665 break;
11666 }
11667 }
11668
11669 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11670 == (c0 & 0xff))
11671 continue;
11672 }
11673
11674 /* replace string */
11675 ws = smp[n].sm_to_w;
11676 s = smp[n].sm_rules;
11677 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11678 if (p0 == 1 && z == 0)
11679 {
11680 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011681 if (reslen > 0 && ws != NULL && *ws != NUL
11682 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011683 || wres[reslen - 1] == *ws))
11684 reslen--;
11685 z0 = 1;
11686 z = 1;
11687 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011688 if (ws != NULL)
11689 while (*ws != NUL && word[i + k0] != NUL)
11690 {
11691 word[i + k0] = *ws;
11692 k0++;
11693 ws++;
11694 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011695 if (k > k0)
11696 mch_memmove(word + i + k0, word + i + k,
11697 sizeof(int) * (STRLEN(word + i + k) + 1));
11698
11699 /* new "actual letter" */
11700 c = word[i];
11701 }
11702 else
11703 {
11704 /* no '<' rule used */
11705 i += k - 1;
11706 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011707 if (ws != NULL)
11708 while (*ws != NUL && ws[1] != NUL
11709 && reslen < MAXWLEN)
11710 {
11711 if (reslen == 0 || wres[reslen - 1] != *ws)
11712 wres[reslen++] = *ws;
11713 ws++;
11714 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011715 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011716 if (ws == NULL)
11717 c = NUL;
11718 else
11719 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011720 if (strstr((char *)s, "^^") != NULL)
11721 {
11722 if (c != NUL)
11723 wres[reslen++] = c;
11724 mch_memmove(word, word + i + 1,
11725 sizeof(int) * (STRLEN(word + i + 1) + 1));
11726 i = 0;
11727 z0 = 1;
11728 }
11729 }
11730 break;
11731 }
11732 }
11733 }
11734 else if (vim_iswhite(c))
11735 {
11736 c = ' ';
11737 k = 1;
11738 }
11739
11740 if (z0 == 0)
11741 {
11742 if (k && !p0 && reslen < MAXWLEN && c != NUL
11743 && (!slang->sl_collapse || reslen == 0
11744 || wres[reslen - 1] != c))
11745 /* condense only double letters */
11746 wres[reslen++] = c;
11747
11748 i++;
11749 z = 0;
11750 k = 0;
11751 }
11752 }
11753
11754 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11755 l = 0;
11756 for (n = 0; n < reslen; ++n)
11757 {
11758 l += mb_char2bytes(wres[n], res + l);
11759 if (l + MB_MAXBYTES > MAXWLEN)
11760 break;
11761 }
11762 res[l] = NUL;
11763}
11764#endif
11765
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011766/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011767 * Compute a score for two sound-a-like words.
11768 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11769 * Instead of a generic loop we write out the code. That keeps it fast by
11770 * avoiding checks that will not be possible.
11771 */
11772 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011773soundalike_score(goodstart, badstart)
11774 char_u *goodstart; /* sound-folded good word */
11775 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011776{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011777 char_u *goodsound = goodstart;
11778 char_u *badsound = badstart;
11779 int goodlen;
11780 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011781 int n;
11782 char_u *pl, *ps;
11783 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011784 int score = 0;
11785
11786 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11787 * counted so much, vowels halfway the word aren't counted at all. */
11788 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11789 {
11790 score = SCORE_DEL / 2;
11791 if (*badsound == '*')
11792 ++badsound;
11793 else
11794 ++goodsound;
11795 }
11796
11797 goodlen = STRLEN(goodsound);
11798 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011799
11800 /* Return quickly if the lenghts are too different to be fixed by two
11801 * changes. */
11802 n = goodlen - badlen;
11803 if (n < -2 || n > 2)
11804 return SCORE_MAXMAX;
11805
11806 if (n > 0)
11807 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011808 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011809 ps = badsound;
11810 }
11811 else
11812 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011813 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011814 ps = goodsound;
11815 }
11816
11817 /* Skip over the identical part. */
11818 while (*pl == *ps && *pl != NUL)
11819 {
11820 ++pl;
11821 ++ps;
11822 }
11823
11824 switch (n)
11825 {
11826 case -2:
11827 case 2:
11828 /*
11829 * Must delete two characters from "pl".
11830 */
11831 ++pl; /* first delete */
11832 while (*pl == *ps)
11833 {
11834 ++pl;
11835 ++ps;
11836 }
11837 /* strings must be equal after second delete */
11838 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011839 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011840
11841 /* Failed to compare. */
11842 break;
11843
11844 case -1:
11845 case 1:
11846 /*
11847 * Minimal one delete from "pl" required.
11848 */
11849
11850 /* 1: delete */
11851 pl2 = pl + 1;
11852 ps2 = ps;
11853 while (*pl2 == *ps2)
11854 {
11855 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011856 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011857 ++pl2;
11858 ++ps2;
11859 }
11860
11861 /* 2: delete then swap, then rest must be equal */
11862 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11863 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011864 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011865
11866 /* 3: delete then substitute, then the rest must be equal */
11867 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011868 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011869
11870 /* 4: first swap then delete */
11871 if (pl[0] == ps[1] && pl[1] == ps[0])
11872 {
11873 pl2 = pl + 2; /* swap, skip two chars */
11874 ps2 = ps + 2;
11875 while (*pl2 == *ps2)
11876 {
11877 ++pl2;
11878 ++ps2;
11879 }
11880 /* delete a char and then strings must be equal */
11881 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011882 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011883 }
11884
11885 /* 5: first substitute then delete */
11886 pl2 = pl + 1; /* substitute, skip one char */
11887 ps2 = ps + 1;
11888 while (*pl2 == *ps2)
11889 {
11890 ++pl2;
11891 ++ps2;
11892 }
11893 /* delete a char and then strings must be equal */
11894 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011895 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011896
11897 /* Failed to compare. */
11898 break;
11899
11900 case 0:
11901 /*
11902 * Lenghts are equal, thus changes must result in same length: An
11903 * insert is only possible in combination with a delete.
11904 * 1: check if for identical strings
11905 */
11906 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011907 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011908
11909 /* 2: swap */
11910 if (pl[0] == ps[1] && pl[1] == ps[0])
11911 {
11912 pl2 = pl + 2; /* swap, skip two chars */
11913 ps2 = ps + 2;
11914 while (*pl2 == *ps2)
11915 {
11916 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011917 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011918 ++pl2;
11919 ++ps2;
11920 }
11921 /* 3: swap and swap again */
11922 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11923 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011924 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011925
11926 /* 4: swap and substitute */
11927 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011928 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011929 }
11930
11931 /* 5: substitute */
11932 pl2 = pl + 1;
11933 ps2 = ps + 1;
11934 while (*pl2 == *ps2)
11935 {
11936 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011937 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011938 ++pl2;
11939 ++ps2;
11940 }
11941
11942 /* 6: substitute and swap */
11943 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11944 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011945 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011946
11947 /* 7: substitute and substitute */
11948 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011949 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011950
11951 /* 8: insert then delete */
11952 pl2 = pl;
11953 ps2 = ps + 1;
11954 while (*pl2 == *ps2)
11955 {
11956 ++pl2;
11957 ++ps2;
11958 }
11959 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011960 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011961
11962 /* 9: delete then insert */
11963 pl2 = pl + 1;
11964 ps2 = ps;
11965 while (*pl2 == *ps2)
11966 {
11967 ++pl2;
11968 ++ps2;
11969 }
11970 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011971 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011972
11973 /* Failed to compare. */
11974 break;
11975 }
11976
11977 return SCORE_MAXMAX;
11978}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980/*
11981 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011982 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011983 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000011984 * The algorithm is described by Du and Chang, 1992.
11985 * The implementation of the algorithm comes from Aspell editdist.cpp,
11986 * edit_distance(). It has been converted from C++ to C and modified to
11987 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 */
11989 static int
11990spell_edit_score(badword, goodword)
11991 char_u *badword;
11992 char_u *goodword;
11993{
11994 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011995 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011996 int j, i;
11997 int t;
11998 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011999 int pbc, pgc;
12000#ifdef FEAT_MBYTE
12001 char_u *p;
12002 int wbadword[MAXWLEN];
12003 int wgoodword[MAXWLEN];
12004
12005 if (has_mbyte)
12006 {
12007 /* Get the characters from the multi-byte strings and put them in an
12008 * int array for easy access. */
12009 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012010 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012011 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012012 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012013 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012014 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012015 }
12016 else
12017#endif
12018 {
12019 badlen = STRLEN(badword) + 1;
12020 goodlen = STRLEN(goodword) + 1;
12021 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012022
12023 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12024#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12026 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012027 if (cnt == NULL)
12028 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029
12030 CNT(0, 0) = 0;
12031 for (j = 1; j <= goodlen; ++j)
12032 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12033
12034 for (i = 1; i <= badlen; ++i)
12035 {
12036 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12037 for (j = 1; j <= goodlen; ++j)
12038 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012039#ifdef FEAT_MBYTE
12040 if (has_mbyte)
12041 {
12042 bc = wbadword[i - 1];
12043 gc = wgoodword[j - 1];
12044 }
12045 else
12046#endif
12047 {
12048 bc = badword[i - 1];
12049 gc = goodword[j - 1];
12050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012051 if (bc == gc)
12052 CNT(i, j) = CNT(i - 1, j - 1);
12053 else
12054 {
12055 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012056 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012057 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12058 else
12059 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12060
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012061 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012062 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012063#ifdef FEAT_MBYTE
12064 if (has_mbyte)
12065 {
12066 pbc = wbadword[i - 2];
12067 pgc = wgoodword[j - 2];
12068 }
12069 else
12070#endif
12071 {
12072 pbc = badword[i - 2];
12073 pgc = goodword[j - 2];
12074 }
12075 if (bc == pgc && pbc == gc)
12076 {
12077 t = SCORE_SWAP + CNT(i - 2, j - 2);
12078 if (t < CNT(i, j))
12079 CNT(i, j) = t;
12080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 }
12082 t = SCORE_DEL + CNT(i - 1, j);
12083 if (t < CNT(i, j))
12084 CNT(i, j) = t;
12085 t = SCORE_INS + CNT(i, j - 1);
12086 if (t < CNT(i, j))
12087 CNT(i, j) = t;
12088 }
12089 }
12090 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012091
12092 i = CNT(badlen - 1, goodlen - 1);
12093 vim_free(cnt);
12094 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012096
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012097/*
12098 * ":spelldump"
12099 */
12100/*ARGSUSED*/
12101 void
12102ex_spelldump(eap)
12103 exarg_T *eap;
12104{
12105 buf_T *buf = curbuf;
12106 langp_T *lp;
12107 slang_T *slang;
12108 idx_T arridx[MAXWLEN];
12109 int curi[MAXWLEN];
12110 char_u word[MAXWLEN];
12111 int c;
12112 char_u *byts;
12113 idx_T *idxs;
12114 linenr_T lnum = 0;
12115 int round;
12116 int depth;
12117 int n;
12118 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012119 char_u *region_names = NULL; /* region names being used */
12120 int do_region = TRUE; /* dump region names and numbers */
12121 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012122
Bram Moolenaar95529562005-08-25 21:21:38 +000012123 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012124 return;
12125
12126 /* Create a new empty buffer by splitting the window. */
12127 do_cmdline_cmd((char_u *)"new");
12128 if (!bufempty() || !buf_valid(buf))
12129 return;
12130
Bram Moolenaar7887d882005-07-01 22:33:52 +000012131 /* Find out if we can support regions: All languages must support the same
12132 * regions or none at all. */
12133 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
12134 {
12135 p = lp->lp_slang->sl_regions;
12136 if (p[0] != 0)
12137 {
12138 if (region_names == NULL) /* first language with regions */
12139 region_names = p;
12140 else if (STRCMP(region_names, p) != 0)
12141 {
12142 do_region = FALSE; /* region names are different */
12143 break;
12144 }
12145 }
12146 }
12147
12148 if (do_region && region_names != NULL)
12149 {
12150 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12151 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12152 }
12153 else
12154 do_region = FALSE;
12155
12156 /*
12157 * Loop over all files loaded for the entries in 'spelllang'.
12158 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012159 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
12160 {
12161 slang = lp->lp_slang;
12162
12163 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12164 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12165
12166 /* round 1: case-folded tree
12167 * round 2: keep-case tree */
12168 for (round = 1; round <= 2; ++round)
12169 {
12170 if (round == 1)
12171 {
12172 byts = slang->sl_fbyts;
12173 idxs = slang->sl_fidxs;
12174 }
12175 else
12176 {
12177 byts = slang->sl_kbyts;
12178 idxs = slang->sl_kidxs;
12179 }
12180 if (byts == NULL)
12181 continue; /* array is empty */
12182
12183 depth = 0;
12184 arridx[0] = 0;
12185 curi[0] = 1;
12186 while (depth >= 0 && !got_int)
12187 {
12188 if (curi[depth] > byts[arridx[depth]])
12189 {
12190 /* Done all bytes at this node, go up one level. */
12191 --depth;
12192 line_breakcheck();
12193 }
12194 else
12195 {
12196 /* Do one more byte at this node. */
12197 n = arridx[depth] + curi[depth];
12198 ++curi[depth];
12199 c = byts[n];
12200 if (c == 0)
12201 {
12202 /* End of word, deal with the word.
12203 * Don't use keep-case words in the fold-case tree,
12204 * they will appear in the keep-case tree.
12205 * Only use the word when the region matches. */
12206 flags = (int)idxs[n];
12207 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012208 && (do_region
12209 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012210 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012211 & lp->lp_region) != 0))
12212 {
12213 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012214 if (!do_region)
12215 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012216
12217 /* Dump the basic word if there is no prefix or
12218 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012219 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012220 if (c == 0 || curi[depth] == 2)
12221 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012222
12223 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012224 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012225 lnum = apply_prefixes(slang, word, round,
12226 flags, lnum);
12227 }
12228 }
12229 else
12230 {
12231 /* Normal char, go one level deeper. */
12232 word[depth++] = c;
12233 arridx[depth] = idxs[n];
12234 curi[depth] = 1;
12235 }
12236 }
12237 }
12238 }
12239 }
12240
12241 /* Delete the empty line that we started with. */
12242 if (curbuf->b_ml.ml_line_count > 1)
12243 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12244
12245 redraw_later(NOT_VALID);
12246}
12247
12248/*
12249 * Dump one word: apply case modifications and append a line to the buffer.
12250 */
12251 static void
12252dump_word(word, round, flags, lnum)
12253 char_u *word;
12254 int round;
12255 int flags;
12256 linenr_T lnum;
12257{
12258 int keepcap = FALSE;
12259 char_u *p;
12260 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012261 char_u badword[MAXWLEN + 10];
12262 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012263
12264 if (round == 1 && (flags & WF_CAPMASK) != 0)
12265 {
12266 /* Need to fix case according to "flags". */
12267 make_case_word(word, cword, flags);
12268 p = cword;
12269 }
12270 else
12271 {
12272 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012273 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12274 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012275 keepcap = TRUE;
12276 }
12277
Bram Moolenaar7887d882005-07-01 22:33:52 +000012278 /* Add flags and regions after a slash. */
12279 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012280 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012281 STRCPY(badword, p);
12282 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012283 if (keepcap)
12284 STRCAT(badword, "=");
12285 if (flags & WF_BANNED)
12286 STRCAT(badword, "!");
12287 else if (flags & WF_RARE)
12288 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012289 if (flags & WF_REGION)
12290 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012291 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012292 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012293 p = badword;
12294 }
12295
12296 ml_append(lnum, p, (colnr_T)0, FALSE);
12297}
12298
12299/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012300 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12301 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012302 * Return the updated line number.
12303 */
12304 static linenr_T
12305apply_prefixes(slang, word, round, flags, startlnum)
12306 slang_T *slang;
12307 char_u *word; /* case-folded word */
12308 int round;
12309 int flags; /* flags with prefix ID */
12310 linenr_T startlnum;
12311{
12312 idx_T arridx[MAXWLEN];
12313 int curi[MAXWLEN];
12314 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012315 char_u word_up[MAXWLEN];
12316 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012317 int c;
12318 char_u *byts;
12319 idx_T *idxs;
12320 linenr_T lnum = startlnum;
12321 int depth;
12322 int n;
12323 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012324 int i;
12325
Bram Moolenaar53805d12005-08-01 07:08:33 +000012326 /* if the word starts with a lower-case letter make the word with an
12327 * upper-case letter in word_up[]. */
12328 c = PTR2CHAR(word);
12329 if (SPELL_TOUPPER(c) != c)
12330 {
12331 onecap_copy(word, word_up, TRUE);
12332 has_word_up = TRUE;
12333 }
12334
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012335 byts = slang->sl_pbyts;
12336 idxs = slang->sl_pidxs;
12337 if (byts != NULL) /* array not is empty */
12338 {
12339 /*
12340 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012341 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012342 */
12343 depth = 0;
12344 arridx[0] = 0;
12345 curi[0] = 1;
12346 while (depth >= 0 && !got_int)
12347 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012348 n = arridx[depth];
12349 len = byts[n];
12350 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012351 {
12352 /* Done all bytes at this node, go up one level. */
12353 --depth;
12354 line_breakcheck();
12355 }
12356 else
12357 {
12358 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012359 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012360 ++curi[depth];
12361 c = byts[n];
12362 if (c == 0)
12363 {
12364 /* End of prefix, find out how many IDs there are. */
12365 for (i = 1; i < len; ++i)
12366 if (byts[n + i] != 0)
12367 break;
12368 curi[depth] += i - 1;
12369
Bram Moolenaar53805d12005-08-01 07:08:33 +000012370 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12371 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012372 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012373 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012374 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012375 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012376 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012377 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012378
12379 /* Check for prefix that matches the word when the
12380 * first letter is upper-case, but only if the prefix has
12381 * a condition. */
12382 if (has_word_up)
12383 {
12384 c = valid_word_prefix(i, n, flags, word_up, slang,
12385 TRUE);
12386 if (c != 0)
12387 {
12388 vim_strncpy(prefix + depth, word_up,
12389 MAXWLEN - depth - 1);
12390 dump_word(prefix, round,
12391 (c & WF_RAREPFX) ? (flags | WF_RARE)
12392 : flags, lnum++);
12393 }
12394 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012395 }
12396 else
12397 {
12398 /* Normal char, go one level deeper. */
12399 prefix[depth++] = c;
12400 arridx[depth] = idxs[n];
12401 curi[depth] = 1;
12402 }
12403 }
12404 }
12405 }
12406
12407 return lnum;
12408}
12409
Bram Moolenaar95529562005-08-25 21:21:38 +000012410/*
12411 * Move "p" to end of word.
12412 */
12413 char_u *
12414spell_to_word_end(start, buf)
12415 char_u *start;
12416 buf_T *buf;
12417{
12418 char_u *p = start;
12419
12420 while (*p != NUL && spell_iswordp(p, buf))
12421 mb_ptr_adv(p);
12422 return p;
12423}
12424
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012425#if defined(FEAT_INS_EXPAND) || defined(PROTO)
12426static int spell_expand_need_cap;
12427
12428/*
12429 * Find start of the word in front of the cursor. We don't check if it is
12430 * badly spelled, with completion we can only change the word in front of the
12431 * cursor.
12432 * Used for Insert mode completion CTRL-X ?.
12433 * Returns the column number of the word.
12434 */
12435 int
12436spell_word_start(startcol)
12437 int startcol;
12438{
12439 char_u *line;
12440 char_u *p;
12441 int col = 0;
12442
Bram Moolenaar95529562005-08-25 21:21:38 +000012443 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012444 return startcol;
12445
12446 /* Find a word character before "startcol". */
12447 line = ml_get_curline();
12448 for (p = line + startcol; p > line; )
12449 {
12450 mb_ptr_back(line, p);
12451 if (spell_iswordp_nmw(p))
12452 break;
12453 }
12454
12455 /* Go back to start of the word. */
12456 while (p > line)
12457 {
12458 col = p - line;
12459 mb_ptr_back(line, p);
12460 if (!spell_iswordp(p, curbuf))
12461 break;
12462 col = 0;
12463 }
12464
12465 /* Need to check for 'spellcapcheck' now, the word is removed before
12466 * expand_spelling() is called. Therefore the ugly global variable. */
12467 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12468
12469 return col;
12470}
12471
12472/*
12473 * Get list of spelling suggestions.
12474 * Used for Insert mode completion CTRL-X ?.
12475 * Returns the number of matches. The matches are in "matchp[]", array of
12476 * allocated strings.
12477 */
12478/*ARGSUSED*/
12479 int
12480expand_spelling(lnum, col, pat, matchp)
12481 linenr_T lnum;
12482 int col;
12483 char_u *pat;
12484 char_u ***matchp;
12485{
12486 garray_T ga;
12487
12488 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12489 *matchp = ga.ga_data;
12490 return ga.ga_len;
12491}
12492#endif
12493
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012494#endif /* FEAT_SYN_HL */