blob: f57ff0c173aeead6374fa8dd25a22fc72d345ee4 [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 Moolenaar0c405862005-06-22 22:26:26 +0000854 * 0X99FF. But when a word character follows do check spelling to find
855 * "3GPP". */
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
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000861 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000862 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000863 nrlen = mi.mi_end - ptr;
864 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000865 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000867
868 /* Try including the digits in the word. */
869 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000871 else
872 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000873
Bram Moolenaar0c405862005-06-22 22:26:26 +0000874 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000875 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000876 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000877 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000878 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000879 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000880 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000881 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000882
883 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
884 {
885 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000886 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000887 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000889 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000890 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000891 if (capcol != NULL)
892 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000893
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000894 /* We always use the characters up to the next non-word character,
895 * also for bad words. */
896 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000897
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000898 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000899 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000900
Bram Moolenaar5195e452005-08-19 20:32:47 +0000901 /* case-fold the word with one non-word character, so that we can check
902 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000903 if (*mi.mi_fend != NUL)
904 mb_ptr_adv(mi.mi_fend);
905
906 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
907 MAXWLEN + 1);
908 mi.mi_fwordlen = STRLEN(mi.mi_fword);
909
910 /* The word is bad unless we recognize it. */
911 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000912 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913
914 /*
915 * Loop over the languages specified in 'spelllang'.
916 * We check them all, because a matching word may be longer than an
917 * already found matching word.
918 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000919 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000920 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000921 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
922
923 /* If reloading fails the language is still in the list but everything
924 * has been cleared. */
925 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
926 continue;
927
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000928 /* Check for a matching word in case-folded words. */
929 find_word(&mi, FIND_FOLDWORD);
930
931 /* Check for a matching word in keep-case words. */
932 find_word(&mi, FIND_KEEPWORD);
933
934 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000935 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000936
937 /* For a NOBREAK language, may want to use a word without a following
938 * word as a backup. */
939 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
940 && mi.mi_result2 != SP_BAD)
941 {
942 mi.mi_result = mi.mi_result2;
943 mi.mi_end = mi.mi_end2;
944 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000945 }
946
947 if (mi.mi_result != SP_OK)
948 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000949 /* If we found a number skip over it. Allows for "42nd". Do flag
950 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000951 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000952 {
953 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
954 return nrlen;
955 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000956
957 /* When we are at a non-word character there is no error, just
958 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000959 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000960 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000961 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
962 {
963 regmatch_T regmatch;
964
965 /* Check for end of sentence. */
966 regmatch.regprog = wp->w_buffer->b_cap_prog;
967 regmatch.rm_ic = FALSE;
968 if (vim_regexec(&regmatch, ptr, 0))
969 *capcol = (int)(regmatch.endp[0] - ptr);
970 }
971
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000972#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000973 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000974 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000975#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000976 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000977 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000978 else if (mi.mi_end == ptr)
979 /* Always include at least one character. Required for when there
980 * is a mixup in "midword". */
981 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000982 else if (mi.mi_result == SP_BAD
983 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
984 {
985 char_u *p, *fp;
986 int save_result = mi.mi_result;
987
988 /* First language in 'spelllang' is NOBREAK. Find first position
989 * at which any word would be valid. */
990 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000991 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000992 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000993 p = mi.mi_word;
994 fp = mi.mi_fword;
995 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000996 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000997 mb_ptr_adv(p);
998 mb_ptr_adv(fp);
999 if (p >= mi.mi_end)
1000 break;
1001 mi.mi_compoff = fp - mi.mi_fword;
1002 find_word(&mi, FIND_COMPOUND);
1003 if (mi.mi_result != SP_BAD)
1004 {
1005 mi.mi_end = p;
1006 break;
1007 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001008 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001009 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001010 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001011 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001012
1013 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001014 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001015 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001016 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001017 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001018 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 }
1020
Bram Moolenaar5195e452005-08-19 20:32:47 +00001021 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1022 {
1023 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001024 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001025 return wrongcaplen;
1026 }
1027
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028 return (int)(mi.mi_end - ptr);
1029}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001030
Bram Moolenaar51485f02005-06-04 21:55:20 +00001031/*
1032 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001033 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1034 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1035 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1036 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037 *
1038 * For a match mip->mi_result is updated.
1039 */
1040 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001041find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001043 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001044{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001045 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001046 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001047 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 int endidxcnt = 0;
1049 int len;
1050 int wlen = 0;
1051 int flen;
1052 int c;
1053 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001054 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001055#ifdef FEAT_MBYTE
1056 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001057#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001058 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001059 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 slang_T *slang = mip->mi_lp->lp_slang;
1061 unsigned flags;
1062 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001063 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001064 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001065 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001066 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001067
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001068 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001069 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001070 /* Check for word with matching case in keep-case tree. */
1071 ptr = mip->mi_word;
1072 flen = 9999; /* no case folding, always enough bytes */
1073 byts = slang->sl_kbyts;
1074 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001075
1076 if (mode == FIND_KEEPCOMPOUND)
1077 /* Skip over the previously found word(s). */
1078 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001079 }
1080 else
1081 {
1082 /* Check for case-folded in case-folded tree. */
1083 ptr = mip->mi_fword;
1084 flen = mip->mi_fwordlen; /* available case-folded bytes */
1085 byts = slang->sl_fbyts;
1086 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001087
1088 if (mode == FIND_PREFIX)
1089 {
1090 /* Skip over the prefix. */
1091 wlen = mip->mi_prefixlen;
1092 flen -= mip->mi_prefixlen;
1093 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001094 else if (mode == FIND_COMPOUND)
1095 {
1096 /* Skip over the previously found word(s). */
1097 wlen = mip->mi_compoff;
1098 flen -= mip->mi_compoff;
1099 }
1100
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001101 }
1102
Bram Moolenaar51485f02005-06-04 21:55:20 +00001103 if (byts == NULL)
1104 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001105
Bram Moolenaar51485f02005-06-04 21:55:20 +00001106 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001107 * Repeat advancing in the tree until:
1108 * - there is a byte that doesn't match,
1109 * - we reach the end of the tree,
1110 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001111 */
1112 for (;;)
1113 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001114 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001115 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001116
1117 len = byts[arridx++];
1118
1119 /* If the first possible byte is a zero the word could end here.
1120 * Remember this index, we first check for the longest word. */
1121 if (byts[arridx] == 0)
1122 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001123 if (endidxcnt == MAXWLEN)
1124 {
1125 /* Must be a corrupted spell file. */
1126 EMSG(_(e_format));
1127 return;
1128 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001129 endlen[endidxcnt] = wlen;
1130 endidx[endidxcnt++] = arridx++;
1131 --len;
1132
1133 /* Skip over the zeros, there can be several flag/region
1134 * combinations. */
1135 while (len > 0 && byts[arridx] == 0)
1136 {
1137 ++arridx;
1138 --len;
1139 }
1140 if (len == 0)
1141 break; /* no children, word must end here */
1142 }
1143
1144 /* Stop looking at end of the line. */
1145 if (ptr[wlen] == NUL)
1146 break;
1147
1148 /* Perform a binary search in the list of accepted bytes. */
1149 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001150 if (c == TAB) /* <Tab> is handled like <Space> */
1151 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001152 lo = arridx;
1153 hi = arridx + len - 1;
1154 while (lo < hi)
1155 {
1156 m = (lo + hi) / 2;
1157 if (byts[m] > c)
1158 hi = m - 1;
1159 else if (byts[m] < c)
1160 lo = m + 1;
1161 else
1162 {
1163 lo = hi = m;
1164 break;
1165 }
1166 }
1167
1168 /* Stop if there is no matching byte. */
1169 if (hi < lo || byts[lo] != c)
1170 break;
1171
1172 /* Continue at the child (if there is one). */
1173 arridx = idxs[lo];
1174 ++wlen;
1175 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001176
1177 /* One space in the good word may stand for several spaces in the
1178 * checked word. */
1179 if (c == ' ')
1180 {
1181 for (;;)
1182 {
1183 if (flen <= 0 && *mip->mi_fend != NUL)
1184 flen = fold_more(mip);
1185 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1186 break;
1187 ++wlen;
1188 --flen;
1189 }
1190 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001191 }
1192
1193 /*
1194 * Verify that one of the possible endings is valid. Try the longest
1195 * first.
1196 */
1197 while (endidxcnt > 0)
1198 {
1199 --endidxcnt;
1200 arridx = endidx[endidxcnt];
1201 wlen = endlen[endidxcnt];
1202
1203#ifdef FEAT_MBYTE
1204 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1205 continue; /* not at first byte of character */
1206#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001207 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001208 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001209 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001210 continue; /* next char is a word character */
1211 word_ends = FALSE;
1212 }
1213 else
1214 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001215 /* The prefix flag is before compound flags. Once a valid prefix flag
1216 * has been found we try compound flags. */
1217 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218
1219#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001221 {
1222 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001223 * when folding case. This can be slow, take a shortcut when the
1224 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001225 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001226 if (STRNCMP(ptr, p, wlen) != 0)
1227 {
1228 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1229 mb_ptr_adv(p);
1230 wlen = p - mip->mi_word;
1231 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001232 }
1233#endif
1234
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001235 /* Check flags and region. For FIND_PREFIX check the condition and
1236 * prefix ID.
1237 * Repeat this if there are more flags/region alternatives until there
1238 * is a match. */
1239 res = SP_BAD;
1240 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1241 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242 {
1243 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001244
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001245 /* For the fold-case tree check that the case of the checked word
1246 * matches with what the word in the tree requires.
1247 * For keep-case tree the case is always right. For prefixes we
1248 * don't bother to check. */
1249 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001251 if (mip->mi_cend != mip->mi_word + wlen)
1252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001253 /* mi_capflags was set for a different word length, need
1254 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001256 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001257 }
1258
Bram Moolenaar0c405862005-06-22 22:26:26 +00001259 if (mip->mi_capflags == WF_KEEPCAP
1260 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001261 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001262 }
1263
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001264 /* When mode is FIND_PREFIX the word must support the prefix:
1265 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001266 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001267 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001268 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001269 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001270 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001271 mip->mi_word + mip->mi_cprefixlen, slang,
1272 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001273 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001274 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001275
1276 /* Use the WF_RARE flag for a rare prefix. */
1277 if (c & WF_RAREPFX)
1278 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001279 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001280 }
1281
Bram Moolenaar78622822005-08-23 21:00:13 +00001282 if (slang->sl_nobreak)
1283 {
1284 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1285 && (flags & WF_BANNED) == 0)
1286 {
1287 /* NOBREAK: found a valid following word. That's all we
1288 * need to know, so return. */
1289 mip->mi_result = SP_OK;
1290 break;
1291 }
1292 }
1293
1294 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1295 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001296 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001297 /* If there is no flag or the word is shorter than
1298 * COMPOUNDMIN reject it quickly.
1299 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001300 * that's too short... Myspell compatibility requires this
1301 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001302 if (((unsigned)flags >> 24) == 0
1303 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001304 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001305#ifdef FEAT_MBYTE
1306 /* For multi-byte chars check character length against
1307 * COMPOUNDMIN. */
1308 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001309 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001310 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1311 wlen - mip->mi_compoff) < slang->sl_compminlen)
1312 continue;
1313#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001314
Bram Moolenaare52325c2005-08-22 22:54:29 +00001315 /* Limit the number of compound words to COMPOUNDMAX if no
1316 * maximum for syllables is specified. */
1317 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1318 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001319 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001320
Bram Moolenaard12a1322005-08-21 22:08:24 +00001321 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001322 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001323 ? slang->sl_compstartflags
1324 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001325 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001326 continue;
1327
Bram Moolenaare52325c2005-08-22 22:54:29 +00001328 if (mode == FIND_COMPOUND)
1329 {
1330 int capflags;
1331
1332 /* Need to check the caps type of the appended compound
1333 * word. */
1334#ifdef FEAT_MBYTE
1335 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1336 mip->mi_compoff) != 0)
1337 {
1338 /* case folding may have changed the length */
1339 p = mip->mi_word;
1340 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1341 mb_ptr_adv(p);
1342 }
1343 else
1344#endif
1345 p = mip->mi_word + mip->mi_compoff;
1346 capflags = captype(p, mip->mi_word + wlen);
1347 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1348 && (flags & WF_FIXCAP) != 0))
1349 continue;
1350
1351 if (capflags != WF_ALLCAP)
1352 {
1353 /* When the character before the word is a word
1354 * character we do not accept a Onecap word. We do
1355 * accept a no-caps word, even when the dictionary
1356 * word specifies ONECAP. */
1357 mb_ptr_back(mip->mi_word, p);
1358 if (spell_iswordp_nmw(p)
1359 ? capflags == WF_ONECAP
1360 : (flags & WF_ONECAP) != 0
1361 && capflags != WF_ONECAP)
1362 continue;
1363 }
1364 }
1365
Bram Moolenaar5195e452005-08-19 20:32:47 +00001366 /* If the word ends the sequence of compound flags of the
1367 * words must match with one of the COMPOUNDFLAGS items and
1368 * the number of syllables must not be too large. */
1369 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1370 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1371 if (word_ends)
1372 {
1373 char_u fword[MAXWLEN];
1374
1375 if (slang->sl_compsylmax < MAXWLEN)
1376 {
1377 /* "fword" is only needed for checking syllables. */
1378 if (ptr == mip->mi_word)
1379 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1380 else
1381 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1382 }
1383 if (!can_compound(slang, fword, mip->mi_compflags))
1384 continue;
1385 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001386 }
1387
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001388 /* Check NEEDCOMPOUND: can't use word without compounding. */
1389 else if (flags & WF_NEEDCOMP)
1390 continue;
1391
Bram Moolenaar78622822005-08-23 21:00:13 +00001392 nobreak_result = SP_OK;
1393
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001394 if (!word_ends)
1395 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001396 int save_result = mip->mi_result;
1397 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001398 langp_T *save_lp = mip->mi_lp;
1399 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001400
1401 /* Check that a valid word follows. If there is one and we
1402 * are compounding, it will set "mi_result", thus we are
1403 * always finished here. For NOBREAK we only check that a
1404 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001405 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001406 if (slang->sl_nobreak)
1407 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001408
1409 /* Find following word in case-folded tree. */
1410 mip->mi_compoff = endlen[endidxcnt];
1411#ifdef FEAT_MBYTE
1412 if (has_mbyte && mode == FIND_KEEPWORD)
1413 {
1414 /* Compute byte length in case-folded word from "wlen":
1415 * byte length in keep-case word. Length may change when
1416 * folding case. This can be slow, take a shortcut when
1417 * the case-folded word is equal to the keep-case word. */
1418 p = mip->mi_fword;
1419 if (STRNCMP(ptr, p, wlen) != 0)
1420 {
1421 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1422 mb_ptr_adv(p);
1423 mip->mi_compoff = p - mip->mi_fword;
1424 }
1425 }
1426#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001427 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001428 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001429
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001430 /* For NOBREAK we need to try all NOBREAK languages, at least
1431 * to find the ".add" file(s). */
1432 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001433 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001434 if (slang->sl_nobreak)
1435 {
1436 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1437 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1438 || !mip->mi_lp->lp_slang->sl_nobreak)
1439 continue;
1440 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001441
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001442 find_word(mip, FIND_COMPOUND);
1443
1444 /* When NOBREAK any word that matches is OK. Otherwise we
1445 * need to find the longest match, thus try with keep-case
1446 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001447 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1448 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001449 /* Find following word in keep-case tree. */
1450 mip->mi_compoff = wlen;
1451 find_word(mip, FIND_KEEPCOMPOUND);
1452
1453 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1454 {
1455 /* Check for following word with prefix. */
1456 mip->mi_compoff = c;
1457 find_prefix(mip, FIND_COMPOUND);
1458 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001459 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001460
1461 if (!slang->sl_nobreak)
1462 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001463 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001464 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001465 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001466
Bram Moolenaar78622822005-08-23 21:00:13 +00001467 if (slang->sl_nobreak)
1468 {
1469 nobreak_result = mip->mi_result;
1470 mip->mi_result = save_result;
1471 mip->mi_end = save_end;
1472 }
1473 else
1474 {
1475 if (mip->mi_result == SP_OK)
1476 break;
1477 continue;
1478 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001479 }
1480
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001481 if (flags & WF_BANNED)
1482 res = SP_BANNED;
1483 else if (flags & WF_REGION)
1484 {
1485 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001486 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001487 res = SP_OK;
1488 else
1489 res = SP_LOCAL;
1490 }
1491 else if (flags & WF_RARE)
1492 res = SP_RARE;
1493 else
1494 res = SP_OK;
1495
Bram Moolenaar78622822005-08-23 21:00:13 +00001496 /* Always use the longest match and the best result. For NOBREAK
1497 * we separately keep the longest match without a following good
1498 * word as a fall-back. */
1499 if (nobreak_result == SP_BAD)
1500 {
1501 if (mip->mi_result2 > res)
1502 {
1503 mip->mi_result2 = res;
1504 mip->mi_end2 = mip->mi_word + wlen;
1505 }
1506 else if (mip->mi_result2 == res
1507 && mip->mi_end2 < mip->mi_word + wlen)
1508 mip->mi_end2 = mip->mi_word + wlen;
1509 }
1510 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001511 {
1512 mip->mi_result = res;
1513 mip->mi_end = mip->mi_word + wlen;
1514 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001515 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001516 mip->mi_end = mip->mi_word + wlen;
1517
Bram Moolenaar78622822005-08-23 21:00:13 +00001518 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001519 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001520 }
1521
Bram Moolenaar78622822005-08-23 21:00:13 +00001522 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001523 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001524 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001525}
1526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001527/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001528 * Return TRUE if "flags" is a valid sequence of compound flags and
1529 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001530 */
1531 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001532can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001533 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001534 char_u *word;
1535 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001536{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001537 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001538#ifdef FEAT_MBYTE
1539 char_u uflags[MAXWLEN * 2];
1540 int i;
1541#endif
1542 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001543
1544 if (slang->sl_compprog == NULL)
1545 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001546#ifdef FEAT_MBYTE
1547 if (enc_utf8)
1548 {
1549 /* Need to convert the single byte flags to utf8 characters. */
1550 p = uflags;
1551 for (i = 0; flags[i] != NUL; ++i)
1552 p += mb_char2bytes(flags[i], p);
1553 *p = NUL;
1554 p = uflags;
1555 }
1556 else
1557#endif
1558 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001559 regmatch.regprog = slang->sl_compprog;
1560 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001561 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001562 return FALSE;
1563
Bram Moolenaare52325c2005-08-22 22:54:29 +00001564 /* Count the number of syllables. This may be slow, do it last. If there
1565 * are too many syllables AND the number of compound words is above
1566 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001567 if (slang->sl_compsylmax < MAXWLEN
1568 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001569 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001570 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001571}
1572
1573/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001574 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1575 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001576 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001577 */
1578 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001579valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001580 int totprefcnt; /* nr of prefix IDs */
1581 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001582 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001583 char_u *word;
1584 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001585 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001586{
1587 int prefcnt;
1588 int pidx;
1589 regprog_T *rp;
1590 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001591 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001592
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001593 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001594 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1595 {
1596 pidx = slang->sl_pidxs[arridx + prefcnt];
1597
1598 /* Check the prefix ID. */
1599 if (prefid != (pidx & 0xff))
1600 continue;
1601
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001602 /* Check if the prefix doesn't combine and the word already has a
1603 * suffix. */
1604 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1605 continue;
1606
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001607 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001608 * stored in the two bytes above the prefix ID byte. */
1609 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001610 if (rp != NULL)
1611 {
1612 regmatch.regprog = rp;
1613 regmatch.rm_ic = FALSE;
1614 if (!vim_regexec(&regmatch, word, 0))
1615 continue;
1616 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001617 else if (cond_req)
1618 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001619
Bram Moolenaar53805d12005-08-01 07:08:33 +00001620 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001621 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001622 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001623 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001624}
1625
1626/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001627 * Check if the word at "mip->mi_word" has a matching prefix.
1628 * If it does, then check the following word.
1629 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001630 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1631 * prefix in a compound word.
1632 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001633 * For a match mip->mi_result is updated.
1634 */
1635 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001636find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001637 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001638 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001639{
1640 idx_T arridx = 0;
1641 int len;
1642 int wlen = 0;
1643 int flen;
1644 int c;
1645 char_u *ptr;
1646 idx_T lo, hi, m;
1647 slang_T *slang = mip->mi_lp->lp_slang;
1648 char_u *byts;
1649 idx_T *idxs;
1650
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001651 byts = slang->sl_pbyts;
1652 if (byts == NULL)
1653 return; /* array is empty */
1654
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001655 /* We use the case-folded word here, since prefixes are always
1656 * case-folded. */
1657 ptr = mip->mi_fword;
1658 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001659 if (mode == FIND_COMPOUND)
1660 {
1661 /* Skip over the previously found word(s). */
1662 ptr += mip->mi_compoff;
1663 flen -= mip->mi_compoff;
1664 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001665 idxs = slang->sl_pidxs;
1666
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001667 /*
1668 * Repeat advancing in the tree until:
1669 * - there is a byte that doesn't match,
1670 * - we reach the end of the tree,
1671 * - or we reach the end of the line.
1672 */
1673 for (;;)
1674 {
1675 if (flen == 0 && *mip->mi_fend != NUL)
1676 flen = fold_more(mip);
1677
1678 len = byts[arridx++];
1679
1680 /* If the first possible byte is a zero the prefix could end here.
1681 * Check if the following word matches and supports the prefix. */
1682 if (byts[arridx] == 0)
1683 {
1684 /* There can be several prefixes with different conditions. We
1685 * try them all, since we don't know which one will give the
1686 * longest match. The word is the same each time, pass the list
1687 * of possible prefixes to find_word(). */
1688 mip->mi_prefarridx = arridx;
1689 mip->mi_prefcnt = len;
1690 while (len > 0 && byts[arridx] == 0)
1691 {
1692 ++arridx;
1693 --len;
1694 }
1695 mip->mi_prefcnt -= len;
1696
1697 /* Find the word that comes after the prefix. */
1698 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001699 if (mode == FIND_COMPOUND)
1700 /* Skip over the previously found word(s). */
1701 mip->mi_prefixlen += mip->mi_compoff;
1702
Bram Moolenaar53805d12005-08-01 07:08:33 +00001703#ifdef FEAT_MBYTE
1704 if (has_mbyte)
1705 {
1706 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001707 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1708 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001709 }
1710 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001711 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001712#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001713 find_word(mip, FIND_PREFIX);
1714
1715
1716 if (len == 0)
1717 break; /* no children, word must end here */
1718 }
1719
1720 /* Stop looking at end of the line. */
1721 if (ptr[wlen] == NUL)
1722 break;
1723
1724 /* Perform a binary search in the list of accepted bytes. */
1725 c = ptr[wlen];
1726 lo = arridx;
1727 hi = arridx + len - 1;
1728 while (lo < hi)
1729 {
1730 m = (lo + hi) / 2;
1731 if (byts[m] > c)
1732 hi = m - 1;
1733 else if (byts[m] < c)
1734 lo = m + 1;
1735 else
1736 {
1737 lo = hi = m;
1738 break;
1739 }
1740 }
1741
1742 /* Stop if there is no matching byte. */
1743 if (hi < lo || byts[lo] != c)
1744 break;
1745
1746 /* Continue at the child (if there is one). */
1747 arridx = idxs[lo];
1748 ++wlen;
1749 --flen;
1750 }
1751}
1752
1753/*
1754 * Need to fold at least one more character. Do until next non-word character
1755 * for efficiency.
1756 * Return the length of the folded chars in bytes.
1757 */
1758 static int
1759fold_more(mip)
1760 matchinf_T *mip;
1761{
1762 int flen;
1763 char_u *p;
1764
1765 p = mip->mi_fend;
1766 do
1767 {
1768 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001769 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001770
1771 /* Include the non-word character so that we can check for the
1772 * word end. */
1773 if (*mip->mi_fend != NUL)
1774 mb_ptr_adv(mip->mi_fend);
1775
1776 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1777 mip->mi_fword + mip->mi_fwordlen,
1778 MAXWLEN - mip->mi_fwordlen);
1779 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1780 mip->mi_fwordlen += flen;
1781 return flen;
1782}
1783
1784/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001785 * Check case flags for a word. Return TRUE if the word has the requested
1786 * case.
1787 */
1788 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001789spell_valid_case(wordflags, treeflags)
1790 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001791 int treeflags; /* flags for the word in the spell tree */
1792{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001793 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001794 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001795 && ((treeflags & WF_ONECAP) == 0
1796 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797}
1798
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799/*
1800 * Return TRUE if spell checking is not enabled.
1801 */
1802 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001803no_spell_checking(wp)
1804 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001805{
Bram Moolenaar95529562005-08-25 21:21:38 +00001806 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001807 {
1808 EMSG(_("E756: Spell checking is not enabled"));
1809 return TRUE;
1810 }
1811 return FALSE;
1812}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001813
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001814/*
1815 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001816 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1817 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001818 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1819 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001820 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001821 */
1822 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001823spell_move_to(wp, dir, allwords, curline, attrp)
1824 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001825 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001826 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001827 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001828 hlf_T *attrp; /* return: attributes of bad word or NULL
1829 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001831 linenr_T lnum;
1832 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001833 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001834 char_u *line;
1835 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001836 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001837 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001838 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001839 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001840 int col;
1841 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001842 char_u *buf = NULL;
1843 int buflen = 0;
1844 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001845 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001846 int found_one = FALSE;
1847 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001848
Bram Moolenaar95529562005-08-25 21:21:38 +00001849 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001850 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001851
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001852 /*
1853 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001854 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001855 *
1856 * When searching backwards, we continue in the line to find the last
1857 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001858 *
1859 * We concatenate the start of the next line, so that wrapped words work
1860 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1861 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001862 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001863 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001864 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001865
1866 while (!got_int)
1867 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001868 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001869
Bram Moolenaar0c405862005-06-22 22:26:26 +00001870 len = STRLEN(line);
1871 if (buflen < len + MAXWLEN + 2)
1872 {
1873 vim_free(buf);
1874 buflen = len + MAXWLEN + 2;
1875 buf = alloc(buflen);
1876 if (buf == NULL)
1877 break;
1878 }
1879
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001880 /* In first line check first word for Capital. */
1881 if (lnum == 1)
1882 capcol = 0;
1883
1884 /* For checking first word with a capital skip white space. */
1885 if (capcol == 0)
1886 capcol = skipwhite(line) - line;
1887
Bram Moolenaar0c405862005-06-22 22:26:26 +00001888 /* Copy the line into "buf" and append the start of the next line if
1889 * possible. */
1890 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001891 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001892 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1893
1894 p = buf + skip;
1895 endp = buf + len;
1896 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001897 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001898 /* When searching backward don't search after the cursor. Unless
1899 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001900 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001901 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001902 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001903 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904 break;
1905
1906 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001907 attr = HLF_COUNT;
Bram Moolenaar95529562005-08-25 21:21:38 +00001908 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001909
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001910 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001911 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001912 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001913 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001914 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001915 found_one = TRUE;
1916
Bram Moolenaar51485f02005-06-04 21:55:20 +00001917 /* When searching forward only accept a bad word after
1918 * the cursor. */
1919 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001920 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001921 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001922 && (wrapped
1923 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001924 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001925 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001926 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001927 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001928 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001929 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001930 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001931 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001932 }
1933 else
1934 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001935
Bram Moolenaar51485f02005-06-04 21:55:20 +00001936 if (can_spell)
1937 {
1938 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001939 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001940#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001941 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001942#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001943 if (dir == FORWARD)
1944 {
1945 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001946 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001947 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001948 if (attrp != NULL)
1949 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001950 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001951 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001952 else if (curline)
1953 /* Insert mode completion: put cursor after
1954 * the bad word. */
1955 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001956 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001957 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001958 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001959 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001960 }
1961
Bram Moolenaar51485f02005-06-04 21:55:20 +00001962 /* advance to character after the word */
1963 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001964 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001965 }
1966
Bram Moolenaar5195e452005-08-19 20:32:47 +00001967 if (dir == BACKWARD && found_pos.lnum != 0)
1968 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001969 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001970 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001971 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001972 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001973 }
1974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001975 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001976 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001977
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001978 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001979 if (dir == BACKWARD)
1980 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001981 /* If we are back at the starting line and searched it again there
1982 * is no match, give up. */
1983 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001984 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001985
1986 if (lnum > 1)
1987 --lnum;
1988 else if (!p_ws)
1989 break; /* at first line and 'nowrapscan' */
1990 else
1991 {
1992 /* Wrap around to the end of the buffer. May search the
1993 * starting line again and accept the last match. */
1994 lnum = wp->w_buffer->b_ml.ml_line_count;
1995 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001996 if (!shortmess(SHM_SEARCH))
1997 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001998 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001999 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002000 }
2001 else
2002 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002003 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2004 ++lnum;
2005 else if (!p_ws)
2006 break; /* at first line and 'nowrapscan' */
2007 else
2008 {
2009 /* Wrap around to the start of the buffer. May search the
2010 * starting line again and accept the first match. */
2011 lnum = 1;
2012 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002013 if (!shortmess(SHM_SEARCH))
2014 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002015 }
2016
2017 /* If we are back at the starting line and there is no match then
2018 * give up. */
2019 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002020 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002021
2022 /* Skip the characters at the start of the next line that were
2023 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002024 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002025 skip = p - endp;
2026 else
2027 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002028
2029 /* Capscol skips over the inserted space. */
2030 --capcol;
2031
2032 /* But after empty line check first word in next line */
2033 if (*skipwhite(line) == NUL)
2034 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002035 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002036
2037 line_breakcheck();
2038 }
2039
Bram Moolenaar0c405862005-06-22 22:26:26 +00002040 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002041 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002042}
2043
2044/*
2045 * For spell checking: concatenate the start of the following line "line" into
2046 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2047 */
2048 void
2049spell_cat_line(buf, line, maxlen)
2050 char_u *buf;
2051 char_u *line;
2052 int maxlen;
2053{
2054 char_u *p;
2055 int n;
2056
2057 p = skipwhite(line);
2058 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2059 p = skipwhite(p + 1);
2060
2061 if (*p != NUL)
2062 {
2063 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002064 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002065 n = p - line;
2066 if (n >= maxlen)
2067 n = maxlen - 1;
2068 vim_memset(buf + 1, ' ', n);
2069 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002070}
2071
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002072typedef struct spelload_S
2073{
2074 char_u sl_lang[MAXWLEN + 1]; /* language name */
2075 slang_T *sl_slang; /* resulting slang_T struct */
2076 int sl_nobreak; /* NOBREAK language found */
2077} spelload_T;
2078
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002079/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002080 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002081 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002083 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002084spell_load_lang(lang)
2085 char_u *lang;
2086{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002087 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002088 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002089 spelload_T sl;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002090
Bram Moolenaarb765d632005-06-07 21:00:02 +00002091 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002092 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002093 STRCPY(sl.sl_lang, lang);
2094 sl.sl_slang = NULL;
2095 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002096
Bram Moolenaarb765d632005-06-07 21:00:02 +00002097 /*
2098 * Find the first spell file for "lang" in 'runtimepath' and load it.
2099 */
2100 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2101 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002102 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002103
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002104 if (r == FAIL && *sl.sl_lang != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002105 {
2106 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002107 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002108 "spell/%s.ascii.spl", lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002109 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002110 }
2111
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002112 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002113 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2114 lang, spell_enc(), lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002115 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002116 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002117 /* At least one file was loaded, now load all the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002118 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002119 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002120 }
2121}
2122
2123/*
2124 * Return the encoding used for spell checking: Use 'encoding', except that we
2125 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2126 */
2127 static char_u *
2128spell_enc()
2129{
2130
2131#ifdef FEAT_MBYTE
2132 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2133 return p_enc;
2134#endif
2135 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002136}
2137
2138/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002139 * Get the name of the .spl file for the internal wordlist into
2140 * "fname[MAXPATHL]".
2141 */
2142 static void
2143int_wordlist_spl(fname)
2144 char_u *fname;
2145{
2146 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2147 int_wordlist, spell_enc());
2148}
2149
2150/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002151 * Allocate a new slang_T.
2152 * Caller must fill "sl_next".
2153 */
2154 static slang_T *
2155slang_alloc(lang)
2156 char_u *lang;
2157{
2158 slang_T *lp;
2159
Bram Moolenaar51485f02005-06-04 21:55:20 +00002160 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002161 if (lp != NULL)
2162 {
2163 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002164 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002165 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002166 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002167 }
2168 return lp;
2169}
2170
2171/*
2172 * Free the contents of an slang_T and the structure itself.
2173 */
2174 static void
2175slang_free(lp)
2176 slang_T *lp;
2177{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002178 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002179 vim_free(lp->sl_fname);
2180 slang_clear(lp);
2181 vim_free(lp);
2182}
2183
2184/*
2185 * Clear an slang_T so that the file can be reloaded.
2186 */
2187 static void
2188slang_clear(lp)
2189 slang_T *lp;
2190{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002191 garray_T *gap;
2192 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002193 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002194 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002195
Bram Moolenaar51485f02005-06-04 21:55:20 +00002196 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002197 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002198 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002199 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002200 vim_free(lp->sl_pbyts);
2201 lp->sl_pbyts = NULL;
2202
Bram Moolenaar51485f02005-06-04 21:55:20 +00002203 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002204 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002205 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002206 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002207 vim_free(lp->sl_pidxs);
2208 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002210 gap = &lp->sl_rep;
2211 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002213 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2214 vim_free(ftp->ft_from);
2215 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002217 ga_clear(gap);
2218
2219 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002220 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002221 {
2222 /* "ga_len" is set to 1 without adding an item for latin1 */
2223 if (gap->ga_data != NULL)
2224 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2225 for (i = 0; i < gap->ga_len; ++i)
2226 vim_free(((int **)gap->ga_data)[i]);
2227 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002228 else
2229 /* SAL items: free salitem_T items */
2230 while (gap->ga_len > 0)
2231 {
2232 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2233 vim_free(smp->sm_lead);
2234 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2235 vim_free(smp->sm_to);
2236#ifdef FEAT_MBYTE
2237 vim_free(smp->sm_lead_w);
2238 vim_free(smp->sm_oneof_w);
2239 vim_free(smp->sm_to_w);
2240#endif
2241 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002242 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002244 for (i = 0; i < lp->sl_prefixcnt; ++i)
2245 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002246 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002247 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002248 lp->sl_prefprog = NULL;
2249
2250 vim_free(lp->sl_midword);
2251 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002252
Bram Moolenaar5195e452005-08-19 20:32:47 +00002253 vim_free(lp->sl_compprog);
2254 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002255 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002256 lp->sl_compprog = NULL;
2257 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002258 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002259
2260 vim_free(lp->sl_syllable);
2261 lp->sl_syllable = NULL;
2262 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002263
Bram Moolenaarea424162005-06-16 21:51:00 +00002264#ifdef FEAT_MBYTE
2265 {
2266 int todo = lp->sl_map_hash.ht_used;
2267 hashitem_T *hi;
2268
2269 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2270 if (!HASHITEM_EMPTY(hi))
2271 {
2272 --todo;
2273 vim_free(hi->hi_key);
2274 }
2275 }
2276 hash_clear(&lp->sl_map_hash);
2277#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002278
2279 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002280 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002281 lp->sl_compsylmax = MAXWLEN;
2282 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283}
2284
2285/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002286 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 * Invoked through do_in_runtimepath().
2288 */
2289 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002290spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002291 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002292 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002293{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002294 spelload_T *slp = (spelload_T *)cookie;
2295 slang_T *slang;
2296
2297 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2298 if (slang != NULL)
2299 {
2300 /* When a previously loaded file has NOBREAK also use it for the
2301 * ".add" files. */
2302 if (slp->sl_nobreak && slang->sl_add)
2303 slang->sl_nobreak = TRUE;
2304 else if (slang->sl_nobreak)
2305 slp->sl_nobreak = TRUE;
2306
2307 slp->sl_slang = slang;
2308 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002309}
2310
2311/*
2312 * Load one spell file and store the info into a slang_T.
2313 *
2314 * This is invoked in two ways:
2315 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2316 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2317 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2318 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002319 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002320 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002321 static slang_T *
2322spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002323 char_u *fname;
2324 char_u *lang;
2325 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002327{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002329 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002330 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002331 char_u *bp;
2332 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002333 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002334 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002335 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002336 int round;
2337 char_u *save_sourcing_name = sourcing_name;
2338 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002339 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002340 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002341 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002342 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002343
Bram Moolenaarb765d632005-06-07 21:00:02 +00002344 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002345 if (fd == NULL)
2346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 if (!silent)
2348 EMSG2(_(e_notopen), fname);
2349 else if (p_verbose > 2)
2350 {
2351 verbose_enter();
2352 smsg((char_u *)e_notopen, fname);
2353 verbose_leave();
2354 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002355 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002356 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002357 if (p_verbose > 2)
2358 {
2359 verbose_enter();
2360 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2361 verbose_leave();
2362 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002363
Bram Moolenaarb765d632005-06-07 21:00:02 +00002364 if (old_lp == NULL)
2365 {
2366 lp = slang_alloc(lang);
2367 if (lp == NULL)
2368 goto endFAIL;
2369
2370 /* Remember the file name, used to reload the file when it's updated. */
2371 lp->sl_fname = vim_strsave(fname);
2372 if (lp->sl_fname == NULL)
2373 goto endFAIL;
2374
2375 /* Check for .add.spl. */
2376 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2377 }
2378 else
2379 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002380
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002381 /* Set sourcing_name, so that error messages mention the file name. */
2382 sourcing_name = fname;
2383 sourcing_lnum = 0;
2384
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002385 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002386 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002387 for (i = 0; i < VIMSPELLMAGICL; ++i)
2388 buf[i] = getc(fd); /* <fileID> */
2389 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2390 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002391 EMSG(_("E757: This does not look like a spell file"));
2392 goto endFAIL;
2393 }
2394 c = getc(fd); /* <versionnr> */
2395 if (c < VIMSPELLVERSION)
2396 {
2397 EMSG(_("E771: Old spell file, needs to be updated"));
2398 goto endFAIL;
2399 }
2400 else if (c > VIMSPELLVERSION)
2401 {
2402 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002403 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002404 }
2405
Bram Moolenaar5195e452005-08-19 20:32:47 +00002406
2407 /*
2408 * <SECTIONS>: <section> ... <sectionend>
2409 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2410 */
2411 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002412 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002413 n = getc(fd); /* <sectionID> or <sectionend> */
2414 if (n == SN_END)
2415 break;
2416 c = getc(fd); /* <sectionflags> */
2417 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2418 /* <sectionlen> */
2419 if (len < 0)
2420 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002421
Bram Moolenaar5195e452005-08-19 20:32:47 +00002422 res = 0;
2423 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002424 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002425 case SN_REGION:
2426 res = read_region_section(fd, lp, len);
2427 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002428
Bram Moolenaar5195e452005-08-19 20:32:47 +00002429 case SN_CHARFLAGS:
2430 res = read_charflags_section(fd);
2431 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002432
Bram Moolenaar5195e452005-08-19 20:32:47 +00002433 case SN_MIDWORD:
2434 lp->sl_midword = read_string(fd, len); /* <midword> */
2435 if (lp->sl_midword == NULL)
2436 goto endFAIL;
2437 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002438
Bram Moolenaar5195e452005-08-19 20:32:47 +00002439 case SN_PREFCOND:
2440 res = read_prefcond_section(fd, lp);
2441 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002442
Bram Moolenaar5195e452005-08-19 20:32:47 +00002443 case SN_REP:
2444 res = read_rep_section(fd, lp);
2445 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002446
Bram Moolenaar5195e452005-08-19 20:32:47 +00002447 case SN_SAL:
2448 res = read_sal_section(fd, lp);
2449 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002450
Bram Moolenaar5195e452005-08-19 20:32:47 +00002451 case SN_SOFO:
2452 res = read_sofo_section(fd, lp);
2453 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002454
Bram Moolenaar5195e452005-08-19 20:32:47 +00002455 case SN_MAP:
2456 p = read_string(fd, len); /* <mapstr> */
2457 if (p == NULL)
2458 goto endFAIL;
2459 set_map_str(lp, p);
2460 vim_free(p);
2461 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002462
Bram Moolenaar5195e452005-08-19 20:32:47 +00002463 case SN_COMPOUND:
2464 res = read_compound(fd, lp, len);
2465 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002466
Bram Moolenaar78622822005-08-23 21:00:13 +00002467 case SN_NOBREAK:
2468 lp->sl_nobreak = TRUE;
2469 break;
2470
Bram Moolenaar5195e452005-08-19 20:32:47 +00002471 case SN_SYLLABLE:
2472 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2473 if (lp->sl_syllable == NULL)
2474 goto endFAIL;
2475 if (init_syl_tab(lp) == FAIL)
2476 goto endFAIL;
2477 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002478
Bram Moolenaar5195e452005-08-19 20:32:47 +00002479 default:
2480 /* Unsupported section. When it's required give an error
2481 * message. When it's not required skip the contents. */
2482 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002483 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002484 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002485 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002486 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002487 while (--len >= 0)
2488 if (getc(fd) < 0)
2489 goto truncerr;
2490 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002491 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002492 if (res == SP_FORMERROR)
2493 {
2494formerr:
2495 EMSG(_(e_format));
2496 goto endFAIL;
2497 }
2498 if (res == SP_TRUNCERROR)
2499 {
2500truncerr:
2501 EMSG(_(e_spell_trunc));
2502 goto endFAIL;
2503 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002504 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002505 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002506 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002507
Bram Moolenaar51485f02005-06-04 21:55:20 +00002508 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002509 * round 2: <KWORDTREE>
2510 * round 3: <PREFIXTREE> */
2511 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002512 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002513 /* The tree size was computed when writing the file, so that we can
2514 * allocate it as one long block. <nodecount> */
2515 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2516 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002517 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002518 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002519 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002520 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002521 bp = lalloc((long_u)len, TRUE);
2522 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002523 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002524 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002525 lp->sl_fbyts = bp;
2526 else if (round == 2)
2527 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002528 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002529 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002530
2531 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002532 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2533 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002534 goto endFAIL;
2535 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002536 lp->sl_fidxs = ip;
2537 else if (round == 2)
2538 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002539 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002540 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002541
2542 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002543 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002544 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002545 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002546 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002547 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002548 }
2549 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002550
Bram Moolenaarb765d632005-06-07 21:00:02 +00002551 /* For a new file link it in the list of spell files. */
2552 if (old_lp == NULL)
2553 {
2554 lp->sl_next = first_lang;
2555 first_lang = lp;
2556 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002557
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002558 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002560endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002561 if (lang != NULL)
2562 /* truncating the name signals the error to spell_load_lang() */
2563 *lang = NUL;
2564 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002565 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002566 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002567
2568endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 if (fd != NULL)
2570 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002571 sourcing_name = save_sourcing_name;
2572 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002573
2574 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002575}
2576
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002577/*
2578 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002579 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002580 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002581 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2582 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002583 */
2584 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002585read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002586 FILE *fd;
2587 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002588 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002589{
2590 int cnt = 0;
2591 int i;
2592 char_u *str;
2593
2594 /* read the length bytes, MSB first */
2595 for (i = 0; i < cnt_bytes; ++i)
2596 cnt = (cnt << 8) + getc(fd);
2597 if (cnt < 0)
2598 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002599 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002600 return NULL;
2601 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002602 *cntp = cnt;
2603 if (cnt == 0)
2604 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002605
Bram Moolenaar5195e452005-08-19 20:32:47 +00002606 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002607 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002608 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002609 return str;
2610}
2611
Bram Moolenaar7887d882005-07-01 22:33:52 +00002612/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002613 * Read a string of length "cnt" from "fd" into allocated memory.
2614 * Returns NULL when out of memory.
2615 */
2616 static char_u *
2617read_string(fd, cnt)
2618 FILE *fd;
2619 int cnt;
2620{
2621 char_u *str;
2622 int i;
2623
2624 /* allocate memory */
2625 str = alloc((unsigned)cnt + 1);
2626 if (str != NULL)
2627 {
2628 /* Read the string. Doesn't check for truncated file. */
2629 for (i = 0; i < cnt; ++i)
2630 str[i] = getc(fd);
2631 str[i] = NUL;
2632 }
2633 return str;
2634}
2635
2636/*
2637 * Read SN_REGION: <regionname> ...
2638 * Return SP_*ERROR flags.
2639 */
2640 static int
2641read_region_section(fd, lp, len)
2642 FILE *fd;
2643 slang_T *lp;
2644 int len;
2645{
2646 int i;
2647
2648 if (len > 16)
2649 return SP_FORMERROR;
2650 for (i = 0; i < len; ++i)
2651 lp->sl_regions[i] = getc(fd); /* <regionname> */
2652 lp->sl_regions[len] = NUL;
2653 return 0;
2654}
2655
2656/*
2657 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2658 * <folcharslen> <folchars>
2659 * Return SP_*ERROR flags.
2660 */
2661 static int
2662read_charflags_section(fd)
2663 FILE *fd;
2664{
2665 char_u *flags;
2666 char_u *fol;
2667 int flagslen, follen;
2668
2669 /* <charflagslen> <charflags> */
2670 flags = read_cnt_string(fd, 1, &flagslen);
2671 if (flagslen < 0)
2672 return flagslen;
2673
2674 /* <folcharslen> <folchars> */
2675 fol = read_cnt_string(fd, 2, &follen);
2676 if (follen < 0)
2677 {
2678 vim_free(flags);
2679 return follen;
2680 }
2681
2682 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2683 if (flags != NULL && fol != NULL)
2684 set_spell_charflags(flags, flagslen, fol);
2685
2686 vim_free(flags);
2687 vim_free(fol);
2688
2689 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2690 if ((flags == NULL) != (fol == NULL))
2691 return SP_FORMERROR;
2692 return 0;
2693}
2694
2695/*
2696 * Read SN_PREFCOND section.
2697 * Return SP_*ERROR flags.
2698 */
2699 static int
2700read_prefcond_section(fd, lp)
2701 FILE *fd;
2702 slang_T *lp;
2703{
2704 int cnt;
2705 int i;
2706 int n;
2707 char_u *p;
2708 char_u buf[MAXWLEN + 1];
2709
2710 /* <prefcondcnt> <prefcond> ... */
2711 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2712 if (cnt <= 0)
2713 return SP_FORMERROR;
2714
2715 lp->sl_prefprog = (regprog_T **)alloc_clear(
2716 (unsigned)sizeof(regprog_T *) * cnt);
2717 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002718 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002719 lp->sl_prefixcnt = cnt;
2720
2721 for (i = 0; i < cnt; ++i)
2722 {
2723 /* <prefcond> : <condlen> <condstr> */
2724 n = getc(fd); /* <condlen> */
2725 if (n < 0 || n >= MAXWLEN)
2726 return SP_FORMERROR;
2727
2728 /* When <condlen> is zero we have an empty condition. Otherwise
2729 * compile the regexp program used to check for the condition. */
2730 if (n > 0)
2731 {
2732 buf[0] = '^'; /* always match at one position only */
2733 p = buf + 1;
2734 while (n-- > 0)
2735 *p++ = getc(fd); /* <condstr> */
2736 *p = NUL;
2737 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2738 }
2739 }
2740 return 0;
2741}
2742
2743/*
2744 * Read REP items section from "fd": <repcount> <rep> ...
2745 * Return SP_*ERROR flags.
2746 */
2747 static int
2748read_rep_section(fd, slang)
2749 FILE *fd;
2750 slang_T *slang;
2751{
2752 int cnt;
2753 garray_T *gap;
2754 fromto_T *ftp;
2755 short *first;
2756 int i;
2757
2758 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2759 if (cnt < 0)
2760 return SP_TRUNCERROR;
2761
2762 gap = &slang->sl_rep;
2763 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002764 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002765
2766 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2767 for (; gap->ga_len < cnt; ++gap->ga_len)
2768 {
2769 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2770 ftp->ft_from = read_cnt_string(fd, 1, &i);
2771 if (i < 0)
2772 return i;
2773 if (i == 0)
2774 return SP_FORMERROR;
2775 ftp->ft_to = read_cnt_string(fd, 1, &i);
2776 if (i <= 0)
2777 {
2778 vim_free(ftp->ft_from);
2779 if (i < 0)
2780 return i;
2781 return SP_FORMERROR;
2782 }
2783 }
2784
2785 /* Fill the first-index table. */
2786 first = slang->sl_rep_first;
2787 for (i = 0; i < 256; ++i)
2788 first[i] = -1;
2789 for (i = 0; i < gap->ga_len; ++i)
2790 {
2791 ftp = &((fromto_T *)gap->ga_data)[i];
2792 if (first[*ftp->ft_from] == -1)
2793 first[*ftp->ft_from] = i;
2794 }
2795 return 0;
2796}
2797
2798/*
2799 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2800 * Return SP_*ERROR flags.
2801 */
2802 static int
2803read_sal_section(fd, slang)
2804 FILE *fd;
2805 slang_T *slang;
2806{
2807 int i;
2808 int cnt;
2809 garray_T *gap;
2810 salitem_T *smp;
2811 int ccnt;
2812 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002813 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002814
2815 slang->sl_sofo = FALSE;
2816
2817 i = getc(fd); /* <salflags> */
2818 if (i & SAL_F0LLOWUP)
2819 slang->sl_followup = TRUE;
2820 if (i & SAL_COLLAPSE)
2821 slang->sl_collapse = TRUE;
2822 if (i & SAL_REM_ACCENTS)
2823 slang->sl_rem_accents = TRUE;
2824
2825 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2826 if (cnt < 0)
2827 return SP_TRUNCERROR;
2828
2829 gap = &slang->sl_sal;
2830 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002831 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002832 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002833
2834 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2835 for (; gap->ga_len < cnt; ++gap->ga_len)
2836 {
2837 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2838 ccnt = getc(fd); /* <salfromlen> */
2839 if (ccnt < 0)
2840 return SP_TRUNCERROR;
2841 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002842 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002843 smp->sm_lead = p;
2844
2845 /* Read up to the first special char into sm_lead. */
2846 for (i = 0; i < ccnt; ++i)
2847 {
2848 c = getc(fd); /* <salfrom> */
2849 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2850 break;
2851 *p++ = c;
2852 }
2853 smp->sm_leadlen = p - smp->sm_lead;
2854 *p++ = NUL;
2855
2856 /* Put (abc) chars in sm_oneof, if any. */
2857 if (c == '(')
2858 {
2859 smp->sm_oneof = p;
2860 for (++i; i < ccnt; ++i)
2861 {
2862 c = getc(fd); /* <salfrom> */
2863 if (c == ')')
2864 break;
2865 *p++ = c;
2866 }
2867 *p++ = NUL;
2868 if (++i < ccnt)
2869 c = getc(fd);
2870 }
2871 else
2872 smp->sm_oneof = NULL;
2873
2874 /* Any following chars go in sm_rules. */
2875 smp->sm_rules = p;
2876 if (i < ccnt)
2877 /* store the char we got while checking for end of sm_lead */
2878 *p++ = c;
2879 for (++i; i < ccnt; ++i)
2880 *p++ = getc(fd); /* <salfrom> */
2881 *p++ = NUL;
2882
2883 /* <saltolen> <salto> */
2884 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2885 if (ccnt < 0)
2886 {
2887 vim_free(smp->sm_lead);
2888 return ccnt;
2889 }
2890
2891#ifdef FEAT_MBYTE
2892 if (has_mbyte)
2893 {
2894 /* convert the multi-byte strings to wide char strings */
2895 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2896 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2897 if (smp->sm_oneof == NULL)
2898 smp->sm_oneof_w = NULL;
2899 else
2900 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2901 if (smp->sm_to == NULL)
2902 smp->sm_to_w = NULL;
2903 else
2904 smp->sm_to_w = mb_str2wide(smp->sm_to);
2905 if (smp->sm_lead_w == NULL
2906 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2907 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2908 {
2909 vim_free(smp->sm_lead);
2910 vim_free(smp->sm_to);
2911 vim_free(smp->sm_lead_w);
2912 vim_free(smp->sm_oneof_w);
2913 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002914 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002915 }
2916 }
2917#endif
2918 }
2919
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002920 if (gap->ga_len > 0)
2921 {
2922 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
2923 * that we need to check the index every time. */
2924 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2925 if ((p = alloc(1)) == NULL)
2926 return SP_OTHERERROR;
2927 p[0] = NUL;
2928 smp->sm_lead = p;
2929 smp->sm_leadlen = 0;
2930 smp->sm_oneof = NULL;
2931 smp->sm_rules = p;
2932 smp->sm_to = NULL;
2933#ifdef FEAT_MBYTE
2934 if (has_mbyte)
2935 {
2936 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2937 smp->sm_leadlen = 0;
2938 smp->sm_oneof_w = NULL;
2939 smp->sm_to_w = NULL;
2940 }
2941#endif
2942 ++gap->ga_len;
2943 }
2944
Bram Moolenaar5195e452005-08-19 20:32:47 +00002945 /* Fill the first-index table. */
2946 set_sal_first(slang);
2947
2948 return 0;
2949}
2950
2951/*
2952 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2953 * Return SP_*ERROR flags.
2954 */
2955 static int
2956read_sofo_section(fd, slang)
2957 FILE *fd;
2958 slang_T *slang;
2959{
2960 int cnt;
2961 char_u *from, *to;
2962 int res;
2963
2964 slang->sl_sofo = TRUE;
2965
2966 /* <sofofromlen> <sofofrom> */
2967 from = read_cnt_string(fd, 2, &cnt);
2968 if (cnt < 0)
2969 return cnt;
2970
2971 /* <sofotolen> <sofoto> */
2972 to = read_cnt_string(fd, 2, &cnt);
2973 if (cnt < 0)
2974 {
2975 vim_free(from);
2976 return cnt;
2977 }
2978
2979 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2980 if (from != NULL && to != NULL)
2981 res = set_sofo(slang, from, to);
2982 else if (from != NULL || to != NULL)
2983 res = SP_FORMERROR; /* only one of two strings is an error */
2984 else
2985 res = 0;
2986
2987 vim_free(from);
2988 vim_free(to);
2989 return res;
2990}
2991
2992/*
2993 * Read the compound section from the .spl file:
2994 * <compmax> <compminlen> <compsylmax> <compflags>
2995 * Returns SP_*ERROR flags.
2996 */
2997 static int
2998read_compound(fd, slang, len)
2999 FILE *fd;
3000 slang_T *slang;
3001 int len;
3002{
3003 int todo = len;
3004 int c;
3005 int atstart;
3006 char_u *pat;
3007 char_u *pp;
3008 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003009 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010
3011 if (todo < 2)
3012 return SP_FORMERROR; /* need at least two bytes */
3013
3014 --todo;
3015 c = getc(fd); /* <compmax> */
3016 if (c < 2)
3017 c = MAXWLEN;
3018 slang->sl_compmax = c;
3019
3020 --todo;
3021 c = getc(fd); /* <compminlen> */
3022 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003023 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003024 slang->sl_compminlen = c;
3025
3026 --todo;
3027 c = getc(fd); /* <compsylmax> */
3028 if (c < 1)
3029 c = MAXWLEN;
3030 slang->sl_compsylmax = c;
3031
3032 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3033 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003034 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3035 * Conversion to utf-8 may double the size. */
3036 c = todo * 2 + 7;
3037#ifdef FEAT_MBYTE
3038 if (enc_utf8)
3039 c += todo * 2;
3040#endif
3041 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003042 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003043 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003044
Bram Moolenaard12a1322005-08-21 22:08:24 +00003045 /* We also need a list of all flags that can appear at the start and one
3046 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003047 cp = alloc(todo + 1);
3048 if (cp == NULL)
3049 {
3050 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003051 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003052 }
3053 slang->sl_compstartflags = cp;
3054 *cp = NUL;
3055
Bram Moolenaard12a1322005-08-21 22:08:24 +00003056 ap = alloc(todo + 1);
3057 if (ap == NULL)
3058 {
3059 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003060 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003061 }
3062 slang->sl_compallflags = ap;
3063 *ap = NUL;
3064
Bram Moolenaar5195e452005-08-19 20:32:47 +00003065 pp = pat;
3066 *pp++ = '^';
3067 *pp++ = '\\';
3068 *pp++ = '(';
3069
3070 atstart = 1;
3071 while (todo-- > 0)
3072 {
3073 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003074
3075 /* Add all flags to "sl_compallflags". */
3076 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003077 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003078 {
3079 *ap++ = c;
3080 *ap = NUL;
3081 }
3082
Bram Moolenaar5195e452005-08-19 20:32:47 +00003083 if (atstart != 0)
3084 {
3085 /* At start of item: copy flags to "sl_compstartflags". For a
3086 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3087 if (c == '[')
3088 atstart = 2;
3089 else if (c == ']')
3090 atstart = 0;
3091 else
3092 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003093 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003094 {
3095 *cp++ = c;
3096 *cp = NUL;
3097 }
3098 if (atstart == 1)
3099 atstart = 0;
3100 }
3101 }
3102 if (c == '/') /* slash separates two items */
3103 {
3104 *pp++ = '\\';
3105 *pp++ = '|';
3106 atstart = 1;
3107 }
3108 else /* normal char, "[abc]" and '*' are copied as-is */
3109 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003110 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003111 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003112#ifdef FEAT_MBYTE
3113 if (enc_utf8)
3114 pp += mb_char2bytes(c, pp);
3115 else
3116#endif
3117 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003118 }
3119 }
3120
3121 *pp++ = '\\';
3122 *pp++ = ')';
3123 *pp++ = '$';
3124 *pp = NUL;
3125
3126 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3127 vim_free(pat);
3128 if (slang->sl_compprog == NULL)
3129 return SP_FORMERROR;
3130
3131 return 0;
3132}
3133
Bram Moolenaar6de68532005-08-24 22:08:48 +00003134/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003135 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003136 * Like strchr() but independent of locale.
3137 */
3138 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003139byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003140 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003141 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003142{
3143 char_u *p;
3144
3145 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003146 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003147 return TRUE;
3148 return FALSE;
3149}
3150
Bram Moolenaar5195e452005-08-19 20:32:47 +00003151#define SY_MAXLEN 30
3152typedef struct syl_item_S
3153{
3154 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3155 int sy_len;
3156} syl_item_T;
3157
3158/*
3159 * Truncate "slang->sl_syllable" at the first slash and put the following items
3160 * in "slang->sl_syl_items".
3161 */
3162 static int
3163init_syl_tab(slang)
3164 slang_T *slang;
3165{
3166 char_u *p;
3167 char_u *s;
3168 int l;
3169 syl_item_T *syl;
3170
3171 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3172 p = vim_strchr(slang->sl_syllable, '/');
3173 while (p != NULL)
3174 {
3175 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003176 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003177 break;
3178 s = p;
3179 p = vim_strchr(p, '/');
3180 if (p == NULL)
3181 l = STRLEN(s);
3182 else
3183 l = p - s;
3184 if (l >= SY_MAXLEN)
3185 return SP_FORMERROR;
3186 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003187 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003188 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3189 + slang->sl_syl_items.ga_len++;
3190 vim_strncpy(syl->sy_chars, s, l);
3191 syl->sy_len = l;
3192 }
3193 return OK;
3194}
3195
3196/*
3197 * Count the number of syllables in "word".
3198 * When "word" contains spaces the syllables after the last space are counted.
3199 * Returns zero if syllables are not defines.
3200 */
3201 static int
3202count_syllables(slang, word)
3203 slang_T *slang;
3204 char_u *word;
3205{
3206 int cnt = 0;
3207 int skip = FALSE;
3208 char_u *p;
3209 int len;
3210 int i;
3211 syl_item_T *syl;
3212 int c;
3213
3214 if (slang->sl_syllable == NULL)
3215 return 0;
3216
3217 for (p = word; *p != NUL; p += len)
3218 {
3219 /* When running into a space reset counter. */
3220 if (*p == ' ')
3221 {
3222 len = 1;
3223 cnt = 0;
3224 continue;
3225 }
3226
3227 /* Find longest match of syllable items. */
3228 len = 0;
3229 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3230 {
3231 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3232 if (syl->sy_len > len
3233 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3234 len = syl->sy_len;
3235 }
3236 if (len != 0) /* found a match, count syllable */
3237 {
3238 ++cnt;
3239 skip = FALSE;
3240 }
3241 else
3242 {
3243 /* No recognized syllable item, at least a syllable char then? */
3244#ifdef FEAT_MBYTE
3245 c = mb_ptr2char(p);
3246 len = (*mb_ptr2len)(p);
3247#else
3248 c = *p;
3249 len = 1;
3250#endif
3251 if (vim_strchr(slang->sl_syllable, c) == NULL)
3252 skip = FALSE; /* No, search for next syllable */
3253 else if (!skip)
3254 {
3255 ++cnt; /* Yes, count it */
3256 skip = TRUE; /* don't count following syllable chars */
3257 }
3258 }
3259 }
3260 return cnt;
3261}
3262
3263/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003264 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003265 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003266 */
3267 static int
3268set_sofo(lp, from, to)
3269 slang_T *lp;
3270 char_u *from;
3271 char_u *to;
3272{
3273 int i;
3274
3275#ifdef FEAT_MBYTE
3276 garray_T *gap;
3277 char_u *s;
3278 char_u *p;
3279 int c;
3280 int *inp;
3281
3282 if (has_mbyte)
3283 {
3284 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3285 * characters. The index is the low byte of the character.
3286 * The list contains from-to pairs with a terminating NUL.
3287 * sl_sal_first[] is used for latin1 "from" characters. */
3288 gap = &lp->sl_sal;
3289 ga_init2(gap, sizeof(int *), 1);
3290 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003291 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003292 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3293 gap->ga_len = 256;
3294
3295 /* First count the number of items for each list. Temporarily use
3296 * sl_sal_first[] for this. */
3297 for (p = from, s = to; *p != NUL && *s != NUL; )
3298 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003299 c = mb_cptr2char_adv(&p);
3300 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003301 if (c >= 256)
3302 ++lp->sl_sal_first[c & 0xff];
3303 }
3304 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003305 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003306
3307 /* Allocate the lists. */
3308 for (i = 0; i < 256; ++i)
3309 if (lp->sl_sal_first[i] > 0)
3310 {
3311 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3312 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003313 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003314 ((int **)gap->ga_data)[i] = (int *)p;
3315 *(int *)p = 0;
3316 }
3317
3318 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3319 * list. */
3320 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3321 for (p = from, s = to; *p != NUL && *s != NUL; )
3322 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003323 c = mb_cptr2char_adv(&p);
3324 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003325 if (c >= 256)
3326 {
3327 /* Append the from-to chars at the end of the list with
3328 * the low byte. */
3329 inp = ((int **)gap->ga_data)[c & 0xff];
3330 while (*inp != 0)
3331 ++inp;
3332 *inp++ = c; /* from char */
3333 *inp++ = i; /* to char */
3334 *inp++ = NUL; /* NUL at the end */
3335 }
3336 else
3337 /* mapping byte to char is done in sl_sal_first[] */
3338 lp->sl_sal_first[c] = i;
3339 }
3340 }
3341 else
3342#endif
3343 {
3344 /* mapping bytes to bytes is done in sl_sal_first[] */
3345 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003346 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003347
3348 for (i = 0; to[i] != NUL; ++i)
3349 lp->sl_sal_first[from[i]] = to[i];
3350 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3351 }
3352
Bram Moolenaar5195e452005-08-19 20:32:47 +00003353 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003354}
3355
3356/*
3357 * Fill the first-index table for "lp".
3358 */
3359 static void
3360set_sal_first(lp)
3361 slang_T *lp;
3362{
3363 salfirst_T *sfirst;
3364 int i;
3365 salitem_T *smp;
3366 int c;
3367 garray_T *gap = &lp->sl_sal;
3368
3369 sfirst = lp->sl_sal_first;
3370 for (i = 0; i < 256; ++i)
3371 sfirst[i] = -1;
3372 smp = (salitem_T *)gap->ga_data;
3373 for (i = 0; i < gap->ga_len; ++i)
3374 {
3375#ifdef FEAT_MBYTE
3376 if (has_mbyte)
3377 /* Use the lowest byte of the first character. For latin1 it's
3378 * the character, for other encodings it should differ for most
3379 * characters. */
3380 c = *smp[i].sm_lead_w & 0xff;
3381 else
3382#endif
3383 c = *smp[i].sm_lead;
3384 if (sfirst[c] == -1)
3385 {
3386 sfirst[c] = i;
3387#ifdef FEAT_MBYTE
3388 if (has_mbyte)
3389 {
3390 int n;
3391
3392 /* Make sure all entries with this byte are following each
3393 * other. Move the ones that are in the wrong position. Do
3394 * keep the same ordering! */
3395 while (i + 1 < gap->ga_len
3396 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3397 /* Skip over entry with same index byte. */
3398 ++i;
3399
3400 for (n = 1; i + n < gap->ga_len; ++n)
3401 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3402 {
3403 salitem_T tsal;
3404
3405 /* Move entry with same index byte after the entries
3406 * we already found. */
3407 ++i;
3408 --n;
3409 tsal = smp[i + n];
3410 mch_memmove(smp + i + 1, smp + i,
3411 sizeof(salitem_T) * n);
3412 smp[i] = tsal;
3413 }
3414 }
3415#endif
3416 }
3417 }
3418}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003419
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003420#ifdef FEAT_MBYTE
3421/*
3422 * Turn a multi-byte string into a wide character string.
3423 * Return it in allocated memory (NULL for out-of-memory)
3424 */
3425 static int *
3426mb_str2wide(s)
3427 char_u *s;
3428{
3429 int *res;
3430 char_u *p;
3431 int i = 0;
3432
3433 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3434 if (res != NULL)
3435 {
3436 for (p = s; *p != NUL; )
3437 res[i++] = mb_ptr2char_adv(&p);
3438 res[i] = NUL;
3439 }
3440 return res;
3441}
3442#endif
3443
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003444/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003445 * Read one row of siblings from the spell file and store it in the byte array
3446 * "byts" and index array "idxs". Recursively read the children.
3447 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003448 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003449 *
3450 * Returns the index follosing the siblings.
3451 * Returns -1 if the file is shorter than expected.
3452 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003453 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003454 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003455read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003456 FILE *fd;
3457 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003458 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003459 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003460 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003461 int prefixtree; /* TRUE for reading PREFIXTREE */
3462 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003463{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003464 int len;
3465 int i;
3466 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003467 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003468 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003469 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003470#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003471
Bram Moolenaar51485f02005-06-04 21:55:20 +00003472 len = getc(fd); /* <siblingcount> */
3473 if (len <= 0)
3474 return -1;
3475
3476 if (startidx + len >= maxidx)
3477 return -2;
3478 byts[idx++] = len;
3479
3480 /* Read the byte values, flag/region bytes and shared indexes. */
3481 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003482 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003483 c = getc(fd); /* <byte> */
3484 if (c < 0)
3485 return -1;
3486 if (c <= BY_SPECIAL)
3487 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003488 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003489 {
3490 /* No flags, all regions. */
3491 idxs[idx] = 0;
3492 c = 0;
3493 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003494 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003495 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003496 if (prefixtree)
3497 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003498 /* Read the optional pflags byte, the prefix ID and the
3499 * condition nr. In idxs[] store the prefix ID in the low
3500 * byte, the condition index shifted up 8 bits, the flags
3501 * shifted up 24 bits. */
3502 if (c == BY_FLAGS)
3503 c = getc(fd) << 24; /* <pflags> */
3504 else
3505 c = 0;
3506
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003507 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003508
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003509 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3510 if (n >= maxprefcondnr)
3511 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003512 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003513 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003514 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003515 {
3516 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003517 * idxs[] the flags go in the low two bytes, region above
3518 * that and prefix ID above the region. */
3519 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003520 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003521 if (c2 == BY_FLAGS2)
3522 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003523 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003524 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003525 if (c & WF_AFX)
3526 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003527 }
3528
Bram Moolenaar51485f02005-06-04 21:55:20 +00003529 idxs[idx] = c;
3530 c = 0;
3531 }
3532 else /* c == BY_INDEX */
3533 {
3534 /* <nodeidx> */
3535 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3536 if (n < 0 || n >= maxidx)
3537 return -2;
3538 idxs[idx] = n + SHARED_MASK;
3539 c = getc(fd); /* <xbyte> */
3540 }
3541 }
3542 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003543 }
3544
Bram Moolenaar51485f02005-06-04 21:55:20 +00003545 /* Recursively read the children for non-shared siblings.
3546 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3547 * remove SHARED_MASK) */
3548 for (i = 1; i <= len; ++i)
3549 if (byts[startidx + i] != 0)
3550 {
3551 if (idxs[startidx + i] & SHARED_MASK)
3552 idxs[startidx + i] &= ~SHARED_MASK;
3553 else
3554 {
3555 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003556 idx = read_tree(fd, byts, idxs, maxidx, idx,
3557 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003558 if (idx < 0)
3559 break;
3560 }
3561 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003562
Bram Moolenaar51485f02005-06-04 21:55:20 +00003563 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003564}
3565
3566/*
3567 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003568 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003569 */
3570 char_u *
3571did_set_spelllang(buf)
3572 buf_T *buf;
3573{
3574 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003575 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003576 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003577 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003578 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003579 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003580 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003581 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003582 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003583 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003584 int len;
3585 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003586 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003587 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003588 char_u *use_region = NULL;
3589 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003590 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003591 int i, j;
3592 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003593
3594 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003595 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003596
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003597 /* loop over comma separated language names. */
3598 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003599 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003600 /* Get one language name. */
3601 copy_option_part(&splp, lang, MAXWLEN, ",");
3602
Bram Moolenaar5482f332005-04-17 20:18:43 +00003603 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003604 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003605
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003606 /* If the name ends in ".spl" use it as the name of the spell file.
3607 * If there is a region name let "region" point to it and remove it
3608 * from the name. */
3609 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3610 {
3611 filename = TRUE;
3612
Bram Moolenaarb6356332005-07-18 21:40:44 +00003613 /* Locate a region and remove it from the file name. */
3614 p = vim_strchr(gettail(lang), '_');
3615 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3616 && !ASCII_ISALPHA(p[3]))
3617 {
3618 vim_strncpy(region_cp, p + 1, 2);
3619 mch_memmove(p, p + 3, len - (p - lang) - 2);
3620 len -= 3;
3621 region = region_cp;
3622 }
3623 else
3624 dont_use_region = TRUE;
3625
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003626 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003627 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3628 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003629 break;
3630 }
3631 else
3632 {
3633 filename = FALSE;
3634 if (len > 3 && lang[len - 3] == '_')
3635 {
3636 region = lang + len - 2;
3637 len -= 3;
3638 lang[len] = NUL;
3639 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003640 else
3641 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003642
3643 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003644 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3645 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003646 break;
3647 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003648
Bram Moolenaarb6356332005-07-18 21:40:44 +00003649 if (region != NULL)
3650 {
3651 /* If the region differs from what was used before then don't
3652 * use it for 'spellfile'. */
3653 if (use_region != NULL && STRCMP(region, use_region) != 0)
3654 dont_use_region = TRUE;
3655 use_region = region;
3656 }
3657
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003658 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003659 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003660 {
3661 if (filename)
3662 (void)spell_load_file(lang, lang, NULL, FALSE);
3663 else
3664 spell_load_lang(lang);
3665 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003666
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003667 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003668 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003669 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003670 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3671 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
3672 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003673 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003674 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003675 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003676 {
3677 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003678 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003679 if (c == REGION_ALL)
3680 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003681 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003682 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003683 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003684 /* This addition file is for other regions. */
3685 region_mask = 0;
3686 }
3687 else
3688 /* This is probably an error. Give a warning and
3689 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003690 smsg((char_u *)
3691 _("Warning: region %s not supported"),
3692 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003693 }
3694 else
3695 region_mask = 1 << c;
3696 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003697
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003698 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003699 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003700 if (ga_grow(&ga, 1) == FAIL)
3701 {
3702 ga_clear(&ga);
3703 return e_outofmem;
3704 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003705 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003706 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3707 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003708 use_midword(slang, buf);
3709 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003710 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003711 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003712 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003713 }
3714
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003715 /* round 0: load int_wordlist, if possible.
3716 * round 1: load first name in 'spellfile'.
3717 * round 2: load second name in 'spellfile.
3718 * etc. */
3719 spf = curbuf->b_p_spf;
3720 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003721 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003722 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003723 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003724 /* Internal wordlist, if there is one. */
3725 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003726 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003727 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003728 }
3729 else
3730 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003731 /* One entry in 'spellfile'. */
3732 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3733 STRCAT(spf_name, ".spl");
3734
3735 /* If it was already found above then skip it. */
3736 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003737 {
3738 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
3739 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003740 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003741 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003742 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003743 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003744 }
3745
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003746 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003747 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3748 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003750 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003751 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003752 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003753 * region name, the region is ignored otherwise. for int_wordlist
3754 * use an arbitrary name. */
3755 if (round == 0)
3756 STRCPY(lang, "internal wordlist");
3757 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003758 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003759 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003760 p = vim_strchr(lang, '.');
3761 if (p != NULL)
3762 *p = NUL; /* truncate at ".encoding.add" */
3763 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003764 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003765
3766 /* If one of the languages has NOBREAK we assume the addition
3767 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003768 if (slang != NULL && nobreak)
3769 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003771 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003772 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003773 region_mask = REGION_ALL;
3774 if (use_region != NULL && !dont_use_region)
3775 {
3776 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003777 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003778 if (c != REGION_ALL)
3779 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003780 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003781 /* This spell file is for other regions. */
3782 region_mask = 0;
3783 }
3784
3785 if (region_mask != 0)
3786 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003787 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
3788 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
3789 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003790 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3791 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003792 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 }
3795 }
3796
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003797 /* Everything is fine, store the new b_langp value. */
3798 ga_clear(&buf->b_langp);
3799 buf->b_langp = ga;
3800
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003801 /* For each language figure out what language to use for sound folding and
3802 * REP items. If the language doesn't support it itself use another one
3803 * with the same name. E.g. for "en-math" use "en". */
3804 for (i = 0; i < ga.ga_len; ++i)
3805 {
3806 lp = LANGP_ENTRY(ga, i);
3807
3808 /* sound folding */
3809 if (lp->lp_slang->sl_sal.ga_len > 0)
3810 /* language does sound folding itself */
3811 lp->lp_sallang = lp->lp_slang;
3812 else
3813 /* find first similar language that does sound folding */
3814 for (j = 0; j < ga.ga_len; ++j)
3815 {
3816 lp2 = LANGP_ENTRY(ga, j);
3817 if (lp2->lp_slang->sl_sal.ga_len > 0
3818 && STRNCMP(lp->lp_slang->sl_name,
3819 lp2->lp_slang->sl_name, 2) == 0)
3820 {
3821 lp->lp_sallang = lp2->lp_slang;
3822 break;
3823 }
3824 }
3825
3826 /* REP items */
3827 if (lp->lp_slang->sl_rep.ga_len > 0)
3828 /* language has REP items itself */
3829 lp->lp_replang = lp->lp_slang;
3830 else
3831 /* find first similar language that does sound folding */
3832 for (j = 0; j < ga.ga_len; ++j)
3833 {
3834 lp2 = LANGP_ENTRY(ga, j);
3835 if (lp2->lp_slang->sl_rep.ga_len > 0
3836 && STRNCMP(lp->lp_slang->sl_name,
3837 lp2->lp_slang->sl_name, 2) == 0)
3838 {
3839 lp->lp_replang = lp2->lp_slang;
3840 break;
3841 }
3842 }
3843 }
3844
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003845 return NULL;
3846}
3847
3848/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003849 * Clear the midword characters for buffer "buf".
3850 */
3851 static void
3852clear_midword(buf)
3853 buf_T *buf;
3854{
3855 vim_memset(buf->b_spell_ismw, 0, 256);
3856#ifdef FEAT_MBYTE
3857 vim_free(buf->b_spell_ismw_mb);
3858 buf->b_spell_ismw_mb = NULL;
3859#endif
3860}
3861
3862/*
3863 * Use the "sl_midword" field of language "lp" for buffer "buf".
3864 * They add up to any currently used midword characters.
3865 */
3866 static void
3867use_midword(lp, buf)
3868 slang_T *lp;
3869 buf_T *buf;
3870{
3871 char_u *p;
3872
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003873 if (lp->sl_midword == NULL) /* there aren't any */
3874 return;
3875
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003876 for (p = lp->sl_midword; *p != NUL; )
3877#ifdef FEAT_MBYTE
3878 if (has_mbyte)
3879 {
3880 int c, l, n;
3881 char_u *bp;
3882
3883 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003884 l = (*mb_ptr2len)(p);
3885 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003886 buf->b_spell_ismw[c] = TRUE;
3887 else if (buf->b_spell_ismw_mb == NULL)
3888 /* First multi-byte char in "b_spell_ismw_mb". */
3889 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3890 else
3891 {
3892 /* Append multi-byte chars to "b_spell_ismw_mb". */
3893 n = STRLEN(buf->b_spell_ismw_mb);
3894 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3895 if (bp != NULL)
3896 {
3897 vim_free(buf->b_spell_ismw_mb);
3898 buf->b_spell_ismw_mb = bp;
3899 vim_strncpy(bp + n, p, l);
3900 }
3901 }
3902 p += l;
3903 }
3904 else
3905#endif
3906 buf->b_spell_ismw[*p++] = TRUE;
3907}
3908
3909/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003910 * Find the region "region[2]" in "rp" (points to "sl_regions").
3911 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003912 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913 */
3914 static int
3915find_region(rp, region)
3916 char_u *rp;
3917 char_u *region;
3918{
3919 int i;
3920
3921 for (i = 0; ; i += 2)
3922 {
3923 if (rp[i] == NUL)
3924 return REGION_ALL;
3925 if (rp[i] == region[0] && rp[i + 1] == region[1])
3926 break;
3927 }
3928 return i / 2;
3929}
3930
3931/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003932 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003933 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003934 * Word WF_ONECAP
3935 * W WORD WF_ALLCAP
3936 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003937 */
3938 static int
3939captype(word, end)
3940 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003941 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003942{
3943 char_u *p;
3944 int c;
3945 int firstcap;
3946 int allcap;
3947 int past_second = FALSE; /* past second word char */
3948
3949 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003950 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003951 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952 return 0; /* only non-word characters, illegal word */
3953#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003954 if (has_mbyte)
3955 c = mb_ptr2char_adv(&p);
3956 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003957#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003958 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003959 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003960
3961 /*
3962 * Need to check all letters to find a word with mixed upper/lower.
3963 * But a word with an upper char only at start is a ONECAP.
3964 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003965 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003966 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003967 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003968 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003969 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003970 {
3971 /* UUl -> KEEPCAP */
3972 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003973 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003974 allcap = FALSE;
3975 }
3976 else if (!allcap)
3977 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003978 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003979 past_second = TRUE;
3980 }
3981
3982 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003984 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003985 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003986 return 0;
3987}
3988
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003989/*
3990 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3991 * capital. So that make_case_word() can turn WOrd into Word.
3992 * Add ALLCAP for "WOrD".
3993 */
3994 static int
3995badword_captype(word, end)
3996 char_u *word;
3997 char_u *end;
3998{
3999 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004000 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004001 int l, u;
4002 int first;
4003 char_u *p;
4004
4005 if (flags & WF_KEEPCAP)
4006 {
4007 /* Count the number of UPPER and lower case letters. */
4008 l = u = 0;
4009 first = FALSE;
4010 for (p = word; p < end; mb_ptr_adv(p))
4011 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004012 c = PTR2CHAR(p);
4013 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004014 {
4015 ++u;
4016 if (p == word)
4017 first = TRUE;
4018 }
4019 else
4020 ++l;
4021 }
4022
4023 /* If there are more UPPER than lower case letters suggest an
4024 * ALLCAP word. Otherwise, if the first letter is UPPER then
4025 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4026 * require three upper case letters. */
4027 if (u > l && u > 2)
4028 flags |= WF_ALLCAP;
4029 else if (first)
4030 flags |= WF_ONECAP;
4031 }
4032 return flags;
4033}
4034
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004035# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4036/*
4037 * Free all languages.
4038 */
4039 void
4040spell_free_all()
4041{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004042 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004043 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004044 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004045
4046 /* Go through all buffers and handle 'spelllang'. */
4047 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4048 ga_clear(&buf->b_langp);
4049
4050 while (first_lang != NULL)
4051 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004052 slang = first_lang;
4053 first_lang = slang->sl_next;
4054 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004055 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004056
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004057 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004058 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004059 /* Delete the internal wordlist and its .spl file */
4060 mch_remove(int_wordlist);
4061 int_wordlist_spl(fname);
4062 mch_remove(fname);
4063 vim_free(int_wordlist);
4064 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004065 }
4066
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004067 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004068}
4069# endif
4070
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004071# if defined(FEAT_MBYTE) || defined(PROTO)
4072/*
4073 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004074 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 */
4076 void
4077spell_reload()
4078{
4079 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004080 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004081
Bram Moolenaarea408852005-06-25 22:49:46 +00004082 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004083 init_spell_chartab();
4084
4085 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004086 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087
4088 /* Go through all buffers and handle 'spelllang'. */
4089 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4090 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004091 /* Only load the wordlists when 'spelllang' is set and there is a
4092 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004094 {
4095 FOR_ALL_WINDOWS(wp)
4096 if (wp->w_buffer == buf && wp->w_p_spell)
4097 {
4098 (void)did_set_spelllang(buf);
4099# ifdef FEAT_WINDOWS
4100 break;
4101# endif
4102 }
4103 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004104 }
4105}
4106# endif
4107
Bram Moolenaarb765d632005-06-07 21:00:02 +00004108/*
4109 * Reload the spell file "fname" if it's loaded.
4110 */
4111 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004113 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004115{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004116 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004117 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004118
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004119 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004120 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004121 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004122 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004123 slang_clear(slang);
4124 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004125 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004126 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004127 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004128 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004129 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004131
4132 /* When "zg" was used and the file wasn't loaded yet, should redo
4133 * 'spelllang' to get it loaded. */
4134 if (added_word && !didit)
4135 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004136}
4137
4138
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004139/*
4140 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004141 */
4142
Bram Moolenaar51485f02005-06-04 21:55:20 +00004143#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004144 and .dic file. */
4145/*
4146 * Main structure to store the contents of a ".aff" file.
4147 */
4148typedef struct afffile_S
4149{
4150 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004151 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004152 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004153 unsigned af_rar; /* RAR ID for rare word */
4154 unsigned af_kep; /* KEP ID for keep-case word */
4155 unsigned af_bad; /* BAD ID for banned word */
4156 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004157 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004158 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004159 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4160 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004161 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004162} afffile_T;
4163
Bram Moolenaar6de68532005-08-24 22:08:48 +00004164#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004165#define AFT_LONG 1 /* flags are two characters */
4166#define AFT_CAPLONG 2 /* flags are one or two characters */
4167#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004168
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004169typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004170/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4171struct affentry_S
4172{
4173 affentry_T *ae_next; /* next affix with same name/number */
4174 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4175 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004176 char_u *ae_cond; /* condition (NULL for ".") */
4177 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004178 char_u ae_rare; /* rare affix */
4179 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004180};
4181
Bram Moolenaar6de68532005-08-24 22:08:48 +00004182#ifdef FEAT_MBYTE
4183# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4184#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004185# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004186#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004187
Bram Moolenaar51485f02005-06-04 21:55:20 +00004188/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4189typedef struct affheader_S
4190{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004191 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4192 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4193 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004194 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004195 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004196 affentry_T *ah_first; /* first affix entry */
4197} affheader_T;
4198
4199#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4200
Bram Moolenaar6de68532005-08-24 22:08:48 +00004201/* Flag used in compound items. */
4202typedef struct compitem_S
4203{
4204 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4205 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4206 int ci_newID; /* affix ID after renumbering. */
4207} compitem_T;
4208
4209#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4210
Bram Moolenaar51485f02005-06-04 21:55:20 +00004211/*
4212 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004213 * the need to keep track of each allocated thing, everything is freed all at
4214 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004215 */
4216#define SBLOCKSIZE 16000 /* size of sb_data */
4217typedef struct sblock_S sblock_T;
4218struct sblock_S
4219{
4220 sblock_T *sb_next; /* next block in list */
4221 int sb_used; /* nr of bytes already in use */
4222 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004223};
4224
4225/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004226 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004227 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004228typedef struct wordnode_S wordnode_T;
4229struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004230{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004231 union /* shared to save space */
4232 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004233 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004234 int index; /* index in written nodes (valid after first
4235 round) */
4236 } wn_u1;
4237 union /* shared to save space */
4238 {
4239 wordnode_T *next; /* next node with same hash key */
4240 wordnode_T *wnode; /* parent node that will write this node */
4241 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004242 wordnode_T *wn_child; /* child (next byte in word) */
4243 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4244 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004245 int wn_refs; /* Nr. of references to this node. Only
4246 relevant for first node in a list of
4247 siblings, in following siblings it is
4248 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004249 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004250 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004251 prefix ID or 0 */
4252 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4253 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004254 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004255#ifdef SPELL_PRINTTREE
4256 int wn_nr; /* sequence nr for printing */
4257#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004258};
4259
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004260#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4261
Bram Moolenaar51485f02005-06-04 21:55:20 +00004262#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004263
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264/*
4265 * Info used while reading the spell files.
4266 */
4267typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004268{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004269 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004270 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004271
Bram Moolenaar51485f02005-06-04 21:55:20 +00004272 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004273 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004274
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004275 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004276
Bram Moolenaar51485f02005-06-04 21:55:20 +00004277 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004278 long si_blocks_cnt; /* memory blocks allocated */
4279 long si_compress_cnt; /* words to add before lowering
4280 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004281 wordnode_T *si_first_free; /* List of nodes that have been freed during
4282 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004283 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004284#ifdef SPELL_PRINTTREE
4285 int si_wordnode_nr; /* sequence nr for nodes */
4286#endif
4287
4288
Bram Moolenaar51485f02005-06-04 21:55:20 +00004289 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004290 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004291 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004292 int si_region; /* region mask */
4293 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004294 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004295 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004296 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004297 int si_region_count; /* number of regions supported (1 when there
4298 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004299 char_u si_region_name[16]; /* region names; used only if
4300 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301
4302 garray_T si_rep; /* list of fromto_T entries from REP lines */
4303 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004304 char_u *si_sofofr; /* SOFOFROM text */
4305 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 int si_followup; /* soundsalike: ? */
4307 int si_collapse; /* soundsalike: ? */
4308 int si_rem_accents; /* soundsalike: remove accents */
4309 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004310 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004311 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004312 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004313 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004314 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004315 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004316 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004317 garray_T si_prefcond; /* table with conditions for postponed
4318 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004319 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004320 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004321} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004322
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004323static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004324static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4325static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4326static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004327static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004328static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4329static void aff_check_number __ARGS((int spinval, int affval, char *name));
4330static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004331static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4333static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004334static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004335static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004336static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004337static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004338static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004339static 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 +00004340static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4341static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4342static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004343static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004344static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004345static 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 +00004346static 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 +00004347static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4348static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4349static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4350static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4351static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004352static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004353static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004354static void clear_node __ARGS((wordnode_T *node));
4355static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004357static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004358
Bram Moolenaar53805d12005-08-01 07:08:33 +00004359/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4360 * but it must be negative to indicate the prefix tree to tree_add_word().
4361 * Use a negative number with the lower 8 bits zero. */
4362#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004363
Bram Moolenaar5195e452005-08-19 20:32:47 +00004364/*
4365 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4366 */
4367static long compress_start = 30000; /* memory / SBLOCKSIZE */
4368static long compress_inc = 100; /* memory / SBLOCKSIZE */
4369static long compress_added = 500000; /* word count */
4370
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004371#ifdef SPELL_PRINTTREE
4372/*
4373 * For debugging the tree code: print the current tree in a (more or less)
4374 * readable format, so that we can see what happens when adding a word and/or
4375 * compressing the tree.
4376 * Based on code from Olaf Seibert.
4377 */
4378#define PRINTLINESIZE 1000
4379#define PRINTWIDTH 6
4380
4381#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4382 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4383
4384static char line1[PRINTLINESIZE];
4385static char line2[PRINTLINESIZE];
4386static char line3[PRINTLINESIZE];
4387
4388 static void
4389spell_clear_flags(wordnode_T *node)
4390{
4391 wordnode_T *np;
4392
4393 for (np = node; np != NULL; np = np->wn_sibling)
4394 {
4395 np->wn_u1.index = FALSE;
4396 spell_clear_flags(np->wn_child);
4397 }
4398}
4399
4400 static void
4401spell_print_node(wordnode_T *node, int depth)
4402{
4403 if (node->wn_u1.index)
4404 {
4405 /* Done this node before, print the reference. */
4406 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4407 PRINTSOME(line2, depth, " ", 0, 0);
4408 PRINTSOME(line3, depth, " ", 0, 0);
4409 msg(line1);
4410 msg(line2);
4411 msg(line3);
4412 }
4413 else
4414 {
4415 node->wn_u1.index = TRUE;
4416
4417 if (node->wn_byte != NUL)
4418 {
4419 if (node->wn_child != NULL)
4420 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4421 else
4422 /* Cannot happen? */
4423 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4424 }
4425 else
4426 PRINTSOME(line1, depth, " $ ", 0, 0);
4427
4428 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4429
4430 if (node->wn_sibling != NULL)
4431 PRINTSOME(line3, depth, " | ", 0, 0);
4432 else
4433 PRINTSOME(line3, depth, " ", 0, 0);
4434
4435 if (node->wn_byte == NUL)
4436 {
4437 msg(line1);
4438 msg(line2);
4439 msg(line3);
4440 }
4441
4442 /* do the children */
4443 if (node->wn_byte != NUL && node->wn_child != NULL)
4444 spell_print_node(node->wn_child, depth + 1);
4445
4446 /* do the siblings */
4447 if (node->wn_sibling != NULL)
4448 {
4449 /* get rid of all parent details except | */
4450 STRCPY(line1, line3);
4451 STRCPY(line2, line3);
4452 spell_print_node(node->wn_sibling, depth);
4453 }
4454 }
4455}
4456
4457 static void
4458spell_print_tree(wordnode_T *root)
4459{
4460 if (root != NULL)
4461 {
4462 /* Clear the "wn_u1.index" fields, used to remember what has been
4463 * done. */
4464 spell_clear_flags(root);
4465
4466 /* Recursively print the tree. */
4467 spell_print_node(root, 0);
4468 }
4469}
4470#endif /* SPELL_PRINTTREE */
4471
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004473 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004474 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004475 */
4476 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004477spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004478 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004479 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004480{
4481 FILE *fd;
4482 afffile_T *aff;
4483 char_u rline[MAXLINELEN];
4484 char_u *line;
4485 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004486#define MAXITEMCNT 7
4487 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004488 int itemcnt;
4489 char_u *p;
4490 int lnum = 0;
4491 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004492 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004493 int aff_todo = 0;
4494 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004495 char_u *low = NULL;
4496 char_u *fol = NULL;
4497 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004498 int do_rep;
4499 int do_sal;
4500 int do_map;
4501 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004502 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004503 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004504 int compminlen = 0; /* COMPOUNDMIN value */
4505 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4506 int compmax = 0; /* COMPOUNDMAX value */
4507 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4508 concatenated */
4509 char_u *midword = NULL; /* MIDWORD value */
4510 char_u *syllable = NULL; /* SYLLABLE value */
4511 char_u *sofofrom = NULL; /* SOFOFROM value */
4512 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004513
Bram Moolenaar51485f02005-06-04 21:55:20 +00004514 /*
4515 * Open the file.
4516 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004517 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518 if (fd == NULL)
4519 {
4520 EMSG2(_(e_notopen), fname);
4521 return NULL;
4522 }
4523
Bram Moolenaarb765d632005-06-07 21:00:02 +00004524 if (spin->si_verbose || p_verbose > 2)
4525 {
4526 if (!spin->si_verbose)
4527 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004528 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004529 out_flush();
4530 if (!spin->si_verbose)
4531 verbose_leave();
4532 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004534 /* Only do REP lines when not done in another .aff file already. */
4535 do_rep = spin->si_rep.ga_len == 0;
4536
4537 /* Only do SAL lines when not done in another .aff file already. */
4538 do_sal = spin->si_sal.ga_len == 0;
4539
4540 /* Only do MAP lines when not done in another .aff file already. */
4541 do_map = spin->si_map.ga_len == 0;
4542
Bram Moolenaar51485f02005-06-04 21:55:20 +00004543 /*
4544 * Allocate and init the afffile_T structure.
4545 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004546 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004547 if (aff == NULL)
4548 return NULL;
4549 hash_init(&aff->af_pref);
4550 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004551 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004552
4553 /*
4554 * Read all the lines in the file one by one.
4555 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004556 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004557 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004558 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004559 ++lnum;
4560
4561 /* Skip comment lines. */
4562 if (*rline == '#')
4563 continue;
4564
4565 /* Convert from "SET" to 'encoding' when needed. */
4566 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004567#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004568 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004570 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004571 if (pc == NULL)
4572 {
4573 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4574 fname, lnum, rline);
4575 continue;
4576 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 line = pc;
4578 }
4579 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004580#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004581 {
4582 pc = NULL;
4583 line = rline;
4584 }
4585
4586 /* Split the line up in white separated items. Put a NUL after each
4587 * item. */
4588 itemcnt = 0;
4589 for (p = line; ; )
4590 {
4591 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4592 ++p;
4593 if (*p == NUL)
4594 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004595 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004596 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004597 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004598 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004599 ++p;
4600 if (*p == NUL)
4601 break;
4602 *p++ = NUL;
4603 }
4604
4605 /* Handle non-empty lines. */
4606 if (itemcnt > 0)
4607 {
4608 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4609 && aff->af_enc == NULL)
4610 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004611#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004612 /* Setup for conversion from "ENC" to 'encoding'. */
4613 aff->af_enc = enc_canonize(items[1]);
4614 if (aff->af_enc != NULL && !spin->si_ascii
4615 && convert_setup(&spin->si_conv, aff->af_enc,
4616 p_enc) == FAIL)
4617 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4618 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004619 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004620#else
4621 smsg((char_u *)_("Conversion in %s not supported"), fname);
4622#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004623 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004624 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4625 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004626 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004627 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004628 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004629 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004630 aff->af_flagtype = AFT_NUM;
4631 else if (STRCMP(items[1], "caplong") == 0)
4632 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004633 else
4634 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4635 fname, lnum, items[1]);
4636 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004637 || aff->af_needaffix != 0
4638 || aff->af_needcomp != 0
4639 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004640 || aff->af_suff.ht_used > 0
4641 || aff->af_pref.ht_used > 0)
4642 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4643 fname, lnum, items[1]);
4644 }
4645 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4646 && midword == NULL)
4647 {
4648 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004649 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004650 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004652 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004653 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004654 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004657 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004658 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4659 && aff->af_slash == 0)
4660 {
4661 aff->af_slash = items[1][0];
4662 if (items[1][1] != NUL)
4663 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4664 fname, lnum, items[1]);
4665 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004666 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4667 && aff->af_rar == 0)
4668 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004669 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4670 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004671 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004672 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4673 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004674 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004675 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4676 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004677 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004678 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4679 && aff->af_bad == 0)
4680 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004681 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4682 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004683 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004684 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4685 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004686 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004687 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4688 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004689 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004690 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
4691 && aff->af_needcomp == 0)
4692 {
4693 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
4694 fname, lnum);
4695 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004696 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004697 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004698 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004699 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4700 * "Na" into "Na+", "1234" into "1234+". */
4701 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004702 if (p != NULL)
4703 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004704 STRCPY(p, items[1]);
4705 STRCAT(p, "+");
4706 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004707 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004708 }
4709 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4710 {
4711 /* Concatenate this string to previously defined ones, using a
4712 * slash to separate them. */
4713 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004714 if (compflags != NULL)
4715 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004716 p = getroom(spin, l, FALSE);
4717 if (p != NULL)
4718 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004719 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004720 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004721 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004722 STRCAT(p, "/");
4723 }
4724 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004725 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004726 }
4727 }
4728 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004729 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004730 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004731 compmax = atoi((char *)items[1]);
4732 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004733 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4734 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004735 }
4736 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004737 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004738 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004739 compminlen = atoi((char *)items[1]);
4740 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004741 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4742 fname, lnum, items[1]);
4743 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004744 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004745 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004746 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004747 compsylmax = atoi((char *)items[1]);
4748 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004749 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4750 fname, lnum, items[1]);
4751 }
4752 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004753 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004754 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004755 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004756 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004757 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4758 {
4759 spin->si_nobreak = TRUE;
4760 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004761 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4762 {
4763 aff->af_pfxpostpone = TRUE;
4764 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004765 else if ((STRCMP(items[0], "PFX") == 0
4766 || STRCMP(items[0], "SFX") == 0)
4767 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004768 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004769 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004770 int lasti = 4;
4771 char_u key[AH_KEY_LEN];
4772
4773 if (*items[0] == 'P')
4774 tp = &aff->af_pref;
4775 else
4776 tp = &aff->af_suff;
4777
4778 /* Myspell allows the same affix name to be used multiple
4779 * times. The affix files that do this have an undocumented
4780 * "S" flag on all but the last block, thus we check for that
4781 * and store it in ah_follows. */
4782 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4783 hi = hash_find(tp, key);
4784 if (!HASHITEM_EMPTY(hi))
4785 {
4786 cur_aff = HI2AH(hi);
4787 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4788 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4789 fname, lnum, items[1]);
4790 if (!cur_aff->ah_follows)
4791 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4792 fname, lnum, items[1]);
4793 }
4794 else
4795 {
4796 /* New affix letter. */
4797 cur_aff = (affheader_T *)getroom(spin,
4798 sizeof(affheader_T), TRUE);
4799 if (cur_aff == NULL)
4800 break;
4801 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4802 fname, lnum);
4803 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4804 break;
4805 if (cur_aff->ah_flag == aff->af_bad
4806 || cur_aff->ah_flag == aff->af_rar
4807 || cur_aff->ah_flag == aff->af_kep
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004808 || cur_aff->ah_flag == aff->af_needaffix
4809 || cur_aff->ah_flag == aff->af_needcomp)
4810 smsg((char_u *)_("Affix also used for BAD/RAR/KEP/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00004811 fname, lnum, items[1]);
4812 STRCPY(cur_aff->ah_key, items[1]);
4813 hash_add(tp, cur_aff->ah_key);
4814
4815 cur_aff->ah_combine = (*items[2] == 'Y');
4816 }
4817
4818 /* Check for the "S" flag, which apparently means that another
4819 * block with the same affix name is following. */
4820 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4821 {
4822 ++lasti;
4823 cur_aff->ah_follows = TRUE;
4824 }
4825 else
4826 cur_aff->ah_follows = FALSE;
4827
Bram Moolenaar8db73182005-06-17 21:51:16 +00004828 /* Myspell allows extra text after the item, but that might
4829 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004830 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004831 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4832 fname, lnum, items[4]);
4833
Bram Moolenaar95529562005-08-25 21:21:38 +00004834 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004835 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4836 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004837
Bram Moolenaar95529562005-08-25 21:21:38 +00004838 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004839 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004840 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004841 {
4842 /* Use a new number in the .spl file later, to be able
4843 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004844 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004845 cur_aff->ah_newID = ++spin->si_newprefID;
4846
4847 /* We only really use ah_newID if the prefix is
4848 * postponed. We know that only after handling all
4849 * the items. */
4850 did_postpone_prefix = FALSE;
4851 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004852 else
4853 /* Did use the ID in a previous block. */
4854 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004855 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004856
Bram Moolenaar51485f02005-06-04 21:55:20 +00004857 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004858 }
4859 else if ((STRCMP(items[0], "PFX") == 0
4860 || STRCMP(items[0], "SFX") == 0)
4861 && aff_todo > 0
4862 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004863 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004864 {
4865 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004866 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004867 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004868 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004869 int lasti = 5;
4870
Bram Moolenaar5195e452005-08-19 20:32:47 +00004871 /* Check for "rare" and "nocomp" after the other info. */
4872 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004873 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004874 if (!rare && STRICMP(items[lasti], "rare") == 0)
4875 {
4876 rare = TRUE;
4877 ++lasti;
4878 }
4879 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4880 {
4881 nocomp = TRUE;
4882 ++lasti;
4883 }
4884 else
4885 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004886 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004887
Bram Moolenaar8db73182005-06-17 21:51:16 +00004888 /* Myspell allows extra text after the item, but that might
4889 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004890 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004891 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004892
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004893 /* New item for an affix letter. */
4894 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004895 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004896 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004897 if (aff_entry == NULL)
4898 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004899 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004900 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004901
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004902 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004903 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004904 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004905 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004906
Bram Moolenaar51485f02005-06-04 21:55:20 +00004907 /* Don't use an affix entry with non-ASCII characters when
4908 * "spin->si_ascii" is TRUE. */
4909 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004910 || has_non_ascii(aff_entry->ae_add)))
4911 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004912 aff_entry->ae_next = cur_aff->ah_first;
4913 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004914
4915 if (STRCMP(items[4], ".") != 0)
4916 {
4917 char_u buf[MAXLINELEN];
4918
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004919 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004920 if (*items[0] == 'P')
4921 sprintf((char *)buf, "^%s", items[4]);
4922 else
4923 sprintf((char *)buf, "%s$", items[4]);
4924 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004925 RE_MAGIC + RE_STRING + RE_STRICT);
4926 if (aff_entry->ae_prog == NULL)
4927 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4928 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004929 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004930
4931 /* For postponed prefixes we need an entry in si_prefcond
4932 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004933 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004934 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004935 /* When the chop string is one lower-case letter and
4936 * the add string ends in the upper-case letter we set
4937 * the "upper" flag, clear "ae_chop" and remove the
4938 * letters from "ae_add". The condition must either
4939 * be empty or start with the same letter. */
4940 if (aff_entry->ae_chop != NULL
4941 && aff_entry->ae_add != NULL
4942#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004943 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004944 aff_entry->ae_chop)] == NUL
4945#else
4946 && aff_entry->ae_chop[1] == NUL
4947#endif
4948 )
4949 {
4950 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004951
Bram Moolenaar53805d12005-08-01 07:08:33 +00004952 c = PTR2CHAR(aff_entry->ae_chop);
4953 c_up = SPELL_TOUPPER(c);
4954 if (c_up != c
4955 && (aff_entry->ae_cond == NULL
4956 || PTR2CHAR(aff_entry->ae_cond) == c))
4957 {
4958 p = aff_entry->ae_add
4959 + STRLEN(aff_entry->ae_add);
4960 mb_ptr_back(aff_entry->ae_add, p);
4961 if (PTR2CHAR(p) == c_up)
4962 {
4963 upper = TRUE;
4964 aff_entry->ae_chop = NULL;
4965 *p = NUL;
4966
4967 /* The condition is matched with the
4968 * actual word, thus must check for the
4969 * upper-case letter. */
4970 if (aff_entry->ae_cond != NULL)
4971 {
4972 char_u buf[MAXLINELEN];
4973#ifdef FEAT_MBYTE
4974 if (has_mbyte)
4975 {
4976 onecap_copy(items[4], buf, TRUE);
4977 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004978 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004979 }
4980 else
4981#endif
4982 *aff_entry->ae_cond = c_up;
4983 if (aff_entry->ae_cond != NULL)
4984 {
4985 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004986 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004987 vim_free(aff_entry->ae_prog);
4988 aff_entry->ae_prog = vim_regcomp(
4989 buf, RE_MAGIC + RE_STRING);
4990 }
4991 }
4992 }
4993 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004994 }
4995
Bram Moolenaar53805d12005-08-01 07:08:33 +00004996 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004997 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004998 int idx;
4999 char_u **pp;
5000 int n;
5001
Bram Moolenaar6de68532005-08-24 22:08:48 +00005002 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005003 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5004 --idx)
5005 {
5006 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5007 if (str_equal(p, aff_entry->ae_cond))
5008 break;
5009 }
5010 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5011 {
5012 /* Not found, add a new condition. */
5013 idx = spin->si_prefcond.ga_len++;
5014 pp = ((char_u **)spin->si_prefcond.ga_data)
5015 + idx;
5016 if (aff_entry->ae_cond == NULL)
5017 *pp = NULL;
5018 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005019 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005020 aff_entry->ae_cond);
5021 }
5022
5023 /* Add the prefix to the prefix tree. */
5024 if (aff_entry->ae_add == NULL)
5025 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005026 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005027 p = aff_entry->ae_add;
5028 /* PFX_FLAGS is a negative number, so that
5029 * tree_add_word() knows this is the prefix tree. */
5030 n = PFX_FLAGS;
5031 if (rare)
5032 n |= WFP_RARE;
5033 if (!cur_aff->ah_combine)
5034 n |= WFP_NC;
5035 if (upper)
5036 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005037 tree_add_word(spin, p, spin->si_prefroot, n,
5038 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005039 did_postpone_prefix = TRUE;
5040 }
5041
5042 /* Didn't actually use ah_newID, backup si_newprefID. */
5043 if (aff_todo == 0 && !did_postpone_prefix)
5044 {
5045 --spin->si_newprefID;
5046 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005047 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005048 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005049 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005050 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005051 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5052 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005053 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005054 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005055 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005056 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5057 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005058 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005059 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005060 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005061 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5062 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005063 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005064 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005065 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005067 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005069 if (!isdigit(*items[1]))
5070 smsg((char_u *)_("Expected REP count in %s line %d"),
5071 fname, lnum);
5072 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005073 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005074 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005075 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005076 /* Myspell ignores extra arguments, we require it starts with
5077 * # to detect mistakes. */
5078 if (itemcnt > 3 && items[3][0] != '#')
5079 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005080 if (do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005081 {
5082 /* Replace underscore with space (can't include a space
5083 * directly). */
5084 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5085 if (*p == '_')
5086 *p = ' ';
5087 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5088 if (*p == '_')
5089 *p = ' ';
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005090 add_fromto(spin, &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005092 }
5093 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5094 {
5095 /* MAP item or count */
5096 if (!found_map)
5097 {
5098 /* First line contains the count. */
5099 found_map = TRUE;
5100 if (!isdigit(*items[1]))
5101 smsg((char_u *)_("Expected MAP count in %s line %d"),
5102 fname, lnum);
5103 }
5104 else if (do_map)
5105 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005106 int c;
5107
5108 /* Check that every character appears only once. */
5109 for (p = items[1]; *p != NUL; )
5110 {
5111#ifdef FEAT_MBYTE
5112 c = mb_ptr2char_adv(&p);
5113#else
5114 c = *p++;
5115#endif
5116 if ((spin->si_map.ga_len > 0
5117 && vim_strchr(spin->si_map.ga_data, c)
5118 != NULL)
5119 || vim_strchr(p, c) != NULL)
5120 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5121 fname, lnum);
5122 }
5123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005124 /* We simply concatenate all the MAP strings, separated by
5125 * slashes. */
5126 ga_concat(&spin->si_map, items[1]);
5127 ga_append(&spin->si_map, '/');
5128 }
5129 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005130 /* Accept "SAL from to" and "SAL from to # comment". */
5131 else if (STRCMP(items[0], "SAL") == 0
5132 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133 {
5134 if (do_sal)
5135 {
5136 /* SAL item (sounds-a-like)
5137 * Either one of the known keys or a from-to pair. */
5138 if (STRCMP(items[1], "followup") == 0)
5139 spin->si_followup = sal_to_bool(items[2]);
5140 else if (STRCMP(items[1], "collapse_result") == 0)
5141 spin->si_collapse = sal_to_bool(items[2]);
5142 else if (STRCMP(items[1], "remove_accents") == 0)
5143 spin->si_rem_accents = sal_to_bool(items[2]);
5144 else
5145 /* when "to" is "_" it means empty */
5146 add_fromto(spin, &spin->si_sal, items[1],
5147 STRCMP(items[2], "_") == 0 ? (char_u *)""
5148 : items[2]);
5149 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005150 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005151 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005152 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005153 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005154 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005155 }
5156 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005157 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005158 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005159 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005160 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005161 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005162 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005163 fname, lnum, items[0]);
5164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005165 }
5166
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005167 if (fol != NULL || low != NULL || upp != NULL)
5168 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005169 if (spin->si_clear_chartab)
5170 {
5171 /* Clear the char type tables, don't want to use any of the
5172 * currently used spell properties. */
5173 init_spell_chartab();
5174 spin->si_clear_chartab = FALSE;
5175 }
5176
Bram Moolenaar3982c542005-06-08 21:56:31 +00005177 /*
5178 * Don't write a word table for an ASCII file, so that we don't check
5179 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005180 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005181 * mb_get_class(), the list of chars in the file will be incomplete.
5182 */
5183 if (!spin->si_ascii
5184#ifdef FEAT_MBYTE
5185 && !enc_utf8
5186#endif
5187 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005188 {
5189 if (fol == NULL || low == NULL || upp == NULL)
5190 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5191 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005192 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005193 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005194
5195 vim_free(fol);
5196 vim_free(low);
5197 vim_free(upp);
5198 }
5199
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005200 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005201 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005202 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005203 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5204 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005205 }
5206
Bram Moolenaar6de68532005-08-24 22:08:48 +00005207 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005208 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005209 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5210 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005211 }
5212
Bram Moolenaar6de68532005-08-24 22:08:48 +00005213 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005214 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005215 if (syllable == NULL)
5216 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5217 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5218 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005219 }
5220
Bram Moolenaar6de68532005-08-24 22:08:48 +00005221 if (compflags != NULL)
5222 process_compflags(spin, aff, compflags);
5223
5224 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005225 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005226 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005227 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005228 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005229 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005230 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005231 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005232 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005233 }
5234
Bram Moolenaar6de68532005-08-24 22:08:48 +00005235 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005236 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005237 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5238 spin->si_syllable = syllable;
5239 }
5240
5241 if (sofofrom != NULL || sofoto != NULL)
5242 {
5243 if (sofofrom == NULL || sofoto == NULL)
5244 smsg((char_u *)_("Missing SOFO%s line in %s"),
5245 sofofrom == NULL ? "FROM" : "TO", fname);
5246 else if (spin->si_sal.ga_len > 0)
5247 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005248 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005249 {
5250 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5251 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5252 spin->si_sofofr = sofofrom;
5253 spin->si_sofoto = sofoto;
5254 }
5255 }
5256
5257 if (midword != NULL)
5258 {
5259 aff_check_string(spin->si_midword, midword, "MIDWORD");
5260 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005261 }
5262
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005263 vim_free(pc);
5264 fclose(fd);
5265 return aff;
5266}
5267
5268/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005269 * Turn an affix flag name into a number, according to the FLAG type.
5270 * returns zero for failure.
5271 */
5272 static unsigned
5273affitem2flag(flagtype, item, fname, lnum)
5274 int flagtype;
5275 char_u *item;
5276 char_u *fname;
5277 int lnum;
5278{
5279 unsigned res;
5280 char_u *p = item;
5281
5282 res = get_affitem(flagtype, &p);
5283 if (res == 0)
5284 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005285 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005286 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5287 fname, lnum, item);
5288 else
5289 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5290 fname, lnum, item);
5291 }
5292 if (*p != NUL)
5293 {
5294 smsg((char_u *)_(e_affname), fname, lnum, item);
5295 return 0;
5296 }
5297
5298 return res;
5299}
5300
5301/*
5302 * Get one affix name from "*pp" and advance the pointer.
5303 * Returns zero for an error, still advances the pointer then.
5304 */
5305 static unsigned
5306get_affitem(flagtype, pp)
5307 int flagtype;
5308 char_u **pp;
5309{
5310 int res;
5311
Bram Moolenaar95529562005-08-25 21:21:38 +00005312 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005313 {
5314 if (!VIM_ISDIGIT(**pp))
5315 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005316 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005317 return 0;
5318 }
5319 res = getdigits(pp);
5320 }
5321 else
5322 {
5323#ifdef FEAT_MBYTE
5324 res = mb_ptr2char_adv(pp);
5325#else
5326 res = *(*pp)++;
5327#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005328 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005329 && res >= 'A' && res <= 'Z'))
5330 {
5331 if (**pp == NUL)
5332 return 0;
5333#ifdef FEAT_MBYTE
5334 res = mb_ptr2char_adv(pp) + (res << 16);
5335#else
5336 res = *(*pp)++ + (res << 16);
5337#endif
5338 }
5339 }
5340 return res;
5341}
5342
5343/*
5344 * Process the "compflags" string used in an affix file and append it to
5345 * spin->si_compflags.
5346 * The processing involves changing the affix names to ID numbers, so that
5347 * they fit in one byte.
5348 */
5349 static void
5350process_compflags(spin, aff, compflags)
5351 spellinfo_T *spin;
5352 afffile_T *aff;
5353 char_u *compflags;
5354{
5355 char_u *p;
5356 char_u *prevp;
5357 unsigned flag;
5358 compitem_T *ci;
5359 int id;
5360 int len;
5361 char_u *tp;
5362 char_u key[AH_KEY_LEN];
5363 hashitem_T *hi;
5364
5365 /* Make room for the old and the new compflags, concatenated with a / in
5366 * between. Processing it makes it shorter, but we don't know by how
5367 * much, thus allocate the maximum. */
5368 len = STRLEN(compflags) + 1;
5369 if (spin->si_compflags != NULL)
5370 len += STRLEN(spin->si_compflags) + 1;
5371 p = getroom(spin, len, FALSE);
5372 if (p == NULL)
5373 return;
5374 if (spin->si_compflags != NULL)
5375 {
5376 STRCPY(p, spin->si_compflags);
5377 STRCAT(p, "/");
5378 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005379 spin->si_compflags = p;
5380 tp = p + STRLEN(p);
5381
5382 for (p = compflags; *p != NUL; )
5383 {
5384 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5385 /* Copy non-flag characters directly. */
5386 *tp++ = *p++;
5387 else
5388 {
5389 /* First get the flag number, also checks validity. */
5390 prevp = p;
5391 flag = get_affitem(aff->af_flagtype, &p);
5392 if (flag != 0)
5393 {
5394 /* Find the flag in the hashtable. If it was used before, use
5395 * the existing ID. Otherwise add a new entry. */
5396 vim_strncpy(key, prevp, p - prevp);
5397 hi = hash_find(&aff->af_comp, key);
5398 if (!HASHITEM_EMPTY(hi))
5399 id = HI2CI(hi)->ci_newID;
5400 else
5401 {
5402 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5403 if (ci == NULL)
5404 break;
5405 STRCPY(ci->ci_key, key);
5406 ci->ci_flag = flag;
5407 /* Avoid using a flag ID that has a special meaning in a
5408 * regexp (also inside []). */
5409 do
5410 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005411 check_renumber(spin);
5412 id = spin->si_newcompID--;
5413 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005414 ci->ci_newID = id;
5415 hash_add(&aff->af_comp, ci->ci_key);
5416 }
5417 *tp++ = id;
5418 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005419 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005420 ++p;
5421 }
5422 }
5423
5424 *tp = NUL;
5425}
5426
5427/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005428 * Check that the new IDs for postponed affixes and compounding don't overrun
5429 * each other. We have almost 255 available, but start at 0-127 to avoid
5430 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5431 * When that is used up an error message is given.
5432 */
5433 static void
5434check_renumber(spin)
5435 spellinfo_T *spin;
5436{
5437 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5438 {
5439 spin->si_newprefID = 127;
5440 spin->si_newcompID = 255;
5441 }
5442}
5443
5444/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005445 * Return TRUE if flag "flag" appears in affix list "afflist".
5446 */
5447 static int
5448flag_in_afflist(flagtype, afflist, flag)
5449 int flagtype;
5450 char_u *afflist;
5451 unsigned flag;
5452{
5453 char_u *p;
5454 unsigned n;
5455
5456 switch (flagtype)
5457 {
5458 case AFT_CHAR:
5459 return vim_strchr(afflist, flag) != NULL;
5460
Bram Moolenaar95529562005-08-25 21:21:38 +00005461 case AFT_CAPLONG:
5462 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005463 for (p = afflist; *p != NUL; )
5464 {
5465#ifdef FEAT_MBYTE
5466 n = mb_ptr2char_adv(&p);
5467#else
5468 n = *p++;
5469#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005470 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005471 && *p != NUL)
5472#ifdef FEAT_MBYTE
5473 n = mb_ptr2char_adv(&p) + (n << 16);
5474#else
5475 n = *p++ + (n << 16);
5476#endif
5477 if (n == flag)
5478 return TRUE;
5479 }
5480 break;
5481
Bram Moolenaar95529562005-08-25 21:21:38 +00005482 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005483 for (p = afflist; *p != NUL; )
5484 {
5485 n = getdigits(&p);
5486 if (n == flag)
5487 return TRUE;
5488 if (*p != NUL) /* skip over comma */
5489 ++p;
5490 }
5491 break;
5492 }
5493 return FALSE;
5494}
5495
5496/*
5497 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5498 */
5499 static void
5500aff_check_number(spinval, affval, name)
5501 int spinval;
5502 int affval;
5503 char *name;
5504{
5505 if (spinval != 0 && spinval != affval)
5506 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5507}
5508
5509/*
5510 * Give a warning when "spinval" and "affval" strings are set and not the same.
5511 */
5512 static void
5513aff_check_string(spinval, affval, name)
5514 char_u *spinval;
5515 char_u *affval;
5516 char *name;
5517{
5518 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5519 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5520}
5521
5522/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005523 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5524 * NULL as equal.
5525 */
5526 static int
5527str_equal(s1, s2)
5528 char_u *s1;
5529 char_u *s2;
5530{
5531 if (s1 == NULL || s2 == NULL)
5532 return s1 == s2;
5533 return STRCMP(s1, s2) == 0;
5534}
5535
5536/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005537 * Add a from-to item to "gap". Used for REP and SAL items.
5538 * They are stored case-folded.
5539 */
5540 static void
5541add_fromto(spin, gap, from, to)
5542 spellinfo_T *spin;
5543 garray_T *gap;
5544 char_u *from;
5545 char_u *to;
5546{
5547 fromto_T *ftp;
5548 char_u word[MAXWLEN];
5549
5550 if (ga_grow(gap, 1) == OK)
5551 {
5552 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5553 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005554 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005555 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005556 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005557 ++gap->ga_len;
5558 }
5559}
5560
5561/*
5562 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5563 */
5564 static int
5565sal_to_bool(s)
5566 char_u *s;
5567{
5568 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5569}
5570
5571/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005572 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5573 * When "s" is NULL FALSE is returned.
5574 */
5575 static int
5576has_non_ascii(s)
5577 char_u *s;
5578{
5579 char_u *p;
5580
5581 if (s != NULL)
5582 for (p = s; *p != NUL; ++p)
5583 if (*p >= 128)
5584 return TRUE;
5585 return FALSE;
5586}
5587
5588/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005589 * Free the structure filled by spell_read_aff().
5590 */
5591 static void
5592spell_free_aff(aff)
5593 afffile_T *aff;
5594{
5595 hashtab_T *ht;
5596 hashitem_T *hi;
5597 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005598 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005599 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005600
5601 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005602
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005603 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005604 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5605 {
5606 todo = ht->ht_used;
5607 for (hi = ht->ht_array; todo > 0; ++hi)
5608 {
5609 if (!HASHITEM_EMPTY(hi))
5610 {
5611 --todo;
5612 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005613 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5614 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005615 }
5616 }
5617 if (ht == &aff->af_suff)
5618 break;
5619 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005620
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005621 hash_clear(&aff->af_pref);
5622 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005623 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005624}
5625
5626/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005627 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005628 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005629 */
5630 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005631spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005632 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005633 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005634 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005635{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005636 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005637 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005638 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005639 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005640 char_u store_afflist[MAXWLEN];
5641 int pfxlen;
5642 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005643 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005644 char_u *pc;
5645 char_u *w;
5646 int l;
5647 hash_T hash;
5648 hashitem_T *hi;
5649 FILE *fd;
5650 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005651 int non_ascii = 0;
5652 int retval = OK;
5653 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005654 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005655 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005656
Bram Moolenaar51485f02005-06-04 21:55:20 +00005657 /*
5658 * Open the file.
5659 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005660 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005661 if (fd == NULL)
5662 {
5663 EMSG2(_(e_notopen), fname);
5664 return FAIL;
5665 }
5666
Bram Moolenaar51485f02005-06-04 21:55:20 +00005667 /* The hashtable is only used to detect duplicated words. */
5668 hash_init(&ht);
5669
Bram Moolenaarb765d632005-06-07 21:00:02 +00005670 if (spin->si_verbose || p_verbose > 2)
5671 {
5672 if (!spin->si_verbose)
5673 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005674 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005675 out_flush();
5676 if (!spin->si_verbose)
5677 verbose_leave();
5678 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005679
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005680 /* start with a message for the first line */
5681 spin->si_msg_count = 999999;
5682
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005683 /* Read and ignore the first line: word count. */
5684 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005685 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005686 EMSG2(_("E760: No word count in %s"), fname);
5687
5688 /*
5689 * Read all the lines in the file one by one.
5690 * The words are converted to 'encoding' here, before being added to
5691 * the hashtable.
5692 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005693 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005694 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005695 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005697 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005698 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005699
Bram Moolenaar51485f02005-06-04 21:55:20 +00005700 /* Remove CR, LF and white space from the end. White space halfway
5701 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005702 l = STRLEN(line);
5703 while (l > 0 && line[l - 1] <= ' ')
5704 --l;
5705 if (l == 0)
5706 continue; /* empty line */
5707 line[l] = NUL;
5708
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005709 /* Find the optional affix names. Replace the SLASH character by a
5710 * slash. */
5711 afflist = NULL;
5712 for (p = line; *p != NUL; mb_ptr_adv(p))
5713 {
5714 if (*p == affile->af_slash)
5715 *p = '/';
5716 else if (*p == '/')
5717 {
5718 *p = NUL;
5719 afflist = p + 1;
5720 break;
5721 }
5722 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005723
5724 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5725 if (spin->si_ascii && has_non_ascii(line))
5726 {
5727 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005728 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005729 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005730
Bram Moolenaarb765d632005-06-07 21:00:02 +00005731#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005732 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005733 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005734 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005736 if (pc == NULL)
5737 {
5738 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5739 fname, lnum, line);
5740 continue;
5741 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005742 w = pc;
5743 }
5744 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005745#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005746 {
5747 pc = NULL;
5748 w = line;
5749 }
5750
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005751 /* This takes time, print a message every 10000 words. */
5752 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005753 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005754 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005755 vim_snprintf((char *)message, sizeof(message),
5756 _("line %6d, word %6d - %s"),
5757 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5758 msg_start();
5759 msg_puts_long_attr(message, 0);
5760 msg_clr_eos();
5761 msg_didout = FALSE;
5762 msg_col = 0;
5763 out_flush();
5764 }
5765
Bram Moolenaar51485f02005-06-04 21:55:20 +00005766 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005767 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005768 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005769 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005770 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005771 if (retval == FAIL)
5772 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005773
Bram Moolenaar51485f02005-06-04 21:55:20 +00005774 hash = hash_hash(dw);
5775 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005776 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005777 {
5778 if (p_verbose > 0)
5779 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005780 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005781 else if (duplicate == 0)
5782 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5783 fname, lnum, dw);
5784 ++duplicate;
5785 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005786 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005787 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005788
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005789 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005790 store_afflist[0] = NUL;
5791 pfxlen = 0;
5792 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005793 if (afflist != NULL)
5794 {
5795 /* Check for affix name that stands for keep-case word and stands
5796 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005797 if (affile->af_kep != 0 && flag_in_afflist(
5798 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005799 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005800 if (affile->af_rar != 0 && flag_in_afflist(
5801 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005802 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005803 if (affile->af_bad != 0 && flag_in_afflist(
5804 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005805 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005806 if (affile->af_needaffix != 0 && flag_in_afflist(
5807 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005808 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005809 if (affile->af_needcomp != 0 && flag_in_afflist(
5810 affile->af_flagtype, afflist, affile->af_needcomp))
5811 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005812
5813 if (affile->af_pfxpostpone)
5814 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005815 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005816
Bram Moolenaar5195e452005-08-19 20:32:47 +00005817 if (spin->si_compflags != NULL)
5818 /* Need to store the list of compound flags with the word.
5819 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005820 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005821 }
5822
Bram Moolenaar51485f02005-06-04 21:55:20 +00005823 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005824 if (store_word(spin, dw, flags, spin->si_region,
5825 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005826 retval = FAIL;
5827
5828 if (afflist != NULL)
5829 {
5830 /* Find all matching suffixes and add the resulting words.
5831 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005832 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005833 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005834 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005835 retval = FAIL;
5836
5837 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005838 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005839 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005840 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005841 retval = FAIL;
5842 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005843 }
5844
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005845 if (duplicate > 0)
5846 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005847 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005848 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5849 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005850 hash_clear(&ht);
5851
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005852 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005853 return retval;
5854}
5855
5856/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005857 * Get the list of prefix IDs from the affix list "afflist".
5858 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005859 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5860 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005861 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005862 static int
5863get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005864 afffile_T *affile;
5865 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005866 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005867{
5868 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005870 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005871 int id;
5872 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005873 hashitem_T *hi;
5874
Bram Moolenaar6de68532005-08-24 22:08:48 +00005875 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005876 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005877 prevp = p;
5878 if (get_affitem(affile->af_flagtype, &p) != 0)
5879 {
5880 /* A flag is a postponed prefix flag if it appears in "af_pref"
5881 * and it's ID is not zero. */
5882 vim_strncpy(key, prevp, p - prevp);
5883 hi = hash_find(&affile->af_pref, key);
5884 if (!HASHITEM_EMPTY(hi))
5885 {
5886 id = HI2AH(hi)->ah_newID;
5887 if (id != 0)
5888 store_afflist[cnt++] = id;
5889 }
5890 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005891 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005892 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005893 }
5894
Bram Moolenaar5195e452005-08-19 20:32:47 +00005895 store_afflist[cnt] = NUL;
5896 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005897}
5898
5899/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005900 * Get the list of compound IDs from the affix list "afflist" that are used
5901 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005902 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005903 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005904 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005905get_compflags(affile, afflist, store_afflist)
5906 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005907 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005908 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005909{
5910 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005911 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005912 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005913 char_u key[AH_KEY_LEN];
5914 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005915
Bram Moolenaar6de68532005-08-24 22:08:48 +00005916 for (p = afflist; *p != NUL; )
5917 {
5918 prevp = p;
5919 if (get_affitem(affile->af_flagtype, &p) != 0)
5920 {
5921 /* A flag is a compound flag if it appears in "af_comp". */
5922 vim_strncpy(key, prevp, p - prevp);
5923 hi = hash_find(&affile->af_comp, key);
5924 if (!HASHITEM_EMPTY(hi))
5925 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5926 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005927 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005928 ++p;
5929 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005930
Bram Moolenaar5195e452005-08-19 20:32:47 +00005931 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005932}
5933
5934/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005935 * Apply affixes to a word and store the resulting words.
5936 * "ht" is the hashtable with affentry_T that need to be applied, either
5937 * prefixes or suffixes.
5938 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5939 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005940 *
5941 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005942 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005943 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005944store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5945 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005946 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005947 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005948 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005949 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005950 hashtab_T *ht;
5951 hashtab_T *xht;
5952 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005953 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005954 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005955 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5956 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005957{
5958 int todo;
5959 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005960 affheader_T *ah;
5961 affentry_T *ae;
5962 regmatch_T regmatch;
5963 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005964 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005965 int i;
5966 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005967 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005968 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005969 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005970 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005971
Bram Moolenaar51485f02005-06-04 21:55:20 +00005972 todo = ht->ht_used;
5973 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005974 {
5975 if (!HASHITEM_EMPTY(hi))
5976 {
5977 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005978 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005979
Bram Moolenaar51485f02005-06-04 21:55:20 +00005980 /* Check that the affix combines, if required, and that the word
5981 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005982 if ((!comb || ah->ah_combine) && flag_in_afflist(
5983 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005984 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005985 /* Loop over all affix entries with this name. */
5986 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005987 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005988 /* Check the condition. It's not logical to match case
5989 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005990 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005991 * Another requirement from Myspell is that the chop
5992 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005993 * For prefixes, when "PFXPOSTPONE" was used, only do
5994 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005995 regmatch.regprog = ae->ae_prog;
5996 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005997 if ((xht != NULL || !affile->af_pfxpostpone
5998 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005999 && (ae->ae_chop == NULL
6000 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006001 && (ae->ae_prog == NULL
6002 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006003 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006004 /* Match. Remove the chop and add the affix. */
6005 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006006 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006007 /* prefix: chop/add at the start of the word */
6008 if (ae->ae_add == NULL)
6009 *newword = NUL;
6010 else
6011 STRCPY(newword, ae->ae_add);
6012 p = word;
6013 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006014 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006015 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006016#ifdef FEAT_MBYTE
6017 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006018 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006019 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006020 for ( ; i > 0; --i)
6021 mb_ptr_adv(p);
6022 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006023 else
6024#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006025 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006026 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006027 STRCAT(newword, p);
6028 }
6029 else
6030 {
6031 /* suffix: chop/add at the end of the word */
6032 STRCPY(newword, word);
6033 if (ae->ae_chop != NULL)
6034 {
6035 /* Remove chop string. */
6036 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006037 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006038 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006039 mb_ptr_back(newword, p);
6040 *p = NUL;
6041 }
6042 if (ae->ae_add != NULL)
6043 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006044 }
6045
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006046 /* Obey the "rare" flag of the affix. */
6047 if (ae->ae_rare)
6048 use_flags = flags | WF_RARE;
6049 else
6050 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006051
6052 /* Obey the "nocomp" flag of the affix: don't use the
6053 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006054 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006055 if (ae->ae_nocomp && pfxlist != NULL)
6056 {
6057 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6058 use_pfxlist = pfx_pfxlist;
6059 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006060
6061 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006062 if (spin->si_prefroot != NULL
6063 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006064 {
6065 /* ... add a flag to indicate an affix was used. */
6066 use_flags |= WF_HAS_AFF;
6067
6068 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006069 * affixes is not allowed. But do use the
6070 * compound flags after them. */
6071 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6072 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006073 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006074
Bram Moolenaar51485f02005-06-04 21:55:20 +00006075 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006076 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006077 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006078 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006079
Bram Moolenaar51485f02005-06-04 21:55:20 +00006080 /* When added a suffix and combining is allowed also
6081 * try adding prefixes additionally. */
6082 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006083 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006084 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006085 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006086 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006087 }
6088 }
6089 }
6090 }
6091 }
6092
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006093 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006094}
6095
6096/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006097 * Read a file with a list of words.
6098 */
6099 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006100spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006101 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006102 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006103{
6104 FILE *fd;
6105 long lnum = 0;
6106 char_u rline[MAXLINELEN];
6107 char_u *line;
6108 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006109 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006110 int l;
6111 int retval = OK;
6112 int did_word = FALSE;
6113 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006114 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006115 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006116
6117 /*
6118 * Open the file.
6119 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006120 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006121 if (fd == NULL)
6122 {
6123 EMSG2(_(e_notopen), fname);
6124 return FAIL;
6125 }
6126
Bram Moolenaarb765d632005-06-07 21:00:02 +00006127 if (spin->si_verbose || p_verbose > 2)
6128 {
6129 if (!spin->si_verbose)
6130 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006131 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006132 out_flush();
6133 if (!spin->si_verbose)
6134 verbose_leave();
6135 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006136
6137 /*
6138 * Read all the lines in the file one by one.
6139 */
6140 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6141 {
6142 line_breakcheck();
6143 ++lnum;
6144
6145 /* Skip comment lines. */
6146 if (*rline == '#')
6147 continue;
6148
6149 /* Remove CR, LF and white space from the end. */
6150 l = STRLEN(rline);
6151 while (l > 0 && rline[l - 1] <= ' ')
6152 --l;
6153 if (l == 0)
6154 continue; /* empty or blank line */
6155 rline[l] = NUL;
6156
6157 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6158 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006159#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006160 if (spin->si_conv.vc_type != CONV_NONE)
6161 {
6162 pc = string_convert(&spin->si_conv, rline, NULL);
6163 if (pc == NULL)
6164 {
6165 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6166 fname, lnum, rline);
6167 continue;
6168 }
6169 line = pc;
6170 }
6171 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006172#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006173 {
6174 pc = NULL;
6175 line = rline;
6176 }
6177
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006178 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006179 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006180 ++line;
6181 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006182 {
6183 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006184 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6185 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006186 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006187 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6188 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006189 else
6190 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006191#ifdef FEAT_MBYTE
6192 char_u *enc;
6193
Bram Moolenaar51485f02005-06-04 21:55:20 +00006194 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006195 line += 10;
6196 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006197 if (enc != NULL && !spin->si_ascii
6198 && convert_setup(&spin->si_conv, enc,
6199 p_enc) == FAIL)
6200 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006201 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006202 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006203 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006204#else
6205 smsg((char_u *)_("Conversion in %s not supported"), fname);
6206#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006207 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006208 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006209 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006210
Bram Moolenaar3982c542005-06-08 21:56:31 +00006211 if (STRNCMP(line, "regions=", 8) == 0)
6212 {
6213 if (spin->si_region_count > 1)
6214 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6215 fname, lnum, line);
6216 else
6217 {
6218 line += 8;
6219 if (STRLEN(line) > 16)
6220 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6221 fname, lnum, line);
6222 else
6223 {
6224 spin->si_region_count = STRLEN(line) / 2;
6225 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006226
6227 /* Adjust the mask for a word valid in all regions. */
6228 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006229 }
6230 }
6231 continue;
6232 }
6233
Bram Moolenaar7887d882005-07-01 22:33:52 +00006234 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6235 fname, lnum, line - 1);
6236 continue;
6237 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006238
Bram Moolenaar7887d882005-07-01 22:33:52 +00006239 flags = 0;
6240 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006241
Bram Moolenaar7887d882005-07-01 22:33:52 +00006242 /* Check for flags and region after a slash. */
6243 p = vim_strchr(line, '/');
6244 if (p != NULL)
6245 {
6246 *p++ = NUL;
6247 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006248 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006249 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006250 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006251 else if (*p == '!') /* Bad, bad, wicked word. */
6252 flags |= WF_BANNED;
6253 else if (*p == '?') /* Rare word. */
6254 flags |= WF_RARE;
6255 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006256 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006257 if ((flags & WF_REGION) == 0) /* first one */
6258 regionmask = 0;
6259 flags |= WF_REGION;
6260
6261 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006262 if (l > spin->si_region_count)
6263 {
6264 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006265 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006266 break;
6267 }
6268 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006269 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006270 else
6271 {
6272 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6273 fname, lnum, p);
6274 break;
6275 }
6276 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006277 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006278 }
6279
6280 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6281 if (spin->si_ascii && has_non_ascii(line))
6282 {
6283 ++non_ascii;
6284 continue;
6285 }
6286
6287 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006288 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006289 {
6290 retval = FAIL;
6291 break;
6292 }
6293 did_word = TRUE;
6294 }
6295
6296 vim_free(pc);
6297 fclose(fd);
6298
Bram Moolenaarb765d632005-06-07 21:00:02 +00006299 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6300 {
6301 if (p_verbose > 2)
6302 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006303 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6304 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006305 if (p_verbose > 2)
6306 verbose_leave();
6307 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006308 return retval;
6309}
6310
6311/*
6312 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006313 * This avoids calling free() for every little struct we use (and keeping
6314 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006315 * The memory is cleared to all zeros.
6316 * Returns NULL when out of memory.
6317 */
6318 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006319getroom(spin, len, align)
6320 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006321 size_t len; /* length needed */
6322 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006323{
6324 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006325 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006326
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006327 if (align && bl != NULL)
6328 /* Round size up for alignment. On some systems structures need to be
6329 * aligned to the size of a pointer (e.g., SPARC). */
6330 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6331 & ~(sizeof(char *) - 1);
6332
Bram Moolenaar51485f02005-06-04 21:55:20 +00006333 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6334 {
6335 /* Allocate a block of memory. This is not freed until much later. */
6336 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6337 if (bl == NULL)
6338 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006339 bl->sb_next = spin->si_blocks;
6340 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006341 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006342 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006343 }
6344
6345 p = bl->sb_data + bl->sb_used;
6346 bl->sb_used += len;
6347
6348 return p;
6349}
6350
6351/*
6352 * Make a copy of a string into memory allocated with getroom().
6353 */
6354 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006355getroom_save(spin, s)
6356 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006357 char_u *s;
6358{
6359 char_u *sc;
6360
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006361 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006362 if (sc != NULL)
6363 STRCPY(sc, s);
6364 return sc;
6365}
6366
6367
6368/*
6369 * Free the list of allocated sblock_T.
6370 */
6371 static void
6372free_blocks(bl)
6373 sblock_T *bl;
6374{
6375 sblock_T *next;
6376
6377 while (bl != NULL)
6378 {
6379 next = bl->sb_next;
6380 vim_free(bl);
6381 bl = next;
6382 }
6383}
6384
6385/*
6386 * Allocate the root of a word tree.
6387 */
6388 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006389wordtree_alloc(spin)
6390 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006391{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006392 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006393}
6394
6395/*
6396 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006397 * Always store it in the case-folded tree. For a keep-case word this is
6398 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6399 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006400 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006401 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6402 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006403 */
6404 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006405store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006406 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006407 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006408 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006409 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006410 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006411 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006412{
6413 int len = STRLEN(word);
6414 int ct = captype(word, word + len);
6415 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006416 int res = OK;
6417 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006419 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006420 for (p = pfxlist; res == OK; ++p)
6421 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006422 if (!need_affix || (p != NULL && *p != NUL))
6423 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006424 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006425 if (p == NULL || *p == NUL)
6426 break;
6427 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006428 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006429
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006430 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006431 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006432 for (p = pfxlist; res == OK; ++p)
6433 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006434 if (!need_affix || (p != NULL && *p != NUL))
6435 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006436 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006437 if (p == NULL || *p == NUL)
6438 break;
6439 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006440 ++spin->si_keepwcount;
6441 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006442 return res;
6443}
6444
6445/*
6446 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006447 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6448 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006449 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006450 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006451 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006452tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006453 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006454 char_u *word;
6455 wordnode_T *root;
6456 int flags;
6457 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006458 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006460 wordnode_T *node = root;
6461 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006462 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006463 wordnode_T **prev = NULL;
6464 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006465
Bram Moolenaar51485f02005-06-04 21:55:20 +00006466 /* Add each byte of the word to the tree, including the NUL at the end. */
6467 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006468 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006469 /* When there is more than one reference to this node we need to make
6470 * a copy, so that we can modify it. Copy the whole list of siblings
6471 * (we don't optimize for a partly shared list of siblings). */
6472 if (node != NULL && node->wn_refs > 1)
6473 {
6474 --node->wn_refs;
6475 copyprev = prev;
6476 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6477 {
6478 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006479 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006480 if (np == NULL)
6481 return FAIL;
6482 np->wn_child = copyp->wn_child;
6483 if (np->wn_child != NULL)
6484 ++np->wn_child->wn_refs; /* child gets extra ref */
6485 np->wn_byte = copyp->wn_byte;
6486 if (np->wn_byte == NUL)
6487 {
6488 np->wn_flags = copyp->wn_flags;
6489 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006490 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006491 }
6492
6493 /* Link the new node in the list, there will be one ref. */
6494 np->wn_refs = 1;
6495 *copyprev = np;
6496 copyprev = &np->wn_sibling;
6497
6498 /* Let "node" point to the head of the copied list. */
6499 if (copyp == node)
6500 node = np;
6501 }
6502 }
6503
Bram Moolenaar51485f02005-06-04 21:55:20 +00006504 /* Look for the sibling that has the same character. They are sorted
6505 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006506 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006507 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006508 while (node != NULL
6509 && (node->wn_byte < word[i]
6510 || (node->wn_byte == NUL
6511 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006512 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006513 : node->wn_flags < (flags & WN_MASK)
6514 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006515 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006517 prev = &node->wn_sibling;
6518 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006519 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006520 if (node == NULL
6521 || node->wn_byte != word[i]
6522 || (word[i] == NUL
6523 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006524 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006525 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006526 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006527 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006528 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006529 if (np == NULL)
6530 return FAIL;
6531 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006532
6533 /* If "node" is NULL this is a new child or the end of the sibling
6534 * list: ref count is one. Otherwise use ref count of sibling and
6535 * make ref count of sibling one (matters when inserting in front
6536 * of the list of siblings). */
6537 if (node == NULL)
6538 np->wn_refs = 1;
6539 else
6540 {
6541 np->wn_refs = node->wn_refs;
6542 node->wn_refs = 1;
6543 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006544 *prev = np;
6545 np->wn_sibling = node;
6546 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006547 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006548
Bram Moolenaar51485f02005-06-04 21:55:20 +00006549 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006550 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006551 node->wn_flags = flags;
6552 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006553 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006555 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006556 prev = &node->wn_child;
6557 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006558 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006559#ifdef SPELL_PRINTTREE
6560 smsg("Added \"%s\"", word);
6561 spell_print_tree(root->wn_sibling);
6562#endif
6563
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006564 /* count nr of words added since last message */
6565 ++spin->si_msg_count;
6566
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006567 if (spin->si_compress_cnt > 1)
6568 {
6569 if (--spin->si_compress_cnt == 1)
6570 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006571 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006572 }
6573
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006574 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006575 * When we have allocated lots of memory we need to compress the word tree
6576 * to free up some room. But compression is slow, and we might actually
6577 * need that room, thus only compress in the following situations:
6578 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006579 * "compress_start" blocks.
6580 * 2. When compressed before and used "compress_inc" blocks before
6581 * adding "compress_added" words (si_compress_cnt > 1).
6582 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006583 * (si_compress_cnt == 1) and the number of free nodes drops below the
6584 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006585 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006586#ifndef SPELL_PRINTTREE
6587 if (spin->si_compress_cnt == 1
6588 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006589 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006590#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006591 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006592 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006593 * when the freed up room has been used and another "compress_inc"
6594 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006595 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006596 spin->si_blocks_cnt -= compress_inc;
6597 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006598
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006599 if (spin->si_verbose)
6600 {
6601 msg_start();
6602 msg_puts((char_u *)_(msg_compressing));
6603 msg_clr_eos();
6604 msg_didout = FALSE;
6605 msg_col = 0;
6606 out_flush();
6607 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006608
6609 /* Compress both trees. Either they both have many nodes, which makes
6610 * compression useful, or one of them is small, which means
6611 * compression goes fast. */
6612 wordtree_compress(spin, spin->si_foldroot);
6613 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006614 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006615
6616 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006617}
6618
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006619/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006620 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6621 * Sets "sps_flags".
6622 */
6623 int
6624spell_check_msm()
6625{
6626 char_u *p = p_msm;
6627 long start = 0;
6628 long inc = 0;
6629 long added = 0;
6630
6631 if (!VIM_ISDIGIT(*p))
6632 return FAIL;
6633 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6634 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6635 if (*p != ',')
6636 return FAIL;
6637 ++p;
6638 if (!VIM_ISDIGIT(*p))
6639 return FAIL;
6640 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6641 if (*p != ',')
6642 return FAIL;
6643 ++p;
6644 if (!VIM_ISDIGIT(*p))
6645 return FAIL;
6646 added = getdigits(&p) * 1024;
6647 if (*p != NUL)
6648 return FAIL;
6649
6650 if (start == 0 || inc == 0 || added == 0 || inc > start)
6651 return FAIL;
6652
6653 compress_start = start;
6654 compress_inc = inc;
6655 compress_added = added;
6656 return OK;
6657}
6658
6659
6660/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006661 * Get a wordnode_T, either from the list of previously freed nodes or
6662 * allocate a new one.
6663 */
6664 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006665get_wordnode(spin)
6666 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006667{
6668 wordnode_T *n;
6669
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006670 if (spin->si_first_free == NULL)
6671 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006672 else
6673 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006674 n = spin->si_first_free;
6675 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006676 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006677 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006678 }
6679#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006680 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006681#endif
6682 return n;
6683}
6684
6685/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006686 * Decrement the reference count on a node (which is the head of a list of
6687 * siblings). If the reference count becomes zero free the node and its
6688 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006689 */
6690 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006691deref_wordnode(spin, node)
6692 spellinfo_T *spin;
6693 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006694{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695 wordnode_T *np;
6696
6697 if (--node->wn_refs == 0)
6698 for (np = node; np != NULL; np = np->wn_sibling)
6699 {
6700 if (np->wn_child != NULL)
6701 deref_wordnode(spin, np->wn_child);
6702 free_wordnode(spin, np);
6703 }
6704}
6705
6706/*
6707 * Free a wordnode_T for re-use later.
6708 * Only the "wn_child" field becomes invalid.
6709 */
6710 static void
6711free_wordnode(spin, n)
6712 spellinfo_T *spin;
6713 wordnode_T *n;
6714{
6715 n->wn_child = spin->si_first_free;
6716 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006717 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006718}
6719
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006720/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006721 * Compress a tree: find tails that are identical and can be shared.
6722 */
6723 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006724wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006725 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006726 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006727{
6728 hashtab_T ht;
6729 int n;
6730 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006731 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006732
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006733 /* Skip the root itself, it's not actually used. The first sibling is the
6734 * start of the tree. */
6735 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006736 {
6737 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006739
6740#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006741 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006742#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006743 {
6744 if (!spin->si_verbose)
6745 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006746 if (tot > 1000000)
6747 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006748 else if (tot == 0)
6749 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006750 else
6751 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006752 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006753 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006754 if (p_verbose > 2)
6755 verbose_leave();
6756 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006757#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006758 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006759#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006760 hash_clear(&ht);
6761 }
6762}
6763
6764/*
6765 * Compress a node, its siblings and its children, depth first.
6766 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006767 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006768 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006769node_compress(spin, node, ht, tot)
6770 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006771 wordnode_T *node;
6772 hashtab_T *ht;
6773 int *tot; /* total count of nodes before compressing,
6774 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006775{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006776 wordnode_T *np;
6777 wordnode_T *tp;
6778 wordnode_T *child;
6779 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006780 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006781 int len = 0;
6782 unsigned nr, n;
6783 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006784
Bram Moolenaar51485f02005-06-04 21:55:20 +00006785 /*
6786 * Go through the list of siblings. Compress each child and then try
6787 * finding an identical child to replace it.
6788 * Note that with "child" we mean not just the node that is pointed to,
6789 * but the whole list of siblings, of which the node is the first.
6790 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006791 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006792 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006793 ++len;
6794 if ((child = np->wn_child) != NULL)
6795 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006796 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006797 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006798
6799 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006800 hash = hash_hash(child->wn_u1.hashkey);
6801 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006802 tp = NULL;
6803 if (!HASHITEM_EMPTY(hi))
6804 {
6805 /* There are children with an identical hash value. Now check
6806 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006807 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006808 if (node_equal(child, tp))
6809 {
6810 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006811 * current one. This means the current child and all
6812 * its siblings is unlinked from the tree. */
6813 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006814 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006815 np->wn_child = tp;
6816 ++compressed;
6817 break;
6818 }
6819 if (tp == NULL)
6820 {
6821 /* No other child with this hash value equals the child of
6822 * the node, add it to the linked list after the first
6823 * item. */
6824 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006825 child->wn_u2.next = tp->wn_u2.next;
6826 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006827 }
6828 }
6829 else
6830 /* No other child has this hash value, add it to the
6831 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006832 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006833 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006834 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006835 *tot += len;
6836
6837 /*
6838 * Make a hash key for the node and its siblings, so that we can quickly
6839 * find a lookalike node. This must be done after compressing the sibling
6840 * list, otherwise the hash key would become invalid by the compression.
6841 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006842 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006843 nr = 0;
6844 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006845 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006846 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006847 /* end node: use wn_flags, wn_region and wn_affixID */
6848 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006849 else
6850 /* byte node: use the byte value and the child pointer */
6851 n = np->wn_byte + ((long_u)np->wn_child << 8);
6852 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006853 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006854
6855 /* Avoid NUL bytes, it terminates the hash key. */
6856 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006857 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006858 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006859 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006860 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006861 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006862 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006863 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6864 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006865
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006866 /* Check for CTRL-C pressed now and then. */
6867 fast_breakcheck();
6868
Bram Moolenaar51485f02005-06-04 21:55:20 +00006869 return compressed;
6870}
6871
6872/*
6873 * Return TRUE when two nodes have identical siblings and children.
6874 */
6875 static int
6876node_equal(n1, n2)
6877 wordnode_T *n1;
6878 wordnode_T *n2;
6879{
6880 wordnode_T *p1;
6881 wordnode_T *p2;
6882
6883 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6884 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6885 if (p1->wn_byte != p2->wn_byte
6886 || (p1->wn_byte == NUL
6887 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006888 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006889 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006890 : (p1->wn_child != p2->wn_child)))
6891 break;
6892
6893 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006894}
6895
6896/*
6897 * Write a number to file "fd", MSB first, in "len" bytes.
6898 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006899 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006900put_bytes(fd, nr, len)
6901 FILE *fd;
6902 long_u nr;
6903 int len;
6904{
6905 int i;
6906
6907 for (i = len - 1; i >= 0; --i)
6908 putc((int)(nr >> (i * 8)), fd);
6909}
6910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006911static int
6912#ifdef __BORLANDC__
6913_RTLENTRYF
6914#endif
6915rep_compare __ARGS((const void *s1, const void *s2));
6916
6917/*
6918 * Function given to qsort() to sort the REP items on "from" string.
6919 */
6920 static int
6921#ifdef __BORLANDC__
6922_RTLENTRYF
6923#endif
6924rep_compare(s1, s2)
6925 const void *s1;
6926 const void *s2;
6927{
6928 fromto_T *p1 = (fromto_T *)s1;
6929 fromto_T *p2 = (fromto_T *)s2;
6930
6931 return STRCMP(p1->ft_from, p2->ft_from);
6932}
6933
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006934/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006935 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006936 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006937 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006938 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006939write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006940 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006941 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006942{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006943 FILE *fd;
6944 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006945 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006946 wordnode_T *tree;
6947 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006948 int i;
6949 int l;
6950 garray_T *gap;
6951 fromto_T *ftp;
6952 char_u *p;
6953 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006954 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006955
Bram Moolenaarb765d632005-06-07 21:00:02 +00006956 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006957 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006958 {
6959 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006960 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006961 }
6962
Bram Moolenaar5195e452005-08-19 20:32:47 +00006963 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006964 /* <fileID> */
6965 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006966 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006967 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006968 retval = FAIL;
6969 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006970 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006971
Bram Moolenaar5195e452005-08-19 20:32:47 +00006972 /*
6973 * <SECTIONS>: <section> ... <sectionend>
6974 */
6975
6976 /* SN_REGION: <regionname> ...
6977 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006978 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006979 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006980 putc(SN_REGION, fd); /* <sectionID> */
6981 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6982 l = spin->si_region_count * 2;
6983 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6984 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6985 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006986 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006987 }
6988 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006989 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006990
Bram Moolenaar5195e452005-08-19 20:32:47 +00006991 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6992 *
6993 * The table with character flags and the table for case folding.
6994 * This makes sure the same characters are recognized as word characters
6995 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006996 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006997 * 'encoding'.
6998 * Also skip this for an .add.spl file, the main spell file must contain
6999 * the table (avoids that it conflicts). File is shorter too.
7000 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007001 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007002 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007003 char_u folchars[128 * 8];
7004 int flags;
7005
Bram Moolenaard12a1322005-08-21 22:08:24 +00007006 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007007 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7008
7009 /* Form the <folchars> string first, we need to know its length. */
7010 l = 0;
7011 for (i = 128; i < 256; ++i)
7012 {
7013#ifdef FEAT_MBYTE
7014 if (has_mbyte)
7015 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7016 else
7017#endif
7018 folchars[l++] = spelltab.st_fold[i];
7019 }
7020 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7021
7022 fputc(128, fd); /* <charflagslen> */
7023 for (i = 128; i < 256; ++i)
7024 {
7025 flags = 0;
7026 if (spelltab.st_isw[i])
7027 flags |= CF_WORD;
7028 if (spelltab.st_isu[i])
7029 flags |= CF_UPPER;
7030 fputc(flags, fd); /* <charflags> */
7031 }
7032
7033 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7034 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007035 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007036
Bram Moolenaar5195e452005-08-19 20:32:47 +00007037 /* SN_MIDWORD: <midword> */
7038 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007039 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007040 putc(SN_MIDWORD, fd); /* <sectionID> */
7041 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7042
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007043 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007044 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007045 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7046 }
7047
Bram Moolenaar5195e452005-08-19 20:32:47 +00007048 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7049 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007050 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007051 putc(SN_PREFCOND, fd); /* <sectionID> */
7052 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7053
7054 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7055 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7056
7057 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007058 }
7059
Bram Moolenaar5195e452005-08-19 20:32:47 +00007060 /* SN_REP: <repcount> <rep> ...
7061 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007063 /* Sort the REP items. */
7064 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
7065 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007066
Bram Moolenaar5195e452005-08-19 20:32:47 +00007067 /* round 1: SN_REP section
7068 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007069 for (round = 1; round <= 2; ++round)
7070 {
7071 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007073 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007074 putc(SN_REP, fd); /* <sectionID> */
7075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007076 else
7077 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007078 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7079 /* using SN_SOFO section instead of SN_SAL */
7080 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007081 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007082 putc(SN_SAL, fd); /* <sectionID> */
7083 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007084
Bram Moolenaar5195e452005-08-19 20:32:47 +00007085 /* This is for making suggestions, section is not required. */
7086 putc(0, fd); /* <sectionflags> */
7087
7088 /* Compute the length of what follows. */
7089 l = 2; /* count <repcount> or <salcount> */
7090 for (i = 0; i < gap->ga_len; ++i)
7091 {
7092 ftp = &((fromto_T *)gap->ga_data)[i];
7093 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7094 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7095 }
7096 if (round == 2)
7097 ++l; /* count <salflags> */
7098 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7099
7100 if (round == 2)
7101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007102 i = 0;
7103 if (spin->si_followup)
7104 i |= SAL_F0LLOWUP;
7105 if (spin->si_collapse)
7106 i |= SAL_COLLAPSE;
7107 if (spin->si_rem_accents)
7108 i |= SAL_REM_ACCENTS;
7109 putc(i, fd); /* <salflags> */
7110 }
7111
7112 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7113 for (i = 0; i < gap->ga_len; ++i)
7114 {
7115 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7116 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7117 ftp = &((fromto_T *)gap->ga_data)[i];
7118 for (rr = 1; rr <= 2; ++rr)
7119 {
7120 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7121 l = STRLEN(p);
7122 putc(l, fd);
7123 fwrite(p, l, (size_t)1, fd);
7124 }
7125 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007127 }
7128
Bram Moolenaar5195e452005-08-19 20:32:47 +00007129 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7130 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007131 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7132 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007133 putc(SN_SOFO, fd); /* <sectionID> */
7134 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007135
7136 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007137 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7138 /* <sectionlen> */
7139
7140 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7141 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007142
7143 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007144 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7145 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007146 }
7147
Bram Moolenaar5195e452005-08-19 20:32:47 +00007148 /* SN_MAP: <mapstr>
7149 * This is for making suggestions, section is not required. */
7150 if (spin->si_map.ga_len > 0)
7151 {
7152 putc(SN_MAP, fd); /* <sectionID> */
7153 putc(0, fd); /* <sectionflags> */
7154 l = spin->si_map.ga_len;
7155 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7156 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7157 /* <mapstr> */
7158 }
7159
7160 /* SN_COMPOUND: compound info.
7161 * We don't mark it required, when not supported all compound words will
7162 * be bad words. */
7163 if (spin->si_compflags != NULL)
7164 {
7165 putc(SN_COMPOUND, fd); /* <sectionID> */
7166 putc(0, fd); /* <sectionflags> */
7167
7168 l = STRLEN(spin->si_compflags);
7169 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7170 putc(spin->si_compmax, fd); /* <compmax> */
7171 putc(spin->si_compminlen, fd); /* <compminlen> */
7172 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7173 /* <compflags> */
7174 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7175 }
7176
Bram Moolenaar78622822005-08-23 21:00:13 +00007177 /* SN_NOBREAK: NOBREAK flag */
7178 if (spin->si_nobreak)
7179 {
7180 putc(SN_NOBREAK, fd); /* <sectionID> */
7181 putc(0, fd); /* <sectionflags> */
7182
7183 /* It's empty, the precense of the section flags the feature. */
7184 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7185 }
7186
Bram Moolenaar5195e452005-08-19 20:32:47 +00007187 /* SN_SYLLABLE: syllable info.
7188 * We don't mark it required, when not supported syllables will not be
7189 * counted. */
7190 if (spin->si_syllable != NULL)
7191 {
7192 putc(SN_SYLLABLE, fd); /* <sectionID> */
7193 putc(0, fd); /* <sectionflags> */
7194
7195 l = STRLEN(spin->si_syllable);
7196 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7197 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7198 }
7199
7200 /* end of <SECTIONS> */
7201 putc(SN_END, fd); /* <sectionend> */
7202
Bram Moolenaar50cde822005-06-05 21:54:54 +00007203
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007204 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007205 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007206 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007207 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007208 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007209 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007210 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007211 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007212 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007213 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007214 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007215 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007216
Bram Moolenaar0c405862005-06-22 22:26:26 +00007217 /* Clear the index and wnode fields in the tree. */
7218 clear_node(tree);
7219
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007221 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007222 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007223 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007224
Bram Moolenaar51485f02005-06-04 21:55:20 +00007225 /* number of nodes in 4 bytes */
7226 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007227 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007228
Bram Moolenaar51485f02005-06-04 21:55:20 +00007229 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007230 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007231 }
7232
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007233 /* Write another byte to check for errors. */
7234 if (putc(0, fd) == EOF)
7235 retval = FAIL;
7236
7237 if (fclose(fd) == EOF)
7238 retval = FAIL;
7239
7240 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007241}
7242
7243/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007244 * Clear the index and wnode fields of "node", it siblings and its
7245 * children. This is needed because they are a union with other items to save
7246 * space.
7247 */
7248 static void
7249clear_node(node)
7250 wordnode_T *node;
7251{
7252 wordnode_T *np;
7253
7254 if (node != NULL)
7255 for (np = node; np != NULL; np = np->wn_sibling)
7256 {
7257 np->wn_u1.index = 0;
7258 np->wn_u2.wnode = NULL;
7259
7260 if (np->wn_byte != NUL)
7261 clear_node(np->wn_child);
7262 }
7263}
7264
7265
7266/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007268 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007269 * This first writes the list of possible bytes (siblings). Then for each
7270 * byte recursively write the children.
7271 *
7272 * NOTE: The code here must match the code in read_tree(), since assumptions
7273 * are made about the indexes (so that we don't have to write them in the
7274 * file).
7275 *
7276 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007277 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007278 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007279put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007280 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007281 wordnode_T *node;
7282 int index;
7283 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007284 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007285{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007286 int newindex = index;
7287 int siblingcount = 0;
7288 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007289 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007290
Bram Moolenaar51485f02005-06-04 21:55:20 +00007291 /* If "node" is zero the tree is empty. */
7292 if (node == NULL)
7293 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007294
Bram Moolenaar51485f02005-06-04 21:55:20 +00007295 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007296 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007297
7298 /* Count the number of siblings. */
7299 for (np = node; np != NULL; np = np->wn_sibling)
7300 ++siblingcount;
7301
7302 /* Write the sibling count. */
7303 if (fd != NULL)
7304 putc(siblingcount, fd); /* <siblingcount> */
7305
7306 /* Write each sibling byte and optionally extra info. */
7307 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007308 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007309 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007310 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007311 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007312 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007313 /* For a NUL byte (end of word) write the flags etc. */
7314 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007315 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007316 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007317 * associated condition nr (stored in wn_region). The
7318 * byte value is misused to store the "rare" and "not
7319 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007320 if (np->wn_flags == (short_u)PFX_FLAGS)
7321 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007322 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007323 {
7324 putc(BY_FLAGS, fd); /* <byte> */
7325 putc(np->wn_flags, fd); /* <pflags> */
7326 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007327 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007328 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007329 }
7330 else
7331 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007332 /* For word trees we write the flag/region items. */
7333 flags = np->wn_flags;
7334 if (regionmask != 0 && np->wn_region != regionmask)
7335 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007336 if (np->wn_affixID != 0)
7337 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007338 if (flags == 0)
7339 {
7340 /* word without flags or region */
7341 putc(BY_NOFLAGS, fd); /* <byte> */
7342 }
7343 else
7344 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007345 if (np->wn_flags >= 0x100)
7346 {
7347 putc(BY_FLAGS2, fd); /* <byte> */
7348 putc(flags, fd); /* <flags> */
7349 putc((unsigned)flags >> 8, fd); /* <flags2> */
7350 }
7351 else
7352 {
7353 putc(BY_FLAGS, fd); /* <byte> */
7354 putc(flags, fd); /* <flags> */
7355 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007356 if (flags & WF_REGION)
7357 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007358 if (flags & WF_AFX)
7359 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007360 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007361 }
7362 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007363 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007364 else
7365 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007366 if (np->wn_child->wn_u1.index != 0
7367 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368 {
7369 /* The child is written elsewhere, write the reference. */
7370 if (fd != NULL)
7371 {
7372 putc(BY_INDEX, fd); /* <byte> */
7373 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007374 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007375 }
7376 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007377 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007378 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007379 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007380
Bram Moolenaar51485f02005-06-04 21:55:20 +00007381 if (fd != NULL)
7382 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7383 {
7384 EMSG(_(e_write));
7385 return 0;
7386 }
7387 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007388 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007389
7390 /* Space used in the array when reading: one for each sibling and one for
7391 * the count. */
7392 newindex += siblingcount + 1;
7393
7394 /* Recursively dump the children of each sibling. */
7395 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007396 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7397 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007398 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007399
7400 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007401}
7402
7403
7404/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007405 * ":mkspell [-ascii] outfile infile ..."
7406 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007407 */
7408 void
7409ex_mkspell(eap)
7410 exarg_T *eap;
7411{
7412 int fcount;
7413 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007414 char_u *arg = eap->arg;
7415 int ascii = FALSE;
7416
7417 if (STRNCMP(arg, "-ascii", 6) == 0)
7418 {
7419 ascii = TRUE;
7420 arg = skipwhite(arg + 6);
7421 }
7422
7423 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7424 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007426 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007427 FreeWild(fcount, fnames);
7428 }
7429}
7430
7431/*
7432 * Create a Vim spell file from one or more word lists.
7433 * "fnames[0]" is the output file name.
7434 * "fnames[fcount - 1]" is the last input file name.
7435 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7436 * and ".spl" is appended to make the output file name.
7437 */
7438 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007439mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007440 int fcount;
7441 char_u **fnames;
7442 int ascii; /* -ascii argument given */
7443 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007444 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007445{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007446 char_u fname[MAXPATHL];
7447 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007448 char_u **innames;
7449 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007450 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007451 int i;
7452 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007453 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007454 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007455 spellinfo_T spin;
7456
7457 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007458 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007459 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007460 spin.si_followup = TRUE;
7461 spin.si_rem_accents = TRUE;
7462 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7463 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7464 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007465 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007466 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007467
Bram Moolenaarb765d632005-06-07 21:00:02 +00007468 /* default: fnames[0] is output file, following are input files */
7469 innames = &fnames[1];
7470 incount = fcount - 1;
7471
7472 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007473 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007474 len = STRLEN(fnames[0]);
7475 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7476 {
7477 /* For ":mkspell path/en.latin1.add" output file is
7478 * "path/en.latin1.add.spl". */
7479 innames = &fnames[0];
7480 incount = 1;
7481 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7482 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007483 else if (fcount == 1)
7484 {
7485 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7486 innames = &fnames[0];
7487 incount = 1;
7488 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7489 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7490 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007491 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7492 {
7493 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007494 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007495 }
7496 else
7497 /* Name should be language, make the file name from it. */
7498 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7499 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7500
7501 /* Check for .ascii.spl. */
7502 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7503 spin.si_ascii = TRUE;
7504
7505 /* Check for .add.spl. */
7506 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7507 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007508 }
7509
Bram Moolenaarb765d632005-06-07 21:00:02 +00007510 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007511 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007512 else if (vim_strchr(gettail(wfname), '_') != NULL)
7513 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007514 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007515 EMSG(_("E754: Only up to 8 regions supported"));
7516 else
7517 {
7518 /* Check for overwriting before doing things that may take a lot of
7519 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007520 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007521 {
7522 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007523 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007524 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007525 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007526 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007527 EMSG2(_(e_isadir2), wfname);
7528 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007529 }
7530
7531 /*
7532 * Init the aff and dic pointers.
7533 * Get the region names if there are more than 2 arguments.
7534 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007535 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007536 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007537 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007538
Bram Moolenaar3982c542005-06-08 21:56:31 +00007539 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007540 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007541 len = STRLEN(innames[i]);
7542 if (STRLEN(gettail(innames[i])) < 5
7543 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007544 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007545 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7546 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007547 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007548 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7549 spin.si_region_name[i * 2 + 1] =
7550 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007551 }
7552 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007553 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007554
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007555 spin.si_foldroot = wordtree_alloc(&spin);
7556 spin.si_keeproot = wordtree_alloc(&spin);
7557 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007558 if (spin.si_foldroot == NULL
7559 || spin.si_keeproot == NULL
7560 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007561 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007562 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007563 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007564 }
7565
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007566 /* When not producing a .add.spl file clear the character table when
7567 * we encounter one in the .aff file. This means we dump the current
7568 * one in the .spl file if the .aff file doesn't define one. That's
7569 * better than guessing the contents, the table will match a
7570 * previously loaded spell file. */
7571 if (!spin.si_add)
7572 spin.si_clear_chartab = TRUE;
7573
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007574 /*
7575 * Read all the .aff and .dic files.
7576 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007577 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007578 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007579 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007580 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007581 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007582 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007583
Bram Moolenaarb765d632005-06-07 21:00:02 +00007584 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007585 if (mch_stat((char *)fname, &st) >= 0)
7586 {
7587 /* Read the .aff file. Will init "spin->si_conv" based on the
7588 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007589 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007590 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007591 error = TRUE;
7592 else
7593 {
7594 /* Read the .dic file and store the words in the trees. */
7595 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007596 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007597 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007598 error = TRUE;
7599 }
7600 }
7601 else
7602 {
7603 /* No .aff file, try reading the file as a word list. Store
7604 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007605 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007606 error = TRUE;
7607 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007608
Bram Moolenaarb765d632005-06-07 21:00:02 +00007609#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007610 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007611 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007612#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007613 }
7614
Bram Moolenaar78622822005-08-23 21:00:13 +00007615 if (spin.si_compflags != NULL && spin.si_nobreak)
7616 MSG(_("Warning: both compounding and NOBREAK specified"));
7617
Bram Moolenaar51485f02005-06-04 21:55:20 +00007618 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007619 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007620 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007621 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007622 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007624 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007625 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007626 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007627 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007628 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007629 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007630 verbose_leave();
7631 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007632 wordtree_compress(&spin, spin.si_foldroot);
7633 wordtree_compress(&spin, spin.si_keeproot);
7634 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007635 }
7636
Bram Moolenaar51485f02005-06-04 21:55:20 +00007637 if (!error)
7638 {
7639 /*
7640 * Write the info in the spell file.
7641 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007642 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007643 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007644 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007645 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007646 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007647 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007648 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007649 verbose_leave();
7650 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007651
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007652 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007653
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007654 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007655 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007656 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007657 verbose_enter();
7658 MSG(_("Done!"));
7659 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007660 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007661 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007662 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007663 verbose_leave();
7664 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007665
Bram Moolenaarb765d632005-06-07 21:00:02 +00007666 /* If the file is loaded need to reload it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007667 if (!error)
7668 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007669 }
7670
7671 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007672 ga_clear(&spin.si_rep);
7673 ga_clear(&spin.si_sal);
7674 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007675 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007676
7677 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007678 for (i = 0; i < incount; ++i)
7679 if (afile[i] != NULL)
7680 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007681
7682 /* Free all the bits and pieces at once. */
7683 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007684 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007685}
7686
Bram Moolenaarb765d632005-06-07 21:00:02 +00007687
7688/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007689 * ":[count]spellgood {word}"
7690 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007691 */
7692 void
7693ex_spell(eap)
7694 exarg_T *eap;
7695{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007696 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007697 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007698}
7699
7700/*
7701 * Add "word[len]" to 'spellfile' as a good or bad word.
7702 */
7703 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007704spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007705 char_u *word;
7706 int len;
7707 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007708 int index; /* "zG" and "zW": zero, otherwise index in
7709 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007710{
7711 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007712 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007713 int new_spf = FALSE;
7714 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007715 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007716 char_u fnamebuf[MAXPATHL];
7717 char_u line[MAXWLEN * 2];
7718 long fpos, fpos_next = 0;
7719 int i;
7720 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007721
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007722 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007723 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007724 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007725 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007726 int_wordlist = vim_tempname('s');
7727 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007728 return;
7729 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007730 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007731 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007732 else
7733 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007734 /* If 'spellfile' isn't set figure out a good default value. */
7735 if (*curbuf->b_p_spf == NUL)
7736 {
7737 init_spellfile();
7738 new_spf = TRUE;
7739 }
7740
7741 if (*curbuf->b_p_spf == NUL)
7742 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007743 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00007744 return;
7745 }
7746
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007747 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7748 {
7749 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7750 if (i == index)
7751 break;
7752 if (*spf == NUL)
7753 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00007754 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007755 return;
7756 }
7757 }
7758
Bram Moolenaarb765d632005-06-07 21:00:02 +00007759 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007760 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007761 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7762 buf = NULL;
7763 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007764 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007765 EMSG(_(e_bufloaded));
7766 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007767 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007768
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007769 fname = fnamebuf;
7770 }
7771
7772 if (bad)
7773 {
7774 /* When the word also appears as good word we need to remove that one,
7775 * since its flags sort before the one with WF_BANNED. */
7776 fd = mch_fopen((char *)fname, "r");
7777 if (fd != NULL)
7778 {
7779 while (!vim_fgets(line, MAXWLEN * 2, fd))
7780 {
7781 fpos = fpos_next;
7782 fpos_next = ftell(fd);
7783 if (STRNCMP(word, line, len) == 0
7784 && (line[len] == '/' || line[len] < ' '))
7785 {
7786 /* Found duplicate word. Remove it by writing a '#' at
7787 * the start of the line. Mixing reading and writing
7788 * doesn't work for all systems, close the file first. */
7789 fclose(fd);
7790 fd = mch_fopen((char *)fname, "r+");
7791 if (fd == NULL)
7792 break;
7793 if (fseek(fd, fpos, SEEK_SET) == 0)
7794 fputc('#', fd);
7795 fseek(fd, fpos_next, SEEK_SET);
7796 }
7797 }
7798 fclose(fd);
7799 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007800 }
7801
7802 fd = mch_fopen((char *)fname, "a");
7803 if (fd == NULL && new_spf)
7804 {
7805 /* We just initialized the 'spellfile' option and can't open the file.
7806 * We may need to create the "spell" directory first. We already
7807 * checked the runtime directory is writable in init_spellfile(). */
7808 STRCPY(NameBuff, fname);
7809 *gettail_sep(NameBuff) = NUL;
7810 if (mch_stat((char *)NameBuff, &st) < 0)
7811 {
7812 /* The directory doesn't exist. Try creating it and opening the
7813 * file again. */
7814 vim_mkdir(NameBuff, 0755);
7815 fd = mch_fopen((char *)fname, "a");
7816 }
7817 }
7818
7819 if (fd == NULL)
7820 EMSG2(_(e_notopen), fname);
7821 else
7822 {
7823 if (bad)
7824 fprintf(fd, "%.*s/!\n", len, word);
7825 else
7826 fprintf(fd, "%.*s\n", len, word);
7827 fclose(fd);
7828
7829 /* Update the .add.spl file. */
7830 mkspell(1, &fname, FALSE, TRUE, TRUE);
7831
7832 /* If the .add file is edited somewhere, reload it. */
7833 if (buf != NULL)
7834 buf_reload(buf);
7835
7836 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007837 }
7838}
7839
7840/*
7841 * Initialize 'spellfile' for the current buffer.
7842 */
7843 static void
7844init_spellfile()
7845{
7846 char_u buf[MAXPATHL];
7847 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007848 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007849 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007850 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007851 int aspath = FALSE;
7852 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007853
7854 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7855 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007856 /* Find the end of the language name. Exclude the region. If there
7857 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007858 for (lend = curbuf->b_p_spl; *lend != NUL
7859 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007860 if (vim_ispathsep(*lend))
7861 {
7862 aspath = TRUE;
7863 lstart = lend + 1;
7864 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007865
7866 /* Loop over all entries in 'runtimepath'. Use the first one where we
7867 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007868 rtp = p_rtp;
7869 while (*rtp != NUL)
7870 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007871 if (aspath)
7872 /* Use directory of an entry with path, e.g., for
7873 * "/dir/lg.utf-8.spl" use "/dir". */
7874 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
7875 else
7876 /* Copy the path from 'runtimepath' to buf[]. */
7877 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00007878 if (filewritable(buf) == 2)
7879 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007880 /* Use the first language name from 'spelllang' and the
7881 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007882 if (aspath)
7883 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
7884 else
7885 {
7886 l = STRLEN(buf);
7887 vim_snprintf((char *)buf + l, MAXPATHL - l,
7888 "/spell/%.*s", (int)(lend - lstart), lstart);
7889 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007890 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007891 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
7892 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
7893 fname != NULL
7894 && strstr((char *)gettail(fname), ".ascii.") != NULL
7895 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00007896 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7897 break;
7898 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007899 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007900 }
7901 }
7902}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007903
Bram Moolenaar51485f02005-06-04 21:55:20 +00007904
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007905/*
7906 * Init the chartab used for spelling for ASCII.
7907 * EBCDIC is not supported!
7908 */
7909 static void
7910clear_spell_chartab(sp)
7911 spelltab_T *sp;
7912{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007913 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007914
7915 /* Init everything to FALSE. */
7916 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7917 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7918 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007919 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007920 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007921 sp->st_upper[i] = i;
7922 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007923
7924 /* We include digits. A word shouldn't start with a digit, but handling
7925 * that is done separately. */
7926 for (i = '0'; i <= '9'; ++i)
7927 sp->st_isw[i] = TRUE;
7928 for (i = 'A'; i <= 'Z'; ++i)
7929 {
7930 sp->st_isw[i] = TRUE;
7931 sp->st_isu[i] = TRUE;
7932 sp->st_fold[i] = i + 0x20;
7933 }
7934 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007935 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007936 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007937 sp->st_upper[i] = i - 0x20;
7938 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007939}
7940
7941/*
7942 * Init the chartab used for spelling. Only depends on 'encoding'.
7943 * Called once while starting up and when 'encoding' changes.
7944 * The default is to use isalpha(), but the spell file should define the word
7945 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007946 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007947 */
7948 void
7949init_spell_chartab()
7950{
7951 int i;
7952
7953 did_set_spelltab = FALSE;
7954 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007955#ifdef FEAT_MBYTE
7956 if (enc_dbcs)
7957 {
7958 /* DBCS: assume double-wide characters are word characters. */
7959 for (i = 128; i <= 255; ++i)
7960 if (MB_BYTE2LEN(i) == 2)
7961 spelltab.st_isw[i] = TRUE;
7962 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007963 else if (enc_utf8)
7964 {
7965 for (i = 128; i < 256; ++i)
7966 {
7967 spelltab.st_isu[i] = utf_isupper(i);
7968 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7969 spelltab.st_fold[i] = utf_fold(i);
7970 spelltab.st_upper[i] = utf_toupper(i);
7971 }
7972 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007973 else
7974#endif
7975 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007976 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007977 for (i = 128; i < 256; ++i)
7978 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007979 if (MB_ISUPPER(i))
7980 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007981 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007982 spelltab.st_isu[i] = TRUE;
7983 spelltab.st_fold[i] = MB_TOLOWER(i);
7984 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007985 else if (MB_ISLOWER(i))
7986 {
7987 spelltab.st_isw[i] = TRUE;
7988 spelltab.st_upper[i] = MB_TOUPPER(i);
7989 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007990 }
7991 }
7992}
7993
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007994/*
7995 * Set the spell character tables from strings in the affix file.
7996 */
7997 static int
7998set_spell_chartab(fol, low, upp)
7999 char_u *fol;
8000 char_u *low;
8001 char_u *upp;
8002{
8003 /* We build the new tables here first, so that we can compare with the
8004 * previous one. */
8005 spelltab_T new_st;
8006 char_u *pf = fol, *pl = low, *pu = upp;
8007 int f, l, u;
8008
8009 clear_spell_chartab(&new_st);
8010
8011 while (*pf != NUL)
8012 {
8013 if (*pl == NUL || *pu == NUL)
8014 {
8015 EMSG(_(e_affform));
8016 return FAIL;
8017 }
8018#ifdef FEAT_MBYTE
8019 f = mb_ptr2char_adv(&pf);
8020 l = mb_ptr2char_adv(&pl);
8021 u = mb_ptr2char_adv(&pu);
8022#else
8023 f = *pf++;
8024 l = *pl++;
8025 u = *pu++;
8026#endif
8027 /* Every character that appears is a word character. */
8028 if (f < 256)
8029 new_st.st_isw[f] = TRUE;
8030 if (l < 256)
8031 new_st.st_isw[l] = TRUE;
8032 if (u < 256)
8033 new_st.st_isw[u] = TRUE;
8034
8035 /* if "LOW" and "FOL" are not the same the "LOW" char needs
8036 * case-folding */
8037 if (l < 256 && l != f)
8038 {
8039 if (f >= 256)
8040 {
8041 EMSG(_(e_affrange));
8042 return FAIL;
8043 }
8044 new_st.st_fold[l] = f;
8045 }
8046
8047 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008048 * case-folding, it's upper case and the "UPP" is the upper case of
8049 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008050 if (u < 256 && u != f)
8051 {
8052 if (f >= 256)
8053 {
8054 EMSG(_(e_affrange));
8055 return FAIL;
8056 }
8057 new_st.st_fold[u] = f;
8058 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008059 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008060 }
8061 }
8062
8063 if (*pl != NUL || *pu != NUL)
8064 {
8065 EMSG(_(e_affform));
8066 return FAIL;
8067 }
8068
8069 return set_spell_finish(&new_st);
8070}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008071
8072/*
8073 * Set the spell character tables from strings in the .spl file.
8074 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008075 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008076set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008077 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008078 int cnt; /* length of "flags" */
8079 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008080{
8081 /* We build the new tables here first, so that we can compare with the
8082 * previous one. */
8083 spelltab_T new_st;
8084 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008085 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008086 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008087
8088 clear_spell_chartab(&new_st);
8089
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008090 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008091 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008092 if (i < cnt)
8093 {
8094 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
8095 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
8096 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008097
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008098 if (*p != NUL)
8099 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008100#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008101 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008102#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008103 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008104#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008105 new_st.st_fold[i + 128] = c;
8106 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
8107 new_st.st_upper[c] = i + 128;
8108 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008109 }
8110
Bram Moolenaar5195e452005-08-19 20:32:47 +00008111 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008112}
8113
8114 static int
8115set_spell_finish(new_st)
8116 spelltab_T *new_st;
8117{
8118 int i;
8119
8120 if (did_set_spelltab)
8121 {
8122 /* check that it's the same table */
8123 for (i = 0; i < 256; ++i)
8124 {
8125 if (spelltab.st_isw[i] != new_st->st_isw[i]
8126 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008127 || spelltab.st_fold[i] != new_st->st_fold[i]
8128 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008129 {
8130 EMSG(_("E763: Word characters differ between spell files"));
8131 return FAIL;
8132 }
8133 }
8134 }
8135 else
8136 {
8137 /* copy the new spelltab into the one being used */
8138 spelltab = *new_st;
8139 did_set_spelltab = TRUE;
8140 }
8141
8142 return OK;
8143}
8144
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008145/*
Bram Moolenaarea408852005-06-25 22:49:46 +00008146 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008147 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00008148 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008149 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00008150 */
8151 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008152spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00008153 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008154 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00008155{
Bram Moolenaarea408852005-06-25 22:49:46 +00008156#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008157 char_u *s;
8158 int l;
8159 int c;
8160
8161 if (has_mbyte)
8162 {
8163 l = MB_BYTE2LEN(*p);
8164 s = p;
8165 if (l == 1)
8166 {
8167 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008168 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008169 {
8170 s = p + 1; /* skip a mid-word character */
8171 l = MB_BYTE2LEN(*s);
8172 }
8173 }
8174 else
8175 {
8176 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008177 if (c < 256 ? buf->b_spell_ismw[c]
8178 : (buf->b_spell_ismw_mb != NULL
8179 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008180 {
8181 s = p + l;
8182 l = MB_BYTE2LEN(*s);
8183 }
8184 }
8185
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008186 c = mb_ptr2char(s);
8187 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008188 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008189 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008190 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008191#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008192
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008193 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
8194}
8195
8196/*
8197 * Return TRUE if "p" points to a word character.
8198 * Unlike spell_iswordp() this doesn't check for "midword" characters.
8199 */
8200 static int
8201spell_iswordp_nmw(p)
8202 char_u *p;
8203{
8204#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008205 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008206
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008207 if (has_mbyte)
8208 {
8209 c = mb_ptr2char(p);
8210 if (c > 255)
8211 return mb_get_class(p) >= 2;
8212 return spelltab.st_isw[c];
8213 }
8214#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008215 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00008216}
8217
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008218#ifdef FEAT_MBYTE
8219/*
8220 * Return TRUE if "p" points to a word character.
8221 * Wide version of spell_iswordp().
8222 */
8223 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008224spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008225 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008226 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008227{
8228 int *s;
8229
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008230 if (*p < 256 ? buf->b_spell_ismw[*p]
8231 : (buf->b_spell_ismw_mb != NULL
8232 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008233 s = p + 1;
8234 else
8235 s = p;
8236
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008237 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008238 {
8239 if (enc_utf8)
8240 return utf_class(*s) >= 2;
8241 if (enc_dbcs)
8242 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
8243 return 0;
8244 }
8245 return spelltab.st_isw[*s];
8246}
8247#endif
8248
Bram Moolenaarea408852005-06-25 22:49:46 +00008249/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008250 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008251 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008252 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008253 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008254write_spell_prefcond(fd, gap)
8255 FILE *fd;
8256 garray_T *gap;
8257{
8258 int i;
8259 char_u *p;
8260 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008261 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008262
Bram Moolenaar5195e452005-08-19 20:32:47 +00008263 if (fd != NULL)
8264 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8265
8266 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008267
8268 for (i = 0; i < gap->ga_len; ++i)
8269 {
8270 /* <prefcond> : <condlen> <condstr> */
8271 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008272 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008273 {
8274 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008275 if (fd != NULL)
8276 {
8277 fputc(len, fd);
8278 fwrite(p, (size_t)len, (size_t)1, fd);
8279 }
8280 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008281 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008282 else if (fd != NULL)
8283 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008284 }
8285
Bram Moolenaar5195e452005-08-19 20:32:47 +00008286 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008287}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008288
8289/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008290 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8291 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008292 * When using a multi-byte 'encoding' the length may change!
8293 * Returns FAIL when something wrong.
8294 */
8295 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008296spell_casefold(str, len, buf, buflen)
8297 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008298 int len;
8299 char_u *buf;
8300 int buflen;
8301{
8302 int i;
8303
8304 if (len >= buflen)
8305 {
8306 buf[0] = NUL;
8307 return FAIL; /* result will not fit */
8308 }
8309
8310#ifdef FEAT_MBYTE
8311 if (has_mbyte)
8312 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008313 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008314 char_u *p;
8315 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008316
8317 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008318 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008319 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008320 if (outi + MB_MAXBYTES > buflen)
8321 {
8322 buf[outi] = NUL;
8323 return FAIL;
8324 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008325 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008326 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008327 }
8328 buf[outi] = NUL;
8329 }
8330 else
8331#endif
8332 {
8333 /* Be quick for non-multibyte encodings. */
8334 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008335 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008336 buf[i] = NUL;
8337 }
8338
8339 return OK;
8340}
8341
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008342#define SPS_BEST 1
8343#define SPS_FAST 2
8344#define SPS_DOUBLE 4
8345
8346static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008347static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008348
8349/*
8350 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008351 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008352 */
8353 int
8354spell_check_sps()
8355{
8356 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008357 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008358 char_u buf[MAXPATHL];
8359 int f;
8360
8361 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008362 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008363
8364 for (p = p_sps; *p != NUL; )
8365 {
8366 copy_option_part(&p, buf, MAXPATHL, ",");
8367
8368 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008369 if (VIM_ISDIGIT(*buf))
8370 {
8371 s = buf;
8372 sps_limit = getdigits(&s);
8373 if (*s != NUL && !VIM_ISDIGIT(*s))
8374 f = -1;
8375 }
8376 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008377 f = SPS_BEST;
8378 else if (STRCMP(buf, "fast") == 0)
8379 f = SPS_FAST;
8380 else if (STRCMP(buf, "double") == 0)
8381 f = SPS_DOUBLE;
8382 else if (STRNCMP(buf, "expr:", 5) != 0
8383 && STRNCMP(buf, "file:", 5) != 0)
8384 f = -1;
8385
8386 if (f == -1 || (sps_flags != 0 && f != 0))
8387 {
8388 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008389 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008390 return FAIL;
8391 }
8392 if (f != 0)
8393 sps_flags = f;
8394 }
8395
8396 if (sps_flags == 0)
8397 sps_flags = SPS_BEST;
8398
8399 return OK;
8400}
8401
8402/* Remember what "z?" replaced. */
8403static char_u *repl_from = NULL;
8404static char_u *repl_to = NULL;
8405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008406/*
8407 * "z?": Find badly spelled word under or after the cursor.
8408 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008409 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008410 */
8411 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008412spell_suggest(count)
8413 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414{
8415 char_u *line;
8416 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008417 char_u wcopy[MAXWLEN + 2];
8418 char_u *p;
8419 int i;
8420 int c;
8421 suginfo_T sug;
8422 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008423 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008424 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008425 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008426 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008427
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008428 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008429 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008430 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008432 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8433 return;
8434
8435 /* No bad word or it starts after the cursor: use the word under the
8436 * cursor. */
8437 curwin->w_cursor = prev_cursor;
8438 line = ml_get_curline();
8439 p = line + curwin->w_cursor.col;
8440 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008441 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008442 mb_ptr_back(line, p);
8443 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008444 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008445 mb_ptr_adv(p);
8446
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008447 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008448 {
8449 beep_flush();
8450 return;
8451 }
8452 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008453 }
8454
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008455 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008456
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008457 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008458 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008459
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008460 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008461
Bram Moolenaar5195e452005-08-19 20:32:47 +00008462 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8463 * 'spellsuggest', whatever is smaller. */
8464 if (sps_limit > (int)Rows - 2)
8465 limit = (int)Rows - 2;
8466 else
8467 limit = sps_limit;
8468 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008469 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008470
8471 if (sug.su_ga.ga_len == 0)
8472 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008473 else if (count > 0)
8474 {
8475 if (count > sug.su_ga.ga_len)
8476 smsg((char_u *)_("Sorry, only %ld suggestions"),
8477 (long)sug.su_ga.ga_len);
8478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008479 else
8480 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008481 vim_free(repl_from);
8482 repl_from = NULL;
8483 vim_free(repl_to);
8484 repl_to = NULL;
8485
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008486#ifdef FEAT_RIGHTLEFT
8487 /* When 'rightleft' is set the list is drawn right-left. */
8488 cmdmsg_rl = curwin->w_p_rl;
8489 if (cmdmsg_rl)
8490 msg_col = Columns - 1;
8491#endif
8492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008493 /* List the suggestions. */
8494 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008495 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008496 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8497 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008498#ifdef FEAT_RIGHTLEFT
8499 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8500 {
8501 /* And now the rabbit from the high hat: Avoid showing the
8502 * untranslated message rightleft. */
8503 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8504 sug.su_badlen, sug.su_badptr);
8505 }
8506#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008507 msg_puts(IObuff);
8508 msg_clr_eos();
8509 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008511 msg_scroll = TRUE;
8512 for (i = 0; i < sug.su_ga.ga_len; ++i)
8513 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008514 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515
8516 /* The suggested word may replace only part of the bad word, add
8517 * the not replaced part. */
8518 STRCPY(wcopy, stp->st_word);
8519 if (sug.su_badlen > stp->st_orglen)
8520 vim_strncpy(wcopy + STRLEN(wcopy),
8521 sug.su_badptr + stp->st_orglen,
8522 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008523 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8524#ifdef FEAT_RIGHTLEFT
8525 if (cmdmsg_rl)
8526 rl_mirror(IObuff);
8527#endif
8528 msg_puts(IObuff);
8529
8530 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008531 msg_puts(IObuff);
8532
8533 /* The word may replace more than "su_badlen". */
8534 if (sug.su_badlen < stp->st_orglen)
8535 {
8536 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8537 stp->st_orglen, sug.su_badptr);
8538 msg_puts(IObuff);
8539 }
8540
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008541 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008542 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008543 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008544 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008545 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008546 stp->st_salscore ? "s " : "",
8547 stp->st_score, stp->st_altscore);
8548 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008549 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008550 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008551#ifdef FEAT_RIGHTLEFT
8552 if (cmdmsg_rl)
8553 /* Mirror the numbers, but keep the leading space. */
8554 rl_mirror(IObuff + 1);
8555#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008556 msg_advance(30);
8557 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008559 msg_putchar('\n');
8560 }
8561
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008562#ifdef FEAT_RIGHTLEFT
8563 cmdmsg_rl = FALSE;
8564 msg_col = 0;
8565#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008566 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008567 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008568 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008569 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008570 }
8571
Bram Moolenaard12a1322005-08-21 22:08:24 +00008572 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8573 {
8574 /* Save the from and to text for :spellrepall. */
8575 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00008576 if (sug.su_badlen > stp->st_orglen)
8577 {
8578 /* Replacing less than "su_badlen", append the remainder to
8579 * repl_to. */
8580 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
8581 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
8582 sug.su_badlen - stp->st_orglen,
8583 sug.su_badptr + stp->st_orglen);
8584 repl_to = vim_strsave(IObuff);
8585 }
8586 else
8587 {
8588 /* Replacing su_badlen or more, use the whole word. */
8589 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8590 repl_to = vim_strsave(stp->st_word);
8591 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008592
8593 /* Replace the word. */
8594 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8595 if (p != NULL)
8596 {
8597 c = sug.su_badptr - line;
8598 mch_memmove(p, line, c);
8599 STRCPY(p + c, stp->st_word);
8600 STRCAT(p, sug.su_badptr + stp->st_orglen);
8601 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8602 curwin->w_cursor.col = c;
8603 changed_bytes(curwin->w_cursor.lnum, c);
8604
8605 /* For redo we use a change-word command. */
8606 ResetRedobuff();
8607 AppendToRedobuff((char_u *)"ciw");
8608 AppendToRedobuff(stp->st_word);
8609 AppendCharToRedobuff(ESC);
8610 }
8611 }
8612 else
8613 curwin->w_cursor = prev_cursor;
8614
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008615 spell_find_cleanup(&sug);
8616}
8617
8618/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008619 * Check if the word at line "lnum" column "col" is required to start with a
8620 * capital. This uses 'spellcapcheck' of the current buffer.
8621 */
8622 static int
8623check_need_cap(lnum, col)
8624 linenr_T lnum;
8625 colnr_T col;
8626{
8627 int need_cap = FALSE;
8628 char_u *line;
8629 char_u *line_copy = NULL;
8630 char_u *p;
8631 colnr_T endcol;
8632 regmatch_T regmatch;
8633
8634 if (curbuf->b_cap_prog == NULL)
8635 return FALSE;
8636
8637 line = ml_get_curline();
8638 endcol = 0;
8639 if ((int)(skipwhite(line) - line) >= (int)col)
8640 {
8641 /* At start of line, check if previous line is empty or sentence
8642 * ends there. */
8643 if (lnum == 1)
8644 need_cap = TRUE;
8645 else
8646 {
8647 line = ml_get(lnum - 1);
8648 if (*skipwhite(line) == NUL)
8649 need_cap = TRUE;
8650 else
8651 {
8652 /* Append a space in place of the line break. */
8653 line_copy = concat_str(line, (char_u *)" ");
8654 line = line_copy;
8655 endcol = STRLEN(line);
8656 }
8657 }
8658 }
8659 else
8660 endcol = col;
8661
8662 if (endcol > 0)
8663 {
8664 /* Check if sentence ends before the bad word. */
8665 regmatch.regprog = curbuf->b_cap_prog;
8666 regmatch.rm_ic = FALSE;
8667 p = line + endcol;
8668 for (;;)
8669 {
8670 mb_ptr_back(line, p);
8671 if (p == line || spell_iswordp_nmw(p))
8672 break;
8673 if (vim_regexec(&regmatch, p, 0)
8674 && regmatch.endp[0] == line + endcol)
8675 {
8676 need_cap = TRUE;
8677 break;
8678 }
8679 }
8680 }
8681
8682 vim_free(line_copy);
8683
8684 return need_cap;
8685}
8686
8687
8688/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008689 * ":spellrepall"
8690 */
8691/*ARGSUSED*/
8692 void
8693ex_spellrepall(eap)
8694 exarg_T *eap;
8695{
8696 pos_T pos = curwin->w_cursor;
8697 char_u *frompat;
8698 int addlen;
8699 char_u *line;
8700 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008701 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008702 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008703
8704 if (repl_from == NULL || repl_to == NULL)
8705 {
8706 EMSG(_("E752: No previous spell replacement"));
8707 return;
8708 }
8709 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8710
8711 frompat = alloc(STRLEN(repl_from) + 7);
8712 if (frompat == NULL)
8713 return;
8714 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8715 p_ws = FALSE;
8716
Bram Moolenaar5195e452005-08-19 20:32:47 +00008717 sub_nsubs = 0;
8718 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008719 curwin->w_cursor.lnum = 0;
8720 while (!got_int)
8721 {
8722 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8723 || u_save_cursor() == FAIL)
8724 break;
8725
8726 /* Only replace when the right word isn't there yet. This happens
8727 * when changing "etc" to "etc.". */
8728 line = ml_get_curline();
8729 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8730 repl_to, STRLEN(repl_to)) != 0)
8731 {
8732 p = alloc(STRLEN(line) + addlen + 1);
8733 if (p == NULL)
8734 break;
8735 mch_memmove(p, line, curwin->w_cursor.col);
8736 STRCPY(p + curwin->w_cursor.col, repl_to);
8737 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8738 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8739 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008740
8741 if (curwin->w_cursor.lnum != prev_lnum)
8742 {
8743 ++sub_nlines;
8744 prev_lnum = curwin->w_cursor.lnum;
8745 }
8746 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008747 }
8748 curwin->w_cursor.col += STRLEN(repl_to);
8749 }
8750
8751 p_ws = save_ws;
8752 curwin->w_cursor = pos;
8753 vim_free(frompat);
8754
Bram Moolenaar5195e452005-08-19 20:32:47 +00008755 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008756 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008757 else
8758 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008759}
8760
8761/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008762 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8763 * a list of allocated strings.
8764 */
8765 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008766spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008767 garray_T *gap;
8768 char_u *word;
8769 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008770 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008771{
8772 suginfo_T sug;
8773 int i;
8774 suggest_T *stp;
8775 char_u *wcopy;
8776
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008777 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008778
8779 /* Make room in "gap". */
8780 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8781 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8782 return;
8783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008785 {
8786 stp = &SUG(sug.su_ga, i);
8787
8788 /* The suggested word may replace only part of "word", add the not
8789 * replaced part. */
8790 wcopy = alloc(STRLEN(stp->st_word)
8791 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8792 if (wcopy == NULL)
8793 break;
8794 STRCPY(wcopy, stp->st_word);
8795 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8796 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8797 }
8798
8799 spell_find_cleanup(&sug);
8800}
8801
8802/*
8803 * Find spell suggestions for the word at the start of "badptr".
8804 * Return the suggestions in "su->su_ga".
8805 * The maximum number of suggestions is "maxcount".
8806 * Note: does use info for the current window.
8807 * This is based on the mechanisms of Aspell, but completely reimplemented.
8808 */
8809 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008810spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008811 char_u *badptr;
8812 suginfo_T *su;
8813 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008814 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008815 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008816{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008817 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008818 char_u buf[MAXPATHL];
8819 char_u *p;
8820 int do_combine = FALSE;
8821 char_u *sps_copy;
8822#ifdef FEAT_EVAL
8823 static int expr_busy = FALSE;
8824#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008825 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008826 int i;
8827 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008828
8829 /*
8830 * Set the info in "*su".
8831 */
8832 vim_memset(su, 0, sizeof(suginfo_T));
8833 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8834 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008835 if (*badptr == NUL)
8836 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008837 hash_init(&su->su_banned);
8838
8839 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008840 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008841 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008842 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008843
8844 if (su->su_badlen >= MAXWLEN)
8845 su->su_badlen = MAXWLEN - 1; /* just in case */
8846 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8847 (void)spell_casefold(su->su_badptr, su->su_badlen,
8848 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008849 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008850 su->su_badflags = badword_captype(su->su_badptr,
8851 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008852 if (need_cap)
8853 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008854
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008855 /* Find the default language for sound folding. We simply use the first
8856 * one in 'spelllang' that supports sound folding. That's good for when
8857 * using multiple files for one language, it's not that bad when mixing
8858 * languages (e.g., "pl,en"). */
8859 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
8860 {
8861 lp = LANGP_ENTRY(curbuf->b_langp, i);
8862 if (lp->lp_sallang != NULL)
8863 {
8864 su->su_sallang = lp->lp_sallang;
8865 break;
8866 }
8867 }
8868
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008869 /* Soundfold the bad word with the default sound folding, so that we don't
8870 * have to do this many times. */
8871 if (su->su_sallang != NULL)
8872 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
8873 su->su_sal_badword);
8874
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008875 /* If the word is not capitalised and spell_check() doesn't consider the
8876 * word to be bad then it might need to be capitalised. Add a suggestion
8877 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008878 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008879 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008880 {
8881 make_case_word(su->su_badword, buf, WF_ONECAP);
8882 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008883 0, TRUE, su->su_sallang);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008884 }
8885
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008886 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008887 if (banbadword)
8888 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008889
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008890 /* Make a copy of 'spellsuggest', because the expression may change it. */
8891 sps_copy = vim_strsave(p_sps);
8892 if (sps_copy == NULL)
8893 return;
8894
8895 /* Loop over the items in 'spellsuggest'. */
8896 for (p = sps_copy; *p != NUL; )
8897 {
8898 copy_option_part(&p, buf, MAXPATHL, ",");
8899
8900 if (STRNCMP(buf, "expr:", 5) == 0)
8901 {
8902#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008903 /* Evaluate an expression. Skip this when called recursively,
8904 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008905 if (!expr_busy)
8906 {
8907 expr_busy = TRUE;
8908 spell_suggest_expr(su, buf + 5);
8909 expr_busy = FALSE;
8910 }
8911#endif
8912 }
8913 else if (STRNCMP(buf, "file:", 5) == 0)
8914 /* Use list of suggestions in a file. */
8915 spell_suggest_file(su, buf + 5);
8916 else
8917 {
8918 /* Use internal method. */
8919 spell_suggest_intern(su);
8920 if (sps_flags & SPS_DOUBLE)
8921 do_combine = TRUE;
8922 }
8923 }
8924
8925 vim_free(sps_copy);
8926
8927 if (do_combine)
8928 /* Combine the two list of suggestions. This must be done last,
8929 * because sorting changes the order again. */
8930 score_combine(su);
8931}
8932
8933#ifdef FEAT_EVAL
8934/*
8935 * Find suggestions by evaluating expression "expr".
8936 */
8937 static void
8938spell_suggest_expr(su, expr)
8939 suginfo_T *su;
8940 char_u *expr;
8941{
8942 list_T *list;
8943 listitem_T *li;
8944 int score;
8945 char_u *p;
8946
8947 /* The work is split up in a few parts to avoid having to export
8948 * suginfo_T.
8949 * First evaluate the expression and get the resulting list. */
8950 list = eval_spell_expr(su->su_badword, expr);
8951 if (list != NULL)
8952 {
8953 /* Loop over the items in the list. */
8954 for (li = list->lv_first; li != NULL; li = li->li_next)
8955 if (li->li_tv.v_type == VAR_LIST)
8956 {
8957 /* Get the word and the score from the items. */
8958 score = get_spellword(li->li_tv.vval.v_list, &p);
8959 if (score >= 0)
8960 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008961 su->su_badlen, score, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008962 }
8963 list_unref(list);
8964 }
8965
8966 /* Sort the suggestions and truncate at "maxcount". */
8967 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8968}
8969#endif
8970
8971/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008972 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008973 */
8974 static void
8975spell_suggest_file(su, fname)
8976 suginfo_T *su;
8977 char_u *fname;
8978{
8979 FILE *fd;
8980 char_u line[MAXWLEN * 2];
8981 char_u *p;
8982 int len;
8983 char_u cword[MAXWLEN];
8984
8985 /* Open the file. */
8986 fd = mch_fopen((char *)fname, "r");
8987 if (fd == NULL)
8988 {
8989 EMSG2(_(e_notopen), fname);
8990 return;
8991 }
8992
8993 /* Read it line by line. */
8994 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8995 {
8996 line_breakcheck();
8997
8998 p = vim_strchr(line, '/');
8999 if (p == NULL)
9000 continue; /* No Tab found, just skip the line. */
9001 *p++ = NUL;
9002 if (STRICMP(su->su_badword, line) == 0)
9003 {
9004 /* Match! Isolate the good word, until CR or NL. */
9005 for (len = 0; p[len] >= ' '; ++len)
9006 ;
9007 p[len] = NUL;
9008
9009 /* If the suggestion doesn't have specific case duplicate the case
9010 * of the bad word. */
9011 if (captype(p, NULL) == 0)
9012 {
9013 make_case_word(p, cword, su->su_badflags);
9014 p = cword;
9015 }
9016
9017 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009018 SCORE_FILE, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009019 }
9020 }
9021
9022 fclose(fd);
9023
9024 /* Sort the suggestions and truncate at "maxcount". */
9025 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9026}
9027
9028/*
9029 * Find suggestions for the internal method indicated by "sps_flags".
9030 */
9031 static void
9032spell_suggest_intern(su)
9033 suginfo_T *su;
9034{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009035 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009036 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009037 *
9038 * Set a maximum score to limit the combination of operations that is
9039 * tried.
9040 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009041 suggest_try_special(su);
9042
9043 /*
9044 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
9045 * from the .aff file and inserting a space (split the word).
9046 */
9047 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009048
9049 /* For the resulting top-scorers compute the sound-a-like score. */
9050 if (sps_flags & SPS_DOUBLE)
9051 score_comp_sal(su);
9052
9053 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009054 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009055 *
9056 * Only do this when we don't have a lot of suggestions yet, because it's
9057 * very slow and often doesn't find new suggestions.
9058 */
9059 if ((sps_flags & SPS_DOUBLE)
9060 || (!(sps_flags & SPS_FAST)
9061 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
9062 {
9063 /* Allow a higher score now. */
9064 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009065 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009066 }
9067
9068 /* When CTRL-C was hit while searching do show the results. */
9069 ui_breakcheck();
9070 if (got_int)
9071 {
9072 (void)vgetc();
9073 got_int = FALSE;
9074 }
9075
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009076 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009077 {
9078 if (sps_flags & SPS_BEST)
9079 /* Adjust the word score for how it sounds like. */
9080 rescore_suggestions(su);
9081
9082 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009083 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009084 }
9085}
9086
9087/*
9088 * Free the info put in "*su" by spell_find_suggest().
9089 */
9090 static void
9091spell_find_cleanup(su)
9092 suginfo_T *su;
9093{
9094 int i;
9095
9096 /* Free the suggestions. */
9097 for (i = 0; i < su->su_ga.ga_len; ++i)
9098 vim_free(SUG(su->su_ga, i).st_word);
9099 ga_clear(&su->su_ga);
9100 for (i = 0; i < su->su_sga.ga_len; ++i)
9101 vim_free(SUG(su->su_sga, i).st_word);
9102 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009103
9104 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009105 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106}
9107
9108/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009109 * Make a copy of "word", with the first letter upper or lower cased, to
9110 * "wcopy[MAXWLEN]". "word" must not be empty.
9111 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 */
9113 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009114onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009116 char_u *wcopy;
9117 int upper; /* TRUE: first letter made upper case */
9118{
9119 char_u *p;
9120 int c;
9121 int l;
9122
9123 p = word;
9124#ifdef FEAT_MBYTE
9125 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009126 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 else
9128#endif
9129 c = *p++;
9130 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009131 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009133 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134#ifdef FEAT_MBYTE
9135 if (has_mbyte)
9136 l = mb_char2bytes(c, wcopy);
9137 else
9138#endif
9139 {
9140 l = 1;
9141 wcopy[0] = c;
9142 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009143 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144}
9145
9146/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009147 * Make a copy of "word" with all the letters upper cased into
9148 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 */
9150 static void
9151allcap_copy(word, wcopy)
9152 char_u *word;
9153 char_u *wcopy;
9154{
9155 char_u *s;
9156 char_u *d;
9157 int c;
9158
9159 d = wcopy;
9160 for (s = word; *s != NUL; )
9161 {
9162#ifdef FEAT_MBYTE
9163 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009164 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 else
9166#endif
9167 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00009168
9169#ifdef FEAT_MBYTE
9170 /* We only change ß to SS when we are certain latin1 is used. It
9171 * would cause weird errors in other 8-bit encodings. */
9172 if (enc_latin1like && c == 0xdf)
9173 {
9174 c = 'S';
9175 if (d - wcopy >= MAXWLEN - 1)
9176 break;
9177 *d++ = c;
9178 }
9179 else
9180#endif
9181 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182
9183#ifdef FEAT_MBYTE
9184 if (has_mbyte)
9185 {
9186 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
9187 break;
9188 d += mb_char2bytes(c, d);
9189 }
9190 else
9191#endif
9192 {
9193 if (d - wcopy >= MAXWLEN - 1)
9194 break;
9195 *d++ = c;
9196 }
9197 }
9198 *d = NUL;
9199}
9200
9201/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009202 * Try finding suggestions by recognizing specific situations.
9203 */
9204 static void
9205suggest_try_special(su)
9206 suginfo_T *su;
9207{
9208 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009209 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009210 int c;
9211 char_u word[MAXWLEN];
9212
9213 /*
9214 * Recognize a word that is repeated: "the the".
9215 */
9216 p = skiptowhite(su->su_fbadword);
9217 len = p - su->su_fbadword;
9218 p = skipwhite(p);
9219 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
9220 {
9221 /* Include badflags: if the badword is onecap or allcap
9222 * use that for the goodword too: "The the" -> "The". */
9223 c = su->su_fbadword[len];
9224 su->su_fbadword[len] = NUL;
9225 make_case_word(su->su_fbadword, word, su->su_badflags);
9226 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009227
9228 /* Give a soundalike score of 0, compute the score as if deleting one
9229 * character. */
9230 add_suggestion(su, &su->su_ga, word, su->su_badlen,
9231 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009232 }
9233}
9234
9235/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00009237 *
9238 * This uses a state machine. At each node in the tree we try various
9239 * operations. When trying if an operation work "depth" is increased and the
9240 * stack[] is used to store info. This allows combinations, thus insert one
9241 * character, replace one and delete another. The number of changes is
9242 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00009243 *
9244 * After implementing this I noticed an article by Kemal Oflazer that
9245 * describes something similar: "Error-tolerant Finite State Recognition with
9246 * Applications to Morphological Analysis and Spelling Correction" (1996).
9247 * The implementation in the article is simplified and requires a stack of
9248 * unknown depth. The implementation here only needs a stack depth of the
9249 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 */
9251 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009252suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009253 suginfo_T *su;
9254{
9255 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
9256 char_u tword[MAXWLEN]; /* good word collected so far */
9257 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009258 char_u preword[MAXWLEN * 3]; /* word found with proper case;
9259 * concatanation of prefix compound
9260 * words and split word. NUL terminated
9261 * when going deeper but not when coming
9262 * back. */
9263 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 trystate_T *sp;
9265 int newscore;
9266 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009267 char_u *byts, *fbyts, *pbyts;
9268 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009270 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009271 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009274 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 int len;
9276 char_u *p;
9277 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00009278 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009279 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009280 slang_T *slang;
9281 int fword_ends;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009282 int lpi;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009283 int maysplit;
9284 int goodword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285
9286 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00009287 * to find matches (esp. REP items). Append some more text, changing
9288 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009290 n = STRLEN(fword);
9291 p = su->su_badptr + su->su_badlen;
9292 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009293
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009294 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009295 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009296 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009297 slang = lp->lp_slang;
9298
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009299 /* If reloading a spell file fails it's still in the list but
9300 * everything has been cleared. */
9301 if (slang->sl_fbyts == NULL)
9302 continue;
9303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009304 /*
9305 * Go through the whole case-fold tree, try changes at each node.
9306 * "tword[]" contains the word collected from nodes in the tree.
9307 * "fword[]" the word we are trying to match with (initially the bad
9308 * word).
9309 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009311 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009312 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009313 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009314
Bram Moolenaarea424162005-06-16 21:51:00 +00009315 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009316 * When there are postponed prefixes we need to use these first. At
9317 * the end of the prefix we continue in the case-fold tree.
9318 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009319 fbyts = slang->sl_fbyts;
9320 fidxs = slang->sl_fidxs;
9321 pbyts = slang->sl_pbyts;
9322 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009323 if (pbyts != NULL)
9324 {
9325 byts = pbyts;
9326 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009327 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009328 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9329 }
9330 else
9331 {
9332 byts = fbyts;
9333 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009334 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009335 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009336 }
9337
9338 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009339 * Loop to find all suggestions. At each round we either:
9340 * - For the current state try one operation, advance "ts_curi",
9341 * increase "depth".
9342 * - When a state is done go to the next, set "ts_state".
9343 * - When all states are tried decrease "depth".
9344 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 while (depth >= 0 && !got_int)
9346 {
9347 sp = &stack[depth];
9348 switch (sp->ts_state)
9349 {
9350 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009351 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009352 /*
9353 * Start of node: Deal with NUL bytes, which means
9354 * tword[] may end here.
9355 */
9356 arridx = sp->ts_arridx; /* current node in the tree */
9357 len = byts[arridx]; /* bytes in this node */
9358 arridx += sp->ts_curi; /* index of current byte */
9359
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009360 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009361 {
9362 /* Skip over the NUL bytes, we use them later. */
9363 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9364 ;
9365 sp->ts_curi += n;
9366
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009367 /* Always past NUL bytes now. */
9368 n = (int)sp->ts_state;
9369 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009370 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009371
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009372 /* At end of a prefix or at start of prefixtree: check for
9373 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009374 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009375 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009376 /* Set su->su_badflags to the caps type at this
9377 * position. Use the caps type until here for the
9378 * prefix itself. */
9379#ifdef FEAT_MBYTE
9380 if (has_mbyte)
9381 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9382 else
9383#endif
9384 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009385 flags = badword_captype(su->su_badptr,
9386 su->su_badptr + n);
9387 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009388 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009389 ++depth;
9390 stack[depth] = stack[depth - 1];
9391 sp = &stack[depth];
9392 sp->ts_prefixdepth = depth - 1;
9393 byts = fbyts;
9394 idxs = fidxs;
9395 sp->ts_state = STATE_START;
9396 sp->ts_curi = 1; /* start just after length byte */
9397 sp->ts_arridx = 0;
9398
Bram Moolenaar53805d12005-08-01 07:08:33 +00009399 /* Move the prefix to preword[] with the right case
9400 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009401 tword[sp->ts_twordlen] = NUL;
9402 make_case_word(tword + sp->ts_splitoff,
9403 preword + sp->ts_prewordlen,
9404 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009405 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009406 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009407 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009408 break;
9409 }
9410
Bram Moolenaar0c405862005-06-22 22:26:26 +00009411 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 {
9413 /* Past bytes in node and/or past NUL bytes. */
9414 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009415 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009416 break;
9417 }
9418
9419 /*
9420 * End of word in tree.
9421 */
9422 ++sp->ts_curi; /* eat one NUL byte */
9423
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009424 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009425 fword_ends = (fword[sp->ts_fidx] == NUL
9426 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9427 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009428
Bram Moolenaard12a1322005-08-21 22:08:24 +00009429 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9430 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009431 {
9432 /* There was a prefix before the word. Check that the
9433 * prefix can be used with this word. */
9434 /* Count the length of the NULs in the prefix. If there
9435 * are none this must be the first try without a prefix.
9436 */
9437 n = stack[sp->ts_prefixdepth].ts_arridx;
9438 len = pbyts[n++];
9439 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9440 ;
9441 if (c > 0)
9442 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009443 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009444 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009445 if (c == 0)
9446 break;
9447
9448 /* Use the WF_RARE flag for a rare prefix. */
9449 if (c & WF_RAREPFX)
9450 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009451
9452 /* Tricky: when checking for both prefix and
9453 * compounding we run into the prefix flag first.
9454 * Remember that it's OK, so that we accept the prefix
9455 * when arriving at a compound flag. */
9456 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009457 }
9458 }
9459
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009460 /* Check NEEDCOMPOUND: can't use word without compounding. Do
9461 * try appending another compound word below. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009462 if (sp->ts_complen == sp->ts_compsplit && fword_ends
9463 && (flags & WF_NEEDCOMP))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009464 goodword_ends = FALSE;
9465 else
9466 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009467
Bram Moolenaard12a1322005-08-21 22:08:24 +00009468 if (sp->ts_complen > sp->ts_compsplit)
9469 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009470 if (slang->sl_nobreak)
9471 {
9472 /* There was a word before this word. When there was
9473 * no change in this word (it was correct) add the
9474 * first word as a suggestion. If this word was
9475 * corrected too, we need to check if a correct word
9476 * follows. */
9477 if (sp->ts_fidx - sp->ts_splitfidx
9478 == sp->ts_twordlen - sp->ts_splitoff
9479 && STRNCMP(fword + sp->ts_splitfidx,
9480 tword + sp->ts_splitoff,
9481 sp->ts_fidx - sp->ts_splitfidx) == 0)
9482 {
9483 preword[sp->ts_prewordlen] = NUL;
9484 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009485 sp->ts_splitfidx - repextra,
9486 sp->ts_score, 0, FALSE,
9487 lp->lp_sallang);
Bram Moolenaar78622822005-08-23 21:00:13 +00009488 break;
9489 }
9490 }
9491 else
9492 {
9493 /* There was a compound word before this word. If
9494 * this word does not support compounding then give up
9495 * (splitting is tried for the word without compound
9496 * flag). */
9497 if (((unsigned)flags >> 24) == 0
9498 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009499 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009500 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009501#ifdef FEAT_MBYTE
9502 /* For multi-byte chars check character length against
9503 * COMPOUNDMIN. */
9504 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009505 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009506 && mb_charlen(tword + sp->ts_splitoff)
9507 < slang->sl_compminlen)
9508 break;
9509#endif
9510
Bram Moolenaar78622822005-08-23 21:00:13 +00009511 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9512 compflags[sp->ts_complen + 1] = NUL;
9513 vim_strncpy(preword + sp->ts_prewordlen,
9514 tword + sp->ts_splitoff,
9515 sp->ts_twordlen - sp->ts_splitoff);
9516 p = preword;
9517 while (*skiptowhite(p) != NUL)
9518 p = skipwhite(skiptowhite(p));
9519 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009520 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009521 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009522
Bram Moolenaar78622822005-08-23 21:00:13 +00009523 /* Get pointer to last char of previous word. */
9524 p = preword + sp->ts_prewordlen;
9525 mb_ptr_back(preword, p);
9526 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009527 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009528 else
9529 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 /*
9532 * Form the word with proper case in preword.
9533 * If there is a word from a previous split, append.
9534 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 if (flags & WF_KEEPCAP)
9536 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009537 find_keepcap_word(slang, tword + sp->ts_splitoff,
9538 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009541 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009542 * use that for the goodword too. But if the badword is
9543 * allcap and it's only one char long use onecap. */
9544 c = su->su_badflags;
9545 if ((c & WF_ALLCAP)
9546#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009547 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009548#else
9549 && su->su_badlen == 1
9550#endif
9551 )
9552 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009553 c |= flags;
9554
9555 /* When appending a compound word after a word character
9556 * don't use Onecap. */
9557 if (p != NULL && spell_iswordp_nmw(p))
9558 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009559 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009560 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562
9563 /* Don't use a banned word. It may appear again as a good
9564 * word, thus remember it. */
9565 if (flags & WF_BANNED)
9566 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009567 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009568 break;
9569 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009570 if ((sp->ts_complen == sp->ts_compsplit
9571 && was_banned(su, preword + sp->ts_prewordlen))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009572 || was_banned(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009573 {
9574 if (slang->sl_compprog == NULL)
9575 break;
9576 /* the word so far was banned but we may try compounding */
9577 goodword_ends = FALSE;
9578 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579
9580 newscore = 0;
9581 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009582 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 newscore += SCORE_REGION;
9584 if (flags & WF_RARE)
9585 newscore += SCORE_RARE;
9586
Bram Moolenaar0c405862005-06-22 22:26:26 +00009587 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009588 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 newscore += SCORE_ICASE;
9590
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009591 maysplit = TRUE;
9592 if (fword_ends && goodword_ends
9593 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009594 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009595 /* The badword also ends: add suggestions. Give a penalty
9596 * when changing non-word char to word char, e.g., "thes,"
9597 * -> "these". */
9598 p = fword + sp->ts_fidx;
9599#ifdef FEAT_MBYTE
9600 if (has_mbyte)
9601 mb_ptr_back(fword, p);
9602 else
9603#endif
9604 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009605 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009606 {
9607 p = preword + STRLEN(preword);
9608#ifdef FEAT_MBYTE
9609 if (has_mbyte)
9610 mb_ptr_back(preword, p);
9611 else
9612#endif
9613 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009614 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009615 newscore += SCORE_NONWORD;
9616 }
9617
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009618 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009619 sp->ts_fidx - repextra,
9620 sp->ts_score + newscore, 0, FALSE,
9621 lp->lp_sallang);
9622
9623 /* When the bad word doesn't end yet, try changing the
9624 * next word. E.g., find suggestions for "the the" where
9625 * the second "the" is different. It's done like a split.
9626 */
9627 if (sp->ts_fidx - repextra >= su->su_badlen)
9628 maysplit = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009630
9631 if (maysplit
9632 && (sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009633#ifdef FEAT_MBYTE
9634 /* Don't split halfway a character. */
9635 && (!has_mbyte || sp->ts_tcharlen == 0)
9636#endif
9637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009639 int try_compound;
9640
9641 /* Get here in two situations:
9642 * 1. The word in the tree ends but the badword continues:
9643 * If the word allows compounding try that. Otherwise
9644 * try a split by inserting a space. For both check
9645 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009646 * For NOBREAK do like compounding to be able to check
9647 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009648 * 2. The badword does end, but it was due to a change
9649 * (e.g., a swap). No need to split, but do check that
9650 * the following word is valid.
9651 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009652 try_compound = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009653 if ((!fword_ends || !goodword_ends)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009654 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009655 && ((unsigned)flags >> 24) != 0
9656 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009657 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009658#ifdef FEAT_MBYTE
9659 && (!has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009660 || slang->sl_compminlen == 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009661 || mb_charlen(tword + sp->ts_splitoff)
9662 >= slang->sl_compminlen)
9663#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00009664 && (slang->sl_compsylmax < MAXWLEN
9665 || sp->ts_complen + 1 - sp->ts_compsplit
9666 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009667 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009668 ? slang->sl_compstartflags
9669 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009670 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009671 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009672 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009673 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9674 compflags[sp->ts_complen + 1] = NUL;
9675 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009676
Bram Moolenaar78622822005-08-23 21:00:13 +00009677 /* For NOBREAK we never try splitting, it won't make any
9678 * word valid. */
9679 if (slang->sl_nobreak)
9680 try_compound = TRUE;
9681
Bram Moolenaard12a1322005-08-21 22:08:24 +00009682 /* If we could add a compound word, and it's also possible
9683 * to split at this point, do the split first and set
9684 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009685 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009686 && try_compound
9687 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009688 {
9689 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009690 sp->ts_flags |= TSF_DIDSPLIT;
9691 --sp->ts_curi; /* do the same NUL again */
9692 compflags[sp->ts_complen] = NUL;
9693 }
9694 else
9695 sp->ts_flags &= ~TSF_DIDSPLIT;
9696
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009697 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00009698 {
9699 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009700 * words so far are valid for compounding. If there
9701 * is only one word it must not have the NEEDCOMPOUND
9702 * flag. */
9703 if (sp->ts_complen == sp->ts_compsplit
9704 && (flags & WF_NEEDCOMP))
9705 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009706 p = preword;
9707 while (*skiptowhite(p) != NUL)
9708 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009709 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009710 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009711 compflags + sp->ts_compsplit))
9712 break;
9713 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009714 }
9715
9716 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009717 {
9718 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009719 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009720 sp->ts_state = STATE_SPLITUNDO;
9721
9722 ++depth;
9723 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009724
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009725 /* Append a space to preword when splitting. */
9726 if (!try_compound && !fword_ends)
9727 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009728 sp->ts_prewordlen = STRLEN(preword);
9729 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009730 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009731
9732 /* If the badword has a non-word character at this
9733 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009734 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009735 * character when the word ends. But only when the
9736 * good word can end. */
9737 if (((!try_compound
9738 && !spell_iswordp_nmw(fword + sp->ts_fidx))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009739 || fword_ends)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009740 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009741 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009742 int l;
9743
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009744#ifdef FEAT_MBYTE
9745 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009746 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009747 else
9748#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009749 l = 1;
9750 if (fword_ends)
9751 {
9752 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009753 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009754 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009755 sp->ts_prewordlen += l;
9756 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009757 }
9758 else
9759 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9760 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009761 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009762
Bram Moolenaard12a1322005-08-21 22:08:24 +00009763 /* When compounding include compound flag in
9764 * compflags[] (already set above). When splitting we
9765 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009766 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009767 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009768 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009769 sp->ts_compsplit = sp->ts_complen;
9770 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009771
Bram Moolenaar53805d12005-08-01 07:08:33 +00009772 /* set su->su_badflags to the caps type at this
9773 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009774#ifdef FEAT_MBYTE
9775 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009776 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009777 else
9778#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009779 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009780 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009781 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009783 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009784 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009785
9786 /* If there are postponed prefixes, try these too. */
9787 if (pbyts != NULL)
9788 {
9789 byts = pbyts;
9790 idxs = pidxs;
9791 sp->ts_prefixdepth = PFD_PREFIXTREE;
9792 sp->ts_state = STATE_NOPREFIX;
9793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 }
9795 }
9796 break;
9797
9798 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009799 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009800 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009801
9802 /* Continue looking for NUL bytes. */
9803 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009804
9805 /* In case we went into the prefix tree. */
9806 byts = fbyts;
9807 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 break;
9809
9810 case STATE_ENDNUL:
9811 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009812 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009813 if (fword[sp->ts_fidx] == NUL
9814#ifdef FEAT_MBYTE
9815 && sp->ts_tcharlen == 0
9816#endif
9817 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 {
9819 /* The badword ends, can't use the bytes in this node. */
9820 sp->ts_state = STATE_DEL;
9821 break;
9822 }
9823 sp->ts_state = STATE_PLAIN;
9824 /*FALLTHROUGH*/
9825
9826 case STATE_PLAIN:
9827 /*
9828 * Go over all possible bytes at this node, add each to
9829 * tword[] and use child node. "ts_curi" is the index.
9830 */
9831 arridx = sp->ts_arridx;
9832 if (sp->ts_curi > byts[arridx])
9833 {
9834 /* Done all bytes at this node, do next state. When still
9835 * at already changed bytes skip the other tricks. */
9836 if (sp->ts_fidx >= sp->ts_fidxtry)
9837 sp->ts_state = STATE_DEL;
9838 else
9839 sp->ts_state = STATE_FINAL;
9840 }
9841 else
9842 {
9843 arridx += sp->ts_curi++;
9844 c = byts[arridx];
9845
9846 /* Normal byte, go one level deeper. If it's not equal to
9847 * the byte in the bad word adjust the score. But don't
9848 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009849 if (c == fword[sp->ts_fidx]
9850#ifdef FEAT_MBYTE
9851 || (sp->ts_tcharlen > 0
9852 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009853#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009854 )
9855 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 else
9857 newscore = SCORE_SUBST;
9858 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9859 && try_deeper(su, stack, depth, newscore))
9860 {
9861 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009862 sp = &stack[depth];
9863 ++sp->ts_fidx;
9864 tword[sp->ts_twordlen++] = c;
9865 sp->ts_arridx = idxs[arridx];
9866#ifdef FEAT_MBYTE
9867 if (newscore == SCORE_SUBST)
9868 sp->ts_isdiff = DIFF_YES;
9869 if (has_mbyte)
9870 {
9871 /* Multi-byte characters are a bit complicated to
9872 * handle: They differ when any of the bytes
9873 * differ and then their length may also differ. */
9874 if (sp->ts_tcharlen == 0)
9875 {
9876 /* First byte. */
9877 sp->ts_tcharidx = 0;
9878 sp->ts_tcharlen = MB_BYTE2LEN(c);
9879 sp->ts_fcharstart = sp->ts_fidx - 1;
9880 sp->ts_isdiff = (newscore != 0)
9881 ? DIFF_YES : DIFF_NONE;
9882 }
9883 else if (sp->ts_isdiff == DIFF_INSERT)
9884 /* When inserting trail bytes don't advance in
9885 * the bad word. */
9886 --sp->ts_fidx;
9887 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9888 {
9889 /* Last byte of character. */
9890 if (sp->ts_isdiff == DIFF_YES)
9891 {
9892 /* Correct ts_fidx for the byte length of
9893 * the character (we didn't check that
9894 * before). */
9895 sp->ts_fidx = sp->ts_fcharstart
9896 + MB_BYTE2LEN(
9897 fword[sp->ts_fcharstart]);
9898
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009899 /* For changing a composing character
9900 * adjust the score from SCORE_SUBST to
9901 * SCORE_SUBCOMP. */
9902 if (enc_utf8
9903 && utf_iscomposing(
9904 mb_ptr2char(tword
9905 + sp->ts_twordlen
9906 - sp->ts_tcharlen))
9907 && utf_iscomposing(
9908 mb_ptr2char(fword
9909 + sp->ts_fcharstart)))
9910 sp->ts_score -=
9911 SCORE_SUBST - SCORE_SUBCOMP;
9912
Bram Moolenaarea424162005-06-16 21:51:00 +00009913 /* For a similar character adjust score
9914 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009915 else if (slang->sl_has_map
9916 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009917 mb_ptr2char(tword
9918 + sp->ts_twordlen
9919 - sp->ts_tcharlen),
9920 mb_ptr2char(fword
9921 + sp->ts_fcharstart)))
9922 sp->ts_score -=
9923 SCORE_SUBST - SCORE_SIMILAR;
9924 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009925 else if (sp->ts_isdiff == DIFF_INSERT
9926 && sp->ts_twordlen > sp->ts_tcharlen)
9927 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009928 p = tword + sp->ts_twordlen
9929 - sp->ts_tcharlen;
9930 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009931 if (enc_utf8 && utf_iscomposing(c))
9932 {
9933 /* Inserting a composing char doesn't
9934 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009935 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009936 - SCORE_INSCOMP;
9937 }
9938 else
9939 {
9940 /* If the previous character was the
9941 * same, thus doubling a character,
9942 * give a bonus to the score. */
9943 mb_ptr_back(tword, p);
9944 if (c == mb_ptr2char(p))
9945 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009946 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009947 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009948 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009949
9950 /* Starting a new char, reset the length. */
9951 sp->ts_tcharlen = 0;
9952 }
9953 }
9954 else
9955#endif
9956 {
9957 /* If we found a similar char adjust the score.
9958 * We do this after calling try_deeper() because
9959 * it's slow. */
9960 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009961 && slang->sl_has_map
9962 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009963 c, fword[sp->ts_fidx - 1]))
9964 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 }
9967 }
9968 break;
9969
9970 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009971#ifdef FEAT_MBYTE
9972 /* When past the first byte of a multi-byte char don't try
9973 * delete/insert/swap a character. */
9974 if (has_mbyte && sp->ts_tcharlen > 0)
9975 {
9976 sp->ts_state = STATE_FINAL;
9977 break;
9978 }
9979#endif
9980 /*
9981 * Try skipping one character in the bad word (delete it).
9982 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 sp->ts_state = STATE_INS;
9984 sp->ts_curi = 1;
9985 if (fword[sp->ts_fidx] != NUL
9986 && try_deeper(su, stack, depth, SCORE_DEL))
9987 {
9988 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009989
9990 /* Advance over the character in fword[]. Give a bonus to
9991 * the score if the same character is following "nn" ->
9992 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009993#ifdef FEAT_MBYTE
9994 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009995 {
9996 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009997 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009998 if (enc_utf8 && utf_iscomposing(c))
9999 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
10000 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +000010001 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
10002 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010003 else
10004#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000010005 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010006 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +000010007 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
10008 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
10009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 break;
10011 }
10012 /*FALLTHROUGH*/
10013
10014 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +000010015 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016 * node. */
10017 n = sp->ts_arridx;
10018 if (sp->ts_curi > byts[n])
10019 {
10020 /* Done all bytes at this node, do next state. */
10021 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010022 }
10023 else
10024 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010025 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010026 n += sp->ts_curi++;
10027 c = byts[n];
10028 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
10029 {
10030 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010031 sp = &stack[depth];
10032 tword[sp->ts_twordlen++] = c;
10033 sp->ts_arridx = idxs[n];
10034#ifdef FEAT_MBYTE
10035 if (has_mbyte)
10036 {
10037 fl = MB_BYTE2LEN(c);
10038 if (fl > 1)
10039 {
10040 /* There are following bytes for the same
10041 * character. We must find all bytes before
10042 * trying delete/insert/swap/etc. */
10043 sp->ts_tcharlen = fl;
10044 sp->ts_tcharidx = 1;
10045 sp->ts_isdiff = DIFF_INSERT;
10046 }
10047 }
Bram Moolenaarea408852005-06-25 22:49:46 +000010048 else
10049 fl = 1;
10050 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000010051#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000010052 {
10053 /* If the previous character was the same, thus
10054 * doubling a character, give a bonus to the
10055 * score. */
10056 if (sp->ts_twordlen >= 2
10057 && tword[sp->ts_twordlen - 2] == c)
10058 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
10059 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010060 }
10061 }
10062 break;
10063
10064 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +000010065 /*
10066 * Swap two bytes in the bad word: "12" -> "21".
10067 * We change "fword" here, it's changed back afterwards.
10068 */
10069 p = fword + sp->ts_fidx;
10070 c = *p;
10071 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010073 /* End of word, can't swap or replace. */
10074 sp->ts_state = STATE_FINAL;
10075 break;
10076 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010077
10078 /* Don't swap if the first character is not a word character.
10079 * SWAP3 etc. also don't make sense then. */
10080 if (!spell_iswordp(p, curbuf))
10081 {
10082 sp->ts_state = STATE_REP_INI;
10083 break;
10084 }
10085
Bram Moolenaarea424162005-06-16 21:51:00 +000010086#ifdef FEAT_MBYTE
10087 if (has_mbyte)
10088 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010089 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010090 c = mb_ptr2char(p);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010091 if (!spell_iswordp(p + n, curbuf))
10092 c2 = c; /* don't swap non-word char */
10093 else
10094 c2 = mb_ptr2char(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010095 }
10096 else
10097#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010098 {
10099 if (!spell_iswordp(p + 1, curbuf))
10100 c2 = c; /* don't swap non-word char */
10101 else
10102 c2 = p[1];
10103 }
10104
10105 /* When characters are identical, swap won't do anything.
10106 * Also get here if the second char is not a word character. */
Bram Moolenaarea424162005-06-16 21:51:00 +000010107 if (c == c2)
10108 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010109 sp->ts_state = STATE_SWAP3;
10110 break;
10111 }
10112 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
10113 {
10114 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010116#ifdef FEAT_MBYTE
10117 if (has_mbyte)
10118 {
10119 fl = mb_char2len(c2);
10120 mch_memmove(p, p + n, fl);
10121 mb_char2bytes(c, p + fl);
10122 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10123 }
10124 else
10125#endif
10126 {
10127 p[0] = c2;
10128 p[1] = c;
10129 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
10130 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 }
10132 else
10133 /* If this swap doesn't work then SWAP3 won't either. */
10134 sp->ts_state = STATE_REP_INI;
10135 break;
10136
Bram Moolenaarea424162005-06-16 21:51:00 +000010137 case STATE_UNSWAP:
10138 /* Undo the STATE_SWAP swap: "21" -> "12". */
10139 p = fword + sp->ts_fidx;
10140#ifdef FEAT_MBYTE
10141 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010143 n = MB_BYTE2LEN(*p);
10144 c = mb_ptr2char(p + n);
10145 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
10146 mb_char2bytes(c, p);
10147 }
10148 else
10149#endif
10150 {
10151 c = *p;
10152 *p = p[1];
10153 p[1] = c;
10154 }
10155 /*FALLTHROUGH*/
10156
10157 case STATE_SWAP3:
10158 /* Swap two bytes, skipping one: "123" -> "321". We change
10159 * "fword" here, it's changed back afterwards. */
10160 p = fword + sp->ts_fidx;
10161#ifdef FEAT_MBYTE
10162 if (has_mbyte)
10163 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010164 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010165 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010166 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010167 c2 = mb_ptr2char(p + n);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010168 if (!spell_iswordp(p + n + fl, curbuf))
10169 c3 = c; /* don't swap non-word char */
10170 else
10171 c3 = mb_ptr2char(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010172 }
10173 else
10174#endif
10175 {
10176 c = *p;
10177 c2 = p[1];
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010178 if (!spell_iswordp(p + 2, curbuf))
10179 c3 = c; /* don't swap non-word char */
10180 else
10181 c3 = p[2];
Bram Moolenaarea424162005-06-16 21:51:00 +000010182 }
10183
10184 /* When characters are identical: "121" then SWAP3 result is
10185 * identical, ROT3L result is same as SWAP: "211", ROT3L
10186 * result is same as SWAP on next char: "112". Thus skip all
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010187 * swapping. Also skip when c3 is NUL.
10188 * Also get here when the third character is not a word
10189 * character. Second character may any char: "a.b" -> "b.a" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010190 if (c == c3 || c3 == NUL)
10191 {
10192 sp->ts_state = STATE_REP_INI;
10193 break;
10194 }
10195 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10196 {
10197 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010199#ifdef FEAT_MBYTE
10200 if (has_mbyte)
10201 {
10202 tl = mb_char2len(c3);
10203 mch_memmove(p, p + n + fl, tl);
10204 mb_char2bytes(c2, p + tl);
10205 mb_char2bytes(c, p + fl + tl);
10206 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
10207 }
10208 else
10209#endif
10210 {
10211 p[0] = p[2];
10212 p[2] = c;
10213 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 }
10216 else
10217 sp->ts_state = STATE_REP_INI;
10218 break;
10219
Bram Moolenaarea424162005-06-16 21:51:00 +000010220 case STATE_UNSWAP3:
10221 /* Undo STATE_SWAP3: "321" -> "123" */
10222 p = fword + sp->ts_fidx;
10223#ifdef FEAT_MBYTE
10224 if (has_mbyte)
10225 {
10226 n = MB_BYTE2LEN(*p);
10227 c2 = mb_ptr2char(p + n);
10228 fl = MB_BYTE2LEN(p[n]);
10229 c = mb_ptr2char(p + n + fl);
10230 tl = MB_BYTE2LEN(p[n + fl]);
10231 mch_memmove(p + fl + tl, p, n);
10232 mb_char2bytes(c, p);
10233 mb_char2bytes(c2, p + tl);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010234 p = p + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010235 }
10236 else
10237#endif
10238 {
10239 c = *p;
10240 *p = p[2];
10241 p[2] = c;
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010242 ++p;
10243 }
10244
10245 if (!spell_iswordp(p, curbuf))
10246 {
10247 /* Middle char is not a word char, skip the rotate.
10248 * First and third char were already checked at swap
10249 * and swap3. */
10250 sp->ts_state = STATE_REP_INI;
10251 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000010252 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010253
Bram Moolenaarea424162005-06-16 21:51:00 +000010254 /* Rotate three characters left: "123" -> "231". We change
10255 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10257 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010258 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010260 p = fword + sp->ts_fidx;
10261#ifdef FEAT_MBYTE
10262 if (has_mbyte)
10263 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010264 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010265 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010266 fl = mb_cptr2len(p + n);
10267 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010268 mch_memmove(p, p + n, fl);
10269 mb_char2bytes(c, p + fl);
10270 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10271 }
10272 else
10273#endif
10274 {
10275 c = *p;
10276 *p = p[1];
10277 p[1] = p[2];
10278 p[2] = c;
10279 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10280 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 }
10282 else
10283 sp->ts_state = STATE_REP_INI;
10284 break;
10285
Bram Moolenaarea424162005-06-16 21:51:00 +000010286 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010287 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010288 p = fword + sp->ts_fidx;
10289#ifdef FEAT_MBYTE
10290 if (has_mbyte)
10291 {
10292 n = MB_BYTE2LEN(*p);
10293 n += MB_BYTE2LEN(p[n]);
10294 c = mb_ptr2char(p + n);
10295 tl = MB_BYTE2LEN(p[n]);
10296 mch_memmove(p + tl, p, n);
10297 mb_char2bytes(c, p);
10298 }
10299 else
10300#endif
10301 {
10302 c = p[2];
10303 p[2] = p[1];
10304 p[1] = *p;
10305 *p = c;
10306 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +000010309 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10311 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010312 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010313 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010314 p = fword + sp->ts_fidx;
10315#ifdef FEAT_MBYTE
10316 if (has_mbyte)
10317 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010318 n = mb_cptr2len(p);
10319 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010320 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010321 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010322 mch_memmove(p + tl, p, n);
10323 mb_char2bytes(c, p);
10324 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
10325 }
10326 else
10327#endif
10328 {
10329 c = p[2];
10330 p[2] = p[1];
10331 p[1] = *p;
10332 *p = c;
10333 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010335 }
10336 else
10337 sp->ts_state = STATE_REP_INI;
10338 break;
10339
Bram Moolenaarea424162005-06-16 21:51:00 +000010340 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010341 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010342 p = fword + sp->ts_fidx;
10343#ifdef FEAT_MBYTE
10344 if (has_mbyte)
10345 {
10346 c = mb_ptr2char(p);
10347 tl = MB_BYTE2LEN(*p);
10348 n = MB_BYTE2LEN(p[tl]);
10349 n += MB_BYTE2LEN(p[tl + n]);
10350 mch_memmove(p, p + tl, n);
10351 mb_char2bytes(c, p + n);
10352 }
10353 else
10354#endif
10355 {
10356 c = *p;
10357 *p = p[1];
10358 p[1] = p[2];
10359 p[2] = c;
10360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 /*FALLTHROUGH*/
10362
10363 case STATE_REP_INI:
10364 /* Check if matching with REP items from the .aff file would
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010365 * work. Quickly skip if:
10366 * - there are no REP items
10367 * - the score is going to be too high anyway
10368 * - already applied a REP item or swapped here */
10369 if (lp->lp_replang == NULL
10370 || sp->ts_score + SCORE_REP >= su->su_maxscore
10371 || sp->ts_fidx < sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010372 {
10373 sp->ts_state = STATE_FINAL;
10374 break;
10375 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010376 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010377
10378 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +000010379 * may match. If the index is -1 there is none. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010380 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010381 if (sp->ts_curi < 0)
10382 {
10383 sp->ts_state = STATE_FINAL;
10384 break;
10385 }
10386
10387 sp->ts_state = STATE_REP;
10388 /*FALLTHROUGH*/
10389
10390 case STATE_REP:
10391 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +000010392 * match replace the characters and check if the resulting
10393 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010394 p = fword + sp->ts_fidx;
10395
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010396 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010397 while (sp->ts_curi < gap->ga_len)
10398 {
10399 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
10400 if (*ftp->ft_from != *p)
10401 {
10402 /* past possible matching entries */
10403 sp->ts_curi = gap->ga_len;
10404 break;
10405 }
10406 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10407 && try_deeper(su, stack, depth, SCORE_REP))
10408 {
10409 /* Need to undo this afterwards. */
10410 sp->ts_state = STATE_REP_UNDO;
10411
10412 /* Change the "from" to the "to" string. */
10413 ++depth;
10414 fl = STRLEN(ftp->ft_from);
10415 tl = STRLEN(ftp->ft_to);
10416 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010419 repextra += tl - fl;
10420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 mch_memmove(p, ftp->ft_to, tl);
10422 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010423#ifdef FEAT_MBYTE
10424 stack[depth].ts_tcharlen = 0;
10425#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 break;
10427 }
10428 }
10429
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010430 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 /* No (more) matches. */
10432 sp->ts_state = STATE_FINAL;
10433
10434 break;
10435
10436 case STATE_REP_UNDO:
10437 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010438 ftp = (fromto_T *)lp->lp_replang->sl_rep.ga_data
10439 + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 fl = STRLEN(ftp->ft_from);
10441 tl = STRLEN(ftp->ft_to);
10442 p = fword + sp->ts_fidx;
10443 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010446 repextra -= tl - fl;
10447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 mch_memmove(p, ftp->ft_from, fl);
10449 sp->ts_state = STATE_REP;
10450 break;
10451
10452 default:
10453 /* Did all possible states at this level, go up one level. */
10454 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010456 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010457 {
10458 /* Continue in or go back to the prefix tree. */
10459 byts = pbyts;
10460 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010461 }
10462
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010463 /* Don't check for CTRL-C too often, it takes time. */
10464 line_breakcheck();
10465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 }
10467 }
10468}
10469
10470/*
10471 * Try going one level deeper in the tree.
10472 */
10473 static int
10474try_deeper(su, stack, depth, score_add)
10475 suginfo_T *su;
10476 trystate_T *stack;
10477 int depth;
10478 int score_add;
10479{
10480 int newscore;
10481
10482 /* Refuse to go deeper if the scrore is getting too big. */
10483 newscore = stack[depth].ts_score + score_add;
10484 if (newscore >= su->su_maxscore)
10485 return FALSE;
10486
Bram Moolenaarea424162005-06-16 21:51:00 +000010487 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 stack[depth + 1].ts_state = STATE_START;
10489 stack[depth + 1].ts_score = newscore;
10490 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010491 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010492 return TRUE;
10493}
10494
Bram Moolenaar53805d12005-08-01 07:08:33 +000010495#ifdef FEAT_MBYTE
10496/*
10497 * Case-folding may change the number of bytes: Count nr of chars in
10498 * fword[flen] and return the byte length of that many chars in "word".
10499 */
10500 static int
10501nofold_len(fword, flen, word)
10502 char_u *fword;
10503 int flen;
10504 char_u *word;
10505{
10506 char_u *p;
10507 int i = 0;
10508
10509 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10510 ++i;
10511 for (p = word; i > 0; mb_ptr_adv(p))
10512 --i;
10513 return (int)(p - word);
10514}
10515#endif
10516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517/*
10518 * "fword" is a good word with case folded. Find the matching keep-case
10519 * words and put it in "kword".
10520 * Theoretically there could be several keep-case words that result in the
10521 * same case-folded word, but we only find one...
10522 */
10523 static void
10524find_keepcap_word(slang, fword, kword)
10525 slang_T *slang;
10526 char_u *fword;
10527 char_u *kword;
10528{
10529 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10530 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010531 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532
10533 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010534 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 int round[MAXWLEN];
10536 int fwordidx[MAXWLEN];
10537 int uwordidx[MAXWLEN];
10538 int kwordlen[MAXWLEN];
10539
10540 int flen, ulen;
10541 int l;
10542 int len;
10543 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010544 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 char_u *p;
10546 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010547 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548
10549 if (byts == NULL)
10550 {
10551 /* array is empty: "cannot happen" */
10552 *kword = NUL;
10553 return;
10554 }
10555
10556 /* Make an all-cap version of "fword". */
10557 allcap_copy(fword, uword);
10558
10559 /*
10560 * Each character needs to be tried both case-folded and upper-case.
10561 * All this gets very complicated if we keep in mind that changing case
10562 * may change the byte length of a multi-byte character...
10563 */
10564 depth = 0;
10565 arridx[0] = 0;
10566 round[0] = 0;
10567 fwordidx[0] = 0;
10568 uwordidx[0] = 0;
10569 kwordlen[0] = 0;
10570 while (depth >= 0)
10571 {
10572 if (fword[fwordidx[depth]] == NUL)
10573 {
10574 /* We are at the end of "fword". If the tree allows a word to end
10575 * here we have found a match. */
10576 if (byts[arridx[depth] + 1] == 0)
10577 {
10578 kword[kwordlen[depth]] = NUL;
10579 return;
10580 }
10581
10582 /* kword is getting too long, continue one level up */
10583 --depth;
10584 }
10585 else if (++round[depth] > 2)
10586 {
10587 /* tried both fold-case and upper-case character, continue one
10588 * level up */
10589 --depth;
10590 }
10591 else
10592 {
10593 /*
10594 * round[depth] == 1: Try using the folded-case character.
10595 * round[depth] == 2: Try using the upper-case character.
10596 */
10597#ifdef FEAT_MBYTE
10598 if (has_mbyte)
10599 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010600 flen = mb_cptr2len(fword + fwordidx[depth]);
10601 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 }
10603 else
10604#endif
10605 ulen = flen = 1;
10606 if (round[depth] == 1)
10607 {
10608 p = fword + fwordidx[depth];
10609 l = flen;
10610 }
10611 else
10612 {
10613 p = uword + uwordidx[depth];
10614 l = ulen;
10615 }
10616
10617 for (tryidx = arridx[depth]; l > 0; --l)
10618 {
10619 /* Perform a binary search in the list of accepted bytes. */
10620 len = byts[tryidx++];
10621 c = *p++;
10622 lo = tryidx;
10623 hi = tryidx + len - 1;
10624 while (lo < hi)
10625 {
10626 m = (lo + hi) / 2;
10627 if (byts[m] > c)
10628 hi = m - 1;
10629 else if (byts[m] < c)
10630 lo = m + 1;
10631 else
10632 {
10633 lo = hi = m;
10634 break;
10635 }
10636 }
10637
10638 /* Stop if there is no matching byte. */
10639 if (hi < lo || byts[lo] != c)
10640 break;
10641
10642 /* Continue at the child (if there is one). */
10643 tryidx = idxs[lo];
10644 }
10645
10646 if (l == 0)
10647 {
10648 /*
10649 * Found the matching char. Copy it to "kword" and go a
10650 * level deeper.
10651 */
10652 if (round[depth] == 1)
10653 {
10654 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10655 flen);
10656 kwordlen[depth + 1] = kwordlen[depth] + flen;
10657 }
10658 else
10659 {
10660 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10661 ulen);
10662 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10663 }
10664 fwordidx[depth + 1] = fwordidx[depth] + flen;
10665 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10666
10667 ++depth;
10668 arridx[depth] = tryidx;
10669 round[depth] = 0;
10670 }
10671 }
10672 }
10673
10674 /* Didn't find it: "cannot happen". */
10675 *kword = NUL;
10676}
10677
10678/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010679 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10680 * su->su_sga.
10681 */
10682 static void
10683score_comp_sal(su)
10684 suginfo_T *su;
10685{
10686 langp_T *lp;
10687 char_u badsound[MAXWLEN];
10688 int i;
10689 suggest_T *stp;
10690 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010691 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010692 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010693
10694 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10695 return;
10696
10697 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010698 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010699 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010700 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010701 if (lp->lp_slang->sl_sal.ga_len > 0)
10702 {
10703 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010704 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010705
10706 for (i = 0; i < su->su_ga.ga_len; ++i)
10707 {
10708 stp = &SUG(su->su_ga, i);
10709
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010710 /* Case-fold the suggested word, sound-fold it and compute the
10711 * sound-a-like score. */
10712 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010713 if (score < SCORE_MAXMAX)
10714 {
10715 /* Add the suggestion. */
10716 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10717 sstp->st_word = vim_strsave(stp->st_word);
10718 if (sstp->st_word != NULL)
10719 {
10720 sstp->st_score = score;
10721 sstp->st_altscore = 0;
10722 sstp->st_orglen = stp->st_orglen;
10723 ++su->su_sga.ga_len;
10724 }
10725 }
10726 }
10727 break;
10728 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010729 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010730}
10731
10732/*
10733 * Combine the list of suggestions in su->su_ga and su->su_sga.
10734 * They are intwined.
10735 */
10736 static void
10737score_combine(su)
10738 suginfo_T *su;
10739{
10740 int i;
10741 int j;
10742 garray_T ga;
10743 garray_T *gap;
10744 langp_T *lp;
10745 suggest_T *stp;
10746 char_u *p;
10747 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010748 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010749 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010750
10751 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010752 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010753 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010754 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010755 if (lp->lp_slang->sl_sal.ga_len > 0)
10756 {
10757 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010758 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010759
10760 for (i = 0; i < su->su_ga.ga_len; ++i)
10761 {
10762 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010763 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10764 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010765 if (stp->st_altscore == SCORE_MAXMAX)
10766 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10767 else
10768 stp->st_score = (stp->st_score * 3
10769 + stp->st_altscore) / 4;
10770 stp->st_salscore = FALSE;
10771 }
10772 break;
10773 }
10774 }
10775
10776 /* Add the alternate score to su_sga. */
10777 for (i = 0; i < su->su_sga.ga_len; ++i)
10778 {
10779 stp = &SUG(su->su_sga, i);
10780 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10781 if (stp->st_score == SCORE_MAXMAX)
10782 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10783 else
10784 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10785 stp->st_salscore = TRUE;
10786 }
10787
10788 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10789 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10790 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10791
10792 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10793 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10794 return;
10795
10796 stp = &SUG(ga, 0);
10797 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10798 {
10799 /* round 1: get a suggestion from su_ga
10800 * round 2: get a suggestion from su_sga */
10801 for (round = 1; round <= 2; ++round)
10802 {
10803 gap = round == 1 ? &su->su_ga : &su->su_sga;
10804 if (i < gap->ga_len)
10805 {
10806 /* Don't add a word if it's already there. */
10807 p = SUG(*gap, i).st_word;
10808 for (j = 0; j < ga.ga_len; ++j)
10809 if (STRCMP(stp[j].st_word, p) == 0)
10810 break;
10811 if (j == ga.ga_len)
10812 stp[ga.ga_len++] = SUG(*gap, i);
10813 else
10814 vim_free(p);
10815 }
10816 }
10817 }
10818
10819 ga_clear(&su->su_ga);
10820 ga_clear(&su->su_sga);
10821
10822 /* Truncate the list to the number of suggestions that will be displayed. */
10823 if (ga.ga_len > su->su_maxcount)
10824 {
10825 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10826 vim_free(stp[i].st_word);
10827 ga.ga_len = su->su_maxcount;
10828 }
10829
10830 su->su_ga = ga;
10831}
10832
10833/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010834 * For the goodword in "stp" compute the soundalike score compared to the
10835 * badword.
10836 */
10837 static int
10838stp_sal_score(stp, su, slang, badsound)
10839 suggest_T *stp;
10840 suginfo_T *su;
10841 slang_T *slang;
10842 char_u *badsound; /* sound-folded badword */
10843{
10844 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010845 char_u *pbad;
10846 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010847 char_u badsound2[MAXWLEN];
10848 char_u fword[MAXWLEN];
10849 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010850 char_u goodword[MAXWLEN];
10851 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010852
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010853 lendiff = (int)(su->su_badlen - stp->st_orglen);
10854 if (lendiff >= 0)
10855 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010856 else
10857 {
10858 /* soundfold the bad word with more characters following */
10859 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10860
10861 /* When joining two words the sound often changes a lot. E.g., "t he"
10862 * sounds like "t h" while "the" sounds like "@". Avoid that by
10863 * removing the space. Don't do it when the good word also contains a
10864 * space. */
10865 if (vim_iswhite(su->su_badptr[su->su_badlen])
10866 && *skiptowhite(stp->st_word) == NUL)
10867 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10868 mch_memmove(p, p + 1, STRLEN(p));
10869
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010870 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010871 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010872 }
10873
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010874 if (lendiff > 0)
10875 {
10876 /* Add part of the bad word to the good word, so that we soundfold
10877 * what replaces the bad word. */
10878 STRCPY(goodword, stp->st_word);
10879 STRNCAT(goodword, su->su_badptr + su->su_badlen - lendiff, lendiff);
10880 pgood = goodword;
10881 }
10882 else
10883 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010884
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010885 /* Sound-fold the word and compute the score for the difference. */
10886 spell_soundfold(slang, pgood, FALSE, goodsound);
10887
10888 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010889}
10890
10891/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010893 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 */
10895 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010896suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 suginfo_T *su;
10898{
10899 char_u salword[MAXWLEN];
10900 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010902 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 int curi[MAXWLEN];
10904 langp_T *lp;
10905 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010906 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 int depth;
10908 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010909 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 int round;
10911 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010912 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010913 int local_score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010914 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010915 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010917 /* Do this for all languages that support sound folding. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010918 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010920 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10921 slang = lp->lp_slang;
10922 if (slang->sl_sal.ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 {
10924 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010925 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926
10927 /*
10928 * Go through the whole tree, soundfold each word and compare.
10929 * round 1: use the case-folded tree.
10930 * round 2: use the keep-case tree.
10931 */
10932 for (round = 1; round <= 2; ++round)
10933 {
10934 if (round == 1)
10935 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010936 byts = slang->sl_fbyts;
10937 idxs = slang->sl_fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 }
10939 else
10940 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010941 byts = slang->sl_kbyts;
10942 idxs = slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010943 if (byts == NULL) /* no keep-case words */
10944 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 }
10946
10947 depth = 0;
10948 arridx[0] = 0;
10949 curi[0] = 1;
10950 while (depth >= 0 && !got_int)
10951 {
10952 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 /* Done all bytes at this node, go up one level. */
10955 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010956 line_breakcheck();
10957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 else
10959 {
10960 /* Do one more byte at this node. */
10961 n = arridx[depth] + curi[depth];
10962 ++curi[depth];
10963 c = byts[n];
10964 if (c == 0)
10965 {
10966 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010967 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10969 {
10970 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010971 /* Sound-fold. Only in keep-case tree need to
10972 * case-fold the word. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010973 spell_soundfold(slang, tword,
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010974 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010976 /* Compute the edit distance between the
10977 * sound-a-like words. */
10978 sound_score = soundalike_score(salword,
10979 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010980
10981 /* Add a penalty for words in another region. */
10982 if ((flags & WF_REGION) && (((unsigned)flags
10983 >> 16) & lp->lp_region) == 0)
10984 local_score = SCORE_REGION;
10985 else
10986 local_score = 0;
10987 sound_score += local_score;
10988
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010989 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010990 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010991 char_u cword[MAXWLEN];
10992 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010993 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010994
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010995 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010996 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010998 /* Need to fix case according to
10999 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011000 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011001 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 }
11003 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011004 p = tword;
11005
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011006 if (sps_flags & SPS_DOUBLE)
11007 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011008 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011009 sound_score, 0, FALSE,
11010 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011011 else
11012 {
11013 /* Compute the score. */
11014 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000011015 su->su_badword, p)
11016 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011017 if (sps_flags & SPS_BEST)
11018 /* give a bonus for the good word
11019 * sounding the same as the bad
11020 * word */
11021 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011022 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011023 RESCORE(score, sound_score),
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011024 sound_score, TRUE,
11025 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011026 else
11027 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011028 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011029 score + sound_score,
11030 0, FALSE,
11031 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011033 }
11034 }
11035
11036 /* Skip over other NUL bytes. */
11037 while (byts[n + 1] == 0)
11038 {
11039 ++n;
11040 ++curi[depth];
11041 }
11042 }
11043 else
11044 {
11045 /* Normal char, go one level deeper. */
11046 tword[depth++] = c;
11047 arridx[depth] = idxs[n];
11048 curi[depth] = 1;
11049 }
11050 }
11051 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 }
11053 }
11054 }
11055}
11056
11057/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011058 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 */
11060 static void
11061make_case_word(fword, cword, flags)
11062 char_u *fword;
11063 char_u *cword;
11064 int flags;
11065{
11066 if (flags & WF_ALLCAP)
11067 /* Make it all upper-case */
11068 allcap_copy(fword, cword);
11069 else if (flags & WF_ONECAP)
11070 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011071 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 else
11073 /* Use goodword as-is. */
11074 STRCPY(cword, fword);
11075}
11076
Bram Moolenaarea424162005-06-16 21:51:00 +000011077/*
11078 * Use map string "map" for languages "lp".
11079 */
11080 static void
11081set_map_str(lp, map)
11082 slang_T *lp;
11083 char_u *map;
11084{
11085 char_u *p;
11086 int headc = 0;
11087 int c;
11088 int i;
11089
11090 if (*map == NUL)
11091 {
11092 lp->sl_has_map = FALSE;
11093 return;
11094 }
11095 lp->sl_has_map = TRUE;
11096
11097 /* Init the array and hash table empty. */
11098 for (i = 0; i < 256; ++i)
11099 lp->sl_map_array[i] = 0;
11100#ifdef FEAT_MBYTE
11101 hash_init(&lp->sl_map_hash);
11102#endif
11103
11104 /*
11105 * The similar characters are stored separated with slashes:
11106 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
11107 * before the same slash. For characters above 255 sl_map_hash is used.
11108 */
11109 for (p = map; *p != NUL; )
11110 {
11111#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011112 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011113#else
11114 c = *p++;
11115#endif
11116 if (c == '/')
11117 headc = 0;
11118 else
11119 {
11120 if (headc == 0)
11121 headc = c;
11122
11123#ifdef FEAT_MBYTE
11124 /* Characters above 255 don't fit in sl_map_array[], put them in
11125 * the hash table. Each entry is the char, a NUL the headchar and
11126 * a NUL. */
11127 if (c >= 256)
11128 {
11129 int cl = mb_char2len(c);
11130 int headcl = mb_char2len(headc);
11131 char_u *b;
11132 hash_T hash;
11133 hashitem_T *hi;
11134
11135 b = alloc((unsigned)(cl + headcl + 2));
11136 if (b == NULL)
11137 return;
11138 mb_char2bytes(c, b);
11139 b[cl] = NUL;
11140 mb_char2bytes(headc, b + cl + 1);
11141 b[cl + 1 + headcl] = NUL;
11142 hash = hash_hash(b);
11143 hi = hash_lookup(&lp->sl_map_hash, b, hash);
11144 if (HASHITEM_EMPTY(hi))
11145 hash_add_item(&lp->sl_map_hash, hi, b, hash);
11146 else
11147 {
11148 /* This should have been checked when generating the .spl
11149 * file. */
11150 EMSG(_("E999: duplicate char in MAP entry"));
11151 vim_free(b);
11152 }
11153 }
11154 else
11155#endif
11156 lp->sl_map_array[c] = headc;
11157 }
11158 }
11159}
11160
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011161/*
11162 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
11163 * lines in the .aff file.
11164 */
11165 static int
11166similar_chars(slang, c1, c2)
11167 slang_T *slang;
11168 int c1;
11169 int c2;
11170{
Bram Moolenaarea424162005-06-16 21:51:00 +000011171 int m1, m2;
11172#ifdef FEAT_MBYTE
11173 char_u buf[MB_MAXBYTES];
11174 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175
Bram Moolenaarea424162005-06-16 21:51:00 +000011176 if (c1 >= 256)
11177 {
11178 buf[mb_char2bytes(c1, buf)] = 0;
11179 hi = hash_find(&slang->sl_map_hash, buf);
11180 if (HASHITEM_EMPTY(hi))
11181 m1 = 0;
11182 else
11183 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11184 }
11185 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011186#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000011187 m1 = slang->sl_map_array[c1];
11188 if (m1 == 0)
11189 return FALSE;
11190
11191
11192#ifdef FEAT_MBYTE
11193 if (c2 >= 256)
11194 {
11195 buf[mb_char2bytes(c2, buf)] = 0;
11196 hi = hash_find(&slang->sl_map_hash, buf);
11197 if (HASHITEM_EMPTY(hi))
11198 m2 = 0;
11199 else
11200 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11201 }
11202 else
11203#endif
11204 m2 = slang->sl_map_array[c2];
11205
11206 return m1 == m2;
11207}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208
11209/*
11210 * Add a suggestion to the list of suggestions.
11211 * Do not add a duplicate suggestion or suggestions with a bad score.
11212 * When "use_score" is not zero it's used, otherwise the score is computed
11213 * with spell_edit_score().
11214 */
11215 static void
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011216add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus, slang)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011218 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011219 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011220 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011221 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011222 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011223 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011224 slang_T *slang; /* language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011226 int goodlen = STRLEN(goodword); /* len of goodword changed */
11227 int badlen = badlenarg; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011229 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011231 hlf_T attr = HLF_COUNT;
Bram Moolenaar1e015462005-09-25 22:16:38 +000011232 char_u longword[MAXWLEN + 1];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011233 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234
Bram Moolenaar1e015462005-09-25 22:16:38 +000011235 /* Check that the word really is valid. Esp. for banned words and for
11236 * split words, such as "the the". Need to append what follows to check
11237 * for that. */
11238 STRCPY(longword, goodword);
11239 vim_strncpy(longword + goodlen, su->su_badptr + badlen, MAXWLEN - goodlen);
11240 (void)spell_check(curwin, longword, &attr, NULL);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011241 if (attr != HLF_COUNT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011242 return;
11243
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011244 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
11245 * "thee the" is added next to changing the first "the" the "thee". */
11246 pgood = goodword + STRLEN(goodword);
11247 pbad = su->su_badptr + badlen;
11248 while (pgood > goodword && pbad > su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011249 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011250 mb_ptr_back(goodword, pgood);
11251 mb_ptr_back(su->su_badptr, pbad);
11252#ifdef FEAT_MBYTE
11253 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011254 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011255 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
11256 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011257 }
11258 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011259#endif
11260 if (*pgood != *pbad)
11261 break;
11262 badlen = pbad - su->su_badptr;
11263 goodlen = pgood - goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011264 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011265 if (badlen == 0 && goodlen == 0)
11266 /* goodword doesn't change anything; may happen for "the the" changing
11267 * the first "the" to itself. */
11268 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011270 if (score <= su->su_maxscore)
11271 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011272 /* Check if the word is already there. Also check the length that is
11273 * being replaced "thes," -> "these" is a different suggestion from
11274 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011275 stp = &SUG(*gap, 0);
11276 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011277 if ((int)STRLEN(stp[i].st_word) == goodlen
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011278 && STRNCMP(stp[i].st_word, goodword, goodlen) == 0
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011279 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011280 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011281 /*
Bram Moolenaar4effc802005-09-30 21:12:02 +000011282 * Found it. Remember the word with the lowest score.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011283 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011284 if (stp[i].st_slang == NULL)
11285 stp[i].st_slang = slang;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011286
11287 new_sug.st_score = score;
11288 new_sug.st_altscore = altscore;
11289 new_sug.st_had_bonus = had_bonus;
11290
11291 if (stp[i].st_had_bonus != had_bonus)
11292 {
11293 /* Only one of the two had the soundalike score computed.
11294 * Need to do that for the other one now, otherwise the
11295 * scores can't be compared. This happens because
11296 * suggest_try_change() doesn't compute the soundalike
Bram Moolenaar4effc802005-09-30 21:12:02 +000011297 * word to keep it fast, while some special methods set
11298 * the soundalike score to zero. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011299 if (had_bonus)
11300 rescore_one(su, &stp[i]);
11301 else
11302 {
11303 new_sug.st_word = goodword;
11304 new_sug.st_slang = stp[i].st_slang;
11305 new_sug.st_orglen = badlen;
11306 rescore_one(su, &new_sug);
11307 }
11308 }
11309
11310 if (stp[i].st_score > new_sug.st_score)
11311 {
11312 stp[i].st_score = new_sug.st_score;
11313 stp[i].st_altscore = new_sug.st_altscore;
11314 stp[i].st_had_bonus = new_sug.st_had_bonus;
11315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 break;
11317 }
11318
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011319 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 {
11321 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011322 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011323 stp->st_word = vim_strnsave(goodword, goodlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 if (stp->st_word != NULL)
11325 {
11326 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011327 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011328 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011329 stp->st_orglen = badlen;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011330 stp->st_slang = slang;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011331 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332
11333 /* If we have too many suggestions now, sort the list and keep
11334 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011335 if (gap->ga_len > SUG_MAX_COUNT(su))
11336 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
11337 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 }
11339 }
11340 }
11341}
11342
11343/*
11344 * Add a word to be banned.
11345 */
11346 static void
11347add_banned(su, word)
11348 suginfo_T *su;
11349 char_u *word;
11350{
11351 char_u *s = vim_strsave(word);
11352 hash_T hash;
11353 hashitem_T *hi;
11354
11355 if (s != NULL)
11356 {
11357 hash = hash_hash(s);
11358 hi = hash_lookup(&su->su_banned, s, hash);
11359 if (HASHITEM_EMPTY(hi))
11360 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011361 else
11362 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 }
11364}
11365
11366/*
11367 * Return TRUE if a word appears in the list of banned words.
11368 */
11369 static int
11370was_banned(su, word)
11371 suginfo_T *su;
11372 char_u *word;
11373{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011374 hashitem_T *hi = hash_find(&su->su_banned, word);
11375
11376 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377}
11378
11379/*
11380 * Free the banned words in "su".
11381 */
11382 static void
11383free_banned(su)
11384 suginfo_T *su;
11385{
11386 int todo;
11387 hashitem_T *hi;
11388
11389 todo = su->su_banned.ht_used;
11390 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
11391 {
11392 if (!HASHITEM_EMPTY(hi))
11393 {
11394 vim_free(hi->hi_key);
11395 --todo;
11396 }
11397 }
11398 hash_clear(&su->su_banned);
11399}
11400
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011401/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011402 * Recompute the score for all suggestions if sound-folding is possible. This
11403 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011404 */
11405 static void
11406rescore_suggestions(su)
11407 suginfo_T *su;
11408{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011409 int i;
11410
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011411 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011412 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011413 rescore_one(su, &SUG(su->su_ga, i));
11414}
11415
11416/*
11417 * Recompute the score for one suggestion if sound-folding is possible.
11418 */
11419 static void
11420rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011421 suginfo_T *su;
11422 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011423{
11424 slang_T *slang = stp->st_slang;
11425 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000011426 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011427
11428 /* Only rescore suggestions that have no sal score yet and do have a
11429 * language. */
11430 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
11431 {
11432 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011433 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011434 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011435 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011436 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000011437 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011438 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000011439
11440 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011441 if (stp->st_altscore == SCORE_MAXMAX)
11442 stp->st_altscore = SCORE_BIG;
11443 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
11444 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011445 }
11446}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448static int
11449#ifdef __BORLANDC__
11450_RTLENTRYF
11451#endif
11452sug_compare __ARGS((const void *s1, const void *s2));
11453
11454/*
11455 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011456 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 */
11458 static int
11459#ifdef __BORLANDC__
11460_RTLENTRYF
11461#endif
11462sug_compare(s1, s2)
11463 const void *s1;
11464 const void *s2;
11465{
11466 suggest_T *p1 = (suggest_T *)s1;
11467 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011468 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011470 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011471 {
11472 n = p1->st_altscore - p2->st_altscore;
11473 if (n == 0)
11474 n = STRICMP(p1->st_word, p2->st_word);
11475 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011476 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477}
11478
11479/*
11480 * Cleanup the suggestions:
11481 * - Sort on score.
11482 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011483 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011485 static int
11486cleanup_suggestions(gap, maxscore, keep)
11487 garray_T *gap;
11488 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011489 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011491 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 int i;
11493
11494 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011495 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496
11497 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011498 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011499 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011500 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011502 gap->ga_len = keep;
11503 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011505 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506}
11507
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011508#if defined(FEAT_EVAL) || defined(PROTO)
11509/*
11510 * Soundfold a string, for soundfold().
11511 * Result is in allocated memory, NULL for an error.
11512 */
11513 char_u *
11514eval_soundfold(word)
11515 char_u *word;
11516{
11517 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011518 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011519 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011520
11521 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11522 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011523 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011524 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011525 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011526 if (lp->lp_slang->sl_sal.ga_len > 0)
11527 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011528 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011529 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011530 return vim_strsave(sound);
11531 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011532 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011533
11534 /* No language with sound folding, return word as-is. */
11535 return vim_strsave(word);
11536}
11537#endif
11538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539/*
11540 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011541 *
11542 * There are many ways to turn a word into a sound-a-like representation. The
11543 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11544 * swedish name matching - survey and test of different algorithms" by Klas
11545 * Erikson.
11546 *
11547 * We support two methods:
11548 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11549 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 */
11551 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011552spell_soundfold(slang, inword, folded, res)
11553 slang_T *slang;
11554 char_u *inword;
11555 int folded; /* "inword" is already case-folded */
11556 char_u *res;
11557{
11558 char_u fword[MAXWLEN];
11559 char_u *word;
11560
11561 if (slang->sl_sofo)
11562 /* SOFOFROM and SOFOTO used */
11563 spell_soundfold_sofo(slang, inword, res);
11564 else
11565 {
11566 /* SAL items used. Requires the word to be case-folded. */
11567 if (folded)
11568 word = inword;
11569 else
11570 {
11571 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11572 word = fword;
11573 }
11574
11575#ifdef FEAT_MBYTE
11576 if (has_mbyte)
11577 spell_soundfold_wsal(slang, word, res);
11578 else
11579#endif
11580 spell_soundfold_sal(slang, word, res);
11581 }
11582}
11583
11584/*
11585 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11586 * SOFOTO lines.
11587 */
11588 static void
11589spell_soundfold_sofo(slang, inword, res)
11590 slang_T *slang;
11591 char_u *inword;
11592 char_u *res;
11593{
11594 char_u *s;
11595 int ri = 0;
11596 int c;
11597
11598#ifdef FEAT_MBYTE
11599 if (has_mbyte)
11600 {
11601 int prevc = 0;
11602 int *ip;
11603
11604 /* The sl_sal_first[] table contains the translation for chars up to
11605 * 255, sl_sal the rest. */
11606 for (s = inword; *s != NUL; )
11607 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011608 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011609 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11610 c = ' ';
11611 else if (c < 256)
11612 c = slang->sl_sal_first[c];
11613 else
11614 {
11615 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11616 if (ip == NULL) /* empty list, can't match */
11617 c = NUL;
11618 else
11619 for (;;) /* find "c" in the list */
11620 {
11621 if (*ip == 0) /* not found */
11622 {
11623 c = NUL;
11624 break;
11625 }
11626 if (*ip == c) /* match! */
11627 {
11628 c = ip[1];
11629 break;
11630 }
11631 ip += 2;
11632 }
11633 }
11634
11635 if (c != NUL && c != prevc)
11636 {
11637 ri += mb_char2bytes(c, res + ri);
11638 if (ri + MB_MAXBYTES > MAXWLEN)
11639 break;
11640 prevc = c;
11641 }
11642 }
11643 }
11644 else
11645#endif
11646 {
11647 /* The sl_sal_first[] table contains the translation. */
11648 for (s = inword; (c = *s) != NUL; ++s)
11649 {
11650 if (vim_iswhite(c))
11651 c = ' ';
11652 else
11653 c = slang->sl_sal_first[c];
11654 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11655 res[ri++] = c;
11656 }
11657 }
11658
11659 res[ri] = NUL;
11660}
11661
11662 static void
11663spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 slang_T *slang;
11665 char_u *inword;
11666 char_u *res;
11667{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011668 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011669 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011670 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011671 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011672 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011673 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011674 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 int n, k = 0;
11676 int z0;
11677 int k0;
11678 int n0;
11679 int c;
11680 int pri;
11681 int p0 = -333;
11682 int c0;
11683
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011684 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011685 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 if (slang->sl_rem_accents)
11687 {
11688 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011689 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011691 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011692 {
11693 *t++ = ' ';
11694 s = skipwhite(s);
11695 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011696 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011698 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 *t++ = *s;
11700 ++s;
11701 }
11702 }
11703 *t = NUL;
11704 }
11705 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011706 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011708 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709
11710 /*
11711 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011712 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011714 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011715 while ((c = word[i]) != NUL)
11716 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011717 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011718 n = slang->sl_sal_first[c];
11719 z0 = 0;
11720
11721 if (n >= 0)
11722 {
11723 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011724 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011726 /* Quickly skip entries that don't match the word. Most
11727 * entries are less then three chars, optimize for that. */
11728 k = smp[n].sm_leadlen;
11729 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011731 if (word[i + 1] != s[1])
11732 continue;
11733 if (k > 2)
11734 {
11735 for (j = 2; j < k; ++j)
11736 if (word[i + j] != s[j])
11737 break;
11738 if (j < k)
11739 continue;
11740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 }
11742
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011743 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011745 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011746 while (*pf != NUL && *pf != word[i + k])
11747 ++pf;
11748 if (*pf == NUL)
11749 continue;
11750 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011751 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011752 s = smp[n].sm_rules;
11753 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754
11755 p0 = *s;
11756 k0 = k;
11757 while (*s == '-' && k > 1)
11758 {
11759 k--;
11760 s++;
11761 }
11762 if (*s == '<')
11763 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011764 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 {
11766 /* determine priority */
11767 pri = *s - '0';
11768 s++;
11769 }
11770 if (*s == '^' && *(s + 1) == '^')
11771 s++;
11772
11773 if (*s == NUL
11774 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011775 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011776 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011778 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011780 && spell_iswordp(word + i - 1, curbuf)
11781 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 {
11783 /* search for followup rules, if: */
11784 /* followup and k > 1 and NO '-' in searchstring */
11785 c0 = word[i + k - 1];
11786 n0 = slang->sl_sal_first[c0];
11787
11788 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011789 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 {
11791 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011792 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011794 /* Quickly skip entries that don't match the word.
11795 * */
11796 k0 = smp[n0].sm_leadlen;
11797 if (k0 > 1)
11798 {
11799 if (word[i + k] != s[1])
11800 continue;
11801 if (k0 > 2)
11802 {
11803 pf = word + i + k + 1;
11804 for (j = 2; j < k0; ++j)
11805 if (*pf++ != s[j])
11806 break;
11807 if (j < k0)
11808 continue;
11809 }
11810 }
11811 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011813 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011814 {
11815 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011816 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011817 while (*pf != NUL && *pf != word[i + k0])
11818 ++pf;
11819 if (*pf == NUL)
11820 continue;
11821 ++k0;
11822 }
11823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011825 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 while (*s == '-')
11827 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011828 /* "k0" gets NOT reduced because
11829 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011830 s++;
11831 }
11832 if (*s == '<')
11833 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011834 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011835 {
11836 p0 = *s - '0';
11837 s++;
11838 }
11839
11840 if (*s == NUL
11841 /* *s == '^' cuts */
11842 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011843 && !spell_iswordp(word + i + k0,
11844 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 {
11846 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849
11850 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 /* rule fits; stop search */
11854 break;
11855 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 }
11857
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011858 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 }
11861
11862 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011863 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011864 if (s == NULL)
11865 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011866 pf = smp[n].sm_rules;
11867 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 if (p0 == 1 && z == 0)
11869 {
11870 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011871 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11872 || res[reslen - 1] == *s))
11873 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 z0 = 1;
11875 z = 1;
11876 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011877 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011878 {
11879 word[i + k0] = *s;
11880 k0++;
11881 s++;
11882 }
11883 if (k > k0)
11884 mch_memmove(word + i + k0, word + i + k,
11885 STRLEN(word + i + k) + 1);
11886
11887 /* new "actual letter" */
11888 c = word[i];
11889 }
11890 else
11891 {
11892 /* no '<' rule used */
11893 i += k - 1;
11894 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011895 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011896 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011897 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011898 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011899 s++;
11900 }
11901 /* new "actual letter" */
11902 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011903 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 {
11905 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011906 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 mch_memmove(word, word + i + 1,
11908 STRLEN(word + i + 1) + 1);
11909 i = 0;
11910 z0 = 1;
11911 }
11912 }
11913 break;
11914 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011915 }
11916 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011917 else if (vim_iswhite(c))
11918 {
11919 c = ' ';
11920 k = 1;
11921 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011922
11923 if (z0 == 0)
11924 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011925 if (k && !p0 && reslen < MAXWLEN && c != NUL
11926 && (!slang->sl_collapse || reslen == 0
11927 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011929 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930
11931 i++;
11932 z = 0;
11933 k = 0;
11934 }
11935 }
11936
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011937 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938}
11939
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011940#ifdef FEAT_MBYTE
11941/*
11942 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11943 * Multi-byte version of spell_soundfold().
11944 */
11945 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011946spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011947 slang_T *slang;
11948 char_u *inword;
11949 char_u *res;
11950{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011951 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011952 int word[MAXWLEN];
11953 int wres[MAXWLEN];
11954 int l;
11955 char_u *s;
11956 int *ws;
11957 char_u *t;
11958 int *pf;
11959 int i, j, z;
11960 int reslen;
11961 int n, k = 0;
11962 int z0;
11963 int k0;
11964 int n0;
11965 int c;
11966 int pri;
11967 int p0 = -333;
11968 int c0;
11969 int did_white = FALSE;
11970
11971 /*
11972 * Convert the multi-byte string to a wide-character string.
11973 * Remove accents, if wanted. We actually remove all non-word characters.
11974 * But keep white space.
11975 */
11976 n = 0;
11977 for (s = inword; *s != NUL; )
11978 {
11979 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011980 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011981 if (slang->sl_rem_accents)
11982 {
11983 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11984 {
11985 if (did_white)
11986 continue;
11987 c = ' ';
11988 did_white = TRUE;
11989 }
11990 else
11991 {
11992 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011993 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011994 continue;
11995 }
11996 }
11997 word[n++] = c;
11998 }
11999 word[n] = NUL;
12000
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012001 /*
12002 * This comes from Aspell phonet.cpp.
12003 * Converted from C++ to C. Added support for multi-byte chars.
12004 * Changed to keep spaces.
12005 */
12006 i = reslen = z = 0;
12007 while ((c = word[i]) != NUL)
12008 {
12009 /* Start with the first rule that has the character in the word. */
12010 n = slang->sl_sal_first[c & 0xff];
12011 z0 = 0;
12012
12013 if (n >= 0)
12014 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012015 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012016 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
12017 {
12018 /* Quickly skip entries that don't match the word. Most
12019 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012020 if (c != ws[0])
12021 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012022 k = smp[n].sm_leadlen;
12023 if (k > 1)
12024 {
12025 if (word[i + 1] != ws[1])
12026 continue;
12027 if (k > 2)
12028 {
12029 for (j = 2; j < k; ++j)
12030 if (word[i + j] != ws[j])
12031 break;
12032 if (j < k)
12033 continue;
12034 }
12035 }
12036
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012037 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012038 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012039 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012040 while (*pf != NUL && *pf != word[i + k])
12041 ++pf;
12042 if (*pf == NUL)
12043 continue;
12044 ++k;
12045 }
12046 s = smp[n].sm_rules;
12047 pri = 5; /* default priority */
12048
12049 p0 = *s;
12050 k0 = k;
12051 while (*s == '-' && k > 1)
12052 {
12053 k--;
12054 s++;
12055 }
12056 if (*s == '<')
12057 s++;
12058 if (VIM_ISDIGIT(*s))
12059 {
12060 /* determine priority */
12061 pri = *s - '0';
12062 s++;
12063 }
12064 if (*s == '^' && *(s + 1) == '^')
12065 s++;
12066
12067 if (*s == NUL
12068 || (*s == '^'
12069 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012070 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012071 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012072 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012073 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012074 && spell_iswordp_w(word + i - 1, curbuf)
12075 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012076 {
12077 /* search for followup rules, if: */
12078 /* followup and k > 1 and NO '-' in searchstring */
12079 c0 = word[i + k - 1];
12080 n0 = slang->sl_sal_first[c0 & 0xff];
12081
12082 if (slang->sl_followup && k > 1 && n0 >= 0
12083 && p0 != '-' && word[i + k] != NUL)
12084 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012085 /* Test follow-up rule for "word[i + k]"; loop over
12086 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012087 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
12088 == (c0 & 0xff); ++n0)
12089 {
12090 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012091 */
12092 if (c0 != ws[0])
12093 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012094 k0 = smp[n0].sm_leadlen;
12095 if (k0 > 1)
12096 {
12097 if (word[i + k] != ws[1])
12098 continue;
12099 if (k0 > 2)
12100 {
12101 pf = word + i + k + 1;
12102 for (j = 2; j < k0; ++j)
12103 if (*pf++ != ws[j])
12104 break;
12105 if (j < k0)
12106 continue;
12107 }
12108 }
12109 k0 += k - 1;
12110
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012111 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012112 {
12113 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012114 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012115 while (*pf != NUL && *pf != word[i + k0])
12116 ++pf;
12117 if (*pf == NUL)
12118 continue;
12119 ++k0;
12120 }
12121
12122 p0 = 5;
12123 s = smp[n0].sm_rules;
12124 while (*s == '-')
12125 {
12126 /* "k0" gets NOT reduced because
12127 * "if (k0 == k)" */
12128 s++;
12129 }
12130 if (*s == '<')
12131 s++;
12132 if (VIM_ISDIGIT(*s))
12133 {
12134 p0 = *s - '0';
12135 s++;
12136 }
12137
12138 if (*s == NUL
12139 /* *s == '^' cuts */
12140 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012141 && !spell_iswordp_w(word + i + k0,
12142 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012143 {
12144 if (k0 == k)
12145 /* this is just a piece of the string */
12146 continue;
12147
12148 if (p0 < pri)
12149 /* priority too low */
12150 continue;
12151 /* rule fits; stop search */
12152 break;
12153 }
12154 }
12155
12156 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
12157 == (c0 & 0xff))
12158 continue;
12159 }
12160
12161 /* replace string */
12162 ws = smp[n].sm_to_w;
12163 s = smp[n].sm_rules;
12164 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
12165 if (p0 == 1 && z == 0)
12166 {
12167 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012168 if (reslen > 0 && ws != NULL && *ws != NUL
12169 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012170 || wres[reslen - 1] == *ws))
12171 reslen--;
12172 z0 = 1;
12173 z = 1;
12174 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012175 if (ws != NULL)
12176 while (*ws != NUL && word[i + k0] != NUL)
12177 {
12178 word[i + k0] = *ws;
12179 k0++;
12180 ws++;
12181 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012182 if (k > k0)
12183 mch_memmove(word + i + k0, word + i + k,
12184 sizeof(int) * (STRLEN(word + i + k) + 1));
12185
12186 /* new "actual letter" */
12187 c = word[i];
12188 }
12189 else
12190 {
12191 /* no '<' rule used */
12192 i += k - 1;
12193 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012194 if (ws != NULL)
12195 while (*ws != NUL && ws[1] != NUL
12196 && reslen < MAXWLEN)
12197 {
12198 if (reslen == 0 || wres[reslen - 1] != *ws)
12199 wres[reslen++] = *ws;
12200 ws++;
12201 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012202 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012203 if (ws == NULL)
12204 c = NUL;
12205 else
12206 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012207 if (strstr((char *)s, "^^") != NULL)
12208 {
12209 if (c != NUL)
12210 wres[reslen++] = c;
12211 mch_memmove(word, word + i + 1,
12212 sizeof(int) * (STRLEN(word + i + 1) + 1));
12213 i = 0;
12214 z0 = 1;
12215 }
12216 }
12217 break;
12218 }
12219 }
12220 }
12221 else if (vim_iswhite(c))
12222 {
12223 c = ' ';
12224 k = 1;
12225 }
12226
12227 if (z0 == 0)
12228 {
12229 if (k && !p0 && reslen < MAXWLEN && c != NUL
12230 && (!slang->sl_collapse || reslen == 0
12231 || wres[reslen - 1] != c))
12232 /* condense only double letters */
12233 wres[reslen++] = c;
12234
12235 i++;
12236 z = 0;
12237 k = 0;
12238 }
12239 }
12240
12241 /* Convert wide characters in "wres" to a multi-byte string in "res". */
12242 l = 0;
12243 for (n = 0; n < reslen; ++n)
12244 {
12245 l += mb_char2bytes(wres[n], res + l);
12246 if (l + MB_MAXBYTES > MAXWLEN)
12247 break;
12248 }
12249 res[l] = NUL;
12250}
12251#endif
12252
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012253/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012254 * Compute a score for two sound-a-like words.
12255 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
12256 * Instead of a generic loop we write out the code. That keeps it fast by
12257 * avoiding checks that will not be possible.
12258 */
12259 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012260soundalike_score(goodstart, badstart)
12261 char_u *goodstart; /* sound-folded good word */
12262 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012263{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012264 char_u *goodsound = goodstart;
12265 char_u *badsound = badstart;
12266 int goodlen;
12267 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012268 int n;
12269 char_u *pl, *ps;
12270 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012271 int score = 0;
12272
12273 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
12274 * counted so much, vowels halfway the word aren't counted at all. */
12275 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
12276 {
12277 score = SCORE_DEL / 2;
12278 if (*badsound == '*')
12279 ++badsound;
12280 else
12281 ++goodsound;
12282 }
12283
12284 goodlen = STRLEN(goodsound);
12285 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012286
12287 /* Return quickly if the lenghts are too different to be fixed by two
12288 * changes. */
12289 n = goodlen - badlen;
12290 if (n < -2 || n > 2)
12291 return SCORE_MAXMAX;
12292
12293 if (n > 0)
12294 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012295 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012296 ps = badsound;
12297 }
12298 else
12299 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012300 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012301 ps = goodsound;
12302 }
12303
12304 /* Skip over the identical part. */
12305 while (*pl == *ps && *pl != NUL)
12306 {
12307 ++pl;
12308 ++ps;
12309 }
12310
12311 switch (n)
12312 {
12313 case -2:
12314 case 2:
12315 /*
12316 * Must delete two characters from "pl".
12317 */
12318 ++pl; /* first delete */
12319 while (*pl == *ps)
12320 {
12321 ++pl;
12322 ++ps;
12323 }
12324 /* strings must be equal after second delete */
12325 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012326 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012327
12328 /* Failed to compare. */
12329 break;
12330
12331 case -1:
12332 case 1:
12333 /*
12334 * Minimal one delete from "pl" required.
12335 */
12336
12337 /* 1: delete */
12338 pl2 = pl + 1;
12339 ps2 = ps;
12340 while (*pl2 == *ps2)
12341 {
12342 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012343 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012344 ++pl2;
12345 ++ps2;
12346 }
12347
12348 /* 2: delete then swap, then rest must be equal */
12349 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12350 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012351 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012352
12353 /* 3: delete then substitute, then the rest must be equal */
12354 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012355 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012356
12357 /* 4: first swap then delete */
12358 if (pl[0] == ps[1] && pl[1] == ps[0])
12359 {
12360 pl2 = pl + 2; /* swap, skip two chars */
12361 ps2 = ps + 2;
12362 while (*pl2 == *ps2)
12363 {
12364 ++pl2;
12365 ++ps2;
12366 }
12367 /* delete a char and then strings must be equal */
12368 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012369 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012370 }
12371
12372 /* 5: first substitute then delete */
12373 pl2 = pl + 1; /* substitute, skip one char */
12374 ps2 = ps + 1;
12375 while (*pl2 == *ps2)
12376 {
12377 ++pl2;
12378 ++ps2;
12379 }
12380 /* delete a char and then strings must be equal */
12381 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012382 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012383
12384 /* Failed to compare. */
12385 break;
12386
12387 case 0:
12388 /*
12389 * Lenghts are equal, thus changes must result in same length: An
12390 * insert is only possible in combination with a delete.
12391 * 1: check if for identical strings
12392 */
12393 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012394 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012395
12396 /* 2: swap */
12397 if (pl[0] == ps[1] && pl[1] == ps[0])
12398 {
12399 pl2 = pl + 2; /* swap, skip two chars */
12400 ps2 = ps + 2;
12401 while (*pl2 == *ps2)
12402 {
12403 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012404 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012405 ++pl2;
12406 ++ps2;
12407 }
12408 /* 3: swap and swap again */
12409 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12410 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012411 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012412
12413 /* 4: swap and substitute */
12414 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012415 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012416 }
12417
12418 /* 5: substitute */
12419 pl2 = pl + 1;
12420 ps2 = ps + 1;
12421 while (*pl2 == *ps2)
12422 {
12423 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012424 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012425 ++pl2;
12426 ++ps2;
12427 }
12428
12429 /* 6: substitute and swap */
12430 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12431 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012432 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012433
12434 /* 7: substitute and substitute */
12435 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012436 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012437
12438 /* 8: insert then delete */
12439 pl2 = pl;
12440 ps2 = ps + 1;
12441 while (*pl2 == *ps2)
12442 {
12443 ++pl2;
12444 ++ps2;
12445 }
12446 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012447 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012448
12449 /* 9: delete then insert */
12450 pl2 = pl + 1;
12451 ps2 = ps;
12452 while (*pl2 == *ps2)
12453 {
12454 ++pl2;
12455 ++ps2;
12456 }
12457 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012458 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012459
12460 /* Failed to compare. */
12461 break;
12462 }
12463
12464 return SCORE_MAXMAX;
12465}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467/*
12468 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012469 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012470 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000012471 * The algorithm is described by Du and Chang, 1992.
12472 * The implementation of the algorithm comes from Aspell editdist.cpp,
12473 * edit_distance(). It has been converted from C++ to C and modified to
12474 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012475 */
12476 static int
12477spell_edit_score(badword, goodword)
12478 char_u *badword;
12479 char_u *goodword;
12480{
12481 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012482 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012483 int j, i;
12484 int t;
12485 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012486 int pbc, pgc;
12487#ifdef FEAT_MBYTE
12488 char_u *p;
12489 int wbadword[MAXWLEN];
12490 int wgoodword[MAXWLEN];
12491
12492 if (has_mbyte)
12493 {
12494 /* Get the characters from the multi-byte strings and put them in an
12495 * int array for easy access. */
12496 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012497 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012498 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012499 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012500 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012501 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012502 }
12503 else
12504#endif
12505 {
12506 badlen = STRLEN(badword) + 1;
12507 goodlen = STRLEN(goodword) + 1;
12508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509
12510 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12511#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012512 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12513 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012514 if (cnt == NULL)
12515 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012516
12517 CNT(0, 0) = 0;
12518 for (j = 1; j <= goodlen; ++j)
12519 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12520
12521 for (i = 1; i <= badlen; ++i)
12522 {
12523 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12524 for (j = 1; j <= goodlen; ++j)
12525 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012526#ifdef FEAT_MBYTE
12527 if (has_mbyte)
12528 {
12529 bc = wbadword[i - 1];
12530 gc = wgoodword[j - 1];
12531 }
12532 else
12533#endif
12534 {
12535 bc = badword[i - 1];
12536 gc = goodword[j - 1];
12537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012538 if (bc == gc)
12539 CNT(i, j) = CNT(i - 1, j - 1);
12540 else
12541 {
12542 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012543 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12545 else
12546 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12547
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012548 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012550#ifdef FEAT_MBYTE
12551 if (has_mbyte)
12552 {
12553 pbc = wbadword[i - 2];
12554 pgc = wgoodword[j - 2];
12555 }
12556 else
12557#endif
12558 {
12559 pbc = badword[i - 2];
12560 pgc = goodword[j - 2];
12561 }
12562 if (bc == pgc && pbc == gc)
12563 {
12564 t = SCORE_SWAP + CNT(i - 2, j - 2);
12565 if (t < CNT(i, j))
12566 CNT(i, j) = t;
12567 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568 }
12569 t = SCORE_DEL + CNT(i - 1, j);
12570 if (t < CNT(i, j))
12571 CNT(i, j) = t;
12572 t = SCORE_INS + CNT(i, j - 1);
12573 if (t < CNT(i, j))
12574 CNT(i, j) = t;
12575 }
12576 }
12577 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012578
12579 i = CNT(badlen - 1, goodlen - 1);
12580 vim_free(cnt);
12581 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012582}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012583
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012584/*
12585 * ":spelldump"
12586 */
12587/*ARGSUSED*/
12588 void
12589ex_spelldump(eap)
12590 exarg_T *eap;
12591{
12592 buf_T *buf = curbuf;
12593 langp_T *lp;
12594 slang_T *slang;
12595 idx_T arridx[MAXWLEN];
12596 int curi[MAXWLEN];
12597 char_u word[MAXWLEN];
12598 int c;
12599 char_u *byts;
12600 idx_T *idxs;
12601 linenr_T lnum = 0;
12602 int round;
12603 int depth;
12604 int n;
12605 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012606 char_u *region_names = NULL; /* region names being used */
12607 int do_region = TRUE; /* dump region names and numbers */
12608 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012609 int lpi;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012610
Bram Moolenaar95529562005-08-25 21:21:38 +000012611 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012612 return;
12613
12614 /* Create a new empty buffer by splitting the window. */
12615 do_cmdline_cmd((char_u *)"new");
12616 if (!bufempty() || !buf_valid(buf))
12617 return;
12618
Bram Moolenaar7887d882005-07-01 22:33:52 +000012619 /* Find out if we can support regions: All languages must support the same
12620 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012621 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012622 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012623 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000012624 p = lp->lp_slang->sl_regions;
12625 if (p[0] != 0)
12626 {
12627 if (region_names == NULL) /* first language with regions */
12628 region_names = p;
12629 else if (STRCMP(region_names, p) != 0)
12630 {
12631 do_region = FALSE; /* region names are different */
12632 break;
12633 }
12634 }
12635 }
12636
12637 if (do_region && region_names != NULL)
12638 {
12639 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12640 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12641 }
12642 else
12643 do_region = FALSE;
12644
12645 /*
12646 * Loop over all files loaded for the entries in 'spelllang'.
12647 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012648 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012649 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012650 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012651 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012652 if (slang->sl_fbyts == NULL) /* reloading failed */
12653 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012654
12655 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12656 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12657
12658 /* round 1: case-folded tree
12659 * round 2: keep-case tree */
12660 for (round = 1; round <= 2; ++round)
12661 {
12662 if (round == 1)
12663 {
12664 byts = slang->sl_fbyts;
12665 idxs = slang->sl_fidxs;
12666 }
12667 else
12668 {
12669 byts = slang->sl_kbyts;
12670 idxs = slang->sl_kidxs;
12671 }
12672 if (byts == NULL)
12673 continue; /* array is empty */
12674
12675 depth = 0;
12676 arridx[0] = 0;
12677 curi[0] = 1;
12678 while (depth >= 0 && !got_int)
12679 {
12680 if (curi[depth] > byts[arridx[depth]])
12681 {
12682 /* Done all bytes at this node, go up one level. */
12683 --depth;
12684 line_breakcheck();
12685 }
12686 else
12687 {
12688 /* Do one more byte at this node. */
12689 n = arridx[depth] + curi[depth];
12690 ++curi[depth];
12691 c = byts[n];
12692 if (c == 0)
12693 {
12694 /* End of word, deal with the word.
12695 * Don't use keep-case words in the fold-case tree,
12696 * they will appear in the keep-case tree.
12697 * Only use the word when the region matches. */
12698 flags = (int)idxs[n];
12699 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012700 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000012701 && (do_region
12702 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012703 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012704 & lp->lp_region) != 0))
12705 {
12706 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012707 if (!do_region)
12708 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012709
12710 /* Dump the basic word if there is no prefix or
12711 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012712 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012713 if (c == 0 || curi[depth] == 2)
12714 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012715
12716 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012717 if (c != 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012718 lnum = dump_prefixes(slang, word, round,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012719 flags, lnum);
12720 }
12721 }
12722 else
12723 {
12724 /* Normal char, go one level deeper. */
12725 word[depth++] = c;
12726 arridx[depth] = idxs[n];
12727 curi[depth] = 1;
12728 }
12729 }
12730 }
12731 }
12732 }
12733
12734 /* Delete the empty line that we started with. */
12735 if (curbuf->b_ml.ml_line_count > 1)
12736 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12737
12738 redraw_later(NOT_VALID);
12739}
12740
12741/*
12742 * Dump one word: apply case modifications and append a line to the buffer.
12743 */
12744 static void
12745dump_word(word, round, flags, lnum)
12746 char_u *word;
12747 int round;
12748 int flags;
12749 linenr_T lnum;
12750{
12751 int keepcap = FALSE;
12752 char_u *p;
12753 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012754 char_u badword[MAXWLEN + 10];
12755 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012756
12757 if (round == 1 && (flags & WF_CAPMASK) != 0)
12758 {
12759 /* Need to fix case according to "flags". */
12760 make_case_word(word, cword, flags);
12761 p = cword;
12762 }
12763 else
12764 {
12765 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012766 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12767 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012768 keepcap = TRUE;
12769 }
12770
Bram Moolenaar7887d882005-07-01 22:33:52 +000012771 /* Add flags and regions after a slash. */
12772 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012773 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012774 STRCPY(badword, p);
12775 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012776 if (keepcap)
12777 STRCAT(badword, "=");
12778 if (flags & WF_BANNED)
12779 STRCAT(badword, "!");
12780 else if (flags & WF_RARE)
12781 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012782 if (flags & WF_REGION)
12783 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012784 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012785 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012786 p = badword;
12787 }
12788
12789 ml_append(lnum, p, (colnr_T)0, FALSE);
12790}
12791
12792/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012793 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12794 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012795 * Return the updated line number.
12796 */
12797 static linenr_T
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012798dump_prefixes(slang, word, round, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012799 slang_T *slang;
12800 char_u *word; /* case-folded word */
12801 int round;
12802 int flags; /* flags with prefix ID */
12803 linenr_T startlnum;
12804{
12805 idx_T arridx[MAXWLEN];
12806 int curi[MAXWLEN];
12807 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012808 char_u word_up[MAXWLEN];
12809 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012810 int c;
12811 char_u *byts;
12812 idx_T *idxs;
12813 linenr_T lnum = startlnum;
12814 int depth;
12815 int n;
12816 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012817 int i;
12818
Bram Moolenaar53805d12005-08-01 07:08:33 +000012819 /* if the word starts with a lower-case letter make the word with an
12820 * upper-case letter in word_up[]. */
12821 c = PTR2CHAR(word);
12822 if (SPELL_TOUPPER(c) != c)
12823 {
12824 onecap_copy(word, word_up, TRUE);
12825 has_word_up = TRUE;
12826 }
12827
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012828 byts = slang->sl_pbyts;
12829 idxs = slang->sl_pidxs;
12830 if (byts != NULL) /* array not is empty */
12831 {
12832 /*
12833 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012834 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012835 */
12836 depth = 0;
12837 arridx[0] = 0;
12838 curi[0] = 1;
12839 while (depth >= 0 && !got_int)
12840 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012841 n = arridx[depth];
12842 len = byts[n];
12843 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012844 {
12845 /* Done all bytes at this node, go up one level. */
12846 --depth;
12847 line_breakcheck();
12848 }
12849 else
12850 {
12851 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012852 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012853 ++curi[depth];
12854 c = byts[n];
12855 if (c == 0)
12856 {
12857 /* End of prefix, find out how many IDs there are. */
12858 for (i = 1; i < len; ++i)
12859 if (byts[n + i] != 0)
12860 break;
12861 curi[depth] += i - 1;
12862
Bram Moolenaar53805d12005-08-01 07:08:33 +000012863 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12864 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012865 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012866 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012867 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012868 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012869 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012870 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012871
12872 /* Check for prefix that matches the word when the
12873 * first letter is upper-case, but only if the prefix has
12874 * a condition. */
12875 if (has_word_up)
12876 {
12877 c = valid_word_prefix(i, n, flags, word_up, slang,
12878 TRUE);
12879 if (c != 0)
12880 {
12881 vim_strncpy(prefix + depth, word_up,
12882 MAXWLEN - depth - 1);
12883 dump_word(prefix, round,
12884 (c & WF_RAREPFX) ? (flags | WF_RARE)
12885 : flags, lnum++);
12886 }
12887 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012888 }
12889 else
12890 {
12891 /* Normal char, go one level deeper. */
12892 prefix[depth++] = c;
12893 arridx[depth] = idxs[n];
12894 curi[depth] = 1;
12895 }
12896 }
12897 }
12898 }
12899
12900 return lnum;
12901}
12902
Bram Moolenaar95529562005-08-25 21:21:38 +000012903/*
12904 * Move "p" to end of word.
12905 */
12906 char_u *
12907spell_to_word_end(start, buf)
12908 char_u *start;
12909 buf_T *buf;
12910{
12911 char_u *p = start;
12912
12913 while (*p != NUL && spell_iswordp(p, buf))
12914 mb_ptr_adv(p);
12915 return p;
12916}
12917
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012918#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012919/*
12920 * Find start of the word in front of the cursor. We don't check if it is
12921 * badly spelled, with completion we can only change the word in front of the
12922 * cursor.
12923 * Used for Insert mode completion CTRL-X ?.
12924 * Returns the column number of the word.
12925 */
12926 int
12927spell_word_start(startcol)
12928 int startcol;
12929{
12930 char_u *line;
12931 char_u *p;
12932 int col = 0;
12933
Bram Moolenaar95529562005-08-25 21:21:38 +000012934 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012935 return startcol;
12936
12937 /* Find a word character before "startcol". */
12938 line = ml_get_curline();
12939 for (p = line + startcol; p > line; )
12940 {
12941 mb_ptr_back(line, p);
12942 if (spell_iswordp_nmw(p))
12943 break;
12944 }
12945
12946 /* Go back to start of the word. */
12947 while (p > line)
12948 {
12949 col = p - line;
12950 mb_ptr_back(line, p);
12951 if (!spell_iswordp(p, curbuf))
12952 break;
12953 col = 0;
12954 }
12955
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012956 return col;
12957}
12958
12959/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000012960 * Need to check for 'spellcapcheck' now, the word is removed before
12961 * expand_spelling() is called. Therefore the ugly global variable.
12962 */
12963static int spell_expand_need_cap;
12964
12965 void
12966spell_expand_check_cap(col)
12967 colnr_T col;
12968{
12969 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12970}
12971
12972/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012973 * Get list of spelling suggestions.
12974 * Used for Insert mode completion CTRL-X ?.
12975 * Returns the number of matches. The matches are in "matchp[]", array of
12976 * allocated strings.
12977 */
12978/*ARGSUSED*/
12979 int
12980expand_spelling(lnum, col, pat, matchp)
12981 linenr_T lnum;
12982 int col;
12983 char_u *pat;
12984 char_u ***matchp;
12985{
12986 garray_T ga;
12987
12988 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12989 *matchp = ga.ga_data;
12990 return ga.ga_len;
12991}
12992#endif
12993
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012994#endif /* FEAT_SYN_HL */