blob: c3f46986bcada7cd6bf03dfc259e4c8b9aef4982 [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 Moolenaarac6e65f2005-08-29 22:25:38 +0000217 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000218 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000219 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000220 *
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 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000276#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000277
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000278#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000279
Bram Moolenaar53805d12005-08-01 07:08:33 +0000280/* flags for <pflags> */
281#define WFP_RARE 0x01 /* rare prefix */
282#define WFP_NC 0x02 /* prefix is not combining */
283#define WFP_UP 0x04 /* to-upper prefix */
284
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000285/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000286 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000287#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
288 * postponed prefix */
289#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
290 * postponed prefix */
291#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
292 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000293
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000294/* Special byte values for <byte>. Some are only used in the tree for
295 * postponed prefixes, some only in the other trees. This is a bit messy... */
296#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000297 * postponed prefix: no <pflags> */
298#define BY_INDEX 1 /* child is shared, index follows */
299#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
300 * postponed prefix: <pflags> follows */
301#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
302 * follow; never used in prefix tree */
303#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000305/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000306 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000307 * One replacement: from "ft_from" to "ft_to". */
308typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000309{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000310 char_u *ft_from;
311 char_u *ft_to;
312} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000313
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000314/* Info from "SAL" entries in ".aff" file used in sl_sal.
315 * The info is split for quick processing by spell_soundfold().
316 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
317typedef struct salitem_S
318{
319 char_u *sm_lead; /* leading letters */
320 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000321 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000322 char_u *sm_rules; /* rules like ^, $, priority */
323 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000324#ifdef FEAT_MBYTE
325 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000326 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000327 int *sm_to_w; /* wide character copy of "sm_to" */
328#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000329} salitem_T;
330
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000331#ifdef FEAT_MBYTE
332typedef int salfirst_T;
333#else
334typedef short salfirst_T;
335#endif
336
Bram Moolenaar5195e452005-08-19 20:32:47 +0000337/* Values for SP_*ERROR are negative, positive values are used by
338 * read_cnt_string(). */
339#define SP_TRUNCERROR -1 /* spell file truncated error */
340#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000341#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000342
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000343/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000344 * Structure used to store words and other info for one language, loaded from
345 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
347 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
348 *
349 * The "byts" array stores the possible bytes in each tree node, preceded by
350 * the number of possible bytes, sorted on byte value:
351 * <len> <byte1> <byte2> ...
352 * The "idxs" array stores the index of the child node corresponding to the
353 * byte in "byts".
354 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000355 * the flags, region mask and affixID for the word. There may be several
356 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357 */
358typedef struct slang_S slang_T;
359struct slang_S
360{
361 slang_T *sl_next; /* next language */
362 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000363 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000365
Bram Moolenaar51485f02005-06-04 21:55:20 +0000366 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000367 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000368 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000369 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000370 char_u *sl_pbyts; /* prefix tree word bytes */
371 idx_T *sl_pidxs; /* prefix tree word indexes */
372
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000373 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000375 char_u *sl_midword; /* MIDWORD string or NULL */
376
Bram Moolenaar5195e452005-08-19 20:32:47 +0000377 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000378 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000379 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
380 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
381 * (NULL when no compounding) */
382 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000383 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000384 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000385 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
386 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000387
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000388 int sl_prefixcnt; /* number of items in "sl_prefprog" */
389 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 garray_T sl_rep; /* list of fromto_T entries from REP lines */
392 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
393 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000394 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000395 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000396 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000397 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
398 * "sl_sal_first" maps chars, when has_mbyte
399 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400 int sl_followup; /* SAL followup */
401 int sl_collapse; /* SAL collapse_result */
402 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000403 int sl_has_map; /* TRUE if there is a MAP line */
404#ifdef FEAT_MBYTE
405 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
406 int sl_map_array[256]; /* MAP for first 256 chars */
407#else
408 char_u sl_map_array[256]; /* MAP for first 256 chars */
409#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410};
411
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000412/* First language that is loaded, start of the linked list of loaded
413 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414static slang_T *first_lang = NULL;
415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000416/* Flags used in .spl file for soundsalike flags. */
417#define SAL_F0LLOWUP 1
418#define SAL_COLLAPSE 2
419#define SAL_REM_ACCENTS 4
420
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000421/*
422 * Structure used in "b_langp", filled from 'spelllang'.
423 */
424typedef struct langp_S
425{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000426 slang_T *lp_slang; /* info for this language */
427 slang_T *lp_sallang; /* language used for sound folding or NULL */
428 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000429 int lp_region; /* bitmask for region or REGION_ALL */
430} langp_T;
431
432#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
433
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000434#define REGION_ALL 0xff /* word valid in all regions */
435
Bram Moolenaar5195e452005-08-19 20:32:47 +0000436#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
437#define VIMSPELLMAGICL 8
438#define VIMSPELLVERSION 50
439
440/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
441#define SN_REGION 0 /* <regionname> section */
442#define SN_CHARFLAGS 1 /* charflags section */
443#define SN_MIDWORD 2 /* <midword> section */
444#define SN_PREFCOND 3 /* <prefcond> section */
445#define SN_REP 4 /* REP items section */
446#define SN_SAL 5 /* SAL items section */
447#define SN_SOFO 6 /* soundfolding section */
448#define SN_MAP 7 /* MAP items section */
449#define SN_COMPOUND 8 /* compound words section */
450#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000451#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000452#define SN_END 255 /* end of sections */
453
454#define SNF_REQUIRED 1 /* <sectionflags>: required section */
455
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000456/* Result values. Lower number is accepted over higher one. */
457#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000458#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000459#define SP_RARE 1
460#define SP_LOCAL 2
461#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000462
Bram Moolenaar7887d882005-07-01 22:33:52 +0000463/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000464static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000465
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000466/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000467 * Information used when looking for suggestions.
468 */
469typedef struct suginfo_S
470{
471 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000472 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475 char_u *su_badptr; /* start of bad word in line */
476 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000477 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000478 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
479 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000480 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
481 slang_T *su_slang_first; /* slang_T used for su_sal_badword */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000483 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000484} suginfo_T;
485
486/* One word suggestion. Used in "si_ga". */
487typedef struct suggest_S
488{
489 char_u *st_word; /* suggested word, allocated string */
490 int st_orglen; /* length of replaced text */
491 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492 int st_altscore; /* used when st_score compares equal */
493 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000494 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000495 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000496} suggest_T;
497
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000498#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000499
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000500/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
501 * called the score may change, thus we need to keep more than what is
502 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000503#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000504
505/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
506 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000507#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000508
509/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000510#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000511#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000512#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000513#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000514#define SCORE_SWAP 90 /* swap two characters */
515#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000516#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000517#define SCORE_SUBST 93 /* substitute a character */
518#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000519#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000520#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000521#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000522#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000523#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000524#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000525#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000526#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000528#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000529#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
530 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000531
532#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000533#define SCORE_MAXMAX 999999 /* accept any score */
534
535/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000536 * Structure to store info for word matching.
537 */
538typedef struct matchinf_S
539{
540 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000541
542 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000543 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000544 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000545 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000546 char_u *mi_cend; /* char after what was used for
547 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000548
549 /* case-folded text */
550 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000551 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000552
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000553 /* for when checking word after a prefix */
554 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000555 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000556 int mi_prefcnt; /* number of entries at mi_prefarridx */
557 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000558#ifdef FEAT_MBYTE
559 int mi_cprefixlen; /* byte length of prefix in original
560 case */
561#else
562# define mi_cprefixlen mi_prefixlen /* it's the same value */
563#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000564
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000565 /* for when checking a compound word */
566 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000567 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
568 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000569
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000570 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000572 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000573 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000574
575 /* for NOBREAK */
576 int mi_result2; /* "mi_resul" without following word */
577 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000578} matchinf_T;
579
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000580/*
581 * The tables used for recognizing word characters according to spelling.
582 * These are only used for the first 256 characters of 'encoding'.
583 */
584typedef struct spelltab_S
585{
586 char_u st_isw[256]; /* flags: is word char */
587 char_u st_isu[256]; /* flags: is uppercase char */
588 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000589 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590} spelltab_T;
591
592static spelltab_T spelltab;
593static int did_set_spelltab;
594
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000595#define CF_WORD 0x01
596#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000597
598static void clear_spell_chartab __ARGS((spelltab_T *sp));
599static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000600static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
601static int spell_iswordp_nmw __ARGS((char_u *p));
602#ifdef FEAT_MBYTE
603static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
604#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000605static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000606
607/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000608 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000609 */
610typedef enum
611{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000612 STATE_START = 0, /* At start of node check for NUL bytes (goodword
613 * ends); if badword ends there is a match, otherwise
614 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000615 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000617 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
618 STATE_PLAIN, /* Use each byte of the node. */
619 STATE_DEL, /* Delete a byte from the bad word. */
620 STATE_INS, /* Insert a byte in the bad word. */
621 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000622 STATE_UNSWAP, /* Undo swap two characters. */
623 STATE_SWAP3, /* Swap two characters over three. */
624 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000626 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000627 STATE_REP_INI, /* Prepare for using REP items. */
628 STATE_REP, /* Use matching REP items from the .aff file. */
629 STATE_REP_UNDO, /* Undo a REP item replacement. */
630 STATE_FINAL /* End of this node. */
631} state_T;
632
633/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000634 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000635 */
636typedef struct trystate_S
637{
Bram Moolenaarea424162005-06-16 21:51:00 +0000638 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000639 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000641 short ts_curi; /* index in list of child nodes */
642 char_u ts_fidx; /* index in fword[], case-folded bad word */
643 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
644 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000645 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000646 * PFD_PREFIXTREE or PFD_NOPREFIX */
647 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000648#ifdef FEAT_MBYTE
649 char_u ts_tcharlen; /* number of bytes in tword character */
650 char_u ts_tcharidx; /* current byte index in tword character */
651 char_u ts_isdiff; /* DIFF_ values */
652 char_u ts_fcharstart; /* index in fword where badword char started */
653#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000654 char_u ts_prewordlen; /* length of word in "preword[]" */
655 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000656 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000657 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000658 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000659 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660} trystate_T;
661
Bram Moolenaarea424162005-06-16 21:51:00 +0000662/* values for ts_isdiff */
663#define DIFF_NONE 0 /* no different byte (yet) */
664#define DIFF_YES 1 /* different byte found */
665#define DIFF_INSERT 2 /* inserting character */
666
Bram Moolenaard12a1322005-08-21 22:08:24 +0000667/* values for ts_flags */
668#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
669#define TSF_DIDSPLIT 2 /* tried split at this point */
670
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000671/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000672#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000673#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
674#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000675
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000676/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000677#define FIND_FOLDWORD 0 /* find word case-folded */
678#define FIND_KEEPWORD 1 /* find keep-case word */
679#define FIND_PREFIX 2 /* find word after prefix */
680#define FIND_COMPOUND 3 /* find case-folded compound word */
681#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000682
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000683static slang_T *slang_alloc __ARGS((char_u *lang));
684static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000685static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000686static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000687static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000688static 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 +0000689static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000690static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000691static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000692static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000693static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000694static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000695static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000696static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000697static 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 +0000698static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000699static char_u *read_string __ARGS((FILE *fd, int cnt));
700static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
701static int read_charflags_section __ARGS((FILE *fd));
702static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
703static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
704static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
705static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
706static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000707static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000708static int init_syl_tab __ARGS((slang_T *slang));
709static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000710static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
711static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000712#ifdef FEAT_MBYTE
713static int *mb_str2wide __ARGS((char_u *s));
714#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000715static 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 +0000716static void clear_midword __ARGS((buf_T *buf));
717static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000718static int find_region __ARGS((char_u *rp, char_u *region));
719static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000720static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000721static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000722static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000723static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000724static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000725static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000726static 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 +0000727#ifdef FEAT_EVAL
728static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
729#endif
730static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
731static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000732static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000733static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000735static void suggest_try_special __ARGS((suginfo_T *su));
736static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000737static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000738#ifdef FEAT_MBYTE
739static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
740#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000741static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000742static void score_comp_sal __ARGS((suginfo_T *su));
743static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000744static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000745static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000746static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000747static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000748static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000749static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000750static void add_banned __ARGS((suginfo_T *su, char_u *word));
751static int was_banned __ARGS((suginfo_T *su, char_u *word));
752static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000753static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000754static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000756static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
757static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
758static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000759#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000760static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000761#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000762static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000763static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000764static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000765static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000766
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000767/*
768 * Use our own character-case definitions, because the current locale may
769 * differ from what the .spl file uses.
770 * These must not be called with negative number!
771 */
772#ifndef FEAT_MBYTE
773/* Non-multi-byte implementation. */
774# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
775# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
776# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
777#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000778# if defined(HAVE_WCHAR_H)
779# include <wchar.h> /* for towupper() and towlower() */
780# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000781/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
782 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
783 * the "w" library function for characters above 255 if available. */
784# ifdef HAVE_TOWLOWER
785# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
786 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
787# else
788# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
789 : (c) < 256 ? spelltab.st_fold[c] : (c))
790# endif
791
792# ifdef HAVE_TOWUPPER
793# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
794 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
795# else
796# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
797 : (c) < 256 ? spelltab.st_upper[c] : (c))
798# endif
799
800# ifdef HAVE_ISWUPPER
801# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
802 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
803# else
804# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000805 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000806# endif
807#endif
808
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000809
810static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000811static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000812static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000813static char *e_affname = N_("Affix name too long in %s line %d: %s");
814static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
815static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000816static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000817
818/*
819 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000820 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000821 * "*attrp" is set to the highlight index for a badly spelled word. For a
822 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000823 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000824 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000825 * "capcol" is used to check for a Capitalised word after the end of a
826 * sentence. If it's zero then perform the check. Return the column where to
827 * check next, or -1 when no sentence end was found. If it's NULL then don't
828 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000829 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000830 * Returns the length of the word in bytes, also when it's OK, so that the
831 * caller can skip over the word.
832 */
833 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000834spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000835 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000837 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000838 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000839{
840 matchinf_T mi; /* Most things are put in "mi" so that it can
841 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000842 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000843 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000845 int lpi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000846
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000847 /* A word never starts at a space or a control character. Return quickly
848 * then, skipping over the character. */
849 if (*ptr <= ' ')
850 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000851 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000852
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000853 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000854 * 0X99FF. But always do check spelling to find "3GPP" and "11
855 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000856 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000858 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
859 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000860 else
861 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +0000862 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000863 }
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 Moolenaar43abc522005-12-10 20:15:02 +0000867 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000868 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000870 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000871 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000872 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000873 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000874
875 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
876 {
877 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000878 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000879 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000880 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000881 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000882 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000883 if (capcol != NULL)
884 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000885
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000886 /* We always use the characters up to the next non-word character,
887 * also for bad words. */
888 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000890 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000891 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000892
Bram Moolenaar5195e452005-08-19 20:32:47 +0000893 /* case-fold the word with one non-word character, so that we can check
894 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000895 if (*mi.mi_fend != NUL)
896 mb_ptr_adv(mi.mi_fend);
897
898 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
899 MAXWLEN + 1);
900 mi.mi_fwordlen = STRLEN(mi.mi_fword);
901
902 /* The word is bad unless we recognize it. */
903 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000904 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000905
906 /*
907 * Loop over the languages specified in 'spelllang'.
908 * We check them all, because a matching word may be longer than an
909 * already found matching word.
910 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000911 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000912 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000913 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
914
915 /* If reloading fails the language is still in the list but everything
916 * has been cleared. */
917 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
918 continue;
919
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000920 /* Check for a matching word in case-folded words. */
921 find_word(&mi, FIND_FOLDWORD);
922
923 /* Check for a matching word in keep-case words. */
924 find_word(&mi, FIND_KEEPWORD);
925
926 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000927 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000928
929 /* For a NOBREAK language, may want to use a word without a following
930 * word as a backup. */
931 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
932 && mi.mi_result2 != SP_BAD)
933 {
934 mi.mi_result = mi.mi_result2;
935 mi.mi_end = mi.mi_end2;
936 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000937 }
938
939 if (mi.mi_result != SP_OK)
940 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000941 /* If we found a number skip over it. Allows for "42nd". Do flag
942 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000943 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000944 {
945 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
946 return nrlen;
947 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000948
949 /* When we are at a non-word character there is no error, just
950 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000951 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000952 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000953 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
954 {
955 regmatch_T regmatch;
956
957 /* Check for end of sentence. */
958 regmatch.regprog = wp->w_buffer->b_cap_prog;
959 regmatch.rm_ic = FALSE;
960 if (vim_regexec(&regmatch, ptr, 0))
961 *capcol = (int)(regmatch.endp[0] - ptr);
962 }
963
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000964#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000965 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000966 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000967#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000968 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000969 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000970 else if (mi.mi_end == ptr)
971 /* Always include at least one character. Required for when there
972 * is a mixup in "midword". */
973 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000974 else if (mi.mi_result == SP_BAD
975 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
976 {
977 char_u *p, *fp;
978 int save_result = mi.mi_result;
979
980 /* First language in 'spelllang' is NOBREAK. Find first position
981 * at which any word would be valid. */
982 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000983 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000984 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000985 p = mi.mi_word;
986 fp = mi.mi_fword;
987 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000988 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000989 mb_ptr_adv(p);
990 mb_ptr_adv(fp);
991 if (p >= mi.mi_end)
992 break;
993 mi.mi_compoff = fp - mi.mi_fword;
994 find_word(&mi, FIND_COMPOUND);
995 if (mi.mi_result != SP_BAD)
996 {
997 mi.mi_end = p;
998 break;
999 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001000 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001001 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001002 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001003 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001004
1005 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001006 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001007 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001008 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001009 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001010 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001011 }
1012
Bram Moolenaar5195e452005-08-19 20:32:47 +00001013 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1014 {
1015 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001016 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001017 return wrongcaplen;
1018 }
1019
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 return (int)(mi.mi_end - ptr);
1021}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022
Bram Moolenaar51485f02005-06-04 21:55:20 +00001023/*
1024 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001025 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1026 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1027 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1028 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001029 *
1030 * For a match mip->mi_result is updated.
1031 */
1032 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001033find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001034 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001036{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001037 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001038 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001039 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040 int endidxcnt = 0;
1041 int len;
1042 int wlen = 0;
1043 int flen;
1044 int c;
1045 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001046 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001047#ifdef FEAT_MBYTE
1048 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001049#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001050 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001051 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 slang_T *slang = mip->mi_lp->lp_slang;
1053 unsigned flags;
1054 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001055 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001056 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001057 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001058 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001060 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001061 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001062 /* Check for word with matching case in keep-case tree. */
1063 ptr = mip->mi_word;
1064 flen = 9999; /* no case folding, always enough bytes */
1065 byts = slang->sl_kbyts;
1066 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001067
1068 if (mode == FIND_KEEPCOMPOUND)
1069 /* Skip over the previously found word(s). */
1070 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001071 }
1072 else
1073 {
1074 /* Check for case-folded in case-folded tree. */
1075 ptr = mip->mi_fword;
1076 flen = mip->mi_fwordlen; /* available case-folded bytes */
1077 byts = slang->sl_fbyts;
1078 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079
1080 if (mode == FIND_PREFIX)
1081 {
1082 /* Skip over the prefix. */
1083 wlen = mip->mi_prefixlen;
1084 flen -= mip->mi_prefixlen;
1085 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001086 else if (mode == FIND_COMPOUND)
1087 {
1088 /* Skip over the previously found word(s). */
1089 wlen = mip->mi_compoff;
1090 flen -= mip->mi_compoff;
1091 }
1092
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001093 }
1094
Bram Moolenaar51485f02005-06-04 21:55:20 +00001095 if (byts == NULL)
1096 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001097
Bram Moolenaar51485f02005-06-04 21:55:20 +00001098 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001099 * Repeat advancing in the tree until:
1100 * - there is a byte that doesn't match,
1101 * - we reach the end of the tree,
1102 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001103 */
1104 for (;;)
1105 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001106 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001108
1109 len = byts[arridx++];
1110
1111 /* If the first possible byte is a zero the word could end here.
1112 * Remember this index, we first check for the longest word. */
1113 if (byts[arridx] == 0)
1114 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001115 if (endidxcnt == MAXWLEN)
1116 {
1117 /* Must be a corrupted spell file. */
1118 EMSG(_(e_format));
1119 return;
1120 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001121 endlen[endidxcnt] = wlen;
1122 endidx[endidxcnt++] = arridx++;
1123 --len;
1124
1125 /* Skip over the zeros, there can be several flag/region
1126 * combinations. */
1127 while (len > 0 && byts[arridx] == 0)
1128 {
1129 ++arridx;
1130 --len;
1131 }
1132 if (len == 0)
1133 break; /* no children, word must end here */
1134 }
1135
1136 /* Stop looking at end of the line. */
1137 if (ptr[wlen] == NUL)
1138 break;
1139
1140 /* Perform a binary search in the list of accepted bytes. */
1141 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001142 if (c == TAB) /* <Tab> is handled like <Space> */
1143 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001144 lo = arridx;
1145 hi = arridx + len - 1;
1146 while (lo < hi)
1147 {
1148 m = (lo + hi) / 2;
1149 if (byts[m] > c)
1150 hi = m - 1;
1151 else if (byts[m] < c)
1152 lo = m + 1;
1153 else
1154 {
1155 lo = hi = m;
1156 break;
1157 }
1158 }
1159
1160 /* Stop if there is no matching byte. */
1161 if (hi < lo || byts[lo] != c)
1162 break;
1163
1164 /* Continue at the child (if there is one). */
1165 arridx = idxs[lo];
1166 ++wlen;
1167 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001168
1169 /* One space in the good word may stand for several spaces in the
1170 * checked word. */
1171 if (c == ' ')
1172 {
1173 for (;;)
1174 {
1175 if (flen <= 0 && *mip->mi_fend != NUL)
1176 flen = fold_more(mip);
1177 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1178 break;
1179 ++wlen;
1180 --flen;
1181 }
1182 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001183 }
1184
1185 /*
1186 * Verify that one of the possible endings is valid. Try the longest
1187 * first.
1188 */
1189 while (endidxcnt > 0)
1190 {
1191 --endidxcnt;
1192 arridx = endidx[endidxcnt];
1193 wlen = endlen[endidxcnt];
1194
1195#ifdef FEAT_MBYTE
1196 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1197 continue; /* not at first byte of character */
1198#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001199 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001200 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001201 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001202 continue; /* next char is a word character */
1203 word_ends = FALSE;
1204 }
1205 else
1206 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001207 /* The prefix flag is before compound flags. Once a valid prefix flag
1208 * has been found we try compound flags. */
1209 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001210
1211#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001212 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001213 {
1214 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001215 * when folding case. This can be slow, take a shortcut when the
1216 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001218 if (STRNCMP(ptr, p, wlen) != 0)
1219 {
1220 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1221 mb_ptr_adv(p);
1222 wlen = p - mip->mi_word;
1223 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 }
1225#endif
1226
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001227 /* Check flags and region. For FIND_PREFIX check the condition and
1228 * prefix ID.
1229 * Repeat this if there are more flags/region alternatives until there
1230 * is a match. */
1231 res = SP_BAD;
1232 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1233 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 {
1235 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001236
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001237 /* For the fold-case tree check that the case of the checked word
1238 * matches with what the word in the tree requires.
1239 * For keep-case tree the case is always right. For prefixes we
1240 * don't bother to check. */
1241 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001243 if (mip->mi_cend != mip->mi_word + wlen)
1244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001245 /* mi_capflags was set for a different word length, need
1246 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001248 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 }
1250
Bram Moolenaar0c405862005-06-22 22:26:26 +00001251 if (mip->mi_capflags == WF_KEEPCAP
1252 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001253 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001254 }
1255
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001256 /* When mode is FIND_PREFIX the word must support the prefix:
1257 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001258 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001259 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001260 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001261 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001262 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001263 mip->mi_word + mip->mi_cprefixlen, slang,
1264 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001265 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001266 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001267
1268 /* Use the WF_RARE flag for a rare prefix. */
1269 if (c & WF_RAREPFX)
1270 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001271 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001272 }
1273
Bram Moolenaar78622822005-08-23 21:00:13 +00001274 if (slang->sl_nobreak)
1275 {
1276 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1277 && (flags & WF_BANNED) == 0)
1278 {
1279 /* NOBREAK: found a valid following word. That's all we
1280 * need to know, so return. */
1281 mip->mi_result = SP_OK;
1282 break;
1283 }
1284 }
1285
1286 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1287 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001288 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001289 /* If there is no flag or the word is shorter than
1290 * COMPOUNDMIN reject it quickly.
1291 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001292 * that's too short... Myspell compatibility requires this
1293 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001294 if (((unsigned)flags >> 24) == 0
1295 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001296 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001297#ifdef FEAT_MBYTE
1298 /* For multi-byte chars check character length against
1299 * COMPOUNDMIN. */
1300 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001301 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001302 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1303 wlen - mip->mi_compoff) < slang->sl_compminlen)
1304 continue;
1305#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001306
Bram Moolenaare52325c2005-08-22 22:54:29 +00001307 /* Limit the number of compound words to COMPOUNDMAX if no
1308 * maximum for syllables is specified. */
1309 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1310 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001311 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001312
Bram Moolenaard12a1322005-08-21 22:08:24 +00001313 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001314 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001315 ? slang->sl_compstartflags
1316 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001317 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001318 continue;
1319
Bram Moolenaare52325c2005-08-22 22:54:29 +00001320 if (mode == FIND_COMPOUND)
1321 {
1322 int capflags;
1323
1324 /* Need to check the caps type of the appended compound
1325 * word. */
1326#ifdef FEAT_MBYTE
1327 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1328 mip->mi_compoff) != 0)
1329 {
1330 /* case folding may have changed the length */
1331 p = mip->mi_word;
1332 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1333 mb_ptr_adv(p);
1334 }
1335 else
1336#endif
1337 p = mip->mi_word + mip->mi_compoff;
1338 capflags = captype(p, mip->mi_word + wlen);
1339 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1340 && (flags & WF_FIXCAP) != 0))
1341 continue;
1342
1343 if (capflags != WF_ALLCAP)
1344 {
1345 /* When the character before the word is a word
1346 * character we do not accept a Onecap word. We do
1347 * accept a no-caps word, even when the dictionary
1348 * word specifies ONECAP. */
1349 mb_ptr_back(mip->mi_word, p);
1350 if (spell_iswordp_nmw(p)
1351 ? capflags == WF_ONECAP
1352 : (flags & WF_ONECAP) != 0
1353 && capflags != WF_ONECAP)
1354 continue;
1355 }
1356 }
1357
Bram Moolenaar5195e452005-08-19 20:32:47 +00001358 /* If the word ends the sequence of compound flags of the
1359 * words must match with one of the COMPOUNDFLAGS items and
1360 * the number of syllables must not be too large. */
1361 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1362 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1363 if (word_ends)
1364 {
1365 char_u fword[MAXWLEN];
1366
1367 if (slang->sl_compsylmax < MAXWLEN)
1368 {
1369 /* "fword" is only needed for checking syllables. */
1370 if (ptr == mip->mi_word)
1371 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1372 else
1373 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1374 }
1375 if (!can_compound(slang, fword, mip->mi_compflags))
1376 continue;
1377 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001378 }
1379
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001380 /* Check NEEDCOMPOUND: can't use word without compounding. */
1381 else if (flags & WF_NEEDCOMP)
1382 continue;
1383
Bram Moolenaar78622822005-08-23 21:00:13 +00001384 nobreak_result = SP_OK;
1385
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001386 if (!word_ends)
1387 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001388 int save_result = mip->mi_result;
1389 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001390 langp_T *save_lp = mip->mi_lp;
1391 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001392
1393 /* Check that a valid word follows. If there is one and we
1394 * are compounding, it will set "mi_result", thus we are
1395 * always finished here. For NOBREAK we only check that a
1396 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 if (slang->sl_nobreak)
1399 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001400
1401 /* Find following word in case-folded tree. */
1402 mip->mi_compoff = endlen[endidxcnt];
1403#ifdef FEAT_MBYTE
1404 if (has_mbyte && mode == FIND_KEEPWORD)
1405 {
1406 /* Compute byte length in case-folded word from "wlen":
1407 * byte length in keep-case word. Length may change when
1408 * folding case. This can be slow, take a shortcut when
1409 * the case-folded word is equal to the keep-case word. */
1410 p = mip->mi_fword;
1411 if (STRNCMP(ptr, p, wlen) != 0)
1412 {
1413 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1414 mb_ptr_adv(p);
1415 mip->mi_compoff = p - mip->mi_fword;
1416 }
1417 }
1418#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001419 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001420 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001421
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001422 /* For NOBREAK we need to try all NOBREAK languages, at least
1423 * to find the ".add" file(s). */
1424 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001425 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001426 if (slang->sl_nobreak)
1427 {
1428 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1429 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1430 || !mip->mi_lp->lp_slang->sl_nobreak)
1431 continue;
1432 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001433
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001434 find_word(mip, FIND_COMPOUND);
1435
1436 /* When NOBREAK any word that matches is OK. Otherwise we
1437 * need to find the longest match, thus try with keep-case
1438 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001439 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1440 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001441 /* Find following word in keep-case tree. */
1442 mip->mi_compoff = wlen;
1443 find_word(mip, FIND_KEEPCOMPOUND);
1444
1445 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1446 {
1447 /* Check for following word with prefix. */
1448 mip->mi_compoff = c;
1449 find_prefix(mip, FIND_COMPOUND);
1450 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001451 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001452
1453 if (!slang->sl_nobreak)
1454 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001455 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001456 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001457 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001458
Bram Moolenaar78622822005-08-23 21:00:13 +00001459 if (slang->sl_nobreak)
1460 {
1461 nobreak_result = mip->mi_result;
1462 mip->mi_result = save_result;
1463 mip->mi_end = save_end;
1464 }
1465 else
1466 {
1467 if (mip->mi_result == SP_OK)
1468 break;
1469 continue;
1470 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001471 }
1472
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473 if (flags & WF_BANNED)
1474 res = SP_BANNED;
1475 else if (flags & WF_REGION)
1476 {
1477 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001478 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001479 res = SP_OK;
1480 else
1481 res = SP_LOCAL;
1482 }
1483 else if (flags & WF_RARE)
1484 res = SP_RARE;
1485 else
1486 res = SP_OK;
1487
Bram Moolenaar78622822005-08-23 21:00:13 +00001488 /* Always use the longest match and the best result. For NOBREAK
1489 * we separately keep the longest match without a following good
1490 * word as a fall-back. */
1491 if (nobreak_result == SP_BAD)
1492 {
1493 if (mip->mi_result2 > res)
1494 {
1495 mip->mi_result2 = res;
1496 mip->mi_end2 = mip->mi_word + wlen;
1497 }
1498 else if (mip->mi_result2 == res
1499 && mip->mi_end2 < mip->mi_word + wlen)
1500 mip->mi_end2 = mip->mi_word + wlen;
1501 }
1502 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001503 {
1504 mip->mi_result = res;
1505 mip->mi_end = mip->mi_word + wlen;
1506 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001507 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001508 mip->mi_end = mip->mi_word + wlen;
1509
Bram Moolenaar78622822005-08-23 21:00:13 +00001510 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001511 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001512 }
1513
Bram Moolenaar78622822005-08-23 21:00:13 +00001514 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001515 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001516 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001517}
1518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001519/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001520 * Return TRUE if "flags" is a valid sequence of compound flags and
1521 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001522 */
1523 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001524can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001525 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001526 char_u *word;
1527 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001528{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001529 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001530#ifdef FEAT_MBYTE
1531 char_u uflags[MAXWLEN * 2];
1532 int i;
1533#endif
1534 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001535
1536 if (slang->sl_compprog == NULL)
1537 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001538#ifdef FEAT_MBYTE
1539 if (enc_utf8)
1540 {
1541 /* Need to convert the single byte flags to utf8 characters. */
1542 p = uflags;
1543 for (i = 0; flags[i] != NUL; ++i)
1544 p += mb_char2bytes(flags[i], p);
1545 *p = NUL;
1546 p = uflags;
1547 }
1548 else
1549#endif
1550 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001551 regmatch.regprog = slang->sl_compprog;
1552 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001553 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001554 return FALSE;
1555
Bram Moolenaare52325c2005-08-22 22:54:29 +00001556 /* Count the number of syllables. This may be slow, do it last. If there
1557 * are too many syllables AND the number of compound words is above
1558 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001559 if (slang->sl_compsylmax < MAXWLEN
1560 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001561 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001562 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001563}
1564
1565/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001566 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1567 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001568 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001569 */
1570 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001571valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001572 int totprefcnt; /* nr of prefix IDs */
1573 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001574 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001575 char_u *word;
1576 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001577 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001578{
1579 int prefcnt;
1580 int pidx;
1581 regprog_T *rp;
1582 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001583 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001584
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001585 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001586 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1587 {
1588 pidx = slang->sl_pidxs[arridx + prefcnt];
1589
1590 /* Check the prefix ID. */
1591 if (prefid != (pidx & 0xff))
1592 continue;
1593
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001594 /* Check if the prefix doesn't combine and the word already has a
1595 * suffix. */
1596 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1597 continue;
1598
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001599 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001600 * stored in the two bytes above the prefix ID byte. */
1601 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001602 if (rp != NULL)
1603 {
1604 regmatch.regprog = rp;
1605 regmatch.rm_ic = FALSE;
1606 if (!vim_regexec(&regmatch, word, 0))
1607 continue;
1608 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001609 else if (cond_req)
1610 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001611
Bram Moolenaar53805d12005-08-01 07:08:33 +00001612 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001613 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001614 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001615 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001616}
1617
1618/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001619 * Check if the word at "mip->mi_word" has a matching prefix.
1620 * If it does, then check the following word.
1621 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001622 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1623 * prefix in a compound word.
1624 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001625 * For a match mip->mi_result is updated.
1626 */
1627 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001628find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001629 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001630 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001631{
1632 idx_T arridx = 0;
1633 int len;
1634 int wlen = 0;
1635 int flen;
1636 int c;
1637 char_u *ptr;
1638 idx_T lo, hi, m;
1639 slang_T *slang = mip->mi_lp->lp_slang;
1640 char_u *byts;
1641 idx_T *idxs;
1642
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001643 byts = slang->sl_pbyts;
1644 if (byts == NULL)
1645 return; /* array is empty */
1646
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001647 /* We use the case-folded word here, since prefixes are always
1648 * case-folded. */
1649 ptr = mip->mi_fword;
1650 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001651 if (mode == FIND_COMPOUND)
1652 {
1653 /* Skip over the previously found word(s). */
1654 ptr += mip->mi_compoff;
1655 flen -= mip->mi_compoff;
1656 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001657 idxs = slang->sl_pidxs;
1658
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001659 /*
1660 * Repeat advancing in the tree until:
1661 * - there is a byte that doesn't match,
1662 * - we reach the end of the tree,
1663 * - or we reach the end of the line.
1664 */
1665 for (;;)
1666 {
1667 if (flen == 0 && *mip->mi_fend != NUL)
1668 flen = fold_more(mip);
1669
1670 len = byts[arridx++];
1671
1672 /* If the first possible byte is a zero the prefix could end here.
1673 * Check if the following word matches and supports the prefix. */
1674 if (byts[arridx] == 0)
1675 {
1676 /* There can be several prefixes with different conditions. We
1677 * try them all, since we don't know which one will give the
1678 * longest match. The word is the same each time, pass the list
1679 * of possible prefixes to find_word(). */
1680 mip->mi_prefarridx = arridx;
1681 mip->mi_prefcnt = len;
1682 while (len > 0 && byts[arridx] == 0)
1683 {
1684 ++arridx;
1685 --len;
1686 }
1687 mip->mi_prefcnt -= len;
1688
1689 /* Find the word that comes after the prefix. */
1690 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001691 if (mode == FIND_COMPOUND)
1692 /* Skip over the previously found word(s). */
1693 mip->mi_prefixlen += mip->mi_compoff;
1694
Bram Moolenaar53805d12005-08-01 07:08:33 +00001695#ifdef FEAT_MBYTE
1696 if (has_mbyte)
1697 {
1698 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001699 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1700 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001701 }
1702 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001703 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001704#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001705 find_word(mip, FIND_PREFIX);
1706
1707
1708 if (len == 0)
1709 break; /* no children, word must end here */
1710 }
1711
1712 /* Stop looking at end of the line. */
1713 if (ptr[wlen] == NUL)
1714 break;
1715
1716 /* Perform a binary search in the list of accepted bytes. */
1717 c = ptr[wlen];
1718 lo = arridx;
1719 hi = arridx + len - 1;
1720 while (lo < hi)
1721 {
1722 m = (lo + hi) / 2;
1723 if (byts[m] > c)
1724 hi = m - 1;
1725 else if (byts[m] < c)
1726 lo = m + 1;
1727 else
1728 {
1729 lo = hi = m;
1730 break;
1731 }
1732 }
1733
1734 /* Stop if there is no matching byte. */
1735 if (hi < lo || byts[lo] != c)
1736 break;
1737
1738 /* Continue at the child (if there is one). */
1739 arridx = idxs[lo];
1740 ++wlen;
1741 --flen;
1742 }
1743}
1744
1745/*
1746 * Need to fold at least one more character. Do until next non-word character
1747 * for efficiency.
1748 * Return the length of the folded chars in bytes.
1749 */
1750 static int
1751fold_more(mip)
1752 matchinf_T *mip;
1753{
1754 int flen;
1755 char_u *p;
1756
1757 p = mip->mi_fend;
1758 do
1759 {
1760 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001761 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001762
1763 /* Include the non-word character so that we can check for the
1764 * word end. */
1765 if (*mip->mi_fend != NUL)
1766 mb_ptr_adv(mip->mi_fend);
1767
1768 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1769 mip->mi_fword + mip->mi_fwordlen,
1770 MAXWLEN - mip->mi_fwordlen);
1771 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1772 mip->mi_fwordlen += flen;
1773 return flen;
1774}
1775
1776/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001777 * Check case flags for a word. Return TRUE if the word has the requested
1778 * case.
1779 */
1780 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001781spell_valid_case(wordflags, treeflags)
1782 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001783 int treeflags; /* flags for the word in the spell tree */
1784{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001785 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001787 && ((treeflags & WF_ONECAP) == 0
1788 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001789}
1790
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791/*
1792 * Return TRUE if spell checking is not enabled.
1793 */
1794 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001795no_spell_checking(wp)
1796 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001797{
Bram Moolenaar95529562005-08-25 21:21:38 +00001798 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799 {
1800 EMSG(_("E756: Spell checking is not enabled"));
1801 return TRUE;
1802 }
1803 return FALSE;
1804}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001805
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806/*
1807 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001808 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1809 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001810 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1811 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001812 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001813 */
1814 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001815spell_move_to(wp, dir, allwords, curline, attrp)
1816 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001817 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001818 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001819 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001820 hlf_T *attrp; /* return: attributes of bad word or NULL
1821 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001823 linenr_T lnum;
1824 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001825 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826 char_u *line;
1827 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001828 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001829 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001831 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001832 int col;
1833 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001834 char_u *buf = NULL;
1835 int buflen = 0;
1836 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001837 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001838 int found_one = FALSE;
1839 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840
Bram Moolenaar95529562005-08-25 21:21:38 +00001841 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001842 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001843
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001844 /*
1845 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001846 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001847 *
1848 * When searching backwards, we continue in the line to find the last
1849 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001850 *
1851 * We concatenate the start of the next line, so that wrapped words work
1852 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1853 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001854 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001855 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001856 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857
1858 while (!got_int)
1859 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001860 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001861
Bram Moolenaar0c405862005-06-22 22:26:26 +00001862 len = STRLEN(line);
1863 if (buflen < len + MAXWLEN + 2)
1864 {
1865 vim_free(buf);
1866 buflen = len + MAXWLEN + 2;
1867 buf = alloc(buflen);
1868 if (buf == NULL)
1869 break;
1870 }
1871
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001872 /* In first line check first word for Capital. */
1873 if (lnum == 1)
1874 capcol = 0;
1875
1876 /* For checking first word with a capital skip white space. */
1877 if (capcol == 0)
1878 capcol = skipwhite(line) - line;
1879
Bram Moolenaar0c405862005-06-22 22:26:26 +00001880 /* Copy the line into "buf" and append the start of the next line if
1881 * possible. */
1882 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001883 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001884 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1885
1886 p = buf + skip;
1887 endp = buf + len;
1888 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001890 /* When searching backward don't search after the cursor. Unless
1891 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001892 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001893 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001894 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001895 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001896 break;
1897
1898 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001899 attr = HLF_COUNT;
Bram Moolenaar95529562005-08-25 21:21:38 +00001900 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001901
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001902 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001905 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001906 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001907 found_one = TRUE;
1908
Bram Moolenaar51485f02005-06-04 21:55:20 +00001909 /* When searching forward only accept a bad word after
1910 * the cursor. */
1911 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001912 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001913 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001914 && (wrapped
1915 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001916 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001917 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001918 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001919 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001920 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001921 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001922 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001923 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001924 }
1925 else
1926 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001927
Bram Moolenaar51485f02005-06-04 21:55:20 +00001928 if (can_spell)
1929 {
1930 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001931 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001932#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001933 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001934#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001935 if (dir == FORWARD)
1936 {
1937 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001938 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001939 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001940 if (attrp != NULL)
1941 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001942 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001943 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001944 else if (curline)
1945 /* Insert mode completion: put cursor after
1946 * the bad word. */
1947 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001948 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001949 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001950 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952 }
1953
Bram Moolenaar51485f02005-06-04 21:55:20 +00001954 /* advance to character after the word */
1955 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001956 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001957 }
1958
Bram Moolenaar5195e452005-08-19 20:32:47 +00001959 if (dir == BACKWARD && found_pos.lnum != 0)
1960 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001961 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001962 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001963 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001964 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001965 }
1966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001968 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001971 if (dir == BACKWARD)
1972 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001973 /* If we are back at the starting line and searched it again there
1974 * is no match, give up. */
1975 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001976 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001977
1978 if (lnum > 1)
1979 --lnum;
1980 else if (!p_ws)
1981 break; /* at first line and 'nowrapscan' */
1982 else
1983 {
1984 /* Wrap around to the end of the buffer. May search the
1985 * starting line again and accept the last match. */
1986 lnum = wp->w_buffer->b_ml.ml_line_count;
1987 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001988 if (!shortmess(SHM_SEARCH))
1989 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001990 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001991 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001992 }
1993 else
1994 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001995 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1996 ++lnum;
1997 else if (!p_ws)
1998 break; /* at first line and 'nowrapscan' */
1999 else
2000 {
2001 /* Wrap around to the start of the buffer. May search the
2002 * starting line again and accept the first match. */
2003 lnum = 1;
2004 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002005 if (!shortmess(SHM_SEARCH))
2006 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002007 }
2008
2009 /* If we are back at the starting line and there is no match then
2010 * give up. */
2011 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002012 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002013
2014 /* Skip the characters at the start of the next line that were
2015 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002016 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002017 skip = p - endp;
2018 else
2019 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002020
2021 /* Capscol skips over the inserted space. */
2022 --capcol;
2023
2024 /* But after empty line check first word in next line */
2025 if (*skipwhite(line) == NUL)
2026 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002027 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002028
2029 line_breakcheck();
2030 }
2031
Bram Moolenaar0c405862005-06-22 22:26:26 +00002032 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002033 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002034}
2035
2036/*
2037 * For spell checking: concatenate the start of the following line "line" into
2038 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2039 */
2040 void
2041spell_cat_line(buf, line, maxlen)
2042 char_u *buf;
2043 char_u *line;
2044 int maxlen;
2045{
2046 char_u *p;
2047 int n;
2048
2049 p = skipwhite(line);
2050 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2051 p = skipwhite(p + 1);
2052
2053 if (*p != NUL)
2054 {
2055 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002056 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002057 n = p - line;
2058 if (n >= maxlen)
2059 n = maxlen - 1;
2060 vim_memset(buf + 1, ' ', n);
2061 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062}
2063
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002064typedef struct spelload_S
2065{
2066 char_u sl_lang[MAXWLEN + 1]; /* language name */
2067 slang_T *sl_slang; /* resulting slang_T struct */
2068 int sl_nobreak; /* NOBREAK language found */
2069} spelload_T;
2070
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002071/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002072 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002073 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002074 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002075 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002076spell_load_lang(lang)
2077 char_u *lang;
2078{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002079 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002080 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002081 spelload_T sl;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082
Bram Moolenaarb765d632005-06-07 21:00:02 +00002083 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002084 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002085 STRCPY(sl.sl_lang, lang);
2086 sl.sl_slang = NULL;
2087 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002088
Bram Moolenaarb765d632005-06-07 21:00:02 +00002089 /*
2090 * Find the first spell file for "lang" in 'runtimepath' and load it.
2091 */
2092 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2093 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002094 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002095
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002096 if (r == FAIL && *sl.sl_lang != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002097 {
2098 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002099 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002100 "spell/%s.ascii.spl", lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002101 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002102 }
2103
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002104 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002105 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2106 lang, spell_enc(), lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002107 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002108 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002109 /* At least one file was loaded, now load all the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002110 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002111 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002112 }
2113}
2114
2115/*
2116 * Return the encoding used for spell checking: Use 'encoding', except that we
2117 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2118 */
2119 static char_u *
2120spell_enc()
2121{
2122
2123#ifdef FEAT_MBYTE
2124 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2125 return p_enc;
2126#endif
2127 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002128}
2129
2130/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002131 * Get the name of the .spl file for the internal wordlist into
2132 * "fname[MAXPATHL]".
2133 */
2134 static void
2135int_wordlist_spl(fname)
2136 char_u *fname;
2137{
2138 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2139 int_wordlist, spell_enc());
2140}
2141
2142/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002143 * Allocate a new slang_T.
2144 * Caller must fill "sl_next".
2145 */
2146 static slang_T *
2147slang_alloc(lang)
2148 char_u *lang;
2149{
2150 slang_T *lp;
2151
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002153 if (lp != NULL)
2154 {
2155 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002157 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002158 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159 }
2160 return lp;
2161}
2162
2163/*
2164 * Free the contents of an slang_T and the structure itself.
2165 */
2166 static void
2167slang_free(lp)
2168 slang_T *lp;
2169{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002170 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002171 vim_free(lp->sl_fname);
2172 slang_clear(lp);
2173 vim_free(lp);
2174}
2175
2176/*
2177 * Clear an slang_T so that the file can be reloaded.
2178 */
2179 static void
2180slang_clear(lp)
2181 slang_T *lp;
2182{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002183 garray_T *gap;
2184 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002185 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002186 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187
Bram Moolenaar51485f02005-06-04 21:55:20 +00002188 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002189 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002190 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002191 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002192 vim_free(lp->sl_pbyts);
2193 lp->sl_pbyts = NULL;
2194
Bram Moolenaar51485f02005-06-04 21:55:20 +00002195 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002196 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002197 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002198 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002199 vim_free(lp->sl_pidxs);
2200 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002202 gap = &lp->sl_rep;
2203 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002204 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002205 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2206 vim_free(ftp->ft_from);
2207 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002209 ga_clear(gap);
2210
2211 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002212 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002213 {
2214 /* "ga_len" is set to 1 without adding an item for latin1 */
2215 if (gap->ga_data != NULL)
2216 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2217 for (i = 0; i < gap->ga_len; ++i)
2218 vim_free(((int **)gap->ga_data)[i]);
2219 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002220 else
2221 /* SAL items: free salitem_T items */
2222 while (gap->ga_len > 0)
2223 {
2224 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2225 vim_free(smp->sm_lead);
2226 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2227 vim_free(smp->sm_to);
2228#ifdef FEAT_MBYTE
2229 vim_free(smp->sm_lead_w);
2230 vim_free(smp->sm_oneof_w);
2231 vim_free(smp->sm_to_w);
2232#endif
2233 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002234 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002236 for (i = 0; i < lp->sl_prefixcnt; ++i)
2237 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002238 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002239 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002240 lp->sl_prefprog = NULL;
2241
2242 vim_free(lp->sl_midword);
2243 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002244
Bram Moolenaar5195e452005-08-19 20:32:47 +00002245 vim_free(lp->sl_compprog);
2246 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002247 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002248 lp->sl_compprog = NULL;
2249 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002250 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002251
2252 vim_free(lp->sl_syllable);
2253 lp->sl_syllable = NULL;
2254 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002255
Bram Moolenaarea424162005-06-16 21:51:00 +00002256#ifdef FEAT_MBYTE
2257 {
2258 int todo = lp->sl_map_hash.ht_used;
2259 hashitem_T *hi;
2260
2261 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2262 if (!HASHITEM_EMPTY(hi))
2263 {
2264 --todo;
2265 vim_free(hi->hi_key);
2266 }
2267 }
2268 hash_clear(&lp->sl_map_hash);
2269#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002270
2271 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002272 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002273 lp->sl_compsylmax = MAXWLEN;
2274 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275}
2276
2277/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002278 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 * Invoked through do_in_runtimepath().
2280 */
2281 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002282spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002284 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002285{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002286 spelload_T *slp = (spelload_T *)cookie;
2287 slang_T *slang;
2288
2289 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2290 if (slang != NULL)
2291 {
2292 /* When a previously loaded file has NOBREAK also use it for the
2293 * ".add" files. */
2294 if (slp->sl_nobreak && slang->sl_add)
2295 slang->sl_nobreak = TRUE;
2296 else if (slang->sl_nobreak)
2297 slp->sl_nobreak = TRUE;
2298
2299 slp->sl_slang = slang;
2300 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002301}
2302
2303/*
2304 * Load one spell file and store the info into a slang_T.
2305 *
2306 * This is invoked in two ways:
2307 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2308 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2309 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2310 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002311 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002312 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002313 static slang_T *
2314spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002315 char_u *fname;
2316 char_u *lang;
2317 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002319{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002321 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002323 char_u *bp;
2324 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002326 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002327 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 int round;
2329 char_u *save_sourcing_name = sourcing_name;
2330 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002331 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002332 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002333 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335
Bram Moolenaarb765d632005-06-07 21:00:02 +00002336 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002337 if (fd == NULL)
2338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 if (!silent)
2340 EMSG2(_(e_notopen), fname);
2341 else if (p_verbose > 2)
2342 {
2343 verbose_enter();
2344 smsg((char_u *)e_notopen, fname);
2345 verbose_leave();
2346 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002347 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002348 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002349 if (p_verbose > 2)
2350 {
2351 verbose_enter();
2352 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2353 verbose_leave();
2354 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002355
Bram Moolenaarb765d632005-06-07 21:00:02 +00002356 if (old_lp == NULL)
2357 {
2358 lp = slang_alloc(lang);
2359 if (lp == NULL)
2360 goto endFAIL;
2361
2362 /* Remember the file name, used to reload the file when it's updated. */
2363 lp->sl_fname = vim_strsave(fname);
2364 if (lp->sl_fname == NULL)
2365 goto endFAIL;
2366
2367 /* Check for .add.spl. */
2368 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2369 }
2370 else
2371 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002372
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002373 /* Set sourcing_name, so that error messages mention the file name. */
2374 sourcing_name = fname;
2375 sourcing_lnum = 0;
2376
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002377 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002378 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002379 for (i = 0; i < VIMSPELLMAGICL; ++i)
2380 buf[i] = getc(fd); /* <fileID> */
2381 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2382 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002383 EMSG(_("E757: This does not look like a spell file"));
2384 goto endFAIL;
2385 }
2386 c = getc(fd); /* <versionnr> */
2387 if (c < VIMSPELLVERSION)
2388 {
2389 EMSG(_("E771: Old spell file, needs to be updated"));
2390 goto endFAIL;
2391 }
2392 else if (c > VIMSPELLVERSION)
2393 {
2394 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002395 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396 }
2397
Bram Moolenaar5195e452005-08-19 20:32:47 +00002398
2399 /*
2400 * <SECTIONS>: <section> ... <sectionend>
2401 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2402 */
2403 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002404 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002405 n = getc(fd); /* <sectionID> or <sectionend> */
2406 if (n == SN_END)
2407 break;
2408 c = getc(fd); /* <sectionflags> */
2409 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2410 /* <sectionlen> */
2411 if (len < 0)
2412 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002413
Bram Moolenaar5195e452005-08-19 20:32:47 +00002414 res = 0;
2415 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002416 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002417 case SN_REGION:
2418 res = read_region_section(fd, lp, len);
2419 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002420
Bram Moolenaar5195e452005-08-19 20:32:47 +00002421 case SN_CHARFLAGS:
2422 res = read_charflags_section(fd);
2423 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002424
Bram Moolenaar5195e452005-08-19 20:32:47 +00002425 case SN_MIDWORD:
2426 lp->sl_midword = read_string(fd, len); /* <midword> */
2427 if (lp->sl_midword == NULL)
2428 goto endFAIL;
2429 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002430
Bram Moolenaar5195e452005-08-19 20:32:47 +00002431 case SN_PREFCOND:
2432 res = read_prefcond_section(fd, lp);
2433 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002434
Bram Moolenaar5195e452005-08-19 20:32:47 +00002435 case SN_REP:
2436 res = read_rep_section(fd, lp);
2437 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002438
Bram Moolenaar5195e452005-08-19 20:32:47 +00002439 case SN_SAL:
2440 res = read_sal_section(fd, lp);
2441 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002442
Bram Moolenaar5195e452005-08-19 20:32:47 +00002443 case SN_SOFO:
2444 res = read_sofo_section(fd, lp);
2445 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002446
Bram Moolenaar5195e452005-08-19 20:32:47 +00002447 case SN_MAP:
2448 p = read_string(fd, len); /* <mapstr> */
2449 if (p == NULL)
2450 goto endFAIL;
2451 set_map_str(lp, p);
2452 vim_free(p);
2453 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002454
Bram Moolenaar5195e452005-08-19 20:32:47 +00002455 case SN_COMPOUND:
2456 res = read_compound(fd, lp, len);
2457 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002458
Bram Moolenaar78622822005-08-23 21:00:13 +00002459 case SN_NOBREAK:
2460 lp->sl_nobreak = TRUE;
2461 break;
2462
Bram Moolenaar5195e452005-08-19 20:32:47 +00002463 case SN_SYLLABLE:
2464 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2465 if (lp->sl_syllable == NULL)
2466 goto endFAIL;
2467 if (init_syl_tab(lp) == FAIL)
2468 goto endFAIL;
2469 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002470
Bram Moolenaar5195e452005-08-19 20:32:47 +00002471 default:
2472 /* Unsupported section. When it's required give an error
2473 * message. When it's not required skip the contents. */
2474 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002475 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002476 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002477 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002478 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002479 while (--len >= 0)
2480 if (getc(fd) < 0)
2481 goto truncerr;
2482 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002483 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002484 if (res == SP_FORMERROR)
2485 {
2486formerr:
2487 EMSG(_(e_format));
2488 goto endFAIL;
2489 }
2490 if (res == SP_TRUNCERROR)
2491 {
2492truncerr:
2493 EMSG(_(e_spell_trunc));
2494 goto endFAIL;
2495 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002496 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002497 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002498 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499
Bram Moolenaar51485f02005-06-04 21:55:20 +00002500 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002501 * round 2: <KWORDTREE>
2502 * round 3: <PREFIXTREE> */
2503 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002504 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002505 /* The tree size was computed when writing the file, so that we can
2506 * allocate it as one long block. <nodecount> */
2507 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2508 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002509 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002510 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002512 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002513 bp = lalloc((long_u)len, TRUE);
2514 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002515 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002517 lp->sl_fbyts = bp;
2518 else if (round == 2)
2519 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002520 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002521 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002522
2523 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002524 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2525 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002526 goto endFAIL;
2527 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002528 lp->sl_fidxs = ip;
2529 else if (round == 2)
2530 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002531 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002532 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002533
2534 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002535 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002536 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002537 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002538 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002539 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002540 }
2541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002542
Bram Moolenaarb765d632005-06-07 21:00:02 +00002543 /* For a new file link it in the list of spell files. */
2544 if (old_lp == NULL)
2545 {
2546 lp->sl_next = first_lang;
2547 first_lang = lp;
2548 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002549
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002550 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002551
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002552endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002553 if (lang != NULL)
2554 /* truncating the name signals the error to spell_load_lang() */
2555 *lang = NUL;
2556 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002557 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002558 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002559
2560endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561 if (fd != NULL)
2562 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563 sourcing_name = save_sourcing_name;
2564 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002565
2566 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567}
2568
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002569/*
2570 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002571 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002572 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002573 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2574 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002575 */
2576 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002577read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002578 FILE *fd;
2579 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002580 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002581{
2582 int cnt = 0;
2583 int i;
2584 char_u *str;
2585
2586 /* read the length bytes, MSB first */
2587 for (i = 0; i < cnt_bytes; ++i)
2588 cnt = (cnt << 8) + getc(fd);
2589 if (cnt < 0)
2590 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002591 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002592 return NULL;
2593 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002594 *cntp = cnt;
2595 if (cnt == 0)
2596 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002597
Bram Moolenaar5195e452005-08-19 20:32:47 +00002598 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002599 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002600 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002601 return str;
2602}
2603
Bram Moolenaar7887d882005-07-01 22:33:52 +00002604/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002605 * Read a string of length "cnt" from "fd" into allocated memory.
2606 * Returns NULL when out of memory.
2607 */
2608 static char_u *
2609read_string(fd, cnt)
2610 FILE *fd;
2611 int cnt;
2612{
2613 char_u *str;
2614 int i;
2615
2616 /* allocate memory */
2617 str = alloc((unsigned)cnt + 1);
2618 if (str != NULL)
2619 {
2620 /* Read the string. Doesn't check for truncated file. */
2621 for (i = 0; i < cnt; ++i)
2622 str[i] = getc(fd);
2623 str[i] = NUL;
2624 }
2625 return str;
2626}
2627
2628/*
2629 * Read SN_REGION: <regionname> ...
2630 * Return SP_*ERROR flags.
2631 */
2632 static int
2633read_region_section(fd, lp, len)
2634 FILE *fd;
2635 slang_T *lp;
2636 int len;
2637{
2638 int i;
2639
2640 if (len > 16)
2641 return SP_FORMERROR;
2642 for (i = 0; i < len; ++i)
2643 lp->sl_regions[i] = getc(fd); /* <regionname> */
2644 lp->sl_regions[len] = NUL;
2645 return 0;
2646}
2647
2648/*
2649 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2650 * <folcharslen> <folchars>
2651 * Return SP_*ERROR flags.
2652 */
2653 static int
2654read_charflags_section(fd)
2655 FILE *fd;
2656{
2657 char_u *flags;
2658 char_u *fol;
2659 int flagslen, follen;
2660
2661 /* <charflagslen> <charflags> */
2662 flags = read_cnt_string(fd, 1, &flagslen);
2663 if (flagslen < 0)
2664 return flagslen;
2665
2666 /* <folcharslen> <folchars> */
2667 fol = read_cnt_string(fd, 2, &follen);
2668 if (follen < 0)
2669 {
2670 vim_free(flags);
2671 return follen;
2672 }
2673
2674 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2675 if (flags != NULL && fol != NULL)
2676 set_spell_charflags(flags, flagslen, fol);
2677
2678 vim_free(flags);
2679 vim_free(fol);
2680
2681 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2682 if ((flags == NULL) != (fol == NULL))
2683 return SP_FORMERROR;
2684 return 0;
2685}
2686
2687/*
2688 * Read SN_PREFCOND section.
2689 * Return SP_*ERROR flags.
2690 */
2691 static int
2692read_prefcond_section(fd, lp)
2693 FILE *fd;
2694 slang_T *lp;
2695{
2696 int cnt;
2697 int i;
2698 int n;
2699 char_u *p;
2700 char_u buf[MAXWLEN + 1];
2701
2702 /* <prefcondcnt> <prefcond> ... */
2703 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2704 if (cnt <= 0)
2705 return SP_FORMERROR;
2706
2707 lp->sl_prefprog = (regprog_T **)alloc_clear(
2708 (unsigned)sizeof(regprog_T *) * cnt);
2709 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002710 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002711 lp->sl_prefixcnt = cnt;
2712
2713 for (i = 0; i < cnt; ++i)
2714 {
2715 /* <prefcond> : <condlen> <condstr> */
2716 n = getc(fd); /* <condlen> */
2717 if (n < 0 || n >= MAXWLEN)
2718 return SP_FORMERROR;
2719
2720 /* When <condlen> is zero we have an empty condition. Otherwise
2721 * compile the regexp program used to check for the condition. */
2722 if (n > 0)
2723 {
2724 buf[0] = '^'; /* always match at one position only */
2725 p = buf + 1;
2726 while (n-- > 0)
2727 *p++ = getc(fd); /* <condstr> */
2728 *p = NUL;
2729 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2730 }
2731 }
2732 return 0;
2733}
2734
2735/*
2736 * Read REP items section from "fd": <repcount> <rep> ...
2737 * Return SP_*ERROR flags.
2738 */
2739 static int
2740read_rep_section(fd, slang)
2741 FILE *fd;
2742 slang_T *slang;
2743{
2744 int cnt;
2745 garray_T *gap;
2746 fromto_T *ftp;
2747 short *first;
2748 int i;
2749
2750 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2751 if (cnt < 0)
2752 return SP_TRUNCERROR;
2753
2754 gap = &slang->sl_rep;
2755 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002756 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002757
2758 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2759 for (; gap->ga_len < cnt; ++gap->ga_len)
2760 {
2761 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2762 ftp->ft_from = read_cnt_string(fd, 1, &i);
2763 if (i < 0)
2764 return i;
2765 if (i == 0)
2766 return SP_FORMERROR;
2767 ftp->ft_to = read_cnt_string(fd, 1, &i);
2768 if (i <= 0)
2769 {
2770 vim_free(ftp->ft_from);
2771 if (i < 0)
2772 return i;
2773 return SP_FORMERROR;
2774 }
2775 }
2776
2777 /* Fill the first-index table. */
2778 first = slang->sl_rep_first;
2779 for (i = 0; i < 256; ++i)
2780 first[i] = -1;
2781 for (i = 0; i < gap->ga_len; ++i)
2782 {
2783 ftp = &((fromto_T *)gap->ga_data)[i];
2784 if (first[*ftp->ft_from] == -1)
2785 first[*ftp->ft_from] = i;
2786 }
2787 return 0;
2788}
2789
2790/*
2791 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2792 * Return SP_*ERROR flags.
2793 */
2794 static int
2795read_sal_section(fd, slang)
2796 FILE *fd;
2797 slang_T *slang;
2798{
2799 int i;
2800 int cnt;
2801 garray_T *gap;
2802 salitem_T *smp;
2803 int ccnt;
2804 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002805 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002806
2807 slang->sl_sofo = FALSE;
2808
2809 i = getc(fd); /* <salflags> */
2810 if (i & SAL_F0LLOWUP)
2811 slang->sl_followup = TRUE;
2812 if (i & SAL_COLLAPSE)
2813 slang->sl_collapse = TRUE;
2814 if (i & SAL_REM_ACCENTS)
2815 slang->sl_rem_accents = TRUE;
2816
2817 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2818 if (cnt < 0)
2819 return SP_TRUNCERROR;
2820
2821 gap = &slang->sl_sal;
2822 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002823 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002824 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002825
2826 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2827 for (; gap->ga_len < cnt; ++gap->ga_len)
2828 {
2829 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2830 ccnt = getc(fd); /* <salfromlen> */
2831 if (ccnt < 0)
2832 return SP_TRUNCERROR;
2833 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002834 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002835 smp->sm_lead = p;
2836
2837 /* Read up to the first special char into sm_lead. */
2838 for (i = 0; i < ccnt; ++i)
2839 {
2840 c = getc(fd); /* <salfrom> */
2841 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2842 break;
2843 *p++ = c;
2844 }
2845 smp->sm_leadlen = p - smp->sm_lead;
2846 *p++ = NUL;
2847
2848 /* Put (abc) chars in sm_oneof, if any. */
2849 if (c == '(')
2850 {
2851 smp->sm_oneof = p;
2852 for (++i; i < ccnt; ++i)
2853 {
2854 c = getc(fd); /* <salfrom> */
2855 if (c == ')')
2856 break;
2857 *p++ = c;
2858 }
2859 *p++ = NUL;
2860 if (++i < ccnt)
2861 c = getc(fd);
2862 }
2863 else
2864 smp->sm_oneof = NULL;
2865
2866 /* Any following chars go in sm_rules. */
2867 smp->sm_rules = p;
2868 if (i < ccnt)
2869 /* store the char we got while checking for end of sm_lead */
2870 *p++ = c;
2871 for (++i; i < ccnt; ++i)
2872 *p++ = getc(fd); /* <salfrom> */
2873 *p++ = NUL;
2874
2875 /* <saltolen> <salto> */
2876 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2877 if (ccnt < 0)
2878 {
2879 vim_free(smp->sm_lead);
2880 return ccnt;
2881 }
2882
2883#ifdef FEAT_MBYTE
2884 if (has_mbyte)
2885 {
2886 /* convert the multi-byte strings to wide char strings */
2887 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2888 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2889 if (smp->sm_oneof == NULL)
2890 smp->sm_oneof_w = NULL;
2891 else
2892 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2893 if (smp->sm_to == NULL)
2894 smp->sm_to_w = NULL;
2895 else
2896 smp->sm_to_w = mb_str2wide(smp->sm_to);
2897 if (smp->sm_lead_w == NULL
2898 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2899 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2900 {
2901 vim_free(smp->sm_lead);
2902 vim_free(smp->sm_to);
2903 vim_free(smp->sm_lead_w);
2904 vim_free(smp->sm_oneof_w);
2905 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002906 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002907 }
2908 }
2909#endif
2910 }
2911
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002912 if (gap->ga_len > 0)
2913 {
2914 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
2915 * that we need to check the index every time. */
2916 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2917 if ((p = alloc(1)) == NULL)
2918 return SP_OTHERERROR;
2919 p[0] = NUL;
2920 smp->sm_lead = p;
2921 smp->sm_leadlen = 0;
2922 smp->sm_oneof = NULL;
2923 smp->sm_rules = p;
2924 smp->sm_to = NULL;
2925#ifdef FEAT_MBYTE
2926 if (has_mbyte)
2927 {
2928 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2929 smp->sm_leadlen = 0;
2930 smp->sm_oneof_w = NULL;
2931 smp->sm_to_w = NULL;
2932 }
2933#endif
2934 ++gap->ga_len;
2935 }
2936
Bram Moolenaar5195e452005-08-19 20:32:47 +00002937 /* Fill the first-index table. */
2938 set_sal_first(slang);
2939
2940 return 0;
2941}
2942
2943/*
2944 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2945 * Return SP_*ERROR flags.
2946 */
2947 static int
2948read_sofo_section(fd, slang)
2949 FILE *fd;
2950 slang_T *slang;
2951{
2952 int cnt;
2953 char_u *from, *to;
2954 int res;
2955
2956 slang->sl_sofo = TRUE;
2957
2958 /* <sofofromlen> <sofofrom> */
2959 from = read_cnt_string(fd, 2, &cnt);
2960 if (cnt < 0)
2961 return cnt;
2962
2963 /* <sofotolen> <sofoto> */
2964 to = read_cnt_string(fd, 2, &cnt);
2965 if (cnt < 0)
2966 {
2967 vim_free(from);
2968 return cnt;
2969 }
2970
2971 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2972 if (from != NULL && to != NULL)
2973 res = set_sofo(slang, from, to);
2974 else if (from != NULL || to != NULL)
2975 res = SP_FORMERROR; /* only one of two strings is an error */
2976 else
2977 res = 0;
2978
2979 vim_free(from);
2980 vim_free(to);
2981 return res;
2982}
2983
2984/*
2985 * Read the compound section from the .spl file:
2986 * <compmax> <compminlen> <compsylmax> <compflags>
2987 * Returns SP_*ERROR flags.
2988 */
2989 static int
2990read_compound(fd, slang, len)
2991 FILE *fd;
2992 slang_T *slang;
2993 int len;
2994{
2995 int todo = len;
2996 int c;
2997 int atstart;
2998 char_u *pat;
2999 char_u *pp;
3000 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003001 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003002
3003 if (todo < 2)
3004 return SP_FORMERROR; /* need at least two bytes */
3005
3006 --todo;
3007 c = getc(fd); /* <compmax> */
3008 if (c < 2)
3009 c = MAXWLEN;
3010 slang->sl_compmax = c;
3011
3012 --todo;
3013 c = getc(fd); /* <compminlen> */
3014 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003015 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003016 slang->sl_compminlen = c;
3017
3018 --todo;
3019 c = getc(fd); /* <compsylmax> */
3020 if (c < 1)
3021 c = MAXWLEN;
3022 slang->sl_compsylmax = c;
3023
3024 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3025 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003026 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3027 * Conversion to utf-8 may double the size. */
3028 c = todo * 2 + 7;
3029#ifdef FEAT_MBYTE
3030 if (enc_utf8)
3031 c += todo * 2;
3032#endif
3033 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003034 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003035 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003036
Bram Moolenaard12a1322005-08-21 22:08:24 +00003037 /* We also need a list of all flags that can appear at the start and one
3038 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003039 cp = alloc(todo + 1);
3040 if (cp == NULL)
3041 {
3042 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003043 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003044 }
3045 slang->sl_compstartflags = cp;
3046 *cp = NUL;
3047
Bram Moolenaard12a1322005-08-21 22:08:24 +00003048 ap = alloc(todo + 1);
3049 if (ap == NULL)
3050 {
3051 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003052 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003053 }
3054 slang->sl_compallflags = ap;
3055 *ap = NUL;
3056
Bram Moolenaar5195e452005-08-19 20:32:47 +00003057 pp = pat;
3058 *pp++ = '^';
3059 *pp++ = '\\';
3060 *pp++ = '(';
3061
3062 atstart = 1;
3063 while (todo-- > 0)
3064 {
3065 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003066
3067 /* Add all flags to "sl_compallflags". */
3068 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003069 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003070 {
3071 *ap++ = c;
3072 *ap = NUL;
3073 }
3074
Bram Moolenaar5195e452005-08-19 20:32:47 +00003075 if (atstart != 0)
3076 {
3077 /* At start of item: copy flags to "sl_compstartflags". For a
3078 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3079 if (c == '[')
3080 atstart = 2;
3081 else if (c == ']')
3082 atstart = 0;
3083 else
3084 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003085 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003086 {
3087 *cp++ = c;
3088 *cp = NUL;
3089 }
3090 if (atstart == 1)
3091 atstart = 0;
3092 }
3093 }
3094 if (c == '/') /* slash separates two items */
3095 {
3096 *pp++ = '\\';
3097 *pp++ = '|';
3098 atstart = 1;
3099 }
3100 else /* normal char, "[abc]" and '*' are copied as-is */
3101 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003102 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003103 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003104#ifdef FEAT_MBYTE
3105 if (enc_utf8)
3106 pp += mb_char2bytes(c, pp);
3107 else
3108#endif
3109 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003110 }
3111 }
3112
3113 *pp++ = '\\';
3114 *pp++ = ')';
3115 *pp++ = '$';
3116 *pp = NUL;
3117
3118 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3119 vim_free(pat);
3120 if (slang->sl_compprog == NULL)
3121 return SP_FORMERROR;
3122
3123 return 0;
3124}
3125
Bram Moolenaar6de68532005-08-24 22:08:48 +00003126/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003127 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003128 * Like strchr() but independent of locale.
3129 */
3130 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003131byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003132 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003133 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003134{
3135 char_u *p;
3136
3137 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003138 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003139 return TRUE;
3140 return FALSE;
3141}
3142
Bram Moolenaar5195e452005-08-19 20:32:47 +00003143#define SY_MAXLEN 30
3144typedef struct syl_item_S
3145{
3146 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3147 int sy_len;
3148} syl_item_T;
3149
3150/*
3151 * Truncate "slang->sl_syllable" at the first slash and put the following items
3152 * in "slang->sl_syl_items".
3153 */
3154 static int
3155init_syl_tab(slang)
3156 slang_T *slang;
3157{
3158 char_u *p;
3159 char_u *s;
3160 int l;
3161 syl_item_T *syl;
3162
3163 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3164 p = vim_strchr(slang->sl_syllable, '/');
3165 while (p != NULL)
3166 {
3167 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003168 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003169 break;
3170 s = p;
3171 p = vim_strchr(p, '/');
3172 if (p == NULL)
3173 l = STRLEN(s);
3174 else
3175 l = p - s;
3176 if (l >= SY_MAXLEN)
3177 return SP_FORMERROR;
3178 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003179 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003180 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3181 + slang->sl_syl_items.ga_len++;
3182 vim_strncpy(syl->sy_chars, s, l);
3183 syl->sy_len = l;
3184 }
3185 return OK;
3186}
3187
3188/*
3189 * Count the number of syllables in "word".
3190 * When "word" contains spaces the syllables after the last space are counted.
3191 * Returns zero if syllables are not defines.
3192 */
3193 static int
3194count_syllables(slang, word)
3195 slang_T *slang;
3196 char_u *word;
3197{
3198 int cnt = 0;
3199 int skip = FALSE;
3200 char_u *p;
3201 int len;
3202 int i;
3203 syl_item_T *syl;
3204 int c;
3205
3206 if (slang->sl_syllable == NULL)
3207 return 0;
3208
3209 for (p = word; *p != NUL; p += len)
3210 {
3211 /* When running into a space reset counter. */
3212 if (*p == ' ')
3213 {
3214 len = 1;
3215 cnt = 0;
3216 continue;
3217 }
3218
3219 /* Find longest match of syllable items. */
3220 len = 0;
3221 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3222 {
3223 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3224 if (syl->sy_len > len
3225 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3226 len = syl->sy_len;
3227 }
3228 if (len != 0) /* found a match, count syllable */
3229 {
3230 ++cnt;
3231 skip = FALSE;
3232 }
3233 else
3234 {
3235 /* No recognized syllable item, at least a syllable char then? */
3236#ifdef FEAT_MBYTE
3237 c = mb_ptr2char(p);
3238 len = (*mb_ptr2len)(p);
3239#else
3240 c = *p;
3241 len = 1;
3242#endif
3243 if (vim_strchr(slang->sl_syllable, c) == NULL)
3244 skip = FALSE; /* No, search for next syllable */
3245 else if (!skip)
3246 {
3247 ++cnt; /* Yes, count it */
3248 skip = TRUE; /* don't count following syllable chars */
3249 }
3250 }
3251 }
3252 return cnt;
3253}
3254
3255/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003256 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003257 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003258 */
3259 static int
3260set_sofo(lp, from, to)
3261 slang_T *lp;
3262 char_u *from;
3263 char_u *to;
3264{
3265 int i;
3266
3267#ifdef FEAT_MBYTE
3268 garray_T *gap;
3269 char_u *s;
3270 char_u *p;
3271 int c;
3272 int *inp;
3273
3274 if (has_mbyte)
3275 {
3276 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3277 * characters. The index is the low byte of the character.
3278 * The list contains from-to pairs with a terminating NUL.
3279 * sl_sal_first[] is used for latin1 "from" characters. */
3280 gap = &lp->sl_sal;
3281 ga_init2(gap, sizeof(int *), 1);
3282 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003283 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003284 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3285 gap->ga_len = 256;
3286
3287 /* First count the number of items for each list. Temporarily use
3288 * sl_sal_first[] for this. */
3289 for (p = from, s = to; *p != NUL && *s != NUL; )
3290 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003291 c = mb_cptr2char_adv(&p);
3292 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003293 if (c >= 256)
3294 ++lp->sl_sal_first[c & 0xff];
3295 }
3296 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003297 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003298
3299 /* Allocate the lists. */
3300 for (i = 0; i < 256; ++i)
3301 if (lp->sl_sal_first[i] > 0)
3302 {
3303 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3304 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003305 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003306 ((int **)gap->ga_data)[i] = (int *)p;
3307 *(int *)p = 0;
3308 }
3309
3310 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3311 * list. */
3312 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3313 for (p = from, s = to; *p != NUL && *s != NUL; )
3314 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003315 c = mb_cptr2char_adv(&p);
3316 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003317 if (c >= 256)
3318 {
3319 /* Append the from-to chars at the end of the list with
3320 * the low byte. */
3321 inp = ((int **)gap->ga_data)[c & 0xff];
3322 while (*inp != 0)
3323 ++inp;
3324 *inp++ = c; /* from char */
3325 *inp++ = i; /* to char */
3326 *inp++ = NUL; /* NUL at the end */
3327 }
3328 else
3329 /* mapping byte to char is done in sl_sal_first[] */
3330 lp->sl_sal_first[c] = i;
3331 }
3332 }
3333 else
3334#endif
3335 {
3336 /* mapping bytes to bytes is done in sl_sal_first[] */
3337 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003338 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003339
3340 for (i = 0; to[i] != NUL; ++i)
3341 lp->sl_sal_first[from[i]] = to[i];
3342 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3343 }
3344
Bram Moolenaar5195e452005-08-19 20:32:47 +00003345 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003346}
3347
3348/*
3349 * Fill the first-index table for "lp".
3350 */
3351 static void
3352set_sal_first(lp)
3353 slang_T *lp;
3354{
3355 salfirst_T *sfirst;
3356 int i;
3357 salitem_T *smp;
3358 int c;
3359 garray_T *gap = &lp->sl_sal;
3360
3361 sfirst = lp->sl_sal_first;
3362 for (i = 0; i < 256; ++i)
3363 sfirst[i] = -1;
3364 smp = (salitem_T *)gap->ga_data;
3365 for (i = 0; i < gap->ga_len; ++i)
3366 {
3367#ifdef FEAT_MBYTE
3368 if (has_mbyte)
3369 /* Use the lowest byte of the first character. For latin1 it's
3370 * the character, for other encodings it should differ for most
3371 * characters. */
3372 c = *smp[i].sm_lead_w & 0xff;
3373 else
3374#endif
3375 c = *smp[i].sm_lead;
3376 if (sfirst[c] == -1)
3377 {
3378 sfirst[c] = i;
3379#ifdef FEAT_MBYTE
3380 if (has_mbyte)
3381 {
3382 int n;
3383
3384 /* Make sure all entries with this byte are following each
3385 * other. Move the ones that are in the wrong position. Do
3386 * keep the same ordering! */
3387 while (i + 1 < gap->ga_len
3388 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3389 /* Skip over entry with same index byte. */
3390 ++i;
3391
3392 for (n = 1; i + n < gap->ga_len; ++n)
3393 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3394 {
3395 salitem_T tsal;
3396
3397 /* Move entry with same index byte after the entries
3398 * we already found. */
3399 ++i;
3400 --n;
3401 tsal = smp[i + n];
3402 mch_memmove(smp + i + 1, smp + i,
3403 sizeof(salitem_T) * n);
3404 smp[i] = tsal;
3405 }
3406 }
3407#endif
3408 }
3409 }
3410}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003411
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003412#ifdef FEAT_MBYTE
3413/*
3414 * Turn a multi-byte string into a wide character string.
3415 * Return it in allocated memory (NULL for out-of-memory)
3416 */
3417 static int *
3418mb_str2wide(s)
3419 char_u *s;
3420{
3421 int *res;
3422 char_u *p;
3423 int i = 0;
3424
3425 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3426 if (res != NULL)
3427 {
3428 for (p = s; *p != NUL; )
3429 res[i++] = mb_ptr2char_adv(&p);
3430 res[i] = NUL;
3431 }
3432 return res;
3433}
3434#endif
3435
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003436/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003437 * Read one row of siblings from the spell file and store it in the byte array
3438 * "byts" and index array "idxs". Recursively read the children.
3439 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003440 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003441 *
3442 * Returns the index follosing the siblings.
3443 * Returns -1 if the file is shorter than expected.
3444 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003445 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003446 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003447read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003448 FILE *fd;
3449 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003450 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003451 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003452 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003453 int prefixtree; /* TRUE for reading PREFIXTREE */
3454 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003455{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003456 int len;
3457 int i;
3458 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003459 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003460 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003461 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003462#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003463
Bram Moolenaar51485f02005-06-04 21:55:20 +00003464 len = getc(fd); /* <siblingcount> */
3465 if (len <= 0)
3466 return -1;
3467
3468 if (startidx + len >= maxidx)
3469 return -2;
3470 byts[idx++] = len;
3471
3472 /* Read the byte values, flag/region bytes and shared indexes. */
3473 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003474 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003475 c = getc(fd); /* <byte> */
3476 if (c < 0)
3477 return -1;
3478 if (c <= BY_SPECIAL)
3479 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003480 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003481 {
3482 /* No flags, all regions. */
3483 idxs[idx] = 0;
3484 c = 0;
3485 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003486 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003487 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003488 if (prefixtree)
3489 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003490 /* Read the optional pflags byte, the prefix ID and the
3491 * condition nr. In idxs[] store the prefix ID in the low
3492 * byte, the condition index shifted up 8 bits, the flags
3493 * shifted up 24 bits. */
3494 if (c == BY_FLAGS)
3495 c = getc(fd) << 24; /* <pflags> */
3496 else
3497 c = 0;
3498
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003499 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003500
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003501 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3502 if (n >= maxprefcondnr)
3503 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003504 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003505 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003506 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003507 {
3508 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003509 * idxs[] the flags go in the low two bytes, region above
3510 * that and prefix ID above the region. */
3511 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003512 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003513 if (c2 == BY_FLAGS2)
3514 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003515 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003516 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003517 if (c & WF_AFX)
3518 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003519 }
3520
Bram Moolenaar51485f02005-06-04 21:55:20 +00003521 idxs[idx] = c;
3522 c = 0;
3523 }
3524 else /* c == BY_INDEX */
3525 {
3526 /* <nodeidx> */
3527 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3528 if (n < 0 || n >= maxidx)
3529 return -2;
3530 idxs[idx] = n + SHARED_MASK;
3531 c = getc(fd); /* <xbyte> */
3532 }
3533 }
3534 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003535 }
3536
Bram Moolenaar51485f02005-06-04 21:55:20 +00003537 /* Recursively read the children for non-shared siblings.
3538 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3539 * remove SHARED_MASK) */
3540 for (i = 1; i <= len; ++i)
3541 if (byts[startidx + i] != 0)
3542 {
3543 if (idxs[startidx + i] & SHARED_MASK)
3544 idxs[startidx + i] &= ~SHARED_MASK;
3545 else
3546 {
3547 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003548 idx = read_tree(fd, byts, idxs, maxidx, idx,
3549 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003550 if (idx < 0)
3551 break;
3552 }
3553 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003554
Bram Moolenaar51485f02005-06-04 21:55:20 +00003555 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003556}
3557
3558/*
3559 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003560 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 */
3562 char_u *
3563did_set_spelllang(buf)
3564 buf_T *buf;
3565{
3566 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003567 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003569 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003570 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003571 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003572 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003574 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003576 int len;
3577 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003578 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003579 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003580 char_u *use_region = NULL;
3581 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003582 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003583 int i, j;
3584 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003585
3586 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003587 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003588
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003589 /* loop over comma separated language names. */
3590 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003591 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003592 /* Get one language name. */
3593 copy_option_part(&splp, lang, MAXWLEN, ",");
3594
Bram Moolenaar5482f332005-04-17 20:18:43 +00003595 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003596 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003597
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003598 /* If the name ends in ".spl" use it as the name of the spell file.
3599 * If there is a region name let "region" point to it and remove it
3600 * from the name. */
3601 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3602 {
3603 filename = TRUE;
3604
Bram Moolenaarb6356332005-07-18 21:40:44 +00003605 /* Locate a region and remove it from the file name. */
3606 p = vim_strchr(gettail(lang), '_');
3607 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3608 && !ASCII_ISALPHA(p[3]))
3609 {
3610 vim_strncpy(region_cp, p + 1, 2);
3611 mch_memmove(p, p + 3, len - (p - lang) - 2);
3612 len -= 3;
3613 region = region_cp;
3614 }
3615 else
3616 dont_use_region = TRUE;
3617
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003618 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003619 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3620 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003621 break;
3622 }
3623 else
3624 {
3625 filename = FALSE;
3626 if (len > 3 && lang[len - 3] == '_')
3627 {
3628 region = lang + len - 2;
3629 len -= 3;
3630 lang[len] = NUL;
3631 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003632 else
3633 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003634
3635 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003636 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3637 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003638 break;
3639 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003640
Bram Moolenaarb6356332005-07-18 21:40:44 +00003641 if (region != NULL)
3642 {
3643 /* If the region differs from what was used before then don't
3644 * use it for 'spellfile'. */
3645 if (use_region != NULL && STRCMP(region, use_region) != 0)
3646 dont_use_region = TRUE;
3647 use_region = region;
3648 }
3649
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003650 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003651 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003652 {
3653 if (filename)
3654 (void)spell_load_file(lang, lang, NULL, FALSE);
3655 else
3656 spell_load_lang(lang);
3657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003658
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003659 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003660 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003661 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003662 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3663 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
3664 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003665 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003666 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003667 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003668 {
3669 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003670 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003671 if (c == REGION_ALL)
3672 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003673 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003674 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003675 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003676 /* This addition file is for other regions. */
3677 region_mask = 0;
3678 }
3679 else
3680 /* This is probably an error. Give a warning and
3681 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003682 smsg((char_u *)
3683 _("Warning: region %s not supported"),
3684 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003685 }
3686 else
3687 region_mask = 1 << c;
3688 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003689
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003690 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003691 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003692 if (ga_grow(&ga, 1) == FAIL)
3693 {
3694 ga_clear(&ga);
3695 return e_outofmem;
3696 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003697 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003698 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3699 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003700 use_midword(slang, buf);
3701 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003702 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003703 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003704 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003705 }
3706
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003707 /* round 0: load int_wordlist, if possible.
3708 * round 1: load first name in 'spellfile'.
3709 * round 2: load second name in 'spellfile.
3710 * etc. */
3711 spf = curbuf->b_p_spf;
3712 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003713 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003714 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003715 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003716 /* Internal wordlist, if there is one. */
3717 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003718 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003719 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003720 }
3721 else
3722 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003723 /* One entry in 'spellfile'. */
3724 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3725 STRCAT(spf_name, ".spl");
3726
3727 /* If it was already found above then skip it. */
3728 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003729 {
3730 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
3731 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003732 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003733 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003734 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003735 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003736 }
3737
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003738 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003739 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3740 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003742 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003744 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003745 * region name, the region is ignored otherwise. for int_wordlist
3746 * use an arbitrary name. */
3747 if (round == 0)
3748 STRCPY(lang, "internal wordlist");
3749 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003750 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003751 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003752 p = vim_strchr(lang, '.');
3753 if (p != NULL)
3754 *p = NUL; /* truncate at ".encoding.add" */
3755 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003756 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003757
3758 /* If one of the languages has NOBREAK we assume the addition
3759 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003760 if (slang != NULL && nobreak)
3761 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003763 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003765 region_mask = REGION_ALL;
3766 if (use_region != NULL && !dont_use_region)
3767 {
3768 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003769 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003770 if (c != REGION_ALL)
3771 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003772 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003773 /* This spell file is for other regions. */
3774 region_mask = 0;
3775 }
3776
3777 if (region_mask != 0)
3778 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003779 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
3780 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
3781 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003782 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3783 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003784 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 }
3787 }
3788
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789 /* Everything is fine, store the new b_langp value. */
3790 ga_clear(&buf->b_langp);
3791 buf->b_langp = ga;
3792
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003793 /* For each language figure out what language to use for sound folding and
3794 * REP items. If the language doesn't support it itself use another one
3795 * with the same name. E.g. for "en-math" use "en". */
3796 for (i = 0; i < ga.ga_len; ++i)
3797 {
3798 lp = LANGP_ENTRY(ga, i);
3799
3800 /* sound folding */
3801 if (lp->lp_slang->sl_sal.ga_len > 0)
3802 /* language does sound folding itself */
3803 lp->lp_sallang = lp->lp_slang;
3804 else
3805 /* find first similar language that does sound folding */
3806 for (j = 0; j < ga.ga_len; ++j)
3807 {
3808 lp2 = LANGP_ENTRY(ga, j);
3809 if (lp2->lp_slang->sl_sal.ga_len > 0
3810 && STRNCMP(lp->lp_slang->sl_name,
3811 lp2->lp_slang->sl_name, 2) == 0)
3812 {
3813 lp->lp_sallang = lp2->lp_slang;
3814 break;
3815 }
3816 }
3817
3818 /* REP items */
3819 if (lp->lp_slang->sl_rep.ga_len > 0)
3820 /* language has REP items itself */
3821 lp->lp_replang = lp->lp_slang;
3822 else
3823 /* find first similar language that does sound folding */
3824 for (j = 0; j < ga.ga_len; ++j)
3825 {
3826 lp2 = LANGP_ENTRY(ga, j);
3827 if (lp2->lp_slang->sl_rep.ga_len > 0
3828 && STRNCMP(lp->lp_slang->sl_name,
3829 lp2->lp_slang->sl_name, 2) == 0)
3830 {
3831 lp->lp_replang = lp2->lp_slang;
3832 break;
3833 }
3834 }
3835 }
3836
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003837 return NULL;
3838}
3839
3840/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003841 * Clear the midword characters for buffer "buf".
3842 */
3843 static void
3844clear_midword(buf)
3845 buf_T *buf;
3846{
3847 vim_memset(buf->b_spell_ismw, 0, 256);
3848#ifdef FEAT_MBYTE
3849 vim_free(buf->b_spell_ismw_mb);
3850 buf->b_spell_ismw_mb = NULL;
3851#endif
3852}
3853
3854/*
3855 * Use the "sl_midword" field of language "lp" for buffer "buf".
3856 * They add up to any currently used midword characters.
3857 */
3858 static void
3859use_midword(lp, buf)
3860 slang_T *lp;
3861 buf_T *buf;
3862{
3863 char_u *p;
3864
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003865 if (lp->sl_midword == NULL) /* there aren't any */
3866 return;
3867
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003868 for (p = lp->sl_midword; *p != NUL; )
3869#ifdef FEAT_MBYTE
3870 if (has_mbyte)
3871 {
3872 int c, l, n;
3873 char_u *bp;
3874
3875 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003876 l = (*mb_ptr2len)(p);
3877 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003878 buf->b_spell_ismw[c] = TRUE;
3879 else if (buf->b_spell_ismw_mb == NULL)
3880 /* First multi-byte char in "b_spell_ismw_mb". */
3881 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3882 else
3883 {
3884 /* Append multi-byte chars to "b_spell_ismw_mb". */
3885 n = STRLEN(buf->b_spell_ismw_mb);
3886 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3887 if (bp != NULL)
3888 {
3889 vim_free(buf->b_spell_ismw_mb);
3890 buf->b_spell_ismw_mb = bp;
3891 vim_strncpy(bp + n, p, l);
3892 }
3893 }
3894 p += l;
3895 }
3896 else
3897#endif
3898 buf->b_spell_ismw[*p++] = TRUE;
3899}
3900
3901/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003902 * Find the region "region[2]" in "rp" (points to "sl_regions").
3903 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003904 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003905 */
3906 static int
3907find_region(rp, region)
3908 char_u *rp;
3909 char_u *region;
3910{
3911 int i;
3912
3913 for (i = 0; ; i += 2)
3914 {
3915 if (rp[i] == NUL)
3916 return REGION_ALL;
3917 if (rp[i] == region[0] && rp[i + 1] == region[1])
3918 break;
3919 }
3920 return i / 2;
3921}
3922
3923/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003925 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003926 * Word WF_ONECAP
3927 * W WORD WF_ALLCAP
3928 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 */
3930 static int
3931captype(word, end)
3932 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003934{
3935 char_u *p;
3936 int c;
3937 int firstcap;
3938 int allcap;
3939 int past_second = FALSE; /* past second word char */
3940
3941 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003942 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003944 return 0; /* only non-word characters, illegal word */
3945#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003946 if (has_mbyte)
3947 c = mb_ptr2char_adv(&p);
3948 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003949#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003950 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003951 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952
3953 /*
3954 * Need to check all letters to find a word with mixed upper/lower.
3955 * But a word with an upper char only at start is a ONECAP.
3956 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003957 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003958 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003959 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003960 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003961 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003962 {
3963 /* UUl -> KEEPCAP */
3964 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003965 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003966 allcap = FALSE;
3967 }
3968 else if (!allcap)
3969 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003970 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971 past_second = TRUE;
3972 }
3973
3974 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003975 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003977 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003978 return 0;
3979}
3980
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003981/*
3982 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3983 * capital. So that make_case_word() can turn WOrd into Word.
3984 * Add ALLCAP for "WOrD".
3985 */
3986 static int
3987badword_captype(word, end)
3988 char_u *word;
3989 char_u *end;
3990{
3991 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003992 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003993 int l, u;
3994 int first;
3995 char_u *p;
3996
3997 if (flags & WF_KEEPCAP)
3998 {
3999 /* Count the number of UPPER and lower case letters. */
4000 l = u = 0;
4001 first = FALSE;
4002 for (p = word; p < end; mb_ptr_adv(p))
4003 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004004 c = PTR2CHAR(p);
4005 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004006 {
4007 ++u;
4008 if (p == word)
4009 first = TRUE;
4010 }
4011 else
4012 ++l;
4013 }
4014
4015 /* If there are more UPPER than lower case letters suggest an
4016 * ALLCAP word. Otherwise, if the first letter is UPPER then
4017 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4018 * require three upper case letters. */
4019 if (u > l && u > 2)
4020 flags |= WF_ALLCAP;
4021 else if (first)
4022 flags |= WF_ONECAP;
4023 }
4024 return flags;
4025}
4026
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004027# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4028/*
4029 * Free all languages.
4030 */
4031 void
4032spell_free_all()
4033{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004034 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004035 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004036 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004037
4038 /* Go through all buffers and handle 'spelllang'. */
4039 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4040 ga_clear(&buf->b_langp);
4041
4042 while (first_lang != NULL)
4043 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004044 slang = first_lang;
4045 first_lang = slang->sl_next;
4046 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004047 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004048
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004049 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004050 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004051 /* Delete the internal wordlist and its .spl file */
4052 mch_remove(int_wordlist);
4053 int_wordlist_spl(fname);
4054 mch_remove(fname);
4055 vim_free(int_wordlist);
4056 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004057 }
4058
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004059 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004060}
4061# endif
4062
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063# if defined(FEAT_MBYTE) || defined(PROTO)
4064/*
4065 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004066 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004067 */
4068 void
4069spell_reload()
4070{
4071 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004072 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004073
Bram Moolenaarea408852005-06-25 22:49:46 +00004074 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 init_spell_chartab();
4076
4077 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004078 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079
4080 /* Go through all buffers and handle 'spelllang'. */
4081 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4082 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004083 /* Only load the wordlists when 'spelllang' is set and there is a
4084 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004086 {
4087 FOR_ALL_WINDOWS(wp)
4088 if (wp->w_buffer == buf && wp->w_p_spell)
4089 {
4090 (void)did_set_spelllang(buf);
4091# ifdef FEAT_WINDOWS
4092 break;
4093# endif
4094 }
4095 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096 }
4097}
4098# endif
4099
Bram Moolenaarb765d632005-06-07 21:00:02 +00004100/*
4101 * Reload the spell file "fname" if it's loaded.
4102 */
4103 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004105 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004107{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004108 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004111 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004112 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004113 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004114 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004115 slang_clear(slang);
4116 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004117 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004118 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004119 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004120 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004121 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123
4124 /* When "zg" was used and the file wasn't loaded yet, should redo
4125 * 'spelllang' to get it loaded. */
4126 if (added_word && !didit)
4127 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004128}
4129
4130
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004131/*
4132 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 */
4134
Bram Moolenaar51485f02005-06-04 21:55:20 +00004135#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004136 and .dic file. */
4137/*
4138 * Main structure to store the contents of a ".aff" file.
4139 */
4140typedef struct afffile_S
4141{
4142 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004143 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004144 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004145 unsigned af_rar; /* RAR ID for rare word */
4146 unsigned af_kep; /* KEP ID for keep-case word */
4147 unsigned af_bad; /* BAD ID for banned word */
4148 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004149 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004150 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004151 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4152 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004153 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004154} afffile_T;
4155
Bram Moolenaar6de68532005-08-24 22:08:48 +00004156#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004157#define AFT_LONG 1 /* flags are two characters */
4158#define AFT_CAPLONG 2 /* flags are one or two characters */
4159#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004160
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004161typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004162/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4163struct affentry_S
4164{
4165 affentry_T *ae_next; /* next affix with same name/number */
4166 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4167 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004168 char_u *ae_cond; /* condition (NULL for ".") */
4169 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004170 char_u ae_rare; /* rare affix */
4171 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004172};
4173
Bram Moolenaar6de68532005-08-24 22:08:48 +00004174#ifdef FEAT_MBYTE
4175# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4176#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004177# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004178#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004179
Bram Moolenaar51485f02005-06-04 21:55:20 +00004180/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4181typedef struct affheader_S
4182{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004183 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4184 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4185 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004187 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004188 affentry_T *ah_first; /* first affix entry */
4189} affheader_T;
4190
4191#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4192
Bram Moolenaar6de68532005-08-24 22:08:48 +00004193/* Flag used in compound items. */
4194typedef struct compitem_S
4195{
4196 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4197 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4198 int ci_newID; /* affix ID after renumbering. */
4199} compitem_T;
4200
4201#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4202
Bram Moolenaar51485f02005-06-04 21:55:20 +00004203/*
4204 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004205 * the need to keep track of each allocated thing, everything is freed all at
4206 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 */
4208#define SBLOCKSIZE 16000 /* size of sb_data */
4209typedef struct sblock_S sblock_T;
4210struct sblock_S
4211{
4212 sblock_T *sb_next; /* next block in list */
4213 int sb_used; /* nr of bytes already in use */
4214 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004215};
4216
4217/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004218 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004219 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004220typedef struct wordnode_S wordnode_T;
4221struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004222{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004223 union /* shared to save space */
4224 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004225 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004226 int index; /* index in written nodes (valid after first
4227 round) */
4228 } wn_u1;
4229 union /* shared to save space */
4230 {
4231 wordnode_T *next; /* next node with same hash key */
4232 wordnode_T *wnode; /* parent node that will write this node */
4233 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004234 wordnode_T *wn_child; /* child (next byte in word) */
4235 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4236 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004237 int wn_refs; /* Nr. of references to this node. Only
4238 relevant for first node in a list of
4239 siblings, in following siblings it is
4240 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004242 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004243 prefix ID or 0 */
4244 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4245 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004246 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004247#ifdef SPELL_PRINTTREE
4248 int wn_nr; /* sequence nr for printing */
4249#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004250};
4251
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004252#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4253
Bram Moolenaar51485f02005-06-04 21:55:20 +00004254#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004255
Bram Moolenaar51485f02005-06-04 21:55:20 +00004256/*
4257 * Info used while reading the spell files.
4258 */
4259typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004261 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004262 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004263
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004265 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004266
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004267 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004268
Bram Moolenaar51485f02005-06-04 21:55:20 +00004269 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004270 long si_blocks_cnt; /* memory blocks allocated */
4271 long si_compress_cnt; /* words to add before lowering
4272 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004273 wordnode_T *si_first_free; /* List of nodes that have been freed during
4274 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004275 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004276#ifdef SPELL_PRINTTREE
4277 int si_wordnode_nr; /* sequence nr for nodes */
4278#endif
4279
4280
Bram Moolenaar51485f02005-06-04 21:55:20 +00004281 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004282 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004283 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004284 int si_region; /* region mask */
4285 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004286 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004287 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004288 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004289 int si_region_count; /* number of regions supported (1 when there
4290 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004291 char_u si_region_name[16]; /* region names; used only if
4292 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293
4294 garray_T si_rep; /* list of fromto_T entries from REP lines */
4295 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004296 char_u *si_sofofr; /* SOFOFROM text */
4297 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 int si_followup; /* soundsalike: ? */
4299 int si_collapse; /* soundsalike: ? */
4300 int si_rem_accents; /* soundsalike: remove accents */
4301 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004302 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004303 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004304 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004305 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004306 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004307 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004308 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004309 garray_T si_prefcond; /* table with conditions for postponed
4310 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004311 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004312 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004313} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004314
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004315static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004316static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4317static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4318static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004319static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004320static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4321static void aff_check_number __ARGS((int spinval, int affval, char *name));
4322static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004323static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4325static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004326static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004327static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004328static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004329static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004330static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004331static 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 +00004332static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4333static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4334static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004335static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004336static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004337static 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 +00004338static 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 +00004339static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4340static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4341static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4342static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4343static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004344static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004345static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004346static void clear_node __ARGS((wordnode_T *node));
4347static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004349static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004350
Bram Moolenaar53805d12005-08-01 07:08:33 +00004351/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4352 * but it must be negative to indicate the prefix tree to tree_add_word().
4353 * Use a negative number with the lower 8 bits zero. */
4354#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004355
Bram Moolenaar5195e452005-08-19 20:32:47 +00004356/*
4357 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4358 */
4359static long compress_start = 30000; /* memory / SBLOCKSIZE */
4360static long compress_inc = 100; /* memory / SBLOCKSIZE */
4361static long compress_added = 500000; /* word count */
4362
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004363#ifdef SPELL_PRINTTREE
4364/*
4365 * For debugging the tree code: print the current tree in a (more or less)
4366 * readable format, so that we can see what happens when adding a word and/or
4367 * compressing the tree.
4368 * Based on code from Olaf Seibert.
4369 */
4370#define PRINTLINESIZE 1000
4371#define PRINTWIDTH 6
4372
4373#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4374 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4375
4376static char line1[PRINTLINESIZE];
4377static char line2[PRINTLINESIZE];
4378static char line3[PRINTLINESIZE];
4379
4380 static void
4381spell_clear_flags(wordnode_T *node)
4382{
4383 wordnode_T *np;
4384
4385 for (np = node; np != NULL; np = np->wn_sibling)
4386 {
4387 np->wn_u1.index = FALSE;
4388 spell_clear_flags(np->wn_child);
4389 }
4390}
4391
4392 static void
4393spell_print_node(wordnode_T *node, int depth)
4394{
4395 if (node->wn_u1.index)
4396 {
4397 /* Done this node before, print the reference. */
4398 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4399 PRINTSOME(line2, depth, " ", 0, 0);
4400 PRINTSOME(line3, depth, " ", 0, 0);
4401 msg(line1);
4402 msg(line2);
4403 msg(line3);
4404 }
4405 else
4406 {
4407 node->wn_u1.index = TRUE;
4408
4409 if (node->wn_byte != NUL)
4410 {
4411 if (node->wn_child != NULL)
4412 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4413 else
4414 /* Cannot happen? */
4415 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4416 }
4417 else
4418 PRINTSOME(line1, depth, " $ ", 0, 0);
4419
4420 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4421
4422 if (node->wn_sibling != NULL)
4423 PRINTSOME(line3, depth, " | ", 0, 0);
4424 else
4425 PRINTSOME(line3, depth, " ", 0, 0);
4426
4427 if (node->wn_byte == NUL)
4428 {
4429 msg(line1);
4430 msg(line2);
4431 msg(line3);
4432 }
4433
4434 /* do the children */
4435 if (node->wn_byte != NUL && node->wn_child != NULL)
4436 spell_print_node(node->wn_child, depth + 1);
4437
4438 /* do the siblings */
4439 if (node->wn_sibling != NULL)
4440 {
4441 /* get rid of all parent details except | */
4442 STRCPY(line1, line3);
4443 STRCPY(line2, line3);
4444 spell_print_node(node->wn_sibling, depth);
4445 }
4446 }
4447}
4448
4449 static void
4450spell_print_tree(wordnode_T *root)
4451{
4452 if (root != NULL)
4453 {
4454 /* Clear the "wn_u1.index" fields, used to remember what has been
4455 * done. */
4456 spell_clear_flags(root);
4457
4458 /* Recursively print the tree. */
4459 spell_print_node(root, 0);
4460 }
4461}
4462#endif /* SPELL_PRINTTREE */
4463
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004466 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004467 */
4468 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004469spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004470 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004471 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472{
4473 FILE *fd;
4474 afffile_T *aff;
4475 char_u rline[MAXLINELEN];
4476 char_u *line;
4477 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004478#define MAXITEMCNT 7
4479 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004480 int itemcnt;
4481 char_u *p;
4482 int lnum = 0;
4483 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004484 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004485 int aff_todo = 0;
4486 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004487 char_u *low = NULL;
4488 char_u *fol = NULL;
4489 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004490 int do_rep;
4491 int do_sal;
4492 int do_map;
4493 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004494 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004495 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004496 int compminlen = 0; /* COMPOUNDMIN value */
4497 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4498 int compmax = 0; /* COMPOUNDMAX value */
4499 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4500 concatenated */
4501 char_u *midword = NULL; /* MIDWORD value */
4502 char_u *syllable = NULL; /* SYLLABLE value */
4503 char_u *sofofrom = NULL; /* SOFOFROM value */
4504 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004505
Bram Moolenaar51485f02005-06-04 21:55:20 +00004506 /*
4507 * Open the file.
4508 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004509 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004510 if (fd == NULL)
4511 {
4512 EMSG2(_(e_notopen), fname);
4513 return NULL;
4514 }
4515
Bram Moolenaarb765d632005-06-07 21:00:02 +00004516 if (spin->si_verbose || p_verbose > 2)
4517 {
4518 if (!spin->si_verbose)
4519 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004520 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004521 out_flush();
4522 if (!spin->si_verbose)
4523 verbose_leave();
4524 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 /* Only do REP lines when not done in another .aff file already. */
4527 do_rep = spin->si_rep.ga_len == 0;
4528
4529 /* Only do SAL lines when not done in another .aff file already. */
4530 do_sal = spin->si_sal.ga_len == 0;
4531
4532 /* Only do MAP lines when not done in another .aff file already. */
4533 do_map = spin->si_map.ga_len == 0;
4534
Bram Moolenaar51485f02005-06-04 21:55:20 +00004535 /*
4536 * Allocate and init the afffile_T structure.
4537 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004538 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004539 if (aff == NULL)
4540 return NULL;
4541 hash_init(&aff->af_pref);
4542 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004543 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004544
4545 /*
4546 * Read all the lines in the file one by one.
4547 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004548 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004549 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004550 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004551 ++lnum;
4552
4553 /* Skip comment lines. */
4554 if (*rline == '#')
4555 continue;
4556
4557 /* Convert from "SET" to 'encoding' when needed. */
4558 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004559#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004560 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004561 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004562 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004563 if (pc == NULL)
4564 {
4565 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4566 fname, lnum, rline);
4567 continue;
4568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569 line = pc;
4570 }
4571 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004572#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 {
4574 pc = NULL;
4575 line = rline;
4576 }
4577
4578 /* Split the line up in white separated items. Put a NUL after each
4579 * item. */
4580 itemcnt = 0;
4581 for (p = line; ; )
4582 {
4583 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4584 ++p;
4585 if (*p == NUL)
4586 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004587 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004588 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004590 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 ++p;
4592 if (*p == NUL)
4593 break;
4594 *p++ = NUL;
4595 }
4596
4597 /* Handle non-empty lines. */
4598 if (itemcnt > 0)
4599 {
4600 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4601 && aff->af_enc == NULL)
4602 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004603#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004604 /* Setup for conversion from "ENC" to 'encoding'. */
4605 aff->af_enc = enc_canonize(items[1]);
4606 if (aff->af_enc != NULL && !spin->si_ascii
4607 && convert_setup(&spin->si_conv, aff->af_enc,
4608 p_enc) == FAIL)
4609 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4610 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004611 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004612#else
4613 smsg((char_u *)_("Conversion in %s not supported"), fname);
4614#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004615 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004616 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4617 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004618 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004619 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004620 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004621 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004622 aff->af_flagtype = AFT_NUM;
4623 else if (STRCMP(items[1], "caplong") == 0)
4624 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004625 else
4626 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4627 fname, lnum, items[1]);
4628 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004629 || aff->af_needaffix != 0
4630 || aff->af_needcomp != 0
4631 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004632 || aff->af_suff.ht_used > 0
4633 || aff->af_pref.ht_used > 0)
4634 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4635 fname, lnum, items[1]);
4636 }
4637 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4638 && midword == NULL)
4639 {
4640 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004641 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004642 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004644 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004646 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004649 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004650 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4651 && aff->af_slash == 0)
4652 {
4653 aff->af_slash = items[1][0];
4654 if (items[1][1] != NUL)
4655 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4656 fname, lnum, items[1]);
4657 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004658 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4659 && aff->af_rar == 0)
4660 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004661 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4662 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004663 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004664 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4665 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004666 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004667 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4668 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004669 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004670 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4671 && aff->af_bad == 0)
4672 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004673 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4674 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004675 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004676 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4677 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004678 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004679 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4680 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004681 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004682 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
4683 && aff->af_needcomp == 0)
4684 {
4685 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
4686 fname, lnum);
4687 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004688 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004689 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004690 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004691 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4692 * "Na" into "Na+", "1234" into "1234+". */
4693 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004694 if (p != NULL)
4695 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004696 STRCPY(p, items[1]);
4697 STRCAT(p, "+");
4698 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004699 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004700 }
4701 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4702 {
4703 /* Concatenate this string to previously defined ones, using a
4704 * slash to separate them. */
4705 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004706 if (compflags != NULL)
4707 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004708 p = getroom(spin, l, FALSE);
4709 if (p != NULL)
4710 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004711 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004712 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004713 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004714 STRCAT(p, "/");
4715 }
4716 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004717 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004718 }
4719 }
4720 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004721 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004722 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004723 compmax = atoi((char *)items[1]);
4724 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004725 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4726 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004727 }
4728 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004729 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004730 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004731 compminlen = atoi((char *)items[1]);
4732 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004733 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4734 fname, lnum, items[1]);
4735 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004736 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004737 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004738 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004739 compsylmax = atoi((char *)items[1]);
4740 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004741 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4742 fname, lnum, items[1]);
4743 }
4744 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004745 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004746 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004747 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004748 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004749 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4750 {
4751 spin->si_nobreak = TRUE;
4752 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004753 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4754 {
4755 aff->af_pfxpostpone = TRUE;
4756 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757 else if ((STRCMP(items[0], "PFX") == 0
4758 || STRCMP(items[0], "SFX") == 0)
4759 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004760 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004761 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004762 int lasti = 4;
4763 char_u key[AH_KEY_LEN];
4764
4765 if (*items[0] == 'P')
4766 tp = &aff->af_pref;
4767 else
4768 tp = &aff->af_suff;
4769
4770 /* Myspell allows the same affix name to be used multiple
4771 * times. The affix files that do this have an undocumented
4772 * "S" flag on all but the last block, thus we check for that
4773 * and store it in ah_follows. */
4774 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4775 hi = hash_find(tp, key);
4776 if (!HASHITEM_EMPTY(hi))
4777 {
4778 cur_aff = HI2AH(hi);
4779 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4780 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4781 fname, lnum, items[1]);
4782 if (!cur_aff->ah_follows)
4783 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4784 fname, lnum, items[1]);
4785 }
4786 else
4787 {
4788 /* New affix letter. */
4789 cur_aff = (affheader_T *)getroom(spin,
4790 sizeof(affheader_T), TRUE);
4791 if (cur_aff == NULL)
4792 break;
4793 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4794 fname, lnum);
4795 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4796 break;
4797 if (cur_aff->ah_flag == aff->af_bad
4798 || cur_aff->ah_flag == aff->af_rar
4799 || cur_aff->ah_flag == aff->af_kep
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004800 || cur_aff->ah_flag == aff->af_needaffix
4801 || cur_aff->ah_flag == aff->af_needcomp)
4802 smsg((char_u *)_("Affix also used for BAD/RAR/KEP/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00004803 fname, lnum, items[1]);
4804 STRCPY(cur_aff->ah_key, items[1]);
4805 hash_add(tp, cur_aff->ah_key);
4806
4807 cur_aff->ah_combine = (*items[2] == 'Y');
4808 }
4809
4810 /* Check for the "S" flag, which apparently means that another
4811 * block with the same affix name is following. */
4812 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4813 {
4814 ++lasti;
4815 cur_aff->ah_follows = TRUE;
4816 }
4817 else
4818 cur_aff->ah_follows = FALSE;
4819
Bram Moolenaar8db73182005-06-17 21:51:16 +00004820 /* Myspell allows extra text after the item, but that might
4821 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004822 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004823 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4824 fname, lnum, items[4]);
4825
Bram Moolenaar95529562005-08-25 21:21:38 +00004826 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004827 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4828 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004829
Bram Moolenaar95529562005-08-25 21:21:38 +00004830 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004831 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004832 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004833 {
4834 /* Use a new number in the .spl file later, to be able
4835 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004836 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004837 cur_aff->ah_newID = ++spin->si_newprefID;
4838
4839 /* We only really use ah_newID if the prefix is
4840 * postponed. We know that only after handling all
4841 * the items. */
4842 did_postpone_prefix = FALSE;
4843 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004844 else
4845 /* Did use the ID in a previous block. */
4846 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004847 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004848
Bram Moolenaar51485f02005-06-04 21:55:20 +00004849 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004850 }
4851 else if ((STRCMP(items[0], "PFX") == 0
4852 || STRCMP(items[0], "SFX") == 0)
4853 && aff_todo > 0
4854 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004855 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004856 {
4857 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004858 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004859 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004860 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004861 int lasti = 5;
4862
Bram Moolenaar5195e452005-08-19 20:32:47 +00004863 /* Check for "rare" and "nocomp" after the other info. */
4864 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004865 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004866 if (!rare && STRICMP(items[lasti], "rare") == 0)
4867 {
4868 rare = TRUE;
4869 ++lasti;
4870 }
4871 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4872 {
4873 nocomp = TRUE;
4874 ++lasti;
4875 }
4876 else
4877 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004878 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004879
Bram Moolenaar8db73182005-06-17 21:51:16 +00004880 /* Myspell allows extra text after the item, but that might
4881 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004882 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004883 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004884
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004885 /* New item for an affix letter. */
4886 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004887 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004888 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004889 if (aff_entry == NULL)
4890 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004891 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004892 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004893
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004894 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004895 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004897 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004898
Bram Moolenaar51485f02005-06-04 21:55:20 +00004899 /* Don't use an affix entry with non-ASCII characters when
4900 * "spin->si_ascii" is TRUE. */
4901 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004902 || has_non_ascii(aff_entry->ae_add)))
4903 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004904 aff_entry->ae_next = cur_aff->ah_first;
4905 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004906
4907 if (STRCMP(items[4], ".") != 0)
4908 {
4909 char_u buf[MAXLINELEN];
4910
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004911 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912 if (*items[0] == 'P')
4913 sprintf((char *)buf, "^%s", items[4]);
4914 else
4915 sprintf((char *)buf, "%s$", items[4]);
4916 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004917 RE_MAGIC + RE_STRING + RE_STRICT);
4918 if (aff_entry->ae_prog == NULL)
4919 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4920 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004922
4923 /* For postponed prefixes we need an entry in si_prefcond
4924 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004925 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004926 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004927 /* When the chop string is one lower-case letter and
4928 * the add string ends in the upper-case letter we set
4929 * the "upper" flag, clear "ae_chop" and remove the
4930 * letters from "ae_add". The condition must either
4931 * be empty or start with the same letter. */
4932 if (aff_entry->ae_chop != NULL
4933 && aff_entry->ae_add != NULL
4934#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004935 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004936 aff_entry->ae_chop)] == NUL
4937#else
4938 && aff_entry->ae_chop[1] == NUL
4939#endif
4940 )
4941 {
4942 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004943
Bram Moolenaar53805d12005-08-01 07:08:33 +00004944 c = PTR2CHAR(aff_entry->ae_chop);
4945 c_up = SPELL_TOUPPER(c);
4946 if (c_up != c
4947 && (aff_entry->ae_cond == NULL
4948 || PTR2CHAR(aff_entry->ae_cond) == c))
4949 {
4950 p = aff_entry->ae_add
4951 + STRLEN(aff_entry->ae_add);
4952 mb_ptr_back(aff_entry->ae_add, p);
4953 if (PTR2CHAR(p) == c_up)
4954 {
4955 upper = TRUE;
4956 aff_entry->ae_chop = NULL;
4957 *p = NUL;
4958
4959 /* The condition is matched with the
4960 * actual word, thus must check for the
4961 * upper-case letter. */
4962 if (aff_entry->ae_cond != NULL)
4963 {
4964 char_u buf[MAXLINELEN];
4965#ifdef FEAT_MBYTE
4966 if (has_mbyte)
4967 {
4968 onecap_copy(items[4], buf, TRUE);
4969 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004970 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004971 }
4972 else
4973#endif
4974 *aff_entry->ae_cond = c_up;
4975 if (aff_entry->ae_cond != NULL)
4976 {
4977 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004978 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004979 vim_free(aff_entry->ae_prog);
4980 aff_entry->ae_prog = vim_regcomp(
4981 buf, RE_MAGIC + RE_STRING);
4982 }
4983 }
4984 }
4985 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004986 }
4987
Bram Moolenaar53805d12005-08-01 07:08:33 +00004988 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004989 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004990 int idx;
4991 char_u **pp;
4992 int n;
4993
Bram Moolenaar6de68532005-08-24 22:08:48 +00004994 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004995 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4996 --idx)
4997 {
4998 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4999 if (str_equal(p, aff_entry->ae_cond))
5000 break;
5001 }
5002 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5003 {
5004 /* Not found, add a new condition. */
5005 idx = spin->si_prefcond.ga_len++;
5006 pp = ((char_u **)spin->si_prefcond.ga_data)
5007 + idx;
5008 if (aff_entry->ae_cond == NULL)
5009 *pp = NULL;
5010 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005011 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005012 aff_entry->ae_cond);
5013 }
5014
5015 /* Add the prefix to the prefix tree. */
5016 if (aff_entry->ae_add == NULL)
5017 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005018 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005019 p = aff_entry->ae_add;
5020 /* PFX_FLAGS is a negative number, so that
5021 * tree_add_word() knows this is the prefix tree. */
5022 n = PFX_FLAGS;
5023 if (rare)
5024 n |= WFP_RARE;
5025 if (!cur_aff->ah_combine)
5026 n |= WFP_NC;
5027 if (upper)
5028 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005029 tree_add_word(spin, p, spin->si_prefroot, n,
5030 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005031 did_postpone_prefix = TRUE;
5032 }
5033
5034 /* Didn't actually use ah_newID, backup si_newprefID. */
5035 if (aff_todo == 0 && !did_postpone_prefix)
5036 {
5037 --spin->si_newprefID;
5038 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005039 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005040 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005041 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005042 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005043 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5044 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005045 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005046 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005047 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005048 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5049 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005050 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005051 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005052 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005053 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5054 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005055 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005056 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005057 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005058 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005059 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005060 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005061 if (!isdigit(*items[1]))
5062 smsg((char_u *)_("Expected REP count in %s line %d"),
5063 fname, lnum);
5064 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005065 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005067 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005068 /* Myspell ignores extra arguments, we require it starts with
5069 * # to detect mistakes. */
5070 if (itemcnt > 3 && items[3][0] != '#')
5071 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005072 if (do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005073 {
5074 /* Replace underscore with space (can't include a space
5075 * directly). */
5076 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5077 if (*p == '_')
5078 *p = ' ';
5079 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5080 if (*p == '_')
5081 *p = ' ';
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005082 add_fromto(spin, &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005083 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005084 }
5085 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5086 {
5087 /* MAP item or count */
5088 if (!found_map)
5089 {
5090 /* First line contains the count. */
5091 found_map = TRUE;
5092 if (!isdigit(*items[1]))
5093 smsg((char_u *)_("Expected MAP count in %s line %d"),
5094 fname, lnum);
5095 }
5096 else if (do_map)
5097 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005098 int c;
5099
5100 /* Check that every character appears only once. */
5101 for (p = items[1]; *p != NUL; )
5102 {
5103#ifdef FEAT_MBYTE
5104 c = mb_ptr2char_adv(&p);
5105#else
5106 c = *p++;
5107#endif
5108 if ((spin->si_map.ga_len > 0
5109 && vim_strchr(spin->si_map.ga_data, c)
5110 != NULL)
5111 || vim_strchr(p, c) != NULL)
5112 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5113 fname, lnum);
5114 }
5115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005116 /* We simply concatenate all the MAP strings, separated by
5117 * slashes. */
5118 ga_concat(&spin->si_map, items[1]);
5119 ga_append(&spin->si_map, '/');
5120 }
5121 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005122 /* Accept "SAL from to" and "SAL from to # comment". */
5123 else if (STRCMP(items[0], "SAL") == 0
5124 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005125 {
5126 if (do_sal)
5127 {
5128 /* SAL item (sounds-a-like)
5129 * Either one of the known keys or a from-to pair. */
5130 if (STRCMP(items[1], "followup") == 0)
5131 spin->si_followup = sal_to_bool(items[2]);
5132 else if (STRCMP(items[1], "collapse_result") == 0)
5133 spin->si_collapse = sal_to_bool(items[2]);
5134 else if (STRCMP(items[1], "remove_accents") == 0)
5135 spin->si_rem_accents = sal_to_bool(items[2]);
5136 else
5137 /* when "to" is "_" it means empty */
5138 add_fromto(spin, &spin->si_sal, items[1],
5139 STRCMP(items[2], "_") == 0 ? (char_u *)""
5140 : items[2]);
5141 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005142 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005143 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005144 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005145 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005146 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005147 }
5148 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005149 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005150 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005151 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005152 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005153 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005154 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005155 fname, lnum, items[0]);
5156 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005157 }
5158
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005159 if (fol != NULL || low != NULL || upp != NULL)
5160 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005161 if (spin->si_clear_chartab)
5162 {
5163 /* Clear the char type tables, don't want to use any of the
5164 * currently used spell properties. */
5165 init_spell_chartab();
5166 spin->si_clear_chartab = FALSE;
5167 }
5168
Bram Moolenaar3982c542005-06-08 21:56:31 +00005169 /*
5170 * Don't write a word table for an ASCII file, so that we don't check
5171 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005172 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005173 * mb_get_class(), the list of chars in the file will be incomplete.
5174 */
5175 if (!spin->si_ascii
5176#ifdef FEAT_MBYTE
5177 && !enc_utf8
5178#endif
5179 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005180 {
5181 if (fol == NULL || low == NULL || upp == NULL)
5182 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5183 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005184 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005185 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005186
5187 vim_free(fol);
5188 vim_free(low);
5189 vim_free(upp);
5190 }
5191
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005192 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005193 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005194 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005195 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5196 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005197 }
5198
Bram Moolenaar6de68532005-08-24 22:08:48 +00005199 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005200 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005201 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5202 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005203 }
5204
Bram Moolenaar6de68532005-08-24 22:08:48 +00005205 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005206 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005207 if (syllable == NULL)
5208 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5209 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5210 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005211 }
5212
Bram Moolenaar6de68532005-08-24 22:08:48 +00005213 if (compflags != NULL)
5214 process_compflags(spin, aff, compflags);
5215
5216 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005217 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005218 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005219 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005220 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005221 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005222 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005223 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005224 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005225 }
5226
Bram Moolenaar6de68532005-08-24 22:08:48 +00005227 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005228 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005229 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5230 spin->si_syllable = syllable;
5231 }
5232
5233 if (sofofrom != NULL || sofoto != NULL)
5234 {
5235 if (sofofrom == NULL || sofoto == NULL)
5236 smsg((char_u *)_("Missing SOFO%s line in %s"),
5237 sofofrom == NULL ? "FROM" : "TO", fname);
5238 else if (spin->si_sal.ga_len > 0)
5239 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005240 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005241 {
5242 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5243 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5244 spin->si_sofofr = sofofrom;
5245 spin->si_sofoto = sofoto;
5246 }
5247 }
5248
5249 if (midword != NULL)
5250 {
5251 aff_check_string(spin->si_midword, midword, "MIDWORD");
5252 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005253 }
5254
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005255 vim_free(pc);
5256 fclose(fd);
5257 return aff;
5258}
5259
5260/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005261 * Turn an affix flag name into a number, according to the FLAG type.
5262 * returns zero for failure.
5263 */
5264 static unsigned
5265affitem2flag(flagtype, item, fname, lnum)
5266 int flagtype;
5267 char_u *item;
5268 char_u *fname;
5269 int lnum;
5270{
5271 unsigned res;
5272 char_u *p = item;
5273
5274 res = get_affitem(flagtype, &p);
5275 if (res == 0)
5276 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005277 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005278 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5279 fname, lnum, item);
5280 else
5281 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5282 fname, lnum, item);
5283 }
5284 if (*p != NUL)
5285 {
5286 smsg((char_u *)_(e_affname), fname, lnum, item);
5287 return 0;
5288 }
5289
5290 return res;
5291}
5292
5293/*
5294 * Get one affix name from "*pp" and advance the pointer.
5295 * Returns zero for an error, still advances the pointer then.
5296 */
5297 static unsigned
5298get_affitem(flagtype, pp)
5299 int flagtype;
5300 char_u **pp;
5301{
5302 int res;
5303
Bram Moolenaar95529562005-08-25 21:21:38 +00005304 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005305 {
5306 if (!VIM_ISDIGIT(**pp))
5307 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005308 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005309 return 0;
5310 }
5311 res = getdigits(pp);
5312 }
5313 else
5314 {
5315#ifdef FEAT_MBYTE
5316 res = mb_ptr2char_adv(pp);
5317#else
5318 res = *(*pp)++;
5319#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005320 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005321 && res >= 'A' && res <= 'Z'))
5322 {
5323 if (**pp == NUL)
5324 return 0;
5325#ifdef FEAT_MBYTE
5326 res = mb_ptr2char_adv(pp) + (res << 16);
5327#else
5328 res = *(*pp)++ + (res << 16);
5329#endif
5330 }
5331 }
5332 return res;
5333}
5334
5335/*
5336 * Process the "compflags" string used in an affix file and append it to
5337 * spin->si_compflags.
5338 * The processing involves changing the affix names to ID numbers, so that
5339 * they fit in one byte.
5340 */
5341 static void
5342process_compflags(spin, aff, compflags)
5343 spellinfo_T *spin;
5344 afffile_T *aff;
5345 char_u *compflags;
5346{
5347 char_u *p;
5348 char_u *prevp;
5349 unsigned flag;
5350 compitem_T *ci;
5351 int id;
5352 int len;
5353 char_u *tp;
5354 char_u key[AH_KEY_LEN];
5355 hashitem_T *hi;
5356
5357 /* Make room for the old and the new compflags, concatenated with a / in
5358 * between. Processing it makes it shorter, but we don't know by how
5359 * much, thus allocate the maximum. */
5360 len = STRLEN(compflags) + 1;
5361 if (spin->si_compflags != NULL)
5362 len += STRLEN(spin->si_compflags) + 1;
5363 p = getroom(spin, len, FALSE);
5364 if (p == NULL)
5365 return;
5366 if (spin->si_compflags != NULL)
5367 {
5368 STRCPY(p, spin->si_compflags);
5369 STRCAT(p, "/");
5370 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005371 spin->si_compflags = p;
5372 tp = p + STRLEN(p);
5373
5374 for (p = compflags; *p != NUL; )
5375 {
5376 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5377 /* Copy non-flag characters directly. */
5378 *tp++ = *p++;
5379 else
5380 {
5381 /* First get the flag number, also checks validity. */
5382 prevp = p;
5383 flag = get_affitem(aff->af_flagtype, &p);
5384 if (flag != 0)
5385 {
5386 /* Find the flag in the hashtable. If it was used before, use
5387 * the existing ID. Otherwise add a new entry. */
5388 vim_strncpy(key, prevp, p - prevp);
5389 hi = hash_find(&aff->af_comp, key);
5390 if (!HASHITEM_EMPTY(hi))
5391 id = HI2CI(hi)->ci_newID;
5392 else
5393 {
5394 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5395 if (ci == NULL)
5396 break;
5397 STRCPY(ci->ci_key, key);
5398 ci->ci_flag = flag;
5399 /* Avoid using a flag ID that has a special meaning in a
5400 * regexp (also inside []). */
5401 do
5402 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005403 check_renumber(spin);
5404 id = spin->si_newcompID--;
5405 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005406 ci->ci_newID = id;
5407 hash_add(&aff->af_comp, ci->ci_key);
5408 }
5409 *tp++ = id;
5410 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005411 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005412 ++p;
5413 }
5414 }
5415
5416 *tp = NUL;
5417}
5418
5419/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005420 * Check that the new IDs for postponed affixes and compounding don't overrun
5421 * each other. We have almost 255 available, but start at 0-127 to avoid
5422 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5423 * When that is used up an error message is given.
5424 */
5425 static void
5426check_renumber(spin)
5427 spellinfo_T *spin;
5428{
5429 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5430 {
5431 spin->si_newprefID = 127;
5432 spin->si_newcompID = 255;
5433 }
5434}
5435
5436/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005437 * Return TRUE if flag "flag" appears in affix list "afflist".
5438 */
5439 static int
5440flag_in_afflist(flagtype, afflist, flag)
5441 int flagtype;
5442 char_u *afflist;
5443 unsigned flag;
5444{
5445 char_u *p;
5446 unsigned n;
5447
5448 switch (flagtype)
5449 {
5450 case AFT_CHAR:
5451 return vim_strchr(afflist, flag) != NULL;
5452
Bram Moolenaar95529562005-08-25 21:21:38 +00005453 case AFT_CAPLONG:
5454 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005455 for (p = afflist; *p != NUL; )
5456 {
5457#ifdef FEAT_MBYTE
5458 n = mb_ptr2char_adv(&p);
5459#else
5460 n = *p++;
5461#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005462 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005463 && *p != NUL)
5464#ifdef FEAT_MBYTE
5465 n = mb_ptr2char_adv(&p) + (n << 16);
5466#else
5467 n = *p++ + (n << 16);
5468#endif
5469 if (n == flag)
5470 return TRUE;
5471 }
5472 break;
5473
Bram Moolenaar95529562005-08-25 21:21:38 +00005474 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005475 for (p = afflist; *p != NUL; )
5476 {
5477 n = getdigits(&p);
5478 if (n == flag)
5479 return TRUE;
5480 if (*p != NUL) /* skip over comma */
5481 ++p;
5482 }
5483 break;
5484 }
5485 return FALSE;
5486}
5487
5488/*
5489 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5490 */
5491 static void
5492aff_check_number(spinval, affval, name)
5493 int spinval;
5494 int affval;
5495 char *name;
5496{
5497 if (spinval != 0 && spinval != affval)
5498 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5499}
5500
5501/*
5502 * Give a warning when "spinval" and "affval" strings are set and not the same.
5503 */
5504 static void
5505aff_check_string(spinval, affval, name)
5506 char_u *spinval;
5507 char_u *affval;
5508 char *name;
5509{
5510 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5511 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5512}
5513
5514/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005515 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5516 * NULL as equal.
5517 */
5518 static int
5519str_equal(s1, s2)
5520 char_u *s1;
5521 char_u *s2;
5522{
5523 if (s1 == NULL || s2 == NULL)
5524 return s1 == s2;
5525 return STRCMP(s1, s2) == 0;
5526}
5527
5528/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005529 * Add a from-to item to "gap". Used for REP and SAL items.
5530 * They are stored case-folded.
5531 */
5532 static void
5533add_fromto(spin, gap, from, to)
5534 spellinfo_T *spin;
5535 garray_T *gap;
5536 char_u *from;
5537 char_u *to;
5538{
5539 fromto_T *ftp;
5540 char_u word[MAXWLEN];
5541
5542 if (ga_grow(gap, 1) == OK)
5543 {
5544 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5545 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005546 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005547 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005548 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005549 ++gap->ga_len;
5550 }
5551}
5552
5553/*
5554 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5555 */
5556 static int
5557sal_to_bool(s)
5558 char_u *s;
5559{
5560 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5561}
5562
5563/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005564 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5565 * When "s" is NULL FALSE is returned.
5566 */
5567 static int
5568has_non_ascii(s)
5569 char_u *s;
5570{
5571 char_u *p;
5572
5573 if (s != NULL)
5574 for (p = s; *p != NUL; ++p)
5575 if (*p >= 128)
5576 return TRUE;
5577 return FALSE;
5578}
5579
5580/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005581 * Free the structure filled by spell_read_aff().
5582 */
5583 static void
5584spell_free_aff(aff)
5585 afffile_T *aff;
5586{
5587 hashtab_T *ht;
5588 hashitem_T *hi;
5589 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005590 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005591 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005592
5593 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005594
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005595 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005596 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5597 {
5598 todo = ht->ht_used;
5599 for (hi = ht->ht_array; todo > 0; ++hi)
5600 {
5601 if (!HASHITEM_EMPTY(hi))
5602 {
5603 --todo;
5604 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005605 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5606 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005607 }
5608 }
5609 if (ht == &aff->af_suff)
5610 break;
5611 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005612
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005613 hash_clear(&aff->af_pref);
5614 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005615 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005616}
5617
5618/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005619 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005620 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005621 */
5622 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005623spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005624 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005625 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005626 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005627{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005628 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005629 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005630 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005631 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005632 char_u store_afflist[MAXWLEN];
5633 int pfxlen;
5634 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005635 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005636 char_u *pc;
5637 char_u *w;
5638 int l;
5639 hash_T hash;
5640 hashitem_T *hi;
5641 FILE *fd;
5642 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005643 int non_ascii = 0;
5644 int retval = OK;
5645 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005646 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005647 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005648
Bram Moolenaar51485f02005-06-04 21:55:20 +00005649 /*
5650 * Open the file.
5651 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005652 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005653 if (fd == NULL)
5654 {
5655 EMSG2(_(e_notopen), fname);
5656 return FAIL;
5657 }
5658
Bram Moolenaar51485f02005-06-04 21:55:20 +00005659 /* The hashtable is only used to detect duplicated words. */
5660 hash_init(&ht);
5661
Bram Moolenaarb765d632005-06-07 21:00:02 +00005662 if (spin->si_verbose || p_verbose > 2)
5663 {
5664 if (!spin->si_verbose)
5665 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005666 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005667 out_flush();
5668 if (!spin->si_verbose)
5669 verbose_leave();
5670 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005671
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005672 /* start with a message for the first line */
5673 spin->si_msg_count = 999999;
5674
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005675 /* Read and ignore the first line: word count. */
5676 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005677 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005678 EMSG2(_("E760: No word count in %s"), fname);
5679
5680 /*
5681 * Read all the lines in the file one by one.
5682 * The words are converted to 'encoding' here, before being added to
5683 * the hashtable.
5684 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005685 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005686 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005687 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005689 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005690 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005691
Bram Moolenaar51485f02005-06-04 21:55:20 +00005692 /* Remove CR, LF and white space from the end. White space halfway
5693 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005694 l = STRLEN(line);
5695 while (l > 0 && line[l - 1] <= ' ')
5696 --l;
5697 if (l == 0)
5698 continue; /* empty line */
5699 line[l] = NUL;
5700
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005701 /* Find the optional affix names. Replace the SLASH character by a
5702 * slash. */
5703 afflist = NULL;
5704 for (p = line; *p != NUL; mb_ptr_adv(p))
5705 {
5706 if (*p == affile->af_slash)
5707 *p = '/';
5708 else if (*p == '/')
5709 {
5710 *p = NUL;
5711 afflist = p + 1;
5712 break;
5713 }
5714 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005715
5716 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5717 if (spin->si_ascii && has_non_ascii(line))
5718 {
5719 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005720 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005721 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005722
Bram Moolenaarb765d632005-06-07 21:00:02 +00005723#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005724 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005725 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005726 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005727 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005728 if (pc == NULL)
5729 {
5730 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5731 fname, lnum, line);
5732 continue;
5733 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005734 w = pc;
5735 }
5736 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005737#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005738 {
5739 pc = NULL;
5740 w = line;
5741 }
5742
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005743 /* This takes time, print a message every 10000 words. */
5744 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005745 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005746 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005747 vim_snprintf((char *)message, sizeof(message),
5748 _("line %6d, word %6d - %s"),
5749 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5750 msg_start();
5751 msg_puts_long_attr(message, 0);
5752 msg_clr_eos();
5753 msg_didout = FALSE;
5754 msg_col = 0;
5755 out_flush();
5756 }
5757
Bram Moolenaar51485f02005-06-04 21:55:20 +00005758 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005759 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005760 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005761 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005762 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005763 if (retval == FAIL)
5764 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005765
Bram Moolenaar51485f02005-06-04 21:55:20 +00005766 hash = hash_hash(dw);
5767 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005769 {
5770 if (p_verbose > 0)
5771 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005772 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005773 else if (duplicate == 0)
5774 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5775 fname, lnum, dw);
5776 ++duplicate;
5777 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005778 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005779 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005780
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005781 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005782 store_afflist[0] = NUL;
5783 pfxlen = 0;
5784 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005785 if (afflist != NULL)
5786 {
5787 /* Check for affix name that stands for keep-case word and stands
5788 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005789 if (affile->af_kep != 0 && flag_in_afflist(
5790 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005791 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005792 if (affile->af_rar != 0 && flag_in_afflist(
5793 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005794 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005795 if (affile->af_bad != 0 && flag_in_afflist(
5796 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005797 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005798 if (affile->af_needaffix != 0 && flag_in_afflist(
5799 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005800 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005801 if (affile->af_needcomp != 0 && flag_in_afflist(
5802 affile->af_flagtype, afflist, affile->af_needcomp))
5803 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005804
5805 if (affile->af_pfxpostpone)
5806 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005807 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005808
Bram Moolenaar5195e452005-08-19 20:32:47 +00005809 if (spin->si_compflags != NULL)
5810 /* Need to store the list of compound flags with the word.
5811 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005812 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005813 }
5814
Bram Moolenaar51485f02005-06-04 21:55:20 +00005815 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005816 if (store_word(spin, dw, flags, spin->si_region,
5817 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005818 retval = FAIL;
5819
5820 if (afflist != NULL)
5821 {
5822 /* Find all matching suffixes and add the resulting words.
5823 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005824 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005825 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005826 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005827 retval = FAIL;
5828
5829 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005830 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005831 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005832 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005833 retval = FAIL;
5834 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005835 }
5836
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005837 if (duplicate > 0)
5838 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005839 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005840 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5841 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005842 hash_clear(&ht);
5843
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005844 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005845 return retval;
5846}
5847
5848/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005849 * Get the list of prefix IDs from the affix list "afflist".
5850 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005851 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5852 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005853 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005854 static int
5855get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005856 afffile_T *affile;
5857 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005858 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005859{
5860 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005861 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005862 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005863 int id;
5864 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005865 hashitem_T *hi;
5866
Bram Moolenaar6de68532005-08-24 22:08:48 +00005867 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005868 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 prevp = p;
5870 if (get_affitem(affile->af_flagtype, &p) != 0)
5871 {
5872 /* A flag is a postponed prefix flag if it appears in "af_pref"
5873 * and it's ID is not zero. */
5874 vim_strncpy(key, prevp, p - prevp);
5875 hi = hash_find(&affile->af_pref, key);
5876 if (!HASHITEM_EMPTY(hi))
5877 {
5878 id = HI2AH(hi)->ah_newID;
5879 if (id != 0)
5880 store_afflist[cnt++] = id;
5881 }
5882 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005883 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005884 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005885 }
5886
Bram Moolenaar5195e452005-08-19 20:32:47 +00005887 store_afflist[cnt] = NUL;
5888 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005889}
5890
5891/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005892 * Get the list of compound IDs from the affix list "afflist" that are used
5893 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005894 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005895 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005896 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005897get_compflags(affile, afflist, store_afflist)
5898 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005899 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005900 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005901{
5902 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005903 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005904 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905 char_u key[AH_KEY_LEN];
5906 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005907
Bram Moolenaar6de68532005-08-24 22:08:48 +00005908 for (p = afflist; *p != NUL; )
5909 {
5910 prevp = p;
5911 if (get_affitem(affile->af_flagtype, &p) != 0)
5912 {
5913 /* A flag is a compound flag if it appears in "af_comp". */
5914 vim_strncpy(key, prevp, p - prevp);
5915 hi = hash_find(&affile->af_comp, key);
5916 if (!HASHITEM_EMPTY(hi))
5917 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5918 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005919 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005920 ++p;
5921 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005922
Bram Moolenaar5195e452005-08-19 20:32:47 +00005923 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005924}
5925
5926/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005927 * Apply affixes to a word and store the resulting words.
5928 * "ht" is the hashtable with affentry_T that need to be applied, either
5929 * prefixes or suffixes.
5930 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5931 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005932 *
5933 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005934 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005935 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005936store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5937 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005938 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005939 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005940 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005941 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005942 hashtab_T *ht;
5943 hashtab_T *xht;
5944 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005945 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005946 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005947 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5948 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005949{
5950 int todo;
5951 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005952 affheader_T *ah;
5953 affentry_T *ae;
5954 regmatch_T regmatch;
5955 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005956 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005957 int i;
5958 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005959 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005960 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005961 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005962 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005963
Bram Moolenaar51485f02005-06-04 21:55:20 +00005964 todo = ht->ht_used;
5965 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005966 {
5967 if (!HASHITEM_EMPTY(hi))
5968 {
5969 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005970 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005971
Bram Moolenaar51485f02005-06-04 21:55:20 +00005972 /* Check that the affix combines, if required, and that the word
5973 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005974 if ((!comb || ah->ah_combine) && flag_in_afflist(
5975 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005976 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005977 /* Loop over all affix entries with this name. */
5978 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005979 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005980 /* Check the condition. It's not logical to match case
5981 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005982 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005983 * Another requirement from Myspell is that the chop
5984 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005985 * For prefixes, when "PFXPOSTPONE" was used, only do
5986 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005987 regmatch.regprog = ae->ae_prog;
5988 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005989 if ((xht != NULL || !affile->af_pfxpostpone
5990 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005991 && (ae->ae_chop == NULL
5992 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005993 && (ae->ae_prog == NULL
5994 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005995 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005996 /* Match. Remove the chop and add the affix. */
5997 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005998 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005999 /* prefix: chop/add at the start of the word */
6000 if (ae->ae_add == NULL)
6001 *newword = NUL;
6002 else
6003 STRCPY(newword, ae->ae_add);
6004 p = word;
6005 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006006 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006007 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006008#ifdef FEAT_MBYTE
6009 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006010 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006011 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006012 for ( ; i > 0; --i)
6013 mb_ptr_adv(p);
6014 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006015 else
6016#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006017 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006018 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006019 STRCAT(newword, p);
6020 }
6021 else
6022 {
6023 /* suffix: chop/add at the end of the word */
6024 STRCPY(newword, word);
6025 if (ae->ae_chop != NULL)
6026 {
6027 /* Remove chop string. */
6028 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006029 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006030 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006031 mb_ptr_back(newword, p);
6032 *p = NUL;
6033 }
6034 if (ae->ae_add != NULL)
6035 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006036 }
6037
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006038 /* Obey the "rare" flag of the affix. */
6039 if (ae->ae_rare)
6040 use_flags = flags | WF_RARE;
6041 else
6042 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006043
6044 /* Obey the "nocomp" flag of the affix: don't use the
6045 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006046 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006047 if (ae->ae_nocomp && pfxlist != NULL)
6048 {
6049 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6050 use_pfxlist = pfx_pfxlist;
6051 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006052
6053 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006054 if (spin->si_prefroot != NULL
6055 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006056 {
6057 /* ... add a flag to indicate an affix was used. */
6058 use_flags |= WF_HAS_AFF;
6059
6060 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006061 * affixes is not allowed. But do use the
6062 * compound flags after them. */
6063 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6064 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006065 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006066
Bram Moolenaar51485f02005-06-04 21:55:20 +00006067 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006068 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006069 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006070 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006071
Bram Moolenaar51485f02005-06-04 21:55:20 +00006072 /* When added a suffix and combining is allowed also
6073 * try adding prefixes additionally. */
6074 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006076 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006077 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006078 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006079 }
6080 }
6081 }
6082 }
6083 }
6084
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006085 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006086}
6087
6088/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006089 * Read a file with a list of words.
6090 */
6091 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006092spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006093 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006094 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006095{
6096 FILE *fd;
6097 long lnum = 0;
6098 char_u rline[MAXLINELEN];
6099 char_u *line;
6100 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006101 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006102 int l;
6103 int retval = OK;
6104 int did_word = FALSE;
6105 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006106 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006107 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006108
6109 /*
6110 * Open the file.
6111 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006112 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006113 if (fd == NULL)
6114 {
6115 EMSG2(_(e_notopen), fname);
6116 return FAIL;
6117 }
6118
Bram Moolenaarb765d632005-06-07 21:00:02 +00006119 if (spin->si_verbose || p_verbose > 2)
6120 {
6121 if (!spin->si_verbose)
6122 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006123 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006124 out_flush();
6125 if (!spin->si_verbose)
6126 verbose_leave();
6127 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006128
6129 /*
6130 * Read all the lines in the file one by one.
6131 */
6132 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6133 {
6134 line_breakcheck();
6135 ++lnum;
6136
6137 /* Skip comment lines. */
6138 if (*rline == '#')
6139 continue;
6140
6141 /* Remove CR, LF and white space from the end. */
6142 l = STRLEN(rline);
6143 while (l > 0 && rline[l - 1] <= ' ')
6144 --l;
6145 if (l == 0)
6146 continue; /* empty or blank line */
6147 rline[l] = NUL;
6148
6149 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6150 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006151#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006152 if (spin->si_conv.vc_type != CONV_NONE)
6153 {
6154 pc = string_convert(&spin->si_conv, rline, NULL);
6155 if (pc == NULL)
6156 {
6157 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6158 fname, lnum, rline);
6159 continue;
6160 }
6161 line = pc;
6162 }
6163 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006164#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006165 {
6166 pc = NULL;
6167 line = rline;
6168 }
6169
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006170 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006171 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006172 ++line;
6173 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006174 {
6175 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006176 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6177 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006178 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006179 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6180 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006181 else
6182 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006183#ifdef FEAT_MBYTE
6184 char_u *enc;
6185
Bram Moolenaar51485f02005-06-04 21:55:20 +00006186 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006187 line += 10;
6188 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006189 if (enc != NULL && !spin->si_ascii
6190 && convert_setup(&spin->si_conv, enc,
6191 p_enc) == FAIL)
6192 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006193 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006194 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006195 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006196#else
6197 smsg((char_u *)_("Conversion in %s not supported"), fname);
6198#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006199 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006200 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006201 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006202
Bram Moolenaar3982c542005-06-08 21:56:31 +00006203 if (STRNCMP(line, "regions=", 8) == 0)
6204 {
6205 if (spin->si_region_count > 1)
6206 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6207 fname, lnum, line);
6208 else
6209 {
6210 line += 8;
6211 if (STRLEN(line) > 16)
6212 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6213 fname, lnum, line);
6214 else
6215 {
6216 spin->si_region_count = STRLEN(line) / 2;
6217 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006218
6219 /* Adjust the mask for a word valid in all regions. */
6220 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006221 }
6222 }
6223 continue;
6224 }
6225
Bram Moolenaar7887d882005-07-01 22:33:52 +00006226 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6227 fname, lnum, line - 1);
6228 continue;
6229 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006230
Bram Moolenaar7887d882005-07-01 22:33:52 +00006231 flags = 0;
6232 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006233
Bram Moolenaar7887d882005-07-01 22:33:52 +00006234 /* Check for flags and region after a slash. */
6235 p = vim_strchr(line, '/');
6236 if (p != NULL)
6237 {
6238 *p++ = NUL;
6239 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006240 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006241 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006242 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006243 else if (*p == '!') /* Bad, bad, wicked word. */
6244 flags |= WF_BANNED;
6245 else if (*p == '?') /* Rare word. */
6246 flags |= WF_RARE;
6247 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006248 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006249 if ((flags & WF_REGION) == 0) /* first one */
6250 regionmask = 0;
6251 flags |= WF_REGION;
6252
6253 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006254 if (l > spin->si_region_count)
6255 {
6256 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006257 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006258 break;
6259 }
6260 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006261 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006262 else
6263 {
6264 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6265 fname, lnum, p);
6266 break;
6267 }
6268 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006269 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006270 }
6271
6272 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6273 if (spin->si_ascii && has_non_ascii(line))
6274 {
6275 ++non_ascii;
6276 continue;
6277 }
6278
6279 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006280 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006281 {
6282 retval = FAIL;
6283 break;
6284 }
6285 did_word = TRUE;
6286 }
6287
6288 vim_free(pc);
6289 fclose(fd);
6290
Bram Moolenaarb765d632005-06-07 21:00:02 +00006291 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6292 {
6293 if (p_verbose > 2)
6294 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006295 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6296 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006297 if (p_verbose > 2)
6298 verbose_leave();
6299 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006300 return retval;
6301}
6302
6303/*
6304 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006305 * This avoids calling free() for every little struct we use (and keeping
6306 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006307 * The memory is cleared to all zeros.
6308 * Returns NULL when out of memory.
6309 */
6310 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006311getroom(spin, len, align)
6312 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006313 size_t len; /* length needed */
6314 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006315{
6316 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006317 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006318
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006319 if (align && bl != NULL)
6320 /* Round size up for alignment. On some systems structures need to be
6321 * aligned to the size of a pointer (e.g., SPARC). */
6322 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6323 & ~(sizeof(char *) - 1);
6324
Bram Moolenaar51485f02005-06-04 21:55:20 +00006325 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6326 {
6327 /* Allocate a block of memory. This is not freed until much later. */
6328 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6329 if (bl == NULL)
6330 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006331 bl->sb_next = spin->si_blocks;
6332 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006333 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006334 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006335 }
6336
6337 p = bl->sb_data + bl->sb_used;
6338 bl->sb_used += len;
6339
6340 return p;
6341}
6342
6343/*
6344 * Make a copy of a string into memory allocated with getroom().
6345 */
6346 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006347getroom_save(spin, s)
6348 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006349 char_u *s;
6350{
6351 char_u *sc;
6352
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006353 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006354 if (sc != NULL)
6355 STRCPY(sc, s);
6356 return sc;
6357}
6358
6359
6360/*
6361 * Free the list of allocated sblock_T.
6362 */
6363 static void
6364free_blocks(bl)
6365 sblock_T *bl;
6366{
6367 sblock_T *next;
6368
6369 while (bl != NULL)
6370 {
6371 next = bl->sb_next;
6372 vim_free(bl);
6373 bl = next;
6374 }
6375}
6376
6377/*
6378 * Allocate the root of a word tree.
6379 */
6380 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006381wordtree_alloc(spin)
6382 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006383{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006384 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006385}
6386
6387/*
6388 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006389 * Always store it in the case-folded tree. For a keep-case word this is
6390 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6391 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006392 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006393 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6394 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006395 */
6396 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006397store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006398 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006399 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006400 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006401 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006402 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006403 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006404{
6405 int len = STRLEN(word);
6406 int ct = captype(word, word + len);
6407 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006408 int res = OK;
6409 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006411 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006412 for (p = pfxlist; res == OK; ++p)
6413 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006414 if (!need_affix || (p != NULL && *p != NUL))
6415 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006416 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006417 if (p == NULL || *p == NUL)
6418 break;
6419 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006420 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006421
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006423 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006424 for (p = pfxlist; res == OK; ++p)
6425 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006426 if (!need_affix || (p != NULL && *p != NUL))
6427 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006428 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006429 if (p == NULL || *p == NUL)
6430 break;
6431 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006432 ++spin->si_keepwcount;
6433 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006434 return res;
6435}
6436
6437/*
6438 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006439 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6440 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006441 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006442 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006443 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006444tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006445 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006446 char_u *word;
6447 wordnode_T *root;
6448 int flags;
6449 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006450 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006451{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006452 wordnode_T *node = root;
6453 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006454 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006455 wordnode_T **prev = NULL;
6456 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006457
Bram Moolenaar51485f02005-06-04 21:55:20 +00006458 /* Add each byte of the word to the tree, including the NUL at the end. */
6459 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006460 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006461 /* When there is more than one reference to this node we need to make
6462 * a copy, so that we can modify it. Copy the whole list of siblings
6463 * (we don't optimize for a partly shared list of siblings). */
6464 if (node != NULL && node->wn_refs > 1)
6465 {
6466 --node->wn_refs;
6467 copyprev = prev;
6468 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6469 {
6470 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006471 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006472 if (np == NULL)
6473 return FAIL;
6474 np->wn_child = copyp->wn_child;
6475 if (np->wn_child != NULL)
6476 ++np->wn_child->wn_refs; /* child gets extra ref */
6477 np->wn_byte = copyp->wn_byte;
6478 if (np->wn_byte == NUL)
6479 {
6480 np->wn_flags = copyp->wn_flags;
6481 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006482 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006483 }
6484
6485 /* Link the new node in the list, there will be one ref. */
6486 np->wn_refs = 1;
6487 *copyprev = np;
6488 copyprev = &np->wn_sibling;
6489
6490 /* Let "node" point to the head of the copied list. */
6491 if (copyp == node)
6492 node = np;
6493 }
6494 }
6495
Bram Moolenaar51485f02005-06-04 21:55:20 +00006496 /* Look for the sibling that has the same character. They are sorted
6497 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006498 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006499 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006500 while (node != NULL
6501 && (node->wn_byte < word[i]
6502 || (node->wn_byte == NUL
6503 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006504 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006505 : node->wn_flags < (flags & WN_MASK)
6506 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006507 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006508 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006509 prev = &node->wn_sibling;
6510 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006511 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006512 if (node == NULL
6513 || node->wn_byte != word[i]
6514 || (word[i] == NUL
6515 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006516 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006517 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006519 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006520 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006521 if (np == NULL)
6522 return FAIL;
6523 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006524
6525 /* If "node" is NULL this is a new child or the end of the sibling
6526 * list: ref count is one. Otherwise use ref count of sibling and
6527 * make ref count of sibling one (matters when inserting in front
6528 * of the list of siblings). */
6529 if (node == NULL)
6530 np->wn_refs = 1;
6531 else
6532 {
6533 np->wn_refs = node->wn_refs;
6534 node->wn_refs = 1;
6535 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006536 *prev = np;
6537 np->wn_sibling = node;
6538 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006539 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006540
Bram Moolenaar51485f02005-06-04 21:55:20 +00006541 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006542 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006543 node->wn_flags = flags;
6544 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006545 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006547 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006548 prev = &node->wn_child;
6549 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006550 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006551#ifdef SPELL_PRINTTREE
6552 smsg("Added \"%s\"", word);
6553 spell_print_tree(root->wn_sibling);
6554#endif
6555
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006556 /* count nr of words added since last message */
6557 ++spin->si_msg_count;
6558
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006559 if (spin->si_compress_cnt > 1)
6560 {
6561 if (--spin->si_compress_cnt == 1)
6562 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006563 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006564 }
6565
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006566 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006567 * When we have allocated lots of memory we need to compress the word tree
6568 * to free up some room. But compression is slow, and we might actually
6569 * need that room, thus only compress in the following situations:
6570 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006571 * "compress_start" blocks.
6572 * 2. When compressed before and used "compress_inc" blocks before
6573 * adding "compress_added" words (si_compress_cnt > 1).
6574 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006575 * (si_compress_cnt == 1) and the number of free nodes drops below the
6576 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006577 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006578#ifndef SPELL_PRINTTREE
6579 if (spin->si_compress_cnt == 1
6580 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006581 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006582#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006583 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006584 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006585 * when the freed up room has been used and another "compress_inc"
6586 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006587 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006588 spin->si_blocks_cnt -= compress_inc;
6589 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006590
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006591 if (spin->si_verbose)
6592 {
6593 msg_start();
6594 msg_puts((char_u *)_(msg_compressing));
6595 msg_clr_eos();
6596 msg_didout = FALSE;
6597 msg_col = 0;
6598 out_flush();
6599 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006600
6601 /* Compress both trees. Either they both have many nodes, which makes
6602 * compression useful, or one of them is small, which means
6603 * compression goes fast. */
6604 wordtree_compress(spin, spin->si_foldroot);
6605 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006606 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006607
6608 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006609}
6610
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006611/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006612 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6613 * Sets "sps_flags".
6614 */
6615 int
6616spell_check_msm()
6617{
6618 char_u *p = p_msm;
6619 long start = 0;
6620 long inc = 0;
6621 long added = 0;
6622
6623 if (!VIM_ISDIGIT(*p))
6624 return FAIL;
6625 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6626 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6627 if (*p != ',')
6628 return FAIL;
6629 ++p;
6630 if (!VIM_ISDIGIT(*p))
6631 return FAIL;
6632 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6633 if (*p != ',')
6634 return FAIL;
6635 ++p;
6636 if (!VIM_ISDIGIT(*p))
6637 return FAIL;
6638 added = getdigits(&p) * 1024;
6639 if (*p != NUL)
6640 return FAIL;
6641
6642 if (start == 0 || inc == 0 || added == 0 || inc > start)
6643 return FAIL;
6644
6645 compress_start = start;
6646 compress_inc = inc;
6647 compress_added = added;
6648 return OK;
6649}
6650
6651
6652/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006653 * Get a wordnode_T, either from the list of previously freed nodes or
6654 * allocate a new one.
6655 */
6656 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006657get_wordnode(spin)
6658 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006659{
6660 wordnode_T *n;
6661
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006662 if (spin->si_first_free == NULL)
6663 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006664 else
6665 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006666 n = spin->si_first_free;
6667 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006668 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006669 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006670 }
6671#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006672 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006673#endif
6674 return n;
6675}
6676
6677/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006678 * Decrement the reference count on a node (which is the head of a list of
6679 * siblings). If the reference count becomes zero free the node and its
6680 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006681 */
6682 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006683deref_wordnode(spin, node)
6684 spellinfo_T *spin;
6685 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006686{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006687 wordnode_T *np;
6688
6689 if (--node->wn_refs == 0)
6690 for (np = node; np != NULL; np = np->wn_sibling)
6691 {
6692 if (np->wn_child != NULL)
6693 deref_wordnode(spin, np->wn_child);
6694 free_wordnode(spin, np);
6695 }
6696}
6697
6698/*
6699 * Free a wordnode_T for re-use later.
6700 * Only the "wn_child" field becomes invalid.
6701 */
6702 static void
6703free_wordnode(spin, n)
6704 spellinfo_T *spin;
6705 wordnode_T *n;
6706{
6707 n->wn_child = spin->si_first_free;
6708 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006709 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006710}
6711
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006712/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006713 * Compress a tree: find tails that are identical and can be shared.
6714 */
6715 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006716wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006717 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006718 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006719{
6720 hashtab_T ht;
6721 int n;
6722 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006723 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006724
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006725 /* Skip the root itself, it's not actually used. The first sibling is the
6726 * start of the tree. */
6727 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006728 {
6729 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006730 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006731
6732#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006733 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006734#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006735 {
6736 if (!spin->si_verbose)
6737 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006738 if (tot > 1000000)
6739 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006740 else if (tot == 0)
6741 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006742 else
6743 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006744 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006745 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006746 if (p_verbose > 2)
6747 verbose_leave();
6748 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006749#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006750 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006751#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006752 hash_clear(&ht);
6753 }
6754}
6755
6756/*
6757 * Compress a node, its siblings and its children, depth first.
6758 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006759 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006760 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006761node_compress(spin, node, ht, tot)
6762 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006763 wordnode_T *node;
6764 hashtab_T *ht;
6765 int *tot; /* total count of nodes before compressing,
6766 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006767{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768 wordnode_T *np;
6769 wordnode_T *tp;
6770 wordnode_T *child;
6771 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006772 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006773 int len = 0;
6774 unsigned nr, n;
6775 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006776
Bram Moolenaar51485f02005-06-04 21:55:20 +00006777 /*
6778 * Go through the list of siblings. Compress each child and then try
6779 * finding an identical child to replace it.
6780 * Note that with "child" we mean not just the node that is pointed to,
6781 * but the whole list of siblings, of which the node is the first.
6782 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006783 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006784 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006785 ++len;
6786 if ((child = np->wn_child) != NULL)
6787 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006788 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006789 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006790
6791 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006792 hash = hash_hash(child->wn_u1.hashkey);
6793 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006794 tp = NULL;
6795 if (!HASHITEM_EMPTY(hi))
6796 {
6797 /* There are children with an identical hash value. Now check
6798 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006799 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006800 if (node_equal(child, tp))
6801 {
6802 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006803 * current one. This means the current child and all
6804 * its siblings is unlinked from the tree. */
6805 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006806 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006807 np->wn_child = tp;
6808 ++compressed;
6809 break;
6810 }
6811 if (tp == NULL)
6812 {
6813 /* No other child with this hash value equals the child of
6814 * the node, add it to the linked list after the first
6815 * item. */
6816 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006817 child->wn_u2.next = tp->wn_u2.next;
6818 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006819 }
6820 }
6821 else
6822 /* No other child has this hash value, add it to the
6823 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006824 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006825 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006826 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006827 *tot += len;
6828
6829 /*
6830 * Make a hash key for the node and its siblings, so that we can quickly
6831 * find a lookalike node. This must be done after compressing the sibling
6832 * list, otherwise the hash key would become invalid by the compression.
6833 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006834 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006835 nr = 0;
6836 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006837 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006838 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006839 /* end node: use wn_flags, wn_region and wn_affixID */
6840 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006841 else
6842 /* byte node: use the byte value and the child pointer */
6843 n = np->wn_byte + ((long_u)np->wn_child << 8);
6844 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006845 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006846
6847 /* Avoid NUL bytes, it terminates the hash key. */
6848 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006849 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006850 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006851 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006852 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006853 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006854 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006855 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6856 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006857
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006858 /* Check for CTRL-C pressed now and then. */
6859 fast_breakcheck();
6860
Bram Moolenaar51485f02005-06-04 21:55:20 +00006861 return compressed;
6862}
6863
6864/*
6865 * Return TRUE when two nodes have identical siblings and children.
6866 */
6867 static int
6868node_equal(n1, n2)
6869 wordnode_T *n1;
6870 wordnode_T *n2;
6871{
6872 wordnode_T *p1;
6873 wordnode_T *p2;
6874
6875 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6876 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6877 if (p1->wn_byte != p2->wn_byte
6878 || (p1->wn_byte == NUL
6879 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006880 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006881 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006882 : (p1->wn_child != p2->wn_child)))
6883 break;
6884
6885 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006886}
6887
6888/*
6889 * Write a number to file "fd", MSB first, in "len" bytes.
6890 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006891 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006892put_bytes(fd, nr, len)
6893 FILE *fd;
6894 long_u nr;
6895 int len;
6896{
6897 int i;
6898
6899 for (i = len - 1; i >= 0; --i)
6900 putc((int)(nr >> (i * 8)), fd);
6901}
6902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006903static int
6904#ifdef __BORLANDC__
6905_RTLENTRYF
6906#endif
6907rep_compare __ARGS((const void *s1, const void *s2));
6908
6909/*
6910 * Function given to qsort() to sort the REP items on "from" string.
6911 */
6912 static int
6913#ifdef __BORLANDC__
6914_RTLENTRYF
6915#endif
6916rep_compare(s1, s2)
6917 const void *s1;
6918 const void *s2;
6919{
6920 fromto_T *p1 = (fromto_T *)s1;
6921 fromto_T *p2 = (fromto_T *)s2;
6922
6923 return STRCMP(p1->ft_from, p2->ft_from);
6924}
6925
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006926/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006927 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006928 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006929 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006930 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006931write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006932 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006933 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006934{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006935 FILE *fd;
6936 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006937 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006938 wordnode_T *tree;
6939 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006940 int i;
6941 int l;
6942 garray_T *gap;
6943 fromto_T *ftp;
6944 char_u *p;
6945 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006946 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006947
Bram Moolenaarb765d632005-06-07 21:00:02 +00006948 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006949 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006950 {
6951 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006952 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006953 }
6954
Bram Moolenaar5195e452005-08-19 20:32:47 +00006955 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006956 /* <fileID> */
6957 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006958 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006959 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006960 retval = FAIL;
6961 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006962 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006963
Bram Moolenaar5195e452005-08-19 20:32:47 +00006964 /*
6965 * <SECTIONS>: <section> ... <sectionend>
6966 */
6967
6968 /* SN_REGION: <regionname> ...
6969 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006970 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006971 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006972 putc(SN_REGION, fd); /* <sectionID> */
6973 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6974 l = spin->si_region_count * 2;
6975 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6976 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6977 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006978 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006979 }
6980 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006981 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006982
Bram Moolenaar5195e452005-08-19 20:32:47 +00006983 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6984 *
6985 * The table with character flags and the table for case folding.
6986 * This makes sure the same characters are recognized as word characters
6987 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006988 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006989 * 'encoding'.
6990 * Also skip this for an .add.spl file, the main spell file must contain
6991 * the table (avoids that it conflicts). File is shorter too.
6992 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006993 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006994 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006995 char_u folchars[128 * 8];
6996 int flags;
6997
Bram Moolenaard12a1322005-08-21 22:08:24 +00006998 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006999 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7000
7001 /* Form the <folchars> string first, we need to know its length. */
7002 l = 0;
7003 for (i = 128; i < 256; ++i)
7004 {
7005#ifdef FEAT_MBYTE
7006 if (has_mbyte)
7007 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7008 else
7009#endif
7010 folchars[l++] = spelltab.st_fold[i];
7011 }
7012 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7013
7014 fputc(128, fd); /* <charflagslen> */
7015 for (i = 128; i < 256; ++i)
7016 {
7017 flags = 0;
7018 if (spelltab.st_isw[i])
7019 flags |= CF_WORD;
7020 if (spelltab.st_isu[i])
7021 flags |= CF_UPPER;
7022 fputc(flags, fd); /* <charflags> */
7023 }
7024
7025 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7026 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007027 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007028
Bram Moolenaar5195e452005-08-19 20:32:47 +00007029 /* SN_MIDWORD: <midword> */
7030 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007031 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007032 putc(SN_MIDWORD, fd); /* <sectionID> */
7033 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7034
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007035 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007036 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007037 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7038 }
7039
Bram Moolenaar5195e452005-08-19 20:32:47 +00007040 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7041 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007042 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007043 putc(SN_PREFCOND, fd); /* <sectionID> */
7044 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7045
7046 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7047 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7048
7049 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007050 }
7051
Bram Moolenaar5195e452005-08-19 20:32:47 +00007052 /* SN_REP: <repcount> <rep> ...
7053 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007055 /* Sort the REP items. */
7056 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
7057 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007058
Bram Moolenaar5195e452005-08-19 20:32:47 +00007059 /* round 1: SN_REP section
7060 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007061 for (round = 1; round <= 2; ++round)
7062 {
7063 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007064 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007065 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007066 putc(SN_REP, fd); /* <sectionID> */
7067 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007068 else
7069 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007070 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7071 /* using SN_SOFO section instead of SN_SAL */
7072 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007073 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007074 putc(SN_SAL, fd); /* <sectionID> */
7075 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007076
Bram Moolenaar5195e452005-08-19 20:32:47 +00007077 /* This is for making suggestions, section is not required. */
7078 putc(0, fd); /* <sectionflags> */
7079
7080 /* Compute the length of what follows. */
7081 l = 2; /* count <repcount> or <salcount> */
7082 for (i = 0; i < gap->ga_len; ++i)
7083 {
7084 ftp = &((fromto_T *)gap->ga_data)[i];
7085 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7086 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7087 }
7088 if (round == 2)
7089 ++l; /* count <salflags> */
7090 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7091
7092 if (round == 2)
7093 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007094 i = 0;
7095 if (spin->si_followup)
7096 i |= SAL_F0LLOWUP;
7097 if (spin->si_collapse)
7098 i |= SAL_COLLAPSE;
7099 if (spin->si_rem_accents)
7100 i |= SAL_REM_ACCENTS;
7101 putc(i, fd); /* <salflags> */
7102 }
7103
7104 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7105 for (i = 0; i < gap->ga_len; ++i)
7106 {
7107 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7108 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7109 ftp = &((fromto_T *)gap->ga_data)[i];
7110 for (rr = 1; rr <= 2; ++rr)
7111 {
7112 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7113 l = STRLEN(p);
7114 putc(l, fd);
7115 fwrite(p, l, (size_t)1, fd);
7116 }
7117 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007119 }
7120
Bram Moolenaar5195e452005-08-19 20:32:47 +00007121 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7122 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007123 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7124 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007125 putc(SN_SOFO, fd); /* <sectionID> */
7126 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007127
7128 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007129 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7130 /* <sectionlen> */
7131
7132 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7133 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007134
7135 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007136 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7137 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007138 }
7139
Bram Moolenaar5195e452005-08-19 20:32:47 +00007140 /* SN_MAP: <mapstr>
7141 * This is for making suggestions, section is not required. */
7142 if (spin->si_map.ga_len > 0)
7143 {
7144 putc(SN_MAP, fd); /* <sectionID> */
7145 putc(0, fd); /* <sectionflags> */
7146 l = spin->si_map.ga_len;
7147 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7148 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7149 /* <mapstr> */
7150 }
7151
7152 /* SN_COMPOUND: compound info.
7153 * We don't mark it required, when not supported all compound words will
7154 * be bad words. */
7155 if (spin->si_compflags != NULL)
7156 {
7157 putc(SN_COMPOUND, fd); /* <sectionID> */
7158 putc(0, fd); /* <sectionflags> */
7159
7160 l = STRLEN(spin->si_compflags);
7161 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7162 putc(spin->si_compmax, fd); /* <compmax> */
7163 putc(spin->si_compminlen, fd); /* <compminlen> */
7164 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7165 /* <compflags> */
7166 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7167 }
7168
Bram Moolenaar78622822005-08-23 21:00:13 +00007169 /* SN_NOBREAK: NOBREAK flag */
7170 if (spin->si_nobreak)
7171 {
7172 putc(SN_NOBREAK, fd); /* <sectionID> */
7173 putc(0, fd); /* <sectionflags> */
7174
7175 /* It's empty, the precense of the section flags the feature. */
7176 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7177 }
7178
Bram Moolenaar5195e452005-08-19 20:32:47 +00007179 /* SN_SYLLABLE: syllable info.
7180 * We don't mark it required, when not supported syllables will not be
7181 * counted. */
7182 if (spin->si_syllable != NULL)
7183 {
7184 putc(SN_SYLLABLE, fd); /* <sectionID> */
7185 putc(0, fd); /* <sectionflags> */
7186
7187 l = STRLEN(spin->si_syllable);
7188 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7189 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7190 }
7191
7192 /* end of <SECTIONS> */
7193 putc(SN_END, fd); /* <sectionend> */
7194
Bram Moolenaar50cde822005-06-05 21:54:54 +00007195
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007196 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007197 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007198 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007199 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007200 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007201 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007202 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007203 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007204 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007205 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007206 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007207 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007208
Bram Moolenaar0c405862005-06-22 22:26:26 +00007209 /* Clear the index and wnode fields in the tree. */
7210 clear_node(tree);
7211
Bram Moolenaar51485f02005-06-04 21:55:20 +00007212 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007213 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007214 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007215 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007216
Bram Moolenaar51485f02005-06-04 21:55:20 +00007217 /* number of nodes in 4 bytes */
7218 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007219 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007220
Bram Moolenaar51485f02005-06-04 21:55:20 +00007221 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007222 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007223 }
7224
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007225 /* Write another byte to check for errors. */
7226 if (putc(0, fd) == EOF)
7227 retval = FAIL;
7228
7229 if (fclose(fd) == EOF)
7230 retval = FAIL;
7231
7232 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007233}
7234
7235/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007236 * Clear the index and wnode fields of "node", it siblings and its
7237 * children. This is needed because they are a union with other items to save
7238 * space.
7239 */
7240 static void
7241clear_node(node)
7242 wordnode_T *node;
7243{
7244 wordnode_T *np;
7245
7246 if (node != NULL)
7247 for (np = node; np != NULL; np = np->wn_sibling)
7248 {
7249 np->wn_u1.index = 0;
7250 np->wn_u2.wnode = NULL;
7251
7252 if (np->wn_byte != NUL)
7253 clear_node(np->wn_child);
7254 }
7255}
7256
7257
7258/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007259 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007260 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007261 * This first writes the list of possible bytes (siblings). Then for each
7262 * byte recursively write the children.
7263 *
7264 * NOTE: The code here must match the code in read_tree(), since assumptions
7265 * are made about the indexes (so that we don't have to write them in the
7266 * file).
7267 *
7268 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007269 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007270 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007271put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007272 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007273 wordnode_T *node;
7274 int index;
7275 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007276 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007277{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007278 int newindex = index;
7279 int siblingcount = 0;
7280 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007281 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007282
Bram Moolenaar51485f02005-06-04 21:55:20 +00007283 /* If "node" is zero the tree is empty. */
7284 if (node == NULL)
7285 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007286
Bram Moolenaar51485f02005-06-04 21:55:20 +00007287 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007288 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007289
7290 /* Count the number of siblings. */
7291 for (np = node; np != NULL; np = np->wn_sibling)
7292 ++siblingcount;
7293
7294 /* Write the sibling count. */
7295 if (fd != NULL)
7296 putc(siblingcount, fd); /* <siblingcount> */
7297
7298 /* Write each sibling byte and optionally extra info. */
7299 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007300 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007301 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007302 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007303 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007304 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007305 /* For a NUL byte (end of word) write the flags etc. */
7306 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007307 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007308 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007309 * associated condition nr (stored in wn_region). The
7310 * byte value is misused to store the "rare" and "not
7311 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007312 if (np->wn_flags == (short_u)PFX_FLAGS)
7313 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007314 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007315 {
7316 putc(BY_FLAGS, fd); /* <byte> */
7317 putc(np->wn_flags, fd); /* <pflags> */
7318 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007319 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007320 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007321 }
7322 else
7323 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007324 /* For word trees we write the flag/region items. */
7325 flags = np->wn_flags;
7326 if (regionmask != 0 && np->wn_region != regionmask)
7327 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007328 if (np->wn_affixID != 0)
7329 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007330 if (flags == 0)
7331 {
7332 /* word without flags or region */
7333 putc(BY_NOFLAGS, fd); /* <byte> */
7334 }
7335 else
7336 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007337 if (np->wn_flags >= 0x100)
7338 {
7339 putc(BY_FLAGS2, fd); /* <byte> */
7340 putc(flags, fd); /* <flags> */
7341 putc((unsigned)flags >> 8, fd); /* <flags2> */
7342 }
7343 else
7344 {
7345 putc(BY_FLAGS, fd); /* <byte> */
7346 putc(flags, fd); /* <flags> */
7347 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007348 if (flags & WF_REGION)
7349 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007350 if (flags & WF_AFX)
7351 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007352 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007353 }
7354 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007355 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007356 else
7357 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007358 if (np->wn_child->wn_u1.index != 0
7359 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007360 {
7361 /* The child is written elsewhere, write the reference. */
7362 if (fd != NULL)
7363 {
7364 putc(BY_INDEX, fd); /* <byte> */
7365 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007366 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007367 }
7368 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007369 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007370 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007371 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007372
Bram Moolenaar51485f02005-06-04 21:55:20 +00007373 if (fd != NULL)
7374 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7375 {
7376 EMSG(_(e_write));
7377 return 0;
7378 }
7379 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007380 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007381
7382 /* Space used in the array when reading: one for each sibling and one for
7383 * the count. */
7384 newindex += siblingcount + 1;
7385
7386 /* Recursively dump the children of each sibling. */
7387 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007388 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7389 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007390 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007391
7392 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007393}
7394
7395
7396/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007397 * ":mkspell [-ascii] outfile infile ..."
7398 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007399 */
7400 void
7401ex_mkspell(eap)
7402 exarg_T *eap;
7403{
7404 int fcount;
7405 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007406 char_u *arg = eap->arg;
7407 int ascii = FALSE;
7408
7409 if (STRNCMP(arg, "-ascii", 6) == 0)
7410 {
7411 ascii = TRUE;
7412 arg = skipwhite(arg + 6);
7413 }
7414
7415 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7416 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007418 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007419 FreeWild(fcount, fnames);
7420 }
7421}
7422
7423/*
7424 * Create a Vim spell file from one or more word lists.
7425 * "fnames[0]" is the output file name.
7426 * "fnames[fcount - 1]" is the last input file name.
7427 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7428 * and ".spl" is appended to make the output file name.
7429 */
7430 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007431mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007432 int fcount;
7433 char_u **fnames;
7434 int ascii; /* -ascii argument given */
7435 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007436 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007437{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007438 char_u fname[MAXPATHL];
7439 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007440 char_u **innames;
7441 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007442 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007443 int i;
7444 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007445 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007446 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007447 spellinfo_T spin;
7448
7449 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007450 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007451 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007452 spin.si_followup = TRUE;
7453 spin.si_rem_accents = TRUE;
7454 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7455 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7456 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007457 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007458 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007459
Bram Moolenaarb765d632005-06-07 21:00:02 +00007460 /* default: fnames[0] is output file, following are input files */
7461 innames = &fnames[1];
7462 incount = fcount - 1;
7463
7464 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007465 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007466 len = STRLEN(fnames[0]);
7467 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7468 {
7469 /* For ":mkspell path/en.latin1.add" output file is
7470 * "path/en.latin1.add.spl". */
7471 innames = &fnames[0];
7472 incount = 1;
7473 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7474 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007475 else if (fcount == 1)
7476 {
7477 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7478 innames = &fnames[0];
7479 incount = 1;
7480 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7481 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7482 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007483 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7484 {
7485 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007486 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007487 }
7488 else
7489 /* Name should be language, make the file name from it. */
7490 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7491 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7492
7493 /* Check for .ascii.spl. */
7494 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7495 spin.si_ascii = TRUE;
7496
7497 /* Check for .add.spl. */
7498 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7499 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007500 }
7501
Bram Moolenaarb765d632005-06-07 21:00:02 +00007502 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007503 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007504 else if (vim_strchr(gettail(wfname), '_') != NULL)
7505 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007506 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007507 EMSG(_("E754: Only up to 8 regions supported"));
7508 else
7509 {
7510 /* Check for overwriting before doing things that may take a lot of
7511 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007512 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007513 {
7514 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007515 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007516 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007517 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007518 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007519 EMSG2(_(e_isadir2), wfname);
7520 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007521 }
7522
7523 /*
7524 * Init the aff and dic pointers.
7525 * Get the region names if there are more than 2 arguments.
7526 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007527 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007528 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007529 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007530
Bram Moolenaar3982c542005-06-08 21:56:31 +00007531 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007532 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007533 len = STRLEN(innames[i]);
7534 if (STRLEN(gettail(innames[i])) < 5
7535 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007536 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007537 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7538 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007539 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007540 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7541 spin.si_region_name[i * 2 + 1] =
7542 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007543 }
7544 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007545 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007546
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007547 spin.si_foldroot = wordtree_alloc(&spin);
7548 spin.si_keeproot = wordtree_alloc(&spin);
7549 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007550 if (spin.si_foldroot == NULL
7551 || spin.si_keeproot == NULL
7552 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007553 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007554 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007555 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007556 }
7557
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007558 /* When not producing a .add.spl file clear the character table when
7559 * we encounter one in the .aff file. This means we dump the current
7560 * one in the .spl file if the .aff file doesn't define one. That's
7561 * better than guessing the contents, the table will match a
7562 * previously loaded spell file. */
7563 if (!spin.si_add)
7564 spin.si_clear_chartab = TRUE;
7565
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007566 /*
7567 * Read all the .aff and .dic files.
7568 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007569 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007570 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007571 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007572 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007573 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007574 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007575
Bram Moolenaarb765d632005-06-07 21:00:02 +00007576 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007577 if (mch_stat((char *)fname, &st) >= 0)
7578 {
7579 /* Read the .aff file. Will init "spin->si_conv" based on the
7580 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007581 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007582 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007583 error = TRUE;
7584 else
7585 {
7586 /* Read the .dic file and store the words in the trees. */
7587 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007588 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007589 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007590 error = TRUE;
7591 }
7592 }
7593 else
7594 {
7595 /* No .aff file, try reading the file as a word list. Store
7596 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007597 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007598 error = TRUE;
7599 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007600
Bram Moolenaarb765d632005-06-07 21:00:02 +00007601#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007602 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007603 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007604#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007605 }
7606
Bram Moolenaar78622822005-08-23 21:00:13 +00007607 if (spin.si_compflags != NULL && spin.si_nobreak)
7608 MSG(_("Warning: both compounding and NOBREAK specified"));
7609
Bram Moolenaar51485f02005-06-04 21:55:20 +00007610 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007611 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007612 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007613 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007614 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007615 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007616 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007617 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007618 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007619 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007620 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007621 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007622 verbose_leave();
7623 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007624 wordtree_compress(&spin, spin.si_foldroot);
7625 wordtree_compress(&spin, spin.si_keeproot);
7626 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007627 }
7628
Bram Moolenaar51485f02005-06-04 21:55:20 +00007629 if (!error)
7630 {
7631 /*
7632 * Write the info in the spell file.
7633 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007634 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007635 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007636 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007637 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007638 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007639 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007640 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007641 verbose_leave();
7642 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007643
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007644 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007645
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007646 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007647 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007648 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007649 verbose_enter();
7650 MSG(_("Done!"));
7651 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007652 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007653 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007654 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007655 verbose_leave();
7656 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007657
Bram Moolenaarb765d632005-06-07 21:00:02 +00007658 /* If the file is loaded need to reload it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007659 if (!error)
7660 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007661 }
7662
7663 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007664 ga_clear(&spin.si_rep);
7665 ga_clear(&spin.si_sal);
7666 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007667 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007668
7669 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007670 for (i = 0; i < incount; ++i)
7671 if (afile[i] != NULL)
7672 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007673
7674 /* Free all the bits and pieces at once. */
7675 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007677}
7678
Bram Moolenaarb765d632005-06-07 21:00:02 +00007679
7680/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007681 * ":[count]spellgood {word}"
7682 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007683 */
7684 void
7685ex_spell(eap)
7686 exarg_T *eap;
7687{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007688 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007689 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007690}
7691
7692/*
7693 * Add "word[len]" to 'spellfile' as a good or bad word.
7694 */
7695 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007696spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007697 char_u *word;
7698 int len;
7699 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007700 int index; /* "zG" and "zW": zero, otherwise index in
7701 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007702{
7703 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007704 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007705 int new_spf = FALSE;
7706 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007707 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007708 char_u fnamebuf[MAXPATHL];
7709 char_u line[MAXWLEN * 2];
7710 long fpos, fpos_next = 0;
7711 int i;
7712 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007713
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007714 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007715 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007716 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007717 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007718 int_wordlist = vim_tempname('s');
7719 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007720 return;
7721 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007722 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007723 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007724 else
7725 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007726 /* If 'spellfile' isn't set figure out a good default value. */
7727 if (*curbuf->b_p_spf == NUL)
7728 {
7729 init_spellfile();
7730 new_spf = TRUE;
7731 }
7732
7733 if (*curbuf->b_p_spf == NUL)
7734 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007735 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00007736 return;
7737 }
7738
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007739 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7740 {
7741 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7742 if (i == index)
7743 break;
7744 if (*spf == NUL)
7745 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00007746 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007747 return;
7748 }
7749 }
7750
Bram Moolenaarb765d632005-06-07 21:00:02 +00007751 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007752 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007753 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7754 buf = NULL;
7755 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007756 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007757 EMSG(_(e_bufloaded));
7758 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007759 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007760
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007761 fname = fnamebuf;
7762 }
7763
7764 if (bad)
7765 {
7766 /* When the word also appears as good word we need to remove that one,
7767 * since its flags sort before the one with WF_BANNED. */
7768 fd = mch_fopen((char *)fname, "r");
7769 if (fd != NULL)
7770 {
7771 while (!vim_fgets(line, MAXWLEN * 2, fd))
7772 {
7773 fpos = fpos_next;
7774 fpos_next = ftell(fd);
7775 if (STRNCMP(word, line, len) == 0
7776 && (line[len] == '/' || line[len] < ' '))
7777 {
7778 /* Found duplicate word. Remove it by writing a '#' at
7779 * the start of the line. Mixing reading and writing
7780 * doesn't work for all systems, close the file first. */
7781 fclose(fd);
7782 fd = mch_fopen((char *)fname, "r+");
7783 if (fd == NULL)
7784 break;
7785 if (fseek(fd, fpos, SEEK_SET) == 0)
7786 fputc('#', fd);
7787 fseek(fd, fpos_next, SEEK_SET);
7788 }
7789 }
7790 fclose(fd);
7791 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007792 }
7793
7794 fd = mch_fopen((char *)fname, "a");
7795 if (fd == NULL && new_spf)
7796 {
7797 /* We just initialized the 'spellfile' option and can't open the file.
7798 * We may need to create the "spell" directory first. We already
7799 * checked the runtime directory is writable in init_spellfile(). */
7800 STRCPY(NameBuff, fname);
7801 *gettail_sep(NameBuff) = NUL;
7802 if (mch_stat((char *)NameBuff, &st) < 0)
7803 {
7804 /* The directory doesn't exist. Try creating it and opening the
7805 * file again. */
7806 vim_mkdir(NameBuff, 0755);
7807 fd = mch_fopen((char *)fname, "a");
7808 }
7809 }
7810
7811 if (fd == NULL)
7812 EMSG2(_(e_notopen), fname);
7813 else
7814 {
7815 if (bad)
7816 fprintf(fd, "%.*s/!\n", len, word);
7817 else
7818 fprintf(fd, "%.*s\n", len, word);
7819 fclose(fd);
7820
7821 /* Update the .add.spl file. */
7822 mkspell(1, &fname, FALSE, TRUE, TRUE);
7823
7824 /* If the .add file is edited somewhere, reload it. */
7825 if (buf != NULL)
7826 buf_reload(buf);
7827
7828 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007829 }
7830}
7831
7832/*
7833 * Initialize 'spellfile' for the current buffer.
7834 */
7835 static void
7836init_spellfile()
7837{
7838 char_u buf[MAXPATHL];
7839 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007840 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007841 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007842 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007843 int aspath = FALSE;
7844 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007845
7846 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7847 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007848 /* Find the end of the language name. Exclude the region. If there
7849 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007850 for (lend = curbuf->b_p_spl; *lend != NUL
7851 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007852 if (vim_ispathsep(*lend))
7853 {
7854 aspath = TRUE;
7855 lstart = lend + 1;
7856 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007857
7858 /* Loop over all entries in 'runtimepath'. Use the first one where we
7859 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007860 rtp = p_rtp;
7861 while (*rtp != NUL)
7862 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007863 if (aspath)
7864 /* Use directory of an entry with path, e.g., for
7865 * "/dir/lg.utf-8.spl" use "/dir". */
7866 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
7867 else
7868 /* Copy the path from 'runtimepath' to buf[]. */
7869 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00007870 if (filewritable(buf) == 2)
7871 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007872 /* Use the first language name from 'spelllang' and the
7873 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007874 if (aspath)
7875 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
7876 else
7877 {
7878 l = STRLEN(buf);
7879 vim_snprintf((char *)buf + l, MAXPATHL - l,
7880 "/spell/%.*s", (int)(lend - lstart), lstart);
7881 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007882 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007883 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
7884 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
7885 fname != NULL
7886 && strstr((char *)gettail(fname), ".ascii.") != NULL
7887 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00007888 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7889 break;
7890 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007891 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007892 }
7893 }
7894}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007895
Bram Moolenaar51485f02005-06-04 21:55:20 +00007896
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007897/*
7898 * Init the chartab used for spelling for ASCII.
7899 * EBCDIC is not supported!
7900 */
7901 static void
7902clear_spell_chartab(sp)
7903 spelltab_T *sp;
7904{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007905 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007906
7907 /* Init everything to FALSE. */
7908 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7909 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7910 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007911 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007912 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007913 sp->st_upper[i] = i;
7914 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007915
7916 /* We include digits. A word shouldn't start with a digit, but handling
7917 * that is done separately. */
7918 for (i = '0'; i <= '9'; ++i)
7919 sp->st_isw[i] = TRUE;
7920 for (i = 'A'; i <= 'Z'; ++i)
7921 {
7922 sp->st_isw[i] = TRUE;
7923 sp->st_isu[i] = TRUE;
7924 sp->st_fold[i] = i + 0x20;
7925 }
7926 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007927 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007928 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007929 sp->st_upper[i] = i - 0x20;
7930 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007931}
7932
7933/*
7934 * Init the chartab used for spelling. Only depends on 'encoding'.
7935 * Called once while starting up and when 'encoding' changes.
7936 * The default is to use isalpha(), but the spell file should define the word
7937 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007938 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007939 */
7940 void
7941init_spell_chartab()
7942{
7943 int i;
7944
7945 did_set_spelltab = FALSE;
7946 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007947#ifdef FEAT_MBYTE
7948 if (enc_dbcs)
7949 {
7950 /* DBCS: assume double-wide characters are word characters. */
7951 for (i = 128; i <= 255; ++i)
7952 if (MB_BYTE2LEN(i) == 2)
7953 spelltab.st_isw[i] = TRUE;
7954 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007955 else if (enc_utf8)
7956 {
7957 for (i = 128; i < 256; ++i)
7958 {
7959 spelltab.st_isu[i] = utf_isupper(i);
7960 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7961 spelltab.st_fold[i] = utf_fold(i);
7962 spelltab.st_upper[i] = utf_toupper(i);
7963 }
7964 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007965 else
7966#endif
7967 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007968 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007969 for (i = 128; i < 256; ++i)
7970 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007971 if (MB_ISUPPER(i))
7972 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007973 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007974 spelltab.st_isu[i] = TRUE;
7975 spelltab.st_fold[i] = MB_TOLOWER(i);
7976 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007977 else if (MB_ISLOWER(i))
7978 {
7979 spelltab.st_isw[i] = TRUE;
7980 spelltab.st_upper[i] = MB_TOUPPER(i);
7981 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007982 }
7983 }
7984}
7985
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007986/*
7987 * Set the spell character tables from strings in the affix file.
7988 */
7989 static int
7990set_spell_chartab(fol, low, upp)
7991 char_u *fol;
7992 char_u *low;
7993 char_u *upp;
7994{
7995 /* We build the new tables here first, so that we can compare with the
7996 * previous one. */
7997 spelltab_T new_st;
7998 char_u *pf = fol, *pl = low, *pu = upp;
7999 int f, l, u;
8000
8001 clear_spell_chartab(&new_st);
8002
8003 while (*pf != NUL)
8004 {
8005 if (*pl == NUL || *pu == NUL)
8006 {
8007 EMSG(_(e_affform));
8008 return FAIL;
8009 }
8010#ifdef FEAT_MBYTE
8011 f = mb_ptr2char_adv(&pf);
8012 l = mb_ptr2char_adv(&pl);
8013 u = mb_ptr2char_adv(&pu);
8014#else
8015 f = *pf++;
8016 l = *pl++;
8017 u = *pu++;
8018#endif
8019 /* Every character that appears is a word character. */
8020 if (f < 256)
8021 new_st.st_isw[f] = TRUE;
8022 if (l < 256)
8023 new_st.st_isw[l] = TRUE;
8024 if (u < 256)
8025 new_st.st_isw[u] = TRUE;
8026
8027 /* if "LOW" and "FOL" are not the same the "LOW" char needs
8028 * case-folding */
8029 if (l < 256 && l != f)
8030 {
8031 if (f >= 256)
8032 {
8033 EMSG(_(e_affrange));
8034 return FAIL;
8035 }
8036 new_st.st_fold[l] = f;
8037 }
8038
8039 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008040 * case-folding, it's upper case and the "UPP" is the upper case of
8041 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008042 if (u < 256 && u != f)
8043 {
8044 if (f >= 256)
8045 {
8046 EMSG(_(e_affrange));
8047 return FAIL;
8048 }
8049 new_st.st_fold[u] = f;
8050 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008051 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008052 }
8053 }
8054
8055 if (*pl != NUL || *pu != NUL)
8056 {
8057 EMSG(_(e_affform));
8058 return FAIL;
8059 }
8060
8061 return set_spell_finish(&new_st);
8062}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008063
8064/*
8065 * Set the spell character tables from strings in the .spl file.
8066 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008067 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008068set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008069 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008070 int cnt; /* length of "flags" */
8071 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008072{
8073 /* We build the new tables here first, so that we can compare with the
8074 * previous one. */
8075 spelltab_T new_st;
8076 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008077 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008078 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008079
8080 clear_spell_chartab(&new_st);
8081
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008082 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008083 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008084 if (i < cnt)
8085 {
8086 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
8087 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
8088 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008089
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008090 if (*p != NUL)
8091 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008092#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008093 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008094#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008095 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008096#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008097 new_st.st_fold[i + 128] = c;
8098 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
8099 new_st.st_upper[c] = i + 128;
8100 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008101 }
8102
Bram Moolenaar5195e452005-08-19 20:32:47 +00008103 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008104}
8105
8106 static int
8107set_spell_finish(new_st)
8108 spelltab_T *new_st;
8109{
8110 int i;
8111
8112 if (did_set_spelltab)
8113 {
8114 /* check that it's the same table */
8115 for (i = 0; i < 256; ++i)
8116 {
8117 if (spelltab.st_isw[i] != new_st->st_isw[i]
8118 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008119 || spelltab.st_fold[i] != new_st->st_fold[i]
8120 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008121 {
8122 EMSG(_("E763: Word characters differ between spell files"));
8123 return FAIL;
8124 }
8125 }
8126 }
8127 else
8128 {
8129 /* copy the new spelltab into the one being used */
8130 spelltab = *new_st;
8131 did_set_spelltab = TRUE;
8132 }
8133
8134 return OK;
8135}
8136
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008137/*
Bram Moolenaarea408852005-06-25 22:49:46 +00008138 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008139 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00008140 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008141 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00008142 */
8143 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008144spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00008145 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008146 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00008147{
Bram Moolenaarea408852005-06-25 22:49:46 +00008148#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008149 char_u *s;
8150 int l;
8151 int c;
8152
8153 if (has_mbyte)
8154 {
8155 l = MB_BYTE2LEN(*p);
8156 s = p;
8157 if (l == 1)
8158 {
8159 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008160 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008161 {
8162 s = p + 1; /* skip a mid-word character */
8163 l = MB_BYTE2LEN(*s);
8164 }
8165 }
8166 else
8167 {
8168 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008169 if (c < 256 ? buf->b_spell_ismw[c]
8170 : (buf->b_spell_ismw_mb != NULL
8171 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008172 {
8173 s = p + l;
8174 l = MB_BYTE2LEN(*s);
8175 }
8176 }
8177
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008178 c = mb_ptr2char(s);
8179 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008180 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008181 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008182 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008183#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008184
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008185 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
8186}
8187
8188/*
8189 * Return TRUE if "p" points to a word character.
8190 * Unlike spell_iswordp() this doesn't check for "midword" characters.
8191 */
8192 static int
8193spell_iswordp_nmw(p)
8194 char_u *p;
8195{
8196#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008197 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008198
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008199 if (has_mbyte)
8200 {
8201 c = mb_ptr2char(p);
8202 if (c > 255)
8203 return mb_get_class(p) >= 2;
8204 return spelltab.st_isw[c];
8205 }
8206#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008207 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00008208}
8209
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008210#ifdef FEAT_MBYTE
8211/*
8212 * Return TRUE if "p" points to a word character.
8213 * Wide version of spell_iswordp().
8214 */
8215 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008216spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008217 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008218 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008219{
8220 int *s;
8221
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008222 if (*p < 256 ? buf->b_spell_ismw[*p]
8223 : (buf->b_spell_ismw_mb != NULL
8224 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008225 s = p + 1;
8226 else
8227 s = p;
8228
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008229 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008230 {
8231 if (enc_utf8)
8232 return utf_class(*s) >= 2;
8233 if (enc_dbcs)
8234 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
8235 return 0;
8236 }
8237 return spelltab.st_isw[*s];
8238}
8239#endif
8240
Bram Moolenaarea408852005-06-25 22:49:46 +00008241/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008242 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008243 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008244 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008245 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008246write_spell_prefcond(fd, gap)
8247 FILE *fd;
8248 garray_T *gap;
8249{
8250 int i;
8251 char_u *p;
8252 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008253 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008254
Bram Moolenaar5195e452005-08-19 20:32:47 +00008255 if (fd != NULL)
8256 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8257
8258 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008259
8260 for (i = 0; i < gap->ga_len; ++i)
8261 {
8262 /* <prefcond> : <condlen> <condstr> */
8263 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008264 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008265 {
8266 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008267 if (fd != NULL)
8268 {
8269 fputc(len, fd);
8270 fwrite(p, (size_t)len, (size_t)1, fd);
8271 }
8272 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008273 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008274 else if (fd != NULL)
8275 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008276 }
8277
Bram Moolenaar5195e452005-08-19 20:32:47 +00008278 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008279}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008280
8281/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008282 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8283 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008284 * When using a multi-byte 'encoding' the length may change!
8285 * Returns FAIL when something wrong.
8286 */
8287 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008288spell_casefold(str, len, buf, buflen)
8289 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008290 int len;
8291 char_u *buf;
8292 int buflen;
8293{
8294 int i;
8295
8296 if (len >= buflen)
8297 {
8298 buf[0] = NUL;
8299 return FAIL; /* result will not fit */
8300 }
8301
8302#ifdef FEAT_MBYTE
8303 if (has_mbyte)
8304 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008305 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008306 char_u *p;
8307 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008308
8309 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008310 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008311 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008312 if (outi + MB_MAXBYTES > buflen)
8313 {
8314 buf[outi] = NUL;
8315 return FAIL;
8316 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008317 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008318 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008319 }
8320 buf[outi] = NUL;
8321 }
8322 else
8323#endif
8324 {
8325 /* Be quick for non-multibyte encodings. */
8326 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008327 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008328 buf[i] = NUL;
8329 }
8330
8331 return OK;
8332}
8333
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008334#define SPS_BEST 1
8335#define SPS_FAST 2
8336#define SPS_DOUBLE 4
8337
8338static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008339static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008340
8341/*
8342 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008343 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008344 */
8345 int
8346spell_check_sps()
8347{
8348 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008349 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008350 char_u buf[MAXPATHL];
8351 int f;
8352
8353 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008354 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008355
8356 for (p = p_sps; *p != NUL; )
8357 {
8358 copy_option_part(&p, buf, MAXPATHL, ",");
8359
8360 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008361 if (VIM_ISDIGIT(*buf))
8362 {
8363 s = buf;
8364 sps_limit = getdigits(&s);
8365 if (*s != NUL && !VIM_ISDIGIT(*s))
8366 f = -1;
8367 }
8368 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008369 f = SPS_BEST;
8370 else if (STRCMP(buf, "fast") == 0)
8371 f = SPS_FAST;
8372 else if (STRCMP(buf, "double") == 0)
8373 f = SPS_DOUBLE;
8374 else if (STRNCMP(buf, "expr:", 5) != 0
8375 && STRNCMP(buf, "file:", 5) != 0)
8376 f = -1;
8377
8378 if (f == -1 || (sps_flags != 0 && f != 0))
8379 {
8380 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008381 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008382 return FAIL;
8383 }
8384 if (f != 0)
8385 sps_flags = f;
8386 }
8387
8388 if (sps_flags == 0)
8389 sps_flags = SPS_BEST;
8390
8391 return OK;
8392}
8393
8394/* Remember what "z?" replaced. */
8395static char_u *repl_from = NULL;
8396static char_u *repl_to = NULL;
8397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008398/*
8399 * "z?": Find badly spelled word under or after the cursor.
8400 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008401 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008402 */
8403 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008404spell_suggest(count)
8405 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008406{
8407 char_u *line;
8408 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409 char_u wcopy[MAXWLEN + 2];
8410 char_u *p;
8411 int i;
8412 int c;
8413 suginfo_T sug;
8414 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008415 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008416 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008417 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008418 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008419
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008420 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008421 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008422 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008423 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008424 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8425 return;
8426
8427 /* No bad word or it starts after the cursor: use the word under the
8428 * cursor. */
8429 curwin->w_cursor = prev_cursor;
8430 line = ml_get_curline();
8431 p = line + curwin->w_cursor.col;
8432 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008433 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008434 mb_ptr_back(line, p);
8435 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008436 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008437 mb_ptr_adv(p);
8438
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008439 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008440 {
8441 beep_flush();
8442 return;
8443 }
8444 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008445 }
8446
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008447 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008449 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008450 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008451
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008452 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008453
Bram Moolenaar5195e452005-08-19 20:32:47 +00008454 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8455 * 'spellsuggest', whatever is smaller. */
8456 if (sps_limit > (int)Rows - 2)
8457 limit = (int)Rows - 2;
8458 else
8459 limit = sps_limit;
8460 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008461 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462
8463 if (sug.su_ga.ga_len == 0)
8464 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008465 else if (count > 0)
8466 {
8467 if (count > sug.su_ga.ga_len)
8468 smsg((char_u *)_("Sorry, only %ld suggestions"),
8469 (long)sug.su_ga.ga_len);
8470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008471 else
8472 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008473 vim_free(repl_from);
8474 repl_from = NULL;
8475 vim_free(repl_to);
8476 repl_to = NULL;
8477
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008478#ifdef FEAT_RIGHTLEFT
8479 /* When 'rightleft' is set the list is drawn right-left. */
8480 cmdmsg_rl = curwin->w_p_rl;
8481 if (cmdmsg_rl)
8482 msg_col = Columns - 1;
8483#endif
8484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008485 /* List the suggestions. */
8486 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008487 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008488 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8489 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008490#ifdef FEAT_RIGHTLEFT
8491 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8492 {
8493 /* And now the rabbit from the high hat: Avoid showing the
8494 * untranslated message rightleft. */
8495 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8496 sug.su_badlen, sug.su_badptr);
8497 }
8498#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008499 msg_puts(IObuff);
8500 msg_clr_eos();
8501 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008503 msg_scroll = TRUE;
8504 for (i = 0; i < sug.su_ga.ga_len; ++i)
8505 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008506 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008507
8508 /* The suggested word may replace only part of the bad word, add
8509 * the not replaced part. */
8510 STRCPY(wcopy, stp->st_word);
8511 if (sug.su_badlen > stp->st_orglen)
8512 vim_strncpy(wcopy + STRLEN(wcopy),
8513 sug.su_badptr + stp->st_orglen,
8514 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008515 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8516#ifdef FEAT_RIGHTLEFT
8517 if (cmdmsg_rl)
8518 rl_mirror(IObuff);
8519#endif
8520 msg_puts(IObuff);
8521
8522 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008523 msg_puts(IObuff);
8524
8525 /* The word may replace more than "su_badlen". */
8526 if (sug.su_badlen < stp->st_orglen)
8527 {
8528 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8529 stp->st_orglen, sug.su_badptr);
8530 msg_puts(IObuff);
8531 }
8532
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008533 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008534 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008535 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008536 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008537 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008538 stp->st_salscore ? "s " : "",
8539 stp->st_score, stp->st_altscore);
8540 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008541 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008542 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008543#ifdef FEAT_RIGHTLEFT
8544 if (cmdmsg_rl)
8545 /* Mirror the numbers, but keep the leading space. */
8546 rl_mirror(IObuff + 1);
8547#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008548 msg_advance(30);
8549 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008550 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008551 msg_putchar('\n');
8552 }
8553
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008554#ifdef FEAT_RIGHTLEFT
8555 cmdmsg_rl = FALSE;
8556 msg_col = 0;
8557#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008558 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008559 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008560 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008561 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 }
8563
Bram Moolenaard12a1322005-08-21 22:08:24 +00008564 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8565 {
8566 /* Save the from and to text for :spellrepall. */
8567 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00008568 if (sug.su_badlen > stp->st_orglen)
8569 {
8570 /* Replacing less than "su_badlen", append the remainder to
8571 * repl_to. */
8572 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
8573 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
8574 sug.su_badlen - stp->st_orglen,
8575 sug.su_badptr + stp->st_orglen);
8576 repl_to = vim_strsave(IObuff);
8577 }
8578 else
8579 {
8580 /* Replacing su_badlen or more, use the whole word. */
8581 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8582 repl_to = vim_strsave(stp->st_word);
8583 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008584
8585 /* Replace the word. */
8586 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8587 if (p != NULL)
8588 {
8589 c = sug.su_badptr - line;
8590 mch_memmove(p, line, c);
8591 STRCPY(p + c, stp->st_word);
8592 STRCAT(p, sug.su_badptr + stp->st_orglen);
8593 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8594 curwin->w_cursor.col = c;
8595 changed_bytes(curwin->w_cursor.lnum, c);
8596
8597 /* For redo we use a change-word command. */
8598 ResetRedobuff();
8599 AppendToRedobuff((char_u *)"ciw");
8600 AppendToRedobuff(stp->st_word);
8601 AppendCharToRedobuff(ESC);
8602 }
8603 }
8604 else
8605 curwin->w_cursor = prev_cursor;
8606
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008607 spell_find_cleanup(&sug);
8608}
8609
8610/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008611 * Check if the word at line "lnum" column "col" is required to start with a
8612 * capital. This uses 'spellcapcheck' of the current buffer.
8613 */
8614 static int
8615check_need_cap(lnum, col)
8616 linenr_T lnum;
8617 colnr_T col;
8618{
8619 int need_cap = FALSE;
8620 char_u *line;
8621 char_u *line_copy = NULL;
8622 char_u *p;
8623 colnr_T endcol;
8624 regmatch_T regmatch;
8625
8626 if (curbuf->b_cap_prog == NULL)
8627 return FALSE;
8628
8629 line = ml_get_curline();
8630 endcol = 0;
8631 if ((int)(skipwhite(line) - line) >= (int)col)
8632 {
8633 /* At start of line, check if previous line is empty or sentence
8634 * ends there. */
8635 if (lnum == 1)
8636 need_cap = TRUE;
8637 else
8638 {
8639 line = ml_get(lnum - 1);
8640 if (*skipwhite(line) == NUL)
8641 need_cap = TRUE;
8642 else
8643 {
8644 /* Append a space in place of the line break. */
8645 line_copy = concat_str(line, (char_u *)" ");
8646 line = line_copy;
8647 endcol = STRLEN(line);
8648 }
8649 }
8650 }
8651 else
8652 endcol = col;
8653
8654 if (endcol > 0)
8655 {
8656 /* Check if sentence ends before the bad word. */
8657 regmatch.regprog = curbuf->b_cap_prog;
8658 regmatch.rm_ic = FALSE;
8659 p = line + endcol;
8660 for (;;)
8661 {
8662 mb_ptr_back(line, p);
8663 if (p == line || spell_iswordp_nmw(p))
8664 break;
8665 if (vim_regexec(&regmatch, p, 0)
8666 && regmatch.endp[0] == line + endcol)
8667 {
8668 need_cap = TRUE;
8669 break;
8670 }
8671 }
8672 }
8673
8674 vim_free(line_copy);
8675
8676 return need_cap;
8677}
8678
8679
8680/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008681 * ":spellrepall"
8682 */
8683/*ARGSUSED*/
8684 void
8685ex_spellrepall(eap)
8686 exarg_T *eap;
8687{
8688 pos_T pos = curwin->w_cursor;
8689 char_u *frompat;
8690 int addlen;
8691 char_u *line;
8692 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008693 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008694 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008695
8696 if (repl_from == NULL || repl_to == NULL)
8697 {
8698 EMSG(_("E752: No previous spell replacement"));
8699 return;
8700 }
8701 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8702
8703 frompat = alloc(STRLEN(repl_from) + 7);
8704 if (frompat == NULL)
8705 return;
8706 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8707 p_ws = FALSE;
8708
Bram Moolenaar5195e452005-08-19 20:32:47 +00008709 sub_nsubs = 0;
8710 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008711 curwin->w_cursor.lnum = 0;
8712 while (!got_int)
8713 {
8714 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8715 || u_save_cursor() == FAIL)
8716 break;
8717
8718 /* Only replace when the right word isn't there yet. This happens
8719 * when changing "etc" to "etc.". */
8720 line = ml_get_curline();
8721 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8722 repl_to, STRLEN(repl_to)) != 0)
8723 {
8724 p = alloc(STRLEN(line) + addlen + 1);
8725 if (p == NULL)
8726 break;
8727 mch_memmove(p, line, curwin->w_cursor.col);
8728 STRCPY(p + curwin->w_cursor.col, repl_to);
8729 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8730 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8731 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008732
8733 if (curwin->w_cursor.lnum != prev_lnum)
8734 {
8735 ++sub_nlines;
8736 prev_lnum = curwin->w_cursor.lnum;
8737 }
8738 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008739 }
8740 curwin->w_cursor.col += STRLEN(repl_to);
8741 }
8742
8743 p_ws = save_ws;
8744 curwin->w_cursor = pos;
8745 vim_free(frompat);
8746
Bram Moolenaar5195e452005-08-19 20:32:47 +00008747 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008748 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008749 else
8750 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008751}
8752
8753/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008754 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8755 * a list of allocated strings.
8756 */
8757 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008758spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008759 garray_T *gap;
8760 char_u *word;
8761 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008762 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008763{
8764 suginfo_T sug;
8765 int i;
8766 suggest_T *stp;
8767 char_u *wcopy;
8768
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008769 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008770
8771 /* Make room in "gap". */
8772 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8773 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8774 return;
8775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008776 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008777 {
8778 stp = &SUG(sug.su_ga, i);
8779
8780 /* The suggested word may replace only part of "word", add the not
8781 * replaced part. */
8782 wcopy = alloc(STRLEN(stp->st_word)
8783 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8784 if (wcopy == NULL)
8785 break;
8786 STRCPY(wcopy, stp->st_word);
8787 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8788 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8789 }
8790
8791 spell_find_cleanup(&sug);
8792}
8793
8794/*
8795 * Find spell suggestions for the word at the start of "badptr".
8796 * Return the suggestions in "su->su_ga".
8797 * The maximum number of suggestions is "maxcount".
8798 * Note: does use info for the current window.
8799 * This is based on the mechanisms of Aspell, but completely reimplemented.
8800 */
8801 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008802spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008803 char_u *badptr;
8804 suginfo_T *su;
8805 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008806 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008807 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008808{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008809 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008810 char_u buf[MAXPATHL];
8811 char_u *p;
8812 int do_combine = FALSE;
8813 char_u *sps_copy;
8814#ifdef FEAT_EVAL
8815 static int expr_busy = FALSE;
8816#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008817 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008818 int i;
8819 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008820
8821 /*
8822 * Set the info in "*su".
8823 */
8824 vim_memset(su, 0, sizeof(suginfo_T));
8825 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8826 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008827 if (*badptr == NUL)
8828 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008829 hash_init(&su->su_banned);
8830
8831 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008832 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008833 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008834 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008835
8836 if (su->su_badlen >= MAXWLEN)
8837 su->su_badlen = MAXWLEN - 1; /* just in case */
8838 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8839 (void)spell_casefold(su->su_badptr, su->su_badlen,
8840 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008841 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008842 su->su_badflags = badword_captype(su->su_badptr,
8843 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008844 if (need_cap)
8845 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008846
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008847 /* Find the default language for sound folding. We simply use the first
8848 * one in 'spelllang' that supports sound folding. That's good for when
8849 * using multiple files for one language, it's not that bad when mixing
8850 * languages (e.g., "pl,en"). */
8851 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
8852 {
8853 lp = LANGP_ENTRY(curbuf->b_langp, i);
8854 if (lp->lp_sallang != NULL)
8855 {
8856 su->su_sallang = lp->lp_sallang;
8857 break;
8858 }
8859 }
8860
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008861 /* Soundfold the bad word with the default sound folding, so that we don't
8862 * have to do this many times. */
8863 if (su->su_sallang != NULL)
8864 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
8865 su->su_sal_badword);
8866
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008867 /* If the word is not capitalised and spell_check() doesn't consider the
8868 * word to be bad then it might need to be capitalised. Add a suggestion
8869 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008870 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008871 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008872 {
8873 make_case_word(su->su_badword, buf, WF_ONECAP);
8874 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008875 0, TRUE, su->su_sallang);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008876 }
8877
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008878 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008879 if (banbadword)
8880 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008881
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008882 /* Make a copy of 'spellsuggest', because the expression may change it. */
8883 sps_copy = vim_strsave(p_sps);
8884 if (sps_copy == NULL)
8885 return;
8886
8887 /* Loop over the items in 'spellsuggest'. */
8888 for (p = sps_copy; *p != NUL; )
8889 {
8890 copy_option_part(&p, buf, MAXPATHL, ",");
8891
8892 if (STRNCMP(buf, "expr:", 5) == 0)
8893 {
8894#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008895 /* Evaluate an expression. Skip this when called recursively,
8896 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008897 if (!expr_busy)
8898 {
8899 expr_busy = TRUE;
8900 spell_suggest_expr(su, buf + 5);
8901 expr_busy = FALSE;
8902 }
8903#endif
8904 }
8905 else if (STRNCMP(buf, "file:", 5) == 0)
8906 /* Use list of suggestions in a file. */
8907 spell_suggest_file(su, buf + 5);
8908 else
8909 {
8910 /* Use internal method. */
8911 spell_suggest_intern(su);
8912 if (sps_flags & SPS_DOUBLE)
8913 do_combine = TRUE;
8914 }
8915 }
8916
8917 vim_free(sps_copy);
8918
8919 if (do_combine)
8920 /* Combine the two list of suggestions. This must be done last,
8921 * because sorting changes the order again. */
8922 score_combine(su);
8923}
8924
8925#ifdef FEAT_EVAL
8926/*
8927 * Find suggestions by evaluating expression "expr".
8928 */
8929 static void
8930spell_suggest_expr(su, expr)
8931 suginfo_T *su;
8932 char_u *expr;
8933{
8934 list_T *list;
8935 listitem_T *li;
8936 int score;
8937 char_u *p;
8938
8939 /* The work is split up in a few parts to avoid having to export
8940 * suginfo_T.
8941 * First evaluate the expression and get the resulting list. */
8942 list = eval_spell_expr(su->su_badword, expr);
8943 if (list != NULL)
8944 {
8945 /* Loop over the items in the list. */
8946 for (li = list->lv_first; li != NULL; li = li->li_next)
8947 if (li->li_tv.v_type == VAR_LIST)
8948 {
8949 /* Get the word and the score from the items. */
8950 score = get_spellword(li->li_tv.vval.v_list, &p);
8951 if (score >= 0)
8952 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008953 su->su_badlen, score, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008954 }
8955 list_unref(list);
8956 }
8957
8958 /* Sort the suggestions and truncate at "maxcount". */
8959 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8960}
8961#endif
8962
8963/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008964 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008965 */
8966 static void
8967spell_suggest_file(su, fname)
8968 suginfo_T *su;
8969 char_u *fname;
8970{
8971 FILE *fd;
8972 char_u line[MAXWLEN * 2];
8973 char_u *p;
8974 int len;
8975 char_u cword[MAXWLEN];
8976
8977 /* Open the file. */
8978 fd = mch_fopen((char *)fname, "r");
8979 if (fd == NULL)
8980 {
8981 EMSG2(_(e_notopen), fname);
8982 return;
8983 }
8984
8985 /* Read it line by line. */
8986 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8987 {
8988 line_breakcheck();
8989
8990 p = vim_strchr(line, '/');
8991 if (p == NULL)
8992 continue; /* No Tab found, just skip the line. */
8993 *p++ = NUL;
8994 if (STRICMP(su->su_badword, line) == 0)
8995 {
8996 /* Match! Isolate the good word, until CR or NL. */
8997 for (len = 0; p[len] >= ' '; ++len)
8998 ;
8999 p[len] = NUL;
9000
9001 /* If the suggestion doesn't have specific case duplicate the case
9002 * of the bad word. */
9003 if (captype(p, NULL) == 0)
9004 {
9005 make_case_word(p, cword, su->su_badflags);
9006 p = cword;
9007 }
9008
9009 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009010 SCORE_FILE, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009011 }
9012 }
9013
9014 fclose(fd);
9015
9016 /* Sort the suggestions and truncate at "maxcount". */
9017 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9018}
9019
9020/*
9021 * Find suggestions for the internal method indicated by "sps_flags".
9022 */
9023 static void
9024spell_suggest_intern(su)
9025 suginfo_T *su;
9026{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009027 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009028 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009029 *
9030 * Set a maximum score to limit the combination of operations that is
9031 * tried.
9032 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009033 suggest_try_special(su);
9034
9035 /*
9036 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
9037 * from the .aff file and inserting a space (split the word).
9038 */
9039 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009040
9041 /* For the resulting top-scorers compute the sound-a-like score. */
9042 if (sps_flags & SPS_DOUBLE)
9043 score_comp_sal(su);
9044
9045 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009046 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009047 *
9048 * Only do this when we don't have a lot of suggestions yet, because it's
9049 * very slow and often doesn't find new suggestions.
9050 */
9051 if ((sps_flags & SPS_DOUBLE)
9052 || (!(sps_flags & SPS_FAST)
9053 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
9054 {
9055 /* Allow a higher score now. */
9056 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009057 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009058 }
9059
9060 /* When CTRL-C was hit while searching do show the results. */
9061 ui_breakcheck();
9062 if (got_int)
9063 {
9064 (void)vgetc();
9065 got_int = FALSE;
9066 }
9067
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009068 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009069 {
9070 if (sps_flags & SPS_BEST)
9071 /* Adjust the word score for how it sounds like. */
9072 rescore_suggestions(su);
9073
9074 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009075 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009076 }
9077}
9078
9079/*
9080 * Free the info put in "*su" by spell_find_suggest().
9081 */
9082 static void
9083spell_find_cleanup(su)
9084 suginfo_T *su;
9085{
9086 int i;
9087
9088 /* Free the suggestions. */
9089 for (i = 0; i < su->su_ga.ga_len; ++i)
9090 vim_free(SUG(su->su_ga, i).st_word);
9091 ga_clear(&su->su_ga);
9092 for (i = 0; i < su->su_sga.ga_len; ++i)
9093 vim_free(SUG(su->su_sga, i).st_word);
9094 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009095
9096 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009097 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098}
9099
9100/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009101 * Make a copy of "word", with the first letter upper or lower cased, to
9102 * "wcopy[MAXWLEN]". "word" must not be empty.
9103 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009104 */
9105 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009106onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009107 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108 char_u *wcopy;
9109 int upper; /* TRUE: first letter made upper case */
9110{
9111 char_u *p;
9112 int c;
9113 int l;
9114
9115 p = word;
9116#ifdef FEAT_MBYTE
9117 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009118 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 else
9120#endif
9121 c = *p++;
9122 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009123 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009124 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009125 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009126#ifdef FEAT_MBYTE
9127 if (has_mbyte)
9128 l = mb_char2bytes(c, wcopy);
9129 else
9130#endif
9131 {
9132 l = 1;
9133 wcopy[0] = c;
9134 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009135 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136}
9137
9138/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009139 * Make a copy of "word" with all the letters upper cased into
9140 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 */
9142 static void
9143allcap_copy(word, wcopy)
9144 char_u *word;
9145 char_u *wcopy;
9146{
9147 char_u *s;
9148 char_u *d;
9149 int c;
9150
9151 d = wcopy;
9152 for (s = word; *s != NUL; )
9153 {
9154#ifdef FEAT_MBYTE
9155 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009156 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 else
9158#endif
9159 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00009160
9161#ifdef FEAT_MBYTE
9162 /* We only change ß to SS when we are certain latin1 is used. It
9163 * would cause weird errors in other 8-bit encodings. */
9164 if (enc_latin1like && c == 0xdf)
9165 {
9166 c = 'S';
9167 if (d - wcopy >= MAXWLEN - 1)
9168 break;
9169 *d++ = c;
9170 }
9171 else
9172#endif
9173 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174
9175#ifdef FEAT_MBYTE
9176 if (has_mbyte)
9177 {
9178 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
9179 break;
9180 d += mb_char2bytes(c, d);
9181 }
9182 else
9183#endif
9184 {
9185 if (d - wcopy >= MAXWLEN - 1)
9186 break;
9187 *d++ = c;
9188 }
9189 }
9190 *d = NUL;
9191}
9192
9193/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009194 * Try finding suggestions by recognizing specific situations.
9195 */
9196 static void
9197suggest_try_special(su)
9198 suginfo_T *su;
9199{
9200 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009201 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009202 int c;
9203 char_u word[MAXWLEN];
9204
9205 /*
9206 * Recognize a word that is repeated: "the the".
9207 */
9208 p = skiptowhite(su->su_fbadword);
9209 len = p - su->su_fbadword;
9210 p = skipwhite(p);
9211 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
9212 {
9213 /* Include badflags: if the badword is onecap or allcap
9214 * use that for the goodword too: "The the" -> "The". */
9215 c = su->su_fbadword[len];
9216 su->su_fbadword[len] = NUL;
9217 make_case_word(su->su_fbadword, word, su->su_badflags);
9218 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009219
9220 /* Give a soundalike score of 0, compute the score as if deleting one
9221 * character. */
9222 add_suggestion(su, &su->su_ga, word, su->su_badlen,
9223 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009224 }
9225}
9226
9227/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009228 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00009229 *
9230 * This uses a state machine. At each node in the tree we try various
9231 * operations. When trying if an operation work "depth" is increased and the
9232 * stack[] is used to store info. This allows combinations, thus insert one
9233 * character, replace one and delete another. The number of changes is
9234 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00009235 *
9236 * After implementing this I noticed an article by Kemal Oflazer that
9237 * describes something similar: "Error-tolerant Finite State Recognition with
9238 * Applications to Morphological Analysis and Spelling Correction" (1996).
9239 * The implementation in the article is simplified and requires a stack of
9240 * unknown depth. The implementation here only needs a stack depth of the
9241 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242 */
9243 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009244suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009245 suginfo_T *su;
9246{
9247 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
9248 char_u tword[MAXWLEN]; /* good word collected so far */
9249 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009250 char_u preword[MAXWLEN * 3]; /* word found with proper case;
9251 * concatanation of prefix compound
9252 * words and split word. NUL terminated
9253 * when going deeper but not when coming
9254 * back. */
9255 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009256 trystate_T *sp;
9257 int newscore;
9258 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009259 char_u *byts, *fbyts, *pbyts;
9260 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009262 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009263 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009266 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 int len;
9268 char_u *p;
9269 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00009270 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009271 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009272 slang_T *slang;
9273 int fword_ends;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009274 int lpi;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009275 int maysplit;
9276 int goodword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277
9278 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00009279 * to find matches (esp. REP items). Append some more text, changing
9280 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009282 n = STRLEN(fword);
9283 p = su->su_badptr + su->su_badlen;
9284 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009286 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009287 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009288 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009289 slang = lp->lp_slang;
9290
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009291 /* If reloading a spell file fails it's still in the list but
9292 * everything has been cleared. */
9293 if (slang->sl_fbyts == NULL)
9294 continue;
9295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 /*
9297 * Go through the whole case-fold tree, try changes at each node.
9298 * "tword[]" contains the word collected from nodes in the tree.
9299 * "fword[]" the word we are trying to match with (initially the bad
9300 * word).
9301 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009303 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009304 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009305 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009306
Bram Moolenaarea424162005-06-16 21:51:00 +00009307 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009308 * When there are postponed prefixes we need to use these first. At
9309 * the end of the prefix we continue in the case-fold tree.
9310 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009311 fbyts = slang->sl_fbyts;
9312 fidxs = slang->sl_fidxs;
9313 pbyts = slang->sl_pbyts;
9314 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009315 if (pbyts != NULL)
9316 {
9317 byts = pbyts;
9318 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009319 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009320 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9321 }
9322 else
9323 {
9324 byts = fbyts;
9325 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009326 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009327 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009328 }
9329
9330 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009331 * Loop to find all suggestions. At each round we either:
9332 * - For the current state try one operation, advance "ts_curi",
9333 * increase "depth".
9334 * - When a state is done go to the next, set "ts_state".
9335 * - When all states are tried decrease "depth".
9336 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009337 while (depth >= 0 && !got_int)
9338 {
9339 sp = &stack[depth];
9340 switch (sp->ts_state)
9341 {
9342 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009343 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 /*
9345 * Start of node: Deal with NUL bytes, which means
9346 * tword[] may end here.
9347 */
9348 arridx = sp->ts_arridx; /* current node in the tree */
9349 len = byts[arridx]; /* bytes in this node */
9350 arridx += sp->ts_curi; /* index of current byte */
9351
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009352 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009353 {
9354 /* Skip over the NUL bytes, we use them later. */
9355 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9356 ;
9357 sp->ts_curi += n;
9358
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009359 /* Always past NUL bytes now. */
9360 n = (int)sp->ts_state;
9361 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009362 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009363
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009364 /* At end of a prefix or at start of prefixtree: check for
9365 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009366 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009367 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009368 /* Set su->su_badflags to the caps type at this
9369 * position. Use the caps type until here for the
9370 * prefix itself. */
9371#ifdef FEAT_MBYTE
9372 if (has_mbyte)
9373 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9374 else
9375#endif
9376 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009377 flags = badword_captype(su->su_badptr,
9378 su->su_badptr + n);
9379 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009380 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009381 ++depth;
9382 stack[depth] = stack[depth - 1];
9383 sp = &stack[depth];
9384 sp->ts_prefixdepth = depth - 1;
9385 byts = fbyts;
9386 idxs = fidxs;
9387 sp->ts_state = STATE_START;
9388 sp->ts_curi = 1; /* start just after length byte */
9389 sp->ts_arridx = 0;
9390
Bram Moolenaar53805d12005-08-01 07:08:33 +00009391 /* Move the prefix to preword[] with the right case
9392 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009393 tword[sp->ts_twordlen] = NUL;
9394 make_case_word(tword + sp->ts_splitoff,
9395 preword + sp->ts_prewordlen,
9396 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009397 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009398 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009399 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009400 break;
9401 }
9402
Bram Moolenaar0c405862005-06-22 22:26:26 +00009403 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 {
9405 /* Past bytes in node and/or past NUL bytes. */
9406 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009407 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 break;
9409 }
9410
9411 /*
9412 * End of word in tree.
9413 */
9414 ++sp->ts_curi; /* eat one NUL byte */
9415
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009416 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009417 fword_ends = (fword[sp->ts_fidx] == NUL
9418 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9419 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420
Bram Moolenaard12a1322005-08-21 22:08:24 +00009421 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9422 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009423 {
9424 /* There was a prefix before the word. Check that the
9425 * prefix can be used with this word. */
9426 /* Count the length of the NULs in the prefix. If there
9427 * are none this must be the first try without a prefix.
9428 */
9429 n = stack[sp->ts_prefixdepth].ts_arridx;
9430 len = pbyts[n++];
9431 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9432 ;
9433 if (c > 0)
9434 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009435 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009436 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009437 if (c == 0)
9438 break;
9439
9440 /* Use the WF_RARE flag for a rare prefix. */
9441 if (c & WF_RAREPFX)
9442 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009443
9444 /* Tricky: when checking for both prefix and
9445 * compounding we run into the prefix flag first.
9446 * Remember that it's OK, so that we accept the prefix
9447 * when arriving at a compound flag. */
9448 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009449 }
9450 }
9451
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009452 /* Check NEEDCOMPOUND: can't use word without compounding. Do
9453 * try appending another compound word below. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009454 if (sp->ts_complen == sp->ts_compsplit && fword_ends
9455 && (flags & WF_NEEDCOMP))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009456 goodword_ends = FALSE;
9457 else
9458 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009459
Bram Moolenaard12a1322005-08-21 22:08:24 +00009460 if (sp->ts_complen > sp->ts_compsplit)
9461 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009462 if (slang->sl_nobreak)
9463 {
9464 /* There was a word before this word. When there was
9465 * no change in this word (it was correct) add the
9466 * first word as a suggestion. If this word was
9467 * corrected too, we need to check if a correct word
9468 * follows. */
9469 if (sp->ts_fidx - sp->ts_splitfidx
9470 == sp->ts_twordlen - sp->ts_splitoff
9471 && STRNCMP(fword + sp->ts_splitfidx,
9472 tword + sp->ts_splitoff,
9473 sp->ts_fidx - sp->ts_splitfidx) == 0)
9474 {
9475 preword[sp->ts_prewordlen] = NUL;
9476 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009477 sp->ts_splitfidx - repextra,
9478 sp->ts_score, 0, FALSE,
9479 lp->lp_sallang);
Bram Moolenaar78622822005-08-23 21:00:13 +00009480 break;
9481 }
9482 }
9483 else
9484 {
9485 /* There was a compound word before this word. If
9486 * this word does not support compounding then give up
9487 * (splitting is tried for the word without compound
9488 * flag). */
9489 if (((unsigned)flags >> 24) == 0
9490 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009491 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009492 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009493#ifdef FEAT_MBYTE
9494 /* For multi-byte chars check character length against
9495 * COMPOUNDMIN. */
9496 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009497 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009498 && mb_charlen(tword + sp->ts_splitoff)
9499 < slang->sl_compminlen)
9500 break;
9501#endif
9502
Bram Moolenaar78622822005-08-23 21:00:13 +00009503 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9504 compflags[sp->ts_complen + 1] = NUL;
9505 vim_strncpy(preword + sp->ts_prewordlen,
9506 tword + sp->ts_splitoff,
9507 sp->ts_twordlen - sp->ts_splitoff);
9508 p = preword;
9509 while (*skiptowhite(p) != NUL)
9510 p = skipwhite(skiptowhite(p));
9511 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009512 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009513 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009514
Bram Moolenaar78622822005-08-23 21:00:13 +00009515 /* Get pointer to last char of previous word. */
9516 p = preword + sp->ts_prewordlen;
9517 mb_ptr_back(preword, p);
9518 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009519 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009520 else
9521 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009522
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523 /*
9524 * Form the word with proper case in preword.
9525 * If there is a word from a previous split, append.
9526 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 if (flags & WF_KEEPCAP)
9528 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009529 find_keepcap_word(slang, tword + sp->ts_splitoff,
9530 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009534 * use that for the goodword too. But if the badword is
9535 * allcap and it's only one char long use onecap. */
9536 c = su->su_badflags;
9537 if ((c & WF_ALLCAP)
9538#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009539 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009540#else
9541 && su->su_badlen == 1
9542#endif
9543 )
9544 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009545 c |= flags;
9546
9547 /* When appending a compound word after a word character
9548 * don't use Onecap. */
9549 if (p != NULL && spell_iswordp_nmw(p))
9550 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009551 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009552 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009553 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554
9555 /* Don't use a banned word. It may appear again as a good
9556 * word, thus remember it. */
9557 if (flags & WF_BANNED)
9558 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009559 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 break;
9561 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009562 if ((sp->ts_complen == sp->ts_compsplit
9563 && was_banned(su, preword + sp->ts_prewordlen))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009564 || was_banned(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009565 {
9566 if (slang->sl_compprog == NULL)
9567 break;
9568 /* the word so far was banned but we may try compounding */
9569 goodword_ends = FALSE;
9570 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571
9572 newscore = 0;
9573 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009574 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 newscore += SCORE_REGION;
9576 if (flags & WF_RARE)
9577 newscore += SCORE_RARE;
9578
Bram Moolenaar0c405862005-06-22 22:26:26 +00009579 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009580 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009581 newscore += SCORE_ICASE;
9582
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009583 maysplit = TRUE;
9584 if (fword_ends && goodword_ends
9585 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009586 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009587 /* The badword also ends: add suggestions. Give a penalty
9588 * when changing non-word char to word char, e.g., "thes,"
9589 * -> "these". */
9590 p = fword + sp->ts_fidx;
9591#ifdef FEAT_MBYTE
9592 if (has_mbyte)
9593 mb_ptr_back(fword, p);
9594 else
9595#endif
9596 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009597 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009598 {
9599 p = preword + STRLEN(preword);
9600#ifdef FEAT_MBYTE
9601 if (has_mbyte)
9602 mb_ptr_back(preword, p);
9603 else
9604#endif
9605 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009606 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009607 newscore += SCORE_NONWORD;
9608 }
9609
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009610 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009611 sp->ts_fidx - repextra,
9612 sp->ts_score + newscore, 0, FALSE,
9613 lp->lp_sallang);
9614
9615 /* When the bad word doesn't end yet, try changing the
9616 * next word. E.g., find suggestions for "the the" where
9617 * the second "the" is different. It's done like a split.
9618 */
9619 if (sp->ts_fidx - repextra >= su->su_badlen)
9620 maysplit = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009622
9623 if (maysplit
9624 && (sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009625#ifdef FEAT_MBYTE
9626 /* Don't split halfway a character. */
9627 && (!has_mbyte || sp->ts_tcharlen == 0)
9628#endif
9629 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009630 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009631 int try_compound;
9632
9633 /* Get here in two situations:
9634 * 1. The word in the tree ends but the badword continues:
9635 * If the word allows compounding try that. Otherwise
9636 * try a split by inserting a space. For both check
9637 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009638 * For NOBREAK do like compounding to be able to check
9639 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009640 * 2. The badword does end, but it was due to a change
9641 * (e.g., a swap). No need to split, but do check that
9642 * the following word is valid.
9643 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009644 try_compound = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009645 if ((!fword_ends || !goodword_ends)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009646 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009647 && ((unsigned)flags >> 24) != 0
9648 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009649 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009650#ifdef FEAT_MBYTE
9651 && (!has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009652 || slang->sl_compminlen == 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009653 || mb_charlen(tword + sp->ts_splitoff)
9654 >= slang->sl_compminlen)
9655#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00009656 && (slang->sl_compsylmax < MAXWLEN
9657 || sp->ts_complen + 1 - sp->ts_compsplit
9658 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009659 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009660 ? slang->sl_compstartflags
9661 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009662 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009663 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009664 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009665 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9666 compflags[sp->ts_complen + 1] = NUL;
9667 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009668
Bram Moolenaar78622822005-08-23 21:00:13 +00009669 /* For NOBREAK we never try splitting, it won't make any
9670 * word valid. */
9671 if (slang->sl_nobreak)
9672 try_compound = TRUE;
9673
Bram Moolenaard12a1322005-08-21 22:08:24 +00009674 /* If we could add a compound word, and it's also possible
9675 * to split at this point, do the split first and set
9676 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009677 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009678 && try_compound
9679 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009680 {
9681 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009682 sp->ts_flags |= TSF_DIDSPLIT;
9683 --sp->ts_curi; /* do the same NUL again */
9684 compflags[sp->ts_complen] = NUL;
9685 }
9686 else
9687 sp->ts_flags &= ~TSF_DIDSPLIT;
9688
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009689 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00009690 {
9691 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009692 * words so far are valid for compounding. If there
9693 * is only one word it must not have the NEEDCOMPOUND
9694 * flag. */
9695 if (sp->ts_complen == sp->ts_compsplit
9696 && (flags & WF_NEEDCOMP))
9697 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009698 p = preword;
9699 while (*skiptowhite(p) != NUL)
9700 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009701 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009702 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009703 compflags + sp->ts_compsplit))
9704 break;
9705 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009706 }
9707
9708 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 {
9710 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009711 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009712 sp->ts_state = STATE_SPLITUNDO;
9713
9714 ++depth;
9715 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009716
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009717 /* Append a space to preword when splitting. */
9718 if (!try_compound && !fword_ends)
9719 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009720 sp->ts_prewordlen = STRLEN(preword);
9721 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009722 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009723
9724 /* If the badword has a non-word character at this
9725 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009726 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009727 * character when the word ends. But only when the
9728 * good word can end. */
9729 if (((!try_compound
9730 && !spell_iswordp_nmw(fword + sp->ts_fidx))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009731 || fword_ends)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009732 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009733 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009734 int l;
9735
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009736#ifdef FEAT_MBYTE
9737 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009738 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009739 else
9740#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009741 l = 1;
9742 if (fword_ends)
9743 {
9744 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009745 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009746 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009747 sp->ts_prewordlen += l;
9748 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009749 }
9750 else
9751 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9752 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009753 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009754
Bram Moolenaard12a1322005-08-21 22:08:24 +00009755 /* When compounding include compound flag in
9756 * compflags[] (already set above). When splitting we
9757 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009758 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009759 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009760 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009761 sp->ts_compsplit = sp->ts_complen;
9762 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009763
Bram Moolenaar53805d12005-08-01 07:08:33 +00009764 /* set su->su_badflags to the caps type at this
9765 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009766#ifdef FEAT_MBYTE
9767 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009768 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009769 else
9770#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009771 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009772 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009773 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009776 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009777
9778 /* If there are postponed prefixes, try these too. */
9779 if (pbyts != NULL)
9780 {
9781 byts = pbyts;
9782 idxs = pidxs;
9783 sp->ts_prefixdepth = PFD_PREFIXTREE;
9784 sp->ts_state = STATE_NOPREFIX;
9785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009786 }
9787 }
9788 break;
9789
9790 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009791 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009792 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793
9794 /* Continue looking for NUL bytes. */
9795 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009796
9797 /* In case we went into the prefix tree. */
9798 byts = fbyts;
9799 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009800 break;
9801
9802 case STATE_ENDNUL:
9803 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009804 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009805 if (fword[sp->ts_fidx] == NUL
9806#ifdef FEAT_MBYTE
9807 && sp->ts_tcharlen == 0
9808#endif
9809 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009810 {
9811 /* The badword ends, can't use the bytes in this node. */
9812 sp->ts_state = STATE_DEL;
9813 break;
9814 }
9815 sp->ts_state = STATE_PLAIN;
9816 /*FALLTHROUGH*/
9817
9818 case STATE_PLAIN:
9819 /*
9820 * Go over all possible bytes at this node, add each to
9821 * tword[] and use child node. "ts_curi" is the index.
9822 */
9823 arridx = sp->ts_arridx;
9824 if (sp->ts_curi > byts[arridx])
9825 {
9826 /* Done all bytes at this node, do next state. When still
9827 * at already changed bytes skip the other tricks. */
9828 if (sp->ts_fidx >= sp->ts_fidxtry)
9829 sp->ts_state = STATE_DEL;
9830 else
9831 sp->ts_state = STATE_FINAL;
9832 }
9833 else
9834 {
9835 arridx += sp->ts_curi++;
9836 c = byts[arridx];
9837
9838 /* Normal byte, go one level deeper. If it's not equal to
9839 * the byte in the bad word adjust the score. But don't
9840 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009841 if (c == fword[sp->ts_fidx]
9842#ifdef FEAT_MBYTE
9843 || (sp->ts_tcharlen > 0
9844 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009845#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009846 )
9847 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009848 else
9849 newscore = SCORE_SUBST;
9850 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9851 && try_deeper(su, stack, depth, newscore))
9852 {
9853 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009854 sp = &stack[depth];
9855 ++sp->ts_fidx;
9856 tword[sp->ts_twordlen++] = c;
9857 sp->ts_arridx = idxs[arridx];
9858#ifdef FEAT_MBYTE
9859 if (newscore == SCORE_SUBST)
9860 sp->ts_isdiff = DIFF_YES;
9861 if (has_mbyte)
9862 {
9863 /* Multi-byte characters are a bit complicated to
9864 * handle: They differ when any of the bytes
9865 * differ and then their length may also differ. */
9866 if (sp->ts_tcharlen == 0)
9867 {
9868 /* First byte. */
9869 sp->ts_tcharidx = 0;
9870 sp->ts_tcharlen = MB_BYTE2LEN(c);
9871 sp->ts_fcharstart = sp->ts_fidx - 1;
9872 sp->ts_isdiff = (newscore != 0)
9873 ? DIFF_YES : DIFF_NONE;
9874 }
9875 else if (sp->ts_isdiff == DIFF_INSERT)
9876 /* When inserting trail bytes don't advance in
9877 * the bad word. */
9878 --sp->ts_fidx;
9879 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9880 {
9881 /* Last byte of character. */
9882 if (sp->ts_isdiff == DIFF_YES)
9883 {
9884 /* Correct ts_fidx for the byte length of
9885 * the character (we didn't check that
9886 * before). */
9887 sp->ts_fidx = sp->ts_fcharstart
9888 + MB_BYTE2LEN(
9889 fword[sp->ts_fcharstart]);
9890
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009891 /* For changing a composing character
9892 * adjust the score from SCORE_SUBST to
9893 * SCORE_SUBCOMP. */
9894 if (enc_utf8
9895 && utf_iscomposing(
9896 mb_ptr2char(tword
9897 + sp->ts_twordlen
9898 - sp->ts_tcharlen))
9899 && utf_iscomposing(
9900 mb_ptr2char(fword
9901 + sp->ts_fcharstart)))
9902 sp->ts_score -=
9903 SCORE_SUBST - SCORE_SUBCOMP;
9904
Bram Moolenaarea424162005-06-16 21:51:00 +00009905 /* For a similar character adjust score
9906 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009907 else if (slang->sl_has_map
9908 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009909 mb_ptr2char(tword
9910 + sp->ts_twordlen
9911 - sp->ts_tcharlen),
9912 mb_ptr2char(fword
9913 + sp->ts_fcharstart)))
9914 sp->ts_score -=
9915 SCORE_SUBST - SCORE_SIMILAR;
9916 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009917 else if (sp->ts_isdiff == DIFF_INSERT
9918 && sp->ts_twordlen > sp->ts_tcharlen)
9919 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009920 p = tword + sp->ts_twordlen
9921 - sp->ts_tcharlen;
9922 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009923 if (enc_utf8 && utf_iscomposing(c))
9924 {
9925 /* Inserting a composing char doesn't
9926 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009927 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009928 - SCORE_INSCOMP;
9929 }
9930 else
9931 {
9932 /* If the previous character was the
9933 * same, thus doubling a character,
9934 * give a bonus to the score. */
9935 mb_ptr_back(tword, p);
9936 if (c == mb_ptr2char(p))
9937 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009938 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009939 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009940 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009941
9942 /* Starting a new char, reset the length. */
9943 sp->ts_tcharlen = 0;
9944 }
9945 }
9946 else
9947#endif
9948 {
9949 /* If we found a similar char adjust the score.
9950 * We do this after calling try_deeper() because
9951 * it's slow. */
9952 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009953 && slang->sl_has_map
9954 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009955 c, fword[sp->ts_fidx - 1]))
9956 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 }
9959 }
9960 break;
9961
9962 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009963#ifdef FEAT_MBYTE
9964 /* When past the first byte of a multi-byte char don't try
9965 * delete/insert/swap a character. */
9966 if (has_mbyte && sp->ts_tcharlen > 0)
9967 {
9968 sp->ts_state = STATE_FINAL;
9969 break;
9970 }
9971#endif
9972 /*
9973 * Try skipping one character in the bad word (delete it).
9974 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 sp->ts_state = STATE_INS;
9976 sp->ts_curi = 1;
9977 if (fword[sp->ts_fidx] != NUL
9978 && try_deeper(su, stack, depth, SCORE_DEL))
9979 {
9980 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009981
9982 /* Advance over the character in fword[]. Give a bonus to
9983 * the score if the same character is following "nn" ->
9984 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009985#ifdef FEAT_MBYTE
9986 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009987 {
9988 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009989 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009990 if (enc_utf8 && utf_iscomposing(c))
9991 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9992 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009993 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9994 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009995 else
9996#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009997 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009998 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009999 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
10000 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
10001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010002 break;
10003 }
10004 /*FALLTHROUGH*/
10005
10006 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +000010007 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 * node. */
10009 n = sp->ts_arridx;
10010 if (sp->ts_curi > byts[n])
10011 {
10012 /* Done all bytes at this node, do next state. */
10013 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 }
10015 else
10016 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010017 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 n += sp->ts_curi++;
10019 c = byts[n];
10020 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
10021 {
10022 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010023 sp = &stack[depth];
10024 tword[sp->ts_twordlen++] = c;
10025 sp->ts_arridx = idxs[n];
10026#ifdef FEAT_MBYTE
10027 if (has_mbyte)
10028 {
10029 fl = MB_BYTE2LEN(c);
10030 if (fl > 1)
10031 {
10032 /* There are following bytes for the same
10033 * character. We must find all bytes before
10034 * trying delete/insert/swap/etc. */
10035 sp->ts_tcharlen = fl;
10036 sp->ts_tcharidx = 1;
10037 sp->ts_isdiff = DIFF_INSERT;
10038 }
10039 }
Bram Moolenaarea408852005-06-25 22:49:46 +000010040 else
10041 fl = 1;
10042 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000010043#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000010044 {
10045 /* If the previous character was the same, thus
10046 * doubling a character, give a bonus to the
10047 * score. */
10048 if (sp->ts_twordlen >= 2
10049 && tword[sp->ts_twordlen - 2] == c)
10050 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
10051 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010052 }
10053 }
10054 break;
10055
10056 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +000010057 /*
10058 * Swap two bytes in the bad word: "12" -> "21".
10059 * We change "fword" here, it's changed back afterwards.
10060 */
10061 p = fword + sp->ts_fidx;
10062 c = *p;
10063 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010064 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010065 /* End of word, can't swap or replace. */
10066 sp->ts_state = STATE_FINAL;
10067 break;
10068 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010069
10070 /* Don't swap if the first character is not a word character.
10071 * SWAP3 etc. also don't make sense then. */
10072 if (!spell_iswordp(p, curbuf))
10073 {
10074 sp->ts_state = STATE_REP_INI;
10075 break;
10076 }
10077
Bram Moolenaarea424162005-06-16 21:51:00 +000010078#ifdef FEAT_MBYTE
10079 if (has_mbyte)
10080 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010081 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010082 c = mb_ptr2char(p);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010083 if (!spell_iswordp(p + n, curbuf))
10084 c2 = c; /* don't swap non-word char */
10085 else
10086 c2 = mb_ptr2char(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010087 }
10088 else
10089#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010090 {
10091 if (!spell_iswordp(p + 1, curbuf))
10092 c2 = c; /* don't swap non-word char */
10093 else
10094 c2 = p[1];
10095 }
10096
10097 /* When characters are identical, swap won't do anything.
10098 * Also get here if the second char is not a word character. */
Bram Moolenaarea424162005-06-16 21:51:00 +000010099 if (c == c2)
10100 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010101 sp->ts_state = STATE_SWAP3;
10102 break;
10103 }
10104 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
10105 {
10106 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010108#ifdef FEAT_MBYTE
10109 if (has_mbyte)
10110 {
10111 fl = mb_char2len(c2);
10112 mch_memmove(p, p + n, fl);
10113 mb_char2bytes(c, p + fl);
10114 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10115 }
10116 else
10117#endif
10118 {
10119 p[0] = c2;
10120 p[1] = c;
10121 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
10122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 }
10124 else
10125 /* If this swap doesn't work then SWAP3 won't either. */
10126 sp->ts_state = STATE_REP_INI;
10127 break;
10128
Bram Moolenaarea424162005-06-16 21:51:00 +000010129 case STATE_UNSWAP:
10130 /* Undo the STATE_SWAP swap: "21" -> "12". */
10131 p = fword + sp->ts_fidx;
10132#ifdef FEAT_MBYTE
10133 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010135 n = MB_BYTE2LEN(*p);
10136 c = mb_ptr2char(p + n);
10137 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
10138 mb_char2bytes(c, p);
10139 }
10140 else
10141#endif
10142 {
10143 c = *p;
10144 *p = p[1];
10145 p[1] = c;
10146 }
10147 /*FALLTHROUGH*/
10148
10149 case STATE_SWAP3:
10150 /* Swap two bytes, skipping one: "123" -> "321". We change
10151 * "fword" here, it's changed back afterwards. */
10152 p = fword + sp->ts_fidx;
10153#ifdef FEAT_MBYTE
10154 if (has_mbyte)
10155 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010156 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010157 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010158 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010159 c2 = mb_ptr2char(p + n);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010160 if (!spell_iswordp(p + n + fl, curbuf))
10161 c3 = c; /* don't swap non-word char */
10162 else
10163 c3 = mb_ptr2char(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010164 }
10165 else
10166#endif
10167 {
10168 c = *p;
10169 c2 = p[1];
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010170 if (!spell_iswordp(p + 2, curbuf))
10171 c3 = c; /* don't swap non-word char */
10172 else
10173 c3 = p[2];
Bram Moolenaarea424162005-06-16 21:51:00 +000010174 }
10175
10176 /* When characters are identical: "121" then SWAP3 result is
10177 * identical, ROT3L result is same as SWAP: "211", ROT3L
10178 * result is same as SWAP on next char: "112". Thus skip all
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010179 * swapping. Also skip when c3 is NUL.
10180 * Also get here when the third character is not a word
10181 * character. Second character may any char: "a.b" -> "b.a" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010182 if (c == c3 || c3 == NUL)
10183 {
10184 sp->ts_state = STATE_REP_INI;
10185 break;
10186 }
10187 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10188 {
10189 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010191#ifdef FEAT_MBYTE
10192 if (has_mbyte)
10193 {
10194 tl = mb_char2len(c3);
10195 mch_memmove(p, p + n + fl, tl);
10196 mb_char2bytes(c2, p + tl);
10197 mb_char2bytes(c, p + fl + tl);
10198 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
10199 }
10200 else
10201#endif
10202 {
10203 p[0] = p[2];
10204 p[2] = c;
10205 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10206 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 }
10208 else
10209 sp->ts_state = STATE_REP_INI;
10210 break;
10211
Bram Moolenaarea424162005-06-16 21:51:00 +000010212 case STATE_UNSWAP3:
10213 /* Undo STATE_SWAP3: "321" -> "123" */
10214 p = fword + sp->ts_fidx;
10215#ifdef FEAT_MBYTE
10216 if (has_mbyte)
10217 {
10218 n = MB_BYTE2LEN(*p);
10219 c2 = mb_ptr2char(p + n);
10220 fl = MB_BYTE2LEN(p[n]);
10221 c = mb_ptr2char(p + n + fl);
10222 tl = MB_BYTE2LEN(p[n + fl]);
10223 mch_memmove(p + fl + tl, p, n);
10224 mb_char2bytes(c, p);
10225 mb_char2bytes(c2, p + tl);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010226 p = p + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010227 }
10228 else
10229#endif
10230 {
10231 c = *p;
10232 *p = p[2];
10233 p[2] = c;
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010234 ++p;
10235 }
10236
10237 if (!spell_iswordp(p, curbuf))
10238 {
10239 /* Middle char is not a word char, skip the rotate.
10240 * First and third char were already checked at swap
10241 * and swap3. */
10242 sp->ts_state = STATE_REP_INI;
10243 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000010244 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010245
Bram Moolenaarea424162005-06-16 21:51:00 +000010246 /* Rotate three characters left: "123" -> "231". We change
10247 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010248 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10249 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010250 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010252 p = fword + sp->ts_fidx;
10253#ifdef FEAT_MBYTE
10254 if (has_mbyte)
10255 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010256 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010257 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010258 fl = mb_cptr2len(p + n);
10259 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010260 mch_memmove(p, p + n, fl);
10261 mb_char2bytes(c, p + fl);
10262 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10263 }
10264 else
10265#endif
10266 {
10267 c = *p;
10268 *p = p[1];
10269 p[1] = p[2];
10270 p[2] = c;
10271 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10272 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010273 }
10274 else
10275 sp->ts_state = STATE_REP_INI;
10276 break;
10277
Bram Moolenaarea424162005-06-16 21:51:00 +000010278 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010279 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010280 p = fword + sp->ts_fidx;
10281#ifdef FEAT_MBYTE
10282 if (has_mbyte)
10283 {
10284 n = MB_BYTE2LEN(*p);
10285 n += MB_BYTE2LEN(p[n]);
10286 c = mb_ptr2char(p + n);
10287 tl = MB_BYTE2LEN(p[n]);
10288 mch_memmove(p + tl, p, n);
10289 mb_char2bytes(c, p);
10290 }
10291 else
10292#endif
10293 {
10294 c = p[2];
10295 p[2] = p[1];
10296 p[1] = *p;
10297 *p = c;
10298 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +000010301 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010302 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10303 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010304 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010306 p = fword + sp->ts_fidx;
10307#ifdef FEAT_MBYTE
10308 if (has_mbyte)
10309 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010310 n = mb_cptr2len(p);
10311 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010312 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010313 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010314 mch_memmove(p + tl, p, n);
10315 mb_char2bytes(c, p);
10316 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
10317 }
10318 else
10319#endif
10320 {
10321 c = p[2];
10322 p[2] = p[1];
10323 p[1] = *p;
10324 *p = c;
10325 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10326 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327 }
10328 else
10329 sp->ts_state = STATE_REP_INI;
10330 break;
10331
Bram Moolenaarea424162005-06-16 21:51:00 +000010332 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010333 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010334 p = fword + sp->ts_fidx;
10335#ifdef FEAT_MBYTE
10336 if (has_mbyte)
10337 {
10338 c = mb_ptr2char(p);
10339 tl = MB_BYTE2LEN(*p);
10340 n = MB_BYTE2LEN(p[tl]);
10341 n += MB_BYTE2LEN(p[tl + n]);
10342 mch_memmove(p, p + tl, n);
10343 mb_char2bytes(c, p + n);
10344 }
10345 else
10346#endif
10347 {
10348 c = *p;
10349 *p = p[1];
10350 p[1] = p[2];
10351 p[2] = c;
10352 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010353 /*FALLTHROUGH*/
10354
10355 case STATE_REP_INI:
10356 /* Check if matching with REP items from the .aff file would
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010357 * work. Quickly skip if:
10358 * - there are no REP items
10359 * - the score is going to be too high anyway
10360 * - already applied a REP item or swapped here */
10361 if (lp->lp_replang == NULL
10362 || sp->ts_score + SCORE_REP >= su->su_maxscore
10363 || sp->ts_fidx < sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 {
10365 sp->ts_state = STATE_FINAL;
10366 break;
10367 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010368 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010369
10370 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +000010371 * may match. If the index is -1 there is none. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010372 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373 if (sp->ts_curi < 0)
10374 {
10375 sp->ts_state = STATE_FINAL;
10376 break;
10377 }
10378
10379 sp->ts_state = STATE_REP;
10380 /*FALLTHROUGH*/
10381
10382 case STATE_REP:
10383 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +000010384 * match replace the characters and check if the resulting
10385 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010386 p = fword + sp->ts_fidx;
10387
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010388 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010389 while (sp->ts_curi < gap->ga_len)
10390 {
10391 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
10392 if (*ftp->ft_from != *p)
10393 {
10394 /* past possible matching entries */
10395 sp->ts_curi = gap->ga_len;
10396 break;
10397 }
10398 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10399 && try_deeper(su, stack, depth, SCORE_REP))
10400 {
10401 /* Need to undo this afterwards. */
10402 sp->ts_state = STATE_REP_UNDO;
10403
10404 /* Change the "from" to the "to" string. */
10405 ++depth;
10406 fl = STRLEN(ftp->ft_from);
10407 tl = STRLEN(ftp->ft_to);
10408 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010411 repextra += tl - fl;
10412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 mch_memmove(p, ftp->ft_to, tl);
10414 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010415#ifdef FEAT_MBYTE
10416 stack[depth].ts_tcharlen = 0;
10417#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 break;
10419 }
10420 }
10421
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010422 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010423 /* No (more) matches. */
10424 sp->ts_state = STATE_FINAL;
10425
10426 break;
10427
10428 case STATE_REP_UNDO:
10429 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010430 ftp = (fromto_T *)lp->lp_replang->sl_rep.ga_data
10431 + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 fl = STRLEN(ftp->ft_from);
10433 tl = STRLEN(ftp->ft_to);
10434 p = fword + sp->ts_fidx;
10435 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010437 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010438 repextra -= tl - fl;
10439 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 mch_memmove(p, ftp->ft_from, fl);
10441 sp->ts_state = STATE_REP;
10442 break;
10443
10444 default:
10445 /* Did all possible states at this level, go up one level. */
10446 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010448 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010449 {
10450 /* Continue in or go back to the prefix tree. */
10451 byts = pbyts;
10452 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010453 }
10454
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010455 /* Don't check for CTRL-C too often, it takes time. */
10456 line_breakcheck();
10457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 }
10459 }
10460}
10461
10462/*
10463 * Try going one level deeper in the tree.
10464 */
10465 static int
10466try_deeper(su, stack, depth, score_add)
10467 suginfo_T *su;
10468 trystate_T *stack;
10469 int depth;
10470 int score_add;
10471{
10472 int newscore;
10473
10474 /* Refuse to go deeper if the scrore is getting too big. */
10475 newscore = stack[depth].ts_score + score_add;
10476 if (newscore >= su->su_maxscore)
10477 return FALSE;
10478
Bram Moolenaarea424162005-06-16 21:51:00 +000010479 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 stack[depth + 1].ts_state = STATE_START;
10481 stack[depth + 1].ts_score = newscore;
10482 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010483 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 return TRUE;
10485}
10486
Bram Moolenaar53805d12005-08-01 07:08:33 +000010487#ifdef FEAT_MBYTE
10488/*
10489 * Case-folding may change the number of bytes: Count nr of chars in
10490 * fword[flen] and return the byte length of that many chars in "word".
10491 */
10492 static int
10493nofold_len(fword, flen, word)
10494 char_u *fword;
10495 int flen;
10496 char_u *word;
10497{
10498 char_u *p;
10499 int i = 0;
10500
10501 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10502 ++i;
10503 for (p = word; i > 0; mb_ptr_adv(p))
10504 --i;
10505 return (int)(p - word);
10506}
10507#endif
10508
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509/*
10510 * "fword" is a good word with case folded. Find the matching keep-case
10511 * words and put it in "kword".
10512 * Theoretically there could be several keep-case words that result in the
10513 * same case-folded word, but we only find one...
10514 */
10515 static void
10516find_keepcap_word(slang, fword, kword)
10517 slang_T *slang;
10518 char_u *fword;
10519 char_u *kword;
10520{
10521 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10522 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010523 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524
10525 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010526 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 int round[MAXWLEN];
10528 int fwordidx[MAXWLEN];
10529 int uwordidx[MAXWLEN];
10530 int kwordlen[MAXWLEN];
10531
10532 int flen, ulen;
10533 int l;
10534 int len;
10535 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010536 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010537 char_u *p;
10538 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010539 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540
10541 if (byts == NULL)
10542 {
10543 /* array is empty: "cannot happen" */
10544 *kword = NUL;
10545 return;
10546 }
10547
10548 /* Make an all-cap version of "fword". */
10549 allcap_copy(fword, uword);
10550
10551 /*
10552 * Each character needs to be tried both case-folded and upper-case.
10553 * All this gets very complicated if we keep in mind that changing case
10554 * may change the byte length of a multi-byte character...
10555 */
10556 depth = 0;
10557 arridx[0] = 0;
10558 round[0] = 0;
10559 fwordidx[0] = 0;
10560 uwordidx[0] = 0;
10561 kwordlen[0] = 0;
10562 while (depth >= 0)
10563 {
10564 if (fword[fwordidx[depth]] == NUL)
10565 {
10566 /* We are at the end of "fword". If the tree allows a word to end
10567 * here we have found a match. */
10568 if (byts[arridx[depth] + 1] == 0)
10569 {
10570 kword[kwordlen[depth]] = NUL;
10571 return;
10572 }
10573
10574 /* kword is getting too long, continue one level up */
10575 --depth;
10576 }
10577 else if (++round[depth] > 2)
10578 {
10579 /* tried both fold-case and upper-case character, continue one
10580 * level up */
10581 --depth;
10582 }
10583 else
10584 {
10585 /*
10586 * round[depth] == 1: Try using the folded-case character.
10587 * round[depth] == 2: Try using the upper-case character.
10588 */
10589#ifdef FEAT_MBYTE
10590 if (has_mbyte)
10591 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010592 flen = mb_cptr2len(fword + fwordidx[depth]);
10593 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 }
10595 else
10596#endif
10597 ulen = flen = 1;
10598 if (round[depth] == 1)
10599 {
10600 p = fword + fwordidx[depth];
10601 l = flen;
10602 }
10603 else
10604 {
10605 p = uword + uwordidx[depth];
10606 l = ulen;
10607 }
10608
10609 for (tryidx = arridx[depth]; l > 0; --l)
10610 {
10611 /* Perform a binary search in the list of accepted bytes. */
10612 len = byts[tryidx++];
10613 c = *p++;
10614 lo = tryidx;
10615 hi = tryidx + len - 1;
10616 while (lo < hi)
10617 {
10618 m = (lo + hi) / 2;
10619 if (byts[m] > c)
10620 hi = m - 1;
10621 else if (byts[m] < c)
10622 lo = m + 1;
10623 else
10624 {
10625 lo = hi = m;
10626 break;
10627 }
10628 }
10629
10630 /* Stop if there is no matching byte. */
10631 if (hi < lo || byts[lo] != c)
10632 break;
10633
10634 /* Continue at the child (if there is one). */
10635 tryidx = idxs[lo];
10636 }
10637
10638 if (l == 0)
10639 {
10640 /*
10641 * Found the matching char. Copy it to "kword" and go a
10642 * level deeper.
10643 */
10644 if (round[depth] == 1)
10645 {
10646 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10647 flen);
10648 kwordlen[depth + 1] = kwordlen[depth] + flen;
10649 }
10650 else
10651 {
10652 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10653 ulen);
10654 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10655 }
10656 fwordidx[depth + 1] = fwordidx[depth] + flen;
10657 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10658
10659 ++depth;
10660 arridx[depth] = tryidx;
10661 round[depth] = 0;
10662 }
10663 }
10664 }
10665
10666 /* Didn't find it: "cannot happen". */
10667 *kword = NUL;
10668}
10669
10670/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010671 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10672 * su->su_sga.
10673 */
10674 static void
10675score_comp_sal(su)
10676 suginfo_T *su;
10677{
10678 langp_T *lp;
10679 char_u badsound[MAXWLEN];
10680 int i;
10681 suggest_T *stp;
10682 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010683 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010684 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010685
10686 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10687 return;
10688
10689 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010690 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010691 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010692 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010693 if (lp->lp_slang->sl_sal.ga_len > 0)
10694 {
10695 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010696 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010697
10698 for (i = 0; i < su->su_ga.ga_len; ++i)
10699 {
10700 stp = &SUG(su->su_ga, i);
10701
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010702 /* Case-fold the suggested word, sound-fold it and compute the
10703 * sound-a-like score. */
10704 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010705 if (score < SCORE_MAXMAX)
10706 {
10707 /* Add the suggestion. */
10708 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10709 sstp->st_word = vim_strsave(stp->st_word);
10710 if (sstp->st_word != NULL)
10711 {
10712 sstp->st_score = score;
10713 sstp->st_altscore = 0;
10714 sstp->st_orglen = stp->st_orglen;
10715 ++su->su_sga.ga_len;
10716 }
10717 }
10718 }
10719 break;
10720 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010721 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010722}
10723
10724/*
10725 * Combine the list of suggestions in su->su_ga and su->su_sga.
10726 * They are intwined.
10727 */
10728 static void
10729score_combine(su)
10730 suginfo_T *su;
10731{
10732 int i;
10733 int j;
10734 garray_T ga;
10735 garray_T *gap;
10736 langp_T *lp;
10737 suggest_T *stp;
10738 char_u *p;
10739 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010740 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010741 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010742
10743 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010744 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010745 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010746 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010747 if (lp->lp_slang->sl_sal.ga_len > 0)
10748 {
10749 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010750 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010751
10752 for (i = 0; i < su->su_ga.ga_len; ++i)
10753 {
10754 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010755 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10756 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010757 if (stp->st_altscore == SCORE_MAXMAX)
10758 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10759 else
10760 stp->st_score = (stp->st_score * 3
10761 + stp->st_altscore) / 4;
10762 stp->st_salscore = FALSE;
10763 }
10764 break;
10765 }
10766 }
10767
10768 /* Add the alternate score to su_sga. */
10769 for (i = 0; i < su->su_sga.ga_len; ++i)
10770 {
10771 stp = &SUG(su->su_sga, i);
10772 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10773 if (stp->st_score == SCORE_MAXMAX)
10774 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10775 else
10776 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10777 stp->st_salscore = TRUE;
10778 }
10779
10780 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10781 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10782 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10783
10784 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10785 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10786 return;
10787
10788 stp = &SUG(ga, 0);
10789 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10790 {
10791 /* round 1: get a suggestion from su_ga
10792 * round 2: get a suggestion from su_sga */
10793 for (round = 1; round <= 2; ++round)
10794 {
10795 gap = round == 1 ? &su->su_ga : &su->su_sga;
10796 if (i < gap->ga_len)
10797 {
10798 /* Don't add a word if it's already there. */
10799 p = SUG(*gap, i).st_word;
10800 for (j = 0; j < ga.ga_len; ++j)
10801 if (STRCMP(stp[j].st_word, p) == 0)
10802 break;
10803 if (j == ga.ga_len)
10804 stp[ga.ga_len++] = SUG(*gap, i);
10805 else
10806 vim_free(p);
10807 }
10808 }
10809 }
10810
10811 ga_clear(&su->su_ga);
10812 ga_clear(&su->su_sga);
10813
10814 /* Truncate the list to the number of suggestions that will be displayed. */
10815 if (ga.ga_len > su->su_maxcount)
10816 {
10817 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10818 vim_free(stp[i].st_word);
10819 ga.ga_len = su->su_maxcount;
10820 }
10821
10822 su->su_ga = ga;
10823}
10824
10825/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010826 * For the goodword in "stp" compute the soundalike score compared to the
10827 * badword.
10828 */
10829 static int
10830stp_sal_score(stp, su, slang, badsound)
10831 suggest_T *stp;
10832 suginfo_T *su;
10833 slang_T *slang;
10834 char_u *badsound; /* sound-folded badword */
10835{
10836 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010837 char_u *pbad;
10838 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010839 char_u badsound2[MAXWLEN];
10840 char_u fword[MAXWLEN];
10841 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010842 char_u goodword[MAXWLEN];
10843 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010844
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010845 lendiff = (int)(su->su_badlen - stp->st_orglen);
10846 if (lendiff >= 0)
10847 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010848 else
10849 {
10850 /* soundfold the bad word with more characters following */
10851 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10852
10853 /* When joining two words the sound often changes a lot. E.g., "t he"
10854 * sounds like "t h" while "the" sounds like "@". Avoid that by
10855 * removing the space. Don't do it when the good word also contains a
10856 * space. */
10857 if (vim_iswhite(su->su_badptr[su->su_badlen])
10858 && *skiptowhite(stp->st_word) == NUL)
10859 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10860 mch_memmove(p, p + 1, STRLEN(p));
10861
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010862 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010863 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010864 }
10865
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010866 if (lendiff > 0)
10867 {
10868 /* Add part of the bad word to the good word, so that we soundfold
10869 * what replaces the bad word. */
10870 STRCPY(goodword, stp->st_word);
10871 STRNCAT(goodword, su->su_badptr + su->su_badlen - lendiff, lendiff);
10872 pgood = goodword;
10873 }
10874 else
10875 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010876
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010877 /* Sound-fold the word and compute the score for the difference. */
10878 spell_soundfold(slang, pgood, FALSE, goodsound);
10879
10880 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010881}
10882
10883/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010885 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010886 */
10887 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010888suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889 suginfo_T *su;
10890{
10891 char_u salword[MAXWLEN];
10892 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010894 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 int curi[MAXWLEN];
10896 langp_T *lp;
10897 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010898 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 int depth;
10900 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010901 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 int round;
10903 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010904 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010905 int local_score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010906 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010907 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010908
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010909 /* Do this for all languages that support sound folding. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010910 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010912 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10913 slang = lp->lp_slang;
10914 if (slang->sl_sal.ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 {
10916 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010917 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918
10919 /*
10920 * Go through the whole tree, soundfold each word and compare.
10921 * round 1: use the case-folded tree.
10922 * round 2: use the keep-case tree.
10923 */
10924 for (round = 1; round <= 2; ++round)
10925 {
10926 if (round == 1)
10927 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010928 byts = slang->sl_fbyts;
10929 idxs = slang->sl_fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 }
10931 else
10932 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010933 byts = slang->sl_kbyts;
10934 idxs = slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010935 if (byts == NULL) /* no keep-case words */
10936 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 }
10938
10939 depth = 0;
10940 arridx[0] = 0;
10941 curi[0] = 1;
10942 while (depth >= 0 && !got_int)
10943 {
10944 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 /* Done all bytes at this node, go up one level. */
10947 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010948 line_breakcheck();
10949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 else
10951 {
10952 /* Do one more byte at this node. */
10953 n = arridx[depth] + curi[depth];
10954 ++curi[depth];
10955 c = byts[n];
10956 if (c == 0)
10957 {
10958 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010959 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10961 {
10962 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010963 /* Sound-fold. Only in keep-case tree need to
10964 * case-fold the word. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010965 spell_soundfold(slang, tword,
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010966 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010968 /* Compute the edit distance between the
10969 * sound-a-like words. */
10970 sound_score = soundalike_score(salword,
10971 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010972
10973 /* Add a penalty for words in another region. */
10974 if ((flags & WF_REGION) && (((unsigned)flags
10975 >> 16) & lp->lp_region) == 0)
10976 local_score = SCORE_REGION;
10977 else
10978 local_score = 0;
10979 sound_score += local_score;
10980
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010981 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010982 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010983 char_u cword[MAXWLEN];
10984 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010985 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010986
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010987 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010988 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010990 /* Need to fix case according to
10991 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010992 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010993 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010994 }
10995 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010996 p = tword;
10997
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010998 if (sps_flags & SPS_DOUBLE)
10999 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011000 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011001 sound_score, 0, FALSE,
11002 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011003 else
11004 {
11005 /* Compute the score. */
11006 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000011007 su->su_badword, p)
11008 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011009 if (sps_flags & SPS_BEST)
11010 /* give a bonus for the good word
11011 * sounding the same as the bad
11012 * word */
11013 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011014 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011015 RESCORE(score, sound_score),
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011016 sound_score, TRUE,
11017 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011018 else
11019 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011020 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011021 score + sound_score,
11022 0, FALSE,
11023 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 }
11026 }
11027
11028 /* Skip over other NUL bytes. */
11029 while (byts[n + 1] == 0)
11030 {
11031 ++n;
11032 ++curi[depth];
11033 }
11034 }
11035 else
11036 {
11037 /* Normal char, go one level deeper. */
11038 tword[depth++] = c;
11039 arridx[depth] = idxs[n];
11040 curi[depth] = 1;
11041 }
11042 }
11043 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 }
11045 }
11046 }
11047}
11048
11049/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011050 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 */
11052 static void
11053make_case_word(fword, cword, flags)
11054 char_u *fword;
11055 char_u *cword;
11056 int flags;
11057{
11058 if (flags & WF_ALLCAP)
11059 /* Make it all upper-case */
11060 allcap_copy(fword, cword);
11061 else if (flags & WF_ONECAP)
11062 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011063 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064 else
11065 /* Use goodword as-is. */
11066 STRCPY(cword, fword);
11067}
11068
Bram Moolenaarea424162005-06-16 21:51:00 +000011069/*
11070 * Use map string "map" for languages "lp".
11071 */
11072 static void
11073set_map_str(lp, map)
11074 slang_T *lp;
11075 char_u *map;
11076{
11077 char_u *p;
11078 int headc = 0;
11079 int c;
11080 int i;
11081
11082 if (*map == NUL)
11083 {
11084 lp->sl_has_map = FALSE;
11085 return;
11086 }
11087 lp->sl_has_map = TRUE;
11088
11089 /* Init the array and hash table empty. */
11090 for (i = 0; i < 256; ++i)
11091 lp->sl_map_array[i] = 0;
11092#ifdef FEAT_MBYTE
11093 hash_init(&lp->sl_map_hash);
11094#endif
11095
11096 /*
11097 * The similar characters are stored separated with slashes:
11098 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
11099 * before the same slash. For characters above 255 sl_map_hash is used.
11100 */
11101 for (p = map; *p != NUL; )
11102 {
11103#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011104 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011105#else
11106 c = *p++;
11107#endif
11108 if (c == '/')
11109 headc = 0;
11110 else
11111 {
11112 if (headc == 0)
11113 headc = c;
11114
11115#ifdef FEAT_MBYTE
11116 /* Characters above 255 don't fit in sl_map_array[], put them in
11117 * the hash table. Each entry is the char, a NUL the headchar and
11118 * a NUL. */
11119 if (c >= 256)
11120 {
11121 int cl = mb_char2len(c);
11122 int headcl = mb_char2len(headc);
11123 char_u *b;
11124 hash_T hash;
11125 hashitem_T *hi;
11126
11127 b = alloc((unsigned)(cl + headcl + 2));
11128 if (b == NULL)
11129 return;
11130 mb_char2bytes(c, b);
11131 b[cl] = NUL;
11132 mb_char2bytes(headc, b + cl + 1);
11133 b[cl + 1 + headcl] = NUL;
11134 hash = hash_hash(b);
11135 hi = hash_lookup(&lp->sl_map_hash, b, hash);
11136 if (HASHITEM_EMPTY(hi))
11137 hash_add_item(&lp->sl_map_hash, hi, b, hash);
11138 else
11139 {
11140 /* This should have been checked when generating the .spl
11141 * file. */
11142 EMSG(_("E999: duplicate char in MAP entry"));
11143 vim_free(b);
11144 }
11145 }
11146 else
11147#endif
11148 lp->sl_map_array[c] = headc;
11149 }
11150 }
11151}
11152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153/*
11154 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
11155 * lines in the .aff file.
11156 */
11157 static int
11158similar_chars(slang, c1, c2)
11159 slang_T *slang;
11160 int c1;
11161 int c2;
11162{
Bram Moolenaarea424162005-06-16 21:51:00 +000011163 int m1, m2;
11164#ifdef FEAT_MBYTE
11165 char_u buf[MB_MAXBYTES];
11166 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011167
Bram Moolenaarea424162005-06-16 21:51:00 +000011168 if (c1 >= 256)
11169 {
11170 buf[mb_char2bytes(c1, buf)] = 0;
11171 hi = hash_find(&slang->sl_map_hash, buf);
11172 if (HASHITEM_EMPTY(hi))
11173 m1 = 0;
11174 else
11175 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11176 }
11177 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011178#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000011179 m1 = slang->sl_map_array[c1];
11180 if (m1 == 0)
11181 return FALSE;
11182
11183
11184#ifdef FEAT_MBYTE
11185 if (c2 >= 256)
11186 {
11187 buf[mb_char2bytes(c2, buf)] = 0;
11188 hi = hash_find(&slang->sl_map_hash, buf);
11189 if (HASHITEM_EMPTY(hi))
11190 m2 = 0;
11191 else
11192 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11193 }
11194 else
11195#endif
11196 m2 = slang->sl_map_array[c2];
11197
11198 return m1 == m2;
11199}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200
11201/*
11202 * Add a suggestion to the list of suggestions.
11203 * Do not add a duplicate suggestion or suggestions with a bad score.
11204 * When "use_score" is not zero it's used, otherwise the score is computed
11205 * with spell_edit_score().
11206 */
11207 static void
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011208add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus, slang)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011209 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011210 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011212 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011213 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011214 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011215 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011216 slang_T *slang; /* language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011218 int goodlen = STRLEN(goodword); /* len of goodword changed */
11219 int badlen = badlenarg; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011221 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011223 hlf_T attr = HLF_COUNT;
Bram Moolenaar1e015462005-09-25 22:16:38 +000011224 char_u longword[MAXWLEN + 1];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011225 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226
Bram Moolenaar1e015462005-09-25 22:16:38 +000011227 /* Check that the word really is valid. Esp. for banned words and for
11228 * split words, such as "the the". Need to append what follows to check
11229 * for that. */
11230 STRCPY(longword, goodword);
11231 vim_strncpy(longword + goodlen, su->su_badptr + badlen, MAXWLEN - goodlen);
11232 (void)spell_check(curwin, longword, &attr, NULL);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011233 if (attr != HLF_COUNT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 return;
11235
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011236 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
11237 * "thee the" is added next to changing the first "the" the "thee". */
11238 pgood = goodword + STRLEN(goodword);
11239 pbad = su->su_badptr + badlen;
11240 while (pgood > goodword && pbad > su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011241 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011242 mb_ptr_back(goodword, pgood);
11243 mb_ptr_back(su->su_badptr, pbad);
11244#ifdef FEAT_MBYTE
11245 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011246 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011247 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
11248 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011249 }
11250 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011251#endif
11252 if (*pgood != *pbad)
11253 break;
11254 badlen = pbad - su->su_badptr;
11255 goodlen = pgood - goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011256 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011257 if (badlen == 0 && goodlen == 0)
11258 /* goodword doesn't change anything; may happen for "the the" changing
11259 * the first "the" to itself. */
11260 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011262 if (score <= su->su_maxscore)
11263 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011264 /* Check if the word is already there. Also check the length that is
11265 * being replaced "thes," -> "these" is a different suggestion from
11266 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011267 stp = &SUG(*gap, 0);
11268 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011269 if ((int)STRLEN(stp[i].st_word) == goodlen
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011270 && STRNCMP(stp[i].st_word, goodword, goodlen) == 0
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011271 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011272 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011273 /*
Bram Moolenaar4effc802005-09-30 21:12:02 +000011274 * Found it. Remember the word with the lowest score.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011275 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011276 if (stp[i].st_slang == NULL)
11277 stp[i].st_slang = slang;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011278
11279 new_sug.st_score = score;
11280 new_sug.st_altscore = altscore;
11281 new_sug.st_had_bonus = had_bonus;
11282
11283 if (stp[i].st_had_bonus != had_bonus)
11284 {
11285 /* Only one of the two had the soundalike score computed.
11286 * Need to do that for the other one now, otherwise the
11287 * scores can't be compared. This happens because
11288 * suggest_try_change() doesn't compute the soundalike
Bram Moolenaar4effc802005-09-30 21:12:02 +000011289 * word to keep it fast, while some special methods set
11290 * the soundalike score to zero. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011291 if (had_bonus)
11292 rescore_one(su, &stp[i]);
11293 else
11294 {
11295 new_sug.st_word = goodword;
11296 new_sug.st_slang = stp[i].st_slang;
11297 new_sug.st_orglen = badlen;
11298 rescore_one(su, &new_sug);
11299 }
11300 }
11301
11302 if (stp[i].st_score > new_sug.st_score)
11303 {
11304 stp[i].st_score = new_sug.st_score;
11305 stp[i].st_altscore = new_sug.st_altscore;
11306 stp[i].st_had_bonus = new_sug.st_had_bonus;
11307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011308 break;
11309 }
11310
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011311 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 {
11313 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011314 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011315 stp->st_word = vim_strnsave(goodword, goodlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 if (stp->st_word != NULL)
11317 {
11318 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011319 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011320 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011321 stp->st_orglen = badlen;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011322 stp->st_slang = slang;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011323 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324
11325 /* If we have too many suggestions now, sort the list and keep
11326 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011327 if (gap->ga_len > SUG_MAX_COUNT(su))
11328 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
11329 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 }
11331 }
11332 }
11333}
11334
11335/*
11336 * Add a word to be banned.
11337 */
11338 static void
11339add_banned(su, word)
11340 suginfo_T *su;
11341 char_u *word;
11342{
11343 char_u *s = vim_strsave(word);
11344 hash_T hash;
11345 hashitem_T *hi;
11346
11347 if (s != NULL)
11348 {
11349 hash = hash_hash(s);
11350 hi = hash_lookup(&su->su_banned, s, hash);
11351 if (HASHITEM_EMPTY(hi))
11352 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011353 else
11354 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 }
11356}
11357
11358/*
11359 * Return TRUE if a word appears in the list of banned words.
11360 */
11361 static int
11362was_banned(su, word)
11363 suginfo_T *su;
11364 char_u *word;
11365{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011366 hashitem_T *hi = hash_find(&su->su_banned, word);
11367
11368 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369}
11370
11371/*
11372 * Free the banned words in "su".
11373 */
11374 static void
11375free_banned(su)
11376 suginfo_T *su;
11377{
11378 int todo;
11379 hashitem_T *hi;
11380
11381 todo = su->su_banned.ht_used;
11382 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
11383 {
11384 if (!HASHITEM_EMPTY(hi))
11385 {
11386 vim_free(hi->hi_key);
11387 --todo;
11388 }
11389 }
11390 hash_clear(&su->su_banned);
11391}
11392
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011393/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011394 * Recompute the score for all suggestions if sound-folding is possible. This
11395 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011396 */
11397 static void
11398rescore_suggestions(su)
11399 suginfo_T *su;
11400{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011401 int i;
11402
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011403 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011404 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011405 rescore_one(su, &SUG(su->su_ga, i));
11406}
11407
11408/*
11409 * Recompute the score for one suggestion if sound-folding is possible.
11410 */
11411 static void
11412rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011413 suginfo_T *su;
11414 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011415{
11416 slang_T *slang = stp->st_slang;
11417 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000011418 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011419
11420 /* Only rescore suggestions that have no sal score yet and do have a
11421 * language. */
11422 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
11423 {
11424 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011425 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011426 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011427 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011428 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000011429 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011430 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000011431
11432 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011433 if (stp->st_altscore == SCORE_MAXMAX)
11434 stp->st_altscore = SCORE_BIG;
11435 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
11436 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011437 }
11438}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011440static int
11441#ifdef __BORLANDC__
11442_RTLENTRYF
11443#endif
11444sug_compare __ARGS((const void *s1, const void *s2));
11445
11446/*
11447 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011448 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 */
11450 static int
11451#ifdef __BORLANDC__
11452_RTLENTRYF
11453#endif
11454sug_compare(s1, s2)
11455 const void *s1;
11456 const void *s2;
11457{
11458 suggest_T *p1 = (suggest_T *)s1;
11459 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011460 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011462 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011463 {
11464 n = p1->st_altscore - p2->st_altscore;
11465 if (n == 0)
11466 n = STRICMP(p1->st_word, p2->st_word);
11467 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011468 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469}
11470
11471/*
11472 * Cleanup the suggestions:
11473 * - Sort on score.
11474 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011475 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011477 static int
11478cleanup_suggestions(gap, maxscore, keep)
11479 garray_T *gap;
11480 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011481 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011483 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 int i;
11485
11486 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011487 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488
11489 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011490 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011491 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011492 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011494 gap->ga_len = keep;
11495 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011497 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498}
11499
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011500#if defined(FEAT_EVAL) || defined(PROTO)
11501/*
11502 * Soundfold a string, for soundfold().
11503 * Result is in allocated memory, NULL for an error.
11504 */
11505 char_u *
11506eval_soundfold(word)
11507 char_u *word;
11508{
11509 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011510 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011511 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011512
11513 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11514 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011515 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011516 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011517 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011518 if (lp->lp_slang->sl_sal.ga_len > 0)
11519 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011520 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011521 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011522 return vim_strsave(sound);
11523 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011524 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011525
11526 /* No language with sound folding, return word as-is. */
11527 return vim_strsave(word);
11528}
11529#endif
11530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531/*
11532 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011533 *
11534 * There are many ways to turn a word into a sound-a-like representation. The
11535 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11536 * swedish name matching - survey and test of different algorithms" by Klas
11537 * Erikson.
11538 *
11539 * We support two methods:
11540 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11541 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542 */
11543 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011544spell_soundfold(slang, inword, folded, res)
11545 slang_T *slang;
11546 char_u *inword;
11547 int folded; /* "inword" is already case-folded */
11548 char_u *res;
11549{
11550 char_u fword[MAXWLEN];
11551 char_u *word;
11552
11553 if (slang->sl_sofo)
11554 /* SOFOFROM and SOFOTO used */
11555 spell_soundfold_sofo(slang, inword, res);
11556 else
11557 {
11558 /* SAL items used. Requires the word to be case-folded. */
11559 if (folded)
11560 word = inword;
11561 else
11562 {
11563 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11564 word = fword;
11565 }
11566
11567#ifdef FEAT_MBYTE
11568 if (has_mbyte)
11569 spell_soundfold_wsal(slang, word, res);
11570 else
11571#endif
11572 spell_soundfold_sal(slang, word, res);
11573 }
11574}
11575
11576/*
11577 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11578 * SOFOTO lines.
11579 */
11580 static void
11581spell_soundfold_sofo(slang, inword, res)
11582 slang_T *slang;
11583 char_u *inword;
11584 char_u *res;
11585{
11586 char_u *s;
11587 int ri = 0;
11588 int c;
11589
11590#ifdef FEAT_MBYTE
11591 if (has_mbyte)
11592 {
11593 int prevc = 0;
11594 int *ip;
11595
11596 /* The sl_sal_first[] table contains the translation for chars up to
11597 * 255, sl_sal the rest. */
11598 for (s = inword; *s != NUL; )
11599 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011600 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011601 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11602 c = ' ';
11603 else if (c < 256)
11604 c = slang->sl_sal_first[c];
11605 else
11606 {
11607 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11608 if (ip == NULL) /* empty list, can't match */
11609 c = NUL;
11610 else
11611 for (;;) /* find "c" in the list */
11612 {
11613 if (*ip == 0) /* not found */
11614 {
11615 c = NUL;
11616 break;
11617 }
11618 if (*ip == c) /* match! */
11619 {
11620 c = ip[1];
11621 break;
11622 }
11623 ip += 2;
11624 }
11625 }
11626
11627 if (c != NUL && c != prevc)
11628 {
11629 ri += mb_char2bytes(c, res + ri);
11630 if (ri + MB_MAXBYTES > MAXWLEN)
11631 break;
11632 prevc = c;
11633 }
11634 }
11635 }
11636 else
11637#endif
11638 {
11639 /* The sl_sal_first[] table contains the translation. */
11640 for (s = inword; (c = *s) != NUL; ++s)
11641 {
11642 if (vim_iswhite(c))
11643 c = ' ';
11644 else
11645 c = slang->sl_sal_first[c];
11646 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11647 res[ri++] = c;
11648 }
11649 }
11650
11651 res[ri] = NUL;
11652}
11653
11654 static void
11655spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011656 slang_T *slang;
11657 char_u *inword;
11658 char_u *res;
11659{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011660 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011662 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011663 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011664 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011666 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 int n, k = 0;
11668 int z0;
11669 int k0;
11670 int n0;
11671 int c;
11672 int pri;
11673 int p0 = -333;
11674 int c0;
11675
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011676 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011677 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 if (slang->sl_rem_accents)
11679 {
11680 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011681 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011683 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011684 {
11685 *t++ = ' ';
11686 s = skipwhite(s);
11687 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011688 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011690 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 *t++ = *s;
11692 ++s;
11693 }
11694 }
11695 *t = NUL;
11696 }
11697 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011698 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011700 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701
11702 /*
11703 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011704 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011706 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 while ((c = word[i]) != NUL)
11708 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011709 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 n = slang->sl_sal_first[c];
11711 z0 = 0;
11712
11713 if (n >= 0)
11714 {
11715 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011716 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011718 /* Quickly skip entries that don't match the word. Most
11719 * entries are less then three chars, optimize for that. */
11720 k = smp[n].sm_leadlen;
11721 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011722 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011723 if (word[i + 1] != s[1])
11724 continue;
11725 if (k > 2)
11726 {
11727 for (j = 2; j < k; ++j)
11728 if (word[i + j] != s[j])
11729 break;
11730 if (j < k)
11731 continue;
11732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 }
11734
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011735 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011737 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011738 while (*pf != NUL && *pf != word[i + k])
11739 ++pf;
11740 if (*pf == NUL)
11741 continue;
11742 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011743 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011744 s = smp[n].sm_rules;
11745 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011746
11747 p0 = *s;
11748 k0 = k;
11749 while (*s == '-' && k > 1)
11750 {
11751 k--;
11752 s++;
11753 }
11754 if (*s == '<')
11755 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011756 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011757 {
11758 /* determine priority */
11759 pri = *s - '0';
11760 s++;
11761 }
11762 if (*s == '^' && *(s + 1) == '^')
11763 s++;
11764
11765 if (*s == NUL
11766 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011767 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011768 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011770 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011772 && spell_iswordp(word + i - 1, curbuf)
11773 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 {
11775 /* search for followup rules, if: */
11776 /* followup and k > 1 and NO '-' in searchstring */
11777 c0 = word[i + k - 1];
11778 n0 = slang->sl_sal_first[c0];
11779
11780 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011781 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 {
11783 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011784 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011786 /* Quickly skip entries that don't match the word.
11787 * */
11788 k0 = smp[n0].sm_leadlen;
11789 if (k0 > 1)
11790 {
11791 if (word[i + k] != s[1])
11792 continue;
11793 if (k0 > 2)
11794 {
11795 pf = word + i + k + 1;
11796 for (j = 2; j < k0; ++j)
11797 if (*pf++ != s[j])
11798 break;
11799 if (j < k0)
11800 continue;
11801 }
11802 }
11803 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011805 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011806 {
11807 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011808 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011809 while (*pf != NUL && *pf != word[i + k0])
11810 ++pf;
11811 if (*pf == NUL)
11812 continue;
11813 ++k0;
11814 }
11815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011817 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 while (*s == '-')
11819 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011820 /* "k0" gets NOT reduced because
11821 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 s++;
11823 }
11824 if (*s == '<')
11825 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011826 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 {
11828 p0 = *s - '0';
11829 s++;
11830 }
11831
11832 if (*s == NUL
11833 /* *s == '^' cuts */
11834 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011835 && !spell_iswordp(word + i + k0,
11836 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011837 {
11838 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011839 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841
11842 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 /* rule fits; stop search */
11846 break;
11847 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 }
11849
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011850 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 }
11853
11854 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011855 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011856 if (s == NULL)
11857 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011858 pf = smp[n].sm_rules;
11859 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 if (p0 == 1 && z == 0)
11861 {
11862 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011863 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11864 || res[reslen - 1] == *s))
11865 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 z0 = 1;
11867 z = 1;
11868 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011869 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 {
11871 word[i + k0] = *s;
11872 k0++;
11873 s++;
11874 }
11875 if (k > k0)
11876 mch_memmove(word + i + k0, word + i + k,
11877 STRLEN(word + i + k) + 1);
11878
11879 /* new "actual letter" */
11880 c = word[i];
11881 }
11882 else
11883 {
11884 /* no '<' rule used */
11885 i += k - 1;
11886 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011887 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011888 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011889 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011890 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 s++;
11892 }
11893 /* new "actual letter" */
11894 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011895 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011896 {
11897 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011898 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011899 mch_memmove(word, word + i + 1,
11900 STRLEN(word + i + 1) + 1);
11901 i = 0;
11902 z0 = 1;
11903 }
11904 }
11905 break;
11906 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 }
11908 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011909 else if (vim_iswhite(c))
11910 {
11911 c = ' ';
11912 k = 1;
11913 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914
11915 if (z0 == 0)
11916 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011917 if (k && !p0 && reslen < MAXWLEN && c != NUL
11918 && (!slang->sl_collapse || reslen == 0
11919 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011921 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011922
11923 i++;
11924 z = 0;
11925 k = 0;
11926 }
11927 }
11928
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011929 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930}
11931
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011932#ifdef FEAT_MBYTE
11933/*
11934 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11935 * Multi-byte version of spell_soundfold().
11936 */
11937 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011938spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011939 slang_T *slang;
11940 char_u *inword;
11941 char_u *res;
11942{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011943 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011944 int word[MAXWLEN];
11945 int wres[MAXWLEN];
11946 int l;
11947 char_u *s;
11948 int *ws;
11949 char_u *t;
11950 int *pf;
11951 int i, j, z;
11952 int reslen;
11953 int n, k = 0;
11954 int z0;
11955 int k0;
11956 int n0;
11957 int c;
11958 int pri;
11959 int p0 = -333;
11960 int c0;
11961 int did_white = FALSE;
11962
11963 /*
11964 * Convert the multi-byte string to a wide-character string.
11965 * Remove accents, if wanted. We actually remove all non-word characters.
11966 * But keep white space.
11967 */
11968 n = 0;
11969 for (s = inword; *s != NUL; )
11970 {
11971 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011972 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011973 if (slang->sl_rem_accents)
11974 {
11975 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11976 {
11977 if (did_white)
11978 continue;
11979 c = ' ';
11980 did_white = TRUE;
11981 }
11982 else
11983 {
11984 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011985 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011986 continue;
11987 }
11988 }
11989 word[n++] = c;
11990 }
11991 word[n] = NUL;
11992
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011993 /*
11994 * This comes from Aspell phonet.cpp.
11995 * Converted from C++ to C. Added support for multi-byte chars.
11996 * Changed to keep spaces.
11997 */
11998 i = reslen = z = 0;
11999 while ((c = word[i]) != NUL)
12000 {
12001 /* Start with the first rule that has the character in the word. */
12002 n = slang->sl_sal_first[c & 0xff];
12003 z0 = 0;
12004
12005 if (n >= 0)
12006 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012007 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012008 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
12009 {
12010 /* Quickly skip entries that don't match the word. Most
12011 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012012 if (c != ws[0])
12013 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012014 k = smp[n].sm_leadlen;
12015 if (k > 1)
12016 {
12017 if (word[i + 1] != ws[1])
12018 continue;
12019 if (k > 2)
12020 {
12021 for (j = 2; j < k; ++j)
12022 if (word[i + j] != ws[j])
12023 break;
12024 if (j < k)
12025 continue;
12026 }
12027 }
12028
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012029 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012030 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012031 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012032 while (*pf != NUL && *pf != word[i + k])
12033 ++pf;
12034 if (*pf == NUL)
12035 continue;
12036 ++k;
12037 }
12038 s = smp[n].sm_rules;
12039 pri = 5; /* default priority */
12040
12041 p0 = *s;
12042 k0 = k;
12043 while (*s == '-' && k > 1)
12044 {
12045 k--;
12046 s++;
12047 }
12048 if (*s == '<')
12049 s++;
12050 if (VIM_ISDIGIT(*s))
12051 {
12052 /* determine priority */
12053 pri = *s - '0';
12054 s++;
12055 }
12056 if (*s == '^' && *(s + 1) == '^')
12057 s++;
12058
12059 if (*s == NUL
12060 || (*s == '^'
12061 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012062 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012063 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012064 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012065 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012066 && spell_iswordp_w(word + i - 1, curbuf)
12067 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012068 {
12069 /* search for followup rules, if: */
12070 /* followup and k > 1 and NO '-' in searchstring */
12071 c0 = word[i + k - 1];
12072 n0 = slang->sl_sal_first[c0 & 0xff];
12073
12074 if (slang->sl_followup && k > 1 && n0 >= 0
12075 && p0 != '-' && word[i + k] != NUL)
12076 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012077 /* Test follow-up rule for "word[i + k]"; loop over
12078 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012079 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
12080 == (c0 & 0xff); ++n0)
12081 {
12082 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012083 */
12084 if (c0 != ws[0])
12085 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012086 k0 = smp[n0].sm_leadlen;
12087 if (k0 > 1)
12088 {
12089 if (word[i + k] != ws[1])
12090 continue;
12091 if (k0 > 2)
12092 {
12093 pf = word + i + k + 1;
12094 for (j = 2; j < k0; ++j)
12095 if (*pf++ != ws[j])
12096 break;
12097 if (j < k0)
12098 continue;
12099 }
12100 }
12101 k0 += k - 1;
12102
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012103 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012104 {
12105 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012106 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012107 while (*pf != NUL && *pf != word[i + k0])
12108 ++pf;
12109 if (*pf == NUL)
12110 continue;
12111 ++k0;
12112 }
12113
12114 p0 = 5;
12115 s = smp[n0].sm_rules;
12116 while (*s == '-')
12117 {
12118 /* "k0" gets NOT reduced because
12119 * "if (k0 == k)" */
12120 s++;
12121 }
12122 if (*s == '<')
12123 s++;
12124 if (VIM_ISDIGIT(*s))
12125 {
12126 p0 = *s - '0';
12127 s++;
12128 }
12129
12130 if (*s == NUL
12131 /* *s == '^' cuts */
12132 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012133 && !spell_iswordp_w(word + i + k0,
12134 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012135 {
12136 if (k0 == k)
12137 /* this is just a piece of the string */
12138 continue;
12139
12140 if (p0 < pri)
12141 /* priority too low */
12142 continue;
12143 /* rule fits; stop search */
12144 break;
12145 }
12146 }
12147
12148 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
12149 == (c0 & 0xff))
12150 continue;
12151 }
12152
12153 /* replace string */
12154 ws = smp[n].sm_to_w;
12155 s = smp[n].sm_rules;
12156 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
12157 if (p0 == 1 && z == 0)
12158 {
12159 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012160 if (reslen > 0 && ws != NULL && *ws != NUL
12161 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012162 || wres[reslen - 1] == *ws))
12163 reslen--;
12164 z0 = 1;
12165 z = 1;
12166 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012167 if (ws != NULL)
12168 while (*ws != NUL && word[i + k0] != NUL)
12169 {
12170 word[i + k0] = *ws;
12171 k0++;
12172 ws++;
12173 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012174 if (k > k0)
12175 mch_memmove(word + i + k0, word + i + k,
12176 sizeof(int) * (STRLEN(word + i + k) + 1));
12177
12178 /* new "actual letter" */
12179 c = word[i];
12180 }
12181 else
12182 {
12183 /* no '<' rule used */
12184 i += k - 1;
12185 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012186 if (ws != NULL)
12187 while (*ws != NUL && ws[1] != NUL
12188 && reslen < MAXWLEN)
12189 {
12190 if (reslen == 0 || wres[reslen - 1] != *ws)
12191 wres[reslen++] = *ws;
12192 ws++;
12193 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012194 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012195 if (ws == NULL)
12196 c = NUL;
12197 else
12198 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012199 if (strstr((char *)s, "^^") != NULL)
12200 {
12201 if (c != NUL)
12202 wres[reslen++] = c;
12203 mch_memmove(word, word + i + 1,
12204 sizeof(int) * (STRLEN(word + i + 1) + 1));
12205 i = 0;
12206 z0 = 1;
12207 }
12208 }
12209 break;
12210 }
12211 }
12212 }
12213 else if (vim_iswhite(c))
12214 {
12215 c = ' ';
12216 k = 1;
12217 }
12218
12219 if (z0 == 0)
12220 {
12221 if (k && !p0 && reslen < MAXWLEN && c != NUL
12222 && (!slang->sl_collapse || reslen == 0
12223 || wres[reslen - 1] != c))
12224 /* condense only double letters */
12225 wres[reslen++] = c;
12226
12227 i++;
12228 z = 0;
12229 k = 0;
12230 }
12231 }
12232
12233 /* Convert wide characters in "wres" to a multi-byte string in "res". */
12234 l = 0;
12235 for (n = 0; n < reslen; ++n)
12236 {
12237 l += mb_char2bytes(wres[n], res + l);
12238 if (l + MB_MAXBYTES > MAXWLEN)
12239 break;
12240 }
12241 res[l] = NUL;
12242}
12243#endif
12244
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012245/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012246 * Compute a score for two sound-a-like words.
12247 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
12248 * Instead of a generic loop we write out the code. That keeps it fast by
12249 * avoiding checks that will not be possible.
12250 */
12251 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012252soundalike_score(goodstart, badstart)
12253 char_u *goodstart; /* sound-folded good word */
12254 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012255{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012256 char_u *goodsound = goodstart;
12257 char_u *badsound = badstart;
12258 int goodlen;
12259 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012260 int n;
12261 char_u *pl, *ps;
12262 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012263 int score = 0;
12264
12265 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
12266 * counted so much, vowels halfway the word aren't counted at all. */
12267 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
12268 {
12269 score = SCORE_DEL / 2;
12270 if (*badsound == '*')
12271 ++badsound;
12272 else
12273 ++goodsound;
12274 }
12275
12276 goodlen = STRLEN(goodsound);
12277 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012278
12279 /* Return quickly if the lenghts are too different to be fixed by two
12280 * changes. */
12281 n = goodlen - badlen;
12282 if (n < -2 || n > 2)
12283 return SCORE_MAXMAX;
12284
12285 if (n > 0)
12286 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012287 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012288 ps = badsound;
12289 }
12290 else
12291 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012292 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012293 ps = goodsound;
12294 }
12295
12296 /* Skip over the identical part. */
12297 while (*pl == *ps && *pl != NUL)
12298 {
12299 ++pl;
12300 ++ps;
12301 }
12302
12303 switch (n)
12304 {
12305 case -2:
12306 case 2:
12307 /*
12308 * Must delete two characters from "pl".
12309 */
12310 ++pl; /* first delete */
12311 while (*pl == *ps)
12312 {
12313 ++pl;
12314 ++ps;
12315 }
12316 /* strings must be equal after second delete */
12317 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012318 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012319
12320 /* Failed to compare. */
12321 break;
12322
12323 case -1:
12324 case 1:
12325 /*
12326 * Minimal one delete from "pl" required.
12327 */
12328
12329 /* 1: delete */
12330 pl2 = pl + 1;
12331 ps2 = ps;
12332 while (*pl2 == *ps2)
12333 {
12334 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012335 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012336 ++pl2;
12337 ++ps2;
12338 }
12339
12340 /* 2: delete then swap, then rest must be equal */
12341 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12342 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012343 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012344
12345 /* 3: delete then substitute, then the rest must be equal */
12346 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012347 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012348
12349 /* 4: first swap then delete */
12350 if (pl[0] == ps[1] && pl[1] == ps[0])
12351 {
12352 pl2 = pl + 2; /* swap, skip two chars */
12353 ps2 = ps + 2;
12354 while (*pl2 == *ps2)
12355 {
12356 ++pl2;
12357 ++ps2;
12358 }
12359 /* delete a char and then strings must be equal */
12360 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012361 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012362 }
12363
12364 /* 5: first substitute then delete */
12365 pl2 = pl + 1; /* substitute, skip one char */
12366 ps2 = ps + 1;
12367 while (*pl2 == *ps2)
12368 {
12369 ++pl2;
12370 ++ps2;
12371 }
12372 /* delete a char and then strings must be equal */
12373 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012374 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012375
12376 /* Failed to compare. */
12377 break;
12378
12379 case 0:
12380 /*
12381 * Lenghts are equal, thus changes must result in same length: An
12382 * insert is only possible in combination with a delete.
12383 * 1: check if for identical strings
12384 */
12385 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012386 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012387
12388 /* 2: swap */
12389 if (pl[0] == ps[1] && pl[1] == ps[0])
12390 {
12391 pl2 = pl + 2; /* swap, skip two chars */
12392 ps2 = ps + 2;
12393 while (*pl2 == *ps2)
12394 {
12395 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012396 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012397 ++pl2;
12398 ++ps2;
12399 }
12400 /* 3: swap and swap again */
12401 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12402 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012403 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012404
12405 /* 4: swap and substitute */
12406 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012407 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012408 }
12409
12410 /* 5: substitute */
12411 pl2 = pl + 1;
12412 ps2 = ps + 1;
12413 while (*pl2 == *ps2)
12414 {
12415 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012416 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012417 ++pl2;
12418 ++ps2;
12419 }
12420
12421 /* 6: substitute and swap */
12422 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12423 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012424 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012425
12426 /* 7: substitute and substitute */
12427 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012428 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012429
12430 /* 8: insert then delete */
12431 pl2 = pl;
12432 ps2 = ps + 1;
12433 while (*pl2 == *ps2)
12434 {
12435 ++pl2;
12436 ++ps2;
12437 }
12438 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012439 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012440
12441 /* 9: delete then insert */
12442 pl2 = pl + 1;
12443 ps2 = ps;
12444 while (*pl2 == *ps2)
12445 {
12446 ++pl2;
12447 ++ps2;
12448 }
12449 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012450 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012451
12452 /* Failed to compare. */
12453 break;
12454 }
12455
12456 return SCORE_MAXMAX;
12457}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012459/*
12460 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012461 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012462 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000012463 * The algorithm is described by Du and Chang, 1992.
12464 * The implementation of the algorithm comes from Aspell editdist.cpp,
12465 * edit_distance(). It has been converted from C++ to C and modified to
12466 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 */
12468 static int
12469spell_edit_score(badword, goodword)
12470 char_u *badword;
12471 char_u *goodword;
12472{
12473 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012474 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012475 int j, i;
12476 int t;
12477 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012478 int pbc, pgc;
12479#ifdef FEAT_MBYTE
12480 char_u *p;
12481 int wbadword[MAXWLEN];
12482 int wgoodword[MAXWLEN];
12483
12484 if (has_mbyte)
12485 {
12486 /* Get the characters from the multi-byte strings and put them in an
12487 * int array for easy access. */
12488 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012489 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012490 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012491 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012492 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012493 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012494 }
12495 else
12496#endif
12497 {
12498 badlen = STRLEN(badword) + 1;
12499 goodlen = STRLEN(goodword) + 1;
12500 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501
12502 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12503#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12505 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012506 if (cnt == NULL)
12507 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508
12509 CNT(0, 0) = 0;
12510 for (j = 1; j <= goodlen; ++j)
12511 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12512
12513 for (i = 1; i <= badlen; ++i)
12514 {
12515 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12516 for (j = 1; j <= goodlen; ++j)
12517 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012518#ifdef FEAT_MBYTE
12519 if (has_mbyte)
12520 {
12521 bc = wbadword[i - 1];
12522 gc = wgoodword[j - 1];
12523 }
12524 else
12525#endif
12526 {
12527 bc = badword[i - 1];
12528 gc = goodword[j - 1];
12529 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012530 if (bc == gc)
12531 CNT(i, j) = CNT(i - 1, j - 1);
12532 else
12533 {
12534 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012535 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012536 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12537 else
12538 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12539
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012540 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012542#ifdef FEAT_MBYTE
12543 if (has_mbyte)
12544 {
12545 pbc = wbadword[i - 2];
12546 pgc = wgoodword[j - 2];
12547 }
12548 else
12549#endif
12550 {
12551 pbc = badword[i - 2];
12552 pgc = goodword[j - 2];
12553 }
12554 if (bc == pgc && pbc == gc)
12555 {
12556 t = SCORE_SWAP + CNT(i - 2, j - 2);
12557 if (t < CNT(i, j))
12558 CNT(i, j) = t;
12559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012560 }
12561 t = SCORE_DEL + CNT(i - 1, j);
12562 if (t < CNT(i, j))
12563 CNT(i, j) = t;
12564 t = SCORE_INS + CNT(i, j - 1);
12565 if (t < CNT(i, j))
12566 CNT(i, j) = t;
12567 }
12568 }
12569 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012570
12571 i = CNT(badlen - 1, goodlen - 1);
12572 vim_free(cnt);
12573 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012574}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012575
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012576/*
12577 * ":spelldump"
12578 */
12579/*ARGSUSED*/
12580 void
12581ex_spelldump(eap)
12582 exarg_T *eap;
12583{
12584 buf_T *buf = curbuf;
12585 langp_T *lp;
12586 slang_T *slang;
12587 idx_T arridx[MAXWLEN];
12588 int curi[MAXWLEN];
12589 char_u word[MAXWLEN];
12590 int c;
12591 char_u *byts;
12592 idx_T *idxs;
12593 linenr_T lnum = 0;
12594 int round;
12595 int depth;
12596 int n;
12597 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012598 char_u *region_names = NULL; /* region names being used */
12599 int do_region = TRUE; /* dump region names and numbers */
12600 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012601 int lpi;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012602
Bram Moolenaar95529562005-08-25 21:21:38 +000012603 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012604 return;
12605
12606 /* Create a new empty buffer by splitting the window. */
12607 do_cmdline_cmd((char_u *)"new");
12608 if (!bufempty() || !buf_valid(buf))
12609 return;
12610
Bram Moolenaar7887d882005-07-01 22:33:52 +000012611 /* Find out if we can support regions: All languages must support the same
12612 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012613 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012614 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012615 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000012616 p = lp->lp_slang->sl_regions;
12617 if (p[0] != 0)
12618 {
12619 if (region_names == NULL) /* first language with regions */
12620 region_names = p;
12621 else if (STRCMP(region_names, p) != 0)
12622 {
12623 do_region = FALSE; /* region names are different */
12624 break;
12625 }
12626 }
12627 }
12628
12629 if (do_region && region_names != NULL)
12630 {
12631 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12632 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12633 }
12634 else
12635 do_region = FALSE;
12636
12637 /*
12638 * Loop over all files loaded for the entries in 'spelllang'.
12639 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012640 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012641 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012642 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012643 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012644 if (slang->sl_fbyts == NULL) /* reloading failed */
12645 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012646
12647 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12648 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12649
12650 /* round 1: case-folded tree
12651 * round 2: keep-case tree */
12652 for (round = 1; round <= 2; ++round)
12653 {
12654 if (round == 1)
12655 {
12656 byts = slang->sl_fbyts;
12657 idxs = slang->sl_fidxs;
12658 }
12659 else
12660 {
12661 byts = slang->sl_kbyts;
12662 idxs = slang->sl_kidxs;
12663 }
12664 if (byts == NULL)
12665 continue; /* array is empty */
12666
12667 depth = 0;
12668 arridx[0] = 0;
12669 curi[0] = 1;
12670 while (depth >= 0 && !got_int)
12671 {
12672 if (curi[depth] > byts[arridx[depth]])
12673 {
12674 /* Done all bytes at this node, go up one level. */
12675 --depth;
12676 line_breakcheck();
12677 }
12678 else
12679 {
12680 /* Do one more byte at this node. */
12681 n = arridx[depth] + curi[depth];
12682 ++curi[depth];
12683 c = byts[n];
12684 if (c == 0)
12685 {
12686 /* End of word, deal with the word.
12687 * Don't use keep-case words in the fold-case tree,
12688 * they will appear in the keep-case tree.
12689 * Only use the word when the region matches. */
12690 flags = (int)idxs[n];
12691 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012692 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000012693 && (do_region
12694 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012695 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012696 & lp->lp_region) != 0))
12697 {
12698 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012699 if (!do_region)
12700 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012701
12702 /* Dump the basic word if there is no prefix or
12703 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012704 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012705 if (c == 0 || curi[depth] == 2)
12706 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012707
12708 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012709 if (c != 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012710 lnum = dump_prefixes(slang, word, round,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012711 flags, lnum);
12712 }
12713 }
12714 else
12715 {
12716 /* Normal char, go one level deeper. */
12717 word[depth++] = c;
12718 arridx[depth] = idxs[n];
12719 curi[depth] = 1;
12720 }
12721 }
12722 }
12723 }
12724 }
12725
12726 /* Delete the empty line that we started with. */
12727 if (curbuf->b_ml.ml_line_count > 1)
12728 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12729
12730 redraw_later(NOT_VALID);
12731}
12732
12733/*
12734 * Dump one word: apply case modifications and append a line to the buffer.
12735 */
12736 static void
12737dump_word(word, round, flags, lnum)
12738 char_u *word;
12739 int round;
12740 int flags;
12741 linenr_T lnum;
12742{
12743 int keepcap = FALSE;
12744 char_u *p;
12745 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012746 char_u badword[MAXWLEN + 10];
12747 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012748
12749 if (round == 1 && (flags & WF_CAPMASK) != 0)
12750 {
12751 /* Need to fix case according to "flags". */
12752 make_case_word(word, cword, flags);
12753 p = cword;
12754 }
12755 else
12756 {
12757 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012758 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12759 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012760 keepcap = TRUE;
12761 }
12762
Bram Moolenaar7887d882005-07-01 22:33:52 +000012763 /* Add flags and regions after a slash. */
12764 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012765 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012766 STRCPY(badword, p);
12767 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012768 if (keepcap)
12769 STRCAT(badword, "=");
12770 if (flags & WF_BANNED)
12771 STRCAT(badword, "!");
12772 else if (flags & WF_RARE)
12773 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012774 if (flags & WF_REGION)
12775 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012776 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012777 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012778 p = badword;
12779 }
12780
12781 ml_append(lnum, p, (colnr_T)0, FALSE);
12782}
12783
12784/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012785 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12786 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012787 * Return the updated line number.
12788 */
12789 static linenr_T
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012790dump_prefixes(slang, word, round, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012791 slang_T *slang;
12792 char_u *word; /* case-folded word */
12793 int round;
12794 int flags; /* flags with prefix ID */
12795 linenr_T startlnum;
12796{
12797 idx_T arridx[MAXWLEN];
12798 int curi[MAXWLEN];
12799 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012800 char_u word_up[MAXWLEN];
12801 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012802 int c;
12803 char_u *byts;
12804 idx_T *idxs;
12805 linenr_T lnum = startlnum;
12806 int depth;
12807 int n;
12808 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012809 int i;
12810
Bram Moolenaar53805d12005-08-01 07:08:33 +000012811 /* if the word starts with a lower-case letter make the word with an
12812 * upper-case letter in word_up[]. */
12813 c = PTR2CHAR(word);
12814 if (SPELL_TOUPPER(c) != c)
12815 {
12816 onecap_copy(word, word_up, TRUE);
12817 has_word_up = TRUE;
12818 }
12819
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012820 byts = slang->sl_pbyts;
12821 idxs = slang->sl_pidxs;
12822 if (byts != NULL) /* array not is empty */
12823 {
12824 /*
12825 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012826 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012827 */
12828 depth = 0;
12829 arridx[0] = 0;
12830 curi[0] = 1;
12831 while (depth >= 0 && !got_int)
12832 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012833 n = arridx[depth];
12834 len = byts[n];
12835 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012836 {
12837 /* Done all bytes at this node, go up one level. */
12838 --depth;
12839 line_breakcheck();
12840 }
12841 else
12842 {
12843 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012844 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012845 ++curi[depth];
12846 c = byts[n];
12847 if (c == 0)
12848 {
12849 /* End of prefix, find out how many IDs there are. */
12850 for (i = 1; i < len; ++i)
12851 if (byts[n + i] != 0)
12852 break;
12853 curi[depth] += i - 1;
12854
Bram Moolenaar53805d12005-08-01 07:08:33 +000012855 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12856 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012857 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012858 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012859 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012860 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012861 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012862 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012863
12864 /* Check for prefix that matches the word when the
12865 * first letter is upper-case, but only if the prefix has
12866 * a condition. */
12867 if (has_word_up)
12868 {
12869 c = valid_word_prefix(i, n, flags, word_up, slang,
12870 TRUE);
12871 if (c != 0)
12872 {
12873 vim_strncpy(prefix + depth, word_up,
12874 MAXWLEN - depth - 1);
12875 dump_word(prefix, round,
12876 (c & WF_RAREPFX) ? (flags | WF_RARE)
12877 : flags, lnum++);
12878 }
12879 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012880 }
12881 else
12882 {
12883 /* Normal char, go one level deeper. */
12884 prefix[depth++] = c;
12885 arridx[depth] = idxs[n];
12886 curi[depth] = 1;
12887 }
12888 }
12889 }
12890 }
12891
12892 return lnum;
12893}
12894
Bram Moolenaar95529562005-08-25 21:21:38 +000012895/*
12896 * Move "p" to end of word.
12897 */
12898 char_u *
12899spell_to_word_end(start, buf)
12900 char_u *start;
12901 buf_T *buf;
12902{
12903 char_u *p = start;
12904
12905 while (*p != NUL && spell_iswordp(p, buf))
12906 mb_ptr_adv(p);
12907 return p;
12908}
12909
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012910#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012911/*
12912 * Find start of the word in front of the cursor. We don't check if it is
12913 * badly spelled, with completion we can only change the word in front of the
12914 * cursor.
12915 * Used for Insert mode completion CTRL-X ?.
12916 * Returns the column number of the word.
12917 */
12918 int
12919spell_word_start(startcol)
12920 int startcol;
12921{
12922 char_u *line;
12923 char_u *p;
12924 int col = 0;
12925
Bram Moolenaar95529562005-08-25 21:21:38 +000012926 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012927 return startcol;
12928
12929 /* Find a word character before "startcol". */
12930 line = ml_get_curline();
12931 for (p = line + startcol; p > line; )
12932 {
12933 mb_ptr_back(line, p);
12934 if (spell_iswordp_nmw(p))
12935 break;
12936 }
12937
12938 /* Go back to start of the word. */
12939 while (p > line)
12940 {
12941 col = p - line;
12942 mb_ptr_back(line, p);
12943 if (!spell_iswordp(p, curbuf))
12944 break;
12945 col = 0;
12946 }
12947
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012948 return col;
12949}
12950
12951/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000012952 * Need to check for 'spellcapcheck' now, the word is removed before
12953 * expand_spelling() is called. Therefore the ugly global variable.
12954 */
12955static int spell_expand_need_cap;
12956
12957 void
12958spell_expand_check_cap(col)
12959 colnr_T col;
12960{
12961 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12962}
12963
12964/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012965 * Get list of spelling suggestions.
12966 * Used for Insert mode completion CTRL-X ?.
12967 * Returns the number of matches. The matches are in "matchp[]", array of
12968 * allocated strings.
12969 */
12970/*ARGSUSED*/
12971 int
12972expand_spelling(lnum, col, pat, matchp)
12973 linenr_T lnum;
12974 int col;
12975 char_u *pat;
12976 char_u ***matchp;
12977{
12978 garray_T ga;
12979
12980 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12981 *matchp = ga.ga_data;
12982 return ga.ga_len;
12983}
12984#endif
12985
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012986#endif /* FEAT_SYN_HL */